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 */
28 #include <sys/param.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
;
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",
78 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
84 /* Return our complete host name */
87 static char *name
= NULL
;
93 g_return_val_if_fail(gethostname(buffer
, 4096) == 0,
97 name
= g_strdup(buffer
);
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
)
119 return 0; /* Failure */
122 /* We are the child process */
125 fprintf(stderr
, "chdir() failed: %s\n",
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",
136 /* We are the parent */
140 void debug_free_string(void *data
)
142 g_print("Freeing string '%s'\n", (char *) data
);
146 char *user_name(uid_t uid
)
151 uid_hash
= g_hash_table_new(NULL
, NULL
);
153 retval
= g_hash_table_lookup(uid_hash
, (gpointer
) uid
);
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
);
168 char *group_name(gid_t gid
)
173 gid_hash
= g_hash_table_new(NULL
, NULL
);
175 retval
= g_hash_table_lookup(gid_hash
, (gpointer
) gid
);
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
);
190 /* Return a string in the form '23Mb' in a static buffer valid until
193 char *format_size(unsigned long size
)
195 static char *buffer
= NULL
;
198 if (size
>= PRETTY_SIZE_LIMIT
)
202 if (size
>= PRETTY_SIZE_LIMIT
)
206 if (size
>= PRETTY_SIZE_LIMIT
)
223 buffer
= g_strdup_printf("%ld %s", size
, units
);
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
;
236 if (size
>= PRETTY_SIZE_LIMIT
)
240 if (size
>= PRETTY_SIZE_LIMIT
)
244 if (size
>= PRETTY_SIZE_LIMIT
)
261 buffer
= g_strdup_printf("%4ld%c", size
, units
);
266 /* Fork and exec argv. Wait and return the child's exit status.
269 int fork_exec_wait(char **argv
)
274 child
= spawn_full(argv
, NULL
);
278 if (waitpid(child
, &status
, 0) == -1)
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
)
303 for (i
= 0; i
< ngroups
; i
++)
305 if (supplemental_groups
[i
] == gid
)
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' : '-';
334 buffer
[14] = m
& S_ISVTX
? 'T' : '-';
343 /* Convert a URI to a local pathname (or NULL if it isn't local).
344 * The returned pointer points inside the input string.
351 char *get_local_path(char *uri
)
355 host
= our_host_name();
362 return uri
; /* Just a local path - no host part */
364 path
= strchr(uri
+ 2, '/');
366 return NULL
; /* //something */
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 */
378 if (strncasecmp(uri
, "file:", 5))
379 return NULL
; /* Don't know this format */
384 return get_local_path(uri
);
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)