charset: fixed crash of utf8_to_local(0)
[midnight-commander.git] / src / util.c
blob5f87b577245475f071c7623affc15bb5b31da563
1 /* Various utilities
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. */
25 #include <config.h>
27 #include <ctype.h>
28 #include <limits.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <iconv.h>
38 #include <langinfo.h>
39 #include <errno.h>
41 #include <mhl/escape.h>
42 #include <mhl/string.h>
44 #include "tty.h"
45 #include "global.h"
46 #include "profile.h"
47 #include "main.h" /* mc_home */
48 #include "cmd.h" /* guess_message_value */
49 #include "mountlist.h"
50 #include "win.h" /* xterm_flag */
51 #include "timefmt.h"
53 #ifdef HAVE_CHARSET
54 #include "charsets.h"
55 #endif
57 #ifdef UTF8
58 #include <wctype.h>
59 #endif
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 */
68 while(n-- && *s)
69 SLsmg_write_char(*s++);
71 else { /* convert wchars back to 8bit encoding */
72 mbstate_t mbs;
73 memset (&mbs, 0, sizeof (mbs));
74 while (n-- && *s) {
75 char buf[MB_LEN_MAX + 1]; /* should use 1 char, but to be sure */
76 if (*s < 0x80) {
77 SLsmg_write_char(*s++); /* ASCII */
79 else {
80 if (wcrtomb(buf, *s++, &mbs) == 1)
81 SLsmg_write_char((wchar_t)(buf[0]));
82 else
83 SLsmg_write_char('?'); /* should not happen */
88 #endif
90 extern void str_replace(char *s, char from, char to)
92 for (; *s != '\0'; s++) {
93 if (*s == from)
94 *s = to;
98 static inline int
99 is_7bit_printable (unsigned char c)
101 return (c > 31 && c < 127);
104 static inline int
105 is_iso_printable (unsigned char c)
107 return ((c > 31 && c < 127) || c >= 160);
110 static inline int
111 is_8bit_printable (unsigned char c)
113 /* "Full 8 bits output" doesn't work on xterm */
114 if (xterm_flag)
115 return is_iso_printable (c);
117 return (c > 31 && c != 127 && c != 155);
120 size_t
121 mbstrlen (const char *str)
123 #ifdef UTF8
124 if (SLsmg_Is_Unicode) {
125 size_t width = 0;
127 for (; *str; str++) {
128 wchar_t c;
129 size_t len;
131 len = mbrtowc (&c, str, MB_CUR_MAX, NULL);
133 if (len == (size_t)(-1) || len == (size_t)(-2)) break;
135 if (len > 0) {
136 int wcsize = wcwidth(c);
137 width += wcsize > 0 ? wcsize : 0;
138 str += len-1;
142 return width;
143 } else
144 #endif
145 return strlen (str);
148 #ifdef UTF8
150 void
151 fix_utf8(char *str)
153 mbstate_t mbs;
155 char *p = str;
157 while (*p) {
158 int len;
159 memset (&mbs, 0, sizeof (mbs));
160 len = mbrlen(p, MB_CUR_MAX, &mbs);
161 if (len == -1) {
162 *p = '?';
163 p++;
164 } else if (len > 0) {
165 p += len;
166 } else {
167 p++;
171 #endif
175 #ifdef UTF8
176 wchar_t *
177 mbstr_to_wchar (const char *str)
179 int len = mbstrlen(str);
180 wchar_t *buf = g_malloc((len+1) * sizeof(wchar_t));
181 mbstate_t mbs;
182 memset (&mbs, 0, sizeof (mbs));
183 mbsrtowcs (buf, &str, len, &mbs);
184 buf[len] = 0;
185 return buf;
188 char *
189 wchar_to_mbstr (const wchar_t *wstr)
191 mbstate_t mbs;
192 const wchar_t *wstr2;
193 char * string;
194 int len;
196 memset (&mbs, 0, sizeof (mbs));
197 wstr2 = wstr;
198 len = wcsrtombs(NULL, &wstr2, 0, &mbs);
199 if (len <= 0)
200 return NULL;
202 string = g_malloc(len + 1);
204 wstr2 = wstr;
205 wcsrtombs(string, &wstr2, len, &mbs);
206 string[len] = 0;
207 return string;
209 #endif
214 is_printable (int c)
216 #ifdef UTF8
217 if (SLsmg_Is_Unicode)
218 return iswprint (c);
219 #endif
220 c &= 0xff;
222 #ifdef HAVE_CHARSET
223 /* "Display bits" is ignored, since the user controls the output
224 by setting the output codepage */
225 return is_8bit_printable (c);
226 #else
227 if (!eight_bit_clean)
228 return is_7bit_printable (c);
230 if (full_eight_bits) {
231 return is_8bit_printable (c);
232 } else
233 return is_iso_printable (c);
234 #endif /* !HAVE_CHARSET */
237 /* Calculates the message dimension in columns and lines. */
238 void
239 msglen (const char *text, int *lines, int *columns)
241 int nlines = 1; /* even the empty string takes one line */
242 int ncolumns = 0;
243 int colindex = 0;
245 for (; *text != '\0'; text++) {
246 if (*text == '\n') {
247 nlines++;
248 colindex = 0;
249 } else {
250 #ifndef UTF8
251 colindex++;
252 if (colindex > ncolumns)
253 #else /* UTF8 */
254 size_t len;
255 wchar_t c;
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;
261 text += len-1;
263 if (++colindex > ncolumns)
264 #endif /* UTF8 */
265 ncolumns = colindex;
269 *lines = nlines;
270 *columns = 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.
278 char *
279 trim (const char *s, char *d, int len)
281 int source_len;
283 /* Sanity check */
284 len = max (len, 0);
286 source_len = strlen (s);
287 if (source_len > len) {
288 /* Cannot fit the whole line */
289 if (len <= 3) {
290 /* We only have room for the dots */
291 memset (d, '.', len);
292 d[len] = 0;
293 return d;
294 } else {
295 /* Begin with ... and add the rest of the source string */
296 memset (d, '.', 3);
297 strcpy (d + 3, s + 3 + source_len - len);
299 } else
300 /* We can copy the whole line */
301 strcpy (d, s);
302 return d;
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.
310 char *
311 name_quote (const char *s, int quote_percent)
313 char *ret, *d;
315 d = ret = g_malloc (strlen (s) * 2 + 2 + 1);
316 if (*s == '-') {
317 *d++ = '.';
318 *d++ = '/';
321 for (; *s; s++, d++) {
322 switch (*s) {
323 case '%':
324 if (quote_percent)
325 *d++ = '%';
326 break;
327 case '\'':
328 case '\\':
329 case '\r':
330 case '\n':
331 case '\t':
332 case '"':
333 case ';':
334 case ' ':
335 case '?':
336 case '|':
337 case '[':
338 case ']':
339 case '{':
340 case '}':
341 case '<':
342 case '>':
343 case '`':
344 case '!':
345 case '$':
346 case '&':
347 case '*':
348 case '(':
349 case ')':
350 *d++ = '\\';
351 break;
352 case '~':
353 case '#':
354 if (d == ret)
355 *d++ = '\\';
356 break;
358 #ifndef UTF8
359 *d = *s;
360 #else /* UTF8 */
362 mbstate_t mbs;
363 int len;
364 memset (&mbs, 0, sizeof (mbs));
365 len = mbrlen(s, MB_CUR_MAX, &mbs);
366 if (len > 0) {
367 while (len-- > 1)
368 *d++ = *s++;
369 *d = *s;
370 } else {
371 *d = '?';
375 #endif /* UTF8 */
377 *d = '\0';
378 return ret;
381 char *
382 fake_name_quote (const char *s, int quote_percent)
384 (void) quote_percent;
385 return g_strdup (s);
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.
393 const char *
394 name_trunc (const char *txt, int trunc_len)
396 static char x[MC_MAXPATHLEN + MC_MAXPATHLEN];
397 int txt_len, first, skip;
398 char *p;
399 const char *str;
401 if ((size_t) trunc_len > sizeof (x) - 1) {
402 trunc_len = sizeof (x) - 1;
404 txt_len = mbstrlen (txt);
405 first = 0;
406 skip = 0;
407 if (txt_len > trunc_len) {
408 first = trunc_len / 2;
409 skip = txt_len - trunc_len + 1;
412 #ifdef UTF8
413 if (SLsmg_Is_Unicode) {
414 mbstate_t s;
415 int mbmax;
417 str = txt;
418 memset (&s, 0, sizeof (s));
419 mbmax = MB_CUR_MAX;
420 p = x;
421 while (p < x + sizeof (x) - 1 && trunc_len) {
422 wchar_t wc;
423 int len;
425 len = mbrtowc (&wc, str, mbmax, &s);
426 if (!len)
427 break;
428 if (len < 0) {
429 memset (&s, 0, sizeof (s));
430 *p = '?';
431 len = 1;
432 str++;
433 } else if (!is_printable (wc)) {
434 *p = '?';
435 str += len;
436 len = 1;
437 } else if (p >= x + sizeof (x) - len)
438 break;
439 else {
440 memcpy (p, str, len);
441 str += len;
443 if (first) {
444 --trunc_len;
445 --first;
446 p += len;
447 if (!first && p < x + sizeof (x) - 1 && trunc_len) {
448 *p++ = '~';
449 --trunc_len;
451 } else if (skip)
452 --skip;
453 else {
454 --trunc_len;
455 p += len;
458 } else
459 #endif
461 str = txt;
462 p = x;
463 while (p < x + sizeof (x) - 1) {
464 if (*str == '\0')
465 break;
466 else if (!is_printable (*str))
467 *p++ = '?';
468 else
469 *p++ = *str;
470 ++str;
471 if (first) {
472 --first;
473 if (!first) {
474 *p++ = '~';
475 str += skip;
480 *p = '\0';
481 return x;
485 * path_trunc() is the same as name_trunc() above but
486 * it deletes possible password from path for security
487 * reasons.
489 const char *
490 path_trunc (const char *path, int trunc_len) {
491 const char *ret;
492 char *secure_path = strip_password (g_strdup (path), 1);
494 ret = name_trunc (secure_path, trunc_len);
495 g_free (secure_path);
497 return ret;
500 const char *
501 size_trunc (double size)
503 static char x [BUF_TINY];
504 long int divisor = 1;
505 const char *xtra = "";
507 if (size > 999999999L){
508 divisor = 1024;
509 xtra = "K";
510 if (size/divisor > 999999999L){
511 divisor = 1024*1024;
512 xtra = "M";
515 g_snprintf (x, sizeof (x), "%.0f%s", (size/divisor), xtra);
516 return x;
519 const char *
520 size_trunc_sep (double size)
522 static char x [60];
523 int count;
524 const char *p, *y;
525 char *d;
527 p = y = size_trunc (size);
528 p += strlen (p) - 1;
529 d = x + sizeof (x) - 1;
530 *d-- = 0;
531 while (p >= y && isalpha ((unsigned char) *p))
532 *d-- = *p--;
533 for (count = 0; p >= y; count++){
534 if (count == 3){
535 *d-- = ',';
536 count = 0;
538 *d-- = *p--;
540 d++;
541 if (*d == ',')
542 d++;
543 return d;
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.
555 void
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,
561 1000000000};
562 static const char * const suffix [] =
563 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL};
564 int j = 0;
566 /* Don't print more than 9 digits - use suffix. */
567 if (len == 0 || len > 9)
568 len = 9;
570 for (j = units; suffix [j] != NULL; j++) {
571 if (size == 0) {
572 if (j == units) {
573 /* Empty files will print "0" even with minimal width. */
574 g_snprintf (buffer, len + 1, "0");
575 break;
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");
581 break;
584 if (size < power10 [len - (j > 0)]) {
585 g_snprintf (buffer, len + 1, "%lu%s", (unsigned long) size, suffix[j]);
586 break;
589 /* Powers of 1024, with rounding. */
590 size = (size + 512) >> 10;
595 is_exe (mode_t mode)
597 if ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode))
598 return 1;
599 return 0;
602 #define ismode(n,m) ((n & m) == m)
604 const char *
605 string_perm (mode_t mode_bits)
607 static char mode[11];
609 strcpy (mode, "----------");
610 if (S_ISDIR (mode_bits))
611 mode[0] = 'd';
612 if (S_ISCHR (mode_bits))
613 mode[0] = 'c';
614 if (S_ISBLK (mode_bits))
615 mode[0] = 'b';
616 if (S_ISLNK (mode_bits))
617 mode[0] = 'l';
618 if (S_ISFIFO (mode_bits))
619 mode[0] = 'p';
620 if (S_ISNAM (mode_bits))
621 mode[0] = 'n';
622 if (S_ISSOCK (mode_bits))
623 mode[0] = 's';
624 if (S_ISDOOR (mode_bits))
625 mode[0] = 'D';
626 if (ismode (mode_bits, S_IXOTH))
627 mode[9] = 'x';
628 if (ismode (mode_bits, S_IWOTH))
629 mode[8] = 'w';
630 if (ismode (mode_bits, S_IROTH))
631 mode[7] = 'r';
632 if (ismode (mode_bits, S_IXGRP))
633 mode[6] = 'x';
634 if (ismode (mode_bits, S_IWGRP))
635 mode[5] = 'w';
636 if (ismode (mode_bits, S_IRGRP))
637 mode[4] = 'r';
638 if (ismode (mode_bits, S_IXUSR))
639 mode[3] = 'x';
640 if (ismode (mode_bits, S_IWUSR))
641 mode[2] = 'w';
642 if (ismode (mode_bits, S_IRUSR))
643 mode[1] = 'r';
644 #ifdef S_ISUID
645 if (ismode (mode_bits, S_ISUID))
646 mode[3] = (mode[3] == 'x') ? 's' : 'S';
647 #endif /* S_ISUID */
648 #ifdef S_ISGID
649 if (ismode (mode_bits, S_ISGID))
650 mode[6] = (mode[6] == 'x') ? 's' : 'S';
651 #endif /* S_ISGID */
652 #ifdef S_ISVTX
653 if (ismode (mode_bits, S_ISVTX))
654 mode[9] = (mode[9] == 'x') ? 't' : 'T';
655 #endif /* S_ISVTX */
656 return mode;
659 /* p: string which might contain an url with a password (this parameter is
660 modified in place).
661 has_prefix = 0: The first parameter is an url without a prefix
662 (user[:pass]@]machine[:port][remote-dir). Delete
663 the password.
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
668 char *
669 strip_password (char *p, int has_prefix)
671 static const struct {
672 const char *name;
673 size_t len;
674 } prefixes[] = { {"/#ftp:", 6},
675 {"ftp://", 6},
676 {"/#mc:", 5},
677 {"mc://", 5},
678 {"/#smb:", 6},
679 {"smb://", 6},
680 {"/#sh:", 5},
681 {"sh://", 5},
682 {"ssh://", 6}
684 char *at, *inner_colon, *dir;
685 size_t i;
686 char *result = p;
688 for (i = 0; i < sizeof (prefixes)/sizeof (prefixes[0]); i++) {
689 char *q;
691 if (has_prefix) {
692 if((q = strstr (p, prefixes[i].name)) == 0)
693 continue;
694 else
695 p = q + prefixes[i].len;
698 if ((dir = strchr (p, PATH_SEP)) != NULL)
699 *dir = '\0';
701 /* search for any possible user */
702 at = strrchr (p, '@');
704 if (dir)
705 *dir = PATH_SEP;
707 /* We have a username */
708 if (at) {
709 inner_colon = memchr (p, ':', at - p);
710 if (inner_colon)
711 memmove (inner_colon, at, strlen(at) + 1);
713 break;
715 return (result);
718 const char *
719 strip_home_and_password(const char *dir)
721 size_t len;
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')){
726 newdir [0] = '~';
727 g_strlcpy (&newdir [1], &dir [len], sizeof(newdir) - 1);
728 return newdir;
731 /* We do not strip homes in /#ftp tree, I do not like ~'s there
732 (see ftpfs.c why) */
733 g_strlcpy (newdir, dir, sizeof(newdir));
734 strip_password (newdir, 1);
735 return newdir;
738 static char *
739 maybe_start_group (char *d, int do_group, int *was_wildcard)
741 if (!do_group)
742 return d;
743 if (*was_wildcard)
744 return d;
745 *was_wildcard = 1;
746 *d++ = '\\';
747 *d++ = '(';
748 return d;
751 static char *
752 maybe_end_group (char *d, int do_group, int *was_wildcard)
754 if (!do_group)
755 return d;
756 if (!*was_wildcard)
757 return d;
758 *was_wildcard = 0;
759 *d++ = '\\';
760 *d++ = ')';
761 return d;
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 ?? */
767 char *
768 convert_pattern (const char *pattern, int match_type, int do_group)
770 char *d;
771 char *new_pattern;
772 int was_wildcard = 0;
773 const char *s;
775 if ((match_type != match_regex) && easy_patterns){
776 new_pattern = g_malloc (MC_MAXPATHLEN);
777 d = new_pattern;
778 if (match_type == match_file)
779 *d++ = '^';
780 for (s = pattern; *s; s++, d++){
781 switch (*s){
782 case '*':
783 d = maybe_start_group (d, do_group, &was_wildcard);
784 *d++ = '.';
785 *d = '*';
786 break;
788 case '?':
789 d = maybe_start_group (d, do_group, &was_wildcard);
790 *d = '.';
791 break;
793 case '.':
794 d = maybe_end_group (d, do_group, &was_wildcard);
795 *d++ = '\\';
796 *d = '.';
797 break;
799 default:
800 d = maybe_end_group (d, do_group, &was_wildcard);
801 *d = *s;
802 break;
805 d = maybe_end_group (d, do_group, &was_wildcard);
806 if (match_type == match_file)
807 *d++ = '$';
808 *d = 0;
809 return new_pattern;
810 } else
811 return g_strdup (pattern);
815 regexp_match (const char *pattern, const char *string, int match_type)
817 static regex_t r;
818 static char *old_pattern = NULL;
819 static int old_type;
820 int rval;
821 char *my_pattern;
823 if (!old_pattern || STRCOMP (old_pattern, pattern) || old_type != match_type){
824 if (old_pattern){
825 regfree (&r);
826 g_free (old_pattern);
827 old_pattern = NULL;
829 my_pattern = convert_pattern (pattern, match_type, 0);
830 if (regcomp (&r, my_pattern, REG_EXTENDED|REG_NOSUB|MC_ARCH_FLAGS)) {
831 g_free (my_pattern);
832 return -1;
834 old_pattern = my_pattern;
835 old_type = match_type;
837 rval = !regexec (&r, string, 0, NULL, 0);
838 return rval;
841 const char *
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);
863 extern char *
864 get_config_string (const char *file, const char *key, const char *defval)
866 char buffer[1024];
867 (void)GetPrivateProfileString (app_text, key, defval, buffer, sizeof(buffer), file);
868 return g_strdup (buffer);
871 extern void
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;
883 char *
884 load_file (const char *filename)
886 FILE *data_file;
887 struct stat s;
888 char *data;
889 long read_size;
891 if ((data_file = fopen (filename, "r")) == NULL){
892 return 0;
894 if (fstat (fileno (data_file), &s) != 0){
895 fclose (data_file);
896 return 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;
901 fclose (data_file);
903 if (read_size > 0)
904 return data;
905 else {
906 g_free (data);
907 return 0;
911 char *
912 utf8_to_local(char *str)
914 iconv_t cd;
915 size_t buflen;
916 char *output;
917 int retry = 1;
919 if (!str)
920 return 0;
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);
931 while (retry)
933 char *wrptr = output;
934 char *inptr = str;
935 size_t insize = buflen;
936 size_t avail = buflen;
937 size_t nconv;
939 nconv = iconv (cd, &inptr, &insize, &wrptr, &avail);
940 if (nconv == (size_t) -1)
942 if (errno == E2BIG)
944 buflen *= 2;
945 g_free(output);
946 output = g_malloc(buflen + 1);
948 else
950 g_free(output);
951 return g_strdup(str);
954 else {
955 retry = 0;
956 *wrptr = 0;
960 iconv_close (cd);
962 return output;
965 char *
966 load_mc_home_file (const char *filename, char **allocated_filename)
968 char *hintfile_base, *hintfile;
969 char *lang;
970 char *data;
971 char *conv_data;
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);
979 if (!data) {
980 g_free (hintfile);
981 /* Fall back to the two-letter language code */
982 if (lang[0] && lang[1])
983 lang[2] = 0;
984 hintfile = g_strconcat (hintfile_base, ".", lang, (char *) NULL);
985 data = load_file (hintfile);
987 if (!data) {
988 g_free (hintfile);
989 hintfile = hintfile_base;
990 data = load_file (hintfile_base);
994 g_free (lang);
996 if (hintfile != hintfile_base)
997 g_free (hintfile_base);
999 if (allocated_filename)
1000 *allocated_filename = hintfile;
1001 else
1002 g_free (hintfile);
1004 conv_data = utf8_to_local(data);
1005 g_free(data);
1007 return conv_data;
1010 /* Check strftime() results. Some systems (i.e. Solaris) have different
1011 short-month-name sizes for different locales */
1012 size_t
1013 i18n_checktimelength (void)
1015 time_t testtime = time (NULL);
1016 struct tm* lt = localtime(&testtime);
1017 size_t length;
1019 if (lt == NULL) {
1020 // huh, localtime() doesnt seem to work ... falling back to "(invalid)"
1021 length = strlen(INVALID_TIME_TEXT);
1022 } else {
1023 char buf [4* MAX_I18NTIMELENGTH + 1];
1024 size_t a, b;
1025 strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), lt);
1026 a = mbstrlen(buf);
1027 strftime (buf, sizeof(buf)-1, _("%b %e %Y"), lt);
1028 b = mbstrlen(buf);
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;
1036 return length;
1039 const char *
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;
1045 const char *fmt;
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. */
1063 fmt = fmtyear;
1064 else
1065 fmt = fmttime;
1067 FMT_LOCALTIME(timebuf, sizeof(timebuf), fmt, when);
1069 return timebuf;
1072 const char *
1073 extract_line (const char *s, const char *top)
1075 static char tmp_line [BUF_MEDIUM];
1076 char *t = tmp_line;
1078 while (*s && *s != '\n' && (size_t) (t - tmp_line) < sizeof (tmp_line)-1 && s < top)
1079 *t++ = *s++;
1080 *t = 0;
1081 return tmp_line;
1084 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
1085 const char *
1086 _icase_search (const char *text, const char *data, int *lng)
1088 const char *d = text;
1089 const char *e = data;
1090 int dlng = 0;
1092 if (lng)
1093 *lng = 0;
1094 for (;*e; e++) {
1095 while (*(e+1) == '\b' && *(e+2)) {
1096 e += 2;
1097 dlng += 2;
1099 if (toupper((unsigned char) *d) == toupper((unsigned char) *e))
1100 d++;
1101 else {
1102 e -= d - text;
1103 d = text;
1104 dlng = 0;
1106 if (!*d) {
1107 if (lng)
1108 *lng = strlen (text) + dlng;
1109 return e+1;
1112 return 0;
1115 /* The basename routine */
1116 const char *
1117 x_basename (const char *s)
1119 const char *where;
1120 return ((where = strrchr (s, PATH_SEP))) ? where + 1 : s;
1124 const char *
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);
1135 #else
1136 g_snprintf (buffer, sizeof (buffer), "%s (%d)",
1137 g_strerror (error_num), error_num);
1138 #endif
1139 return buffer;
1142 const char *
1143 skip_separators (const char *s)
1145 for (;*s; s++)
1146 if (*s != ' ' && *s != '\t' && *s != ',')
1147 break;
1148 return s;
1151 const char *
1152 skip_numbers (const char *s)
1154 for (;*s; s++)
1155 if (!isdigit ((unsigned char) *s))
1156 break;
1157 return 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.
1173 char *
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 */
1179 if (!s)
1180 return 0;
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.
1194 if (*r)
1195 r++;
1196 continue;
1198 #ifndef UTF8
1199 if (is_printable(*r))
1200 *w++ = *r;
1201 ++r;
1202 #else /* UTF8 */
1204 mbstate_t mbs;
1205 int len;
1206 memset (&mbs, 0, sizeof (mbs));
1207 len = mbrlen(r, MB_CUR_MAX, &mbs);
1209 if (len > 0 && (unsigned char)*r >= ' ')
1210 while (len--)
1211 *w++ = *r++;
1212 else {
1213 if (len == -1)
1214 *w++ = '?';
1215 r++;
1218 #endif /* UTF8 */
1220 *w = 0;
1221 return s;
1225 #ifndef USE_VFS
1226 char *
1227 get_current_wd (char *buffer, int size)
1229 char *p;
1230 int len;
1232 p = g_get_current_dir ();
1233 len = strlen(p) + 1;
1235 if (len > size) {
1236 g_free (p);
1237 return NULL;
1240 memcpy (buffer, p, len);
1241 g_free (p);
1243 return buffer;
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;
1261 /* PKZIP_MAGIC */
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 */
1278 if (magic[0] == 037
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')) {
1287 switch (magic[2]) {
1288 case '0':
1289 return COMPRESSION_BZIP;
1290 case 'h':
1291 return COMPRESSION_BZIP2;
1294 return 0;
1297 const char *
1298 decompress_extension (int type)
1300 switch (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");
1307 return 0;
1310 /* Hooks */
1311 void
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;
1323 void
1324 execute_hooks (Hook *hook_list)
1326 Hook *new_hook = 0;
1327 Hook *p;
1329 /* We copy the hook list first so tahat we let the hook
1330 * function call delete_hook
1333 while (hook_list){
1334 add_hook (&new_hook, hook_list->hook_fn, hook_list->hook_data);
1335 hook_list = hook_list->next;
1337 p = new_hook;
1339 while (new_hook){
1340 (*new_hook->hook_fn)(new_hook->hook_data);
1341 new_hook = new_hook->next;
1344 for (hook_list = p; hook_list;){
1345 p = hook_list;
1346 hook_list = hook_list->next;
1347 g_free (p);
1351 void
1352 delete_hook (Hook **hook_list, void (*hook_fn)(void *))
1354 Hook *current, *new_list, *next;
1356 new_list = 0;
1358 for (current = *hook_list; current; current = next){
1359 next = current->next;
1360 if (current->hook_fn == hook_fn)
1361 g_free (current);
1362 else
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 *))
1371 Hook *p;
1373 for (p = hook_list; p; p = p->next)
1374 if (p->hook_fn == hook_fn)
1375 return 1;
1376 return 0;
1379 void
1380 wipe_password (char *passwd)
1382 char *p = passwd;
1384 if (!p)
1385 return;
1386 for (;*p ; p++)
1387 *p = 0;
1388 g_free (passwd);
1391 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
1392 /* Returns a newly allocated string */
1393 char *
1394 convert_controls (const char *p)
1396 char *valcopy = g_strdup (p);
1397 char *q;
1399 /* Parse the escape special character */
1400 for (q = valcopy; *p;){
1401 if (*p == '\\'){
1402 p++;
1403 if ((*p == 'e') || (*p == 'E')){
1404 p++;
1405 *q++ = ESC_CHAR;
1407 } else {
1408 if (*p == '^'){
1409 p++;
1410 if (*p == '^')
1411 *q++ = *p++;
1412 else {
1413 char c = (*p | 0x20);
1414 if (c >= 'a' && c <= 'z') {
1415 *q++ = c - 'a' + 1;
1416 p++;
1417 } else if (*p)
1418 p++;
1420 } else
1421 *q++ = *p++;
1424 *q = 0;
1425 return valcopy;
1428 static char *
1429 resolve_symlinks (const char *path)
1431 char *buf, *buf2, *q, *r, c;
1432 int len;
1433 struct stat mybuf;
1434 const char *p;
1436 if (*path != PATH_SEP)
1437 return NULL;
1438 r = buf = g_malloc (MC_MAXPATHLEN);
1439 buf2 = g_malloc (MC_MAXPATHLEN);
1440 *r++ = PATH_SEP;
1441 *r = 0;
1442 p = path;
1443 for (;;) {
1444 q = strchr (p + 1, PATH_SEP);
1445 if (!q) {
1446 q = strchr (p + 1, 0);
1447 if (q == p + 1)
1448 break;
1450 c = *q;
1451 *q = 0;
1452 if (mc_lstat (path, &mybuf) < 0) {
1453 g_free (buf);
1454 g_free (buf2);
1455 *q = c;
1456 return NULL;
1458 if (!S_ISLNK (mybuf.st_mode))
1459 strcpy (r, p + 1);
1460 else {
1461 len = mc_readlink (path, buf2, MC_MAXPATHLEN - 1);
1462 if (len < 0) {
1463 g_free (buf);
1464 g_free (buf2);
1465 *q = c;
1466 return NULL;
1468 buf2 [len] = 0;
1469 if (*buf2 == PATH_SEP)
1470 strcpy (buf, buf2);
1471 else
1472 strcpy (r, buf2);
1474 canonicalize_pathname (buf);
1475 r = strchr (buf, 0);
1476 if (!*r || *(r - 1) != PATH_SEP) {
1477 *r++ = PATH_SEP;
1478 *r = 0;
1480 *q = c;
1481 p = q;
1482 if (!c)
1483 break;
1485 if (!*buf)
1486 strcpy (buf, PATH_SEP_STR);
1487 else if (*(r - 1) == PATH_SEP && r != buf + 1)
1488 *(r - 1) = 0;
1489 g_free (buf2);
1490 return buf;
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 */
1495 char *
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)
1504 return NULL;
1505 my_second = resolve_symlinks (second);
1506 if (my_second == NULL) {
1507 g_free (my_first);
1508 return NULL;
1510 for (j = 0; j < 2; j++) {
1511 p = my_first;
1512 q = my_second;
1513 for (;;) {
1514 r = strchr (p, PATH_SEP);
1515 s = strchr (q, PATH_SEP);
1516 if (!r || !s)
1517 break;
1518 *r = 0; *s = 0;
1519 if (strcmp (p, q)) {
1520 *r = PATH_SEP; *s = PATH_SEP;
1521 break;
1522 } else {
1523 *r = PATH_SEP; *s = PATH_SEP;
1525 p = r + 1;
1526 q = s + 1;
1528 p--;
1529 for (i = 0; (p = strchr (p + 1, PATH_SEP)) != NULL; i++);
1530 currlen = (i + 1) * 3 + strlen (q) + 1;
1531 if (j) {
1532 if (currlen < prevlen)
1533 g_free (buf);
1534 else {
1535 g_free (my_first);
1536 g_free (my_second);
1537 return buf;
1540 p = buf = g_malloc (currlen);
1541 prevlen = currlen;
1542 for (; i >= 0; i--, p += 3)
1543 strcpy (p, "../");
1544 strcpy (p, q);
1546 g_free (my_first);
1547 g_free (my_second);
1548 return buf;
1551 /* Append text to GList, remove all entries with the same text */
1552 GList *
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);
1566 while (link) {
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);
1573 link = newlink;
1576 return list;
1579 /* Following code heavily borrows from libiberty, mkstemps.c */
1581 /* Number of attempts to create a temporary file */
1582 #ifndef TMP_MAX
1583 #define TMP_MAX 16384
1584 #endif /* !TMP_MAX */
1587 * Arguments:
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.
1594 * Result:
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;
1603 struct timeval tv;
1604 char *tmpbase;
1605 char *tmpname;
1606 char *XXXXXX;
1607 int count;
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);
1612 } else {
1613 tmpbase = g_strdup (prefix);
1616 tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
1617 *pname = tmpname;
1618 XXXXXX = &tmpname[strlen (tmpbase)];
1619 g_free (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;
1627 int fd;
1629 /* Fill in the random bits. */
1630 XXXXXX[0] = letters[v % 62];
1631 v /= 62;
1632 XXXXXX[1] = letters[v % 62];
1633 v /= 62;
1634 XXXXXX[2] = letters[v % 62];
1635 v /= 62;
1636 XXXXXX[3] = letters[v % 62];
1637 v /= 62;
1638 XXXXXX[4] = letters[v % 62];
1639 v /= 62;
1640 XXXXXX[5] = letters[v % 62];
1642 fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
1643 S_IRUSR | S_IWUSR);
1644 if (fd >= 0) {
1645 /* Successfully created. */
1646 return fd;
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). */
1652 value += 7777;
1655 /* Unsuccessful. Free the filename. */
1656 g_free (tmpname);
1657 *pname = NULL;
1659 return -1;
1663 * Read and restore position for the given filename.
1664 * If there is no stored data, return line 1 and col 0.
1666 void
1667 load_file_position (const char *filename, long *line, long *column)
1669 char *fn;
1670 FILE *f;
1671 char buf[MC_MAXPATHLEN + 20];
1672 int len;
1674 /* defaults */
1675 *line = 1;
1676 *column = 0;
1678 /* open file with positions */
1679 fn = mhl_str_dir_plus_file (home_dir, MC_FILEPOS);
1680 f = fopen (fn, "r");
1681 g_free (fn);
1682 if (!f)
1683 return;
1685 len = strlen (filename);
1687 while (fgets (buf, sizeof (buf), f)) {
1688 const char *p;
1690 /* check if the filename matches the beginning of string */
1691 if (strncmp (buf, filename, len) != 0)
1692 continue;
1694 /* followed by single space */
1695 if (buf[len] != ' ')
1696 continue;
1698 /* and string without spaces */
1699 p = &buf[len + 1];
1700 if (strchr (p, ' '))
1701 continue;
1703 *line = strtol(p, const_cast(char **, &p), 10);
1704 if (*p == ';') {
1705 *column = strtol(p+1, const_cast(char **, &p), 10);
1706 if (*p != '\n')
1707 *column = 0;
1708 } else
1709 *line = 1;
1711 fclose (f);
1714 /* Save position for the given file */
1715 void
1716 save_file_position (const char *filename, long line, long column)
1718 char *tmp, *fn;
1719 FILE *f, *t;
1720 char buf[MC_MAXPATHLEN + 20];
1721 int i = 1;
1722 int len;
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");
1731 if (!t) {
1732 g_free (tmp);
1733 g_free (fn);
1734 return;
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");
1744 if (f) {
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], ' '))
1749 continue;
1751 fprintf (t, "%s", buf);
1752 if (++i > MC_FILEPOS_ENTRIES)
1753 break;
1755 fclose (f);
1758 fclose (t);
1759 rename (tmp, fn);
1760 g_free (tmp);
1761 g_free (fn);
1764 extern const char *
1765 cstrcasestr (const char *haystack, const char *needle)
1767 const char *hptr;
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]))
1775 goto next_try;
1777 return hptr;
1778 next_try:
1779 (void) 0;
1781 return NULL;
1784 const char *
1785 cstrstr (const char *haystack, const char *needle)
1787 return strstr(haystack, needle);
1790 extern char *
1791 str_unconst (const char *s)
1793 return (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)
1801 extern int
1802 ascii_alpha_to_cntrl (int ch)
1804 if ((ch >= ASCII_A && ch <= ASCII_Z)
1805 || (ch >= ASCII_a && ch <= ASCII_z)) {
1806 ch &= 0x1f;
1808 return ch;
1811 const char *
1812 Q_ (const char *s)
1814 const char *result, *sep;
1816 result = _(s);
1817 sep = strchr(result, '|');
1818 return (sep != NULL) ? sep + 1 : result;