/******************************************************************************* * * timeset.c * * AUTHOR: * Dan Harkless * * COPYRIGHT: * This file is Copyright (C) 2002 by Dan Harkless, and is released under the * GNU General Public License . * * USAGE: * % timeset [...] * * DESCRIPTION: * Sets the modification time of one or more files to the given integer value * (interpreted as seconds since the epoch). That value can be given in * decimal, hexadecimal, or octal form. The access time of the file(s) will be * preserved. If one of them doesn't exist or has a permissions problem, an * error will be issued and the program will ultimately exit with nonzero * status, but the remaining files will still be processed in the meantime. * * DATE MODIFICATION * ========== ================================================================== * 2002-03-16 Upgraded to work with more than one file. Unfortunately, before, * I had the argument order as because I liked the way * "timeset to " read, but it's awkward to have 1...N * parameters not at the end of the commandline, so I changed the * timestamp to be the first argument, with the file(s) following. * Luckily it would be unlikely for someone to get burned by the * change, since the timestamp is enforced to be a legal integer * value (albeit in one of three bases) with no spurious characters. * 2000-02-11 Original. * *******************************************************************************/ #include /* supposed to precede and */ #include /* for errno */ #include /* for fprintf(), etc. */ #include /* for strtol(), etc. */ #include /* for strerror() and strrchr() */ #include /* for stat() and struct stat */ #include /* for struct utimbuf and utime() */ int main(int argc, char** argv) { char* char_after_number_ptr; char* last_slash = strrchr(argv[0], '/'); char* our_program_name; int exit_status = EXIT_SUCCESS, i; time_t timestamp_integer; if (last_slash == NULL) our_program_name = argv[0]; else our_program_name = last_slash + 1; if (argc < 3) { fprintf(stderr, "Usage: %s [...]\n", our_program_name); return EXIT_FAILURE; } timestamp_integer = strtol(argv[1], &char_after_number_ptr, 0); if (errno != 0) { fprintf(stderr, "%s: \"%s\": %s.\n", our_program_name, argv[1], strerror(errno)); return EXIT_FAILURE; } else if (*char_after_number_ptr != '\0') { fprintf(stderr, "%s: \"%s\": Illegal character in integer value.\n", our_program_name, argv[1]); fprintf(stderr, "%*c %*c^\n", (int)strlen(our_program_name), ' ', char_after_number_ptr - argv[1], ' '); return EXIT_FAILURE; } for (i = 2; i < argc; i++) { struct stat file_stat; if (stat(argv[i], &file_stat) < 0) { fprintf(stderr, "%s: \"%s\": %s.\n", our_program_name, argv[i], strerror(errno)); exit_status = EXIT_FAILURE; } else { struct utimbuf timestamp_struct; timestamp_struct.actime = file_stat.st_atime; timestamp_struct.modtime = timestamp_integer; if (utime(argv[i], ×tamp_struct) < 0) { fprintf(stderr, "%s: \"%s\": %s.\n", our_program_name, argv[i], strerror(errno)); exit_status = EXIT_FAILURE; } } } return exit_status; }