2 * \brief Header: various utilities
8 #include "lib/global.h" /* include <glib.h> */
10 #include <sys/types.h>
14 /*** typedefs(not structures) and defined constants **********************************************/
17 #define MAXSYMLINKS 32
20 #define MAX_SAVED_BOOKMARKS 10
22 #define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
24 /*** enums ***************************************************************************************/
26 /* Pathname canonicalization */
29 CANON_PATH_JOINSLASHES
= 1L << 0, /* Multiple `/'s are collapsed to a single `/'. */
30 CANON_PATH_REMSLASHDOTS
= 1L << 1, /* Leading `./'s, `/'s and trailing `/.'s are removed. */
31 CANON_PATH_REMDOUBLEDOTS
= 1L << 3, /* Non-leading `../'s and trailing `..'s are handled by removing */
32 CANON_PATH_GUARDUNC
= 1L << 4, /* Detect and preserve UNC paths: //server/... */
33 CANON_PATH_ALL
= CANON_PATH_JOINSLASHES
34 | CANON_PATH_REMSLASHDOTS
| CANON_PATH_REMDOUBLEDOTS
| CANON_PATH_GUARDUNC
47 /*** structures declarations (and typedefs of structures)*****************************************/
49 /*** global variables defined in .c file *********************************************************/
51 extern struct sigaction startup_handler
;
53 /*** declarations of public functions ************************************************************/
55 int is_printable (int c
);
57 /* Quote the filename for the purpose of inserting it into the command
58 * line. If quote_percent is 1, replace "%" with "%%" - the percent is
59 * processed by the mc command line. */
60 char *name_quote (const char *c
, int quote_percent
);
62 /* returns a duplicate of c. */
63 char *fake_name_quote (const char *c
, int quote_percent
);
65 /* path_trunc() is the same as str_trunc() but
66 * it deletes possible password from path for security
68 const char *path_trunc (const char *path
, size_t trunc_len
);
70 /* return a static string representing size, appending "K" or "M" for
72 * NOTE: uses the same static buffer as size_trunc_sep. */
73 const char *size_trunc (double size
, gboolean use_si
);
75 /* return a static string representing size, appending "K" or "M" for
76 * big sizes. Separates every three digits by ",".
77 * NOTE: uses the same static buffer as size_trunc. */
78 const char *size_trunc_sep (double size
, gboolean use_si
);
80 /* Print file SIZE to BUFFER, but don't exceed LEN characters,
81 * not including trailing 0. BUFFER should be at least LEN+1 long.
83 * Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
84 void size_trunc_len (char *buffer
, unsigned int len
, off_t size
, int units
, gboolean use_si
);
85 const char *string_perm (mode_t mode_bits
);
87 /* @modifies path. @returns pointer into path. */
88 char *strip_password (char *path
, int has_prefix
);
90 /* @returns a pointer into a static buffer. */
91 const char *strip_home_and_password (const char *dir
);
93 const char *extension (const char *);
94 char *concat_dir_and_file (const char *dir
, const char *file
);
95 const char *unix_error_string (int error_num
);
96 const char *skip_separators (const char *s
);
97 const char *skip_numbers (const char *s
);
98 char *strip_ctrl_codes (char *s
);
100 /* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
101 * ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
102 * Returns a newly allocated string. */
103 char *convert_controls (const char *s
);
105 /* overwrites passwd with '\0's and frees it. */
106 void wipe_password (char *passwd
);
108 char *diff_two_paths (const char *first
, const char *second
);
110 /* Returns the basename of fname. The result is a pointer into fname. */
111 const char *x_basename (const char *fname
);
113 char *load_mc_home_file (const char *from
, const char *filename
, char **allocated_filename
);
115 /* uid/gid managing */
116 void init_groups (void);
117 void destroy_groups (void);
118 int get_user_permissions (struct stat
*buf
);
120 void init_uid_gid_cache (void);
121 char *get_group (int);
122 char *get_owner (int);
124 /* Check if the file exists. If not copy the default */
125 int check_for_default (const char *default_file
, const char *file
);
127 /* Returns a copy of *s until a \n is found and is below top */
128 const char *extract_line (const char *s
, const char *top
);
131 void open_error_pipe (void);
132 void check_error_pipe (void);
133 int close_error_pipe (int error
, const char *text
);
135 /* Process spawning */
136 int my_system (int flags
, const char *shell
, const char *command
);
137 void save_stop_handler (void);
139 /* Tilde expansion */
140 char *tilde_expand (const char *);
142 void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS
);
143 void canonicalize_pathname (char *);
145 /* Misc Unix functions */
146 int my_mkdir (const char *s
, mode_t mode
);
147 int my_rmdir (const char *s
);
149 /* Creating temporary files safely */
150 const char *mc_tmpdir (void);
151 int mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
);
154 #define mc_realpath realpath
156 char *mc_realpath (const char *path
, char *resolved_path
);
159 /* Looks for ``magic'' bytes at the start of the VFS file to guess the
160 * compression type. Side effect: modifies the file position. */
161 enum compression_type
get_compression_type (int fd
, const char *);
162 const char *decompress_extension (int type
);
164 GList
*list_append_unique (GList
* list
, char *text
);
166 /* Position saving and restoring */
167 /* Load position for the given filename */
168 void load_file_position (const char *filename
, long *line
, long *column
, off_t
* offset
,
170 /* Save position for the given filename */
171 void save_file_position (const char *filename
, long line
, long column
, off_t offset
,
175 /* if ch is in [A-Za-z], returns the corresponding control character,
176 * else returns the argument. */
177 extern int ascii_alpha_to_cntrl (int ch
);
180 const char *Q_ (const char *s
);
182 gboolean
mc_util_make_backup_if_possible (const char *, const char *);
183 gboolean
mc_util_restore_from_backup_if_possible (const char *, const char *);
184 gboolean
mc_util_unlink_backup_if_possible (const char *, const char *);
186 char *guess_message_value (void);
188 /*** inline functions **************************************************/
190 static inline gboolean
191 exist_file (const char *name
)
193 return (access (name
, R_OK
) == 0);
196 static inline gboolean
199 return (gboolean
) ((S_IXUSR
& mode
) || (S_IXGRP
& mode
) || (S_IXOTH
& mode
));
202 #endif /* MC_UTIL_H */