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++第一次课堂作业点这里 题目要求:输入半径,计算圆的面积,在调用外部函数,无需使用类.
随机推荐
- 1-QT-文件操作
Qt文本文件的读写操作 Qt文件操作详解(创建.写入.删除.INI.XML文件等 二进制文件的读写文件可以使用QFile类.QStream文本文件的读写建议使用QTextStream类,它操作文件更加 ...
- 七大排序的个人总结(二) 归并排序(Merge
七大排序的个人总结(二) 归并排序(Merge 归并排序(Merge Sort): 归并排序是一个相当“稳定”的算法对于其它排序算法,比如希尔排序,快速排序和堆排序而言,这些算法有所谓的最好与最 ...
- c++ 自动对象
转自: https://www.cnblogs.com/geloutingyu/p/8034904.html 1.自动对象默认情况下,局部变量的生命期局限于所在函数的每次执行期间.只有当定义它的函数被 ...
- laravel创建控制器
一.创建 php artisan make:controller Admin/IndexController 二.目录 app/Http/Controllers/ 控制器存储目录 [ C控制器 ]
- qstring转string
Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 1 QString str("死亡使者赛维"); 这样直接用带中文的字符串进行构造,那么用Q ...
- jmeter写好的脚本检查无误之后就是无法执行成功
今天,用jmeter写好的脚本,检查了好几遍,没有任何错误,但是执行的时候命令发送总是失败,没有cookie,请教高手,才得以解决. 重新创建一个HTTP request,把之前写好的都一一拷贝过来, ...
- 学习bn算法
好处: 1.归一化后有什么好处呢?原因在于神经网络学习过程本质就是为了学习数据分布,一旦训练数据与测试数据的分布不同,那么网络的泛化能力也大大降低: 2.另外一方面,一旦每批训练数据的分布各不相同(b ...
- 基于DDD的现代ASP.NET开发框架--ABP系列文章总目录(转)
出处:http://www.cnblogs.com/mienreal/p/4528470.html ABP相关岗位招聘:给热爱.NET新技术和ABP框架的朋友带来一个高薪的工作机会 ABP交流会录像视 ...
- 多块盘制作成一个lvm
1.创建pv[root@autodeploy nfs]# pvcreate /dev/vdd1WARNING: xfs signature detected on /dev/vdd1 at offse ...
- UVa 12118 nspector's Dilemma (构造+DFS+欧拉回路)
题意:给定n个点,e条边和每条边的长度t,每两个点之间都有路相连,让你求一条最短的路经过这e条边. 析:刚开始想到要判连通,然后把相应的几块加起来,但是,第二个样例就不过,后来一想,那么有欧拉回路的还 ...