(diff_two_paths): minor optimization: exit loop ASAP.
[midnight-commander.git] / lib / util.h
blob78c7ccbfd6231ea6fc22cb0e08a4c5aac6176407
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 /*** enums ***************************************************************************************/
43 /* Pathname canonicalization */
44 typedef enum
46 CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple '/'s are collapsed to a single '/'. */
47 CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading './'s, '/'s and trailing '/.'s are removed. */
48 CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading '../'s and trailing '..'s are handled by removing */
49 CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
50 CANON_PATH_ALL = CANON_PATH_JOINSLASHES
51 | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
52 } CANON_PATH_FLAGS;
54 enum compression_type
56 COMPRESSION_NONE,
57 COMPRESSION_GZIP,
58 COMPRESSION_BZIP,
59 COMPRESSION_BZIP2,
60 COMPRESSION_LZIP,
61 COMPRESSION_LZ4,
62 COMPRESSION_LZMA,
63 COMPRESSION_XZ
66 /* stdout or stderr stream of child process */
67 typedef struct
69 /* file descriptor */
70 int fd;
71 /* data read from fd */
72 char buf[MC_PIPE_BUFSIZE];
73 /* positive: length of data in buf as before read as after;
74 * zero or negative before read: do not read drom fd;
75 * MC_PIPE_STREAM_EOF after read: EOF of fd;
76 * MC_PIPE_STREAM_UNREAD after read: there was not read from fd;
77 * MC_PIPE_ERROR_READ after read: reading error from fd.
79 ssize_t len;
80 /* whether buf is null-terminated or not */
81 gboolean null_term;
82 /* error code in case of len == MC_PIPE_ERROR_READ */
83 int error;
84 } mc_pipe_stream_t;
86 /* Pipe descriptor for child process */
87 typedef struct
89 /* PID of child process */
90 GPid child_pid;
91 /* stdout of child process */
92 mc_pipe_stream_t out;
93 /* stderr of child process */
94 mc_pipe_stream_t err;
95 } mc_pipe_t;
97 /*** structures declarations (and typedefs of structures)*****************************************/
99 /* keys are set only during sorting */
100 typedef struct
102 /* File attributes */
103 size_t fnamelen;
104 char *fname;
105 struct stat st;
106 /* key used for comparing names */
107 char *sort_key;
108 /* key used for comparing extensions */
109 char *second_sort_key;
111 /* Flags */
112 struct
114 unsigned int marked:1; /* File marked in pane window */
115 unsigned int link_to_dir:1; /* If this is a link, does it point to directory? */
116 unsigned int stale_link:1; /* If this is a symlink and points to Charon's land */
117 unsigned int dir_size_computed:1; /* Size of directory was computed with dirsizes_cmd */
118 } f;
119 } file_entry_t;
121 /*** global variables defined in .c file *********************************************************/
123 extern struct sigaction startup_handler;
125 /*** declarations of public functions ************************************************************/
127 int is_printable (int c);
129 /* Quote the filename for the purpose of inserting it into the command
130 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
131 * processed by the mc command line. */
132 char *name_quote (const char *c, gboolean quote_percent);
134 /* returns a duplicate of c. */
135 char *fake_name_quote (const char *c, gboolean quote_percent);
137 /* path_trunc() is the same as str_trunc() but
138 * it deletes possible password from path for security
139 * reasons. */
140 const char *path_trunc (const char *path, size_t trunc_len);
142 /* return a static string representing size, appending "K" or "M" for
143 * big sizes.
144 * NOTE: uses the same static buffer as size_trunc_sep. */
145 const char *size_trunc (uintmax_t size, gboolean use_si);
147 /* return a static string representing size, appending "K" or "M" for
148 * big sizes. Separates every three digits by ",".
149 * NOTE: uses the same static buffer as size_trunc. */
150 const char *size_trunc_sep (uintmax_t size, gboolean use_si);
152 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
153 * not including trailing 0. BUFFER should be at least LEN+1 long.
155 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
156 void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
157 const char *string_perm (mode_t mode_bits);
159 const char *extension (const char *);
160 const char *unix_error_string (int error_num);
161 const char *skip_separators (const char *s);
162 const char *skip_numbers (const char *s);
163 char *strip_ctrl_codes (char *s);
165 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
166 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
167 * Returns a newly allocated string. */
168 char *convert_controls (const char *s);
170 /* overwrites passwd with '\0's and frees it. */
171 void wipe_password (char *passwd);
173 char *diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
175 /* Returns the basename of fname. The result is a pointer into fname. */
176 const char *x_basename (const char *fname);
178 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename,
179 size_t * length);
181 /* uid/gid managing */
182 void init_groups (void);
183 void destroy_groups (void);
184 int get_user_permissions (struct stat *buf);
186 void init_uid_gid_cache (void);
187 const char *get_group (gid_t gid);
188 const char *get_owner (uid_t uid);
190 /* Returns a copy of *s until a \n is found and is below top */
191 const char *extract_line (const char *s, const char *top);
193 /* Error pipes */
194 void open_error_pipe (void);
195 void check_error_pipe (void);
196 int close_error_pipe (int error, const char *text);
198 /* Process spawning */
199 int my_system (int flags, const char *shell, const char *command);
200 int my_systeml (int flags, const char *shell, ...);
201 int my_systemv (const char *command, char *const argv[]);
202 int my_systemv_flags (int flags, const char *command, char *const argv[]);
204 mc_pipe_t *mc_popen (const char *command, GError ** error);
205 void mc_pread (mc_pipe_t * p, GError ** error);
206 void mc_pclose (mc_pipe_t * p, GError ** error);
208 void my_exit (int status);
209 void save_stop_handler (void);
211 /* Tilde expansion */
212 char *tilde_expand (const char *);
214 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
215 void canonicalize_pathname (char *);
217 #ifdef HAVE_REALPATH
218 #define mc_realpath realpath
219 #else
220 char *mc_realpath (const char *path, char *resolved_path);
221 #endif
223 /* Looks for "magic" bytes at the start of the VFS file to guess the
224 * compression type. Side effect: modifies the file position. */
225 enum compression_type get_compression_type (int fd, const char *);
226 const char *decompress_extension (int type);
228 GList *list_append_unique (GList * list, char *text);
230 /* Position saving and restoring */
231 /* Load position for the given filename */
232 void load_file_position (const vfs_path_t * filename_vpath, long *line, long *column,
233 off_t * offset, GArray ** bookmarks);
234 /* Save position for the given filename */
235 void save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
236 GArray * bookmarks);
239 /* if ch is in [A-Za-z], returns the corresponding control character,
240 * else returns the argument. */
241 extern int ascii_alpha_to_cntrl (int ch);
243 #undef Q_
244 const char *Q_ (const char *s);
246 gboolean mc_util_make_backup_if_possible (const char *, const char *);
247 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
248 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
250 char *guess_message_value (void);
252 char *mc_build_filename (const char *first_element, ...);
253 char *mc_build_filenamev (const char *first_element, va_list args);
255 const char *mc_get_profile_root (void);
257 /* *INDENT-OFF* */
258 void mc_propagate_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
259 void mc_replace_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
260 /* *INDENT-ON* */
262 gboolean mc_time_elapsed (guint64 * timestamp, guint64 delay);
264 /*** inline functions **************************************************/
266 static inline gboolean
267 exist_file (const char *name)
269 return (access (name, R_OK) == 0);
272 static inline gboolean
273 is_exe (mode_t mode)
275 return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
278 #endif /* MC_UTIL_H */