권한을 입력받고 그 권한의 파일을 출력하는 것

rocketdan의 이미지

사용자에게 권한을 입력받아서 (r-4, w-2, x-1)
예를들어 6 4 4 를 입력을 받았다면
현재 디렉토리에 있는 644 권한 파일들을 다 출력하려고 합니다
그래서 소스를 짜봤는데 안되네요...
all_num_u,g,o는 다 0으로 초기화 시켜줬습니다 앞에서...
이 소스로 하면 "찾는 파일이 없습니다" 가 계속 나옵니다
입력을 6 4 4 로 받으면...
ls -l 을 치면 644 권한 파일들이 많은데 무조건 "찾는 파일들이 없습니다"
아무래도 if(statbuf.st_mode & S_IWUSR) 이 문장들을 이해 못하는 것 같은데...
아니면 다른데 문제가 있는걸까요?

31 printf("권한숫자를 입력하세요 (6 4 4 ) :");
32 scanf("%d %d %d", &get_num_u, &get_num_g, &get_num_o);
33
34 while((direntp = readdir(dirp)) != NULL){
35
36 stat(direntp->d_name, &statbuf);
37
38
39 // if(statbuf.st_mode & S_IRUSR)의 의미를 사실 모른다 -_-;;
40 if(statbuf.st_mode & S_IRUSR) {
41
42 num_u[0] = 4;
43 }
44
45 if(statbuf.st_mode & S_IWUSR){
46
47 num_u[1] = 2;
48 }
49
50 if(statbuf.st_mode & S_IXUSR){
51
52 num_u[2] = 1;
53 }
54
55 if(statbuf.st_mode & S_IRGRP){
56
57 num_g[0] = 4;
58 }
59
60 if(statbuf.st_mode & S_IWGRP){
61
62 num_g[1] = 2;
63 }
64
65 if(statbuf.st_mode & S_IXGRP){
66
67 num_g[2] = 1;
68 }
69
70 if(statbuf.st_mode & S_IROTH){
71
72 num_o[0] = 4;
73 }
74
75 if(statbuf.st_mode & S_IWOTH){
76
77 num_o[1] = 2;
78 }
79
80 if(statbuf.st_mode & S_IXOTH){
81
82 num_o[2] = 1;
83 }
84
85
86 for(i=0; i<3; i++){
87
88 all_num_u = num_u[i] + all_num_u;
89 all_num_g = num_g[i] + all_num_g;
90 all_num_o = num_o[i] + all_num_o;
91 }
92
93 if((all_num_u == get_num_u) && (all_num_g == get_num_g)
94 && (all_num_o == get_num_o)){
95
96 printf("%s", direntp->d_name);
97
98 }
99 else{
100
101 printf("찾는 파일이 없습니다\n");
102 }

dude7853의 이미지

stat의 man page에 보면
S_IRUSR 00400 owner has read permission
라고 나와 있습니다.

st_mode는 각 권한을 bit로 표시한 것인데,
if(statbuf.st_mode & S_IRUSR)은 S_IRUSR 권한이 설정되어있나를 알아보는 것입니다.

올려주신 코드가 잘 이해가 안가는데 (else랑 매칭되는 if가 없네요-_-)
간단하게 현재 디렉토리에 S_IRUSR를 가진 파일들을 출력하는 코드를 짜보았습니다.
참고가 되셨으면 좋겠습니다.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
 
int main()
{
        DIR     *dirp;
        struct dirent *direntp;
        struct stat statbuf;
        if ((dirp = opendir(".")) == NULL)
                return 1;
        while((direntp = readdir(dirp)) != NULL){
                stat(direntp->d_name, &statbuf);
                if(statbuf.st_mode & S_IRUSR)
                        printf("%s has owner read permissionR\n",direntp->d_name);
        }
        closedir(dirp);
        return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.