util.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /** \file 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 <string.h>
  8. #include <assert.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. /* Returns its argument as a "modifiable" string. This function is
  13. * intended to pass strings to legacy libraries that don't know yet
  14. * about the "const" modifier. The return value of this function
  15. * MUST NOT be modified. */
  16. extern char *str_unconst (const char *);
  17. /* String managing functions */
  18. extern const char *cstrcasestr (const char *haystack, const char *needle);
  19. extern const char *cstrstr (const char *haystack, const char *needle);
  20. void str_replace(char *s, char from, char to);
  21. int is_printable (int c);
  22. void msglen (const char *text, /*@out@*/ int *lines, /*@out@*/ int *columns);
  23. /* Copy from s to d, and trim the beginning if necessary, and prepend
  24. * "..." in this case. The destination string can have at most len
  25. * bytes, not counting trailing 0. */
  26. char *trim (const char *s, char *d, int len);
  27. /* Quote the filename for the purpose of inserting it into the command
  28. * line. If quote_percent is 1, replace "%" with "%%" - the percent is
  29. * processed by the mc command line. */
  30. char *name_quote (const char *c, int quote_percent);
  31. /* returns a duplicate of c. */
  32. char *fake_name_quote (const char *c, int quote_percent);
  33. /* Remove the middle part of the string to fit given length.
  34. * Use "~" to show where the string was truncated.
  35. * Return static buffer, no need to free() it. */
  36. const char *name_trunc (const char *txt, size_t trunc_len);
  37. /* path_trunc() is the same as name_trunc() above but
  38. * it deletes possible password from path for security
  39. * reasons. */
  40. const char *path_trunc (const char *path, size_t trunc_len);
  41. /* return a static string representing size, appending "K" or "M" for
  42. * big sizes.
  43. * NOTE: uses the same static buffer as size_trunc_sep. */
  44. const char *size_trunc (double size);
  45. /* return a static string representing size, appending "K" or "M" for
  46. * big sizes. Separates every three digits by ",".
  47. * NOTE: uses the same static buffer as size_trunc. */
  48. const char *size_trunc_sep (double size);
  49. /* Print file SIZE to BUFFER, but don't exceed LEN characters,
  50. * not including trailing 0. BUFFER should be at least LEN+1 long.
  51. *
  52. * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
  53. void size_trunc_len (char *buffer, unsigned int len, off_t size, int units);
  54. int is_exe (mode_t mode);
  55. const char *string_perm (mode_t mode_bits);
  56. /* @modifies path. @returns pointer into path. */
  57. char *strip_password (char *path, int has_prefix);
  58. /* @returns a pointer into a static buffer. */
  59. const char *strip_home_and_password (const char *dir);
  60. const char *extension (const char *);
  61. char *concat_dir_and_file (const char *dir, const char *file);
  62. const char *unix_error_string (int error_num);
  63. const char *skip_separators (const char *s);
  64. const char *skip_numbers (const char *s);
  65. char *strip_ctrl_codes (char *s);
  66. /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
  67. * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
  68. * Returns a newly allocated string. */
  69. char *convert_controls (const char *s);
  70. /* overwrites passwd with '\0's and frees it. */
  71. void wipe_password (char *passwd);
  72. char *diff_two_paths (const char *first, const char *second);
  73. /* Returns the basename of fname. The result is a pointer into fname. */
  74. const char *x_basename (const char *fname);
  75. char *load_file (const char *filename);
  76. char *load_mc_home_file (const char *, const char *, const char *filename, char ** allocated_filename);
  77. /* uid/gid managing */
  78. void init_groups (void);
  79. void destroy_groups (void);
  80. int get_user_permissions (struct stat *buf);
  81. void init_uid_gid_cache (void);
  82. char *get_group (int);
  83. char *get_owner (int);
  84. #define MAX_I18NTIMELENGTH 20
  85. #define MIN_I18NTIMELENGTH 10
  86. #define STD_I18NTIMELENGTH 12
  87. size_t i18n_checktimelength (void);
  88. const char *file_date (time_t);
  89. int exist_file (const char *name);
  90. /* Check if the file exists. If not copy the default */
  91. int check_for_default (const char *default_file, const char *file);
  92. /* Returns a copy of *s until a \n is found and is below top */
  93. const char *extract_line (const char *s, const char *top);
  94. /* Matching */
  95. enum {
  96. match_file, /* match a filename, use easy_patterns */
  97. match_normal, /* match pattern, use easy_patterns */
  98. match_regex /* match pattern, force using regex */
  99. };
  100. extern int easy_patterns;
  101. /* Error pipes */
  102. void open_error_pipe (void);
  103. void check_error_pipe (void);
  104. int close_error_pipe (int error, const char *text);
  105. /* Process spawning */
  106. int my_system (int flags, const char *shell, const char *command);
  107. void save_stop_handler (void);
  108. extern struct sigaction startup_handler;
  109. /* Tilde expansion */
  110. char *tilde_expand (const char *);
  111. /* Pathname canonicalization */
  112. typedef enum {
  113. CANON_PATH_JOINSLASHES = 1L<<0, /* Multiple `/'s are collapsed to a single `/'. */
  114. CANON_PATH_REMSLASHDOTS = 1L<<1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
  115. CANON_PATH_REMDOUBLEDOTS = 1L<<3, /* Non-leading `../'s and trailing `..'s are handled by removing */
  116. CANON_PATH_GUARDUNC = 1L<<4, /* Detect and preserve UNC paths: //server/... */
  117. CANON_PATH_ALL = CANON_PATH_JOINSLASHES
  118. | CANON_PATH_REMSLASHDOTS
  119. | CANON_PATH_REMDOUBLEDOTS
  120. | CANON_PATH_GUARDUNC
  121. } CANON_PATH_FLAGS;
  122. void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
  123. void canonicalize_pathname (char *);
  124. /* Misc Unix functions */
  125. char *get_current_wd (char *buffer, int size);
  126. int my_mkdir (const char *s, mode_t mode);
  127. int my_rmdir (const char *s);
  128. /* Rotating dash routines */
  129. void use_dash (int flag); /* Disable/Enable rotate_dash routines */
  130. void rotate_dash (void);
  131. /* Creating temporary files safely */
  132. const char *mc_tmpdir (void);
  133. int mc_mkstemps(char **pname, const char *prefix, const char *suffix);
  134. #ifndef PATH_MAX
  135. #ifdef _POSIX_VERSION
  136. #define PATH_MAX _POSIX_PATH_MAX
  137. #else
  138. #ifdef MAXPATHLEN
  139. #define PATH_MAX MAXPATHLEN
  140. #else
  141. #define PATH_MAX 1024
  142. #endif
  143. #endif
  144. #endif
  145. #ifndef MAXSYMLINKS
  146. #define MAXSYMLINKS 32
  147. #endif
  148. char *mc_realpath(const char *path, char resolved_path[]);
  149. enum compression_type {
  150. COMPRESSION_NONE,
  151. COMPRESSION_GZIP,
  152. COMPRESSION_BZIP,
  153. COMPRESSION_BZIP2,
  154. COMPRESSION_LZMA,
  155. COMPRESSION_XZ
  156. };
  157. /* Looks for ``magic'' bytes at the start of the VFS file to guess the
  158. * compression type. Side effect: modifies the file position. */
  159. enum compression_type get_compression_type (int fd, const char*);
  160. const char *decompress_extension (int type);
  161. /* Hook functions */
  162. typedef struct hook {
  163. void (*hook_fn)(void *);
  164. void *hook_data;
  165. struct hook *next;
  166. } Hook;
  167. void add_hook (Hook **hook_list, void (*hook_fn)(void *), void *data);
  168. void execute_hooks (Hook *hook_list);
  169. void delete_hook (Hook **hook_list, void (*hook_fn)(void *));
  170. int hook_present (Hook *hook_list, void (*hook_fn)(void *));
  171. GList *list_append_unique (GList *list, char *text);
  172. /* Position saving and restoring */
  173. /* Load position for the given filename */
  174. void load_file_position (const char *filename, long *line, long *column, off_t *offset);
  175. /* Save position for the given filename */
  176. void save_file_position (const char *filename, long line, long column, off_t offset);
  177. /* OS specific defines */
  178. #define PATH_SEP '/'
  179. #define PATH_SEP_STR "/"
  180. #define PATH_ENV_SEP ':'
  181. #define TMPDIR_DEFAULT "/tmp"
  182. #define SCRIPT_SUFFIX ""
  183. #define get_default_editor() "vi"
  184. #define OS_SORT_CASE_SENSITIVE_DEFAULT 1
  185. #define STRCOMP strcmp
  186. #define STRNCOMP strncmp
  187. #define MC_ARCH_FLAGS 0
  188. /* taken from regex.c: */
  189. /* Jim Meyering writes:
  190. "... Some ctype macros are valid only for character codes that
  191. isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  192. using /bin/cc or gcc but without giving an ansi option). So, all
  193. ctype uses should be through macros like ISPRINT... If
  194. STDC_HEADERS is defined, then autoconf has verified that the ctype
  195. macros don't need to be guarded with references to isascii. ...
  196. Defining isascii to 1 should let any compiler worth its salt
  197. eliminate the && through constant folding." */
  198. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  199. #define ISASCII(c) 1
  200. #else
  201. #define ISASCII(c) isascii(c)
  202. #endif
  203. /* usage: str_cmp ("foo", !=, "bar") */
  204. #define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
  205. /* if ch is in [A-Za-z], returns the corresponding control character,
  206. * else returns the argument. */
  207. extern int ascii_alpha_to_cntrl (int ch);
  208. #undef Q_
  209. const char *Q_ (const char *s);
  210. #define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))
  211. /*
  212. * strcpy is unsafe on overlapping memory areas, so define memmove-alike
  213. * string function.
  214. * Have sense only when:
  215. * * dest <= src
  216. * AND
  217. * * dest and str are pointers to one object (as Roland Illig pointed).
  218. *
  219. * We can't use str*cpy funs here:
  220. * http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
  221. */
  222. static inline char * str_move(char * dest, const char * src)
  223. {
  224. size_t n;
  225. assert (dest<=src);
  226. n = strlen (src) + 1; /* + '\0' */
  227. return memmove (dest, src, n);
  228. }
  229. gboolean mc_util_make_backup_if_possible (const char *, const char *);
  230. gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
  231. gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
  232. char *guess_message_value (void);
  233. #define MC_PTR_FREE(ptr) do { g_free(ptr); (ptr) = NULL; } while (0)
  234. #endif