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) {
131 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
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) {
154 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
161 E_ERROR(
"Compressed file operation for mode %s is not supported", 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);
270 if (li && 0 == strncmp(li->buf,
"\xef\xbb\xbf", 3)) {
271 memmove(li->buf, li->buf + 3, strlen(li->buf + 1));
290 if (li->buf && li->buf[0] ==
'#') {
307 if (fgets(li->buf, li->bsiz, li->fh) == NULL) {
313 li->len = strlen(li->buf);
314 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
322 if (fgets(li->buf + li->len, li->bsiz - li->len, li->fh) == NULL) {
323 li->len += strlen(li->buf + li->len);
326 li->len += strlen(li->buf + li->len);
328 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
341 return lineiter_next_plain(li);
343 for (li = lineiter_next_plain(li); li; li = lineiter_next_plain(li)) {
344 if (li->buf && li->buf[0] !=
'#') {
369 char *output, *outptr;
372 output = outptr = NULL;
373 while (fgets(buf,
sizeof(buf), stream)) {
374 size_t len = strlen(buf);
376 if (output == NULL) {
381 size_t cur = outptr - output;
383 outptr = output + cur;
385 memcpy(outptr, buf, len + 1);
388 if (len <
sizeof(buf)-1 || buf[len-1] ==
'\n')
391 if (out_len) *out_len = outptr - output;
395 #define FREAD_RETRY_COUNT 60
398 fread_retry(
void *pointer, int32 size, int32 num_items, FILE * stream)
406 n_retry_rem = FREAD_RETRY_COUNT;
410 n_items_rem = num_items;
413 n_items_read = fread(&data[loc], size, n_items_rem, stream);
415 n_items_rem -= n_items_read;
417 if (n_items_rem > 0) {
420 if (n_retry_rem == 0)
423 if (n_retry_rem == FREAD_RETRY_COUNT) {
429 loc += n_items_read * size;
434 }
while (n_items_rem > 0);
443 stat_retry(
const char *file,
struct stat * statbuf)
445 WIN32_FIND_DATAW file_data;
450 len = mbstowcs(NULL, file, 0) + 1;
452 mbstowcs(wfile, file, len);
453 if ((h = FindFirstFileW(wfile, &file_data)) == INVALID_HANDLE_VALUE) {
458 memset(statbuf, 0,
sizeof(statbuf));
459 statbuf->st_mtime = file_data.ftLastWriteTime.dwLowDateTime;
460 statbuf->st_size = file_data.nFileSizeLow;
475 return statbuf.st_mtime;
478 #define STAT_RETRY_COUNT 10
486 for (i = 0; i < STAT_RETRY_COUNT; i++) {
488 #ifndef HAVE_SYS_STAT_H
491 if ((fp=(FILE *)fopen(file,
"r"))!= 0)
493 FSEEK( fp, 0, SEEK_END);
494 statbuf->st_size = FTELL( fp );
500 if (stat(file, statbuf) == 0)
519 #ifdef HAVE_SYS_STAT_H
520 if (stat(file, &statbuf) != 0)
527 return statbuf.st_mtime;
533 unsigned char buf, bbits;
560 if (--be->refcount > 0)
572 tbits = nbits + be->bbits;
575 be->buf |= ((bits[0] >> (8 - nbits)) << (8 - tbits));
581 fputc(be->buf | ((bits[i]) >> be->bbits), be->fh);
583 be->buf = (bits[i] << (8 - be->bbits)) & 0xff;
597 unsigned char bits[4];
598 codeword <<= (32 - nbits);
599 bits[0] = (codeword >> 24) & 0xff;
600 bits[1] = (codeword >> 16) & 0xff;
601 bits[2] = (codeword >> 8) & 0xff;
602 bits[3] = codeword & 0xff;
610 fputc(be->buf, be->fh);
616 #if defined(HAVE_SYS_STAT_H) && !defined(__MINGW32__)
623 if (strlen(path) == 0)
626 else if ((rv = mkdir(path, 0777)) == 0)
629 else if (errno == EEXIST)
631 else if (errno != ENOENT) {
640 return mkdir(path, 0777);
643 #elif defined(_WIN32)
648 E_ERROR(
"build_directory() unimplemented on your platform!\n");
655 E_ERROR(
"build_directory() unimplemented on your platform!\n");