forked from boardhead/aged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUtils.cxx
110 lines (97 loc) · 2.55 KB
/
CUtils.cxx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//==============================================================================
// File: CUtils.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "CUtils.h"
char *progname = "aged";
char *progpath = "aged";
#ifdef VAX
unsigned long LIB$WAIT( float* );
#endif
/*---------------------------------------------------------------------------
*/
void setProgname(char *aFilename, char *aPathname)
{
progname = aFilename;
progpath = aPathname;
}
/*
* get current accurate time in seconds using system clock - PH 02/01/98
*/
double double_time(void)
{
/* add support for systems without clock_gettime() or ftime() - PH 01/20/99 */
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
return(tv.tv_sec + 1e-6 * tv.tv_usec);
}
/* portable sleep routine */
void usleep_(unsigned long usec)
{
#ifdef USE_NANOSLEEP
struct timespec ts;
ts.tv_sec = usec / 1000000UL;
ts.tv_nsec = (usec - ts.tv_sec * 1000000UL) * 1000;
nanosleep(&ts,NULL);
#elif VAX
float secs;
secs = usec * 0.000001;
LIB$WAIT(&secs);
#else
usleep(usec);
#endif
}
/* Printf buffer variables */
static char *sPrintfBuffer = NULL;
static int sPrintfBufferMax = 0;
static int sPrintfBufferLen = 0;
void SetPrintfOutput(char *buff, int size)
{
sPrintfBuffer = buff;
sPrintfBufferMax = size;
sPrintfBufferLen = 0;
if (buff && size) *buff = '\0'; /* start with null string in buffer */
}
#ifdef VAX
// necessary to avoid conflict with printf on VAX
int vaxPrintf(char *fmt,...)
#else
int Printf(char *fmt,...)
#endif
{
int len = 0;
va_list varArgList;
va_start(varArgList,fmt);
if (sPrintfBuffer) {
/* check to see if buffer is getting too full */
if (sPrintfBufferLen + 80 > sPrintfBufferMax) {
printf("%s: Printf buffer too full!\n", progname);
} else {
/* add output to buffer */
char *pt = strchr(sPrintfBuffer, '\0');
len = sprintf(pt,"%s: ",progname);
len += vsprintf(pt+len,fmt,varArgList);
sPrintfBufferLen = pt - sPrintfBuffer + len;
}
} else {
len = printf("%s: ", progname);
len += vprintf(fmt,varArgList);
}
va_end(varArgList);
return(len);
}
void quit(char *msg)
{
printf("%s: %s\n",progname,msg);
exit(0);
}