xref: /csrg-svn/usr.bin/uucp/libuu/cpmv.c (revision 17835)
113643Ssam #ifndef lint
2*17835Sralph static char sccsid[] = "@(#)cpmv.c	5.3 (Berkeley) 01/22/85";
313643Ssam #endif
413643Ssam 
513643Ssam #include "uucp.h"
613643Ssam #include <sys/types.h>
713643Ssam #include <sys/stat.h>
813643Ssam 
913643Ssam /***
1013643Ssam  *	xcp(f1, f2)	copy f1 to f2
1113643Ssam  *	char *f1, *f2;
1213643Ssam  *
1313643Ssam  *	return - 0 ok  |  FAIL failed
1413643Ssam  */
1513643Ssam 
1613643Ssam xcp(f1, f2)
1713643Ssam char *f1, *f2;
1813643Ssam {
1913643Ssam 	char buf[BUFSIZ];
2013643Ssam 	register int len;
21*17835Sralph 	register int fp1, fp2;
2213643Ssam 	char *lastpart();
23*17835Sralph 	char full[100];
2413643Ssam 	struct stat s;
2513643Ssam 
26*17835Sralph 	if ((fp1 = open(subfile(f1), 0)) < 0)
27*17835Sralph 		return FAIL;
2813643Ssam 	strcpy(full, f2);
2913643Ssam 	if (stat(subfile(f2), &s) == 0) {
3013643Ssam 		/* check for directory */
3113643Ssam 		if ((s.st_mode & S_IFMT) == S_IFDIR) {
3213643Ssam 			strcat(full, "/");
3313643Ssam 			strcat(full, lastpart(f1));
3413643Ssam 		}
3513643Ssam 	}
3613643Ssam 	DEBUG(4, "full %s\n", full);
37*17835Sralph 	if ((fp2 = creat(subfile(full), 0666)) < 0) {
38*17835Sralph 		close(fp1);
39*17835Sralph 		return FAIL;
4013643Ssam 	}
41*17835Sralph 	while((len = read(fp1, buf, BUFSIZ)) > 0)
42*17835Sralph 		 if (write(fp2, buf, len) != len) {
43*17835Sralph 			len = -1;
44*17835Sralph 			break;
45*17835Sralph 		}
46*17835Sralph 
47*17835Sralph 	close(fp1);
48*17835Sralph 	close(fp2);
49*17835Sralph 	return len < 0 ? FAIL: SUCCESS;
5013643Ssam }
5113643Ssam 
5213643Ssam 
5313643Ssam /*
5413643Ssam  *	xmv(f1, f2)	move f1 to f2
5513643Ssam  *	char * f1, *f2;
5613643Ssam  *
5713643Ssam  *	return  0 ok  |  FAIL failed
5813643Ssam  */
5913643Ssam 
6013643Ssam xmv(f1, f2)
6113643Ssam register char *f1, *f2;
6213643Ssam {
6313643Ssam 	register int ret;
6413643Ssam 
6513643Ssam 	if (link(subfile(f1), subfile(f2)) < 0) {
6613643Ssam 		/*  copy file  */
6713643Ssam 		ret = xcp(f1, f2);
6813643Ssam 		if (ret == 0)
6913643Ssam 			unlink(subfile(f1));
70*17835Sralph 		return ret;
7113643Ssam 	}
7213643Ssam 	unlink(subfile(f1));
73*17835Sralph 	return 0;
7413643Ssam }
75