2 Various utilities - Unix variants
4 Copyright (C) 1994-2023
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
13 Andrew Borodin <aborodin@vmail.ru> 2010-2022
15 The mc_realpath routine is mostly from uClibc package, written
16 by Rick Sladkey <jrs@world.std.com>
18 This file is part of the Midnight Commander.
20 The Midnight Commander is free software: you can redistribute it
21 and/or modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation, either version 3 of the License,
23 or (at your option) any later version.
25 The Midnight Commander is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * \brief Source: various utilities - Unix variant
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
51 #include <sys/types.h>
53 #ifdef HAVE_SYS_SELECT_H
54 #include <sys/select.h>
60 #include "lib/global.h"
62 #include "lib/unixcompat.h"
63 #include "lib/vfs/vfs.h" /* VFS_ENCODING_PREFIX */
64 #include "lib/strutil.h" /* str_move() */
66 #include "lib/widget.h" /* message() */
67 #include "lib/vfs/xdirentry.h"
70 #include "lib/charsets.h"
75 /*** global variables ****************************************************************************/
77 struct sigaction startup_handler
;
79 /*** file scope macro definitions ****************************************************************/
81 #define UID_CACHE_SIZE 200
82 #define GID_CACHE_SIZE 30
84 /*** file scope type declarations ****************************************************************/
101 struct sigaction intr
;
102 struct sigaction quit
;
103 struct sigaction stop
;
104 } my_system_sigactions_t
;
106 /*** forward declarations (file scope functions) *************************************************/
108 /*** file scope variables ************************************************************************/
110 static int_cache uid_cache
[UID_CACHE_SIZE
];
111 static int_cache gid_cache
[GID_CACHE_SIZE
];
113 /* --------------------------------------------------------------------------------------------- */
114 /*** file scope functions ************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
118 i_cache_match (int id
, int_cache
* cache
, int size
)
122 for (i
= 0; i
< size
; i
++)
123 if (cache
[i
].index
== id
)
124 return cache
[i
].string
;
128 /* --------------------------------------------------------------------------------------------- */
131 i_cache_add (int id
, int_cache
* cache
, int size
, char *text
, int *last
)
133 g_free (cache
[*last
].string
);
134 cache
[*last
].string
= g_strdup (text
);
135 cache
[*last
].index
= id
;
136 *last
= ((*last
) + 1) % size
;
139 /* --------------------------------------------------------------------------------------------- */
141 static my_fork_state_t
150 fprintf (stderr
, "\n\nfork () = -1\n");
161 if (waitpid (pid
, &status
, 0) > 0)
162 return WEXITSTATUS (status
) == 0 ? FORK_PARENT
: FORK_ERROR
;
169 /* --------------------------------------------------------------------------------------------- */
172 my_system__save_sigaction_handlers (my_system_sigactions_t
* sigactions
)
174 struct sigaction ignore
;
176 memset (&ignore
, 0, sizeof (ignore
));
177 ignore
.sa_handler
= SIG_IGN
;
178 sigemptyset (&ignore
.sa_mask
);
180 sigaction (SIGINT
, &ignore
, &sigactions
->intr
);
181 sigaction (SIGQUIT
, &ignore
, &sigactions
->quit
);
183 /* Restore the original SIGTSTP handler, we don't want ncurses' */
184 /* handler messing the screen after the SIGCONT */
185 sigaction (SIGTSTP
, &startup_handler
, &sigactions
->stop
);
188 /* --------------------------------------------------------------------------------------------- */
191 my_system__restore_sigaction_handlers (my_system_sigactions_t
* sigactions
)
193 sigaction (SIGINT
, &sigactions
->intr
, NULL
);
194 sigaction (SIGQUIT
, &sigactions
->quit
, NULL
);
195 sigaction (SIGTSTP
, &sigactions
->stop
, NULL
);
198 /* --------------------------------------------------------------------------------------------- */
201 my_system_make_arg_array (int flags
, const char *shell
, char **execute_name
)
203 GPtrArray
*args_array
;
205 args_array
= g_ptr_array_new ();
207 if ((flags
& EXECUTE_AS_SHELL
) != 0)
209 g_ptr_array_add (args_array
, (gpointer
) shell
);
210 g_ptr_array_add (args_array
, (gpointer
) "-c");
211 *execute_name
= g_strdup (shell
);
217 shell_token
= shell
!= NULL
? strchr (shell
, ' ') : NULL
;
218 if (shell_token
== NULL
)
219 *execute_name
= g_strdup (shell
);
221 *execute_name
= g_strndup (shell
, (gsize
) (shell_token
- shell
));
223 g_ptr_array_add (args_array
, (gpointer
) shell
);
228 /* --------------------------------------------------------------------------------------------- */
231 mc_pread_stream (mc_pipe_stream_t
* ps
, const fd_set
* fds
)
236 if (!FD_ISSET (ps
->fd
, fds
))
238 ps
->len
= MC_PIPE_STREAM_UNREAD
;
242 buf_len
= (size_t) ps
->len
;
244 if (buf_len
>= MC_PIPE_BUFSIZE
)
245 buf_len
= ps
->null_term
? MC_PIPE_BUFSIZE
- 1 : MC_PIPE_BUFSIZE
;
249 read_len
= read (ps
->fd
, ps
->buf
, buf_len
);
251 while (read_len
< 0 && errno
== EINTR
);
256 ps
->len
= MC_PIPE_ERROR_READ
;
259 else if (read_len
== 0)
261 ps
->len
= MC_PIPE_STREAM_EOF
;
268 ps
->buf
[(size_t) ps
->len
] = '\0';
274 /* --------------------------------------------------------------------------------------------- */
275 /*** public functions ****************************************************************************/
276 /* --------------------------------------------------------------------------------------------- */
279 get_owner (uid_t uid
)
283 static uid_t uid_last
;
285 name
= i_cache_match ((int) uid
, uid_cache
, UID_CACHE_SIZE
);
289 pwd
= getpwuid (uid
);
292 i_cache_add ((int) uid
, uid_cache
, UID_CACHE_SIZE
, pwd
->pw_name
, (int *) &uid_last
);
297 static char ibuf
[10];
299 g_snprintf (ibuf
, sizeof (ibuf
), "%d", (int) uid
);
304 /* --------------------------------------------------------------------------------------------- */
307 get_group (gid_t gid
)
311 static gid_t gid_last
;
313 name
= i_cache_match ((int) gid
, gid_cache
, GID_CACHE_SIZE
);
317 grp
= getgrgid (gid
);
320 i_cache_add ((int) gid
, gid_cache
, GID_CACHE_SIZE
, grp
->gr_name
, (int *) &gid_last
);
325 static char gbuf
[10];
327 g_snprintf (gbuf
, sizeof (gbuf
), "%d", (int) gid
);
332 /* --------------------------------------------------------------------------------------------- */
333 /* Since ncurses uses a handler that automatically refreshes the */
334 /* screen after a SIGCONT, and we don't want this behavior when */
335 /* spawning a child, we save the original handler here */
338 save_stop_handler (void)
340 sigaction (SIGTSTP
, NULL
, &startup_handler
);
343 /* --------------------------------------------------------------------------------------------- */
345 * Wrapper for _exit() system call.
346 * The _exit() function has gcc's attribute 'noreturn', and this is reason why we can't
349 * @param status exit code
353 /* __attribute__ ((noreturn)) */
359 /* --------------------------------------------------------------------------------------------- */
361 * Call external programs.
363 * @parameter flags addition conditions for running external programs.
364 * @parameter shell shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
365 * Shell (or command) will be found in paths described in PATH variable
366 * (if shell parameter doesn't begin from path delimiter)
367 * @parameter command Command for shell (or first parameter for command, if flags contain EXECUTE_AS_SHELL)
368 * @return 0 if successful, -1 otherwise
372 my_system (int flags
, const char *shell
, const char *command
)
374 return my_systeml (flags
, shell
, command
, NULL
);
377 /* --------------------------------------------------------------------------------------------- */
379 * Call external programs with various parameters number.
381 * @parameter flags addition conditions for running external programs.
382 * @parameter shell shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
383 * Shell (or command) will be found in paths described in PATH variable
384 * (if shell parameter doesn't begin from path delimiter)
385 * @parameter ... Command for shell with addition parameters for shell
386 * (or parameters for command, if flags contain EXECUTE_AS_SHELL).
387 * Should be NULL terminated.
388 * @return 0 if successful, -1 otherwise
392 my_systeml (int flags
, const char *shell
, ...)
394 GPtrArray
*args_array
;
399 args_array
= g_ptr_array_new ();
401 va_start (vargs
, shell
);
402 while ((one_arg
= va_arg (vargs
, char *)) != NULL
)
403 g_ptr_array_add (args_array
, one_arg
);
406 g_ptr_array_add (args_array
, NULL
);
407 status
= my_systemv_flags (flags
, shell
, (char *const *) args_array
->pdata
);
409 g_ptr_array_free (args_array
, TRUE
);
414 /* --------------------------------------------------------------------------------------------- */
416 * Call external programs with array of strings as parameters.
418 * @parameter command command to run. Command will be found in paths described in PATH variable
419 * (if command parameter doesn't begin from path delimiter)
420 * @parameter argv Array of strings (NULL-terminated) with parameters for command
421 * @return 0 if successful, -1 otherwise
425 my_systemv (const char *command
, char *const argv
[])
427 my_fork_state_t fork_state
;
429 my_system_sigactions_t sigactions
;
431 my_system__save_sigaction_handlers (&sigactions
);
433 fork_state
= my_fork ();
441 signal (SIGINT
, SIG_DFL
);
442 signal (SIGQUIT
, SIG_DFL
);
443 signal (SIGTSTP
, SIG_DFL
);
444 signal (SIGCHLD
, SIG_DFL
);
446 execvp (command
, argv
);
447 my_exit (127); /* Exec error */
450 /* no break here, or unreachable-code warning by no returning my_exit() */
455 my_system__restore_sigaction_handlers (&sigactions
);
460 /* --------------------------------------------------------------------------------------------- */
462 * Call external programs with flags and with array of strings as parameters.
464 * @parameter flags addition conditions for running external programs.
465 * @parameter command shell (if flags contain EXECUTE_AS_SHELL), command to run otherwise.
466 * Shell (or command) will be found in paths described in PATH variable
467 * (if shell parameter doesn't begin from path delimiter)
468 * @parameter argv Array of strings (NULL-terminated) with parameters for command
469 * @return 0 if successful, -1 otherwise
473 my_systemv_flags (int flags
, const char *command
, char *const argv
[])
475 char *execute_name
= NULL
;
476 GPtrArray
*args_array
;
479 args_array
= my_system_make_arg_array (flags
, command
, &execute_name
);
481 for (; argv
!= NULL
&& *argv
!= NULL
; argv
++)
482 g_ptr_array_add (args_array
, *argv
);
484 g_ptr_array_add (args_array
, NULL
);
485 status
= my_systemv (execute_name
, (char *const *) args_array
->pdata
);
487 g_free (execute_name
);
488 g_ptr_array_free (args_array
, TRUE
);
493 /* --------------------------------------------------------------------------------------------- */
495 * Create pipe and run child process.
497 * @parameter command command line of child process
498 * @parameter read_out do or don't read the stdout of child process
499 * @parameter read_err do or don't read the stderr of child process
500 * @parameter error contains pointer to object to handle error code and message
502 * @return newly created object of mc_pipe_t class in success, NULL otherwise
506 mc_popen (const char *command
, gboolean read_out
, gboolean read_err
, GError
** error
)
509 const char *const argv
[] = { "/bin/sh", "sh", "-c", command
, NULL
};
511 p
= g_try_new (mc_pipe_t
, 1);
514 mc_replace_error (error
, MC_PIPE_ERROR_CREATE_PIPE
, "%s",
515 _("Cannot create pipe descriptor"));
522 if (!g_spawn_async_with_pipes
523 (NULL
, (gchar
**) argv
, NULL
, G_SPAWN_DO_NOT_REAP_CHILD
| G_SPAWN_FILE_AND_ARGV_ZERO
, NULL
,
524 NULL
, &p
->child_pid
, NULL
, read_out
? &p
->out
.fd
: NULL
, read_err
? &p
->err
.fd
: NULL
,
527 mc_replace_error (error
, MC_PIPE_ERROR_CREATE_PIPE_STREAM
, "%s",
528 _("Cannot create pipe streams"));
532 p
->out
.buf
[0] = '\0';
533 p
->out
.len
= MC_PIPE_BUFSIZE
;
534 p
->out
.null_term
= FALSE
;
536 p
->err
.buf
[0] = '\0';
537 p
->err
.len
= MC_PIPE_BUFSIZE
;
538 p
->err
.null_term
= FALSE
;
547 /* --------------------------------------------------------------------------------------------- */
549 * Read stdout and stderr of pipe asynchronously.
551 * @parameter p pipe descriptor
553 * The lengths of read data contain in p->out.len and p->err.len.
555 * Before read, p->xxx.len is an input. It defines the number of data to read.
556 * Should not be greater than MC_PIPE_BUFSIZE.
558 * After read, p->xxx.len is an output and contains the following:
559 * p->xxx.len > 0: an actual length of read data stored in p->xxx.buf;
560 * p->xxx.len == MC_PIPE_STREAM_EOF: EOF of stream p->xxx;
561 * p->xxx.len == MC_PIPE_STREAM_UNREAD: stream p->xxx was not read;
562 * p->xxx.len == MC_PIPE_ERROR_READ: reading error, and p->xxx.errno is set appropriately.
564 * @parameter error contains pointer to object to handle error code and message
568 mc_pread (mc_pipe_t
* p
, GError
** error
)
570 gboolean read_out
, read_err
;
578 read_out
= p
->out
.fd
>= 0;
579 read_err
= p
->err
.fd
>= 0;
581 if (!read_out
&& !read_err
)
583 p
->out
.len
= MC_PIPE_STREAM_UNREAD
;
584 p
->err
.len
= MC_PIPE_STREAM_UNREAD
;
591 FD_SET (p
->out
.fd
, &fds
);
597 FD_SET (p
->err
.fd
, &fds
);
598 maxfd
= MAX (maxfd
, p
->err
.fd
);
602 res
= select (maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
603 if (res
< 0 && errno
!= EINTR
)
605 mc_propagate_error (error
, MC_PIPE_ERROR_READ
,
607 ("Unexpected error in select() reading data from a child process:\n%s"),
608 unix_error_string (errno
));
613 mc_pread_stream (&p
->out
, &fds
);
615 p
->out
.len
= MC_PIPE_STREAM_UNREAD
;
618 mc_pread_stream (&p
->err
, &fds
);
620 p
->err
.len
= MC_PIPE_STREAM_UNREAD
;
623 /* --------------------------------------------------------------------------------------------- */
625 * Reads a line from @stream. Reading stops after an EOL or a newline. If a newline is read,
626 * it is appended to the line.
628 * @stream mc_pipe_stream_t object
630 * @return newly created GString or NULL in case of EOL;
634 mc_pstream_get_string (mc_pipe_stream_t
* ps
)
638 gboolean escape
= FALSE
;
640 g_return_val_if_fail (ps
!= NULL
, NULL
);
645 size
= ps
->len
- ps
->pos
;
650 s
= ps
->buf
+ ps
->pos
;
655 /* find '\0' or unescaped '\n' */
656 for (i
= 0; i
< size
&& !(s
[i
] == '\0' || (s
[i
] == '\n' && !escape
)); i
++)
657 escape
= s
[i
] == '\\' ? !escape
: FALSE
;
659 if (i
!= size
&& s
[i
] == '\n')
664 return g_string_new_len (s
, i
);
667 /* --------------------------------------------------------------------------------------------- */
669 * Close pipe and destroy pipe descriptor.
671 * @parameter p pipe descriptor
672 * @parameter error contains pointer to object to handle error code and message
676 mc_pclose (mc_pipe_t
* p
, GError
** error
)
682 mc_replace_error (error
, MC_PIPE_ERROR_READ
, "%s",
683 _("Cannot close pipe descriptor (p == NULL)"));
688 res
= close (p
->out
.fd
);
690 res
= close (p
->err
.fd
);
696 res
= waitpid (p
->child_pid
, &status
, 0);
698 while (res
< 0 && errno
== EINTR
);
701 mc_replace_error (error
, MC_PIPE_ERROR_READ
, _("Unexpected error in waitpid():\n%s"),
702 unix_error_string (errno
));
707 /* --------------------------------------------------------------------------------------------- */
710 * Perform tilde expansion if possible.
712 * @param directory pointer to the path
714 * @return newly allocated string, even if it's unchanged.
718 tilde_expand (const char *directory
)
720 struct passwd
*passwd
;
723 if (*directory
!= '~')
724 return g_strdup (directory
);
728 /* d = "~" or d = "~/" */
729 if (*p
== '\0' || IS_PATH_SEP (*p
))
731 passwd
= getpwuid (geteuid ());
732 q
= IS_PATH_SEP (*p
) ? p
+ 1 : "";
736 q
= strchr (p
, PATH_SEP
);
738 passwd
= getpwnam (p
);
743 name
= g_strndup (p
, q
- p
);
744 passwd
= getpwnam (name
);
750 /* If we can't figure the user name, leave tilde unexpanded */
752 return g_strdup (directory
);
754 return g_strconcat (passwd
->pw_dir
, PATH_SEP_STR
, q
, (char *) NULL
);
757 /* --------------------------------------------------------------------------------------------- */
761 * @param path path to file
762 * @param flags canonicalization flags
764 * All modifications of @path are made in place.
765 * Well formed UNC paths are modified only in the local part.
769 canonicalize_pathname_custom (char *path
, canon_path_flags_t flags
)
772 char *lpath
= path
; /* path without leading UNC part */
773 const size_t url_delim_len
= strlen (VFS_PATH_URL_DELIMITER
);
775 /* Detect and preserve UNC paths: //server/... */
776 if ((flags
& CANON_PATH_GUARDUNC
) != 0 && IS_PATH_SEP (path
[0]) && IS_PATH_SEP (path
[1]))
778 for (p
= path
+ 2; p
[0] != '\0' && !IS_PATH_SEP (p
[0]); p
++)
780 if (IS_PATH_SEP (p
[0]) && p
> path
+ 2)
784 if (lpath
[0] == '\0' || lpath
[1] == '\0')
787 if ((flags
& CANON_PATH_JOINSLASHES
) != 0)
789 /* Collapse multiple slashes */
790 for (p
= lpath
; *p
!= '\0'; p
++)
791 if (IS_PATH_SEP (p
[0]) && IS_PATH_SEP (p
[1]) && (p
== lpath
|| *(p
- 1) != ':'))
794 while (IS_PATH_SEP (*(++s
)))
799 /* Collapse "/./" -> "/" */
800 for (p
= lpath
; *p
!= '\0';)
801 if (IS_PATH_SEP (p
[0]) && p
[1] == '.' && IS_PATH_SEP (p
[2]))
807 if ((flags
& CANON_PATH_REMSLASHDOTS
) != 0)
811 /* Remove trailing slashes */
812 for (p
= lpath
+ strlen (lpath
) - 1; p
> lpath
&& IS_PATH_SEP (*p
); p
--)
814 if (p
>= lpath
+ url_delim_len
- 1
815 && strncmp (p
- url_delim_len
+ 1, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
820 /* Remove leading "./" */
821 if (lpath
[0] == '.' && IS_PATH_SEP (lpath
[1]))
823 if (lpath
[2] == '\0')
829 str_move (lpath
, lpath
+ 2);
832 /* Remove trailing "/" or "/." */
833 len
= strlen (lpath
);
837 if (IS_PATH_SEP (lpath
[len
- 1])
838 && (len
< url_delim_len
839 || strncmp (lpath
+ len
- url_delim_len
, VFS_PATH_URL_DELIMITER
,
840 url_delim_len
) != 0))
841 lpath
[len
- 1] = '\0';
842 else if (lpath
[len
- 1] == '.' && IS_PATH_SEP (lpath
[len
- 2]))
850 lpath
[len
- 2] = '\0';
854 /* Collapse "/.." with the previous part of path */
855 if ((flags
& CANON_PATH_REMDOUBLEDOTS
) != 0)
858 const size_t enc_prefix_len
= strlen (VFS_ENCODING_PREFIX
);
859 #endif /* HAVE_CHARSET */
861 for (p
= lpath
; p
[0] != '\0' && p
[1] != '\0' && p
[2] != '\0';)
863 if (!IS_PATH_SEP (p
[0]) || p
[1] != '.' || p
[2] != '.'
864 || (!IS_PATH_SEP (p
[3]) && p
[3] != '\0'))
870 /* search for the previous token */
872 if (s
>= lpath
+ url_delim_len
- 2
873 && strncmp (s
- url_delim_len
+ 2, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
875 s
-= (url_delim_len
- 2);
876 while (s
>= lpath
&& !IS_PATH_SEP (*s
--))
882 if (s
- url_delim_len
> lpath
883 && strncmp (s
- url_delim_len
, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
885 char *vfs_prefix
= s
- url_delim_len
;
888 while (vfs_prefix
> lpath
&& !IS_PATH_SEP (*--vfs_prefix
))
890 if (IS_PATH_SEP (*vfs_prefix
))
892 *(s
- url_delim_len
) = '\0';
894 vclass
= vfs_prefix_to_class (vfs_prefix
);
895 *(s
- url_delim_len
) = *VFS_PATH_URL_DELIMITER
;
897 if (vclass
!= NULL
&& (vclass
->flags
& VFSF_REMOTE
) != 0)
904 if (IS_PATH_SEP (*s
))
912 /* If the previous token is "..", we cannot collapse it */
913 if (s
[0] == '.' && s
[1] == '.' && s
+ 2 == p
)
921 if (s
== lpath
&& IS_PATH_SEP (*s
))
923 /* "/../foo" -> "/foo" */
924 str_move (s
+ 1, p
+ 4);
928 /* "token/../foo" -> "foo" */
930 if ((strncmp (s
, VFS_ENCODING_PREFIX
, enc_prefix_len
) == 0)
931 && (is_supported_encoding (s
+ enc_prefix_len
)))
932 /* special case: remove encoding */
935 #endif /* HAVE_CHARSET */
939 p
= s
> lpath
? s
- 1 : s
;
946 /* "token/.." -> "." */
947 if (!IS_PATH_SEP (lpath
[0]))
953 /* "foo/token/.." -> "foo" */
957 else if ((strncmp (s
, VFS_ENCODING_PREFIX
, enc_prefix_len
) == 0)
958 && (is_supported_encoding (s
+ enc_prefix_len
)))
960 /* special case: remove encoding */
965 /* search for the previous token */
966 /* IS_PATH_SEP (s[-1]) */
967 for (p
= s
- 1; p
>= lpath
&& !IS_PATH_SEP (*p
); p
--)
973 #endif /* HAVE_CHARSET */
976 if (s
>= lpath
+ url_delim_len
977 && strncmp (s
- url_delim_len
, VFS_PATH_URL_DELIMITER
, url_delim_len
) == 0)
989 /* --------------------------------------------------------------------------------------------- */
992 mc_realpath (const char *path
, char *resolved_path
)
995 const char *p
= path
;
996 gboolean absolute_path
= FALSE
;
998 if (IS_PATH_SEP (*p
))
1000 absolute_path
= TRUE
;
1004 /* ignore encoding: skip "#enc:" */
1005 if (g_str_has_prefix (p
, VFS_ENCODING_PREFIX
))
1007 p
+= strlen (VFS_ENCODING_PREFIX
);
1008 p
= strchr (p
, PATH_SEP
);
1011 if (!absolute_path
&& p
[1] != '\0')
1017 #endif /* HAVE_CHARSET */
1019 #ifdef HAVE_REALPATH
1020 return realpath (path
, resolved_path
);
1023 char copy_path
[PATH_MAX
];
1024 char got_path
[PATH_MAX
];
1025 char *new_path
= got_path
;
1028 char link_path
[PATH_MAX
];
1031 #endif /* S_IFLNK */
1033 /* Make a copy of the source path since we may need to modify it. */
1034 if (strlen (path
) >= PATH_MAX
- 2)
1036 errno
= ENAMETOOLONG
;
1040 strcpy (copy_path
, path
);
1042 max_path
= copy_path
+ PATH_MAX
- 2;
1043 /* If it's a relative pathname use getwd for starters. */
1044 if (!IS_PATH_SEP (*path
))
1046 new_path
= g_get_current_dir ();
1047 if (new_path
== NULL
)
1048 strcpy (got_path
, "");
1051 g_snprintf (got_path
, sizeof (got_path
), "%s", new_path
);
1053 new_path
= got_path
;
1056 new_path
+= strlen (got_path
);
1057 if (!IS_PATH_SEP (new_path
[-1]))
1058 *new_path
++ = PATH_SEP
;
1062 *new_path
++ = PATH_SEP
;
1065 /* Expand each slash-separated pathname component. */
1066 while (*path
!= '\0')
1068 /* Ignore stray "/". */
1069 if (IS_PATH_SEP (*path
))
1077 if (path
[1] == '\0' || IS_PATH_SEP (path
[1]))
1084 if (path
[2] == '\0' || IS_PATH_SEP (path
[2]))
1087 /* Ignore ".." at root. */
1088 if (new_path
== got_path
+ 1)
1090 /* Handle ".." by backing up. */
1091 while (!IS_PATH_SEP ((--new_path
)[-1]))
1097 /* Safely copy the next pathname component. */
1098 while (*path
!= '\0' && !IS_PATH_SEP (*path
))
1100 if (path
> max_path
)
1102 errno
= ENAMETOOLONG
;
1105 *new_path
++ = *path
++;
1108 /* Protect against infinite loops. */
1109 if (readlinks
++ > MAXSYMLINKS
)
1114 /* See if latest pathname component is a symlink. */
1116 n
= readlink (got_path
, link_path
, PATH_MAX
- 1);
1119 /* EINVAL means the file exists but isn't a symlink. */
1120 if (errno
!= EINVAL
)
1122 /* Make sure it's null terminated. */
1124 strcpy (resolved_path
, got_path
);
1130 /* Note: readlink doesn't add the null byte. */
1131 link_path
[n
] = '\0';
1132 if (IS_PATH_SEP (*link_path
))
1133 /* Start over for an absolute symlink. */
1134 new_path
= got_path
;
1136 /* Otherwise back up over this component. */
1137 while (!IS_PATH_SEP (*(--new_path
)))
1139 /* Safe sex check. */
1140 if (strlen (path
) + n
>= PATH_MAX
- 2)
1142 errno
= ENAMETOOLONG
;
1145 /* Insert symlink contents into path. */
1146 strcat (link_path
, path
);
1147 strcpy (copy_path
, link_path
);
1150 #endif /* S_IFLNK */
1151 *new_path
++ = PATH_SEP
;
1153 /* Delete trailing slash but don't whomp a lone slash. */
1154 if (new_path
!= got_path
+ 1 && IS_PATH_SEP (new_path
[-1]))
1156 /* Make sure it's null terminated. */
1158 strcpy (resolved_path
, got_path
);
1159 return resolved_path
;
1161 #endif /* HAVE_REALPATH */
1164 /* --------------------------------------------------------------------------------------------- */
1166 * Return the index of the permissions triplet
1171 get_user_permissions (struct stat
*st
)
1173 static gboolean initialized
= FALSE
;
1174 static gid_t
*groups
;
1183 ngroups
= getgroups (0, NULL
);
1185 ngroups
= 0; /* ignore errors */
1187 /* allocate space for one element in addition to what
1188 * will be filled by getgroups(). */
1189 groups
= g_new (gid_t
, ngroups
+ 1);
1193 ngroups
= getgroups (ngroups
, groups
);
1195 ngroups
= 0; /* ignore errors */
1198 /* getgroups() may or may not return the effective group ID,
1199 * so we always include it at the end of the list. */
1200 groups
[ngroups
++] = getegid ();
1205 if (st
->st_uid
== uid
|| uid
== 0)
1208 for (i
= 0; i
< ngroups
; i
++)
1209 if (st
->st_gid
== groups
[i
])
1215 /* --------------------------------------------------------------------------------------------- */
1217 * Build filename from arguments.
1218 * Like to g_build_filename(), but respect VFS_PATH_URL_DELIMITER
1222 mc_build_filenamev (const char *first_element
, va_list args
)
1225 const char *element
= first_element
;
1229 if (element
== NULL
)
1232 path
= g_string_new ("");
1234 absolute
= IS_PATH_SEP (*first_element
);
1238 if (*element
== '\0')
1239 element
= va_arg (args
, char *);
1246 tmp_element
= g_strdup (element
);
1248 element
= va_arg (args
, char *);
1250 canonicalize_pathname (tmp_element
);
1251 len
= strlen (tmp_element
);
1252 start
= IS_PATH_SEP (tmp_element
[0]) ? tmp_element
+ 1 : tmp_element
;
1254 g_string_append (path
, start
);
1255 if (!IS_PATH_SEP (tmp_element
[len
- 1]) && element
!= NULL
)
1256 g_string_append_c (path
, PATH_SEP
);
1258 g_free (tmp_element
);
1261 while (element
!= NULL
);
1264 g_string_prepend_c (path
, PATH_SEP
);
1266 ret
= g_string_free (path
, FALSE
);
1267 canonicalize_pathname (ret
);
1272 /* --------------------------------------------------------------------------------------------- */
1274 * Build filename from arguments.
1275 * Like to g_build_filename(), but respect VFS_PATH_URL_DELIMITER
1279 mc_build_filename (const char *first_element
, ...)
1284 if (first_element
== NULL
)
1287 va_start (args
, first_element
);
1288 ret
= mc_build_filenamev (first_element
, args
);
1293 /* --------------------------------------------------------------------------------------------- */