Check one and only one of start, stop are given.
[oweals/busybox.git] / loginutils / getty.c
index 0176d621cfdb2db3b50e3af87eac35335ed5dcc5..4219ff821b1ae1c87f8f05539112bece7ff53151 100644 (file)
 
 #define _PATH_LOGIN     "/bin/login"
 
-#ifdef linux
+ /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
+#ifdef CONFIG_SYSLOGD
 #include <sys/param.h>
 #define USE_SYSLOG
-#endif
-
-extern void updwtmp(const char *filename, const struct utmp *ut);
-
- /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
-#ifdef USE_SYSLOG
 #include <syslog.h>
 #endif
 
@@ -52,7 +47,10 @@ extern void updwtmp(const char *filename, const struct utmp *ut);
 
 #ifdef LOGIN_PROCESS                   /* defined in System V utmp.h */
 #define        SYSV_STYLE                              /* select System V style getty */
+#ifdef CONFIG_FEATURE_U_W_TMP
+extern void updwtmp(const char *filename, const struct utmp *ut);
 #endif
+#endif  /* LOGIN_PROCESS */
 
  /*
   * Things you may want to modify.
@@ -72,8 +70,6 @@ extern void updwtmp(const char *filename, const struct utmp *ut);
 #include <time.h>
 #endif
 
-#define LOGIN " login: "               /* login prompt */
-
 /* Some shorthands for control characters. */
 
 #define CTL(x)         (x ^ 0100)      /* Assumes ASCII dialect */
@@ -173,6 +169,7 @@ struct chardata init_chardata = {
        0,                                                      /* no capslock */
 };
 
+#if 0
 struct Speedtab {
        long speed;
        int code;
@@ -215,10 +212,10 @@ static struct Speedtab speedtab[] = {
 #endif
        {0, 0},
 };
+#endif
 
 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);
 static void open_tty(char *tty, struct termio *tp, int local);
 static void termio_init(struct termio *tp, int speed, struct options *op);
 static void auto_baud(struct termio *tp);
@@ -234,6 +231,10 @@ static int caps_lock(const char *s);
 static int bcode(const char *s);
 static void error(const char *fmt, ...) __attribute__ ((noreturn));
 
+#ifdef CONFIG_FEATURE_U_W_TMP
+static void update_utmp(char *line);
+#endif
+
 /* The following is used for understandable diagnostics. */
 
 /* Fake hostname for ut_host specified on command line. */
@@ -264,7 +265,7 @@ int getty_main(int argc, char **argv)
        };
 
 #ifdef DEBUGGING
-       dbf = xfopen(DEBUGTERM, "w");
+       dbf = bb_xfopen(DEBUGTERM, "w");
 
        {
                int i;
@@ -286,8 +287,11 @@ int getty_main(int argc, char **argv)
 
        /* Update the utmp file. */
 
+
 #ifdef SYSV_STYLE
+#ifdef CONFIG_FEATURE_U_W_TMP
        update_utmp(options.tty);
+#endif
 #endif
 
        debug("calling open_tty\n");
@@ -381,34 +385,20 @@ static void parse_args(int argc, char **argv, struct options *op)
                switch (c) {
                case 'I':
                        if (!(op->initstring = strdup(optarg)))
-                               error("can't malloc initstring");
+                               error(bb_msg_memory_exhausted);
                                
                        {
-                               char ch, *p, *q;
-                               int i;
+                               const char *p;
+                               char *q;
 
                                /* copy optarg into op->initstring decoding \ddd
                                   octal codes into chars */
                                q = op->initstring;
                                p = optarg;
                                while (*p) {
-                                       if (*p == '\\') {       /* know \\ means \ */
+                                       if (*p == '\\') {
                                                p++;
-                                               if (*p == '\\') {
-                                                       ch = '\\';
-                                                       p++;
-                                               } else {        /* handle \000 - \177 */
-                                                       ch = 0;
-                                                       for (i = 1; i <= 3; i++) {
-                                                               if (*p >= '0' && *p <= '7') {
-                                                                       ch <<= 3;
-                                                                       ch += *p - '0';
-                                                                       p++;
-                                                               } else
-                                                                       break;
-                                                       }
-                                               }
-                                               *q++ = ch;
+                                               *q++ = bb_process_escape_sequence(&p);
                                        } else {
                                                *q++ = *p++;
                                        }
@@ -451,12 +441,12 @@ static void parse_args(int argc, char **argv, struct options *op)
                        op->flags |= F_WAITCRLF;
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
        debug("after getopt loop\n");
        if (argc < optind + 2)          /* check parameter count */
-               show_usage();
+               bb_show_usage();
 
        /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
        if ('0' <= argv[optind][0] && argv[optind][0] <= '9') {
@@ -492,6 +482,7 @@ static void parse_speeds(struct options *op, char *arg)
 }
 
 #ifdef SYSV_STYLE
+#ifdef CONFIG_FEATURE_U_W_TMP
 
 /* update_utmp - update our utmp entry */
 static void update_utmp(char *line)
@@ -544,7 +535,8 @@ static void update_utmp(char *line)
        }
 }
 
-#endif
+#endif /* CONFIG_FEATURE_U_W_TMP */
+#endif /* SYSV_STYLE */
 
 /* open_tty - set up tty as standard { input, output, error } */
 static void open_tty(char *tty, struct termio *tp, int local)
@@ -757,142 +749,10 @@ static void auto_baud(struct termio *tp)
 /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
 static void do_prompt(struct options *op, struct termio *tp)
 {
-#ifdef ISSUE
-       FILE *fd;
-       int oflag;
-       int c;
-       struct utsname uts;
-
-       (void) uname(&uts);
-#endif
-
-       (void) write(1, "\r\n", 2);     /* start a new line */
 #ifdef ISSUE                                   /* optional: show /etc/issue */
-       if ((op->flags & F_ISSUE) && (fd = fopen(op->issue, "r"))) {
-               oflag = tp->c_oflag;    /* save current setting */
-               tp->c_oflag |= (ONLCR | OPOST); /* map NL in output to CR-NL */
-               (void) ioctl(0, TCSETAW, tp);
-
-
-               while ((c = getc(fd)) != EOF) {
-                       if (c == '\\') {
-                               c = getc(fd);
-
-                               switch (c) {
-                               case 's':
-                                       (void) printf("%s", uts.sysname);
-                                       break;
-
-                               case 'n':
-                                       (void) printf("%s", uts.nodename);
-                                       break;
-
-                               case 'r':
-                                       (void) printf("%s", uts.release);
-                                       break;
-
-                               case 'v':
-                                       (void) printf("%s", uts.version);
-                                       break;
-
-                               case 'm':
-                                       (void) printf("%s", uts.machine);
-                                       break;
-
-                               case 'o':
-                               {
-                                       char domainname[256];
-
-                                       getdomainname(domainname, sizeof(domainname));
-                                       domainname[sizeof(domainname) - 1] = '\0';
-                                       printf("%s", domainname);
-                               }
-                                       break;
-
-                               case 'd':
-                               case 't':
-                               {
-                                       char *weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu",
-                                               "Fri", "Sat"
-                                       };
-                                       char *month[] = { "Jan", "Feb", "Mar", "Apr", "May",
-                                               "Jun", "Jul", "Aug", "Sep", "Oct",
-                                               "Nov", "Dec"
-                                       };
-                                       time_t now;
-                                       struct tm *tm;
-
-                                       (void) time(&now);
-                                       tm = localtime(&now);
-
-                                       if (c == 'd')
-                                               (void) printf("%s %s %d  %d",
-                                                                         weekday[tm->tm_wday],
-                                                                         month[tm->tm_mon], tm->tm_mday,
-                                                                         tm->tm_year <
-                                                                         70 ? tm->tm_year +
-                                                                         2000 : tm->tm_year + 1900);
-                                       else
-                                               (void) printf("%02d:%02d:%02d", tm->tm_hour,
-                                                                         tm->tm_min, tm->tm_sec);
-
-                                       break;
-                               }
-
-                               case 'l':
-                                       (void) printf("%s", op->tty);
-                                       break;
-
-                               case 'b':
-                               {
-                                       int i;
-
-                                       for (i = 0; speedtab[i].speed; i++) {
-                                               if (speedtab[i].code == (tp->c_cflag & CBAUD)) {
-                                                       printf("%ld", speedtab[i].speed);
-                                                       break;
-                                               }
-                                       }
-                                       break;
-                               }
-                               case 'u':
-                               case 'U':
-                               {
-                                       int users = 0;
-                                       struct utmp *ut;
-
-                                       setutent();
-                                       while ((ut = getutent()))
-                                               if (ut->ut_type == USER_PROCESS)
-                                                       users++;
-                                       endutent();
-                                       printf("%d ", users);
-                                       if (c == 'U')
-                                               printf((users == 1) ? "user" : "users");
-                                       break;
-                               }
-                               default:
-                                       (void) putchar(c);
-                               }
-                       } else
-                               (void) putchar(c);
-               }
-               fflush(stdout);
-
-               tp->c_oflag = oflag;    /* restore settings */
-               (void) ioctl(0, TCSETAW, tp);   /* wait till output is gone */
-               (void) fclose(fd);
-       }
+       print_login_issue(op->issue, op->tty);
 #endif
-#ifdef __linux__
-       {
-               char hn[MAXHOSTNAMELEN + 1];
-
-               (void) gethostname(hn, MAXHOSTNAMELEN);
-               write(1, hn, strlen(hn));
-       }
-#endif
-       (void) write(1, LOGIN, sizeof(LOGIN) - 1);      /* always show login prompt */
+       print_login_prompt();
 }
 
 /* next_speed - select next baud rate */
@@ -1029,11 +889,7 @@ static void termio_final(struct options *op, struct termio *tp, struct chardata
        tp->c_cc[VQUIT] = DEF_QUIT;     /* default quit */
        tp->c_cc[VEOF] = DEF_EOF;       /* default EOF character */
        tp->c_cc[VEOL] = DEF_EOL;
-#ifdef __linux__
        tp->c_cc[VSWTC] = DEF_SWITCH;   /* default switch character */
-#else
-       tp->c_cc[VSWTCH] = DEF_SWITCH;  /* default switch character */
-#endif
 
        /* Account for special characters seen in input. */
 
@@ -1099,6 +955,7 @@ static int caps_lock(const char *s)
 /* bcode - convert speed string to speed code; return 0 on failure */
 static int bcode(const char *s)
 {
+#if 0
        struct Speedtab *sp;
        long speed = atol(s);
 
@@ -1106,6 +963,14 @@ static int bcode(const char *s)
                if (sp->speed == speed)
                        return (sp->code);
        return (0);
+#else
+       int r;
+
+       if ((r = bb_value_to_baud(atol(s))) > 0) {
+               return r;
+       }
+       return 0;
+#endif
 }
 
 /* error - report errors to console or syslog; only understands %s and %m */
@@ -1128,7 +993,7 @@ static void error(const char *fmt, ...)
        buf[0] = '\0';
        bp = buf;
 #else
-       strncpy(buf, applet_name, 256);
+       strncpy(buf, bb_applet_name, 256);
        strncat(buf, ": ", 256);
        buf[255] = 0;
        bp = buf + strlen(buf);