linux ls -l:程序实现
-
#include <stdio.h>
-
#include <sys/types.h>
-
#include <dirent.h>
-
#include <stdlib.h>
-
#include <dirent.h>
-
#include <sys/stat.h>
-
#include <unistd.h>
-
#include <pwd.h>
-
#include <grp.h>
-
#include <time.h>
-
#include <string.h>
-
-
#define do_error(error) \//宏 打印错误信息
-
do{\
-
perror("error");\
-
exit(-1);\
-
}while(0)\
-
-
int main (int argc, char *argv[])
-
{
-
DIR *dirp;
-
struct dirent *direntp;
-
struct stat info;
-
struct passwd *passwd_p;
-
struct group *group_p;
-
struct tm *tm_p;
-
char fullpath[128] = {0};
-
// char str[11] = "----------";//-rwx------
-
if((dirp = opendir(argv[1])) == NULL)//打开由程序给的路径参数./a.out ./(将程序编译成a.out,等价于ls -l ./)
-
do_error(opendir);
-
while((direntp = readdir(dirp)) != NULL)//读取第一个文件,默认文件加1,读取之后指针指向下一个文件,循环遍历全部文件
-
{
-
if(direntp->d_name[0] != '.')//屏蔽隐藏文件.或..
-
{
-
sprintf(fullpath,"%s/%s",argv[1],direntp->d_name);//由于readdir操作的是当前路径,
-
//当查看其它路径文件属性时使用直接direntp->d_name会出错,这里将路径和文件名粘贴起来
-
//也可以使用strcat函数拼接。
-
if(stat(fullpath,&info) == -1)
-
perror("stat");
-
char str[11] = "----------";
-
/* if(S_ISREG(info.st_mode)) str[0] = '-';
-
if(S_ISDIR(info.st_mode)) str[0] = 'd';
-
if(S_ISCHR(info.st_mode)) str[0] = 'c';
-
if(S_ISBLK(info.st_mode)) str[0] = 'b';
-
if(S_ISFIFO(info.st_mode)) str[0] = 'f';
-
if(S_ISLNK(info.st_mode)) str[0] = 'l';
-
if(S_ISSOCK(info.st_mode)) str[0] = 's';*/
-
-
switch(info.st_mode & S_IFMT) //查看属性
-
{
-
case S_IFREG: str[0] = '-'; break;
-
case S_IFDIR: str[0] = 'd'; break;
-
case S_IFCHR: str[0] = 'c'; break;
-
case S_IFBLK: str[0] = 'b'; break;
-
case S_IFIFO: str[0] = 'f'; break;
-
case S_IFLNK: str[0] = 'l'; break;
-
case S_IFSOCK: str[0] = 's'; break;
-
}
-
-
if(info.st_mode & S_IRUSR) str[1] = 'r';
-
if(info.st_mode & S_IWUSR) str[2] = 'w';
-
if(info.st_mode & S_IXUSR) str[3] = 'x';
-
-
if(info.st_mode & S_IRGRP) str[4] = 'r';
-
if(info.st_mode & S_IWGRP) str[5] = 'w';
-
if(info.st_mode & S_IXGRP) str[6] = 'x';
-
-
if(info.st_mode & S_IROTH) str[7] = 'r';
-
if(info.st_mode & S_IWOTH) str[8] = 'w';
-
if(info.st_mode & S_IXOTH) str[9] = 'x';
-
-
passwd_p = getpwuid(info.st_uid);
-
group_p = getgrgid(info.st_gid);
-
-
tm_p = localtime(&info.st_mtime); //获取时间属性,文章末尾附上时间函数的使用
-
-
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);
-
}
-
}
-
-
closedir(dirp);
-
return 0;
-
}
************************************************
二: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
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <time.h>
-
-
int main (int argc, char *argv[])
-
{
-
int count = 1;
-
time_t t;
-
struct tm *tm;
-
char buff[100];
-
FILE *fp;
-
if((fp = fopen(argv[1],"a+")) == NULL)
-
perror("fopen");
-
while(fgets(buff, 100, fp) != NULL)
-
{
-
int len=strlen(buff);//这里可以直接写为count++;有一点小bug
-
if(buff[len-1]=='\n')
-
count++;
-
}
-
while(1)
-
{
-
t = time(NULL);//t=time((time_t *)NULL);t是秒数计数开始点为1900.0100
-
tm = localtime(&t);//
-
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);
-
fflush(fp);
-
sleep(1);
-
}
-
return 0;
-
}
阅读(2892) | 评论(0) | 转发(0) |