aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/bootlib/osfcopy.c
blob: 037d6eff09d4e73a609248c2c680a1652827b49d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "bootlib.h"

extern	int  os_access (char *fname, int mode, int type);


/* OS_FCOPY -- Copy a file.  Used by RTAR to resolve links.
 */
int
os_fcopy (
  char	*oldfile,
  char	*newfile
)
{
	XCHAR	buf[SZ_FBUF];
	XINT	status,	junk, maxch = SZ_FBUF, mode = 0, in, out, n, nw;

	extern  int ZOPNTX(), ZGETTX(), ZCLSTX(), ZPUTTX();


	if (os_access (oldfile,0,0) == NO)
	    return (ERR);

	if (os_access (oldfile, 0, TEXT_FILE) == YES) {
	    if (bdebug)
		fprintf (stderr, "copy text file '%s' -> '%s'\n",
		    oldfile, newfile);

	    mode = READ_ONLY;
	    ZOPNTX ((PKCHAR *)vfn2osfn(oldfile,0), &mode, &in);
	    if (in == XERR)
		return (ERR);

	    mode = NEW_FILE;
	    ZOPNTX ((PKCHAR *)vfn2osfn(newfile,1), &mode, &out);
	    if (out == XERR) {
		ZCLSTX (&in, &status);
		return (ERR);
	    }

	    while (ZGETTX (&in, buf, &maxch, &n), n != XEOF) {
		if (n != XERR)
		    ZPUTTX (&out, buf, &n, &status);
		if (n == XERR || status == XERR) {
		    ZCLSTX (&in, &junk);
		    ZCLSTX (&out, &junk);
		    return (ERR);
		}
	    }

	    ZCLSTX (&in, &status);
	    ZCLSTX (&out, &status);

	    return (status);

	} else {
	    if (bdebug)
		fprintf (stderr, "copy binary file `%s' -> `%s'\n",
		    oldfile, newfile);

	    if ((in = open (vfn2osfn(oldfile,0), 0)) == ERR)
		return (ERR);
	    if ((out = creat (vfn2osfn(newfile,1), 0644)) == ERR) {
		close (in);
		return (ERR);
	    }

	    while ((n = read (in, (char *)buf, SZ_FBUF)) > 0)
		nw = write (out, (char *)buf, n);

	    close (in);
	    close (out);
	    if (n < 0)
		return (ERR);
	}

	return (ERR);
}