r42: Applications are now run from the user's home directory. Rename now
[rox-filer.git] / ROX-Filer / src / support.c
blob19d2e5a4360f55f6192ae6b3ce35bd7c547368cf
1 /* vi: set cindent:
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * By Thomas Leonard, <tal197@ecs.soton.ac.uk>.
6 */
8 /* support.c - (non-GUI) useful routines */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <sys/param.h>
14 #include <unistd.h>
16 #include <glib.h>
18 #include "support.h"
20 /* Static prototypes */
23 /* Like g_strdup, but does realpath() too (if possible) */
24 char *pathdup(char *path)
26 char real[MAXPATHLEN];
28 g_return_val_if_fail(path != NULL, NULL);
30 if (realpath(path, real))
31 return g_strdup(real);
33 return g_strdup(path);
36 /* Join the path to the leaf (adding a / between them) and
37 * return a pointer to a buffer with the result. Buffer is valid until
38 * the next call to make_path.
40 GString *make_path(char *dir, char *leaf)
42 static GString *buffer = NULL;
44 if (!buffer)
45 buffer = g_string_new(NULL);
47 g_return_val_if_fail(dir != NULL, buffer);
48 g_return_val_if_fail(leaf != NULL, buffer);
50 g_string_sprintf(buffer, "%s%s%s",
51 dir,
52 dir[0] == '/' && dir[1] == '\0' ? "" : "/",
53 leaf);
55 return buffer;
58 /* Return our complete host name */
59 char *our_host_name()
61 static char *name = NULL;
63 if (!name)
65 char buffer[4096];
67 g_return_val_if_fail(gethostname(buffer, 4096) == 0,
68 "localhost");
70 buffer[4095] = '\0';
71 name = g_strdup(buffer);
74 return name;
77 /* fork() and run a new program.
78 * Returns the new PID, or 0 on failure.
80 int spawn(char **argv)
82 return spawn_full(argv, NULL, 0);
85 /* As spawn(), but cd to dir first (if dir is non-NULL) */
86 int spawn_full(char **argv, char *dir, SpawnFlags flags)
88 int child;
90 child = fork();
92 if (child == -1)
93 return 0; /* Failure */
94 else if (child == 0)
96 /* We are the child process */
97 if (dir)
98 if (chdir(dir))
99 fprintf(stderr, "chdir() failed: %s\n",
100 g_strerror(errno));
101 execvp(argv[0], argv);
102 fprintf(stderr, "execvp(%s, ...) failed: %s\n",
103 argv[0],
104 g_strerror(errno));
105 _exit(0);
108 /* We are the parent */
109 return child;
112 void debug_free_string(void *data)
114 g_print("Freeing string '%s'\n", (char *) data);
115 g_free(data);