/* maxln.c - program to check max line length in source files */ /* Copyright (c) 1998 by Ira E McDonald (High North Inc) */ /* NOTE -- Please read full description at 'strip_synopsis[]' */ /* later in this program source file. */ /* NOTE -- This program uses strictly ISO 9899:1990 (ISO C) */ /* compliant header files, functions, and statements. */ /* However, this program declares BOTH strictly ISO C */ /* compliant fully specified function prototypes AND */ /* traditional C function declarations for greater */ /* portability to older systems (eg, SunOS). */ /* NOTE -- This program compiles warning free in strict ISO C. */ /* Standard Header Files */ #include #include #include #include #include /* Standard Logicals for C (Pascal Style) */ /* NOTE -- The specific intent behind the use of the 'Standard */ /* Logicals for C' in a program is to ensure: 1) the */ /* appearance of an equal sign ('=') ALWAYS indicates */ /* an assignment operation; 2) the appearance of an */ /* ampersand ('&') ALWAYS indicates EITHER a bitwise */ /* AND or a Reference operation; and 3) the appearance */ /* of a vertical bar ('|') ALWAYS indicates a bitwise */ /* OR operation. */ /* NOTE -- For strict compliance with ISO 9899 AM1:1994 (ISO C) */ /* the following logicals should be in lowercase, */ /* as per the header file 'iso646.h'. */ #ifdef __STDC__ #undef LT #undef GT #undef LE #undef GE #undef EQ #undef NE #undef AND #undef OR #undef NOT #undef TRUE #undef FALSE #endif #define LT < #define GT > #define LE <= #define GE >= #define EQ == #define NE != #define AND && #define OR || #define NOT ! #define TRUE 1 #define FALSE 0 /* Standard Simple Types for C */ /* NOTE -- The specific intent behind the use of the 'Standard */ /* Simple Types for C' is to ensure: 1) the maximal */ /* clarity of intended program behavior; and 2) the use */ /* of a single token for any simple type declaration. */ /* NOTE -- For strict compliance with the 'Clean C' dialect, */ /* compatible with both ISO C and ISO C++ (draft) and */ /* described in 'C: A Reference Manual' by Harbison */ /* and Steele (4th Edition, January 1995), the simple */ /* type 'boolean' should be respelled 'bool'. */ #ifdef __STDC__ #undef uchar #undef ushort #undef uint #undef ulong #undef boolean #endif #define uchar unsigned char #define ushort unsigned short #define uint unsigned int #define ulong unsigned long #define boolean unsigned char /*********************************************************************/ /* Global constants */ /*********************************************************************/ #define FMAX 250 /* file max line length */ /*********************************************************************/ /* Global variables */ /*********************************************************************/ static char *maxln_synopsis[] = { /* 'maxln_synopsis' */ "Usage: maxln [-nn] [-v] filename ...", "", " -nn Fence column - longest desired line length", " (decimal number, defaults to column 72)", " -v Verbose log output mode", " (HINT - redirect verbose log output to a file)", "", "Ex: maxln -72 -v myfile.txt >myfile.log", " (fence in column 72,", " verbose log output to file 'myfile.log')", "", "'maxln' is a utility to detect overlength lines in text files", "Copyright (c) 1998 by Ira E McDonald (High North Inc)", NULL }; /* 'maxln_synopsis' */ static char report_hdr1[] = "Total Fence Fence Max Filename\n"; static char report_hdr2[] = "Lines Lines Column Length \n"; static char report_hdr3[] = "--------------------------------------------\n"; /*********************************************************************/ /* Global function declarations */ /*********************************************************************/ #ifdef __STDC__ extern int main ( /* Mainline */ int argc, /* Argument Count */ char *argv[]); /* Argument Vector */ #else extern int main (); /* Mainline */ #endif #ifdef __STDC__ static void maxln_show_synopsis ( /* Display Program Synopsis */ void); /* No Arguments */ #else static void maxln_show_synopsis (); /* Display Program Synopsis */ #endif #ifdef __STDC__ static void maxln_scan_file ( /* Scan File */ char *fn, /* File Name */ int fc, /* Fence Column */ boolean vf); /* Verbose Flag */ #else static void maxln_scan_file (); /* Scan File */ #endif #ifdef __STDC__ static void maxln_error_exit ( /* Log Error and Exit */ char *es); /* Error Message String */ #else static void maxln_error_exit (); /* Log Error and Exit */ #endif /*********************************************************************/ /* Function: main() */ /*********************************************************************/ #ifdef __STDC__ extern int main ( /* Mainline */ int argc, /* Argument Count */ char *argv[]) /* Argument Vector */ #else extern int main ( /* Mainline */ argc, /* Argument Count */ argv) /* Argument Vector */ int argc; /* Argument Count */ char *argv[]; /* Argument Vector */ #endif { /* 'main' */ int ai; /* argument index */ char *ap; /* argument pointer */ int fc = 72; /* fence column */ boolean vf = FALSE; /* verbose flag */ /* Check for missing input parameters */ if (argc LT 2) maxln_show_synopsis (); /* Check for switches */ for (ai = 1; ai LT argc; ai++) { ap = argv[ai]; if (*ap NE '-') break; for (ap++; *ap; ap++) { if (isdigit (*ap)) { fc = atoi (ap); if ((fc LT 1) OR (fc GT FMAX)) maxln_show_synopsis (); break; } switch (*ap) { case 'v': case 'V': vf = TRUE; break; default: break; } } } /* Check for missing input filenames */ if (ai GE argc) maxln_show_synopsis (); /* Check for missing filename(s) */ if (ai GE argc) maxln_show_synopsis (); /* Display report headers */ printf (report_hdr1); printf (report_hdr2); printf (report_hdr3); /* Check all specified files */ for (; ai LT argc; ai++) { /* Get next input filename */ ap = argv[ai]; if (strlen (ap) EQ 0) maxln_show_synopsis (); /* Process next input file */ maxln_scan_file (ap, fc, vf); } printf ("maxln: Normal termination\n"); return (0); } /* 'main' */ /*********************************************************************/ /* Function: maxln_show_synopsis() */ /*********************************************************************/ #ifdef __STDC__ static void maxln_show_synopsis ( /* Display Program Synopsis */ void) /* No Arguments */ #else static void maxln_show_synopsis ( /* Display Program Synopsis */ ) /* No Arguments */ #endif { /* 'maxln_show_synopsis' */ int si; /* synopsis index */ /* Display program synopsis */ for (si = 0; maxln_synopsis[si]; si++) (void) printf ("%s\n", maxln_synopsis[si]); exit (0); } /* 'maxln_show_synopsis' */ /*********************************************************************/ /* Function: maxln_scan_file() */ /*********************************************************************/ #ifdef __STDC__ static void maxln_scan_file ( /* Scan File */ char *fn, /* File Name */ int fc, /* Fence Column */ boolean vf) /* Verbose Flag */ #else static void maxln_scan_file ( /* Scan File */ fn, /* File Name */ fc, /* Fence Column */ vf) /* Verbose Flag */ char *fn; /* File Name */ int fc; /* Fence Column */ boolean vf; /* Verbose Flag */ #endif { /* 'maxln_scan_file' */ long fcc = 0; /* fence column exceed count */ long flc = 0; /* file lines count */ int fmi = 0; /* file max line length */ int fli; /* file line length */ static FILE *fp = 0; /* file pointer */ char fs[FMAX]; /* file input string */ boolean ff = FALSE; /* file (printed) flag */ char ms[80]; /* message string */ /* Open input file */ if (fp) fp = freopen (fn, "r", fp); else fp = fopen (fn, "r"); if (NOT fp) { sprintf (ms, "Error opening file '%s'", fn); maxln_error_exit (ms); } /* Read input file checking for maximum line length */ for (;;) { if (NOT fgets (fs, FMAX, fp)) break; flc++; fli = strlen (fs); if (fli) fli--; if (fli LE fc) continue; fcc++; if (fli GT fmi) fmi = fli; if (NOT fc) continue; if (NOT vf) continue; if (NOT ff) { printf ("File: %s\n", fn); ff = TRUE; } printf ("Line: %ld Length: %d\n", flc, fli); } /* Close input file */ fclose (fp); /* Report on this file */ printf ("%6ld %6ld %6d %6d %s\n", flc, fcc, fc, fmi, fn); } /* 'maxln_scan_file' */ /*********************************************************************/ /* Function: maxln_error_exit() */ /*********************************************************************/ #ifdef __STDC__ static void maxln_error_exit ( /* Log Error and Exit */ char *es) /* Error Message String */ #else static void maxln_error_exit ( /* Log Error and Exit */ es) /* Error Message String */ char *es; /* Error Message String */ #endif { /* 'maxln_error_exit' */ printf ("maxln: %s\n", es); printf ("maxln: Abnormal termination\n"); exit (1); } /* 'maxln_error_exit' */