OS-specific defines are moved from lib/util.h into lib/global.h.
[midnight-commander/osp/fridrivaxxx.git] / lib / util.h
blob26aca9299db5b64955c7650c3f6f67c6e9613c98
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 /* taken from regex.c: */
23 /* Jim Meyering writes:
25 "... Some ctype macros are valid only for character codes that
26 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
27 using /bin/cc or gcc but without giving an ansi option). So, all
28 ctype uses should be through macros like ISPRINT... If
29 STDC_HEADERS is defined, then autoconf has verified that the ctype
30 macros don't need to be guarded with references to isascii. ...
31 Defining isascii to 1 should let any compiler worth its salt
32 eliminate the && through constant folding." */
34 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
35 #define ISASCII(c) 1
36 #else
37 #define ISASCII(c) isascii(c)
38 #endif
40 /* usage: str_cmp ("foo", !=, "bar") */
41 #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
43 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
45 /*** enums ***************************************************************************************/
47 /* Pathname canonicalization */
48 typedef enum
50 CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
51 CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
52 CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
53 CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
54 CANON_PATH_ALL = CANON_PATH_JOINSLASHES
55 | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
56 } CANON_PATH_FLAGS;
58 enum compression_type
60 COMPRESSION_NONE,
61 COMPRESSION_GZIP,
62 COMPRESSION_BZIP,
63 COMPRESSION_BZIP2,
64 COMPRESSION_LZMA,
65 COMPRESSION_XZ
68 /*** structures declarations (and typedefs of structures)*****************************************/
70 /*** global variables defined in .c file *********************************************************/
72 extern struct sigaction startup_handler;
74 /*** declarations of public functions ************************************************************/
76 int is_printable (int c);
77 void msglen (const char *text, /*@out@ */ int *lines, /*@out@ */ int *columns);
79 /* Quote the filename for the purpose of inserting it into the command
80 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
81 * processed by the mc command line. */
82 char *name_quote (const char *c, int quote_percent);
84 /* returns a duplicate of c. */
85 char *fake_name_quote (const char *c, int quote_percent);
87 /* path_trunc() is the same as str_trunc() but
88 * it deletes possible password from path for security
89 * reasons. */
90 const char *path_trunc (const char *path, size_t trunc_len);
92 /* return a static string representing size, appending "K" or "M" for
93 * big sizes.
94 * NOTE: uses the same static buffer as size_trunc_sep. */
95 const char *size_trunc (double size, gboolean use_si);
97 /* return a static string representing size, appending "K" or "M" for
98 * big sizes. Separates every three digits by ",".
99 * NOTE: uses the same static buffer as size_trunc. */
100 const char *size_trunc_sep (double size, gboolean use_si);
102 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
103 * not including trailing 0. BUFFER should be at least LEN+1 long.
105 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
106 void size_trunc_len (char *buffer, unsigned int len, off_t size, int units, gboolean use_si);
107 const char *string_perm (mode_t mode_bits);
109 /* @modifies path. @returns pointer into path. */
110 char *strip_password (char *path, int has_prefix);
112 /* @returns a pointer into a static buffer. */
113 const char *strip_home_and_password (const char *dir);
115 const char *extension (const char *);
116 char *concat_dir_and_file (const char *dir, const char *file);
117 const char *unix_error_string (int error_num);
118 const char *skip_separators (const char *s);
119 const char *skip_numbers (const char *s);
120 char *strip_ctrl_codes (char *s);
122 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
123 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
124 * Returns a newly allocated string. */
125 char *convert_controls (const char *s);
127 /* overwrites passwd with '\0's and frees it. */
128 void wipe_password (char *passwd);
130 char *diff_two_paths (const char *first, const char *second);
132 /* Returns the basename of fname. The result is a pointer into fname. */
133 const char *x_basename (const char *fname);
135 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
137 /* uid/gid managing */
138 void init_groups (void);
139 void destroy_groups (void);
140 int get_user_permissions (struct stat *buf);
142 void init_uid_gid_cache (void);
143 char *get_group (int);
144 char *get_owner (int);
146 /* Check if the file exists. If not copy the default */
147 int check_for_default (const char *default_file, const char *file);
149 /* Returns a copy of *s until a \n is found and is below top */
150 const char *extract_line (const char *s, const char *top);
152 /* Error pipes */
153 void open_error_pipe (void);
154 void check_error_pipe (void);
155 int close_error_pipe (int error, const char *text);
157 /* Process spawning */
158 int my_system (int flags, const char *shell, const char *command);
159 void save_stop_handler (void);
161 /* Tilde expansion */
162 char *tilde_expand (const char *);
164 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
165 void canonicalize_pathname (char *);
167 /* Misc Unix functions */
168 int my_mkdir (const char *s, mode_t mode);
169 int my_rmdir (const char *s);
171 /* Creating temporary files safely */
172 const char *mc_tmpdir (void);
173 int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
175 #ifdef HAVE_REALPATH
176 #define mc_realpath realpath
177 #else
178 char *mc_realpath (const char *path, char *resolved_path);
179 #endif
181 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
182 * compression type. Side effect: modifies the file position. */
183 enum compression_type get_compression_type (int fd, const char *);
184 const char *decompress_extension (int type);
186 GList *list_append_unique (GList * list, char *text);
188 /* Position saving and restoring */
189 /* Load position for the given filename */
190 void load_file_position (const char *filename, long *line, long *column, off_t * offset,
191 GArray **bookmarks);
192 /* Save position for the given filename */
193 void save_file_position (const char *filename, long line, long column, off_t offset,
194 GArray *bookmarks);
197 /* if ch is in [A-Za-z], returns the corresponding control character,
198 * else returns the argument. */
199 extern int ascii_alpha_to_cntrl (int ch);
201 #undef Q_
202 const char *Q_ (const char *s);
204 gboolean mc_util_make_backup_if_possible (const char *, const char *);
205 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
206 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
208 char *guess_message_value (void);
210 /*** inline functions **************************************************/
212 static inline gboolean
213 exist_file (const char *name)
215 return (access (name, R_OK) == 0);
218 static inline gboolean
219 is_exe (mode_t mode)
221 return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
224 #endif /* MC_UTIL_H */