Revert "Replace self-made code for work with ini-files to glib native code"
[midnight-commander.git] / src / util.h
blob76c72e91a4f9447e0e7a81a1b6f0e4fb6c41099e
2 /** \file util.h
3 * \brief Header: various utilities
4 */
6 #ifndef MC_UTIL_H
7 #define MC_UTIL_H
9 #include <sys/types.h>
10 #include <assert.h>
11 #include <string.h>
13 /* Returns its argument as a "modifiable" string. This function is
14 * intended to pass strings to legacy libraries that don't know yet
15 * about the "const" modifier. The return value of this function
16 * MUST NOT be modified. */
17 extern char *str_unconst (const char *);
19 /* String managing functions */
21 extern const char *cstrcasestr (const char *haystack, const char *needle);
22 extern const char *cstrstr (const char *haystack, const char *needle);
24 void str_replace(char *s, char from, char to);
25 int is_printable (int c);
26 void msglen (const char *text, /*@out@*/ int *lines, /*@out@*/ int *columns);
28 /* Copy from s to d, and trim the beginning if necessary, and prepend
29 * "..." in this case. The destination string can have at most len
30 * bytes, not counting trailing 0. */
31 char *trim (const char *s, char *d, int len);
33 /* Quote the filename for the purpose of inserting it into the command
34 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
35 * processed by the mc command line. */
36 char *name_quote (const char *c, int quote_percent);
38 /* returns a duplicate of c. */
39 char *fake_name_quote (const char *c, int quote_percent);
41 /* Remove the middle part of the string to fit given length.
42 * Use "~" to show where the string was truncated.
43 * Return static buffer, no need to free() it. */
44 const char *name_trunc (const char *txt, size_t trunc_len);
46 /* path_trunc() is the same as name_trunc() above but
47 * it deletes possible password from path for security
48 * reasons. */
49 const char *path_trunc (const char *path, size_t trunc_len);
51 /* return a static string representing size, appending "K" or "M" for
52 * big sizes.
53 * NOTE: uses the same static buffer as size_trunc_sep. */
54 const char *size_trunc (double size);
56 /* return a static string representing size, appending "K" or "M" for
57 * big sizes. Separates every three digits by ",".
58 * NOTE: uses the same static buffer as size_trunc. */
59 const char *size_trunc_sep (double size);
61 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
62 * not including trailing 0. BUFFER should be at least LEN+1 long.
64 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
65 void size_trunc_len (char *buffer, int len, off_t size, int units);
66 int is_exe (mode_t mode);
67 const char *string_perm (mode_t mode_bits);
69 /* @modifies path. @returns pointer into path. */
70 char *strip_password (char *path, int has_prefix);
72 /* @returns a pointer into a static buffer. */
73 const char *strip_home_and_password (const char *dir);
75 const char *extension (const char *);
76 char *concat_dir_and_file (const char *dir, const char *file);
77 const char *unix_error_string (int error_num);
78 const char *skip_separators (const char *s);
79 const char *skip_numbers (const char *s);
80 char *strip_ctrl_codes (char *s);
82 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
83 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
84 * Returns a newly allocated string. */
85 char *convert_controls (const char *s);
87 /* overwrites passwd with '\0's and frees it. */
88 void wipe_password (char *passwd);
90 char *diff_two_paths (const char *first, const char *second);
92 /* Returns the basename of fname. The result is a pointer into fname. */
93 const char *x_basename (const char *fname);
95 /* Profile managing functions */
96 int set_int (const char *, const char *, int);
97 int get_int (const char *, const char *, int);
98 extern char * get_config_string (const char *, const char *, const char *);
99 extern void set_config_string (const char *, const char *, const char *);
101 char *load_file (const char *filename);
102 char *load_mc_home_file (const char *filename, char ** allocated_filename);
104 /* uid/gid managing */
105 void init_groups (void);
106 void destroy_groups (void);
107 int get_user_permissions (struct stat *buf);
109 void init_uid_gid_cache (void);
110 char *get_group (int);
111 char *get_owner (int);
113 #define MAX_I18NTIMELENGTH 14
114 #define MIN_I18NTIMELENGTH 10
115 #define STD_I18NTIMELENGTH 12
117 size_t i18n_checktimelength (void);
118 const char *file_date (time_t);
120 int exist_file (const char *name);
122 /* Returns a copy of *s until a \n is found and is below top */
123 const char *extract_line (const char *s, const char *top);
125 /* Matching */
126 enum {
127 match_file, /* match a filename, use easy_patterns */
128 match_normal, /* match pattern, use easy_patterns */
129 match_regex /* match pattern, force using regex */
132 extern int easy_patterns;
134 /* Error pipes */
135 void open_error_pipe (void);
136 void check_error_pipe (void);
137 int close_error_pipe (int error, const char *text);
139 /* Process spawning */
140 int my_system (int flags, const char *shell, const char *command);
141 void save_stop_handler (void);
142 extern struct sigaction startup_handler;
144 /* Tilde expansion */
145 char *tilde_expand (const char *);
147 /* Pathname canonicalization */
148 void canonicalize_pathname (char *);
150 /* Misc Unix functions */
151 char *get_current_wd (char *buffer, int size);
152 int my_mkdir (const char *s, mode_t mode);
153 int my_rmdir (const char *s);
155 /* Rotating dash routines */
156 void use_dash (int flag); /* Disable/Enable rotate_dash routines */
157 void rotate_dash (void);
159 /* Creating temporary files safely */
160 const char *mc_tmpdir (void);
161 int mc_mkstemps(char **pname, const char *prefix, const char *suffix);
163 #ifndef PATH_MAX
164 #ifdef _POSIX_VERSION
165 #define PATH_MAX _POSIX_PATH_MAX
166 #else
167 #ifdef MAXPATHLEN
168 #define PATH_MAX MAXPATHLEN
169 #else
170 #define PATH_MAX 1024
171 #endif
172 #endif
173 #endif
175 #ifndef MAXSYMLINKS
176 #define MAXSYMLINKS 32
177 #endif
179 char *mc_realpath(const char *path, char resolved_path[]);
181 enum compression_type {
182 COMPRESSION_NONE,
183 COMPRESSION_GZIP,
184 COMPRESSION_BZIP,
185 COMPRESSION_BZIP2,
186 COMPRESSION_LZMA,
187 COMPRESSION_XZ
190 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
191 * compression type. Side effect: modifies the file position. */
192 enum compression_type get_compression_type (int fd);
193 const char *decompress_extension (int type);
195 /* Hook functions */
197 typedef struct hook {
198 void (*hook_fn)(void *);
199 void *hook_data;
200 struct hook *next;
201 } Hook;
203 void add_hook (Hook **hook_list, void (*hook_fn)(void *), void *data);
204 void execute_hooks (Hook *hook_list);
205 void delete_hook (Hook **hook_list, void (*hook_fn)(void *));
206 int hook_present (Hook *hook_list, void (*hook_fn)(void *));
208 GList *list_append_unique (GList *list, char *text);
210 /* Position saving and restoring */
212 /* file where positions are stored */
213 #define MC_FILEPOS ".mc/filepos"
214 /* temporary file */
215 #define MC_FILEPOS_TMP ".mc/filepos.tmp"
216 /* maximum entries in MC_FILEPOS */
217 #define MC_FILEPOS_ENTRIES 1024
218 /* Load position for the given filename */
219 void load_file_position (const char *filename, long *line, long *column);
220 /* Save position for the given filename */
221 void save_file_position (const char *filename, long line, long column);
224 /* OS specific defines */
225 #define PATH_SEP '/'
226 #define PATH_SEP_STR "/"
227 #define PATH_ENV_SEP ':'
228 #define TMPDIR_DEFAULT "/tmp"
229 #define SCRIPT_SUFFIX ""
230 #define get_default_editor() "vi"
231 #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
232 #define STRCOMP strcmp
233 #define STRNCOMP strncmp
234 #define MC_ARCH_FLAGS 0
236 /* taken from regex.c: */
237 /* Jim Meyering writes:
239 "... Some ctype macros are valid only for character codes that
240 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
241 using /bin/cc or gcc but without giving an ansi option). So, all
242 ctype uses should be through macros like ISPRINT... If
243 STDC_HEADERS is defined, then autoconf has verified that the ctype
244 macros don't need to be guarded with references to isascii. ...
245 Defining isascii to 1 should let any compiler worth its salt
246 eliminate the && through constant folding." */
248 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
249 #define ISASCII(c) 1
250 #else
251 #define ISASCII(c) isascii(c)
252 #endif
254 /* usage: str_cmp ("foo", !=, "bar") */
255 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
257 /* if ch is in [A-Za-z], returns the corresponding control character,
258 * else returns the argument. */
259 extern int ascii_alpha_to_cntrl (int ch);
261 #undef Q_
262 const char *Q_ (const char *s);
265 gboolean shell_is_char_escaped ( const char * );
266 char *shell_unescape( const char * );
267 char *shell_escape( const char * );
269 #define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))
272 * strcpy is unsafe on overlapping memory areas, so define memmove-alike
273 * string function.
274 * Have sense only when:
275 * * dest <= src
276 * AND
277 * * dest and str are pointers to one object (as Roland Illig pointed).
279 * We can't use str*cpy funs here:
280 * http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
282 static inline char * str_move(char * dest, const char * src)
284 size_t n;
286 assert (dest<=src);
288 n = strlen (src) + 1; /* + '\0' */
290 return memmove (dest, src, n);
293 #define MC_PTR_FREE(ptr) do { g_free(ptr); (ptr) = NULL; } while (0)
295 #endif