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

Annotation of src/usr.bin/oldrdist/main.c, Revision 1.2

1.2     ! deraadt     1: /*     $OpenBSD$       */
        !             2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1983, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /* from: static char sccsid[] = "@(#)main.c    8.1 (Berkeley) 6/9/93"; */
1.2     ! deraadt    44: static char *rcsid = "$OpenBSD: main.c,v 1.1 1996/02/03 12:12:00 dm Exp $";
1.1       dm         45: #endif /* not lint */
                     46:
                     47: #include "defs.h"
                     48:
                     49: #define NHOSTS 100
                     50:
                     51: /*
                     52:  * Remote distribution program.
                     53:  */
                     54:
                     55: char   *distfile = NULL;
                     56: #define _RDIST_TMP     "/rdistXXXXXX"
                     57: char   tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
                     58: char   *tempname;
                     59:
                     60: int    debug;          /* debugging flag */
                     61: int    nflag;          /* NOP flag, just print commands without executing */
                     62: int    qflag;          /* Quiet. Don't print messages */
                     63: int    options;        /* global options */
                     64: int    iamremote;      /* act as remote server for transfering files */
                     65:
                     66: FILE   *fin = NULL;    /* input file pointer */
                     67: int    rem = -1;       /* file descriptor to remote source/sink process */
                     68: char   host[32];       /* host name */
                     69: int    nerrs;          /* number of errors while sending/receiving */
                     70: char   user[10];       /* user's name */
                     71: char   homedir[128];   /* user's home directory */
                     72: int    userid;         /* user's user ID */
                     73: int    groupid;        /* user's group ID */
                     74:
                     75: struct passwd *pw;     /* pointer to static area used by getpwent */
                     76: struct group *gr;      /* pointer to static area used by getgrent */
                     77:
                     78: static void usage __P((void));
                     79: static void docmdargs __P((int, char *[]));
                     80:
                     81: int
                     82: main(argc, argv)
                     83:        int argc;
                     84:        char *argv[];
                     85: {
                     86:        register char *arg;
                     87:        int cmdargs = 0;
                     88:        char *dhosts[NHOSTS], **hp = dhosts;
                     89:
                     90:        pw = getpwuid(userid = getuid());
                     91:        if (pw == NULL) {
                     92:                fprintf(stderr, "%s: Who are you?\n", argv[0]);
                     93:                exit(1);
                     94:        }
                     95:        strcpy(user, pw->pw_name);
                     96:        strcpy(homedir, pw->pw_dir);
                     97:        groupid = pw->pw_gid;
                     98:        gethostname(host, sizeof(host));
                     99:        strcpy(tempfile, _PATH_TMP);
                    100:        strcat(tempfile, _RDIST_TMP);
                    101:        if ((tempname = rindex(tempfile, '/')) != 0)
                    102:                tempname++;
                    103:        else
                    104:                tempname = tempfile;
                    105:
                    106:        while (--argc > 0) {
                    107:                if ((arg = *++argv)[0] != '-')
                    108:                        break;
                    109:                if (!strcmp(arg, "-Server"))
                    110:                        iamremote++;
                    111:                else while (*++arg)
                    112:                        switch (*arg) {
                    113:                        case 'f':
                    114:                                if (--argc <= 0)
                    115:                                        usage();
                    116:                                distfile = *++argv;
                    117:                                if (distfile[0] == '-' && distfile[1] == '\0')
                    118:                                        fin = stdin;
                    119:                                break;
                    120:
                    121:                        case 'm':
                    122:                                if (--argc <= 0)
                    123:                                        usage();
                    124:                                if (hp >= &dhosts[NHOSTS-2]) {
                    125:                                        fprintf(stderr, "rdist: too many destination hosts\n");
                    126:                                        exit(1);
                    127:                                }
                    128:                                *hp++ = *++argv;
                    129:                                break;
                    130:
                    131:                        case 'd':
                    132:                                if (--argc <= 0)
                    133:                                        usage();
                    134:                                define(*++argv);
                    135:                                break;
                    136:
                    137:                        case 'D':
                    138:                                debug++;
                    139:                                break;
                    140:
                    141:                        case 'c':
                    142:                                cmdargs++;
                    143:                                break;
                    144:
                    145:                        case 'n':
                    146:                                if (options & VERIFY) {
                    147:                                        printf("rdist: -n overrides -v\n");
                    148:                                        options &= ~VERIFY;
                    149:                                }
                    150:                                nflag++;
                    151:                                break;
                    152:
                    153:                        case 'q':
                    154:                                qflag++;
                    155:                                break;
                    156:
                    157:                        case 'b':
                    158:                                options |= COMPARE;
                    159:                                break;
                    160:
                    161:                        case 'R':
                    162:                                options |= REMOVE;
                    163:                                break;
                    164:
                    165:                        case 'v':
                    166:                                if (nflag) {
                    167:                                        printf("rdist: -n overrides -v\n");
                    168:                                        break;
                    169:                                }
                    170:                                options |= VERIFY;
                    171:                                break;
                    172:
                    173:                        case 'w':
                    174:                                options |= WHOLE;
                    175:                                break;
                    176:
                    177:                        case 'y':
                    178:                                options |= YOUNGER;
                    179:                                break;
                    180:
                    181:                        case 'h':
                    182:                                options |= FOLLOW;
                    183:                                break;
                    184:
                    185:                        case 'i':
                    186:                                options |= IGNLNKS;
                    187:                                break;
                    188:
                    189:                        default:
                    190:                                usage();
                    191:                        }
                    192:        }
                    193:        *hp = NULL;
                    194:
                    195:        seteuid(userid);
                    196:        mktemp(tempfile);
                    197:
                    198:        if (iamremote) {
                    199:                server();
                    200:                exit(nerrs != 0);
                    201:        }
                    202:
                    203:        if (cmdargs)
                    204:                docmdargs(argc, argv);
                    205:        else {
                    206:                if (fin == NULL) {
                    207:                        if(distfile == NULL) {
                    208:                                if((fin = fopen("distfile","r")) == NULL)
                    209:                                        fin = fopen("Distfile", "r");
                    210:                        } else
                    211:                                fin = fopen(distfile, "r");
                    212:                        if(fin == NULL) {
                    213:                                perror(distfile ? distfile : "distfile");
                    214:                                exit(1);
                    215:                        }
                    216:                }
                    217:                yyparse();
                    218:                if (nerrs == 0)
                    219:                        docmds(dhosts, argc, argv);
                    220:        }
                    221:
                    222:        exit(nerrs != 0);
                    223: }
                    224:
                    225: static void
                    226: usage()
                    227: {
                    228:        printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
                    229:        printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
                    230:        exit(1);
                    231: }
                    232:
                    233: /*
                    234:  * rcp like interface for distributing files.
                    235:  */
                    236: static void
                    237: docmdargs(nargs, args)
                    238:        int nargs;
                    239:        char *args[];
                    240: {
                    241:        register struct namelist *nl, *prev;
                    242:        register char *cp;
                    243:        struct namelist *files, *hosts;
                    244:        struct subcmd *cmds;
                    245:        char *dest;
                    246:        static struct namelist tnl = { NULL, NULL };
                    247:        int i;
                    248:
                    249:        if (nargs < 2)
                    250:                usage();
                    251:
                    252:        prev = NULL;
                    253:        for (i = 0; i < nargs - 1; i++) {
                    254:                nl = makenl(args[i]);
                    255:                if (prev == NULL)
                    256:                        files = prev = nl;
                    257:                else {
                    258:                        prev->n_next = nl;
                    259:                        prev = nl;
                    260:                }
                    261:        }
                    262:
                    263:        cp = args[i];
                    264:        if ((dest = index(cp, ':')) != NULL)
                    265:                *dest++ = '\0';
                    266:        tnl.n_name = cp;
                    267:        hosts = expand(&tnl, E_ALL);
                    268:        if (nerrs)
                    269:                exit(1);
                    270:
                    271:        if (dest == NULL || *dest == '\0')
                    272:                cmds = NULL;
                    273:        else {
                    274:                cmds = makesubcmd(INSTALL);
                    275:                cmds->sc_options = options;
                    276:                cmds->sc_name = dest;
                    277:        }
                    278:
                    279:        if (debug) {
                    280:                printf("docmdargs()\nfiles = ");
                    281:                prnames(files);
                    282:                printf("hosts = ");
                    283:                prnames(hosts);
                    284:        }
                    285:        insert(NULL, files, hosts, cmds);
                    286:        docmds(NULL, 0, NULL);
                    287: }
                    288:
                    289: /*
                    290:  * Print a list of NAME blocks (mostly for debugging).
                    291:  */
                    292: void
                    293: prnames(nl)
                    294:        register struct namelist *nl;
                    295: {
                    296:        printf("( ");
                    297:        while (nl != NULL) {
                    298:                printf("%s ", nl->n_name);
                    299:                nl = nl->n_next;
                    300:        }
                    301:        printf(")\n");
                    302: }
                    303:
                    304: #if __STDC__
                    305: #include <stdarg.h>
                    306: #else
                    307: #include <varargs.h>
                    308: #endif
                    309:
                    310: void
                    311: #if __STDC__
                    312: warn(const char *fmt, ...)
                    313: #else
                    314: warn(fmt, va_alist)
                    315:        char *fmt;
                    316:         va_dcl
                    317: #endif
                    318: {
                    319:        extern int yylineno;
                    320:        va_list ap;
                    321: #if __STDC__
                    322:        va_start(ap, fmt);
                    323: #else
                    324:        va_start(ap);
                    325: #endif
                    326:        (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
                    327:        (void)vfprintf(stderr, fmt, ap);
                    328:        (void)fprintf(stderr, "\n");
                    329:        va_end(ap);
                    330: }