Replaced 6 with & as & where meant
[midnight-commander.git] / src / util.h
blob4e9a113ee5d7d481e2cebfa6dc9fd93143c18539
1 #ifndef MC_UTIL_H
2 #define MC_UTIL_H
4 #include <sys/types.h>
6 /* Returns its argument as a "modifiable" string. This function is
7 * intended to pass strings to legacy libraries that don't know yet
8 * about the "const" modifier. The return value of this function
9 * MUST NOT be modified. */
10 extern char *str_unconst (const char *);
12 /* String managing functions */
14 extern const char *cstrcasestr (const char *haystack, const char *needle);
15 extern const char *cstrstr (const char *haystack, const char *needle);
17 void str_replace(char *s, char from, char to);
18 int is_printable (int c);
19 void msglen (const char *text, /*@out@*/ int *lines, /*@out@*/ int *columns);
21 /* Copy from s to d, and trim the beginning if necessary, and prepend
22 * "..." in this case. The destination string can have at most len
23 * bytes, not counting trailing 0. */
24 char *trim (const char *s, char *d, int len);
26 /* Quote the filename for the purpose of inserting it into the command
27 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
28 * processed by the mc command line. */
29 char *name_quote (const char *c, int quote_percent);
31 /* returns a duplicate of c. */
32 char *fake_name_quote (const char *c, int quote_percent);
34 /* Remove the middle part of the string to fit given length.
35 * Use "~" to show where the string was truncated.
36 * Return static buffer, no need to free() it. */
37 const char *name_trunc (const char *txt, int trunc_len);
39 /* path_trunc() is the same as name_trunc() above but
40 * it deletes possible password from path for security
41 * reasons. */
42 const char *path_trunc (const char *path, int trunc_len);
44 /* return a static string representing size, appending "K" or "M" for
45 * big sizes.
46 * NOTE: uses the same static buffer as size_trunc_sep. */
47 const char *size_trunc (double size);
49 /* return a static string representing size, appending "K" or "M" for
50 * big sizes. Separates every three digits by ",".
51 * NOTE: uses the same static buffer as size_trunc. */
52 const char *size_trunc_sep (double size);
54 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
55 * not including trailing 0. BUFFER should be at least LEN+1 long.
57 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
58 void size_trunc_len (char *buffer, int len, off_t size, int units);
59 int is_exe (mode_t mode);
60 const char *string_perm (mode_t mode_bits);
62 /* @modifies path. @returns pointer into path. */
63 char *strip_password (char *path, int has_prefix);
65 /* @returns a pointer into a static buffer. */
66 const char *strip_home_and_password (const char *dir);
68 const char *extension (const char *);
69 char *concat_dir_and_file (const char *dir, const char *file);
70 const char *unix_error_string (int error_num);
71 const char *skip_separators (const char *s);
72 const char *skip_numbers (const char *s);
73 char *strip_ctrl_codes (char *s);
75 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
76 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
77 * Returns a newly allocated string. */
78 char *convert_controls (const char *s);
80 /* overwrites passwd with '\0's and frees it. */
81 void wipe_password (char *passwd);
83 char *diff_two_paths (const char *first, const char *second);
85 /* Returns the basename of fname. The result is a pointer into fname. */
86 const char *x_basename (const char *fname);
88 /* Profile managing functions */
89 int set_int (const char *, const char *, int);
90 int get_int (const char *, const char *, int);
91 extern char * get_config_string (const char *, const char *, const char *);
92 extern void set_config_string (const char *, const char *, const char *);
94 char *load_file (const char *filename);
95 char *load_mc_home_file (const char *filename, char ** allocated_filename);
97 /* uid/gid managing */
98 void init_groups (void);
99 void destroy_groups (void);
100 int get_user_permissions (struct stat *buf);
102 void init_uid_gid_cache (void);
103 char *get_group (int);
104 char *get_owner (int);
106 #define MAX_I18NTIMELENGTH 14
107 #define MIN_I18NTIMELENGTH 10
108 #define STD_I18NTIMELENGTH 12
110 size_t i18n_checktimelength (void);
111 const char *file_date (time_t);
113 int exist_file (const char *name);
115 /* Returns a copy of *s until a \n is found and is below top */
116 const char *extract_line (const char *s, const char *top);
117 const char *_icase_search (const char *text, const char *data, int *lng);
118 #define icase_search(T,D) _icase_search((T), (D), NULL)
120 /* Matching */
121 enum {
122 match_file, /* match a filename, use easy_patterns */
123 match_normal, /* match pattern, use easy_patterns */
124 match_regex /* match pattern, force using regex */
127 extern int easy_patterns;
128 char *convert_pattern (const char *pattern, int match_type, int do_group);
129 int regexp_match (const char *pattern, const char *string, int match_type);
131 /* Error pipes */
132 void open_error_pipe (void);
133 void check_error_pipe (void);
134 int close_error_pipe (int error, const char *text);
136 /* Process spawning */
137 int my_system (int flags, const char *shell, const char *command);
138 void save_stop_handler (void);
139 extern struct sigaction startup_handler;
141 /* Tilde expansion */
142 char *tilde_expand (const char *);
144 /* Pathname canonicalization */
145 void canonicalize_pathname (char *);
147 /* Misc Unix functions */
148 char *get_current_wd (char *buffer, int size);
149 int my_mkdir (const char *s, mode_t mode);
150 int my_rmdir (const char *s);
152 /* Rotating dash routines */
153 void use_dash (int flag); /* Disable/Enable rotate_dash routines */
154 void rotate_dash (void);
156 /* Creating temporary files safely */
157 const char *mc_tmpdir (void);
158 int mc_mkstemps(char **pname, const char *prefix, const char *suffix);
160 #ifndef PATH_MAX
161 #ifdef _POSIX_VERSION
162 #define PATH_MAX _POSIX_PATH_MAX
163 #else
164 #ifdef MAXPATHLEN
165 #define PATH_MAX MAXPATHLEN
166 #else
167 #define PATH_MAX 1024
168 #endif
169 #endif
170 #endif
172 #ifndef MAXSYMLINKS
173 #define MAXSYMLINKS 32
174 #endif
176 char *mc_realpath(const char *path, char resolved_path[]);
178 enum compression_type {
179 COMPRESSION_NONE,
180 COMPRESSION_GZIP,
181 COMPRESSION_BZIP,
182 COMPRESSION_BZIP2
185 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
186 * compression type. Side effect: modifies the file position. */
187 enum compression_type get_compression_type (int fd);
188 const char *decompress_extension (int type);
190 /* Hook functions */
192 typedef struct hook {
193 void (*hook_fn)(void *);
194 void *hook_data;
195 struct hook *next;
196 } Hook;
198 void add_hook (Hook **hook_list, void (*hook_fn)(void *), void *data);
199 void execute_hooks (Hook *hook_list);
200 void delete_hook (Hook **hook_list, void (*hook_fn)(void *));
201 int hook_present (Hook *hook_list, void (*hook_fn)(void *));
203 GList *list_append_unique (GList *list, char *text);
205 /* Position saving and restoring */
207 /* file where positions are stored */
208 #define MC_FILEPOS ".mc/filepos"
209 /* temporary file */
210 #define MC_FILEPOS_TMP ".mc/filepos.tmp"
211 /* maximum entries in MC_FILEPOS */
212 #define MC_FILEPOS_ENTRIES 1024
213 /* Load position for the given filename */
214 void load_file_position (const char *filename, long *line, long *column);
215 /* Save position for the given filename */
216 void save_file_position (const char *filename, long line, long column);
219 /* OS specific defines */
220 #define PATH_SEP '/'
221 #define PATH_SEP_STR "/"
222 #define PATH_ENV_SEP ':'
223 #define TMPDIR_DEFAULT "/tmp"
224 #define SCRIPT_SUFFIX ""
225 #define get_default_editor() "vi"
226 #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
227 #define STRCOMP strcmp
228 #define STRNCOMP strncmp
229 #define MC_ARCH_FLAGS 0
231 /* taken from regex.c: */
232 /* Jim Meyering writes:
234 "... Some ctype macros are valid only for character codes that
235 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
236 using /bin/cc or gcc but without giving an ansi option). So, all
237 ctype uses should be through macros like ISPRINT... If
238 STDC_HEADERS is defined, then autoconf has verified that the ctype
239 macros don't need to be guarded with references to isascii. ...
240 Defining isascii to 1 should let any compiler worth its salt
241 eliminate the && through constant folding." */
243 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
244 #define ISASCII(c) 1
245 #else
246 #define ISASCII(c) isascii(c)
247 #endif
249 /* usage: str_cmp ("foo", !=, "bar") */
250 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
252 /* if ch is in [A-Za-z], returns the corresponding control character,
253 * else returns the argument. */
254 extern int ascii_alpha_to_cntrl (int ch);
256 #undef Q_
257 const char *Q_ (const char *s);
259 #endif