2 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2007 Free Software Foundation, Inc.
4 Written 1994, 1995, 1996 by:
5 Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
6 Jakub Jelinek, Mauricio Plaza.
8 The file_date routine is mostly from GNU's fileutils package,
9 written by Richard Stallman and David MacKenzie.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
34 #include <sys/types.h>
41 #include <mhl/escape.h>
42 #include <mhl/string.h>
47 #include "main.h" /* mc_home */
48 #include "cmd.h" /* guess_message_value */
49 #include "mountlist.h"
50 #include "win.h" /* xterm_flag */
61 static const char app_text
[] = "Midnight-Commander";
62 int easy_patterns
= 1;
64 #if SLANG_VERSION >= 20000
65 void SLsmg_write_nwchars(wchar_t *s
, size_t n
)
67 if (SLsmg_is_utf8_mode()) { /* slang can handle it directly */
69 SLsmg_write_char(*s
++);
71 else { /* convert wchars back to 8bit encoding */
73 memset (&mbs
, 0, sizeof (mbs
));
75 char buf
[MB_LEN_MAX
+ 1]; /* should use 1 char, but to be sure */
77 SLsmg_write_char(*s
++); /* ASCII */
80 if (wcrtomb(buf
, *s
++, &mbs
) == 1)
81 SLsmg_write_char((wchar_t)(buf
[0]));
83 SLsmg_write_char('?'); /* should not happen */
90 extern void str_replace(char *s
, char from
, char to
)
92 for (; *s
!= '\0'; s
++) {
99 is_7bit_printable (unsigned char c
)
101 return (c
> 31 && c
< 127);
105 is_iso_printable (unsigned char c
)
107 return ((c
> 31 && c
< 127) || c
>= 160);
111 is_8bit_printable (unsigned char c
)
113 /* "Full 8 bits output" doesn't work on xterm */
115 return is_iso_printable (c
);
117 return (c
> 31 && c
!= 127 && c
!= 155);
121 mbstrlen (const char *str
)
124 if (SLsmg_Is_Unicode
) {
127 for (; *str
; str
++) {
131 len
= mbrtowc (&c
, str
, MB_CUR_MAX
, NULL
);
133 if (len
== (size_t)(-1) || len
== (size_t)(-2)) break;
136 int wcsize
= wcwidth(c
);
137 width
+= wcsize
> 0 ? wcsize
: 0;
159 memset (&mbs
, 0, sizeof (mbs
));
160 len
= mbrlen(p
, MB_CUR_MAX
, &mbs
);
164 } else if (len
> 0) {
177 mbstr_to_wchar (const char *str
)
179 int len
= mbstrlen(str
);
180 wchar_t *buf
= g_malloc((len
+1) * sizeof(wchar_t));
182 memset (&mbs
, 0, sizeof (mbs
));
183 mbsrtowcs (buf
, &str
, len
, &mbs
);
189 wchar_to_mbstr (const wchar_t *wstr
)
192 const wchar_t *wstr2
;
196 memset (&mbs
, 0, sizeof (mbs
));
198 len
= wcsrtombs(NULL
, &wstr2
, 0, &mbs
);
202 string
= g_malloc(len
+ 1);
205 wcsrtombs(string
, &wstr2
, len
, &mbs
);
217 if (SLsmg_Is_Unicode
)
223 /* "Display bits" is ignored, since the user controls the output
224 by setting the output codepage */
225 return is_8bit_printable (c
);
227 if (!eight_bit_clean
)
228 return is_7bit_printable (c
);
230 if (full_eight_bits
) {
231 return is_8bit_printable (c
);
233 return is_iso_printable (c
);
234 #endif /* !HAVE_CHARSET */
237 /* Calculates the message dimension in columns and lines. */
239 msglen (const char *text
, int *lines
, int *columns
)
241 int nlines
= 1; /* even the empty string takes one line */
245 for (; *text
!= '\0'; text
++) {
252 if (colindex
> ncolumns
)
257 len
= mbrtowc (&c
, text
, MB_CUR_MAX
, NULL
);
258 if (len
> 0 && len
!= (size_t)(-1) && len
!= (size_t)(-2)) {
259 int wcsize
= wcwidth(c
);
260 colindex
+= wcsize
> 0 ? wcsize
-1 : -1;
263 if (++colindex
> ncolumns
)
274 * Copy from s to d, and trim the beginning if necessary, and prepend
275 * "..." in this case. The destination string can have at most len
276 * bytes, not counting trailing 0.
279 trim (const char *s
, char *d
, int len
)
286 source_len
= strlen (s
);
287 if (source_len
> len
) {
288 /* Cannot fit the whole line */
290 /* We only have room for the dots */
291 memset (d
, '.', len
);
295 /* Begin with ... and add the rest of the source string */
297 strcpy (d
+ 3, s
+ 3 + source_len
- len
);
300 /* We can copy the whole line */
306 * Quote the filename for the purpose of inserting it into the command
307 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
308 * processed by the mc command line.
311 name_quote (const char *s
, int quote_percent
)
315 d
= ret
= g_malloc (strlen (s
) * 2 + 2 + 1);
321 for (; *s
; s
++, d
++) {
364 memset (&mbs
, 0, sizeof (mbs
));
365 len
= mbrlen(s
, MB_CUR_MAX
, &mbs
);
382 fake_name_quote (const char *s
, int quote_percent
)
384 (void) quote_percent
;
389 * Remove the middle part of the string to fit given length.
390 * Use "~" to show where the string was truncated.
391 * Return static buffer, no need to free() it.
394 name_trunc (const char *txt
, int trunc_len
)
396 static char x
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
397 int txt_len
, first
, skip
;
401 if ((size_t) trunc_len
> sizeof (x
) - 1) {
402 trunc_len
= sizeof (x
) - 1;
404 txt_len
= mbstrlen (txt
);
407 if (txt_len
> trunc_len
) {
408 first
= trunc_len
/ 2;
409 skip
= txt_len
- trunc_len
+ 1;
413 if (SLsmg_Is_Unicode
) {
418 memset (&s
, 0, sizeof (s
));
421 while (p
< x
+ sizeof (x
) - 1 && trunc_len
) {
425 len
= mbrtowc (&wc
, str
, mbmax
, &s
);
429 memset (&s
, 0, sizeof (s
));
433 } else if (!is_printable (wc
)) {
437 } else if (p
>= x
+ sizeof (x
) - len
)
440 memcpy (p
, str
, len
);
447 if (!first
&& p
< x
+ sizeof (x
) - 1 && trunc_len
) {
463 while (p
< x
+ sizeof (x
) - 1) {
466 else if (!is_printable (*str
))
485 * path_trunc() is the same as name_trunc() above but
486 * it deletes possible password from path for security
490 path_trunc (const char *path
, int trunc_len
) {
492 char *secure_path
= strip_password (g_strdup (path
), 1);
494 ret
= name_trunc (secure_path
, trunc_len
);
495 g_free (secure_path
);
501 size_trunc (double size
)
503 static char x
[BUF_TINY
];
504 long int divisor
= 1;
505 const char *xtra
= "";
507 if (size
> 999999999L){
510 if (size
/divisor
> 999999999L){
515 g_snprintf (x
, sizeof (x
), "%.0f%s", (size
/divisor
), xtra
);
520 size_trunc_sep (double size
)
527 p
= y
= size_trunc (size
);
529 d
= x
+ sizeof (x
) - 1;
531 while (p
>= y
&& isalpha ((unsigned char) *p
))
533 for (count
= 0; p
>= y
; count
++){
547 * Print file SIZE to BUFFER, but don't exceed LEN characters,
548 * not including trailing 0. BUFFER should be at least LEN+1 long.
549 * This function is called for every file on panels, so avoid
550 * floating point by any means.
552 * Units: size units (filesystem sizes are 1K blocks)
553 * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
556 size_trunc_len (char *buffer
, int len
, off_t size
, int units
)
558 /* Avoid taking power for every file. */
559 static const off_t power10
[] =
560 {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
562 static const char * const suffix
[] =
563 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL
};
566 /* Don't print more than 9 digits - use suffix. */
567 if (len
== 0 || len
> 9)
570 for (j
= units
; suffix
[j
] != NULL
; j
++) {
573 /* Empty files will print "0" even with minimal width. */
574 g_snprintf (buffer
, len
+ 1, "0");
578 /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
579 g_snprintf (buffer
, len
+ 1, (len
> 1) ? "~%s" : "%s",
580 (j
> 1) ? suffix
[j
- 1] : "B");
584 if (size
< power10
[len
- (j
> 0)]) {
585 g_snprintf (buffer
, len
+ 1, "%lu%s", (unsigned long) size
, suffix
[j
]);
589 /* Powers of 1024, with rounding. */
590 size
= (size
+ 512) >> 10;
597 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
602 #define ismode(n,m) ((n & m) == m)
605 string_perm (mode_t mode_bits
)
607 static char mode
[11];
609 strcpy (mode
, "----------");
610 if (S_ISDIR (mode_bits
))
612 if (S_ISCHR (mode_bits
))
614 if (S_ISBLK (mode_bits
))
616 if (S_ISLNK (mode_bits
))
618 if (S_ISFIFO (mode_bits
))
620 if (S_ISNAM (mode_bits
))
622 if (S_ISSOCK (mode_bits
))
624 if (S_ISDOOR (mode_bits
))
626 if (ismode (mode_bits
, S_IXOTH
))
628 if (ismode (mode_bits
, S_IWOTH
))
630 if (ismode (mode_bits
, S_IROTH
))
632 if (ismode (mode_bits
, S_IXGRP
))
634 if (ismode (mode_bits
, S_IWGRP
))
636 if (ismode (mode_bits
, S_IRGRP
))
638 if (ismode (mode_bits
, S_IXUSR
))
640 if (ismode (mode_bits
, S_IWUSR
))
642 if (ismode (mode_bits
, S_IRUSR
))
645 if (ismode (mode_bits
, S_ISUID
))
646 mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
649 if (ismode (mode_bits
, S_ISGID
))
650 mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
653 if (ismode (mode_bits
, S_ISVTX
))
654 mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
659 /* p: string which might contain an url with a password (this parameter is
661 has_prefix = 0: The first parameter is an url without a prefix
662 (user[:pass]@]machine[:port][remote-dir). Delete
664 has_prefix = 1: Search p for known url prefixes. If found delete
665 the password from the url.
666 Caveat: only the first url is found
669 strip_password (char *p
, int has_prefix
)
671 static const struct {
674 } prefixes
[] = { {"/#ftp:", 6},
684 char *at
, *inner_colon
, *dir
;
688 for (i
= 0; i
< sizeof (prefixes
)/sizeof (prefixes
[0]); i
++) {
692 if((q
= strstr (p
, prefixes
[i
].name
)) == 0)
695 p
= q
+ prefixes
[i
].len
;
698 if ((dir
= strchr (p
, PATH_SEP
)) != NULL
)
701 /* search for any possible user */
702 at
= strrchr (p
, '@');
707 /* We have a username */
709 inner_colon
= memchr (p
, ':', at
- p
);
711 memmove (inner_colon
, at
, strlen(at
) + 1);
719 strip_home_and_password(const char *dir
)
722 static char newdir
[MC_MAXPATHLEN
];
724 if (home_dir
&& !strncmp (dir
, home_dir
, len
= strlen (home_dir
)) &&
725 (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0')){
727 g_strlcpy (&newdir
[1], &dir
[len
], sizeof(newdir
) - 1);
731 /* We do not strip homes in /#ftp tree, I do not like ~'s there
733 g_strlcpy (newdir
, dir
, sizeof(newdir
));
734 strip_password (newdir
, 1);
739 maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
752 maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
764 /* If shell patterns are on converts a shell pattern to a regular
765 expression. Called by regexp_match and mask_rename. */
766 /* Shouldn't we support [a-fw] type wildcards as well ?? */
768 convert_pattern (const char *pattern
, int match_type
, int do_group
)
772 int was_wildcard
= 0;
775 if ((match_type
!= match_regex
) && easy_patterns
){
776 new_pattern
= g_malloc (MC_MAXPATHLEN
);
778 if (match_type
== match_file
)
780 for (s
= pattern
; *s
; s
++, d
++){
783 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
789 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
794 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
800 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
805 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
806 if (match_type
== match_file
)
811 return g_strdup (pattern
);
815 regexp_match (const char *pattern
, const char *string
, int match_type
)
818 static char *old_pattern
= NULL
;
823 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
826 g_free (old_pattern
);
829 my_pattern
= convert_pattern (pattern
, match_type
, 0);
830 if (regcomp (&r
, my_pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
834 old_pattern
= my_pattern
;
835 old_type
= match_type
;
837 rval
= !regexec (&r
, string
, 0, NULL
, 0);
842 extension (const char *filename
)
844 const char *d
= strrchr (filename
, '.');
845 return (d
!= NULL
) ? d
+ 1 : "";
849 get_int (const char *file
, const char *key
, int def
)
851 return GetPrivateProfileInt (app_text
, key
, def
, file
);
855 set_int (const char *file
, const char *key
, int value
)
857 char buffer
[BUF_TINY
];
859 g_snprintf (buffer
, sizeof (buffer
), "%d", value
);
860 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
864 get_config_string (const char *file
, const char *key
, const char *defval
)
867 (void)GetPrivateProfileString (app_text
, key
, defval
, buffer
, sizeof(buffer
), file
);
868 return g_strdup (buffer
);
872 set_config_string (const char *file
, const char *key
, const char *val
)
874 (void)WritePrivateProfileString (app_text
, key
, val
, file
);
878 exist_file (const char *name
)
880 return access (name
, R_OK
) == 0;
884 load_file (const char *filename
)
891 if ((data_file
= fopen (filename
, "r")) == NULL
){
894 if (fstat (fileno (data_file
), &s
) != 0){
898 data
= g_malloc (s
.st_size
+1);
899 read_size
= fread (data
, 1, s
.st_size
, data_file
);
900 data
[read_size
] = 0;
912 utf8_to_local(char *str
)
922 buflen
= strlen(str
);
924 cd
= iconv_open (nl_langinfo(CODESET
), "UTF-8");
925 if (cd
== (iconv_t
) -1) {
926 return g_strdup(str
);
929 output
= g_malloc(buflen
+ 1);
933 char *wrptr
= output
;
935 size_t insize
= buflen
;
936 size_t avail
= buflen
;
939 nconv
= iconv (cd
, &inptr
, &insize
, &wrptr
, &avail
);
940 if (nconv
== (size_t) -1)
946 output
= g_malloc(buflen
+ 1);
951 return g_strdup(str
);
966 load_mc_home_file (const char *filename
, char **allocated_filename
)
968 char *hintfile_base
, *hintfile
;
973 hintfile_base
= mhl_str_dir_plus_file (mc_home
, filename
);
974 lang
= guess_message_value ();
976 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
977 data
= load_file (hintfile
);
981 /* Fall back to the two-letter language code */
982 if (lang
[0] && lang
[1])
984 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
985 data
= load_file (hintfile
);
989 hintfile
= hintfile_base
;
990 data
= load_file (hintfile_base
);
996 if (hintfile
!= hintfile_base
)
997 g_free (hintfile_base
);
999 if (allocated_filename
)
1000 *allocated_filename
= hintfile
;
1004 conv_data
= utf8_to_local(data
);
1010 /* Check strftime() results. Some systems (i.e. Solaris) have different
1011 short-month-name sizes for different locales */
1013 i18n_checktimelength (void)
1015 time_t testtime
= time (NULL
);
1016 struct tm
* lt
= localtime(&testtime
);
1020 // huh, localtime() doesnt seem to work ... falling back to "(invalid)"
1021 length
= strlen(INVALID_TIME_TEXT
);
1023 char buf
[4* MAX_I18NTIMELENGTH
+ 1];
1025 strftime (buf
, sizeof(buf
)-1, _("%b %e %H:%M"), lt
);
1027 strftime (buf
, sizeof(buf
)-1, _("%b %e %Y"), lt
);
1029 length
= max (a
, b
);
1032 /* Don't handle big differences. Use standard value (email bug, please) */
1033 if ( length
> MAX_I18NTIMELENGTH
|| length
< MIN_I18NTIMELENGTH
)
1034 length
= STD_I18NTIMELENGTH
;
1040 file_date (time_t when
)
1042 static char timebuf
[4 * MAX_I18NTIMELENGTH
+ 1];
1043 time_t current_time
= time ((time_t) 0);
1044 static const char *fmtyear
, *fmttime
;
1047 if ( fmtyear
== NULL
) {
1048 /* strftime() format string for old dates */
1049 fmtyear
= _("%b %e %Y");
1050 /* strftime() format string for recent dates */
1051 fmttime
= _("%b %e %H:%M");
1054 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
1055 || current_time
< when
- 60L * 60L) /* In the future. */
1056 /* The file is fairly old or in the future.
1057 POSIX says the cutoff is 6 months old;
1058 approximate this by 6*30 days.
1059 Allow a 1 hour slop factor for what is considered "the future",
1060 to allow for NFS server/client clock disagreement.
1061 Show the year instead of the time of day. */
1067 FMT_LOCALTIME(timebuf
, sizeof(timebuf
), fmt
, when
);
1073 extract_line (const char *s
, const char *top
)
1075 static char tmp_line
[BUF_MEDIUM
];
1078 while (*s
&& *s
!= '\n' && (size_t) (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
1084 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
1086 _icase_search (const char *text
, const char *data
, int *lng
)
1088 const char *d
= text
;
1089 const char *e
= data
;
1095 while (*(e
+1) == '\b' && *(e
+2)) {
1099 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
1108 *lng
= strlen (text
) + dlng
;
1115 /* The basename routine */
1117 x_basename (const char *s
)
1120 return ((where
= strrchr (s
, PATH_SEP
))) ? where
+ 1 : s
;
1125 unix_error_string (int error_num
)
1127 static char buffer
[BUF_LARGE
];
1128 #if GLIB_MAJOR_VERSION >= 2
1129 gchar
*strerror_currentlocale
;
1131 strerror_currentlocale
= g_locale_from_utf8(g_strerror (error_num
), -1, NULL
, NULL
, NULL
);
1132 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
1133 strerror_currentlocale
, error_num
);
1134 g_free(strerror_currentlocale
);
1136 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
1137 g_strerror (error_num
), error_num
);
1143 skip_separators (const char *s
)
1146 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
1152 skip_numbers (const char *s
)
1155 if (!isdigit ((unsigned char) *s
))
1160 /* Remove all control sequences from the argument string. We define
1161 * "control sequence", in a sort of pidgin BNF, as follows:
1163 * control-seq = Esc non-'['
1164 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
1166 * This scheme works for all the terminals described in my termcap /
1167 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
1168 * terminals. If I hear from a single person who uses such a terminal
1169 * with MC, I'll be glad to add support for it. (Dugan)
1170 * Non-printable characters are also removed.
1174 strip_ctrl_codes (char *s
)
1176 char *w
; /* Current position where the stripped data is written */
1177 char *r
; /* Current position where the original data is read */
1182 for (w
= s
, r
= s
; *r
; ) {
1183 if (*r
== ESC_CHAR
) {
1184 /* Skip the control sequence's arguments */ ;
1185 if (*(++r
) == '[') {
1186 /* strchr() matches trailing binary 0 */
1187 while (*(++r
) && strchr ("0123456789;?", *r
));
1191 * Now we are at the last character of the sequence.
1192 * Skip it unless it's binary 0.
1199 if (is_printable(*r
))
1206 memset (&mbs
, 0, sizeof (mbs
));
1207 len
= mbrlen(r
, MB_CUR_MAX
, &mbs
);
1209 if (len
> 0 && (unsigned char)*r
>= ' ')
1227 get_current_wd (char *buffer
, int size
)
1232 p
= g_get_current_dir ();
1233 len
= strlen(p
) + 1;
1240 memcpy (buffer
, p
, len
);
1245 #endif /* !USE_VFS */
1247 enum compression_type
1248 get_compression_type (int fd
)
1250 unsigned char magic
[4];
1252 /* Read the magic signature */
1253 if (mc_read (fd
, (char *) magic
, 4) != 4)
1254 return COMPRESSION_NONE
;
1256 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
1257 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)) {
1258 return COMPRESSION_GZIP
;
1262 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003
1263 && magic
[3] == 004) {
1264 /* Read compression type */
1265 mc_lseek (fd
, 8, SEEK_SET
);
1266 if (mc_read (fd
, (char *) magic
, 2) != 2)
1267 return COMPRESSION_NONE
;
1269 /* Gzip can handle only deflated (8) or stored (0) files */
1270 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
1271 return COMPRESSION_NONE
;
1273 /* Compatible with gzip */
1274 return COMPRESSION_GZIP
;
1277 /* PACK_MAGIC and LZH_MAGIC and compress magic */
1279 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)) {
1280 /* Compatible with gzip */
1281 return COMPRESSION_GZIP
;
1284 /* BZIP and BZIP2 files */
1285 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
1286 (magic
[3] >= '1') && (magic
[3] <= '9')) {
1289 return COMPRESSION_BZIP
;
1291 return COMPRESSION_BZIP2
;
1298 decompress_extension (int type
)
1301 case COMPRESSION_GZIP
: return "#ugz";
1302 case COMPRESSION_BZIP
: return "#ubz";
1303 case COMPRESSION_BZIP2
: return "#ubz2";
1305 /* Should never reach this place */
1306 fprintf (stderr
, "Fatal: decompress_extension called with an unknown argument\n");
1312 add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
1314 Hook
*new_hook
= g_new (Hook
, 1);
1316 new_hook
->hook_fn
= hook_fn
;
1317 new_hook
->next
= *hook_list
;
1318 new_hook
->hook_data
= data
;
1320 *hook_list
= new_hook
;
1324 execute_hooks (Hook
*hook_list
)
1329 /* We copy the hook list first so tahat we let the hook
1330 * function call delete_hook
1334 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
1335 hook_list
= hook_list
->next
;
1340 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
1341 new_hook
= new_hook
->next
;
1344 for (hook_list
= p
; hook_list
;){
1346 hook_list
= hook_list
->next
;
1352 delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
1354 Hook
*current
, *new_list
, *next
;
1358 for (current
= *hook_list
; current
; current
= next
){
1359 next
= current
->next
;
1360 if (current
->hook_fn
== hook_fn
)
1363 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
1365 *hook_list
= new_list
;
1369 hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
1373 for (p
= hook_list
; p
; p
= p
->next
)
1374 if (p
->hook_fn
== hook_fn
)
1380 wipe_password (char *passwd
)
1391 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1392 /* Returns a newly allocated string */
1394 convert_controls (const char *p
)
1396 char *valcopy
= g_strdup (p
);
1399 /* Parse the escape special character */
1400 for (q
= valcopy
; *p
;){
1403 if ((*p
== 'e') || (*p
== 'E')){
1413 char c
= (*p
| 0x20);
1414 if (c
>= 'a' && c
<= 'z') {
1429 resolve_symlinks (const char *path
)
1431 char *buf
, *buf2
, *q
, *r
, c
;
1436 if (*path
!= PATH_SEP
)
1438 r
= buf
= g_malloc (MC_MAXPATHLEN
);
1439 buf2
= g_malloc (MC_MAXPATHLEN
);
1444 q
= strchr (p
+ 1, PATH_SEP
);
1446 q
= strchr (p
+ 1, 0);
1452 if (mc_lstat (path
, &mybuf
) < 0) {
1458 if (!S_ISLNK (mybuf
.st_mode
))
1461 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
- 1);
1469 if (*buf2
== PATH_SEP
)
1474 canonicalize_pathname (buf
);
1475 r
= strchr (buf
, 0);
1476 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1486 strcpy (buf
, PATH_SEP_STR
);
1487 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1493 /* Finds out a relative path from first to second, i.e. goes as many ..
1494 * as needed up in first and then goes down using second */
1496 diff_two_paths (const char *first
, const char *second
)
1498 char *p
, *q
, *r
, *s
, *buf
= NULL
;
1499 int i
, j
, prevlen
= -1, currlen
;
1500 char *my_first
= NULL
, *my_second
= NULL
;
1502 my_first
= resolve_symlinks (first
);
1503 if (my_first
== NULL
)
1505 my_second
= resolve_symlinks (second
);
1506 if (my_second
== NULL
) {
1510 for (j
= 0; j
< 2; j
++) {
1514 r
= strchr (p
, PATH_SEP
);
1515 s
= strchr (q
, PATH_SEP
);
1519 if (strcmp (p
, q
)) {
1520 *r
= PATH_SEP
; *s
= PATH_SEP
;
1523 *r
= PATH_SEP
; *s
= PATH_SEP
;
1529 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1530 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1532 if (currlen
< prevlen
)
1540 p
= buf
= g_malloc (currlen
);
1542 for (; i
>= 0; i
--, p
+= 3)
1551 /* Append text to GList, remove all entries with the same text */
1553 list_append_unique (GList
*list
, char *text
)
1555 GList
*link
, *newlink
;
1558 * Go to the last position and traverse the list backwards
1559 * starting from the second last entry to make sure that we
1560 * are not removing the current link.
1562 list
= g_list_append (list
, text
);
1563 list
= g_list_last (list
);
1564 link
= g_list_previous (list
);
1567 newlink
= g_list_previous (link
);
1568 if (!strcmp ((char *) link
->data
, text
)) {
1569 g_free (link
->data
);
1570 g_list_remove_link (list
, link
);
1571 g_list_free_1 (link
);
1579 /* Following code heavily borrows from libiberty, mkstemps.c */
1581 /* Number of attempts to create a temporary file */
1583 #define TMP_MAX 16384
1584 #endif /* !TMP_MAX */
1588 * pname (output) - pointer to the name of the temp file (needs g_free).
1589 * NULL if the function fails.
1590 * prefix - part of the filename before the random part.
1591 * Prepend $TMPDIR or /tmp if there are no path separators.
1592 * suffix - if not NULL, part of the filename after the random part.
1595 * handle of the open file or -1 if couldn't open any.
1598 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
1600 static const char letters
[]
1601 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1602 static unsigned long value
;
1609 if (strchr (prefix
, PATH_SEP
) == NULL
) {
1610 /* Add prefix first to find the position of XXXXXX */
1611 tmpbase
= mhl_str_dir_plus_file (mc_tmpdir (), prefix
);
1613 tmpbase
= g_strdup (prefix
);
1616 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
1618 XXXXXX
= &tmpname
[strlen (tmpbase
)];
1621 /* Get some more or less random data. */
1622 gettimeofday (&tv
, NULL
);
1623 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
1625 for (count
= 0; count
< TMP_MAX
; ++count
) {
1626 unsigned long v
= value
;
1629 /* Fill in the random bits. */
1630 XXXXXX
[0] = letters
[v
% 62];
1632 XXXXXX
[1] = letters
[v
% 62];
1634 XXXXXX
[2] = letters
[v
% 62];
1636 XXXXXX
[3] = letters
[v
% 62];
1638 XXXXXX
[4] = letters
[v
% 62];
1640 XXXXXX
[5] = letters
[v
% 62];
1642 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
,
1645 /* Successfully created. */
1649 /* This is a random value. It is only necessary that the next
1650 TMP_MAX values generated by adding 7777 to VALUE are different
1651 with (module 2^32). */
1655 /* Unsuccessful. Free the filename. */
1663 * Read and restore position for the given filename.
1664 * If there is no stored data, return line 1 and col 0.
1667 load_file_position (const char *filename
, long *line
, long *column
)
1671 char buf
[MC_MAXPATHLEN
+ 20];
1678 /* open file with positions */
1679 fn
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS
);
1680 f
= fopen (fn
, "r");
1685 len
= strlen (filename
);
1687 while (fgets (buf
, sizeof (buf
), f
)) {
1690 /* check if the filename matches the beginning of string */
1691 if (strncmp (buf
, filename
, len
) != 0)
1694 /* followed by single space */
1695 if (buf
[len
] != ' ')
1698 /* and string without spaces */
1700 if (strchr (p
, ' '))
1703 *line
= strtol(p
, const_cast(char **, &p
), 10);
1705 *column
= strtol(p
+1, const_cast(char **, &p
), 10);
1714 /* Save position for the given file */
1716 save_file_position (const char *filename
, long line
, long column
)
1720 char buf
[MC_MAXPATHLEN
+ 20];
1724 len
= strlen (filename
);
1726 tmp
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS_TMP
);
1727 fn
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS
);
1729 /* open temporary file */
1730 t
= fopen (tmp
, "w");
1737 /* put the new record */
1738 if (line
!= 1 || column
!= 0) {
1739 fprintf (t
, "%s %ld;%ld\n", filename
, line
, column
);
1742 /* copy records from the old file */
1743 f
= fopen (fn
, "r");
1745 while (fgets (buf
, sizeof (buf
), f
)) {
1746 /* Skip entries for the current filename */
1747 if (strncmp (buf
, filename
, len
) == 0 && buf
[len
] == ' '
1748 && !strchr (&buf
[len
+ 1], ' '))
1751 fprintf (t
, "%s", buf
);
1752 if (++i
> MC_FILEPOS_ENTRIES
)
1765 cstrcasestr (const char *haystack
, const char *needle
)
1768 size_t i
, needle_len
;
1770 needle_len
= strlen (needle
);
1771 for (hptr
= haystack
; *hptr
!= '\0'; hptr
++) {
1772 for (i
= 0; i
< needle_len
; i
++) {
1773 if (toupper ((unsigned char) hptr
[i
]) !=
1774 toupper ((unsigned char) needle
[i
]))
1785 cstrstr (const char *haystack
, const char *needle
)
1787 return strstr(haystack
, needle
);
1791 str_unconst (const char *s
)
1796 #define ASCII_A (0x40 + 1)
1797 #define ASCII_Z (0x40 + 26)
1798 #define ASCII_a (0x60 + 1)
1799 #define ASCII_z (0x60 + 26)
1802 ascii_alpha_to_cntrl (int ch
)
1804 if ((ch
>= ASCII_A
&& ch
<= ASCII_Z
)
1805 || (ch
>= ASCII_a
&& ch
<= ASCII_z
)) {
1814 const char *result
, *sep
;
1817 sep
= strchr(result
, '|');
1818 return (sep
!= NULL
) ? sep
+ 1 : result
;