34#include <sys/utsname.h>
47 *c = (
char *)malloc(
sizeof(name.nodename) +
sizeof(name.sysname) + 4);
48 strcpy(*c, name.nodename);
50 strcat(*c, name.sysname);
53 *c = (
char *)malloc(8);
54 strcpy(*c,
"unknown");
64 static pid_t pgrp = -1;
71 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && ctty_pgrp == pgrp);
81 struct winsize winsize;
83 if (
ioctl(fileno(
stdout), TIOCGWINSZ, &winsize) != -1)
84 return (winsize.ws_col ? winsize.ws_col : 80);
92 char buf[512], fmt[64];
94 int ratio = 100 * actual / max;
95 if (ratio < 0) { ratio = 0; }
98 "Internal warning: progress_bar called with actual %i > max %i\n",
102 sprintf(buf,
"%d", max);
106 sprintf(fmt,
"\r[%%%dd/%%%dd]",
i,
i);
107 sprintf(buf, fmt, actual, max);
108 sprintf(buf + strlen(buf),
" %3d%%", ratio);
112 i = barlength * ratio / 100;
113 sprintf(buf + strlen(buf),
"|%.*s%*s|",
i,
114 "*******************************************************"
115 "*******************************************************"
116 "*******************************************************"
117 "*******************************************************"
118 "*******************************************************"
119 "*******************************************************"
120 "*******************************************************",
127 const double elapsed = (double)(now.tv_sec -
start->tv_sec);
129 if (elapsed <= 0.0 || actual <= 0) {
130 sprintf(buf + strlen(buf),
" --:-- ETA");
132 int remaining = (int) (max / (actual / elapsed) - elapsed);
133 if (remaining < 0) { remaining = 0; }
134 i = remaining / 3600;
136 sprintf(buf + strlen(buf),
"%4d:",
i);
138 sprintf(buf + strlen(buf),
" ");
139 i = remaining % 3600;
140 sprintf(buf + strlen(buf),
"%02d:%02d%s",
i / 60,
i % 60,
" ETA");
149 assert(max > 0 &&
"progress_bar: max must be > 0");
153 show_bar = isatty(STDOUT_FILENO);
154 if (!show_bar) {
return; }
159 if (!show_bar) {
return; }
subroutine start()
start the timer (save starting time)
static int foreground_proc(void)
void foreground_print_progress(const int actual, const int max, const struct timeval *start)
int gettimeofday(struct timeval *__restrict __tv, void *__restrict __tz) __attribute__((__nothrow__
int uname(struct utsname *__name) __attribute__((__nothrow__
int ioctl(int __fd, unsigned long int __request,...) __attribute__((__nothrow__
void progress_bar(int actual, int max)