diff options
author | Lev Walkin <vlm@lionet.info> | 2005-03-04 08:52:50 +0000 |
---|---|---|
committer | Lev Walkin <vlm@lionet.info> | 2005-03-04 08:52:50 +0000 |
commit | 4604d03d985731fb0a42e62c0e5b0b626f4d6c64 (patch) | |
tree | ccce01adbeabeedf2ed3e7cc235c9c5ff9590e90 /libasn1compiler/asn1c_compat.c | |
parent | 21d00009376ee1e675301eeecea88b1ba2c91f18 (diff) |
retaining old file if contents are the same
Diffstat (limited to 'libasn1compiler/asn1c_compat.c')
-rw-r--r-- | libasn1compiler/asn1c_compat.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/libasn1compiler/asn1c_compat.c b/libasn1compiler/asn1c_compat.c index ebc6d3f0..2a80251a 100644 --- a/libasn1compiler/asn1c_compat.c +++ b/libasn1compiler/asn1c_compat.c @@ -14,7 +14,7 @@ #endif FILE * -asn1c_open_file(const char *name, const char *ext) { +asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) { int created = 1; #ifndef WIN32 struct stat sb; @@ -22,22 +22,35 @@ asn1c_open_file(const char *name, const char *ext) { char *fname; size_t len; FILE *fp; + int ret; int fd; /* * Compute filenames. */ - len = strlen(name) + strlen(ext) + 1; + len = strlen(name) + strlen(ext) + sizeof(".XXXXXX"); fname = alloca(len); - snprintf(fname, len, "%s%s", name, ext); - - /* - * Create files. - */ - fd = open(fname, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE); - if(fd == -1 && errno == EEXIST) { - fd = open(fname, O_WRONLY, DEFFILEMODE); - created = 0; + ret = snprintf(fname, len, "%s%s%s", name, ext, + opt_tmpname ? ".XXXXXX" : ""); + assert(ret > 0 && ret < len); + + if(opt_tmpname) { + /* + * Create temporary file. + */ + fd = mkstemp(fname); +#ifndef WIN32 + (void)fchmod(fd, DEFFILEMODE); +#endif + } else { + /* + * Create specified file, or open the old one. + */ + fd = open(fname, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE); + if(fd == -1 && errno == EEXIST) { + fd = open(fname, O_WRONLY, DEFFILEMODE); + created = 0; + } } if(fd == -1) { perror(fname); @@ -68,6 +81,13 @@ asn1c_open_file(const char *name, const char *ext) { if(created) unlink(fname); close(fd); } + + /* Return the temporary file name */ + if(opt_tmpname) { + *opt_tmpname = strdup(fname); + assert(*opt_tmpname); + } + return fp; } |