/*
|
/*
|
* cklength - check the length of lines in a file
|
* cklength - check the length of lines in a file
|
*
|
*
|
* This program check to see if the files passed to it on the command line
|
* This program check to see if the files passed to it on the command line
|
* contain a line which exceeds the maximum allowable length. The default
|
* contain a line which exceeds the maximum allowable length. The default
|
* maximum line length is 80.
|
* maximum line length is 80.
|
*
|
*
|
* usage: cklength [ -v ] [ arg ... ] files...
|
* usage: cklength [ -v ] [ arg ... ] files...
|
* -l length -- maximum line length
|
* -l length -- maximum line length
|
* -v -- verbose
|
* -v -- verbose
|
*
|
*
|
* $Id: cklength.c,v 1.2 2001-09-27 12:02:53 chris Exp $
|
* $Id: cklength.c,v 1.2 2001-09-27 12:02:53 chris Exp $
|
*/
|
*/
|
|
|
#define GETOPTARGS "l:nNv"
|
#define GETOPTARGS "l:nNv"
|
|
|
char *USAGE = "\
|
char *USAGE = "\
|
usage: cklength [ -v ] [ arg ... ] files... \n\
|
usage: cklength [ -v ] [ arg ... ] files... \n\
|
-l length -- maximum line length\n\
|
-l length -- maximum line length\n\
|
-n -- report line numbers for offending lines\n\
|
-n -- report line numbers for offending lines\n\
|
-N -- report line numbers and length for offending lines\n\
|
-N -- report line numbers and length for offending lines\n\
|
-v -- verbose\n\
|
-v -- verbose\n\
|
\n\
|
\n\
|
Print the name of files which have at least 1 line which exceeds the\n\
|
Print the name of files which have at least 1 line which exceeds the\n\
|
maximum line length. The default maximum line length is 80.\n\
|
maximum line length. The default maximum line length is 80.\n\
|
";
|
";
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <ctype.h>
|
#include <ctype.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <string.h>
|
#include <string.h>
|
#include <memory.h>
|
#include <memory.h>
|
#include <stdarg.h>
|
#include <stdarg.h>
|
#include <errno.h>
|
#include <errno.h>
|
|
|
#include "config.h"
|
#include "config.h"
|
|
|
#ifndef VMS
|
#ifndef VMS
|
#ifndef HAVE_STRERROR
|
#ifndef HAVE_STRERROR
|
extern int sys_nerr;
|
extern int sys_nerr;
|
extern char *sys_errlist[];
|
extern char *sys_errlist[];
|
|
|
#define strerror( _err ) \
|
#define strerror( _err ) \
|
((_err) < sys_nerr) ? sys_errlist [(_err)] : "unknown error"
|
((_err) < sys_nerr) ? sys_errlist [(_err)] : "unknown error"
|
|
|
#else /* HAVE_STRERROR */
|
#else /* HAVE_STRERROR */
|
char *strerror ();
|
char *strerror ();
|
#endif
|
#endif
|
#else /* VMS */
|
#else /* VMS */
|
char *strerror (int,...);
|
char *strerror (int,...);
|
#endif
|
#endif
|
|
|
|
|
#define BUFFER_SIZE 512
|
#define BUFFER_SIZE 512
|
|
|
#define SUCCESS 0
|
#define SUCCESS 0
|
#define FAILURE -1
|
#define FAILURE -1
|
#define Failed(x) (((int) (x)) == FAILURE)
|
#define Failed(x) (((int) (x)) == FAILURE)
|
#define TRUE 1
|
#define TRUE 1
|
#define FALSE 0
|
#define FALSE 0
|
#define STREQ(a,b) (strcmp(a,b) == 0)
|
#define STREQ(a,b) (strcmp(a,b) == 0)
|
#define NUMELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
|
#define NUMELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
|
|
|
/*
|
/*
|
* Definitions for unsigned "ints"; especially for use in data structures
|
* Definitions for unsigned "ints"; especially for use in data structures
|
* that will be shared among (potentially) different cpu's (we punt on
|
* that will be shared among (potentially) different cpu's (we punt on
|
* byte ordering problems tho)
|
* byte ordering problems tho)
|
*/
|
*/
|
|
|
typedef unsigned char u8;
|
typedef unsigned char u8;
|
typedef unsigned short u16;
|
typedef unsigned short u16;
|
typedef unsigned long u32;
|
typedef unsigned long u32;
|
|
|
/*
|
/*
|
* vars controlled by command line options
|
* vars controlled by command line options
|
*/
|
*/
|
|
|
int verbose = FALSE; /* be verbose */
|
int verbose = FALSE; /* be verbose */
|
int report_line_numbers = FALSE; /* report line numbers of offenders */
|
int report_line_numbers = FALSE; /* report line numbers of offenders */
|
int report_line_length = FALSE; /* report line length of offenders */
|
int report_line_length = FALSE; /* report line length of offenders */
|
|
|
int line_length = 80; /* maximum allowable line length */
|
int line_length = 80; /* maximum allowable line length */
|
|
|
extern char *optarg; /* getopt(3) control vars */
|
extern char *optarg; /* getopt(3) control vars */
|
extern int optind, opterr;
|
extern int optind, opterr;
|
|
|
char *progname; /* for error() */
|
char *progname; /* for error() */
|
|
|
int process(char *arg);
|
int process(char *arg);
|
void error(int errn, ...);
|
void error(int errn, ...);
|
long getparm(char *s, long min, long max, char *msg);
|
long getparm(char *s, long min, long max, char *msg);
|
|
|
#define ERR_ERRNO (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */
|
#define ERR_ERRNO (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */
|
#define ERR_FATAL (ERR_ERRNO / 2) /* fatal error ; no return */
|
#define ERR_FATAL (ERR_ERRNO / 2) /* fatal error ; no return */
|
#define ERR_ABORT (ERR_ERRNO / 4) /* fatal error ; abort */
|
#define ERR_ABORT (ERR_ERRNO / 4) /* fatal error ; abort */
|
#define ERR_MASK (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */
|
#define ERR_MASK (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */
|
|
|
#define stol(p) strtol(p, (char **) NULL, 0)
|
#define stol(p) strtol(p, (char **) NULL, 0)
|
int Open(), Read(), Write();
|
int Open(), Read(), Write();
|
|
|
int main(
|
int main(
|
int argc,
|
int argc,
|
char **argv
|
char **argv
|
)
|
)
|
{
|
{
|
register int c;
|
register int c;
|
int showusage = FALSE; /* usage error? */
|
int showusage = FALSE; /* usage error? */
|
int rc = 0;
|
int rc = 0;
|
|
|
/*
|
/*
|
* figure out invocation leaf-name
|
* figure out invocation leaf-name
|
*/
|
*/
|
|
|
if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
|
if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
|
progname = argv[0];
|
progname = argv[0];
|
else
|
else
|
progname++;
|
progname++;
|
|
|
argv[0] = progname; /* for getopt err reporting */
|
argv[0] = progname; /* for getopt err reporting */
|
|
|
/*
|
/*
|
* Check options and arguments.
|
* Check options and arguments.
|
*/
|
*/
|
|
|
opterr = 0; /* we'll report all errors */
|
opterr = 0; /* we'll report all errors */
|
while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
|
while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
|
switch (c)
|
switch (c)
|
{
|
{
|
case 'l': /* line length */
|
case 'l': /* line length */
|
line_length = atoi( optarg );
|
line_length = atoi( optarg );
|
if ( line_length < 0 || line_length > BUFFER_SIZE )
|
if ( line_length < 0 || line_length > BUFFER_SIZE )
|
error(ERR_FATAL, "(%d) is illegal line length\n",line_length);
|
error(ERR_FATAL, "(%d) is illegal line length\n",line_length);
|
break;
|
break;
|
|
|
case 'n': /* toggle report_line_numbers */
|
case 'n': /* toggle report_line_numbers */
|
report_line_numbers = ! report_line_numbers;
|
report_line_numbers = ! report_line_numbers;
|
break;
|
break;
|
|
|
case 'N': /* toggle both reports */
|
case 'N': /* toggle both reports */
|
report_line_numbers = ! report_line_numbers;
|
report_line_numbers = ! report_line_numbers;
|
report_line_length = ! report_line_length;
|
report_line_length = ! report_line_length;
|
break;
|
break;
|
|
|
case 'v': /* toggle verbose */
|
case 'v': /* toggle verbose */
|
verbose = ! verbose;
|
verbose = ! verbose;
|
break;
|
break;
|
|
|
case '?':
|
case '?':
|
showusage = TRUE;
|
showusage = TRUE;
|
}
|
}
|
|
|
if (showusage)
|
if (showusage)
|
{
|
{
|
(void) fprintf(stderr, "%s", USAGE);
|
(void) fprintf(stderr, "%s", USAGE);
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
/*
|
/*
|
* traverse and process the arguments
|
* traverse and process the arguments
|
*/
|
*/
|
|
|
for ( ; argv[optind]; optind++)
|
for ( ; argv[optind]; optind++)
|
if (Failed(process(argv[optind])))
|
if (Failed(process(argv[optind])))
|
rc = FAILURE;
|
rc = FAILURE;
|
|
|
return rc;
|
return rc;
|
}
|
}
|
|
|
|
|
/*
|
/*
|
* process(arg)
|
* process(arg)
|
*/
|
*/
|
|
|
int
|
int
|
process(char *arg)
|
process(char *arg)
|
{
|
{
|
FILE *in;
|
FILE *in;
|
char *bptr;
|
char *bptr;
|
char buffer[ BUFFER_SIZE ];
|
char buffer[ BUFFER_SIZE ];
|
int line_number;
|
int line_number;
|
int length;
|
int length;
|
int count;
|
int count;
|
int rc = SUCCESS; /* succeed by default */
|
int rc = SUCCESS; /* succeed by default */
|
|
|
in = fopen( arg, "r" );
|
in = fopen( arg, "r" );
|
if (!in)
|
if (!in)
|
error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
|
error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
|
|
|
count = 0;
|
count = 0;
|
|
|
for ( line_number=1 ; ; line_number++ ) {
|
for ( line_number=1 ; ; line_number++ ) {
|
bptr = fgets( buffer, BUFFER_SIZE, in );
|
bptr = fgets( buffer, BUFFER_SIZE, in );
|
if (!bptr)
|
if (!bptr)
|
break;
|
break;
|
|
|
/*
|
/*
|
* Don't count the carriage return.
|
* Don't count the carriage return.
|
*/
|
*/
|
|
|
length = strlen( buffer ) - 1;
|
length = strlen( buffer ) - 1;
|
|
|
if ( length <= line_length )
|
if ( length <= line_length )
|
continue;
|
continue;
|
|
|
if ( count == 0 ) {
|
if ( count == 0 ) {
|
fprintf( stderr, "%s\n", arg );
|
fprintf( stderr, "%s\n", arg );
|
if ( !report_line_numbers )
|
if ( !report_line_numbers )
|
break;
|
break;
|
}
|
}
|
|
|
if ( verbose )
|
if ( verbose )
|
fprintf( stderr, "TOO LONG:%d: %s\n", line_number, buffer );
|
fprintf( stderr, "TOO LONG:%d: %s\n", line_number, buffer );
|
|
|
if ( report_line_numbers ) {
|
if ( report_line_numbers ) {
|
if ( report_line_length )
|
if ( report_line_length )
|
fprintf( stderr, "%d: %d\n" , line_number, length );
|
fprintf( stderr, "%d: %d\n" , line_number, length );
|
else
|
else
|
fprintf( stderr, "%d\n" , line_number );
|
fprintf( stderr, "%d\n" , line_number );
|
}
|
}
|
|
|
count++;
|
count++;
|
|
|
}
|
}
|
|
|
fclose( in );
|
fclose( in );
|
return rc;
|
return rc;
|
}
|
}
|
|
|
/*
|
/*
|
* error(errn, arglist)
|
* error(errn, arglist)
|
* report an error to stderr using printf(3) conventions.
|
* report an error to stderr using printf(3) conventions.
|
* Any output is preceded by '<progname>: '
|
* Any output is preceded by '<progname>: '
|
*
|
*
|
* Uses ERR_FATAL bit to request exit(errn)
|
* Uses ERR_FATAL bit to request exit(errn)
|
* ERR_ABORT to request abort()
|
* ERR_ABORT to request abort()
|
* ERR_ERRNO to indicate use of errno instead of argument.
|
* ERR_ERRNO to indicate use of errno instead of argument.
|
*
|
*
|
* If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its
|
* If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its
|
* associated error message is appended to the output.
|
* associated error message is appended to the output.
|
*/
|
*/
|
|
|
/*VARARGS*/
|
/*VARARGS*/
|
|
|
void
|
void
|
error(int error_flag, ...)
|
error(int error_flag, ...)
|
{
|
{
|
va_list arglist;
|
va_list arglist;
|
register char *format;
|
register char *format;
|
int local_errno;
|
int local_errno;
|
|
|
extern int errno;
|
extern int errno;
|
|
|
(void) fflush(stdout); /* in case stdout/stderr same */
|
(void) fflush(stdout); /* in case stdout/stderr same */
|
|
|
local_errno = error_flag & ~ERR_MASK;
|
local_errno = error_flag & ~ERR_MASK;
|
if (error_flag & ERR_ERRNO) /* use errno? */
|
if (error_flag & ERR_ERRNO) /* use errno? */
|
local_errno = errno;
|
local_errno = errno;
|
|
|
va_start(arglist, error_flag);
|
va_start(arglist, error_flag);
|
format = va_arg(arglist, char *);
|
format = va_arg(arglist, char *);
|
(void) fprintf(stderr, "%s: ", progname);
|
(void) fprintf(stderr, "%s: ", progname);
|
(void) vfprintf(stderr, format, arglist);
|
(void) vfprintf(stderr, format, arglist);
|
va_end(arglist);
|
va_end(arglist);
|
|
|
if (local_errno)
|
if (local_errno)
|
(void) fprintf(stderr, " (%s)\n", strerror(local_errno));
|
(void) fprintf(stderr, " (%s)\n", strerror(local_errno));
|
|
|
(void) fflush(stderr);
|
(void) fflush(stderr);
|
|
|
if (error_flag & (ERR_FATAL | ERR_ABORT))
|
if (error_flag & (ERR_FATAL | ERR_ABORT))
|
{
|
{
|
if (error_flag & ERR_FATAL)
|
if (error_flag & ERR_FATAL)
|
{
|
{
|
error(0, "fatal error, exiting");
|
error(0, "fatal error, exiting");
|
exit(local_errno ? local_errno : 1);
|
exit(local_errno ? local_errno : 1);
|
}
|
}
|
else
|
else
|
{
|
{
|
error(0, "fatal error, aborting");
|
error(0, "fatal error, aborting");
|
abort();
|
abort();
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
long
|
long
|
getparm(char *s,
|
getparm(char *s,
|
long min,
|
long min,
|
long max,
|
long max,
|
char *msg)
|
char *msg)
|
{
|
{
|
long val;
|
long val;
|
|
|
if ( ! strchr("0123456789-", *s))
|
if ( ! strchr("0123456789-", *s))
|
{
|
{
|
error(ERR_FATAL, "'%s' is not a number", s);
|
error(ERR_FATAL, "'%s' is not a number", s);
|
return min;
|
return min;
|
}
|
}
|
|
|
val = strtol(s, (char **) NULL, 0);
|
val = strtol(s, (char **) NULL, 0);
|
if ((val < min) || (val > max))
|
if ((val < min) || (val > max))
|
{
|
{
|
if (min == max)
|
if (min == max)
|
error(ERR_FATAL, "%s can only be %ld", s, min);
|
error(ERR_FATAL, "%s can only be %ld", s, min);
|
else
|
else
|
error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max);
|
error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max);
|
}
|
}
|
|
|
return val;
|
return val;
|
}
|
}
|
|
|
|
|
/*
|
/*
|
* Open()
|
* Open()
|
* Perform open(2), returning the file descriptor. Prints
|
* Perform open(2), returning the file descriptor. Prints
|
* error message if open fails.
|
* error message if open fails.
|
*/
|
*/
|
|
|
int
|
int
|
Open(char *file,
|
Open(char *file,
|
int oflag,
|
int oflag,
|
int mode)
|
int mode)
|
{
|
{
|
int O_fd;
|
int O_fd;
|
|
|
if (Failed(O_fd = open(file, oflag, mode)))
|
if (Failed(O_fd = open(file, oflag, mode)))
|
error(
|
error(
|
ERR_ERRNO | ERR_FATAL,
|
ERR_ERRNO | ERR_FATAL,
|
"open('%s', 0x%x, 0%o) failed", file, oflag, mode
|
"open('%s', 0x%x, 0%o) failed", file, oflag, mode
|
);
|
);
|
|
|
return O_fd;
|
return O_fd;
|
}
|
}
|
|
|
/*
|
/*
|
* Read()
|
* Read()
|
* Perform read(2); prints error message if fails.
|
* Perform read(2); prints error message if fails.
|
*/
|
*/
|
|
|
int
|
int
|
Read(int file,
|
Read(int file,
|
char *buffer,
|
char *buffer,
|
unsigned int count)
|
unsigned int count)
|
{
|
{
|
int nbytes;
|
int nbytes;
|
|
|
if (Failed(nbytes = read(file, buffer, count)))
|
if (Failed(nbytes = read(file, buffer, count)))
|
error(
|
error(
|
ERR_ERRNO | ERR_FATAL,
|
ERR_ERRNO | ERR_FATAL,
|
"read(%d, 0x%x, %d) failed", file, buffer, count
|
"read(%d, 0x%x, %d) failed", file, buffer, count
|
);
|
);
|
|
|
return nbytes;
|
return nbytes;
|
}
|
}
|
|
|
/*
|
/*
|
* Write()
|
* Write()
|
* Perform write(2); prints error message if fails.
|
* Perform write(2); prints error message if fails.
|
*/
|
*/
|
|
|
int
|
int
|
Write(int file,
|
Write(int file,
|
char *buffer,
|
char *buffer,
|
unsigned int count)
|
unsigned int count)
|
{
|
{
|
int nbytes;
|
int nbytes;
|
|
|
if (Failed(nbytes = write(file, buffer, count)))
|
if (Failed(nbytes = write(file, buffer, count)))
|
error(
|
error(
|
ERR_ERRNO | ERR_FATAL,
|
ERR_ERRNO | ERR_FATAL,
|
"write(%d, 0x%x, %d) failed", file, buffer, count
|
"write(%d, 0x%x, %d) failed", file, buffer, count
|
);
|
);
|
|
|
return nbytes;
|
return nbytes;
|
}
|
}
|
|
|