1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if !defined(lint) && defined(DOSCCS)
   8: char copyright[] =
   9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: 
  12: static char sccsid[] = "@(#)main.c	5.1.2 (2.11BSD) 1997/07/03";
  13: #endif
  14: 
  15: #include <paths.h>
  16: #include "defs.h"
  17: 
  18: #define NHOSTS 100
  19: 
  20: /*
  21:  * Remote distribution program.
  22:  */
  23: 
  24: char    *distfile = NULL;
  25: #define _RDIST_TMP  "/rdistXXXXXX"
  26: /* Can't use "sizeof (_PATH_TMP) + sizeof (_RDIST_TMP) + 1" because of xstr */
  27: char    tempfile[24];
  28: char    *tempname;
  29: 
  30: int debug;      /* debugging flag */
  31: int nflag;      /* NOP flag, just print commands without executing */
  32: int qflag;      /* Quiet. Don't print messages */
  33: int options;    /* global options */
  34: int iamremote;  /* act as remote server for transfering files */
  35: 
  36: FILE    *fin = NULL;    /* input file pointer */
  37: int rem = -1;   /* file descriptor to remote source/sink process */
  38: char    host[MAXHOSTNAMELEN];   /* host name */
  39: int nerrs;      /* number of errors while sending/receiving */
  40: char    user[20];   /* user's name */
  41: char    homedir[128];   /* user's home directory */
  42: int userid;     /* user's user ID */
  43: int groupid;    /* user's group ID */
  44: 
  45: struct  passwd *pw; /* pointer to static area used by getpwent */
  46: struct  group *gr;  /* pointer to static area used by getgrent */
  47: 
  48: main(argc, argv)
  49:     int argc;
  50:     char *argv[];
  51: {
  52:     register char *arg;
  53:     int cmdargs = 0;
  54:     char *dhosts[NHOSTS], **hp = dhosts;
  55:     char ebuf[128], obuf[256], ibuf[256];
  56: 
  57:     setbuffer(stdin, ibuf, sizeof ibuf);
  58:     setvbuf(stdout, obuf, _IOLBF, sizeof obuf);
  59:     setvbuf(stderr, ebuf, _IOLBF, sizeof ebuf);
  60: 
  61:     pw = getpwuid(userid = getuid());
  62:     if (pw == NULL) {
  63:         fprintf(stderr, "%s: Who are you?\n", argv[0]);
  64:         exit(1);
  65:     }
  66:     strcpy(user, pw->pw_name);
  67:     strcpy(homedir, pw->pw_dir);
  68:     groupid = pw->pw_gid;
  69:     gethostname(host, sizeof(host));
  70:     strcpy(tempfile, _PATH_TMP);
  71:     strcat(tempfile, _RDIST_TMP);
  72:     tempname = rindex(tempfile, '/') + 1;
  73: 
  74:     while (--argc > 0) {
  75:         if ((arg = *++argv)[0] != '-')
  76:             break;
  77:         if (!strcmp(arg, "-Server"))
  78:             iamremote++;
  79:         else while (*++arg)
  80:             switch (*arg) {
  81:             case 'f':
  82:                 if (--argc <= 0)
  83:                     usage();
  84:                 distfile = *++argv;
  85:                 if (distfile[0] == '-' && distfile[1] == '\0')
  86:                     fin = stdin;
  87:                 break;
  88: 
  89:             case 'm':
  90:                 if (--argc <= 0)
  91:                     usage();
  92:                 if (hp >= &dhosts[NHOSTS-2]) {
  93:                     fprintf(stderr, "rdist: too many destination hosts\n");
  94:                     exit(1);
  95:                 }
  96:                 *hp++ = *++argv;
  97:                 break;
  98: 
  99:             case 'd':
 100:                 if (--argc <= 0)
 101:                     usage();
 102:                 define(*++argv);
 103:                 break;
 104: 
 105:             case 'D':
 106:                 debug++;
 107:                 break;
 108: 
 109:             case 'c':
 110:                 cmdargs++;
 111:                 break;
 112: 
 113:             case 'n':
 114:                 if (options & VERIFY) {
 115:                     printf("rdist: -n overrides -v\n");
 116:                     options &= ~VERIFY;
 117:                 }
 118:                 nflag++;
 119:                 break;
 120: 
 121:             case 'q':
 122:                 qflag++;
 123:                 break;
 124: 
 125:             case 'b':
 126:                 options |= COMPARE;
 127:                 break;
 128: 
 129:             case 'R':
 130:                 options |= REMOVE;
 131:                 break;
 132: 
 133:             case 'v':
 134:                 if (nflag) {
 135:                     printf("rdist: -n overrides -v\n");
 136:                     break;
 137:                 }
 138:                 options |= VERIFY;
 139:                 break;
 140: 
 141:             case 'w':
 142:                 options |= WHOLE;
 143:                 break;
 144: 
 145:             case 'y':
 146:                 options |= YOUNGER;
 147:                 break;
 148: 
 149:             case 'h':
 150:                 options |= FOLLOW;
 151:                 break;
 152: 
 153:             case 'i':
 154:                 options |= IGNLNKS;
 155:                 break;
 156: 
 157:             default:
 158:                 usage();
 159:             }
 160:     }
 161:     *hp = NULL;
 162: 
 163:     setreuid(0, userid);
 164:     mktemp(tempfile);
 165: 
 166:     if (iamremote) {
 167:         server();
 168:         exit(nerrs != 0);
 169:     }
 170: 
 171:     if (cmdargs)
 172:         docmdargs(argc, argv);
 173:     else {
 174:         if (fin == NULL) {
 175:             if(distfile == NULL) {
 176:                 if((fin = fopen("distfile","r")) == NULL)
 177:                     fin = fopen("Distfile", "r");
 178:             } else
 179:                 fin = fopen(distfile, "r");
 180:             if(fin == NULL) {
 181:                 perror(distfile ? distfile : "distfile");
 182:                 exit(1);
 183:             }
 184:         }
 185:         yyparse();
 186:         if (nerrs == 0)
 187:             docmds(dhosts, argc, argv);
 188:     }
 189: 
 190:     exit(nerrs != 0);
 191: }
 192: 
 193: usage()
 194: {
 195:     printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
 196:     printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
 197:     exit(1);
 198: }
 199: 
 200: /*
 201:  * rcp like interface for distributing files.
 202:  */
 203: docmdargs(nargs, args)
 204:     int nargs;
 205:     char *args[];
 206: {
 207:     register struct namelist *nl, *prev;
 208:     register char *cp;
 209:     struct namelist *files, *hosts;
 210:     struct subcmd *cmds;
 211:     char *dest;
 212:     static struct namelist tnl = { NULL, NULL };
 213:     int i;
 214: 
 215:     if (nargs < 2)
 216:         usage();
 217: 
 218:     prev = NULL;
 219:     for (i = 0; i < nargs - 1; i++) {
 220:         nl = makenl(args[i]);
 221:         if (prev == NULL)
 222:             files = prev = nl;
 223:         else {
 224:             prev->n_next = nl;
 225:             prev = nl;
 226:         }
 227:     }
 228: 
 229:     cp = args[i];
 230:     if ((dest = index(cp, ':')) != NULL)
 231:         *dest++ = '\0';
 232:     tnl.n_name = cp;
 233:     hosts = expand(&tnl, E_ALL);
 234:     if (nerrs)
 235:         exit(1);
 236: 
 237:     if (dest == NULL || *dest == '\0')
 238:         cmds = NULL;
 239:     else {
 240:         cmds = makesubcmd(INSTALL);
 241:         cmds->sc_options = options;
 242:         cmds->sc_name = dest;
 243:     }
 244: 
 245:     if (debug) {
 246:         printf("docmdargs()\nfiles = ");
 247:         prnames(files);
 248:         printf("hosts = ");
 249:         prnames(hosts);
 250:     }
 251:     insert(NULL, files, hosts, cmds);
 252:     docmds(NULL, 0, NULL);
 253: }
 254: 
 255: /*
 256:  * Print a list of NAME blocks (mostly for debugging).
 257:  */
 258: prnames(nl)
 259:     register struct namelist *nl;
 260: {
 261:     printf("( ");
 262:     while (nl != NULL) {
 263:         printf("%s ", nl->n_name);
 264:         nl = nl->n_next;
 265:     }
 266:     printf(")\n");
 267: }

Defined functions

docmdargs defined in line 203; used 1 times
main defined in line 48; never used
prnames defined in line 258; used 5 times
usage defined in line 193; used 5 times

Defined variables

copyright defined in line 8; never used
distfile defined in line 24; used 7 times
gr defined in line 46; used 14 times
groupid defined in line 43; used 1 times
  • in line 68
homedir defined in line 41; used 1 times
  • in line 67
host defined in line 38; used 26 times
iamremote defined in line 34; used 10 times
nerrs defined in line 39; used 15 times
nflag defined in line 31; used 14 times
options defined in line 33; used 30 times
pw defined in line 45; used 29 times
qflag defined in line 32; used 6 times
rem defined in line 37; used 53 times
sccsid defined in line 12; never used
tempfile defined in line 27; used 16 times
tempname defined in line 28; used 1 times
  • in line 72
user defined in line 40; used 1 times
  • in line 66
userid defined in line 42; used 2 times

Defined macros

NHOSTS defined in line 18; used 2 times
_RDIST_TMP defined in line 25; used 1 times
  • in line 71
Last modified: 1997-07-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 4168
Valid CSS Valid XHTML 1.0 Strict