标签:nis errno time etop hang mtime ati link alt
main.c
#include<stdio.h>
#include<unistd.h>
#include"ls.h"
int main(int argc,char *argv[])
{
char *optstring ="-l:a:i:h:";
int c;
while(1)
{
c=getopt(argc,argv,optstring);
if(c==-1)
break;
switch(c)
{
case ‘l‘:
lsl(argv,optarg);
break;
case ‘a‘:
lsa(optarg);
break;
case ‘i‘:
lsi(optarg);
break;
case ‘h‘:
lsh(optarg);
break;
case ‘?‘:
printf("不认识此选项%s\n",argv[optind-1]);
break;
case 1:
printf("非选项参数%s\n",argv[optind-1]);
break;
default:
break;
}
}
return 0;
}
ls.h
#ifndef __LS_H_
#define __LS_H_
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<dirent.h>
#include<glob.h>
#include<string.h>
#include<errno.h>
#define BUFSIZE 1024
//void common(char**argv,char *path);
void lsl(char**argv,char *path);
void lsa(char *path);
void lsi(char *path);
void lsh(char *path);
#endif
ls.h
#include"ls.h"
void lsl(char**argv,char *path)
{
struct stat ll;
glob_t globres;
char buf[BUFSIZE]={};
if(stat(path,&ll)<0)
{
perror("lstat()");
}
for(int i=0;i<globres.gl_pathc;i++)
{
if(stat(argv[2],&ll)==-1)
{
perror("stat()");
}
//文件类型:用S_IFMT 和st_mode做&操作
switch(ll.st_mode&S_IFMT)
{
case S_IFREG:
printf("-");
break;
case S_IFDIR:
printf("d");
break;
case S_IFSOCK:
printf("s");
break;
case S_IFLNK:
printf("l");
break;
case S_IFBLK:
printf("b");
break;
case S_IFCHR:
printf("c");
break;
case S_IFIFO:
printf("f");
break;
default:
break;
}
//权限:所有者
if(ll.st_mode &S_IRUSR)
putchar(‘r‘);
else
putchar(‘-‘);
if(ll.st_mode &S_IWUSR)
putchar(‘w‘);
else
putchar(‘-‘);
if(ll.st_mode&S_IXUSR){
if(ll.st_mode &S_ISUID){
putchar(‘s‘);}
else
putchar(‘x‘);
}
else
putchar(‘-‘);
//所属组
if(ll.st_mode &S_IRGRP)
putchar(‘r‘);
else
putchar(‘-‘);
if(ll.st_mode &S_IWGRP)
putchar(‘w‘);
else
putchar(‘-‘);
if(ll.st_mode &S_IXGRP)
putchar(‘x‘);
else
putchar(‘-‘);
//其他用户
if(ll.st_mode &S_IROTH)
putchar(‘r‘);
else
putchar(‘-‘);
if(ll.st_mode &S_IWOTH)
putchar(‘w‘);
else
putchar(‘-‘);
if(ll.st_mode &S_IXOTH)
putchar(‘x‘);
else
putchar(‘-‘);
// putchar(‘\n‘);
//硬链接
printf(" %ld ",ll.st_nlink);
// putchar(‘\n‘);
//文件拥有者名
struct passwd *pwd=NULL;
pwd =getpwuid(ll.st_uid);
printf("%s ",pwd->pw_name);
// putchar(‘\n‘);
//文件所属组
struct group *grp=NULL;
grp=getgrgid(ll.st_gid);
printf("%s ",grp->gr_name);
//总字节个数,磁盘空间大小st_block/2 k
printf("%ld ",ll.st_size);
//获取文件时间 atime mtime ctime
/* 1>访问时间(access time 简写为atime)
2>修改时间(modify time 简写为mtime)
3>状态修改时间(change time 简写为ctime)
*/
struct tm *tmp=NULL;
tmp=localtime(&ll.st_mtime);
// printf("%p\n",tmp);
char buf[BUFSIZE]={};
strftime(buf,BUFSIZE,"%m月 %d %H:%M ",tmp);
printf("%s",buf);
//文件名
// printf("%s",);
//文件名
putchar(‘\n‘);
strcpy(buf,path);
strcat(buf,"/*");
glob(buf,0,NULL,&globres);
}
}
void lsh(char *path)
{
struct stat h;
glob_t globres;
if(lstat(path,&h)<0)
{
perror("lstat()");
}
if(!S_ISDIR(h.st_mode))
{
printf("%s",path);
}
DIR *dp=opendir(path);
struct dirent *rd=NULL;
while(1)
{
rd=readdir(dp);
if(rd==NULL)
{
if(errno)
{
perror("readdir()");
closedir(dp);
}
break;
}
if((rd->d_name)[0]!=‘.‘)
{
printf("%s ",rd->d_name);
}
}
closedir(dp);
printf("\n");
/*
if (glob(path, GLOB_NOSORT, NULL, &globres) != 0) {
fprintf(stderr, "glob error\n");
return 1;
}
// glob(argv[2], GLOB_APPEND | GLOB_NOSORT, NULL, &globres);
for (int i = 0; i < globres.gl_pathc; i++) {
printf("%s\n", (globres.gl_pathv)[i]);
}
globfree(&globres);
*/
}
void lsa(char *path)
{
struct stat a;
glob_t globres;
if(lstat(path,&a)<0)
{
perror("lstat()");
}
if(!S_ISDIR(a.st_mode))
{
printf("%s",path);
}
DIR *dp=opendir(path);
struct dirent *rd=NULL;
while(1)
{
rd=readdir(dp);
if(rd==NULL)
{
if(errno)
{
perror("readdir()");
closedir(dp);
}
break;
}
printf("%s ",rd->d_name);
}
closedir(dp);
printf("\n");
}
void lsi(char *path)
{
struct stat li;
glob_t globres;
char buf[BUFSIZE]={};
if(lstat(path,&li)<0)
{
perror("lstat()");
return;
}
if(!S_ISDIR(li.st_mode))
{
printf("%ld %s\n",li.st_ino,path);
return;
}
strcpy(buf,path);
strcat(buf,"/*");
glob(buf,0,NULL,&globres);
for(int i=0;i<globres.gl_pathc;i++)
{
if(lstat(((globres.gl_pathv)[i]),&li)<0)
{
perror("lstat()");
return;
}
char *o=strrchr((globres.gl_pathv)[i],‘/‘);
printf("%ld %s\n",li.st_ino,++o);
}
globfree(&globres);
}
makefile
main:ls.o main.o
gcc ls.o main.c -o main
clean:
rm -rf *.o main
标签:nis errno time etop hang mtime ati link alt
原文地址:https://www.cnblogs.com/lmhya/p/10543445.html