/* By accepting this notice, you agree to be bound by the following agreements: This software product, printline , is copyrighted (C) 2005 by Josef Maimon, New York, New York, USA, with all rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (version 2) as published by the Free Software Foundation. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License (GPL) for more details. You should have received a copy of the GNU General Public License (GPL) along with this program. */ #include #include #include #include #include #include void usage(void); void print_version(void); void convert_escape(char *); char * ProgName; #ifndef MAX_BUF #define MAX_BUF 4096 #endif /* MAX_BUF */ int main(int argc,char **argv){ char * buffer = NULL; ssize_t size; ssize_t offset = 0; unsigned readlinenum = 0; int i = 0, ch = 0, haveinfile = 0 , haveoutfile = 0, havereadlinenum = 0; int ret = 0; unsigned maxreadlinenum = 0, havemaxreadlinenum = 0, minreadlinenum = 0, haveminreadlinenum = 0; FILE * outfile = NULL, * infile = NULL; int printed_anything = 0; char * delim = ""; int havedelim = 0; int delimnewline = 1; int delimspace = 0; int trimdelim = 0; char * trimdelimch = NULL; char * delimspacech = NULL; size_t bytes_read = 0; size_t bytes_written = 0; ProgName = argv[0]; while ((ch = getopt(argc, argv, "sS:tT:d:m:x:Nn:i:o:h?v")) != EOF) switch(ch) { case 'N': delimnewline = 0; break; case 'T': trimdelimch = optarg; convert_escape(trimdelimch); break; case 't': trimdelim = 1; break; case 'S': delimspacech = optarg; convert_escape(delimspacech); break; case 's': delimspace =1; break; case 'd': delim = optarg; convert_escape(delim); havedelim = 1; break; case 'i': infile = fopen(optarg,"r"); if(!infile) { perror("Could not open file"); exit(1); } haveinfile = 1; break; case 'o': outfile = fopen(optarg,"w"); if(!outfile) { perror("Could not open file"); exit(1); } haveoutfile = 1; break; case 'n': readlinenum = atoi(optarg); havereadlinenum = 1; break; case 'x': maxreadlinenum = atoi(optarg); havemaxreadlinenum = 1; break; case 'm': minreadlinenum = atoi(optarg); haveminreadlinenum = 1; break; case 'v': print_version(); case 'h': /* Fall through */ case '?': /* Fall through */ default: usage(); } buffer = malloc(BUFSIZ); if(buffer) size = BUFSIZ; if(!haveinfile) infile = stdin; if(!haveoutfile) outfile = stdout; if(!havereadlinenum) if (argc > 1 && optind < argc) havereadlinenum = (readlinenum = atoi(argv[optind])); if (!havereadlinenum && !havemaxreadlinenum && !haveminreadlinenum && !havedelim && !trimdelim && !delimspace) goto shorter_circuit; ret = 2; while( -1 != (bytes_read = getdelim(&buffer,&size,(havedelim ? *delim : '\n'),infile)) ) { if (bytes_read == 1 && *buffer == '\0' && feof(infile) && (!havedelim || '\0' != *delim)) break; offset = 0; i++; if (!havereadlinenum && !havemaxreadlinenum && !haveminreadlinenum) goto short_circuit; if((!trimdelim && !delimspace) && !readlinenum && !maxreadlinenum && minreadlinenum && i >= minreadlinenum) { /* shortcut */ shorter_circuit: while(!feof(infile) && (bytes_read = fread(buffer,1,(size >= BUFSIZ) ? BUFSIZ : size ,infile))>0) while(!feof(outfile) && (bytes_written = fwrite(buffer,1,bytes_read,outfile)) && (bytes_read -= bytes_written)); exit(0); } if( (!readlinenum || i > readlinenum) && (!maxreadlinenum || i > maxreadlinenum) && (!minreadlinenum) ) exit(printed_anything ? 0 : 1); else if( i == readlinenum || (maxreadlinenum && minreadlinenum && i <= maxreadlinenum && i >= minreadlinenum) || (minreadlinenum && !maxreadlinenum && i >= minreadlinenum) || (maxreadlinenum && !minreadlinenum && i <= maxreadlinenum) ) { short_circuit: printed_anything = 1; if (delimspace) { int i = 2; if (delimspacech) { int j = 0; while(*(delimspacech+j)) { while(!(*(buffer+bytes_read-i) == *(delimspacech+j)) && i < bytes_read) { i++; j=-1; break; } j++; } } else { while(!isspace(*(buffer+bytes_read-i)) && i < bytes_read) i++; } if (i != bytes_read) offset = bytes_read - i + 1; } if (trimdelim) { int i = 0; if (trimdelimch) { int j = 0; while(*(trimdelimch+j)) { while(*(buffer+i) == *(trimdelimch+j) && i < bytes_read) { i++; j=-1; break; } j++; } } else while(isspace(*(buffer+i)) && i < bytes_read) i++; offset = i; } if (havedelim && *delim != '\n' && delimnewline) if (buffer[bytes_read-1] == *delim) buffer[bytes_read-1] = '\n'; fwrite(buffer+offset,1,bytes_read-offset,outfile); } } exit(printed_anything ? 0 : 1); return (0); } void usage(void){ fprintf(stderr,"Usage: %s [-st] [-S spacechars] [-T spacechars] [-d delim] [-N] [-n lineno] [-m minlineno] [-x maxlineno] [-i infile] [-o outfile] [-v] [-?h]\n",basename(ProgName)); fprintf(stderr, "OR\n" "Usage: %s N , where n is the number line to print from stdin\n",basename(ProgName)); fprintf(stderr, "\n" "-s : print from first space before delimiter\n" "-S : list of charachters to use for -s instead of whitespace\n" "-t : trim leading spaces\n" "-T : list of charachters to use for -t instead of whitespace\n" "-d delimchar : a delimiter charachter. Only the first char is used\n" "-n lineno : Number line to be printed. Must be positive\n" "-N : dont convert non newline delimiter char to newline\n" "-x maxlineno : To number line to be printed. Must be positive\n" "-m minlinno : From number line to be printed. Must be positive\n" "-i infile : File to process as input. Defaults to stdin\n" "-o outfile : File to send output. Defaults to stdout\n" "-v : Prints the version and exits\n" "-h -? : This message\n" "\n" "Line number may also be set by the first non option style argument\n" "If there is no line number specified, %s will print all its input\n",basename(ProgName)); fprintf(stderr, "To convert the delim charachter to anything other than newline, see tr(1)\n" "\n" "Escape sequences are supported as in echo(1)\n" ); exit(1); } void print_version(void){ fprintf(stderr,"%s: Version 0.8 Copyright 2005 Joe Maimon. This program is licensed under the GNU GPL v2\n",basename(ProgName)); usage(); } void convert_escape(char *s) { register char *s1 = s; char c; while ((c = *s++)) { if (c == '\\' && *s) { switch (c = *s++) { case 'a': c = '\007'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = (int) 0x0B; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': c -= '0'; if (*s >= '0' && *s <= '7') c = c * 8 + (*s++ - '0'); if (*s >= '0' && *s <= '7') c = c * 8 + (*s++ - '0'); break; case '\\': break; default: c = '\\'; break; } } *s1++ = c; } *s1 = '\0'; }