学习时候做的一个简单的练习,内容可能不够完善,部分命令考虑的并不充分,仅提参考
文件结构
将.c后缀的代码与.h后缀的代码分开存放分别存放与src与include目录下
main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/stat.h>
#include<sys/types.h>
#include"ls.h"
#include"cp.h"
#include"cat.h"
#include"touch.h"
int main(int argc, const char *argv[])
{
while(1)
{
char cmd[32] = {0}; //输入的命令
char *cmd_arg[10] = {NULL}; //用于存储被拆分的命令
printf("[myshell#]");
fgets(cmd,sizeof(cmd),stdin);
int size = strlen(cmd);
if(cmd[size - 1] == '\n')
{
cmd[size - 1] = '\0';
}
int i = 0;
cmd_arg[i] = strtok(cmd," ");
while(cmd_arg[i] !=NULL)
{
i++;
cmd_arg[i] = strtok(NULL," ");
if(cmd_arg[i] == NULL)
{
i = 0;
break;
}
}
if(cmd_arg[0] == NULL)
continue;
if(strcmp(cmd_arg[0],"ls")==0)
{
ls(cmd_arg[1],cmd_arg[2]);
}
else if(strcmp(cmd_arg[0],"mkdir")==0)
{
int ret = mkdir(cmd_arg[1],0775);
if(ret==-1)
printf("mkdir error\n");
}
else if(strcmp(cmd_arg[0],"rm")==0)
{
int ret = remove(cmd_arg[1]);
if(ret==-1)
printf("rmdir error\n");
}
else if(strcmp(cmd_arg[0],"cd")==0)
{
int ret = chdir(cmd_arg[1]);
if(ret==-1)
{
printf("路径不存在\n");
}
}
else if(strcmp(cmd_arg[0],"pwd")==0)
{
if(cmd_arg[1] == NULL)
{
char p[32] = {0};
char *q = getcwd(p,sizeof(p));
if(q == NULL)
{
printf("路径不存在\n");
continue;
}
printf("%s\n",q);
}
else
{
continue;
}
}
else if(strcmp(cmd_arg[0],"cp")==0)
{
cp(cmd_arg[1],cmd_arg[2]);
}
else if(strcmp(cmd_arg[0],"cat")==0)
{
cat(cmd_arg[1]);
}
else if(strcmp(cmd_arg[0],"touch")==0)
{
touch(cmd_arg[1]);
}
else if(strcmp(cmd_arg[0],"exit")==0)
{
break;
}
else if(strcmp(cmd_arg[0],"ln")==0)
{
if(strcmp(cmd_arg[1],"-s")==0)
{
int ret = symlink(cmd_arg[2],cmd_arg[3]);
if(ret == -1)
{
printf("创建失败\n");
continue;
}
}
else
{
int ret = link(cmd_arg[1],cmd_arg[2]);
if(ret == -1)
{
printf("创建失败\n");
continue;
}
}
}
else if(strcmp(cmd_arg[0],"mv")==0)
{
int ret = rename(cmd_arg[1],cmd_arg[2]);
if(ret == -1)
{
"mv error\n";
}
}
}
}
ls.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include"ls.h"
#include <pwd.h>
#include <grp.h>
#include <time.h>
int ls(char *a,char *b)
{
if(a == NULL)
{
DIR *dp = opendir(".");
if(dp == NULL)
{
printf("ls error\n");
}
while(1)
{
struct dirent *p = readdir(dp);
if(p == NULL)
{
break;
}
if(p->d_name[0] == '.')
{
continue;
}
printf("%s\n",p->d_name);
}
}
else if(strcmp(a,"-a")==0)
{
if(b == NULL)
{
DIR *dp = opendir(".");
if(dp == NULL)
{
printf("ls error\n");
continue;
}
while(1)
{
struct dirent *p = readdir(dp);
if(p == NULL)
{
break;
}
printf("%s\n",p->d_name);
}
}
else
{
DIR *dp = opendir(b);
if(dp == NULL)
{
printf("ls error\n");
continue;
}
while(1)
{
struct dirent *p = readdir(dp);
if(p == NULL)
{
break;
}
printf("%s\n",p->d_name);
}
}
}
else if(strcmp(a,"-l")==0)
{
if(b == NULL)
{
DIR *dp = opendir(".");
if(dp == NULL)
{
printf("ls error\n");
}
while(1)
{
struct dirent *p = readdir(dp);
struct stat sta;
if(p == NULL)
{
break;
}
int ret = lstat(p->d_name, &sta);
if (ret < 0)
{
printf("fail stat\n");
return -1;
}
switch(sta.st_mode & S_IFMT)
{
case S_IFBLK:putchar('b'); break;
case S_IFIFO:putchar('p'); break;
case S_IFCHR:putchar('c'); break;
case S_IFDIR:putchar('d'); break;
case S_IFLNK:putchar('l'); break;
case S_IFSOCK:putchar('s'); break;
case S_IFREG:putchar('-'); break;
default:putchar('x');break;
}
sta.st_mode & S_IRUSR ? putchar('r') : putchar('-');
sta.st_mode & S_IWUSR ? putchar('w') : putchar('-');
sta.st_mode & S_IXUSR ? putchar('x') : putchar('-');
sta.st_mode & S_IRGRP ? putchar('r') : putchar('-');
sta.st_mode & S_IWGRP ? putchar('w') : putchar('-');
sta.st_mode & S_IXGRP ? putchar('x') : putchar('-');
sta.st_mode & S_IROTH ? putchar('r') : putchar('-');
sta.st_mode & S_IWOTH ? putchar('w') : putchar('-');
sta.st_mode & S_IXOTH ? putchar('x') : putchar('-');
struct passwd *pw = getpwuid(sta.st_uid);
printf(" %s", pw->pw_name);
//通过组id获取组的组名
struct group *pg = getgrgid(sta.st_gid);
printf(" %s", pg->gr_name);
//获取文件最后被修改的时间 硬链接个数 文件大小
struct tm *pt = localtime(&sta.st_mtime);
printf(" %ld %ld %2d月 %2d %2d:%2d ", sta.st_nlink, sta.st_size,
pt->tm_mon+1, pt->tm_mday, pt->tm_hour, pt->tm_min);
printf("%s\n", p->d_name);
}
}
else
{
DIR *dp = opendir(b);
if(dp == NULL)
{
printf("ls error\n");
}
while(1)
{
struct dirent *p = readdir(dp);
struct stat sta;
if(p == NULL)
{
break;
}
int ret = lstat(p->d_name, &sta);
if (ret < 0)
{
printf("fail stat\n");
return -1;
}
switch(sta.st_mode & S_IFMT)
{
case S_IFBLK:putchar('b'); break;
case S_IFIFO:putchar('p'); break;
case S_IFCHR:putchar('c'); break;
case S_IFDIR:putchar('d'); break;
case S_IFLNK:putchar('l'); break;
case S_IFSOCK:putchar('s'); break;
case S_IFREG:putchar('-'); break;
default:putchar('x');break;
}
sta.st_mode & S_IRUSR ? putchar('r') : putchar('-');
sta.st_mode & S_IWUSR ? putchar('w') : putchar('-');
sta.st_mode & S_IXUSR ? putchar('x') : putchar('-');
sta.st_mode & S_IRGRP ? putchar('r') : putchar('-');
sta.st_mode & S_IWGRP ? putchar('w') : putchar('-');
sta.st_mode & S_IXGRP ? putchar('x') : putchar('-');
sta.st_mode & S_IROTH ? putchar('r') : putchar('-');
sta.st_mode & S_IWOTH ? putchar('w') : putchar('-');
sta.st_mode & S_IXOTH ? putchar('x') : putchar('-');
struct passwd *pw = getpwuid(sta.st_uid);
printf(" %s", pw->pw_name);
//通过组id获取组的组名
struct group *pg = getgrgid(sta.st_gid);
printf(" %s", pg->gr_name);
//获取文件最后被修改的时间 硬链接个数 文件大小
struct tm *pt = localtime(&sta.st_mtime);
printf(" %ld %ld %2d月 %2d %2d:%2d ", sta.st_nlink, sta.st_size,
pt->tm_mon+1, pt->tm_mday, pt->tm_hour, pt->tm_min);
printf("%s\n", p->d_name);
}
}
}
else
{
DIR *dp = opendir(a);
if(dp == NULL)
{
printf("ls error\n");
}
while(1)
{
struct dirent *p = readdir(dp);
if(p == NULL)
{
break;
}
if(p->d_name[0] == '.')
{
continue;
}
printf("%s\n",p->d_name);
}
}
return 0;
}
ls.h文件
#ifndef __LS_H__
#define __LS_h__
extern int ls(char *a,char *b);
#endif
cp.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include"cp.h"
int cp(char *a, char *b)
{
FILE *P1 = fopen(a,"r");
FILE *P2 = fopen(b,"w");
if(P1 == NULL && P2 == NULL)
{
printf("open flie error\n");
}
while(1)
{
int ret = fgetc(P1);
if(ret == EOF)
{
break;
}
fputc(ret,P2);
}
fclose(P1);
fclose(P2);
return 0;
}
cp.h文件
#ifndef __CP_H__
#define __CP_H__
extern int cp(char *a,char *b);
#endif
cat.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include"cat.h"
int cat(char *a)
{
FILE *P1 = fopen(a,"r");
if(P1 == NULL)
{
printf("open flie error\n");
}
while(1)
{
int ret = fgetc(P1);
if(ret == EOF)
{
break;
}
printf("%c",ret);
}
return 0;
}
cat.h文件
#ifndef __CAT_H__
#define __CAT_H__
extern int cat(char *a);
#endif
touch.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include"touch.h"
int touch(char *a)
{
FILE *P1 = fopen(a,"w");
if(P1 == NULL)
{
printf("open flie error\n");
}
fclose(P1);
return 0;
}
touch.h文件
#ifndef __TOUCH_H__
#define __TOUCH_H__
extern int touch(char *a);
#endif
Makefile文件
存放在src目录下,让编译变得方便快捷
SRC=main.o
SRC+=cat.o
SRC+=ls.o
SRC+=touch.o
SRC+=cp.o
OUT=app
CC=gcc
INCLUDE=../include
$(OUT):$(SRC)
$(CC) $^ -o $@ -I$(INCLUDE)
%.o:%.c
$(CC) -c $^ -o $@ -I$(INCLUDE)
总结
代码实现了ls cd pwd cat touch mkdir等相关命令的实现,敲完之后收获颇丰,个人认为学习语言最重要的还是实际,感谢您的观看,虽然可能没人会看吧。