实现linux下ls -l的c源码

转载请注明出处:珍惜他人成果

下面是关于linux下文件的属性源码(自己写的,仅供参考):

1 #include<stdio.h>

  2 #include<sys/types.h>

  3 #include<sys/stat.h>

  4 #include<fcntl.h>

  5 #include<unistd.h>

  6 #include<string.h>

  7 #include<errno.h>

  8 #include<time.h>

  9 #include<pwd.h>

 10 #include<grp.h>

 11 #include<dirent.h>

 12 int main(int argc,char *argv[]){

 13         DIR *fp;

 14         struct dirent *dirent;

 15         int fd;

 16         struct stat buf;

 17         struct tm  *time_out;

 18         struct passwd *pwd;

 19         struct group *grp;

 20 

 21 

 22         if(NULL !=(fp=opendir(argv[1]))){

 23                 if(NULL == (dirent=readdir(fp))){

 24                         fprintf(stdout,"%s\n",strerror(errno));

 25                         return 0;

 26                 }//if

 27                 while(dirent != NULL){

 28 

 29         if(-1 == (fd=open(dirent->d_name,O_RDONLY))){

 30                 fprintf(stdout,"%s\n",strerror(errno));

 31                 return 0;

 32         }

 33 

 34 //将文件的一些信息先写入到buf

 35         if(fstat(fd,&buf)){

 36                 fprintf(stdout,"%s\n",strerror(errno));

 37                 return 0;

 38         }

 39 //文件属性判断

 40         if(S_ISREG(buf.st_mode)) putchar('-');

 41         else if(S_ISBLK(buf.st_mode)) putchar('b');

 42         else if(S_ISCHR(buf.st_mode)) putchar('c');

 43         else if(S_ISDIR(buf.st_mode)) putchar('d');

 44         else if(S_ISLNK(buf.st_mode)) putchar('l');

 45         else if(S_ISSOCK(buf.st_mode)) putchar('s');

 46         else if(S_ISFIFO(buf.st_mode)) putchar('p');

 47 

 48 //判断文件权限

 49 

 50         if(buf.st_mode&S_IRUSR) putchar('r');

 51         else putchar('-');

 52 

 53         if(buf.st_mode&S_IWUSR) putchar('w');

 54         else putchar('-');

 55 

 56         if(buf.st_mode&S_IXUSR) putchar('x');

 57         else putchar('-');

 58 

 59         if(buf.st_mode&S_IRGRP) putchar('r');

 60         else putchar('-');

 61         if(buf.st_mode&S_IWGRP) putchar('w');

 62         else putchar('-');

 63         if(buf.st_mode&S_IXGRP) putchar('x');

 64         else putchar('-');

 65         if(buf.st_mode&S_IROTH) putchar('r');

 66         else putchar('-');

 67         if(buf.st_mode&S_IWOTH) putchar('w');

 68         else putchar('-');

69         if(buf.st_mode&S_IXOTH) putchar('x');

 70         else putchar('-');

 71         putchar(' ');

 72 

 73         time_out=localtime(&(buf.st_ctime));

 74         //localtime(&(buf.st_ctime));

 75 

 76         fprintf(stdout,"%d ",buf.st_nlink);

 77         if(NULL != (pwd=getpwuid(buf.st_uid))){

 78                 printf("%s ",pwd->pw_name);

 79         }else printf("%d ",buf.st_uid);

 80 

 81         if(NULL != (grp=getgrgid(buf.st_gid))){

 82                 printf("%s ",grp->gr_name);

 83         }else printf("%d ",buf.st_gid);

 84         fprintf(stdout,"%ld ",buf.st_size);

 85         fprintf(stdout,"%d-%d-%d %d:%d:%d ",time_out-

>tm_year+1900,time_out-    >tm_mon+1,time_out->tm_mday,time_out-

>tm_hour,time_out->tm_min,time_out->tm_    sec);

 86         fprintf(stdout,"%s\n",dirent->d_name);

 87 

 88                 if(NULL == (dirent=readdir(fp)))

 89                         return 0;

 90                 }//while        

 91 

 92         }//if

 93         else {

 94 

 95         if(-1 == (fd=open(argv[1],O_RDONLY))){

 96                 fprintf(stdout,"%s\n",strerror(errno));

 97                 return 0;

 98         }

 99 

100 //将文件的一些信息先写入到buf

101         if(fstat(fd,&buf)){

102                 fprintf(stdout,"%s\n",strerror(errno));

103                 return 0;

104         }

105 //文件属性判断

106         if(S_ISREG(buf.st_mode)) putchar('-');

107         else if(S_ISBLK(buf.st_mode)) putchar('b');

108         else if(S_ISCHR(buf.st_mode)) putchar('c');

109         else if(S_ISDIR(buf.st_mode)) putchar('d');

110         else if(S_ISLNK(buf.st_mode)) putchar('l');

111         else if(S_ISSOCK(buf.st_mode)) putchar('s');

112         else if(S_ISFIFO(buf.st_mode)) putchar('p');

113 

114 //判断文件权限

115 

116         if(buf.st_mode&S_IRUSR) putchar('r');

117         else putchar('-');

118 

119         if(buf.st_mode&S_IWUSR) putchar('w');

120         else putchar('-');

121 

122         if(buf.st_mode&S_IXUSR) putchar('x');

123         else putchar('-');

124 

125         if(buf.st_mode&S_IRGRP) putchar('r');

126         else putchar('-');

127         if(buf.st_mode&S_IWGRP) putchar('w');

128         else putchar('-');

129         if(buf.st_mode&S_IXGRP) putchar('x');

130         else putchar('-');

131         if(buf.st_mode&S_IROTH) putchar('r');

132         else putchar('-');

133         if(buf.st_mode&S_IWOTH) putchar('w');

134         else putchar('-');

135         if(buf.st_mode&S_IXOTH) putchar('x');

136         else putchar('-');

137         putchar(' ');

138 

139         time_out=localtime(&(buf.st_ctime));

140         //localtime(&(buf.st_ctime));

141 

142         fprintf(stdout,"%d ",buf.st_nlink);

143         if(NULL != (pwd=getpwuid(buf.st_uid))){

144                 printf("%s ",pwd->pw_name);

145         }else printf("%d ",buf.st_uid);

146 

147         if(NULL != (grp=getgrgid(buf.st_gid))){

148                 printf("%s ",grp->gr_name);

149         }else printf("%d ",buf.st_gid);

150         fprintf(stdout,"%ld ",buf.st_size);

151         fprintf(stdout,"%d-%d-%d %d:%d:%d ",time_out-

>tm_year+1900,time_out-    >tm_mon+1,time_out->tm_mday,time_out-

>tm_hour,time_out->tm_min,time_out->tm_sec);

152         fprintf(stdout,"%s\n",argv[1]);

153         }//else

154 

155 

156         return 0;

157 }

     

上面的函数主要用到的函数以及知识:

                               

知识一:需要用到opendir(),readdir()这两个函数的原型如下:

DIR *opendir(const char *name);其中参数是文件名字符串;出错返回NULL;成

功返回文件描述符;如果不是目录文件返回打印出错误信息;

struct dirent *readdir(DIR *dirp);其参数是一个DIR *类型的参数,其返回值

是一个struct dirent *类型的指针;

知识二:open()(上面有描述);

知识三:需要用到stat(),fstat(),lstat();

其函数原型分别为:

int stat(const char*path,struct stat *buf);第一个参数全路径(文件);第

二个参数用于存放该文件的状态信息的结构体

int fstat(int fd,struct stat *buf);第一个参数文件描述符,第二个参数用于

存放该文件描述符指向文件的状态信息;

int lstat(const char *path,struct stat *buf);第一个参数全路径(文件);

第二个参数用于存放该文件的状态信息

关于该函数的详细描述我会在后面补充上:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值