[BACK]Return to cu.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tip

Annotation of src/usr.bin/tip/cu.c, Revision 1.24

1.24    ! deraadt     1: /*     $OpenBSD: cu.c,v 1.23 2007/11/26 09:28:34 martynas Exp $        */
1.3       millert     2: /*     $NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $       */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.14      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.21      ray        33: #include <err.h>
                     34: #include <paths.h>
                     35:
1.1       deraadt    36: #include "tip.h"
                     37:
1.18      moritz     38: static void    cuusage(void);
1.1       deraadt    39:
                     40: /*
                     41:  * Botch the interface to look like cu's
                     42:  */
1.4       deraadt    43: void
1.17      deraadt    44: cumain(int argc, char *argv[])
1.1       deraadt    45: {
1.11      miod       46:        int ch, i, parity;
1.20      millert    47:        const char *errstr;
1.1       deraadt    48:        static char sbuf[12];
                     49:
1.6       millert    50:        if (argc < 2)
                     51:                cuusage();
1.22      moritz     52:        CU = DV = NULL;
1.1       deraadt    53:        BR = DEFBR;
1.11      miod       54:        parity = 0;     /* none */
1.20      millert    55:
                     56:        /*
                     57:         * Convert obsolecent -### speed to modern -s### syntax which
                     58:         * getopt() can handle.
                     59:         */
                     60:        for (i = 1; i < argc; i++) {
                     61:                if (argv[i][0] == '-') {
                     62:                        switch (argv[i][1]) {
                     63:                        case '0': case '1': case '2': case '3': case '4':
                     64:                        case '5': case '6': case '7': case '8': case '9':
                     65:                                ch = snprintf(sbuf, sizeof(sbuf), "-s%s",
                     66:                                    &argv[i][1]);
                     67:                                if (ch <= 0 || ch >= sizeof(sbuf)) {
                     68:                                        errx(3, "invalid speed: %s",
                     69:                                            &argv[i][1]);
                     70:                                }
                     71:                                argv[i] = sbuf;
                     72:                                break;
                     73:                        case '-':
                     74:                                /* if we get "--" stop processing args */
                     75:                                if (argv[i][2] == '\0')
                     76:                                        goto getopt;
                     77:                                break;
                     78:                        }
                     79:                }
                     80:        }
                     81:
                     82: getopt:
                     83:        while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
1.17      deraadt    84:                switch (ch) {
1.1       deraadt    85:                case 'a':
1.21      ray        86:                        if (optarg[0] == '\0')
                     87:                                errx(3, "invalid acu: \"\"");
1.6       millert    88:                        CU = optarg;
                     89:                        break;
                     90:                case 'l':
1.8       deraadt    91:                        if (DV != NULL) {
                     92:                                fprintf(stderr,
1.23      martynas   93:                                    "%s: cannot specify multiple -l options\n",
1.8       deraadt    94:                                    __progname);
                     95:                                exit(3);
                     96:                        }
                     97:                        if (strchr(optarg, '/'))
                     98:                                DV = optarg;
                     99:                        else
1.21      ray       100:                                if (asprintf(&DV, "%s%s", _PATH_DEV, optarg) == -1)
                    101:                                        err(3, "asprintf");
1.1       deraadt   102:                        break;
                    103:                case 's':
1.20      millert   104:                        BR = (int)strtonum(optarg, 0, INT_MAX, &errstr);
                    105:                        if (errstr)
                    106:                                errx(3, "speed is %s: %s", errstr, optarg);
1.6       millert   107:                        break;
                    108:                case 'h':
1.9       millert   109:                        setboolean(value(LECHO), TRUE);
1.6       millert   110:                        HD = TRUE;
                    111:                        break;
                    112:                case 't':
                    113:                        HW = 1, DU = -1;
1.1       deraadt   114:                        break;
1.6       millert   115:                case 'o':
1.11      miod      116:                        if (parity != 0)
                    117:                                parity = 0;     /* -e -o */
                    118:                        else
                    119:                                parity = 1;     /* odd */
1.6       millert   120:                        break;
                    121:                case 'e':
1.11      miod      122:                        if (parity != 0)
                    123:                                parity = 0;     /* -o -e */
                    124:                        else
                    125:                                parity = -1;    /* even */
1.1       deraadt   126:                        break;
                    127:                default:
1.10      pvalchev  128:                        cuusage();
1.1       deraadt   129:                        break;
                    130:                }
                    131:        }
1.6       millert   132:        argc -= optind;
                    133:        argv += optind;
                    134:
                    135:        switch (argc) {
                    136:        case 1:
                    137:                PN = argv[0];
                    138:                break;
                    139:        case 0:
                    140:                break;
                    141:        default:
                    142:                cuusage();
                    143:                break;
                    144:        }
                    145:
1.1       deraadt   146:        signal(SIGINT, cleanup);
                    147:        signal(SIGQUIT, cleanup);
                    148:        signal(SIGHUP, cleanup);
                    149:        signal(SIGTERM, cleanup);
1.16      otto      150:        signal(SIGCHLD, SIG_DFL);
1.1       deraadt   151:
                    152:        /*
                    153:         * The "cu" host name is used to define the
                    154:         * attributes of the generic dialer.
                    155:         */
1.4       deraadt   156:        (void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
1.1       deraadt   157:        if ((i = hunt(sbuf)) == 0) {
                    158:                printf("all ports busy\n");
                    159:                exit(3);
                    160:        }
                    161:        if (i == -1) {
                    162:                printf("link down\n");
                    163:                (void)uu_unlock(uucplock);
                    164:                exit(3);
                    165:        }
                    166:        setbuf(stdout, NULL);
                    167:        loginit();
                    168:        user_uid();
                    169:        vinit();
1.11      miod      170:        switch (parity) {
                    171:        case -1:
                    172:                setparity("even");
                    173:                break;
                    174:        case 1:
                    175:                setparity("odd");
                    176:                break;
                    177:        default:
                    178:                setparity("none");
                    179:                break;
                    180:        }
1.9       millert   181:        setboolean(value(VERBOSE), FALSE);
1.13      millert   182:        if (HW && ttysetup(BR)) {
                    183:                fprintf(stderr, "%s: unsupported speed %ld\n",
                    184:                    __progname, BR);
                    185:                daemon_uid();
                    186:                (void)uu_unlock(uucplock);
                    187:                exit(3);
                    188:        }
1.17      deraadt   189:        if (con()) {
1.1       deraadt   190:                printf("Connect failed\n");
                    191:                daemon_uid();
                    192:                (void)uu_unlock(uucplock);
                    193:                exit(1);
                    194:        }
1.13      millert   195:        if (!HW && ttysetup(BR)) {
                    196:                fprintf(stderr, "%s: unsupported speed %ld\n",
                    197:                    __progname, BR);
                    198:                daemon_uid();
                    199:                (void)uu_unlock(uucplock);
                    200:                exit(3);
                    201:        }
1.6       millert   202: }
                    203:
1.18      moritz    204: static void
1.17      deraadt   205: cuusage(void)
1.6       millert   206: {
1.19      jmc       207:        fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
                    208:            "[-s speed | -speed] [phone-number]\n");
1.6       millert   209:        exit(8);
1.1       deraadt   210: }