util.h 11 KB

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