util.h 9.8 KB

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