日期: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. [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  2. Linux符号连接的层数过多

    转:http://blog.csdn.net/ta893115871/article/details/7458869 创建符号链接的时候一定要使用绝对路径,例如:/usr/local/cxxt/con ...

  3. 基于Jenkins的持续集成CI

    CI(continuous integration)持续集成 一次构建:可能包含编译,测试,审查和部署,以及其他一些事情,一次构建就是将源代码放在一起,并验证软件是否可以作为一个一致的单元运行的过程. ...

  4. linux-radhat-gitlab服务搭建

    1.安装gitlab的依赖项 sudo yum install -y curl policycoreutils-python openssh-server cronie 2.设置防火墙 sudo lo ...

  5. Luogu 2912 [USACO08OCT]牧场散步Pasture Walking

    快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...

  6. JDK和Eclipse的下载路径

    JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse http://www.eclipse.or ...

  7. 【Maven】安装及配置(Linux)

    本文介绍Linux环境下安装Maven 安装环境和软件 系统:Linux(CentOS) 软件:apache-maven-3.3.9-bin.tar.gz(解压版). 安装步骤 maven是基于Jav ...

  8. 2018.06.30 cdq分治

    #cdq分治 ##一种奇妙的分治方法 优点:可以顶替复杂的高级数据结构:常数比较小. 缺点:必须离线操作. CDQ分治的基本思想十分简单.如下: 我们要解决一系列问题,包含修改和查询操作,我们将这些问 ...

  9. EF生成的SQL语句执行顺序问题。

    //实体被更改后,再做删除,EF只生成删除语句 //实体删除后再更改,EF报错 //添加语句会再,更改,删除后执行,更AddObject位置无关 //一个实体多个字段被改,只会生成一句update / ...

  10. Shell 中expr的使用

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