forked from bonsai/harakit
		
	trincat(1): removed file
This commit is contained in:
		
							parent
							
								
									c8d6e9f5e4
								
							
						
					
					
						commit
						27a227e365
					
				
							
								
								
									
										78
									
								
								trincat.c
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								trincat.c
									
									
									
									
									
								
							@ -1,78 +0,0 @@
 | 
				
			|||||||
#include <errno.h> /* maybe */
 | 
					 | 
				
			||||||
#include <fcntl.h> /* no */
 | 
					 | 
				
			||||||
#include <sysexits.h> /* sure */
 | 
					 | 
				
			||||||
#include <stdbool.h> /* no */
 | 
					 | 
				
			||||||
#include <stdio.h> /* yes */
 | 
					 | 
				
			||||||
#include <stdlib.h> /* yes */
 | 
					 | 
				
			||||||
#include <string.h> /* maybe */
 | 
					 | 
				
			||||||
#include <unistd.h> /* yes */
 | 
					 | 
				
			||||||
#define BUF_SIZE 4096
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
unsigned char u = 0; /* unbuffered */
 | 
					 | 
				
			||||||
unsigned char buf[BUF_SIZE];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void usage(char *argv0){
 | 
					 | 
				
			||||||
	fprintf(stderr, "Usage: %s (-u) (file...)\n", argv0);
 | 
					 | 
				
			||||||
	exit(EX_USAGE);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int main(int argc, char *argv[]) {
 | 
					 | 
				
			||||||
	char *argv0;
 | 
					 | 
				
			||||||
	extern int optind;
 | 
					 | 
				
			||||||
	int c;
 | 
					 | 
				
			||||||
	FILE *in;
 | 
					 | 
				
			||||||
	argv0 = argv[0];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	while ((c = getopt(argc, argv, "u")) != -1) {
 | 
					 | 
				
			||||||
			switch(c) {
 | 
					 | 
				
			||||||
					/*
 | 
					 | 
				
			||||||
					 * From cat(1p):
 | 
					 | 
				
			||||||
					 *
 | 
					 | 
				
			||||||
					 * -u        Write bytes from the input file to the standard output
 | 
					 | 
				
			||||||
					 *           without delay as each is read.
 | 
					 | 
				
			||||||
					 */
 | 
					 | 
				
			||||||
					case 'u':
 | 
					 | 
				
			||||||
						u = 1;
 | 
					 | 
				
			||||||
						break;
 | 
					 | 
				
			||||||
					default:
 | 
					 | 
				
			||||||
						usage(argv[0]);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	while(++argv != NULL) /* increments over argv[0] */
 | 
					 | 
				
			||||||
		/*
 | 
					 | 
				
			||||||
			 * From cat(1p):
 | 
					 | 
				
			||||||
			 *
 | 
					 | 
				
			||||||
			 * file      A pathname of an input file. If no file operands are
 | 
					 | 
				
			||||||
			 *           specified, the standard input shall be used. If a file is
 | 
					 | 
				
			||||||
			 *           '-', the cat utility shall read from the standard input at
 | 
					 | 
				
			||||||
			 *           that point in the sequence. The cat utility shall not close
 | 
					 | 
				
			||||||
			 *           and reopen standard input when it is referenced in this way,
 | 
					 | 
				
			||||||
			 *           but shall accept multiple occurrences of '-' as a file
 | 
					 | 
				
			||||||
			 *           operand.
 | 
					 | 
				
			||||||
			 */
 | 
					 | 
				
			||||||
	if(argv[0][0] == '-' && argv[0][1] == '\0') in = stdin;
 | 
					 | 
				
			||||||
					else if ((in = fopen(argv[0], "r")) == NULL) {
 | 
					 | 
				
			||||||
							// TODO: Add error handling
 | 
					 | 
				
			||||||
							usage(argv0);
 | 
					 | 
				
			||||||
							return EX_NOINPUT;
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					int byte = 0;
 | 
					 | 
				
			||||||
					char *buf = calloc(4096, 1);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					while ((c = fgetc(in) != EOF)) {
 | 
					 | 
				
			||||||
							if (u) {
 | 
					 | 
				
			||||||
									putchar(c);
 | 
					 | 
				
			||||||
							} else {
 | 
					 | 
				
			||||||
									/* fucky wucky buffering */
 | 
					 | 
				
			||||||
							}
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					if(!u) fputs(buf, stdout);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	        if(in != stdin) fclose(file); /* who cares if it cant close */
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return EX_OK;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user