Last active
June 11, 2022 00:53
-
-
Save miikka/bb2c26034074ebec156f to your computer and use it in GitHub Desktop.
Please have a spinner in sleep(1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* We present: high-quality highly accurate nanosleep with a spinner. | |
* | |
* Compile and run: | |
* | |
* gcc -shared -fPIC -o libsleep.so sleep.c | |
* LD_PRELOAD=./libsleep.so sleep 3 | |
* */ | |
#include <time.h> | |
const char spinner_chars[] = "|\\-/"; | |
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { | |
struct timespec ts; | |
unsigned int i; | |
ts.tv_sec = 0; | |
ts.tv_nsec = 100000000; // 100 ms | |
for (i = rqtp->tv_sec * 10; i > 0; i--) { | |
putchar('\r'); | |
putchar(spinner_chars[i % 4]); | |
fflush(0); | |
__nanosleep(&ts, NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment