| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include "system.h" |
| 30 |
#include "mfileio.h" |
| 31 |
#include "error.h" |
| 32 |
|
| 33 |
#ifdef IODEBUG |
| 34 |
static FILE *iodebug_file = NULL; |
| 35 |
static int event = 0; |
| 36 |
static void io_debug_init(void) |
| 37 |
{ |
| 38 |
if (!iodebug_file) { |
| 39 |
iodebug_file = fopen ("fopen.log", "wb"); |
| 40 |
fprintf (stderr, "\n*** File IO debugging started ***\n"); |
| 41 |
} |
| 42 |
if (!iodebug_file) { |
| 43 |
fprintf (stderr, "\nError opening io log\n"); |
| 44 |
} |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifdef IODEBUG |
| 49 |
FILE *mfopen(const char *name, const char *mode, const char *function, int line) |
| 50 |
{ |
| 51 |
FILE *tmp; |
| 52 |
io_debug_init(); |
| 53 |
#if defined(WIN32) |
| 54 |
tmp = fsyscp_fopen (name, mode); |
| 55 |
#else |
| 56 |
tmp = fopen (name, mode); |
| 57 |
#endif |
| 58 |
event += 1; |
| 59 |
fprintf(iodebug_file, "%p %07d [fopen] %s:%d\n", tmp, event, |
| 60 |
function, line); |
| 61 |
return tmp; |
| 62 |
} |
| 63 |
int mfclose(FILE *file, const char *function, int line) |
| 64 |
{ |
| 65 |
io_debug_init(); |
| 66 |
event += 1; |
| 67 |
fprintf(iodebug_file, "%p %07d [fclose] %s:%d\n", file, event, |
| 68 |
function, line); |
| 69 |
return fclose(file); |
| 70 |
} |
| 71 |
#endif |
| 72 |
|
| 73 |
static void os_error(void) |
| 74 |
{ |
| 75 |
ERROR ("io: An OS command failed that should not have.\n"); |
| 76 |
} |
| 77 |
|
| 78 |
void seek_absolute (FILE *file, int32_t pos) |
| 79 |
{ |
| 80 |
if (fseek(file, (long)pos, SEEK_SET)) { |
| 81 |
os_error(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void seek_relative (FILE *file, int32_t pos) |
| 86 |
{ |
| 87 |
if (fseek(file, (long)pos, SEEK_CUR)) { |
| 88 |
os_error(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
void seek_end (FILE *file) |
| 94 |
{ |
| 95 |
if (fseek(file, 0L, SEEK_END)) { |
| 96 |
os_error(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
int32_t tell_position (FILE *file) |
| 101 |
{ |
| 102 |
long size = ftell (file); |
| 103 |
if (size < 0) |
| 104 |
os_error(); |
| 105 |
#if LONG_MAX > 0x7fffffff |
| 106 |
if (size > 0x7fffffff) |
| 107 |
ERROR ("ftell: file size %ld exceeds 0x7fffffff.\n", size); |
| 108 |
#endif |
| 109 |
return size; |
| 110 |
} |
| 111 |
|
| 112 |
int32_t file_size (FILE *file) |
| 113 |
{ |
| 114 |
int32_t size; |
| 115 |
/* Seek to end */ |
| 116 |
seek_end (file); |
| 117 |
size = tell_position (file); |
| 118 |
rewind (file); |
| 119 |
return size; |
| 120 |
} |
| 121 |
|
| 122 |
off_t xfile_size (FILE *file, const char *name) |
| 123 |
{ |
| 124 |
off_t size; |
| 125 |
xseek_end (file, name); |
| 126 |
size = xtell_position (file, name); |
| 127 |
rewind (file); |
| 128 |
return size; |
| 129 |
} |
| 130 |
|
| 131 |
/* Unlike fgets, mfgets works with \r, \n, or \r\n end of lines. */ |
| 132 |
char *mfgets (char *buffer, int length, FILE *file) |
| 133 |
{ |
| 134 |
int ch = 0, i = 0; |
| 135 |
while (i < length-1 && (ch = fgetc (file)) >= 0 && ch != '\n' && ch != '\r') |
| 136 |
buffer[i++] = ch; |
| 137 |
buffer[i] = 0; |
| 138 |
if (ch < 0 && i == 0) |
| 139 |
return NULL; |
| 140 |
if (ch == '\r' && (ch = fgetc (file)) >= 0 && (ch != '\n')) |
| 141 |
ungetc (ch, file); |
| 142 |
return buffer; |
| 143 |
} |
| 144 |
|
| 145 |
/* As each lines may contain null-characters, so outptr here is NOT |
| 146 |
* null-terminated string. |
| 147 |
* Returns -1 for when EOF is already reached, and -2 if buffer has no |
| 148 |
* enough space. |
| 149 |
*/ |
| 150 |
int |
| 151 |
mfreadln (char *buf, int size, FILE *fp) |
| 152 |
{ |
| 153 |
int c; |
| 154 |
int len = 0; |
| 155 |
|
| 156 |
while ((c = fgetc(fp)) != EOF && c != '\n' && c != '\r') { |
| 157 |
if (len >= size) { |
| 158 |
return -2; |
| 159 |
} |
| 160 |
buf[len++] = (char) c; |
| 161 |
} |
| 162 |
if (c == EOF && len == 0) { |
| 163 |
return -1; |
| 164 |
} |
| 165 |
if (c == '\r' && (c = fgetc(fp)) >= 0 && (c != '\n')) |
| 166 |
ungetc(c, fp); |
| 167 |
|
| 168 |
return len; |
| 169 |
} |
| 170 |
|
| 171 |
char work_buffer[WORK_BUFFER_SIZE]; |