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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/* Josh Pieper, (c) 2000 */
/* This file is distributed under the GPL, see file COPYING for details */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#ifndef WIN32
#include <unistd.h>
#endif
#ifdef MCHECK
#include <mcheck.h>
#endif
#include "gnut_lib.h"
#include "gnut_threads.h"
int gnut_mprobe(void *d)
{
#ifdef MCHECK
return mprobe(d);
#else
return 0;
#endif
}
#ifndef HAVE_XMALLOC
#ifndef xmalloc
char *xmalloc(int size)
{
char *ptr;
ptr=malloc(size);
if (!ptr) { g_debug(0,"malloc(%d) failed!\n", size); exit(1); }
return ptr;
}
#endif
#endif
char *expand_path(char *a)
{
char *b;
if (strncmp(a,"~/", 2)==0) {
#ifndef WIN32
char *c;
c=getenv("HOME");
#else
char c[MAX_PATH];
GetWindowsDirectory(c, MAX_PATH);
#endif
b=(char *)xmalloc(strlen(a)+strlen(c)+1);
strcpy(b, c);
strcat(b, &a[1]);
return b;
}
b=(char *) xmalloc(strlen(a)+1);
strcpy(b,a);
return b;
}
int gnut_lib_debug=0;
#ifndef PTHREADS_DRAFT4
pthread_mutex_t _g_debug_mutex=PTHREAD_MUTEX_INITIALIZER;
#else
pthread_mutex_t _g_debug_mutex;
#endif
#ifndef WIN32
void _g_debug(char *file, int line, int num, char *format, ...)
{
va_list argp;
if (gnut_lib_debug>=num) {
pthread_mutex_lock(&_g_debug_mutex);
fprintf(stderr,"%i %s:%i > ",
getpid(),file,line);
fflush(stdout);
va_start(argp,format);
vfprintf(stderr,format,argp);
va_end(argp);
pthread_mutex_unlock(&_g_debug_mutex);
}
}
#else
void g_debug(int a, char *format, ...)
{
va_list argp;
if (gnut_lib_debug>=a) {
pthread_mutex_lock(&_g_debug_mutex);
fprintf(stderr,"%li > ", (long) pthread_self());
va_start(argp,format);
vfprintf(stderr,format,argp);
va_end(argp);
pthread_mutex_unlock(&_g_debug_mutex);
}
}
#endif
char *gnut_strdelimit(char *string, char *delim, char new_delim)
{
char *c;
for (c=string;*c;c++) {
if (strchr(delim,*c)) *c=new_delim;
}
return string;
}
|