void test_milliseconds_to_long_string() { equal("less than one second", milliseconds_to_long_string(500)); equal("5 seconds", milliseconds_to_long_string(5000)); equal("2 seconds", milliseconds_to_long_string(2500)); equal("1 minute", milliseconds_to_long_string(MS_MIN)); equal("5 minutes", milliseconds_to_long_string(5 * MS_MIN)); equal("1 hour", milliseconds_to_long_string(MS_HOUR)); equal("2 days", milliseconds_to_long_string(2 * MS_DAY)); equal("2 weeks", milliseconds_to_long_string(15 * MS_DAY)); equal("1 year", milliseconds_to_long_string(MS_YEAR)); equal("3 years", milliseconds_to_long_string(3 * MS_YEAR)); }
void show_status_of(const char *pidfile) { off_t size; struct stat s; // stat if (stat(pidfile, &s) < 0) { perror("stat()"); exit(1); } size = s.st_size; // opens int fd = open(pidfile, O_RDONLY, 0); if (fd < 0) { perror("open()"); exit(1); } // read char buf[size]; if (size != read(fd, buf, size)) { perror("read()"); exit(1); } // updtime time_t modified = s.st_mtime; struct timeval t; gettimeofday(&t, NULL); time_t now = t.tv_sec; time_t secs = now - modified; // status pid_t pid = atoi(buf); if (alive(pid)) { char *str = milliseconds_to_long_string(secs * 1000); printf("%d:alive:%s", pid, str); free(str); } else { printf("%d:dead", pid); } close(fd); }