xref: /csrg-svn/usr.bin/uucp/libuu/cpmv.c (revision 62386)
148652Sbostic /*-
2*62386Sbostic  * Copyright (c) 1985, 1993
3*62386Sbostic  *	The Regents of the University of California.  All rights reserved.
448652Sbostic  *
548652Sbostic  * %sccs.include.proprietary.c%
648652Sbostic  */
748652Sbostic 
813643Ssam #ifndef lint
9*62386Sbostic static char sccsid[] = "@(#)cpmv.c	8.1 (Berkeley) 06/06/93";
1048652Sbostic #endif /* not lint */
1113643Ssam 
1213643Ssam #include "uucp.h"
1313643Ssam #include <sys/stat.h>
1413643Ssam 
1523597Sbloom /*LINTLIBRARY*/
1623597Sbloom 
1723597Sbloom /*
1823597Sbloom  *	copy f1 to f2
1913643Ssam  *
2025129Sbloom  *	return - SUCCESS | FAIL
2113643Ssam  */
xcp(f1,f2)2213643Ssam xcp(f1, f2)
2313643Ssam char *f1, *f2;
2413643Ssam {
2513643Ssam 	char buf[BUFSIZ];
2613643Ssam 	register int len;
2717835Sralph 	register int fp1, fp2;
2813643Ssam 	char *lastpart();
2925129Sbloom 	char full[MAXFULLNAME];
3013643Ssam 	struct stat s;
3113643Ssam 
3217835Sralph 	if ((fp1 = open(subfile(f1), 0)) < 0)
3317835Sralph 		return FAIL;
3413643Ssam 	strcpy(full, f2);
3513643Ssam 	if (stat(subfile(f2), &s) == 0) {
3613643Ssam 		/* check for directory */
3713643Ssam 		if ((s.st_mode & S_IFMT) == S_IFDIR) {
3813643Ssam 			strcat(full, "/");
3913643Ssam 			strcat(full, lastpart(f1));
4013643Ssam 		}
4113643Ssam 	}
4213643Ssam 	DEBUG(4, "full %s\n", full);
4317835Sralph 	if ((fp2 = creat(subfile(full), 0666)) < 0) {
4417835Sralph 		close(fp1);
4517835Sralph 		return FAIL;
4613643Ssam 	}
4717835Sralph 	while((len = read(fp1, buf, BUFSIZ)) > 0)
4817835Sralph 		 if (write(fp2, buf, len) != len) {
4917835Sralph 			len = -1;
5017835Sralph 			break;
5117835Sralph 		}
5217835Sralph 
5317835Sralph 	close(fp1);
5417835Sralph 	close(fp2);
5517835Sralph 	return len < 0 ? FAIL: SUCCESS;
5613643Ssam }
5713643Ssam 
5813643Ssam 
5913643Ssam /*
6025129Sbloom  *	move f1 to f2
6113643Ssam  *
6213643Ssam  *	return  0 ok  |  FAIL failed
6313643Ssam  */
xmv(f1,f2)6413643Ssam xmv(f1, f2)
6513643Ssam register char *f1, *f2;
6613643Ssam {
6713643Ssam 	register int ret;
6813643Ssam 
6925129Sbloom 	(void) unlink(subfile(f2));
7013643Ssam 	if (link(subfile(f1), subfile(f2)) < 0) {
7113643Ssam 		/*  copy file  */
7213643Ssam 		ret = xcp(f1, f2);
7313643Ssam 		if (ret == 0)
7413643Ssam 			unlink(subfile(f1));
7517835Sralph 		return ret;
7613643Ssam 	}
7725129Sbloom 	(void) unlink(subfile(f1));
7817835Sralph 	return 0;
7913643Ssam }
80