filetime.c (2765B)
1 // $Id$ 2 3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de> 4 // based on filetime.cc by Jacques Gelinas 5 // 6 // This program is free software; you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation; either version 2, or (at your option) 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program; if not, write to the Free Software 18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 20 #ifdef HAVE_CONFIG_H 21 # include <config.h> 22 #endif 23 24 #include "util.h" 25 #include "lib/internal.h" 26 27 #include <stdlib.h> 28 #include <stdio.h> 29 #include <string.h> 30 #include <errno.h> 31 #include <time.h> 32 #include <sys/stat.h> 33 34 static void 35 showHelp(char const *cmd) 36 { 37 WRITE_MSG(1, "Usage: "); 38 WRITE_STR(1, cmd); 39 WRITE_MSG(1, 40 " [--] <filename>\n" 41 "\n" 42 "Shows the relative age of <filename>\n" 43 "\n" 44 "Please report bugs to " PACKAGE_BUGREPORT "\n"); 45 exit(0); 46 } 47 48 static void 49 showVersion() 50 { 51 WRITE_MSG(1, 52 "filetime " VERSION " -- shows age of a file\n" 53 "This program is part of " PACKAGE_STRING "\n\n" 54 "Copyright (C) 2004 Enrico Scholz\n" 55 VERSION_COPYRIGHT_DISCLAIMER); 56 exit(0); 57 } 58 59 int main (int argc, char *argv[]) 60 { 61 int idx = 1; 62 struct stat st; 63 64 if (argc>=2) { 65 if (strcmp(argv[1], "--help") ==0) showHelp(argv[0]); 66 if (strcmp(argv[1], "--version")==0) showVersion(); 67 if (strcmp(argv[1], "--") ==0) ++idx; 68 } 69 if (argc<idx+1) 70 WRITE_MSG(2, "No filename specified; use '--help' for more information\n"); 71 else if (stat(argv[idx], &st)==-1) 72 PERROR_Q("stat", argv[idx]); 73 else { 74 time_t now = time(NULL); 75 time_t since = now - st.st_mtime; 76 int days = since / (24*60*60); 77 int today = since % (24*60*60); 78 int hours = today / (60*60); 79 int minutes = (today % (60*60)) / 60; 80 81 char buf[3*sizeof(time_t)*3 + 128]; 82 size_t l = 0; 83 84 if (days > 0) { 85 l = utilvserver_fmt_ulong(buf, days); 86 buf[l++] = ' '; 87 #define MSG "days, " 88 memcpy(buf+l, MSG, sizeof(MSG)-1); l += sizeof(MSG)-1; 89 } 90 91 if (hours<10) buf[l++] = '0'; 92 l += utilvserver_fmt_ulong(buf+l, hours); 93 buf[l++] = ':'; 94 if (minutes<10) buf[l++] = '0'; 95 l += utilvserver_fmt_ulong(buf+l, minutes); 96 buf[l++] = '\n'; 97 98 Vwrite(1, buf, l); 99 return EXIT_SUCCESS; 100 } 101 102 return EXIT_FAILURE; 103 } 104