3 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
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
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
38 void *shm_setup(int size)
43 shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
45 printf("can't get private shared memory of %d bytes: %s\n",
50 ret = (void *)shmat(shmid, 0, 0);
51 if (!ret || ret == (void *)-1) {
52 printf("can't attach to shared memory\n");
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
60 See Stevens "advanced programming in unix env" for details
62 shmctl(shmid, IPC_RMID, 0);
69 /****************************************************************************
70 similar to string_sub() but allows for any character to be substituted.
72 ****************************************************************************/
73 void all_string_sub(char *s,const char *pattern,const char *insert)
78 if (!insert || !pattern || !s) return;
84 if (!*pattern) return;
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);
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)
103 static char *last_ptr=NULL;
107 if (!ptr) ptr = &last_ptr;
108 if (!ptr) return(False);
112 /* default to simple separators */
113 if (!sep) sep = " \t\n\r";
115 /* find the first non sep char */
116 while(*s && strchr(sep,*s)) s++;
119 if (! *s) return(False);
121 /* copy over the token */
122 for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++) {
129 *ptr = (*s) ? s+1 : s;
137 return a timeval for the current time
139 struct timeval timeval_current(void)
142 gettimeofday(&tv, NULL);
147 return the number of seconds elapsed since a given time
149 double timeval_elapsed(struct timeval *tv)
151 struct timeval tv2 = timeval_current();
152 return (tv2.tv_sec - tv->tv_sec) +
153 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
157 return the number of seconds elapsed since a given time
159 double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
161 return (tv2->tv_sec - tv1->tv_sec) +
162 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
168 Sleep for a specified number of milliseconds.
170 void msleep(unsigned int t)
174 tval.tv_sec = t/1000;
175 tval.tv_usec = 1000*(t%1000);
176 /* this should be the real select - do NOT replace
178 select(0,NULL,NULL,NULL,&tval);