From 8aa9043674138f3ce80d448f91bd6c5d85f15f42 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 27 Mar 2022 21:28:28 -0400 Subject: update formatting, improve error messages, use strftime() --- fdate.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'fdate.c') diff --git a/fdate.c b/fdate.c index 5a25803..d1a015a 100644 --- a/fdate.c +++ b/fdate.c @@ -1,21 +1,39 @@ /* reads a filename from stdin and prints its time-last-modified, in format [d]d yyyy */ +#include +#include #include -#include +#include #include -#include #include +#include + +#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; } -- cgit v1.2.1