remove an invalid comment
[amitay/dbench.git] / util.c
1 /* 
2    dbench version 2
3    Copyright (C) Andrew Tridgell 1999
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "dbench.h"
20
21 #ifndef SHM_W
22 #define SHM_W 0000200
23 #endif
24
25 #ifndef SHM_R
26 #define SHM_R 0000400
27 #endif
28
29 /* return a pointer to a anonymous shared memory segment of size "size"
30    which will persist across fork() but will disappear when all processes
31    exit 
32
33    The memory is zeroed 
34
35    This function uses system5 shared memory. It takes advantage of a property
36    that the memory is not destroyed if it is attached when the id is removed
37    */
38 void *shm_setup(int size)
39 {
40         int shmid;
41         void *ret;
42
43         shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
44         if (shmid == -1) {
45                 printf("can't get private shared memory of %d bytes: %s\n",
46                        size, 
47                        strerror(errno));
48                 exit(1);
49         }
50         ret = (void *)shmat(shmid, 0, 0);
51         if (!ret || ret == (void *)-1) {
52                 printf("can't attach to shared memory\n");
53                 return NULL;
54         }
55         /* the following releases the ipc, but note that this process
56            and all its children will still have access to the memory, its
57            just that the shmid is no longer valid for other shm calls. This
58            means we don't leave behind lots of shm segments after we exit 
59
60            See Stevens "advanced programming in unix env" for details
61            */
62         shmctl(shmid, IPC_RMID, 0);
63
64         memset(ret, 0, size);
65         
66         return ret;
67 }
68
69 /****************************************************************************
70 similar to string_sub() but allows for any character to be substituted. 
71 Use with caution!
72 ****************************************************************************/
73 void all_string_sub(char *s,const char *pattern,const char *insert)
74 {
75         char *p;
76         size_t ls,lp,li;
77
78         if (!insert || !pattern || !s) return;
79
80         ls = strlen(s);
81         lp = strlen(pattern);
82         li = strlen(insert);
83
84         if (!*pattern) return;
85         
86         while (lp <= ls && (p = strstr(s,pattern))) {
87                 memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
88                 memcpy(p, insert, li);
89                 s = p + li;
90                 ls += (li-lp);
91         }
92 }
93
94
95 /****************************************************************************
96   Get the next token from a string, return False if none found
97   handles double-quotes. 
98 Based on a routine by GJC@VILLAGE.COM. 
99 Extensively modified by Andrew.Tridgell@anu.edu.au
100 ****************************************************************************/
101 BOOL next_token(char **ptr,char *buff,char *sep)
102 {
103         static char *last_ptr=NULL;
104         char *s;
105         BOOL quoted;
106         
107         if (!ptr) ptr = &last_ptr;
108         if (!ptr) return(False);
109         
110         s = *ptr;
111         
112         /* default to simple separators */
113         if (!sep) sep = " \t\n\r";
114         
115         /* find the first non sep char */
116         while(*s && strchr(sep,*s)) s++;
117         
118         /* nothing left? */
119         if (! *s) return(False);
120         
121         /* copy over the token */
122         for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++) {
123                 if (*s == '\"') 
124                         quoted = !quoted;
125                 else
126                         *buff++ = *s;
127         }
128         
129         *ptr = (*s) ? s+1 : s;  
130         *buff = 0;
131         last_ptr = *ptr;
132         
133         return(True);
134 }
135
136 /*
137   return a timeval for the current time
138 */
139 struct timeval timeval_current(void)
140 {
141         struct timeval tv;
142         gettimeofday(&tv, NULL);
143         return tv;
144 }
145
146 /*
147   return the number of seconds elapsed since a given time
148 */
149 double timeval_elapsed(struct timeval *tv)
150 {
151         struct timeval tv2 = timeval_current();
152         return (tv2.tv_sec - tv->tv_sec) + 
153                (tv2.tv_usec - tv->tv_usec)*1.0e-6;
154 }
155
156 /*
157   return the number of seconds elapsed since a given time
158 */
159 double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
160 {
161         return (tv2->tv_sec - tv1->tv_sec) + 
162                (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
163 }
164
165
166
167 /**
168  Sleep for a specified number of milliseconds.
169 **/
170 void msleep(unsigned int t)
171 {
172         struct timeval tval;  
173
174         tval.tv_sec = t/1000;
175         tval.tv_usec = 1000*(t%1000);
176         /* this should be the real select - do NOT replace
177            with sys_select() */
178         select(0,NULL,NULL,NULL,&tval);
179 }