summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-27 21:28:28 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-27 21:28:28 -0400
commit8aa9043674138f3ce80d448f91bd6c5d85f15f42 (patch)
tree1b85192272bf3e31db79bc2c2862614f2d89ae5d
parent4af8886a4bb8963b15af3b3828d33152281bf9a3 (diff)
update formatting, improve error messages, use strftime()
-rw-r--r--fdate.c42
1 files changed, 30 insertions, 12 deletions
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 <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;
}