1. System calls to use
1> about Catalogs
#include <dirent.h> ①dirent structural body
struct Dirent {
ino_t D_ino; /* Inode Number * *
off_t D_off; /* not offset; NOTES */
unsigned short d_reclen; /* Length of this record * *
unsigned char d_type; /* type of file; Not supported by the all filesystem types * *
Char d_name[256]; * FileName * *
};②readdir
struct Dirent *readdir (DIR *dirp);
③opendir
DIR *opendir (const char *name);
④telldir
Long Telldir (DIR *dirp);
⑤seekdir
void Seekdir (DIR *dirp, Long Loc); ⑥rewinddir
void Rewinddir (DIR *dirp); ⑦closedir
int Closedir (DIR *dirp); 2> File Information
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> ①stat structural body
struct STAT {
dev_t St_dev; /* ID of device containing file */
ino_t St_ino; /* Inode Number * *
mode_t St_mode; * Protection * *
nlink_t St_nlink; /* Number of hard links */
uid_t St_uid; /* User ID of owner/*
gid_t St_gid; /* Group ID of owner/*
dev_t St_rdev; /* Device ID (if special file) * *
off_t st_size; /* Total size, in bytes * *
blksize_t st_blksize; /* BlockSize for filesystem I/O
blkcnt_t st_blocks; /* Number of 512B blocks allocated * *
time_t St_atime; /* Time of last access */
time_t St_mtime; /* Time of last modification * *
time_t St_ctime; /* Time of last status change * *
};②stat
int stat (const char *path, struct stat *buf); 2. The writing of LS
/* ls2.c * Purpose List contents of directory or Directories * action if no args, use. else list files in args * uses stat and pwd.h and grp.h * bug:try ls2/tmp * * #include <stdio.h> #in
Clude <sys/types.h> #include <dirent.h> #include <sys/stat.h> void Do_ls (char[]);
void Dostat (char *);
void Show_file_info (char *, struct stat *);
void Mode_to_letters (int, char []);
Char *uid_to_name (uid_t);
Char *gid_to_name (gid_t);
Main (int AC, char *av[]) {if (AC = 1) Do_ls (".");
else while (--AC) {printf ("%s:\n", *++av);
Do_ls (*AV); } void Do_ls (char dirname[]) * * List files in directory called DirName/{DIR *dir_ptr; /* The directory * * struct dirent *direntp;
/* Each entry */if ((Dir_ptr = Opendir (dirname)) = = NULL) fprintf (stderr, "Ls1:cannot open%s\n", dirname);
else {while (DIRENTP = Readdir (dir_ptr))!= NULL) Dostat (direntp->d_name);
Closedir (DIR_PTR); }} VOID Dostat (char *filename) {struct STAT info; if (stat (filename, &info) = = 1)/* Cannot stat/perror (filename);
/* Say why * * Else show info/show_file_info (filename, &info); } void Show_file_info (char *filename, struct stat *info_p)/* Display the info about ' filename '.
The info is stored in struct at *info_p/{char *uid_to_name (), *ctime (), *gid_to_name (), *filemode ();
void Mode_to_letters ();
Char modestr[11];
Mode_to_letters (Info_p->st_mode, MODESTR);
printf ("%s", MODESTR);
printf ("%4d", (int) info_p->st_nlink);
printf ("%-8s", Uid_to_name (Info_p->st_uid));
printf ("%-8s", Gid_to_name (Info_p->st_gid));
printf ("%8ld", (long) info_p->st_size);
printf ("%.12s", 4+ctime (&info_p->st_mtime));
printf ("%s\n", filename); } * * Utility functions/* * This function takes a mode value and a char array * and puts into the char array th
e file type and the* Nine letters that correspond to the bits in mode. * Note:it does not code setuid, setgid, and sticky * Codes/void mode_to_letters (int mode, char str[]) {strcpy (str, "----------"); /* Default=no perms */if (S_isdir (mode)) str[0] = ' d '; * directory? */if (S_ISCHR (mode)) str[0] = ' C '; /* Char devices */if (S_ISBLK (mode)) str[0] = ' B '; /* block Device */if (mode & S_IRUSR) str[1] = ' R ';
/* 3 bits for user */if (mode & S_IWUSR) str[2] = ' W ';
if (Mode & S_IXUSR) str[3] = ' x '; if (Mode & S_IRGRP) str[4] = ' R ';
/* 3 bits for Group */if (mode & S_IWGRP) str[5] = ' W ';
if (Mode & S_IXGRP) str[6] = ' x '; if (Mode & S_iroth) str[7] = ' R ';
/* 3 bits for the other */if (mode & S_iwoth) str[8] = ' W ';
if (Mode & S_ixoth) str[9] = ' x '; #include <pwd.h> char *uid_to_name (uid_t UID)/* Returns pointer to usernameAssociated with UID, uses GETPW () * * * struct passwd *getpwuid (), *pw_ptr;
static Char numstr[10];
if ((pw_ptr = Getpwuid (uid)) = = NULL) {sprintf (Numstr, "%d", UID);
return numstr;
else return pw_ptr->pw_name; #include <grp.h> char *gid_to_name (gid_t GID)/* Returns pointer to group number GID.
Used Getgrgid (3) */{struct Group *getgrgid (), *grp_ptr;
static Char numstr[10];
if ((grp_ptr = Getgrgid (gid)) = = NULL) {sprintf (Numstr, "%d", GID);
return numstr;
else return grp_ptr->gr_name; }