1*8192Smckusick static char sccsid[] = "@(#)netcp.c 4.1 (Berkeley) 09/12/82";
2*8192Smckusick
3*8192Smckusick # include "defs.h"
4*8192Smckusick /* sccs id variable */
5*8192Smckusick static char *netcp_sid = "@(#)netcp.c 1.2";
6*8192Smckusick
7*8192Smckusick
8*8192Smckusick /* global variables */
9*8192Smckusick struct userinfo status;
10*8192Smckusick
11*8192Smckusick /* netcp - copy with remote machines */
main(argc,argv)12*8192Smckusick main(argc,argv)
13*8192Smckusick char **argv;
14*8192Smckusick {
15*8192Smckusick char rcmd[BUFSIZ], acmd[BUFSIZ], *sn;
16*8192Smckusick char mchto, mchfrom, sfnto[FNS], sfnfrom[FNS];
17*8192Smckusick
18*8192Smckusick argv[argc] = 0;
19*8192Smckusick debugflg = DBV;
20*8192Smckusick if(argc < 3)goto usage;
21*8192Smckusick argv++, argc--;
22*8192Smckusick while(argv[0][0] == '-'){
23*8192Smckusick switch(argv[0][1]){
24*8192Smckusick case 'b': status.nonotify++; break;
25*8192Smckusick case 'f': status.force++; break;
26*8192Smckusick case 'l': harg(status.login); break;
27*8192Smckusick case 'n': status.nowrite++; break;
28*8192Smckusick case 'p': harg(status.mpasswd); break;
29*8192Smckusick case 'q': status.quiet++; break;
30*8192Smckusick default: fprintf(stderr,"Unknown option %s\n",argv[0]);
31*8192Smckusick break;
32*8192Smckusick }
33*8192Smckusick argc--;argv++;
34*8192Smckusick }
35*8192Smckusick if(argc > 2)goto usage;
36*8192Smckusick sprintf(rcmd,"netcp %s %s",argv[0],argv[1]);
37*8192Smckusick mchfrom = analfile(sfnfrom,argv[0]);
38*8192Smckusick mchto = analfile(sfnto,argv[1]);
39*8192Smckusick if(mchfrom == 0 || mchto == 0){
40*8192Smckusick fprintf(stderr,"Unknown machine\n");
41*8192Smckusick exit(EX_NOHOST);
42*8192Smckusick }
43*8192Smckusick if(sfnfrom[0] == 0 || sfnto[0] == 0){
44*8192Smckusick fprintf(stderr,"Must specify both file names\n");
45*8192Smckusick exit(EX_USAGE);
46*8192Smckusick }
47*8192Smckusick if(mchfrom == local && mchto == local){
48*8192Smckusick fprintf(stderr,"Error: both files are on this machine\n");
49*8192Smckusick exit(EX_USAGE);
50*8192Smckusick }
51*8192Smckusick else if(mchfrom == local)
52*8192Smckusick kexecl(netcmd,"net","-m",longname(mchto), "-o",sfnto,"-s",sfnfrom,
53*8192Smckusick "-c",rcmd,"cat",0);
54*8192Smckusick else if(mchto == local){
55*8192Smckusick kexecl(netcmd,"net","-m",longname(mchfrom), "-r",sfnto,"-i",sfnfrom,
56*8192Smckusick "-c",rcmd,"cat",0);
57*8192Smckusick }
58*8192Smckusick /* remote for both */
59*8192Smckusick else if(mchto == mchfrom)
60*8192Smckusick kexecl(netcmd,"net","-m",longname(mchto),"-c",rcmd,"cp",sfnfrom,sfnto,0);
61*8192Smckusick else {
62*8192Smckusick /* experimental - still needs debugging */
63*8192Smckusick fprintf(stderr,
64*8192Smckusick "Experimental - Machines normally must be the same\n");
65*8192Smckusick
66*8192Smckusick /* collect info on the "To Machine" */
67*8192Smckusick remote = mchto;
68*8192Smckusick /* get status.login and passwd from .netrc if poss. */
69*8192Smckusick commandfile();
70*8192Smckusick if(status.login[0] == 0 || status.mpasswd[0] == 0){
71*8192Smckusick sn = SnFromUid(getuid());
72*8192Smckusick if(sn == NULL){
73*8192Smckusick fprintf(stderr,"Unknown user\n");
74*8192Smckusick exit(EX_OSFILE);
75*8192Smckusick }
76*8192Smckusick strcpy(status.localname,sn);
77*8192Smckusick /* check environ */
78*8192Smckusick envloginpasswd(remote,status.login,status.mpasswd);
79*8192Smckusick /* prompt on terminal */
80*8192Smckusick promptlogin(remote);
81*8192Smckusick }
82*8192Smckusick /* should use -q option */
83*8192Smckusick sprintf(acmd,"%s -l %s -p %s %s %s",
84*8192Smckusick NETCPCMD,status.login,status.mpasswd,argv[0],argv[1]);
85*8192Smckusick
86*8192Smckusick /* send the netcp command to the "From" machine */
87*8192Smckusick remote = mchfrom;
88*8192Smckusick status.login[0] = status.mpasswd[0] = 0;
89*8192Smckusick mexecl(netcmd,"net","-m",longname(mchfrom),"-c",rcmd,acmd,0);
90*8192Smckusick }
91*8192Smckusick perror(netcmd);
92*8192Smckusick fprintf(stderr,"Network is down\n");
93*8192Smckusick exit(EX_UNAVAILABLE);
94*8192Smckusick usage:
95*8192Smckusick printf("Usage: netcp [-l ...] [-p ...] [-f] [-n] fromfile tofile\n");
96*8192Smckusick exit(EX_USAGE);
97*8192Smckusick }
98*8192Smckusick
analfile(sfn,addr)99*8192Smckusick analfile(sfn,addr)
100*8192Smckusick char *sfn;
101*8192Smckusick char *addr;
102*8192Smckusick {
103*8192Smckusick register char *file;
104*8192Smckusick char work[FNS], *s, c0,c1,c2,c3,c,colon=0,mch;
105*8192Smckusick mch = local;
106*8192Smckusick strcpy(work,addr);
107*8192Smckusick s = work;
108*8192Smckusick file = s;
109*8192Smckusick while(*s){
110*8192Smckusick if(*s == '/')break;
111*8192Smckusick if(*s == ':'){
112*8192Smckusick colon = 1;
113*8192Smckusick *s++ = 0;
114*8192Smckusick break;
115*8192Smckusick }
116*8192Smckusick s++;
117*8192Smckusick }
118*8192Smckusick if(colon){ /* name specified */
119*8192Smckusick mch = lookup(file);
120*8192Smckusick if(mch == 0){
121*8192Smckusick return(mch);
122*8192Smckusick }
123*8192Smckusick file = s;
124*8192Smckusick }
125*8192Smckusick else {
126*8192Smckusick s = file;
127*8192Smckusick c0 = *s++;
128*8192Smckusick c1 = *s++;
129*8192Smckusick c2 = *s++;
130*8192Smckusick c3 = *s++;
131*8192Smckusick if(c0 == '/' && c1 != '/' && islower(c1))
132*8192Smckusick if(c3 == '/')mch = c1; /* CC name */
133*8192Smckusick }
134*8192Smckusick strcpy(sfn,file);
135*8192Smckusick s = sfn;
136*8192Smckusick /* check for bad chars in file name */
137*8192Smckusick while(c = *s++)
138*8192Smckusick if(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\b')
139*8192Smckusick err("Invalid character '%c'\n",c);
140*8192Smckusick return(mch);
141*8192Smckusick }
142