嵌入式学习——Linux高级编程复习(MiniShell)——文件IO、标准IO、目录IO、软硬链接、makefile

 1. 主函数

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"
#include "ln_soft.h"
#include "rm.h"
#include "touch.h"
#include "mv.h"

int main(void)
{
    while (1)
    {
        char absolute_path[256] = {0};
        char *p = NULL;
        p = getcwd(absolute_path, sizeof(absolute_path));//获得当前目录的绝对路径
        if (NULL == p)
        {
            printf("getcwd error!\n");
            return -1;
        }
        printf("[linux@ubuntu:~%s$] ", absolute_path);//打印当前所在绝对路径

        char str[32] = {0};
        char *arg[4] = {NULL};
        int i = 0;
        int cnt = 0;//统计空格的个数

        fgets(str, sizeof(str), stdin);//从终端获得命令
        while (str[i] != '\n')//统计空格个数区分不同命令
        {
            if (str[i] == ' ')
            {
                cnt++;
            }
            i++;
        }

        if (3 == cnt)//拆分三个空格的命令
        {
            arg[0] = strtok(str, " ");
            arg[1] = strtok(NULL, " ");
            arg[2] = strtok(NULL, " ");
            arg[3] = strtok(NULL, "\n");

            Myln_s(arg[2], arg[3]);
        }
        else if (2 == cnt)//拆分两个空格的命令
        {
            arg[0] = strtok(str, " ");
            arg[1] = strtok(NULL, " ");
            arg[2] = strtok(NULL, "\n");

            char str[32] = {0};//将命令后的第一个路径存入str中
            int i = 0;
            while (*(arg[0]+i) != '\0')
            {
                str[i] = *(arg[0]+i);
                i++;
            }
            str[i] = '\0';

            if (0 == strcmp(str, "cp"))//cp 1 2
            {
                MyCp(arg[1], arg[2]);
            }
            else if (0 == strcmp(str, "ln"))//ln 1 2(hardlink)
            {
                Myln_h(arg[1]);
            }
            else if (0 == strcmp(str, "mv"))//mv 1 2
            {
                Mymv(arg[1], arg[2]);
            }
        }
        else if (1 == cnt)//拆分一个空格的命令
        {
            arg[0] = strtok(str, " ");
            arg[1] = strtok(NULL, "\n");

            char str[32] = {0};//将命令后的第一个路径存入str中
            int i = 0;
            while (*(arg[0]+i) != '\0')
            {
                str[i] = *(arg[0]+i);
                i++;
            }
            str[i] = '\0';

            if (0 == strcmp(str, "cat"))//cat 1
            {    
                Mycat(arg[1]);
            }
            else if (0 == strcmp(str, "cd"))//cd 1
            {	
                Mycd(arg[1]);
            }
            else if (0 == strcmp(str, "rm"))//rm
            {	
                Myrm(arg[1]);
            }
            else if (0 == strcmp(str, "touch"))//touch
            {	
                Mytouch(arg[1]);
            }
        }
        else if (0 == cnt)//拆分无空格的命令
        {   
            arg[0] = strtok(str, "\n");

            char str[32] = {0};//将命令路径存入str中
            int i = 0;
            while (*(arg[0]+i) != '\0')
            {
                str[i] = *(arg[0]+i);
                i++;
            }
            str[i] = '\0';

            if (0 == strcmp(str, "ls"))//ls
            {    
                Myls();
            }
            else if (0 == strcmp(str, "ll"))
            {
                Myll();
            }
            else if (0 == strcmp(str, "quit"))
            {
                break;
            }
        }
    }

    return 0;
}

2. cat

2.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cat.h"

int Mycat(char *str1)
{
    int src = 0;

    src = open(str1, O_RDONLY);
    if(-1 == src)
    {
        printf("open error");
    }

    while (1)
    {
        char buf[4096] = {0};
        ssize_t size = 0;

        size = read(src, buf, sizeof(buf));
        if (0 == size)
        {
            break;
        }
        fputs(buf, stdout);
    }

    close(src);

    return 0;
}

2.2 头文件

#ifndef _CAT_H
#define _CAT_H

extern int Mycat(char *str1);

#endif

3. cd

3.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 

int Mycd(char *str)
{
    int ret = chdir(str);

    if(-1 == ret)
    {
        printf("chdir error!\n");
    }

    return 0;
}

3.2 头文件

#ifndef _CD_H_
#define _CD_H_

extern int Mycd(char *str);

#endif

4. cp

4.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"

int MyCp(char *str1, char *str2)
{
    int src = 0;//cp 1 2
    int dst = 0;

    src = open(str1, O_RDONLY);
    dst = open(str2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(-1 == src || -1 == dst)
    {
        printf("open error");
    }

    while (1)
    {
        char buf[4096] = {0};
        ssize_t size = 0;

        size = read(src, buf, sizeof(buf));
        if (0 == size)
        {
            break;
        }
        write(dst, buf, size);
    }

    close(src);
    close(dst);

    return 0;
}

4.2 头文件

#ifndef _CP_H_
#define _CP_H_

extern int MyCp(char *str1, char *str2);

#endif

5. ll

5.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"

int Myll(void)
{
    DIR *dir = opendir("./");

    if (NULL == dir)
    {
        printf("opendir error!\n");
        return -1;
    }
    
    while (1)
    {
        struct dirent *info = readdir(dir);

        if (NULL == info)
        {
            break;
        }

        struct stat st;
        int ret = stat(info->d_name, &st);
        if (-1 == ret)
        {
            printf("stat error!\n");
            return -1;
        }

        ///
        if(S_ISREG(st.st_mode))//判断文件类型
        {
            fputc('-',stdout);
        }
        else if(S_ISDIR(st.st_mode))
        {
            fputc('d',stdout);
        }
        else 
        {
            fputc('o',stdout);
        }

        
        if(S_IRUSR & st.st_mode)//判断用户的操作权限
        {
            fputc('r',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IWUSR & st.st_mode)
        {
            fputc('w',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IXUSR & st.st_mode)
        {
            fputc('x',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        //
        if(S_IRGRP & st.st_mode)//判断组的操作权限
        {
            fputc('r',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IWGRP & st.st_mode)
        {
            fputc('w',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IXGRP & st.st_mode)
        {
            fputc('x',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        
        if(S_IROTH & st.st_mode)//判断其他的操作权限
        {
            fputc('r',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IWOTH & st.st_mode)
        {
            fputc('w',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        if(S_IXOTH & st.st_mode)
        {
            fputc('x',stdout);
        }else 
        {
            fputc('-',stdout);
        }

        struct tm *(tm_info) = localtime(&(st.st_mtime));
        struct passwd *pw = getpwuid(st.st_uid);
        struct group *gd = getgrgid(st.st_gid);

        printf("%lu ", st.st_nlink);
        printf("%s ", pw->pw_name);
        printf("%s ", gd->gr_name);
        printf("%5lu ", st.st_size);
        switch (tm_info->tm_mon)
        {
            case 0: printf("January "); break;
            case 1: printf("February "); break;
            case 2: printf("March "); break;
            case 3: printf("April "); break;
            case 4: printf("May "); break;
            case 5: printf("June "); break;
            case 6: printf("July "); break;
            case 7: printf("August "); break;
            case 8: printf("September "); break;
            case 9: printf("October "); break;
            case 10: printf("November "); break;
            case 11: printf("December "); break;
        }

        printf("%02d %02d:%02d ", tm_info->tm_mday, tm_info->tm_hour, tm_info->tm_min);
        printf("%s ", info->d_name);
        putchar('\n'); 
    }

        return 0;
}

5.2 头文件

#ifndef _LL_H_
#define _LL_H_

extern int Myll(void);

#endif

6. ln_hard

6.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"

int Myln_h(char *str)
{
    int ret = link(str, "hardlink");

    if(-1 == ret)
    {
        printf("link error\n");
        return -1;
    }

    return 0;
}

6.2 头文件

#ifndef _LN_HARD_H_
#define _LN_HARD_H_

extern int Myln_h(char *str);

#endif

7. ln_soft

7.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"
#include "ln_soft.h"

int  Myln_s(char *str1, char *str2)
{
	int ret = symlink(str1, str2);

    if(-1 == ret)
    {
        printf("symlink error\n");
        return -1;
    }

	return 0;
}       

7.2 头文件

#ifndef _LN_SORT_H_
#define _LN_SORT_H_

extern int Myln_s(char *str1, char *str2);

#endif

8. ls

8.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"

int Myls(void)
{
    DIR* dir = opendir("./");   

    if(NULL == dir)
    {
        printf("opendir error!\n");
        return -1;
    }

    while(1)
    {
        struct dirent *info =  readdir(dir);
        if(NULL == info)
        {
            break;
        }
        if (*(info->d_name) != '.')
        {
            printf("%s  ",info->d_name);
        }
    }
    putchar('\n');

    closedir(dir);

    return 0;
}

8.2 头文件

#ifndef _LS_H_
#define _LS_H_

extern int Myls(void);

#endif

9. mv

9.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"
#include "ln_soft.h"
#include "rm.h"
#include "touch.h"
#include "mv.h"

int Mymv(char *str1, char *str2)
{
	MyCp(str1, str2);
	Myrm(str1);

	return 0;
}

9.2 头文件

#ifndef _MV_H_
#define _MV_H_

extern int Mymv(char *str1, char *str2);
extern int MyCp(char *str1, char *str2);
extern int Myrm(char *str);

#endif

10. rm

10.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"
#include "ln_soft.h"
#include "rm.h"

int Myrm(char *str)
{
    int ret= remove(str);

    if(-1 == ret)
    {
        printf("remove error\n");
        return -1;
    }

    return 0;
}

10.2 头文件

#ifndef _RM_H_
#define _RM_H_

extern int Myrm(char *str);

#endif

11. touch

11.1 源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "cp.h"
#include "cat.h" 
#include "cd.h" 
#include "ls.h"
#include "ll.h"
#include "ln_hard.h"
#include "ln_soft.h"
#include "rm.h"
#include "touch.h"

int Mytouch(char *str)
{
    int dst = 0;

    dst = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(-1 == dst)
    {
        printf("open error");
        return -1;
    }

    close(dst);   

    return 0;
}

11.2 头文件

#ifndef _TOUCH_H
#define _TOUCH_H

extern int Mytouch(char *str);

#endif

11. Makefile

OBJ = a.out
SRC = MiniShell.c
SRC += cp.c cat.c cd.c ls.c ll.c ln_hard.c ln_soft.c rm.c touch.c mv.c

$(OBJ) : $(SRC)
	gcc $(SRC) -o $(OBJ)

clean:
	rm $(OBJ)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值