r109: Option processing is now done with getopt_long.
[rox-filer.git] / ROX-Filer / src / support.c
blobedebeccd02823060a4cc100aa13a0aa8fd383417
1 /* vi: set cindent:
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 <sys/param.h>
28 #include <unistd.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <fcntl.h>
33 #include <glib.h>
35 #include "main.h"
36 #include "support.h"
38 /* Static prototypes */
41 /* Like g_strdup, but does realpath() too (if possible) */
42 char *pathdup(char *path)
44 char real[MAXPATHLEN];
46 g_return_val_if_fail(path != NULL, NULL);
48 if (realpath(path, real))
49 return g_strdup(real);
51 return g_strdup(path);
54 /* Join the path to the leaf (adding a / between them) and
55 * return a pointer to a buffer with the result. Buffer is valid until
56 * the next call to make_path.
58 GString *make_path(char *dir, char *leaf)
60 static GString *buffer = NULL;
62 if (!buffer)
63 buffer = g_string_new(NULL);
65 g_return_val_if_fail(dir != NULL, buffer);
66 g_return_val_if_fail(leaf != NULL, buffer);
68 g_string_sprintf(buffer, "%s%s%s",
69 dir,
70 dir[0] == '/' && dir[1] == '\0' ? "" : "/",
71 leaf);
73 return buffer;
76 /* Return our complete host name */
77 char *our_host_name()
79 static char *name = NULL;
81 if (!name)
83 char buffer[4096];
85 g_return_val_if_fail(gethostname(buffer, 4096) == 0,
86 "localhost");
88 buffer[4095] = '\0';
89 name = g_strdup(buffer);
92 return name;
95 /* fork() and run a new program.
96 * Returns the new PID, or 0 on failure.
98 int spawn(char **argv)
100 return spawn_full(argv, NULL, 0);
103 /* As spawn(), but cd to dir first (if dir is non-NULL) */
104 int spawn_full(char **argv, char *dir, SpawnFlags flags)
106 int child;
108 child = fork();
110 if (child == -1)
111 return 0; /* Failure */
112 else if (child == 0)
114 /* We are the child process */
115 if (dir)
116 if (chdir(dir))
117 fprintf(stderr, "chdir() failed: %s\n",
118 g_strerror(errno));
119 dup2(to_error_log, STDERR_FILENO);
120 fcntl(STDERR_FILENO, F_SETFD, 0);
121 execvp(argv[0], argv);
122 fprintf(stderr, "execvp(%s, ...) failed: %s\n",
123 argv[0],
124 g_strerror(errno));
125 _exit(0);
128 /* We are the parent */
129 return child;
132 void debug_free_string(void *data)
134 g_print("Freeing string '%s'\n", (char *) data);
135 g_free(data);
138 char *user_name(uid_t uid)
140 struct passwd *passwd;
141 GString *tmp;
142 char *retval;
144 passwd = getpwuid(uid);
145 if (passwd)
146 return passwd->pw_name;
147 tmp = g_string_new(NULL);
148 g_string_sprintf(tmp, "[%d]", (int) uid);
149 retval = tmp->str;
150 g_string_free(tmp, FALSE);
151 return retval;
154 char *group_name(gid_t gid)
156 struct group *group;
157 GString *tmp;
158 char *retval;
160 group = getgrgid(gid);
161 if (group)
162 return group->gr_name;
163 tmp = g_string_new(NULL);
164 g_string_sprintf(tmp, "[%d]", (int) gid);
165 retval = tmp->str;
166 g_string_free(tmp, FALSE);
167 return retval;