ChangeLog: Added the bugfix with `operating_mode'.
[collectd.git] / src / libping / util.c
1 /**
2  * UTILITY module
3  *
4  * Copyright (C) 2001 Jeffrey Fulmer <jdfulmer@armstrong.com>
5  * This file is part of LIBPING
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif /* HAVE_CONFIG_H */
25
26 #include <util.h>
27
28 #ifdef STDC_HEADERS
29 # include <string.h>
30 #else
31 # ifndef HAVE_STRCHR
32 #  define strchr index
33 #  define strrchr rindex
34 # endif /* HAVE_STRCHR */
35 char *strchr (), *strrchr ();
36 # ifndef HAVE_MEMCPY
37 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
38 #  define memmove(d, s, n) bcopy ((s), (d), (n))
39 # endif /* HAVE_MEMCPY  */
40 #endif  /* STDC_HEADERS */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include "memory.h"
48
49 /**
50  * elapsed_time
51  * returns an int value for the difference
52  * between now and starttime in milliseconds.
53  */
54 int
55 elapsed_time( struct timeval *starttime ){
56   struct timeval *newtime;
57   int elapsed;
58   newtime = (struct timeval*)malloc( sizeof(struct timeval));
59   gettimeofday(newtime,NULL);
60   elapsed = 0;
61   
62   if(( newtime->tv_usec - starttime->tv_usec) > 0 ){
63     elapsed += (newtime->tv_usec - starttime->tv_usec)/1000 ;
64   }
65   else{
66     elapsed += ( 1000000 + newtime->tv_usec - starttime->tv_usec ) /1000;
67     newtime->tv_sec--;
68   }
69   if(( newtime->tv_sec - starttime->tv_sec ) > 0 ){
70     elapsed += 1000 * ( newtime->tv_sec - starttime->tv_sec );
71   }
72   if( elapsed < 1 )
73     elapsed = 1;
74   
75   free( newtime );
76   return( elapsed );
77
78
79 /**
80  * substring        
81  * returns a char pointer from int start 
82  * to int length.
83  */
84 char * 
85 substring (char *buffer, int start, int length)
86 {
87   char *sub;
88   sub = xmalloc (sizeof (char) * (length + 1));
89         
90   if ((length < 1) || (start < 0) || (start > strlen (buffer)))
91     return NULL;
92
93   if (strlen (buffer) < length){
94     sub = (char*) strdup (buffer);
95     return buffer;
96   }
97
98   if (sub == NULL){
99     perror( "insufficient memory." );
100     exit( 1 );
101   }
102
103   memset (sub, 0, length + 1);
104
105   buffer += start;
106   memcpy (sub, buffer, length);
107
108   return sub;
109 }
110