| 1 |
/* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki. |
| 2 |
|
| 3 |
Copyright (C) 2016 by VTeX Ltd (www.vtex.lt), |
| 4 |
the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis. |
| 5 |
|
| 6 |
Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and |
| 7 |
Shunsaku Hirata, the dvipdfmx project team. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include "system.h" |
| 29 |
#include "mfileio.h" |
| 30 |
#include "error.h" |
| 31 |
|
| 32 |
#ifdef IODEBUG |
| 33 |
static FILE *iodebug_file = NULL; |
| 34 |
static long event = 0; |
| 35 |
static void io_debug_init(void) |
| 36 |
{ |
| 37 |
if (!iodebug_file) { |
| 38 |
iodebug_file = fopen ("fopen.log", "wb"); |
| 39 |
fprintf (stderr, "\n*** File IO debugging started ***\n"); |
| 40 |
} |
| 41 |
if (!iodebug_file) { |
| 42 |
fprintf (stderr, "\nError opening io log\n"); |
| 43 |
} |
| 44 |
} |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef IODEBUG |
| 48 |
FILE *mfopen(const char *name, const char *mode, const char *function, int line) |
| 49 |
{ |
| 50 |
FILE *tmp; |
| 51 |
io_debug_init(); |
| 52 |
#if defined(WIN32) |
| 53 |
tmp = fsyscp_fopen (name, mode); |
| 54 |
#else |
| 55 |
tmp = fopen (name, mode); |
| 56 |
#endif |
| 57 |
event += 1; |
| 58 |
fprintf(iodebug_file, "%p %07ld [fopen] %s:%d\n", tmp, event, |
| 59 |
function, line); |
| 60 |
return tmp; |
| 61 |
} |
| 62 |
int mfclose(FILE *file, const char *function, int line) |
| 63 |
{ |
| 64 |
io_debug_init(); |
| 65 |
event += 1; |
| 66 |
fprintf(iodebug_file, "%p %07ld [fclose] %s:%d\n", file, event, |
| 67 |
function, line); |
| 68 |
return fclose(file); |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
static void os_error(void) |
| 73 |
{ |
| 74 |
ERROR ("io: An OS command failed that should not have.\n"); |
| 75 |
} |
| 76 |
|
| 77 |
void seek_absolute (FILE *file, long pos) |
| 78 |
{ |
| 79 |
if (fseek(file, pos, SEEK_SET)) { |
| 80 |
os_error(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void seek_relative (FILE *file, long pos) |
| 85 |
{ |
| 86 |
if (fseek(file, pos, SEEK_CUR)) { |
| 87 |
os_error(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
void seek_end (FILE *file) |
| 93 |
{ |
| 94 |
if (fseek(file, 0L, SEEK_END)) { |
| 95 |
os_error(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
long tell_position (FILE *file) |
| 100 |
{ |
| 101 |
long size; |
| 102 |
if ((size = ftell (file)) < 0) { |
| 103 |
os_error(); |
| 104 |
} |
| 105 |
return size; |
| 106 |
} |
| 107 |
|
| 108 |
long file_size (FILE *file) |
| 109 |
{ |
| 110 |
long size; |
| 111 |
/* Seek to end */ |
| 112 |
seek_end (file); |
| 113 |
size = tell_position (file); |
| 114 |
rewind (file); |
| 115 |
return (size); |
| 116 |
} |
| 117 |
|
| 118 |
off_t xfile_size (FILE *file, const char *name) |
| 119 |
{ |
| 120 |
off_t size; |
| 121 |
xseek_end (file, name); |
| 122 |
size = xtell_position (file, name); |
| 123 |
rewind (file); |
| 124 |
return size; |
| 125 |
} |
| 126 |
|
| 127 |
/* Unlike fgets, mfgets works with \r, \n, or \r\n end of lines. */ |
| 128 |
char *mfgets (char *buffer, int length, FILE *file) |
| 129 |
{ |
| 130 |
int ch = 0, i = 0; |
| 131 |
while (i < length-1 && (ch = fgetc (file)) >= 0 && ch != '\n' && ch != '\r') |
| 132 |
buffer[i++] = ch; |
| 133 |
buffer[i] = 0; |
| 134 |
if (ch < 0 && i == 0) |
| 135 |
return NULL; |
| 136 |
if (ch == '\r' && (ch = fgetc (file)) >= 0 && (ch != '\n')) |
| 137 |
ungetc (ch, file); |
| 138 |
return buffer; |
| 139 |
} |
| 140 |
|
| 141 |
char work_buffer[WORK_BUFFER_SIZE]; |