/* file_cut.c COPYRIGHT (C) 1995-2002 Norbert H. Doerry This program reads in an input file, then writes the file to an output file after skipping a number of initial characters. SYNTAX: FILE_CUT INFILE OUTFILE -cX -hXX -lX -sXXXXX -nXX or FILE_CUT INFILE -oOUTFILE -cX -hXX -lX -sXXXXX -nXX INFILE: Name of the input filename, if not specified use STDIN OUTFILE: Name of the output filename, if not specified use STDOUT -cX: Start copying after this ascii character is reached -hXX: Start copying after this Hex character is reached -lX: Start copying after X "newline" characters are read in -sXXXXX: Start copying after this string is reached -nXX : Start copying after this many characters are read in Note, only one of the start parameters can be specified. Version 1.0 18 July 1995 Version 1.1 22 June 2002: Added GNU License Modified print_syntax to take a FILE * as argument Modified calls to print_syntax Added -oOption This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. If you find any bugs in this program, please email me: doerry@aol.com */ #define VERSION "1.1" #include #include #include #include typedef struct file_cut { char infile[256]; FILE *in; char outfile[256]; FILE *out; int start_type; /* PAR_C, PAR_H, PAR_L, PAR_S PAR_N PAR_UNDEFINED */ char start_char; long start_line; char *start_string; long start_char_nbr; int debug; } FILE_CUT; #define PAR_UNDEFINED 0 #define PAR_C 1 #define PAR_H 2 #define PAR_L 3 #define PAR_S 4 #define PAR_N 5 void init_file_cut(FILE_CUT *); int read_command_line(int,char **,FILE_CUT *); int skip_chars(FILE_CUT *); void copy_chars(FILE_CUT *); void print_syntax(FILE *); void print_debug(FILE_CUT *); int main(int argc, char **argv) { FILE_CUT fc; init_file_cut(&fc); if (read_command_line(argc,argv,&fc)) { if (fc.debug) print_debug(&fc); exit(EXIT_FAILURE); } if (fc.debug) print_debug(&fc); if (skip_chars(&fc) == 0) copy_chars(&fc); if (fc.in != stdin) fclose(fc.in); if (fc.out != stdout) fclose (fc.out); return EXIT_SUCCESS; } void init_file_cut(FILE_CUT *pfc) { pfc->infile[0] = (char) NULL; pfc->in = (FILE *) NULL; pfc->outfile[0] = (char) NULL; pfc->out = (FILE *) NULL; pfc->start_type = PAR_UNDEFINED; pfc->start_char = (char) NULL; pfc->start_line = 0l; pfc->start_string = (char *) NULL; pfc->start_char_nbr = 0l; pfc->debug = 0; } int read_command_line(int argc,char **argv,FILE_CUT *pfc) { int i; int hb,lb; char ch; int slen; for (i=1;istart_type == PAR_UNDEFINED) { pfc->start_char = argv[i][2]; pfc->start_type = PAR_C; } else { fprintf(stderr,"\n ***ERROR: Only one start parameter allowed\n"); print_syntax(stderr); return 1; } } else if (tolower(argv[i][1]) == 'h') { if (pfc->start_type == PAR_UNDEFINED) { hb = lb = 0; /* initialize high and low bytes to zero */ if (isxdigit((int) argv[i][2])) /* convert the first byte */ { ch = (char) tolower(argv[i][2]); lb = (ch >= 'a' && ch <= 'f') ? (int) (ch - 'a' + 10) : (int) (ch - '0') ; if(isxdigit((int) argv[i][3])) /* convert the second byte */ { hb = lb; ch = (char) tolower(argv[i][3]); lb = (ch >= 'a' && ch <= 'f') ? (int) (ch - 'a' + 10) : (int) (ch - '0') ; } } pfc->start_char = (char) (hb*16 + lb); pfc->start_type = PAR_H; } else { fprintf(stderr,"\n ***ERROR: Only one start parameter allowed\n"); print_syntax(stderr); return 1; } } else if (tolower(argv[i][1]) == 'l') { if (pfc->start_type == PAR_UNDEFINED) { pfc->start_line = atol(argv[i]+2); pfc->start_type = PAR_L; } else { fprintf(stderr,"\n ***ERROR: Only one start parameter allowed\n"); print_syntax(stderr); return 1; } } else if (tolower(argv[i][1]) == 's') { if (pfc->start_type == PAR_UNDEFINED) { slen = strlen(argv[i] + 2); if (slen == 0) { fprintf(stderr,"\n ***ERROR: No string argument provided\n"); print_syntax(stderr); return 1; } pfc->start_string = (char *) calloc((size_t) slen+1,sizeof(char)); if (pfc->start_string == (char *) NULL) { fprintf(stderr,"\n ***ERROR: Out of Memory\n"); return 1; } strcpy(pfc->start_string,argv[i]+2); pfc->start_type = PAR_S; } else { fprintf(stderr,"\n ***ERROR: Only one start parameter allowed\n"); print_syntax(stderr); return 1; } } else if (tolower(argv[i][1]) == 'n') { if (pfc->start_type == PAR_UNDEFINED) { pfc->start_char_nbr = atol(argv[i]+2); pfc->start_type = PAR_N; } else { fprintf(stderr,"\n ***ERROR: Only one start parameter allowed\n"); print_syntax(stderr); return 1; } } else if (tolower(argv[i][1]) == 'd') /* set the debug flag */ pfc->debug = 1; else if (argv[i][1] == '?') /* print help: syntax */ { print_syntax(stdout); exit(EXIT_SUCCESS); } else if (argv[i][1] == 'o') /* output file */ { strcpy(pfc->outfile,argv[i]+2); pfc->out = fopen(pfc->outfile,"wb"); if (pfc->out == (FILE *) NULL) { fprintf(stderr,"\n ***ERROR: Unable to open OUTPUT file %s\n", pfc->outfile); return 1; } } else { fprintf(stderr,"\n ***ERROR: Illegal Parameter -%c\n",argv[i][1]); print_syntax(stderr); return 1; } } /* skip blank arguments */ else if (argv[i][0] == (char) NULL) continue; /* otherwise its a filename */ else if (pfc->in == (FILE *) NULL) { strcpy(pfc->infile,argv[i]); pfc->in = fopen(pfc->infile,"rb"); if (pfc->in == (FILE *) NULL) { fprintf(stderr,"\n ***ERROR: Unable to open INPUT file %s\n", pfc->infile); return 1; } } else if (pfc->out == (FILE *) NULL) { strcpy(pfc->outfile,argv[i]); pfc->out = fopen(pfc->outfile,"wb"); if (pfc->out == (FILE *) NULL) { fprintf(stderr,"\n ***ERROR: Unable to open OUTPUT file %s\n", pfc->outfile); return 1; } } else { print_syntax(stderr); return 1; } } if (pfc->infile[0] == (char) NULL || pfc->in == (FILE *) NULL) pfc->in = stdin; if (pfc->outfile[0] == (char) NULL || pfc->out == (FILE *) NULL) pfc->out = stdout; return 0; } int skip_chars(FILE_CUT *pfc) { char ch; long lcnt = 0; if (pfc->start_type == PAR_UNDEFINED) return 0; /* no skipping required */ if (pfc->start_line <= 0 && pfc->start_type == PAR_L) return 0; /* no skipping required */ while (feof(pfc->in) == 0) { ch = (char) fgetc(pfc->in); if (pfc->start_type == PAR_C || pfc->start_type == PAR_H) { if (ch != pfc->start_char) continue; else return 0; } else if (pfc->start_type == PAR_S) { if (ch == pfc->start_string[lcnt]) lcnt++; else lcnt = 0; if (lcnt == (long) strlen(pfc->start_string)) return 0; else continue; } else if (pfc->start_type == PAR_L) { if (ch == '\n') lcnt++; if (lcnt == pfc->start_line) return 0; else continue; } else if (pfc->start_type == PAR_N) { lcnt++; if (lcnt == pfc->start_char_nbr) return 0; else continue; } } return 1; } void copy_chars(FILE_CUT *pfc) { while (feof(pfc->in) == 0) { fputc(fgetc(pfc->in),pfc->out); } } void print_syntax(FILE *out) { fprintf(out,"\n FILE_CUT version %s of %s\n",VERSION,__DATE__); fprintf(out," COPYRIGHT (C) 1995-2002 Norbert H. Doerry\n"); fprintf(out, "\nSYNTAX: FILE_CUT [INFILE] [OUTFILE] [-cX -hXX -lX -sXXXXX -nX]\n"); fprintf(out," INFILE = Input Filename (STDIN if omitted)\n"); fprintf(out," OUTFILE = Output Filename (STDOUT if omitted)\n"); fprintf(out," -cX = Copy after ASCII char X is reached\n"); fprintf(out," -hXX = Copy after Hex char XX is reached\n"); fprintf(out," -lX = Copy after X newline chars reached\n"); fprintf(out," -sXXXXX = Copy after string XXXXX is reached\n"); fprintf(out," -nX = Copy after reading in X chars\n\n"); } void print_debug(FILE_CUT *pfc) { fprintf(stderr,"\n FILE_CUT\n\n"); if (pfc->in == stdin) fprintf(stderr," INPUT FILE: (stdin)\n"); else if (pfc->in == (FILE *) NULL) fprintf(stderr," INPUT FILE points to NULL (%s)\n",pfc->infile); else fprintf(stderr," INPUT FILE: %s\n",pfc->infile); if (pfc->out == stdout) fprintf(stderr," OUTPUT FILE: (stdout)\n"); else if (pfc->out == (FILE *) NULL) fprintf(stderr," OUTPUT FILE points to NULL (%s)\n",pfc->outfile); else fprintf(stderr," OUTPUT FILE: %s\n",pfc->outfile); if (pfc->start_type == PAR_UNDEFINED) fprintf(stderr," No start parameters defined\n"); else if (pfc->start_type == PAR_C) fprintf(stderr," Start Character is (%c) (%2x)\n",pfc->start_char, pfc->start_char); else if (pfc->start_type == PAR_H) fprintf(stderr," Start Hex Character is (%2x)\n",pfc->start_char); else if (pfc->start_type == PAR_L) fprintf(stderr," Start Line is %li\n",pfc->start_line); else if (pfc->start_type == PAR_S) fprintf(stderr," Start String is %s\n",pfc->start_string); else if (pfc->start_type == PAR_N) fprintf(stderr," Start Char Nbr is %li\n",pfc->start_char_nbr); else fprintf(stderr," Unknown Start Type (%d)\n", pfc->start_type); }