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. */
33 #include <sys/types.h>
37 #include <mhl/string.h>
41 #include "main.h" /* mc_home */
42 #include "cmd.h" /* guess_message_value */
43 #include "mountlist.h"
44 #include "win.h" /* xterm_flag */
51 static const char app_text
[] = "Midnight-Commander";
52 int easy_patterns
= 1;
54 extern void str_replace(char *s
, char from
, char to
)
56 for (; *s
!= '\0'; s
++) {
63 is_7bit_printable (unsigned char c
)
65 return (c
> 31 && c
< 127);
69 is_iso_printable (unsigned char c
)
71 return ((c
> 31 && c
< 127) || c
>= 160);
75 is_8bit_printable (unsigned char c
)
77 /* "Full 8 bits output" doesn't work on xterm */
79 return is_iso_printable (c
);
81 return (c
> 31 && c
!= 127 && c
!= 155);
90 /* "Display bits" is ignored, since the user controls the output
91 by setting the output codepage */
92 return is_8bit_printable (c
);
95 return is_7bit_printable (c
);
97 if (full_eight_bits
) {
98 return is_8bit_printable (c
);
100 return is_iso_printable (c
);
101 #endif /* !HAVE_CHARSET */
104 /* Calculates the message dimensions (lines and columns) */
106 msglen (const char *text
, int *lines
, int *columns
)
108 int nlines
= 1; /* even the empty string takes one line */
112 for (; *text
!= '\0'; text
++) {
118 if (colindex
> ncolumns
)
128 * Copy from s to d, and trim the beginning if necessary, and prepend
129 * "..." in this case. The destination string can have at most len
130 * bytes, not counting trailing 0.
133 trim (const char *s
, char *d
, int len
)
140 source_len
= strlen (s
);
141 if (source_len
> len
) {
142 /* Cannot fit the whole line */
144 /* We only have room for the dots */
145 memset (d
, '.', len
);
149 /* Begin with ... and add the rest of the source string */
151 strcpy (d
+ 3, s
+ 3 + source_len
- len
);
154 /* We can copy the whole line */
160 * Quote the filename for the purpose of inserting it into the command
161 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
162 * processed by the mc command line.
165 name_quote (const char *s
, int quote_percent
)
169 d
= ret
= g_malloc (strlen (s
) * 2 + 2 + 1);
175 for (; *s
; s
++, d
++) {
219 fake_name_quote (const char *s
, int quote_percent
)
221 (void) quote_percent
;
222 return mhl_str_dup (s
);
226 * Remove the middle part of the string to fit given length.
227 * Use "~" to show where the string was truncated.
228 * Return static buffer, no need to free() it.
231 name_trunc (const char *txt
, int trunc_len
)
233 static char x
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
237 if ((size_t) trunc_len
> sizeof (x
) - 1) {
238 trunc_len
= sizeof (x
) - 1;
240 txt_len
= strlen (txt
);
241 if (txt_len
<= trunc_len
) {
244 int y
= (trunc_len
/ 2) + (trunc_len
% 2);
246 strncpy (x
+ y
, txt
+ txt_len
- (trunc_len
/ 2), trunc_len
/ 2);
251 if (!is_printable (*p
))
257 * path_trunc() is the same as name_trunc() above but
258 * it deletes possible password from path for security
262 path_trunc (const char *path
, int trunc_len
) {
264 char *secure_path
= strip_password (mhl_str_dup (path
), 1);
266 ret
= name_trunc (secure_path
, trunc_len
);
267 g_free (secure_path
);
273 size_trunc (double size
)
275 static char x
[BUF_TINY
];
276 long int divisor
= 1;
277 const char *xtra
= "";
279 if (size
> 999999999L){
282 if (size
/divisor
> 999999999L){
287 g_snprintf (x
, sizeof (x
), "%.0f%s", (size
/divisor
), xtra
);
292 size_trunc_sep (double size
)
299 p
= y
= size_trunc (size
);
301 d
= x
+ sizeof (x
) - 1;
303 while (p
>= y
&& isalpha ((unsigned char) *p
))
305 for (count
= 0; p
>= y
; count
++){
319 * Print file SIZE to BUFFER, but don't exceed LEN characters,
320 * not including trailing 0. BUFFER should be at least LEN+1 long.
321 * This function is called for every file on panels, so avoid
322 * floating point by any means.
324 * Units: size units (filesystem sizes are 1K blocks)
325 * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
328 size_trunc_len (char *buffer
, int len
, off_t size
, int units
)
330 /* Avoid taking power for every file. */
331 static const off_t power10
[] =
332 {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
334 static const char * const suffix
[] =
335 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL
};
338 /* Don't print more than 9 digits - use suffix. */
339 if (len
== 0 || len
> 9)
342 for (j
= units
; suffix
[j
] != NULL
; j
++) {
345 /* Empty files will print "0" even with minimal width. */
346 g_snprintf (buffer
, len
+ 1, "0");
350 /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
351 g_snprintf (buffer
, len
+ 1, (len
> 1) ? "~%s" : "%s",
352 (j
> 1) ? suffix
[j
- 1] : "B");
356 if (size
< power10
[len
- (j
> 0)]) {
357 g_snprintf (buffer
, len
+ 1, "%lu%s", (unsigned long) size
, suffix
[j
]);
361 /* Powers of 1024, with rounding. */
362 size
= (size
+ 512) >> 10;
369 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
374 #define ismode(n,m) ((n & m) == m)
377 string_perm (mode_t mode_bits
)
379 static char mode
[11];
381 strcpy (mode
, "----------");
382 if (S_ISDIR (mode_bits
))
384 if (S_ISCHR (mode_bits
))
386 if (S_ISBLK (mode_bits
))
388 if (S_ISLNK (mode_bits
))
390 if (S_ISFIFO (mode_bits
))
392 if (S_ISNAM (mode_bits
))
394 if (S_ISSOCK (mode_bits
))
396 if (S_ISDOOR (mode_bits
))
398 if (ismode (mode_bits
, S_IXOTH
))
400 if (ismode (mode_bits
, S_IWOTH
))
402 if (ismode (mode_bits
, S_IROTH
))
404 if (ismode (mode_bits
, S_IXGRP
))
406 if (ismode (mode_bits
, S_IWGRP
))
408 if (ismode (mode_bits
, S_IRGRP
))
410 if (ismode (mode_bits
, S_IXUSR
))
412 if (ismode (mode_bits
, S_IWUSR
))
414 if (ismode (mode_bits
, S_IRUSR
))
417 if (ismode (mode_bits
, S_ISUID
))
418 mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
421 if (ismode (mode_bits
, S_ISGID
))
422 mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
425 if (ismode (mode_bits
, S_ISVTX
))
426 mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
431 /* p: string which might contain an url with a password (this parameter is
433 has_prefix = 0: The first parameter is an url without a prefix
434 (user[:pass]@]machine[:port][remote-dir). Delete
436 has_prefix = 1: Search p for known url prefixes. If found delete
437 the password from the url.
438 Caveat: only the first url is found
441 strip_password (char *p
, int has_prefix
)
443 static const struct {
446 } prefixes
[] = { {"/#ftp:", 6},
456 char *at
, *inner_colon
, *dir
;
460 for (i
= 0; i
< sizeof (prefixes
)/sizeof (prefixes
[0]); i
++) {
464 if((q
= strstr (p
, prefixes
[i
].name
)) == 0)
467 p
= q
+ prefixes
[i
].len
;
470 if ((dir
= strchr (p
, PATH_SEP
)) != NULL
)
473 /* search for any possible user */
474 at
= strrchr (p
, '@');
479 /* We have a username */
481 inner_colon
= memchr (p
, ':', at
- p
);
483 memmove (inner_colon
, at
, strlen(at
) + 1);
491 strip_home_and_password(const char *dir
)
494 static char newdir
[MC_MAXPATHLEN
];
496 if (home_dir
&& !strncmp (dir
, home_dir
, len
= strlen (home_dir
)) &&
497 (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0')){
499 g_strlcpy (&newdir
[1], &dir
[len
], sizeof(newdir
) - 1);
503 /* We do not strip homes in /#ftp tree, I do not like ~'s there
505 g_strlcpy (newdir
, dir
, sizeof(newdir
));
506 strip_password (newdir
, 1);
511 maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
524 maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
536 /* If shell patterns are on converts a shell pattern to a regular
537 expression. Called by regexp_match and mask_rename. */
538 /* Shouldn't we support [a-fw] type wildcards as well ?? */
540 convert_pattern (const char *pattern
, int match_type
, int do_group
)
544 int was_wildcard
= 0;
547 if ((match_type
!= match_regex
) && easy_patterns
){
548 new_pattern
= g_malloc (MC_MAXPATHLEN
);
550 if (match_type
== match_file
)
552 for (s
= pattern
; *s
; s
++, d
++){
555 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
561 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
566 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
572 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
577 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
578 if (match_type
== match_file
)
583 return mhl_str_dup (pattern
);
587 regexp_match (const char *pattern
, const char *string
, int match_type
)
590 static char *old_pattern
= NULL
;
595 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
598 g_free (old_pattern
);
601 my_pattern
= convert_pattern (pattern
, match_type
, 0);
602 if (regcomp (&r
, my_pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
606 old_pattern
= my_pattern
;
607 old_type
= match_type
;
609 rval
= !regexec (&r
, string
, 0, NULL
, 0);
614 extension (const char *filename
)
616 const char *d
= strrchr (filename
, '.');
617 return (d
!= NULL
) ? d
+ 1 : "";
621 get_int (const char *file
, const char *key
, int def
)
623 return GetPrivateProfileInt (app_text
, key
, def
, file
);
627 set_int (const char *file
, const char *key
, int value
)
629 char buffer
[BUF_TINY
];
631 g_snprintf (buffer
, sizeof (buffer
), "%d", value
);
632 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
636 get_config_string (const char *file
, const char *key
, const char *defval
)
639 (void)GetPrivateProfileString (app_text
, key
, defval
, buffer
, sizeof(buffer
), file
);
640 return mhl_str_dup (buffer
);
644 set_config_string (const char *file
, const char *key
, const char *val
)
646 (void)WritePrivateProfileString (app_text
, key
, val
, file
);
650 exist_file (const char *name
)
652 return access (name
, R_OK
) == 0;
656 load_file (const char *filename
)
663 if ((data_file
= fopen (filename
, "r")) == NULL
){
666 if (fstat (fileno (data_file
), &s
) != 0){
670 data
= g_malloc (s
.st_size
+1);
671 read_size
= fread (data
, 1, s
.st_size
, data_file
);
672 data
[read_size
] = 0;
684 load_mc_home_file (const char *filename
, char **allocated_filename
)
686 char *hintfile_base
, *hintfile
;
690 hintfile_base
= concat_dir_and_file (mc_home
, filename
);
691 lang
= guess_message_value ();
693 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
694 data
= load_file (hintfile
);
698 /* Fall back to the two-letter language code */
699 if (lang
[0] && lang
[1])
701 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
702 data
= load_file (hintfile
);
706 hintfile
= hintfile_base
;
707 data
= load_file (hintfile_base
);
713 if (hintfile
!= hintfile_base
)
714 g_free (hintfile_base
);
716 if (allocated_filename
)
717 *allocated_filename
= hintfile
;
724 /* Check strftime() results. Some systems (i.e. Solaris) have different
725 short-month-name sizes for different locales */
727 i18n_checktimelength (void)
729 time_t testtime
= time (NULL
);
730 struct tm
* lt
= localtime(&testtime
);
734 // huh, localtime() doesnt seem to work ... falling back to "(invalid)"
735 length
= strlen(INVALID_TIME_TEXT
);
737 char buf
[MAX_I18NTIMELENGTH
+ 1];
739 a
= strftime (buf
, sizeof(buf
)-1, _("%b %e %H:%M"), lt
);
740 b
= strftime (buf
, sizeof(buf
)-1, _("%b %e %Y"), lt
);
744 /* Don't handle big differences. Use standard value (email bug, please) */
745 if ( length
> MAX_I18NTIMELENGTH
|| length
< MIN_I18NTIMELENGTH
)
746 length
= STD_I18NTIMELENGTH
;
752 file_date (time_t when
)
754 static char timebuf
[MAX_I18NTIMELENGTH
+ 1];
755 time_t current_time
= time ((time_t) 0);
756 static size_t i18n_timelength
= 0;
757 static const char *fmtyear
, *fmttime
;
760 if (i18n_timelength
== 0){
761 i18n_timelength
= i18n_checktimelength() + 1;
763 /* strftime() format string for old dates */
764 fmtyear
= _("%b %e %Y");
765 /* strftime() format string for recent dates */
766 fmttime
= _("%b %e %H:%M");
769 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
770 || current_time
< when
- 60L * 60L) /* In the future. */
771 /* The file is fairly old or in the future.
772 POSIX says the cutoff is 6 months old;
773 approximate this by 6*30 days.
774 Allow a 1 hour slop factor for what is considered "the future",
775 to allow for NFS server/client clock disagreement.
776 Show the year instead of the time of day. */
782 FMT_LOCALTIME(timebuf
, i18n_timelength
, fmt
, when
);
788 extract_line (const char *s
, const char *top
)
790 static char tmp_line
[BUF_MEDIUM
];
793 while (*s
&& *s
!= '\n' && (size_t) (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
799 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
801 _icase_search (const char *text
, const char *data
, int *lng
)
803 const char *d
= text
;
804 const char *e
= data
;
810 while (*(e
+1) == '\b' && *(e
+2)) {
814 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
823 *lng
= strlen (text
) + dlng
;
830 /* The basename routine */
832 x_basename (const char *s
)
835 return ((where
= strrchr (s
, PATH_SEP
))) ? where
+ 1 : s
;
840 unix_error_string (int error_num
)
842 static char buffer
[BUF_LARGE
];
843 #if GLIB_MAJOR_VERSION >= 2
844 gchar
*strerror_currentlocale
;
846 strerror_currentlocale
= g_locale_from_utf8(g_strerror (error_num
), -1, NULL
, NULL
, NULL
);
847 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
848 strerror_currentlocale
, error_num
);
849 g_free(strerror_currentlocale
);
851 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
852 g_strerror (error_num
), error_num
);
858 skip_separators (const char *s
)
861 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
867 skip_numbers (const char *s
)
870 if (!isdigit ((unsigned char) *s
))
875 /* Remove all control sequences from the argument string. We define
876 * "control sequence", in a sort of pidgin BNF, as follows:
878 * control-seq = Esc non-'['
879 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
881 * This scheme works for all the terminals described in my termcap /
882 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
883 * terminals. If I hear from a single person who uses such a terminal
884 * with MC, I'll be glad to add support for it. (Dugan)
885 * Non-printable characters are also removed.
889 strip_ctrl_codes (char *s
)
891 char *w
; /* Current position where the stripped data is written */
892 char *r
; /* Current position where the original data is read */
897 for (w
= s
, r
= s
; *r
; ) {
898 if (*r
== ESC_CHAR
) {
899 /* Skip the control sequence's arguments */ ;
901 /* strchr() matches trailing binary 0 */
902 while (*(++r
) && strchr ("0123456789;?", *r
));
906 * Now we are at the last character of the sequence.
907 * Skip it unless it's binary 0.
914 if (is_printable(*r
))
925 get_current_wd (char *buffer
, int size
)
930 p
= g_get_current_dir ();
938 memcpy (buffer
, p
, len
);
943 #endif /* !USE_VFS */
945 enum compression_type
946 get_compression_type (int fd
)
948 unsigned char magic
[4];
950 /* Read the magic signature */
951 if (mc_read (fd
, (char *) magic
, 4) != 4)
952 return COMPRESSION_NONE
;
954 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
955 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)) {
956 return COMPRESSION_GZIP
;
960 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003
961 && magic
[3] == 004) {
962 /* Read compression type */
963 mc_lseek (fd
, 8, SEEK_SET
);
964 if (mc_read (fd
, (char *) magic
, 2) != 2)
965 return COMPRESSION_NONE
;
967 /* Gzip can handle only deflated (8) or stored (0) files */
968 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
969 return COMPRESSION_NONE
;
971 /* Compatible with gzip */
972 return COMPRESSION_GZIP
;
975 /* PACK_MAGIC and LZH_MAGIC and compress magic */
977 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)) {
978 /* Compatible with gzip */
979 return COMPRESSION_GZIP
;
982 /* BZIP and BZIP2 files */
983 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
984 (magic
[3] >= '1') && (magic
[3] <= '9')) {
987 return COMPRESSION_BZIP
;
989 return COMPRESSION_BZIP2
;
996 decompress_extension (int type
)
999 case COMPRESSION_GZIP
: return "#ugz";
1000 case COMPRESSION_BZIP
: return "#ubz";
1001 case COMPRESSION_BZIP2
: return "#ubz2";
1003 /* Should never reach this place */
1004 fprintf (stderr
, "Fatal: decompress_extension called with an unknown argument\n");
1010 add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
1012 Hook
*new_hook
= g_new (Hook
, 1);
1014 new_hook
->hook_fn
= hook_fn
;
1015 new_hook
->next
= *hook_list
;
1016 new_hook
->hook_data
= data
;
1018 *hook_list
= new_hook
;
1022 execute_hooks (Hook
*hook_list
)
1027 /* We copy the hook list first so tahat we let the hook
1028 * function call delete_hook
1032 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
1033 hook_list
= hook_list
->next
;
1038 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
1039 new_hook
= new_hook
->next
;
1042 for (hook_list
= p
; hook_list
;){
1044 hook_list
= hook_list
->next
;
1050 delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
1052 Hook
*current
, *new_list
, *next
;
1056 for (current
= *hook_list
; current
; current
= next
){
1057 next
= current
->next
;
1058 if (current
->hook_fn
== hook_fn
)
1061 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
1063 *hook_list
= new_list
;
1067 hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
1071 for (p
= hook_list
; p
; p
= p
->next
)
1072 if (p
->hook_fn
== hook_fn
)
1078 wipe_password (char *passwd
)
1089 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1090 /* Returns a newly allocated string */
1092 convert_controls (const char *p
)
1094 char *valcopy
= mhl_str_dup (p
);
1097 /* Parse the escape special character */
1098 for (q
= valcopy
; *p
;){
1101 if ((*p
== 'e') || (*p
== 'E')){
1111 char c
= (*p
| 0x20);
1112 if (c
>= 'a' && c
<= 'z') {
1127 resolve_symlinks (const char *path
)
1129 char *buf
, *buf2
, *q
, *r
, c
;
1134 if (*path
!= PATH_SEP
)
1136 r
= buf
= g_malloc (MC_MAXPATHLEN
);
1137 buf2
= g_malloc (MC_MAXPATHLEN
);
1142 q
= strchr (p
+ 1, PATH_SEP
);
1144 q
= strchr (p
+ 1, 0);
1150 if (mc_lstat (path
, &mybuf
) < 0) {
1156 if (!S_ISLNK (mybuf
.st_mode
))
1159 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
- 1);
1167 if (*buf2
== PATH_SEP
)
1172 canonicalize_pathname (buf
);
1173 r
= strchr (buf
, 0);
1174 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1184 strcpy (buf
, PATH_SEP_STR
);
1185 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1191 /* Finds out a relative path from first to second, i.e. goes as many ..
1192 * as needed up in first and then goes down using second */
1194 diff_two_paths (const char *first
, const char *second
)
1196 char *p
, *q
, *r
, *s
, *buf
= NULL
;
1197 int i
, j
, prevlen
= -1, currlen
;
1198 char *my_first
= NULL
, *my_second
= NULL
;
1200 my_first
= resolve_symlinks (first
);
1201 if (my_first
== NULL
)
1203 my_second
= resolve_symlinks (second
);
1204 if (my_second
== NULL
) {
1208 for (j
= 0; j
< 2; j
++) {
1212 r
= strchr (p
, PATH_SEP
);
1213 s
= strchr (q
, PATH_SEP
);
1217 if (strcmp (p
, q
)) {
1218 *r
= PATH_SEP
; *s
= PATH_SEP
;
1221 *r
= PATH_SEP
; *s
= PATH_SEP
;
1227 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1228 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1230 if (currlen
< prevlen
)
1238 p
= buf
= g_malloc (currlen
);
1240 for (; i
>= 0; i
--, p
+= 3)
1249 /* If filename is NULL, then we just append PATH_SEP to the dir */
1251 concat_dir_and_file (const char *dir
, const char *file
)
1253 int i
= strlen (dir
);
1255 if (dir
[i
-1] == PATH_SEP
)
1256 return g_strconcat (dir
, file
, (char *) NULL
);
1258 return g_strconcat (dir
, PATH_SEP_STR
, file
, (char *) NULL
);
1261 /* Append text to GList, remove all entries with the same text */
1263 list_append_unique (GList
*list
, char *text
)
1265 GList
*link
, *newlink
;
1268 * Go to the last position and traverse the list backwards
1269 * starting from the second last entry to make sure that we
1270 * are not removing the current link.
1272 list
= g_list_append (list
, text
);
1273 list
= g_list_last (list
);
1274 link
= g_list_previous (list
);
1277 newlink
= g_list_previous (link
);
1278 if (!strcmp ((char *) link
->data
, text
)) {
1279 g_free (link
->data
);
1280 g_list_remove_link (list
, link
);
1281 g_list_free_1 (link
);
1289 /* Following code heavily borrows from libiberty, mkstemps.c */
1291 /* Number of attempts to create a temporary file */
1293 #define TMP_MAX 16384
1294 #endif /* !TMP_MAX */
1298 * pname (output) - pointer to the name of the temp file (needs g_free).
1299 * NULL if the function fails.
1300 * prefix - part of the filename before the random part.
1301 * Prepend $TMPDIR or /tmp if there are no path separators.
1302 * suffix - if not NULL, part of the filename after the random part.
1305 * handle of the open file or -1 if couldn't open any.
1308 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
1310 static const char letters
[]
1311 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1312 static unsigned long value
;
1319 if (strchr (prefix
, PATH_SEP
) == NULL
) {
1320 /* Add prefix first to find the position of XXXXXX */
1321 tmpbase
= concat_dir_and_file (mc_tmpdir (), prefix
);
1323 tmpbase
= mhl_str_dup (prefix
);
1326 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
1328 XXXXXX
= &tmpname
[strlen (tmpbase
)];
1331 /* Get some more or less random data. */
1332 gettimeofday (&tv
, NULL
);
1333 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
1335 for (count
= 0; count
< TMP_MAX
; ++count
) {
1336 unsigned long v
= value
;
1339 /* Fill in the random bits. */
1340 XXXXXX
[0] = letters
[v
% 62];
1342 XXXXXX
[1] = letters
[v
% 62];
1344 XXXXXX
[2] = letters
[v
% 62];
1346 XXXXXX
[3] = letters
[v
% 62];
1348 XXXXXX
[4] = letters
[v
% 62];
1350 XXXXXX
[5] = letters
[v
% 62];
1352 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
,
1355 /* Successfully created. */
1359 /* This is a random value. It is only necessary that the next
1360 TMP_MAX values generated by adding 7777 to VALUE are different
1361 with (module 2^32). */
1365 /* Unsuccessful. Free the filename. */
1373 * Read and restore position for the given filename.
1374 * If there is no stored data, return line 1 and col 0.
1377 load_file_position (const char *filename
, long *line
, long *column
)
1381 char buf
[MC_MAXPATHLEN
+ 20];
1388 /* open file with positions */
1389 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1390 f
= fopen (fn
, "r");
1395 len
= strlen (filename
);
1397 while (fgets (buf
, sizeof (buf
), f
)) {
1400 /* check if the filename matches the beginning of string */
1401 if (strncmp (buf
, filename
, len
) != 0)
1404 /* followed by single space */
1405 if (buf
[len
] != ' ')
1408 /* and string without spaces */
1410 if (strchr (p
, ' '))
1413 *line
= strtol(p
, const_cast(char **, &p
), 10);
1415 *column
= strtol(p
+1, const_cast(char **, &p
), 10);
1424 /* Save position for the given file */
1426 save_file_position (const char *filename
, long line
, long column
)
1430 char buf
[MC_MAXPATHLEN
+ 20];
1434 len
= strlen (filename
);
1436 tmp
= concat_dir_and_file (home_dir
, MC_FILEPOS_TMP
);
1437 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1439 /* open temporary file */
1440 t
= fopen (tmp
, "w");
1447 /* put the new record */
1448 if (line
!= 1 || column
!= 0) {
1449 fprintf (t
, "%s %ld;%ld\n", filename
, line
, column
);
1452 /* copy records from the old file */
1453 f
= fopen (fn
, "r");
1455 while (fgets (buf
, sizeof (buf
), f
)) {
1456 /* Skip entries for the current filename */
1457 if (strncmp (buf
, filename
, len
) == 0 && buf
[len
] == ' '
1458 && !strchr (&buf
[len
+ 1], ' '))
1461 fprintf (t
, "%s", buf
);
1462 if (++i
> MC_FILEPOS_ENTRIES
)
1475 cstrcasestr (const char *haystack
, const char *needle
)
1478 size_t i
, needle_len
;
1480 needle_len
= strlen (needle
);
1481 for (hptr
= haystack
; *hptr
!= '\0'; hptr
++) {
1482 for (i
= 0; i
< needle_len
; i
++) {
1483 if (toupper ((unsigned char) hptr
[i
]) !=
1484 toupper ((unsigned char) needle
[i
]))
1495 cstrstr (const char *haystack
, const char *needle
)
1497 return strstr(haystack
, needle
);
1501 str_unconst (const char *s
)
1506 #define ASCII_A (0x40 + 1)
1507 #define ASCII_Z (0x40 + 26)
1508 #define ASCII_a (0x60 + 1)
1509 #define ASCII_z (0x60 + 26)
1512 ascii_alpha_to_cntrl (int ch
)
1514 if ((ch
>= ASCII_A
&& ch
<= ASCII_Z
)
1515 || (ch
>= ASCII_a
&& ch
<= ASCII_z
)) {
1524 const char *result
, *sep
;
1527 sep
= strchr(result
, '|');
1528 return (sep
!= NULL
) ? sep
+ 1 : result
;