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