int main(int argc, char **argv) { char line[2048]; int c; FILE *in, *out; char *outfn = NULL, *newfn = NULL; int verbose = 0; while ((c = getopt (argc, argv, "f:v")) != EOF) { switch (c) { case 'f': in = fopen(optarg, "r"); if (!in) { perror(optarg); exit(1); } parse_config_file(in); fclose(in); break; case 'v': verbose++; break; default: fprintf(stderr, "%s: [-f config-file] [file]\n", argv[0]); break; } } if (optind < argc) { in = fopen(argv[optind], "r"); if (!in) { perror(argv[optind]); exit(1); } optind++; } else in = stdin; if (optind < argc) { outfn = argv[optind]; newfn = (char *) malloc(strlen(outfn)+20); if (!newfn) { fprintf(stderr, "Memory error! Exiting.\n"); exit(1); } strcpy(newfn, outfn); strcat(newfn, ".new"); out = fopen(newfn, "w"); if (!out) { perror(newfn); exit(1); } } else { out = stdout; outfn = 0; } while (!feof(in)) { if (fgets(line, sizeof(line), in) == NULL) break; substitute_line(line); fputs(line, out); } fclose(in); fclose(out); if (outfn) { if (compare_file(outfn, newfn)) { if (verbose) printf("No change, keeping %s.\n", outfn); unlink(newfn); } else { if (verbose) printf("Creating or replacing %s.\n", outfn); rename(newfn, outfn); } } return (0); }
int main(int argc, char **argv) { char line[2048]; int c; FILE *in, *out; char *outfn = NULL, *newfn = NULL; int verbose = 0; int adjust_timestamp = 0; struct stat stbuf; struct utimbuf ut; while ((c = getopt (argc, argv, "f:tv")) != EOF) { switch (c) { case 'f': in = fopen(optarg, "r"); if (!in) { perror(optarg); exit(1); } parse_config_file(in); fclose(in); break; case 't': adjust_timestamp++; break; case 'v': verbose++; break; default: fprintf(stderr, "%s: [-f config-file] [file]\n", argv[0]); break; } } if (optind < argc) { in = fopen(argv[optind], "r"); if (!in) { perror(argv[optind]); exit(1); } optind++; } else in = stdin; if (optind < argc) { outfn = argv[optind]; newfn = (char *) malloc(strlen(outfn)+20); if (!newfn) { fprintf(stderr, "Memory error! Exiting.\n"); exit(1); } strcpy(newfn, outfn); strcat(newfn, ".new"); out = fopen(newfn, "w"); if (!out) { perror(newfn); exit(1); } } else { out = stdout; outfn = 0; } while (!feof(in)) { if (fgets(line, sizeof(line), in) == NULL) break; substitute_line(line); fputs(line, out); } fclose(in); fclose(out); if (outfn) { struct stat st; if (compare_file(outfn, newfn)) { if (verbose) printf("No change, keeping %s.\n", outfn); if (adjust_timestamp) { if (stat(outfn, &stbuf) == 0) { if (verbose) printf("Updating modtime for %s\n", outfn); ut.actime = stbuf.st_atime; ut.modtime = time(0); if (utime(outfn, &ut) < 0) perror("utime"); } } unlink(newfn); } else { if (verbose) printf("Creating or replacing %s.\n", outfn); rename(newfn, outfn); } /* set read-only to alert user it is a generated file */ if (stat(outfn, &st) == 0) chmod(outfn, st.st_mode & ~0222); } return (0); }
int main(int argc, char **argv) { char line[2048]; int c; int fd; FILE *in, *out, *old = NULL; char *outfn = NULL, *newfn = NULL; int verbose = 0; int adjust_timestamp = 0; int got_atime = 0; struct stat stbuf; struct timeval tv[2]; while ((c = getopt (argc, argv, "f:tv")) != EOF) { switch (c) { case 'f': in = fopen(optarg, "r"); if (!in) { perror(optarg); exit(1); } parse_config_file(in); fclose(in); break; case 't': adjust_timestamp++; break; case 'v': verbose++; break; default: fprintf(stderr, "%s: [-f config-file] [file]\n", argv[0]); break; } } if (optind < argc) { in = fopen(argv[optind], "r"); if (!in) { perror(argv[optind]); exit(1); } optind++; } else in = stdin; if (optind < argc) { outfn = argv[optind]; newfn = (char *) malloc(strlen(outfn)+20); if (!newfn) { fprintf(stderr, "Memory error! Exiting.\n"); exit(1); } strcpy(newfn, outfn); strcat(newfn, ".new"); fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444); if (fd < 0) { perror(newfn); exit(1); } out = fdopen(fd, "w+"); if (!out) { perror("fdopen"); exit(1); } fd = open(outfn, O_RDONLY); if (fd > 0) { /* save the original atime, if possible */ if (fstat(fd, &stbuf) == 0) { #if HAVE_STRUCT_STAT_ST_ATIM tv[0].tv_sec = stbuf.st_atim.tv_sec; tv[0].tv_usec = stbuf.st_atim.tv_nsec / 1000; #else tv[0].tv_sec = stbuf.st_atime; tv[0].tv_usec = 0; #endif got_atime = 1; } old = fdopen(fd, "r"); if (!old) close(fd); } } else { out = stdout; outfn = 0; } while (!feof(in)) { if (fgets(line, sizeof(line), in) == NULL) break; substitute_line(line); fputs(line, out); } fclose(in); if (outfn) { fflush(out); rewind(out); if (old && compare_file(old, out)) { if (verbose) printf("No change, keeping %s.\n", outfn); if (adjust_timestamp) { if (verbose) printf("Updating modtime for %s\n", outfn); if (gettimeofday(&tv[1], NULL) < 0) { perror("gettimeofday"); exit(1); } if (got_atime == 0) tv[0] = tv[1]; else if (verbose) printf("Using original atime\n"); #ifdef HAVE_FUTIMES if (futimes(fileno(old), tv) < 0) perror("futimes"); #else if (utimes(outfn, tv) < 0) perror("utimes"); #endif } fclose(out); if (unlink(newfn) < 0) perror("unlink"); } else { if (verbose) printf("Creating or replacing %s.\n", outfn); fclose(out); if (old) fclose(old); old = NULL; if (rename(newfn, outfn) < 0) { perror("rename"); exit(1); } } } if (old) fclose(old); return (0); }