This source file includes following definitions.
- i_cache_match
- i_cache_add
- my_fork_state
- my_system__save_sigaction_handlers
- my_system__restore_sigaction_handlers
- my_system_make_arg_array
- mc_pread_stream
- get_owner
- get_group
- save_stop_handler
- my_exit
- my_signal
- my_sigaction
- my_fork
- my_execvp
- my_get_current_dir
- my_system
- my_systeml
- my_systemv
- my_systemv_flags
- mc_popen
- mc_pread
- mc_pstream_get_string
- mc_pclose
- tilde_expand
- canonicalize_pathname_custom
- mc_realpath
- get_user_permissions
- mc_build_filenamev
- mc_build_filename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 #include <config.h>
39
40 #include <ctype.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #ifdef HAVE_SYS_PARAM_H
49 # include <sys/param.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #ifdef HAVE_SYS_SELECT_H
54 # include <sys/select.h>
55 #endif
56 #include <sys/wait.h>
57 #include <pwd.h>
58 #include <grp.h>
59
60 #include "lib/global.h"
61
62 #include "lib/unixcompat.h"
63 #include "lib/vfs/vfs.h"
64 #include "lib/strutil.h"
65 #include "lib/util.h"
66 #include "lib/widget.h"
67 #include "lib/vfs/xdirentry.h"
68
69 #ifdef HAVE_CHARSET
70 # include "lib/charsets.h"
71 #endif
72
73
74
75 struct sigaction startup_handler;
76
77
78
79 #define UID_CACHE_SIZE 200
80 #define GID_CACHE_SIZE 30
81
82
83
84 typedef struct
85 {
86 int index;
87 char *string;
88 } int_cache;
89
90 typedef enum
91 {
92 FORK_ERROR = -1,
93 FORK_CHILD,
94 FORK_PARENT,
95 } my_fork_state_t;
96
97 typedef struct
98 {
99 struct sigaction intr;
100 struct sigaction quit;
101 struct sigaction stop;
102 } my_system_sigactions_t;
103
104
105
106
107
108 static int_cache uid_cache[UID_CACHE_SIZE];
109 static int_cache gid_cache[GID_CACHE_SIZE];
110
111
112
113
114
115 static char *
116 i_cache_match (int id, int_cache *cache, int size)
117 {
118 int i;
119
120 for (i = 0; i < size; i++)
121 if (cache[i].index == id)
122 return cache[i].string;
123 return 0;
124 }
125
126
127
128 static void
129 i_cache_add (int id, int_cache *cache, int size, char *text, int *last)
130 {
131 g_free (cache[*last].string);
132 cache[*last].string = g_strdup (text);
133 cache[*last].index = id;
134 *last = ((*last) + 1) % size;
135 }
136
137
138
139 static my_fork_state_t
140 my_fork_state (void)
141 {
142 pid_t pid;
143
144 pid = my_fork ();
145
146 if (pid < 0)
147 {
148 fprintf (stderr, "\n\nfork () = -1\n");
149 return FORK_ERROR;
150 }
151
152 if (pid == 0)
153 return FORK_CHILD;
154
155 while (TRUE)
156 {
157 int status = 0;
158
159 if (waitpid (pid, &status, 0) > 0)
160 return WEXITSTATUS (status) == 0 ? FORK_PARENT : FORK_ERROR;
161
162 if (errno != EINTR)
163 return FORK_ERROR;
164 }
165 }
166
167
168
169 static void
170 my_system__save_sigaction_handlers (my_system_sigactions_t *sigactions)
171 {
172 struct sigaction ignore;
173
174 memset (&ignore, 0, sizeof (ignore));
175 ignore.sa_handler = SIG_IGN;
176 sigemptyset (&ignore.sa_mask);
177
178 my_sigaction (SIGINT, &ignore, &sigactions->intr);
179 my_sigaction (SIGQUIT, &ignore, &sigactions->quit);
180
181
182
183 my_sigaction (SIGTSTP, &startup_handler, &sigactions->stop);
184 }
185
186
187
188 static void
189 my_system__restore_sigaction_handlers (my_system_sigactions_t *sigactions)
190 {
191 my_sigaction (SIGINT, &sigactions->intr, NULL);
192 my_sigaction (SIGQUIT, &sigactions->quit, NULL);
193 my_sigaction (SIGTSTP, &sigactions->stop, NULL);
194 }
195
196
197
198 static GPtrArray *
199 my_system_make_arg_array (int flags, const char *shell)
200 {
201 GPtrArray *args_array;
202
203 if ((flags & EXECUTE_AS_SHELL) != 0)
204 {
205 args_array = g_ptr_array_new ();
206 g_ptr_array_add (args_array, (gpointer) shell);
207 g_ptr_array_add (args_array, (gpointer) "-c");
208 }
209 else if (shell == NULL || *shell == '\0')
210 {
211 args_array = g_ptr_array_new ();
212 g_ptr_array_add (args_array, NULL);
213 }
214 else
215 args_array = str_tokenize (shell);
216
217 return args_array;
218 }
219
220
221
222 static void
223 mc_pread_stream (mc_pipe_stream_t *ps, const fd_set *fds)
224 {
225 size_t buf_len;
226 ssize_t read_len;
227
228 if (!FD_ISSET (ps->fd, fds))
229 {
230 ps->len = MC_PIPE_STREAM_UNREAD;
231 return;
232 }
233
234 buf_len = (size_t) ps->len;
235
236 if (buf_len >= MC_PIPE_BUFSIZE)
237 buf_len = ps->null_term ? MC_PIPE_BUFSIZE - 1 : MC_PIPE_BUFSIZE;
238
239 do
240 {
241 read_len = read (ps->fd, ps->buf, buf_len);
242 }
243 while (read_len < 0 && errno == EINTR);
244
245 if (read_len < 0)
246 {
247
248 ps->len = MC_PIPE_ERROR_READ;
249 ps->error = errno;
250 }
251 else if (read_len == 0)
252
253 ps->len = MC_PIPE_STREAM_EOF;
254 else
255 {
256
257 ps->len = read_len;
258
259 if (ps->null_term)
260 ps->buf[(size_t) ps->len] = '\0';
261 }
262
263 ps->pos = 0;
264 }
265
266
267
268
269
270 const char *
271 get_owner (uid_t uid)
272 {
273 struct passwd *pwd;
274 char *name;
275 static uid_t uid_last;
276
277 name = i_cache_match ((int) uid, uid_cache, UID_CACHE_SIZE);
278 if (name != NULL)
279 return name;
280
281 pwd = getpwuid (uid);
282 if (pwd != NULL)
283 {
284 i_cache_add ((int) uid, uid_cache, UID_CACHE_SIZE, pwd->pw_name, (int *) &uid_last);
285 return pwd->pw_name;
286 }
287 else
288 {
289 static char ibuf[10];
290
291 g_snprintf (ibuf, sizeof (ibuf), "%d", (int) uid);
292 return ibuf;
293 }
294 }
295
296
297
298 const char *
299 get_group (gid_t gid)
300 {
301 struct group *grp;
302 char *name;
303 static gid_t gid_last;
304
305 name = i_cache_match ((int) gid, gid_cache, GID_CACHE_SIZE);
306 if (name != NULL)
307 return name;
308
309 grp = getgrgid (gid);
310 if (grp != NULL)
311 {
312 i_cache_add ((int) gid, gid_cache, GID_CACHE_SIZE, grp->gr_name, (int *) &gid_last);
313 return grp->gr_name;
314 }
315 else
316 {
317 static char gbuf[10];
318
319 g_snprintf (gbuf, sizeof (gbuf), "%d", (int) gid);
320 return gbuf;
321 }
322 }
323
324
325
326
327
328
329 void
330 save_stop_handler (void)
331 {
332 my_sigaction (SIGTSTP, NULL, &startup_handler);
333 }
334
335
336
337
338
339
340
341
342
343
344 void
345
346 my_exit (int status)
347 {
348 _exit (status);
349 }
350
351
352
353
354
355
356 sighandler_t
357 my_signal (int signum, sighandler_t handler)
358 {
359 return signal (signum, handler);
360 }
361
362
363
364
365
366
367 int
368 my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
369 {
370 return sigaction (signum, act, oldact);
371 }
372
373
374
375
376
377
378 pid_t
379 my_fork (void)
380 {
381 return fork ();
382 }
383
384
385
386
387
388
389 int
390 my_execvp (const char *file, char *const argv[])
391 {
392 return execvp (file, argv);
393 }
394
395
396
397
398
399
400 char *
401 my_get_current_dir (void)
402 {
403 return g_get_current_dir ();
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 int
420 my_system (int flags, const char *shell, const char *command)
421 {
422 return my_systeml (flags, shell, command, NULL);
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 int
440 my_systeml (int flags, const char *shell, ...)
441 {
442 GPtrArray *args_array;
443 int status = 0;
444 va_list vargs;
445 char *one_arg;
446
447 args_array = g_ptr_array_new ();
448
449 va_start (vargs, shell);
450 while ((one_arg = va_arg (vargs, char *)) != NULL)
451 g_ptr_array_add (args_array, one_arg);
452 va_end (vargs);
453
454 g_ptr_array_add (args_array, NULL);
455 status = my_systemv_flags (flags, shell, (char *const *) args_array->pdata);
456
457 g_ptr_array_free (args_array, TRUE);
458
459 return status;
460 }
461
462
463
464
465
466
467
468
469
470
471
472 int
473 my_systemv (const char *command, char *const argv[])
474 {
475 my_fork_state_t fork_state;
476 int status = 0;
477 my_system_sigactions_t sigactions;
478
479 my_system__save_sigaction_handlers (&sigactions);
480
481 fork_state = my_fork_state ();
482 switch (fork_state)
483 {
484 case FORK_ERROR:
485 status = -1;
486 break;
487 case FORK_CHILD:
488 {
489 my_signal (SIGINT, SIG_DFL);
490 my_signal (SIGQUIT, SIG_DFL);
491 my_signal (SIGTSTP, SIG_DFL);
492 my_signal (SIGCHLD, SIG_DFL);
493
494 my_execvp (command, argv);
495 my_exit (127);
496 }
497 MC_FALLTHROUGH;
498
499 default:
500 status = 0;
501 break;
502 }
503 my_system__restore_sigaction_handlers (&sigactions);
504
505 return status;
506 }
507
508
509
510
511
512
513
514
515
516
517
518
519
520 int
521 my_systemv_flags (int flags, const char *command, char *const argv[])
522 {
523 const char *execute_name;
524 GPtrArray *args_array;
525 int status = 0;
526
527 args_array = my_system_make_arg_array (flags, command);
528
529 execute_name = g_ptr_array_index (args_array, 0);
530
531 for (; argv != NULL && *argv != NULL; argv++)
532 g_ptr_array_add (args_array, *argv);
533
534 g_ptr_array_add (args_array, NULL);
535 status = my_systemv (execute_name, (char *const *) args_array->pdata);
536
537 g_ptr_array_free (args_array, TRUE);
538
539 return status;
540 }
541
542
543
544
545
546
547
548
549
550
551
552
553
554 mc_pipe_t *
555 mc_popen (const char *command, gboolean read_out, gboolean read_err, GError **error)
556 {
557 mc_pipe_t *p;
558 const char *const argv[] = { "/bin/sh", "sh", "-c", command, NULL };
559
560 p = g_try_new (mc_pipe_t, 1);
561 if (p == NULL)
562 {
563 mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE, "%s",
564 _ ("Cannot create pipe descriptor"));
565 goto ret_err;
566 }
567
568 p->out.fd = -1;
569 p->err.fd = -1;
570
571 if (!g_spawn_async_with_pipes (NULL, (gchar **) argv, NULL,
572 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_FILE_AND_ARGV_ZERO, NULL,
573 NULL, &p->child_pid, NULL, read_out ? &p->out.fd : NULL,
574 read_err ? &p->err.fd : NULL, error))
575 {
576 mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE_STREAM, "%s",
577 _ ("Cannot create pipe streams"));
578 goto ret_err;
579 }
580
581 p->out.buf[0] = '\0';
582 p->out.len = MC_PIPE_BUFSIZE;
583 p->out.null_term = FALSE;
584
585 p->err.buf[0] = '\0';
586 p->err.len = MC_PIPE_BUFSIZE;
587 p->err.null_term = FALSE;
588
589 return p;
590
591 ret_err:
592 g_free (p);
593 return NULL;
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616 void
617 mc_pread (mc_pipe_t *p, GError **error)
618 {
619 gboolean read_out, read_err;
620 fd_set fds;
621 int maxfd = 0;
622 int res;
623
624 if (error != NULL)
625 *error = NULL;
626
627 read_out = p->out.fd >= 0;
628 read_err = p->err.fd >= 0;
629
630 if (!read_out && !read_err)
631 {
632 p->out.len = MC_PIPE_STREAM_UNREAD;
633 p->err.len = MC_PIPE_STREAM_UNREAD;
634 return;
635 }
636
637 FD_ZERO (&fds);
638 if (read_out)
639 {
640 FD_SET (p->out.fd, &fds);
641 maxfd = p->out.fd;
642 }
643
644 if (read_err)
645 {
646 FD_SET (p->err.fd, &fds);
647 maxfd = MAX (maxfd, p->err.fd);
648 }
649
650
651 res = select (maxfd + 1, &fds, NULL, NULL, NULL);
652 if (res < 0 && errno != EINTR)
653 {
654 mc_propagate_error (
655 error, MC_PIPE_ERROR_READ,
656 _ ("Unexpected error in select() reading data from a child process:\n%s"),
657 unix_error_string (errno));
658 return;
659 }
660
661 if (read_out)
662 mc_pread_stream (&p->out, &fds);
663 else
664 p->out.len = MC_PIPE_STREAM_UNREAD;
665
666 if (read_err)
667 mc_pread_stream (&p->err, &fds);
668 else
669 p->err.len = MC_PIPE_STREAM_UNREAD;
670 }
671
672
673
674
675
676
677
678
679
680
681
682 GString *
683 mc_pstream_get_string (mc_pipe_stream_t *ps)
684 {
685 char *s;
686 size_t size, i;
687 gboolean escape = FALSE;
688
689 g_return_val_if_fail (ps != NULL, NULL);
690
691 if (ps->len < 0)
692 return NULL;
693
694 size = ps->len - ps->pos;
695
696 if (size == 0)
697 return NULL;
698
699 s = ps->buf + ps->pos;
700
701 if (s[0] == '\0')
702 return NULL;
703
704
705 for (i = 0; i < size && !(s[i] == '\0' || (s[i] == '\n' && !escape)); i++)
706 escape = s[i] == '\\' ? !escape : FALSE;
707
708 if (i != size && s[i] == '\n')
709 i++;
710
711 ps->pos += i;
712
713 return g_string_new_len (s, i);
714 }
715
716
717
718
719
720
721
722
723
724 void
725 mc_pclose (mc_pipe_t *p, GError **error)
726 {
727 int res;
728
729 if (p == NULL)
730 {
731 mc_replace_error (error, MC_PIPE_ERROR_READ, "%s",
732 _ ("Cannot close pipe descriptor (p == NULL)"));
733 return;
734 }
735
736 if (p->out.fd >= 0)
737 res = close (p->out.fd);
738 if (p->err.fd >= 0)
739 res = close (p->err.fd);
740
741 do
742 {
743 int status;
744
745 res = waitpid (p->child_pid, &status, 0);
746 }
747 while (res < 0 && errno == EINTR);
748
749 if (res < 0)
750 mc_replace_error (error, MC_PIPE_ERROR_READ, _ ("Unexpected error in waitpid():\n%s"),
751 unix_error_string (errno));
752
753 g_free (p);
754 }
755
756
757
758
759
760
761
762
763
764
765
766 char *
767 tilde_expand (const char *directory)
768 {
769 struct passwd *passwd;
770 const char *p, *q;
771
772 if (*directory != '~')
773 return g_strdup (directory);
774
775 p = directory + 1;
776
777
778 if (*p == '\0' || IS_PATH_SEP (*p))
779 {
780 passwd = getpwuid (geteuid ());
781 q = IS_PATH_SEP (*p) ? p + 1 : "";
782 }
783 else
784 {
785 q = strchr (p, PATH_SEP);
786 if (q == NULL)
787 passwd = getpwnam (p);
788 else
789 {
790 char *name;
791
792 name = g_strndup (p, q - p);
793 passwd = getpwnam (name);
794 q++;
795 g_free (name);
796 }
797 }
798
799
800 if (passwd == NULL)
801 return g_strdup (directory);
802
803 return g_strconcat (passwd->pw_dir, PATH_SEP_STR, q, (char *) NULL);
804 }
805
806
807
808
809
810
811
812
813
814
815
816
817 void
818 canonicalize_pathname_custom (char *path, canon_path_flags_t flags)
819 {
820 char *p, *s;
821 char *lpath = path;
822 const size_t url_delim_len = strlen (VFS_PATH_URL_DELIMITER);
823
824
825 if ((flags & CANON_PATH_GUARDUNC) != 0 && IS_PATH_SEP (path[0]) && IS_PATH_SEP (path[1]))
826 {
827 for (p = path + 2; p[0] != '\0' && !IS_PATH_SEP (p[0]); p++)
828 ;
829 if (IS_PATH_SEP (p[0]) && p > path + 2)
830 lpath = p;
831 }
832
833 if (lpath[0] == '\0' || lpath[1] == '\0')
834 return;
835
836 if ((flags & CANON_PATH_JOINSLASHES) != 0)
837 {
838
839 for (p = lpath; *p != '\0'; p++)
840 if (IS_PATH_SEP (p[0]) && IS_PATH_SEP (p[1]) && (p == lpath || *(p - 1) != ':'))
841 {
842 s = p + 1;
843 while (IS_PATH_SEP (*(++s)))
844 ;
845 str_move (p + 1, s);
846 }
847
848
849 for (p = lpath; *p != '\0';)
850 if (IS_PATH_SEP (p[0]) && p[1] == '.' && IS_PATH_SEP (p[2]))
851 str_move (p, p + 2);
852 else
853 p++;
854 }
855
856 if ((flags & CANON_PATH_REMSLASHDOTS) != 0)
857 {
858 size_t len;
859
860
861 for (p = lpath + strlen (lpath) - 1; p > lpath && IS_PATH_SEP (*p); p--)
862 {
863 if (p >= lpath + url_delim_len - 1
864 && strncmp (p - url_delim_len + 1, VFS_PATH_URL_DELIMITER, url_delim_len) == 0)
865 break;
866 *p = '\0';
867 }
868
869
870 if (lpath[0] == '.' && IS_PATH_SEP (lpath[1]))
871 {
872 if (lpath[2] == '\0')
873 {
874 lpath[1] = '\0';
875 return;
876 }
877
878 str_move (lpath, lpath + 2);
879 }
880
881
882 len = strlen (lpath);
883 if (len < 2)
884 return;
885
886 if (IS_PATH_SEP (lpath[len - 1])
887 && (len < url_delim_len
888 || strncmp (lpath + len - url_delim_len, VFS_PATH_URL_DELIMITER, url_delim_len)
889 != 0))
890 lpath[len - 1] = '\0';
891 else if (lpath[len - 1] == '.' && IS_PATH_SEP (lpath[len - 2]))
892 {
893 if (len == 2)
894 {
895 lpath[1] = '\0';
896 return;
897 }
898
899 lpath[len - 2] = '\0';
900 }
901 }
902
903
904 if ((flags & CANON_PATH_REMDOUBLEDOTS) != 0)
905 {
906 #ifdef HAVE_CHARSET
907 const size_t enc_prefix_len = strlen (VFS_ENCODING_PREFIX);
908 #endif
909
910 for (p = lpath; p[0] != '\0' && p[1] != '\0' && p[2] != '\0';)
911 {
912 if (!IS_PATH_SEP (p[0]) || p[1] != '.' || p[2] != '.'
913 || (!IS_PATH_SEP (p[3]) && p[3] != '\0'))
914 {
915 p++;
916 continue;
917 }
918
919
920 s = p - 1;
921 if (s >= lpath + url_delim_len - 2
922 && strncmp (s - url_delim_len + 2, VFS_PATH_URL_DELIMITER, url_delim_len) == 0)
923 {
924 s -= (url_delim_len - 2);
925 while (s >= lpath && !IS_PATH_SEP (*s--))
926 ;
927 }
928
929 while (s >= lpath)
930 {
931 if (s - url_delim_len > lpath
932 && strncmp (s - url_delim_len, VFS_PATH_URL_DELIMITER, url_delim_len) == 0)
933 {
934 char *vfs_prefix = s - url_delim_len;
935 vfs_class *vclass;
936
937 while (vfs_prefix > lpath && !IS_PATH_SEP (*--vfs_prefix))
938 ;
939 if (IS_PATH_SEP (*vfs_prefix))
940 vfs_prefix++;
941 *(s - url_delim_len) = '\0';
942
943 vclass = vfs_prefix_to_class (vfs_prefix);
944 *(s - url_delim_len) = *VFS_PATH_URL_DELIMITER;
945
946 if (vclass != NULL && (vclass->flags & VFSF_REMOTE) != 0)
947 {
948 s = vfs_prefix;
949 continue;
950 }
951 }
952
953 if (IS_PATH_SEP (*s))
954 break;
955
956 s--;
957 }
958
959 s++;
960
961
962 if (s[0] == '.' && s[1] == '.' && s + 2 == p)
963 {
964 p += 3;
965 continue;
966 }
967
968 if (p[3] != '\0')
969 {
970 if (s == lpath && IS_PATH_SEP (*s))
971 {
972
973 str_move (s + 1, p + 4);
974 }
975 else
976 {
977
978 #ifdef HAVE_CHARSET
979 if (strncmp (s, VFS_ENCODING_PREFIX, enc_prefix_len) == 0)
980 {
981 char *enc;
982
983 enc = vfs_get_encoding (s, -1);
984
985 if (is_supported_encoding (enc))
986
987 str_move (s, p + 1);
988 else
989 str_move (s, p + 4);
990
991 g_free (enc);
992 }
993 else
994 #endif
995 str_move (s, p + 4);
996 }
997
998 p = s > lpath ? s - 1 : s;
999 continue;
1000 }
1001
1002
1003 if (s == lpath)
1004 {
1005
1006 if (!IS_PATH_SEP (lpath[0]))
1007 lpath[0] = '.';
1008 lpath[1] = '\0';
1009 }
1010 else
1011 {
1012
1013 if (s == lpath + 1)
1014 s[0] = '\0';
1015 #ifdef HAVE_CHARSET
1016 else if (strncmp (s, VFS_ENCODING_PREFIX, enc_prefix_len) == 0)
1017 {
1018 char *enc;
1019 gboolean ok;
1020
1021 enc = vfs_get_encoding (s, -1);
1022 ok = is_supported_encoding (enc);
1023 g_free (enc);
1024
1025 if (!ok)
1026 goto last;
1027
1028
1029 s[0] = '.';
1030 s[1] = '.';
1031 s[2] = '\0';
1032
1033
1034
1035 for (p = s - 1; p >= lpath && !IS_PATH_SEP (*p); p--)
1036 ;
1037
1038 if (p >= lpath)
1039 continue;
1040 }
1041 #endif
1042 else
1043 {
1044 #ifdef HAVE_CHARSET
1045 last:
1046 #endif
1047 if (s >= lpath + url_delim_len
1048 && strncmp (s - url_delim_len, VFS_PATH_URL_DELIMITER, url_delim_len) == 0)
1049 *s = '\0';
1050 else
1051 s[-1] = '\0';
1052 }
1053 }
1054
1055 break;
1056 }
1057 }
1058 }
1059
1060
1061
1062 char *
1063 mc_realpath (const char *path, char *resolved_path)
1064 {
1065 #ifdef HAVE_CHARSET
1066 const char *p = path;
1067 gboolean absolute_path = FALSE;
1068
1069 if (IS_PATH_SEP (*p))
1070 {
1071 absolute_path = TRUE;
1072 p++;
1073 }
1074
1075
1076 if (g_str_has_prefix (p, VFS_ENCODING_PREFIX))
1077 {
1078 p += strlen (VFS_ENCODING_PREFIX);
1079 p = strchr (p, PATH_SEP);
1080 if (p != NULL)
1081 {
1082 if (!absolute_path && p[1] != '\0')
1083 p++;
1084
1085 path = p;
1086 }
1087 }
1088 #endif
1089
1090 #ifdef HAVE_REALPATH
1091 return realpath (path, resolved_path);
1092 #else
1093 {
1094 char copy_path[PATH_MAX];
1095 char got_path[PATH_MAX];
1096 char *new_path = got_path;
1097 char *max_path;
1098 # ifdef S_IFLNK
1099 char link_path[PATH_MAX];
1100 int readlinks = 0;
1101 int n;
1102 # endif
1103
1104
1105 if (strlen (path) >= PATH_MAX - 2)
1106 {
1107 errno = ENAMETOOLONG;
1108 return NULL;
1109 }
1110
1111 strcpy (copy_path, path);
1112 path = copy_path;
1113 max_path = copy_path + PATH_MAX - 2;
1114
1115 if (!IS_PATH_SEP (*path))
1116 {
1117 new_path = my_get_current_dir ();
1118 if (new_path == NULL)
1119 strcpy (got_path, "");
1120 else
1121 {
1122 g_snprintf (got_path, sizeof (got_path), "%s", new_path);
1123 g_free (new_path);
1124 new_path = got_path;
1125 }
1126
1127 new_path += strlen (got_path);
1128 if (!IS_PATH_SEP (new_path[-1]))
1129 *new_path++ = PATH_SEP;
1130 }
1131 else
1132 {
1133 *new_path++ = PATH_SEP;
1134 path++;
1135 }
1136
1137 while (*path != '\0')
1138 {
1139
1140 if (IS_PATH_SEP (*path))
1141 {
1142 path++;
1143 continue;
1144 }
1145 if (*path == '.')
1146 {
1147
1148 if (path[1] == '\0' || IS_PATH_SEP (path[1]))
1149 {
1150 path++;
1151 continue;
1152 }
1153 if (path[1] == '.')
1154 {
1155 if (path[2] == '\0' || IS_PATH_SEP (path[2]))
1156 {
1157 path += 2;
1158
1159 if (new_path == got_path + 1)
1160 continue;
1161
1162 while (!IS_PATH_SEP ((--new_path)[-1]))
1163 ;
1164 continue;
1165 }
1166 }
1167 }
1168
1169 while (*path != '\0' && !IS_PATH_SEP (*path))
1170 {
1171 if (path > max_path)
1172 {
1173 errno = ENAMETOOLONG;
1174 return NULL;
1175 }
1176 *new_path++ = *path++;
1177 }
1178 # ifdef S_IFLNK
1179
1180 if (readlinks++ > MAXSYMLINKS)
1181 {
1182 errno = ELOOP;
1183 return NULL;
1184 }
1185
1186 *new_path = '\0';
1187 n = readlink (got_path, link_path, PATH_MAX - 1);
1188 if (n < 0)
1189 {
1190
1191 if (errno != EINVAL)
1192 {
1193
1194 *new_path = '\0';
1195 strcpy (resolved_path, got_path);
1196 return NULL;
1197 }
1198 }
1199 else
1200 {
1201
1202 link_path[n] = '\0';
1203 if (IS_PATH_SEP (*link_path))
1204
1205 new_path = got_path;
1206 else
1207
1208 while (!IS_PATH_SEP (*(--new_path)))
1209 ;
1210
1211 if (strlen (path) + n >= PATH_MAX - 2)
1212 {
1213 errno = ENAMETOOLONG;
1214 return NULL;
1215 }
1216
1217 strcat (link_path, path);
1218 strcpy (copy_path, link_path);
1219 path = copy_path;
1220 }
1221 # endif
1222 *new_path++ = PATH_SEP;
1223 }
1224
1225 if (new_path != got_path + 1 && IS_PATH_SEP (new_path[-1]))
1226 new_path--;
1227
1228 *new_path = '\0';
1229 strcpy (resolved_path, got_path);
1230 return resolved_path;
1231 }
1232 #endif
1233 }
1234
1235
1236
1237
1238
1239
1240
1241 int
1242 get_user_permissions (struct stat *st)
1243 {
1244 static gboolean initialized = FALSE;
1245 static gid_t *groups;
1246 static int ngroups;
1247 static uid_t uid;
1248 int i;
1249
1250 if (!initialized)
1251 {
1252 uid = geteuid ();
1253
1254 ngroups = getgroups (0, NULL);
1255 if (ngroups == -1)
1256 ngroups = 0;
1257
1258
1259
1260 groups = g_new (gid_t, ngroups + 1);
1261
1262 if (ngroups != 0)
1263 {
1264 ngroups = getgroups (ngroups, groups);
1265 if (ngroups == -1)
1266 ngroups = 0;
1267 }
1268
1269
1270
1271 groups[ngroups++] = getegid ();
1272
1273 initialized = TRUE;
1274 }
1275
1276 if (st->st_uid == uid || uid == 0)
1277 return 0;
1278
1279 for (i = 0; i < ngroups; i++)
1280 if (st->st_gid == groups[i])
1281 return 1;
1282
1283 return 2;
1284 }
1285
1286
1287
1288
1289
1290
1291
1292 char *
1293 mc_build_filenamev (const char *first_element, va_list args)
1294 {
1295 gboolean absolute;
1296 const char *element = first_element;
1297 GString *path;
1298 char *ret;
1299
1300 if (first_element == NULL)
1301 return NULL;
1302
1303 absolute = IS_PATH_SEP (*first_element);
1304
1305 path = g_string_new (absolute ? PATH_SEP_STR : "");
1306
1307 do
1308 {
1309 if (*element == '\0')
1310 element = va_arg (args, char *);
1311 else
1312 {
1313 char *tmp_element;
1314 const char *start;
1315
1316 tmp_element = g_strdup (element);
1317
1318 element = va_arg (args, char *);
1319
1320 canonicalize_pathname (tmp_element);
1321 start = IS_PATH_SEP (tmp_element[0]) ? tmp_element + 1 : tmp_element;
1322
1323 g_string_append (path, start);
1324 if (!IS_PATH_SEP (path->str[path->len - 1]) && element != NULL)
1325 g_string_append_c (path, PATH_SEP);
1326
1327 g_free (tmp_element);
1328 }
1329 }
1330 while (element != NULL);
1331
1332 ret = g_string_free (path, FALSE);
1333 canonicalize_pathname (ret);
1334
1335 return ret;
1336 }
1337
1338
1339
1340
1341
1342
1343
1344 char *
1345 mc_build_filename (const char *first_element, ...)
1346 {
1347 va_list args;
1348 char *ret;
1349
1350 if (first_element == NULL)
1351 return NULL;
1352
1353 va_start (args, first_element);
1354 ret = mc_build_filenamev (first_element, args);
1355 va_end (args);
1356 return ret;
1357 }
1358
1359