更新时间:2022-10-04 17:20:13
1 extern int cat_main(int argc, char **argv)
2 {
3 int status = EXIT_SUCCESS;
4
5 if (argc == 1) {
6 print_file(stdin);
7 return status;
8 }
9
10 while (--argc > 0) {
11 if(!(strcmp(*++argv, "-"))) {
12 print_file(stdin);
13 } else if (print_file_by_name(*argv) == FALSE) {
14 status = EXIT_FAILURE;
15 }
16 }
17 return status;
18 }
1 extern void print_file(FILE *file)
2 {
3 fflush(stdout);
4 copyfd(fileno(file), fileno(stdout));//将file文件的内容拷贝到stdout
5 fclose(file);
6 }
V658tekiMacBook-Pro:busybox-0.60.3 neilhappy$ cat -
input--stdin
input--stdin
1 extern int print_file_by_name(char *filename)
2 {
3 struct stat statBuf;
4 int status = TRUE;
5
6 if(is_directory(filename, TRUE, &statBuf)==TRUE) {
7 error_msg("%s: Is directory", filename);
8 status = FALSE;
9 } else {
10 FILE *f = wfopen(filename, "r");
11 if(f!=NULL)
12 print_file(f);
13 else
14 status = FALSE;
15 }
16
17 return status;
18 }
1 int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
2 {
3 int status;
4 int didMalloc = 0;
5
6 if (statBuf == NULL) {
7 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
8 ++didMalloc;
9 }
10
11 if (followLinks == TRUE)
12 status = stat(fileName, statBuf);
13 else
14 status = lstat(fileName, statBuf);
15
16 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
17 status = FALSE;
18 }
19 else status = TRUE;
20
21 if (didMalloc) {
22 free(statBuf);
23 statBuf = NULL;
24 }
25 return status;
26 }