home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / rdist / main.c,v < prev    next >
Encoding:
Text File  |  1994-07-25  |  7.3 KB  |  353 lines

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