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