ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
file.c File Reference
#include "file.h"
#include "keatures.h"
#include "utilities.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for file.c:

Go to the source code of this file.

Macros

#define RETURN_TRUE_IF_COMPRESSED(SUFFIX, SIGNATURE)
 

Functions

ABC_NAMESPACE_IMPL_START bool kissat_file_exists (const char *path)
 
bool kissat_file_readable (const char *path)
 
bool kissat_file_writable (const char *path)
 
size_t kissat_file_size (const char *path)
 
bool kissat_find_executable (const char *name)
 
void kissat_read_already_open_file (file *file, FILE *f, const char *path)
 
void kissat_write_already_open_file (file *file, FILE *f, const char *path)
 
bool kissat_looks_like_a_compressed_file (const char *path)
 
bool kissat_open_to_read_file (file *file, const char *path)
 
bool kissat_open_to_write_file (file *file, const char *path)
 
void kissat_close_file (file *file)
 

Macro Definition Documentation

◆ RETURN_TRUE_IF_COMPRESSED

#define RETURN_TRUE_IF_COMPRESSED ( SUFFIX,
SIGNATURE )
Value:
if (kissat_has_suffix (path, SUFFIX) && \
match_signature (path, SIGNATURE)) \
return true
ABC_NAMESPACE_IMPL_START bool kissat_has_suffix(const char *str, const char *suffix)
Definition utilities.c:7

Function Documentation

◆ kissat_close_file()

void kissat_close_file ( file * file)

Definition at line 308 of file file.c.

308 {
311#ifdef KISSAT_HAS_COMPRESSION
312 if (file->close && file->compressed)
313 pclose (file->file);
314#else
316#endif
317 if (file->close && !file->compressed)
318 fclose (file->file);
319 file->file = 0;
320}
#define KISSAT_assert(ignore)
Definition global.h:13
Definition file.h:23
bool compressed
Definition file.h:27
FILE * file
Definition file.h:24
bool close
Definition file.h:25
int pclose()
Here is the call graph for this function:

◆ kissat_file_exists()

ABC_NAMESPACE_IMPL_START bool kissat_file_exists ( const char * path)

Definition at line 22 of file file.c.

22 {
23 if (!path)
24 return false;
25 struct stat buf;
26 if (stat (path, &buf))
27 return false;
28 return true;
29}

◆ kissat_file_readable()

bool kissat_file_readable ( const char * path)

Definition at line 31 of file file.c.

31 {
32 if (!path)
33 return false;
34 struct stat buf;
35 if (stat (path, &buf))
36 return false;
37 if (access (path, R_OK))
38 return false;
39 return true;
40}
Here is the caller graph for this function:

◆ kissat_file_size()

size_t kissat_file_size ( const char * path)

Definition at line 98 of file file.c.

98 {
99 struct stat buf;
100 if (stat (path, &buf))
101 return 0;
102 return (size_t) buf.st_size;
103}

◆ kissat_file_writable()

bool kissat_file_writable ( const char * path)

Definition at line 42 of file file.c.

42 {
43 int res;
44 if (!path)
45 res = 1;
46 else if (!strcmp (path, "/dev/null"))
47 res = 0;
48 else {
49 if (!*path)
50 res = 2;
51 else {
52 struct stat buf;
53 const char *p = strrchr (path, '/');
54 if (!p) {
55 if (stat (path, &buf)) {
56 if (errno == ENOENT)
57 res = 0;
58 else
59 res = -2;
60 } else if (S_ISDIR (buf.st_mode))
61 res = 3;
62 else if (access (path, W_OK))
63 res = 4;
64 else
65 res = 0;
66 } else if (!p[1])
67 res = 5;
68 else {
69 const size_t len = p - path;
70 char *dirname = (char*)malloc (len + 1);
71 if (dirname) {
72 strncpy (dirname, path, len);
73 dirname[len] = 0;
74 if (stat (dirname, &buf))
75 res = 6;
76 else if (!S_ISDIR (buf.st_mode))
77 res = 7;
78 else if (access (dirname, W_OK))
79 res = 8;
80 else if (stat (path, &buf)) {
81 if (errno == ENOENT)
82 res = 0;
83 else
84 res = -3;
85 } else if (access (path, W_OK))
86 res = 9;
87 else
88 res = 0;
89 free (dirname);
90 } else
91 res = 10;
92 }
93 }
94 }
95 return !res;
96}
Cube * p
Definition exorList.c:222
char * strrchr()
char * strncpy()
int strcmp()
VOID_HACK free()
char * malloc()
Here is the call graph for this function:

◆ kissat_find_executable()

bool kissat_find_executable ( const char * name)

Definition at line 105 of file file.c.

105 {
106 const size_t name_len = strlen (name);
107 const char *environment = getenv ("PATH");
108 if (!environment)
109 return false;
110 const size_t dirs_len = strlen (environment);
111 char *dirs = (char*)malloc (dirs_len + 1);
112 if (!dirs)
113 return false;
114 strcpy (dirs, environment);
115 bool res = false;
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++)
119 KISSAT_assert (q + 1 < end);
120 *q++ = 0;
121 const size_t path_len = (q - dir) + name_len;
122 char *path = (char*)malloc (path_len + 1);
123 if (!path) {
124 free (dirs);
125 return false;
126 }
127 sprintf (path, "%s/%s", dir, name);
128 KISSAT_assert (strlen (path) == path_len);
129 res = kissat_file_readable (path);
130 free (path);
131 }
132 free (dirs);
133 return res;
134}
bool kissat_file_readable(const char *path)
Definition file.c:31
char * name
Definition main.h:24
int strlen()
char * sprintf()
char * strcpy()
char * getenv()
Here is the call graph for this function:

◆ kissat_looks_like_a_compressed_file()

bool kissat_looks_like_a_compressed_file ( const char * path)

Definition at line 219 of file file.c.

219 {
220#define RETURN_TRUE_IF_COMPRESSED(SUFFIX, SIGNATURE) \
221 if (kissat_has_suffix (path, SUFFIX) && \
222 match_signature (path, SIGNATURE)) \
223 return true
224
225 RETURN_TRUE_IF_COMPRESSED (".bz2", bz2sig);
226 RETURN_TRUE_IF_COMPRESSED (".gz", gzsig);
227 RETURN_TRUE_IF_COMPRESSED (".lzma", lzmasig);
228 RETURN_TRUE_IF_COMPRESSED (".7z", sig7z);
229 RETURN_TRUE_IF_COMPRESSED (".xz", xzsig);
230 RETURN_TRUE_IF_COMPRESSED (".Z", Zsig);
231
232 return false;
233}
#define RETURN_TRUE_IF_COMPRESSED(SUFFIX, SIGNATURE)

◆ kissat_open_to_read_file()

bool kissat_open_to_read_file ( file * file,
const char * path )

Definition at line 237 of file file.c.

237 {
238#ifdef KISSAT_HAS_COMPRESSION
239#define READ_PIPE(SUFFIX, CMD, SIG) \
240 do { \
241 if (kissat_has_suffix (path, SUFFIX)) { \
242 file->file = read_pipe (CMD, SIG, path); \
243 if (!file->file) \
244 break; \
245 file->close = true; \
246 file->reading = true; \
247 file->compressed = true; \
248 file->path = path; \
249 file->bytes = 0; \
250 return true; \
251 } \
252 } while (0)
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);
259#endif
260 file->file = fopen (path, "r");
261 if (!file->file)
262 return false;
263 file->close = true;
264 file->reading = true;
265 file->compressed = false;
266 file->path = path;
267 file->bytes = 0;
268
269 return true;
270}
bool reading
Definition file.h:26
uint64_t bytes
Definition file.h:29
const char * path
Definition file.h:28
Here is the call graph for this function:

◆ kissat_open_to_write_file()

bool kissat_open_to_write_file ( file * file,
const char * path )

Definition at line 272 of file file.c.

272 {
273#if defined(KISSAT_HAS_COMPRESSION) && !defined(SAFE)
274#define WRITE_PIPE(SUFFIX, CMD) \
275 do { \
276 if (kissat_has_suffix (path, SUFFIX)) { \
277 if (SUFFIX[1] == '7' && kissat_file_readable (path) && \
278 unlink (path)) \
279 return false; \
280 file->file = write_pipe (CMD, path); \
281 if (!file->file) \
282 return false; \
283 file->close = true; \
284 file->reading = false; \
285 file->compressed = true; \
286 file->path = path; \
287 file->bytes = 0; \
288 return true; \
289 } \
290 } while (0)
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");
296#endif
297 file->file = fopen (path, "w");
298 if (!file->file)
299 return false;
300 file->close = true;
301 file->reading = false;
302 file->compressed = false;
303 file->path = path;
304 file->bytes = 0;
305 return true;
306}
Here is the call graph for this function:

◆ kissat_read_already_open_file()

void kissat_read_already_open_file ( file * file,
FILE * f,
const char * path )

Definition at line 198 of file file.c.

198 {
199 file->file = f;
200 file->close = false;
201 file->reading = true;
202 file->compressed = false;
203 file->path = path;
204 file->bytes = 0;
205}
Here is the call graph for this function:

◆ kissat_write_already_open_file()

void kissat_write_already_open_file ( file * file,
FILE * f,
const char * path )

Definition at line 207 of file file.c.

208 {
209 file->file = f;
210 file->close = false;
211 file->reading = false;
212 file->compressed = false;
213 file->path = path;
214 file->bytes = 0;
215}
Here is the call graph for this function: