X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Ffile-name.c;h=f8ad11d07a342f22fd5bf4cfa026d9a93a412ce0;hb=e1fb96f07a06f3133f54702ed8706493989789fe;hp=23d7ee0889f2c75460e8d479eb177b7acc2f75d9;hpb=458d169f64134f4e0a9d9b72398666a01761fcf8;p=pspp-builds.git diff --git a/src/data/file-name.c b/src/data/file-name.c index 23d7ee08..f8ad11d0 100644 --- a/src/data/file-name.c +++ b/src/data/file-name.c @@ -20,8 +20,10 @@ #include #include +#include #include #include +#include #include "intprops.h" #include "minmax.h" @@ -252,7 +254,7 @@ safety_violation (const char *fn) FILE * fn_open (const char *fn, const char *mode) { - assert (mode[0] == 'r' || mode[0] == 'w'); + assert (mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a'); if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-"))) return stdin; @@ -267,7 +269,7 @@ fn_open (const char *fn, const char *mode) if (get_safer_mode ()) return safety_violation (fn); - return popen (&fn[1], mode); + return popen (&fn[1], mode[0] == 'r' ? "r" : "w"); } else if (*fn && fn[strlen (fn) - 1] == '|') { @@ -281,7 +283,7 @@ fn_open (const char *fn, const char *mode) memcpy (s, fn, strlen (fn) - 1); s[strlen (fn) - 1] = 0; - f = popen (s, mode); + f = popen (s, mode[0] == 'r' ? "r" : "w"); local_free (s); @@ -320,6 +322,30 @@ fn_close (const char *fn, FILE *f) return fclose (f); } +/* Creates a new file named FN with the given PERMISSIONS bits, + and returns a stream for it or a null pointer on failure. + MODE should be "w" or "wb". */ +FILE * +create_stream (const char *fn, const char *mode, mode_t permissions) +{ + int fd; + FILE *stream; + + fd = open (fn, O_WRONLY | O_CREAT | O_TRUNC, permissions); + if (fd < 0) + return NULL; + + stream = fdopen (fd, mode); + if (stream == NULL) + { + int save_errno = errno; + close (fd); + errno = save_errno; + } + + return stream; +} + #if !(defined _WIN32 || defined __WIN32__) /* A file's identity. */ struct file_identity