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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include <sys/types.h>
30 #include <limits.h> /* INT_MAX */
38 #include "main.h" /* mc_home */
39 #include "cmd.h" /* guess_message_value */
40 #include "../vfs/vfs.h"
41 #include "mountlist.h"
42 #include "win.h" /* xterm_flag */
48 static const char app_text
[] = "Midnight-Commander";
49 int easy_patterns
= 1;
52 is_7bit_printable (unsigned char c
)
54 return (c
> 31 && c
< 127);
58 is_iso_printable (unsigned char c
)
60 return ((c
> 31 && c
< 127) || c
>= 160);
64 is_8bit_printable (unsigned char c
)
66 /* "Full 8 bits output" doesn't work on xterm */
68 return is_iso_printable (c
);
70 return (c
> 31 && c
!= 127 && c
!= 155);
79 /* "Display bits" is ignored, since the user controls the output
80 by setting the output codepage */
81 return is_8bit_printable (c
);
84 return is_7bit_printable (c
);
86 if (full_eight_bits
) {
87 return is_8bit_printable (c
);
89 return is_iso_printable (c
);
90 #endif /* !HAVE_CHARSET */
93 /* Returns the message dimensions (lines and columns) */
94 int msglen (char *text
, int *lines
)
99 for (*lines
= 1;*text
; text
++){
113 * Copy from s to d, and trim the beginning if necessary, and prepend
114 * "..." in this case. The destination string can have at most len
115 * bytes, not counting trailing 0.
118 trim (char *s
, char *d
, int len
)
125 source_len
= strlen (s
);
126 if (source_len
> len
) {
127 /* Cannot fit the whole line */
129 /* We only have room for the dots */
130 memset (d
, '.', len
);
134 /* Begin with ... and add the rest of the source string */
136 strcpy (d
+ 3, s
+ 3 + source_len
- len
);
139 /* We can copy the whole line */
145 * Quote the filename for the purpose of inserting it into the command
146 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
147 * processed by the mc command line.
150 name_quote (const char *s
, int quote_percent
)
154 d
= ret
= g_malloc (strlen (s
) * 2 + 2 + 1);
160 for (; *s
; s
++, d
++) {
204 fake_name_quote (const char *s
, int quote_percent
)
210 * Remove the middle part of the string to fit given length.
211 * Use "~" to show where the string was truncated.
212 * Return static buffer, no need to free() it.
215 name_trunc (const char *txt
, int trunc_len
)
217 static char x
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
221 if (trunc_len
> sizeof (x
) - 1) {
222 trunc_len
= sizeof (x
) - 1;
224 txt_len
= strlen (txt
);
225 if (txt_len
<= trunc_len
) {
228 int y
= (trunc_len
/ 2) + (trunc_len
% 2);
230 strncpy (x
+ y
, txt
+ txt_len
- (trunc_len
/ 2), trunc_len
/ 2);
235 if (!is_printable (*p
))
240 char *size_trunc (double size
)
242 static char x
[BUF_TINY
];
243 long int divisor
= 1;
246 if (size
> 999999999L){
249 if (size
/divisor
> 999999999L){
254 g_snprintf (x
, sizeof (x
), "%.0f%s", (size
/divisor
), xtra
);
258 char *size_trunc_sep (double size
)
264 p
= y
= size_trunc (size
);
266 d
= x
+ sizeof (x
) - 1;
268 while (p
>= y
&& isalpha ((unsigned char) *p
))
270 for (count
= 0; p
>= y
; count
++){
284 * Print file SIZE to BUFFER, but don't exceed LEN characters,
285 * not including trailing 0. BUFFER should be at least LEN+1 long.
286 * This function is called for every file on panels, so avoid
287 * floating point by any means.
289 * Units: size units (filesystem sizes are 1K blocks)
290 * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
293 size_trunc_len (char *buffer
, int len
, off_t size
, int units
)
295 /* Avoid taking power for every file. */
296 static const off_t power10
[] =
297 {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
299 static const char * const suffix
[] =
300 {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL
};
303 /* Don't print more than 9 digits - use suffix. */
304 if (len
== 0 || len
> 9)
307 for (j
= units
; suffix
[j
] != NULL
; j
++) {
310 /* Empty files will print "0" even with minimal width. */
311 g_snprintf (buffer
, len
+ 1, "0");
315 /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
316 g_snprintf (buffer
, len
+ 1, (len
> 1) ? "~%s" : "%s",
317 (j
> 1) ? suffix
[j
- 1] : "B");
321 if (size
< power10
[len
- (j
> 0)]) {
322 g_snprintf (buffer
, len
+ 1, "%lu%s", (unsigned long) size
, suffix
[j
]);
326 /* Powers of 1024, with rounding. */
327 size
= (size
+ 512) >> 10;
331 int is_exe (mode_t mode
)
333 if ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
))
338 #define ismode(n,m) ((n & m) == m)
341 string_perm (mode_t mode_bits
)
343 static char mode
[11];
345 strcpy (mode
, "----------");
346 if (S_ISDIR (mode_bits
))
348 if (S_ISCHR (mode_bits
))
350 if (S_ISBLK (mode_bits
))
352 if (S_ISLNK (mode_bits
))
354 if (S_ISFIFO (mode_bits
))
356 if (S_ISSOCK (mode_bits
))
358 if (S_ISDOOR (mode_bits
))
360 if (ismode (mode_bits
, S_IXOTH
))
362 if (ismode (mode_bits
, S_IWOTH
))
364 if (ismode (mode_bits
, S_IROTH
))
366 if (ismode (mode_bits
, S_IXGRP
))
368 if (ismode (mode_bits
, S_IWGRP
))
370 if (ismode (mode_bits
, S_IRGRP
))
372 if (ismode (mode_bits
, S_IXUSR
))
374 if (ismode (mode_bits
, S_IWUSR
))
376 if (ismode (mode_bits
, S_IRUSR
))
379 if (ismode (mode_bits
, S_ISUID
))
380 mode
[3] = (mode
[3] == 'x') ? 's' : 'S';
383 if (ismode (mode_bits
, S_ISGID
))
384 mode
[6] = (mode
[6] == 'x') ? 's' : 'S';
387 if (ismode (mode_bits
, S_ISVTX
))
388 mode
[9] = (mode
[9] == 'x') ? 't' : 'T';
393 /* p: string which might contain an url with a password (this parameter is
395 has_prefix = 0: The first parameter is an url without a prefix
396 (user[:pass]@]machine[:port][remote-dir). Delete
398 has_prefix = 1: Search p for known url prefixes. If found delete
399 the password from the url.
400 Cavevat: only the first url is found
403 strip_password (char *p
, int has_prefix
)
405 static const struct {
408 } prefixes
[] = { {"/#ftp:", 6},
413 char *at
, *inner_colon
, *dir
;
417 for (i
= 0; i
< sizeof (prefixes
)/sizeof (prefixes
[0]); i
++) {
421 if((q
= strstr (p
, prefixes
[i
].name
)) == 0)
424 p
= q
+ prefixes
[i
].len
;
427 if ((dir
= strchr (p
, PATH_SEP
)) != NULL
)
429 /* search for any possible user */
430 at
= strchr (p
, '@');
432 /* We have a username */
435 inner_colon
= strchr (p
, ':');
438 strcpy (inner_colon
, at
);
447 char *strip_home_and_password(const char *dir
)
450 static char newdir
[MC_MAXPATHLEN
];
452 if (home_dir
&& !strncmp (dir
, home_dir
, len
= strlen (home_dir
)) &&
453 (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0')){
455 strcpy (&newdir
[1], &dir
[len
]);
459 /* We do not strip homes in /#ftp tree, I do not like ~'s there
461 strcpy (newdir
, dir
);
462 strip_password (newdir
, 1);
466 static char *maybe_start_group (char *d
, int do_group
, int *was_wildcard
)
478 static char *maybe_end_group (char *d
, int do_group
, int *was_wildcard
)
490 /* If shell patterns are on converts a shell pattern to a regular
491 expression. Called by regexp_match and mask_rename. */
492 /* Shouldn't we support [a-fw] type wildcards as well ?? */
493 char *convert_pattern (char *pattern
, int match_type
, int do_group
)
497 int was_wildcard
= 0;
500 new_pattern
= g_malloc (MC_MAXPATHLEN
);
502 if (match_type
== match_file
)
504 for (s
= pattern
; *s
; s
++, d
++){
507 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
513 d
= maybe_start_group (d
, do_group
, &was_wildcard
);
518 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
524 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
529 d
= maybe_end_group (d
, do_group
, &was_wildcard
);
530 if (match_type
== match_file
)
535 return g_strdup (pattern
);
538 int regexp_match (char *pattern
, char *string
, int match_type
)
541 static char *old_pattern
= NULL
;
545 if (!old_pattern
|| STRCOMP (old_pattern
, pattern
) || old_type
!= match_type
){
548 g_free (old_pattern
);
551 pattern
= convert_pattern (pattern
, match_type
, 0);
552 if (regcomp (&r
, pattern
, REG_EXTENDED
|REG_NOSUB
|MC_ARCH_FLAGS
)) {
556 old_pattern
= pattern
;
557 old_type
= match_type
;
559 rval
= !regexec (&r
, string
, 0, NULL
, 0);
563 char *extension (char *filename
)
570 d
= filename
+ strlen (filename
) - 1;
571 for (;d
>= filename
; d
--){
578 int get_int (char *file
, char *key
, int def
)
580 return GetPrivateProfileInt (app_text
, key
, def
, file
);
583 int set_int (char *file
, char *key
, int value
)
585 char buffer
[BUF_TINY
];
587 g_snprintf (buffer
, sizeof (buffer
), "%d", value
);
588 return WritePrivateProfileString (app_text
, key
, buffer
, file
);
591 int exist_file (char *name
)
593 return access (name
, R_OK
) == 0;
596 char *load_file (char *filename
)
603 if ((data_file
= fopen (filename
, "r")) == NULL
){
606 if (fstat (fileno (data_file
), &s
) != 0){
610 data
= (char *) g_malloc (s
.st_size
+1);
611 read_size
= fread (data
, 1, s
.st_size
, data_file
);
612 data
[read_size
] = 0;
623 char *load_mc_home_file (const char *filename
, char ** allocated_filename
)
625 char *hintfile_base
, *hintfile
;
629 hintfile_base
= concat_dir_and_file (mc_home
, filename
);
630 lang
= guess_message_value ();
632 hintfile
= g_strdup_printf ("%s.%s", hintfile_base
, lang
);
633 data
= load_file (hintfile
);
637 hintfile
= g_strdup_printf ("%s.%.2s", hintfile_base
, lang
);
638 data
= load_file (hintfile
);
642 hintfile
= hintfile_base
;
643 data
= load_file (hintfile_base
);
649 if (hintfile
!= hintfile_base
)
650 g_free (hintfile_base
);
652 if (allocated_filename
)
653 *allocated_filename
= hintfile
;
660 /* Check strftime() results. Some systems (i.e. Solaris) have different
661 short-month-name sizes for different locales */
662 size_t i18n_checktimelength (void)
665 char buf
[MAX_I18NTIMELENGTH
+ 1];
666 time_t testtime
= time (NULL
);
668 a
= strftime (buf
, sizeof(buf
)-1, _("%b %e %H:%M"), localtime(&testtime
));
669 b
= strftime (buf
, sizeof(buf
)-1, _("%b %e %Y"), localtime(&testtime
));
673 /* Don't handle big differences. Use standard value (email bug, please) */
674 if ( length
> MAX_I18NTIMELENGTH
|| length
< MIN_I18NTIMELENGTH
)
675 length
= STD_I18NTIMELENGTH
;
680 char *file_date (time_t when
)
682 static char timebuf
[MAX_I18NTIMELENGTH
+ 1];
683 time_t current_time
= time ((time_t) 0);
684 static size_t i18n_timelength
= 0;
685 static char *fmtyear
, *fmttime
;
688 if (i18n_timelength
== 0){
689 i18n_timelength
= i18n_checktimelength() + 1;
691 /* strftime() format string for old dates */
692 fmtyear
= _("%b %e %Y");
693 /* strftime() format string for recent dates */
694 fmttime
= _("%b %e %H:%M");
697 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
698 || current_time
< when
- 60L * 60L) /* In the future. */
699 /* The file is fairly old or in the future.
700 POSIX says the cutoff is 6 months old;
701 approximate this by 6*30 days.
702 Allow a 1 hour slop factor for what is considered "the future",
703 to allow for NFS server/client clock disagreement.
704 Show the year instead of the time of day. */
710 strftime (timebuf
, i18n_timelength
, fmt
, localtime(&when
));
714 char *extract_line (char *s
, char *top
)
716 static char tmp_line
[BUF_MEDIUM
];
719 while (*s
&& *s
!= '\n' && (t
- tmp_line
) < sizeof (tmp_line
)-1 && s
< top
)
725 /* FIXME: I should write a faster version of this (Aho-Corasick stuff) */
726 char * _icase_search (char *text
, char *data
, int *lng
)
735 while (*(e
+1) == '\b' && *(e
+2)) {
739 if (toupper((unsigned char) *d
) == toupper((unsigned char) *e
))
748 *lng
= strlen (text
) + dlng
;
755 /* The basename routine */
756 char *x_basename (char *s
)
759 return ((where
= strrchr (s
, PATH_SEP
))) ? where
+ 1 : s
;
763 char *unix_error_string (int error_num
)
765 static char buffer
[BUF_LARGE
];
767 g_snprintf (buffer
, sizeof (buffer
), "%s (%d)",
768 g_strerror (error_num
), error_num
);
772 char *skip_separators (char *s
)
775 if (*s
!= ' ' && *s
!= '\t' && *s
!= ',')
780 char *skip_numbers (char *s
)
783 if (!isdigit ((unsigned int) *s
))
788 /* Remove all control sequences from the argument string. We define
789 * "control sequence", in a sort of pidgin BNF, as follows:
791 * control-seq = Esc non-'['
792 * | Esc '[' (0 or more digits or ';' or '?') (any other char)
794 * This scheme works for all the terminals described in my termcap /
795 * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
796 * terminals. If I hear from a single person who uses such a terminal
797 * with MC, I'll be glad to add support for it. (Dugan)
798 * Non-printable characters are also removed.
801 char *strip_ctrl_codes (char *s
)
803 char *w
; /* Current position where the stripped data is written */
804 char *r
; /* Current position where the original data is read */
809 for (w
= s
, r
= s
; *r
; ) {
810 if (*r
== ESC_CHAR
) {
811 /* Skip the control sequence's arguments */ ;
813 /* strchr() matches trailing binary 0 */
814 while (*(++r
) && strchr ("0123456789;?", *r
));
818 * Now we are at the last character of the sequence.
819 * Skip it unless it's binary 0.
826 if (is_printable(*r
))
836 char *get_current_wd (char *buffer
, int size
)
841 p
= g_get_current_dir ();
849 strncpy (buffer
, p
, len
);
854 #endif /* !USE_VFS */
856 /* This function returns 0 if the file is not in not compressed by
857 * one of the supported compressors (gzip, bzip, bzip2). Otherwise,
858 * the compression type is returned, as defined in util.h
859 * Warning: this function moves the current file pointer */
860 int get_compression_type (int fd
)
862 unsigned char magic
[4];
864 /* Read the magic signature */
865 if (mc_read (fd
, (char *) magic
, 4) != 4)
866 return COMPRESSION_NONE
;
868 /* GZIP_MAGIC and OLD_GZIP_MAGIC */
869 if (magic
[0] == 037 && (magic
[1] == 0213 || magic
[1] == 0236)) {
870 return COMPRESSION_GZIP
;
874 if (magic
[0] == 0120 && magic
[1] == 0113 && magic
[2] == 003
875 && magic
[3] == 004) {
876 /* Read compression type */
877 mc_lseek (fd
, 8, SEEK_SET
);
878 if (mc_read (fd
, (char *) magic
, 2) != 2)
879 return COMPRESSION_NONE
;
881 /* Gzip can handle only deflated (8) or stored (0) files */
882 if ((magic
[0] != 8 && magic
[0] != 0) || magic
[1] != 0)
883 return COMPRESSION_NONE
;
885 /* Compatible with gzip */
886 return COMPRESSION_GZIP
;
889 /* PACK_MAGIC and LZH_MAGIC and compress magic */
891 && (magic
[1] == 036 || magic
[1] == 0240 || magic
[1] == 0235)) {
892 /* Compatible with gzip */
893 return COMPRESSION_GZIP
;
896 /* BZIP and BZIP2 files */
897 if ((magic
[0] == 'B') && (magic
[1] == 'Z') &&
898 (magic
[3] >= '1') && (magic
[3] <= '9')) {
901 return COMPRESSION_BZIP
;
903 return COMPRESSION_BZIP2
;
910 decompress_extension (int type
)
913 case COMPRESSION_GZIP
: return "#ugz";
914 case COMPRESSION_BZIP
: return "#ubz";
915 case COMPRESSION_BZIP2
: return "#ubz2";
917 /* Should never reach this place */
918 fprintf (stderr
, "Fatal: decompress_extension called with an unknown argument\n");
923 void add_hook (Hook
**hook_list
, void (*hook_fn
)(void *), void *data
)
925 Hook
*new_hook
= g_new (Hook
, 1);
927 new_hook
->hook_fn
= hook_fn
;
928 new_hook
->next
= *hook_list
;
929 new_hook
->hook_data
= data
;
931 *hook_list
= new_hook
;
934 void execute_hooks (Hook
*hook_list
)
939 /* We copy the hook list first so tahat we let the hook
940 * function call delete_hook
944 add_hook (&new_hook
, hook_list
->hook_fn
, hook_list
->hook_data
);
945 hook_list
= hook_list
->next
;
950 (*new_hook
->hook_fn
)(new_hook
->hook_data
);
951 new_hook
= new_hook
->next
;
954 for (hook_list
= p
; hook_list
;){
956 hook_list
= hook_list
->next
;
961 void delete_hook (Hook
**hook_list
, void (*hook_fn
)(void *))
963 Hook
*current
, *new_list
, *next
;
967 for (current
= *hook_list
; current
; current
= next
){
968 next
= current
->next
;
969 if (current
->hook_fn
== hook_fn
)
972 add_hook (&new_list
, current
->hook_fn
, current
->hook_data
);
974 *hook_list
= new_list
;
977 int hook_present (Hook
*hook_list
, void (*hook_fn
)(void *))
981 for (p
= hook_list
; p
; p
= p
->next
)
982 if (p
->hook_fn
== hook_fn
)
987 void wipe_password (char *passwd
)
998 /* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */
999 /* Returns a newly allocated string */
1000 char *convert_controls (char *s
)
1002 char *valcopy
= g_strdup (s
);
1005 /* Parse the escape special character */
1006 for (p
= s
, q
= valcopy
; *p
;){
1009 if ((*p
== 'e') || (*p
== 'E')){
1020 if (*p
>= 'a' && *p
<= 'z') {
1021 *q
++ = *p
++ - 'a' + 1;
1033 static char *resolve_symlinks (char *path
)
1035 char *buf
, *buf2
, *p
, *q
, *r
, c
;
1039 if (*path
!= PATH_SEP
)
1041 r
= buf
= g_malloc (MC_MAXPATHLEN
);
1042 buf2
= g_malloc (MC_MAXPATHLEN
);
1047 q
= strchr (p
+ 1, PATH_SEP
);
1049 q
= strchr (p
+ 1, 0);
1055 if (mc_lstat (path
, &mybuf
) < 0) {
1061 if (!S_ISLNK (mybuf
.st_mode
))
1064 len
= mc_readlink (path
, buf2
, MC_MAXPATHLEN
);
1072 if (*buf2
== PATH_SEP
)
1077 canonicalize_pathname (buf
);
1078 r
= strchr (buf
, 0);
1079 if (!*r
|| *(r
- 1) != PATH_SEP
) {
1089 strcpy (buf
, PATH_SEP_STR
);
1090 else if (*(r
- 1) == PATH_SEP
&& r
!= buf
+ 1)
1096 /* Finds out a relative path from first to second, i.e. goes as many ..
1097 * as needed up in first and then goes down using second */
1098 char *diff_two_paths (char *first
, char *second
)
1100 char *p
, *q
, *r
, *s
, *buf
= 0;
1101 int i
, j
, prevlen
= -1, currlen
;
1103 first
= resolve_symlinks (first
);
1106 for (j
= 0; j
< 2; j
++) {
1109 second
= resolve_symlinks (second
);
1110 if (second
== NULL
) {
1117 r
= strchr (p
, PATH_SEP
);
1118 s
= strchr (q
, PATH_SEP
);
1122 if (strcmp (p
, q
)) {
1123 *r
= PATH_SEP
; *s
= PATH_SEP
;
1126 *r
= PATH_SEP
; *s
= PATH_SEP
;
1132 for (i
= 0; (p
= strchr (p
+ 1, PATH_SEP
)) != NULL
; i
++);
1133 currlen
= (i
+ 1) * 3 + strlen (q
) + 1;
1135 if (currlen
< prevlen
)
1143 p
= buf
= g_malloc (currlen
);
1145 for (; i
>= 0; i
--, p
+= 3)
1154 /* If filename is NULL, then we just append PATH_SEP to the dir */
1156 concat_dir_and_file (const char *dir
, const char *file
)
1158 int i
= strlen (dir
);
1160 if (dir
[i
-1] == PATH_SEP
)
1161 return g_strconcat (dir
, file
, NULL
);
1163 return g_strconcat (dir
, PATH_SEP_STR
, file
, NULL
);
1167 /* Append text to GList, remove all entries with the same text */
1169 list_append_unique (GList
*list
, char *text
)
1171 GList
*link
, *newlink
;
1174 * Go to the last position and traverse the list backwards
1175 * starting from the second last entry to make sure that we
1176 * are not removing the current link.
1178 list
= g_list_append (list
, text
);
1179 list
= g_list_last (list
);
1180 link
= g_list_previous (list
);
1183 newlink
= g_list_previous (link
);
1184 if (!strcmp ((char *) link
->data
, text
)) {
1185 g_list_remove_link (list
, link
);
1186 g_list_free_1 (link
);
1195 /* Following code heavily borrows from libiberty, mkstemps.c */
1197 /* Number of attempts to create a temporary file */
1199 #define TMP_MAX 16384
1200 #endif /* !TMP_MAX */
1204 * pname (output) - pointer to the name of the temp file (needs g_free).
1205 * NULL if the function fails.
1206 * prefix - part of the filename before the random part.
1207 * Prepend $TMPDIR or /tmp if there are no path separators.
1208 * suffix - if not NULL, part of the filename after the random part.
1211 * handle of the open file or -1 if couldn't open any.
1214 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
1216 static const char letters
[]
1217 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1218 static unsigned long value
;
1225 if (strchr (prefix
, PATH_SEP
) == NULL
) {
1226 /* Add prefix first to find the position of XXXXXX */
1227 tmpbase
= concat_dir_and_file (mc_tmpdir (), prefix
);
1229 tmpbase
= g_strdup (prefix
);
1232 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, NULL
);
1234 XXXXXX
= &tmpname
[strlen (tmpbase
)];
1237 /* Get some more or less random data. */
1238 gettimeofday (&tv
, NULL
);
1239 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
1241 for (count
= 0; count
< TMP_MAX
; ++count
) {
1242 unsigned long v
= value
;
1245 /* Fill in the random bits. */
1246 XXXXXX
[0] = letters
[v
% 62];
1248 XXXXXX
[1] = letters
[v
% 62];
1250 XXXXXX
[2] = letters
[v
% 62];
1252 XXXXXX
[3] = letters
[v
% 62];
1254 XXXXXX
[4] = letters
[v
% 62];
1256 XXXXXX
[5] = letters
[v
% 62];
1258 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
,
1261 /* Successfully created. */
1265 /* This is a random value. It is only necessary that the next
1266 TMP_MAX values generated by adding 7777 to VALUE are different
1267 with (module 2^32). */
1271 /* Unsuccessful. Free the filename. */
1280 * Read and restore position for the given filename.
1281 * If there is no stored data, return line 1 and col 0.
1284 load_file_position (char *filename
, long *line
, long *column
)
1288 char buf
[MC_MAXPATHLEN
+ 20];
1295 /* open file with positions */
1296 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1297 f
= fopen (fn
, "r");
1302 len
= strlen (filename
);
1304 while (fgets (buf
, sizeof (buf
), f
)) {
1307 /* check if the filename matches the beginning of string */
1308 if (strncmp (buf
, filename
, len
) != 0)
1311 /* followed by single space */
1312 if (buf
[len
] != ' ')
1315 /* and string without spaces */
1317 if (strchr (p
, ' '))
1321 p
= strchr (buf
, ';');
1322 *column
= atol (&p
[1]);
1327 /* Save position for the given file */
1329 save_file_position (char *filename
, long line
, long column
)
1333 char buf
[MC_MAXPATHLEN
+ 20];
1337 len
= strlen (filename
);
1339 tmp
= concat_dir_and_file (home_dir
, MC_FILEPOS_TMP
);
1340 fn
= concat_dir_and_file (home_dir
, MC_FILEPOS
);
1342 /* open temporary file */
1343 t
= fopen (tmp
, "w");
1350 /* put the new record */
1351 fprintf (t
, "%s %ld;%ld\n", filename
, line
, column
);
1353 /* copy records from the old file */
1354 f
= fopen (fn
, "r");
1356 while (fgets (buf
, sizeof (buf
), f
)) {
1357 /* Skip entries for the current filename */
1358 if (strncmp (buf
, filename
, len
) == 0 && buf
[len
] == ' '
1359 && !strchr (&buf
[len
+ 1], ' '))
1362 fprintf (t
, "%s", buf
);
1363 if (++i
> MC_FILEPOS_ENTRIES
)