aboutsummaryrefslogtreecommitdiff
path: root/sys/fio/fcopy.x
blob: de21fba8740c7c41d2da5b356201cc0a1d9dc409 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<fset.h>

define	MIN_BUFSIZE	512


# FCOPY -- Copy a file.  Works for either text or binary files.  The new file
# will not be created unless the input file can be opened successfully.  All
# buffer space is dynamically allocated, and buffer sizes are automatically
# adjusted by the system for efficient sequential access (the actual buffer
# size is dependent on the machine, device, and file type).

procedure fcopy (oldfile, newfile)

char	oldfile[ARB]
char	newfile[ARB]

int	in, out, file_type, fd
int	open(), access(), fsfopen(), fstdfile()
errchk	open, fcopyo, access

begin
	if (access (oldfile, 0, TEXT_FILE) == YES)
	    file_type = TEXT_FILE
	else
	    file_type = BINARY_FILE

	in = open (oldfile, READ_ONLY, file_type)
	if (fstdfile (newfile, out) == NO) {
	    iferr (call fmkcopy (oldfile, newfile)) {
		call close (in)
		call erract (EA_ERROR)
	    }
	    out = open (newfile, APPEND, file_type)
	}

	# Warn user if the file being copied has subfiles.
	ifnoerr (fd = fsfopen (oldfile, READ_ONLY)) {
	    call close (fd)
	    call eprintf ("Warning from fcopy: file `%s' has subfiles\n")
		call pargstr (oldfile)
	}

	# Copy the file.
	call fcopyo (in, out)

	call close (in)
	call close (out)
end


# FCOPYO -- Copy a file, where both the input and output files have
# already been open.  Works regardless of the datatype of the files.

procedure fcopyo (in, out)

int	in			# input file descriptor
int	out			# output file descriptor

pointer	sp, buf
int	buf_size
int	fstati(), read()
errchk	read, write

begin
	call smark (sp)

	# Set up file buffers, intermediate buffer for efficient
	# sequential i/o (advice is ignored if text file).  Local buffer
	# is made same size as FIO buffer.

	call fseti (in, F_ADVICE, SEQUENTIAL)
	call fseti (out, F_ADVICE, SEQUENTIAL)
	buf_size = max (MIN_BUFSIZE, fstati (in, F_BUFSIZE))
	call salloc (buf, buf_size, TY_CHAR)

	while (read (in, Memc[buf], buf_size) != EOF)
	    call write (out, Memc[buf], fstati (in, F_NCHARS))

	call sfree (sp)
end