test-chmtime.con commit GIT 1.5.6-rc2 (218bf69)
   1#include "git-compat-util.h"
   2#include <utime.h>
   3
   4static const char usage_str[] = "(+|=|=+|=-|-)<seconds> <file>...";
   5
   6int main(int argc, const char *argv[])
   7{
   8        int i;
   9        int set_eq;
  10        long int set_time;
  11        char *test;
  12        const char *timespec;
  13
  14        if (argc < 3)
  15                goto usage;
  16
  17        timespec = argv[1];
  18        set_eq = (*timespec == '=') ? 1 : 0;
  19        if (set_eq) {
  20                timespec++;
  21                if (*timespec == '+') {
  22                        set_eq = 2; /* relative "in the future" */
  23                        timespec++;
  24                }
  25        }
  26        set_time = strtol(timespec, &test, 10);
  27        if (*test) {
  28                fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
  29                goto usage;
  30        }
  31        if ((set_eq && set_time < 0) || set_eq == 2) {
  32                time_t now = time(NULL);
  33                set_time += now;
  34        }
  35
  36        for (i = 2; i < argc; i++) {
  37                struct stat sb;
  38                struct utimbuf utb;
  39
  40                if (stat(argv[i], &sb) < 0) {
  41                        fprintf(stderr, "Failed to stat %s: %s\n",
  42                                argv[i], strerror(errno));
  43                        return -1;
  44                }
  45
  46                utb.actime = sb.st_atime;
  47                utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
  48
  49                if (utime(argv[i], &utb) < 0) {
  50                        fprintf(stderr, "Failed to modify time on %s: %s\n",
  51                                argv[i], strerror(errno));
  52                        return -1;
  53                }
  54        }
  55
  56        return 0;
  57
  58usage:
  59        fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
  60        return -1;
  61}