51 #ifdef HAVE_SYS_TYPES_H
52 #include <sys/types.h>
55 #ifdef HAVE_SYS_STAT_H
59 #if defined(_WIN32) && !defined(CYGWIN)
81 guess_comptype(
char const *file, int32 *ispipe, int32 *isgz)
89 && ((strcmp(file + k - 2,
".Z") == 0)
90 || (strcmp(file + k - 2,
".z") == 0))) {
92 *isgz = COMP_COMPRESS;
94 else if ((k > 3) && ((strcmp(file + k - 3,
".gz") == 0)
95 || (strcmp(file + k - 3,
".GZ") == 0))) {
99 else if ((k > 4) && ((strcmp(file + k - 4,
".bz2") == 0)
100 || (strcmp(file + k - 4,
".BZ2") == 0))) {
107 fopen_comp(
const char *file,
const char *mode, int32 * ispipe)
115 guess_comptype(file, ispipe, &isgz);
121 E_FATAL(
"No popen() on WinCE\n");
123 if (strcmp(mode,
"r") == 0) {
127 command =
string_join(
"gunzip" EXEEXT,
" -c ", file, NULL);
130 command =
string_join(
"zcat" EXEEXT,
" ", file, NULL);
133 command =
string_join(
"bunzip2" EXEEXT,
" -c ", file, NULL);
137 E_FATAL(
"Unknown compression type %d\n", isgz);
139 if ((fp = popen(command, mode)) == NULL) {
140 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
146 else if (strcmp(mode,
"w") == 0) {
150 command =
string_join(
"gzip" EXEEXT,
" > ", file, NULL);
153 command =
string_join(
"compress" EXEEXT,
" -c > ", file, NULL);
156 command =
string_join(
"bzip2" EXEEXT,
" > ", file, NULL);
160 E_FATAL(
"Unknown compression type %d\n", isgz);
162 if ((fp = popen(command, mode)) == NULL) {
163 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
170 E_ERROR(
"Compressed file operation for mode %s is not supported\n", mode);
176 fp = fopen(file, mode);
188 #if defined(_WIN32) && (!defined(__SYMBIAN32__))
212 if ((fh =
fopen_comp(file,
"r", ispipe)) != NULL)
219 guess_comptype(file, ispipe, &isgz);
222 strcpy(tmpfile, file);
225 tmpfile[k - 3] =
'\0';
228 tmpfile[k - 4] =
'\0';
231 tmpfile[k - 2] =
'\0';
234 strcpy(tmpfile + k,
".gz");
235 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
236 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
240 strcpy(tmpfile + k,
".bz2");
241 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
242 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
246 strcpy(tmpfile + k,
".Z");
247 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
248 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
255 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
279 if (li && 0 == strncmp(li->buf,
"\xef\xbb\xbf", 3)) {
280 memmove(li->buf, li->buf + 3, strlen(li->buf + 1));
299 if (li->buf && li->buf[0] ==
'#') {
316 if (fgets(li->buf, li->bsiz, li->fh) == NULL) {
322 li->len = (int32)strlen(li->buf);
323 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
331 if (fgets(li->buf + li->len, li->bsiz - li->len, li->fh) == NULL) {
332 li->len += strlen(li->buf + li->len);
335 li->len += strlen(li->buf + li->len);
337 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
350 return lineiter_next_plain(li);
352 for (li = lineiter_next_plain(li); li; li = lineiter_next_plain(li)) {
355 if (li->buf[0] != 0 && li->buf[0] !=
'#')
379 char *output, *outptr;
382 output = outptr = NULL;
383 while (fgets(buf,
sizeof(buf), stream)) {
384 size_t len = strlen(buf);
386 if (output == NULL) {
391 size_t cur = outptr - output;
392 output = (
char *)
ckd_realloc(output, cur + len + 1);
393 outptr = output + cur;
395 memcpy(outptr, buf, len + 1);
398 if (len <
sizeof(buf)-1 || buf[len-1] ==
'\n')
401 if (out_len) *out_len = outptr - output;
405 #define FREAD_RETRY_COUNT 60
408 fread_retry(
void *pointer, int32 size, int32 num_items, FILE * stream)
416 n_retry_rem = FREAD_RETRY_COUNT;
418 data = (
char *)pointer;
420 n_items_rem = num_items;
423 n_items_read = fread(&data[loc], size, n_items_rem, stream);
425 n_items_rem -= n_items_read;
427 if (n_items_rem > 0) {
430 if (n_retry_rem == 0)
433 if (n_retry_rem == FREAD_RETRY_COUNT) {
439 loc += n_items_read * size;
440 #if !defined(_WIN32) && defined(HAVE_UNISTD_H)
444 }
while (n_items_rem > 0);
452 stat_retry(
const char *file,
struct stat * statbuf)
454 WIN32_FIND_DATAW file_data;
459 len = mbstowcs(NULL, file, 0) + 1;
461 mbstowcs(wfile, file, len);
462 if ((h = FindFirstFileW(wfile, &file_data)) == INVALID_HANDLE_VALUE) {
467 memset(statbuf, 0,
sizeof(*statbuf));
468 statbuf->st_mtime = file_data.ftLastWriteTime.dwLowDateTime;
469 statbuf->st_size = file_data.nFileSizeLow;
484 return ((int32) statbuf.st_mtime);
487 #define STAT_RETRY_COUNT 10
493 for (i = 0; i < STAT_RETRY_COUNT; i++) {
494 #ifndef HAVE_SYS_STAT_H
497 if ((fp = (FILE *)fopen(file,
"r")) != 0) {
498 fseek(fp, 0, SEEK_END);
499 statbuf->st_size = ftell(fp);
504 if (stat(file, statbuf) == 0)
523 #ifdef HAVE_SYS_STAT_H
524 if (stat(file, &statbuf) != 0)
531 return ((int32) statbuf.st_mtime);
537 unsigned char buf, bbits;
564 if (--be->refcount > 0)
576 tbits = nbits + be->bbits;
579 be->buf |= ((bits[0] >> (8 - nbits)) << (8 - tbits));
585 fputc(be->buf | ((bits[i]) >> be->bbits), be->fh);
587 be->buf = (bits[i] << (8 - be->bbits)) & 0xff;
601 unsigned char bits[4];
602 codeword <<= (32 - nbits);
603 bits[0] = (codeword >> 24) & 0xff;
604 bits[1] = (codeword >> 16) & 0xff;
605 bits[2] = (codeword >> 8) & 0xff;
606 bits[3] = codeword & 0xff;
614 fputc(be->buf, be->fh);
626 if (strlen(path) == 0)
629 #if defined(_WIN32) && !defined(CYGWIN)
630 else if ((rv = _mkdir(path)) == 0)
632 #elif defined(HAVE_SYS_STAT_H)
633 else if ((rv = mkdir(path, 0777)) == 0)
638 else if (errno == EEXIST)
640 else if (errno != ENOENT) {
650 #if defined(_WIN32) && !defined(CYGWIN)
652 #elif defined(HAVE_SYS_STAT_H)
653 return mkdir(path, 0777);