summaryrefslogtreecommitdiff
path: root/Build/source/texk/seetexk/seek.c
blob: 64c8baa703787f4d9dd0723cfa228bff3bc2fa91 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 1987, 1989 University of Maryland
 * Department of Computer Science.  All rights reserved.
 * Permission to copy for any purpose is hereby granted
 * so long as this copyright notice remains intact.
 */

#ifndef lint
static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/seek.c,v 3.1 89/08/22 21:59:04 chris Exp $";
#endif

/*
 * SeekFile copies an input stdio file, if necessary, so that
 * it produces a stdio file on which fseek() works properly.
 * It returns NULL if this cannot be done; in that case, the
 * input file is closed (or otherwise rendered worthless).
 *
 * CopyFile copies an input file unconditionally.  (On non-Unix
 * machines, it might `accidentally' not copy it.)
 *
 * On Unix machines, this means `if the input is a pipe or tty,
 * copy it to a temporary file'.  On other systems, all stdio files
 * might happen to be seekable.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef KPATHSEA
#include <kpathsea/c-fopen.h>
#endif

#include <stdio.h>
#include "types.h"		/* for BSD_FILE_SYSTEM */
#include "seek.h"

#ifdef HAVE_PROTOTYPES
extern int MakeRWTempFile(char *);
#else
extern int MakeRWTempFile();
#endif

#ifdef EASY_BUT_NOT_GOOD

FILE *CopyFile(f) FILE *f; { return (f); }
FILE *SeekFile(f) FILE *f; { return (f); }

#else

#include <errno.h>
#ifdef BSD_FILE_SYSTEM
#include <sys/param.h>		/* want MAXBSIZE */
#else
#include <sys/types.h>
#endif
#include <sys/stat.h>

#ifndef KPATHSEA
long	lseek();
char	*malloc();

extern int errno;
#endif

/*
 * Make and return a version of `f' on which fseek works (unconditionally).
 * This code is somewhat Unix-specific.
 */
FILE *
CopyFile(f)
	FILE *f;
{
	register int tf, n, ifd, w;
	register char *p, *buf;
	register int blksize;
	struct stat st;
	int e;
#ifdef MAXBSIZE
#define BSIZE MAXBSIZE
#else
#define BSIZE BUFSIZ
#endif
	char stackbuf[BSIZE];
#if defined(WIN32) || defined(MSDOS) || defined(__CYGWIN32__)
	int orig_fdmode;
#endif
	char *open_mode;

	/* get a read/write temp file which will vanish when closed */
	if ((tf = MakeRWTempFile(stackbuf)) < 0) {
		e = errno;
		(void) fclose(f);
		errno = e;
		return (NULL);
	}

	/* compute buffer size and choose buffer */
	ifd = fileno(f);
	buf = stackbuf;
	blksize = sizeof stackbuf;
#ifdef BSD_FILE_SYSTEM
	if (fstat(tf, &st) == 0 && st.st_blksize > blksize) {
		/*
		 * The output block size is the important one,
		 * but the input block size is not irrelevant.
		 *
		 * This should actually compute the lcm, but we
		 * will rely on block sizes being powers of two
		 * (so that the larger is the lcm).
		 */
		blksize = st.st_blksize;
		if (fstat(ifd, &st) == 0 && st.st_blksize > blksize)
			blksize = st.st_blksize;
		if ((buf = malloc((unsigned)blksize)) == NULL) {
			buf = stackbuf;
			blksize = sizeof stackbuf;
		}
	}
#endif

#if defined(WIN32) || defined(MSDOS) || defined(__CYGWIN32__)
	/* make sure we open the temp file in the same mode that
	   the original handle was open.  */
	orig_fdmode = setmode(ifd, 0);
	setmode(tf, orig_fdmode);
#endif
	/* copy from input file to temp file */
	(void) lseek(ifd, 0L, 0);	/* paranoia */
	while ((n = read(ifd, p = buf, blksize)) > 0) {
		do {
			if ((w = write(tf, p, n)) < 0) {
				(void) close(tf);
				(void) fclose(f);
				return (NULL);
			}
			p += w;
		} while ((n -= w) > 0);
	}
	e = errno;		/* in case n < 0 */
	if (buf != stackbuf)
		free(buf);
	if (n < 0) {
		(void) close(tf);
		(void) fclose(f);
		errno = e;
		return (NULL);
	}

	/* discard the input file, and rewind and open the temporary */
	(void) fclose(f);
	(void) lseek(tf, 0L, 0);
	errno = 0;
#if defined(WIN32) || defined(MSDOS) || defined(__CYGWIN32__)
	open_mode =  orig_fdmode == O_BINARY ? FOPEN_RBIN_MODE : "r";
#else
	open_mode = "r";
#endif
	if ((f = fdopen(tf, open_mode)) == NULL) {
		if (errno == 0)
			e = EMFILE;
		(void) close(tf);
		errno = e;
	}
	return (f);
}

/*
 * Copy an input file, but only if necessary.
 */
FILE *SeekFile(f)
	FILE *f;
{
	int fd = fileno(f);

	return (lseek(fd, 0L, 1) >= 0 && !isatty(fd) ? f : CopyFile(f));
}

#endif /* EASY_BUT_NOT_GOOD */