3 * \brief Header: various utilities
9 #include "lib/global.h" /* include <glib.h> */
13 #include <sys/types.h>
17 extern char *user_recent_timeformat
; /* time format string for recent dates */
18 extern char *user_old_timeformat
; /* time format string for older dates */
20 /* Returns its argument as a "modifiable" string. This function is
21 * intended to pass strings to legacy libraries that don't know yet
22 * about the "const" modifier. The return value of this function
23 * MUST NOT be modified. */
24 extern char *str_unconst (const char *);
26 /* String managing functions */
28 extern const char *cstrcasestr (const char *haystack
, const char *needle
);
29 extern const char *cstrstr (const char *haystack
, const char *needle
);
31 void str_replace (char *s
, char from
, char to
);
32 int is_printable (int c
);
33 void msglen (const char *text
, /*@out@ */ int *lines
, /*@out@ */ int *columns
);
35 /* Copy from s to d, and trim the beginning if necessary, and prepend
36 * "..." in this case. The destination string can have at most len
37 * bytes, not counting trailing 0. */
38 char *trim (const char *s
, char *d
, int len
);
40 /* Quote the filename for the purpose of inserting it into the command
41 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
42 * processed by the mc command line. */
43 char *name_quote (const char *c
, int quote_percent
);
45 /* returns a duplicate of c. */
46 char *fake_name_quote (const char *c
, int quote_percent
);
48 /* Remove the middle part of the string to fit given length.
49 * Use "~" to show where the string was truncated.
50 * Return static buffer, no need to free() it. */
51 const char *name_trunc (const char *txt
, size_t trunc_len
);
53 /* path_trunc() is the same as name_trunc() above but
54 * it deletes possible password from path for security
56 const char *path_trunc (const char *path
, size_t trunc_len
);
58 /* return a static string representing size, appending "K" or "M" for
60 * NOTE: uses the same static buffer as size_trunc_sep. */
61 const char *size_trunc (double size
, gboolean use_si
);
63 /* return a static string representing size, appending "K" or "M" for
64 * big sizes. Separates every three digits by ",".
65 * NOTE: uses the same static buffer as size_trunc. */
66 const char *size_trunc_sep (double size
, gboolean use_si
);
68 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
69 * not including trailing 0. BUFFER should be at least LEN+1 long.
71 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
72 void size_trunc_len (char *buffer
, unsigned int len
, off_t size
, int units
, gboolean use_si
);
73 int is_exe (mode_t mode
);
74 const char *string_perm (mode_t mode_bits
);
76 /* @modifies path. @returns pointer into path. */
77 char *strip_password (char *path
, int has_prefix
);
79 /* @returns a pointer into a static buffer. */
80 const char *strip_home_and_password (const char *dir
);
82 const char *extension (const char *);
83 char *concat_dir_and_file (const char *dir
, const char *file
);
84 const char *unix_error_string (int error_num
);
85 const char *skip_separators (const char *s
);
86 const char *skip_numbers (const char *s
);
87 char *strip_ctrl_codes (char *s
);
89 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
90 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
91 * Returns a newly allocated string. */
92 char *convert_controls (const char *s
);
94 /* overwrites passwd with '\0's and frees it. */
95 void wipe_password (char *passwd
);
97 char *diff_two_paths (const char *first
, const char *second
);
99 /* Returns the basename of fname. The result is a pointer into fname. */
100 const char *x_basename (const char *fname
);
102 char *load_file (const char *filename
);
103 char *load_mc_home_file (const char *, const char *, const char *filename
,
104 char **allocated_filename
);
106 /* uid/gid managing */
107 void init_groups (void);
108 void destroy_groups (void);
109 int get_user_permissions (struct stat
*buf
);
111 void init_uid_gid_cache (void);
112 char *get_group (int);
113 char *get_owner (int);
115 #define MAX_I18NTIMELENGTH 20
116 #define MIN_I18NTIMELENGTH 10
117 #define STD_I18NTIMELENGTH 12
119 size_t i18n_checktimelength (void);
120 const char *file_date (time_t);
122 int exist_file (const char *name
);
124 /* Check if the file exists. If not copy the default */
125 int check_for_default (const char *default_file
, const char *file
);
127 /* Returns a copy of *s until a \n is found and is below top */
128 const char *extract_line (const char *s
, const char *top
);
133 match_file
, /* match a filename, use easy_patterns */
134 match_normal
, /* match pattern, use easy_patterns */
135 match_regex
/* match pattern, force using regex */
138 extern int easy_patterns
;
141 void open_error_pipe (void);
142 void check_error_pipe (void);
143 int close_error_pipe (int error
, const char *text
);
145 /* Process spawning */
146 int my_system (int flags
, const char *shell
, const char *command
);
147 void save_stop_handler (void);
148 extern struct sigaction startup_handler
;
150 /* Tilde expansion */
151 char *tilde_expand (const char *);
153 /* Pathname canonicalization */
156 CANON_PATH_JOINSLASHES
= 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
157 CANON_PATH_REMSLASHDOTS
= 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
158 CANON_PATH_REMDOUBLEDOTS
= 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
159 CANON_PATH_GUARDUNC
= 1L << 4, /* Detect and preserve UNC paths: //server/... */
160 CANON_PATH_ALL
= CANON_PATH_JOINSLASHES
161 | CANON_PATH_REMSLASHDOTS
| CANON_PATH_REMDOUBLEDOTS
| CANON_PATH_GUARDUNC
163 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS
);
164 void canonicalize_pathname (char *);
166 /* Misc Unix functions */
167 char *get_current_wd (char *buffer
, int size
);
168 int my_mkdir (const char *s
, mode_t mode
);
169 int my_rmdir (const char *s
);
171 /* Rotating dash routines */
172 void use_dash (int flag
); /* Disable/Enable rotate_dash routines */
173 void rotate_dash (void);
175 /* Creating temporary files safely */
176 const char *mc_tmpdir (void);
177 int mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
);
180 #ifdef _POSIX_VERSION
181 #define PATH_MAX _POSIX_PATH_MAX
184 #define PATH_MAX MAXPATHLEN
186 #define PATH_MAX 1024
192 #define MAXSYMLINKS 32
196 #define mc_realpath realpath
198 char *mc_realpath (const char *path
, char *resolved_path
);
201 enum compression_type
211 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
212 * compression type. Side effect: modifies the file position. */
213 enum compression_type
get_compression_type (int fd
, const char *);
214 const char *decompress_extension (int type
);
220 void (*hook_fn
) (void *);
225 void add_hook (Hook
** hook_list
, void (*hook_fn
) (void *), void *data
);
226 void execute_hooks (Hook
* hook_list
);
227 void delete_hook (Hook
** hook_list
, void (*hook_fn
) (void *));
228 int hook_present (Hook
* hook_list
, void (*hook_fn
) (void *));
230 GList
*list_append_unique (GList
* list
, char *text
);
232 /* Position saving and restoring */
234 /* Load position for the given filename */
235 void load_file_position (const char *filename
, long *line
, long *column
, off_t
* offset
);
236 /* Save position for the given filename */
237 void save_file_position (const char *filename
, long line
, long column
, off_t offset
);
240 /* OS specific defines */
242 #define PATH_SEP_STR "/"
243 #define PATH_ENV_SEP ':'
244 #define TMPDIR_DEFAULT "/tmp"
245 #define SCRIPT_SUFFIX ""
246 #define get_default_editor() "vi"
247 #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
248 #define STRCOMP strcmp
249 #define STRNCOMP strncmp
250 #define MC_ARCH_FLAGS 0
252 /* taken from regex.c: */
253 /* Jim Meyering writes:
255 "... Some ctype macros are valid only for character codes that
256 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
257 using /bin/cc or gcc but without giving an ansi option). So, all
258 ctype uses should be through macros like ISPRINT... If
259 STDC_HEADERS is defined, then autoconf has verified that the ctype
260 macros don't need to be guarded with references to isascii. ...
261 Defining isascii to 1 should let any compiler worth its salt
262 eliminate the && through constant folding." */
264 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
267 #define ISASCII(c) isascii(c)
270 /* usage: str_cmp ("foo", !=, "bar") */
271 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
273 /* if ch is in [A-Za-z], returns the corresponding control character,
274 * else returns the argument. */
275 extern int ascii_alpha_to_cntrl (int ch
);
278 const char *Q_ (const char *s
);
280 #define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))
283 * strcpy is unsafe on overlapping memory areas, so define memmove-alike
285 * Have sense only when:
288 * * dest and str are pointers to one object (as Roland Illig pointed).
290 * We can't use str*cpy funs here:
291 * http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
294 str_move (char *dest
, const char *src
)
298 assert (dest
<= src
);
300 n
= strlen (src
) + 1; /* + '\0' */
302 return (char *) memmove (dest
, src
, n
);
305 gboolean
mc_util_make_backup_if_possible (const char *, const char *);
306 gboolean
mc_util_restore_from_backup_if_possible (const char *, const char *);
307 gboolean
mc_util_unlink_backup_if_possible (const char *, const char *);
309 char *guess_message_value (void);
311 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
313 #endif /* MC_UTIL_H */