-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
198 lines (159 loc) · 4.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
dbench version 2
Copyright (C) Andrew Tridgell 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "dbench.h"
#ifndef SHM_W
#define SHM_W 0000200
#endif
#ifndef SHM_R
#define SHM_R 0000400
#endif
/* return a pointer to a anonymous shared memory segment of size "size"
which will persist across fork() but will disappear when all processes
exit
The memory is zeroed
This function uses system5 shared memory. It takes advantage of a property
that the memory is not destroyed if it is attached when the id is removed
*/
void *shm_setup(int size)
{
int shmid;
void *ret;
shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
if (shmid == -1) {
printf("can't get private shared memory of %d bytes: %s\n",
size,
strerror(errno));
exit(1);
}
ret = (void *)shmat(shmid, 0, 0);
if (!ret || ret == (void *)-1) {
printf("can't attach to shared memory\n");
return NULL;
}
/* the following releases the ipc, but note that this process
and all its children will still have access to the memory, its
just that the shmid is no longer valid for other shm calls. This
means we don't leave behind lots of shm segments after we exit
See Stevens "advanced programming in unix env" for details
*/
shmctl(shmid, IPC_RMID, 0);
memset(ret, 0, size);
return ret;
}
/****************************************************************************
similar to string_sub() but allows for any character to be substituted.
Use with caution!
****************************************************************************/
void all_string_sub(char *s,const char *pattern,const char *insert)
{
char *p;
size_t ls,lp,li;
if (!insert || !pattern || !s) return;
ls = strlen(s);
lp = strlen(pattern);
li = strlen(insert);
if (!*pattern) return;
while (lp <= ls && (p = strstr(s,pattern))) {
memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
memcpy(p, insert, li);
s = p + li;
ls += (li-lp);
}
}
void single_string_sub(char *s,const char *pattern,const char *insert)
{
char *p;
size_t ls,lp,li;
if (!insert || !pattern || !s) return;
ls = strlen(s);
lp = strlen(pattern);
li = strlen(insert);
if (!*pattern) return;
p = strstr(s,pattern);
memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
memcpy(p, insert, li);
s = p + li;
ls += (li-lp);
}
/****************************************************************************
Get the next token from a string, return False if none found
handles double-quotes.
Based on a routine by [email protected].
Extensively modified by [email protected]
****************************************************************************/
BOOL next_token(char **ptr,char *buff,char *sep)
{
static char *last_ptr=NULL;
char *s;
BOOL quoted;
if (!ptr) ptr = &last_ptr;
if (!ptr) return(False);
s = *ptr;
/* default to simple separators */
if (!sep) sep = " \t\n\r";
/* find the first non sep char */
while(*s && strchr(sep,*s)) s++;
/* nothing left? */
if (! *s) return(False);
/* copy over the token */
for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++) {
if (*s == '\"')
quoted = !quoted;
else
*buff++ = *s;
}
*ptr = (*s) ? s+1 : s;
*buff = 0;
last_ptr = *ptr;
return(True);
}
/*
return a timeval for the current time
*/
struct timeval timeval_current(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv;
}
/*
return the number of seconds elapsed since a given time
*/
double timeval_elapsed(struct timeval *tv)
{
struct timeval tv2 = timeval_current();
return (tv2.tv_sec - tv->tv_sec) +
(tv2.tv_usec - tv->tv_usec)*1.0e-6;
}
/*
return the number of seconds elapsed since a given time
*/
double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
{
return (tv2->tv_sec - tv1->tv_sec) +
(tv2->tv_usec - tv1->tv_usec)*1.0e-6;
}
/**
Sleep for a specified number of milliseconds.
**/
void msleep(unsigned int t)
{
struct timeval tval;
tval.tv_sec = t/1000;
tval.tv_usec = 1000*(t%1000);
/* this should be the real select - do NOT replace
with sys_select() */
select(0,NULL,NULL,NULL,&tval);
}