2 * \brief Header: various utilities
10 #include <inttypes.h> /* uintmax_t */
13 #include "lib/global.h" /* include <glib.h> */
15 #include "lib/vfs/vfs.h"
17 /*** typedefs(not structures) and defined constants **********************************************/
20 #define MAXSYMLINKS 32
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 MC_PIPE_BUFSIZE BUF_8K
31 #define MC_PIPE_STREAM_EOF 0
32 #define MC_PIPE_STREAM_UNREAD -1
33 #define MC_PIPE_ERROR_CREATE_PIPE -2
34 #define MC_PIPE_ERROR_PARSE_COMMAND -3
35 #define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4
36 #define MC_PIPE_ERROR_READ -5
38 /*** enums ***************************************************************************************/
40 /* Pathname canonicalization */
43 CANON_PATH_JOINSLASHES
= 1L << 0, /* Multiple '/'s are collapsed to a single '/'. */
44 CANON_PATH_REMSLASHDOTS
= 1L << 1, /* Leading './'s, '/'s and trailing '/.'s are removed. */
45 CANON_PATH_REMDOUBLEDOTS
= 1L << 3, /* Non-leading '../'s and trailing '..'s are handled by removing */
46 CANON_PATH_GUARDUNC
= 1L << 4, /* Detect and preserve UNC paths: //server/... */
47 CANON_PATH_ALL
= CANON_PATH_JOINSLASHES
48 | CANON_PATH_REMSLASHDOTS
| CANON_PATH_REMDOUBLEDOTS
| CANON_PATH_GUARDUNC
63 /* stdout or stderr stream of child process */
68 /* data read from fd */
69 char buf
[MC_PIPE_BUFSIZE
];
70 /* positive: length of data in buf as before read as after;
71 * zero or negative before read: do not read drom fd;
72 * MC_PIPE_STREAM_EOF after read: EOF of fd;
73 * MC_PIPE_STREAM_UNREAD after read: there was not read from fd;
74 * MC_PIPE_ERROR_READ after read: reading error from fd.
77 /* whether buf is null-terminated or not */
79 /* error code in case of len == MC_PIPE_ERROR_READ */
83 /* Pipe descriptor for child process */
86 /* PID of child process */
88 /* stdout of child process */
90 /* stderr of child process */
94 /*** structures declarations (and typedefs of structures)*****************************************/
96 /* keys are set only during sorting */
103 /* key used for comparing names */
105 /* key used for comparing extensions */
106 char *second_sort_key
;
111 unsigned int marked
:1; /* File marked in pane window */
112 unsigned int link_to_dir
:1; /* If this is a link, does it point to directory? */
113 unsigned int stale_link
:1; /* If this is a symlink and points to Charon's land */
114 unsigned int dir_size_computed
:1; /* Size of directory was computed with dirsizes_cmd */
118 /*** global variables defined in .c file *********************************************************/
120 extern struct sigaction startup_handler
;
122 /*** declarations of public functions ************************************************************/
124 int is_printable (int c
);
126 /* Quote the filename for the purpose of inserting it into the command
127 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
128 * processed by the mc command line. */
129 char *name_quote (const char *c
, gboolean quote_percent
);
131 /* returns a duplicate of c. */
132 char *fake_name_quote (const char *c
, gboolean quote_percent
);
134 /* path_trunc() is the same as str_trunc() but
135 * it deletes possible password from path for security
137 const char *path_trunc (const char *path
, size_t trunc_len
);
139 /* return a static string representing size, appending "K" or "M" for
141 * NOTE: uses the same static buffer as size_trunc_sep. */
142 const char *size_trunc (uintmax_t size
, gboolean use_si
);
144 /* return a static string representing size, appending "K" or "M" for
145 * big sizes. Separates every three digits by ",".
146 * NOTE: uses the same static buffer as size_trunc. */
147 const char *size_trunc_sep (uintmax_t size
, gboolean use_si
);
149 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
150 * not including trailing 0. BUFFER should be at least LEN+1 long.
152 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
153 void size_trunc_len (char *buffer
, unsigned int len
, uintmax_t size
, int units
, gboolean use_si
);
154 const char *string_perm (mode_t mode_bits
);
156 const char *extension (const char *);
157 const char *unix_error_string (int error_num
);
158 const char *skip_separators (const char *s
);
159 const char *skip_numbers (const char *s
);
160 char *strip_ctrl_codes (char *s
);
162 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
163 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
164 * Returns a newly allocated string. */
165 char *convert_controls (const char *s
);
167 /* overwrites passwd with '\0's and frees it. */
168 void wipe_password (char *passwd
);
170 char *diff_two_paths (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
172 /* Returns the basename of fname. The result is a pointer into fname. */
173 const char *x_basename (const char *fname
);
175 char *load_mc_home_file (const char *from
, const char *filename
, char **allocated_filename
,
178 /* uid/gid managing */
179 void init_groups (void);
180 void destroy_groups (void);
181 int get_user_permissions (struct stat
*buf
);
183 void init_uid_gid_cache (void);
184 const char *get_group (gid_t gid
);
185 const char *get_owner (uid_t uid
);
187 /* Returns a copy of *s until a \n is found and is below top */
188 const char *extract_line (const char *s
, const char *top
);
191 void open_error_pipe (void);
192 void check_error_pipe (void);
193 int close_error_pipe (int error
, const char *text
);
195 /* Process spawning */
196 int my_system (int flags
, const char *shell
, const char *command
);
197 int my_systeml (int flags
, const char *shell
, ...);
198 int my_systemv (const char *command
, char *const argv
[]);
199 int my_systemv_flags (int flags
, const char *command
, char *const argv
[]);
201 mc_pipe_t
*mc_popen (const char *command
, GError
** error
);
202 void mc_pread (mc_pipe_t
* p
, GError
** error
);
203 void mc_pclose (mc_pipe_t
* p
, GError
** error
);
205 void my_exit (int status
);
206 void save_stop_handler (void);
208 /* Tilde expansion */
209 char *tilde_expand (const char *);
211 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS
);
212 void canonicalize_pathname (char *);
215 #define mc_realpath realpath
217 char *mc_realpath (const char *path
, char *resolved_path
);
220 /* Looks for "magic" bytes at the start of the VFS file to guess the
221 * compression type. Side effect: modifies the file position. */
222 enum compression_type
get_compression_type (int fd
, const char *);
223 const char *decompress_extension (int type
);
225 GList
*list_append_unique (GList
* list
, char *text
);
227 /* Position saving and restoring */
228 /* Load position for the given filename */
229 void load_file_position (const vfs_path_t
* filename_vpath
, long *line
, long *column
,
230 off_t
* offset
, GArray
** bookmarks
);
231 /* Save position for the given filename */
232 void save_file_position (const vfs_path_t
* filename_vpath
, long line
, long column
, off_t offset
,
236 /* if ch is in [A-Za-z], returns the corresponding control character,
237 * else returns the argument. */
238 extern int ascii_alpha_to_cntrl (int ch
);
241 const char *Q_ (const char *s
);
243 gboolean
mc_util_make_backup_if_possible (const char *, const char *);
244 gboolean
mc_util_restore_from_backup_if_possible (const char *, const char *);
245 gboolean
mc_util_unlink_backup_if_possible (const char *, const char *);
247 char *guess_message_value (void);
249 char *mc_build_filename (const char *first_element
, ...);
250 char *mc_build_filenamev (const char *first_element
, va_list args
);
253 void mc_propagate_error (GError
** dest
, int code
, const char *format
, ...) G_GNUC_PRINTF (3, 4);
254 void mc_replace_error (GError
** dest
, int code
, const char *format
, ...) G_GNUC_PRINTF (3, 4);
257 gboolean
mc_time_elapsed (guint64
* timestamp
, guint64 delay
);
259 /*** inline functions **************************************************/
261 static inline gboolean
262 exist_file (const char *name
)
264 return (access (name
, R_OK
) == 0);
267 static inline gboolean
270 return (gboolean
) ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
));
273 #endif /* MC_UTIL_H */