BSD 3 development
[unix-history] / usr / src / cmd / uucp / cpmv.c
CommitLineData
3e0036ea
BJ
1#include "uucp.h"
2#include <sys/types.h>
3#include <sys/stat.h>
4
5/***
6 * xcp(f1, f2) copy f1 to f2
7 * char *f1, *f2;
8 *
9 * return - 0 ok | FAIL failed
10 */
11
12xcp(f1, f2)
13char *f1, *f2;
14{
15 char buf[BUFSIZ];
16 int len;
17 FILE *fp1, *fp2;
18 char *lastpart();
19 char full[100];
20 struct stat s;
21
22 if ((fp1 = fopen(f1, "r")) == NULL)
23 return(FAIL);
24 strcpy(full, f2);
25 if (stat(f2, &s) == 0) {
26 /* check for directory */
27 if ((s.st_mode & S_IFMT) == S_IFDIR) {
28 strcat(full, "/");
29 strcat(full, lastpart(f1));
30 }
31 }
32 DEBUG(4, "full %s\n", full);
33 if ((fp2 = fopen(full, "w")) == NULL) {
34 fclose(fp1);
35 return(FAIL);
36 }
37 while((len = fread(buf, sizeof (char), BUFSIZ, fp1)) > 0)
38 fwrite(buf, sizeof (char), len, fp2);
39 fclose(fp1);
40 fclose(fp2);
41 chmod(f2, 0666);
42 return(0);
43}
44
45
46/*
47 * xmv(f1, f2) move f1 to f2
48 * char * f1, *f2;
49 *
50 * return 0 ok | FAIL failed
51 */
52
53xmv(f1, f2)
54char *f1, *f2;
55{
56 int ret;
57
58 if (link(f1, f2) < 0) {
59 /* copy file */
60 ret = xcp(f1, f2);
61 if (ret == 0)
62 unlink(f1);
63 return(ret);
64 }
65 unlink(f1);
66 return(0);
67}