#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
int GetFileType(mode_t mode){
if(S_ISBLK(mode)) return 'b';
if(S_ISCHR(mode)) return 'c';
if(S_ISDIR(mode)) return 'd';
if(S_ISFIFO(mode)) return 'p';
if(S_ISREG(mode)) return '-';
if(S_ISLNK(mode)) return 'l';
return 1;
}
void setmem(void *ps, char ch, size_t sz){
register n = 0;
for(; n<sz; ++n){
*((char*)ps+n) = ch;
}
}
int GetFileMode(mode_t mode, char *resp){
if(resp == NULL) return 0;
setmem(resp,'-',9);
if(mode & S_IRUSR) resp[0]='r';
if(mode & S_IWUSR) resp[1]='w';
if(mode & S_IXUSR) resp[2]='x';
if(mode & S_IRGRP) resp[3]='r';
if(mode & S_IWGRP) resp[4]='w';
if(mode & S_IXGRP) resp[5]='x';
if(mode & S_IROTH) resp[6]='r';
if(mode & S_IWOTH) resp[7]='w';
if(mode & S_IXOTH) resp[8]='x';
return 9;
}
int GetFileUID(struct stat mode){return mode.st_uid;}
int GetFileGID(struct stat mode){return mode.st_gid;}
int GetFileLINK(struct stat mode){return mode.st_nlink;}
int GetFileSIZE(struct stat mode){return mode.st_size;}
void do_ls(char []); //普通显示
void do_ls_a(char []); //显示隐藏文件
void do_ls_l(char []); //显示文件所有信息
void do_ls_F(char []); //显示文件类型
int main(int argc, char *argv[]){
if (argc == 1){//ls
do_ls(".");
}else if(argc == 2 && argv[1][0] != '-'){//ls -
do_ls(argv[1]);
}else if(argc == 2 && !strcmp(argv[1],"-l")){ //ls -l
do_ls_l(".");
}else if (argc == 2 && !strcmp(argv[1], "-a")){//ls -a
do_ls_a(".");
}else if (argc == 2 && !strcmp(argv[1], "-F")){//ls -F
do_ls_F(".");
}else if(argc == 3){//有三个参数
if( !strcmp(argv[1],"-l")){
do_ls_l(argv[2]);
}else if(!strcmp(argv[1],"-a")){
do_ls_a(argv[2]);
}
if( !strcmp(argv[2],"-l")){
do_ls_l(argv[1]);
}else if(!strcmp(argv[2],"-a")){
do_ls_a(argv[1]);
}
}
return 0;
}
void do_ls(char dirname[]){
DIR *dir_ptr;
int n = 0;
struct dirent *direntp;
if((dir_ptr = opendir(dirname)) == NULL){
fprintf(stderr,"ls: cannot open %s", dirname);
}else{
while((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.'){
n++;
printf("%s/t",direntp->d_name);
}
if ((n % 4 == 0) && (n > 0)) {
printf("/n");
n = 0;
}
}
if( n % 4 != 0)printf("/n");
}
closedir(dir_ptr);
}
void do_ls_a(char dirname[]){
DIR *dir_ptr;
int n = 0;
struct dirent *direntp;
if((dir_ptr = opendir(dirname)) == NULL){
fprintf(stderr,"ls: cannot open %s", dirname);
}else{
while((direntp = readdir(dir_ptr)) != NULL){
n++;
printf("%s/t/t",direntp->d_name);
if (n == 2){
printf("/n");
n = 0;
}
}
closedir(dir_ptr);
}
}
void do_ls_l(char dirname[]){
DIR *dir_ptr;
struct dirent *direntp;
struct stat statbuf;
if((dir_ptr = opendir(dirname)) == NULL){
fprintf(stderr,"ls: cannot open %s/n", dirname);
}else{
while((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.'){
lstat(direntp->d_name,&statbuf);
char mode[10];
mode[9] = 0;
struct passwd *upw;
upw = getpwuid(GetFileUID(statbuf));
struct group *fg;
int fgid = GetFileGID(statbuf);
fg = getgrgid(fgid);
struct tm *ft;
ft = localtime(&statbuf.st_mtime);
GetFileMode(statbuf.st_mode,mode);
printf("%c%s %d %s %s %d ",
GetFileType(statbuf.st_mode),
mode,
GetFileLINK(statbuf),
upw->pw_name,
fg->gr_name,
GetFileSIZE(statbuf));
printf("%d-%d-%d %d:%d ",ft->tm_year + 1900,ft->tm_mon,ft->tm_mday,ft->tm_hour,ft->tm_min);
printf("%s/n",direntp->d_name);
}
}
closedir(dir_ptr);
}
}
void do_ls_F(char dirname[]){
DIR *dir_ptr;
struct dirent *direntp;
struct stat statbuf;
if((dir_ptr = opendir(dirname)) == NULL){
fprintf(stderr,"ls: cannot open %s/n", dirname);
}else{
while((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.'){
lstat(direntp->d_name,&statbuf);
char mode[10];
mode[9] = 0;
struct passwd *upw;
upw = getpwuid(GetFileUID(statbuf));
struct group *fg;
int fgid = GetFileGID(statbuf);
fg = getgrgid(fgid);
struct tm *ft;
ft = localtime(&statbuf.st_mtime);
GetFileMode(statbuf.st_mode,mode);
printf("%c%s/t%s/n",GetFileType(statbuf.st_mode),mode,direntp->d_name);
}
}
closedir(dir_ptr);
}
}
linux 模拟ls
最新推荐文章于 2024-10-30 13:57:55 发布