-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
50 lines (45 loc) · 1.71 KB
/
util.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
#include "blather.h"
// If condition is a truthy value, call perror() with a message
// constructed with the fmt and additional args provided then exit.
// Example:
//
// checkfail(myfd==-1, 1, "Couldn't open file %s", filename);
void check_fail(int condition, int perr, char *fmt, ...) {
if(!condition){
return;
}
if(perr){
char msg[MAXLINE]; // buffer for message
va_list myargs; // declare a va_list type variable
va_start(myargs, fmt); // initialise the va_list variable with the ... after fmt
vsnprintf(msg,MAXLINE,fmt,myargs); // forward the '...' to vsnprintf()
va_end(myargs); // clean up the va_list
perror(msg);
}
else{
va_list myargs; // declare a va_list type variable
va_start(myargs, fmt); // initialise the va_list variable with the ... after fmt
vfprintf(stderr,fmt,myargs); // forward the '...' to vfprintf()
va_end(myargs); // clean up the va_list
}
exit(1);
}
// Print like printf but only if DEBUG is defined as 1. Print to
// standard error.
void dbg_printf(char *fmt, ...){
if(DEBUG){
va_list myargs; // declare a va_list type variable
va_start(myargs, fmt); // initialise the va_list variable with the ... after fmt
vfprintf(stderr,fmt,myargs); // forward the '...' to vfprintf()
va_end(myargs); // clean up the va_list
}
}
// Sleep the running program for the given number of nanoseconds and
// seconds.
void pause_for(long nanos, int secs){
struct timespec tm = {
.tv_nsec = nanos,
.tv_sec = secs,
};
nanosleep(&tm,NULL);
}