Thread: displaying file permissions

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    displaying file permissions

    I am having some troubles in displaying the file permissions for a file!!! ...At the moment, all it does is display the integer format for file permissions, as all i am doing is:

    Code:
    printf("%d", fileattrib.st_mode);
    which outputs the following (along with a few other lines of output code)

    Code:
    list of contents for: cwd
    33261   1 root     root         15343 Fri Nov 25 11:34:48 2005 myls
    33188   1 root     root          7735 Fri Nov 25 11:34:43 2005 myls.c
    33188   1 root     root          7131 Fri Nov 25 10:15:49 2005 myls.c~
    16877   2 root     root          4096 Fri Nov 25 11:31:09 2005 test1
    I need to find out how to format it so that it outputs the following for the file permissions: (output from the propper ls command using -l switch)

    Code:
    total 36
    -rwxr-xr-x    1 root     root        15343 Nov 25 11:34 myls
    -rw-r--r--    1 root     root         7735 Nov 25 11:34 myls.c
    -rw-r--r--    1 root     root         7131 Nov 25 10:15 myls.c~
    drwxr-xr-x    2 root     root         4096 Nov 25 11:31 test1
    Anyone know how to do this??? ...i have looked around on the net, some people say to use sperm, but this wont compile, so i am stuck!!!! Cheers, matt.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Take the value of "myls" permission, it's 33261. Now convert it to octal, the value is 100755.
    It's a lot easier to understand that way. Hint:
    Code:
    7 = 111 in binary - rwx
    5 = 101 in binary - r-x
    5 = 101 in binary - r-x

  3. #3
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    ah ha!!!! thankyou very very very much!!!! ...now i understand how it becomes 755!!!! cheers

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Found another way that works...

    Code:
    struct stat fileattrib;
    int fileMode;       
    
            /* print a leading dash as start of file/directory permissions */
            printf("-");
            fileMode = fileattrib.st_mode;
            /* Check owner permissions */
            if ((fileMode & S_IRUSR) && (fileMode & S_IREAD))
              printf("r");
            else
              printf("-");
            if ((fileMode & S_IWUSR) && (fileMode & S_IWRITE)) 
              printf("w");
            else
              printf("-");
            if ((fileMode & S_IXUSR) && (fileMode & S_IEXEC))
              printf("x");
            else
              printf("-");
            /* Check group permissions */
            if ((fileMode & S_IRGRP) && (fileMode & S_IREAD))
              printf("r");
            else
              printf("-");
            if ((fileMode & S_IWGRP) && (fileMode & S_IWRITE))
              printf("w");
            else
              printf("-");
            if ((fileMode & S_IXGRP) && (fileMode & S_IEXEC))
              printf("x");
            else
              printf("-");
            /* check other user permissions */
            if ((fileMode & S_IROTH) && (fileMode & S_IREAD))
              printf("r");
            else
              printf("-");
            if ((fileMode & S_IWOTH) && (fileMode & S_IWRITE))
              printf("w");
            else
              printf("-");
            if ((fileMode & S_IXOTH) && (fileMode & S_IEXEC))
              /* because this is the last permission, leave 3 blank spaces after print */
              printf("x   ");
    DONE! I will stick to this because its my own code, and it works well enough for me!!!!!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You probably want an else on the last if.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    LOL ...Just missed out the copy bit of the last:

    Code:
    else
      printf("-   );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM