日期:2019/3/16

作业:实现命令cat, cp, echo。

myecho命令

#include <stdio.h>

int main(int argc, char *argv[])

{

    int i = 0;

    printf("argument count = %d\n", argc);

    for (; i < argc; i++)

        printf("%s\n", argv[i]);

    return
0;

}

 

 
 

mycat命令

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/stat.h>

#include <fcntl.h>

static
char buf[256] = {0};

int main(int argc, char *argv[])

{

    printf("Running program is %s\n", argv[0]);

    printf("Argument count is %d\n", argc);

    printf("File name is %s\n", argv[1]);

    int file_desc = open(argv[1], O_RDONLY);

    if (file_desc == -1)

    {

        perror("file is not existed!");

        exit(EXIT_FAILURE);

    }

    int flag = read(file_desc, buf, 255);

    while (flag != 0 && flag != -1)

    {

        printf("%s", buf);

        memset(buf, 0, sizeof(buf));

        flag = read(file_desc, buf, 255);

    }

    return
0;

}

 

 

 

 
 

mycp命令

不支持dst为目录的cp命令。

 

#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

static
char buf[256] = {0};

int main(int argc, char *argv[])

{

    printf("src is %s\n", argv[1]);

    printf("dst is %s\n", argv[2]);

    int src = open(argv[1], O_RDONLY);

    if (src == -1)

    {

        perror("file doesn't exist!\n");

        exit(EXIT_FAILURE);

    }

    int dst = open(argv[2], O_RDWR | O_CREAT);

    int flag = read(src, buf, 255);

    while (flag != 0 && flag != -1)

    {

        write(dst, buf, 255);

        memset(buf, 0, sizeof(buf));

        flag = read(src, buf, 255);

    }

    return
0;

}

 

 

mycp2命令

支持dst为目录。

#include <sys/types.h>

#include <dirent.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

static
char buf[256] = {0};

void cp_to_file(int src, int dst)

{

    int flag = read(src, buf, 255);

    while (flag != 0 && flag != -1)

    {

        write(dst, buf, flag);

        memset(buf, 0, sizeof(buf));

        flag = read(src, buf, 255);

    }

    close(src);

    close(dst);

}

int main(int argc, char *argv[])

{

    printf("src is %s\n", argv[1]);

    printf("dst is %s\n", argv[2]);

    int src = open(argv[1], O_RDONLY);

    if (src == -1)

    {

        perror("file doesn't exist!\n");

        exit(EXIT_FAILURE);

    }

    int dst;

    DIR *pdir = opendir(argv[2]);

    if (pdir == NULL)

    { //dst is file

        dst = open(argv[2], O_RDWR | O_CREAT);

        cp_to_file(src, dst);

    }

    else

    { //dst is dir

        printf("%s is a dir\n", argv[2]);

        char temp[256];

        strcpy(temp, argv[2]);

        if (temp[strlen(temp) - 1] != '/')

            strcat(temp, "/");

        strcat(temp, argv[1]);

        puts(temp);

        dst = open(temp, O_RDWR | O_CREAT);

        cp_to_file(src, dst);

    }

    return
0;

}

 

 

  

OSLab课堂作业1的更多相关文章

  1. OSLab课堂作业2

      日期:2019/3/23 内容: 实现内容 要求 mysys.c 实现函数mysys,用于执行一个系统命令. mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍 使 ...

  2. 栋哥你好,让我们回顾最初认识C++的时候(课堂作业)

    计算器的第一步,至今还记记忆犹新,本次的课堂作业,便是那个框架.闲话少叙,代码如下传送门: Main.cpp #include "stdafx.h" #include<ios ...

  3. 20155213 第十二周课堂作业MySort

    20155213 第十二周课堂作业MySort 作业要求 模拟实现Linux下Sort -t : -k 2的功能 参考 Sort的实现 提交码云链接和代码运行截图 初始代码 1 import java ...

  4. 课堂作业-Bag类的实现

    课堂作业-Bag类的实现 要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进 ...

  5. Java课程课堂作业代码

    前言 本文章只是单纯记录课堂老师布置的课堂作业代码,题目都比较简单,所以没有写解题思路,相信大家都能理解,当然其中有的解法和代码不是最优的,当时只是为了完成题目,后来也懒得改了,如果有不恰当或者不正确 ...

  6. Java课堂作业详解

    今天的Java课堂留下了一个作业:使用Eclipse编写一个程序,使输入的两个数进行加和,并且输出他们的和.对于这个题目,我们首先可以把它分解成为三个不同的小步骤 第一步就是输入这两个数,因为我们无需 ...

  7. 百度前端学院js课堂作业合集+分析(更新中...)

    第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. 面向对象程序设计_课堂作业_01_Circle

    The 1st classwork of the C++ program 题目: Create a program that asks for the radius of a circle and p ...

  9. C++ 课堂作业1.0

    c++第一次课堂作业点这里 题目要求:输入半径,计算圆的面积,在调用外部函数,无需使用类.

随机推荐

  1. error: In function ‘void* opencv_showimg(void*)’:

    今天这个问题折磨了我一下午,终于知道是为什么了,心酸历程.....赶紧来记录一下 错误: /home/wj/workspace/Loitor_VI_Sensor_SDK_V1./SDK/src/cam ...

  2. Spring框架的特点

    1. 为什么要学习Spring的框架 * 方便解耦,简化开发 * Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 * AOP编程的支持 * Spring提供面向切面 ...

  3. Halcon对文件的创建、读取、写入、删除等操作总结

    Halcon可以操作普通文本文件,也可以操作二进制文件.如下图所示,只需要设置“FileType”参数的取值即可明确是操作文本文件还是二进制文件: 下面的程序是操作一个.txt文本文件的完整代码: * ...

  4. linux下memcache安装

    安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # ...

  5. ldd "symbol lookup error"问题解决

    http://www.linuxquestions.org/questions/slackware-14/symbol-lookup-error-usr-lib-libgtk-x11-2-0-so-0 ...

  6. 1、HttpClient初探

    HttpClient是它的核心接口.可以理解为一个简单的浏览器. 主要方法有: getParams();   获取运行参数 getConnectionManager(); 获取连接管理器.连接管理器中 ...

  7. DB2 字符串的字段转为整形时的问题

    正确语句: select int(cast(substr(dpt_leader_ids,4,posstr(dpt_leader_ids,',0)')-4) as varchar(8))), d.* f ...

  8. 2018.09.18 atcoder Best Representation(kmp)

    传送门 思路简单不知为何调试了很久. 显然要么分成n个(所有字符相同),要么分成1个(原字符串无循环节),要么分成两个(有长度至少为2的循环节). 一开始以为可以直接hash搞定. 后来wa了几次之后 ...

  9. hdu-1394(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 思路:建立一个空线段树,求出逆序数,(逆序数性质:交换两个相邻数,逆序数+1或-1, 交换两个不 ...

  10. Shell 中expr的使用

    1.expr命令一般用于整数值,其一般格式为:expr argument operator argument一般的用法是使用expr做算术运算,如:[root@centos ~]# expr 10 + ...