#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)cmp.c 8.2 (Berkeley) %G%";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
+
+#include <err.h>
#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+
#include "extern.h"
int lflag, sflag;
static void usage __P((void));
+int
main(argc, argv)
int argc;
char *argv[];
argc -= optind;
if (lflag && sflag)
- err("only one of -l and -s may be specified");
+ errx(ERR_EXIT, "only one of -l and -s may be specified");
if (argc < 2 || argc > 4)
usage();
file1 = "stdin";
}
else if ((fd1 = open(file1, O_RDONLY, 0)) < 0)
- err("%s: %s", file1, strerror(errno));
+ err(ERR_EXIT, "%s", file1);
if (strcmp(file2 = argv[1], "-") == 0) {
if (special)
- err("standard input may only be specified once");
+ errx(ERR_EXIT,
+ "standard input may only be specified once");
special = 1;
fd2 = 0;
file2 = "stdin";
}
else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
- err("%s: %s", file2, strerror(errno));
+ err(ERR_EXIT, "%s", file2);
skip1 = argc > 2 ? strtol(argv[2], NULL, 10) : 0;
skip2 = argc == 4 ? strtol(argv[3], NULL, 10) : 0;
if (!special) {
if (fstat(fd1, &sb1))
- err("%s: %s", file1, strerror(errno));
+ err(ERR_EXIT, "%s", file1);
if (!S_ISREG(sb1.st_mode))
special = 1;
else {
if (fstat(fd2, &sb2))
- err("%s: %s", file2, strerror(errno));
+ err(ERR_EXIT, "%s", file2);
if (!S_ISREG(sb2.st_mode))
special = 1;
}
static void
usage()
{
+
(void)fprintf(stderr,
"usage: cmp [-l | s] file1 file2 [skip1 [skip2]]\n");
- exit(2);
+ exit(ERR_EXIT);
}
*
* %sccs.include.redist.c%
*
- * @(#)extern.h 8.1 (Berkeley) %G%
+ * @(#)extern.h 8.2 (Berkeley) %G%
*/
+#define OK_EXIT 0
+#define DIFF_EXIT 1
+#define ERR_EXIT 2 /* error exit code */
+
void c_regular __P((int, char *, off_t, off_t, int, char *, off_t, off_t));
void c_special __P((int, char *, off_t, int, char *, off_t));
void diffmsg __P((char *, char *, off_t, off_t));
void eofmsg __P((char *));
-void err __P((const char *fmt, ...));
extern int lflag, sflag;
*/
#ifndef lint
-static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)misc.c 8.2 (Berkeley) %G%";
#endif /* not lint */
#include <sys/types.h>
+
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
+
#include "extern.h"
void
char *file;
{
if (!sflag)
- (void)fprintf(stderr, "cmp: EOF on %s\n", file);
- exit(1);
+ warnx("EOF on %s", file);
+ exit(DIFF_EXIT);
}
void
if (!sflag)
(void)printf("%s %s differ: char %qd, line %qd\n",
file1, file2, byte, line);
- exit(1);
-}
-
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
-
-void
-#if __STDC__
-err(const char *fmt, ...)
-#else
-err(fmt, va_alist)
- char *fmt;
- va_dcl
-#endif
-{
- va_list ap;
-#if __STDC__
- va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
- (void)fprintf(stderr, "cmp: ");
- (void)vfprintf(stderr, fmt, ap);
- va_end(ap);
- (void)fprintf(stderr, "\n");
- exit(2);
- /* NOTREACHED */
+ exit(DIFF_EXIT);
}
*/
#ifndef lint
-static char sccsid[] = "@(#)regular.c 8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)regular.c 8.2 (Berkeley) %G%";
#endif /* not lint */
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
+#include <err.h>
#include <limits.h>
-#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+
#include "extern.h"
void
char *file1, *file2;
off_t skip1, len1, skip2, len2;
{
- register u_char ch, *p1, *p2;
- register off_t byte, length, line;
+ u_char ch, *p1, *p2;
+ off_t byte, length, line;
int dfound;
if (sflag && len1 != len2)
if ((p1 = (u_char *)mmap(NULL,
(size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1)
- err("%s: %s", file1, strerror(errno));
+ err(ERR_EXIT, "%s", file1);
if ((p2 = (u_char *)mmap(NULL,
(size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1)
- err("%s: %s", file2, strerror(errno));
+ err(ERR_EXIT, "%s", file2);
dfound = 0;
for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
if (len1 != len2)
eofmsg (len1 > len2 ? file2 : file1);
if (dfound)
- exit(1);
+ exit(DIFF_EXIT);
}
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)col.c 8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)col.c 8.2 (Berkeley) %G%";
#endif /* not lint */
-#include <errno.h>
#include <ctype.h>
+#include <err.h>
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
#define BS '\b' /* backspace */
#define TAB '\t' /* tab */
int l_max_col; /* max column in the line */
};
-LINE *alloc_line();
-void *xmalloc();
-
-CSET last_set; /* char_set of last char printed */
-LINE *lines;
-int compress_spaces; /* if doing space -> tab conversion */
-int fine; /* if `fine' resolution (half lines) */
-int max_bufd_lines; /* max # lines to keep in memory */
-int nblank_lines; /* # blanks after last flushed line */
-int no_backspaces; /* if not to output any backspaces */
+LINE *alloc_line __P((void));
+void dowarn __P((int));
+void flush_line __P((LINE *));
+void flush_lines __P((int));
+void flush_blanks __P((void));
+void free_line __P((LINE *));
+void usage __P((void));
+void wrerr __P((void));
+void *xmalloc __P((void *, size_t));
+
+CSET last_set; /* char_set of last char printed */
+LINE *lines;
+int compress_spaces; /* if doing space -> tab conversion */
+int fine; /* if `fine' resolution (half lines) */
+int max_bufd_lines; /* max # lines to keep in memory */
+int nblank_lines; /* # blanks after last flushed line */
+int no_backspaces; /* if not to output any backspaces */
#define PUTC(ch) \
if (putchar(ch) == EOF) \
wrerr();
+int
main(argc, argv)
int argc;
char **argv;
{
- extern int optind;
- extern char *optarg;
- register int ch;
+ int ch;
CHAR *c;
CSET cur_set; /* current character set */
LINE *l; /* current line */
}
} else {
if (!warned++)
- warn(cur_line);
+ dowarn(cur_line);
cur_line -= nmove;
}
}
exit(0);
}
+void
flush_lines(nflush)
int nflush;
{
* is the number of half line feeds, otherwise it is the number of whole line
* feeds.
*/
+void
flush_blanks()
{
int half, i, nb;
* Write a line to stdout taking care of space to tab conversion (-h flag)
* and character set shifts.
*/
+void
flush_line(l)
LINE *l;
{
count = (int *)xmalloc((void *)count,
(unsigned)sizeof(int) * count_size);
}
- bzero((char *)count, sizeof(int) * l->l_max_col + 1);
+ memset((char *)count, 0, sizeof(int) * l->l_max_col + 1);
for (i = nchars, c = l->l_line; --i >= 0; c++)
count[c->c_column]++;
l = line_freelist;
line_freelist = l->l_next;
- bzero(l, sizeof(LINE));
- return(l);
+ memset(l, 0, sizeof(LINE));
+ return (l);
}
+void
free_line(l)
LINE *l;
{
+
l->l_next = line_freelist;
line_freelist = l;
}
void *p;
size_t size;
{
- if (!(p = (void *)realloc(p, size))) {
- (void)fprintf(stderr, "col: %s.\n", strerror(ENOMEM));
- exit(1);
- }
- return(p);
+
+ if (!(p = (void *)realloc(p, size)))
+ err(1, NULL);
+ return (p);
}
+void
usage()
{
+
(void)fprintf(stderr, "usage: col [-bfx] [-l nline]\n");
exit(1);
}
+void
wrerr()
{
+
(void)fprintf(stderr, "col: write error.\n");
exit(1);
}
-warn(line)
+void
+dowarn(line)
int line;
{
- (void)fprintf(stderr,
- "col: warning: can't back up %s.\n", line < 0 ?
- "past first line" : "-- line already flushed");
+
+ warnx("warning: can't back up %s",
+ line < 0 ? "past first line" : "-- line already flushed");
}