Whamcloud - gitweb
debian: add support for DEB_BUILD_OPTIONS=parallel=N
[tools/e2fsprogs.git] / util / subst.c
index 36eaa94..be2a0dd 100644 (file)
@@ -1,12 +1,15 @@
 /*
  * 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>
 #include <errno.h>
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
 #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>
@@ -291,13 +301,30 @@ static int compare_file(FILE *old_f, FILE *new_f)
        return retval;
 }
 
+void set_utimes(const char *filename, int fd, const struct timeval times[2])
+{
+#ifdef HAVE_FUTIMES
+       if (futimes(fd, times) < 0)
+               perror("futimes");
+#elif HAVE_UTIMES
+       if (utimes(filename, times) < 0)
+               perror("utimes");
+#else
+       struct utimbuf ut;
+
+       ut.actime = times[0].tv_sec;
+       ut.modtime = times[1].tv_sec;
+       if (utime(filename, &ut) < 0)
+               perror("utime");
+#endif
+}
 
 
 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;
@@ -348,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);
@@ -405,20 +432,22 @@ int main(int argc, char **argv)
                                        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
+                               set_utimes(outfn, fileno(old), tv);
                        }
+#ifndef _WIN32
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
+#endif
                        fclose(out);
                        if (unlink(newfn) < 0)
                                perror("unlink");
                } else {
                        if (verbose)
                                printf("Creating or replacing %s.\n", outfn);
+#ifndef _WIN32
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
+#endif
                        fclose(out);
                        if (old)
                                fclose(old);
@@ -431,6 +460,8 @@ int main(int argc, char **argv)
        }
        if (old)
                fclose(old);
+       if (newfn)
+               free(newfn);
        return (0);
 }