Moved string-related routines from lib/util.[ch] into lib/strutil.
[midnight-commander/osp/fridrivaxxx.git] / lib / util.h
blob4d9df18cdf005ab6e698aa7539d168d25328e357
1 /** \file util.h
2 * \brief Header: various utilities
3 */
5 #ifndef MC_UTIL_H
6 #define MC_UTIL_H
8 #include "lib/global.h" /* include <glib.h> */
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
14 /*** typedefs(not structures) and defined constants **********************************************/
16 #ifndef MAXSYMLINKS
17 #define MAXSYMLINKS 32
18 #endif
20 #define MAX_SAVED_BOOKMARKS 10
22 /* OS specific defines */
23 #define PATH_SEP '/'
24 #define PATH_SEP_STR "/"
25 #define PATH_ENV_SEP ':'
26 #define TMPDIR_DEFAULT "/tmp"
27 #define SCRIPT_SUFFIX ""
28 #define get_default_editor() "vi"
29 #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
30 #define STRCOMP strcmp
31 #define STRNCOMP strncmp
32 #define MC_ARCH_FLAGS 0
34 /* taken from regex.c: */
35 /* Jim Meyering writes:
37 "... Some ctype macros are valid only for character codes that
38 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
39 using /bin/cc or gcc but without giving an ansi option). So, all
40 ctype uses should be through macros like ISPRINT... If
41 STDC_HEADERS is defined, then autoconf has verified that the ctype
42 macros don't need to be guarded with references to isascii. ...
43 Defining isascii to 1 should let any compiler worth its salt
44 eliminate the && through constant folding." */
46 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
47 #define ISASCII(c) 1
48 #else
49 #define ISASCII(c) isascii(c)
50 #endif
52 /* usage: str_cmp ("foo", !=, "bar") */
53 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
55 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
57 /*** enums ***************************************************************************************/
59 /* Pathname canonicalization */
60 typedef enum
62 CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
63 CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
64 CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
65 CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
66 CANON_PATH_ALL = CANON_PATH_JOINSLASHES
67 | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
68 } CANON_PATH_FLAGS;
70 enum compression_type
72 COMPRESSION_NONE,
73 COMPRESSION_GZIP,
74 COMPRESSION_BZIP,
75 COMPRESSION_BZIP2,
76 COMPRESSION_LZMA,
77 COMPRESSION_XZ
80 /*** structures declarations (and typedefs of structures)*****************************************/
82 /*** global variables defined in .c file *********************************************************/
84 extern struct sigaction startup_handler;
86 /*** declarations of public functions ************************************************************/
88 int is_printable (int c);
89 void msglen (const char *text, /*@out@ */ int *lines, /*@out@ */ int *columns);
91 /* Quote the filename for the purpose of inserting it into the command
92 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
93 * processed by the mc command line. */
94 char *name_quote (const char *c, int quote_percent);
96 /* returns a duplicate of c. */
97 char *fake_name_quote (const char *c, int quote_percent);
99 /* path_trunc() is the same as str_trunc() but
100 * it deletes possible password from path for security
101 * reasons. */
102 const char *path_trunc (const char *path, size_t trunc_len);
104 /* return a static string representing size, appending "K" or "M" for
105 * big sizes.
106 * NOTE: uses the same static buffer as size_trunc_sep. */
107 const char *size_trunc (double size, gboolean use_si);
109 /* return a static string representing size, appending "K" or "M" for
110 * big sizes. Separates every three digits by ",".
111 * NOTE: uses the same static buffer as size_trunc. */
112 const char *size_trunc_sep (double size, gboolean use_si);
114 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
115 * not including trailing 0. BUFFER should be at least LEN+1 long.
117 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
118 void size_trunc_len (char *buffer, unsigned int len, off_t size, int units, gboolean use_si);
119 int is_exe (mode_t mode);
120 const char *string_perm (mode_t mode_bits);
122 /* @modifies path. @returns pointer into path. */
123 char *strip_password (char *path, int has_prefix);
125 /* @returns a pointer into a static buffer. */
126 const char *strip_home_and_password (const char *dir);
128 const char *extension (const char *);
129 char *concat_dir_and_file (const char *dir, const char *file);
130 const char *unix_error_string (int error_num);
131 const char *skip_separators (const char *s);
132 const char *skip_numbers (const char *s);
133 char *strip_ctrl_codes (char *s);
135 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
136 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
137 * Returns a newly allocated string. */
138 char *convert_controls (const char *s);
140 /* overwrites passwd with '\0's and frees it. */
141 void wipe_password (char *passwd);
143 char *diff_two_paths (const char *first, const char *second);
145 /* Returns the basename of fname. The result is a pointer into fname. */
146 const char *x_basename (const char *fname);
148 char *load_file (const char *filename);
149 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
151 /* uid/gid managing */
152 void init_groups (void);
153 void destroy_groups (void);
154 int get_user_permissions (struct stat *buf);
156 void init_uid_gid_cache (void);
157 char *get_group (int);
158 char *get_owner (int);
160 int exist_file (const char *name);
162 /* Check if the file exists. If not copy the default */
163 int check_for_default (const char *default_file, const char *file);
165 /* Returns a copy of *s until a \n is found and is below top */
166 const char *extract_line (const char *s, const char *top);
168 /* Error pipes */
169 void open_error_pipe (void);
170 void check_error_pipe (void);
171 int close_error_pipe (int error, const char *text);
173 /* Process spawning */
174 int my_system (int flags, const char *shell, const char *command);
175 void save_stop_handler (void);
177 /* Tilde expansion */
178 char *tilde_expand (const char *);
180 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
181 void canonicalize_pathname (char *);
183 /* Misc Unix functions */
184 int my_mkdir (const char *s, mode_t mode);
185 int my_rmdir (const char *s);
187 /* Creating temporary files safely */
188 const char *mc_tmpdir (void);
189 int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
191 #ifdef HAVE_REALPATH
192 #define mc_realpath realpath
193 #else
194 char *mc_realpath (const char *path, char *resolved_path);
195 #endif
197 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
198 * compression type. Side effect: modifies the file position. */
199 enum compression_type get_compression_type (int fd, const char *);
200 const char *decompress_extension (int type);
202 GList *list_append_unique (GList * list, char *text);
204 /* Position saving and restoring */
205 /* Load position for the given filename */
206 void load_file_position (const char *filename, long *line, long *column, off_t * offset,
207 GArray **bookmarks);
208 /* Save position for the given filename */
209 void save_file_position (const char *filename, long line, long column, off_t offset,
210 GArray *bookmarks);
213 /* if ch is in [A-Za-z], returns the corresponding control character,
214 * else returns the argument. */
215 extern int ascii_alpha_to_cntrl (int ch);
217 #undef Q_
218 const char *Q_ (const char *s);
220 gboolean mc_util_make_backup_if_possible (const char *, const char *);
221 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
222 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
224 char *guess_message_value (void);
226 /*** inline functions **************************************************/
228 #endif /* MC_UTIL_H */