日期:2014-05-16  浏览次数:20872 次

Linux查看文件与目录相关属性(类似ls命令)程序的C代码汇总(代码为转帖)
[转] 写道
#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) bu