What does ls do? How does it work?<br />What does ls do? <br />Lists the contents of directories<br />Displays information...
direct structure<br />structdirent<br />{<br />ino_td_ino;       /* inode number */<br />off_td_off;        /* offset to t...
stat structure<br />struct stat {<br />dev_tst_dev;    	/* ID of device containing file */<br />ino_tst_ino;     	/* inode...
Converting File Mode to a String<br />
Flag for st_mode field<br />The following flags are defined for the st_mode field:<br />#define S_IFMT	0170000   bit mask ...
Flag for st_mode field<br />The following flags are defined for the st_mode field:<br /> #define S_ISUID    	0004000   set...
Flag for st_mode field<br />#define S_IRWXO	00007	mask for permissions for others (not in group)<br />#define S_IROTH	0000...
File Type Macros<br />The following POSIX macros are defined to check the file type using the st_mode field:<br />        ...
struct passwd<br />The passwd structure is defined in <pwd.h> as follows:<br />structpasswd {<br />               char   *...
sprintf<br />#include <stdio.h><br />main()<br />{<br />   char st[5]="test";<br />   char outString[50]="";<br />sprintf(...
Special Constant Expressions<br />Constant values can be declared explicitly as long values, in fact, by placing the lette...
Advanced programmers, writing systems software, often find it convenient to work with hexadecimal or octal numbers since t...
For example: <br />oct_value = 077; /* 77 octal */ <br />hex_value = 0xFFEF; /* FFEF hex */ <br />
Three Special Bits<br />Set-User-ID Bit<br />Example, How can a regular user change his or her password since passwd file ...
Three Special Bits<br />The SUID bit tells the kernel to run the program as though is were being run by the owner of the p...
A mask to test for the SUID Bit<br />#define S_ISUID	0004000<br />0000 100 000 000 000<br />A mask to test for the SGID Bi...
Three Special Bits<br />The Sticky bit: <br />The sticky bit tells the kernel to keep the program on the swap device even ...
Understanding file permissions on Unix: a brief tutorial <br />http://www.dartmouth.edu/~rc/help/faq/permissions.html<br /...
Upcoming SlideShare
Loading in …5
×

Chapter 3

588 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
588
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 3

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6. What does ls do? How does it work?<br />What does ls do? <br />Lists the contents of directories<br />Displays information about files<br />How does it work?<br />How to list the contents of a directory?<br />How to obtain and display properties of a file?<br />How to determine if a name refers to a file or a directory?<br />
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12. direct structure<br />structdirent<br />{<br />ino_td_ino; /* inode number */<br />off_td_off; /* offset to the next dirent */<br />unsigned short d_reclen; /* length of this record */<br />unsigned char d_type; /* type of file */<br />char d_name[256]; /* filename */<br /> };<br />
  13. 13.
  14. 14.
  15. 15.
  16. 16.
  17. 17.
  18. 18. stat structure<br />struct stat {<br />dev_tst_dev; /* ID of device containing file */<br />ino_tst_ino; /* inode number */<br />mode_tst_mode; /* protection: type and permission */<br />nlink_tst_nlink; /* number of hard links */<br />uid_tst_uid; /* user ID of owner */<br />gid_tst_gid; /* group ID of owner */<br />dev_tst_rdev; /* device ID (if special file) */<br />off_tst_size; /* total size, in bytes */<br />blksize_tst_blksize; /* blocksize for file system I/O */<br />blkcnt_tst_blocks; /* number of 512B blocks allocated */<br />time_tst_atime; /* time of last access */<br />time_tst_mtime; /* time of last modification */<br />time_tst_ctime; /* time of last status (properties) change */<br /> };<br />
  19. 19.
  20. 20. Converting File Mode to a String<br />
  21. 21.
  22. 22.
  23. 23.
  24. 24.
  25. 25. Flag for st_mode field<br />The following flags are defined for the st_mode field:<br />#define S_IFMT 0170000 bit mask for the file type bit fields<br /> #define S_IFSOCK 0140000 socket<br /> #define S_IFLNK 0120000 symbolic link<br /> #define S_IFREG 0100000 regular file<br /> #define S_IFBLK 0060000 block device<br /> #define S_IFDIR 0040000 directory<br /> #define S_IFCHR 0020000 character device<br /> #define S_IFIFO 0010000 FIFO<br />
  26. 26. Flag for st_mode field<br />The following flags are defined for the st_mode field:<br /> #define S_ISUID 0004000 set UID bit<br /> #define S_ISGID 0002000 set-group-ID bit (see below)<br /> #define S_ISVTX 0001000 sticky bit (see below)<br /> #define S_IRWXU 00700 mask for file owner permissions<br /> #define S_IRUSR 00400 owner has read permission<br /> #define S_IWUSR 00200 owner has write permission<br /> #define S_IXUSR 00100 owner has execute permission<br /> #define S_IRWXG 00070 mask for group permissions<br /> #define S_IRGRP 00040 group has read permission<br /> #define S_IWGRP 00020 group has write permission<br /> #define S_IXGRP 00010 group has execute permission<br />
  27. 27. Flag for st_mode field<br />#define S_IRWXO 00007 mask for permissions for others (not in group)<br />#define S_IROTH 00004 others have read permission<br />#define S_IWOTH 00002 others have write permission<br />#define S_IXOTH 00001 others have execute permission<br />
  28. 28. File Type Macros<br />The following POSIX macros are defined to check the file type using the st_mode field:<br /> S_ISREG(m) is it a regular file?<br /> S_ISDIR(m) directory?<br /> S_ISCHR(m) character device?<br /> S_ISBLK(m) block device?<br /> S_ISFIFO(m) FIFO (named pipe)?<br /> S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)<br /> S_ISSOCK(m) socket? (Not in POSIX.1-1996.)<br />
  29. 29.
  30. 30. struct passwd<br />The passwd structure is defined in <pwd.h> as follows:<br />structpasswd {<br /> char *pw_name; /* username */<br /> char *pw_passwd; /* user password */<br />uid_tpw_uid; /* user ID */<br />gid_tpw_gid; /* group ID */<br /> char *pw_gecos; /* real name */<br /> char *pw_dir; /* home directory */<br /> char *pw_shell; /* shell program */<br /> };<br />
  31. 31. sprintf<br />#include <stdio.h><br />main()<br />{<br /> char st[5]="test";<br /> char outString[50]="";<br />sprintf(outString, "This is a %s", st);<br />printf("%sn", outString);<br />}<br />
  32. 32.
  33. 33. Special Constant Expressions<br />Constant values can be declared explicitly as long values, in fact, by placing the letter L after the constant. <br />long int variable = 23L; <br />variable = 236526598L<br />
  34. 34. Advanced programmers, writing systems software, often find it convenient to work with hexadecimal or octal numbers since these number bases have a special relationship to binary. <br />A constant in one of these types is declared by placing either 0 (zero) or 0x in front of the appropriate value.If ddd is a value, then: <br />Octal number 0ddd<br />Hexadecimal number 0xddd<br />
  35. 35. For example: <br />oct_value = 077; /* 77 octal */ <br />hex_value = 0xFFEF; /* FFEF hex */ <br />
  36. 36. Three Special Bits<br />Set-User-ID Bit<br />Example, How can a regular user change his or her password since passwd file is owned by root?<br />-rw-r--r-- 1 rootroot 2391 2009-09-01 10:59 /etc/passwd<br />Solution:<br />Give permission to the program, not to user. The program, /usr/bin/passwd, is owned by root and has the set-user-ID bit set.<br />-rwsr-xr-x 1 root root 29328 2008-04-08 09:49 /usr/bin/passwd<br />
  37. 37. Three Special Bits<br />The SUID bit tells the kernel to run the program as though is were being run by the owner of the program.<br />This bit causes the kernel to set the effective user ID to the user ID of the owner of the program.<br />The Set-Group-ID bit: sgid<br />If a program belongs to group g and the set group ID is set, the program runs as though it were being a member of group g.<br />
  38. 38. A mask to test for the SUID Bit<br />#define S_ISUID 0004000<br />0000 100 000 000 000<br />A mask to test for the SGID Bit<br />#define S_ISGID0002000<br />0000 010 000 000 000<br />Indicates a octal number<br />suid<br />Other<br />group<br />User<br />sgid<br />Other<br />group<br />User<br />Other<br />Other<br />
  39. 39. Three Special Bits<br />The Sticky bit: <br />The sticky bit tells the kernel to keep the program on the swap device even if no one is using it.<br />The sway device is faster than hard disk because no fragments happen.<br />
  40. 40. Understanding file permissions on Unix: a brief tutorial <br />http://www.dartmouth.edu/~rc/help/faq/permissions.html<br />-rw-rw---- 2 richard staff 12040 Aug 20 1996 admin/userinfo<br />drwxr-xr-x 3 richard user 2048 May 13 09:27 public<br />s in the place where 'x' would normally go is called the set-UID or set-groupID flag.<br />

×