forked from bonsai/harakit
		
	cat(1p): cleanup
This commit is contained in:
		
							parent
							
								
									0c290646ec
								
							
						
					
					
						commit
						f041ae5d63
					
				
							
								
								
									
										30
									
								
								src/cat.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								src/cat.c
									
									
									
									
									
								
							| @ -20,6 +20,7 @@ | ||||
| 
 | ||||
| #include <errno.h> | ||||
| #include <sysexits.h> | ||||
| #include <sys/stat.h> | ||||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
| #include <unistd.h> | ||||
| @ -29,17 +30,18 @@ void cat(FILE *file, bool u) { | ||||
| 	int p = 0; /* index counter for bytes in buffered reading */ | ||||
| 	char buf[4096]; /* buffer for buffered reading */ | ||||
| 
 | ||||
| 	if (u) { | ||||
| 		while (byte != EOF) { | ||||
| 			byte = fgetc(file); | ||||
| 
 | ||||
| 		if (u) { | ||||
| 			putchar(byte); | ||||
| 		} | ||||
| 	} else { | ||||
| 		while (byte != EOF) { | ||||
| 			byte = fgetc(file); | ||||
| 			if (p > sizeof(buf)) { | ||||
| 				fputs(buf, stdout); | ||||
| 				p = 0; | ||||
| 			} | ||||
| 
 | ||||
| 			} else { | ||||
| 				buf[p] = byte; | ||||
| 				p += 1; | ||||
| 			} | ||||
| @ -47,8 +49,7 @@ void cat(FILE *file, bool u) { | ||||
| 
 | ||||
| 		fwrite(buf, 1, p, stdout); | ||||
| 		fflush(stdout); | ||||
| 
 | ||||
| 	if (file != stdin) { fclose(file); } | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
| @ -91,11 +92,20 @@ int main(int argc, char *argv[]) { | ||||
| 	} | ||||
| 	 | ||||
| 	FILE *file; | ||||
| 	struct stat stats; | ||||
| 
 | ||||
| 	for (i = optind; i < argc; i++) { | ||||
| 		if (argv[i][0] == '-' && argv[i][1] != '\0') { | ||||
| 			continue; | ||||
| 		} else if (argv[i][0] == '-' && argv[i][1] == '\0') { | ||||
| 		if (argv[i][0] == '-') { | ||||
| 			switch (argv[i][1]) { | ||||
| 				case '\0': | ||||
| 					file = stdin; | ||||
| 					break; | ||||
| 				default: | ||||
| 					continue; | ||||
| 			} | ||||
| 		} else if (stat(argv[i], &stats) == 0 && S_ISDIR(stats.st_mode)) { | ||||
| 			fprintf(stderr, "%s: %s: Is a directory.\n", argv[0], argv[i]); | ||||
| 			return EX_NOINPUT; | ||||
| 		} else if ((file = fopen(argv[i], "r")) == NULL) { | ||||
| 			switch (errno) { | ||||
| 				case EACCES: | ||||
| @ -118,7 +128,9 @@ int main(int argc, char *argv[]) { | ||||
| 					return EX_UNAVAILABLE; | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		cat(file, u); | ||||
| 		if (file != stdin) { fclose(file); } | ||||
| 	} | ||||
| 
 | ||||
| 	return EX_OK; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user