util.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /** \file lib/util.h
  2. * \brief Header: various utilities
  3. */
  4. #ifndef MC_UTIL_H
  5. #define MC_UTIL_H
  6. #include "lib/global.h" /* include <glib.h> */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <inttypes.h> /* uintmax_t */
  10. #include <unistd.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 MC_PIPE_BUFSIZE BUF_8K
  21. #define MC_PIPE_STREAM_EOF 0
  22. #define MC_PIPE_STREAM_UNREAD -1
  23. #define MC_PIPE_ERROR_CREATE_PIPE -2
  24. #define MC_PIPE_ERROR_PARSE_COMMAND -3
  25. #define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4
  26. #define MC_PIPE_ERROR_READ -5
  27. /*** enums ***************************************************************************************/
  28. /* Pathname canonicalization */
  29. typedef enum
  30. {
  31. CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple '/'s are collapsed to a single '/'. */
  32. CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading './'s, '/'s and trailing '/.'s are removed. */
  33. CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading '../'s and trailing '..'s are handled by removing */
  34. CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
  35. CANON_PATH_ALL = CANON_PATH_JOINSLASHES
  36. | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
  37. } CANON_PATH_FLAGS;
  38. enum compression_type
  39. {
  40. COMPRESSION_NONE,
  41. COMPRESSION_GZIP,
  42. COMPRESSION_BZIP,
  43. COMPRESSION_BZIP2,
  44. COMPRESSION_LZMA,
  45. COMPRESSION_XZ
  46. };
  47. /* stdout or stderr stream of child process */
  48. typedef struct
  49. {
  50. /* file descriptor */
  51. int fd;
  52. /* data read from fd */
  53. char buf[MC_PIPE_BUFSIZE];
  54. /* positive: length of data in buf as before read as after;
  55. * zero or negative before read: do not read drom fd;
  56. * MC_PIPE_STREAM_EOF after read: EOF of fd;
  57. * MC_PIPE_STREAM_UNREAD after read: there was not read from fd;
  58. * MC_PIPE_ERROR_READ after read: reading error from fd.
  59. */
  60. ssize_t len;
  61. /* whether buf is null-terminated or not */
  62. gboolean null_term;
  63. /* error code in case of len == MC_PIPE_ERROR_READ */
  64. int error;
  65. } mc_pipe_stream_t;
  66. /* Pipe descriptor for child process */
  67. typedef struct
  68. {
  69. /* PID of child process */
  70. GPid child_pid;
  71. /* stdout of child process */
  72. mc_pipe_stream_t out;
  73. /* stderr of child process */
  74. mc_pipe_stream_t err;
  75. } mc_pipe_t;
  76. /*** structures declarations (and typedefs of structures)*****************************************/
  77. /* keys are set only during sorting */
  78. typedef struct
  79. {
  80. /* File attributes */
  81. size_t fnamelen;
  82. char *fname;
  83. struct stat st;
  84. /* key used for comparing names */
  85. char *sort_key;
  86. /* key used for comparing extensions */
  87. char *second_sort_key;
  88. /* Flags */
  89. struct
  90. {
  91. unsigned int marked:1; /* File marked in pane window */
  92. unsigned int link_to_dir:1; /* If this is a link, does it point to directory? */
  93. unsigned int stale_link:1; /* If this is a symlink and points to Charon's land */
  94. unsigned int dir_size_computed:1; /* Size of directory was computed with dirsizes_cmd */
  95. } f;
  96. } file_entry_t;
  97. /*** global variables defined in .c file *********************************************************/
  98. extern struct sigaction startup_handler;
  99. /*** declarations of public functions ************************************************************/
  100. int is_printable (int c);
  101. /* Quote the filename for the purpose of inserting it into the command
  102. * line. If quote_percent is 1, replace "%" with "%%" - the percent is
  103. * processed by the mc command line. */
  104. char *name_quote (const char *c, gboolean quote_percent);
  105. /* returns a duplicate of c. */
  106. char *fake_name_quote (const char *c, gboolean quote_percent);
  107. /* path_trunc() is the same as str_trunc() but
  108. * it deletes possible password from path for security
  109. * reasons. */
  110. const char *path_trunc (const char *path, size_t trunc_len);
  111. /* return a static string representing size, appending "K" or "M" for
  112. * big sizes.
  113. * NOTE: uses the same static buffer as size_trunc_sep. */
  114. const char *size_trunc (uintmax_t size, gboolean use_si);
  115. /* return a static string representing size, appending "K" or "M" for
  116. * big sizes. Separates every three digits by ",".
  117. * NOTE: uses the same static buffer as size_trunc. */
  118. const char *size_trunc_sep (uintmax_t size, gboolean use_si);
  119. /* Print file SIZE to BUFFER, but don't exceed LEN characters,
  120. * not including trailing 0. BUFFER should be at least LEN+1 long.
  121. *
  122. * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
  123. void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
  124. const char *string_perm (mode_t mode_bits);
  125. const char *extension (const char *);
  126. const char *unix_error_string (int error_num);
  127. const char *skip_separators (const char *s);
  128. const char *skip_numbers (const char *s);
  129. char *strip_ctrl_codes (char *s);
  130. /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
  131. * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
  132. * Returns a newly allocated string. */
  133. char *convert_controls (const char *s);
  134. /* overwrites passwd with '\0's and frees it. */
  135. void wipe_password (char *passwd);
  136. char *diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
  137. /* Returns the basename of fname. The result is a pointer into fname. */
  138. const char *x_basename (const char *fname);
  139. char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
  140. /* uid/gid managing */
  141. void init_groups (void);
  142. void destroy_groups (void);
  143. int get_user_permissions (struct stat *buf);
  144. void init_uid_gid_cache (void);
  145. char *get_group (int);
  146. char *get_owner (int);
  147. /* Returns a copy of *s until a \n is found and is below top */
  148. const char *extract_line (const char *s, const char *top);
  149. /* Error pipes */
  150. void open_error_pipe (void);
  151. void check_error_pipe (void);
  152. int close_error_pipe (int error, const char *text);
  153. /* Process spawning */
  154. int my_system (int flags, const char *shell, const char *command);
  155. int my_systeml (int flags, const char *shell, ...);
  156. int my_systemv (const char *command, char *const argv[]);
  157. int my_systemv_flags (int flags, const char *command, char *const argv[]);
  158. mc_pipe_t *mc_popen (const char *command, GError ** error);
  159. void mc_pread (mc_pipe_t * p, GError ** error);
  160. void mc_pclose (mc_pipe_t * p, GError ** error);
  161. void my_exit (int status);
  162. void save_stop_handler (void);
  163. /* Tilde expansion */
  164. char *tilde_expand (const char *);
  165. void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
  166. void canonicalize_pathname (char *);
  167. #ifdef HAVE_REALPATH
  168. #define mc_realpath realpath
  169. #else
  170. char *mc_realpath (const char *path, char *resolved_path);
  171. #endif
  172. /* Looks for "magic" bytes at the start of the VFS file to guess the
  173. * compression type. Side effect: modifies the file position. */
  174. enum compression_type get_compression_type (int fd, const char *);
  175. const char *decompress_extension (int type);
  176. GList *list_append_unique (GList * list, char *text);
  177. /* Position saving and restoring */
  178. /* Load position for the given filename */
  179. void load_file_position (const vfs_path_t * filename_vpath, long *line, long *column,
  180. off_t * offset, GArray ** bookmarks);
  181. /* Save position for the given filename */
  182. void save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
  183. GArray * bookmarks);
  184. /* if ch is in [A-Za-z], returns the corresponding control character,
  185. * else returns the argument. */
  186. extern int ascii_alpha_to_cntrl (int ch);
  187. #undef Q_
  188. const char *Q_ (const char *s);
  189. gboolean mc_util_make_backup_if_possible (const char *, const char *);
  190. gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
  191. gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
  192. char *guess_message_value (void);
  193. char *mc_build_filename (const char *first_element, ...);
  194. char *mc_build_filenamev (const char *first_element, va_list args);
  195. void mc_propagate_error (GError ** dest, int code, const char *format, ...);
  196. void mc_replace_error (GError ** dest, int code, const char *format, ...);
  197. gboolean mc_time_elapsed (guint64 * timestamp, guint64 delay);
  198. /*** inline functions **************************************************/
  199. static inline gboolean
  200. exist_file (const char *name)
  201. {
  202. return (access (name, R_OK) == 0);
  203. }
  204. static inline gboolean
  205. is_exe (mode_t mode)
  206. {
  207. return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
  208. }
  209. #endif /* MC_UTIL_H */