Skip to content

Instantly share code, notes, and snippets.

@miikka
Last active June 11, 2022 00:53
Show Gist options
  • Save miikka/bb2c26034074ebec156f to your computer and use it in GitHub Desktop.
Save miikka/bb2c26034074ebec156f to your computer and use it in GitHub Desktop.
Please have a spinner in sleep(1)
/* 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