Merge branch '27_group_undo'
[pantumic.git] / lib / util.h
bloba6aa3a444a7b65a5294160fa72b7f37225172f4f
1 /** \file util.h
2 * \brief Header: various utilities
3 */
5 #ifndef MC_UTIL_H
6 #define MC_UTIL_H
8 #include "lib/global.h" /* include <glib.h> */
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <inttypes.h> /* uintmax_t */
13 #include <unistd.h>
15 /*** typedefs(not structures) and defined constants **********************************************/
17 #ifndef MAXSYMLINKS
18 #define MAXSYMLINKS 32
19 #endif
21 #define MAX_SAVED_BOOKMARKS 10
23 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
25 /*** enums ***************************************************************************************/
27 /* Pathname canonicalization */
28 typedef enum
30 CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
31 CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
32 CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
33 CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
34 CANON_PATH_ALL = CANON_PATH_JOINSLASHES
35 | CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
36 } CANON_PATH_FLAGS;
38 enum compression_type
40 COMPRESSION_NONE,
41 COMPRESSION_GZIP,
42 COMPRESSION_BZIP,
43 COMPRESSION_BZIP2,
44 COMPRESSION_LZMA,
45 COMPRESSION_XZ
48 /*** structures declarations (and typedefs of structures)*****************************************/
50 /*** global variables defined in .c file *********************************************************/
52 extern struct sigaction startup_handler;
54 /*** declarations of public functions ************************************************************/
56 int is_printable (int c);
58 /* Quote the filename for the purpose of inserting it into the command
59 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
60 * processed by the mc command line. */
61 char *name_quote (const char *c, int quote_percent);
63 /* returns a duplicate of c. */
64 char *fake_name_quote (const char *c, int quote_percent);
66 /* path_trunc() is the same as str_trunc() but
67 * it deletes possible password from path for security
68 * reasons. */
69 const char *path_trunc (const char *path, size_t trunc_len);
71 /* return a static string representing size, appending "K" or "M" for
72 * big sizes.
73 * NOTE: uses the same static buffer as size_trunc_sep. */
74 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);
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.
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);
88 /* @modifies path. @returns pointer into path. */
89 char *strip_password (char *path, int has_prefix);
91 /* @returns a pointer into a static buffer. */
92 const char *strip_home_and_password (const char *dir);
94 const char *extension (const char *);
95 char *concat_dir_and_file (const char *dir, const char *file);
96 const char *unix_error_string (int error_num);
97 const char *skip_separators (const char *s);
98 const char *skip_numbers (const char *s);
99 char *strip_ctrl_codes (char *s);
101 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
102 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
103 * Returns a newly allocated string. */
104 char *convert_controls (const char *s);
106 /* overwrites passwd with '\0's and frees it. */
107 void wipe_password (char *passwd);
109 char *diff_two_paths (const char *first, const char *second);
111 /* Returns the basename of fname. The result is a pointer into fname. */
112 const char *x_basename (const char *fname);
114 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
116 /* uid/gid managing */
117 void init_groups (void);
118 void destroy_groups (void);
119 int get_user_permissions (struct stat *buf);
121 void init_uid_gid_cache (void);
122 char *get_group (int);
123 char *get_owner (int);
125 /* Check if the file exists. If not copy the default */
126 int check_for_default (const char *default_file, const char *file);
128 /* Returns a copy of *s until a \n is found and is below top */
129 const char *extract_line (const char *s, const char *top);
131 /* Error pipes */
132 void open_error_pipe (void);
133 void check_error_pipe (void);
134 int close_error_pipe (int error, const char *text);
136 /* Process spawning */
137 int my_system (int flags, const char *shell, const char *command);
138 void save_stop_handler (void);
140 /* Tilde expansion */
141 char *tilde_expand (const char *);
143 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
144 void canonicalize_pathname (char *);
146 /* Misc Unix functions */
147 int my_mkdir (const char *s, mode_t mode);
148 int my_rmdir (const char *s);
150 /* Creating temporary files safely */
151 const char *mc_tmpdir (void);
152 int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
154 #ifdef HAVE_REALPATH
155 #define mc_realpath realpath
156 #else
157 char *mc_realpath (const char *path, char *resolved_path);
158 #endif
160 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
161 * compression type. Side effect: modifies the file position. */
162 enum compression_type get_compression_type (int fd, const char *);
163 const char *decompress_extension (int type);
165 GList *list_append_unique (GList * list, char *text);
167 /* Position saving and restoring */
168 /* Load position for the given filename */
169 void load_file_position (const char *filename, long *line, long *column, off_t * offset,
170 GArray ** bookmarks);
171 /* Save position for the given filename */
172 void save_file_position (const char *filename, long line, long column, off_t offset,
173 GArray * bookmarks);
176 /* if ch is in [A-Za-z], returns the corresponding control character,
177 * else returns the argument. */
178 extern int ascii_alpha_to_cntrl (int ch);
180 #undef Q_
181 const char *Q_ (const char *s);
183 gboolean mc_util_make_backup_if_possible (const char *, const char *);
184 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
185 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
187 char *guess_message_value (void);
189 /*** inline functions **************************************************/
191 static inline gboolean
192 exist_file (const char *name)
194 return (access (name, R_OK) == 0);
197 static inline gboolean
198 is_exe (mode_t mode)
200 return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
203 #endif /* MC_UTIL_H */