今天我们不讲什么新内容,来用以前的知识进行应用,做一些小练习。
1.shell的制作:
这就是自制的shell终端:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<sys/wait.h>
4 #include<string.h>
5 #include<unistd.h>
6 #include<ctype.h> //这都是需要用的头文件。
7
8 #define MAXLINE 1024 //宏定义。
9
10 char cmdline[MAXLINE+1]; //定义一个字符数组。
11 char *argv[8];
12 int argc;
13 void Init() //初始化各个值,并对数组清空。
14 {
15 argc=0;
16 memset(cmdline,0x00,sizeof(cmdline));
17 }
18
19 int read_cmd() //读命令函数。
20 {
21 return fgets(cmdline,MAXLINE,stdin)==NULL ? 0:1; //没读到返回0.
22 }
23 void parse_cmd() //解析命令函数。
25 int flag=0; //定义一个标志位。
26 int i=0;
27 for(i=0;cmdline[i]!='\0';i++) //循环所输入的命令的各个字符。
28 {
29 if(flag==0 && !(isspace(cmdline[i]))){ //如果标志位为0或者数组不为空。
30 flag=1;
31 argv[argc]=cmdline+i; //则先置成1,再将这个字符的地址赋给命令行参数。
32 argc++; //字符向后走一位。
33 }
34 else if(isspace(cmdline[i])){ //如果为空,则置成0,并退出循环。
35 flag=0;
36 cmdline[i]='\0';
37 }
38 }
39 argv[argc]=NULL; //最后给命令行参数的最后一项置成空。
40 }
41
42 void execut_cmd() //执行命令函数。
43 {
44 if(fork()==0){ //在子进程里执行替换命令,子进程死亡之后,父进程在收尸。
45 execvp(argv[0],argv);
46 exit(1);
47 }
48 wait(NULL); //防止僵尸进程。
49
50 }
51 void print_cmd() //打印函数。
52 {
53 int i;
54 printf("argc=%d\n",argc); //打印出有几个命令参数。
55 for(i=0;i<argc;i++)
56 {
57 printf("\targv[%d]=%s\n",i,argv[i]); //参数分别是什么。
58 }
59 }
60 int main(void) //主函数运行即可。
61 {
62 while(1){
63 Init();
64 printf("shell > ");
65 if(read_cmd()==0)break;
66 parse_cmd();
67 print_cmd();
68 execut_cmd();
69 }
70 }
运行结果为:
[ymk@localhost d7]$ ./my
shell > ls -l
argc=2
argv[0]=ls
argv[1]=-l
total 120
-rwxrwxr-x. 1 ymk ymk 9216 Jul 27 18:06 a.out
-rwxrwxr-x. 1 ymk ymk 9216 Jul 28 00:11 my
-rw-rw-r--. 1 ymk ymk 990 Jul 28 00:12 myshell.c
-rwxrwxr-x. 1 ymk ymk 9032 Jul 27 18:34 s
-rwxrwxr-x. 1 ymk ymk 9160 Jul 27 18:44 s1
-rwxrwxr-x. 1 ymk ymk 13496 Jul 27 18:54 s2
-rwxrwxr-x. 1 ymk ymk 13496 Jul 27 18:52 -s2
-rwxrwxr-x. 1 ymk ymk 13576 Jul 27 20:12 s3
-rw-rw-r--. 1 ymk ymk 947 Jul 28 00:11 shell.c
-rw-rw-r--. 1 ymk ymk 1569 Jul 27 18:44 stu1.c
-rw-rw-r--. 1 ymk ymk 2299 Jul 27 20:20 stu2.c
-rw-rw-r--. 1 ymk ymk 990 Jul 28 00:26 stu.c
-rw-rw-r--. 1 ymk ymk 44 Jul 27 20:13 stu.dat
2.学生管理系统:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<assert.h>
5
6 typedef struct stu_{ //定义一个有关学生信息的结构体。
7 char name[32];
8 int no;
9 char sex[5];
10 }stu_t;
11
12 typedef struct node_{ //关于学生信息的链表。
13 stu_t stu;
14 struct node_ *next;
15 }node_t;
16
17 void List_insert(node_t **head) //普通的单链表头插。
18 {
19 node_t *p=malloc(sizeof(node_t));
20 assert(head);
21 p->next=NULL;
22 printf("name: ");
23 scanf("%s",p->stu.name);
24 printf("no: ");
25 scanf("%d",&p->stu.no);
26 printf("sex: ");
27 scanf("%s",p->stu.sex);
28
29 if(*head==NULL){
30 *head=p;
31 }
32 else{
33 p->next=*head;
34 *head=p;
35 }
36 }
37
38 void List_show(node_t *head) //展示学生信息。
39 {
40 while(head!=NULL)
41 {
42 printf("no=%d,name=%s,sex=%s\n",head->stu.no,head->stu.n ame,head->stu.sex);
43 head=head->next;
44 }
45 printf("\n");
46 }
47
48 void List_destroy(node_t **head) //销毁信息。
49 {
50 assert(head);
51 node_t *cur=*head;
52 while(cur!=NULL)
53 {
54 *head=cur->next;
55 free(cur);
56 cur=*head;
57 }
58 }
59 int menu() //菜单。
60 {
61 printf("+++++++++++++++++++++++++\n");
62 printf("++++ Welcome ++++\n");
63 printf("++++1.add 2.find++++\n");
64 printf("++++0.exit 3.save++++\n");
65 printf("+++++++++++++++++++++++++\n");
66 printf("+++++++++++++++++++++++++\n");
67 printf(">");
68 int choose;
69 scanf("%d",&choose);
70 return choose;
71 }
72 void save(node_t *head) //储存。
73 {
74 FILE *fp=fopen("stu.dat","w"); //打开文件,权限为可写。
75 if(fp==NULL)perror("fopen"),exit(1);
76
77 node_t *cur=head;
78 while(cur!=NULL)
79 {
80 fwrite(&(cur->stu),sizeof(stu_t),1,fp);
81 cur=cur->next;
82 }
83 fclose(fp);
84 }
85 int main(void)
86 {
87 node_t *head=NULL;
88 int choose=0;
89 do{
90 choose=menu(); //主函数通过switch来决定操作。
91 switch(choose){
92 case 1:
93 List_insert(&head);
94 break;
95 case 2:
96 List_show(head);
97 break;
98 case 3:
99 save(head);
100 break;
101 case 0:
102 save(head);
103 printf("Thanks!\n");
104 exit(0);
105 default:
106 choose=1;
107 break;
108 }
109 }while(choose!=0);
110 }
运行结果:
[ymk@localhost d7]$ ./s2
+++++++++++++++++++++++++
++++ Welcome ++++
++++1.add 2.find++++
++++0.exit 3.save++++
+++++++++++++++++++++++++
+++++++++++++++++++++++++
>1
name: yu
no: 1
sex: nan
+++++++++++++++++++++++++
++++ Welcome ++++
++++1.add 2.find++++
++++0.exit 3.save++++
+++++++++++++++++++++++++
+++++++++++++++++++++++++
>2
no=1,name=yu,sex=nan
+++++++++++++++++++++++++
++++ Welcome ++++
++++1.add 2.find++++
++++0.exit 3.save++++
+++++++++++++++++++++++++
+++++++++++++++++++++++++
>0
Thanks!
[ymk@localhost d7]$ od -c stu.dat
0000000 y u \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000040 001 \0 \0 \0 n a n \0 \0 \0 \0 \0
0000054
[ymk@localhost d7]$ ls -l stu.dat
-rw-rw-r--. 1 ymk ymk 44 Jul 28 00:39 stu.dat
这个系统可以实现添加,查看以及保存的小功能。并且在stu.dat文件里也能查看到保存的信息。