2 Various utilities - Unix variants
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
8 Miguel de Icaza, 1994, 1995, 1996
9 Janne Kukonlehto, 1994, 1995, 1996
10 Dugan Porter, 1994, 1995, 1996
11 Jakub Jelinek, 1994, 1995, 1996
12 Mauricio Plaza, 1994, 1995, 1996
14 The mc_realpath routine is mostly from uClibc package, written
15 by Rick Sladkey <jrs@world.std.com>
17 This file is part of the Midnight Commander.
19 The Midnight Commander is free software: you can redistribute it
20 and/or modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation, either version 3 of the License,
22 or (at your option) any later version.
24 The Midnight Commander is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 * \brief Source: various utilities - Unix variant
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
50 #include <sys/types.h>
52 #ifdef HAVE_SYS_SELECT_H
53 #include <sys/select.h>
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
59 #ifdef HAVE_GET_PROCESS_STATS
60 #include <sys/procstats.h>
65 #include "lib/global.h"
67 #include "lib/unixcompat.h"
68 #include "lib/vfs/vfs.h" /* VFS_ENCODING_PREFIX */
69 #include "lib/strutil.h" /* str_move() */
71 #include "lib/widget.h" /* message() */
72 #include "lib/vfs/xdirentry.h"
75 #include "lib/charsets.h"
80 /*** global variables ****************************************************************************/
82 struct sigaction startup_handler
;
84 /*** file scope macro definitions ****************************************************************/
86 #define UID_CACHE_SIZE 200
87 #define GID_CACHE_SIZE 30
89 /* Pipes are guaranteed to be able to hold at least 4096 bytes */
90 /* More than that would be unportable */
91 #define MAX_PIPE_SIZE 4096
93 /*** file scope type declarations ****************************************************************/
110 struct sigaction intr
;
111 struct sigaction quit
;
112 struct sigaction stop
;
113 } my_system_sigactions_t
;
115 /*** file scope variables ************************************************************************/
117 static int_cache uid_cache
[UID_CACHE_SIZE
];
118 static int_cache gid_cache
[GID_CACHE_SIZE
];
120 static int error_pipe
[2]; /* File descriptors of error pipe */
121 static int old_error
; /* File descriptor of old standard error */
123 /*** file scope functions ************************************************************************/
124 /* --------------------------------------------------------------------------------------------- */
127 i_cache_match (int id
, int_cache
* cache
, int size
)
131 for (i
= 0; i
< size
; i
++)
132 if (cache
[i
].index
== id
)
133 return cache
[i
].string
;
137 /* --------------------------------------------------------------------------------------------- */
140 i_cache_add (int id
, int_cache
* cache
, int size
, char *text
, int *last
)
142 g_free (cache
[*last
].string
);
143 cache
[*last
].string
= g_strdup (text
);
144 cache
[*last
].index
= id
;
145 *last
= ((*last
) + 1) % size
;
148 /* --------------------------------------------------------------------------------------------- */
150 static my_fork_state_t
159 fprintf (stderr
, "\n\nfork () = -1\n");
170 if (waitpid (pid
, &status
, 0) > 0)
171 return WEXITSTATUS (status
) == 0 ? FORK_PARENT
: FORK_ERROR
;
178 /* --------------------------------------------------------------------------------------------- */
181 my_system__save_sigaction_handlers (my_system_sigactions_t
* sigactions
)
183 struct sigaction ignore
;
185 memset (&ignore
, 0, sizeof (ignore
));
186 ignore
.sa_handler
= SIG_IGN
;
187 sigemptyset (&ignore
.sa_mask
);
189 sigaction (SIGINT
, &ignore
, &sigactions
->intr
);
190 sigaction (SIGQUIT
, &ignore
, &sigactions
->quit
);
192 /* Restore the original SIGTSTP handler, we don't want ncurses' */
193 /* handler messing the screen after the SIGCONT */
194 sigaction (SIGTSTP
, &startup_handler
, &sigactions
->stop
);
197 /* --------------------------------------------------------------------------------------------- */
200 my_system__restore_sigaction_handlers (my_system_sigactions_t
* sigactions
)
202 sigaction (SIGINT
, &sigactions
->intr
, NULL
);
203 sigaction (SIGQUIT
, &sigactions
->quit
, NULL
);
204 sigaction (SIGTSTP
, &sigactions
->stop
, NULL
);
207 /* --------------------------------------------------------------------------------------------- */
210 my_system_make_arg_array (int flags
, const char *shell
, char **execute_name
)
212 GPtrArray
*args_array
;
214 args_array
= g_ptr_array_new ();
216 if ((flags
& EXECUTE_AS_SHELL
) != 0)
218 g_ptr_array_add (args_array
, (gpointer
) shell
);
219 g_ptr_array_add (args_array
, (gpointer
) "-c");
220 *execute_name
= g_strdup (shell
);
226 shell_token
= shell
!= NULL
? strchr (shell
, ' ') : NULL
;
227 if (shell_token
== NULL
)
228 *execute_name
= g_strdup (shell
);
230 *execute_name
= g_strndup (shell
, (gsize
) (shell_token
- shell
));
232 g_ptr_array_add (args_array
, (gpointer
) shell
);
237 /* --------------------------------------------------------------------------------------------- */
240 mc_pread_stream (mc_pipe_stream_t
* ps
, const fd_set
* fds
)
245 if (!FD_ISSET (ps
->fd
, fds
))
247 ps
->len
= MC_PIPE_STREAM_UNREAD
;
251 buf_len
= (size_t) ps
->len
;
253 if (buf_len
>= MC_PIPE_BUFSIZE
)
254 buf_len
= ps
->null_term
? MC_PIPE_BUFSIZE
- 1 : MC_PIPE_BUFSIZE
;
258 read_len
= read (ps
->fd
, ps
->buf
, buf_len
);
260 while (read_len
< 0 && errno
== EINTR
);
265 ps
->len
= MC_PIPE_ERROR_READ
;
268 else if (read_len
== 0)
270 ps
->len
= MC_PIPE_STREAM_EOF
;
277 ps
->buf
[(size_t) ps
->len
] = '\0';
281 /* --------------------------------------------------------------------------------------------- */
282 /*** public functions ****************************************************************************/
283 /* --------------------------------------------------------------------------------------------- */
286 get_owner (uid_t uid
)
290 static uid_t uid_last
;
292 name
= i_cache_match ((int) uid
, uid_cache
, UID_CACHE_SIZE
);
296 pwd
= getpwuid (uid
);
299 i_cache_add ((int) uid
, uid_cache
, UID_CACHE_SIZE
, pwd
->pw_name
, (int *) &uid_last
);
304 static char ibuf
[10];
306 g_snprintf (ibuf
, sizeof (ibuf
), "%d", (int) uid
);
311 /* --------------------------------------------------------------------------------------------- */
314 get_group (gid_t gid
)
318 static gid_t gid_last
;
320 name
= i_cache_match ((int) gid
, gid_cache
, GID_CACHE_SIZE
);
324 grp
= getgrgid (gid
);
327 i_cache_add ((int) gid
, gid_cache
, GID_CACHE_SIZE
, grp
->gr_name
, (int *) &gid_last
);
332 static char gbuf
[10];
334 g_snprintf (gbuf
, sizeof (gbuf
), "%d", (int) gid
);
339 /* --------------------------------------------------------------------------------------------- */
340 /* Since ncurses uses a handler that automatically refreshes the */
341 /* screen after a SIGCONT, and we don't want this behavior when */
342 /* spawning a child, we save the original handler here */
345 save_stop_handler (void)
347 sigaction (SIGTSTP
, NULL
, &startup_handler
);
350 /* --------------------------------------------------------------------------------------------- */
352 * Wrapper for _exit() system call.
353 * The _exit() function has gcc's attribute 'noreturn', and this is reason why we can't
356 * @param status exit code
365 /* --------------------------------------------------------------------------------------------- */
367 * Call external programs.
369 * @parameter flags addition conditions for running external programs.
370 * @parameter shell shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
371 * Shell (or command) will be found in paths described in PATH variable
372 * (if shell parameter doesn't begin from path delimiter)
373 * @parameter command Command for shell (or first parameter for command, if flags contain EXECUTE_AS_SHELL)
374 * @return 0 if successfull, -1 otherwise
378 my_system (int flags
, const char *shell
, const char *command
)
380 return my_systeml (flags
, shell
, command
, NULL
);
383 /* --------------------------------------------------------------------------------------------- */
385 * Call external programs with various parameters number.
387 * @parameter flags addition conditions for running external programs.
388 * @parameter shell shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
389 * Shell (or command) will be found in pathes described in PATH variable
390 * (if shell parameter doesn't begin from path delimiter)
391 * @parameter ... Command for shell with addition parameters for shell
392 * (or parameters for command, if flags contain EXECUTE_AS_SHELL).
393 * Should be NULL terminated.
394 * @return 0 if successfull, -1 otherwise
399 my_systeml (int flags
, const char *shell
, ...)
401 GPtrArray
*args_array
;
406 args_array
= g_ptr_array_new ();
408 va_start (vargs
, shell
);
409 while ((one_arg
= va_arg (vargs
, char *)) != NULL
)
410 g_ptr_array_add (args_array
, one_arg
);
413 g_ptr_array_add (args_array
, NULL
);
414 status
= my_systemv_flags (flags
, shell
, (char *const *) args_array
->pdata
);
416 g_ptr_array_free (args_array
, TRUE
);
421 /* --------------------------------------------------------------------------------------------- */
423 * Call external programs with array of strings as parameters.
425 * @parameter command command to run. Command will be found in paths described in PATH variable
426 * (if command parameter doesn't begin from path delimiter)
427 * @parameter argv Array of strings (NULL-terminated) with parameters for command
428 * @return 0 if successfull, -1 otherwise
432 my_systemv (const char *command
, char *const argv
[])
434 my_fork_state_t fork_state
;
436 my_system_sigactions_t sigactions
;
438 my_system__save_sigaction_handlers (&sigactions
);
440 fork_state
= my_fork ();
448 signal (SIGINT
, SIG_DFL
);
449 signal (SIGQUIT
, SIG_DFL
);
450 signal (SIGTSTP
, SIG_DFL
);
451 signal (SIGCHLD
, SIG_DFL
);
453 execvp (command
, argv
);
454 my_exit (127); /* Exec error */
461 my_system__restore_sigaction_handlers (&sigactions
);
466 /* --------------------------------------------------------------------------------------------- */
468 * Call external programs with flags and with array of strings as parameters.
470 * @parameter flags addition conditions for running external programs.
471 * @parameter command shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
472 * Shell (or command) will be found in paths described in PATH variable
473 * (if shell parameter doesn't begin from path delimiter)
474 * @parameter argv Array of strings (NULL-terminated) with parameters for command
475 * @return 0 if successfull, -1 otherwise
479 my_systemv_flags (int flags
, const char *command
, char *const argv
[])
481 char *execute_name
= NULL
;
482 GPtrArray
*args_array
;
485 args_array
= my_system_make_arg_array (flags
, command
, &execute_name
);
487 for (; argv
!= NULL
&& *argv
!= NULL
; argv
++)
488 g_ptr_array_add (args_array
, *argv
);
490 g_ptr_array_add (args_array
, NULL
);
491 status
= my_systemv (execute_name
, (char *const *) args_array
->pdata
);
493 g_free (execute_name
);
494 g_ptr_array_free (args_array
, TRUE
);
499 /* --------------------------------------------------------------------------------------------- */
501 * Create pipe and run child process.
503 * @parameter command command line of child process
504 * @paremeter error contains pointer to object to handle error code and message
506 * @return newly created object of mc_pipe_t class in success, NULL otherwise
510 mc_popen (const char *command
, GError
** error
)
515 p
= g_try_new (mc_pipe_t
, 1);
518 mc_replace_error (error
, MC_PIPE_ERROR_CREATE_PIPE
, "%s",
519 _("Cannot create pipe descriptor"));
523 if (!g_shell_parse_argv (command
, NULL
, &argv
, error
))
525 mc_replace_error (error
, MC_PIPE_ERROR_PARSE_COMMAND
, "%s",
526 _("Cannot parse command for pipe"));
530 if (!g_spawn_async_with_pipes
531 (NULL
, argv
, NULL
, G_SPAWN_DO_NOT_REAP_CHILD
| G_SPAWN_SEARCH_PATH
, NULL
, NULL
,
532 &p
->child_pid
, NULL
, &p
->out
.fd
, &p
->err
.fd
, error
))
534 mc_replace_error (error
, MC_PIPE_ERROR_CREATE_PIPE_STREAM
, "%s",
535 _("Cannot create pipe streams"));
541 p
->out
.buf
[0] = '\0';
542 p
->out
.len
= MC_PIPE_BUFSIZE
;
543 p
->out
.null_term
= FALSE
;
545 p
->err
.buf
[0] = '\0';
546 p
->err
.len
= MC_PIPE_BUFSIZE
;
547 p
->err
.null_term
= FALSE
;
556 /* --------------------------------------------------------------------------------------------- */
558 * Read stdout and stderr of pipe asynchronously.
560 * @parameter p pipe descriptor
562 * The lengths of read data contain in p->out.len and p->err.len.
563 * Before read, p->xxx.len is an input:
564 * p->xxx.len > 0: do read stream p->xxx and store data in p->xxx.buf;
565 * p->xxx.len <= 0: do not read stream p->xxx.
567 * After read, p->xxx.len is an output and contains the following:
568 * p->xxx.len > 0: an actual length of read data stored in p->xxx.buf;
569 * p->xxx.len == MC_PIPE_STREAM_EOF: EOF of stream p->xxx;
570 * p->xxx.len == MC_PIPE_STREAM_UNREAD: stream p->xxx was not read;
571 * p->xxx.len == MC_PIPE_ERROR_READ: reading error, and p->xxx.errno is set appropriately.
573 * @paremeter error contains pointer to object to handle error code and message
577 mc_pread (mc_pipe_t
* p
, GError
** error
)
579 gboolean read_out
, read_err
;
587 read_out
= p
->out
.fd
>= 0 && p
->out
.len
> 0;
588 read_err
= p
->err
.fd
>= 0 && p
->err
.len
> 0;
590 if (!read_out
&& !read_err
)
592 p
->out
.len
= MC_PIPE_STREAM_UNREAD
;
593 p
->err
.len
= MC_PIPE_STREAM_UNREAD
;
600 FD_SET (p
->out
.fd
, &fds
);
606 FD_SET (p
->err
.fd
, &fds
);
607 maxfd
= max (maxfd
, p
->err
.fd
);
611 res
= select (maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
612 if (res
< 0 && errno
!= EINTR
)
614 mc_propagate_error (error
, MC_PIPE_ERROR_READ
,
616 ("Unexpected error in select() reading data from a child process:\n%s"),
617 unix_error_string (errno
));
622 mc_pread_stream (&p
->out
, &fds
);
624 p
->out
.len
= MC_PIPE_STREAM_UNREAD
;
627 mc_pread_stream (&p
->err
, &fds
);
629 p
->err
.len
= MC_PIPE_STREAM_UNREAD
;
632 /* --------------------------------------------------------------------------------------------- */
634 * Close pipe and destroy pipe descriptor.
636 * @paremeter p pipe descriptor
637 * @paremeter error contains pointer to object to handle error code and message
641 mc_pclose (mc_pipe_t
* p
, GError
** error
)
646 res
= close (p
->out
.fd
);
648 res
= close (p
->err
.fd
);
654 res
= waitpid (p
->child_pid
, &status
, 0);
656 while (res
< 0 && errno
== EINTR
);
659 mc_replace_error (error
, MC_PIPE_ERROR_READ
, _("Unexpected error in waitpid():\n%s"),
660 unix_error_string (errno
));
665 /* --------------------------------------------------------------------------------------------- */
668 * Perform tilde expansion if possible.
670 * @param directory pointer to the path
672 * @return newly allocated string, even if it's unchanged.
676 tilde_expand (const char *directory
)
678 struct passwd
*passwd
;
681 if (*directory
!= '~')
682 return g_strdup (directory
);
686 /* d = "~" or d = "~/" */
687 if (*p
== '\0' || IS_PATH_SEP (*p
))
689 passwd
= getpwuid (geteuid ());
690 q
= IS_PATH_SEP (*p
) ? p
+ 1 : "";
694 q
= strchr (p
, PATH_SEP
);
697 passwd
= getpwnam (p
);
703 name
= g_strndup (p
, q
- p
);
704 passwd
= getpwnam (name
);
710 /* If we can't figure the user name, leave tilde unexpanded */
712 return g_strdup (directory
);
714 return g_strconcat (passwd
->pw_dir
, PATH_SEP_STR
, q
, (char *) NULL
);
717 /* --------------------------------------------------------------------------------------------- */
719 * Creates a pipe to hold standard error for a later analysis.
720 * The pipe can hold 4096 bytes. Make sure no more is written
721 * or a deadlock might occur.
725 open_error_pipe (void)
727 if (pipe (error_pipe
) < 0)
729 message (D_NORMAL
, _("Warning"), _("Pipe failed"));
731 old_error
= dup (STDERR_FILENO
);
732 if (old_error
< 0 || close (STDERR_FILENO
) != 0 || dup (error_pipe
[1]) != STDERR_FILENO
)
734 message (D_NORMAL
, _("Warning"), _("Dup failed"));
736 close (error_pipe
[0]);
742 * Settng stderr in nonblocking mode as we close it earlier, than
743 * program stops. We try to read some error at program startup,
744 * but we should not block on it.
746 * TODO: make piped stdin/stderr poll()/select()able to get rid
750 fd_flags
= fcntl (error_pipe
[0], F_GETFL
, NULL
);
753 fd_flags
|= O_NONBLOCK
;
754 if (fcntl (error_pipe
[0], F_SETFL
, fd_flags
) == -1)
756 /* TODO: handle it somehow */
760 /* we never write there */
761 close (error_pipe
[1]);
765 /* --------------------------------------------------------------------------------------------- */
769 * @param error '-1' - ignore errors, '0' - display warning, '1' - display error
770 * @param text is prepended to the error message from the pipe
772 * @return not 0 if an error was displayed
776 close_error_pipe (int error
, const char *text
)
779 char msg
[MAX_PIPE_SIZE
];
783 if (error_pipe
[0] == -1)
786 if (error
< 0 || (error
> 0 && (error
& D_ERROR
) != 0))
789 title
= _("Warning");
792 if (dup2 (old_error
, STDERR_FILENO
) == -1)
797 message (error
, MSG_ERROR
, _("Error dup'ing old error pipe"));
801 len
= read (error_pipe
[0], msg
, sizeof (msg
) - 1);
805 close (error_pipe
[0]);
809 return 0; /* Just ignore error message */
813 return 0; /* Nothing to show */
815 /* Show message from pipe */
816 message (error
, title
, "%s", msg
);
820 /* Show given text and possible message from pipe */
821 message (error
, title
, "%s\n%s", text
, msg
);
826 /* --------------------------------------------------------------------------------------------- */
828 * Canonicalize path, and return a new path. Do everything in place.
829 * The new path differs from path in:
830 * Multiple '/'s are collapsed to a single '/'.
831 * Leading './'s and trailing '/.'s are removed.
832 * Trailing '/'s are removed.
833 * Non-leading '../'s and trailing '..'s are handled by removing
834 * portions of the path.
835 * Well formed UNC paths are modified only in the local part.
839 custom_canonicalize_pathname (char *path
, CANON_PATH_FLAGS flags
)
842 char *lpath
= path
; /* path without leading UNC part */
843 const size_t url_delim_len
= strlen (VFS_PATH_URL_DELIMITER
);
845 /* Detect and preserve UNC paths: //server/... */
846 if ((flags
& CANON_PATH_GUARDUNC
) != 0 && IS_PATH_SEP (path
[0]) && IS_PATH_SEP (path
[1]))
849 while (p
[0] != '\0' && !IS_PATH_SEP (p
[0]))
851 if (IS_PATH_SEP (p
[0]) && p
> path
+ 2)
855 if (!lpath
[0] || !lpath
[1])
858 if (flags
& CANON_PATH_JOINSLASHES
)
860 /* Collapse multiple slashes */
864 if (IS_PATH_SEP (p
[0]) && IS_PATH_SEP (p
[1]) && (p
== lpath
|| *(p
- 1) != ':'))
867 while (IS_PATH_SEP (*(++s
)))
875 if (flags
& CANON_PATH_JOINSLASHES
)
877 /* Collapse "/./" -> "/" */
881 if (IS_PATH_SEP (p
[0]) && p
[1] == '.' && IS_PATH_SEP (p
[2]))
888 if (flags
& CANON_PATH_REMSLASHDOTS
)
892 /* Remove trailing slashes */
893 p
= lpath
+ strlen (lpath
) - 1;
894 while (p
> lpath
&& IS_PATH_SEP (*p
))
896 if (p
>= lpath
+ url_delim_len
- 1
897 && strncmp (p
- url_delim_len
+ 1, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
902 /* Remove leading "./" */
903 if (lpath
[0] == '.' && IS_PATH_SEP (lpath
[1]))
912 str_move (lpath
, lpath
+ 2);
916 /* Remove trailing "/" or "/." */
917 len
= strlen (lpath
);
920 if (IS_PATH_SEP (lpath
[len
- 1])
921 && (len
< url_delim_len
922 || strncmp (lpath
+ len
- url_delim_len
, VFS_PATH_URL_DELIMITER
,
923 url_delim_len
) != 0))
925 lpath
[len
- 1] = '\0';
929 if (lpath
[len
- 1] == '.' && IS_PATH_SEP (lpath
[len
- 2]))
938 lpath
[len
- 2] = '\0';
944 if (flags
& CANON_PATH_REMDOUBLEDOTS
)
947 const size_t enc_prefix_len
= strlen (VFS_ENCODING_PREFIX
);
948 #endif /* HAVE_CHARSET */
950 /* Collapse "/.." with the previous part of path */
952 while (p
[0] && p
[1] && p
[2])
954 if (!IS_PATH_SEP (p
[0]) || p
[1] != '.' || p
[2] != '.'
955 || (!IS_PATH_SEP (p
[3]) && p
[3] != '\0'))
961 /* search for the previous token */
963 if (s
>= lpath
+ url_delim_len
- 2
964 && strncmp (s
- url_delim_len
+ 2, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
966 s
-= (url_delim_len
- 2);
967 while (s
>= lpath
&& !IS_PATH_SEP (*s
--))
973 if (s
- url_delim_len
> lpath
974 && strncmp (s
- url_delim_len
, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
976 char *vfs_prefix
= s
- url_delim_len
;
979 while (vfs_prefix
> lpath
&& !IS_PATH_SEP (*--vfs_prefix
))
981 if (IS_PATH_SEP (*vfs_prefix
))
983 *(s
- url_delim_len
) = '\0';
985 vclass
= vfs_prefix_to_class (vfs_prefix
);
986 *(s
- url_delim_len
) = *VFS_PATH_URL_DELIMITER
;
990 struct vfs_s_subclass
*sub
= (struct vfs_s_subclass
*) vclass
->data
;
991 if (sub
!= NULL
&& sub
->flags
& VFS_S_REMOTE
)
999 if (IS_PATH_SEP (*s
))
1007 /* If the previous token is "..", we cannot collapse it */
1008 if (s
[0] == '.' && s
[1] == '.' && s
+ 2 == p
)
1016 if (s
== lpath
&& IS_PATH_SEP (*s
))
1018 /* "/../foo" -> "/foo" */
1019 str_move (s
+ 1, p
+ 4);
1023 /* "token/../foo" -> "foo" */
1025 if ((strncmp (s
, VFS_ENCODING_PREFIX
, enc_prefix_len
) == 0)
1026 && (is_supported_encoding (s
+ enc_prefix_len
)))
1027 /* special case: remove encoding */
1028 str_move (s
, p
+ 1);
1030 #endif /* HAVE_CHARSET */
1031 str_move (s
, p
+ 4);
1033 p
= (s
> lpath
) ? s
- 1 : s
;
1040 /* "token/.." -> "." */
1041 if (!IS_PATH_SEP (lpath
[0]))
1047 /* "foo/token/.." -> "foo" */
1051 else if ((strncmp (s
, VFS_ENCODING_PREFIX
, enc_prefix_len
) == 0)
1052 && (is_supported_encoding (s
+ enc_prefix_len
)))
1054 /* special case: remove encoding */
1059 /* search for the previous token */
1060 /* IS_PATH_SEP (s[-1]) */
1062 while (p
>= lpath
&& !IS_PATH_SEP (*p
))
1068 #endif /* HAVE_CHARSET */
1071 if (s
>= lpath
+ url_delim_len
1072 && strncmp (s
- url_delim_len
, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
1084 /* --------------------------------------------------------------------------------------------- */
1087 canonicalize_pathname (char *path
)
1089 custom_canonicalize_pathname (path
, CANON_PATH_ALL
);
1092 /* --------------------------------------------------------------------------------------------- */
1094 #ifdef HAVE_GET_PROCESS_STATS
1096 gettimeofday (struct timeval
*tp
, void *tzp
)
1098 return get_process_stats (tp
, PS_SELF
, 0, 0);
1100 #endif /* HAVE_GET_PROCESS_STATS */
1102 /* --------------------------------------------------------------------------------------------- */
1104 #ifndef HAVE_REALPATH
1106 mc_realpath (const char *path
, char *resolved_path
)
1108 char copy_path
[PATH_MAX
];
1109 char link_path
[PATH_MAX
];
1110 char got_path
[PATH_MAX
];
1111 char *new_path
= got_path
;
1116 /* Make a copy of the source path since we may need to modify it. */
1117 if (strlen (path
) >= PATH_MAX
- 2)
1119 errno
= ENAMETOOLONG
;
1122 strcpy (copy_path
, path
);
1124 max_path
= copy_path
+ PATH_MAX
- 2;
1125 /* If it's a relative pathname use getwd for starters. */
1126 if (!IS_PATH_SEP (*path
))
1128 new_path
= g_get_current_dir ();
1129 if (new_path
== NULL
)
1131 strcpy (got_path
, "");
1135 g_snprintf (got_path
, sizeof (got_path
), "%s", new_path
);
1137 new_path
= got_path
;
1140 new_path
+= strlen (got_path
);
1141 if (!IS_PATH_SEP (new_path
[-1]))
1142 *new_path
++ = PATH_SEP
;
1146 *new_path
++ = PATH_SEP
;
1149 /* Expand each slash-separated pathname component. */
1150 while (*path
!= '\0')
1152 /* Ignore stray "/". */
1153 if (IS_PATH_SEP (*path
))
1161 if (path
[1] == '\0' || IS_PATH_SEP (path
[1]))
1168 if (path
[2] == '\0' || IS_PATH_SEP (path
[2]))
1171 /* Ignore ".." at root. */
1172 if (new_path
== got_path
+ 1)
1174 /* Handle ".." by backing up. */
1175 while (!IS_PATH_SEP ((--new_path
)[-1]))
1181 /* Safely copy the next pathname component. */
1182 while (*path
!= '\0' && !IS_PATH_SEP (*path
))
1184 if (path
> max_path
)
1186 errno
= ENAMETOOLONG
;
1189 *new_path
++ = *path
++;
1192 /* Protect against infinite loops. */
1193 if (readlinks
++ > MAXSYMLINKS
)
1198 /* See if latest pathname component is a symlink. */
1200 n
= readlink (got_path
, link_path
, PATH_MAX
- 1);
1203 /* EINVAL means the file exists but isn't a symlink. */
1204 if (errno
!= EINVAL
)
1206 /* Make sure it's null terminated. */
1208 strcpy (resolved_path
, got_path
);
1214 /* Note: readlink doesn't add the null byte. */
1215 link_path
[n
] = '\0';
1216 if (IS_PATH_SEP (*link_path
))
1217 /* Start over for an absolute symlink. */
1218 new_path
= got_path
;
1220 /* Otherwise back up over this component. */
1221 while (!IS_PATH_SEP (*(--new_path
)))
1223 /* Safe sex check. */
1224 if (strlen (path
) + n
>= PATH_MAX
- 2)
1226 errno
= ENAMETOOLONG
;
1229 /* Insert symlink contents into path. */
1230 strcat (link_path
, path
);
1231 strcpy (copy_path
, link_path
);
1234 #endif /* S_IFLNK */
1235 *new_path
++ = PATH_SEP
;
1237 /* Delete trailing slash but don't whomp a lone slash. */
1238 if (new_path
!= got_path
+ 1 && IS_PATH_SEP (new_path
[-1]))
1240 /* Make sure it's null terminated. */
1242 strcpy (resolved_path
, got_path
);
1243 return resolved_path
;
1245 #endif /* HAVE_REALPATH */
1247 /* --------------------------------------------------------------------------------------------- */
1249 * Return the index of the permissions triplet
1254 get_user_permissions (struct stat
*st
)
1256 static gboolean initialized
= FALSE
;
1257 static gid_t
*groups
;
1266 ngroups
= getgroups (0, NULL
);
1268 ngroups
= 0; /* ignore errors */
1270 /* allocate space for one element in addition to what
1271 * will be filled by getgroups(). */
1272 groups
= g_new (gid_t
, ngroups
+ 1);
1276 ngroups
= getgroups (ngroups
, groups
);
1278 ngroups
= 0; /* ignore errors */
1281 /* getgroups() may or may not return the effective group ID,
1282 * so we always include it at the end of the list. */
1283 groups
[ngroups
++] = getegid ();
1288 if (st
->st_uid
== uid
|| uid
== 0)
1291 for (i
= 0; i
< ngroups
; i
++)
1293 if (st
->st_gid
== groups
[i
])
1300 /* --------------------------------------------------------------------------------------------- */
1302 * Build filename from arguments.
1303 * Like to g_build_filename(), but respect VFS_PATH_URL_DELIMITER
1307 mc_build_filenamev (const char *first_element
, va_list args
)
1310 const char *element
= first_element
;
1314 if (element
== NULL
)
1317 path
= g_string_new ("");
1319 absolute
= IS_PATH_SEP (*first_element
);
1323 if (*element
== '\0')
1324 element
= va_arg (args
, char *);
1331 tmp_element
= g_strdup (element
);
1333 element
= va_arg (args
, char *);
1335 canonicalize_pathname (tmp_element
);
1336 len
= strlen (tmp_element
);
1337 start
= IS_PATH_SEP (tmp_element
[0]) ? tmp_element
+ 1 : tmp_element
;
1339 g_string_append (path
, start
);
1340 if (!IS_PATH_SEP (tmp_element
[len
- 1]) && element
!= NULL
)
1341 g_string_append_c (path
, PATH_SEP
);
1343 g_free (tmp_element
);
1346 while (element
!= NULL
);
1349 g_string_prepend_c (path
, PATH_SEP
);
1351 ret
= g_string_free (path
, FALSE
);
1352 canonicalize_pathname (ret
);
1357 /* --------------------------------------------------------------------------------------------- */
1359 * Build filename from arguments.
1360 * Like to g_build_filename(), but respect VFS_PATH_URL_DELIMITER
1364 mc_build_filename (const char *first_element
, ...)
1369 if (first_element
== NULL
)
1372 va_start (args
, first_element
);
1373 ret
= mc_build_filenamev (first_element
, args
);
1378 /* --------------------------------------------------------------------------------------------- */