+/* $NetBSD: banner.c,v 1.17 2008/07/20 01:03:20 lukem Exp $ */
+
/*
- * Copyright (c) 1980 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1980, 1993, 1994\
+ The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
-/*static char sccsid[] = "from: @(#)banner.c 4.3 (Berkeley) 6/1/90";*/
-static char rcsid[] = "$Id: banner.c,v 1.2 1993/08/01 18:56:19 mycroft Exp $";
+#if 0
+static char sccsid[] = "@(#)banner.c 8.4 (Berkeley) 4/29/95";
+#else
+__RCSID("$NetBSD: banner.c,v 1.17 2008/07/20 01:03:20 lukem Exp $");
+#endif
#endif /* not lint */
/*
* banner - prints large signs
- * banner [-w#] [-d] [-t] message ...
+ * banner [-w width] [-d] [-t] message ...
*/
+#include <err.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#define MAXMSG 1024
#define DWIDTH 132
#define NBYTES 9271
/* Pointers into data_table for each ASCII char */
-int asc_ptr[NCHARS] = {
+const int asc_ptr[NCHARS] = {
/* ^@ */ 0, 0, 0, 0, 0, 0, 0, 0,
/* ^H */ 0, 0, 0, 0, 0, 0, 0, 0,
/* ^P */ 0, 0, 0, 0, 0, 0, 0, 0,
* is the next elt in array) and goto second
* next element in array.
*/
-char data_table[NBYTES] = {
+const unsigned char data_table[NBYTES] = {
/* 0 1 2 3 4 5 6 7 8 9 */
/* 0 */ 129, 227, 130, 34, 6, 90, 19, 129, 32, 10,
/* 10 */ 74, 40, 129, 31, 12, 64, 53, 129, 30, 14,
/* 9270 */ 193
};
-int i,j;
-int width = DWIDTH; /* -w option: scrunch letters to 80 columns */
-int debug;
-int trace;
-char line[DWIDTH];
-char print[DWIDTH];
-char message[MAXMSG];
-int nchars;
-int linen;
-int x,y;
-int term;
-int pc;
-int max;
+char line[DWIDTH];
+char message[MAXMSG];
+char print[DWIDTH];
+int debug, i, j, linen, max, nchars, pc, term, trace, x, y;
+int width = DWIDTH; /* -w option: scrunch letters to 80 columns */
-main(argc, argv)
- int argc;
- char **argv;
+static void
+toolong(void)
+{
+ errx(EXIT_FAILURE, "message too long");
+}
+
+int
+main(int argc, char *argv[])
{
- extern char *optarg;
- extern int optind;
int ch;
- char *strcpy(), *strcat();
- while ((ch = getopt(argc, argv, "w:td")) != EOF)
- switch((char)ch) {
- case 'w':
- width = atoi(optarg);
- if (width <= 0)
- width = 80;
- break;
+ while ((ch = getopt(argc, argv, "w:td")) != -1)
+ switch (ch) {
case 'd':
debug = 1;
break;
case 't':
trace = 1;
break;
+ case 'w':
+ width = atoi(optarg);
+ if (width <= 0 || width > DWIDTH)
+ errx(1, "illegal argument for -w option");
+ break;
case '?':
default:
- fprintf(stderr, "usage: banner [-w width]\n");
+ (void)fprintf(stderr, "usage: banner [-w width]\n");
exit(1);
}
argc -= optind;
argv += optind;
- for (i=0; i<width; i++) {
- j = i * 132 / width;
+ for (i = 0; i < width; i++) {
+ j = i * DWIDTH / width;
print[j] = 1;
}
-#ifdef notdef
- {
-#define dir(f) "/e1/mrh/ucb/lib/f"
-#define INDTBL dir(ban.dat.indtbl)
-#define OBJECT dir(ban.dat.object)
- FILE *fd;
-
- fd = fopen(INDTBL, "r");
- for (i=0; i<NCHARS; i++) {
- fscanf(fd, "%d", &asc_ptr[i]);
- }
- fclose(fd);
-
- fd = fopen(OBJECT, "r");
- fread(data_table, 1, NBYTES, fd);
- fclose(fd);
- }
-#endif
-
/* Have now read in the data. Next get the message to be printed. */
if (*argv) {
- strcpy(message, *argv);
+ const size_t msize = sizeof(message);
+
+ if (strlcpy(message, *argv, msize) >= msize)
+ toolong();
while (*++argv) {
- strcat(message, " ");
- strcat(message, *argv);
+ if (strlcat(message, " ", msize) >= msize)
+ toolong();
+ if (strlcat(message, *argv, msize) >= msize)
+ toolong();
}
nchars = strlen(message);
} else {
- fprintf(stderr,"Message: ");
+ if (isatty(fileno(stdin)))
+ fprintf(stderr,"Message: ");
(void)fgets(message, sizeof(message), stdin);
nchars = strlen(message);
message[nchars--] = '\0'; /* get rid of newline */
/* some debugging print statements */
if (debug) {
printf("int asc_ptr[128] = {\n");
- for (i=0; i<128; i++) {
+ for (i = 0; i < 128; i++) {
printf("%4d, ",asc_ptr[i]);
if ((i+1) % 8 == 0)
printf("\n");
}
printf("};\nchar data_table[NBYTES] = {\n");
printf(" /* ");
- for (i=0; i<10; i++) printf(" %3d ",i);
+ for (i = 0; i < 10; i++) printf(" %3d ",i);
printf(" */\n");
- for (i=0; i<NBYTES; i += 10) {
+ for (i = 0; i < NBYTES; i += 10) {
printf("/* %4d */ ",i);
- for (j=i; j<i+10; j++) {
- x = data_table[j] & 0377;
+ for (j = i; j < i+10; j++) {
+ x = data_table[j];
printf(" %3d, ",x);
}
putchar('\n');
/* check message to make sure it's legal */
j = 0;
- for (i=0; i<nchars; i++)
- if (asc_ptr[message[i]] == 0) {
- printf("The character '%c' is not in my character set.\n",message[i]);
+ for (i = 0; i < nchars; i++)
+ if ((u_char) message[i] >= NCHARS ||
+ asc_ptr[(u_char) message[i]] == 0) {
+ warnx("The character '%c' is not in my character set",
+ message[i]);
j++;
}
- if (j) exit(1);
+ if (j)
+ exit(1);
if (trace)
printf("Message '%s' is OK\n",message);
/* Now have message. Print it one character at a time. */
- for (i=0; i<nchars; i++) {
+ for (i = 0; i < nchars; i++) {
if (trace)
printf("Char #%d: %c\n", i, message[i]);
- for (j=0; j<DWIDTH; j++) line[j] = ' ';
- pc = asc_ptr[message[i]];
+ for (j = 0; j < DWIDTH; j++) line[j] = ' ';
+ pc = asc_ptr[(u_char) message[i]];
term = 0;
max = 0;
linen = 0;
- while ( !term ) {
- if (pc<0 || pc > NBYTES) {
+ while (!term) {
+ if (pc < 0 || pc > NBYTES) {
printf("bad pc: %d\n",pc);
exit(1);
}
- x = data_table[pc] & 0377;
- if (trace)
- printf("pc=%d, term=%d, max=%d, linen=%d, x=%d\n",pc,term,max,linen,x);
+ x = data_table[pc];
+ if (trace) {
+ printf("pc=%d, term=%d, max=%d, linen=%d, x=%d",
+ pc,term,max,linen,x);
+ printf("\n");
+ }
if (x >= 128) {
if (x>192) term++;
x = x & 63;
while (x--) {
if (print[linen++]) {
- for (j=0; j<=max; j++)
+ for (j=0; j <= max; j++)
if (print[j])
putchar(line[j]);
putchar('\n');
}
}
- for (j=0; j<DWIDTH; j++) line[j] = ' ';
+ for (j = 0; j < DWIDTH; j++) line[j] = ' ';
pc++;
}
else {
y = data_table[pc+1];
/* compensate for narrow teminals */
-#ifdef notdef
+#if 0 /* notdef */
x = (x*width + (DWIDTH/2)) / DWIDTH;
y = (y*width + (DWIDTH/2)) / DWIDTH;
#endif