summaryrefslogtreecommitdiff
path: root/fdate.c
blob: d1a015a38eb0556dce9bf1e40ba7dcf3d08f2486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 <string.h>
#include <sys/stat.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 st;
  if (stat(buf, &st) != 0) {
    fprintf(stderr, "fdate: error reading date from \"%s\": %s\n", buf,
      strerror(errno));
    return 1;
  }

  struct tm *tm = localtime(&st.st_mtime);
  strftime(buf, sizeof(buf), "%d %B %Y", tm);
  puts(buf[0] == '0' ? buf + 1 : buf);
  return 0;
}