util.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <inttypes.h> /* uintmax_t */
  10. #include <unistd.h>
  11. /*** typedefs(not structures) and defined constants **********************************************/
  12. #ifndef MAXSYMLINKS
  13. #define MAXSYMLINKS 32
  14. #endif
  15. #define MAX_SAVED_BOOKMARKS 10
  16. #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
  17. /*** enums ***************************************************************************************/
  18. /* Pathname canonicalization */
  19. typedef enum
  20. {
  21. CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
  22. CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
  23. CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
  24. CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
  25. CANON_PATH_ALL = CANON_PATH_JOINSLASHES
  26. | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
  27. } CANON_PATH_FLAGS;
  28. enum compression_type
  29. {
  30. COMPRESSION_NONE,
  31. COMPRESSION_GZIP,
  32. COMPRESSION_BZIP,
  33. COMPRESSION_BZIP2,
  34. COMPRESSION_LZMA,
  35. COMPRESSION_XZ
  36. };
  37. /*** structures declarations (and typedefs of structures)*****************************************/
  38. /* keys are set only during sorting */
  39. typedef struct
  40. {
  41. /* File attributes */
  42. size_t fnamelen;
  43. char *fname;
  44. struct stat st;
  45. /* key used for comparing names */
  46. char *sort_key;
  47. /* key used for comparing extensions */
  48. char *second_sort_key;
  49. /* Flags */
  50. struct
  51. {
  52. unsigned int marked:1; /* File marked in pane window */
  53. unsigned int link_to_dir:1; /* If this is a link, does it point to directory? */
  54. unsigned int stale_link:1; /* If this is a symlink and points to Charon's land */
  55. unsigned int dir_size_computed:1; /* Size of directory was computed with dirsizes_cmd */
  56. } f;
  57. } file_entry;
  58. /*** global variables defined in .c file *********************************************************/
  59. extern struct sigaction startup_handler;
  60. /*** declarations of public functions ************************************************************/
  61. int is_printable (int c);
  62. /* Quote the filename for the purpose of inserting it into the command
  63. * line. If quote_percent is 1, replace "%" with "%%" - the percent is
  64. * processed by the mc command line. */
  65. char *name_quote (const char *c, int quote_percent);
  66. /* returns a duplicate of c. */
  67. char *fake_name_quote (const char *c, int quote_percent);
  68. /* path_trunc() is the same as str_trunc() but
  69. * it deletes possible password from path for security
  70. * reasons. */
  71. const char *path_trunc (const char *path, size_t trunc_len);
  72. /* return a static string representing size, appending "K" or "M" for
  73. * big sizes.
  74. * NOTE: uses the same static buffer as size_trunc_sep. */
  75. const char *size_trunc (uintmax_t size, gboolean use_si);
  76. /* return a static string representing size, appending "K" or "M" for
  77. * big sizes. Separates every three digits by ",".
  78. * NOTE: uses the same static buffer as size_trunc. */
  79. const char *size_trunc_sep (uintmax_t size, gboolean use_si);
  80. /* Print file SIZE to BUFFER, but don't exceed LEN characters,
  81. * not including trailing 0. BUFFER should be at least LEN+1 long.
  82. *
  83. * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
  84. void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
  85. const char *string_perm (mode_t mode_bits);
  86. /* @modifies path. @returns pointer into path. */
  87. char *strip_password (char *path, int has_prefix);
  88. /* @returns a pointer into a static buffer. */
  89. const char *strip_home_and_password (const char *dir);
  90. const char *extension (const char *);
  91. char *concat_dir_and_file (const char *dir, const char *file);
  92. const char *unix_error_string (int error_num);
  93. const char *skip_separators (const char *s);
  94. const char *skip_numbers (const char *s);
  95. char *strip_ctrl_codes (char *s);
  96. /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
  97. * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
  98. * Returns a newly allocated string. */
  99. char *convert_controls (const char *s);
  100. /* overwrites passwd with '\0's and frees it. */
  101. void wipe_password (char *passwd);
  102. char *diff_two_paths (const char *first, const char *second);
  103. /* Returns the basename of fname. The result is a pointer into fname. */
  104. const char *x_basename (const char *fname);
  105. char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
  106. /* uid/gid managing */
  107. void init_groups (void);
  108. void destroy_groups (void);
  109. int get_user_permissions (struct stat *buf);
  110. void init_uid_gid_cache (void);
  111. char *get_group (int);
  112. char *get_owner (int);
  113. /* Returns a copy of *s until a \n is found and is below top */
  114. const char *extract_line (const char *s, const char *top);
  115. /* Error pipes */
  116. void open_error_pipe (void);
  117. void check_error_pipe (void);
  118. int close_error_pipe (int error, const char *text);
  119. /* Process spawning */
  120. int my_system (int flags, const char *shell, const char *command);
  121. void save_stop_handler (void);
  122. /* Tilde expansion */
  123. char *tilde_expand (const char *);
  124. void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
  125. void canonicalize_pathname (char *);
  126. /* Misc Unix functions */
  127. int my_mkdir (const char *s, mode_t mode);
  128. int my_rmdir (const char *s);
  129. /* Creating temporary files safely */
  130. const char *mc_tmpdir (void);
  131. int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
  132. #ifdef HAVE_REALPATH
  133. #define mc_realpath realpath
  134. #else
  135. char *mc_realpath (const char *path, char *resolved_path);
  136. #endif
  137. /* Looks for ``magic'' bytes at the start of the VFS file to guess the
  138. * compression type. Side effect: modifies the file position. */
  139. enum compression_type get_compression_type (int fd, const char *);
  140. const char *decompress_extension (int type);
  141. GList *list_append_unique (GList * list, char *text);
  142. /* Position saving and restoring */
  143. /* Load position for the given filename */
  144. void load_file_position (const char *filename, long *line, long *column, off_t * offset,
  145. GArray ** bookmarks);
  146. /* Save position for the given filename */
  147. void save_file_position (const char *filename, long line, long column, off_t offset,
  148. GArray * bookmarks);
  149. /* if ch is in [A-Za-z], returns the corresponding control character,
  150. * else returns the argument. */
  151. extern int ascii_alpha_to_cntrl (int ch);
  152. #undef Q_
  153. const char *Q_ (const char *s);
  154. gboolean mc_util_make_backup_if_possible (const char *, const char *);
  155. gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
  156. gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
  157. char *guess_message_value (void);
  158. char *mc_build_filename (const char *first_element, ...);
  159. /*** inline functions **************************************************/
  160. static inline gboolean
  161. exist_file (const char *name)
  162. {
  163. return (access (name, R_OK) == 0);
  164. }
  165. static inline gboolean
  166. is_exe (mode_t mode)
  167. {
  168. return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
  169. }
  170. #endif /* MC_UTIL_H */