46 #ifdef HAVE_SYS_STAT_H
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
72 guess_comptype(
char const *file, int32 *ispipe, int32 *isgz)
80 && ((strcmp(file + k - 2,
".Z") == 0)
81 || (strcmp(file + k - 2,
".z") == 0))) {
83 *isgz = COMP_COMPRESS;
85 else if ((k > 3) && ((strcmp(file + k - 3,
".gz") == 0)
86 || (strcmp(file + k - 3,
".GZ") == 0))) {
90 else if ((k > 4) && ((strcmp(file + k - 4,
".bz2") == 0)
91 || (strcmp(file + k - 4,
".BZ2") == 0))) {
98 fopen_comp(
const char *file,
const char *mode, int32 * ispipe)
106 guess_comptype(file, ispipe, &isgz);
112 E_FATAL(
"No popen() on WinCE\n");
114 if (strcmp(mode,
"r") == 0) {
118 command =
string_join(
"gunzip" EXEEXT,
" -c ", file, NULL);
121 command =
string_join(
"zcat" EXEEXT,
" ", file, NULL);
124 command =
string_join(
"bunzip2" EXEEXT,
" -c ", file, NULL);
128 E_FATAL(
"Unknown compression type %d\n", isgz);
130 if ((fp = popen(command, mode)) == NULL) {
137 else if (strcmp(mode,
"w") == 0) {
141 command =
string_join(
"gzip" EXEEXT,
" > ", file, NULL);
144 command =
string_join(
"compress" EXEEXT,
" -c > ", file, NULL);
147 command =
string_join(
"bzip2" EXEEXT,
" > ", file, NULL);
151 E_FATAL(
"Unknown compression type %d\n", isgz);
153 if ((fp = popen(command, mode)) == NULL) {
161 E_ERROR(
"fopen_comp not implemented for mode = %s\n", mode);
167 fp = fopen(file, mode);
179 #if defined(_WIN32) && (!defined(__SYMBIAN32__))
203 if ((fh =
fopen_comp(file,
"r", ispipe)) != NULL)
210 guess_comptype(file, ispipe, &isgz);
213 strcpy(tmpfile, file);
216 tmpfile[k - 3] =
'\0';
219 tmpfile[k - 4] =
'\0';
222 tmpfile[k - 2] =
'\0';
225 strcpy(tmpfile + k,
".gz");
226 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
227 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
231 strcpy(tmpfile + k,
".bz2");
232 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
233 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
237 strcpy(tmpfile + k,
".Z");
238 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
239 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
246 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
273 if (fgets(li->buf, li->bsiz, li->fh) == NULL) {
279 li->len = strlen(li->buf);
280 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
288 if (fgets(li->buf + li->len, li->bsiz - li->len, li->fh) == NULL) {
289 li->len += strlen(li->buf + li->len);
292 li->len += strlen(li->buf + li->len);
294 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
314 char *output, *outptr;
317 output = outptr = NULL;
318 while (fgets(buf,
sizeof(buf), stream)) {
319 size_t len = strlen(buf);
321 if (output == NULL) {
326 size_t cur = outptr - output;
328 outptr = output + cur;
330 memcpy(outptr, buf, len + 1);
333 if (len <
sizeof(buf)-1 || buf[len-1] ==
'\n')
336 if (out_len) *out_len = outptr - output;
341 #define FREAD_RETRY_COUNT 60
344 fread_retry(
void *pointer, int32 size, int32 num_items, FILE * stream)
352 n_retry_rem = FREAD_RETRY_COUNT;
356 n_items_rem = num_items;
359 n_items_read = fread(&data[loc], size, n_items_rem, stream);
361 n_items_rem -= n_items_read;
363 if (n_items_rem > 0) {
366 if (n_retry_rem == 0)
369 if (n_retry_rem == FREAD_RETRY_COUNT) {
375 loc += n_items_read * size;
380 }
while (n_items_rem > 0);
389 stat_retry(
const char *file,
struct stat * statbuf)
391 WIN32_FIND_DATAW file_data;
396 len = mbstowcs(NULL, file, 0) + 1;
398 mbstowcs(wfile, file, len);
399 if ((h = FindFirstFileW(wfile, &file_data)) == INVALID_HANDLE_VALUE) {
404 memset(statbuf, 0,
sizeof(statbuf));
405 statbuf->st_mtime = file_data.ftLastWriteTime.dwLowDateTime;
406 statbuf->st_size = file_data.nFileSizeLow;
421 return ((int32) statbuf.st_mtime);
424 #define STAT_RETRY_COUNT 10
432 for (i = 0; i < STAT_RETRY_COUNT; i++) {
434 #ifndef HAVE_SYS_STAT_H
437 if ((fp=(FILE *)fopen(file,
"r"))!= 0)
439 fseek( fp, 0, SEEK_END);
440 statbuf->st_size = ftell( fp );
446 if (stat(file, statbuf) == 0)
465 #ifdef HAVE_SYS_STAT_H
466 if (stat(file, &statbuf) != 0)
473 return ((int32) statbuf.st_mtime);
479 unsigned char buf, bbits;
506 if (--be->refcount > 0)
518 tbits = nbits + be->bbits;
521 be->buf |= ((bits[0] >> (8 - nbits)) << (8 - tbits));
527 fputc(be->buf | ((bits[i]) >> be->bbits), be->fh);
529 be->buf = (bits[i] << (8 - be->bbits)) & 0xff;
543 unsigned char bits[4];
544 codeword <<= (32 - nbits);
545 bits[0] = (codeword >> 24) & 0xff;
546 bits[1] = (codeword >> 16) & 0xff;
547 bits[2] = (codeword >> 8) & 0xff;
548 bits[3] = codeword & 0xff;
556 fputc(be->buf, be->fh);
562 #ifdef HAVE_SYS_STAT_H
569 if (strlen(path) == 0)
572 else if ((rv = mkdir(path, 0777)) == 0)
575 else if (errno == EEXIST)
577 else if (errno != ENOENT) {
586 return mkdir(path, 0777);
589 #elif defined(_WIN32)
594 E_ERROR(
"build_directory() unimplemented on your platform!\n");
601 E_ERROR(
"build_directory() unimplemented on your platform!\n");