r208: Added Bernard Jungen's patch:
[rox-filer/ma.git] / ROX-Filer / src / support.c
blob960321d0880daa8a116d75ea722ef02e65cdfb3a
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* support.c - (non-GUI) useful routines */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <sys/param.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <fcntl.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <string.h>
36 #include <time.h>
38 #include <glib.h>
40 #include "main.h"
41 #include "support.h"
43 static GHashTable *uid_hash = NULL; /* UID -> User name */
44 static GHashTable *gid_hash = NULL; /* GID -> Group name */
46 /* Static prototypes */
49 /* Like g_strdup, but does realpath() too (if possible) */
50 char *pathdup(char *path)
52 char real[MAXPATHLEN];
54 g_return_val_if_fail(path != NULL, NULL);
56 if (realpath(path, real))
57 return g_strdup(real);
59 return g_strdup(path);
62 /* Join the path to the leaf (adding a / between them) and
63 * return a pointer to a buffer with the result. Buffer is valid until
64 * the next call to make_path.
66 GString *make_path(char *dir, char *leaf)
68 static GString *buffer = NULL;
70 if (!buffer)
71 buffer = g_string_new(NULL);
73 g_return_val_if_fail(dir != NULL, buffer);
74 g_return_val_if_fail(leaf != NULL, buffer);
76 g_string_sprintf(buffer, "%s%s%s",
77 dir,
78 dir[0] == '/' && dir[1] == '\0' ? "" : "/",
79 leaf);
81 return buffer;
84 /* Return our complete host name */
85 char *our_host_name()
87 static char *name = NULL;
89 if (!name)
91 char buffer[4096];
93 g_return_val_if_fail(gethostname(buffer, 4096) == 0,
94 "localhost");
96 buffer[4095] = '\0';
97 name = g_strdup(buffer);
100 return name;
103 /* fork() and run a new program.
104 * Returns the new PID, or 0 on failure.
106 pid_t spawn(char **argv)
108 return spawn_full(argv, NULL);
111 /* As spawn(), but cd to dir first (if dir is non-NULL) */
112 pid_t spawn_full(char **argv, char *dir)
114 int child;
116 child = fork();
118 if (child == -1)
119 return 0; /* Failure */
120 else if (child == 0)
122 /* We are the child process */
123 if (dir)
124 if (chdir(dir))
125 fprintf(stderr, "chdir() failed: %s\n",
126 g_strerror(errno));
127 dup2(to_error_log, STDERR_FILENO);
128 close_on_exec(STDERR_FILENO, FALSE);
129 execvp(argv[0], argv);
130 fprintf(stderr, "execvp(%s, ...) failed: %s\n",
131 argv[0],
132 g_strerror(errno));
133 _exit(0);
136 /* We are the parent */
137 return child;
140 void debug_free_string(void *data)
142 g_print("Freeing string '%s'\n", (char *) data);
143 g_free(data);
146 char *user_name(uid_t uid)
148 char *retval;
150 if (!uid_hash)
151 uid_hash = g_hash_table_new(NULL, NULL);
153 retval = g_hash_table_lookup(uid_hash, (gpointer) uid);
155 if (!retval)
157 struct passwd *passwd;
159 passwd = getpwuid(uid);
160 retval = passwd ? g_strdup(passwd->pw_name)
161 : g_strdup_printf("[%d]", (int) uid);
162 g_hash_table_insert(uid_hash, (gpointer) uid, retval);
165 return retval;
168 char *group_name(gid_t gid)
170 char *retval;
172 if (!gid_hash)
173 gid_hash = g_hash_table_new(NULL, NULL);
175 retval = g_hash_table_lookup(gid_hash, (gpointer) gid);
177 if (!retval)
179 struct group *group;
181 group = getgrgid(gid);
182 retval = group ? g_strdup(group->gr_name)
183 : g_strdup_printf("[%d]", (int) gid);
184 g_hash_table_insert(gid_hash, (gpointer) gid, retval);
187 return retval;
190 /* Return a string in the form '23Mb' in a static buffer valid until
191 * the next call.
193 char *format_size(unsigned long size)
195 static char *buffer = NULL;
196 char *units;
198 if (size >= PRETTY_SIZE_LIMIT)
200 size += 1023;
201 size >>= 10;
202 if (size >= PRETTY_SIZE_LIMIT)
204 size += 1023;
205 size >>= 10;
206 if (size >= PRETTY_SIZE_LIMIT)
208 size += 1023;
209 size >>= 10;
210 units = "Gb";
212 else
213 units = "Mb";
215 else
216 units = "K";
218 else
219 units = "bytes";
221 if (buffer)
222 g_free(buffer);
223 buffer = g_strdup_printf("%ld %s", size, units);
225 return buffer;
228 /* Return a string in the form '23Mb' in a static buffer valid until
229 * the next call. Aligned to the right.
231 char *format_size_aligned(unsigned long size)
233 static char *buffer = NULL;
234 char units;
236 if (size >= PRETTY_SIZE_LIMIT)
238 size += 1023;
239 size >>= 10;
240 if (size >= PRETTY_SIZE_LIMIT)
242 size += 1023;
243 size >>= 10;
244 if (size >= PRETTY_SIZE_LIMIT)
246 size += 1023;
247 size >>= 10;
248 units = 'G';
250 else
251 units = 'M';
253 else
254 units = 'K';
256 else
257 units = ' ';
259 if (buffer)
260 g_free(buffer);
261 buffer = g_strdup_printf("%4ld%c", size, units);
263 return buffer;
266 /* Fork and exec argv. Wait and return the child's exit status.
267 * -1 if spawn fails.
269 int fork_exec_wait(char **argv)
271 pid_t child;
272 int status = -1;
274 child = spawn_full(argv, NULL);
276 while (child)
278 if (waitpid(child, &status, 0) == -1)
280 if (errno != EINTR)
281 return -1;
283 else
284 break;
287 return status;
290 /* If a file has this UID and GID, which permissions apply to us?
291 * 0 = User, 1 = Group, 2 = World
293 gint applicable(uid_t uid, gid_t gid)
295 int i;
297 if (uid == euid)
298 return 0;
300 if (gid == egid)
301 return 1;
303 for (i = 0; i < ngroups; i++)
305 if (supplemental_groups[i] == gid)
306 return 1;
309 return 2;
312 /* Converts a file's mode to a string. Result is a pointer
313 * to a static buffer, valid until the next call.
315 char *pretty_permissions(mode_t m)
317 static char buffer[] = "rwx,rwx,rwx/UGT";
319 buffer[0] = m & S_IRUSR ? 'r' : '-';
320 buffer[1] = m & S_IWUSR ? 'w' : '-';
321 buffer[2] = m & S_IXUSR ? 'x' : '-';
323 buffer[4] = m & S_IRGRP ? 'r' : '-';
324 buffer[5] = m & S_IWGRP ? 'w' : '-';
325 buffer[6] = m & S_IXGRP ? 'x' : '-';
327 buffer[8] = m & S_IROTH ? 'r' : '-';
328 buffer[9] = m & S_IWOTH ? 'w' : '-';
329 buffer[10] = m & S_IXOTH ? 'x' : '-';
331 buffer[12] = m & S_ISUID ? 'U' : '-';
332 buffer[13] = m & S_ISGID ? 'G' : '-';
333 #ifdef S_ISVTX
334 buffer[14] = m & S_ISVTX ? 'T' : '-';
335 buffer[15] = 0;
336 #else
337 buffer[14] = 0;
338 #endif
340 return buffer;
343 /* Convert a URI to a local pathname (or NULL if it isn't local).
344 * The returned pointer points inside the input string.
345 * Possible formats:
346 * /path
347 * ///path
348 * //host/path
349 * file://host/path
351 char *get_local_path(char *uri)
353 char *host;
355 host = our_host_name();
357 if (*uri == '/')
359 char *path;
361 if (uri[1] != '/')
362 return uri; /* Just a local path - no host part */
364 path = strchr(uri + 2, '/');
365 if (!path)
366 return NULL; /* //something */
368 if (path - uri == 2)
369 return path; /* ///path */
370 if (strlen(host) == path - uri - 2 &&
371 strncmp(uri + 2, host, path - uri - 2) == 0)
372 return path; /* //myhost/path */
374 return NULL; /* From a different host */
376 else
378 if (strncasecmp(uri, "file:", 5))
379 return NULL; /* Don't know this format */
381 uri += 5;
383 if (*uri == '/')
384 return get_local_path(uri);
386 return NULL;
390 /* Set the close-on-exec flag for this FD.
391 * TRUE means that an exec()'d process will not get the FD.
393 void close_on_exec(int fd, gboolean close)
395 if (fcntl(fd, F_SETFD, close))
396 g_warning("fcntl() failed: %s\n", g_strerror(errno));
399 void set_blocking(int fd, gboolean blocking)
401 if (fcntl(fd, F_SETFL, blocking ? 0 : O_NONBLOCK))
402 g_warning("fcntl() failed: %s\n", g_strerror(errno));
405 /* Format this time nicely. The result is a pointer to a static buffer,
406 * valid until the next call.
408 char *pretty_time(time_t *time)
410 static char time_buf[32];
412 if (strftime(time_buf, sizeof(time_buf),
413 TIME_FORMAT, localtime(time)) == 0)
414 time_buf[0]= 0;
416 return time_buf;