added vfs_path_element_valid() function
[midnight-commander.git] / lib / util.h
blob95b4af607fb1fa46c76adcb30a916d648537458a
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 /* keys are set only during sorting */
51 typedef struct
53 /* File attributes */
54 size_t fnamelen;
55 char *fname;
56 struct stat st;
57 /* key used for comparing names */
58 char *sort_key;
59 /* key used for comparing extensions */
60 char *second_sort_key;
62 /* Flags */
63 struct
65 unsigned int marked:1; /* File marked in pane window */
66 unsigned int link_to_dir:1; /* If this is a link, does it point to directory? */
67 unsigned int stale_link:1; /* If this is a symlink and points to Charon's land */
68 unsigned int dir_size_computed:1; /* Size of directory was computed with dirsizes_cmd */
69 } f;
70 } file_entry;
72 /*** global variables defined in .c file *********************************************************/
74 extern struct sigaction startup_handler;
76 /*** declarations of public functions ************************************************************/
78 int is_printable (int c);
80 /* Quote the filename for the purpose of inserting it into the command
81 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
82 * processed by the mc command line. */
83 char *name_quote (const char *c, int quote_percent);
85 /* returns a duplicate of c. */
86 char *fake_name_quote (const char *c, int quote_percent);
88 /* path_trunc() is the same as str_trunc() but
89 * it deletes possible password from path for security
90 * reasons. */
91 const char *path_trunc (const char *path, size_t trunc_len);
93 /* return a static string representing size, appending "K" or "M" for
94 * big sizes.
95 * NOTE: uses the same static buffer as size_trunc_sep. */
96 const char *size_trunc (uintmax_t size, gboolean use_si);
98 /* return a static string representing size, appending "K" or "M" for
99 * big sizes. Separates every three digits by ",".
100 * NOTE: uses the same static buffer as size_trunc. */
101 const char *size_trunc_sep (uintmax_t size, gboolean use_si);
103 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
104 * not including trailing 0. BUFFER should be at least LEN+1 long.
106 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
107 void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
108 const char *string_perm (mode_t mode_bits);
110 /* @modifies path. @returns pointer into path. */
111 char *strip_password (char *path, int has_prefix);
113 /* @returns a pointer into a static buffer. */
114 const char *strip_home_and_password (const char *dir);
116 const char *extension (const char *);
117 char *concat_dir_and_file (const char *dir, const char *file);
118 const char *unix_error_string (int error_num);
119 const char *skip_separators (const char *s);
120 const char *skip_numbers (const char *s);
121 char *strip_ctrl_codes (char *s);
123 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
124 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
125 * Returns a newly allocated string. */
126 char *convert_controls (const char *s);
128 /* overwrites passwd with '\0's and frees it. */
129 void wipe_password (char *passwd);
131 char *diff_two_paths (const char *first, const char *second);
133 /* Returns the basename of fname. The result is a pointer into fname. */
134 const char *x_basename (const char *fname);
136 char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename);
138 /* uid/gid managing */
139 void init_groups (void);
140 void destroy_groups (void);
141 int get_user_permissions (struct stat *buf);
143 void init_uid_gid_cache (void);
144 char *get_group (int);
145 char *get_owner (int);
147 /* Returns a copy of *s until a \n is found and is below top */
148 const char *extract_line (const char *s, const char *top);
150 /* Error pipes */
151 void open_error_pipe (void);
152 void check_error_pipe (void);
153 int close_error_pipe (int error, const char *text);
155 /* Process spawning */
156 int my_system (int flags, const char *shell, const char *command);
157 void save_stop_handler (void);
159 /* Tilde expansion */
160 char *tilde_expand (const char *);
162 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
163 void canonicalize_pathname (char *);
165 /* Misc Unix functions */
166 int my_mkdir (const char *s, mode_t mode);
167 int my_rmdir (const char *s);
169 /* Creating temporary files safely */
170 const char *mc_tmpdir (void);
171 int mc_mkstemps (char **pname, const char *prefix, const char *suffix);
173 #ifdef HAVE_REALPATH
174 #define mc_realpath realpath
175 #else
176 char *mc_realpath (const char *path, char *resolved_path);
177 #endif
179 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
180 * compression type. Side effect: modifies the file position. */
181 enum compression_type get_compression_type (int fd, const char *);
182 const char *decompress_extension (int type);
184 GList *list_append_unique (GList * list, char *text);
186 /* Position saving and restoring */
187 /* Load position for the given filename */
188 void load_file_position (const char *filename, long *line, long *column, off_t * offset,
189 GArray ** bookmarks);
190 /* Save position for the given filename */
191 void save_file_position (const char *filename, long line, long column, off_t offset,
192 GArray * bookmarks);
195 /* if ch is in [A-Za-z], returns the corresponding control character,
196 * else returns the argument. */
197 extern int ascii_alpha_to_cntrl (int ch);
199 #undef Q_
200 const char *Q_ (const char *s);
202 gboolean mc_util_make_backup_if_possible (const char *, const char *);
203 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
204 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
206 char *guess_message_value (void);
208 /*** inline functions **************************************************/
210 static inline gboolean
211 exist_file (const char *name)
213 return (access (name, R_OK) == 0);
216 static inline gboolean
217 is_exe (mode_t mode)
219 return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
222 #endif /* MC_UTIL_H */