1 / 25

Coding ls

Coding ls. CNS 3060. This example is taken from chapter 3 in the Molay book. A directory is just a file. It contains the names of directories and files. Each entry in the file represents a directory or a file A directory is never empty. It always contains

marlee
Download Presentation

Coding ls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coding ls CNS 3060

  2. This example is taken from chapter 3 in the Molay book.

  3. A directory is just a file. It contains the names of directories and files. Each entry in the file represents a directory or a file A directory is never empty. It always contains . the name of the currrent directory .. the name of the parent directory

  4. file permissions group . = hidden file What does ls do? links file owner the file type file size (bytes) file name time last modified

  5. What’s in the man pages . . . man direct*

  6. we have discussed these functions

  7. We can find the definition of a directory entry in the dirent man pages, or in sys/dirent.h

  8. the file’s inode number the file name ...name is null terminated

  9. So ... how do we get all of the other information about the file?

  10. The stat function returns file information in a stat structure

  11. not very readable!

  12. set-group-id set-user-id type group user other u g s r w x r w x r w x sticky-bit st_mode field

  13. set-group-id set-user-id type group user other u g s r w x r w x r w x sticky-bit st_mode field You can mask these fields out, but it is easier to use macros

  14. file type macros S_ISFIFO(mode) S_ISDIR(mode) S_ISCHR(mode) S_ISBLK(mode) S_ISREG(mode)

  15. using macro to mask

  16. Permission Bits if( mode & S_IRUSR ) if (mode & S_IWUSR) if (mode & S_IXUSR) if( mode & S_IRGRP ) if (mode & S_IWGRP) if (mode & S_IXGRP) if( mode & S_IROTH ) if (mode & S_IWOTH) if (mode & S_IXOTH)

  17. Test read permission for file owner

  18. To convert the user-id to a string, you could look up the user name in the /etc/passwd file. But ... on OS/X student accounts are not kept in /etc/passwd However, there is a function getpwuid that takes a user-id as an argument and returns a struct containing the user name.

  19. defined in /usr/include/pwd.h struct passwd { char *pw_name; char *pw_passwd; __uid_t pw_uid; __gid_t pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; };

  20. /* this function converts a uid to a user name */ char *uid_to_name(uid_t uid) { return getpwuid(uid)->pw_name; }

  21. Class Exercise Write a program named fileinfo. It takes a single filename as a parameter. It prints out information on the file as shown in the following slide.

More Related