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 int my_mkdir (const char *s
, mode_t mode
);
168 int my_rmdir (const char *s
);
170 /* Rotating dash routines */
171 void use_dash (int flag
); /* Disable/Enable rotate_dash routines */
172 void rotate_dash (void);
174 /* Creating temporary files safely */
175 const char *mc_tmpdir (void);
176 int mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
);
179 #ifdef _POSIX_VERSION
180 #define PATH_MAX _POSIX_PATH_MAX
183 #define PATH_MAX MAXPATHLEN
185 #define PATH_MAX 1024
191 #define MAXSYMLINKS 32
195 #define mc_realpath realpath
197 char *mc_realpath (const char *path
, char *resolved_path
);
200 enum compression_type
210 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
211 * compression type. Side effect: modifies the file position. */
212 enum compression_type
get_compression_type (int fd
, const char *);
213 const char *decompress_extension (int type
);
219 void (*hook_fn
) (void *);
224 void add_hook (Hook
** hook_list
, void (*hook_fn
) (void *), void *data
);
225 void execute_hooks (Hook
* hook_list
);
226 void delete_hook (Hook
** hook_list
, void (*hook_fn
) (void *));
227 int hook_present (Hook
* hook_list
, void (*hook_fn
) (void *));
229 GList
*list_append_unique (GList
* list
, char *text
);
231 /* Position saving and restoring */
232 #define MAX_SAVED_BOOKMARKS 10
233 /* Load position for the given filename */
234 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
,
241 /* OS specific defines */
243 #define PATH_SEP_STR "/"
244 #define PATH_ENV_SEP ':'
245 #define TMPDIR_DEFAULT "/tmp"
246 #define SCRIPT_SUFFIX ""
247 #define get_default_editor() "vi"
248 #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
249 #define STRCOMP strcmp
250 #define STRNCOMP strncmp
251 #define MC_ARCH_FLAGS 0
253 /* taken from regex.c: */
254 /* Jim Meyering writes:
256 "... Some ctype macros are valid only for character codes that
257 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
258 using /bin/cc or gcc but without giving an ansi option). So, all
259 ctype uses should be through macros like ISPRINT... If
260 STDC_HEADERS is defined, then autoconf has verified that the ctype
261 macros don't need to be guarded with references to isascii. ...
262 Defining isascii to 1 should let any compiler worth its salt
263 eliminate the && through constant folding." */
265 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
268 #define ISASCII(c) isascii(c)
271 /* usage: str_cmp ("foo", !=, "bar") */
272 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
274 /* if ch is in [A-Za-z], returns the corresponding control character,
275 * else returns the argument. */
276 extern int ascii_alpha_to_cntrl (int ch
);
279 const char *Q_ (const char *s
);
281 #define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))
284 * strcpy is unsafe on overlapping memory areas, so define memmove-alike
286 * Have sense only when:
289 * * dest and str are pointers to one object (as Roland Illig pointed).
291 * We can't use str*cpy funs here:
292 * http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
295 str_move (char *dest
, const char *src
)
299 assert (dest
<= src
);
301 n
= strlen (src
) + 1; /* + '\0' */
303 return (char *) memmove (dest
, src
, n
);
306 gboolean
mc_util_make_backup_if_possible (const char *, const char *);
307 gboolean
mc_util_restore_from_backup_if_possible (const char *, const char *);
308 gboolean
mc_util_unlink_backup_if_possible (const char *, const char *);
310 char *guess_message_value (void);
312 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
314 #endif /* MC_UTIL_H */