sfeed_plain.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <sys/types.h>
  2. #include <locale.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include "util.h"
  7. static time_t comparetime;
  8. static char *line;
  9. static size_t linesize;
  10. static void
  11. printfeed(FILE *fp, const char *feedname)
  12. {
  13. char *fields[FieldLast];
  14. struct tm rtm, *tm;
  15. time_t parsedtime;
  16. ssize_t linelen;
  17. while ((linelen = getline(&line, &linesize, fp)) > 0 &&
  18. !ferror(stdout)) {
  19. if (line[linelen - 1] == '\n')
  20. line[--linelen] = '\0';
  21. parseline(line, fields);
  22. parsedtime = 0;
  23. if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
  24. (tm = localtime_r(&parsedtime, &rtm))) {
  25. if (parsedtime >= comparetime)
  26. fputs("N ", stdout);
  27. else
  28. fputs(" ", stdout);
  29. fprintf(stdout, "%04d-%02d-%02d %02d:%02d ",
  30. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  31. tm->tm_hour, tm->tm_min);
  32. } else {
  33. fputs(" ", stdout);
  34. }
  35. if (feedname[0]) {
  36. printutf8pad(stdout, feedname, 15, ' ');
  37. fputs(" ", stdout);
  38. }
  39. printutf8pad(stdout, fields[FieldTitle], 70, ' ');
  40. printf(" %s\n", fields[FieldLink]);
  41. }
  42. }
  43. int
  44. main(int argc, char *argv[])
  45. {
  46. FILE *fp;
  47. char *name;
  48. int i;
  49. if (pledge("stdio rpath", NULL) == -1)
  50. err(1, "pledge");
  51. setlocale(LC_CTYPE, "");
  52. if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
  53. err(1, "pledge");
  54. if ((comparetime = time(NULL)) == (time_t)-1)
  55. errx(1, "time");
  56. /* 1 day is old news */
  57. comparetime -= 86400;
  58. if (argc == 1) {
  59. printfeed(stdin, "");
  60. checkfileerror(stdin, "<stdin>", 'r');
  61. } else {
  62. for (i = 1; i < argc; i++) {
  63. if (!(fp = fopen(argv[i], "r")))
  64. err(1, "fopen: %s", argv[i]);
  65. name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
  66. printfeed(fp, name);
  67. checkfileerror(fp, argv[i], 'r');
  68. checkfileerror(stdout, "<stdout>", 'w');
  69. fclose(fp);
  70. }
  71. }
  72. checkfileerror(stdout, "<stdout>", 'w');
  73. return 0;
  74. }