OSLab课堂作业1
日期: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 }
|
mycat命令
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h> static 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 }
|
mycp命令
不支持dst为目录的cp命令。
|
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> static 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 }
|
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 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 }
|
OSLab课堂作业1的更多相关文章
- OSLab课堂作业2
日期:2019/3/23 内容: 实现内容 要求 mysys.c 实现函数mysys,用于执行一个系统命令. mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍 使 ...
- 栋哥你好,让我们回顾最初认识C++的时候(课堂作业)
计算器的第一步,至今还记记忆犹新,本次的课堂作业,便是那个框架.闲话少叙,代码如下传送门: Main.cpp #include "stdafx.h" #include<ios ...
- 20155213 第十二周课堂作业MySort
20155213 第十二周课堂作业MySort 作业要求 模拟实现Linux下Sort -t : -k 2的功能 参考 Sort的实现 提交码云链接和代码运行截图 初始代码 1 import java ...
- 课堂作业-Bag类的实现
课堂作业-Bag类的实现 要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进 ...
- Java课程课堂作业代码
前言 本文章只是单纯记录课堂老师布置的课堂作业代码,题目都比较简单,所以没有写解题思路,相信大家都能理解,当然其中有的解法和代码不是最优的,当时只是为了完成题目,后来也懒得改了,如果有不恰当或者不正确 ...
- Java课堂作业详解
今天的Java课堂留下了一个作业:使用Eclipse编写一个程序,使输入的两个数进行加和,并且输出他们的和.对于这个题目,我们首先可以把它分解成为三个不同的小步骤 第一步就是输入这两个数,因为我们无需 ...
- 百度前端学院js课堂作业合集+分析(更新中...)
第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 面向对象程序设计_课堂作业_01_Circle
The 1st classwork of the C++ program 题目: Create a program that asks for the radius of a circle and p ...
- C++ 课堂作业1.0
c++第一次课堂作业点这里 题目要求:输入半径,计算圆的面积,在调用外部函数,无需使用类.
随机推荐
- ubuntu 安装 google Gtest
1.安装源代码 在ubuntu的桌面上,右键选择打开终端,在终端中输入如下命令: $ sudo apt-get install libgtest-dev 下载源码后,apt将会在目录/usr/src/ ...
- apache中开启rewrite
1.在apache配置文件httpd.conf中找到如下行: #LoadModule rewrite_module modules/mod_rewrite.so 去掉该行前面的#号 2.在httpd. ...
- laravel中if
1.控制器 2.模板
- Laravel 中使用原生的 PHPExcel
1.安装 composer require maatwebsite/excel 之后,程序中就可以使用 PHPExcel 了 2.控制器中 public function export(Request ...
- 【Log】logback指定配置文件(二)
通常我们在不同的环境使用不同的日志配置文件,本章讲指定logback的配置文件,如何使用logback参照[Log]logback的配置和使用(一) 写一个配置加载类,注意JoranConfigura ...
- KBMMW 4.83.00 发布
新版本又来了,端午节都不让大家过好:) 这次终于支持ios 64了,不用我再手工改了. Components4Developers is a company established in 1999 w ...
- 2018.10.20 bzoj2748: [HAOI2012]音量调节(背包)
传送门 这题是不是太sbsbsb了一点. 难度直逼普及-. 直接背包判存在性就行了. 代码: #include<bits/stdc++.h> using namespace std; bo ...
- git将本地仓库强制替换掉远程仓库
$ git remote add origin <url> $ git push --force --set-upstream origin master
- 主机和docker容器拷贝文件
从主机复制到容器sudo docker cp host_path containerID:container_path 从容器复制到主机sudo docker cp containerID:conta ...
- 常见XML解析器
xpp3 官网 http://www.extreme.indiana.edu/xgws/xsoap/xpp/ 简介 Xml Pull Parser (in short XPP) is a stream ...