实践代码

intmain(int argc, char *argv[]){}

argc是用来表示在命令行下输入命令时的参数个数,包括指令本身;argv[]是用来取得你输入的参数。针对具体指令分析如下(每一步解释由注释形式给出)。

头文件的用处:

stdio.h 标准输入输出stdlib.h    C标准函数库unistd.h    Unix类系统定义符号常量fcntl.h  定义了很多宏和open,fcntl函数原型sys/types.h 基本系统数据类型dirent.h unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数、readdir函数。termios.h 在Posix规范中定义的标准接口

cp

#include        <stdio.h>//标准输入输出
#include <stdlib.h>//C标准函数库 #include <unistd.h>//Unix类系统定义符号常量 #include <fcntl.h>//定义了很多宏和open,fcntl函数原型 #define BUFFERSIZE 4096//定义存储器容量 #define COPYMODE 0644//定义复制的长度 voidoops(char *, char *);intmain(int argc, char *argv[]) {int in_fd, out_fd, n_chars;//三个描述符值 char buf[BUFFERSIZE];//存储器位置 /*cp的参数有两个,分别是要复制的文件,和目的目录,这样一共应该是有三个操作数 所以要先检查argc的值是否为三,如果不是,返回标准错误*/ if (argc != 3) {fprintf(stderr, "usage: %s source destination\n", *argv);exit(1); }/*检查cp的第一个参数,要复制的文件,用open打开,in_fd为open返回的描述符 如果返回-1,代表打开失败,提示错误*/ if ((in_fd = open(argv[1], O_RDONLY)) == -1) oops("Cannot open ", argv[1]);/*检查cp的第二个参数,复制的目的地址,用create在目的地址创建新文件,out_fd为open返回的描述符 如果返回-1,代表创建失败,提示错误*/ if ((out_fd = creat(argv[2], COPYMODE)) == -1) oops("Cannot creat", argv[2]);/*cp指令的动作就是读取一个文件的内容到存储器,在新的地址创建空白文件,再从存储器将内容写入新文件。 这里判断复制是否成功: 如果能读取顺利,而读取的位数和写的位数不同,是写错误; 如果读取失败,是读错误。*/ while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0)if (write(out_fd, buf, n_chars) != n_chars) oops("Write error to ", argv[2]);if (n_chars == -1) oops("Read error from ", argv[1]);/*这里执行的是关闭文件的动作,in_fd和out_fd两个文件描述符 所指向的文件只要有一个关闭错误,就提示关闭错误。*/ if (close(in_fd) == -1 || close(out_fd) == -1) oops("Error closing files", "");}/*这个是用来输出错误信息的函数*/ voidoops(char *s1, char *s2) {fprintf(stderr, "Error: %s ", s1); perror(s2);//用来将上一个函数发生错误的原因输出到标准设备(stderr) exit(1);}

ls(1)

#include   <stdio.h>
#include    <sys/types.h>
#include <dirent.h> voiddo_ls(char []);intmain(int argc, char *argv[]) {/*如果操作数只有1个,表明ls后面没有带参数,默认为当前目录,.表示当前目录。*/ if ( argc == 1 ) do_ls( "." );/*如果ls后面有参数,就把参数读入argv中。*/ else while ( --argc ){printf("%s:\n", *++argv ); do_ls( *argv ); }return0;}/*因为ls和dir功能相近,用dir来实现ls*/ voiddo_ls( char dirname[] ) { DIR *dir_ptr;struct dirent *direntp;/*如果没有指向的那个地址,报错*/ if ( ( dir_ptr = opendir( dirname ) ) == NULL )fprintf(stderr,"ls1: cannot open %s\n", dirname);else { /*递归的方式来读取*/ while ( ( direntp = readdir( dir_ptr ) ) != NULL )printf("%s\n", direntp->d_name ); closedir(dir_ptr); }}

ls2

ls2前半部分和ls1一样,所不同的只是多出来了一部分,用来显示文件的详细信息,比如用户名,群组名,大小,创建时间,读写权限等。

#include   <stdio.h>
#include    <stdlib.h>
#include <utmp.h> #include <fcntl.h> #include <unistd.h> #define SHOWHOST intshow_info( struct utmp *utbufp ) {printf("%-8.8s", utbufp->ut_name); printf(" "); printf("%-8.8s", utbufp->ut_line); printf(" "); printf("%10ld", utbufp->ut_time); printf(" "); #ifdef SHOWHOST printf("(%s)", utbufp->ut_host); #endif printf("\n"); return0;}intmain() {struct utmp current_record; int utmpfd; int reclen = sizeof(current_record);/*打开UTMP_FILE读取信息,如果打开失败则输出失败信息。*/ if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){ perror( UTMP_FILE ); exit(1); }/*读取信息到存储器中,reclen就是是读的字节数,然后再调用函数打印出来。*/ while ( read(utmpfd, &current_record, reclen) == reclen ) show_info(&current_record); close(utmpfd);return0; }

echostate

这个代码是用来检查命令行中的提示符是否显示的,如果显示,输入的命令都可见,不显示则表示输入的命令不可见,具体例子结合setecho代码一起

#include        <stdio.h>
#include        <stdlib.h>
#include <termios.h> intmain() {struct termios info;int rv; rv = tcgetattr( 0, &info ); /* read values from driver */ if ( rv == -1 ){ perror( "tcgetattr");exit(1); }if ( info.c_lflag & ECHO )printf(" echo is on , since its bit is 1\n");else printf(" echo is OFF, since its bit is 0\n");return0;}

setecho

这个与上面对应,改变echo的状态

#include        <stdio.h>
#include        <stdlib.h>
#include <termios.h> #define oops(s,x) { perror(s); exit(x); } intmain(int argc, char *argv[]) {struct termios info;if ( argc == 1 ) exit(0);if ( tcgetattr(0,&info) == -1 ) oops("tcgettattr", 1);if ( argv[1][0] == 'y' ) info.c_lflag |= ECHO ;/*打开提示符*/ else info.c_lflag &= ~ECHO ;/*隐藏提示符*/ if ( tcsetattr(0,TCSANOW,&info) == -1 ) oops("tcsetattr",2);

return0;}

可以看出来,当echo is on的时候,输入的指令是可见的,当设置为off的时候,输入指令不可见

fileinfo

这个功能用来实现显示文件信息,建立了一个stat数据结构。

先判断命令是否有操作数,有的话才能继续进行下去,如果没有报错就打印出来相关文件信息,报错就用perror将报错信息打印出来。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> voidshow_stat_info(char *, struct stat *);intmain(int argc, char *argv[]) {struct stat info; if (argc>1) {

if( stat(argv[1], &info) != -1 ){ show_stat_info( argv[1], &info );return0; }else perror(argv[1]);  }return1;}voidshow_stat_info(char *fname, struct stat *buf) {printf(" mode: %o\n", buf->st_mode); printf(" links: %d\n", buf->st_nlink); printf(" user: %d\n", buf->st_uid); printf(" group: %d\n", buf->st_gid); printf(" size: %d\n", (int)buf->st_size); printf("modtime: %d\n", (int)buf->st_mtime); printf(" name: %s\n", fname ); }

filesize

用st_size成员来计算文件的字节数大小,先判断是否有错误,没有的话就调用。

#include <stdio.h>
#include <sys/stat.h>
intmain() {struct stat infobuf; if ( stat( "/etc/passwd", &infobuf) == -1 ) perror("/etc/passwd");else printf(" The size of /etc/passwd is %d\n", infobuf.st_size );}

spwd

#include <stdio.h>
#include    <stdlib.h>
#include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> ino_t get_inode(char *);voidprintpathto(ino_t);voidinum_to_name(ino_t , char *, int );intmain() { printpathto( get_inode( "." ) ); putchar('\n'); return0;}voidprintpathto( ino_t this_inode ) { ino_t my_inode ;char its_name[BUFSIZ];if ( get_inode("..") != this_inode ) { chdir( ".." );  inum_to_name(this_inode,its_name,BUFSIZ); my_inode = get_inode( "." );  printpathto( my_inode ); printf("/%s", its_name ); 

 }}voidinum_to_name(ino_t inode_to_find , char *namebuf, int buflen) { DIR *dir_ptr; struct dirent *direntp;  dir_ptr = opendir( "." );if ( dir_ptr == NULL ){ perror( "." );exit(1); }while ( ( direntp = readdir( dir_ptr ) ) != NULL )if ( direntp->d_ino == inode_to_find ) {strncpy( namebuf, direntp->d_name, buflen); namebuf[buflen-1] = '\0';  closedir( dir_ptr );return; }fprintf(stderr, "error looking for inum %d\n", (int) inode_to_find);exit(1);}ino_t get_inode( char *fname ) {struct stat info;if ( stat( fname , &info ) == -1 ){fprintf(stderr, "Cannot stat "); perror(fname);exit(1); }return info.st_ino;}

这个代码的功能是列出当前目录:

testioctl

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>int main(){   struct winsize size;  if( isatty(STDOUT_FILENO) == 0)     exit(1);  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {     perror("ioctl TIOCGWINSZ error");       exit(1);  } printf("%d rows %d columns\n", size.ws_row, size.ws_col);   return 0;}

实践代码:下面四图各为老师给的代码运行的结果。cp1,cp2,who1,who2

 

参考资料:         1.fs.tar压缩包;         2.20135302的博客         3.《深入理解计算机》         4.exp1里pdf教程

学习总结:
本章代码阅读理解,编译运行代码简单,重点是要阅读代码,在学习的过程中,我尽量少用百度,多练习使用man学习理解相关系统调用, 理解参数、返回值的含义。用grep -nr xxx  /usr/include 查宏定义,给代码加注释。操作简单,其实上周就用做这个作业,这周看得更仔细一点。把代码编译运行了一下。
遇到的问题:在导入代码的时候,不知道放入哪个文件夹。后来经过查找找到了。若导入的是压缩包,要在命令行里解压缩;或者可以直接导入解压缩过后的压缩包。

转载于:https://www.cnblogs.com/zhengwei0712/p/4967014.html

信息安全系统设计基础第十周学习总结相关推荐

  1. 20135327郭皓——信息安全系统设计基础第十周学习总结

    第十周(11.09-11.15): 学习计时:共6小时 读书: 代码: 作业: 博客: 一.学习目标 1 理解I/O代码 实践代码 cp1.c 这是一个将目标文件复制到目的文件的程序,具体如下: 1 ...

  2. 20135213——信息安全系统设计基础第十周学习总结

    第八章异常控制流 一.学习目标 1. 了解异常及其种类 2. 理解进程和并发的概念 3. 掌握进程创建和控制的系统调用及函数使用:fork,exec,wait,waitpid,exit,getpid, ...

  3. 20135203齐岳 信息安全系统设计基础第十三周学习总结

    20135203齐岳 信息安全系统设计基础第十三周学习总结 学习计时:8/9共小时(计划/实际) 读书:4/5 代码:1/1 作业:1/1 博客:2/2 第十二章 并发编程 一.学习目标 掌握三种并发 ...

  4. # 20155337 2017-2018-1 《信息安全系统设计基础》第一周学习总结

    20155337 2017-2018-1 <信息安全系统设计基础>第一周学习总结 教材学习内容总结 1.1信息就是位+上下文 hello.c程序是以字节序列的方式储存在文件中的.每个字节都 ...

  5. 20145227《信息安全系统设计基础》第一周学习总结

    20145227<信息安全系统设计基础>第一周学习总结 学习内容总结 Linux是一个操作系统.如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命 ...

  6. 2017-2018-1 20155227 《信息安全系统设计基础》第一周学习总结

    2017-2018-1 20155227 <信息安全系统设计基础>第一周学习总结 教材学习内容总结 快速浏览一遍教材,课本每章提出至少一个自己不懂的或最想解决的问题并在期末回答这些问题 一 ...

  7. # 2017-2018-1 20155224 《信息安全系统设计基础》第九周学习总结

    2017-2018-1 20155224 <信息安全系统设计基础>第九周学习总结 教材学习内容总结 存储器 随机访问存储器(RAM): 静态RAM:用来作为高速缓存存储器,每个位存储在一个 ...

  8. 2018-2019-1 20165206 《信息安全系统设计基础》第九周学习总结

    - 2018-2019-1 20165206 <信息安全系统设计基础>第九周学习总结 - 教材学习内容总结 计算机系统的主存被组织成一个由M个连续的字节大小的单元组成的数组.每个字节都有一 ...

  9. 20135219洪韶武——信息安全系统设计基础第五周学习总结

    信息安全系统设计基础第五周学习总结 学习任务:教材第四章[处理器体系结构] 学习时间:10小时  一.教材知识点梳理[4.1-4.3] 1.ISA[指令集体系结构] 一个处理器支持的指令和指令的字节级 ...

最新文章

  1. 常考题 | IoU 计算
  2. RHEL7恢复root密码
  3. 事务例子_Redis事务系列之一Redis事务详解
  4. Leo的AR代码学习之create-react-class
  5. uniapp返回上一页_一例万级写入并发,百亿级数据,毫秒级返回架构分享
  6. 有道词典Linux版下载安装
  7. java中怎么把两个JTextfield中的数字相加的值放到另一个JTextfield?_如何将jtextfield中的值解析为整数并对其执行一些数学操作?...
  8. 二维数组的空间复杂度_剑指 offer 面试题精选图解 04 . 二维数组中的查找
  9. dudu注意:这个可能是bug吧?
  10. 计算机综合布线课程,综合布线工程课程教与学(教学大纲)
  11. STM32F4应用-串口通信
  12. Android参考之代号、标签和版本号
  13. uniapp 生成商品海报并分享保存
  14. “华为杯”山东理工大学第十一届ACM程序设计竞赛(正式赛)
  15. matlab2020 安装MinGW-w64 C/C++编译器下载和安装【亲测有用】
  16. 1300:鸡蛋的硬度
  17. 你见过的最全面的Python重点知识总结
  18. FindNextFile函数
  19. 计算机系统基础实验报告
  20. BT种子破案,FBI可以,你也行

热门文章

  1. netty之ObjectSizeEstimator
  2. 毕业5年决定你的一生_2
  3. gcc与gdb,函数小结
  4. 谈谈“个人电子信息”的保护
  5. 使用 Web 标准生成 ASP.NET 2.0 Web 站点
  6. mysql binlog的查询
  7. 梯度下降原理及Python实现
  8. Postgres多版本控制
  9. Linux内核源代码分析-第三章 内核体系结构概述-3
  10. 总结八个好用的Python爬虫技巧