| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#include <stdio.h> |
|---|
| 23 |
#include <errno.h> |
|---|
| 24 |
#include <err.h> |
|---|
| 25 |
#include <string.h> |
|---|
| 26 |
|
|---|
| 27 |
#include "libswish3.h" |
|---|
| 28 |
|
|---|
| 29 |
extern int SWISH_DEBUG; |
|---|
| 30 |
extern int errno; |
|---|
| 31 |
|
|---|
| 32 |
static void no_nulls( |
|---|
| 33 |
xmlChar *filename, |
|---|
| 34 |
xmlChar *buffer, |
|---|
| 35 |
int bytes_read |
|---|
| 36 |
); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
static void |
|---|
| 41 |
no_nulls( |
|---|
| 42 |
xmlChar *filename, |
|---|
| 43 |
xmlChar *buffer, |
|---|
| 44 |
int bytes_read |
|---|
| 45 |
) |
|---|
| 46 |
{ |
|---|
| 47 |
if (xmlStrlen(buffer) < bytes_read) { |
|---|
| 48 |
int i; |
|---|
| 49 |
int j = 0; |
|---|
| 50 |
int i_bytes_read = (int)bytes_read; |
|---|
| 51 |
|
|---|
| 52 |
for (i = 0; i < i_bytes_read; ++i) { |
|---|
| 53 |
if (buffer[i] == '\0') { |
|---|
| 54 |
buffer[i] = '\n'; |
|---|
| 55 |
j++; |
|---|
| 56 |
} |
|---|
| 57 |
if (buffer[i] == SWISH_TOKENPOS_BUMPER[0]) { |
|---|
| 58 |
buffer[i] = '\n'; |
|---|
| 59 |
j++; |
|---|
| 60 |
} |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
if (j) { |
|---|
| 64 |
SWISH_WARN |
|---|
| 65 |
("Substituted %d embedded null or connector character(s) in file '%s' with newline(s)", |
|---|
| 66 |
j, filename); |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
xmlChar * |
|---|
| 73 |
swish_slurp_fh( |
|---|
| 74 |
FILE * fh, |
|---|
| 75 |
long flen |
|---|
| 76 |
) |
|---|
| 77 |
{ |
|---|
| 78 |
|
|---|
| 79 |
size_t bytes_read; |
|---|
| 80 |
xmlChar *buffer; |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
buffer = swish_xmalloc(flen + 1); |
|---|
| 85 |
*buffer = '\0'; |
|---|
| 86 |
|
|---|
| 87 |
bytes_read = fread(buffer, sizeof(xmlChar), flen, fh); |
|---|
| 88 |
|
|---|
| 89 |
if (bytes_read != flen) { |
|---|
| 90 |
SWISH_CROAK("did not read expected bytes: %ld expected, %d read", flen, |
|---|
| 91 |
bytes_read); |
|---|
| 92 |
} |
|---|
| 93 |
buffer[bytes_read] = '\0'; |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
no_nulls((xmlChar *)"filehandle", buffer, (int)bytes_read); |
|---|
| 98 |
|
|---|
| 99 |
return buffer; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
xmlChar * |
|---|
| 103 |
swish_slurp_file_len( |
|---|
| 104 |
xmlChar *filename, |
|---|
| 105 |
long flen |
|---|
| 106 |
) |
|---|
| 107 |
{ |
|---|
| 108 |
size_t bytes_read; |
|---|
| 109 |
FILE *fp; |
|---|
| 110 |
xmlChar *buffer; |
|---|
| 111 |
|
|---|
| 112 |
if (flen > SWISH_MAX_FILE_LEN) { |
|---|
| 113 |
flen = SWISH_MAX_FILE_LEN; |
|---|
| 114 |
SWISH_WARN("max file len %ld exceeded - cannot read %ld bytes from %s", |
|---|
| 115 |
SWISH_MAX_FILE_LEN, flen, filename); |
|---|
| 116 |
|
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
buffer = swish_xmalloc(flen + 1); |
|---|
| 120 |
|
|---|
| 121 |
if ((fp = fopen((char *)filename, "r")) == 0) { |
|---|
| 122 |
SWISH_CROAK("Error reading file %s: %s", filename, strerror(errno)); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
bytes_read = fread(buffer, sizeof(xmlChar), flen, fp); |
|---|
| 126 |
|
|---|
| 127 |
if (bytes_read != flen) { |
|---|
| 128 |
SWISH_CROAK("did not read expected bytes: %ld expected, %d read (%s)", |
|---|
| 129 |
flen, bytes_read, strerror(errno)); |
|---|
| 130 |
} |
|---|
| 131 |
buffer[bytes_read] = '\0'; |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
if (fclose(fp)) |
|---|
| 135 |
SWISH_CROAK("error closing filehandle for %s: %s", filename, |
|---|
| 136 |
strerror(errno)); |
|---|
| 137 |
|
|---|
| 138 |
no_nulls(filename, buffer, (int)bytes_read); |
|---|
| 139 |
|
|---|
| 140 |
return buffer; |
|---|
| 141 |
|
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
xmlChar * |
|---|
| 145 |
swish_slurp_file( |
|---|
| 146 |
xmlChar *filename |
|---|
| 147 |
) |
|---|
| 148 |
{ |
|---|
| 149 |
struct stat info; |
|---|
| 150 |
|
|---|
| 151 |
if (stat((char *)filename, &info)) { |
|---|
| 152 |
SWISH_CROAK("Can't stat %s: %s\n", filename, strerror(errno)); |
|---|
| 153 |
} |
|---|
| 154 |
return swish_slurp_file_len(filename, info.st_size); |
|---|
| 155 |
|
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
boolean |
|---|
| 159 |
swish_file_exists( |
|---|
| 160 |
xmlChar *filename |
|---|
| 161 |
) |
|---|
| 162 |
{ |
|---|
| 163 |
struct stat info; |
|---|
| 164 |
if (stat((char *)filename, &info)) { |
|---|
| 165 |
return 0; |
|---|
| 166 |
} |
|---|
| 167 |
return 1; |
|---|
| 168 |
} |
|---|