xref: /original-bsd/usr.bin/uucp/libuu/cpmv.c (revision c577960b)
1 #ifndef lint
2 static char sccsid[] = "@(#)cpmv.c	5.5 (Berkeley) 10/09/85";
3 #endif
4 
5 #include "uucp.h"
6 #include <sys/stat.h>
7 
8 /*LINTLIBRARY*/
9 
10 /*
11  *	copy f1 to f2
12  *
13  *	return - SUCCESS | FAIL
14  */
15 xcp(f1, f2)
16 char *f1, *f2;
17 {
18 	char buf[BUFSIZ];
19 	register int len;
20 	register int fp1, fp2;
21 	char *lastpart();
22 	char full[MAXFULLNAME];
23 	struct stat s;
24 
25 	if ((fp1 = open(subfile(f1), 0)) < 0)
26 		return FAIL;
27 	strcpy(full, f2);
28 	if (stat(subfile(f2), &s) == 0) {
29 		/* check for directory */
30 		if ((s.st_mode & S_IFMT) == S_IFDIR) {
31 			strcat(full, "/");
32 			strcat(full, lastpart(f1));
33 		}
34 	}
35 	DEBUG(4, "full %s\n", full);
36 	if ((fp2 = creat(subfile(full), 0666)) < 0) {
37 		close(fp1);
38 		return FAIL;
39 	}
40 	while((len = read(fp1, buf, BUFSIZ)) > 0)
41 		 if (write(fp2, buf, len) != len) {
42 			len = -1;
43 			break;
44 		}
45 
46 	close(fp1);
47 	close(fp2);
48 	return len < 0 ? FAIL: SUCCESS;
49 }
50 
51 
52 /*
53  *	move f1 to f2
54  *
55  *	return  0 ok  |  FAIL failed
56  */
57 xmv(f1, f2)
58 register char *f1, *f2;
59 {
60 	register int ret;
61 
62 	(void) unlink(subfile(f2));
63 	if (link(subfile(f1), subfile(f2)) < 0) {
64 		/*  copy file  */
65 		ret = xcp(f1, f2);
66 		if (ret == 0)
67 			unlink(subfile(f1));
68 		return ret;
69 	}
70 	(void) unlink(subfile(f1));
71 	return 0;
72 }
73