1 /* $NetBSD: main.c,v 1.18 2006/03/18 09:46:35 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 6/9/93";
42 __RCSID("$NetBSD: main.c,v 1.18 2006/03/18 09:46:35 christos Exp $");
46 #include <sys/types.h>
57 * Remote distribution program.
60 char *distfile
= NULL
;
61 #define _RDIST_TMP "/rdistXXXXXX"
62 char tempfile
[sizeof _PATH_TMP
+ sizeof _RDIST_TMP
+ 1];
65 int debug
; /* debugging flag */
66 int nflag
; /* NOP flag, just print commands without executing */
67 int qflag
; /* Quiet. Don't print messages */
68 int options
; /* global options */
69 int iamremote
; /* act as remote server for transfering files */
71 FILE *fin
= NULL
; /* input file pointer */
72 int rem
= -1; /* file descriptor to remote source/sink process */
73 char host
[MAXHOSTNAMELEN
+ 1]; /* host name */
74 int nerrs
; /* number of errors while sending/receiving */
75 char user
[34]; /* user's name */
76 char homedir
[PATH_MAX
]; /* user's home directory */
77 uid_t userid
; /* user's user ID */
78 gid_t groupid
; /* user's group ID */
80 struct passwd
*pw
; /* pointer to static area used by getpwent */
81 struct group
*gr
; /* pointer to static area used by getgrent */
83 int main(int, char **);
84 static void usage(void);
85 static void docmdargs(int, char *[]);
88 main(int argc
, char **argv
)
92 char *dhosts
[NHOSTS
], **hp
= dhosts
;
95 pw
= getpwuid(userid
= getuid());
97 fprintf(stderr
, "%s: Who are you?\n", argv
[0]);
100 strlcpy(user
, pw
->pw_name
, sizeof(user
));
101 strlcpy(homedir
, pw
->pw_dir
, sizeof(homedir
));
102 groupid
= pw
->pw_gid
;
103 gethostname(host
, sizeof(host
));
104 host
[sizeof(host
) - 1] = '\0';
105 strlcpy(tempfile
, _PATH_TMP
, sizeof(tempfile
));
106 strlcat(tempfile
, _RDIST_TMP
, sizeof(tempfile
));
107 if ((tempname
= strrchr(tempfile
, '/')) != 0)
113 if ((arg
= *++argv
)[0] != '-')
115 if (!strcmp(arg
, "-Server"))
123 if (distfile
[0] == '-' && distfile
[1] == '\0')
130 if (hp
>= &dhosts
[NHOSTS
-2]) {
131 fprintf(stderr
, "rdist: too many destination hosts\n");
152 if (options
& VERIFY
) {
153 printf("rdist: -n overrides -v\n");
173 printf("rdist: -n overrides -v\n");
202 fd
= mkstemp(tempfile
);
204 err(1, "could not make a temporary file");
214 docmdargs(argc
, argv
);
217 if (distfile
== NULL
) {
218 if ((fin
= fopen("distfile","r")) == NULL
)
219 fin
= fopen("Distfile", "r");
221 fin
= fopen(distfile
, "r");
223 perror(distfile
? distfile
: "distfile");
230 docmds(dhosts
, argc
, argv
);
241 (void)fprintf(stderr
,
242 "usage: %s [-bDhinqRvwy] [-d var=value] [-f distfile] [-m host] "
244 "or : %s [-bDhinqRvwy] -c name ... [login@]host[:dest]\n",
245 getprogname(), getprogname());
250 * rcp like interface for distributing files.
253 docmdargs(int nargs
, char **args
)
255 struct namelist
*nl
, *prev
;
257 struct namelist
*files
, *hosts
;
260 static struct namelist tnl
= { NULL
, NULL
};
268 for (i
= 0; i
< nargs
- 1; i
++) {
269 nl
= makenl(args
[i
]);
279 if ((dest
= strchr(cp
, ':')) != NULL
)
282 hosts
= expand(&tnl
, E_ALL
);
286 if (dest
== NULL
|| *dest
== '\0')
289 cmds
= makesubcmd(INSTALL
);
290 cmds
->sc_options
= options
;
291 cmds
->sc_name
= dest
;
295 printf("docmdargs()\nfiles = ");
300 insert(NULL
, files
, hosts
, cmds
);
301 docmds(NULL
, 0, NULL
);
308 * Print a list of NAME blocks (mostly for debugging).
311 prnames(struct namelist
*nl
)
315 printf("%s ", nl
->n_name
);