標籤:style blog http 使用 strong 檔案
跟著達內視頻,學習UC進階編程,完成程式小練習。
主要練習的函數為:
int lstat(const char *path, struct stat *buf);
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);此函數, 第一次使用。
time_t mktime(struct tm *tm);//把分離的時間合成整數,寫項目代碼中,當時自己實現了這個函數功能。
#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)//檔案屬性-rwxr--r--{/*列印檔案類型*/ 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"); }/*U檔案所有者的許可權*/ 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檔案所有組的許可權*/ 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其它使用者的許可權*/ 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)//永久連結數{printf("%lu", st.st_nlink);printf(" ");}void show03(struct stat st)//使用者名稱{struct passwd *psd ;psd = getpwuid(st.st_uid); printf("%s", psd->pw_name);printf(" ");}void show04(struct stat st)//組名{struct group *grp = getgrgid(st.st_gid);printf("%s", grp->gr_name);printf(" ");}void show05(struct stat st)//檔案大小{printf("%lu", st.st_size);printf(" ");}void show06(struct stat st)//檔案時間{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)//檔案名稱{printf("%s", fname); printf(" ");}int main(int argc, const char *argv[]){int ret = 0;struct stat st;if(argc<2){printf("./a.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("");//換行return 0;}
函數的介面設計的方面,不是很合理,主要是練習函數的使用。