Manual pages: mcmcdiffmceditmcview

root/lib/util.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. mc_propagate_error
  2. is_exe
  3. canonicalize_pathname

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

/* [previous][next][first][last][top][bottom][index][help]  */