#include <stdio.h>
#include <glib.h>
#include <errno.h>
-#include <sys/types.h>
-#ifdef G_OS_UNIX
-#include <pthread.h>
-#endif
-
-#ifdef HAVE_PWD_H
-#include <pwd.h>
+#ifdef G_OS_WIN32
+#include <direct.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef G_OS_WIN32
-#include <direct.h>
-#include <io.h>
-#endif
-
-
gchar *
g_build_path (const gchar *separator, const gchar *first_element, ...)
{
va_list args;
g_return_val_if_fail (separator != NULL, NULL);
- g_return_val_if_fail (first_element != NULL, NULL);
+
+ if (first_element == NULL)
+ return g_strdup ("");
result = g_string_sized_new (48);
g_string_append_len (result, s, p - s);
if (next && *next){
- g_string_append (result, separator);
+ if (strncmp (separator, result->str + strlen (result->str) - slen, slen))
+ g_string_append (result, separator);
for (; strncmp (next, separator, slen) == 0; )
next += slen;
return g_strdup (&r[1]);
}
-gboolean
-g_path_is_absolute (const char *filename)
+#ifndef HAVE_STRTOK_R
+// This is from BSD's strtok_r
+
+char *
+strtok_r(char *s, const char *delim, char **last)
{
- g_return_val_if_fail (filename != NULL, FALSE);
-#ifdef G_OS_WIN32
- if (filename[0] != '\0' && filename[1] != '\0' && filename[1] == ':' &&
- filename[2] != '\0' && filename[2] == '\\')
- return TRUE;
- else
- return FALSE;
-#else
- return (*filename == '/');
-#endif
+ char *spanp;
+ int c, sc;
+ char *tok;
+
+ if (s == NULL && (s = *last) == NULL)
+ return NULL;
+
+ /*
+ * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
+ */
+cont:
+ c = *s++;
+ for (spanp = (char *)delim; (sc = *spanp++) != 0; ){
+ if (c == sc)
+ goto cont;
+ }
+
+ if (c == 0){ /* no non-delimiter characters */
+ *last = NULL;
+ return NULL;
+ }
+ tok = s - 1;
+
+ /*
+ * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
+ * Note that delim must have one NUL; we stop if we see that, too.
+ */
+ for (;;){
+ c = *s++;
+ spanp = (char *)delim;
+ do {
+ if ((sc = *spanp++) == c) {
+ if (c == 0)
+ s = NULL;
+ else {
+ char *w = s - 1;
+ *w = '\0';
+ }
+ *last = s;
+ return tok;
+ }
+ }
+ while (sc != 0);
+ }
+ /* NOTREACHED */
}
+#endif
gchar *
g_find_program_in_path (const gchar *program)
char *x = p, *l;
gchar *curdir = NULL;
char *save;
+#ifdef G_OS_WIN32
+ char *program_exe;
+ char *suffix_list[5] = {".exe",".cmd",".bat",".com",NULL};
+ int listx;
+ gboolean hasSuffix;
+#endif
g_return_val_if_fail (program != NULL, NULL);
x = curdir;
}
+#ifdef G_OS_WIN32
+ /* see if program already has a suffix */
+ listx = 0;
+ hasSuffix = FALSE;
+ while (!hasSuffix && suffix_list[listx]) {
+ hasSuffix = g_str_has_suffix(program,suffix_list[listx++]);
+ }
+#endif
+
while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
char *probe_path;
return probe_path;
}
g_free (probe_path);
- }
- g_free (curdir);
- g_free (p);
- return NULL;
-}
-
-gchar *
-g_get_current_dir (void)
-{
- int s = 32;
- char *buffer = NULL, *r;
- gboolean fail;
-
- do {
- buffer = g_realloc (buffer, s);
- r = getcwd (buffer, s);
- fail = (r == NULL && errno == ERANGE);
- if (fail) {
- s <<= 1;
- }
- } while (fail);
-
- return r;
-}
-
-#if defined (G_OS_UNIX)
-
-static pthread_mutex_t home_lock = PTHREAD_MUTEX_INITIALIZER;
-static const gchar *home_dir;
-
-/* Give preference to /etc/passwd than HOME */
-const gchar *
-g_get_home_dir (void)
-{
- if (home_dir == NULL){
- pthread_mutex_lock (&home_lock);
- if (home_dir == NULL){
-#ifdef HAVE_GETPWENT_R
- struct passwd pwbuf, *track;
- char buf [4096];
- uid_t uid;
-
- uid = getuid ();
-
- setpwent ();
-
- while (getpwent_r (&pwbuf, buf, sizeof (buf), &track) == 0){
- if (pwbuf.pw_uid == uid){
- home_dir = g_strdup (pwbuf.pw_dir);
- break;
- }
- }
- endpwent ();
-#endif
- if (home_dir == NULL)
- home_dir = g_getenv ("HOME");
- pthread_mutex_unlock (&home_lock);
- }
- }
- return home_dir;
-}
-
-#elif defined (G_OS_WIN32)
-#include <windows.h>
-
-const gchar *
-g_get_home_dir (void)
-{
- /* FIXME */
- const gchar *drive = g_getenv ("HOMEDRIVE");
- const gchar *path = g_getenv ("HOMEPATH");
- gchar *home_dir = NULL;
-
- if (drive && path) {
- home_dir = malloc(strlen(drive) + strlen(path) +1);
- if (home_dir) {
- sprintf(home_dir, "%s%s", drive, path);
- }
- }
-
- return home_dir;
-}
-
-#else
-const gchar *
-g_get_home_dir (void)
-{
- g_error ("%s", "g_get_home_dir not implemented on this platform");
- return NULL;
-}
-
-#endif
-
-static const char *tmp_dir;
-
-#ifdef G_OS_UNIX
-static pthread_mutex_t tmp_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif
-
-const gchar *
-g_get_tmp_dir (void)
-{
- if (tmp_dir == NULL){
-#ifdef G_OS_UNIX
- pthread_mutex_lock (&tmp_lock);
-#endif
- if (tmp_dir == NULL){
- tmp_dir = g_getenv ("TMPDIR");
- if (tmp_dir == NULL){
- tmp_dir = g_getenv ("TMP");
- if (tmp_dir == NULL){
- tmp_dir = g_getenv ("TEMP");
- if (tmp_dir == NULL)
-#if defined (G_OS_WIN32)
- tmp_dir = "C:\\temp";
-#else
- tmp_dir = "/tmp";
-#endif
+#ifdef G_OS_WIN32
+ /* check for program with a suffix attached */
+ if (!hasSuffix) {
+ listx = 0;
+ while (suffix_list[listx]) {
+ program_exe = g_strjoin(NULL,program,suffix_list[listx],NULL);
+ probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program_exe, NULL);
+ if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
+ g_free (curdir);
+ g_free (p);
+ g_free (program_exe);
+ return probe_path;
}
+ listx++;
+ g_free (probe_path);
+ g_free (program_exe);
}
}
-#ifdef G_OS_UNIX
- pthread_mutex_unlock (&tmp_lock);
#endif
}
- return tmp_dir;
-}
-
-const char *
-g_get_user_name (void)
-{
- return g_getenv ("USER");
+ g_free (curdir);
+ g_free (p);
+ return NULL;
}
static char *name;