Debian Codegraph @

texk/seetexk/seek.c

raw
1 /* Copyright (c) 1987, 1989, 2012 University of Maryland Department of
2 Computer Science.
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation, the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions: The above copyright notice, this permission
11 notice and the disclaimer statement shall be included in all copies
12 or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * SeekFile copies an input stdio file, if necessary, so that
25 * it produces a stdio file on which fseek() works properly.
26 * It returns NULL if this cannot be done; in that case, the
27 * input file is closed (or otherwise rendered worthless).
28 *
29 * CopyFile copies an input file unconditionally. (On non-Unix
30 * machines, it might `accidentally' not copy it.)
31 *
32 * On Unix machines, this means `if the input is a pipe or tty,
33 * copy it to a temporary file'. On other systems, all stdio files
34 * might happen to be seekable.
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef KPATHSEA
42 #include <kpathsea/c-fopen.h>
43 #endif
44
45 #include <stdio.h>
46 #include "types.h" /* for BSD_FILE_SYSTEM */
47 #include "seek.h"
48 #include "tempfile.h"
49
50 #include <errno.h>
51 #ifdef BSD_FILE_SYSTEM
52 #include <sys/param.h> /* want MAXBSIZE */
53 #else
54 #include <sys/types.h>
55 #endif
56 #include <sys/stat.h>
57
58 #ifndef KPATHSEA
59 long lseek();
60 char *malloc();
61
62 extern int errno;
63 #endif
64
65 /*
66 * Make and return a version of `f' on which fseek works (unconditionally).
67 * This code is somewhat Unix-specific.
68 */
69 FILE *
70 CopyFile(FILE *f)
71 {
72 register int tf, n, ifd, w;
73 register char *p, *buf;
74 register int blksize;
75 #ifdef BSD_FILE_SYSTEM
76 struct stat st;
77 #endif
78 int e;
79 #ifdef MAXBSIZE
80 #define BSIZE MAXBSIZE
81 #else
82 #define BSIZE BUFSIZ
83 #endif
84 char stackbuf[BSIZE];
85 #if defined(WIN32) || defined(MSDOS)
86 int orig_fdmode;
87 #endif
88 const char *open_mode;
89
90 /* get a read/write temp file which will vanish when closed */
91 if ((tf = MakeRWTempFile(stackbuf)) < 0) {
92 e = errno;
93 (void) fclose(f);
94 errno = e;
95 return (NULL);
96 }
97
98 /* compute buffer size and choose buffer */
99 ifd = fileno(f);
100 buf = stackbuf;
101 blksize = sizeof stackbuf;
102 #ifdef BSD_FILE_SYSTEM
103 if (fstat(tf, &st) == 0 && st.st_blksize > blksize) {
104 /*
105 * The output block size is the important one,
106 * but the input block size is not irrelevant.
107 *
108 * This should actually compute the lcm, but we
109 * will rely on block sizes being powers of two
110 * (so that the larger is the lcm).
111 */
112 blksize = st.st_blksize;
113 if (fstat(ifd, &st) == 0 && st.st_blksize > blksize)
114 blksize = st.st_blksize;
115 if ((buf = malloc((unsigned)blksize)) == NULL) {
116 buf = stackbuf;
117 blksize = sizeof stackbuf;
118 }
119 }
120 #endif
121
122 #if defined(WIN32) || defined(MSDOS)
123 /* make sure we open the temp file in the same mode that
124 the original handle was open. */
125 orig_fdmode = setmode(ifd, 0);
126 setmode(tf, orig_fdmode);
127 #endif
128 /* copy from input file to temp file */
129 (void) lseek(ifd, 0L, 0); /* paranoia */
130 while ((n = read(ifd, p = buf, blksize)) > 0) {
131 do {
132 if ((w = write(tf, p, n)) < 0) {
133 (void) close(tf);
134 (void) fclose(f);
135 return (NULL);
136 }
137 p += w;
138 } while ((n -= w) > 0);
139 }
140 e = errno; /* in case n < 0 */
141 if (buf != stackbuf)
142 free(buf);
143 if (n < 0) {
144 (void) close(tf);
145 (void) fclose(f);
146 errno = e;
147 return (NULL);
148 }
149
150 /* discard the input file, and rewind and open the temporary */
151 (void) fclose(f);
152 (void) lseek(tf, 0L, 0);
153 errno = 0;
154 #if defined(WIN32) || defined(MSDOS)
155 open_mode = orig_fdmode == O_BINARY ? FOPEN_RBIN_MODE : "r";
156 #else
157 open_mode = "r";
158 #endif
159 if ((f = fdopen(tf, open_mode)) == NULL) {
160 if (errno == 0)
161 e = EMFILE;
162 (void) close(tf);
163 errno = e;
164 }
165 return (f);
166 }
167
168 /*
169 * Copy an input file, but only if necessary.
170 */
171 FILE *SeekFile(FILE *f)
172 {
173 int fd = fileno(f);
174
175 return (lseek(fd, 0L, 1) >= 0 && !isatty(fd) ? f : CopyFile(f));
176 }