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