Moved functions mc_mkstemp() and mc_tempdir() to VFS module.
[midnight-commander.git] / lib / util.h
blob077c361eef41fd7620f8e36168414b9d77cc66e0
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 #ifdef HAVE_REALPATH
170 #define mc_realpath realpath
171 #else
172 char *mc_realpath (const char *path, char *resolved_path);
173 #endif
175 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
176 * compression type. Side effect: modifies the file position. */
177 enum compression_type get_compression_type (int fd, const char *);
178 const char *decompress_extension (int type);
180 GList *list_append_unique (GList * list, char *text);
182 /* Position saving and restoring */
183 /* Load position for the given filename */
184 void load_file_position (const char *filename, long *line, long *column, off_t * offset,
185 GArray ** bookmarks);
186 /* Save position for the given filename */
187 void save_file_position (const char *filename, long line, long column, off_t offset,
188 GArray * bookmarks);
191 /* if ch is in [A-Za-z], returns the corresponding control character,
192 * else returns the argument. */
193 extern int ascii_alpha_to_cntrl (int ch);
195 #undef Q_
196 const char *Q_ (const char *s);
198 gboolean mc_util_make_backup_if_possible (const char *, const char *);
199 gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
200 gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
202 char *guess_message_value (void);
204 char *mc_build_filename (const char *first_element, ...);
205 char *mc_build_filenamev (const char *first_element, va_list args);
207 /*** inline functions **************************************************/
209 static inline gboolean
210 exist_file (const char *name)
212 return (access (name, R_OK) == 0);
215 static inline gboolean
216 is_exe (mode_t mode)
218 return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
221 #endif /* MC_UTIL_H */