/** * br_extract_prefix: * path: The full path of an executable or library. * Returns: The prefix, or NULL on error. This string should be freed when no longer needed. * * Extracts the prefix from path. This function assumes that your executable * or library is installed in an LSB-compatible directory structure. * * Example: * br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr" * br_extract_prefix ("/usr/local/lib/libfoo.so"); --> Returns "/usr/local" * br_extract_prefix ("/usr/local/libfoo.so"); --> Returns "/usr" */ char * br_extract_prefix (const char *path) { const char *end; char *tmp, *result; br_return_val_if_fail (path != (char *) NULL, (char *) NULL); if (!*path) return strdup ("/"); end = strrchr (path, '/'); if (!end) return strdup (path); tmp = br_strndup ((char *) path, end - path); if (!*tmp) { free (tmp); return strdup ("/"); } end = strrchr (tmp, '/'); if (!end) return tmp; result = br_strndup (tmp, end - tmp); free (tmp); if (!*result) { free (result); result = strdup ("/"); } return result; }
/** * br_locate: * symbol: A symbol that belongs to the app/library you want to locate. * Returns: A newly allocated string containing the full path of the * app/library that func belongs to, or NULL on error. This * string should be freed when not when no longer needed. * * Finds out to which application or library symbol belongs, then locate * the full path of that application or library. * Note that symbol cannot be a pointer to a function. That will not work. * * Example: * --> main.c * #include "prefix.h" * #include "libfoo.h" * * int main (int argc, char *argv[]) { * printf ("Full path of this app: %s\n", br_locate (&argc)); * libfoo_start (); * return 0; * } * * --> libfoo.c starts here * #include "prefix.h" * * void libfoo_start () { * --> "" is a symbol that belongs to libfoo (because it's called * --> from libfoo_start()); that's why this works. * printf ("libfoo is located in: %s\n", br_locate ("")); * } */ char * br_locate (void *symbol) { char line[5000]; FILE *f; char *path; br_return_val_if_fail (symbol != NULL, NULL); f = fopen ("/proc/self/maps", "r"); if (!f) { if (fallback_func) return fallback_func(symbol, fallback_data); else return NULL; } while (!feof (f)) { unsigned long start, end; if (!fgets (line, sizeof (line), f)) continue; if (!strstr (line, " r-xp ") || !strchr (line, '/')) continue; sscanf (line, "%lx-%lx ", &start, &end); if (symbol >= (void *) start && symbol < (void *) end) { char *tmp; size_t len; /* Extract the filename; it is always an absolute path */ path = strchr (line, '/'); /* Get rid of the newline */ tmp = strrchr (path, '\n'); if (tmp) *tmp = 0; /* Get rid of "(deleted)" */ len = strlen (path); if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0) { tmp = path + len - 10; *tmp = 0; } fclose(f); return strdup (path); } } fclose (f); return NULL; }
/** * br_prepend_prefix: * symbol: A symbol that belongs to the app/library you want to locate. * path: The path that you want to prepend the prefix to. * Returns: The new path, or NULL on error. This string should be freed when no * longer needed. * * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path. * Note that symbol cannot be a pointer to a function. That will not work. * * Example: * --> The application is /usr/bin/foo * br_prepend_prefix (&argc, "/share/foo/data.png"); --> Returns "/usr/share/foo/data.png" */ char * br_prepend_prefix (void *symbol, const char *path) { char *tmp, *newpath; br_return_val_if_fail (symbol != NULL, NULL); br_return_val_if_fail (path != NULL, NULL); tmp = br_locate_prefix (symbol); if (!tmp) return NULL; if (strcmp (tmp, "/") == 0) newpath = strdup (path); else newpath = br_strcat (tmp, path); /* Get rid of compiler warning ("br_prepend_prefix never used") */ if (0) br_prepend_prefix (NULL, NULL); free (tmp); return newpath; }
/** * br_locate_prefix: * symbol: A symbol that belongs to the app/library you want to locate. * Returns: A prefix. This string should be freed when no longer needed. * * Locates the full path of the app/library that symbol belongs to, and return * the prefix of that path, or NULL on error. * Note that symbol cannot be a pointer to a function. That will not work. * * Example: * --> This application is located in /usr/bin/foo * br_locate_prefix (&argc); --> returns: "/usr" */ char * br_locate_prefix (void *symbol) { char *path, *prefix; br_return_val_if_fail (symbol != NULL, NULL); path = br_locate (symbol); if (!path) return NULL; prefix = br_extract_prefix (path); free (path); return prefix; }
/* Emulates glibc's strndup() */ static char * br_strndup (char *str, size_t size) { char *result = NULL; size_t len; br_return_val_if_fail (str != NULL, NULL); len = strlen (str); if (!len) return strdup (""); result = (char *) calloc (sizeof (char), len + 1); memcpy (result, str, size); return result; }
/** * br_extract_dir: * path: A path. * Returns: A directory name. This string should be freed when no longer needed. * * Extracts the directory component of path. Similar to g_dirname() or the dirname * commandline application. * * Example: * br_extract_dir ("/usr/local/foobar"); --> Returns: "/usr/local" */ char * br_extract_dir (const char *path) { char *end, *result; br_return_val_if_fail (path != (char *) NULL, (char *) NULL); end = strrchr (path, '/'); if (!end) return strdup ("."); while (end > path && *end == '/') end--; result = br_strndup ((char *) path, end - path + 1); if (!*result) { free (result); return strdup ("/"); } else return result; }