2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
13 #include <tsearch/search.h>
24 #include <sys/types.h>
29 #include <cbtcommon/debug.h>
33 typedef int (*compare_func
)(const void *, const void *);
35 static void * string_tree
;
36 char *readfile(char const *filename
, char *buf
, size_t size
)
42 fp
= fopen(filename
, "r");
46 ptr
= fgets(buf
, size
, fp
);
53 if (buf
[len
-1] == '\n')
59 char *strrep(char *s
, char find
, char replace
)
75 static char prefix
[PATH_MAX
];
81 if (!(home
= getenv("HOME")))
83 debug(DEBUG_APPERROR
, "HOME environment variable not set");
87 if (snprintf(prefix
, PATH_MAX
, "%s/%s", home
, CVSPS_PREFIX
) >= PATH_MAX
)
89 debug(DEBUG_APPERROR
, "prefix buffer overflow");
93 /* Make sure the prefix directory exists */
94 if (stat(prefix
, &sbuf
) < 0)
100 ret
= mkdir(prefix
, 0777);
104 debug(DEBUG_SYSERROR
, "Cannot create the cvsps directory '%s'", CVSPS_PREFIX
);
110 if (!(S_ISDIR(sbuf
.st_mode
)))
111 debug(DEBUG_APPERROR
, "cvsps directory '%s' is not a directory!", CVSPS_PREFIX
);
117 char *xstrdup(char const *str
)
124 debug(DEBUG_ERROR
, "strdup failed");
131 void strzncpy(char * dst
, const char * src
, int n
)
133 strncpy(dst
, src
, n
);
137 char *get_string(char const *str
)
144 res
= (char **)tfind(str
, &string_tree
, (compare_func
)strcmp
);
147 char *key
= xstrdup(str
);
148 res
= (char **)tsearch(key
, &string_tree
, (compare_func
)strcmp
);
155 static int get_int_substr(const char * str
, const regmatch_t
* p
)
158 memcpy(buff
, str
+ p
->rm_so
, p
->rm_eo
- p
->rm_so
);
159 buff
[p
->rm_eo
- p
->rm_so
] = 0;
163 static time_t mktime_utc(struct tm
* tm
)
165 char * old_tz
= getenv("TZ");
168 setenv("TZ", "UTC", 1);
175 setenv("TZ", old_tz
, 1);
184 void convert_date(time_t * t
, const char * dte
)
186 static regex_t date_re
;
190 size_t nmatch
= MAX_MATCH
;
191 regmatch_t match
[MAX_MATCH
];
195 if (regcomp(&date_re
, "([0-9]{4})[-/]([0-9]{2})[-/]([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", REG_EXTENDED
))
197 fprintf(stderr
, "FATAL: date regex compilation error\n");
203 if (regexec(&date_re
, dte
, nmatch
, match
, 0) == 0)
205 regmatch_t
* pm
= match
;
208 /* first regmatch_t is match location of entire re */
211 tm
.tm_year
= get_int_substr(dte
, pm
++);
212 tm
.tm_mon
= get_int_substr(dte
, pm
++);
213 tm
.tm_mday
= get_int_substr(dte
, pm
++);
214 tm
.tm_hour
= get_int_substr(dte
, pm
++);
215 tm
.tm_min
= get_int_substr(dte
, pm
++);
216 tm
.tm_sec
= get_int_substr(dte
, pm
++);
221 *t
= mktime_utc(&tm
);
229 static struct timeval start_time
;
233 gettimeofday(&start_time
, NULL
);
236 void timing_stop(const char * msg
)
238 struct timeval stop_time
;
239 gettimeofday(&stop_time
, NULL
);
240 stop_time
.tv_sec
-= start_time
.tv_sec
;
241 stop_time
.tv_usec
-= start_time
.tv_usec
;
242 if (stop_time
.tv_usec
< 0)
243 stop_time
.tv_sec
--,stop_time
.tv_usec
+= 1000000;
245 printf("Elapsed time for %s: %d.%06d\n", msg
, (int)stop_time
.tv_sec
, (int)stop_time
.tv_usec
);
248 extern char ** environ
;
250 /* taken from the linux manual page for system
251 See http://linux.die.net/man/3/system
253 int my_system (const char *command
)
256 /* Launch child process */
258 ZeroMemory( &si
, sizeof( si
) );
259 si
.cb
= sizeof( si
);
260 PROCESS_INFORMATION pi
;
261 ZeroMemory( &pi
, sizeof( pi
) );
263 0, /* module name (0 means use first token in command line) */
264 (char*)command
, /* command line */
265 0, /* process security attributes */
266 0, /* primary thread security attributes */
267 TRUE
, /* handles are inherited */
268 0, /* creation flags (none) */
269 0, /* use parent's environment */
270 0, /* use parent's current directory */
271 &si
, /* STARTUPINFO pointer */
272 &pi
/* receives PROCESS_INFORMATION */
274 debug(DEBUG_APPERROR
, "CreateProcess failed.\n command line was: %s", command
);
278 /* Wait for child process to exit */
279 DWORD timeout
= 10*1000; /* milliseconds */
280 DWORD ret
= WaitForSingleObject( pi
.hProcess
, timeout
);
281 if (ret
== WAIT_OBJECT_0
) {
282 /* OK. Child has exited */
284 else if (ret
== WAIT_FAILED
) {
285 DWORD last_err
= GetLastError();
286 debug(DEBUG_APPERROR
, "WaitForSingleObject failed.\n GetLastError returned : %d", last_err
);
290 debug(DEBUG_APPERROR
, "WaitForSingleObject returned %x", ret
);
294 /* Retrieve exit code */
296 if( !GetExitCodeProcess( pi
.hProcess
, &exit_code
) ) {
297 DWORD last_err
= GetLastError();
298 debug(DEBUG_APPERROR
, "GetExitCodeProcess failed.\n GetLastError returned : %d", last_err
);
315 argv
[2] = (char*)command
; /* discard const */
317 execve("/bin/sh", argv
, environ
);
321 if (waitpid(pid
, &status
, 0) == -1) {
330 int escape_filename(char * dst
, int len
, const char * src
)
332 static char * naughty_chars
= " \\\"'@<>=;|&()#$`?*[!:{";
336 while (len
> 1 && *src
)
338 if (strchr(naughty_chars
, *src
))
353 return (*src
== 0) ? 0 : -1;