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

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