[go: up one dir, main page]

Menu

[r1]: / src / auxiliar.c  Maximize  Restore  History

Download this file

72 lines (51 with data), 1.6 kB

 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
/***************************************************************************
auxiliar.c - description
-------------------
begin : Tue Aug 26 2003
copyright : (C) 2003 by Instituto Tecnologico de Informatica (ITI)
email : tt2iti@iti.upv.es
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "auxiliar.h"
/* Check if memory was allocated to pointer ptr */
void memtest(void *ptr)
{
if (ptr==NULL) {
fprintf(stderr,"%s: insufficient memory\n",__FILE__);
exit(EXIT_FAILURE);
}
}
double elapsed_time(timeval_t *t1, timeval_t *t2){
long sec;
long usec;
sec = (*t2).tv_sec - (*t1).tv_sec;
if ((*t1).tv_usec < (*t2).tv_usec)
usec = (*t2).tv_usec - (*t1).tv_usec;
else{
usec = 1000000 - (*t1).tv_usec + (*t2).tv_usec;
sec--;
}
return (double) sec + ((double) usec / 1000000);
}
#ifdef WIN
char* strdup(const char* string){
char* string_copy=NULL;
if(string!=NULL){
memtest(string_copy=malloc(sizeof(char)*(strlen(string)+1)));
memcpy(string_copy,string, sizeof(char)*(strlen(string)+1));
}
return string_copy;
}
char* strndup(const char* string, int length){
char* string_copy=NULL;
if(string!=NULL){
memtest(string_copy=malloc(sizeof(char)*(length+1)));
memcpy(string_copy,string, sizeof(char)*(length));
string_copy[length]='\0';
}
return string_copy;
}
#endif