mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:32:46 +00:00 
			
		
		
		
	Add stdin support to wc program
This commit is contained in:
		
							parent
							
								
									8b0d530584
								
							
						
					
					
						commit
						bc23db2c71
					
				
					 1 changed files with 43 additions and 27 deletions
				
			
		|  | @ -16,6 +16,8 @@ struct Count { | |||
|     unsigned long lines = 0; | ||||
| }; | ||||
| 
 | ||||
| int wc(const String& filename, Vector<Count>& counts); | ||||
| 
 | ||||
| void report(const Count& count) | ||||
| { | ||||
|     if (output_lines) { | ||||
|  | @ -93,36 +95,50 @@ int main(int argc, char** argv) | |||
|     } | ||||
| 
 | ||||
|     Vector<String> files = args.get_single_values(); | ||||
|     if (files.is_empty()) { | ||||
|         fprintf(stderr, "wc: No files provided\n"); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     Vector<Count> counts; | ||||
|     for (const auto& f : files) { | ||||
|         FILE* fp = fopen(f.characters(), "r"); | ||||
|         if (fp == nullptr) { | ||||
|             fprintf(stderr, "wc: Could not open file '%s'\n", f.characters()); | ||||
|             return 1; | ||||
|         } | ||||
| 
 | ||||
|         Count count { f }; | ||||
|         char* line = nullptr; | ||||
|         size_t len = 0; | ||||
|         ssize_t n_read = 0; | ||||
|         while ((n_read = getline(&line, &len, fp)) != -1) { | ||||
|             count.lines++; | ||||
|             count.words += count_words(line); | ||||
|             count.chars += n_read; | ||||
|         } | ||||
| 
 | ||||
|         counts.append(count); | ||||
|         fclose(fp); | ||||
|         if (line) { | ||||
|             free(line); | ||||
|     int status; | ||||
|     if (files.is_empty()) { | ||||
|         status = wc("", counts); | ||||
|         if (status != 0) | ||||
|             return status; | ||||
|     } else { | ||||
|         for (const auto& f : files) { | ||||
|             status = wc(f, counts); | ||||
|             if (status != 0) | ||||
|                 return status; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     report(counts); | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| int wc(const String& filename, Vector<Count>& counts) | ||||
| { | ||||
|     FILE* fp = nullptr; | ||||
|     if (filename == "" || filename == "-") { | ||||
|         fp = stdin; | ||||
|     } else { | ||||
|         fp = fopen(filename.characters(), "r"); | ||||
|         if (fp == nullptr) { | ||||
|             fprintf(stderr, "wc: Could not open file '%s'\n", filename.characters()); | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     Count count { filename }; | ||||
|     char* line = nullptr; | ||||
|     size_t len = 0; | ||||
|     ssize_t n_read = 0; | ||||
|     while ((n_read = getline(&line, &len, fp)) != -1) { | ||||
|         count.lines++; | ||||
|         count.words += count_words(line); | ||||
|         count.chars += n_read; | ||||
|     } | ||||
| 
 | ||||
|     counts.append(count); | ||||
|     fclose(fp); | ||||
|     if (line) { | ||||
|         free(line); | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tim Morgan
						Tim Morgan