Octopus
varia.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2002 M. Marques, A. Castro, A. Rubio, G. Bertsch
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21#include <config.h>
22
23#include <math.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#if __has_include(<unistd.h>)
29#include <unistd.h>
30#endif
31
32#ifdef _POSIX_VERSION
33#include <sys/ioctl.h>
34#include <sys/time.h>
35#include <sys/types.h>
36#include <sys/utsname.h>
37#include <termios.h>
38#endif
39
40#include "varia.h"
41
45void sysname(char **c) {
46#ifdef _POSIX_VERSION
47 struct utsname name;
48 uname(&name);
49 *c = (char *)malloc(sizeof(name.nodename) + sizeof(name.sysname) + 4);
50 strcpy(*c, name.nodename);
51 strcat(*c, " (");
52 strcat(*c, name.sysname);
53 strcat(*c, ")");
54#else
55 *c = (char *)malloc(8);
56 strcpy(*c, "unknown");
57#endif
58}
59
64static int foreground_proc(void) {
65#ifdef _POSIX_VERSION
66 static pid_t pgrp = -1;
67 int ctty_pgrp;
69 if (pgrp == -1)
70 pgrp = getpgrp();
72#ifdef _POSIX_VERSION
73 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && ctty_pgrp == pgrp);
74#endif
76#else
77 return 0;
78#endif
80
81int getttywidth(void) {
82#ifdef _POSIX_VERSION
83 struct winsize winsize;
84
85 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
86 return (winsize.ws_col ? winsize.ws_col : 80);
87 else
88#endif
89 return (80);
95void progress_bar(int actual, int max) {
96 static struct timeval start;
97 static int old_pos, next_print;
98 struct timeval now;
99 char buf[512], fmt[64];
100 int i, j, ratio, barlength, remaining;
101 double elapsed;
102
103 if (actual < 0) {
104 (void)gettimeofday(&start, (struct timezone *)0);
105 actual = 0;
106 old_pos = 0;
107 next_print = 10;
108 }
109
110 if (max > 0) {
111 ratio = 100 * actual / max;
112 if (ratio < 0)
113 ratio = 0;
114 if (ratio > 100) {
115 ratio = 100;
116 fprintf(stderr,
117 "Internal warning: progress_bar called with actual %i > max %i\n",
118 actual, max);
120 } else
121 ratio = 100;
123 if (foreground_proc() == 0) {
124 if (old_pos == 0) {
125 printf("ETA: ");
128 barlength = getttywidth() - 6;
130 j = actual * (barlength - 1) / max;
131 if (j > barlength || actual == max)
132 j = barlength;
133 if (j < 1)
134 j = 1;
136 if (j > old_pos) {
137 for (i = old_pos + 1; i <= j; i++)
138 if (i * 100 / barlength >= next_print) {
139 printf("%1d", next_print / 10 % 10);
140 next_print += 10;
141 } else
142 printf(".");
143 old_pos = j;
144 if (j == barlength)
145 printf("\n");
146 }
147
148 } else {
149
150 sprintf(buf, "%d", max);
151 i = strlen(buf);
152 if (i < 3)
153 i = 3;
154 sprintf(fmt, "\r[%%%dd/%%%dd]", i, i);
155 sprintf(buf, fmt, actual, max);
156 sprintf(buf + strlen(buf), " %3d%%", ratio);
157
158 barlength = getttywidth() - strlen(buf) - 16;
159 if (barlength > 0) {
160 i = barlength * ratio / 100;
161 sprintf(buf + strlen(buf), "|%.*s%*s|", i,
162 "*******************************************************"
163 "*******************************************************"
164 "*******************************************************"
165 "*******************************************************"
166 "*******************************************************"
167 "*******************************************************"
168 "*******************************************************",
169 barlength - i, "");
170 }
171
172 /* time information now */
173 (void)gettimeofday(&now, (struct timezone *)0);
174 elapsed = now.tv_sec - start.tv_sec;
175
176 if (elapsed <= 0.0 || actual <= 0) {
177 sprintf(buf + strlen(buf), " --:-- ETA");
178 } else {
179 remaining = (int)(max / (actual / elapsed) - elapsed);
180 if (remaining < 0)
181 remaining = 0;
182
183 i = remaining / 3600;
184 if (i)
185 sprintf(buf + strlen(buf), "%4d:", i);
186 else
187 sprintf(buf + strlen(buf), " ");
188 i = remaining % 3600;
189 sprintf(buf + strlen(buf), "%02d:%02d%s", i / 60, i % 60, " ETA");
190 }
191 printf("%s", buf);
192 }
193
194 fflush(stdout);
195}
196
197#undef timersub
__pid_t pid_t
Definition: io_binary.c:267
subroutine start()
start the timer (save starting time)
Definition: walltimer.F90:264
ptrdiff_t i
Definition: operate_inc.c:12
ptrdiff_t j
Definition: operate_inc.c:12
static int foreground_proc(void)
Definition: varia.c:4621
int gettimeofday(struct timeval *__restrict __tv, void *__restrict __tz) __attribute__((__nothrow__
void sysname(char **c)
Definition: varia.c:4602
FILE * stdout
int getttywidth(void)
Definition: varia.c:4642
FILE * stderr
int uname(struct utsname *__name) __attribute__((__nothrow__
int ioctl(int __fd, unsigned long int __request,...) __attribute__((__nothrow__
void progress_bar(int actual, int max)
Definition: varia.c:4664