util.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /*** global variables defined in .c file *********************************************************/
  39. extern struct sigaction startup_handler;
  40. /*** declarations of public functions ************************************************************/
  41. int is_printable (int c);
  42. /* Quote the filename for the purpose of inserting it into the command
  43. * line. If quote_percent is 1, replace "%" with "%%" - the percent is
  44. * processed by the mc command line. */
  45. char *name_quote (const char *c, int quote_percent);
  46. /* returns a duplicate of c. */
  47. char *fake_name_quote (const char *c, int quote_percent);
  48. /* path_trunc() is the same as str_trunc() but
  49. * it deletes possible password from path for security
  50. * reasons. */
  51. const char *path_trunc (const char *path, size_t trunc_len);
  52. /* return a static string representing size, appending "K" or "M" for
  53. * big sizes.
  54. * NOTE: uses the same static buffer as size_trunc_sep. */
  55. const char *size_trunc (uintmax_t size, gboolean use_si);
  56. /* return a static string representing size, appending "K" or "M" for
  57. * big sizes. Separates every three digits by ",".
  58. * NOTE: uses the same static buffer as size_trunc. */
  59. const char *size_trunc_sep (uintmax_t size, gboolean use_si);
  60. /* Print file SIZE to BUFFER, but don't exceed LEN characters,
  61. * not including trailing 0. BUFFER should be at least LEN+1 long.
  62. *
  63. * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
  64. void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
  65. const char *string_perm (mode_t mode_bits);
  66. /* @modifies path. @returns pointer into path. */
  67. char *strip_password (char *path, int has_prefix);
  68. /* @returns a pointer into a static buffer. */
  69. const char *strip_home_and_password (const char *dir);
  70. const char *extension (const char *);
  71. char *concat_dir_and_file (const char *dir, const char *file);
  72. const char *unix_error_string (int error_num);
  73. const char *skip_separators (const char *s);
  74. const char *skip_numbers (const char *s);
  75. char *strip_ctrl_codes (char *s);
  76. /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
  77. * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
  78. * Returns a newly allocated string. */
  79. char *convert_controls (const char *s);
  80. /* overwrites passwd with '\0's and frees it. */
  81. void wipe_password (char *passwd);
  82. char *diff_two_paths (const char *first, const char *second);
  83. /* Returns the basename of fname. The result is a pointer into fname. */
  84. const char *x_basename (const char *fname);
  85. char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
  86. /* uid/gid managing */
  87. void init_groups (void);
  88. void destroy_groups (void);
  89. int get_user_permissions (struct stat *buf);
  90. void init_uid_gid_cache (void);
  91. char *get_group (int);
  92. char *get_owner (int);
  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. /* Error pipes */
  98. void open_error_pipe (void);
  99. void check_error_pipe (void);
  100. int close_error_pipe (int error, const char *text);
  101. /* Process spawning */
  102. int my_system (int flags, const char *shell, const char *command);
  103. void save_stop_handler (void);
  104. /* Tilde expansion */
  105. char *tilde_expand (const char *);
  106. void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
  107. void canonicalize_pathname (char *);
  108. /* Misc Unix functions */
  109. int my_mkdir (const char *s, mode_t mode);
  110. int my_rmdir (const char *s);
  111. /* Creating temporary files safely */
  112. const char *mc_tmpdir (void);
  113. int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
  114. #ifdef HAVE_REALPATH
  115. #define mc_realpath realpath
  116. #else
  117. char *mc_realpath (const char *path, char *resolved_path);
  118. #endif
  119. /* Looks for ``magic'' bytes at the start of the VFS file to guess the
  120. * compression type. Side effect: modifies the file position. */
  121. enum compression_type get_compression_type (int fd, const char *);
  122. const char *decompress_extension (int type);
  123. GList *list_append_unique (GList * list, char *text);
  124. /* Position saving and restoring */
  125. /* Load position for the given filename */
  126. void load_file_position (const char *filename, long *line, long *column, off_t * offset,
  127. GArray ** bookmarks);
  128. /* Save position for the given filename */
  129. void save_file_position (const char *filename, long line, long column, off_t offset,
  130. GArray * bookmarks);
  131. /* if ch is in [A-Za-z], returns the corresponding control character,
  132. * else returns the argument. */
  133. extern int ascii_alpha_to_cntrl (int ch);
  134. #undef Q_
  135. const char *Q_ (const char *s);
  136. gboolean mc_util_make_backup_if_possible (const char *, const char *);
  137. gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
  138. gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
  139. char *guess_message_value (void);
  140. /*** inline functions **************************************************/
  141. static inline gboolean
  142. exist_file (const char *name)
  143. {
  144. return (access (name, R_OK) == 0);
  145. }
  146. static inline gboolean
  147. is_exe (mode_t mode)
  148. {
  149. return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
  150. }
  151. #endif /* MC_UTIL_H */