static char sccsid
[] = "@(#)rcp.c 4.4 82/11/14";
char *colon(), *index(), *rindex(), *malloc(), *strcpy(), *sprintf();
int iamremote
, targetshouldbedirectory
;
struct passwd
*getpwuid();
#define ga() (void) write(rem, "", 1)
char buf
[BUFSIZ
], cmd
[16];
pwd
= getpwuid(getuid());
fprintf(stderr
, "who are you?\n");
if (argc
> 0 && !strcmp(*argv
, "-r")) {
if (argc
> 0 && !strcmp(*argv
, "-d")) {
targetshouldbedirectory
= 1;
if (argc
> 0 && !strcmp(*argv
, "-f")) {
argc
--, argv
++; iamremote
= 1;
if (argc
> 0 && !strcmp(*argv
, "-t")) {
argc
--, argv
++; iamremote
= 1;
targetshouldbedirectory
= 1;
(void) sprintf(cmd
, "rcp%s%s",
iamrecursive
? " -r" : "", targetshouldbedirectory
? " -d" : "");
sigsys(SIGPIPE
, lostconn
);
targ
= colon(argv
[argc
- 1]);
tuser
= rindex(argv
[argc
- 1], '.');
for (i
= 0; i
< argc
- 1; i
++) {
suser
= rindex(argv
[i
], '.');
(void) sprintf(buf
, "rsh %s -L %s %s %s '%s:%s' </dev/null",
src
, argv
[argc
- 1], targ
);
(void) sprintf(buf
, "rsh %s %s %s '%s:%s' </dev/null",
src
, argv
[argc
- 1], targ
);
(void) sprintf(buf
, "%s -t %s",
rem
= rcmd(&host
, IPPORT_CMDSERVER
,
if (targetshouldbedirectory
)
verifydir(argv
[argc
- 1]);
for (i
= 0; i
< argc
- 1; i
++) {
(void) sprintf(buf
, "/bin/cp%s %s %s",
iamrecursive
? " -r" : "",
argv
[i
], argv
[argc
- 1]);
suser
= rindex(argv
[i
], '.');
(void) sprintf(buf
, "%s -f %s", cmd
, src
);
rem
= rcmd(&host
, IPPORT_CMDSERVER
,
if ((stb
.st_mode
& S_IFMT
) == S_IFDIR
)
error("rcp: %s: %s.\n", cp
, sys_errlist
[errno
]);
if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
fprintf(stderr
, "rcp: invalid user name %s\n", cp0
);
for (x
= 0; x
< argc
; x
++) {
if (access(name
, 4) < 0 || (f
= open(name
, 0)) < 0) {
error("rcp: %s: %s\n", name
, sys_errlist
[errno
]);
switch (stb
.st_mode
&S_IFMT
) {
rsource(name
, (int)stb
.st_mode
);
error("rcp: %s: not a plain file\n", name
);
last
= rindex(name
, '/');
(void) sprintf(buf
, "C%04o %D %s\n",
stb
.st_mode
&07777, stb
.st_size
, last
);
(void) write(rem
, buf
, strlen(buf
));
for (i
= 0; i
< stb
.st_size
; i
+= BUFSIZ
) {
if (i
+ amt
> stb
.st_size
)
if (sizerr
== 0 && read(f
, buf
, amt
) != amt
)
(void) write(rem
, buf
, amt
);
error("rcp: %s: file changed size\n", name
);
error("%s: %s\n", name
, sys_errlist
[errno
]);
last
= rindex(name
, '/');
(void) sprintf(buf
, "D%04o %d %s\n", mode
&07777, 0, last
);
(void) write(rem
, buf
, strlen(buf
));
while (dp
= readdir(d
)) {
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (strlen(name
) + 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
error("%s/%s: Name too long.\n", name
, dp
->d_name
);
(void) sprintf(buf
, "%s/%s", name
, dp
->d_name
);
(void) write(rem
, "E\n", 2);
char resp
, c
, rbuf
[BUFSIZ
], *cp
= rbuf
;
if (read(rem
, &resp
, 1) != 1)
if (read(rem
, &c
, 1) != 1)
} while (cp
< &rbuf
[BUFSIZ
] && c
!= '\n');
(void) write(2, rbuf
, cp
- rbuf
);
fprintf(stderr
, "rcp: lost connection\n");
char cmdbuf
[BUFSIZ
], nambuf
[BUFSIZ
], buf
[BUFSIZ
], *cp
;
int of
, mode
, wrerr
, exists
;
struct stat stb
; int targisdir
= 0;
#define SCREWUP(str) { whopp = str; goto screwup; }
error("rcp: ambiguous target\n");
if (targetshouldbedirectory
)
if (stat(targ
, &stb
) == 0 && (stb
.st_mode
& S_IFMT
) == S_IFDIR
)
if (read(rem
, cp
, 1) <= 0)
SCREWUP("unexpected '\\n'");
if (read(rem
, cp
, 1) != 1)
SCREWUP("lost connection");
if (cmdbuf
[0] == '\01' || cmdbuf
[0] == '\02') {
(void) write(2, cmdbuf
, strlen(cmdbuf
));
if (*cp
!= 'C' && *cp
!= 'D')
SCREWUP("expected control record");
for (; cp
< cmdbuf
+5; cp
++) {
if (*cp
< '0' || *cp
> '7')
mode
= (mode
<< 3) | (*cp
- '0');
SCREWUP("mode not delimited");
while (*cp
>= '0' && *cp
<= '9')
size
= size
* 10 + (*cp
++ - '0');
SCREWUP("size not delimited");
(void) sprintf(nambuf
, "%s%s%s", targ
,
(void) strcpy(nambuf
, targ
);
exists
= stat(nambuf
, &stb
) == 0;
if (exists
&& access(nambuf
, 2) < 0)
{ char *slash
= rindex(nambuf
, '/'), *dir
;
if (exists
== 0 && access(dir
, 2) < 0)
if (stat(nambuf
, &stb
) == 0) {
if ((stb
.st_mode
&S_IFMT
) != S_IFDIR
) {
} else if (mkdir(nambuf
, mode
) < 0)
if ((of
= creat(nambuf
, mode
)) < 0) {
error("rcp: %s: %s\n", nambuf
, sys_errlist
[errno
]);
(void) stat(nambuf
, &stb
);
(void) chown(nambuf
, pwd
->pw_uid
, stb
.st_gid
);
(void) chmod(nambuf
, mode
&~ mask
);
for (i
= 0; i
< size
; i
+= BUFSIZ
) {
int j
= read(rem
, cp
, amt
);
if (wrerr
== 0 && write(of
, buf
, amt
) != amt
)
error("rcp: %s: %s\n", cp
, sys_errlist
[errno
]);
error("rcp: protocol screwup: %s\n", whopp
);
error(fmt
, a1
, a2
, a3
, a4
, a5
)
char buf
[BUFSIZ
], *cp
= buf
;
(void) sprintf(cp
, fmt
, a1
, a2
, a3
, a4
, a5
);
(void) write(rem
, buf
, strlen(buf
));
(void) write(2, buf
+1, strlen(buf
+1));
if (chmod(name
, mode
) < 0) {
execv("/bin/mkdir", argv
);
execv("/usr/bin/mkdir", argv
);