26 if (stat (path, &buf))
35 if (stat (path, &buf))
37 if (access (path, R_OK))
46 else if (!
strcmp (path,
"/dev/null"))
55 if (stat (path, &buf)) {
60 }
else if (S_ISDIR (buf.st_mode))
62 else if (access (path, W_OK))
69 const size_t len =
p - path;
70 char *dirname = (
char*)
malloc (len + 1);
74 if (stat (dirname, &buf))
76 else if (!S_ISDIR (buf.st_mode))
78 else if (access (dirname, W_OK))
80 else if (stat (path, &buf)) {
85 }
else if (access (path, W_OK))
100 if (stat (path, &buf))
102 return (
size_t) buf.st_size;
107 const char *environment =
getenv (
"PATH");
110 const size_t dirs_len =
strlen (environment);
111 char *dirs = (
char*)
malloc (dirs_len + 1);
114 strcpy (dirs, environment);
116 const char *end = dirs + dirs_len + 1;
117 for (
char *dir = dirs, *q; !res && dir != end; dir = q) {
118 for (q = dir; *q && *q !=
':'; q++)
121 const size_t path_len = (q - dir) + name_len;
122 char *path = (
char*)
malloc (path_len + 1);
136static int bz2sig[] = {0x42, 0x5A, 0x68, EOF};
137static int gzsig[] = {0x1F, 0x8B, EOF};
138static int lzmasig[] = {0x5D, 0x00, 0x00, 0x80, 0x00, EOF};
139static int sig7z[] = {0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, EOF};
140static int xzsig[] = {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00, 0x00, EOF};
141static int Zsig[] = {0x1F, 0x9D, 0x90, EOF};
143static bool match_signature (
const char *path,
const int *sig) {
145 FILE *tmp = fopen (path,
"r");
149 for (
const int *
p = sig; res && (*
p != EOF);
p++)
150 res = (getc (tmp) == *
p);
155#ifdef KISSAT_HAS_COMPRESSION
157static FILE *open_pipe (
const char *fmt,
const char *path,
160 while (fmt[name_len] && fmt[name_len] !=
' ')
180static FILE *read_pipe (
const char *fmt,
const int *sig,
const char *path) {
183 if (sig && !match_signature (path, sig))
185 return open_pipe (fmt, path,
"r");
190static FILE *write_pipe (
const char *fmt,
const char *path) {
191 return open_pipe (fmt, path,
"w");
217#ifndef KISSAT_HAS_COMPRESSION
220#define RETURN_TRUE_IF_COMPRESSED(SUFFIX, SIGNATURE) \
221 if (kissat_has_suffix (path, SUFFIX) && \
222 match_signature (path, SIGNATURE)) \
238#ifdef KISSAT_HAS_COMPRESSION
239#define READ_PIPE(SUFFIX, CMD, SIG) \
241 if (kissat_has_suffix (path, SUFFIX)) { \
242 file->file = read_pipe (CMD, SIG, path); \
245 file->close = true; \
246 file->reading = true; \
247 file->compressed = true; \
253 READ_PIPE (
".bz2",
"bzip2 -c -d %s", bz2sig);
254 READ_PIPE (
".gz",
"gzip -c -d %s", gzsig);
255 READ_PIPE (
".lzma",
"lzma -c -d %s", lzmasig);
256 READ_PIPE (
".7z",
"7z x -so %s 2>/dev/null", sig7z);
257 READ_PIPE (
".xz",
"xz -c -d %s", xzsig);
258 READ_PIPE (
".Z",
"gzip -c -d %s", Zsig);
273#if defined(KISSAT_HAS_COMPRESSION) && !defined(SAFE)
274#define WRITE_PIPE(SUFFIX, CMD) \
276 if (kissat_has_suffix (path, SUFFIX)) { \
277 if (SUFFIX[1] == '7' && kissat_file_readable (path) && \
280 file->file = write_pipe (CMD, path); \
283 file->close = true; \
284 file->reading = false; \
285 file->compressed = true; \
291 WRITE_PIPE (
".bz2",
"bzip2 -c > %s");
292 WRITE_PIPE (
".gz",
"gzip -c > %s");
293 WRITE_PIPE (
".lzma",
"lzma -c > %s");
294 WRITE_PIPE (
".7z",
"7z a -si %s 2>/dev/null");
295 WRITE_PIPE (
".xz",
"xz -c > %s");
311#ifdef KISSAT_HAS_COMPRESSION
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
ABC_NAMESPACE_IMPL_START bool kissat_file_exists(const char *path)
void kissat_write_already_open_file(file *file, FILE *f, const char *path)
void kissat_close_file(file *file)
#define RETURN_TRUE_IF_COMPRESSED(SUFFIX, SIGNATURE)
bool kissat_open_to_read_file(file *file, const char *path)
size_t kissat_file_size(const char *path)
void kissat_read_already_open_file(file *file, FILE *f, const char *path)
bool kissat_open_to_write_file(file *file, const char *path)
bool kissat_file_readable(const char *path)
bool kissat_file_writable(const char *path)
bool kissat_find_executable(const char *name)
bool kissat_looks_like_a_compressed_file(const char *path)
#define KISSAT_assert(ignore)