| 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 | * Mupen64plus-ui-console - osal_files_unix.c * |
| 3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
| 4 | * Copyright (C) 2009 Richard Goedeken * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 20 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 21 | |
| 22 | /* This implements all kinds of system-dependent file handling |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <dirent.h> // for opendir(), readdir(), closedir() |
| 31 | |
| 32 | #include "main.h" |
| 33 | #include "m64p_types.h" |
| 34 | #include "osal_preproc.h" |
| 35 | #include "osal_files.h" |
| 36 | |
| 37 | /* definitions for system directories to search when looking for mupen64plus plugins */ |
| 38 | #if defined(PLUGINDIR) |
| 39 | const int osal_libsearchdirs = 4; |
| 40 | const char *osal_libsearchpath[4] = { PLUGINDIR, "/usr/local/lib/mupen64plus", "/usr/lib/mupen64plus", "./" }; |
| 41 | #else |
| 42 | const int osal_libsearchdirs = 3; |
| 43 | const char *osal_libsearchpath[3] = { "/usr/local/lib/mupen64plus", "/usr/lib/mupen64plus", "./" }; |
| 44 | #endif |
| 45 | |
| 46 | osal_lib_search *osal_library_search(const char *searchpath) |
| 47 | { |
| 48 | osal_lib_search *head = NULL, *curr = NULL; |
| 49 | DIR *dir; |
| 50 | struct dirent *entry; |
| 51 | |
| 52 | #ifdef __APPLE__ |
| 53 | const char* suffix = ".dylib"; |
| 54 | #else |
| 55 | const char* suffix = ".so"; |
| 56 | #endif |
| 57 | |
| 58 | dir = opendir(searchpath); |
| 59 | if (dir == NULL) |
| 60 | return NULL; |
| 61 | |
| 62 | /* look for any shared libraries in this folder */ |
| 63 | while ((entry = readdir(dir)) != NULL) |
| 64 | { |
| 65 | osal_lib_search *newlib = NULL; |
| 66 | if (strcmp(entry->d_name + strlen(entry->d_name) - strlen(suffix), suffix) != 0) |
| 67 | continue; |
| 68 | /* this is a .so file, so add it to the list */ |
| 69 | newlib = malloc(sizeof(osal_lib_search)); |
| 70 | if (newlib == NULL) |
| 71 | { |
| 72 | DebugMessage(M64MSG_ERROR, "Memory allocation error in osal_library_search()!"); |
| 73 | osal_free_lib_list(head); |
| 74 | closedir(dir); |
| 75 | return NULL; |
| 76 | } |
| 77 | if (head == NULL) |
| 78 | { |
| 79 | head = curr = newlib; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | curr->next = newlib; |
| 84 | curr = newlib; |
| 85 | } |
| 86 | /* set up the filepath and filename members */ |
| 87 | strncpy(curr->filepath, searchpath, PATH_MAX-2); |
| 88 | curr->filepath[PATH_MAX-2] = 0; |
| 89 | if (curr->filepath[strlen(curr->filepath)-1] != '/') |
| 90 | strcat(curr->filepath, "/"); |
| 91 | int pathlen = strlen(curr->filepath); |
| 92 | curr->filename = curr->filepath + pathlen; |
| 93 | strncat(curr->filepath, entry->d_name, PATH_MAX - pathlen - 1); |
| 94 | curr->filepath[PATH_MAX-1] = 0; |
| 95 | /* set plugin_type and next pointer */ |
| 96 | curr->plugin_type = 0; |
| 97 | curr->next = NULL; |
| 98 | } |
| 99 | |
| 100 | closedir(dir); |
| 101 | return head; |
| 102 | } |
| 103 | |
| 104 | void osal_free_lib_list(osal_lib_search *head) |
| 105 | { |
| 106 | while (head != NULL) |
| 107 | { |
| 108 | osal_lib_search *next = head->next; |
| 109 | free(head); |
| 110 | head = next; |
| 111 | } |
| 112 | } |
| 113 | |