xref: /original-bsd/usr.bin/uucp/libuu/cpmv.c (revision 8216efb6)
19677bed1Sbostic /*-
2*8216efb6Sbostic  * Copyright (c) 1985, 1993
3*8216efb6Sbostic  *	The Regents of the University of California.  All rights reserved.
49677bed1Sbostic  *
59677bed1Sbostic  * %sccs.include.proprietary.c%
69677bed1Sbostic  */
79677bed1Sbostic 
855e42db3Ssam #ifndef lint
9*8216efb6Sbostic static char sccsid[] = "@(#)cpmv.c	8.1 (Berkeley) 06/06/93";
109677bed1Sbostic #endif /* not lint */
1155e42db3Ssam 
1255e42db3Ssam #include "uucp.h"
1355e42db3Ssam #include <sys/stat.h>
1455e42db3Ssam 
15dd774741Sbloom /*LINTLIBRARY*/
16dd774741Sbloom 
17dd774741Sbloom /*
18dd774741Sbloom  *	copy f1 to f2
1955e42db3Ssam  *
2089e46e8dSbloom  *	return - SUCCESS | FAIL
2155e42db3Ssam  */
xcp(f1,f2)2255e42db3Ssam xcp(f1, f2)
2355e42db3Ssam char *f1, *f2;
2455e42db3Ssam {
2555e42db3Ssam 	char buf[BUFSIZ];
2655e42db3Ssam 	register int len;
27c9dd95d8Sralph 	register int fp1, fp2;
2855e42db3Ssam 	char *lastpart();
2989e46e8dSbloom 	char full[MAXFULLNAME];
3055e42db3Ssam 	struct stat s;
3155e42db3Ssam 
32c9dd95d8Sralph 	if ((fp1 = open(subfile(f1), 0)) < 0)
33c9dd95d8Sralph 		return FAIL;
3455e42db3Ssam 	strcpy(full, f2);
3555e42db3Ssam 	if (stat(subfile(f2), &s) == 0) {
3655e42db3Ssam 		/* check for directory */
3755e42db3Ssam 		if ((s.st_mode & S_IFMT) == S_IFDIR) {
3855e42db3Ssam 			strcat(full, "/");
3955e42db3Ssam 			strcat(full, lastpart(f1));
4055e42db3Ssam 		}
4155e42db3Ssam 	}
4255e42db3Ssam 	DEBUG(4, "full %s\n", full);
43c9dd95d8Sralph 	if ((fp2 = creat(subfile(full), 0666)) < 0) {
44c9dd95d8Sralph 		close(fp1);
45c9dd95d8Sralph 		return FAIL;
4655e42db3Ssam 	}
47c9dd95d8Sralph 	while((len = read(fp1, buf, BUFSIZ)) > 0)
48c9dd95d8Sralph 		 if (write(fp2, buf, len) != len) {
49c9dd95d8Sralph 			len = -1;
50c9dd95d8Sralph 			break;
51c9dd95d8Sralph 		}
52c9dd95d8Sralph 
53c9dd95d8Sralph 	close(fp1);
54c9dd95d8Sralph 	close(fp2);
55c9dd95d8Sralph 	return len < 0 ? FAIL: SUCCESS;
5655e42db3Ssam }
5755e42db3Ssam 
5855e42db3Ssam 
5955e42db3Ssam /*
6089e46e8dSbloom  *	move f1 to f2
6155e42db3Ssam  *
6255e42db3Ssam  *	return  0 ok  |  FAIL failed
6355e42db3Ssam  */
xmv(f1,f2)6455e42db3Ssam xmv(f1, f2)
6555e42db3Ssam register char *f1, *f2;
6655e42db3Ssam {
6755e42db3Ssam 	register int ret;
6855e42db3Ssam 
6989e46e8dSbloom 	(void) unlink(subfile(f2));
7055e42db3Ssam 	if (link(subfile(f1), subfile(f2)) < 0) {
7155e42db3Ssam 		/*  copy file  */
7255e42db3Ssam 		ret = xcp(f1, f2);
7355e42db3Ssam 		if (ret == 0)
7455e42db3Ssam 			unlink(subfile(f1));
75c9dd95d8Sralph 		return ret;
7655e42db3Ssam 	}
7789e46e8dSbloom 	(void) unlink(subfile(f1));
78c9dd95d8Sralph 	return 0;
7955e42db3Ssam }
80