getopt
This commit is contained in:
		
							
								
								
									
										20
									
								
								tee.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								tee.c
									
									
									
									
									
								
							@@ -2,29 +2,35 @@
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	bool aflag = false;
 | 
			
		||||
	char buf[BUFSIZ];
 | 
			
		||||
	char buf[BUFSIZ], c;
 | 
			
		||||
	int i, nfps = 1;
 | 
			
		||||
	size_t n;
 | 
			
		||||
	FILE **fps;
 | 
			
		||||
 | 
			
		||||
	if(argc > 1 && !strcmp(argv[1], "-a"))
 | 
			
		||||
		aflag = true;
 | 
			
		||||
	while((c = getopt(argc, argv, "a")) != -1)
 | 
			
		||||
		switch(c) {
 | 
			
		||||
		case 'a':
 | 
			
		||||
			aflag = true;
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			exit(EXIT_FAILURE);
 | 
			
		||||
		}
 | 
			
		||||
	if(!(fps = malloc(sizeof *fps)))
 | 
			
		||||
		eprintf("malloc:");
 | 
			
		||||
	fps[nfps-1] = stdout;
 | 
			
		||||
 | 
			
		||||
	for(i = aflag ? 2 : 1; i < argc; i++) {
 | 
			
		||||
	for(; optind < argc; optind++) {
 | 
			
		||||
		if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
 | 
			
		||||
			eprintf("realloc:");
 | 
			
		||||
		if(!(fps[nfps-1] = fopen(argv[i], aflag ? "a" : "w")))
 | 
			
		||||
			eprintf("fopen %s:", argv[i]);
 | 
			
		||||
		if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w")))
 | 
			
		||||
			eprintf("fopen %s:", argv[optind]);
 | 
			
		||||
	}
 | 
			
		||||
	while((n = fread(buf, 1, sizeof buf, stdin)) > 0)
 | 
			
		||||
		for(i = 0; i < nfps; i++)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user