#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#define BUFSIZE 1024
static int cnt;
void name_color(const char *name);
int mycmp(const char *p);
int myls(char *p, char *d_name, int mod);
int myls_dir(char *p, int mod);
int main (int argc, char **argv)
{
char *optstring = "-laih";//选项字符串
//a选项字符 -a选项参数//带-需要识别非选项字符
int c;
int mod = 0;
int sum = 0;
char *p = NULL;
if(argc < 2)
return 1;
while(1) {
c = getopt(argc, argv, optstring);
if(c == -1)
break;
switch(c) {
case 'i':
mod |= 1 << 2;
break;
case 'l':
mod |= 1 << 1;
break;
case 'a':
mod |= 1;
case 'h':
break;
case '?':
printf("含有未识别标识符\n");
break;
case 1:
p = argv[optind-1];
default:
break;
}
}
sum = myls_dir(p, mod);
if(mod & 2)
printf("总用量 %d\n", sum);
}
int myls_dir(char *p, int mod)
{
struct stat mystat;
DIR *dp = NULL;
struct dirent *entry = NULL;
char buf[BUFSIZE] = {};
dp = opendir(p);
int sum = 0;
if(stat(p, &mystat) == -1) {
perror("stat");
return -1;
}
if((mystat.st_mode & S_IFMT) != S_IFDIR) { //非目录
sum += myls(NULL, p, mod);
return 0;
}
//为目录
if(NULL == dp) {
perror("opendir()");
return -1;
}
while(1) {
entry = readdir(dp);
if(NULL == entry) {
if(errno) {
perror("readdir()");
closedir(dp);
return -1;
}
break;
}
/* memset(buf, '\0', BUFSIZE);
strcpy(buf, p);
strcat(buf, "/");
strcat(buf, entry->d_name);
*/ if((mod & 1) || mycmp(entry->d_name))
sum += myls(p, entry->d_name, mod);
}
if(cnt != 0)
printf("\n");
return sum/2;
}
int mycmp(const char *p)
{
return *p != '.';
}
int myls(char *p, char *d_name, int mod)
{
struct stat mystat;
char buf[BUFSIZE] = {};
memset(buf, '\0', BUFSIZE);
strcpy(buf, p);
strcat(buf, "/");
strcat(buf, d_name);
if(stat(buf, &mystat) == -1) {
perror("stat");
return -1;
}
if(mod & 4) { //显示ID
printf("%ld ", mystat.st_ino);
}
if(mod & 2) { //显示长格式
//类型
switch(mystat.st_mode & S_IFMT) {
case S_IFREG:
printf("-");
break;
case S_IFDIR:
printf("d");
break;
case S_IFCHR:
printf("c");
break;
case S_IFBLK:
printf("b");
break;
case S_IFIFO:
printf("p");
break;
case S_IFLNK:
printf("l");
break;
case S_IFSOCK:
printf("s");
default:
break;
}
//读写执行权限
if (mystat.st_mode & S_IRUSR)
putchar('r');
else
putchar('-');
if (mystat.st_mode & S_IWUSR)
putchar('w');
else
putchar('-');
if (mystat.st_mode & S_IXUSR) {
if(mystat.st_mode & S_ISUID)
putchar('s');
else
putchar('x');
}
else
putchar('-');
if (mystat.st_mode & S_IRGRP)
putchar('r');
else
putchar('-');
if (mystat.st_mode & S_IWGRP)
putchar('w');
else
putchar('-');
if (mystat.st_mode & S_IXGRP)
putchar('x');
else
putchar('-');
if (mystat.st_mode & S_IROTH)
putchar('r');
else
putchar('-');
if (mystat.st_mode & S_IWOTH)
putchar('w');
else
putchar('-');
if (mystat.st_mode & S_IXOTH) {
if(mystat.st_mode & S_ISVTX)
putchar('t');
else
putchar('x');
}
else
putchar('-');
printf(" %ld ", mystat.st_nlink);
struct passwd *pwd = NULL;
struct group *gid = NULL;
//所属用户和组
pwd = getpwuid(mystat.st_uid);
printf("%s ", pwd->pw_name);
gid = getgrgid(mystat.st_gid);
printf("%s", gid->gr_name);
//总字节个数
printf("%6ld ", mystat.st_size);
struct tm *tm_p = NULL;
tm_p = localtime(&mystat.st_mtim.tv_sec);
char buf[BUFSIZE] = {};
//最后修改日期
strftime(buf, BUFSIZE, "%m月 %d %H:%M", tm_p);
if(*buf == '0')
printf("%s ", buf+1);
else
printf("%s ", buf);
}
//显示文件名
name_color(d_name);
printf("\n");
#if 0 //格式调整
if(mod & 2) //长格式直接换行
printf("\n");
else if(mod & 4) { //显示id每行显示4项
if(cnt == 3) {
printf("\n");
cnt = 0;
}
else
cnt++;
}
else if(cnt == 5) { //通常显示6项
printf("\n");
cnt = 0;
}
else
cnt++;
#endif
return mystat.st_blocks;
}
void name_color(const char *name)
{
char *p = NULL;
p = strrchr(name, '.');
if(p != NULL) {
if(*name == '.') //以.开头为隐藏文件设置高亮
printf("\033[34m\033[1m%-10s\033[0m ", name);
else if(strcmp(p, ".out") == 0) //.out结尾为可执行文件设置高亮
printf("\033[32m\033[1m%-10s\033[0m ", name);
else
printf("%-10s ", name); //通常带点文件
}
else
printf("%-10s ", name); //通常不带点文件
}