Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182827
  • 博文数量: 54
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2018
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-31 23:14
文章存档

2014年(2)

2013年(52)

分类: 嵌入式

2013-08-21 20:24:36

linux ls -l:程序实现

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <pwd.h>
  9. #include <grp.h>
  10. #include <time.h>
  11. #include <string.h>

  12. #define do_error(error) \//宏 打印错误信息
  13.     do{\
  14.         perror("error");\
  15.         exit(-1);\
  16.     }while(0)\

  17. int main (int argc, char *argv[])
  18. {
  19.     DIR *dirp;
  20.     struct dirent *direntp;
  21.     struct stat info;
  22.     struct passwd *passwd_p;
  23.     struct group *group_p;
  24.     struct tm *tm_p;
  25.     char fullpath[128] = {0};
  26. //    char str[11] = "----------";//-rwx------
  27.     if((dirp = opendir(argv[1])) == NULL)//打开由程序给的路径参数./a.out ./(将程序编译成a.out,等价于ls -l ./)
  28.         do_error(opendir);
  29.     while((direntp = readdir(dirp)) != NULL)//读取第一个文件,默认文件加1,读取之后指针指向下一个文件,循环遍历全部文件
  30.     {
  31.         if(direntp->d_name[0] != '.')//屏蔽隐藏文件.或..
  32.         {
  33.             sprintf(fullpath,"%s/%s",argv[1],direntp->d_name);//由于readdir操作的是当前路径,
  34.                                                               //当查看其它路径文件属性时使用直接direntp->d_name会出错,这里将路径和文件名粘贴起来
  35.                                                               //也可以使用strcat函数拼接。
  36.             if(stat(fullpath,&info) == -1)
  37.                 perror("stat");
  38.             char str[11] = "----------";
  39. /*            if(S_ISREG(info.st_mode)) str[0] = '-';
  40.             if(S_ISDIR(info.st_mode)) str[0] = 'd';
  41.             if(S_ISCHR(info.st_mode)) str[0] = 'c';
  42.             if(S_ISBLK(info.st_mode)) str[0] = 'b';
  43.             if(S_ISFIFO(info.st_mode)) str[0] = 'f';
  44.             if(S_ISLNK(info.st_mode)) str[0] = 'l';
  45.             if(S_ISSOCK(info.st_mode)) str[0] = 's';*/
  46.             
  47.             switch(info.st_mode & S_IFMT)               //查看属性
  48.             {
  49.                 case S_IFREG: str[0] = '-'; break;
  50.                 case S_IFDIR: str[0] = 'd'; break;
  51.                 case S_IFCHR: str[0] = 'c'; break;
  52.                 case S_IFBLK: str[0] = 'b'; break;
  53.                 case S_IFIFO: str[0] = 'f'; break;
  54.                 case S_IFLNK: str[0] = 'l'; break;
  55.                 case S_IFSOCK: str[0] = 's'; break;
  56.             }
  57.             
  58.             if(info.st_mode & S_IRUSR) str[1] = 'r';
  59.             if(info.st_mode & S_IWUSR) str[2] = 'w';
  60.             if(info.st_mode & S_IXUSR) str[3] = 'x';    

  61.             if(info.st_mode & S_IRGRP) str[4] = 'r';
  62.             if(info.st_mode & S_IWGRP) str[5] = 'w';
  63.             if(info.st_mode & S_IXGRP) str[6] = 'x';
  64.                 
  65.             if(info.st_mode & S_IROTH) str[7] = 'r';
  66.             if(info.st_mode & S_IWOTH) str[8] = 'w';
  67.             if(info.st_mode & S_IXOTH) str[9] = 'x';
  68.             
  69.             passwd_p = getpwuid(info.st_uid);
  70.             group_p = getgrgid(info.st_gid);
  71.             
  72.             tm_p = localtime(&info.st_mtime);    //获取时间属性,文章末尾附上时间函数的使用
  73.             
  74.             printf("%s %d %s %s %6ld %d-%02d-%02d %02d:%02d:%02d %s\n",str,info.st_nlink,passwd_p->pw_name, group_p->gr_name, info.st_size, tm_p->tm_year+1900, tm_p->tm_mon+1, tm_p->tm_mday, tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec, direntp->d_name);    
  75.         }
  76.     }    
  77.     
  78.     closedir(dirp);
  79.     return 0;
  80. }
************************************************
二:time localtime
    每隔一秒向文件传入一个时间,程序再次运行,时间接上次序号,如:
  1,2007-30 15:16:42
  2,2007-30 15:16:43
  3,2007-30 15:16:44
  4,2007-30 15:20:12
  5,2007-30 15:20:13
    

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <time.h>

  4. int main (int argc, char *argv[])
  5. {
  6.     int count = 1;
  7.     time_t t;
  8.     struct tm *tm;
  9.     char buff[100];
  10.     FILE *fp;
  11.     if((fp = fopen(argv[1],"a+")) == NULL)
  12.         perror("fopen");
  13.     while(fgets(buff, 100, fp) != NULL)
  14.     {
  15.         int len=strlen(buff);//这里可以直接写为count++;有一点小bug
  16.         if(buff[len-1]=='\n')
  17.             count++;
  18.     }
  19.     while(1)
  20.     {
  21.         t = time(NULL);//t=time((time_t *)NULL);t是秒数计数开始点为1900.0100
  22.         tm = localtime(&t);//
  23.         fprintf(fp, "%d,%d-%d-%d %d:%d:%d\n", count++, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  24.         fflush(fp);
  25.         sleep(1);
  26.     }
  27.     return 0;
  28. }
阅读(2839) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~