日期: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. Python学习记录day7

    目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...

  2. MQ基础概念和介绍

    一.中间件 MQ是一种中间件产品,至于什么是中间件,中间件能干什么,参见以下链接: http://baike.baidu.com/view/23710.htm 二.WebSphere MQ的原理 We ...

  3. Linux dkpg命令

    一.简介 dpkg 是Debian Package 的简写,是Debian系列系统下的一个软件安装.更新及移除工具. 二.常用指令 1.查询功能 查看软件包信息: dpkg -info xxx.deb ...

  4. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  5. laravel创建控制器

    一.创建 php artisan make:controller Admin/IndexController 二.目录 app/Http/Controllers/ 控制器存储目录 [ C控制器 ]

  6. PhpStorm 注册相关

    网址 http://idea.lanyus.com/ 最新(2017年9月)PhpStorm 2017.3 .WebStorm 2017.2.5.PyCharm  2016.3激活方式 打开网址 ht ...

  7. 调用数据库--stone

    from Mysql_operate_class import mysql def saveMysqlData(sql, dbname="algorithm"): pym = my ...

  8. centos 挂载u盘

    1.创建一个目录来挂载U盘 mkdir /mnt/usb #创建usb目录挂载U盘 2.插上U盘,查看移动设备状态 fdisk -l #(注意:参数是小写字母 l 不是数字 1) 会看到类似这一行:/ ...

  9. sci-hub 下载地址更新

    #  2017-12-14 可用 http://www.sci-hub.tw/ 文献共享平台

  10. memmove、memcpy、strcpy、memset的实现

    memmove.memcpy.strcpy.memset 原型为: void *memmove( void* dest, const void* src, size_t count ); char*  ...