Using System Call Programming Using System Call Using System Call Programming Implementation DIR Command

xiaoxiao2021-03-06  63

It is also a very simple applet that uses some directory operations.

#include #include #include #include #include #include

Static int GET_INFO (Const Char * filename) {struct stat statbuf; if (stat (filename, & statbuf) == - 1) {Printf ("% s / n", filename); return (1);} if (S_ISDIR) Statbuf.ST_MODE)) Printf ("% S / T Directory / TMODIFIED AT% S", FileName, CTIME (& statbuf.st_mtime)); if (S_ISREG (STATBUF.ST_MODE)) Printf ("% s / tsize:% ld Bytes) / TMODIFIED AT% S ", filename, statbuf.st_size, ctime (& statbuf.st_mtime); return (0);}

INT Main (int Argc, char ** argv) {DIR * DIRP; STRUCT DIRENT * DIRENTP;

IF ((DIRP = OpenDir (argv [1]))) {while ((DirentP = ReadDir (DIRP))! = null) get_info (DirentP-> D_NAME); / * Printf ("% s / t", DirentP- > d_name); * / closedir (DIRP); exit (1);} else {printf ("error / n"); exit (1);}}

In fact, there is still a problem, in the current directory, it is very good, but if DIR is a different path, you can't display the specific information of some files. Specifically, Stat (FileName, & statbuf) will be equal to -1 If you just want to display the directory name and file name, you can delete the get_info function, you can get the comment in the MIAN function.

Give you a nice website, IBM developer garden Linux version

Http://www-900.ibm.com/developerWorks/cn/linux/index.shtml

转载请注明原文地址:https://www.9cbs.com/read-118399.html

New Post(0)