今天整理了一下以前学习的知识和做的作业什么的,把以前上《UNIX环境下的高级编程》课的时候做的作业贴出来,跟大家共享一下哈。 #include<unistd.h> #include<time.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<dirent.h> #include<stdlib.h> #include<stdio.h> #include<pwd.h> #include<grp.h> int main(int argc,char *argv[]) { int i,fd; DIR *dp; struct stat statbuf; struct dirent *dirp; struct passwd *pwd; struct group *grp; struct tm *time; if(argc!=2){ printf("usage:ls directory_name"); exit(1); } if((dp=opendir(argv[1]))==NULL){ printf("can't open %s",argv[1]); exit(1); } while((dirp=readdir(dp))!=NULL){ if(stat(dirp->d_name,&statbuf)<0){ printf("%s:stat error/n",argv[i]); exit(1); } if(S_ISDIR(statbuf.st_mode)) //mode printf("d"); else printf("-"); if(statbuf.st_mode & S_IRUSR) printf("r"); else printf("-"); if(statbuf.st_mode & S_IWUSR) printf("w"); else printf("-"); if(statbuf.st_mode & S_IXUSR) printf("x"); else printf("-"); if(statbuf.st_mode & S_IRGRP) printf("r"); else printf("-"); if(statbuf.st_mode & S_IWGRP) printf("w"); else printf("-"); if(statbuf.st_mode & S_IXGRP) printf("x"); else printf("-"); if(statbuf.st_mode & S_IROTH) printf("r"); else printf("-"); if(statbuf.st_mode & S_IWOTH) printf("w"); else printf("-"); if(statbuf.st_mode & S_IXOTH) printf("x "); else printf("- "); printf("%2d ",statbuf.st_nlink); pwd=getpwuid(statbuf.st_uid); //user printf("%8s ",pwd->pw_name); grp=getgrgid(statbuf.st_gid); printf("%8s ",grp->gr_name); //group printf("%7d ",statbuf.st_size); //size time=localtime(&statbuf.st_mtime); //time printf("%02d-%02d %02d:%02d ",time->tm_mon+1,time->tm_mday,time->tm_hour,time->tm_min); printf("%-15s/n",dirp->d_name); //name } closedir(dp); exit(0); }