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

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

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