-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
40 lines (31 loc) · 774 Bytes
/
cat.c
File metadata and controls
40 lines (31 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* **************************
* The cat command of shell
* *************************/
#include "main.h"
int cats(const char *p)
{
char path[PATH_SIZE];
char *start;
char *end;
char buf[PATH_SIZE];
FILE *fp;
memset(path,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @
start = strchr(p,' ');
end = strchr(p,'\n');
if(!start || !end)
{
printf("can't support this format \n");
return 1;
}
strncpy(path,p+4,end-start-1); // get the path in inputting command
fp=fopen(path,"r"); // opne the file
if(NULL==fp)
{
printf("can't opne file:%s \n",path);
return 1;
}
while(NULL != fgets(buf,PATH_SIZE,fp)) // get the content of file ,and show them
printf("%s",buf);
fclose(fp); // close the file,after using it
return 0;
}