#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/types.h>
#include <grp.h>
extern void show_prompt();
extern int do_handle();
extern int do_select(char *argv[]);
extern int do_ls(char *path);
extern int do_cp(char *src, char *dst);
extern int do_cat(char *name);
extern int do_cd(char *path);
extern int do_ln_s(char *src, char *dst);
extern int do_rm(char *name);
extern int touch(char *name);
extern int do_mv(char *src, char *dst);
extern int do_ll(char path);
extern void show_info(char name);
extern int do_ln(char *src, char *dst);
#endif // HEAD_H
#include “head.h”
int main(void)
{
while(1)
{
do_handle();
printf(“Hello World!\n”);
}
return 0;
}
#include “head.h”
void show_prompt()
{
//linux@ubuntu:~/202/sec1/05minishell$
char path[125];
getcwd(path,125);
printf(“linux@ubuntu:%s$”,path);
}
int do_handle()
{
char cmd_line[50];
while(1)
{
show_prompt();
fgets(cmd_line, 50, stdin);
cmd_line[strlen(cmd_line)-1] = ‘\0’;
if(0 != strlen(cmd_line))
{
break;
}
}
char *argv[4] ={NULL};
argv[0] = strtok(cmd_line, " ");
argv[1] = strtok(NULL, " ");
argv[2] = strtok(NULL, " ");
argv[3] = strtok(NULL, "\0");
do_select(argv);
return 0;
}
int do_select(char *argv[])
{
if(0 == strcmp(“cat”, argv[0]))
{
if(NULL != argv[1])
{
do_cat(argv[1]);
printf(“cat is ok\n”);
}
printf(“cat_cmd error\n”);
}
if(0 == strcmp("cd", argv[0]))
{
if(NULL != argv[1])
{
do_cd(argv[1]);
printf("cd is ok\n");
}
else
{
printf("cd_cmd error\n");
}
}
if(0 == strcmp("ls", argv[0]))
{
do_ls(argv[1]);
printf("ls is ok\n");
}
if(0 == strcmp("ll", argv[0]))
{
do_ll(argv[1]);
//printf("ll is ok\n");
}
/*if(0 == strcmp("ln", argv[0]))
{
if(0 == strcmp("-s", argv[1]))
{
do_ln_s(argv[2], argv[3]);
}
else
{
if(NULL != argv[2] && NULL !=argv[3])
{
do_ln(argv[1], argv[2]);
}
}
}*/
if(0 == strcmp("ln", argv[0]))
{
// do_ln(argv[1], argv[2]);
printf("ln is ok\n");
}
if(0 == strcmp("ln", argv[0]) && 0 == strcmp("-s", argv[1]))
{
if(NULL != argv[2] && NULL !=argv[3])
{
do_ln_s(argv[2], argv[3]);
printf("ln -s is ok\n");
}
else
{
printf("ln-s_cmd error\n");
}
}
if(0 == strcmp("rm", argv[0]))
{
if(NULL != argv[1])
{
do_rm(argv[1]);
}
else
{
printf("rm_cmd error\n");
}
}
if(0 == strcmp(“touch”, argv[0]))
{
if(NULL != argv[1])
{
touch(argv[1]);
}
else
{
printf(“touch_cmd error\n”);
}
}
if(0 == strcmp(“mv”, argv[0]))
{
if(NULL != argv[1] && NULL != argv[2])
{
do_mv(argv[1], argv[2]);
}
}
if(0 == strcmp("cp", argv[0]))
{
//void* 0
if((NULL!= argv[1]) && (0!=argv[2]))
{
do_cp(argv[1], argv[2]);
printf("cp is ok\n");
}
else
{
printf("cp cmd error\n");
}
}
if(0 == strcmp("quit", argv[0]))
{
exit(0);
}
return 0;
}
int do_ln(char *src, char *dst)
{
int ret = link(src, dst);
if(-1 == ret)
{
perror(“link_creat error”);
}
return 0;
}
void show_info(char* name)
{
struct stat *buf= malloc(sizeof(struct stat));
int ret = lstat(name,buf);
if(-1 == ret)
{
printf(“stat error %s\n”,name);
return ;
}
if(S_ISREG(buf->st_mode))
{
printf("-");
}
else if(S_ISDIR(buf->st_mode))
{
printf("d");
}
else
{
printf("o");
}
if(buf->st_mode & S_IRUSR)
{
printf("r");
}
else
{
printf("-");
}
if(buf->st_mode & S_IWUSR)
{
printf("w");
}
else
{
printf("-");
}
if(buf->st_mode & S_IXUSR)
{
printf("x");
}
else
{
printf("-");
}
printf(" ");
time_t tm = buf ->st_mtime;
struct tm * p = localtime(&tm);
struct passwd * pw = getpwuid(buf->st_uid);
struct group *gr = getgrgid(buf->st_gid);
printf("%d %s %s %lu %d月 %d %d:%d %s\n",buf->st_nlink, pw->pw_name, gr->gr_name, buf->st_size
, p->tm_mon+1, p->tm_mday, p->tm_hour, p->tm_min, name);
free(buf);
}
int do_ll(char *path)
{
DIR *dir;
if(NULL == path)
{
dir = opendir("./");
}
else
{
dir = opendir(path);
}
if(NULL == dir)
{
return 1;
}
while(1)
{
struct dirent * dir_info = readdir(dir);
if(NULL == dir_info)
{
break;
}
char pathname[512]={0};
if(NULL == path)
{
char path[512];
getcwd(path,512);
sprintf(pathname,"%s/%s", path,dir_info->d_name);
}
else
{
sprintf(pathname, "%s/%s", path, dir_info->d_name);
}
show_info(pathname);
}
closedir(dir);
return 0;
}
int do_mv(char *src, char *dst)
{
FILE *fp1 = fopen(src, “r”);
if(NULL == fp1)
{
perror(“open_rm_src error”);
}
FILE *fp2 = fopen(dst, "w+");
if(NULL == fp2)
{
perror("open_rm_dst error");
}
while(1)
{
char buf[512] ={0};
char *ret = fgets(buf,512,fp1);
if(NULL == ret)
{
break;
}
fputs(buf,fp2);
}
fclose(fp1);
remove(src);
fclose(fp2);
return 0;
}
int touch(char *name)
{
int fd = open(name, O_RDWR|O_CREAT, 0666);
if(-1 == fd)
{
perror(“file_creat error”);
}
else
{
printf("file_creat success\n");
}
close(fd);
return 0;
}
int do_rm(char *name)
{
int ret = remove(name);
if(0 == ret)
{
printf(“remove_success\n”);
}
return 0;
}
int do_ln_s(char *src, char *dst)
{
int ret = symlink(src, dst);
if(0 == ret)
{
printf(“soft_creat success\n”);
}
return 0;
}
int do_cd(char *path)
{
chdir(path);
return 0;
}
int do_cat(char *name)
{
FILE *fp = fopen(name, "r");
if(NULL == fp)
{
perror("cat_open error");
}
while(1)
{
char buf[512] ={0};
char * ret = fgets(buf, 512, fp);
if(NULL == ret)
{
break;
}
fputs(buf,stdout);
}
fclose(fp);
return 0;
}
int do_cp(char *src, char *dst)
{
FILE *fp1 = fopen(src, “r”);
if(NULL == fp1)
{
perror(“cp_fopen1 error”);
}
FILE *fp2 = fopen(dst, "w");
if(NULL == fp2)
{
perror("cp_fopen2 error");
}
while(1)
{
char buf[200] = {0};
int ret = fread(buf, 1, 200,fp1);
if(0 == ret)
{
break;
}
fwrite(buf, 1, ret, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
int do_ls(char *path)
{
if(NULL == path)
{
DIR* dir = opendir("./");
if(NULL == dir)
{
printf("opendir error\n");
return 1;
}
struct dirent * info;
while(1)
{
info = readdir(dir);
if(NULL == info)
{
break;
}
/*if(DT_REG == info->d_type)
{
printf("普通文件\t");
}
if(DT_DIR == info->d_type)
{
printf("目录文件\t");
}*/
printf("%s\n",info->d_name);
}
closedir(dir);
}
else
{
DIR* dir = opendir(path);
if(NULL == dir)
{
printf("opendir error\n");
return 1;
}
struct dirent * info;
while(1)
{
info = readdir(dir);
if(NULL == info)
{
break;
}
/*if(DT_REG == info->d_type)
{
printf("普通文件\t");
}
if(DT_DIR == info->d_type)
{
printf("目录文件\t");
}*/
printf("%s\n",info->d_name);
}
closedir(dir);
}
return 0;
}