linux下,编写一个c语言程序实现...(详细见正文)!急!200分悬赏!
编写一个程序,实现判断指定目录下各种文件的存取权限,文件大小以及文件的索引节点号。这是题目,没有具体要求,可自由发挥。谁能给讲讲我该怎么编,最好是能提供详细代码!急...
编写一个程序,实现判断指定目录下各种文件的存取权限,文件大小以及文件的索引节点号。
这是题目,没有具体要求,可自由发挥。
谁能给讲讲我该怎么编,最好是能提供详细代码!急 展开
这是题目,没有具体要求,可自由发挥。
谁能给讲讲我该怎么编,最好是能提供详细代码!急 展开
展开全部
参考ln -l命令的输出结果,编写了以下程序(即输出结果和ls -l命令的输出结果相似),通过命令行传入要查看的目录,如果没有传入参数,则显出当前目录:
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <time.h>
char *getmod(mode_t mode,char *line) /*生成权限描述字符串*/
{
memset(line,0,sizeof(char)*11);
strcat(line,S_ISDIR(mode)?"d":"-");
strcat(line,(mode&S_IRWXU)&S_IRUSR?"r":"-");
strcat(line,(mode&S_IRWXU)&S_IWUSR?"w":"-");
strcat(line,(mode&S_IRWXU)&S_IXUSR?"x":"-");
strcat(line,(mode&S_IRWXG)&S_IRGRP?"r":"-");
strcat(line,(mode&S_IRWXG)&S_IWGRP?"w":"-");
strcat(line,(mode&S_IRWXG)&S_IXGRP?"x":"-");
strcat(line,(mode&S_IRWXO)&S_IROTH?"r":"-");
strcat(line,(mode&S_IRWXO)&S_IWOTH?"w":"-");
strcat(line,(mode&S_IRWXO)&S_IXOTH?"x":"-");
return line;
}
char *directory(char *argv) /*从程序参数取出目录*/
{
int i;
for (i=strlen(argv)-1;i;--i)
if (argv[i]=='/'){
argv[i+1]='\0';
break;
}
return argv;
}
int main(int argc,char *argv[])
{
DIR *dirp;
struct dirent *dirst;
struct stat finfo;
char *path,fname[512],mod[11],ctm[10];
struct passwd *user=NULL;
struct tm *ltm;
if (argc==1) path=directory(argv[0]);
else path=argv[1];
dirp=opendir(path);
if (!dirp)
{
fprintf(stderr,"ERROR\n");
exit(-1);
}
for (dirst=readdir(dirp);dirst;dirst=readdir(dirp))
{
strcpy(fname,path);
lstat(strcat(strcat(fname,"/"),dirst->d_name),&finfo);
user=getpwuid(finfo.st_uid);
printf("%s\t%10s\t",getmod(finfo.st_mode,mod),user->pw_name);
printf("%10d\t%9d\t",finfo.st_ino,finfo.st_size);
ltm=localtime(&finfo.st_mtime);
strftime(ctm,9,"%b",ltm);
printf("%5s",ctm);
strftime(ctm,9,"%d",ltm);
printf("%3s",ctm);
strftime(ctm,9,"%Y",ltm);
printf("%5s\t",ctm);
printf("%s\n",dirst->d_name);
}
closedir(dirp);
return 0;
}
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <time.h>
char *getmod(mode_t mode,char *line) /*生成权限描述字符串*/
{
memset(line,0,sizeof(char)*11);
strcat(line,S_ISDIR(mode)?"d":"-");
strcat(line,(mode&S_IRWXU)&S_IRUSR?"r":"-");
strcat(line,(mode&S_IRWXU)&S_IWUSR?"w":"-");
strcat(line,(mode&S_IRWXU)&S_IXUSR?"x":"-");
strcat(line,(mode&S_IRWXG)&S_IRGRP?"r":"-");
strcat(line,(mode&S_IRWXG)&S_IWGRP?"w":"-");
strcat(line,(mode&S_IRWXG)&S_IXGRP?"x":"-");
strcat(line,(mode&S_IRWXO)&S_IROTH?"r":"-");
strcat(line,(mode&S_IRWXO)&S_IWOTH?"w":"-");
strcat(line,(mode&S_IRWXO)&S_IXOTH?"x":"-");
return line;
}
char *directory(char *argv) /*从程序参数取出目录*/
{
int i;
for (i=strlen(argv)-1;i;--i)
if (argv[i]=='/'){
argv[i+1]='\0';
break;
}
return argv;
}
int main(int argc,char *argv[])
{
DIR *dirp;
struct dirent *dirst;
struct stat finfo;
char *path,fname[512],mod[11],ctm[10];
struct passwd *user=NULL;
struct tm *ltm;
if (argc==1) path=directory(argv[0]);
else path=argv[1];
dirp=opendir(path);
if (!dirp)
{
fprintf(stderr,"ERROR\n");
exit(-1);
}
for (dirst=readdir(dirp);dirst;dirst=readdir(dirp))
{
strcpy(fname,path);
lstat(strcat(strcat(fname,"/"),dirst->d_name),&finfo);
user=getpwuid(finfo.st_uid);
printf("%s\t%10s\t",getmod(finfo.st_mode,mod),user->pw_name);
printf("%10d\t%9d\t",finfo.st_ino,finfo.st_size);
ltm=localtime(&finfo.st_mtime);
strftime(ctm,9,"%b",ltm);
printf("%5s",ctm);
strftime(ctm,9,"%d",ltm);
printf("%3s",ctm);
strftime(ctm,9,"%Y",ltm);
printf("%5s\t",ctm);
printf("%s\n",dirst->d_name);
}
closedir(dirp);
return 0;
}
展开全部
最简单的方法,使用system函数,调用本来就有的命令ls
具体用法
#include <stdlib.h>
void main(){
char path[]="/";
char cmdline[1000];
sprintf(cmdline,"ls -l \"%s\"",path);
system(cmdline);
}
具体用法
#include <stdlib.h>
void main(){
char path[]="/";
char cmdline[1000];
sprintf(cmdline,"ls -l \"%s\"",path);
system(cmdline);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
int
main()
{
struct stat buf;
/*use octarray for determining if permission bits set*/
static short octarray[9] = {0400,0200,0100,0040,0020,0010,0004,0002,0001};
/*mnemonic codes for file permissions,10 chars long because of the terminating null*/
static char perms[10] = "rwxrwxrwx";
char descrip[10];
int j;
char *filename = "test";
char *ptr;
if(stat(filename,&buf) == -1)
{ fprintf(stderr,"Couldn't stat %s\n",filename);
exit(1);
}
/*check the file type*/
if (S_ISREG(buf.st_mode)) ptr = "regular";
else if (S_ISDIR(buf.st_mode)) ptr = "directory";
else if (S_ISCHR(buf.st_mode)) ptr = "character special";
else if (S_ISBLK(buf.st_mode)) ptr = "block special";
else if (S_ISFIFO(buf.st_mode)) ptr = "fifo";
#ifdef S_ISLINK
else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link";
#endif
#ifdef S_ISSOCK
else if (S_ISSOCK(buf.st_mode)) ptr = "socket";
#endif
else ptr = "** unknown mode **";
/*put permissions into readable form*/
for(j = 0; j < 9; j++ )
{
/*test whether permission set using bitwise*/
if(buf.st_mode &octarray[j])
descrip[j] = perms[j];
else
descrip [j] = '-';
}
descrip[9] = '\0';/*make sure we've a string*/
/*display file niformation*/
printf("\nFile name: %s\n",filename);
printf("Type: %s\n",ptr);
printf("Size: %ld bytes.\n",buf.st_size);
printf("Permissions:%s\n",descrip);
printf("i-node number(serial number):%ld\n",buf.st_ino);
printf("Number of links:%d\n",buf.st_nlink);
printf("User-id: %d,Group-id: %d\n",buf.st_uid,buf.st_gid);
printf("Last-change time of i-node status:%s",ctime(&buf.st_ctime));
printf("Time of last modification:%s",ctime(&buf.st_mtime));
printf("Best I/O block size:%ld\n",buf.st_blksize);
printf("Number of 512-byte blocks allocated:%ld\n",buf.st_blocks);
exit(0);
}
下面是得到一个文件属性的函数,相当楼主看了就明白了:
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *filename, struct stat *buf); //! prototype,原型
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -最近修改时间*/
time_t st_ctime; /* time of last status change - */
};
具体的代码就不写了,不过我可以告诉楼主如果想学linux C的话建议楼主看一本书--<<UNIX环境下的高级编程>>,此书相当经典!!在网上有PDF版本的
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
int
main()
{
struct stat buf;
/*use octarray for determining if permission bits set*/
static short octarray[9] = {0400,0200,0100,0040,0020,0010,0004,0002,0001};
/*mnemonic codes for file permissions,10 chars long because of the terminating null*/
static char perms[10] = "rwxrwxrwx";
char descrip[10];
int j;
char *filename = "test";
char *ptr;
if(stat(filename,&buf) == -1)
{ fprintf(stderr,"Couldn't stat %s\n",filename);
exit(1);
}
/*check the file type*/
if (S_ISREG(buf.st_mode)) ptr = "regular";
else if (S_ISDIR(buf.st_mode)) ptr = "directory";
else if (S_ISCHR(buf.st_mode)) ptr = "character special";
else if (S_ISBLK(buf.st_mode)) ptr = "block special";
else if (S_ISFIFO(buf.st_mode)) ptr = "fifo";
#ifdef S_ISLINK
else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link";
#endif
#ifdef S_ISSOCK
else if (S_ISSOCK(buf.st_mode)) ptr = "socket";
#endif
else ptr = "** unknown mode **";
/*put permissions into readable form*/
for(j = 0; j < 9; j++ )
{
/*test whether permission set using bitwise*/
if(buf.st_mode &octarray[j])
descrip[j] = perms[j];
else
descrip [j] = '-';
}
descrip[9] = '\0';/*make sure we've a string*/
/*display file niformation*/
printf("\nFile name: %s\n",filename);
printf("Type: %s\n",ptr);
printf("Size: %ld bytes.\n",buf.st_size);
printf("Permissions:%s\n",descrip);
printf("i-node number(serial number):%ld\n",buf.st_ino);
printf("Number of links:%d\n",buf.st_nlink);
printf("User-id: %d,Group-id: %d\n",buf.st_uid,buf.st_gid);
printf("Last-change time of i-node status:%s",ctime(&buf.st_ctime));
printf("Time of last modification:%s",ctime(&buf.st_mtime));
printf("Best I/O block size:%ld\n",buf.st_blksize);
printf("Number of 512-byte blocks allocated:%ld\n",buf.st_blocks);
exit(0);
}
下面是得到一个文件属性的函数,相当楼主看了就明白了:
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *filename, struct stat *buf); //! prototype,原型
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -最近修改时间*/
time_t st_ctime; /* time of last status change - */
};
具体的代码就不写了,不过我可以告诉楼主如果想学linux C的话建议楼主看一本书--<<UNIX环境下的高级编程>>,此书相当经典!!在网上有PDF版本的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询