/* cblnum - automated COBOL line numberer * (C) 2005 Andrew Kesterson andrew@aklabs.net * Possible room for improvement here: use getopt() or * similar to allow the user to specify the step in the * line numbering. */ #include #define LINESIZE 4096 int getline(FILE *fp, char *buff) { if ( fp != NULL && buff != NULL ) { int i = 0; int ws = 0; char c = 0x00; c = fgetc(fp); while ( !feof(fp) ) { if ( (c == ' ' || c == '\t') && i == 0 && ws <= 8) { /* skip leading whitespace for editors * that autoindent like slickedit for * cobol -- but don't strip >8 */ ws++; c = fgetc(fp); continue; } else if ( c != '\n' && i < LINESIZE ) { buff[i] = c; } else if ( i >= LINESIZE ) { fprintf(stderr, "buff was too small for line from file.\n"); return -1; } else break; i++; c = fgetc(fp); } return i; } else return -1; } int main(int argc, char **argv) { if ( argc < 2 ) { printf("(C)Andrew Kesterson andrew@aklabs.net 2005\n"); printf("\tcblnum (input file) \n\toutputs to stdout\n"); return 0; } char *buffer = (char *)malloc(LINESIZE); // should be big enough for most.. if ( !buffer ) { fprintf(stderr, "cblnum: Out of memory.\n"); return -1; } FILE *fp = fopen(argv[1], "r"); if ( !fp ) { fprintf(stderr, "cblnum: Couldn't open file for reading.\n"); return -1; } int line = 100; while (!feof(fp)) { if ( getline(fp, buffer) > 1 ) { // the >1 is an ugly hack to account for junk coming // up at EOF, but I don't think any cobol statements // could be just 1 char long..... printf("%06d %s\n", line, buffer); line += 100; memset(buffer, 0x00, LINESIZE); } } fclose(fp); return 0; }