pure-statsdecode.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <config.h>
  2. #include "ftpd.h"
  3. #ifdef WITH_DMALLOC
  4. # include <dmalloc.h>
  5. #endif
  6. static void usage(void)
  7. {
  8. puts("Usage: pure-statsdecode [stats log file] [-]");
  9. }
  10. int main(int argc, char *argv[])
  11. {
  12. int instamp = 0;
  13. int c;
  14. const char *file;
  15. FILE *fp;
  16. time_t date;
  17. struct tm *tm;
  18. char timestamp[42];
  19. if (argc != 2) {
  20. usage();
  21. return 1;
  22. }
  23. #ifdef HAVE_SETLOCALE
  24. # ifdef LC_MESSAGES
  25. (void) setlocale(LC_MESSAGES, "");
  26. # endif
  27. # ifdef LC_CTYPE
  28. (void) setlocale(LC_CTYPE, "");
  29. # endif
  30. # ifdef LC_COLLATE
  31. (void) setlocale(LC_COLLATE, "");
  32. # endif
  33. #endif
  34. file = argv[1];
  35. if (*file == '-' && file[1] == 0) {
  36. fp = stdin;
  37. } else {
  38. if ((fp = fopen(file, "r")) == NULL) {
  39. perror("Can't open file: ");
  40. return -1;
  41. }
  42. }
  43. while ((c = getc(fp)) != EOF) {
  44. if (instamp >= 0) {
  45. if (isdigit(c)) {
  46. if (instamp < (int) (sizeof timestamp - 1U)) {
  47. timestamp[instamp] = (char) c;
  48. instamp++;
  49. }
  50. } else {
  51. timestamp[instamp] = 0;
  52. instamp = -1;
  53. date = (time_t) strtoul(timestamp, NULL, 10);
  54. tm = localtime(&date);
  55. if (tm == NULL) {
  56. printf("- - ");
  57. } else{
  58. printf("%d/%02d/%02d %02d:%02d:%02d ",
  59. tm->tm_year + 1900,
  60. tm->tm_mon + 1,
  61. tm->tm_mday,
  62. tm->tm_hour,
  63. tm->tm_min,
  64. tm->tm_sec);
  65. }
  66. }
  67. } else {
  68. if (c == '\n' || !ISCTRLCODE(c)) {
  69. putchar(c);
  70. }
  71. }
  72. if (c == '\n') {
  73. fflush(fp);
  74. instamp = 0;
  75. }
  76. }
  77. fclose(fp);
  78. return 0;
  79. }