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/>.
30 /* return a pointer to a anonymous shared memory segment of size "size"
31 which will persist across fork() but will disappear when all processes
36 This function uses system5 shared memory. It takes advantage of a property
37 that the memory is not destroyed if it is attached when the id is removed
39 void *shm_setup(int size)
44 shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
46 printf("can't get private shared memory of %d bytes: %s\n",
51 ret = (void *)shmat(shmid, 0, 0);
52 if (!ret || ret == (void *)-1) {
53 printf("can't attach to shared memory\n");
56 /* the following releases the ipc, but note that this process
57 and all its children will still have access to the memory, its
58 just that the shmid is no longer valid for other shm calls. This
59 means we don't leave behind lots of shm segments after we exit
61 See Stevens "advanced programming in unix env" for details
63 shmctl(shmid, IPC_RMID, 0);
70 /****************************************************************************
71 similar to string_sub() but allows for any character to be substituted.
73 ****************************************************************************/
74 void all_string_sub(char *s,const char *pattern,const char *insert)
79 if (!insert || !pattern || !s) return;
85 if (!*pattern) return;
87 while (lp <= ls && (p = strstr(s,pattern))) {
88 memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
89 memcpy(p, insert, li);
96 /****************************************************************************
97 Get the next token from a string, return False if none found
98 handles double-quotes.
99 Based on a routine by GJC@VILLAGE.COM.
100 Extensively modified by Andrew.Tridgell@anu.edu.au
101 ****************************************************************************/
102 BOOL next_token(char **ptr,char *buff,char *sep)
104 static char *last_ptr=NULL;
108 if (!ptr) ptr = &last_ptr;
109 if (!ptr) return(False);
113 /* default to simple separators */
114 if (!sep) sep = " \t\n\r";
116 /* find the first non sep char */
117 while(*s && strchr(sep,*s)) s++;
120 if (! *s) return(False);
122 /* copy over the token */
123 for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++) {
130 *ptr = (*s) ? s+1 : s;
138 return a timeval for the current time
140 struct timeval timeval_current(void)
143 gettimeofday(&tv, NULL);
148 return the number of seconds elapsed since a given time
150 double timeval_elapsed(struct timeval *tv)
152 struct timeval tv2 = timeval_current();
153 return (tv2.tv_sec - tv->tv_sec) +
154 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
158 return the number of seconds elapsed since a given time
160 double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
162 return (tv2->tv_sec - tv1->tv_sec) +
163 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
169 Sleep for a specified number of milliseconds.
171 void msleep(unsigned int t)
175 tval.tv_sec = t/1000;
176 tval.tv_usec = 1000*(t%1000);
177 /* this should be the real select - do NOT replace
179 select(0,NULL,NULL,NULL,&tval);