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 <assert.h>
24#include <math.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#ifdef _POSIX_VERSION
31#include <sys/ioctl.h>
32#include <sys/time.h>
33#include <sys/types.h>
34#include <sys/utsname.h>
35#include <termios.h>
36#endif
37
38#include "varia.h"
39
43void sysname(char **c) {
44#ifdef _POSIX_VERSION
45 struct utsname name;
46 uname(&name);
47 *c = (char *)malloc(sizeof(name.nodename) + sizeof(name.sysname) + 4);
48 strcpy(*c, name.nodename);
49 strcat(*c, " (");
50 strcat(*c, name.sysname);
51 strcat(*c, ")");
52#else
53 *c = (char *)malloc(8);
54 strcpy(*c, "unknown");
55#endif
56}
57
62static int foreground_proc(void) {
63#ifdef _POSIX_VERSION
64 static pid_t pgrp = -1;
65 int ctty_pgrp;
66
67 if (pgrp == -1)
68 pgrp = getpgrp();
69
70#ifdef _POSIX_VERSION
71 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && ctty_pgrp == pgrp);
72#endif
73
74#else
75 return 0;
76#endif
77}
78
79int getttywidth(void) {
80#ifdef _POSIX_VERSION
81 struct winsize winsize;
82
83 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
84 return (winsize.ws_col ? winsize.ws_col : 80);
85 else
86#endif
87 return (80);
90void foreground_print_progress(const int actual, const int max, const struct timeval *start) {
92 char buf[512], fmt[64];
93
94 int ratio = 100 * actual / max;
95 if (ratio < 0) { ratio = 0; }
96 if (ratio > 100) {
97 fprintf(stderr,
98 "Internal warning: progress_bar called with actual %i > max %i\n",
99 actual, max);
100 }
102 sprintf(buf, "%d", max);
103 int i = strlen(buf);
104 if (i < 3)
105 i = 3;
106 sprintf(fmt, "\r[%%%dd/%%%dd]", i, i);
107 sprintf(buf, fmt, actual, max);
108 sprintf(buf + strlen(buf), " %3d%%", ratio);
110 const int barlength = getttywidth() - strlen(buf) - 16;
111 if (barlength > 0) {
112 i = barlength * ratio / 100;
113 sprintf(buf + strlen(buf), "|%.*s%*s|", i,
114 "*******************************************************"
115 "*******************************************************"
116 "*******************************************************"
117 "*******************************************************"
118 "*******************************************************"
119 "*******************************************************"
120 "*******************************************************",
121 barlength - i, "");
122 }
123
124 /* time information now */
125 struct timeval now;
126 (void) gettimeofday(&now, (struct timezone *) 0);
127 const double elapsed = (double)(now.tv_sec - start->tv_sec);
128
129 if (elapsed <= 0.0 || actual <= 0) {
130 sprintf(buf + strlen(buf), " --:-- ETA");
131 } else {
132 int remaining = (int) (max / (actual / elapsed) - elapsed);
133 if (remaining < 0) { remaining = 0; }
134 i = remaining / 3600;
135 if (i)
136 sprintf(buf + strlen(buf), "%4d:", i);
137 else
138 sprintf(buf + strlen(buf), " ");
139 i = remaining % 3600;
140 sprintf(buf + strlen(buf), "%02d:%02d%s", i / 60, i % 60, " ETA");
142 printf("%s", buf);
145void progress_bar(int actual, int max) {
146 static struct timeval start;
147 static int show_bar;
148
149 assert(max > 0 && "progress_bar: max must be > 0");
151 // Initialise on first call
152 if (actual < 0) {
153 show_bar = isatty(STDOUT_FILENO);
154 if (!show_bar) { return; }
155 (void) gettimeofday(&start, (struct timezone *) 0);
156 actual = 0;
157 }
159 if (!show_bar) { return; }
160
161 if (foreground_proc() == 1) {
163 }
164
165 fflush(stdout);
166}
167
168#undef timersub
__pid_t pid_t
Definition: debug_low.c:411
subroutine start()
start the timer (save starting time)
Definition: walltimer.F90:266
ptrdiff_t i
Definition: operate_inc.c:12
static int foreground_proc(void)
Definition: varia.c:4642
void foreground_print_progress(const int actual, const int max, const struct timeval *start)
Definition: varia.c:4682
int gettimeofday(struct timeval *__restrict __tv, void *__restrict __tz) __attribute__((__nothrow__
void sysname(char **c)
Definition: varia.c:4623
FILE * stdout
int getttywidth(void)
Definition: varia.c:4663
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:4741