util.h 12 KB

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