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 < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-25  |  7.1 KB  |  329 lines

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