Calculate pauses in file operations.
[midnight-commander.git] / lib / util.h
blob4486983164cce318b3c74fbd0bb546b590d90b21
1 /** \file lib/util.h
2 * \brief Header: various utilities
3 */
5 #ifndef MC_UTIL_H
6 #define MC_UTIL_H
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <inttypes.h> /* uintmax_t */
11 #include <unistd.h>
13 #include "lib/global.h" /* include <glib.h> */
15 #include "lib/vfs/vfs.h"
17 /*** typedefs(not structures) and defined constants **********************************************/
19 #ifndef MAXSYMLINKS
20 #define MAXSYMLINKS 32
21 #endif
23 #define MAX_SAVED_BOOKMARKS 10
25 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
27 #define mc_return_if_error(mcerror) do { if (mcerror != NULL && *mcerror != NULL) return; } while (0)
28 #define mc_return_val_if_error(mcerror, mcvalue) do { if (mcerror != NULL && *mcerror != NULL) return mcvalue; } while (0)
30 #define whitespace(c) ((c) == ' ' || (c) == '\t')
31 #define whiteness(c) (whitespace (c) || (c) == '\n')
33 #define MC_PIPE_BUFSIZE BUF_8K
34 #define MC_PIPE_STREAM_EOF 0
35 #define MC_PIPE_STREAM_UNREAD -1
36 #define MC_PIPE_ERROR_CREATE_PIPE -2
37 #define MC_PIPE_ERROR_PARSE_COMMAND -3
38 #define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4
39 #define MC_PIPE_ERROR_READ -5
41 /* gnulib efa15594e17fc20827dba66414fb391e99905394
43 *_GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2.
44 * It returns
45 * 1 if n1 > n2
46 * 0 if n1 == n2
47 * -1 if n1 < n2
48 * The native code (n1 > n2 ? 1 : n1 < n2 ? -1 : 0) produces a conditional
49 * jump with nearly all GCC versions up to GCC 10.
50 * This variant (n1 < n2 ? -1 : n1 > n2) produces a conditional with many
51 * GCC versions up to GCC 9.
52 * The better code (n1 > n2) - (n1 < n2) from Hacker's Delight para 2-9
53 * avoids conditional jumps in all GCC versions >= 3.4.
55 #define _GL_CMP(n1, n2) (((n1) > (n2)) - ((n1) < (n2)))
57 /* Difference or zero */
58 #define DOZ(a, b) ((a) > (b) ? (a) - (b) : 0)
60 /* flags for shell_execute */
61 #define EXECUTE_INTERNAL (1 << 0)
62 #define EXECUTE_AS_SHELL (1 << 2)
63 #define EXECUTE_HIDE (1 << 3)
65 /*** enums ***************************************************************************************/
67 /* Pathname canonicalization */
68 /* *INDENT-OFF* */
69 typedef enum
71 CANON_PATH_NOCHANGE = 0,
72 CANON_PATH_JOINSLASHES = 1L << 0, /**< Multiple '/'s are collapsed to a single '/' */
73 CANON_PATH_REMSLASHDOTS = 1L << 1, /**< Leading './'s, '/'s and trailing '/.'s are removed */
74 CANON_PATH_REMDOUBLEDOTS = 1L << 3, /**< Non-leading '../'s and trailing '..'s are handled by removing
75 portions of the path */
76 CANON_PATH_GUARDUNC = 1L << 4, /**< Detect and preserve UNC paths: //server/... */
77 CANON_PATH_ALL = CANON_PATH_JOINSLASHES | CANON_PATH_REMSLASHDOTS
78 | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC /**< All flags */
79 } canon_path_flags_t;
80 /* *INDENT-ON* */
82 enum compression_type
84 COMPRESSION_NONE,
85 COMPRESSION_ZIP,
86 COMPRESSION_GZIP,
87 COMPRESSION_BZIP,
88 COMPRESSION_BZIP2,
89 COMPRESSION_LZIP,
90 COMPRESSION_LZ4,
91 COMPRESSION_LZMA,
92 COMPRESSION_LZO,
93 COMPRESSION_XZ,
94 COMPRESSION_ZSTD,
97 /* stdout or stderr stream of child process */
98 typedef struct
100 /* file descriptor */
101 int fd;
102 /* data read from fd */
103 char buf[MC_PIPE_BUFSIZE];
104 /* current position in @buf (used by mc_pstream_get_string()) */
105 size_t pos;
106 /* positive: length of data in buf;
107 * MC_PIPE_STREAM_EOF: EOF of fd;
108 * MC_PIPE_STREAM_UNREAD: there was not read from fd;
109 * MC_PIPE_ERROR_READ: reading error from fd.
111 ssize_t len;
112 /* whether buf is null-terminated or not */
113 gboolean null_term;
114 /* error code in case of len == MC_PIPE_ERROR_READ */
115 int error;
116 } mc_pipe_stream_t;
118 /* Pipe descriptor for child process */
119 typedef struct
121 /* PID of child process */
122 GPid child_pid;
123 /* stdout of child process */
124 mc_pipe_stream_t out;
125 /* stderr of child process */
126 mc_pipe_stream_t err;
127 } mc_pipe_t;
129 /* sighandler_t is GNU extension */
130 #ifndef HAVE_SIGHANDLER_T
131 typedef void (*sighandler_t) (int);
132 #endif
134 /*** structures declarations (and typedefs of structures)*****************************************/
136 /*** global variables defined in .c file *********************************************************/
138 extern struct sigaction startup_handler;
140 /*** declarations of public functions ************************************************************/
142 int is_printable (int c);
144 /* Quote the filename for the purpose of inserting it into the command
145 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
146 * processed by the mc command line. */
147 char *name_quote (const char *c, gboolean quote_percent);
149 /* returns a duplicate of c. */
150 char *fake_name_quote (const char *c, gboolean quote_percent);
152 /* path_trunc() is the same as str_trunc() but
153 * it deletes possible password from path for security
154 * reasons. */
155 const char *path_trunc (const char *path, size_t trunc_len);
157 /* return a static string representing size, appending "K" or "M" for
158 * big sizes.
159 * NOTE: uses the same static buffer as size_trunc_sep. */
160 const char *size_trunc (uintmax_t size, gboolean use_si);
162 /* return a static string representing size, appending "K" or "M" for
163 * big sizes. Separates every three digits by ",".
164 * NOTE: uses the same static buffer as size_trunc. */
165 const char *size_trunc_sep (uintmax_t size, gboolean use_si);
167 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
168 * not including trailing 0. BUFFER should be at least LEN+1 long.
170 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
171 void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
172 const char *string_perm (mode_t mode_bits);
174 const char *extension (const char *);
175 const char *unix_error_string (int error_num);
176 const char *skip_separators (const char *s);
177 const char *skip_numbers (const char *s);
178 char *strip_ctrl_codes (char *s);
180 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
181 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
182 * Returns a newly allocated string. */
183 char *convert_controls (const char *s);
185 /* overwrites passwd with '\0's and frees it. */
186 void wipe_password (char *passwd);
188 char *diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
190 /* Returns the basename of fname. The result is a pointer into fname. */
191 const char *x_basename (const char *fname);
193 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename,
194 size_t *length);
196 /* uid/gid managing */
197 void init_groups (void);
198 void destroy_groups (void);
199 int get_user_permissions (struct stat *buf);
201 void init_uid_gid_cache (void);
202 const char *get_group (gid_t gid);
203 const char *get_owner (uid_t uid);
205 /* Returns a copy of *s until a \n is found and is below top */
206 const char *extract_line (const char *s, const char *top, size_t *len);
208 /* System call wrappers */
209 MC_MOCKABLE sighandler_t my_signal (int signum, sighandler_t handler);
210 MC_MOCKABLE int my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact);
211 MC_MOCKABLE pid_t my_fork (void);
212 MC_MOCKABLE int my_execvp (const char *file, char *const argv[]);
213 MC_MOCKABLE char *my_get_current_dir (void);
215 /* Process spawning */
216 int my_system (int flags, const char *shell, const char *command);
217 int my_systeml (int flags, const char *shell, ...);
218 int my_systemv (const char *command, char *const argv[]);
219 int my_systemv_flags (int flags, const char *command, char *const argv[]);
221 mc_pipe_t *mc_popen (const char *command, gboolean read_out, gboolean read_err, GError ** error);
222 void mc_pread (mc_pipe_t * p, GError ** error);
223 void mc_pclose (mc_pipe_t * p, GError ** error);
225 GString *mc_pstream_get_string (mc_pipe_stream_t * ps);
227 MC_MOCKABLE void my_exit (int status);
228 void save_stop_handler (void);
230 /* Tilde expansion */
231 char *tilde_expand (const char *directory);
233 void canonicalize_pathname_custom (char *path, canon_path_flags_t flags);
235 char *mc_realpath (const char *path, char *resolved_path);
237 /* Looks for "magic" bytes at the start of the VFS file to guess the
238 * compression type. Side effect: modifies the file position. */
239 enum compression_type get_compression_type (int fd, const char *name);
240 const char *decompress_extension (int type);
242 GList *list_append_unique (GList * list, char *text);
244 /* Position saving and restoring */
245 /* Load position for the given filename */
246 void load_file_position (const vfs_path_t * filename_vpath, long *line, long *column,
247 off_t * offset, GArray ** bookmarks);
248 /* Save position for the given filename */
249 void save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
250 GArray * bookmarks);
253 /* if ch is in [A-Za-z], returns the corresponding control character,
254 * else returns the argument. */
255 extern int ascii_alpha_to_cntrl (int ch);
257 #undef Q_
258 const char *Q_ (const char *s);
260 gboolean mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix);
261 gboolean mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix);
262 gboolean mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix);
264 MC_MOCKABLE char *guess_message_value (void);
266 char *mc_build_filename (const char *first_element, ...);
267 char *mc_build_filenamev (const char *first_element, va_list args);
269 const char *mc_get_profile_root (void);
271 /* *INDENT-OFF* */
272 void mc_propagate_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
273 void mc_replace_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
274 /* *INDENT-ON* */
276 gboolean mc_time_elapsed (gint64 * timestamp, gint64 delay);
278 /* --------------------------------------------------------------------------------------------- */
279 /*** inline functions ****************************************************************************/
280 /* --------------------------------------------------------------------------------------------- */
282 static inline gboolean
283 exist_file (const char *name)
285 return (access (name, R_OK) == 0);
288 /* --------------------------------------------------------------------------------------------- */
290 static inline gboolean
291 is_exe (mode_t mode)
293 return ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0);
296 /* --------------------------------------------------------------------------------------------- */
298 * Canonicalize path with CANON_PATH_ALL.
300 * @param path path to file
301 * @param flags canonicalization flags
303 * All modifications of @path are made in place.
304 * Well formed UNC paths are modified only in the local part.
307 static inline void
308 canonicalize_pathname (char *path)
310 canonicalize_pathname_custom (path, CANON_PATH_ALL);
313 /* --------------------------------------------------------------------------------------------- */
315 #endif /* MC_UTIL_H */