0ce082182c33432b9c321fa891a3675f9e2c1ded
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / dlutils.c
1 /* dlutils.c - handy functions and definitions for dl_*.xs files
2  *
3  * Currently this file is simply #included into dl_*.xs/.c files.
4  * It should really be split into a dlutils.h and dlutils.c
5  *
6  */
7
8
9 /* pointer to allocated memory for last error message */
10 static char *LastError  = (char*)NULL;
11
12
13
14 #ifdef DEBUGGING
15 /* currently not connected to $DynaLoader::dl_error but should be */
16 static int dl_debug = 0;
17 #define DLDEBUG(level,code)     if(dl_debug>=level){ code; }
18 #else
19 #define DLDEBUG(level,code)
20 #endif
21
22
23 static void
24 dl_generic_private_init()       /* called by dl_*.xs dl_private_init() */
25 {
26 #ifdef DEBUGGING
27     char *perl_dl_debug = getenv("PERL_DL_DEBUG");
28     if (perl_dl_debug)
29         dl_debug = atoi(perl_dl_debug);
30 #endif
31 }
32
33
34 /* SaveError() takes printf style args and saves the result in LastError */
35 #ifdef STANDARD_C
36 static void
37 SaveError(char* pat, ...)
38 #else
39 /*VARARGS0*/
40 static void
41 SaveError(pat, va_alist)
42     char *pat;
43     va_dcl
44 #endif
45 {
46     va_list args;
47     char *message;
48     int len;
49
50     /* This code is based on croak/warn but I'm not sure where mess() */
51     /* gets its buffer space from! */
52
53 #ifdef I_STDARG
54     va_start(args, pat);
55 #else
56     va_start(args);
57 #endif
58     message = mess(pat, &args);
59     va_end(args);
60
61     len = strlen(message) + 1 ; /* include terminating null char */
62
63     /* Allocate some memory for the error message */
64     if (LastError)
65         LastError = (char*)saferealloc(LastError, len) ;
66     else
67         LastError = safemalloc(len) ;
68
69     /* Copy message into LastError (including terminating null char)    */
70     strncpy(LastError, message, len) ;
71     DLDEBUG(2,fprintf(stderr,"DynaLoader: stored error msg '%s'\n",LastError));
72 }
73
74
75 /* prepend underscore to s. write into buf. return buf. */
76 char *
77 dl_add_underscore(s, buf)
78 char *s;
79 char *buf;
80 {
81     *buf = '_';
82     (void)strcpy(buf + 1, s);
83     return buf;
84 }
85