xref: /csrg-svn/usr.bin/uucp/libuu/cpmv.c (revision 25129)
113643Ssam #ifndef lint
2*25129Sbloom static char sccsid[] = "@(#)cpmv.c	5.5 (Berkeley) 10/09/85";
313643Ssam #endif
413643Ssam 
513643Ssam #include "uucp.h"
613643Ssam #include <sys/stat.h>
713643Ssam 
823597Sbloom /*LINTLIBRARY*/
923597Sbloom 
1023597Sbloom /*
1123597Sbloom  *	copy f1 to f2
1213643Ssam  *
13*25129Sbloom  *	return - SUCCESS | FAIL
1413643Ssam  */
1513643Ssam xcp(f1, f2)
1613643Ssam char *f1, *f2;
1713643Ssam {
1813643Ssam 	char buf[BUFSIZ];
1913643Ssam 	register int len;
2017835Sralph 	register int fp1, fp2;
2113643Ssam 	char *lastpart();
22*25129Sbloom 	char full[MAXFULLNAME];
2313643Ssam 	struct stat s;
2413643Ssam 
2517835Sralph 	if ((fp1 = open(subfile(f1), 0)) < 0)
2617835Sralph 		return FAIL;
2713643Ssam 	strcpy(full, f2);
2813643Ssam 	if (stat(subfile(f2), &s) == 0) {
2913643Ssam 		/* check for directory */
3013643Ssam 		if ((s.st_mode & S_IFMT) == S_IFDIR) {
3113643Ssam 			strcat(full, "/");
3213643Ssam 			strcat(full, lastpart(f1));
3313643Ssam 		}
3413643Ssam 	}
3513643Ssam 	DEBUG(4, "full %s\n", full);
3617835Sralph 	if ((fp2 = creat(subfile(full), 0666)) < 0) {
3717835Sralph 		close(fp1);
3817835Sralph 		return FAIL;
3913643Ssam 	}
4017835Sralph 	while((len = read(fp1, buf, BUFSIZ)) > 0)
4117835Sralph 		 if (write(fp2, buf, len) != len) {
4217835Sralph 			len = -1;
4317835Sralph 			break;
4417835Sralph 		}
4517835Sralph 
4617835Sralph 	close(fp1);
4717835Sralph 	close(fp2);
4817835Sralph 	return len < 0 ? FAIL: SUCCESS;
4913643Ssam }
5013643Ssam 
5113643Ssam 
5213643Ssam /*
53*25129Sbloom  *	move f1 to f2
5413643Ssam  *
5513643Ssam  *	return  0 ok  |  FAIL failed
5613643Ssam  */
5713643Ssam xmv(f1, f2)
5813643Ssam register char *f1, *f2;
5913643Ssam {
6013643Ssam 	register int ret;
6113643Ssam 
62*25129Sbloom 	(void) unlink(subfile(f2));
6313643Ssam 	if (link(subfile(f1), subfile(f2)) < 0) {
6413643Ssam 		/*  copy file  */
6513643Ssam 		ret = xcp(f1, f2);
6613643Ssam 		if (ret == 0)
6713643Ssam 			unlink(subfile(f1));
6817835Sralph 		return ret;
6913643Ssam 	}
70*25129Sbloom 	(void) unlink(subfile(f1));
7117835Sralph 	return 0;
7213643Ssam }
73