~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to login-utils/agetty.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* agetty.c - another getty program for Linux. By W. Z. Venema 1989
2
 
   Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
3
 
   This program is freely distributable. The entire man-page used to
4
 
   be here. Now read the real man-page agetty.8 instead.
5
 
 
6
 
   -f option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
7
 
   
8
 
   1999-02-22 Arkadiusz Miļæ½kiewicz <misiek@pld.ORG.PL>
9
 
   - added Native Language Support
10
 
 
11
 
   1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
12
 
   - enable hardware flow control before displaying /etc/issue
13
 
   
14
 
*/
15
 
 
16
 
#include <stdio.h>
17
 
#include <unistd.h>
18
 
#include <stdlib.h>
19
 
#include <string.h>
20
 
#include <termios.h>
21
 
#include <signal.h>
22
 
#include <errno.h>
23
 
#include <sys/types.h>
24
 
#include <sys/stat.h>
25
 
#include <fcntl.h>
26
 
#include <stdarg.h>
27
 
#include <ctype.h>
28
 
#include <utmp.h>
29
 
#include <getopt.h>
30
 
#include <time.h>
31
 
#include <sys/file.h>
32
 
#include <sys/socket.h>
33
 
#include <netdb.h>
34
 
 
35
 
#include "strutils.h"
36
 
#include "nls.h"
37
 
#include "pathnames.h"
38
 
#include "c.h"
39
 
 
40
 
#ifdef __linux__
41
 
#include <sys/param.h>
42
 
#define USE_SYSLOG
43
 
#endif
44
 
 
45
 
 /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
46
 
 
47
 
#ifdef  USE_SYSLOG
48
 
#include <syslog.h>
49
 
#endif
50
 
 
51
 
 /*
52
 
  * Some heuristics to find out what environment we are in: if it is not
53
 
  * System V, assume it is SunOS 4.
54
 
  */
55
 
 
56
 
#ifdef LOGIN_PROCESS                    /* defined in System V utmp.h */
57
 
#define SYSV_STYLE                      /* select System V style getty */
58
 
#endif
59
 
 
60
 
 /*
61
 
  * Things you may want to modify.
62
 
  * 
63
 
  * If ISSUE is not defined, agetty will never display the contents of the
64
 
  * /etc/issue file. You will not want to spit out large "issue" files at the
65
 
  * wrong baud rate. Relevant for System V only.
66
 
  * 
67
 
  * You may disagree with the default line-editing etc. characters defined
68
 
  * below. Note, however, that DEL cannot be used for interrupt generation
69
 
  * and for line editing at the same time.
70
 
  */
71
 
 
72
 
#ifdef  SYSV_STYLE
73
 
#define ISSUE "/etc/issue"              /* displayed before the login prompt */
74
 
#include <sys/utsname.h>
75
 
#endif
76
 
 
77
 
#define LOGIN " login: "                /* login prompt */
78
 
 
79
 
/* Some shorthands for control characters. */
80
 
 
81
 
#define CTL(x)          (x ^ 0100)      /* Assumes ASCII dialect */
82
 
#define CR              CTL('M')        /* carriage return */
83
 
#define NL              CTL('J')        /* line feed */
84
 
#define BS              CTL('H')        /* back space */
85
 
#define DEL             CTL('?')        /* delete */
86
 
 
87
 
/* Defaults for line-editing etc. characters; you may want to change this. */
88
 
 
89
 
#define DEF_ERASE       DEL             /* default erase character */
90
 
#define DEF_INTR        CTL('C')        /* default interrupt character */
91
 
#define DEF_QUIT        CTL('\\')       /* default quit char */
92
 
#define DEF_KILL        CTL('U')        /* default kill char */
93
 
#define DEF_EOF         CTL('D')        /* default EOF char */
94
 
#define DEF_EOL         0
95
 
#define DEF_SWITCH      0               /* default switch char */
96
 
 
97
 
#ifndef MAXHOSTNAMELEN
98
 
# ifdef HOST_NAME_MAX
99
 
#  define MAXHOSTNAMELEN HOST_NAME_MAX
100
 
# else
101
 
#  define MAXHOSTNAMELEN 64
102
 
# endif
103
 
#endif
104
 
 
105
 
 /*
106
 
  * When multiple baud rates are specified on the command line, the first one
107
 
  * we will try is the first one specified.
108
 
  */
109
 
 
110
 
#define FIRST_SPEED     0
111
 
 
112
 
/* Storage for command-line options. */
113
 
 
114
 
#define MAX_SPEED       10              /* max. nr. of baud rates */
115
 
 
116
 
struct options {
117
 
    int     flags;                      /* toggle switches, see below */
118
 
    int     timeout;                    /* time-out period */
119
 
    char   *login;                      /* login program */
120
 
    char   *tty;                        /* name of tty */
121
 
    char   *initstring;                 /* modem init string */
122
 
    char   *issue;                      /* alternative issue file */
123
 
    int     numspeed;                   /* number of baud rates to try */
124
 
    int     speeds[MAX_SPEED];          /* baud rates to be tried */
125
 
    int     eightbits;                  /* assume 8bit-clean tty */
126
 
};
127
 
 
128
 
#define F_PARSE         (1<<0)          /* process modem status messages */
129
 
#define F_ISSUE         (1<<1)          /* display /etc/issue */
130
 
#define F_RTSCTS        (1<<2)          /* enable RTS/CTS flow control */
131
 
#define F_LOCAL         (1<<3)          /* force local */
132
 
#define F_INITSTRING    (1<<4)          /* initstring is set */
133
 
#define F_WAITCRLF      (1<<5)          /* wait for CR or LF */
134
 
#define F_CUSTISSUE     (1<<6)          /* give alternative issue file */
135
 
#define F_NOPROMPT      (1<<7)          /* don't ask for login name! */
136
 
#define F_LCUC          (1<<8)          /* Support for *LCUC stty modes */
137
 
#define F_KEEPSPEED     (1<<9)          /* Follow baud rate from kernel */
138
 
#define F_KEEPCFLAGS    (1<<10)         /* Reuse c_cflags setup from kernel */
139
 
 
140
 
/* Storage for things detected while the login name was read. */
141
 
 
142
 
struct chardata {
143
 
    int     erase;                      /* erase character */
144
 
    int     kill;                       /* kill character */
145
 
    int     eol;                        /* end-of-line character */
146
 
    int     parity;                     /* what parity did we see */
147
 
    int     capslock;                   /* upper case without lower case */
148
 
};
149
 
 
150
 
/* Initial values for the above. */
151
 
 
152
 
struct chardata init_chardata = {
153
 
    DEF_ERASE,                          /* default erase character */
154
 
    DEF_KILL,                           /* default kill character */
155
 
    13,                                 /* default eol char */
156
 
    0,                                  /* space parity */
157
 
    0,                                  /* no capslock */
158
 
};
159
 
 
160
 
struct Speedtab {
161
 
    long    speed;
162
 
    int     code;
163
 
};
164
 
 
165
 
static struct Speedtab speedtab[] = {
166
 
    { 50, B50 },
167
 
    { 75, B75 },
168
 
    { 110, B110 },
169
 
    { 134, B134 },
170
 
    { 150, B150 },
171
 
    { 200, B200 },
172
 
    { 300, B300 },
173
 
    { 600, B600 },
174
 
    { 1200, B1200 },
175
 
    { 1800, B1800 },
176
 
    { 2400, B2400 },
177
 
    { 4800, B4800 },
178
 
    { 9600, B9600 },
179
 
#ifdef  B19200
180
 
    { 19200, B19200 },
181
 
#endif
182
 
#ifdef  B38400
183
 
    { 38400, B38400 },
184
 
#endif
185
 
#ifdef  EXTA
186
 
    { 19200, EXTA },
187
 
#endif
188
 
#ifdef  EXTB
189
 
    { 38400, EXTB },
190
 
#endif
191
 
#ifdef B57600
192
 
    { 57600, B57600 },
193
 
#endif
194
 
#ifdef B115200
195
 
    { 115200, B115200 },
196
 
#endif
197
 
#ifdef B230400
198
 
    { 230400, B230400 },
199
 
#endif
200
 
    { 0, 0 },
201
 
};
202
 
 
203
 
#define P_(s) s
204
 
int main P_((int argc, char **argv));
205
 
void parse_args P_((int argc, char **argv, struct options *op));
206
 
void parse_speeds P_((struct options *op, char *arg));
207
 
void update_utmp P_((char *line));
208
 
void open_tty P_((char *tty, struct termios *tp, int local));
209
 
void termio_init P_((struct termios *tp, struct options *op));
210
 
void auto_baud P_((struct termios *tp));
211
 
void do_prompt P_((struct options *op, struct termios *tp));
212
 
void next_speed P_((struct termios *tp, struct options *op));
213
 
char *get_logname P_((struct options *op, struct chardata *cp, struct termios *tp));
214
 
void termio_final P_((struct options *op, struct termios *tp, struct chardata *cp));
215
 
int caps_lock P_((char *s));
216
 
int bcode P_((char *s));
217
 
void usage P_((void));
218
 
void error P_((const char *, ...));
219
 
#undef P_
220
 
 
221
 
/* The following is used for understandable diagnostics. */
222
 
 
223
 
char *progname;
224
 
 
225
 
/* Fake hostname for ut_host specified on command line. */
226
 
char *fakehost = NULL;
227
 
 
228
 
/* ... */
229
 
#ifdef DEBUGGING
230
 
#define debug(s) fprintf(dbf,s); fflush(dbf)
231
 
FILE *dbf;
232
 
#else
233
 
#define debug(s) /* nothing */
234
 
#endif
235
 
 
236
 
int
237
 
main(argc, argv)
238
 
     int     argc;
239
 
     char  **argv;
240
 
{
241
 
    char   *logname = NULL;             /* login name, given to /bin/login */
242
 
    struct chardata chardata;           /* set by get_logname() */
243
 
    struct termios termios;             /* terminal mode bits */
244
 
    static struct options options = {
245
 
        F_ISSUE,                        /* show /etc/issue (SYSV_STYLE) */
246
 
        0,                              /* no timeout */
247
 
        _PATH_LOGIN,                    /* default login program */
248
 
        "tty1",                         /* default tty line */
249
 
        "",                             /* modem init string */
250
 
        ISSUE,                          /* default issue file */
251
 
        0,                              /* no baud rates known yet */
252
 
    };
253
 
 
254
 
       setlocale(LC_ALL, "");
255
 
       bindtextdomain(PACKAGE, LOCALEDIR);
256
 
       textdomain(PACKAGE);
257
 
    
258
 
    /* The BSD-style init command passes us a useless process name. */
259
 
 
260
 
#ifdef  SYSV_STYLE
261
 
       {
262
 
               char *ptr;
263
 
               progname = argv[0];
264
 
               if ((ptr = strrchr(argv[0], '/')))
265
 
                       progname = ++ptr;
266
 
       }
267
 
#else
268
 
       progname = "agetty";
269
 
#endif
270
 
 
271
 
#ifdef DEBUGGING
272
 
        dbf = fopen("/dev/ttyp0", "w");
273
 
 
274
 
        {       int i;
275
 
        
276
 
                for(i = 1; i < argc; i++) {
277
 
                        debug(argv[i]);
278
 
                }
279
 
        }
280
 
#endif
281
 
 
282
 
    /* Parse command-line arguments. */
283
 
 
284
 
    parse_args(argc, argv, &options);
285
 
 
286
 
#ifdef __linux__
287
 
        setsid();
288
 
#endif
289
 
        
290
 
    /* Update the utmp file. */
291
 
 
292
 
#ifdef  SYSV_STYLE
293
 
    update_utmp(options.tty);
294
 
#endif
295
 
 
296
 
    debug("calling open_tty\n");
297
 
    /* Open the tty as standard { input, output, error }. */
298
 
    open_tty(options.tty, &termios, options.flags & F_LOCAL);
299
 
 
300
 
    tcsetpgrp(0, getpid());
301
 
    /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
302
 
    debug("calling termio_init\n");
303
 
    termio_init(&termios, &options);
304
 
 
305
 
    /* write the modem init string and DON'T flush the buffers */
306
 
    if (options.flags & F_INITSTRING) {
307
 
        debug("writing init string\n");
308
 
        ignore_result( write(1, options.initstring, strlen(options.initstring)) );
309
 
    }
310
 
 
311
 
    if (!(options.flags & F_LOCAL)) {
312
 
        /* go to blocking write mode unless -L is specified */
313
 
        fcntl(1, F_SETFL, fcntl(1, F_GETFL, 0) & ~O_NONBLOCK);
314
 
    }
315
 
 
316
 
    /* Optionally detect the baud rate from the modem status message. */
317
 
    debug("before autobaud\n");
318
 
    if (options.flags & F_PARSE)
319
 
        auto_baud(&termios);
320
 
 
321
 
    /* Set the optional timer. */
322
 
    if (options.timeout)
323
 
        (void) alarm((unsigned) options.timeout);
324
 
 
325
 
    /* optionally wait for CR or LF before writing /etc/issue */
326
 
    if (options.flags & F_WAITCRLF) {
327
 
        char ch;
328
 
 
329
 
        debug("waiting for cr-lf\n");
330
 
        while(read(0, &ch, 1) == 1) {
331
 
            ch &= 0x7f;   /* strip "parity bit" */
332
 
#ifdef DEBUGGING
333
 
            fprintf(dbf, "read %c\n", ch);
334
 
#endif
335
 
            if (ch == '\n' || ch == '\r') break;
336
 
        }
337
 
    }
338
 
 
339
 
    chardata = init_chardata;
340
 
    if (!(options.flags & F_NOPROMPT)) {
341
 
        /* Read the login name. */
342
 
        debug("reading login name\n");
343
 
        while ((logname = get_logname(&options, &chardata, &termios)) == 0)
344
 
          next_speed(&termios, &options);
345
 
    }
346
 
 
347
 
    /* Disable timer. */
348
 
 
349
 
    if (options.timeout)
350
 
        (void) alarm(0);
351
 
 
352
 
    /* Finalize the termios settings. */
353
 
 
354
 
    termio_final(&options, &termios, &chardata);
355
 
 
356
 
    /* Now the newline character should be properly written. */
357
 
 
358
 
    ignore_result( write(1, "\n", 1) );
359
 
 
360
 
    /* Let the login program take care of password validation. */
361
 
 
362
 
    (void) execl(options.login, options.login, "--", logname, NULL);
363
 
    error(_("%s: can't exec %s: %m"), options.tty, options.login);
364
 
}
365
 
 
366
 
/* parse-args - parse command-line arguments */
367
 
 
368
 
void
369
 
parse_args(argc, argv, op)
370
 
     int     argc;
371
 
     char  **argv;
372
 
     struct options *op;
373
 
{
374
 
    extern char *optarg;                /* getopt */
375
 
    extern int optind;                  /* getopt */
376
 
    int     c;
377
 
 
378
 
    while (isascii(c = getopt(argc, argv, "8cI:LH:f:hil:mst:wUn"))) {
379
 
        switch (c) {
380
 
        case 'c':
381
 
            op->flags |= F_KEEPCFLAGS;
382
 
            break;
383
 
        case '8':
384
 
            op->eightbits = 1;
385
 
            break;
386
 
        case 'I':
387
 
            if (!(op->initstring = malloc(strlen(optarg)+1))) {
388
 
                error(_("can't malloc initstring"));
389
 
                break;
390
 
            }
391
 
            {
392
 
                char ch, *p, *q;
393
 
                int i;
394
 
 
395
 
                /* copy optarg into op->initstring decoding \ddd
396
 
                   octal codes into chars */
397
 
                q = op->initstring;
398
 
                p = optarg;
399
 
                while (*p) {
400
 
                    if (*p == '\\') {           /* know \\ means \ */
401
 
                        p++;
402
 
                        if (*p == '\\') {
403
 
                            ch = '\\';
404
 
                            p++;
405
 
                        } else {                /* handle \000 - \177 */
406
 
                            ch = 0;
407
 
                            for (i = 1; i <= 3; i++) {
408
 
                                if (*p >= '0' && *p <= '7') {
409
 
                                    ch <<= 3;
410
 
                                    ch += *p - '0';
411
 
                                    p++;
412
 
                                } else 
413
 
                                  break;
414
 
                            }
415
 
                        }
416
 
                        *q++ = ch;
417
 
                    } else {
418
 
                        *q++ = *p++;
419
 
                    }
420
 
                }
421
 
                *q = '\0';
422
 
            }
423
 
            op->flags |= F_INITSTRING;
424
 
            break;
425
 
 
426
 
        case 'L':                               /* force local */
427
 
            op->flags |= F_LOCAL;
428
 
            break;
429
 
        case 'H':                               /* fake login host */
430
 
            fakehost = optarg;
431
 
            break;
432
 
        case 'f':                               /* custom issue file */
433
 
            op->flags |= F_CUSTISSUE;
434
 
            op->issue = optarg;
435
 
            break;
436
 
        case 'h':                               /* enable h/w flow control */
437
 
            op->flags |= F_RTSCTS;
438
 
            break;
439
 
        case 'i':                               /* do not show /etc/issue */
440
 
            op->flags &= ~F_ISSUE;
441
 
            break;
442
 
        case 'l':
443
 
            op->login = optarg;                 /* non-default login program */
444
 
            break;
445
 
        case 'm':                               /* parse modem status message */
446
 
            op->flags |= F_PARSE;
447
 
            break;
448
 
        case 'n':
449
 
            op->flags |= F_NOPROMPT;
450
 
            break;
451
 
        case 's':
452
 
            op->flags |= F_KEEPSPEED;           /* keep kernel defined speed */
453
 
            break;
454
 
        case 't':                               /* time out */
455
 
            if ((op->timeout = atoi(optarg)) <= 0)
456
 
                error(_("bad timeout value: %s"), optarg);
457
 
            break;
458
 
        case 'w':
459
 
            op->flags |= F_WAITCRLF;
460
 
            break;
461
 
        case 'U':
462
 
            op->flags |= F_LCUC;
463
 
            break;
464
 
        default:
465
 
            usage();
466
 
        }
467
 
    }
468
 
        debug("after getopt loop\n");
469
 
    if (argc < optind + 2)                      /* check parameter count */
470
 
        usage();
471
 
 
472
 
    /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
473
 
    if('0' <= argv[optind][0] && argv[optind][0] <= '9') {
474
 
        /* a number first, assume it's a speed (BSD style) */
475
 
        parse_speeds(op, argv[optind++]);       /* baud rate(s) */
476
 
        op->tty = argv[optind];                 /* tty name */
477
 
    } else {
478
 
        op->tty = argv[optind++];               /* tty name */
479
 
        parse_speeds(op, argv[optind]);         /* baud rate(s) */
480
 
    }
481
 
 
482
 
    optind++;
483
 
    if (argc > optind && argv[optind])
484
 
        setenv ("TERM", argv[optind], 1);
485
 
 
486
 
#ifdef DO_DEVFS_FIDDLING
487
 
    /*
488
 
     * some devfs junk, following Goswin Brederlow:
489
 
     *   turn ttyS<n> into tts/<n>
490
 
     *   turn tty<n> into vc/<n>
491
 
     */
492
 
    if (op->tty && strlen(op->tty) < 90) {
493
 
            char dev_name[100];
494
 
            struct stat st;
495
 
 
496
 
            if (strncmp(op->tty, "ttyS", 4) == 0) {
497
 
                    strcpy(dev_name, "/dev/");
498
 
                    strcat(dev_name, op->tty);
499
 
                    if (stat(dev_name, &st) < 0) {
500
 
                            strcpy(dev_name, "/dev/tts/");
501
 
                            strcat(dev_name, op->tty + 4);
502
 
                            if (stat(dev_name, &st) == 0)
503
 
                                    op->tty = strdup(dev_name + 5);
504
 
                    }
505
 
            } else if (strncmp(op->tty, "tty", 3) == 0) {
506
 
                    strcpy(dev_name, "/dev/");
507
 
                    strncat(dev_name, op->tty, 90);
508
 
                    if (stat(dev_name, &st) < 0) {
509
 
                            strcpy(dev_name, "/dev/vc/");
510
 
                            strcat(dev_name, op->tty + 3);
511
 
                            if (stat(dev_name, &st) == 0)
512
 
                                    op->tty = strdup(dev_name + 5);
513
 
                    }
514
 
            }
515
 
    }
516
 
#endif
517
 
 
518
 
    debug("exiting parseargs\n");
519
 
}
520
 
 
521
 
/* parse_speeds - parse alternate baud rates */
522
 
 
523
 
void
524
 
parse_speeds(op, arg)
525
 
     struct options *op;
526
 
     char   *arg;
527
 
{
528
 
    char   *cp;
529
 
 
530
 
        debug("entered parse_speeds\n");
531
 
    for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
532
 
        if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
533
 
            error(_("bad speed: %s"), cp);
534
 
        if (op->numspeed >= MAX_SPEED)
535
 
            error(_("too many alternate speeds"));
536
 
    }
537
 
    debug("exiting parsespeeds\n");
538
 
}
539
 
 
540
 
#ifdef  SYSV_STYLE
541
 
 
542
 
/* update_utmp - update our utmp entry */
543
 
void
544
 
update_utmp(line)
545
 
     char   *line;
546
 
{
547
 
    struct  utmp ut;
548
 
    time_t  t;
549
 
    int     mypid = getpid();
550
 
    struct  utmp *utp;
551
 
 
552
 
    /*
553
 
     * The utmp file holds miscellaneous information about things started by
554
 
     * /sbin/init and other system-related events. Our purpose is to update
555
 
     * the utmp entry for the current process, in particular the process type
556
 
     * and the tty line we are listening to. Return successfully only if the
557
 
     * utmp file can be opened for update, and if we are able to find our
558
 
     * entry in the utmp file.
559
 
     */
560
 
 
561
 
    utmpname(_PATH_UTMP);
562
 
    setutent();
563
 
 
564
 
    /* Find mypid in utmp. Earlier code here tested only
565
 
       utp->ut_type != INIT_PROCESS, so maybe the >= here should be >.
566
 
       The present code is taken from login.c, so if this is changed,
567
 
       maybe login has to be changed as well. */
568
 
    while ((utp = getutent()))
569
 
            if (utp->ut_pid == mypid
570
 
                && utp->ut_type >= INIT_PROCESS
571
 
                && utp->ut_type <= DEAD_PROCESS)
572
 
                    break;
573
 
 
574
 
    if (utp) {
575
 
        memcpy(&ut, utp, sizeof(ut));
576
 
    } else {
577
 
        /* some inits don't initialize utmp... */
578
 
        memset(&ut, 0, sizeof(ut));
579
 
        strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
580
 
    }
581
 
        
582
 
    strncpy(ut.ut_user, "LOGIN", sizeof(ut.ut_user));
583
 
    strncpy(ut.ut_line, line, sizeof(ut.ut_line));
584
 
    if (fakehost)
585
 
        strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
586
 
    time(&t);
587
 
    ut.ut_time = t;
588
 
    ut.ut_type = LOGIN_PROCESS;
589
 
    ut.ut_pid = mypid;
590
 
 
591
 
    pututline(&ut);
592
 
    endutent();
593
 
 
594
 
    {
595
 
#ifdef HAVE_UPDWTMP
596
 
        updwtmp(_PATH_WTMP, &ut);
597
 
#else
598
 
        int ut_fd;
599
 
        int lf;
600
 
 
601
 
        if ((lf = open(_PATH_WTMPLOCK, O_CREAT|O_WRONLY, 0660)) >= 0) {
602
 
            flock(lf, LOCK_EX);
603
 
            if ((ut_fd = open(_PATH_WTMP, O_APPEND|O_WRONLY)) >= 0) {
604
 
                ignore_result( write(ut_fd, &ut, sizeof(ut)) );
605
 
                close(ut_fd);
606
 
            }
607
 
            flock(lf, LOCK_UN);
608
 
            close(lf);
609
 
        }
610
 
#endif
611
 
    }
612
 
}
613
 
 
614
 
#endif
615
 
 
616
 
/* open_tty - set up tty as standard { input, output, error } */
617
 
void
618
 
open_tty(tty, tp, local)
619
 
     char   *tty;
620
 
     struct termios *tp;
621
 
     int    local;
622
 
{
623
 
    /* Get rid of the present standard { output, error} if any. */
624
 
 
625
 
    (void) close(1);
626
 
    (void) close(2);
627
 
    errno = 0;                                  /* ignore above errors */
628
 
 
629
 
    /* Set up new standard input, unless we are given an already opened port. */
630
 
 
631
 
    if (strcmp(tty, "-")) {
632
 
        struct stat st;
633
 
 
634
 
        /* Sanity checks... */
635
 
 
636
 
        if (chdir("/dev"))
637
 
            error(_("/dev: chdir() failed: %m"));
638
 
        if (stat(tty, &st) < 0)
639
 
            error("/dev/%s: %m", tty);
640
 
        if ((st.st_mode & S_IFMT) != S_IFCHR)
641
 
            error(_("/dev/%s: not a character device"), tty);
642
 
 
643
 
        /* Open the tty as standard input. */
644
 
 
645
 
        (void) close(0);
646
 
        errno = 0;                              /* ignore close(2) errors */
647
 
 
648
 
        debug("open(2)\n");
649
 
        if (open(tty, O_RDWR|O_NONBLOCK, 0) != 0)
650
 
            error(_("/dev/%s: cannot open as standard input: %m"), tty);
651
 
 
652
 
    } else {
653
 
 
654
 
        /*
655
 
         * Standard input should already be connected to an open port. Make
656
 
         * sure it is open for read/write.
657
 
         */
658
 
 
659
 
        if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR)
660
 
            error(_("%s: not open for read/write"), tty);
661
 
    }
662
 
 
663
 
    /* Set up standard output and standard error file descriptors. */
664
 
    debug("duping\n");
665
 
    if (dup(0) != 1 || dup(0) != 2)             /* set up stdout and stderr */
666
 
        error(_("%s: dup problem: %m"), tty);   /* we have a problem */
667
 
 
668
 
    /*
669
 
     * The following ioctl will fail if stdin is not a tty, but also when
670
 
     * there is noise on the modem control lines. In the latter case, the
671
 
     * common course of action is (1) fix your cables (2) give the modem more
672
 
     * time to properly reset after hanging up. SunOS users can achieve (2)
673
 
     * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
674
 
     * 5 seconds seems to be a good value.
675
 
     */
676
 
 
677
 
    if (tcgetattr(0, tp) < 0)
678
 
        error("%s: tcgetattr: %m", tty);
679
 
 
680
 
    /*
681
 
     * It seems to be a terminal. Set proper protections and ownership. Mode
682
 
     * 0622 is suitable for SYSV <4 because /bin/login does not change
683
 
     * protections. SunOS 4 login will change the protections to 0620 (write
684
 
     * access for group tty) after the login has succeeded.
685
 
     *
686
 
     * Linux login(1) will change tty permissions.
687
 
     */
688
 
 
689
 
    /*
690
 
     * Let us use 0600 for Linux for the period between getty and login
691
 
     */
692
 
    ignore_result( chown(tty, 0, 0) );          /* root, sys */
693
 
    ignore_result( chmod(tty, 0600) );          /* 0622: crw--w--w- */
694
 
    errno = 0;                                  /* ignore above errors */
695
 
}
696
 
 
697
 
/* termio_init - initialize termios settings */
698
 
 
699
 
char gbuf[1024];
700
 
char area[1024];
701
 
 
702
 
void
703
 
termio_init(tp, op)
704
 
     struct termios *tp;
705
 
     struct options *op;
706
 
{
707
 
    speed_t ispeed, ospeed;
708
 
 
709
 
    if (op->flags & F_KEEPSPEED) {
710
 
        ispeed = cfgetispeed(tp);               /* save the original setting */
711
 
        ospeed = cfgetospeed(tp);
712
 
    } else
713
 
        ospeed = ispeed = op->speeds[FIRST_SPEED];
714
 
 
715
 
    /*
716
 
     * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
717
 
     * Special characters are set after we have read the login name; all
718
 
     * reads will be done in raw mode anyway. Errors will be dealt with
719
 
     * lateron.
720
 
     */
721
 
    /* flush input and output queues, important for modems! */
722
 
    (void) tcflush(0, TCIOFLUSH);
723
 
 
724
 
    tp->c_iflag = tp->c_lflag = tp->c_oflag = 0;
725
 
 
726
 
    if (!(op->flags & F_KEEPCFLAGS))
727
 
        tp->c_cflag = CS8 | HUPCL | CREAD | (tp->c_cflag & CLOCAL);
728
 
 
729
 
    /* Note that the speed is stored in the c_cflag termios field, so we have
730
 
     * set the speed always when the cflag se reseted.
731
 
     */
732
 
    cfsetispeed(tp, ispeed);
733
 
    cfsetospeed(tp, ospeed);
734
 
 
735
 
    if (op->flags & F_LOCAL) {
736
 
        tp->c_cflag |= CLOCAL;
737
 
    }
738
 
 
739
 
#ifdef HAVE_STRUCT_TERMIOS_C_LINE
740
 
    tp->c_line = 0;
741
 
#endif
742
 
    tp->c_cc[VMIN] = 1;
743
 
    tp->c_cc[VTIME] = 0;
744
 
 
745
 
    /* Optionally enable hardware flow control */
746
 
 
747
 
#ifdef  CRTSCTS
748
 
    if (op->flags & F_RTSCTS)
749
 
        tp->c_cflag |= CRTSCTS;
750
 
#endif
751
 
 
752
 
    (void) tcsetattr(0, TCSANOW, tp);
753
 
 
754
 
    /* go to blocking input even in local mode */
755
 
    fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
756
 
 
757
 
    debug("term_io 2\n");
758
 
}
759
 
 
760
 
/* auto_baud - extract baud rate from modem status message */
761
 
void
762
 
auto_baud(tp)
763
 
     struct termios *tp;
764
 
{
765
 
    int     speed;
766
 
    int     vmin;
767
 
    unsigned iflag;
768
 
    char    buf[BUFSIZ];
769
 
    char   *bp;
770
 
    int     nread;
771
 
 
772
 
    /*
773
 
     * This works only if the modem produces its status code AFTER raising
774
 
     * the DCD line, and if the computer is fast enough to set the proper
775
 
     * baud rate before the message has gone by. We expect a message of the
776
 
     * following format:
777
 
     * 
778
 
     * <junk><number><junk>
779
 
     * 
780
 
     * The number is interpreted as the baud rate of the incoming call. If the
781
 
     * modem does not tell us the baud rate within one second, we will keep
782
 
     * using the current baud rate. It is advisable to enable BREAK
783
 
     * processing (comma-separated list of baud rates) if the processing of
784
 
     * modem status messages is enabled.
785
 
     */
786
 
 
787
 
    /*
788
 
     * Use 7-bit characters, don't block if input queue is empty. Errors will
789
 
     * be dealt with lateron.
790
 
     */
791
 
 
792
 
    iflag = tp->c_iflag;
793
 
    tp->c_iflag |= ISTRIP;                      /* enable 8th-bit stripping */
794
 
    vmin = tp->c_cc[VMIN];
795
 
    tp->c_cc[VMIN] = 0;                         /* don't block if queue empty */
796
 
    tcsetattr(0, TCSANOW, tp);
797
 
 
798
 
    /*
799
 
     * Wait for a while, then read everything the modem has said so far and
800
 
     * try to extract the speed of the dial-in call.
801
 
     */
802
 
 
803
 
    (void) sleep(1);
804
 
    if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) {
805
 
        buf[nread] = '\0';
806
 
        for (bp = buf; bp < buf + nread; bp++) {
807
 
            if (isascii(*bp) && isdigit(*bp)) {
808
 
                if ((speed = bcode(bp))) {
809
 
                    cfsetispeed(tp, speed);
810
 
                    cfsetospeed(tp, speed);
811
 
                }
812
 
                break;
813
 
            }
814
 
        }
815
 
    }
816
 
    /* Restore terminal settings. Errors will be dealt with lateron. */
817
 
 
818
 
    tp->c_iflag = iflag;
819
 
    tp->c_cc[VMIN] = vmin;
820
 
    (void) tcsetattr(0, TCSANOW, tp);
821
 
}
822
 
 
823
 
/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
824
 
void
825
 
do_prompt(op, tp)
826
 
     struct options *op;
827
 
     struct termios *tp;
828
 
{
829
 
#ifdef  ISSUE
830
 
    FILE    *fd;
831
 
    int     oflag;
832
 
    int     c;
833
 
    struct utsname uts;
834
 
 
835
 
    (void) uname(&uts);
836
 
#endif
837
 
 
838
 
    ignore_result( write(1, "\r\n", 2) );                       /* start a new line */
839
 
#ifdef  ISSUE                                   /* optional: show /etc/issue */
840
 
    if ((op->flags & F_ISSUE) && (fd = fopen(op->issue, "r"))) {
841
 
        oflag = tp->c_oflag;                    /* save current setting */
842
 
        tp->c_oflag |= (ONLCR | OPOST);         /* map NL in output to CR-NL */
843
 
        (void) tcsetattr(0, TCSADRAIN, tp);
844
 
 
845
 
 
846
 
        while ((c = getc(fd)) != EOF)
847
 
        {
848
 
            if (c == '\\')
849
 
              {
850
 
                c = getc(fd);
851
 
                
852
 
                switch (c)
853
 
                  {
854
 
                  case 's':
855
 
                    (void) printf ("%s", uts.sysname);
856
 
                    break;
857
 
                    
858
 
                  case 'n':
859
 
                    (void) printf ("%s", uts.nodename);
860
 
                    break;
861
 
                    
862
 
                  case 'r':
863
 
                    (void) printf ("%s", uts.release);
864
 
                    break;
865
 
                    
866
 
                  case 'v':
867
 
                    (void) printf ("%s", uts.version);
868
 
                    break;
869
 
                    
870
 
                  case 'm':
871
 
                    (void) printf ("%s", uts.machine);
872
 
                    break;
873
 
 
874
 
                  case 'o':
875
 
                   {
876
 
                     char domainname[MAXHOSTNAMELEN+1];
877
 
#ifdef HAVE_GETDOMAINNAME
878
 
                     if (getdomainname(domainname, sizeof(domainname)))
879
 
#endif
880
 
                         strcpy(domainname, "unknown_domain");
881
 
                     domainname[sizeof(domainname)-1] = '\0';
882
 
                     printf ("%s", domainname);
883
 
                   }
884
 
                  break;
885
 
 
886
 
                  case 'O':
887
 
                   {
888
 
                        char *dom = "unknown_domain";
889
 
                        char host[MAXHOSTNAMELEN+1];
890
 
                        struct addrinfo hints, *info = NULL;
891
 
 
892
 
                        memset(&hints, 0, sizeof(hints));
893
 
                        hints.ai_flags = AI_CANONNAME;
894
 
 
895
 
                        if (gethostname(host, sizeof(host)) ||
896
 
                            getaddrinfo(host, NULL, &hints, &info) ||
897
 
                            info == NULL)
898
 
                                fputs(dom, stdout);
899
 
                        else {
900
 
                                char *canon;
901
 
 
902
 
                                if (info->ai_canonname &&
903
 
                                    (canon = strchr(info->ai_canonname, '.')))
904
 
                                        dom = canon + 1;
905
 
                                fputs(dom, stdout);
906
 
                                freeaddrinfo(info);
907
 
                        }
908
 
                   }
909
 
                  break;
910
 
 
911
 
                  case 'd':
912
 
                  case 't':
913
 
                    {
914
 
                      time_t now;
915
 
                      struct tm *tm;
916
 
 
917
 
                      (void) time (&now);
918
 
                      tm = localtime(&now);
919
 
 
920
 
                      if (c == 'd')
921
 
                        (void) printf ("%s %s %d  %d",
922
 
                                nl_langinfo(ABDAY_1 + tm->tm_wday),
923
 
                                nl_langinfo(ABMON_1 + tm->tm_mon),
924
 
                                tm->tm_mday,
925
 
                                tm->tm_year < 70 ? tm->tm_year + 2000 :
926
 
                                tm->tm_year + 1900);
927
 
                      else
928
 
                        (void) printf ("%02d:%02d:%02d",
929
 
                                tm->tm_hour, tm->tm_min, tm->tm_sec);
930
 
                      break;
931
 
                    }
932
 
 
933
 
                  case 'l':
934
 
                      (void) printf ("%s", op->tty);
935
 
                      break;
936
 
 
937
 
                  case 'b':
938
 
                    {
939
 
                        int i;
940
 
 
941
 
                        for (i = 0; speedtab[i].speed; i++) {
942
 
                            if (speedtab[i].code == cfgetispeed(tp)) {
943
 
                                printf("%ld", speedtab[i].speed);
944
 
                                break;
945
 
                            }
946
 
                        }
947
 
                        break;
948
 
                    }
949
 
                  case 'u':
950
 
                  case 'U':
951
 
                    {
952
 
                      int users = 0;
953
 
                      struct utmp *ut;
954
 
                      setutent();
955
 
                      while ((ut = getutent()))
956
 
                        if (ut->ut_type == USER_PROCESS)
957
 
                          users++;
958
 
                      endutent();
959
 
                      printf ("%d ", users);
960
 
                      if (c == 'U')
961
 
                        printf ((users == 1) ? _("user") : _("users"));
962
 
                      break;
963
 
                    }
964
 
                  default:
965
 
                    (void) putchar(c);
966
 
                  }
967
 
              }
968
 
            else
969
 
              (void) putchar(c);
970
 
        }
971
 
        fflush(stdout);
972
 
 
973
 
        tp->c_oflag = oflag;                    /* restore settings */
974
 
        (void) tcsetattr(0, TCSADRAIN, tp);     /* wait till output is gone */
975
 
        (void) fclose(fd);
976
 
    }
977
 
#endif
978
 
    {
979
 
        char hn[MAXHOSTNAMELEN+1];
980
 
        if (gethostname(hn, sizeof(hn)) == 0)
981
 
            ignore_result( write(1, hn, strlen(hn)) );
982
 
    }
983
 
    ignore_result( write(1, LOGIN, sizeof(LOGIN) - 1) );        /* always show login prompt */
984
 
}
985
 
 
986
 
/* next_speed - select next baud rate */
987
 
void
988
 
next_speed(tp, op)
989
 
     struct termios *tp;
990
 
     struct options *op;
991
 
{
992
 
    static int baud_index = -1;
993
 
 
994
 
    if (baud_index == -1)
995
 
        /*
996
 
         * if the F_KEEPSPEED flags is set then the FIRST_SPEED is not
997
 
         * tested yet (see termio_init()).
998
 
         */
999
 
        baud_index = (op->flags & F_KEEPSPEED) ? FIRST_SPEED :
1000
 
                                                 1 % op->numspeed;
1001
 
    else
1002
 
        baud_index = (baud_index + 1) % op->numspeed;
1003
 
 
1004
 
    cfsetispeed(tp, op->speeds[baud_index]);
1005
 
    cfsetospeed(tp, op->speeds[baud_index]);
1006
 
    (void) tcsetattr(0, TCSANOW, tp);
1007
 
}
1008
 
 
1009
 
/* get_logname - get user name, establish parity, speed, erase, kill, eol */
1010
 
 
1011
 
char   *get_logname(op, cp, tp)
1012
 
     struct options *op;
1013
 
     struct chardata *cp;
1014
 
     struct termios *tp;
1015
 
{
1016
 
    static char logname[BUFSIZ];
1017
 
    char   *bp;
1018
 
    char    c;                          /* input character, full eight bits */
1019
 
    char    ascval;                     /* low 7 bits of input character */
1020
 
    int     bits;                       /* # of "1" bits per character */
1021
 
    int     mask;                       /* mask with 1 bit up */
1022
 
    static char *erase[] = {            /* backspace-space-backspace */
1023
 
        "\010\040\010",                 /* space parity */
1024
 
        "\010\040\010",                 /* odd parity */
1025
 
        "\210\240\210",                 /* even parity */
1026
 
        "\210\240\210",                 /* no parity */
1027
 
    };
1028
 
 
1029
 
    /* Initialize kill, erase, parity etc. (also after switching speeds). */
1030
 
 
1031
 
    *cp = init_chardata;
1032
 
 
1033
 
    /* Flush pending input (esp. after parsing or switching the baud rate). */
1034
 
 
1035
 
    (void) sleep(1);
1036
 
    (void) tcflush(0, TCIFLUSH);
1037
 
 
1038
 
    /* Prompt for and read a login name. */
1039
 
 
1040
 
    for (*logname = 0; *logname == 0; /* void */ ) {
1041
 
 
1042
 
        /* Write issue file and prompt, with "parity" bit == 0. */
1043
 
 
1044
 
        do_prompt(op, tp);
1045
 
 
1046
 
        /* Read name, watch for break, parity, erase, kill, end-of-line. */
1047
 
 
1048
 
        for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) {
1049
 
 
1050
 
            /* Do not report trivial EINTR/EIO errors. */
1051
 
 
1052
 
            if (read(0, &c, 1) < 1) {
1053
 
                if (errno == EINTR || errno == EIO)
1054
 
                    exit(EXIT_SUCCESS);
1055
 
                error(_("%s: read: %m"), op->tty);
1056
 
            }
1057
 
            /* Do BREAK handling elsewhere. */
1058
 
 
1059
 
            if ((c == 0) && op->numspeed > 1)
1060
 
                return EXIT_SUCCESS;
1061
 
            /* Do parity bit handling. */
1062
 
 
1063
 
            if (op->eightbits) {
1064
 
                ascval = c;
1065
 
            } else if (c != (ascval = (c & 0177))) {    /* "parity" bit on */
1066
 
                for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
1067
 
                    if (mask & ascval)
1068
 
                        bits++;                 /* count "1" bits */
1069
 
                cp->parity |= ((bits & 1) ? 1 : 2);
1070
 
            }
1071
 
            /* Do erase, kill and end-of-line processing. */
1072
 
 
1073
 
            switch (ascval) {
1074
 
            case CR:
1075
 
            case NL:
1076
 
                *bp = 0;                        /* terminate logname */
1077
 
                cp->eol = ascval;               /* set end-of-line char */
1078
 
                break;
1079
 
            case BS:
1080
 
            case DEL:
1081
 
            case '#':
1082
 
                cp->erase = ascval;             /* set erase character */
1083
 
                if (bp > logname) {
1084
 
                    ignore_result( write(1, erase[cp->parity], 3) );
1085
 
                    bp--;
1086
 
                }
1087
 
                break;
1088
 
            case CTL('U'):
1089
 
            case '@':
1090
 
                cp->kill = ascval;              /* set kill character */
1091
 
                while (bp > logname) {
1092
 
                    ignore_result( write(1, erase[cp->parity], 3) );
1093
 
                    bp--;
1094
 
                }
1095
 
                break;
1096
 
            case CTL('D'):
1097
 
                exit(EXIT_SUCCESS);
1098
 
            default:
1099
 
                if (!isascii(ascval) || !isprint(ascval)) {
1100
 
                     /* ignore garbage characters */ ;
1101
 
                } else if (bp - logname >= sizeof(logname) - 1) {
1102
 
                    error(_("%s: input overrun"), op->tty);
1103
 
                } else {
1104
 
                    ignore_result( write(1, &c, 1) );   /* echo the character */
1105
 
                    *bp++ = ascval;             /* and store it */
1106
 
                }
1107
 
                break;
1108
 
            }
1109
 
        }
1110
 
    }
1111
 
    /* Handle names with upper case and no lower case. */
1112
 
    if ((op->flags & F_LCUC) && (cp->capslock = caps_lock(logname))) {
1113
 
        for (bp = logname; *bp; bp++)
1114
 
            if (isupper(*bp))
1115
 
                *bp = tolower(*bp);             /* map name to lower case */
1116
 
    }
1117
 
    return logname;
1118
 
}
1119
 
 
1120
 
/* termio_final - set the final tty mode bits */
1121
 
void
1122
 
termio_final(op, tp, cp)
1123
 
     struct options *op;
1124
 
     struct termios *tp;
1125
 
     struct chardata *cp;
1126
 
{
1127
 
    /* General terminal-independent stuff. */
1128
 
 
1129
 
    tp->c_iflag |= IXON | IXOFF;                /* 2-way flow control */
1130
 
    tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK| ECHOKE;
1131
 
      /* no longer| ECHOCTL | ECHOPRT*/
1132
 
    tp->c_oflag |= OPOST;
1133
 
    /* tp->c_cflag = 0; */
1134
 
    tp->c_cc[VINTR] = DEF_INTR;                 /* default interrupt */
1135
 
    tp->c_cc[VQUIT] = DEF_QUIT;                 /* default quit */
1136
 
    tp->c_cc[VEOF] = DEF_EOF;                   /* default EOF character */
1137
 
    tp->c_cc[VEOL] = DEF_EOL;
1138
 
#ifdef __linux__
1139
 
    tp->c_cc[VSWTC] = DEF_SWITCH;               /* default switch character */
1140
 
#elif defined(VSWTCH)
1141
 
    tp->c_cc[VSWTCH] = DEF_SWITCH;              /* default switch character */
1142
 
#endif
1143
 
 
1144
 
    /* Account for special characters seen in input. */
1145
 
 
1146
 
    if (cp->eol == CR) {
1147
 
        tp->c_iflag |= ICRNL;                   /* map CR in input to NL */
1148
 
        tp->c_oflag |= ONLCR;                   /* map NL in output to CR-NL */
1149
 
    }
1150
 
    tp->c_cc[VERASE] = cp->erase;               /* set erase character */
1151
 
    tp->c_cc[VKILL] = cp->kill;                 /* set kill character */
1152
 
 
1153
 
    /* Account for the presence or absence of parity bits in input. */
1154
 
 
1155
 
    switch (cp->parity) {
1156
 
    case 0:                                     /* space (always 0) parity */
1157
 
        break;
1158
 
    case 1:                                     /* odd parity */
1159
 
        tp->c_cflag |= PARODD;
1160
 
        /* FALLTHROUGH */
1161
 
    case 2:                                     /* even parity */
1162
 
        tp->c_cflag |= PARENB;
1163
 
        tp->c_iflag |= INPCK | ISTRIP;
1164
 
        /* FALLTHROUGH */
1165
 
    case (1 | 2):                               /* no parity bit */
1166
 
        tp->c_cflag &= ~CSIZE;
1167
 
        tp->c_cflag |= CS7;
1168
 
        break;
1169
 
    }
1170
 
    /* Account for upper case without lower case. */
1171
 
 
1172
 
    if (cp->capslock) {
1173
 
#ifdef IUCLC
1174
 
        tp->c_iflag |= IUCLC;
1175
 
#endif
1176
 
#ifdef XCASE    
1177
 
        tp->c_lflag |= XCASE;
1178
 
#endif
1179
 
#ifdef OLCUC
1180
 
        tp->c_oflag |= OLCUC;
1181
 
#endif
1182
 
    }
1183
 
    /* Optionally enable hardware flow control */
1184
 
 
1185
 
#ifdef  CRTSCTS
1186
 
    if (op->flags & F_RTSCTS)
1187
 
        tp->c_cflag |= CRTSCTS;
1188
 
#endif
1189
 
 
1190
 
    /* Finally, make the new settings effective */
1191
 
 
1192
 
    if (tcsetattr(0, TCSANOW, tp) < 0)
1193
 
        error("%s: tcsetattr: TCSANOW: %m", op->tty);
1194
 
}
1195
 
 
1196
 
/* caps_lock - string contains upper case without lower case */
1197
 
int
1198
 
caps_lock(s)
1199
 
     char   *s;
1200
 
{
1201
 
    int     capslock;
1202
 
 
1203
 
    for (capslock = 0; *s; s++) {
1204
 
        if (islower(*s))
1205
 
            return EXIT_SUCCESS;
1206
 
        if (capslock == 0)
1207
 
            capslock = isupper(*s);
1208
 
    }
1209
 
    return capslock;
1210
 
}
1211
 
 
1212
 
/* bcode - convert speed string to speed code; return 0 on failure */
1213
 
int
1214
 
bcode(s)
1215
 
     char   *s;
1216
 
{
1217
 
    struct Speedtab *sp;
1218
 
    long    speed = atol(s);
1219
 
 
1220
 
    for (sp = speedtab; sp->speed; sp++)
1221
 
        if (sp->speed == speed)
1222
 
            return sp->code;
1223
 
    return 0;
1224
 
}
1225
 
 
1226
 
/* usage - explain */
1227
 
 
1228
 
void __attribute__((__noreturn__)) usage(void)
1229
 
{
1230
 
    fprintf(stderr, _("Usage: %s [-8hiLmsUw] [-l login_program] [-t timeout] [-I initstring] [-H login_host] baud_rate,... line [termtype]\nor\t[-hiLmw] [-l login_program] [-t timeout] [-I initstring] [-H login_host] line baud_rate,... [termtype]\n"), progname);
1231
 
    exit(EXIT_FAILURE);
1232
 
}
1233
 
 
1234
 
/* error - report errors to console or syslog; only understands %s and %m */
1235
 
 
1236
 
#define str2cpy(b,s1,s2)        strcat(strcpy(b,s1),s2)
1237
 
 
1238
 
void
1239
 
error(const char *fmt, ...) {
1240
 
    va_list ap;
1241
 
#ifndef USE_SYSLOG
1242
 
    int     fd;
1243
 
#endif
1244
 
    char    buf[BUFSIZ];
1245
 
    char   *bp;
1246
 
 
1247
 
    /*
1248
 
     * If the diagnostic is reported via syslog(3), the process name is
1249
 
     * automatically prepended to the message. If we write directly to
1250
 
     * /dev/console, we must prepend the process name ourselves.
1251
 
     */
1252
 
 
1253
 
#ifdef USE_SYSLOG
1254
 
    buf[0] = '\0';
1255
 
    bp = buf;
1256
 
#else
1257
 
    (void) str2cpy(buf, progname, ": ");
1258
 
    bp = buf + strlen(buf);
1259
 
#endif
1260
 
 
1261
 
    /*
1262
 
     * %s expansion is done by hand. On a System V Release 2 system without
1263
 
     * shared libraries and without syslog(3), linking with the the stdio
1264
 
     * library would make the program three times as big...
1265
 
     *
1266
 
     * %m expansion is done here as well. Too bad syslog(3) does not have a
1267
 
     * vsprintf() like interface.
1268
 
     */
1269
 
 
1270
 
    va_start(ap, fmt);
1271
 
    while (*fmt && bp < &buf[BUFSIZ-1]) {
1272
 
        if (strncmp(fmt, "%s", 2) == 0) {
1273
 
            xstrncpy(bp, va_arg(ap, char *), &buf[BUFSIZ-1] - bp);
1274
 
            bp += strlen(bp);
1275
 
            fmt += 2;
1276
 
        } else if (strncmp(fmt, "%m", 2) == 0) {
1277
 
            xstrncpy(bp, strerror(errno), &buf[BUFSIZ-1] - bp);
1278
 
            bp += strlen(bp);
1279
 
            fmt += 2;
1280
 
        } else {
1281
 
            *bp++ = *fmt++;
1282
 
        }
1283
 
    }
1284
 
    *bp = 0;
1285
 
    va_end(ap);
1286
 
 
1287
 
    /*
1288
 
     * Write the diagnostic directly to /dev/console if we do not use the
1289
 
     * syslog(3) facility.
1290
 
     */
1291
 
 
1292
 
#ifdef  USE_SYSLOG
1293
 
    (void) openlog(progname, LOG_PID, LOG_AUTHPRIV);
1294
 
    (void) syslog(LOG_ERR, "%s", buf);
1295
 
    closelog();
1296
 
#else
1297
 
    /* Terminate with CR-LF since the console mode is unknown. */
1298
 
    (void) strcat(bp, "\r\n");
1299
 
    if ((fd = open("/dev/console", 1)) >= 0) {
1300
 
        ignore_result( write(fd, buf, strlen(buf)) );
1301
 
        (void) close(fd);
1302
 
    }
1303
 
#endif
1304
 
    (void) sleep((unsigned) 10);                /* be kind to init(8) */
1305
 
    exit(1);
1306
 
}