随笔分类 -  UNIX-LINUX编程实践教程->实例代码注解

摘要:一 分析 要实现一个shell,需包含3个步骤 1)读入指令 2)指令解析 3)执行指令1 从键盘读入指令 从键盘读入指令的几个要点: 1)调用getc函数等待并获取用户键盘输入。 2)每一行命令的结束符为'\n',getsline函数就是通过这个结束符来判断用户是否完成指令的输入。#include #include #include int main(){ char* cmdLine = (char*)malloc(sizeof(char)*100); char* prompt = "print your cmd >"; int i; while(1 阅读全文
posted @ 2013-07-02 11:18 布兰姥爷 阅读(525) 评论(0) 推荐(0)
摘要:一 分析 shell启动一个程序,包括以下几步: 1)从用户读入指令字符串 2)shell建立一个新进程 3)在新进程中运行指令并等待进程结束 用户如何读入指令我们就不在此探讨了,这里主要探讨如何在一个程序里启动另一个程序。二 一个程序如何运行另一个程序1 使用execvp函数来启动另一个程序 execvp()函数 找到指定路径的文件并执行该文件 头文件:#include 函数原型:int execvp(const char *file ,char * const argv []); 参数: file 可执行文件的路径+文件名 argv 参数组 返... 阅读全文
posted @ 2013-06-19 10:41 布兰姥爷 阅读(4527) 评论(0) 推荐(0)
摘要:一 问题 设置回显位的状态,命令行参数为y则开启,否则关闭。二 分析 标准输入的文件描述符为0. 使用tcgetattr()函数和termios结构体可获得标准输入的属性。 使用tcsetattr()函数和termios结构体可以将更改后的属性设置重新写回标准输入。三 实现#include <stdio.h>#include <termios.h>#define oops(s,x) {perror(s);exit(x)};main(int ac,char *av[]){ struct termios info; /*必须带有参数*/ if(ac == 1) { ... 阅读全文
posted @ 2013-03-06 09:45 布兰姥爷 阅读(791) 评论(0) 推荐(0)
摘要:一 问题 读取驱动设置并显示回显位的状态。二 分析 标准输入的文件描述符为0 使用tcgetattr()函数和termios结构体可以读取到设备的属性 回显位状态放置在termios.c_lflag中三 实现#include <stdio.h>#include <termios.h>#include <stdlib.h>main(){ struct termios info; int rv; /*读取终端设置*/ rv = tcgetattr(0,&info); if(rv == -1) { ... 阅读全文
posted @ 2013-03-05 15:31 布兰姥爷 阅读(417) 评论(0) 推荐(0)
摘要:一 问题 实现pwd功能二 分析 结合stat()函数和stat结构体,可根据目录名获得目标的i-节点号 使用chdir()函数可以改变用户的当前路径 结合readdir()函数和direntp结构体,可以根据i-节点号获得其对应的目录名称 1)获得当前目录A的i-节点号Ai 2)跳转到父目录,根据i-节点号Ai就能知道目录A的名称 3)依次倒退到根目录为止三 实现1 头文件#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#includ 阅读全文
posted @ 2013-02-27 11:16 布兰姥爷 阅读(617) 评论(0) 推荐(0)
摘要:一 问题 对ls1的功能进行扩展,以达到类似ll命令的功能。二 分析 在ls1中,我们利用readdir()函数和dirent结构体来获得目标文件夹内的文件名(dirent->d_name)。 现在我们借助函数stat()和结构体stat以及上面得到的文件名来获得该目录内文件的详细信息(参看后面的“相关函数与结构体”部分)。三 实现1 头文件#include<stdio.h>#include<sys/types.h>#include<dirent.h>#include<sys/stat.h>#include<string.h>2 阅读全文
posted @ 2013-01-29 21:40 布兰姥爷 阅读(1182) 评论(0) 推荐(0)
摘要:一 问题 实现ls的功能二 分析 1 在linux中,要查看文件夹中有什么内容,可以使用readdir函数直接读该文件夹的内容,并以结构体dirent保存该内容 2 文件夹中有两个特殊文件"."和"..",表示当前文件夹和上一文件夹 3 "ls"表明读取当前文件夹,"ls filename"表示读取指定文件夹,"ls filename1 filename2"表示读取多个指定文件夹三 实现(以读取当前文件夹为例)1 打开该文件夹# include<stdio.h># include&l 阅读全文
posted @ 2013-01-16 10:22 布兰姥爷 阅读(710) 评论(0) 推荐(0)
摘要:/* cp1.c * version 1 of cp - uses read and write with tunable buffer size * usage:cp1 src dest */#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<stdlib.h>#define BUFFERSIZE 4096#define COPYMODE 0644void oops(char *s1,char *s2);main(int ac,char *av[]){ int in_fd,o 阅读全文
posted @ 2012-12-11 10:16 布兰姥爷 阅读(367) 评论(0) 推荐(0)
摘要:/* who1.c - a first version of the who program * open,read UTMP file, and show results */#include<stdio.h>#include<utmp.h>#include<fcntl.h>#include<unistd.h>#include<stdlib.h>#define SHOWHOSTvoid show_info(struct utmp * utbufp);int main(){ struct utmp current_record; in 阅读全文
posted @ 2012-12-07 15:13 布兰姥爷 阅读(456) 评论(0) 推荐(0)
摘要:/* more01.c - version0.1 of more * read and print 24 lines then pause for a few special commands */#include<stdio.h>#include<stdlib.h>#define PAGELEN 24#define LINELEN 512void do_more(FILE*);int see_more();int main(int ac, char *av[]){ FILE *fp;//定义一个FIEL指针 if(ac==1) { //显示... 阅读全文
posted @ 2012-12-06 10:08 布兰姥爷 阅读(654) 评论(0) 推荐(1)