mc/lib/util.h

276 lines
9.0 KiB
C
Raw Permalink Normal View History

/** \file lib/util.h
* \brief Header: various utilities
*/
#ifndef MC_UTIL_H
#define MC_UTIL_H
1998-02-27 07:54:42 +03:00
#include <sys/types.h>
2009-02-06 02:30:45 +03:00
#include <sys/stat.h>
#include <inttypes.h> /* uintmax_t */
#include <unistd.h>
#include "lib/global.h" /* include <glib.h> */
#include "lib/vfs/vfs.h"
/*** typedefs(not structures) and defined constants **********************************************/
#ifndef MAXSYMLINKS
#define MAXSYMLINKS 32
#endif
#define MAX_SAVED_BOOKMARKS 10
#define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0)
#define mc_return_if_error(mcerror) do { if (mcerror != NULL && *mcerror != NULL) return; } while (0)
#define mc_return_val_if_error(mcerror, mcvalue) do { if (mcerror != NULL && *mcerror != NULL) return mcvalue; } while (0)
#define whitespace(c) ((c) == ' ' || (c) == '\t')
#define whiteness(c) (whitespace (c) || (c) == '\n')
#define MC_PIPE_BUFSIZE BUF_8K
#define MC_PIPE_STREAM_EOF 0
#define MC_PIPE_STREAM_UNREAD -1
#define MC_PIPE_ERROR_CREATE_PIPE -2
#define MC_PIPE_ERROR_PARSE_COMMAND -3
#define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4
#define MC_PIPE_ERROR_READ -5
/*** enums ***************************************************************************************/
/* Pathname canonicalization */
typedef enum
{
CANON_PATH_JOINSLASHES = 1L << 0, /* Multiple '/'s are collapsed to a single '/'. */
CANON_PATH_REMSLASHDOTS = 1L << 1, /* Leading './'s, '/'s and trailing '/.'s are removed. */
CANON_PATH_REMDOUBLEDOTS = 1L << 3, /* Non-leading '../'s and trailing '..'s are handled by removing */
CANON_PATH_GUARDUNC = 1L << 4, /* Detect and preserve UNC paths: //server/... */
CANON_PATH_ALL = CANON_PATH_JOINSLASHES
| CANON_PATH_REMSLASHDOTS | CANON_PATH_REMDOUBLEDOTS | CANON_PATH_GUARDUNC
} CANON_PATH_FLAGS;
enum compression_type
{
COMPRESSION_NONE,
COMPRESSION_GZIP,
COMPRESSION_BZIP,
COMPRESSION_BZIP2,
COMPRESSION_LZIP,
COMPRESSION_LZ4,
COMPRESSION_LZMA,
COMPRESSION_XZ,
COMPRESSION_ZSTD,
};
/* stdout or stderr stream of child process */
typedef struct
{
/* file descriptor */
int fd;
/* data read from fd */
char buf[MC_PIPE_BUFSIZE];
/* positive: length of data in buf as before read as after;
* zero or negative before read: do not read drom fd;
* MC_PIPE_STREAM_EOF after read: EOF of fd;
* MC_PIPE_STREAM_UNREAD after read: there was not read from fd;
* MC_PIPE_ERROR_READ after read: reading error from fd.
*/
ssize_t len;
/* whether buf is null-terminated or not */
gboolean null_term;
/* error code in case of len == MC_PIPE_ERROR_READ */
int error;
} mc_pipe_stream_t;
/* Pipe descriptor for child process */
typedef struct
{
/* PID of child process */
GPid child_pid;
/* stdout of child process */
mc_pipe_stream_t out;
/* stderr of child process */
mc_pipe_stream_t err;
} mc_pipe_t;
/*** structures declarations (and typedefs of structures)*****************************************/
/* keys are set only during sorting */
typedef struct
{
/* File attributes */
size_t fnamelen;
char *fname;
struct stat st;
/* key used for comparing names */
char *sort_key;
/* key used for comparing extensions */
char *second_sort_key;
/* Flags */
struct
{
unsigned int marked:1; /* File marked in pane window */
unsigned int link_to_dir:1; /* If this is a link, does it point to directory? */
unsigned int stale_link:1; /* If this is a symlink and points to Charon's land */
unsigned int dir_size_computed:1; /* Size of directory was computed with dirsizes_cmd */
} f;
} file_entry_t;
/*** global variables defined in .c file *********************************************************/
extern struct sigaction startup_handler;
/*** declarations of public functions ************************************************************/
int is_printable (int c);
/* 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. */
char *name_quote (const char *c, gboolean quote_percent);
/* returns a duplicate of c. */
char *fake_name_quote (const char *c, gboolean quote_percent);
/* path_trunc() is the same as str_trunc() but
* it deletes possible password from path for security
* reasons. */
const char *path_trunc (const char *path, size_t trunc_len);
/* 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 (uintmax_t size, gboolean use_si);
/* 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 (uintmax_t size, gboolean use_si);
/* Return a static string representing size, appending "K" or "M" for
* big sizes. but don't exceed LEN characters,
* Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
const char *size_trunc_len (unsigned int len, uintmax_t size, unsigned int units, gboolean use_si);
const char *string_perm (mode_t mode_bits);
const char *extension (const char *);
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 07:54:42 +03:00
char *strip_ctrl_codes (char *s);
/* 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);
/* overwrites passwd with '\0's and frees it. */
1998-02-27 07:54:42 +03:00
void wipe_password (char *passwd);
char *diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
1998-02-27 07:54:42 +03:00
2004-08-16 22:19:37 +04:00
/* Returns the basename of fname. The result is a pointer into fname. */
const char *x_basename (const char *fname);
1998-02-27 07:54:42 +03:00
char *load_mc_home_file (const char *from, const char *filename, char **allocated_filename,
size_t * length);
1998-02-27 07:54:42 +03:00
/* uid/gid managing */
void init_groups (void);
void destroy_groups (void);
int get_user_permissions (struct stat *buf);
1998-02-27 07:54:42 +03:00
void init_uid_gid_cache (void);
const char *get_group (gid_t gid);
const char *get_owner (uid_t uid);
1998-02-27 07:54:42 +03:00
/* Returns a copy of *s until a \n is found and is below top */
const char *extract_line (const char *s, const char *top);
1998-02-27 07:54:42 +03:00
/* Error pipes */
void open_error_pipe (void);
void check_error_pipe (void);
int close_error_pipe (int error, const char *text);
1998-02-27 07:54:42 +03:00
/* Process spawning */
int my_system (int flags, const char *shell, const char *command);
int my_systeml (int flags, const char *shell, ...);
int my_systemv (const char *command, char *const argv[]);
int my_systemv_flags (int flags, const char *command, char *const argv[]);
mc_pipe_t *mc_popen (const char *command, GError ** error);
void mc_pread (mc_pipe_t * p, GError ** error);
void mc_pclose (mc_pipe_t * p, GError ** error);
void my_exit (int status);
1998-02-27 07:54:42 +03:00
void save_stop_handler (void);
/* Tilde expansion */
2000-04-18 12:58:42 +04:00
char *tilde_expand (const char *);
1998-02-27 07:54:42 +03:00
Ticket #1828: Improved symlink handling in ftpfs Originally from: http://mail.gnome.org/archives/mc-devel/2005-April/msg00035.html The routines `vfs_s_find_entry_linear()' and `vfs_s_find_entry_tree()' call `canonicalize_pathname()' on entry. This routine eats `..' path components in certain cases. In case of ftpfs this is not desired - the path should be kept as is since the code in direntry.c doesn't have enough knowledge of the directory structure on the remote end. Assume that there is a path like this on the remote server /path1/path2/path3 The `path2' component is a symlink to some directory and `path3' is a symlink stored in `path2' which is relative to `path2' i.e. path2 path3 -> ../some/other/path Now, the code in direntry.c will determine that `path3' is a symlink and will try to resolve (vfs_s_resolve_symlink) it by passing the following path /path1/path2/../some/other/path to `vfs_s_find_entry_linear' . As I've said above this routine calls `canonicalize_pathname' on entry which will modify the path like this: /path1/some/other/path Now this is clearly wrong since `path2' is a symlink and it should be resolved first. In the case of ftpfs the code in direntry.c doesn't have enough knowledge about physycal directory layout on the remote filesystem so it shouldn't try to canonicalize the path. The path should be left as is and passed to the remote end for processing. Fix issue: Changed function canonicalize_pathname (as fact, renamed to custom_canonicalize_pathname) In this function added ability to partial canonicalize of pathname. And some functions from vfs/direntry.c calls custom_canonicalize_pathname without removal of '..' stuff. Signed-off-by: Slava Zanko <slavazanko@gmail.com>
2009-11-23 11:45:46 +03:00
void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS);
void canonicalize_pathname (char *);
1998-02-27 07:54:42 +03:00
char *mc_realpath (const char *path, char *resolved_path);
/* 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, const char *);
const char *decompress_extension (int type);
1998-02-27 07:54:42 +03:00
GList *list_append_unique (GList * list, char *text);
/* Position saving and restoring */
/* Load position for the given filename */
void load_file_position (const vfs_path_t * filename_vpath, long *line, long *column,
off_t * offset, GArray ** bookmarks);
/* Save position for the given filename */
void save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
GArray * bookmarks);
/* if ch is in [A-Za-z], returns the corresponding control character,
* else returns the argument. */
extern int ascii_alpha_to_cntrl (int ch);
#undef Q_
const char *Q_ (const char *s);
gboolean mc_util_make_backup_if_possible (const char *, const char *);
gboolean mc_util_restore_from_backup_if_possible (const char *, const char *);
gboolean mc_util_unlink_backup_if_possible (const char *, const char *);
char *guess_message_value (void);
char *mc_build_filename (const char *first_element, ...);
char *mc_build_filenamev (const char *first_element, va_list args);
const char *mc_get_profile_root (void);
/* *INDENT-OFF* */
void mc_propagate_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
void mc_replace_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4);
/* *INDENT-ON* */
gboolean mc_time_elapsed (guint64 * timestamp, guint64 delay);
/*** inline functions **************************************************/
static inline gboolean
exist_file (const char *name)
{
return (access (name, R_OK) == 0);
}
static inline gboolean
is_exe (mode_t mode)
{
return (gboolean) ((S_IXUSR & mode) || (S_IXGRP & mode) || (S_IXOTH & mode));
}
#endif /* MC_UTIL_H */