Whamcloud - gitweb
debian: mark logsave as Multi-Arch: foreign
[tools/e2fsprogs.git] / util / subst.c
index db72923..66d7d9a 100644 (file)
@@ -1,13 +1,14 @@
 /*
  * subst.c --- substitution program
  *
- * Subst is used as a quicky program to do @ substitutions
+ * Subst is used as a quickie program to do @ substitutions
  *
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #else
+#define HAVE_SYS_STAT_H
 #define HAVE_SYS_TIME_H
 #endif
 #include <stdio.h>
@@ -28,6 +29,9 @@
 #include <fcntl.h>
 #include <time.h>
 #include <utime.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
 
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
@@ -320,7 +324,7 @@ int main(int argc, char **argv)
 {
        char    line[2048];
        int     c;
-       int     fd;
+       int     fd, ofd = -1;
        FILE    *in, *out, *old = NULL;
        char    *outfn = NULL, *newfn = NULL;
        int     verbose = 0;
@@ -371,12 +375,12 @@ int main(int argc, char **argv)
                }
                strcpy(newfn, outfn);
                strcat(newfn, ".new");
-               fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
-               if (fd < 0) {
+               ofd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0644);
+               if (ofd < 0) {
                        perror(newfn);
                        exit(1);
                }
-               out = fdopen(fd, "w+");
+               out = fdopen(ofd, "w+");
                if (!out) {
                        perror("fdopen");
                        exit(1);
@@ -430,12 +434,16 @@ int main(int argc, char **argv)
                                        printf("Using original atime\n");
                                set_utimes(outfn, fileno(old), tv);
                        }
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
                        fclose(out);
                        if (unlink(newfn) < 0)
                                perror("unlink");
                } else {
                        if (verbose)
                                printf("Creating or replacing %s.\n", outfn);
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
                        fclose(out);
                        if (old)
                                fclose(old);
@@ -448,6 +456,8 @@ int main(int argc, char **argv)
        }
        if (old)
                fclose(old);
+       if (newfn)
+               free(newfn);
        return (0);
 }