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>
38 #include <mhl/string.h>
42 #include "main.h" /* mc_home */
43 #include "cmd.h" /* guess_message_value */
44 #include "mountlist.h"
45 #include "win.h" /* xterm_flag */
52 static const char app_text
[] = "Midnight-Commander";
53 int easy_patterns
= 1;
55 extern void str_replace(char *s
, char from
, char to
)
57 for (; *s
!= '\0'; s
++) {
64 is_7bit_printable (unsigned char c
)
66 return (c
> 31 && c
< 127);
70 is_iso_printable (unsigned char c
)
72 return ((c
> 31 && c
< 127) || c
>= 160);
76 is_8bit_printable (unsigned char c
)
78 /* "Full 8 bits output" doesn't work on xterm */
80 return is_iso_printable (c
);
82 return (c
> 31 && c
!= 127 && c
!= 155);
91 /* "Display bits" is ignored, since the user controls the output
92 by setting the output codepage */
93 return is_8bit_printable (c
);
96 return is_7bit_printable (c
);
98 if (full_eight_bits
) {
99 return is_8bit_printable (c
);
101 return is_iso_printable (c
);
102 #endif /* !HAVE_CHARSET */
105 /* Calculates the message dimensions (lines and columns) */
107 msglen (const char *text
, int *lines
, int *columns
)
109 int nlines
= 1; /* even the empty string takes one line */
113 for (; *text
!= '\0'; text
++) {
119 if (colindex
> ncolumns
)
129 * Copy from s to d, and trim the beginning if necessary, and prepend
130 * "..." in this case. The destination string can have at most len
131 * bytes, not counting trailing 0.
134 trim (const char *s
, char *d
, int len
)
141 source_len
= strlen (s
);
142 if (source_len
> len
) {
143 /* Cannot fit the whole line */
145 /* We only have room for the dots */
146 memset (d
, '.', len
);
150 /* Begin with ... and add the rest of the source string */
152 strcpy (d
+ 3, s
+ 3 + source_len
- len
);
155 /* We can copy the whole line */
161 * Quote the filename for the purpose of inserting it into the command
162 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
163 * processed by the mc command line.
166 name_quote (const char *s
, int quote_percent
)
170 d
= ret
= g_malloc (strlen (s
) * 2 + 2 + 1);
176 for (; *s
; s
++, d
++) {
220 fake_name_quote (const char *s
, int quote_percent
)
222 (void) quote_percent
;
227 * Remove the middle part of the string to fit given length.
228 * Use "~" to show where the string was truncated.
229 * Return static buffer, no need to free() it.
232 name_trunc (const char *txt
, int trunc_len
)
234 static char x
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
238 if ((size_t) trunc_len
> sizeof (x
) - 1) {
239 trunc_len
= sizeof (x
) - 1;
241 txt_len
= strlen (txt
);
242 if (txt_len
<= trunc_len
) {
245 int y
= (trunc_len
/ 2) + (trunc_len
% 2);
247 strncpy (x
+ y
, txt
+ txt_len
- (trunc_len
/ 2), trunc_len
/ 2);
252 if (!is_printable (*p
))
258 * path_trunc() is the same as name_trunc() above but
259 * it deletes possible password from path for security
263 path_trunc (const char *path
, int trunc_len
) {
265 char *secure_path
= strip_password (g_strdup (path
), 1);
267 ret
= name_trunc (secure_path
, trunc_len
);
268 g_free (secure_path
);
274 size_trunc (double size
)
276 static char x
[BUF_TINY
];
277 long int divisor
= 1;
278 const char *xtra
= "";
280 if (size
> 999999999L){
283 if (size
/divisor
> 999999999L){
288 g_snprintf (x
, sizeof (x
), "%.0f%s", (size
/divisor
), xtra
);
293 size_trunc_sep (double size
)
300 p
= y
= size_trunc (size
);
302 d
= x
+ sizeof (x
) - 1;
304 while (p
>= y
&& isalpha ((unsigned char) *p
))
306 for (count
= 0; p
>= y
; count
++){
320 * Print file SIZE to BUFFER, but don't exceed LEN characters,
321 * not including trailing 0. BUFFER should be at least LEN+1 long.
322 * This function is called for every file on panels, so avoid
323 * floating point by any means.
325 * Units: size units (filesystem sizes are 1K blocks)
326 * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
329 size_trunc_len (char *buffer
, int len
, off_t size
, int units
)
331 /* Avoid taking power for every file. */
332 static const off_t power10
[] =
333 {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
335 static const char * const suffix
[] =
336 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL
};
339 /* Don't print more than 9 digits - use suffix. */
340 if (len
== 0 || len
> 9)
343 for (j
= units
; suffix
[j
] != NULL
; j
++) {
346 /* Empty files will print "0" even with minimal width. */
347 g_snprintf (buffer
, len
+ 1, "0");
351 /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
352 g_snprintf (buffer
, len
+ 1, (len
> 1) ? "~%s" : "%s",
353 (j
> 1) ? suffix
[j
- 1] : "B");
357 if (size
< power10
[len
- (j
> 0)]) {
358 g_snprintf (buffer
, len
+ 1, "%lu%s", (unsigned long) size
, suffix
[j
]);
362 /* Powers of 1024, with rounding. */
363 size
= (size
+ 512) >> 10;
370 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
375 #define ismode(n,m) ((n & m) == m)
378 string_perm (mode_t mode_bits
)
380 static char mode
[11];
382 strcpy (mode
, "----------");
383 if (S_ISDIR (mode_bits
))
385 if (S_ISCHR (mode_bits
))
387 if (S_ISBLK (mode_bits
))
389 if (S_ISLNK (mode_bits
))
391 if (S_ISFIFO (mode_bits
))
393 if (S_ISNAM (mode_bits
))
395 if (S_ISSOCK (mode_bits
))
397 if (S_ISDOOR (mode_bits
))
399 if (ismode (mode_bits
, S_IXOTH
))
401 if (ismode (mode_bits
, S_IWOTH
))
403 if (ismode (mode_bits
, S_IROTH
))
405 if (ismode (mode_bits
, S_IXGRP
))
407 if (ismode (mode_bits
, S_IWGRP
))
409 if (ismode (mode_bits
, S_IRGRP
))
411 if (ismode (mode_bits
, S_IXUSR
))
413 if (ismode (mode_bits
, S_IWUSR
))
415 if (ismode (mode_bits
, S_IRUSR
))
418 if (ismode (mode_bits
, S_ISUID
))
419 mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
422 if (ismode (mode_bits
, S_ISGID
))
423 mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
426 if (ismode (mode_bits
, S_ISVTX
))
427 mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
432 /* p: string which might contain an url with a password (this parameter is
434 has_prefix = 0: The first parameter is an url without a prefix
435 (user[:pass]@]machine[:port][remote-dir). Delete
437 has_prefix = 1: Search p for known url prefixes. If found delete
438 the password from the url.
439 Caveat: only the first url is found
442 strip_password (char *p
, int has_prefix
)
444 static const struct {
447 } prefixes
[] = { {"/#ftp:", 6},
457 char *at
, *inner_colon
, *dir
;
461 for (i
= 0; i
< sizeof (prefixes
)/sizeof (prefixes
[0]); i
++) {
465 if((q
= strstr (p
, prefixes
[i
].name
)) == 0)
468 p
= q
+ prefixes
[i
].len
;
471 if ((dir
= strchr (p
, PATH_SEP
)) != NULL
)
474 /* search for any possible user */
475 at
= strrchr (p
, '@');
480 /* We have a username */
482 inner_colon
= memchr (p
, ':', at
- p
);
484 memmove (inner_colon
, at
, strlen(at
) + 1);
492 strip_home_and_password(const char *dir
)
495 static char newdir
[MC_MAXPATHLEN
];
497 if (home_dir
&& !strncmp (dir
, home_dir
, len
= strlen (home_dir
)) &&
498 (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0')){
500 g_strlcpy (&newdir
[1], &dir
[len
], sizeof(newdir
) - 1);
504 /* We do not strip homes in /#ftp tree, I do not like ~'s there
506 g_strlcpy (newdir
, dir
, sizeof(newdir
));
507 strip_password (newdir
, 1);
512 maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
525 maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
537 /* If shell patterns are on converts a shell pattern to a regular
538 expression. Called by regexp_match and mask_rename. */
539 /* Shouldn't we support [a-fw] type wildcards as well ?? */
541 convert_pattern (const char *pattern
, int match_type
, int do_group
)
545 int was_wildcard
= 0;
548 if ((match_type
!= match_regex
) && easy_patterns
){
549 new_pattern
= g_malloc (MC_MAXPATHLEN
);
551 if (match_type
== match_file
)
553 for (s
= pattern
; *s
; s
++, d
++){
556 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
562 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
567 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
573 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
578 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
579 if (match_type
== match_file
)
584 return g_strdup (pattern
);
588 regexp_match (const char *pattern
, const char *string
, int match_type
)
591 static char *old_pattern
= NULL
;
596 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
599 g_free (old_pattern
);
602 my_pattern
= convert_pattern (pattern
, match_type
, 0);
603 if (regcomp (&r
, my_pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
607 old_pattern
= my_pattern
;
608 old_type
= match_type
;
610 rval
= !regexec (&r
, string
, 0, NULL
, 0);
615 extension (const char *filename
)
617 const char *d
= strrchr (filename
, '.');
618 return (d
!= NULL
) ? d
+ 1 : "";
622 get_int (const char *file
, const char *key
, int def
)
624 return GetPrivateProfileInt (app_text
, key
, def
, file
);
628 set_int (const char *file
, const char *key
, int value
)
630 char buffer
[BUF_TINY
];
632 g_snprintf (buffer
, sizeof (buffer
), "%d", value
);
633 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
637 get_config_string (const char *file
, const char *key
, const char *defval
)
640 (void)GetPrivateProfileString (app_text
, key
, defval
, buffer
, sizeof(buffer
), file
);
641 return g_strdup (buffer
);
645 set_config_string (const char *file
, const char *key
, const char *val
)
647 (void)WritePrivateProfileString (app_text
, key
, val
, file
);
651 exist_file (const char *name
)
653 return access (name
, R_OK
) == 0;
657 load_file (const char *filename
)
664 if ((data_file
= fopen (filename
, "r")) == NULL
){
667 if (fstat (fileno (data_file
), &s
) != 0){
671 data
= g_malloc (s
.st_size
+1);
672 read_size
= fread (data
, 1, s
.st_size
, data_file
);
673 data
[read_size
] = 0;
685 load_mc_home_file (const char *filename
, char **allocated_filename
)
687 char *hintfile_base
, *hintfile
;
691 hintfile_base
= mhl_str_dir_plus_file (mc_home
, filename
);
692 lang
= guess_message_value ();
694 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
695 data
= load_file (hintfile
);
699 /* Fall back to the two-letter language code */
700 if (lang
[0] && lang
[1])
702 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
703 data
= load_file (hintfile
);
707 hintfile
= hintfile_base
;
708 data
= load_file (hintfile_base
);
714 if (hintfile
!= hintfile_base
)
715 g_free (hintfile_base
);
717 if (allocated_filename
)
718 *allocated_filename
= hintfile
;
725 /* Check strftime() results. Some systems (i.e. Solaris) have different
726 short-month-name sizes for different locales */
728 i18n_checktimelength (void)
730 time_t testtime
= time (NULL
);
731 struct tm
* lt
= localtime(&testtime
);
735 // huh, localtime() doesnt seem to work ... falling back to "(invalid)"
736 length
= strlen(INVALID_TIME_TEXT
);
738 char buf
[MAX_I18NTIMELENGTH
+ 1];
740 a
= strftime (buf
, sizeof(buf
)-1, _("%b %e %H:%M"), lt
);
741 b
= strftime (buf
, sizeof(buf
)-1, _("%b %e %Y"), lt
);
745 /* Don't handle big differences. Use standard value (email bug, please) */
746 if ( length
> MAX_I18NTIMELENGTH
|| length
< MIN_I18NTIMELENGTH
)
747 length
= STD_I18NTIMELENGTH
;
753 file_date (time_t when
)
755 static char timebuf
[MAX_I18NTIMELENGTH
+ 1];
756 time_t current_time
= time ((time_t) 0);
757 static size_t i18n_timelength
= 0;
758 static const char *fmtyear
, *fmttime
;
761 if (i18n_timelength
== 0){
762 i18n_timelength
= i18n_checktimelength() + 1;
764 /* strftime() format string for old dates */
765 fmtyear
= _("%b %e %Y");
766 /* strftime() format string for recent dates */
767 fmttime
= _("%b %e %H:%M");
770 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
771 || current_time
< when
- 60L * 60L) /* In the future. */
772 /* The file is fairly old or in the future.
773 POSIX says the cutoff is 6 months old;
774 approximate this by 6*30 days.
775 Allow a 1 hour slop factor for what is considered "the future",
776 to allow for NFS server/client clock disagreement.
777 Show the year instead of the time of day. */
783 FMT_LOCALTIME(timebuf
, i18n_timelength
, fmt
, when
);
789 extract_line (const char *s
, const char *top
)
791 static char tmp_line
[BUF_MEDIUM
];
794 while (*s
&& *s
!= '\n' && (size_t) (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
800 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
802 _icase_search (const char *text
, const char *data
, int *lng
)
804 const char *d
= text
;
805 const char *e
= data
;
811 while (*(e
+1) == '\b' && *(e
+2)) {
815 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
824 *lng
= strlen (text
) + dlng
;
831 /* The basename routine */
833 x_basename (const char *s
)
836 return ((where
= strrchr (s
, PATH_SEP
))) ? where
+ 1 : s
;
841 unix_error_string (int error_num
)
843 static char buffer
[BUF_LARGE
];
844 #if GLIB_MAJOR_VERSION >= 2
845 gchar
*strerror_currentlocale
;
847 strerror_currentlocale
= g_locale_from_utf8(g_strerror (error_num
), -1, NULL
, NULL
, NULL
);
848 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
849 strerror_currentlocale
, error_num
);
850 g_free(strerror_currentlocale
);
852 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
853 g_strerror (error_num
), error_num
);
859 skip_separators (const char *s
)
862 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
868 skip_numbers (const char *s
)
871 if (!isdigit ((unsigned char) *s
))
876 /* Remove all control sequences from the argument string. We define
877 * "control sequence", in a sort of pidgin BNF, as follows:
879 * control-seq = Esc non-'['
880 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
882 * This scheme works for all the terminals described in my termcap /
883 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
884 * terminals. If I hear from a single person who uses such a terminal
885 * with MC, I'll be glad to add support for it. (Dugan)
886 * Non-printable characters are also removed.
890 strip_ctrl_codes (char *s
)
892 char *w
; /* Current position where the stripped data is written */
893 char *r
; /* Current position where the original data is read */
898 for (w
= s
, r
= s
; *r
; ) {
899 if (*r
== ESC_CHAR
) {
900 /* Skip the control sequence's arguments */ ;
902 /* strchr() matches trailing binary 0 */
903 while (*(++r
) && strchr ("0123456789;?", *r
));
907 * Now we are at the last character of the sequence.
908 * Skip it unless it's binary 0.
915 if (is_printable(*r
))
926 get_current_wd (char *buffer
, int size
)
931 p
= g_get_current_dir ();
939 memcpy (buffer
, p
, len
);
944 #endif /* !USE_VFS */
946 enum compression_type
947 get_compression_type (int fd
)
949 unsigned char magic
[4];
951 /* Read the magic signature */
952 if (mc_read (fd
, (char *) magic
, 4) != 4)
953 return COMPRESSION_NONE
;
955 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
956 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)) {
957 return COMPRESSION_GZIP
;
961 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003
962 && magic
[3] == 004) {
963 /* Read compression type */
964 mc_lseek (fd
, 8, SEEK_SET
);
965 if (mc_read (fd
, (char *) magic
, 2) != 2)
966 return COMPRESSION_NONE
;
968 /* Gzip can handle only deflated (8) or stored (0) files */
969 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
970 return COMPRESSION_NONE
;
972 /* Compatible with gzip */
973 return COMPRESSION_GZIP
;
976 /* PACK_MAGIC and LZH_MAGIC and compress magic */
978 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)) {
979 /* Compatible with gzip */
980 return COMPRESSION_GZIP
;
983 /* BZIP and BZIP2 files */
984 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
985 (magic
[3] >= '1') && (magic
[3] <= '9')) {
988 return COMPRESSION_BZIP
;
990 return COMPRESSION_BZIP2
;
997 decompress_extension (int type
)
1000 case COMPRESSION_GZIP
: return "#ugz";
1001 case COMPRESSION_BZIP
: return "#ubz";
1002 case COMPRESSION_BZIP2
: return "#ubz2";
1004 /* Should never reach this place */
1005 fprintf (stderr
, "Fatal: decompress_extension called with an unknown argument\n");
1011 add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
1013 Hook
*new_hook
= g_new (Hook
, 1);
1015 new_hook
->hook_fn
= hook_fn
;
1016 new_hook
->next
= *hook_list
;
1017 new_hook
->hook_data
= data
;
1019 *hook_list
= new_hook
;
1023 execute_hooks (Hook
*hook_list
)
1028 /* We copy the hook list first so tahat we let the hook
1029 * function call delete_hook
1033 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
1034 hook_list
= hook_list
->next
;
1039 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
1040 new_hook
= new_hook
->next
;
1043 for (hook_list
= p
; hook_list
;){
1045 hook_list
= hook_list
->next
;
1051 delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
1053 Hook
*current
, *new_list
, *next
;
1057 for (current
= *hook_list
; current
; current
= next
){
1058 next
= current
->next
;
1059 if (current
->hook_fn
== hook_fn
)
1062 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
1064 *hook_list
= new_list
;
1068 hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
1072 for (p
= hook_list
; p
; p
= p
->next
)
1073 if (p
->hook_fn
== hook_fn
)
1079 wipe_password (char *passwd
)
1090 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1091 /* Returns a newly allocated string */
1093 convert_controls (const char *p
)
1095 char *valcopy
= g_strdup (p
);
1098 /* Parse the escape special character */
1099 for (q
= valcopy
; *p
;){
1102 if ((*p
== 'e') || (*p
== 'E')){
1112 char c
= (*p
| 0x20);
1113 if (c
>= 'a' && c
<= 'z') {
1128 resolve_symlinks (const char *path
)
1130 char *buf
, *buf2
, *q
, *r
, c
;
1135 if (*path
!= PATH_SEP
)
1137 r
= buf
= g_malloc (MC_MAXPATHLEN
);
1138 buf2
= g_malloc (MC_MAXPATHLEN
);
1143 q
= strchr (p
+ 1, PATH_SEP
);
1145 q
= strchr (p
+ 1, 0);
1151 if (mc_lstat (path
, &mybuf
) < 0) {
1157 if (!S_ISLNK (mybuf
.st_mode
))
1160 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
- 1);
1168 if (*buf2
== PATH_SEP
)
1173 canonicalize_pathname (buf
);
1174 r
= strchr (buf
, 0);
1175 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1185 strcpy (buf
, PATH_SEP_STR
);
1186 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1192 /* Finds out a relative path from first to second, i.e. goes as many ..
1193 * as needed up in first and then goes down using second */
1195 diff_two_paths (const char *first
, const char *second
)
1197 char *p
, *q
, *r
, *s
, *buf
= NULL
;
1198 int i
, j
, prevlen
= -1, currlen
;
1199 char *my_first
= NULL
, *my_second
= NULL
;
1201 my_first
= resolve_symlinks (first
);
1202 if (my_first
== NULL
)
1204 my_second
= resolve_symlinks (second
);
1205 if (my_second
== NULL
) {
1209 for (j
= 0; j
< 2; j
++) {
1213 r
= strchr (p
, PATH_SEP
);
1214 s
= strchr (q
, PATH_SEP
);
1218 if (strcmp (p
, q
)) {
1219 *r
= PATH_SEP
; *s
= PATH_SEP
;
1222 *r
= PATH_SEP
; *s
= PATH_SEP
;
1228 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1229 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1231 if (currlen
< prevlen
)
1239 p
= buf
= g_malloc (currlen
);
1241 for (; i
>= 0; i
--, p
+= 3)
1250 /* Append text to GList, remove all entries with the same text */
1252 list_append_unique (GList
*list
, char *text
)
1254 GList
*link
, *newlink
;
1257 * Go to the last position and traverse the list backwards
1258 * starting from the second last entry to make sure that we
1259 * are not removing the current link.
1261 list
= g_list_append (list
, text
);
1262 list
= g_list_last (list
);
1263 link
= g_list_previous (list
);
1266 newlink
= g_list_previous (link
);
1267 if (!strcmp ((char *) link
->data
, text
)) {
1268 g_free (link
->data
);
1269 g_list_remove_link (list
, link
);
1270 g_list_free_1 (link
);
1278 /* Following code heavily borrows from libiberty, mkstemps.c */
1280 /* Number of attempts to create a temporary file */
1282 #define TMP_MAX 16384
1283 #endif /* !TMP_MAX */
1287 * pname (output) - pointer to the name of the temp file (needs g_free).
1288 * NULL if the function fails.
1289 * prefix - part of the filename before the random part.
1290 * Prepend $TMPDIR or /tmp if there are no path separators.
1291 * suffix - if not NULL, part of the filename after the random part.
1294 * handle of the open file or -1 if couldn't open any.
1297 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
1299 static const char letters
[]
1300 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1301 static unsigned long value
;
1308 if (strchr (prefix
, PATH_SEP
) == NULL
) {
1309 /* Add prefix first to find the position of XXXXXX */
1310 tmpbase
= mhl_str_dir_plus_file (mc_tmpdir (), prefix
);
1312 tmpbase
= g_strdup (prefix
);
1315 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
1317 XXXXXX
= &tmpname
[strlen (tmpbase
)];
1320 /* Get some more or less random data. */
1321 gettimeofday (&tv
, NULL
);
1322 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
1324 for (count
= 0; count
< TMP_MAX
; ++count
) {
1325 unsigned long v
= value
;
1328 /* Fill in the random bits. */
1329 XXXXXX
[0] = letters
[v
% 62];
1331 XXXXXX
[1] = letters
[v
% 62];
1333 XXXXXX
[2] = letters
[v
% 62];
1335 XXXXXX
[3] = letters
[v
% 62];
1337 XXXXXX
[4] = letters
[v
% 62];
1339 XXXXXX
[5] = letters
[v
% 62];
1341 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
,
1344 /* Successfully created. */
1348 /* This is a random value. It is only necessary that the next
1349 TMP_MAX values generated by adding 7777 to VALUE are different
1350 with (module 2^32). */
1354 /* Unsuccessful. Free the filename. */
1362 * Read and restore position for the given filename.
1363 * If there is no stored data, return line 1 and col 0.
1366 load_file_position (const char *filename
, long *line
, long *column
)
1370 char buf
[MC_MAXPATHLEN
+ 20];
1377 /* open file with positions */
1378 fn
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS
);
1379 f
= fopen (fn
, "r");
1384 len
= strlen (filename
);
1386 while (fgets (buf
, sizeof (buf
), f
)) {
1389 /* check if the filename matches the beginning of string */
1390 if (strncmp (buf
, filename
, len
) != 0)
1393 /* followed by single space */
1394 if (buf
[len
] != ' ')
1397 /* and string without spaces */
1399 if (strchr (p
, ' '))
1402 *line
= strtol(p
, const_cast(char **, &p
), 10);
1404 *column
= strtol(p
+1, const_cast(char **, &p
), 10);
1413 /* Save position for the given file */
1415 save_file_position (const char *filename
, long line
, long column
)
1419 char buf
[MC_MAXPATHLEN
+ 20];
1423 len
= strlen (filename
);
1425 tmp
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS_TMP
);
1426 fn
= mhl_str_dir_plus_file (home_dir
, MC_FILEPOS
);
1428 /* open temporary file */
1429 t
= fopen (tmp
, "w");
1436 /* put the new record */
1437 if (line
!= 1 || column
!= 0) {
1438 fprintf (t
, "%s %ld;%ld\n", filename
, line
, column
);
1441 /* copy records from the old file */
1442 f
= fopen (fn
, "r");
1444 while (fgets (buf
, sizeof (buf
), f
)) {
1445 /* Skip entries for the current filename */
1446 if (strncmp (buf
, filename
, len
) == 0 && buf
[len
] == ' '
1447 && !strchr (&buf
[len
+ 1], ' '))
1450 fprintf (t
, "%s", buf
);
1451 if (++i
> MC_FILEPOS_ENTRIES
)
1464 cstrcasestr (const char *haystack
, const char *needle
)
1467 size_t i
, needle_len
;
1469 needle_len
= strlen (needle
);
1470 for (hptr
= haystack
; *hptr
!= '\0'; hptr
++) {
1471 for (i
= 0; i
< needle_len
; i
++) {
1472 if (toupper ((unsigned char) hptr
[i
]) !=
1473 toupper ((unsigned char) needle
[i
]))
1484 cstrstr (const char *haystack
, const char *needle
)
1486 return strstr(haystack
, needle
);
1490 str_unconst (const char *s
)
1495 #define ASCII_A (0x40 + 1)
1496 #define ASCII_Z (0x40 + 26)
1497 #define ASCII_a (0x60 + 1)
1498 #define ASCII_z (0x60 + 26)
1501 ascii_alpha_to_cntrl (int ch
)
1503 if ((ch
>= ASCII_A
&& ch
<= ASCII_Z
)
1504 || (ch
>= ASCII_a
&& ch
<= ASCII_z
)) {
1513 const char *result
, *sep
;
1516 sep
= strchr(result
, '|');
1517 return (sep
!= NULL
) ? sep
+ 1 : result
;