int main (int argc, char **argv) { const char *asnfile = NULL; ksba_asn_tree_t asn_tree = NULL; int rc; if (!argc || (argc > 1 && (!strcmp (argv[1],"--help") || !strcmp (argv[1],"-h"))) ) usage (0); argc--; argv++; if (argc && !strcmp (*argv,"--module")) { argc--; argv++; if (!argc) usage (1); asnfile = *argv; argc--; argv++; } if (asnfile) { rc = ksba_asn_parse_file (asnfile, &asn_tree, 0); if (rc) { print_error ("parsing `%s' failed: rc=%d\n", asnfile, rc); exit (1); } } if (!argc) one_file (stdin, "-", asn_tree); else { for (; argc; argc--, argv++) { FILE *fp; fp = fopen (*argv, "r"); if (!fp) print_error ("can't open `%s': %s\n", *argv, strerror (errno)); else { one_file (fp, *argv, asn_tree); fclose (fp); } } } ksba_asn_tree_release (asn_tree); return error_counter? 1:0; }
int rename2 (const char *oldname, const char *newname) { int retval; char dir1[FILENAME_MAX], dir2[FILENAME_MAX]; struct stat fstate; dirname2 (oldname, dir1); dirname2 (newname, dir2); // We should use dirname{2}() in case oldname or newname doesn't exist yet if (one_filesystem (dir1, dir2)) { if (access (newname, F_OK) == 0 && !one_file (oldname, newname)) { stat (newname, &fstate); chmod (newname, fstate.st_mode | S_IWUSR); remove (newname); // *try* to remove or rename() will fail } retval = rename (oldname, newname); } else { retval = fcopy_raw (oldname, newname); // don't remove unless the file can be copied if (retval == 0) { stat (oldname, &fstate); chmod (oldname, fstate.st_mode | S_IWUSR); remove (oldname); } } return retval; }
int fcopy_raw (const char *src, const char *dest) // Raw file copy function. Raw, because it will copy the file data as it is, // unlike fcopy(). Don't merge fcopy_raw() with fcopy(). They have both their // uses. { #ifdef USE_ZLIB #undef fopen #undef fread #undef fwrite #undef fclose #endif FILE *fh, *fh2; int seg_len; char buf[MAXBUFSIZE]; if (one_file (dest, src)) return -1; if (!(fh = fopen (src, "rb"))) return -1; if (!(fh2 = fopen (dest, "wb"))) { fclose (fh); return -1; } while ((seg_len = fread (buf, 1, MAXBUFSIZE, fh))) fwrite (buf, 1, seg_len, fh2); fclose (fh); fclose (fh2); return 0; #ifdef USE_ZLIB #define fopen fopen2 #define fread fread2 #define fwrite fwrite2 #define fclose fclose2 #endif }
int fcopy (const char *src, size_t start, size_t len, const char *dest, const char *mode) { FILE *output; int result = 0; if (one_file (dest, src)) // other code depends on this return -1; // behaviour! if (!(output = fopen (dest, mode))) { errno = ENOENT; return -1; } fseek (output, 0, SEEK_END); result = quick_io_func (fcopy_func, MAXBUFSIZE, output, start, len, src, "rb"); fclose (output); sync (); return result == -1 ? result : 0; }