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)
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
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 */
27 #include <sys/param.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
;
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",
70 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
76 /* Return our complete host name */
79 static char *name
= NULL
;
85 g_return_val_if_fail(gethostname(buffer
, 4096) == 0,
89 name
= g_strdup(buffer
);
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
)
111 return 0; /* Failure */
114 /* We are the child process */
117 fprintf(stderr
, "chdir() failed: %s\n",
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",
128 /* We are the parent */
132 void debug_free_string(void *data
)
134 g_print("Freeing string '%s'\n", (char *) data
);
138 char *user_name(uid_t uid
)
140 struct passwd
*passwd
;
144 passwd
= getpwuid(uid
);
146 return passwd
->pw_name
;
147 tmp
= g_string_new(NULL
);
148 g_string_sprintf(tmp
, "[%d]", (int) uid
);
150 g_string_free(tmp
, FALSE
);
154 char *group_name(gid_t gid
)
160 group
= getgrgid(gid
);
162 return group
->gr_name
;
163 tmp
= g_string_new(NULL
);
164 g_string_sprintf(tmp
, "[%d]", (int) gid
);
166 g_string_free(tmp
, FALSE
);