2012-08-31 18:05:29 +04:00
|
|
|
/** \file lib/util.h
|
2009-02-05 21:28:18 +03:00
|
|
|
* \brief Header: various utilities
|
|
|
|
*/
|
|
|
|
|
2004-12-03 22:17:46 +03:00
|
|
|
#ifndef MC_UTIL_H
|
|
|
|
#define MC_UTIL_H
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
#include <sys/types.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <sys/stat.h>
|
2010-11-27 16:35:59 +03:00
|
|
|
#include <inttypes.h> /* uintmax_t */
|
2009-05-10 19:01:15 +04:00
|
|
|
#include <unistd.h>
|
2009-02-06 15:01:28 +03:00
|
|
|
|
2015-04-11 12:30:34 +03:00
|
|
|
#include "lib/global.h" /* include <glib.h> */
|
|
|
|
|
2011-09-27 23:13:50 +04:00
|
|
|
#include "lib/vfs/vfs.h"
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** 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)
|
|
|
|
|
2014-07-15 16:53:06 +04:00
|
|
|
#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)
|
|
|
|
|
2016-12-11 21:31:22 +03:00
|
|
|
#define whitespace(c) ((c) == ' ' || (c) == '\t')
|
|
|
|
#define whiteness(c) (whitespace (c) || (c) == '\n')
|
|
|
|
|
2014-07-03 09:58:57 +04:00
|
|
|
#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
|
2014-07-15 16:53:06 +04:00
|
|
|
|
2020-07-26 19:39:16 +03:00
|
|
|
/* gnulib efa15594e17fc20827dba66414fb391e99905394
|
|
|
|
|
|
|
|
*_GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2.
|
|
|
|
* It returns
|
|
|
|
* 1 if n1 > n2
|
|
|
|
* 0 if n1 == n2
|
|
|
|
* -1 if n1 < n2
|
|
|
|
* The native code (n1 > n2 ? 1 : n1 < n2 ? -1 : 0) produces a conditional
|
|
|
|
* jump with nearly all GCC versions up to GCC 10.
|
|
|
|
* This variant (n1 < n2 ? -1 : n1 > n2) produces a conditional with many
|
|
|
|
* GCC versions up to GCC 9.
|
|
|
|
* The better code (n1 > n2) - (n1 < n2) from Hacker's Delight para 2-9
|
|
|
|
* avoids conditional jumps in all GCC versions >= 3.4.
|
|
|
|
*/
|
|
|
|
#define _GL_CMP(n1, n2) (((n1) > (n2)) - ((n1) < (n2)))
|
|
|
|
|
2022-05-29 12:32:56 +03:00
|
|
|
/* Difference or zero */
|
2021-05-16 14:10:30 +03:00
|
|
|
#define DOZ(a, b) ((a) > (b) ? (a) - (b) : 0)
|
|
|
|
|
2024-03-10 11:11:18 +03:00
|
|
|
/* flags for shell_execute */
|
|
|
|
#define EXECUTE_INTERNAL (1 << 0)
|
|
|
|
#define EXECUTE_AS_SHELL (1 << 2)
|
|
|
|
#define EXECUTE_HIDE (1 << 3)
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** enums ***************************************************************************************/
|
|
|
|
|
|
|
|
/* Pathname canonicalization */
|
2022-05-07 11:23:42 +03:00
|
|
|
/* *INDENT-OFF* */
|
2010-11-08 13:21:45 +03:00
|
|
|
typedef enum
|
|
|
|
{
|
2022-05-07 11:23:42 +03:00
|
|
|
CANON_PATH_NOCHANGE = 0,
|
|
|
|
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
|
|
|
|
portions of the path */
|
|
|
|
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 /**< All flags */
|
|
|
|
} canon_path_flags_t;
|
|
|
|
/* *INDENT-ON* */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
enum compression_type
|
|
|
|
{
|
|
|
|
COMPRESSION_NONE,
|
2022-10-02 11:27:34 +03:00
|
|
|
COMPRESSION_ZIP,
|
2010-11-08 13:21:45 +03:00
|
|
|
COMPRESSION_GZIP,
|
|
|
|
COMPRESSION_BZIP,
|
|
|
|
COMPRESSION_BZIP2,
|
2016-01-10 10:36:19 +03:00
|
|
|
COMPRESSION_LZIP,
|
2015-12-27 22:53:02 +03:00
|
|
|
COMPRESSION_LZ4,
|
2010-11-08 13:21:45 +03:00
|
|
|
COMPRESSION_LZMA,
|
2023-11-19 17:30:37 +03:00
|
|
|
COMPRESSION_LZO,
|
2018-03-18 20:23:33 +03:00
|
|
|
COMPRESSION_XZ,
|
|
|
|
COMPRESSION_ZSTD,
|
2010-11-08 13:21:45 +03:00
|
|
|
};
|
|
|
|
|
2014-07-03 09:58:57 +04:00
|
|
|
/* stdout or stderr stream of child process */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* file descriptor */
|
|
|
|
int fd;
|
|
|
|
/* data read from fd */
|
|
|
|
char buf[MC_PIPE_BUFSIZE];
|
2021-02-10 13:16:18 +03:00
|
|
|
/* current position in @buf (used by mc_pstream_get_string()) */
|
|
|
|
size_t pos;
|
2021-02-24 14:10:52 +03:00
|
|
|
/* positive: length of data in buf;
|
|
|
|
* MC_PIPE_STREAM_EOF: EOF of fd;
|
|
|
|
* MC_PIPE_STREAM_UNREAD: there was not read from fd;
|
|
|
|
* MC_PIPE_ERROR_READ: reading error from fd.
|
2014-07-03 09:58:57 +04:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** structures declarations (and typedefs of structures)*****************************************/
|
|
|
|
|
|
|
|
/*** global variables defined in .c file *********************************************************/
|
|
|
|
|
|
|
|
extern struct sigaction startup_handler;
|
|
|
|
|
|
|
|
/*** declarations of public functions ************************************************************/
|
|
|
|
|
2010-05-14 12:12:53 +04:00
|
|
|
int is_printable (int c);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
|
|
|
/* 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. */
|
2014-09-03 10:59:37 +04:00
|
|
|
char *name_quote (const char *c, gboolean quote_percent);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
|
|
|
/* returns a duplicate of c. */
|
2014-09-03 10:59:37 +04:00
|
|
|
char *fake_name_quote (const char *c, gboolean quote_percent);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
2010-11-09 21:53:22 +03:00
|
|
|
/* path_trunc() is the same as str_trunc() but
|
2004-11-03 22:43:17 +03:00
|
|
|
* it deletes possible password from path for security
|
|
|
|
* reasons. */
|
2009-02-01 23:44:44 +03:00
|
|
|
const char *path_trunc (const char *path, size_t trunc_len);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
|
|
|
/* return a static string representing size, appending "K" or "M" for
|
|
|
|
* big sizes.
|
|
|
|
* NOTE: uses the same static buffer as size_trunc_sep. */
|
2010-11-27 16:35:59 +03:00
|
|
|
const char *size_trunc (uintmax_t size, gboolean use_si);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
|
|
|
/* 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. */
|
2010-11-27 16:35:59 +03:00
|
|
|
const char *size_trunc_sep (uintmax_t size, gboolean use_si);
|
2004-08-29 20:42:40 +04:00
|
|
|
|
|
|
|
/* Print file SIZE to BUFFER, but don't exceed LEN characters,
|
2016-11-08 13:19:29 +03:00
|
|
|
* not including trailing 0. BUFFER should be at least LEN+1 long.
|
2004-08-29 20:42:40 +04:00
|
|
|
*
|
|
|
|
* Units: size units (0=bytes, 1=Kbytes, 2=Mbytes, etc.) */
|
2010-11-27 16:35:59 +03:00
|
|
|
void size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si);
|
2004-08-30 03:20:01 +04:00
|
|
|
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);
|
2004-08-30 03:20:01 +04:00
|
|
|
|
2004-09-26 22:36:54 +04: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-30 03:20:01 +04:00
|
|
|
|
|
|
|
/* overwrites passwd with '\0's and frees it. */
|
1998-02-27 07:54:42 +03:00
|
|
|
void wipe_password (char *passwd);
|
2004-08-30 03:20:01 +04:00
|
|
|
|
2011-09-27 23:13:50 +04:00
|
|
|
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
|
|
|
|
2016-01-11 16:17:08 +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);
|
2001-09-01 17:47:34 +04:00
|
|
|
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);
|
2015-06-24 16:09:58 +03:00
|
|
|
const char *get_group (gid_t gid);
|
2015-06-24 16:14:36 +03:00
|
|
|
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 */
|
2004-08-30 03:20:01 +04:00
|
|
|
const char *extract_line (const char *s, const char *top);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Process spawning */
|
1998-04-24 05:08:06 +04:00
|
|
|
int my_system (int flags, const char *shell, const char *command);
|
2013-01-09 21:25:04 +04:00
|
|
|
int my_systeml (int flags, const char *shell, ...);
|
|
|
|
int my_systemv (const char *command, char *const argv[]);
|
2013-01-10 17:22:59 +04:00
|
|
|
int my_systemv_flags (int flags, const char *command, char *const argv[]);
|
2013-01-09 21:25:04 +04:00
|
|
|
|
2021-02-24 14:10:52 +03:00
|
|
|
mc_pipe_t *mc_popen (const char *command, gboolean read_out, gboolean read_err, GError ** error);
|
2014-07-03 09:58:57 +04:00
|
|
|
void mc_pread (mc_pipe_t * p, GError ** error);
|
|
|
|
void mc_pclose (mc_pipe_t * p, GError ** error);
|
|
|
|
|
2021-02-10 13:16:18 +03:00
|
|
|
GString *mc_pstream_get_string (mc_pipe_stream_t * ps);
|
|
|
|
|
2013-01-05 14:48:50 +04:00
|
|
|
void my_exit (int status);
|
1998-02-27 07:54:42 +03:00
|
|
|
void save_stop_handler (void);
|
|
|
|
|
|
|
|
/* Tilde expansion */
|
2021-05-16 13:56:44 +03:00
|
|
|
char *tilde_expand (const char *directory);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2022-05-07 11:23:42 +03:00
|
|
|
void canonicalize_pathname_custom (char *path, canon_path_flags_t flags);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2010-05-13 21:28:12 +04:00
|
|
|
char *mc_realpath (const char *path, char *resolved_path);
|
2005-01-13 22:37:46 +03:00
|
|
|
|
2013-05-26 10:11:06 +04:00
|
|
|
/* Looks for "magic" bytes at the start of the VFS file to guess the
|
2005-07-14 10:31:29 +04:00
|
|
|
* compression type. Side effect: modifies the file position. */
|
2021-05-16 13:56:44 +03:00
|
|
|
enum compression_type get_compression_type (int fd, const char *name);
|
2002-11-12 14:20:08 +03:00
|
|
|
const char *decompress_extension (int type);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2010-05-14 12:12:53 +04:00
|
|
|
GList *list_append_unique (GList * list, char *text);
|
2002-12-08 09:51:22 +03:00
|
|
|
|
|
|
|
/* Position saving and restoring */
|
|
|
|
/* Load position for the given filename */
|
2011-11-08 23:44:38 +04:00
|
|
|
void load_file_position (const vfs_path_t * filename_vpath, long *line, long *column,
|
|
|
|
off_t * offset, GArray ** bookmarks);
|
2002-12-08 09:51:22 +03:00
|
|
|
/* Save position for the given filename */
|
2011-11-08 23:44:38 +04:00
|
|
|
void save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
|
2010-11-22 17:15:28 +03:00
|
|
|
GArray * bookmarks);
|
2002-12-08 09:51:22 +03:00
|
|
|
|
|
|
|
|
2005-07-18 13:31:37 +04: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 05:18:38 +03:00
|
|
|
#undef Q_
|
|
|
|
const char *Q_ (const char *s);
|
|
|
|
|
2021-05-16 13:56:44 +03:00
|
|
|
gboolean mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix);
|
|
|
|
gboolean mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix);
|
|
|
|
gboolean mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix);
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
char *guess_message_value (void);
|
|
|
|
|
2011-06-17 21:17:16 +04:00
|
|
|
char *mc_build_filename (const char *first_element, ...);
|
2011-07-17 20:59:47 +04:00
|
|
|
char *mc_build_filenamev (const char *first_element, va_list args);
|
2011-06-17 21:17:16 +04:00
|
|
|
|
2016-11-29 11:33:10 +03:00
|
|
|
const char *mc_get_profile_root (void);
|
|
|
|
|
2015-08-25 15:28:01 +03:00
|
|
|
/* *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* */
|
2014-07-15 16:53:06 +04:00
|
|
|
|
2020-11-22 15:40:18 +03:00
|
|
|
gboolean mc_time_elapsed (gint64 * timestamp, gint64 delay);
|
2014-09-10 09:35:23 +04:00
|
|
|
|
2022-11-13 15:23:04 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** inline functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-02-07 17:10:33 +03:00
|
|
|
|
2010-11-11 10:42:59 +03:00
|
|
|
static inline gboolean
|
|
|
|
exist_file (const char *name)
|
|
|
|
{
|
|
|
|
return (access (name, R_OK) == 0);
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:23:04 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-11-11 16:02:10 +03:00
|
|
|
static inline gboolean
|
|
|
|
is_exe (mode_t mode)
|
|
|
|
{
|
2021-05-16 12:09:12 +03:00
|
|
|
return ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0);
|
2010-11-11 16:02:10 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:23:04 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Canonicalize path with CANON_PATH_ALL.
|
|
|
|
*
|
|
|
|
* @param path path to file
|
|
|
|
* @param flags canonicalization flags
|
|
|
|
*
|
|
|
|
* All modifications of @path are made in place.
|
|
|
|
* Well formed UNC paths are modified only in the local part.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
canonicalize_pathname (char *path)
|
|
|
|
{
|
|
|
|
canonicalize_pathname_custom (path, CANON_PATH_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-05-14 12:12:53 +04:00
|
|
|
#endif /* MC_UTIL_H */
|