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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
33 #include <sys/types.h>
39 #include "main.h" /* mc_home */
40 #include "cmd.h" /* guess_message_value */
41 #include "mountlist.h"
42 #include "win.h" /* xterm_flag */
48 static const char app_text
[] = "Midnight-Commander";
49 int easy_patterns
= 1;
51 extern void str_replace(char *s
, char from
, char to
)
53 for (; *s
!= '\0'; s
++) {
60 is_7bit_printable (unsigned char c
)
62 return (c
> 31 && c
< 127);
66 is_iso_printable (unsigned char c
)
68 return ((c
> 31 && c
< 127) || c
>= 160);
72 is_8bit_printable (unsigned char c
)
74 /* "Full 8 bits output" doesn't work on xterm */
76 return is_iso_printable (c
);
78 return (c
> 31 && c
!= 127 && c
!= 155);
87 /* "Display bits" is ignored, since the user controls the output
88 by setting the output codepage */
89 return is_8bit_printable (c
);
92 return is_7bit_printable (c
);
94 if (full_eight_bits
) {
95 return is_8bit_printable (c
);
97 return is_iso_printable (c
);
98 #endif /* !HAVE_CHARSET */
101 /* Calculates the message dimensions (lines and columns) */
102 void msglen (const char *text
, int *lines
, int *columns
)
104 int nlines
= 1; /* even the empty string takes one line */
108 for (; *text
!= '\0'; text
++) {
114 if (colindex
> ncolumns
)
124 * Copy from s to d, and trim the beginning if necessary, and prepend
125 * "..." in this case. The destination string can have at most len
126 * bytes, not counting trailing 0.
129 trim (const char *s
, char *d
, int len
)
136 source_len
= strlen (s
);
137 if (source_len
> len
) {
138 /* Cannot fit the whole line */
140 /* We only have room for the dots */
141 memset (d
, '.', len
);
145 /* Begin with ... and add the rest of the source string */
147 strcpy (d
+ 3, s
+ 3 + source_len
- len
);
150 /* We can copy the whole line */
156 * Quote the filename for the purpose of inserting it into the command
157 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
158 * processed by the mc command line.
161 name_quote (const char *s
, int quote_percent
)
165 d
= ret
= g_malloc (strlen (s
) * 2 + 2 + 1);
171 for (; *s
; s
++, d
++) {
215 fake_name_quote (const char *s
, int quote_percent
)
217 (void) quote_percent
;
222 * Remove the middle part of the string to fit given length.
223 * Use "~" to show where the string was truncated.
224 * Return static buffer, no need to free() it.
227 name_trunc (const char *txt
, int trunc_len
)
229 static char x
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
233 if ((size_t) trunc_len
> sizeof (x
) - 1) {
234 trunc_len
= sizeof (x
) - 1;
236 txt_len
= strlen (txt
);
237 if (txt_len
<= trunc_len
) {
240 int y
= (trunc_len
/ 2) + (trunc_len
% 2);
242 strncpy (x
+ y
, txt
+ txt_len
- (trunc_len
/ 2), trunc_len
/ 2);
247 if (!is_printable (*p
))
253 * path_trunc() is the same as name_trunc() above but
254 * it deletes possible password from path for security
258 path_trunc (const char *path
, int trunc_len
) {
260 char *secure_path
= strip_password (g_strdup (path
), 1);
262 ret
= name_trunc (secure_path
, trunc_len
);
263 g_free (secure_path
);
268 const char *size_trunc (double size
)
270 static char x
[BUF_TINY
];
271 long int divisor
= 1;
272 const char *xtra
= "";
274 if (size
> 999999999L){
277 if (size
/divisor
> 999999999L){
282 g_snprintf (x
, sizeof (x
), "%.0f%s", (size
/divisor
), xtra
);
286 const char *size_trunc_sep (double size
)
293 p
= y
= size_trunc (size
);
295 d
= x
+ sizeof (x
) - 1;
297 while (p
>= y
&& isalpha ((unsigned char) *p
))
299 for (count
= 0; p
>= y
; count
++){
313 * Print file SIZE to BUFFER, but don't exceed LEN characters,
314 * not including trailing 0. BUFFER should be at least LEN+1 long.
315 * This function is called for every file on panels, so avoid
316 * floating point by any means.
318 * Units: size units (filesystem sizes are 1K blocks)
319 * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
322 size_trunc_len (char *buffer
, int len
, off_t size
, int units
)
324 /* Avoid taking power for every file. */
325 static const off_t power10
[] =
326 {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
328 static const char * const suffix
[] =
329 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL
};
332 /* Don't print more than 9 digits - use suffix. */
333 if (len
== 0 || len
> 9)
336 for (j
= units
; suffix
[j
] != NULL
; j
++) {
339 /* Empty files will print "0" even with minimal width. */
340 g_snprintf (buffer
, len
+ 1, "0");
344 /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
345 g_snprintf (buffer
, len
+ 1, (len
> 1) ? "~%s" : "%s",
346 (j
> 1) ? suffix
[j
- 1] : "B");
350 if (size
< power10
[len
- (j
> 0)]) {
351 g_snprintf (buffer
, len
+ 1, "%lu%s", (unsigned long) size
, suffix
[j
]);
355 /* Powers of 1024, with rounding. */
356 size
= (size
+ 512) >> 10;
360 int is_exe (mode_t mode
)
362 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
367 #define ismode(n,m) ((n & m) == m)
370 string_perm (mode_t mode_bits
)
372 static char mode
[11];
374 strcpy (mode
, "----------");
375 if (S_ISDIR (mode_bits
))
377 if (S_ISCHR (mode_bits
))
379 if (S_ISBLK (mode_bits
))
381 if (S_ISLNK (mode_bits
))
383 if (S_ISFIFO (mode_bits
))
385 if (S_ISNAM (mode_bits
))
387 if (S_ISSOCK (mode_bits
))
389 if (S_ISDOOR (mode_bits
))
391 if (ismode (mode_bits
, S_IXOTH
))
393 if (ismode (mode_bits
, S_IWOTH
))
395 if (ismode (mode_bits
, S_IROTH
))
397 if (ismode (mode_bits
, S_IXGRP
))
399 if (ismode (mode_bits
, S_IWGRP
))
401 if (ismode (mode_bits
, S_IRGRP
))
403 if (ismode (mode_bits
, S_IXUSR
))
405 if (ismode (mode_bits
, S_IWUSR
))
407 if (ismode (mode_bits
, S_IRUSR
))
410 if (ismode (mode_bits
, S_ISUID
))
411 mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
414 if (ismode (mode_bits
, S_ISGID
))
415 mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
418 if (ismode (mode_bits
, S_ISVTX
))
419 mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
424 /* p: string which might contain an url with a password (this parameter is
426 has_prefix = 0: The first parameter is an url without a prefix
427 (user[:pass]@]machine[:port][remote-dir). Delete
429 has_prefix = 1: Search p for known url prefixes. If found delete
430 the password from the url.
431 Caveat: only the first url is found
434 strip_password (char *p
, int has_prefix
)
436 static const struct {
439 } prefixes
[] = { {"/#ftp:", 6},
449 char *at
, *inner_colon
, *dir
;
453 for (i
= 0; i
< sizeof (prefixes
)/sizeof (prefixes
[0]); i
++) {
457 if((q
= strstr (p
, prefixes
[i
].name
)) == 0)
460 p
= q
+ prefixes
[i
].len
;
463 if ((dir
= strchr (p
, PATH_SEP
)) != NULL
)
466 /* search for any possible user */
467 at
= strrchr (p
, '@');
472 /* We have a username */
474 inner_colon
= memchr (p
, ':', at
- p
);
476 memmove (inner_colon
, at
, strlen(at
) + 1);
483 const char *strip_home_and_password(const char *dir
)
486 static char newdir
[MC_MAXPATHLEN
];
488 if (home_dir
&& !strncmp (dir
, home_dir
, len
= strlen (home_dir
)) &&
489 (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0')){
491 g_strlcpy (&newdir
[1], &dir
[len
], sizeof(newdir
) - 1);
495 /* We do not strip homes in /#ftp tree, I do not like ~'s there
497 g_strlcpy (newdir
, dir
, sizeof(newdir
));
498 strip_password (newdir
, 1);
502 static char *maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
514 static char *maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
526 /* If shell patterns are on converts a shell pattern to a regular
527 expression. Called by regexp_match and mask_rename. */
528 /* Shouldn't we support [a-fw] type wildcards as well ?? */
529 char *convert_pattern (const char *pattern
, int match_type
, int do_group
)
533 int was_wildcard
= 0;
536 if ((match_type
!= match_regex
) && easy_patterns
){
537 new_pattern
= g_malloc (MC_MAXPATHLEN
);
539 if (match_type
== match_file
)
541 for (s
= pattern
; *s
; s
++, d
++){
544 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
550 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
555 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
561 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
566 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
567 if (match_type
== match_file
)
572 return g_strdup (pattern
);
575 int regexp_match (const char *pattern
, const char *string
, int match_type
)
578 static char *old_pattern
= NULL
;
583 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
586 g_free (old_pattern
);
589 my_pattern
= convert_pattern (pattern
, match_type
, 0);
590 if (regcomp (&r
, my_pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
594 old_pattern
= my_pattern
;
595 old_type
= match_type
;
597 rval
= !regexec (&r
, string
, 0, NULL
, 0);
601 const char *extension (const char *filename
)
603 const char *d
= strrchr (filename
, '.');
604 return (d
!= NULL
) ? d
+ 1 : "";
607 int get_int (const char *file
, const char *key
, int def
)
609 return GetPrivateProfileInt (app_text
, key
, def
, file
);
612 int set_int (const char *file
, const char *key
, int value
)
614 char buffer
[BUF_TINY
];
616 g_snprintf (buffer
, sizeof (buffer
), "%d", value
);
617 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
621 get_config_string (const char *file
, const char *key
, const char *defval
)
624 (void)GetPrivateProfileString (app_text
, key
, defval
, buffer
, sizeof(buffer
), file
);
625 return g_strdup (buffer
);
629 set_config_string (const char *file
, const char *key
, const char *val
)
631 (void)WritePrivateProfileString (app_text
, key
, val
, file
);
634 int exist_file (const char *name
)
636 return access (name
, R_OK
) == 0;
639 char *load_file (const char *filename
)
646 if ((data_file
= fopen (filename
, "r")) == NULL
){
649 if (fstat (fileno (data_file
), &s
) != 0){
653 data
= g_malloc (s
.st_size
+1);
654 read_size
= fread (data
, 1, s
.st_size
, data_file
);
655 data
[read_size
] = 0;
667 load_mc_home_file (const char *filename
, char **allocated_filename
)
669 char *hintfile_base
, *hintfile
;
673 hintfile_base
= concat_dir_and_file (mc_home
, filename
);
674 lang
= guess_message_value ();
676 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
677 data
= load_file (hintfile
);
681 /* Fall back to the two-letter language code */
682 if (lang
[0] && lang
[1])
684 hintfile
= g_strconcat (hintfile_base
, ".", lang
, (char *) NULL
);
685 data
= load_file (hintfile
);
689 hintfile
= hintfile_base
;
690 data
= load_file (hintfile_base
);
696 if (hintfile
!= hintfile_base
)
697 g_free (hintfile_base
);
699 if (allocated_filename
)
700 *allocated_filename
= hintfile
;
707 /* Check strftime() results. Some systems (i.e. Solaris) have different
708 short-month-name sizes for different locales */
709 size_t i18n_checktimelength (void)
712 char buf
[MAX_I18NTIMELENGTH
+ 1];
713 time_t testtime
= time (NULL
);
715 a
= strftime (buf
, sizeof(buf
)-1, _("%b %e %H:%M"), localtime(&testtime
));
716 b
= strftime (buf
, sizeof(buf
)-1, _("%b %e %Y"), localtime(&testtime
));
720 /* Don't handle big differences. Use standard value (email bug, please) */
721 if ( length
> MAX_I18NTIMELENGTH
|| length
< MIN_I18NTIMELENGTH
)
722 length
= STD_I18NTIMELENGTH
;
727 const char *file_date (time_t when
)
729 static char timebuf
[MAX_I18NTIMELENGTH
+ 1];
730 time_t current_time
= time ((time_t) 0);
731 static size_t i18n_timelength
= 0;
732 static const char *fmtyear
, *fmttime
;
735 if (i18n_timelength
== 0){
736 i18n_timelength
= i18n_checktimelength() + 1;
738 /* strftime() format string for old dates */
739 fmtyear
= _("%b %e %Y");
740 /* strftime() format string for recent dates */
741 fmttime
= _("%b %e %H:%M");
744 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
745 || current_time
< when
- 60L * 60L) /* In the future. */
746 /* The file is fairly old or in the future.
747 POSIX says the cutoff is 6 months old;
748 approximate this by 6*30 days.
749 Allow a 1 hour slop factor for what is considered "the future",
750 to allow for NFS server/client clock disagreement.
751 Show the year instead of the time of day. */
757 strftime (timebuf
, i18n_timelength
, fmt
, localtime(&when
));
761 const char *extract_line (const char *s
, const char *top
)
763 static char tmp_line
[BUF_MEDIUM
];
766 while (*s
&& *s
!= '\n' && (size_t) (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
772 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
773 const char * _icase_search (const char *text
, const char *data
, int *lng
)
775 const char *d
= text
;
776 const char *e
= data
;
782 while (*(e
+1) == '\b' && *(e
+2)) {
786 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
795 *lng
= strlen (text
) + dlng
;
802 /* The basename routine */
803 const char *x_basename (const char *s
)
806 return ((where
= strrchr (s
, PATH_SEP
))) ? where
+ 1 : s
;
810 const char *unix_error_string (int error_num
)
812 static char buffer
[BUF_LARGE
];
813 #if GLIB_MAJOR_VERSION >= 2
814 gchar
*strerror_currentlocale
;
816 strerror_currentlocale
= g_locale_from_utf8(g_strerror (error_num
), -1, NULL
, NULL
, NULL
);
817 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
818 strerror_currentlocale
, error_num
);
819 g_free(strerror_currentlocale
);
821 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
822 g_strerror (error_num
), error_num
);
827 const char *skip_separators (const char *s
)
830 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
835 const char *skip_numbers (const char *s
)
838 if (!isdigit ((unsigned char) *s
))
843 /* Remove all control sequences from the argument string. We define
844 * "control sequence", in a sort of pidgin BNF, as follows:
846 * control-seq = Esc non-'['
847 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
849 * This scheme works for all the terminals described in my termcap /
850 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
851 * terminals. If I hear from a single person who uses such a terminal
852 * with MC, I'll be glad to add support for it. (Dugan)
853 * Non-printable characters are also removed.
856 char *strip_ctrl_codes (char *s
)
858 char *w
; /* Current position where the stripped data is written */
859 char *r
; /* Current position where the original data is read */
864 for (w
= s
, r
= s
; *r
; ) {
865 if (*r
== ESC_CHAR
) {
866 /* Skip the control sequence's arguments */ ;
868 /* strchr() matches trailing binary 0 */
869 while (*(++r
) && strchr ("0123456789;?", *r
));
873 * Now we are at the last character of the sequence.
874 * Skip it unless it's binary 0.
881 if (is_printable(*r
))
891 char *get_current_wd (char *buffer
, int size
)
896 p
= g_get_current_dir ();
904 memcpy (buffer
, p
, len
);
909 #endif /* !USE_VFS */
911 enum compression_type
912 get_compression_type (int fd
)
914 unsigned char magic
[4];
916 /* Read the magic signature */
917 if (mc_read (fd
, (char *) magic
, 4) != 4)
918 return COMPRESSION_NONE
;
920 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
921 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)) {
922 return COMPRESSION_GZIP
;
926 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003
927 && magic
[3] == 004) {
928 /* Read compression type */
929 mc_lseek (fd
, 8, SEEK_SET
);
930 if (mc_read (fd
, (char *) magic
, 2) != 2)
931 return COMPRESSION_NONE
;
933 /* Gzip can handle only deflated (8) or stored (0) files */
934 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
935 return COMPRESSION_NONE
;
937 /* Compatible with gzip */
938 return COMPRESSION_GZIP
;
941 /* PACK_MAGIC and LZH_MAGIC and compress magic */
943 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)) {
944 /* Compatible with gzip */
945 return COMPRESSION_GZIP
;
948 /* BZIP and BZIP2 files */
949 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
950 (magic
[3] >= '1') && (magic
[3] <= '9')) {
953 return COMPRESSION_BZIP
;
955 return COMPRESSION_BZIP2
;
962 decompress_extension (int type
)
965 case COMPRESSION_GZIP
: return "#ugz";
966 case COMPRESSION_BZIP
: return "#ubz";
967 case COMPRESSION_BZIP2
: return "#ubz2";
969 /* Should never reach this place */
970 fprintf (stderr
, "Fatal: decompress_extension called with an unknown argument\n");
975 void add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
977 Hook
*new_hook
= g_new (Hook
, 1);
979 new_hook
->hook_fn
= hook_fn
;
980 new_hook
->next
= *hook_list
;
981 new_hook
->hook_data
= data
;
983 *hook_list
= new_hook
;
986 void execute_hooks (Hook
*hook_list
)
991 /* We copy the hook list first so tahat we let the hook
992 * function call delete_hook
996 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
997 hook_list
= hook_list
->next
;
1002 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
1003 new_hook
= new_hook
->next
;
1006 for (hook_list
= p
; hook_list
;){
1008 hook_list
= hook_list
->next
;
1013 void delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
1015 Hook
*current
, *new_list
, *next
;
1019 for (current
= *hook_list
; current
; current
= next
){
1020 next
= current
->next
;
1021 if (current
->hook_fn
== hook_fn
)
1024 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
1026 *hook_list
= new_list
;
1029 int hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
1033 for (p
= hook_list
; p
; p
= p
->next
)
1034 if (p
->hook_fn
== hook_fn
)
1039 void wipe_password (char *passwd
)
1050 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1051 /* Returns a newly allocated string */
1052 char *convert_controls (const char *p
)
1054 char *valcopy
= g_strdup (p
);
1057 /* Parse the escape special character */
1058 for (q
= valcopy
; *p
;){
1061 if ((*p
== 'e') || (*p
== 'E')){
1071 char c
= (*p
| 0x20);
1072 if (c
>= 'a' && c
<= 'z') {
1086 static char *resolve_symlinks (const char *path
)
1088 char *buf
, *buf2
, *q
, *r
, c
;
1093 if (*path
!= PATH_SEP
)
1095 r
= buf
= g_malloc (MC_MAXPATHLEN
);
1096 buf2
= g_malloc (MC_MAXPATHLEN
);
1101 q
= strchr (p
+ 1, PATH_SEP
);
1103 q
= strchr (p
+ 1, 0);
1109 if (mc_lstat (path
, &mybuf
) < 0) {
1115 if (!S_ISLNK (mybuf
.st_mode
))
1118 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
- 1);
1126 if (*buf2
== PATH_SEP
)
1131 canonicalize_pathname (buf
);
1132 r
= strchr (buf
, 0);
1133 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1143 strcpy (buf
, PATH_SEP_STR
);
1144 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1150 /* Finds out a relative path from first to second, i.e. goes as many ..
1151 * as needed up in first and then goes down using second */
1152 char *diff_two_paths (const char *first
, const char *second
)
1154 char *p
, *q
, *r
, *s
, *buf
= 0;
1155 int i
, j
, prevlen
= -1, currlen
;
1156 char *my_first
= NULL
, *my_second
= NULL
;
1158 my_first
= resolve_symlinks (first
);
1159 if (my_first
== NULL
)
1161 for (j
= 0; j
< 2; j
++) {
1164 my_second
= resolve_symlinks (second
);
1165 if (my_second
== NULL
) {
1172 r
= strchr (p
, PATH_SEP
);
1173 s
= strchr (q
, PATH_SEP
);
1177 if (strcmp (p
, q
)) {
1178 *r
= PATH_SEP
; *s
= PATH_SEP
;
1181 *r
= PATH_SEP
; *s
= PATH_SEP
;
1187 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1188 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1190 if (currlen
< prevlen
)
1198 p
= buf
= g_malloc (currlen
);
1200 for (; i
>= 0; i
--, p
+= 3)
1209 /* If filename is NULL, then we just append PATH_SEP to the dir */
1211 concat_dir_and_file (const char *dir
, const char *file
)
1213 int i
= strlen (dir
);
1215 if (dir
[i
-1] == PATH_SEP
)
1216 return g_strconcat (dir
, file
, (char *) NULL
);
1218 return g_strconcat (dir
, PATH_SEP_STR
, file
, (char *) NULL
);
1222 /* Append text to GList, remove all entries with the same text */
1224 list_append_unique (GList
*list
, char *text
)
1226 GList
*link
, *newlink
;
1229 * Go to the last position and traverse the list backwards
1230 * starting from the second last entry to make sure that we
1231 * are not removing the current link.
1233 list
= g_list_append (list
, text
);
1234 list
= g_list_last (list
);
1235 link
= g_list_previous (list
);
1238 newlink
= g_list_previous (link
);
1239 if (!strcmp ((char *) link
->data
, text
)) {
1240 g_free (link
->data
);
1241 g_list_remove_link (list
, link
);
1242 g_list_free_1 (link
);
1251 /* Following code heavily borrows from libiberty, mkstemps.c */
1253 /* Number of attempts to create a temporary file */
1255 #define TMP_MAX 16384
1256 #endif /* !TMP_MAX */
1260 * pname (output) - pointer to the name of the temp file (needs g_free).
1261 * NULL if the function fails.
1262 * prefix - part of the filename before the random part.
1263 * Prepend $TMPDIR or /tmp if there are no path separators.
1264 * suffix - if not NULL, part of the filename after the random part.
1267 * handle of the open file or -1 if couldn't open any.
1270 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
1272 static const char letters
[]
1273 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1274 static unsigned long value
;
1281 if (strchr (prefix
, PATH_SEP
) == NULL
) {
1282 /* Add prefix first to find the position of XXXXXX */
1283 tmpbase
= concat_dir_and_file (mc_tmpdir (), prefix
);
1285 tmpbase
= g_strdup (prefix
);
1288 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
1290 XXXXXX
= &tmpname
[strlen (tmpbase
)];
1293 /* Get some more or less random data. */
1294 gettimeofday (&tv
, NULL
);
1295 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
1297 for (count
= 0; count
< TMP_MAX
; ++count
) {
1298 unsigned long v
= value
;
1301 /* Fill in the random bits. */
1302 XXXXXX
[0] = letters
[v
% 62];
1304 XXXXXX
[1] = letters
[v
% 62];
1306 XXXXXX
[2] = letters
[v
% 62];
1308 XXXXXX
[3] = letters
[v
% 62];
1310 XXXXXX
[4] = letters
[v
% 62];
1312 XXXXXX
[5] = letters
[v
% 62];
1314 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
,
1317 /* Successfully created. */
1321 /* This is a random value. It is only necessary that the next
1322 TMP_MAX values generated by adding 7777 to VALUE are different
1323 with (module 2^32). */
1327 /* Unsuccessful. Free the filename. */
1336 * Read and restore position for the given filename.
1337 * If there is no stored data, return line 1 and col 0.
1340 load_file_position (const char *filename
, long *line
, long *column
)
1344 char buf
[MC_MAXPATHLEN
+ 20];
1351 /* open file with positions */
1352 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1353 f
= fopen (fn
, "r");
1358 len
= strlen (filename
);
1360 while (fgets (buf
, sizeof (buf
), f
)) {
1363 /* check if the filename matches the beginning of string */
1364 if (strncmp (buf
, filename
, len
) != 0)
1367 /* followed by single space */
1368 if (buf
[len
] != ' ')
1371 /* and string without spaces */
1373 if (strchr (p
, ' '))
1376 *line
= strtol(p
, const_cast(char **, &p
), 10);
1378 *column
= strtol(p
+1, const_cast(char **, &p
), 10);
1387 /* Save position for the given file */
1389 save_file_position (const char *filename
, long line
, long column
)
1393 char buf
[MC_MAXPATHLEN
+ 20];
1397 len
= strlen (filename
);
1399 tmp
= concat_dir_and_file (home_dir
, MC_FILEPOS_TMP
);
1400 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1402 /* open temporary file */
1403 t
= fopen (tmp
, "w");
1410 /* put the new record */
1411 if (line
!= 1 || column
!= 0) {
1412 fprintf (t
, "%s %ld;%ld\n", filename
, line
, column
);
1415 /* copy records from the old file */
1416 f
= fopen (fn
, "r");
1418 while (fgets (buf
, sizeof (buf
), f
)) {
1419 /* Skip entries for the current filename */
1420 if (strncmp (buf
, filename
, len
) == 0 && buf
[len
] == ' '
1421 && !strchr (&buf
[len
+ 1], ' '))
1424 fprintf (t
, "%s", buf
);
1425 if (++i
> MC_FILEPOS_ENTRIES
)
1438 cstrcasestr (const char *haystack
, const char *needle
)
1441 size_t i
, needle_len
;
1443 needle_len
= strlen (needle
);
1444 for (hptr
= haystack
; *hptr
!= '\0'; hptr
++) {
1445 for (i
= 0; i
< needle_len
; i
++) {
1446 if (toupper ((unsigned char) hptr
[i
]) !=
1447 toupper ((unsigned char) needle
[i
]))
1458 cstrstr (const char *haystack
, const char *needle
)
1460 return strstr(haystack
, needle
);
1464 str_unconst (const char *s
)
1469 #define ASCII_A (0x40 + 1)
1470 #define ASCII_Z (0x40 + 26)
1471 #define ASCII_a (0x60 + 1)
1472 #define ASCII_z (0x60 + 26)
1475 ascii_alpha_to_cntrl (int ch
)
1477 if ((ch
>= ASCII_A
&& ch
<= ASCII_Z
)
1478 || (ch
>= ASCII_a
&& ch
<= ASCII_z
)) {
1485 gettext_ui (const char *s
)
1487 const char *result
, *sep
;
1490 sep
= strchr(result
, '|');
1491 return (sep
!= NULL
) ? sep
+ 1 : result
;