util.h 6.8 KB

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