More code cleanup, that is use bit mask for eight bit option, use modern speed_t type, split local error() into local error(), warn(), and dolog() for fine graduated logging with syslogger. Signed-off-by: Werner Fink <werner@xxxxxxx> --- term-utils/agetty.c | 123 ++++++++++++++++++++++++++++++-------------------- 1 files changed, 74 insertions(+), 49 deletions(-) diff --git a/term-utils/agetty.c b/term-utils/agetty.c index e0eac38..15998df 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -108,15 +108,14 @@ #define MAX_SPEED 10 /* max. nr. of baud rates */ struct options { - int flags; /* toggle switches, see below */ - int timeout; /* time-out period */ - char *login; /* login program */ - char *tty; /* name of tty */ - char *initstring; /* modem init string */ - char *issue; /* alternative issue file */ - int numspeed; /* number of baud rates to try */ - int speeds[MAX_SPEED]; /* baud rates to be tried */ - int eightbits; /* assume 8bit-clean tty */ + int flags; /* toggle switches, see below */ + int timeout; /* time-out period */ + char *login; /* login program */ + char *tty; /* name of tty */ + char *initstring; /* modem init string */ + char *issue; /* alternative issue file */ + int numspeed; /* number of baud rates to try */ + speed_t speeds[MAX_SPEED]; /* baud rates to be tried */ }; #define F_PARSE (1<<0) /* process modem status messages */ @@ -130,6 +129,7 @@ struct options { #define F_LCUC (1<<8) /* support for *LCUC stty modes */ #define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */ #define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */ +#define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */ /* Storage for things detected while the login name was read. */ struct chardata { @@ -151,7 +151,7 @@ struct chardata init_chardata = { struct Speedtab { long speed; - int code; + speed_t code; }; static struct Speedtab speedtab[] = { @@ -192,6 +192,8 @@ static struct Speedtab speedtab[] = { {0, 0}, }; +#undef warn +#undef error static void parse_args(int argc, char **argv, struct options *op); static void parse_speeds(struct options *op, char *arg); static void update_utmp(char *line); @@ -205,12 +207,13 @@ static char *get_logname(struct options *op, static void termio_final(struct options *op, struct termios *tp, struct chardata *cp); static int caps_lock(char *s); -static int bcode(char *s); -static void usage(FILE * out); -static void error(const char *, ...); +static speed_t bcode(char *s); +static void usage(FILE * out) __attribute__((__noreturn__)); +static void error(const char *, ...) __attribute__((__noreturn__)) __attribute__((__format__(printf, 1, 2))); +static void warn (const char *, ...) __attribute__((__format__(printf, 1, 2))); /* Fake hostname for ut_host specified on command line. */ -char *fakehost = NULL; +static char *fakehost; #ifdef DEBUGGING #define debug(s) fprintf(dbf,s); fflush(dbf) @@ -231,7 +234,8 @@ int main(int argc, char **argv) "tty1", /* default tty line */ "", /* modem init string */ ISSUE, /* default issue file */ - 0, /* no baud rates known yet */ + 0, /* no baud rates known yet */ + { 0 } }; setlocale(LC_ALL, ""); @@ -325,15 +329,17 @@ int main(int argc, char **argv) /* Let the login program take care of password validation. */ execl(options.login, options.login, "--", logname, NULL); - error(_("%s: can't exec %s: %m"), options.tty, options.login); + warn(_("%s: can't exec %s: %m"), options.tty, options.login); + sleep(5); + exit(EXIT_FAILURE); } /* Parse command-line arguments. */ -void parse_args(int argc, char **argv, struct options *op) +static void parse_args(int argc, char **argv, struct options *op) { extern char *optarg; extern int optind; - int c; + int c, nipple = 0, index; enum { VERSION_OPTION = CHAR_MAX + 1, @@ -360,12 +366,14 @@ void parse_args(int argc, char **argv, struct options *op) { NULL, 0, 0, 0 } }; - while ((c = - getopt_long(argc, argv, "8cf:hH:iI:l:Lmnst:Uw", longopts, - NULL)) != -1) { + while ((c = getopt_long(argc, argv, "8cf:hH:iI:l:Lmnst:Uw", longopts, + &index)) != -1) { switch (c) { + case 0: + op->flags |= nipple; + break; case '8': - op->eightbits = 1; + op->flags |= F_EIGHTBITS; break; case 'c': op->flags |= F_KEEPCFLAGS; @@ -467,7 +475,7 @@ void parse_args(int argc, char **argv, struct options *op) debug("after getopt loop\n"); if (argc < optind + 2) { - warnx(_("not enough arguments")); + warn(_("not enough arguments")); usage(stderr); } @@ -522,7 +530,7 @@ void parse_args(int argc, char **argv, struct options *op) } /* Parse alternate baud rates. */ -void parse_speeds(struct options *op, char *arg) +static void parse_speeds(struct options *op, char *arg) { char *cp; @@ -539,7 +547,7 @@ void parse_speeds(struct options *op, char *arg) #ifdef SYSV_STYLE /* Update our utmp entry. */ -void update_utmp(char *line) +static void update_utmp(char *line) { struct utmp ut; time_t t; @@ -616,7 +624,7 @@ void update_utmp(char *line) #endif /* SYSV_STYLE */ /* Set up tty as stdin, stdout & stderr. */ -void open_tty(char *tty, struct termios *tp, int local) +static void open_tty(char *tty, struct termios *tp, int __attribute__((__unused__)) local) { /* Get rid of the present outputs. */ close(STDOUT_FILENO); @@ -685,10 +693,7 @@ void open_tty(char *tty, struct termios *tp, int local) } /* Initialize termios settings. */ -char gbuf[1024]; -char area[1024]; - -void termio_init(struct options *op, struct termios *tp) +static void termio_init(struct options *op, struct termios *tp) { speed_t ispeed, ospeed; @@ -746,9 +751,9 @@ void termio_init(struct options *op, struct termios *tp) } /* Extract baud rate from modem status message. */ -void auto_baud(struct termios *tp) +static void auto_baud(struct termios *tp) { - int speed; + speed_t speed; int vmin; unsigned iflag; char buf[BUFSIZ]; @@ -806,12 +811,12 @@ void auto_baud(struct termios *tp) } /* Show login prompt, optionally preceded by /etc/issue contents. */ -void do_prompt(struct options *op, struct termios *tp) +static void do_prompt(struct options *op, struct termios *tp) { #ifdef ISSUE FILE *fd; int oflag; - int c; + int c, i; struct utsname uts; uname(&uts); @@ -924,7 +929,7 @@ void do_prompt(struct options *op, struct termios *tp) printf("%s", op->tty); break; case 'b': - for (int i = 0; speedtab[i].speed; i++) + for (i = 0; speedtab[i].speed; i++) if (speedtab[i].code == cfgetispeed(tp)) printf("%ld", speedtab[i].speed); break; @@ -974,7 +979,7 @@ void do_prompt(struct options *op, struct termios *tp) } /* Select next baud rate. */ -void next_speed(struct options *op, struct termios *tp) +static void next_speed(struct options *op, struct termios *tp) { static int baud_index = -1; @@ -994,7 +999,7 @@ void next_speed(struct options *op, struct termios *tp) } /* Get user name, establish parity, speed, erase, kill & eol. */ -char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) +static char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) { static char logname[BUFSIZ]; char *bp; @@ -1039,7 +1044,7 @@ char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) if ((c == 0) && op->numspeed > 1) return EXIT_SUCCESS; /* Do parity bit handling. */ - if (op->eightbits) { + if (op->flags & F_EIGHTBITS) { ascval = c; } else if (c != (ascval = (c & 0177))) { /* Set "parity" bit on. */ @@ -1087,7 +1092,7 @@ char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) default: if (!isascii(ascval) || !isprint(ascval)) { /* Ignore garbage characters. */ ; - } else if (bp - logname >= sizeof(logname) - 1) { + } else if ((size_t)(bp - logname) >= sizeof(logname) - 1) { error(_("%s: input overrun"), op->tty); } else { /* Echo the character... */ @@ -1109,7 +1114,7 @@ char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) } /* Set the final tty mode bits. */ -void termio_final(struct options *op, struct termios *tp, struct chardata *cp) +static void termio_final(struct options *op, struct termios *tp, struct chardata *cp) { /* General terminal-independent stuff. */ @@ -1185,7 +1190,7 @@ void termio_final(struct options *op, struct termios *tp, struct chardata *cp) * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242 */ -int caps_lock(char *s) +static int caps_lock(char *s) { int capslock; @@ -1199,8 +1204,7 @@ int caps_lock(char *s) } /* Convert speed string to speed code; return 0 on failure. */ -int bcode(s) -char *s; +static speed_t bcode(char *s) { struct Speedtab *sp; long speed = atol(s); @@ -1211,7 +1215,7 @@ char *s; return 0; } -void __attribute__ ((__noreturn__)) usage(FILE * out) +static void __attribute__ ((__noreturn__)) usage(FILE * out) { fprintf(out, _("\nUsage:\n" " %1$s [options] line baud_rate,... [termtype]\n" @@ -1243,9 +1247,8 @@ void __attribute__ ((__noreturn__)) usage(FILE * out) /* Report errors to console or syslog; only understands %s and %m. */ #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2) -void error(const char *fmt, ...) +static void dolog(int priority, const char *fmt, va_list ap) { - va_list ap; #ifndef USE_SYSLOG int fd; #endif @@ -1273,7 +1276,9 @@ void error(const char *fmt, ...) * %m expansion is done here as well. Too bad syslog(3) does not have a * vsprintf() like interface. */ - va_start(ap, fmt); +#ifdef __linux__ + vsnprintf (bp, sizeof(buf)-strlen(buf), fmt, ap); +#else while (*fmt && bp < &buf[BUFSIZ - 1]) { if (strncmp(fmt, "%s", 2) == 0) { xstrncpy(bp, va_arg(ap, char *), &buf[BUFSIZ - 1] - bp); @@ -1288,7 +1293,7 @@ void error(const char *fmt, ...) } } *bp = 0; - va_end(ap); +#endif /* * Write the diagnostic directly to /dev/console if we do not use the @@ -1296,7 +1301,7 @@ void error(const char *fmt, ...) */ #ifdef USE_SYSLOG openlog(program_invocation_short_name, LOG_PID, LOG_AUTHPRIV); - syslog(LOG_ERR, "%s", buf); + syslog(priority, "%s", buf); closelog(); #else /* Terminate with CR-LF since the console mode is unknown. */ @@ -1306,7 +1311,27 @@ void error(const char *fmt, ...) close(fd); } #endif /* USE_SYSLOG */ +} + +static void error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + dolog(LOG_ERR, fmt, ap); + va_end(ap); + /* Be kind to init(8). */ sleep(10); exit(EXIT_FAILURE); } + +static void warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + dolog(LOG_WARNING, fmt, ap); + va_end(ap); +} + -- 1.6.0.2 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html