/* 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; ProgName = argv[0]; if(argc <= 1) usage(); while ((ch = getopt(argc, argv, "m:x:n:i:o:h?v")) != EOF) switch(ch) { 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(4096); if(buffer) size = 4096; if(!haveinfile) infile = stdin; if(!haveoutfile) outfile = stdout; if(!havereadlinenum && !havemaxreadlinenum && !haveminreadlinenum) readlinenum = atoi(argv[argc - 1]); ret = 2; while( -1 != (getline(&buffer,&size,infile)) ) { i++; if(!readlinenum && !maxreadlinenum && minreadlinenum && i >= minreadlinenum) { /* shortcut */ size_t bytes_read = 0; size_t bytes_written = 0; while((bytes_read = fread(buffer,1,4096,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) ) { printed_anything = 1; fputs(buffer,outfile); } } exit(printed_anything ? 0 : 1); return (0); } void usage(void){ fprintf(stderr,"Usage: %s [-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,"-n lineno : Number line to be printed. Must be positive\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"); exit(1); } void print_version(void){ fprintf(stderr,"%s: Version 0.3 Copyright 2005 Joe Maimon. This program is licensed under the GNU GPL v2\n",basename(ProgName)); usage(); }