diff options
-rw-r--r-- | fdate.c | 42 |
1 files changed, 30 insertions, 12 deletions
@@ -1,21 +1,39 @@ /* reads a filename from stdin and prints its time-last-modified, in format [d]d <Month-name> yyyy */ +#include <errno.h> +#include <limits.h> #include <stdio.h> -#include <sys/types.h> +#include <string.h> #include <sys/stat.h> -#include <unistd.h> #include <time.h> +#include <unistd.h> + +#ifndef PATH_MAX +#ifdef _XOPEN_PATH_MAX +#define PATH_MAX _XOPEN_PATH_MAX +#else +#define PATH_MAX _POSIX_PATH_MAX +#endif +#endif + +int main(void) +{ + char buf[PATH_MAX]; + if (scanf("%s", buf) != 1) { + fprintf(stderr, "fdate: invalid input\n"); + return 1; + } -struct stat buf; -struct tm *t; + struct stat st; + if (stat(buf, &st) != 0) { + fprintf(stderr, "fdate: error reading date from \"%s\": %s\n", buf, + strerror(errno)); + return 1; + } -char *month[] = {"January","February","March","April","May","June", - "July","August","September","October","November","December"}; -int main() -{ char f[200]; - if(scanf("%s",f)==1&&stat(f,&buf)==0) - t=localtime(&buf.st_mtime), - printf("%d %s %4d\n",(*t).tm_mday,month[(*t).tm_mon],(*t).tm_year+1900); - else fprintf(stderr,"fdate: bad file \"%s\"\n",f); + struct tm *tm = localtime(&st.st_mtime); + strftime(buf, sizeof(buf), "%d %B %Y", tm); + puts(buf[0] == '0' ? buf + 1 : buf); + return 0; } |