Skip to content

Instantly share code, notes, and snippets.

@nxghtmvrx
Forked from rofl0r/sleep.c
Created March 27, 2020 20:56
Show Gist options
  • Select an option

  • Save nxghtmvrx/a2770038f16eaed1856706a71fd9f9b9 to your computer and use it in GitHub Desktop.

Select an option

Save nxghtmvrx/a2770038f16eaed1856706a71fd9f9b9 to your computer and use it in GitHub Desktop.
gnu compatible sleep tool.
#define _POSIX_C_SOURCE 200809L
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int i;
char* end;
int times;
for(i = 1; i < argc; i++) {
float s = strtof(argv[i], &end);
switch(*end) {
case 'm':
times = 60;
break;
case 'h':
times = 60 * 60;
break;
case 'd':
times = 60 * 60 * 24;
break;
default:
times = 1;
}
while(times--) {
unsigned long long nsec = (s * 1000000000.f);
nanosleep(&(struct timespec) {nsec / 1000000000, nsec % 1000000000}, NULL);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment