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>
41 /* Static prototypes */
44 /* Like g_strdup, but does realpath() too (if possible) */
45 char *pathdup(char *path
)
47 char real
[MAXPATHLEN
];
49 g_return_val_if_fail(path
!= NULL
, NULL
);
51 if (realpath(path
, real
))
52 return g_strdup(real
);
54 return g_strdup(path
);
57 /* Join the path to the leaf (adding a / between them) and
58 * return a pointer to a buffer with the result. Buffer is valid until
59 * the next call to make_path.
61 GString
*make_path(char *dir
, char *leaf
)
63 static GString
*buffer
= NULL
;
66 buffer
= g_string_new(NULL
);
68 g_return_val_if_fail(dir
!= NULL
, buffer
);
69 g_return_val_if_fail(leaf
!= NULL
, buffer
);
71 g_string_sprintf(buffer
, "%s%s%s",
73 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
79 /* Return our complete host name */
82 static char *name
= NULL
;
88 g_return_val_if_fail(gethostname(buffer
, 4096) == 0,
92 name
= g_strdup(buffer
);
98 /* fork() and run a new program.
99 * Returns the new PID, or 0 on failure.
101 pid_t
spawn(char **argv
)
103 return spawn_full(argv
, NULL
);
106 /* As spawn(), but cd to dir first (if dir is non-NULL) */
107 pid_t
spawn_full(char **argv
, char *dir
)
114 return 0; /* Failure */
117 /* We are the child process */
120 fprintf(stderr
, "chdir() failed: %s\n",
122 dup2(to_error_log
, STDERR_FILENO
);
123 fcntl(STDERR_FILENO
, F_SETFD
, 0);
124 execvp(argv
[0], argv
);
125 fprintf(stderr
, "execvp(%s, ...) failed: %s\n",
131 /* We are the parent */
135 void debug_free_string(void *data
)
137 g_print("Freeing string '%s'\n", (char *) data
);
141 char *user_name(uid_t uid
)
143 struct passwd
*passwd
;
147 passwd
= getpwuid(uid
);
149 return passwd
->pw_name
;
150 tmp
= g_string_new(NULL
);
151 g_string_sprintf(tmp
, "[%d]", (int) uid
);
153 g_string_free(tmp
, FALSE
);
157 char *group_name(gid_t gid
)
163 group
= getgrgid(gid
);
165 return group
->gr_name
;
166 tmp
= g_string_new(NULL
);
167 g_string_sprintf(tmp
, "[%d]", (int) gid
);
169 g_string_free(tmp
, FALSE
);
173 /* Return a string in the form '23Mb' in a static buffer valid until
176 char *format_size(unsigned long size
)
178 static char *buffer
= NULL
;
181 if (size
>= PRETTY_SIZE_LIMIT
)
185 if (size
>= PRETTY_SIZE_LIMIT
)
189 if (size
>= PRETTY_SIZE_LIMIT
)
206 buffer
= g_strdup_printf("%ld %s", size
, units
);
211 /* Fork and exec argv. Wait and return the child's exit status.
214 int fork_exec_wait(char **argv
)
219 child
= spawn_full(argv
, NULL
);
223 if (waitpid(child
, &status
, 0) == -1)
235 /* Converts a file's mode to a string. Result is a pointer
236 * to a static buffer, valid until the next call.
237 * uid and gid are for the file - the permissions that affect the
238 * filer will be highlighted.
240 char *pretty_permissions(uid_t uid
, gid_t gid
, mode_t m
)
242 static char buffer
[] = "(rwx),rwx,rwx/UGT";
247 else if (gid
== egid
)
254 for (i
= 0; i
< ngroups
; i
++)
256 if (supplemental_groups
[i
] == gid
)
264 sprintf(buffer
, "%s%c%c%c%s,%s%c%c%c%s,%s%c%c%c%s/%c%c"
270 high
== 0 ? "(" : "",
271 m
& S_IRUSR
? 'r' : '-',
272 m
& S_IWUSR
? 'w' : '-',
273 m
& S_IXUSR
? 'x' : '-',
274 high
== 0 ? ")" : "",
276 high
== 1 ? "(" : "",
277 m
& S_IRGRP
? 'r' : '-',
278 m
& S_IWGRP
? 'w' : '-',
279 m
& S_IXGRP
? 'x' : '-',
280 high
== 1 ? ")" : "",
282 high
== 2 ? "(" : "",
283 m
& S_IROTH
? 'r' : '-',
284 m
& S_IWOTH
? 'w' : '-',
285 m
& S_IXOTH
? 'x' : '-',
286 high
== 2 ? ")" : "",
288 m
& S_ISUID
? 'U' : '-',
289 m
& S_ISGID
? 'G' : '-'
291 , m
& S_ISVTX
? 'T' : '-'