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>
39 /* Static prototypes */
42 /* Like g_strdup, but does realpath() too (if possible) */
43 char *pathdup(char *path
)
45 char real
[MAXPATHLEN
];
47 g_return_val_if_fail(path
!= NULL
, NULL
);
49 if (realpath(path
, real
))
50 return g_strdup(real
);
52 return g_strdup(path
);
55 /* Join the path to the leaf (adding a / between them) and
56 * return a pointer to a buffer with the result. Buffer is valid until
57 * the next call to make_path.
59 GString
*make_path(char *dir
, char *leaf
)
61 static GString
*buffer
= NULL
;
64 buffer
= g_string_new(NULL
);
66 g_return_val_if_fail(dir
!= NULL
, buffer
);
67 g_return_val_if_fail(leaf
!= NULL
, buffer
);
69 g_string_sprintf(buffer
, "%s%s%s",
71 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
77 /* Return our complete host name */
80 static char *name
= NULL
;
86 g_return_val_if_fail(gethostname(buffer
, 4096) == 0,
90 name
= g_strdup(buffer
);
96 /* fork() and run a new program.
97 * Returns the new PID, or 0 on failure.
99 pid_t
spawn(char **argv
)
101 return spawn_full(argv
, NULL
);
104 /* As spawn(), but cd to dir first (if dir is non-NULL) */
105 pid_t
spawn_full(char **argv
, char *dir
)
112 return 0; /* Failure */
115 /* We are the child process */
118 fprintf(stderr
, "chdir() failed: %s\n",
120 dup2(to_error_log
, STDERR_FILENO
);
121 fcntl(STDERR_FILENO
, F_SETFD
, 0);
122 execvp(argv
[0], argv
);
123 fprintf(stderr
, "execvp(%s, ...) failed: %s\n",
129 /* We are the parent */
133 void debug_free_string(void *data
)
135 g_print("Freeing string '%s'\n", (char *) data
);
139 char *user_name(uid_t uid
)
141 struct passwd
*passwd
;
145 passwd
= getpwuid(uid
);
147 return passwd
->pw_name
;
148 tmp
= g_string_new(NULL
);
149 g_string_sprintf(tmp
, "[%d]", (int) uid
);
151 g_string_free(tmp
, FALSE
);
155 char *group_name(gid_t gid
)
161 group
= getgrgid(gid
);
163 return group
->gr_name
;
164 tmp
= g_string_new(NULL
);
165 g_string_sprintf(tmp
, "[%d]", (int) gid
);
167 g_string_free(tmp
, FALSE
);
171 /* Return a string in the form '23Mb' in a static buffer valid until
174 char *format_size(unsigned long size
)
176 static char *buffer
= NULL
;
201 buffer
= g_strdup_printf("%ld %s", size
, units
);
206 /* Fork and exec argv. Wait and return the child's exit status.
209 int fork_exec_wait(char **argv
)
214 child
= spawn_full(argv
, NULL
);
218 if (waitpid(child
, &status
, 0) == -1)