2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
19 #include <sys/types.h>
22 #include <cbtcommon/debug.h>
26 typedef int (*compare_func
)(const void *, const void *);
28 static void * string_tree
;
29 char *readfile(char const *filename
, char *buf
, size_t size
)
35 fp
= fopen(filename
, "r");
39 ptr
= fgets(buf
, size
, fp
);
46 if (buf
[len
-1] == '\n')
52 char *strrep(char *s
, char find
, char replace
)
68 static char prefix
[PATH_MAX
];
74 if (!(home
= getenv("HOME")))
76 debug(DEBUG_APPERROR
, "HOME environment variable not set");
80 if (snprintf(prefix
, PATH_MAX
, "%s/%s", home
, CVSPS_PREFIX
) >= PATH_MAX
)
82 debug(DEBUG_APPERROR
, "prefix buffer overflow");
86 /* Make sure the prefix directory exists */
87 if (stat(prefix
, &sbuf
) < 0)
90 ret
= mkdir(prefix
, 0777);
93 debug(DEBUG_SYSERROR
, "Cannot create the cvsps directory '%s'", CVSPS_PREFIX
);
99 if (!(S_ISDIR(sbuf
.st_mode
)))
100 debug(DEBUG_APPERROR
, "cvsps directory '%s' is not a directory!", CVSPS_PREFIX
);
106 char *xstrdup(char const *str
)
113 debug(DEBUG_ERROR
, "strdup failed");
120 void strzncpy(char * dst
, const char * src
, int n
)
122 strncpy(dst
, src
, n
);
126 char *get_string(char const *str
)
133 res
= (char **)tfind(str
, &string_tree
, (compare_func
)strcmp
);
136 char *key
= xstrdup(str
);
137 res
= (char **)tsearch(key
, &string_tree
, (compare_func
)strcmp
);
144 static int get_int_substr(const char * str
, const regmatch_t
* p
)
147 memcpy(buff
, str
+ p
->rm_so
, p
->rm_eo
- p
->rm_so
);
148 buff
[p
->rm_eo
- p
->rm_so
] = 0;
152 static time_t mktime_utc(struct tm
* tm
)
154 char * old_tz
= getenv("TZ");
157 setenv("TZ", "UTC", 1);
164 setenv("TZ", old_tz
, 1);
173 void convert_date(time_t * t
, const char * dte
)
175 static regex_t date_re
;
179 size_t nmatch
= MAX_MATCH
;
180 regmatch_t match
[MAX_MATCH
];
184 if (regcomp(&date_re
, "([0-9]{4})[-/]([0-9]{2})[-/]([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", REG_EXTENDED
))
186 fprintf(stderr
, "FATAL: date regex compilation error\n");
192 if (regexec(&date_re
, dte
, nmatch
, match
, 0) == 0)
194 regmatch_t
* pm
= match
;
197 /* first regmatch_t is match location of entire re */
200 tm
.tm_year
= get_int_substr(dte
, pm
++);
201 tm
.tm_mon
= get_int_substr(dte
, pm
++);
202 tm
.tm_mday
= get_int_substr(dte
, pm
++);
203 tm
.tm_hour
= get_int_substr(dte
, pm
++);
204 tm
.tm_min
= get_int_substr(dte
, pm
++);
205 tm
.tm_sec
= get_int_substr(dte
, pm
++);
210 *t
= mktime_utc(&tm
);
218 static struct timeval start_time
;
222 gettimeofday(&start_time
, NULL
);
225 void timing_stop(const char * msg
)
227 struct timeval stop_time
;
228 gettimeofday(&stop_time
, NULL
);
229 stop_time
.tv_sec
-= start_time
.tv_sec
;
230 stop_time
.tv_usec
-= start_time
.tv_usec
;
231 if (stop_time
.tv_usec
< 0)
232 stop_time
.tv_sec
--,stop_time
.tv_usec
+= 1000000;
234 printf("Elapsed time for %s: %d.%06d\n", msg
, (int)stop_time
.tv_sec
, (int)stop_time
.tv_usec
);
237 extern char ** environ
;
239 /* taken from the linux manual page for system */
240 int my_system (const char *command
)
253 argv
[2] = (char*)command
; /* discard const */
255 execve("/bin/sh", argv
, environ
);
259 if (waitpid(pid
, &status
, 0) == -1) {
267 int escape_filename(char * dst
, int len
, const char * src
)
269 static char * naughty_chars
= " \\\"'@<>=;|&()#$`?*[!:{";
273 while (len
> 1 && *src
)
275 if (strchr(naughty_chars
, *src
))
290 return (*src
== 0) ? 0 : -1;