#define _BSD_SOURCE 1 #include #include #include #include #include #define TERM_WIDTH 80 void chill(long nanosecs) { static struct timespec req, rem; req.tv_nsec = nanosecs; nanosleep(&req, &rem); } int determine_screen_width (void) { /* If there's a way to get the terminal size using POSIX tcgetattr(), somebody please tell me. */ #ifdef TIOCGWINSZ int fd; struct winsize wsz; fd = fileno (stderr); if (ioctl (fd, TIOCGWINSZ, &wsz) < 0) return 0; /* most likely ENOTTY */ return wsz.ws_col; #elif defined(WINDOWS) CONSOLE_SCREEN_BUFFER_INFO csbi; if (!GetConsoleScreenBufferInfo (GetStdHandle (STD_ERROR_HANDLE), &csbi)) return 0; return csbi.dwSize.X; #else /* neither TIOCGWINSZ nor WINDOWS */ return 0; #endif /* neither TIOCGWINSZ nor WINDOWS */ } int main(int argc, char** argv) { char* bar; int i, term_width, bar_width; term_width = determine_screen_width(); bar_width = term_width - 1; bar = malloc(term_width); memset(bar, ' ', bar_width); bar[1] = '['; bar[bar_width - 1] = ']'; for( i = 2; i < bar_width - 1; i++ ) { bar[i] = '*'; printf("\r%s", bar); // '\r' is a carriage return -- resets the cursor position to the beginning of the line fflush(stdout); // necessary or we don't get any output until after this loop chill(50000000); // chill like a panther in a tree } printf("\n"); }