r68: Added full Adjust actions for files/dirs/apps for windows and panels.
[rox-filer.git] / ROX-Filer / src / support.c
blob08046037f6a498868628c96203fac821662e0fe1
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>
15 #include <pwd.h>
16 #include <grp.h>
18 #include <glib.h>
20 #include "support.h"
22 /* Static prototypes */
25 /* Like g_strdup, but does realpath() too (if possible) */
26 char *pathdup(char *path)
28 char real[MAXPATHLEN];
30 g_return_val_if_fail(path != NULL, NULL);
32 if (realpath(path, real))
33 return g_strdup(real);
35 return g_strdup(path);
38 /* Join the path to the leaf (adding a / between them) and
39 * return a pointer to a buffer with the result. Buffer is valid until
40 * the next call to make_path.
42 GString *make_path(char *dir, char *leaf)
44 static GString *buffer = NULL;
46 if (!buffer)
47 buffer = g_string_new(NULL);
49 g_return_val_if_fail(dir != NULL, buffer);
50 g_return_val_if_fail(leaf != NULL, buffer);
52 g_string_sprintf(buffer, "%s%s%s",
53 dir,
54 dir[0] == '/' && dir[1] == '\0' ? "" : "/",
55 leaf);
57 return buffer;
60 /* Return our complete host name */
61 char *our_host_name()
63 static char *name = NULL;
65 if (!name)
67 char buffer[4096];
69 g_return_val_if_fail(gethostname(buffer, 4096) == 0,
70 "localhost");
72 buffer[4095] = '\0';
73 name = g_strdup(buffer);
76 return name;
79 /* fork() and run a new program.
80 * Returns the new PID, or 0 on failure.
82 int spawn(char **argv)
84 return spawn_full(argv, NULL, 0);
87 /* As spawn(), but cd to dir first (if dir is non-NULL) */
88 int spawn_full(char **argv, char *dir, SpawnFlags flags)
90 int child;
92 child = fork();
94 if (child == -1)
95 return 0; /* Failure */
96 else if (child == 0)
98 /* We are the child process */
99 if (dir)
100 if (chdir(dir))
101 fprintf(stderr, "chdir() failed: %s\n",
102 g_strerror(errno));
103 execvp(argv[0], argv);
104 fprintf(stderr, "execvp(%s, ...) failed: %s\n",
105 argv[0],
106 g_strerror(errno));
107 _exit(0);
110 /* We are the parent */
111 return child;
114 void debug_free_string(void *data)
116 g_print("Freeing string '%s'\n", (char *) data);
117 g_free(data);
120 char *user_name(uid_t uid)
122 static char buffer[40];
123 struct passwd *passwd;
125 passwd = getpwuid(uid);
126 if (passwd)
127 return passwd->pw_name;
128 snprintf(buffer, sizeof(buffer), "[%d]", uid);
129 return buffer;
132 char *group_name(gid_t gid)
134 static char buffer[40];
135 struct group *group;
137 group = getgrgid(gid);
138 if (group)
139 return group->gr_name;
140 snprintf(buffer, sizeof(buffer), "[%d]", gid);
141 return buffer;