/** * 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_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; }
/** Extracts the directory component of a path. * * Similar to g_dirname() or the dirname commandline application. * * Example: * \code * br_dirname ("/usr/local/foobar"); --> Returns: "/usr/local" * \endcode * * @param path A path. * @returns A directory name. This string should be freed when no longer needed. */ char * br_dirname (const char *path) { char *end, *result; if (path == (const char *) NULL) return (char *) NULL; end = strrchr (path, '/'); if (end == (const char *) NULL) return strdup ("."); while (end > path && *end == '/') end--; result = br_strndup (path, end - path + 1); if (result[0] == 0) { free (result); return strdup ("/"); } else return result; }