/******************************************************************************* * * timestamp.c * * AUTHOR: * Dan Harkless * * COPYRIGHT: * This file is Copyright (C) 1999 by Dan Harkless, and is released under the * GNU General Public License . * * USAGE: * % timestamp [ [...]] * * DESCRIPTION: * Prints out the modification time of one or more files in seconds past the * epoch. If no files are specified, gives the current time in that format. * * DATE MODIFICATION * ========== ================================================================== * 1999-05-19 Allow the specification of multiple files on the commandline. If * no files are specified, give current time in seconds since epoch. * 1999-04-09 Failing to open a file doesn't necessarily mean it doesn't exist. * 1996-05-31 Original. * *******************************************************************************/ #include /* supposed to precede */ #include /* for errno */ #include /* for fprintf(), etc. */ #include /* for EXIT_FAILURE and EXIT_SUCCESS */ #include /* for strerror() and strrchr() */ #include /* for stat() and struct stat */ #include /* for time() */ int main(int argc, char** argv) { char* last_slash = strrchr(argv[0], '/'); char* our_program_name; int exit_status = EXIT_SUCCESS; if (last_slash == NULL) our_program_name = argv[0]; else our_program_name = last_slash + 1; if (argc == 1) /* Print out the current time. */ printf("%ld\n", time(NULL)); else { int i; for (i = 1; i < argc; i++) { struct stat file_stat; if (stat(argv[i], &file_stat) < 0) { fprintf(stderr, "%s: \"%s\": %s.\n", our_program_name, argv[1], strerror(errno)); exit_status = EXIT_FAILURE; } else if (argc == 2) /* Like grep, don't print file name if only one specified. */ printf("%ld\n", file_stat.st_mtime); else /* Like grep, print file name if more than one specified. */ printf("%s: %ld\n", argv[i], file_stat.st_mtime); } } return exit_status; }