Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The utility is a graphic timer. It should be launched in a terminal
- * emulator. It determines the size of the terminal window and then
- * fills all of it with the character "W", in the number of minutes
- * indicated in the definition of the macro SESSION_DURATION_MINS. */
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #define SESSION_DURATION_MINS 20
- #define MICROSECS_IN_A_MINUTE 60000000
- int main() {
- struct winsize w;
- // Get the number of columns and the number of lines of the terminal window
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
- perror("ioctl failed!");
- return 1;
- }
- int char_places_number = (int)(w.ws_row * w.ws_col);
- if (char_places_number <= 0) {
- fprintf(stderr, "Invalid terminal size.\n");
- return 1;
- }
- useconds_t timeout_microsecs = (useconds_t)(SESSION_DURATION_MINS * MICROSECS_IN_A_MINUTE / char_places_number);
- // Clear the screen
- printf("\033[H\033[J");
- for (int i = 0; i < char_places_number; i++) {
- (void) putchar(87);
- (void) fflush(stdout);
- (void) usleep(timeout_microsecs);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment