/* By accepting this notice, you agree to be bound by the following agreements: This software product, printline , is copyrighted (C) 2003 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 void usage(void); void print_version(void); char * ProgName; #ifndef MAX_BUF #define MAX_BUF 4096 #endif /* MAX_BUF */ int main(int argc,char **argv){ char * buffer = NULL; ssize_t size; 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 = '\0'; int havedelim = 0; int delimnewline = 1; size_t bytes_read = 0; size_t bytes_written = 0; ProgName = argv[0]; while ((ch = getopt(argc, argv, "d:m:x:Nn:i:o:h?v")) != EOF) switch(ch) { case 'N': delimnewline = 0; break; case 'd': delim = optarg[0]; 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) goto shorter_circuit; ret = 2; while( -1 != (bytes_read = getdelim(&buffer,&size,(havedelim ? delim : '\n'),infile)) ) { i++; if (!havereadlinenum && !havemaxreadlinenum && !haveminreadlinenum) goto short_circuit; if(!readlinenum && !maxreadlinenum && minreadlinenum && i >= minreadlinenum) { /* shortcut */ shorter_circuit: while((bytes_read = fread(buffer,1,(size >= BUFSIZ) ? BUFSIZ : size ,infile))>0) while((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 (havedelim && delim != '\n' && delimnewline) if (buffer[bytes_read+1] == delim) buffer[bytes_read] = '\n'; fwrite(buffer,1,bytes_read,outfile); } } exit(printed_anything ? 0 : 1); return (0); } void usage(void){ fprintf(stderr,"Usage: %s [-d delim] [-N] [-n lineno] [-m minlineno] [-x maxlineno] [-i infile] [-o outfile] [-v] [-?h]\n",basename(ProgName)); fprintf(stderr,"OR\n"); fprintf(stderr,"Usage: %s N , where n is the number line to print from stdin\n",basename(ProgName)); fprintf(stderr,"-d delimchar : a delimiter charachter. Only the first char is used\n"); fprintf(stderr,"-n lineno : Number line to be printed. Must be positive\n"); fprintf(stderr,"-N : dont convert non newline delimiter char to newline\n"); fprintf(stderr,"-x maxlineno : To number line to be printed. Must be positive\n"); fprintf(stderr,"-m minlinno : From number line to be printed. Must be positive\n"); fprintf(stderr,"-i infile : File to process as input. Defaults to stdin\n"); fprintf(stderr,"-o outfile : File to send output. Defaults to stdout\n"); fprintf(stderr,"-v : Prints the version and exits\n"); fprintf(stderr,"-h -? : This message\n"); fprintf(stderr,"\n"); fprintf(stderr,"Line number may also be set by the first non option style argument\n"); fprintf(stderr,"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"); exit(1); } void print_version(void){ fprintf(stderr,"%s: Version 0.5 Copyright 2005 Joe Maimon. This program is licensced under the GNU GPL v2\n",basename(ProgName)); usage(); }