250 likes | 421 Views
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
E N D
Coding ls CNS 3060
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
file permissions group . = hidden file What does ls do? links file owner the file type file size (bytes) file name time last modified
What’s in the man pages . . . man direct*
we have discussed these functions
We can find the definition of a directory entry in the dirent man pages, or in sys/dirent.h
the file’s inode number the file name ...name is null terminated
So ... how do we get all of the other information about the file?
The stat function returns file information in a stat structure
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
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
file type macros S_ISFIFO(mode) S_ISDIR(mode) S_ISCHR(mode) S_ISBLK(mode) S_ISREG(mode)
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)
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.
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; };
/* this function converts a uid to a user name */ char *uid_to_name(uid_t uid) { return getpwuid(uid)->pw_name; }
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.