4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
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 */
31 #include <sys/param.h>
47 static GHashTable
*uid_hash
= NULL
; /* UID -> User name */
48 static GHashTable
*gid_hash
= NULL
; /* GID -> Group name */
50 /* Static prototypes */
53 /* Like g_strdup, but does realpath() too (if possible) */
54 char *pathdup(char *path
)
56 char real
[MAXPATHLEN
];
58 g_return_val_if_fail(path
!= NULL
, NULL
);
60 if (realpath(path
, real
))
61 return g_strdup(real
);
63 return g_strdup(path
);
66 /* Join the path to the leaf (adding a / between them) and
67 * return a pointer to a buffer with the result. Buffer is valid until
68 * the next call to make_path.
70 GString
*make_path(char *dir
, char *leaf
)
72 static GString
*buffer
= NULL
;
75 buffer
= g_string_new(NULL
);
77 g_return_val_if_fail(dir
!= NULL
, buffer
);
78 g_return_val_if_fail(leaf
!= NULL
, buffer
);
80 g_string_sprintf(buffer
, "%s%s%s",
82 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
88 /* Return our complete host name */
89 char *our_host_name(void)
91 static char *name
= NULL
;
93 if (option_get_int("dnd_no_hostnames"))
100 if (gethostname(buffer
, 4096) == 0)
102 /* gethostname doesn't always return the full name... */
106 ent
= gethostbyname(buffer
);
107 name
= g_strdup(ent
? ent
->h_name
: buffer
);
111 g_warning("gethostname() failed - using localhost\n");
112 name
= g_strdup("localhost");
119 /* fork() and run a new program.
120 * Returns the new PID, or 0 on failure.
122 pid_t
spawn(char **argv
)
124 return spawn_full(argv
, NULL
);
127 /* As spawn(), but cd to dir first (if dir is non-NULL) */
128 pid_t
spawn_full(char **argv
, char *dir
)
135 return 0; /* Failure */
138 /* We are the child process */
141 fprintf(stderr
, "chdir() failed: %s\n",
143 execvp(argv
[0], argv
);
144 fprintf(stderr
, "execvp(%s, ...) failed: %s\n",
150 /* We are the parent */
154 void debug_free_string(void *data
)
156 g_print("Freeing string '%s'\n", (char *) data
);
160 char *user_name(uid_t uid
)
165 uid_hash
= g_hash_table_new(NULL
, NULL
);
167 retval
= g_hash_table_lookup(uid_hash
, GINT_TO_POINTER(uid
));
171 struct passwd
*passwd
;
173 passwd
= getpwuid(uid
);
174 retval
= passwd
? g_strdup(passwd
->pw_name
)
175 : g_strdup_printf("[%d]", (int) uid
);
176 g_hash_table_insert(uid_hash
, GINT_TO_POINTER(uid
), retval
);
182 char *group_name(gid_t gid
)
187 gid_hash
= g_hash_table_new(NULL
, NULL
);
189 retval
= g_hash_table_lookup(gid_hash
, GINT_TO_POINTER(gid
));
195 group
= getgrgid(gid
);
196 retval
= group
? g_strdup(group
->gr_name
)
197 : g_strdup_printf("[%d]", (int) gid
);
198 g_hash_table_insert(gid_hash
, GINT_TO_POINTER(gid
), retval
);
204 /* Return a string in the form '23Mb' in a static buffer valid until
207 char *format_size(unsigned long size
)
209 static char *buffer
= NULL
;
212 if (size
>= PRETTY_SIZE_LIMIT
)
216 if (size
>= PRETTY_SIZE_LIMIT
)
220 if (size
>= PRETTY_SIZE_LIMIT
)
239 buffer
= g_strdup_printf("%ld %s", size
, units
);
244 /* Return a string in the form '23Mb' in a static buffer valid until
245 * the next call. Aligned to the right.
247 char *format_size_aligned(unsigned long size
)
249 static char *buffer
= NULL
;
252 if (size
>= PRETTY_SIZE_LIMIT
)
256 if (size
>= PRETTY_SIZE_LIMIT
)
260 if (size
>= PRETTY_SIZE_LIMIT
)
277 buffer
= g_strdup_printf("%4ld%c", size
, units
);
282 /* Fork and exec argv. Wait and return the child's exit status.
285 int fork_exec_wait(char **argv
)
290 child
= spawn_full(argv
, NULL
);
294 if (waitpid(child
, &status
, 0) == -1)
306 /* If a file has this UID and GID, which permissions apply to us?
307 * 0 = User, 1 = Group, 2 = World
309 gint
applicable(uid_t uid
, gid_t gid
)
319 for (i
= 0; i
< ngroups
; i
++)
321 if (supplemental_groups
[i
] == gid
)
328 /* Converts a file's mode to a string. Result is a pointer
329 * to a static buffer, valid until the next call.
331 char *pretty_permissions(mode_t m
)
333 static char buffer
[] = "rwx,rwx,rwx/UGT";
335 buffer
[0] = m
& S_IRUSR
? 'r' : '-';
336 buffer
[1] = m
& S_IWUSR
? 'w' : '-';
337 buffer
[2] = m
& S_IXUSR
? 'x' : '-';
339 buffer
[4] = m
& S_IRGRP
? 'r' : '-';
340 buffer
[5] = m
& S_IWGRP
? 'w' : '-';
341 buffer
[6] = m
& S_IXGRP
? 'x' : '-';
343 buffer
[8] = m
& S_IROTH
? 'r' : '-';
344 buffer
[9] = m
& S_IWOTH
? 'w' : '-';
345 buffer
[10] = m
& S_IXOTH
? 'x' : '-';
347 buffer
[12] = m
& S_ISUID
? 'U' : '-';
348 buffer
[13] = m
& S_ISGID
? 'G' : '-';
350 buffer
[14] = m
& S_ISVTX
? 'T' : '-';
359 /* Gets the canonical name for address and compares to our_host_name() */
360 static gboolean
is_local_address(char *address
)
364 ent
= gethostbyname(address
);
366 return strcmp(our_host_name(), ent
? ent
->h_name
: address
) == 0;
369 /* Convert a URI to a local pathname (or NULL if it isn't local).
370 * The returned pointer points inside the input string.
377 char *get_local_path(char *uri
)
381 char *path
, *uri_host
;
384 return uri
; /* Just a local path - no host part */
386 path
= strchr(uri
+ 2, '/');
388 return NULL
; /* //something */
391 return path
; /* ///path */
393 uri_host
= g_strndup(uri
+ 2, path
- uri
- 2);
394 if (is_local_address(uri_host
))
397 return path
; /* //myhost/path */
401 return NULL
; /* From a different host */
405 if (strncasecmp(uri
, "file:", 5))
406 return NULL
; /* Don't know this format */
411 return get_local_path(uri
);
417 /* Set the close-on-exec flag for this FD.
418 * TRUE means that an exec()'d process will not get the FD.
420 void close_on_exec(int fd
, gboolean close
)
422 if (fcntl(fd
, F_SETFD
, close
))
423 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
426 void set_blocking(int fd
, gboolean blocking
)
428 if (fcntl(fd
, F_SETFL
, blocking
? 0 : O_NONBLOCK
))
429 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
432 /* Format this time nicely. The result is a pointer to a static buffer,
433 * valid until the next call.
435 char *pretty_time(time_t *time
)
437 static char time_buf
[32];
439 if (strftime(time_buf
, sizeof(time_buf
),
440 TIME_FORMAT
, localtime(time
)) == 0)
447 # define O_NOFOLLOW 0x0
451 /* Copy data from 'read_fd' FD to 'write_fd' FD until EOF or error.
452 * Returns 0 on success, -1 on error (and sets errno).
454 static int copy_fd(int read_fd
, int write_fd
)
459 while ((got
= mc_read(read_fd
, buffer
, sizeof(buffer
))) > 0)
467 c
= mc_write(write_fd
, buffer
+ sent
, got
- sent
);
478 /* 'from' and 'to' are complete pathnames of files (not dirs or symlinks).
479 * This spawns 'cp' to do the copy if lstat() succeeds, otherwise we
480 * do the copy manually using vfs.
482 * Returns an error string, or NULL on success.
484 guchar
*copy_file(guchar
*from
, guchar
*to
)
486 char *argv
[] = {"cp", "-pRf", NULL
, NULL
, NULL
};
491 if (lstat(from
, &info
))
497 if (mc_lstat(from
, &info
))
498 return g_strerror(errno
);
500 /* Regular lstat() can't find it, but mc_lstat() can,
501 * so try reading it with VFS.
504 if (!S_ISREG(info
.st_mode
))
507 read_fd
= mc_open(from
, O_RDONLY
);
510 write_fd
= mc_open(to
,
511 O_NOFOLLOW
| O_WRONLY
| O_CREAT
| O_TRUNC
,
512 info
.st_mode
& 0777);
516 if (copy_fd(read_fd
, write_fd
))
519 /* (yes, the single | is right) */
520 if (mc_close(read_fd
) | mc_close(write_fd
))
521 return g_strerror(errno
);
529 return error
? g_strerror(error
) : _("Copy error");
536 if (fork_exec_wait(argv
))
537 return _("Copy failed");
541 /* 'word' has all special characters escaped so that it may be inserted
542 * into a shell command.
543 * Eg: 'My Dir?' becomes 'My\ Dir\?'. g_free() the result.
545 guchar
*shell_escape(guchar
*word
)
550 tmp
= g_string_new(NULL
);
554 if (strchr(" ?*['\"$~\\|();!`&", *word
))
555 g_string_append_c(tmp
, '\\');
556 g_string_append_c(tmp
, *word
);
561 g_string_free(tmp
, FALSE
);
565 /* TRUE iff `sub' is (or would be) an object inside the directory `parent',
566 * (or the two are the same item/directory).
567 * FALSE if parent doesn't exist.
569 gboolean
is_sub_dir(char *sub
, char *parent
)
571 struct stat parent_info
;
573 /* These were pathdup()s, but that follows symlinks!
574 * I guess they were here for a reason - something is probably
579 if (mc_stat(parent
, &parent_info
))
580 return FALSE
; /* Parent doesn't exist */
587 if (mc_stat(sub
, &info
) == 0)
589 if (info
.st_dev
== parent_info
.st_dev
&&
590 info
.st_ino
== parent_info
.st_ino
)
597 slash
= strrchr(sub
, '/');
618 /* True if the string 'list' contains 'item'.
619 * Eg ("close", "close, help") -> TRUE
621 gboolean
in_list(guchar
*item
, guchar
*list
)
629 if (strncmp(item
, list
, len
) == 0 && !isalpha(list
[len
]))
631 list
= strchr(list
, ',');
634 while (isspace(*++list
))