351
352 if (errno != 0) {
353 /* C runtime error that has no corresponding DOS error code */
354 const char *s;
355 int n;
356
357 s = strerror(errno);
358 n = (int)strlen(s);
359 if (n >= len) {
360 n = len - 1;
361 }
362 (void)strncpy(buf, s, n);
363 buf[n] = '\0';
364 return n;
365 }
366
367 return 0;
368 }
369
370 static void dll_build_name(char* buffer, size_t buflen,
371 const char* pname, const char* fname) {
372 // Loosley based on os_windows.cpp
373
374 char *pathname = (char *)pname;
375 *buffer = '\0';
376 while (strlen(pathname) > 0) {
377 char *p = strchr(pathname, ';');
378 if (p == NULL) {
379 p = pathname + strlen(pathname);
380 }
381 /* check for NULL path */
382 if (p == pathname) {
383 continue;
384 }
385 if (*(p-1) == ':' || *(p-1) == '\\') {
386 (void)_snprintf(buffer, buflen, "%.*s%s.dll", (int)(p - pathname),
387 pathname, fname);
388 } else {
389 (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (int)(p - pathname),
390 pathname, fname);
391 }
392 if (_access(buffer, 0) == 0) {
393 break;
394 }
395 if (*p == '\0') {
396 pathname = p;
397 } else {
398 pathname = p + 1;
399 }
400 *buffer = '\0';
401 }
402 }
403
404 /* Build a machine dependent library name out of a path and file name. */
405 void
406 md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
407 {
408 int pnamelen;
409
410 pnamelen = pname ? (int)strlen(pname) : 0;
411
412 *holder = '\0';
413 /* Quietly truncates on buffer overflow. Should be an error. */
414 if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
415 return;
416 }
417
418 if (pnamelen == 0) {
419 sprintf(holder, "%s.dll", fname);
420 } else {
421 dll_build_name(holder, holderlen, pname, fname);
422 }
423 }
424
425 void *
426 md_load_library(const char * name, char *err_buf, int err_buflen)
|
351
352 if (errno != 0) {
353 /* C runtime error that has no corresponding DOS error code */
354 const char *s;
355 int n;
356
357 s = strerror(errno);
358 n = (int)strlen(s);
359 if (n >= len) {
360 n = len - 1;
361 }
362 (void)strncpy(buf, s, n);
363 buf[n] = '\0';
364 return n;
365 }
366
367 return 0;
368 }
369
370 static void dll_build_name(char* buffer, size_t buflen,
371 const char* paths, const char* fname) {
372 char *path, *paths_copy, *next_token;
373
374 paths_copy = strdup(paths);
375 if (paths_copy == NULL) {
376 return;
377 }
378
379 next_token = NULL;
380 path = strtok_s(paths_copy, ";", &next_token);
381
382 while(path != NULL) {
383 _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
384 if (_access(buffer, 0) == 0) {
385 break;
386 }
387 *buffer = '\0';
388 path = strtok_s(NULL, ";", &next_token);
389 }
390
391 free(paths_copy);
392 }
393
394 /* Build a machine dependent library name out of a path and file name. */
395 void
396 md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)
397 {
398 int pnamelen;
399
400 pnamelen = pname ? (int)strlen(pname) : 0;
401
402 *holder = '\0';
403 /* Quietly truncates on buffer overflow. Should be an error. */
404 if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
405 return;
406 }
407
408 if (pnamelen == 0) {
409 sprintf(holder, "%s.dll", fname);
410 } else {
411 dll_build_name(holder, holderlen, pname, fname);
412 }
413 }
414
415 void *
416 md_load_library(const char * name, char *err_buf, int err_buflen)
|