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