-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.c
77 lines (65 loc) · 1.56 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* utils.c
* Auxiliary functions for crxprof
*/
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include "crxprof.h"
void
print_message(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
printf("--- ");
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
/**
* sleeping loop. Sleep on select(2) but awake on keypress (ENTER).
* Since user may press several characters before ENTER, we have
* to discard 'em all
*/
void
wait4keypress(bool *key_pressed)
{
*key_pressed = false;
if (isatty(STDIN_FILENO)) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
if (select(STDIN_FILENO+1, &fds, NULL, NULL, NULL /* timeout = infinite */) == 1) {
static char buf[16];
int nb;
assert( FD_ISSET(STDIN_FILENO, &fds) );
*key_pressed = true;
if (ioctl(STDIN_FILENO, FIONREAD, &nb) == -1) {
warn("ioctl STDIN_FILENO failed");
return;
}
/* simply discard all data */
while(nb) {
ssize_t nr = read(STDIN_FILENO, buf, nb > sizeof(buf) ? sizeof(buf) : nb);
if (nr <= 0)
return;
nb -= nr;
}
}
}
else {
select(0, NULL, NULL, NULL, NULL); /* simply sleep if non-terminal */
}
}
bool
has_openvz()
{
struct stat st;
return stat("/proc/vz", &st) == 0;
}