mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 13:32:45 +00:00 
			
		
		
		
	cat: Fix some oversights in error handling.
Error messages should go to stderr so they don't get mixed in with the program's output. Also added a check for the return value of write().
This commit is contained in:
		
							parent
							
								
									fe380d8f49
								
							
						
					
					
						commit
						6fa727a88e
					
				
					 1 changed files with 8 additions and 3 deletions
				
			
		|  | @ -10,7 +10,7 @@ int main(int argc, char** argv) | |||
| { | ||||
|     int fd = argc > 1 ? open(argv[1], O_RDONLY) : 0; | ||||
|     if (fd == -1) { | ||||
|         printf("failed to open %s: %s\n", argv[1], strerror(errno)); | ||||
|         fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno)); | ||||
|         return 1; | ||||
|     } | ||||
|     for (;;) { | ||||
|  | @ -19,10 +19,15 @@ int main(int argc, char** argv) | |||
|         if (nread == 0) | ||||
|             break; | ||||
|         if (nread < 0) { | ||||
|             printf("read() error: %s\n", strerror(errno)); | ||||
|             perror("read"); | ||||
|             return 2; | ||||
|         } | ||||
|         write(1, buf, nread); | ||||
|         ssize_t nwritten = write(1, buf, nread); | ||||
|         if (nwritten < 0) { | ||||
|             perror("write"); | ||||
|             return 3; | ||||
|         } | ||||
|         ASSERT(nwritten == nread); | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling