Follow the Dana video to learn UC advanced programming and complete the small program exercises.
The main exercise functions are:
Int lstat (const char * path, struct stat * BUF );
Size_t strftime (char * s, size_t Max, const char * format, const struct TM * TM); this function is used for the first time.
Time_t mktime (struct TM * TM); // combines the separated time into an integer and writes it into the project code. At that time, this function was implemented by myself.
# Include <stdio. h> # include <string. h> # include <sys/types. h> # include <sys/STAT. h> # include <unistd. h> # include <time. h> # include <GRP. h> # include <PWD. h> void show01 (struct stat st) // file attributes-rwxr -- r -- {/* print file type */If (s_islnk (St. st_mode) {printf ("L");} else if (s_isreg (St. st_mode) {printf ("-");} else if (s_isdir (St. st_mode) {printf ("D");} else if (s_ischr (St. st_mode) {printf ("C");} else if (s_isblk (St. st_mode) {printf ("B");} else if (s_isfifo (St. st_mode) {printf ("F");} else if (s_issock (St. st_mode) {printf ("S");}/* all permissions of the U file */If (St. st_mode & s_irusr) {printf ("R") ;}else {printf ("-") ;}if (St. st_mode & s_iwusr) {printf ("W") ;}else {printf ("-") ;}if (St. st_mode & s_ixusr) {printf ("X") ;}else {printf ("-") ;}/ * g file group permissions */If (St. st_mode & s_irgrp) {printf ("R") ;}else {printf ("-") ;}if (St. st_mode & s_iwgrp) {printf ("W");} else {printf ("-");} If (St. st_mode & s_ixgrp) {printf ("X") ;}else {printf ("-") ;}/ * O other user permissions */If (St. st_mode & s_iroth) {printf ("R") ;}else {printf ("-") ;}if (St. st_mode & s_iwoth) {printf ("W") ;}else {printf ("-") ;}if (St. st_mode & s_ixoth) {printf ("X") ;}else {printf ("-") ;}printf ("") ;}void show02 (struct stat st) // number of hard links {printf ("% lu", St. st_nlink); printf ("");} void show03 (struct stat st) // username {struct passwd * PSD; PSD = getpwuid (St. st_uid); printf ("% s", PSD-> pw_name); printf ("");} void show04 (struct stat st) // group name {struct Group * GRP = getgrgid (St. st_gid); printf ("% s", GRP-> gr_name); printf ("");} void show05 (struct stat st) // file size {printf ("% lu", St. st_size); printf ("");} void show06 (struct stat st) // file time {char timebuf [20]; struct TM * newtime = localtime (& St. st_mtime); strftime (timebuf, 20, "% B % d % H: % m", newtime); printf ("% s", timebuf ); printf ("");} void show07 (const char * fname) // file name {printf ("% s", fname); printf ("");} int main (INT argc, const char * argv []) {int ret = 0; struct stat st; If (argc <2) {printf (". /. out file \ n "); return;} ret = lstat (argv [1], & St); If (Ret <0) perror (" lstat ()"); show01 (ST); show02 (ST); show03 (ST); show04 (ST); show05 (ST); show06 (ST); show07 (argv [1]); puts (""); // line feed return 0 ;}
The Interface Design of the function is not very reasonable. It is mainly used to practice the function.
UC advanced programming-implement myls Program