Permissions

Permissions

Post by quantiu » Tue, 26 Nov 2002 00:53:35



Hello!
How can i show the permissions of a file or a directory like the command
stat?

Example:

stat /root/tmp
......
drwx------

Thanks :-)

 
 
 

Permissions

Post by Owen Jacobso » Tue, 26 Nov 2002 02:46:13



> Hello!
> How can i show the permissions of a file or a directory like the command
> stat?

> Example:

> stat /root/tmp
> ......
> drwx------

Check the man pages on stat(2).  This is probably what you're looking for.

HTH, Owen
--
When all else fails, run.

 
 
 

Permissions

Post by Eric P. McC » Tue, 26 Nov 2002 06:38:57



> Hello!
> How can i show the permissions of a file or a directory like the
> command stat?

You could always look at the source for the `stat' program.  But here
you go anyway:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

/* do a single "rwx" for the bits in bits, using string[0] through string[2] */
void get_rwx(int bits, char *string) {
  string[0] = (bits & 4) ? 'r' : '-';
  string[1] = (bits & 2) ? 'w' : '-';
  string[2] = (bits & 1) ? 'x' : '-';

Quote:}

/* get the file type character */
void get_type(int bits, char *string) {
  switch(bits) {
  case S_IFIFO:
    *string = 'p';
    break;

  case S_IFCHR:
    *string = 'c';
    break;

  case S_IFDIR:
    *string = 'd';
    break;

  case S_IFBLK:
    *string = 'b';
    break;

  case S_IFREG:
    *string = '-';
    break;

  case S_IFLNK:
    *string = 'l';
    break;

  case S_IFSOCK:
    *string = 's';
    break;

  /* not likely to see this */
  case S_IFWHT:
    *string = 'x';
    break;

  /* and should never see this */
  default:
    *string = '?';
    break;
  }

Quote:}

int main(int argc, char **argv) {
  struct stat st;
  int i, errors = 0;
  char mode[11];

  /* complain if we're called without any filenames */
  if(argc < 2) {
    fputs("Usage: fmode FILE...\n", stderr);
    return 1;
  }

  /* ensure zero-termination */
  mode[11] = 0;

  for(i = 1; i < argc; i++) {
    if(lstat(argv[i], &st) < 0) {
      perror(argv[i]);
      errors++;
      continue;
    }

    /* general file type (block, char, etc.) */
    get_type(st.st_mode & S_IFMT, mode);

    /* file permissions */
    get_rwx((st.st_mode & S_IRWXU) >> 6, &mode[1]);
    get_rwx((st.st_mode & S_IRWXG) >> 3, &mode[4]);
    get_rwx(st.st_mode & S_IRWXO, &mode[7]);

    /* special bits: setuid, setgid, sticky */
    if(st.st_mode & S_ISUID)
      mode[3] = 's';

    if(st.st_mode & S_ISGID)
      mode[6] = 'S';

    if(st.st_mode & S_ISTXT)
      mode[9] = 't';

    /* print the original octal mode, our textual mode, and the filename */
    printf("0%o  %s  %s\n", st.st_mode, mode, argv[i]);
  }

  return errors;

Quote:}

--

"Last I checked, it wasn't the power cord for the Clue Generator that
was sticking up your ass." - John Novak, rasfwrj

 
 
 

1. Permission Woes - can't add write permission

I am trying to have Netscape use the Mail folder on one of my DOS
partitions, so as to keep all the mail in one place. This works fine for
root, but as a user Netscape can't do this because the user lacks write
permission for the file. Not surprising so far, but no matter what I
try, I can't add write permission for this set of files for "anyone" but
the owner, root. I have fooled around with groups and alternate users,
tried the graphical permission, console 'chmod', but nothing ever
changes. I never get any errors, just no result. I found a place to
change root's ability to make changes from 700 and put 777 just to see,
but even then, nothing. How can it be that root can write to these
files, but cannot bestow permission on "anyone" else?

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Two video cards for Linux Xwindows

3. Is it possible to have execute permissions without read permissions?

4. lpd filtering remote printer?

5. How to reset permissions on file with no read permissions

6. Linux harddrive repair kit available ?

7. Do group permissions always override permissions for other (both more and less restrictive)?

8. reverse zone

9. file permissions/permission execution

10. how to set file permission for a vfat partition

11. resetting permission through script.

12. NFS, NIS and file permissions

13. file permissions PLEASE HELP