2004-12-03 19:17:46 +00:00
|
|
|
#ifndef MC_UTIL_H
|
|
|
|
#define MC_UTIL_H
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-02-07 16:10:33 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-02-06 14:01:28 +02:00
|
|
|
|
2005-04-13 18:47:12 +00:00
|
|
|
/* Returns its argument as a "modifiable" string. This function is
|
|
|
|
* intended to pass strings to legacy libraries that don't know yet
|
|
|
|
* about the "const" modifier. The return value of this function
|
|
|
|
* MUST NOT be modified. */
|
|
|
|
extern char *str_unconst (const char *);
|
|
|
|
|
1998-02-27 04:54:42 +00:00
|
|
|
/* String managing functions */
|
|
|
|
|
2004-12-02 20:08:06 +00:00
|
|
|
extern const char *cstrcasestr (const char *haystack, const char *needle);
|
2005-05-23 11:21:26 +00:00
|
|
|
extern const char *cstrstr (const char *haystack, const char *needle);
|
2004-12-02 20:08:06 +00:00
|
|
|
|
2004-08-19 11:26:48 +00:00
|
|
|
void str_replace(char *s, char from, char to);
|
1998-03-18 06:24:20 +00:00
|
|
|
int is_printable (int c);
|
2005-02-05 13:03:11 +00:00
|
|
|
void msglen (const char *text, /*@out@*/ int *lines, /*@out@*/ int *columns);
|
2004-08-29 16:42:40 +00:00
|
|
|
|
|
|
|
/* Copy from s to d, and trim the beginning if necessary, and prepend
|
|
|
|
* "..." in this case. The destination string can have at most len
|
|
|
|
* bytes, not counting trailing 0. */
|
|
|
|
char *trim (const char *s, char *d, int len);
|
|
|
|
|
|
|
|
/* Quote the filename for the purpose of inserting it into the command
|
|
|
|
* line. If quote_percent is 1, replace "%" with "%%" - the percent is
|
|
|
|
* processed by the mc command line. */
|
1998-02-27 04:54:42 +00:00
|
|
|
char *name_quote (const char *c, int quote_percent);
|
2004-08-29 16:42:40 +00:00
|
|
|
|
|
|
|
/* returns a duplicate of c. */
|
1998-02-27 04:54:42 +00:00
|
|
|
char *fake_name_quote (const char *c, int quote_percent);
|
2004-08-29 16:42:40 +00:00
|
|
|
|
|
|
|
/* Remove the middle part of the string to fit given length.
|
|
|
|
* Use "~" to show where the string was truncated.
|
|
|
|
* Return static buffer, no need to free() it. */
|
2009-02-01 22:44:44 +02:00
|
|
|
const char *name_trunc (const char *txt, size_t trunc_len);
|
2004-11-03 19:43:17 +00:00
|
|
|
|
|
|
|
/* path_trunc() is the same as name_trunc() above but
|
|
|
|
* it deletes possible password from path for security
|
|
|
|
* reasons. */
|
2009-02-01 22:44:44 +02:00
|
|
|
const char *path_trunc (const char *path, size_t trunc_len);
|
2004-08-29 16:42:40 +00:00
|
|
|
|
|
|
|
/* return a static string representing size, appending "K" or "M" for
|
|
|
|
* big sizes.
|
|
|
|
* NOTE: uses the same static buffer as size_trunc_sep. */
|
|
|
|
const char *size_trunc (double size);
|
|
|
|
|
|
|
|
/* return a static string representing size, appending "K" or "M" for
|
|
|
|
* big sizes. Separates every three digits by ",".
|
|
|
|
* NOTE: uses the same static buffer as size_trunc. */
|
|
|
|
const char *size_trunc_sep (double size);
|
|
|
|
|
|
|
|
/* Print file SIZE to BUFFER, but don't exceed LEN characters,
|
|
|
|
* not including trailing 0. BUFFER should be at least LEN+1 long.
|
|
|
|
*
|
|
|
|
* Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
|
2001-08-30 16:41:08 +00:00
|
|
|
void size_trunc_len (char *buffer, int len, off_t size, int units);
|
1998-03-18 06:24:20 +00:00
|
|
|
int is_exe (mode_t mode);
|
2004-08-29 23:20:01 +00:00
|
|
|
const char *string_perm (mode_t mode_bits);
|
|
|
|
|
|
|
|
/* @modifies path. @returns pointer into path. */
|
1999-02-03 23:19:40 +00:00
|
|
|
char *strip_password (char *path, int has_prefix);
|
2004-08-29 23:20:01 +00:00
|
|
|
|
|
|
|
/* @returns a pointer into a static buffer. */
|
|
|
|
const char *strip_home_and_password (const char *dir);
|
|
|
|
|
|
|
|
const char *extension (const char *);
|
2009-02-06 11:17:03 +01:00
|
|
|
char *concat_dir_and_file (const char *dir, const char *file);
|
2004-08-29 23:20:01 +00:00
|
|
|
const char *unix_error_string (int error_num);
|
|
|
|
const char *skip_separators (const char *s);
|
|
|
|
const char *skip_numbers (const char *s);
|
1998-02-27 04:54:42 +00:00
|
|
|
char *strip_ctrl_codes (char *s);
|
2004-08-29 23:20:01 +00:00
|
|
|
|
2004-09-26 18:36:54 +00:00
|
|
|
/* Replaces "\\E" and "\\e" with "\033". Replaces "^" + [a-z] with
|
|
|
|
* ((char) 1 + (c - 'a')). The same goes for "^" + [A-Z].
|
|
|
|
* Returns a newly allocated string. */
|
|
|
|
char *convert_controls (const char *s);
|
2004-08-29 23:20:01 +00:00
|
|
|
|
|
|
|
/* overwrites passwd with '\0's and frees it. */
|
1998-02-27 04:54:42 +00:00
|
|
|
void wipe_password (char *passwd);
|
2004-08-29 23:20:01 +00:00
|
|
|
|
|
|
|
char *diff_two_paths (const char *first, const char *second);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
2004-08-16 18:19:37 +00:00
|
|
|
/* Returns the basename of fname. The result is a pointer into fname. */
|
|
|
|
const char *x_basename (const char *fname);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Profile managing functions */
|
2004-08-29 23:20:01 +00:00
|
|
|
int set_int (const char *, const char *, int);
|
|
|
|
int get_int (const char *, const char *, int);
|
2005-09-07 08:47:22 +00:00
|
|
|
extern char * get_config_string (const char *, const char *, const char *);
|
|
|
|
extern void set_config_string (const char *, const char *, const char *);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
2004-08-29 23:20:01 +00:00
|
|
|
char *load_file (const char *filename);
|
2001-06-09 07:13:46 +00:00
|
|
|
char *load_mc_home_file (const char *filename, char ** allocated_filename);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* uid/gid managing */
|
|
|
|
void init_groups (void);
|
2001-09-01 13:47:34 +00:00
|
|
|
void destroy_groups (void);
|
|
|
|
int get_user_permissions (struct stat *buf);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
void init_uid_gid_cache (void);
|
|
|
|
char *get_group (int);
|
|
|
|
char *get_owner (int);
|
|
|
|
|
1999-08-06 19:24:04 +00:00
|
|
|
#define MAX_I18NTIMELENGTH 14
|
|
|
|
#define MIN_I18NTIMELENGTH 10
|
|
|
|
#define STD_I18NTIMELENGTH 12
|
|
|
|
|
1999-04-30 12:41:41 +00:00
|
|
|
size_t i18n_checktimelength (void);
|
2004-08-29 23:20:01 +00:00
|
|
|
const char *file_date (time_t);
|
1999-08-06 19:24:04 +00:00
|
|
|
|
2004-08-29 23:20:01 +00:00
|
|
|
int exist_file (const char *name);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Returns a copy of *s until a \n is found and is below top */
|
2004-08-29 23:20:01 +00:00
|
|
|
const char *extract_line (const char *s, const char *top);
|
|
|
|
const char *_icase_search (const char *text, const char *data, int *lng);
|
1998-02-27 04:54:42 +00:00
|
|
|
#define icase_search(T,D) _icase_search((T), (D), NULL)
|
|
|
|
|
|
|
|
/* Matching */
|
2003-06-07 00:37:28 +00:00
|
|
|
enum {
|
|
|
|
match_file, /* match a filename, use easy_patterns */
|
|
|
|
match_normal, /* match pattern, use easy_patterns */
|
2004-08-17 11:13:10 +00:00
|
|
|
match_regex /* match pattern, force using regex */
|
2003-06-07 00:37:28 +00:00
|
|
|
};
|
|
|
|
|
1998-02-27 04:54:42 +00:00
|
|
|
extern int easy_patterns;
|
2004-08-29 23:20:01 +00:00
|
|
|
char *convert_pattern (const char *pattern, int match_type, int do_group);
|
|
|
|
int regexp_match (const char *pattern, const char *string, int match_type);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Error pipes */
|
|
|
|
void open_error_pipe (void);
|
|
|
|
void check_error_pipe (void);
|
2004-08-29 23:20:01 +00:00
|
|
|
int close_error_pipe (int error, const char *text);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Process spawning */
|
1998-04-24 01:08:06 +00:00
|
|
|
int my_system (int flags, const char *shell, const char *command);
|
1998-02-27 04:54:42 +00:00
|
|
|
void save_stop_handler (void);
|
|
|
|
extern struct sigaction startup_handler;
|
|
|
|
|
|
|
|
/* Tilde expansion */
|
2000-04-18 08:58:42 +00:00
|
|
|
char *tilde_expand (const char *);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Pathname canonicalization */
|
2004-01-23 23:53:37 +00:00
|
|
|
void canonicalize_pathname (char *);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Misc Unix functions */
|
|
|
|
char *get_current_wd (char *buffer, int size);
|
2004-08-29 23:20:01 +00:00
|
|
|
int my_mkdir (const char *s, mode_t mode);
|
|
|
|
int my_rmdir (const char *s);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Rotating dash routines */
|
|
|
|
void use_dash (int flag); /* Disable/Enable rotate_dash routines */
|
|
|
|
void rotate_dash (void);
|
|
|
|
|
2001-05-21 16:21:07 +00:00
|
|
|
/* Creating temporary files safely */
|
2002-12-26 11:38:34 +00:00
|
|
|
const char *mc_tmpdir (void);
|
2001-05-21 16:21:07 +00:00
|
|
|
int mc_mkstemps(char **pname, const char *prefix, const char *suffix);
|
|
|
|
|
2005-01-13 19:37:46 +00:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#ifdef _POSIX_VERSION
|
|
|
|
#define PATH_MAX _POSIX_PATH_MAX
|
|
|
|
#else
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#else
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAXSYMLINKS
|
|
|
|
#define MAXSYMLINKS 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *mc_realpath(const char *path, char resolved_path[]);
|
|
|
|
|
2005-07-14 06:31:29 +00:00
|
|
|
enum compression_type {
|
2002-07-02 21:09:25 +00:00
|
|
|
COMPRESSION_NONE,
|
|
|
|
COMPRESSION_GZIP,
|
|
|
|
COMPRESSION_BZIP,
|
|
|
|
COMPRESSION_BZIP2
|
1998-02-27 04:54:42 +00:00
|
|
|
};
|
|
|
|
|
2005-07-14 06:31:29 +00:00
|
|
|
/* Looks for ``magic'' bytes at the start of the VFS file to guess the
|
|
|
|
* compression type. Side effect: modifies the file position. */
|
|
|
|
enum compression_type get_compression_type (int fd);
|
2002-11-12 11:20:08 +00:00
|
|
|
const char *decompress_extension (int type);
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* Hook functions */
|
|
|
|
|
|
|
|
typedef struct hook {
|
|
|
|
void (*hook_fn)(void *);
|
|
|
|
void *hook_data;
|
|
|
|
struct hook *next;
|
|
|
|
} Hook;
|
|
|
|
|
|
|
|
void add_hook (Hook **hook_list, void (*hook_fn)(void *), void *data);
|
|
|
|
void execute_hooks (Hook *hook_list);
|
|
|
|
void delete_hook (Hook **hook_list, void (*hook_fn)(void *));
|
|
|
|
int hook_present (Hook *hook_list, void (*hook_fn)(void *));
|
|
|
|
|
2003-02-18 06:12:57 +00:00
|
|
|
GList *list_append_unique (GList *list, char *text);
|
2002-12-08 06:51:22 +00:00
|
|
|
|
|
|
|
/* Position saving and restoring */
|
|
|
|
|
|
|
|
/* file where positions are stored */
|
|
|
|
#define MC_FILEPOS ".mc/filepos"
|
|
|
|
/* temporary file */
|
|
|
|
#define MC_FILEPOS_TMP ".mc/filepos.tmp"
|
|
|
|
/* maximum entries in MC_FILEPOS */
|
|
|
|
#define MC_FILEPOS_ENTRIES 1024
|
|
|
|
/* Load position for the given filename */
|
2004-08-29 23:20:01 +00:00
|
|
|
void load_file_position (const char *filename, long *line, long *column);
|
2002-12-08 06:51:22 +00:00
|
|
|
/* Save position for the given filename */
|
2004-08-29 23:20:01 +00:00
|
|
|
void save_file_position (const char *filename, long line, long column);
|
2002-12-08 06:51:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* OS specific defines */
|
2003-07-23 03:22:32 +00:00
|
|
|
#define PATH_SEP '/'
|
|
|
|
#define PATH_SEP_STR "/"
|
|
|
|
#define PATH_ENV_SEP ':'
|
|
|
|
#define TMPDIR_DEFAULT "/tmp"
|
|
|
|
#define SCRIPT_SUFFIX ""
|
|
|
|
#define get_default_editor() "vi"
|
|
|
|
#define OS_SORT_CASE_SENSITIVE_DEFAULT 1
|
|
|
|
#define STRCOMP strcmp
|
|
|
|
#define STRNCOMP strncmp
|
|
|
|
#define MC_ARCH_FLAGS 0
|
1998-02-27 04:54:42 +00:00
|
|
|
|
|
|
|
/* taken from regex.c: */
|
|
|
|
/* Jim Meyering writes:
|
|
|
|
|
|
|
|
"... Some ctype macros are valid only for character codes that
|
|
|
|
isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
|
|
|
|
using /bin/cc or gcc but without giving an ansi option). So, all
|
|
|
|
ctype uses should be through macros like ISPRINT... If
|
|
|
|
STDC_HEADERS is defined, then autoconf has verified that the ctype
|
|
|
|
macros don't need to be guarded with references to isascii. ...
|
|
|
|
Defining isascii to 1 should let any compiler worth its salt
|
|
|
|
eliminate the && through constant folding." */
|
|
|
|
|
|
|
|
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
|
|
|
|
#define ISASCII(c) 1
|
|
|
|
#else
|
|
|
|
#define ISASCII(c) isascii(c)
|
|
|
|
#endif
|
|
|
|
|
2005-02-08 10:46:01 +00:00
|
|
|
/* usage: str_cmp ("foo", !=, "bar") */
|
|
|
|
#define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0)
|
|
|
|
|
2005-07-18 09:31:37 +00:00
|
|
|
/* if ch is in [A-Za-z], returns the corresponding control character,
|
|
|
|
* else returns the argument. */
|
|
|
|
extern int ascii_alpha_to_cntrl (int ch);
|
|
|
|
|
2005-11-03 02:18:38 +00:00
|
|
|
#undef Q_
|
|
|
|
const char *Q_ (const char *s);
|
|
|
|
|
2009-02-06 14:01:28 +02:00
|
|
|
|
|
|
|
gboolean shell_is_char_escaped ( const char * );
|
2009-02-06 14:50:22 +01:00
|
|
|
char *shell_unescape( const char * );
|
2009-02-06 14:01:28 +02:00
|
|
|
char *shell_escape( const char * );
|
|
|
|
|
|
|
|
#define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))
|
2009-02-07 16:10:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* strcpy is unsafe on overlapping memory areas, so define memmove-alike
|
|
|
|
* string function.
|
|
|
|
* Have sense only when:
|
|
|
|
* * dest <= src
|
|
|
|
* AND
|
|
|
|
* * dest and str are pointers to one object (as Roland Illig pointed).
|
|
|
|
*
|
|
|
|
* We can't use str*cpy funs here:
|
|
|
|
* http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
|
|
|
|
*/
|
|
|
|
static inline char * str_move(char * dest, const char * src)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
assert (dest<=src);
|
|
|
|
|
|
|
|
n = strlen (src) + 1; /* + '\0' */
|
|
|
|
|
|
|
|
return memmove (dest, src, n);
|
|
|
|
}
|
2009-02-06 14:01:28 +02:00
|
|
|
|
|
|
|
#define MC_PTR_FREE(ptr) do { g_free(ptr); (ptr) = NULL; } while (0)
|
|
|
|
|
2004-12-03 19:17:46 +00:00
|
|
|
#endif
|