您的位置:首页 > 其它

自定义mac的ll命令

2015-08-01 00:00 239 查看
摘要: mac本身不自带ll命令,而每次都要使用ls -l显得特别麻烦。所以附上下面的源代码。大家可以自行编译成ll命令

#include<stdio.h>
#include<time.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<.h>
do_ls([]);
dostat( *);
show_file_info( *, stat *);
mode_to_letters(,[]);
* uid_to_name(uid_t);
* gid_to_name(gid_t);

main( argc, *argv[]){
(argc==)
do_ls();

printf();
}

do_ls( dirname[]){
DIR *dir_ptr;        dirent *direntp;         ((dir_ptr=opendir(dirname))==)
fprintf(stderr,,dirname);
{
((direntp=readdir(dir_ptr))!=)
dostat(direntp->d_name);
closedir(dir_ptr);
}
}

dostat( *filename){
stat info;
(lstat(filename,&info)==-)
perror();

show_file_info(filename,&info);
}

show_file_info( *filename, stat *info_p){
modestr[];
mode_to_letters(info_p->st_mode,modestr);
printf(,modestr);
printf(,()info_p->st_nlink);
printf(,uid_to_name(info_p->st_uid));
printf(,gid_to_name(info_p->st_gid));
printf(,()info_p->st_size);
time_t timelong=info_p->st_mtime;
tm *htime=localtime(&timelong);
printf(,htime->tm_year+,htime->tm_mon+,htime->tm_mday,htime->tm_hour,htime->tm_min);
printf(,filename);
}

mode_to_letters( mode, str[]){
strcpy(str,);
(S_ISDIR(mode))   str[]=;
(S_ISCHR(mode))   str[]=;
(S_ISBLK(mode))   str[]=;

(mode & S_IRUSR)  str[]=;
(mode & S_IWUSR)  str[]=;
(mode & S_IXUSR)  str[]=;

(mode & S_IRGRP)  str[]=;
(mode & S_IWGRP)  str[]=;
(mode & S_IXGRP)  str[]=;

(mode & S_IROTH)  str[]=;
(mode & S_IWOTH)  str[]=;
(mode & S_IXOTH)  str[]=;
}

#include<pwd.h>      * uid_to_name(uid_t uid){
passwd *pw_str;
numstr[];
((pw_str=getpwuid(uid))==NULL){
sprintf(numstr,,uid);                numstr;
}

pw_str->pw_name;
}

#include<grp.h>
* gid_to_name(gid_t gid){
group *grp_ptr;
numstr[];
((grp_ptr=getgrgid(gid))==NULL){
sprintf(numstr,,gid);
numstr;
}

grp_ptr->gr_name;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mac ll命令