您的位置:首页 > 其它

实现ls -l功能 和目录实现

2014-08-20 01:55 99 查看
需要知道的函数

man 3 opendir

man 3 readdir

man 2 getcwd(保存当前目录)

man 2 chdir (使程序有该目录)

man 2 stat (一个函数,产生结构体存放一个文件的全部信息)

man ctime (规范时间)

man getpwuid (通过用户id得到用户名)

man getgrgid(通过组id得到组名)

可以查看用户id和组id

vim/etc/passwd

vim/etc/group

目标目录与程序目录

程序目录就字啊命令行的位置,不会变,但目标目录如果不及时chdir(old_path);就会是目标目录陷入混乱,等在一次对目标目录惊醒操作的时候就不知道当前的操作是哪个目录,其中old_path是之前程序目录的将其保存到old_path中

ls-l功能实现

src文件中

这里没有排序,为了使主要程序更清晰

main.cpp

#include<head.h>
int main(int argc,char *argv[]){
DIR *pdir;
pdir=opendir(argv[1]);
if(pdir==NULL){
perror("opendir");
return 0;
}
struct dirent *dent;
char old_path[128];
getcwd(old_path,128);
chdir(argv[1]);
while((dent=readdir(pdir))!=NULL){
if(dent->d_name[0]!='.')
show_file_info(dent->d_name);
}
closedir(pdir);
chdir(old_path);
}
<pre name="code" class="cpp"><pre name="code" class="cpp"><strong>show_file_info.cpp</strong>


show_file_info.cpp

#include<head.h>
char *time_format(char *src){
int len=strlen(src);
for(int i=len-1;i>=0;i--){
if(src[i]==':'){
src[i]='\0';
break;
}
}
return src+4;
}
void mode_to_string(mode_t mode,char dest[]){
memset(dest,'_',15);
if(S_ISDIR(mode))
dest[0]='d';
if(S_ISREG(mode))
dest[0]='_';
if(mode & S_IRUSR)
dest[1]='r';
if(mode & S_IWUSR)
dest[2]='w';
if(mode & S_IXUSR)
dest[3]='x';
if(mode & S_IRGRP)
dest[4]='r';
if(mode & S_IWGRP)
dest[5]='w';
if(mode & S_IXGRP)
dest[6]='x';
if(mode & S_IROTH)
dest[7]='r';
if(mode & S_IWOTH)
dest[8]='w';
if(mode & S_IXOTH)
dest[9]='x';
dest[10]='\0';
}
void show_file_info(char filename[]){
struct stat my_stat;
memset(&my_stat,0,sizeof(my_stat));
stat(filename,&my_stat);
char dest[15];
mode_to_string(my_stat.st_mode,dest);
printf("%-11s%-2u%-5s%-5s%-5u%-13s%-12s\n",dest,my_stat.st_nlink,getpwuid(my_stat.st_uid)->pw_name,getgrgid(my_stat.st_gid)->gr_name,(unsigned)my_stat.st_size,time_format(ctime(&my_stat.st_atime)),filename);
}
include文件中

#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<pwd.h>
#include<grp.h>
using namespace std;
void show_file_info(char *);



目录功能实现printf("%*s%s\n",spc_cnt," ",pent->d_name); 通过spc_cnt控制空格的大小

小if(strncmp(pent->d_name,",",1)==0||strncmp(pent->d_name,"..",2)==0) continue;

缺少这个就会陷入死循环,因为.. , 是每个目录都有的目录

#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>
using namespace std;
void print_tree(int spc_cnt,char* dirname){
DIR *pdir;
pdir=opendir(dirname);
if(pdir==NULL){
perror("opendir:");
return;
}
char old_path[128];
getcwd(old_path,128);
chdir(dirname);

struct dirent *pent;
while((pent=readdir(pdir))!=NULL){
if(strncmp(pent->d_name,".",1)==0||strncmp(pent->d_name,"..",2)==0)//忘记这个就会进入死循环
continue;
struct stat my_stat;
memset(&my_stat,0,sizeof(my_stat));
stat(pent->d_name,&my_stat);
if(S_ISDIR(my_stat.st_mode)){
printf("%*s%s\n",spc_cnt," ",pent->d_name);
print_tree(spc_cnt+5,pent->d_name);
}
else{
printf("%*s%s\n",spc_cnt," ",pent->d_name);
}
}
closedir(pdir);
chdir(old_path);
}
int main(int argc,char *argv[]){
print_tree(5,argv[1]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: