Linux检查文件与目录相关特点(相似ls指令)程序的C代码汇总(代码为转帖)sohu - 威尼斯人

Linux检查文件与目录相关特点(相似ls指令)程序的C代码汇总(代码为转帖)sohu

2019-02-12 11:29:51 | 作者: 小蕊 | 标签: 文件,目录,代码 | 浏览: 1229

[转] 写道 #include fcntl.h

#include sys/file.h

#include stdlib.h

#include stdio.h

#include unistd.h

#include sys/types.h

#include sys/stat.h

#include dirent.h

#include time.h

#include errno.h



static int get_file_size_time(const char *filename)
{
struct stat statbuf;
if(stat(filename, statbuf)-1)
{
printf("Get stat on %s Error:%s\n",
filename,strerror(errno));
return(-1);
}
if(S_ISDIR(statbuf.st_mode))return(1);
if(S_ISREG(statbuf.st_mode))
printf("%s size:%ld bytes\tmodified at %s",filename,statbuf.st_size,ctime( statbuf.st_mtime));
return(0);
}
int main(int argc,char **argv)
{
DIR *dirp;
struct dirent *direntp;
int stats;
if(argc!=2)
{
printf("Usage:%s filename\n\a",argv[0]);
exit(1);
}
if(((stats=get_file_size_time(argv[1]))0)||(stats-1))exit(1);
if((dirp=opendir(argv[1]))NULL)
{
printf("Open Directory %s Error:%s\n",
argv[1],strerror(errno));
exit(1);
}
while((direntp=readdir(dirp))!=NULL)
if(get_file_size_time(direntp- d_name)-1)
break;
closedir(dirp);
exit(1);
}


 

这段代码用来显现指定目录文件的巨细与修正日期。

 [转自别人文件] 写道 #include "stdio.h"
#include "string.h"
#include "time.h"
#include "unistd.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "pwd.h"
#include "grp.h"
#include "dirent.h"
#define NAME_SIZE 100
typedef struct item
{
char name[NAME_SIZE];
int length;
struct stat st;
struct item *next;
}nNode;

#define MARK_FILE(value,flags) ((flags) = (((value) ALL_FILE)|(flags)))
#define DEL_FILE(value,flags) ((flags) = ((~(value)) (flags)))
#define MARK_INFO(value,flags) ((flags) = (((value) STAT_ALL_INFO)|(flags)))
#define DEL_INFO(value,flags) ((flags) = ((~(value)) (flags)))
#define MARK(value,flags) ((flags) = ((value)|(flags)))
#define DEL(value,flags) ((flags) = ((~(value)) (flags)))

#define FILE_TYPE 0xf8000000
#define OTH_FILE 0x80000000
#define DIR_FILE 0x40000000
#define REG_FILE 0x20000000
#define BAK_FILE 0x10000000
#define DOT_FILE 0x08000000
#define ALL_FILE 0xf8000000

#define STAT_ALL_INFO 0x07f00000
#define STAT_GROUP 0x04000000
#define STAT_OWNER 0x02000000
#define STAT_NUMID 0x01000000
#define STAT_SIZE 0x00800000 //0 present bytes 1 present k or m
#define STAT_INODE 0x00400000
#define STAT_TIME 0x00200000
#define STAT_PERMISSION 0x00100000

#define STAT_COLOR 0x00080000
#define STAT_RECUR 0x00040000
#define STAT_HR 0x00020000

void AddnNode(nNode **head,char *name,struct stat *st);
void freeNode(nNode *head );
void do_ls(char *filename,int flags,nNode **head);
void showitem(char *name ,struct stat *st,int flags);
void showfile(nNode *head,int flags);
void quitsort(char **array,int high,int low);


void format_permission(struct stat *st)//显现权限
{

int mode = st- st_mode;
char buf[11];
memset(buf,-,10);
buf[10]=0;

if(S_ISDIR(mode)) buf[0] = d;
else if(S_ISCHR(mode)) buf[0] = c;
else if(S_ISBLK(mode)) buf[0] = b;
else if(S_ISFIFO(mode)) buf[0] = p;
else if(S_ISLNK(mode)) buf[0] = l;
else if(S_ISSOCK(mode)) buf[0] = s;

if(S_IRUSR mode) buf[1] = r;
if(S_IWUSR mode) buf[2] = w;
if(S_IXUSR mode) buf[3] = x;

if(S_IRGRP mode) buf[4] = r;
if(S_IWGRP mode) buf[5] = w;
if(S_IXGRP mode) buf[6] = x;

if(S_IROTH mode) buf[7] = r;
if(S_IWOTH mode) buf[8] = w;
if(S_IXOTH mode) buf[9] = x;

printf("%s ",buf);
}

void format_group(struct stat *st)
{
int id = st- st_gid;
struct group *buf = getgrgid(id);
if(buf)
{
printf("%8s",buf- gr_name);//显现组名
}else
{
printf("%d",id);
}
}

void format_owner(struct stat *st)
{
int id = st- st_uid;
struct passwd *buf_pw = getpwuid(id);
if(buf_pw)
{
printf("%8s",buf_pw- pw_name);//显现用户名
}else
{
printf("%d",id);
}
}

void format_time(struct stat *st)
{
char *buf= ctime( (st- st_ctime)); //最近一次被更改的时刻
buf +=4;
buf[strlen(buf)-1]=0;
printf("%20s ",buf);
}

void format_numid(struct stat *st)
{
printf("%-8s, %-8s",st- st_uid,st- st_gid);
}


int main(int argc,char **argv)
{
int flags =REG_FILE|DIR_FILE|BAK_FILE|OTH_FILE;
int *file_index = (int *)malloc(sizeof(int)*argc);
int files= 0;
int i=0;
for(i=1;i argc;i++)
{

if(!strcmp("-l",argv[i]))
{
MARK_FILE(ALL_FILE,flags);
MARK_INFO(STAT_ALL_INFO,flags);
DEL(STAT_HR,flags);
DEL(STAT_NUMID,flags);
}else if(!strcmp("-L",argv[i]))
{
MARK_FILE(ALL_FILE,flags);
MARK_INFO(STAT_ALL_INFO,flags);
//DEL(STAT_HR,flags);
//DEL(STAT_NUMID,flags);
}
else
{
file_index[files++] = i;
}
}

if(files0)
{
char path[1024];
nNode *head=NULL;
if(getcwd(path,1024)NULL)
return -1;
printf("%s : \n",path);
do_ls(path,flags, head);
showfile(head,flags);
}
else
{
for(i=0;i files;i++)
{
nNode *head=NULL;
char path[1024];
do_ls(argv[file_index[i]],flags, head);
if(getcwd(path,1024)NULL)
return -1;
printf("%s : \n",path);
showfile(head,flags);
freeNode(head);
printf("\n");
}
}
return 0;
}

int ListLength(nNode *head)
{
int length=0;
while(head)
{
length++;
head = head- next;
}
return length;
}

void display(nNode *array[],int length,int flags)
{
int i=0;
if(flags STAT_ALL_INFO)
{
for(i=0; i length;i++)
{
showitem(array[i]- name, (array[i]- st),flags);
}
printf("\n\n");
}
else
{
int columns = 97;
int maxcolumns = 10;
int columnpos[10]={0},col=10,j;

for(i = maxcolumns ;i i -=1 )
{
col = i;
for(j = 0;(j i) (j length);j++)
{
int max=0,k=j;
while(k length)
{
if(array[k]- length array[max]- length)
max = k;
k = k + i;
}
columnpos[j] = array[max]- length +3;
}
int z;
for(z=1;z ++z )
{
columnpos[z] +=columnpos[z-1];
}
if(columnpos[z-1] = columns)
break;
}
char line[97];
for(i=0;i length;i++)
{
if( (i % col) 0 )
{
if(i!=0)
{
printf("%s",line);
printf("\n");
}
memset(line, ,96);
line[96]=0;
strncpy(line,array[i]- name,strlen(array[i]- name));
}
else
{
int pos = columnpos[ (i%col)-1 ];
strncpy(line+pos,array[i]- name,strlen(array[i]- name));
}
}
printf("%s\n",line);
}
}

void showfile(nNode *head,int flags)
{
int length = ListLength(head);
int i=0;
nNode **array = (nNode *)malloc(sizeof(nNode *)*length);
if(!array)
{
perror("array memory error");
return ;
}
for(i=0;i length;i++)
{
array[i] = head;
head = head- next;
}
quitsort((char **)array,length -1 , 0 );
display(array,length,flags);
}

void showitem(char *name ,struct stat *st,int flags)
{
if( (name[strlen(name)-1]~) (!(BAK_FILE flags)))
return ;
if( (name[0].) (!(DOT_FILE flags)))
return;

if( S_ISDIR(st- st_mode) (!(DIR_FILE flags)))
return;
if( S_ISREG(st- st_mode) (!(REG_FILE flags)))
return;

if(flags STAT_ALL_INFO)
{
format_permission(st); //文件的类型和存取的权限
printf("%4d ",st- st_nlink); //连到该文件的硬衔接数目
}
if(flags STAT_OWNER)
format_owner(st); //依据文件所有者的用户识别码,得出用户名
if(flags STAT_GROUP)
format_group(st); //依据文件所有者的组识别码,得出组名
if(flags STAT_NUMID)
format_numid(st); //文件所有者的用户识别码,组识别码

if(flags STAT_SIZE)
printf("%8ld ",st- st_size); //文件巨细,以字节核算
else if(flags STAT_HR)
printf("%-dk ",st- st_size/1024 +1);

if(flags STAT_TIME)
format_time(st); //最近一次被更改的时刻
printf("%s ",name); //文件名
if(flags (STAT_ALL_INFO))
printf("\n");
}

void AddnNode(nNode **head,char *name,struct stat *st)
{
nNode *p = (nNode *)malloc(sizeof(nNode));
if(!p)
perror("malloc memory error");
p- next = *head;
*head = p;
(*head) - length = strlen(name);
strcpy((*head)- name,name);
memcpy( ((*head)- st),st,sizeof(struct stat));
}

void freeNode(nNode *head )
{
nNode *p=head;
while(p)
{
p = head- next;
free(head);
head = p;
}
}

void swap(char **array,int i,int j)
{
char *tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}

void quitsort(char **array,int high,int low)//快速排序算法

{
int l=low,h=high;
char *tmp = array[low];
while(l h)
{
while(l h)
{
if( strcmp(array[l], tmp ) 0 )
{
swap(array,l,h);
h;
break;
}
++l;
}
while(l h)
{
if(strcmp(array[h], tmp) 0 )
{
swap(array,l,h);
++l;
break;
}
h;
}
}
if(strcmp(array[l], tmp) 0 )
{
l;
}
if(low high)
swap(array,l,low);
if(low l)
quitsort(array,l-1,low);
if(l high)
quitsort(array,high,l+1);
}


void do_ls(char *filename,int flags,nNode **head)
{
DIR *dir;
char parent_dir[100];
struct dirent *direntp;
struct stat st;

if((dir = opendir(filename))NULL)
{
perror("open dir error");
goto OPENDIR_ERROR;
}
if(getcwd(parent_dir,100)NULL) //保存当时目录绝对路径到parent_dir
{
perror("get parent dir error");
goto GETCURDIR_ERROR;
}
if(-1 chdir(filename)) //进入到 filename 所指定的目录下
{
perror("change dir error");
goto CHDIR_ERROR;
}

while( (direntp = readdir(dir)) != NULL)
{
if(-1 lstat(direntp- d_name, st)) //lstat函数中的第一个参数为文件的绝对路径
continue;
if((!(flags STAT_ALL_INFO)) (direntp- d_name[0] .) )
continue;
AddnNode(head,direntp- d_name, st);
if(S_ISDIR(st.st_mode) (flags STAT_RECUR) strcmp(".",direntp- d_name) strcmp( direntp- d_name,".."))
{
nNode *newhead=NULL;
do_ls(direntp- d_name,flags, newhead);
printf("%s :\n",direntp- d_name);
showfile(newhead,flags);
freeNode(newhead);
}
}

if(-1 chdir(parent_dir)) //回到本来的目录下
{
perror("go back to parent error");
goto CHDIR_ERROR;
}

CHDIR_ERROR:
GETCURDIR_ERROR:
closedir(dir);
OPENDIR_ERROR:
;
}

 

 

上面这段代码比较底层,自己看不太懂,发在这儿我们一同学习。(类ls)

 

 

[转帖自别人博客] 写道 #include stdio.h
#include stdlib.h
#include string.h
#include unistd.h
#include dirent.h
#include pwd.h
#include grp.h
#include time.h
#include sys/types.h
#include sys/stat.h

// 转化时刻显现格局
char * ptime(time_t tv)
{
static char t[64];
struct tm tmv;

strftime(t, 20, "%Y/%m/%d %T", localtime( tv));
return t;
}

int main(int argc, char *argv[])
{
char szMode[16]; // 权限信息
char szUser[64]; // 用户称号
char szGrp[64]; // 组称号
char szTime[64]; // 文件时刻

DIR *dp;

struct dirent *dirp;
struct stat st;

struct passwd *pw;
struct group *pg;

if( argc != 2 )
{
printf("usage: %s directory_name \n", argv[0]);
exit(0);
}

/* 翻开目录 */
if( (dp = opendir(argv[1])) NULL )
{
perror(argv[1]);
exit(1);
}

if( chdir(argv[1]) 0 )
{
perror("chdir");
exit(1);
}

/* 逐个读取目录条目 */
while( (dirp = readdir(dp)) != NULL )
{
if( lstat(dirp- d_name, st) 0 )
{
perror("lstat");
exit(0);
}

// 初始化权限信息
strcpy(szMode, "-");

/* 判别文件类型 */
if( S_ISDIR(st.st_mode) )
szMode[0] = d;
else if( S_ISLNK(st.st_mode) )
szMode[0] = l;

if( st.st_mode S_IRUSR )
szMode[1] = r;
if( st.st_mode S_IWUSR )
szMode[2] = w;
if( st.st_mode S_IXUSR )
szMode[3] = x;
if( st.st_mode S_IRGRP )
szMode[4] = r;
if( st.st_mode S_IWGRP )
szMode[5] = w;
if( st.st_mode S_IXGRP )
szMode[6] = x;
if( st.st_mode S_IROTH )
szMode[7] = r;
if( st.st_mode S_IWOTH )
szMode[8] = w;
if( st.st_mode S_IXOTH )
szMode[9] = x;

// 获取用户称号
if( (pw = getpwuid(st.st_uid)) NULL )
sprintf(szUser, "%d", st.st_uid);
else
strcpy(szUser, pw- pw_name);

// 获取组称号
if( (pg = getgrgid(st.st_gid)) NULL )
sprintf(szGrp, "%d", st.st_gid);
else
strcpy(szGrp, pg- gr_name);

strcpy(szTime, ptime(st.st_mtime));
printf("%s %4d %16s %16s %16ld %s %s\n", szMode, st.st_nlink, szUser, szGrp, (long)(st.st_size), szTime, dirp- d_name);
}

/* 封闭目录 */
closedir(dp);
exit(0);
}

 

 

 

这段要比一上段简单了解得多。(类ls)

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表威尼斯人立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章