2 Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
3 Written 1994, 1995, 1996 by:
4 Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
5 Jakub Jelinek, Mauricio Plaza.
7 The file_date routine is mostly from GNU's fileutils package,
8 written by Richard Stallman and David MacKenzie.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #if defined(__os2__) /* OS/2 need io.h! .ado */
30 #include <sys/types.h>
35 #include <signal.h> /* my_system */
36 #include <limits.h> /* INT_MAX */
39 # include <sys/time.h___>
41 # include <sys/time.h> /* alex: sys/select.h defines struct timeval */
43 #endif /* SCO_FLAVOR */
44 #include <sys/param.h>
45 #include <sys/types.h>
48 #include <errno.h> /* my_system */
50 # include <sys/timeb.h> /* alex: for struct timeb, used in time.h */
51 #endif /* SCO_FLAVOR */
59 #ifdef HAVE_SYS_SELECT_H
60 # include <sys/select.h>
64 # if defined(__GLIBC__) && (__GLIBC__ < 2)
65 # include <linux/termios.h> /* This is needed for TIOCLINUX */
69 # include <sys/ioctl.h>
73 #include "mountlist.h"
75 /* From dialog.h (not wanting to include it as
76 it requires including a lot of other files, too) */
77 int message (int error
, char *header
, char *text
, ...);
80 #if defined(HAVE_RX_H) && defined(HAVE_REGCOMP)
88 #include "user.h" /* expand_format */
89 #include "../vfs/vfs.h"
93 char app_text
[] = "Midnight-Commander";
95 int easy_patterns
= 1;
96 int align_extensions
= 1;
99 struct mount_entry
*mount_list
= NULL
;
102 char *strdup (const char *s
)
104 char *t
= malloc (strlen (s
)+1);
110 int is_printable (int c
)
112 static const unsigned char xterm_printable
[] = {
113 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
114 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
115 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
116 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
117 1,1,1,1,0,0,1,1,0,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,
118 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
119 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
120 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
123 extern int xterm_flag
;
124 extern int eight_bit_clean
;
125 extern int full_eight_bits
;
128 if (eight_bit_clean
){
129 if (full_eight_bits
){
131 return xterm_printable
[c
];
133 return (c
> 31 && c
!= 127);
135 return ((c
>31 && c
< 127) || c
>= 160);
137 return (c
> 31 && c
< 127);
140 /* Returns the message dimensions (lines and columns) */
141 int msglen (char *text
, int *lines
)
146 for (*lines
= 1;*text
; text
++){
159 char *trim (char *s
, char *d
, int len
)
161 int source_len
= strlen (s
);
163 if (source_len
> len
){
164 strcpy (d
, s
+(source_len
-len
));
174 name_quote (const char *s
, int quote_percent
)
178 d
= ret
= xmalloc (strlen (s
)*2 + 2 + 1, "quote_name");
184 for (; *s
; s
++, d
++) {
218 fake_name_quote (const char *s
, int quote_percent
)
223 /* If passed an empty txt (this usually means that there is an error)
224 * in the upper layers, we return "/"
226 char *name_trunc (char *txt
, int trunc_len
)
228 static char x
[MC_MAXPATHLEN
+MC_MAXPATHLEN
];
235 if (trunc_len
> sizeof (x
)-1){
236 fprintf (stderr
, _("name_trunc: too big"));
237 trunc_len
= sizeof (x
)-1;
239 txt_len
= strlen (txt
);
240 if (txt_len
<= trunc_len
)
242 else if (tilde_trunc
){
243 int y
= trunc_len
% 2;
244 strncpy (x
, txt
, (trunc_len
/2)+y
);
245 strncpy (x
+(trunc_len
/2)+y
, txt
+txt_len
-(trunc_len
/2), trunc_len
/2);
246 x
[(trunc_len
/2)+y
] = '~';
248 strncpy (x
, txt
, trunc_len
-1);
249 x
[trunc_len
-1] = '>';
253 if (!is_printable (*p
))
258 char *size_trunc (long int size
)
261 long int divisor
= 1;
264 if (size
> 999999999L){
267 if (size
/divisor
> 999999999L){
272 sprintf (x
, "%ld%s", (size
/divisor
), xtra
);
276 char *size_trunc_sep (long int size
)
282 p
= y
= size_trunc (size
);
284 d
= x
+ sizeof (x
) - 1;
286 while (p
>= y
&& isalpha (*p
))
288 for (count
= 0; p
>= y
; count
++){
301 int is_exe (mode_t mode
)
303 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
308 #define ismode(n,m) ((n & m) == m)
310 char *string_perm (mode_t mode_bits
)
312 static char mode
[11];
314 strcpy (mode
, "----------");
315 if (ismode (mode_bits
, S_IFDIR
)) mode
[0] = 'd';
317 if (ismode (mode_bits
, S_IFSOCK
)) mode
[0] = 's';
319 if (ismode (mode_bits
, S_IXOTH
)) mode
[9] = 'x';
320 if (ismode (mode_bits
, S_IWOTH
)) mode
[8] = 'w';
321 if (ismode (mode_bits
, S_IROTH
)) mode
[7] = 'r';
322 if (ismode (mode_bits
, S_IXGRP
)) mode
[6] = 'x';
323 if (ismode (mode_bits
, S_IWGRP
)) mode
[5] = 'w';
324 if (ismode (mode_bits
, S_IRGRP
)) mode
[4] = 'r';
325 if (ismode (mode_bits
, S_IXUSR
)) mode
[3] = 'x';
326 if (ismode (mode_bits
, S_IWUSR
)) mode
[2] = 'w';
327 if (ismode (mode_bits
, S_IRUSR
)) mode
[1] = 'r';
329 if (ismode (mode_bits
, S_ISUID
)) mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
330 if (ismode (mode_bits
, S_ISGID
)) mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
331 if (ismode (mode_bits
, S_IFCHR
)) mode
[0] = 'c';
332 if (ismode (mode_bits
, S_IFBLK
)) mode
[0] = 'b';
333 if (ismode (mode_bits
, S_ISVTX
)) mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
334 if (ismode (mode_bits
, S_IFLNK
)) mode
[0] = 'l';
335 if (ismode (mode_bits
, S_IFIFO
)) mode
[0] = 's';
341 strip_password (char *path
)
343 char *at
, *inner_colon
, *dir
;
345 if ((dir
= strchr (path
, PATH_SEP
)) != NULL
)
347 /* search for any possible user */
348 at
= strchr (path
, '@');
350 /* We have a username */
353 inner_colon
= strchr (path
, ':');
356 strcpy (inner_colon
, at
);
363 char *strip_home_and_password(char *dir
)
365 static char newdir
[MC_MAXPATHLEN
], *p
, *q
;
367 if (home_dir
&& !strncmp (dir
, home_dir
, strlen (home_dir
))){
369 strcpy (&newdir
[1], &dir
[strlen (home_dir
)]);
373 else if (!strncmp (dir
, "ftp://", 6)) {
374 strip_password (strcpy (newdir
, dir
) + 6);
375 if ((p
= strchr (newdir
+ 6, PATH_SEP
)) != NULL
) {
377 q
= ftpfs_gethome (newdir
);
379 if (q
!= 0 && strcmp (q
, PATH_SEP_STR
) && !strncmp (p
, q
, strlen (q
) - 1)) {
381 strcat (newdir
, p
+ strlen (q
) - 1);
385 } else if (!strncmp (dir
, "mc:", 3)) {
387 strcpy (newdir
, dir
);
388 if (newdir
[3] == '/' && newdir
[4] == '/') {
390 strip_password ( newdir
+ 5);
393 strip_password (newdir
+ 3);
395 if ((p
= strchr (pth
, PATH_SEP
)) != NULL
) {
397 q
= mcfs_gethome (newdir
);
400 if (strcmp (q
, PATH_SEP_STR
) && !strncmp (p
, q
, strlen (q
) - 1)) {
402 strcat (newdir
, p
+ strlen (q
) - 1);
413 static char *maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
425 static char *maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
437 /* If shell patterns are on converts a shell pattern to a regular
438 expression. Called by regexp_match and mask_rename. */
439 /* Shouldn't we support [a-fw] type wildcards as well ?? */
440 char *convert_pattern (char *pattern
, int match_type
, int do_group
)
444 int was_wildcard
= 0;
447 new_pattern
= malloc (MC_MAXPATHLEN
);
449 if (match_type
== match_file
)
451 for (s
= pattern
; *s
; s
++, d
++){
454 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
460 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
465 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
471 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
476 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
477 if (match_type
== match_file
)
482 return strdup (pattern
);
485 int regexp_match (char *pattern
, char *string
, int match_type
)
488 static char *old_pattern
= NULL
;
492 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
497 pattern
= convert_pattern (pattern
, match_type
, 0);
498 if (regcomp (&r
, pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
502 old_pattern
= pattern
;
503 old_type
= match_type
;
505 rval
= !regexec (&r
, string
, 0, NULL
, 0);
509 char *extension (char *filename
)
513 if (!strlen (filename
))
516 d
= filename
+ strlen (filename
) - 1;
517 for (;d
>= filename
; d
--){
524 /* This routine uses the fact that x is at most 14 chars or so */
525 char *split_extension (char *x
, int pad
)
530 if (!align_extensions)
533 if (strlen (x) >= pad)
536 if ((ext = extension (x)) == x || *ext == 0)
540 for (i = strlen (x); i < pad; i++)
545 for (i = 0; i < l; i++)
546 xbuf [pad-i] = *(ext+l-i-1);
547 for (i = xbuf + (ext - x); i <
552 void *do_xmalloc (int size
)
554 void *m
= malloc (size
);
557 fprintf (stderr
, "memory exhausted\n");
562 #endif /* HAVE_MAD */
564 int get_int (char *file
, char *key
, int def
)
566 return GetPrivateProfileInt (app_text
, key
, def
, file
);
569 int set_int (char *file
, char *key
, int value
)
573 sprintf (buffer
, "%d", value
);
574 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
577 int exist_file (char *name
)
579 return access (name
, R_OK
) == 0;
582 char *load_file (char *filename
)
589 if (stat (filename
, &s
) != 0){
593 if ((data_file
= fopen (filename
, "rt")) == NULL
){
595 if ((data_file
= fopen (filename
, "r")) == NULL
){
599 data
= (char *) xmalloc (s
.st_size
+1, "util, load_file");
601 memset(data
,0,s
.st_size
+1);
603 read_size
= fread (data
, 1, s
.st_size
, data_file
);
604 data
[read_size
] = 0;
616 char *file_date (time_t when
)
618 static char timebuf
[40];
619 time_t current_time
= time ((time_t) 0);
625 strcpy (timebuf
, p
? p
: "-----");
627 strcpy (timebuf
, ctime (&when
));
629 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
630 || current_time
< when
- 60L * 60L) /* In the future. */
632 /* The file is fairly old or in the future.
633 POSIX says the cutoff is 6 months old;
634 approximate this by 6*30 days.
635 Allow a 1 hour slop factor for what is considered "the future",
636 to allow for NFS server/client clock disagreement.
637 Show the year instead of the time of day. */
638 strcpy (timebuf
+ 11, timebuf
+ 19);
644 /* Like file_date, but packs the data to fit in 10 columns */
645 char *file_date_pck (time_t when
)
647 /* FIXME: Should return only 10 chars, not 14 */
648 return file_date (when
);
651 char *extract_line (char *s
, char *top
)
653 static char tmp_line
[500];
656 while (*s
&& *s
!= '\n' && (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
662 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
663 char * _icase_search (char *text
, char *data
, int *lng
)
672 while (*(e
+1) == '\b' && *(e
+2)) {
676 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
685 *lng
= strlen (text
) + dlng
;
692 /* The basename routine */
693 char *x_basename (char *s
)
696 return ((where
= strrchr (s
, PATH_SEP
)))? where
+ 1 : s
;
699 char *get_full_name (char *dir
, char *file
)
702 char *d
= malloc (strlen (dir
) + strlen (file
) + 2);
706 if (dir
[i
- 1] != PATH_SEP
|| dir
[i
] != 0)
707 strcat (d
, PATH_SEP_STR
);
708 file
= x_basename (file
);
713 void my_putenv (char *name
, char *data
)
717 full
= xmalloc (strlen (name
) + strlen (data
) + 2, "util, my_putenv");
718 sprintf (full
, "%s=%s", name
, data
);
720 /* WARNING: NEVER FREE THE full VARIABLE!!!!!!!!!!!!!!!!!!!!!!!! */
721 /* It is used by putenv. Freeing it will corrupt the environment */
725 static void my_putenv_expand (char *name
, char macro_code
)
729 data
= expand_format (macro_code
);
730 my_putenv (name
, data
);
734 /* Puts some status information in to the environment so that
735 processes to be executed can access it. */
736 static void prepare_environment (void)
738 my_putenv_expand ("MC_CURRENT_DIR", 'd');
739 my_putenv_expand ("MC_OTHER_DIR", 'D');
740 my_putenv_expand ("MC_CURRENT_FILE", 'f');
741 my_putenv_expand ("MC_OTHER_FILE", 'F');
742 my_putenv_expand ("MC_CURRENT_TAGGED", 't');
743 my_putenv_expand ("MC_OTHER_TAGGED", 'T');
744 /* MC_CONTROL_FILE has been added to environment on startup */
748 char *unix_error_string (int error_num
)
750 static char buffer
[256];
754 error_msg
= strerror (error_num
);
757 extern char *sys_errlist
[];
758 if ((0 <= error_num
) && (error_num
< sys_nerr
))
759 error_msg
= sys_errlist
[error_num
];
761 error_msg
= "strange errno";
763 sprintf (buffer
, "%s (%d)", error_msg
, error_num
);
767 char *copy_strings (const char *first
,...)
776 len
= strlen (first
);
777 va_start (ap
, first
);
779 while ((data
= va_arg (ap
, char *))!=0)
780 len
+= strlen (data
);
784 result
= xmalloc (len
, "copy_strings");
786 va_start (ap
, first
);
787 strcpy (result
, first
);
788 while ((data
= va_arg (ap
, char *)) != 0)
789 strcat (result
, data
);
795 long blocks2kilos (int blocks
, int bsize
)
798 return blocks
* (bsize
/ 1024);
799 } else if (bsize
< 1024){
800 return blocks
/ (1024 /bsize
);
805 void init_my_statfs (void)
808 mount_list
= read_filesystem_list (1, 1);
812 char *skip_separators (char *s
)
815 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
820 char *skip_numbers (char *s
)
828 /* Remove all control sequences from the argument string. We define
829 * "control sequence", in a sort of pidgin BNF, as follows:
831 * control-seq = Esc non-'['
832 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
834 * This scheme works for all the terminals described in my termcap /
835 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
836 * terminals. If I hear from a single person who uses such a terminal
837 * with MC, I'll be glad to add support for it. (Dugan)
840 char *strip_ctrl_codes (char *s
)
842 int i
; /* Current length of the string's correct (stripped) prefix */
843 int j
; /* Number of control characters we have skipped so far */
848 for (i
= 0, j
= 0; s
[i
+j
]; ++i
)
849 if (s
[i
+j
] != ESC_CHAR
){
854 if (s
[i
+j
++] == '[')
855 while (strchr ("0123456789;?", s
[i
+j
++]))
856 /* Skip the control sequence's arguments */ ;
863 #ifndef HAVE_STRCASECMP
864 /* At least one version of HP/UX lacks this */
865 /* Assumes ASCII encoding */
866 int strcasecmp (const char *s
, const char *d
)
868 register signed int result
;
871 if (result
= (0x20 | *s
) - (0x20 | *d
))
880 #endif /* HAVE_STRCASECMP */
882 /* getwd is better than getcwd, the later uses a popen ("pwd"); */
883 char *get_current_wd (char *buffer
, int size
)
888 p
= (char *) getwd (buffer
);
890 p
= getcwd (buffer
, size
);
895 #define CHECK(x) if (x == -1) return 0;
897 long get_small_endian_long (int fd
)
899 unsigned char a
, b
, c
, d
;
901 /* It needs to be read one byte at the time to avoid endianess
902 portability problems */
903 CHECK (mc_read (fd
, &a
, 1));
904 CHECK (mc_read (fd
, &b
, 1));
905 CHECK (mc_read (fd
, &c
, 1));
906 CHECK (mc_read (fd
, &d
, 1));
907 return (d
<< 24) | (c
<< 16) | (b
<< 8) | a
;
910 /* This function returns 0 if the file is not in gunzip format */
911 /* or how much memory must be allocated to load the gziped file */
912 /* Warning: this function moves the current file pointer */
913 long int is_gunzipable (int fd
, int *type
)
915 unsigned char magic
[4];
917 *type
= ISGUNZIPABLE_GUNZIP
;
919 /* Read the magic signature */
920 CHECK (mc_read (fd
, &magic
[0], 1));
921 CHECK (mc_read (fd
, &magic
[1], 1));
922 CHECK (mc_read (fd
, &magic
[2], 1));
923 CHECK (mc_read (fd
, &magic
[3], 1));
925 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
926 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)){
927 /* Read the uncompressed size of the file */
928 mc_lseek (fd
, -4, SEEK_END
);
929 return get_small_endian_long (fd
);
933 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003 && magic
[3] == 004){
934 /* Read compression type */
935 mc_lseek (fd
, 8, SEEK_SET
);
936 CHECK (mc_read (fd
, &magic
[0], 1));
937 CHECK (mc_read (fd
, &magic
[1], 1));
939 /* Gzip can handle only deflated (8) or stored (0) files */
940 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
942 /* Read the uncompressed size of the first file in the archive */
943 mc_lseek (fd
, 22, SEEK_SET
);
944 return get_small_endian_long (fd
);
947 /* PACK_MAGIC and LZH_MAGIC and compress magic */
948 if (magic
[0] == 037 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)){
949 /* In case the file is packed, sco lzhed or compress_magic, the */
950 /* program guesses that the uncompressed size is (at most) four */
951 /* times the length of the compressed size, if the compression */
952 /* ratio is more than 4:1 the end of the file is not displayed */
953 return 4*mc_lseek (fd
, 0, SEEK_END
);
956 /* BZIP and BZIP2 files */
957 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
958 (magic
[3] >= '1') && (magic
[3] <= '9')){
961 *type
= ISGUNZIPABLE_BZIP
;
962 return 5*mc_lseek (fd
, 0, SEEK_END
);
964 *type
= ISGUNZIPABLE_BZIP2
;
965 return 5*mc_lseek (fd
, 0, SEEK_END
);
972 decompress_command (int type
)
975 case ISGUNZIPABLE_GUNZIP
:
978 case ISGUNZIPABLE_BZIP
:
981 case ISGUNZIPABLE_BZIP2
:
984 /* Should never reach this place */
985 fprintf (stderr
, "Fatal: decompress_command called with an unknown argument\n");
990 decompress_command_and_arg (int type
, char **cmd
, char **flags
)
993 case ISGUNZIPABLE_GUNZIP
:
998 case ISGUNZIPABLE_BZIP
:
1004 case ISGUNZIPABLE_BZIP2
:
1012 /* Should never reach this place */
1013 fprintf (stderr
, "Fatal: decompress_command called with an unknown argument\n");
1017 void add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
1019 Hook
*new_hook
= xmalloc (sizeof (Hook
), "add_hook");
1021 new_hook
->hook_fn
= hook_fn
;
1022 new_hook
->next
= *hook_list
;
1023 new_hook
->hook_data
= data
;
1025 *hook_list
= new_hook
;
1028 void execute_hooks (Hook
*hook_list
)
1033 /* We copy the hook list first so tahat we let the hook
1034 * function call delete_hook
1038 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
1039 hook_list
= hook_list
->next
;
1044 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
1045 new_hook
= new_hook
->next
;
1048 for (hook_list
= p
; hook_list
;){
1050 hook_list
= hook_list
->next
;
1055 void delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
1057 Hook
*current
, *new_list
, *next
;
1061 for (current
= *hook_list
; current
; current
= next
){
1062 next
= current
->next
;
1063 if (current
->hook_fn
== hook_fn
)
1066 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
1068 *hook_list
= new_list
;
1071 int hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
1075 for (p
= hook_list
; p
; p
= p
->next
)
1076 if (p
->hook_fn
== hook_fn
)
1081 void wipe_password (char *passwd
)
1090 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1091 /* Returns a newly allocated string */
1092 char *convert_controls (char *s
)
1094 char *valcopy
= strdup (s
);
1097 /* Parse the escape special character */
1098 for (p
= s
, q
= valcopy
; *p
;){
1101 if ((*p
== 'e') || (*p
== 'E')){
1112 if (*p
>= 'a' && *p
<= 'z') {
1113 *q
++ = *p
++ - 'a' + 1;
1125 /* Reverse the string */
1126 char *reverse_string (char *string
)
1128 int len
= strlen (string
);
1130 const int steps
= len
/2;
1132 for (i
= 0; i
< steps
; i
++){
1133 char c
= string
[i
];
1135 string
[i
] = string
[len
-i
-1];
1136 string
[len
-i
-1] = c
;
1141 char *resolve_symlinks (char *path
)
1143 char *buf
, *buf2
, *p
, *q
, *r
, c
;
1147 if (*path
!= PATH_SEP
)
1149 r
= buf
= xmalloc (MC_MAXPATHLEN
, "resolve symlinks");
1150 buf2
= xmalloc (MC_MAXPATHLEN
, "resolve symlinks");
1155 q
= strchr (p
+ 1, PATH_SEP
);
1157 q
= strchr (p
+ 1, 0);
1163 if (mc_lstat (path
, &mybuf
) < 0) {
1169 if (!S_ISLNK (mybuf
.st_mode
))
1172 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
);
1180 if (*buf2
== PATH_SEP
)
1185 canonicalize_pathname (buf
);
1186 r
= strchr (buf
, 0);
1187 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1197 strcpy (buf
, PATH_SEP_STR
);
1198 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1204 /* Finds out a relative path from first to second, i.e. goes as many ..
1205 * as needed up in first and then goes down using second */
1206 char *diff_two_paths (char *first
, char *second
)
1208 char *p
, *q
, *r
, *s
, *buf
= 0;
1209 int i
, j
, prevlen
= -1, currlen
;
1211 first
= resolve_symlinks (first
);
1214 for (j
= 0; j
< 2; j
++) {
1217 second
= resolve_symlinks (second
);
1218 if (second
== NULL
) {
1225 r
= strchr (p
, PATH_SEP
);
1226 s
= strchr (q
, PATH_SEP
);
1230 if (strcmp (p
, q
)) {
1231 *r
= PATH_SEP
; *s
= PATH_SEP
;
1234 *r
= PATH_SEP
; *s
= PATH_SEP
;
1240 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1241 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1243 if (currlen
< prevlen
)
1251 p
= buf
= xmalloc (currlen
, "diff 2 paths");
1253 for (; i
>= 0; i
--, p
+= 3)
1262 #ifndef HAVE_TRUNCATE
1263 /* On SCO and Windows NT systems */
1264 int my_ftruncate (int fd
, long size
)
1267 if(_chsize(fd
, size
))
1278 return fcntl (fd
, F_FREESP
, &lk
);
1282 int truncate (const char *path
, long size
)
1287 fd
= open (path
, O_RDWR
, 0);
1290 res
= my_ftruncate (fd
, size
);
1301 concat_dir_and_file (const char *dir
, const char *file
)
1303 int l
= strlen (dir
);
1305 if (dir
[l
-1] == PATH_SEP
)
1306 return copy_strings (dir
, file
, 0);
1308 return copy_strings (dir
, PATH_SEP_STR
, file
, 0);