diff --git a/lib/filehighlight.h b/lib/filehighlight.h index 0999920de..5048410d7 100644 --- a/lib/filehighlight.h +++ b/lib/filehighlight.h @@ -11,7 +11,8 @@ /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_fhl_struct { +typedef struct mc_fhl_struct +{ mc_config_t *config; GPtrArray *filters; } mc_fhl_t; @@ -29,4 +30,5 @@ gboolean mc_fhl_read_ini_file (mc_fhl_t *, const gchar *); gboolean mc_fhl_parse_ini_file (mc_fhl_t *); void mc_fhl_clear (mc_fhl_t *); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/filehighlight/common.c b/lib/filehighlight/common.c index 56c7a04fd..0472fd761 100644 --- a/lib/filehighlight/common.c +++ b/lib/filehighlight/common.c @@ -82,12 +82,14 @@ mc_fhl_new (gboolean need_auto_fill) if (!need_auto_fill) return fhl; - if (!mc_fhl_init_from_standard_files (fhl)) { + if (!mc_fhl_init_from_standard_files (fhl)) + { g_free (fhl); return NULL; } - if (!mc_fhl_parse_ini_file (fhl)) { + if (!mc_fhl_parse_ini_file (fhl)) + { mc_fhl_free (&fhl); return NULL; } diff --git a/lib/filehighlight/get-color.c b/lib/filehighlight/get-color.c index 7ad0efdd8..ec8a0f89e 100644 --- a/lib/filehighlight/get-color.c +++ b/lib/filehighlight/get-color.c @@ -44,7 +44,7 @@ /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ -/*inline functions*/ +/*inline functions */ inline static gboolean mc_fhl_is_file (file_entry * fe) { @@ -141,8 +141,8 @@ inline static gboolean mc_fhl_is_special (file_entry * fe) { return - (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe) || mc_fhl_is_special_door (fe) - ); + (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe) + || mc_fhl_is_special_door (fe)); } @@ -154,7 +154,8 @@ mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_ent gboolean my_color = FALSE; (void) fhl; - switch (mc_filter->file_type) { + switch (mc_filter->file_type) + { case MC_FLHGH_FTYPE_T_FILE: if (mc_fhl_is_file (fe)) my_color = TRUE; @@ -254,9 +255,11 @@ mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe) if (fhl == NULL) return NORMAL_COLOR; - for (i = 0; i < fhl->filters->len; i++) { + for (i = 0; i < fhl->filters->len; i++) + { mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i); - switch (mc_filter->type) { + switch (mc_filter->type) + { case MC_FLHGH_T_FTYPE: ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe); if (ret > 0) diff --git a/lib/filehighlight/ini-file-read.c b/lib/filehighlight/ini-file-read.c index fc34678c7..07ef158fc 100644 --- a/lib/filehighlight/ini-file-read.c +++ b/lib/filehighlight/ini-file-read.c @@ -77,12 +77,14 @@ mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name) int i; gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", ""); - if (*param_type == '\0') { + if (*param_type == '\0') + { g_free (param_type); return FALSE; } - for (i = 0; types[i] != NULL; i++) { + for (i = 0; types[i] != NULL; i++) + { if (strcmp (types[i], param_type) == 0) break; } @@ -107,7 +109,8 @@ mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name) mc_fhl_filter_t *mc_filter; gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", ""); - if (*regexp == '\0') { + if (*regexp == '\0') + { g_free (regexp); return FALSE; } @@ -201,7 +204,8 @@ mc_fhl_init_from_standard_files (mc_fhl_t * fhl) /* ${datadir}/mc/filehighlight.ini */ name = concat_dir_and_file (mc_home_alt, MC_FHL_INI_FILE); - if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) { + if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) + { g_free (name); return FALSE; } @@ -209,7 +213,8 @@ mc_fhl_init_from_standard_files (mc_fhl_t * fhl) /* ${sysconfdir}/mc/filehighlight.ini */ name = concat_dir_and_file (mc_home, MC_FHL_INI_FILE); - if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) { + if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) + { g_free (name); return FALSE; } @@ -218,7 +223,8 @@ mc_fhl_init_from_standard_files (mc_fhl_t * fhl) /* ~/.mc/filehighlight.ini */ name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL); - if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) { + if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) + { g_free (name); return FALSE; } @@ -242,17 +248,21 @@ mc_fhl_parse_ini_file (mc_fhl_t * fhl) if (group_names == NULL) return FALSE; - while (*group_names) { + while (*group_names) + { - if (mc_config_has_param (fhl->config, *group_names, "type")) { + if (mc_config_has_param (fhl->config, *group_names, "type")) + { /* parse filetype filter */ mc_fhl_parse_get_file_type_id (fhl, *group_names); } - if (mc_config_has_param (fhl->config, *group_names, "regexp")) { + if (mc_config_has_param (fhl->config, *group_names, "regexp")) + { /* parse regexp filter */ mc_fhl_parse_get_regexp (fhl, *group_names); } - if (mc_config_has_param (fhl->config, *group_names, "extensions")) { + if (mc_config_has_param (fhl->config, *group_names, "extensions")) + { /* parse extensions filter */ mc_fhl_parse_get_extensions (fhl, *group_names); } diff --git a/lib/filehighlight/internal.h b/lib/filehighlight/internal.h index 4fc16e75c..6c6da56e1 100644 --- a/lib/filehighlight/internal.h +++ b/lib/filehighlight/internal.h @@ -5,13 +5,15 @@ /*** enums ***************************************************************************************/ -typedef enum { +typedef enum +{ MC_FLHGH_T_FTYPE, MC_FLHGH_T_EXT, MC_FLHGH_T_FREGEXP } mc_flhgh_filter_type; -typedef enum { +typedef enum +{ MC_FLHGH_FTYPE_T_FILE, MC_FLHGH_FTYPE_T_FILE_EXE, MC_FLHGH_FTYPE_T_DIR, @@ -31,7 +33,8 @@ typedef enum { /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_fhl_filter_struct { +typedef struct mc_fhl_filter_struct +{ int color_pair_index; gchar *fgcolor; @@ -51,5 +54,5 @@ void mc_fhl_array_free (mc_fhl_t *); gboolean mc_fhl_init_from_standard_files (mc_fhl_t *); - +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/fileloc.h b/lib/fileloc.h index 73878fe06..908e26b1f 100644 --- a/lib/fileloc.h +++ b/lib/fileloc.h @@ -1,4 +1,3 @@ - /** \file fileloc.h * \brief Header: config files list * @@ -14,65 +13,76 @@ #ifndef MC_FILELOC_H #define MC_FILELOC_H +/*** typedefs(not structures) and defined constants **********************************************/ + #ifndef MC_USERCONF_DIR -#define MC_USERCONF_DIR ".mc" +#define MC_USERCONF_DIR ".mc" #endif +#define TAGS_NAME "TAGS" -#define TAGS_NAME "TAGS" +#define MC_GLOBAL_CONFIG_FILE "mc.lib" +#define MC_GLOBAL_MENU "mc.menu" +#define MC_LOCAL_MENU ".mc.menu" +#define MC_HINT "hints" PATH_SEP_STR "mc.hint" +#define MC_HELP "help" PATH_SEP_STR "mc.hlp" +#define GLOBAL_KEYMAP_FILE "mc.keymap" +#define CHARSETS_LIST "mc.charsets" +#define MC_LIB_EXT "mc.ext" -#define MC_GLOBAL_CONFIG_FILE "mc.lib" -#define MC_GLOBAL_MENU "mc.menu" -#define MC_LOCAL_MENU ".mc.menu" -#define MC_HINT "hints" PATH_SEP_STR "mc.hint" -#define MC_HELP "help" PATH_SEP_STR "mc.hlp" -#define GLOBAL_KEYMAP_FILE "mc.keymap" -#define CHARSETS_LIST "mc.charsets" -#define MC_LIB_EXT "mc.ext" +#define FISH_PREFIX "fish" -#define FISH_PREFIX "fish" +#define FISH_LS_FILE "ls" +#define FISH_EXISTS_FILE "fexists" +#define FISH_MKDIR_FILE "mkdir" +#define FISH_UNLINK_FILE "unlink" +#define FISH_CHOWN_FILE "chown" +#define FISH_CHMOD_FILE "chmod" +#define FISH_RMDIR_FILE "rmdir" +#define FISH_LN_FILE "ln" +#define FISH_MV_FILE "mv" +#define FISH_HARDLINK_FILE "hardlink" +#define FISH_GET_FILE "get" +#define FISH_SEND_FILE "send" +#define FISH_APPEND_FILE "append" +#define FISH_INFO_FILE "info" -#define FISH_LS_FILE "ls" -#define FISH_EXISTS_FILE "fexists" -#define FISH_MKDIR_FILE "mkdir" -#define FISH_UNLINK_FILE "unlink" -#define FISH_CHOWN_FILE "chown" -#define FISH_CHMOD_FILE "chmod" -#define FISH_RMDIR_FILE "rmdir" -#define FISH_LN_FILE "ln" -#define FISH_MV_FILE "mv" -#define FISH_HARDLINK_FILE "hardlink" -#define FISH_GET_FILE "get" -#define FISH_SEND_FILE "send" -#define FISH_APPEND_FILE "append" -#define FISH_INFO_FILE "info" +#define MC_EXTFS_DIR "extfs.d" -#define MC_EXTFS_DIR "extfs.d" - -#define MC_BASHRC_FILE "bashrc" -#define MC_CONFIG_FILE "ini" -#define MC_FILEBIND_FILE "bindings" -#define MC_FILEPOS_FILE "filepos" -#define MC_HISTORY_FILE "history" -#define MC_HOTLIST_FILE "hotlist" -#define MC_USERMENU_FILE "menu" -#define MC_TREESTORE_FILE "Tree" -#define MC_PANELS_FILE "panels.ini" -#define MC_FHL_INI_FILE "filehighlight.ini" -#define MC_SKINS_SUBDIR "skins" +#define MC_BASHRC_FILE "bashrc" +#define MC_CONFIG_FILE "ini" +#define MC_FILEBIND_FILE "bindings" +#define MC_FILEPOS_FILE "filepos" +#define MC_HISTORY_FILE "history" +#define MC_HOTLIST_FILE "hotlist" +#define MC_USERMENU_FILE "menu" +#define MC_TREESTORE_FILE "Tree" +#define MC_PANELS_FILE "panels.ini" +#define MC_FHL_INI_FILE "filehighlight.ini" +#define MC_SKINS_SUBDIR "skins" /* editor home directory */ -#define EDIT_DIR MC_USERCONF_DIR PATH_SEP_STR "cedit" +#define EDIT_DIR MC_USERCONF_DIR PATH_SEP_STR "cedit" /* file names */ -#define EDIT_SYNTAX_FILE EDIT_DIR PATH_SEP_STR "Syntax" -#define EDIT_CLIP_FILE EDIT_DIR PATH_SEP_STR "cooledit.clip" -#define EDIT_MACRO_FILE EDIT_DIR PATH_SEP_STR "cooledit.macros" -#define EDIT_BLOCK_FILE EDIT_DIR PATH_SEP_STR "cooledit.block" -#define EDIT_TEMP_FILE EDIT_DIR PATH_SEP_STR "cooledit.temp" +#define EDIT_SYNTAX_FILE EDIT_DIR PATH_SEP_STR "Syntax" +#define EDIT_CLIP_FILE EDIT_DIR PATH_SEP_STR "cooledit.clip" +#define EDIT_MACRO_FILE EDIT_DIR PATH_SEP_STR "cooledit.macros" +#define EDIT_BLOCK_FILE EDIT_DIR PATH_SEP_STR "cooledit.block" +#define EDIT_TEMP_FILE EDIT_DIR PATH_SEP_STR "cooledit.temp" -#define EDIT_GLOBAL_MENU "cedit.menu" -#define EDIT_LOCAL_MENU ".cedit.menu" -#define EDIT_HOME_MENU EDIT_DIR PATH_SEP_STR "menu" +#define EDIT_GLOBAL_MENU "cedit.menu" +#define EDIT_LOCAL_MENU ".cedit.menu" +#define EDIT_HOME_MENU EDIT_DIR PATH_SEP_STR "menu" + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/fs.h b/lib/fs.h index edb7e09e1..0afb929dc 100644 --- a/lib/fs.h +++ b/lib/fs.h @@ -1,4 +1,3 @@ - /** \file fs.h * \brief Header: fs compatibility definitions */ @@ -11,7 +10,9 @@ #include #include #include +#include +/*** typedefs(not structures) and defined constants **********************************************/ /* Replacement for permission bits missing in sys/stat.h */ #ifndef S_ISLNK @@ -52,7 +53,6 @@ #endif /* unistd.h defines _POSIX_VERSION on POSIX.1 systems. */ -#include #define NLENGTH(dirent) (strlen ((dirent)->d_name)) #define DIRENT_LENGTH_COMPUTED 1 @@ -62,6 +62,16 @@ # define MC_MAXFILENAMELEN MAXNAMLEN #endif +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ + static inline void compute_namelen (struct dirent *dent __attribute__ ((unused))) { diff --git a/lib/glibcompat.c b/lib/glibcompat.c index 6bbf7bc91..7bf3510fd 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -21,7 +21,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ + */ /** \file glibcompat.c * \brief Source: compatibility with older versions of glib @@ -50,10 +50,10 @@ #if ! GLIB_CHECK_VERSION (2, 13, 0) /* - This is incomplete copy of same glib-function. - For older glib (less than 2.13) functional is enought. - For full version of glib welcome to glib update. -*/ + This is incomplete copy of same glib-function. + For older glib (less than 2.13) functional is enought. + For full version of glib welcome to glib update. + */ gboolean g_unichar_iszerowidth (gunichar c) { diff --git a/lib/glibcompat.h b/lib/glibcompat.h index b87e0ea99..920db53c8 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -19,5 +19,6 @@ gboolean g_unichar_iszerowidth (gunichar); gboolean g_file_set_contents (const gchar *, const gchar *, gssize, GError **); #endif /* ! GLIB_CHECK_VERSION (2, 7, 0) */ +/*** inline functions ****************************************************************************/ #endif /* MC_GLIBCOMPAT_H */ diff --git a/lib/global.h b/lib/global.h index d48cbef26..f4fcef024 100644 --- a/lib/global.h +++ b/lib/global.h @@ -1,47 +1,47 @@ - /** \file global.h * \brief Header: %global definitions for compatibility * * This file should be included after all system includes and before all local includes. */ - #ifndef MC_GLOBAL_H #define MC_GLOBAL_H #if defined(STDC_HEADERS) || defined(HAVE_STRING_H) -# include +#include /* An ANSI string.h and pre-ANSI memory.h might conflict */ -# if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H) -# include -# endif /* !STDC_HEADERS & HAVE_MEMORY_H */ +#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H) +#include +#endif /* !STDC_HEADERS & HAVE_MEMORY_H */ #else /* !STDC_HEADERS & !HAVE_STRING_H */ -# include +#include /* memory and strings.h conflict on other systems */ #endif /* !STDC_HEADERS & !HAVE_STRING_H */ #ifdef HAVE_SYS_PARAM_H -# include +#include #endif +/*** typedefs(not structures) and defined constants **********************************************/ + /* The O_BINARY definition was taken from gettext */ #if !defined O_BINARY && defined _O_BINARY /* For MSC-compatible compilers. */ -# define O_BINARY _O_BINARY +#define O_BINARY _O_BINARY #endif #ifdef __BEOS__ /* BeOS 5 has O_BINARY, but is has no effect. */ -# undef O_BINARY +#undef O_BINARY #endif /* On reasonable systems, binary I/O is the default. */ #ifndef O_BINARY -# define O_BINARY 0 +#define O_BINARY 0 #endif /* Replacement for O_NONBLOCK */ #ifndef O_NONBLOCK -#ifdef O_NDELAY /* SYSV */ +#ifdef O_NDELAY /* SYSV */ #define O_NONBLOCK O_NDELAY #else /* BSD */ #define O_NONBLOCK FNDELAY @@ -49,38 +49,38 @@ #endif /* !O_NONBLOCK */ #ifdef HAVE_SYS_SELECT_H -# include +#include #endif #if defined(__QNX__) && !defined(__QNXNTO__) /* exec*() from */ -# include +#include #endif #include #include "glibcompat.h" #ifndef __GNUC__ -# define __attribute__(x) +#define __attribute__(x) #endif #ifdef ENABLE_NLS -# include -# define _(String) gettext (String) -# ifdef gettext_noop -# define N_(String) gettext_noop (String) -# else -# define N_(String) (String) -# endif +#include +#define _(String) gettext (String) +#ifdef gettext_noop +#define N_(String) gettext_noop (String) +#else +#define N_(String) (String) +#endif #else /* Stubs that do something close enough. */ -# define textdomain(String) -# define gettext(String) (String) -# define ngettext(String1,String2,Num) (((Num) == 1) ? (String1) : (String2)) -# define dgettext(Domain,Message) (Message) -# define dcgettext(Domain,Message,Type) (Message) -# define bindtextdomain(Domain,Directory) -# define _(String) (String) -# define N_(String) (String) +#define textdomain(String) +#define gettext(String) (String) +#define ngettext(String1,String2,Num) (((Num) == 1) ? (String1) : (String2)) +#define dgettext(Domain,Message) (Message) +#define dcgettext(Domain,Message,Type) (Message) +#define bindtextdomain(Domain,Directory) +#define _(String) (String) +#define N_(String) (String) #endif /* !ENABLE_NLS */ #include "fs.h" @@ -90,8 +90,6 @@ #include "lib/logging.h" #endif -extern const char *home_dir; - #ifdef min #undef min #endif @@ -100,21 +98,19 @@ extern const char *home_dir; #undef max #endif -#define min(x, y) ((x) > (y) ? (y) : (x)) -#define max(x, y) ((x) > (y) ? (x) : (y)) +#define min(x, y) ((x) > (y) ? (y) : (x)) +#define max(x, y) ((x) > (y) ? (x) : (y)) /* Just for keeping Your's brains from invention a proper size of the buffer :-) */ -#define BUF_10K 10240L -#define BUF_8K 8192L -#define BUF_4K 4096L -#define BUF_1K 1024L +#define BUF_10K 10240L +#define BUF_8K 8192L +#define BUF_4K 4096L +#define BUF_1K 1024L -#define BUF_LARGE BUF_1K -#define BUF_MEDIUM 512 -#define BUF_SMALL 128 -#define BUF_TINY 64 - -void refresh_screen (void *); +#define BUF_LARGE BUF_1K +#define BUF_MEDIUM 512 +#define BUF_SMALL 128 +#define BUF_TINY 64 /* AIX compiler doesn't understand '\e' */ #define ESC_CHAR '\033' @@ -125,14 +121,27 @@ void refresh_screen (void *); #if 0 #ifdef MC_ENABLE_DEBUGGING_CODE -# undef NDEBUG +#undef NDEBUG #else -# define NDEBUG +#define NDEBUG #endif #include #endif #define MC_ERROR mc_main_error_quark () + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +extern const char *home_dir; + +/*** declarations of public functions ************************************************************/ + +void refresh_screen (void *); GQuark mc_main_error_quark (void); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/lock.c b/lib/lock.c index 10615e9bd..db1924cf1 100644 --- a/lib/lock.c +++ b/lib/lock.c @@ -59,19 +59,30 @@ #include "src/wtools.h" /* query_dialog() */ +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + #define BUF_SIZE 255 #define PID_BUF_SIZE 10 +/*** file scope type declarations ****************************************************************/ + struct lock_s { char *who; pid_t pid; }; +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /** \fn static char * lock_build_name (void) * \brief builds user@host.domain.pid string (need to be freed) * \return a pointer to lock filename */ + static char * lock_build_name (void) { @@ -98,6 +109,8 @@ lock_build_name (void) return g_strdup_printf ("%s@%s.%d", user, host, (int) getpid ()); } +/* --------------------------------------------------------------------------------------------- */ + static char * lock_build_symlink_name (const char *fname) { @@ -116,7 +129,11 @@ lock_build_symlink_name (const char *fname) return symlink_name; } -/* Extract pid from user@host.domain.pid string */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Extract pid from user@host.domain.pid string + */ + static struct lock_s * lock_extract_info (const char *str) { @@ -148,7 +165,11 @@ lock_extract_info (const char *str) return &lock; } -/* Extract user@host.domain.pid from lock file (static string) */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Extract user@host.domain.pid from lock file (static string) + */ + static char * lock_get_info (const char *lockfname) { @@ -162,10 +183,14 @@ lock_get_info (const char *lockfname) return buf; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /* Tries to raise file lock Returns 1 on success, 0 on failure, -1 if abort Warning: Might do screen refresh and lose edit->force */ + int lock_file (const char *fname) { @@ -237,8 +262,12 @@ lock_file (const char *fname) return symlink_ok ? 1 : 0; } -/* Lowers file lock if possible - Always returns 0 */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Lowers file lock if possible + * @returns Always 0 + */ + int unlock_file (const char *fname) { @@ -279,3 +308,5 @@ unlock_file (const char *fname) g_free (lockfname); return 0; } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/lock.h b/lib/lock.h index 98bebac6f..412f38c0b 100644 --- a/lib/lock.h +++ b/lib/lock.h @@ -9,7 +9,19 @@ #ifndef MC_LOCK_H #define MC_LOCK_H +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + int lock_file (const char *fname); int unlock_file (const char *fname); +/*** inline functions ****************************************************************************/ + #endif /* MC_LOCK_H */ diff --git a/lib/logging.c b/lib/logging.c index 06df0e8b9..01a6ab3a3 100644 --- a/lib/logging.c +++ b/lib/logging.c @@ -39,42 +39,61 @@ #include "src/setup.h" -/*** file scope functions **********************************************/ + +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static gboolean -is_logging_enabled(void) +is_logging_enabled (void) { - static gboolean logging_initialized = FALSE; - static gboolean logging_enabled = FALSE; + static gboolean logging_initialized = FALSE; + static gboolean logging_enabled = FALSE; - if (!logging_initialized) { - logging_enabled = mc_config_get_bool (mc_main_config, - CONFIG_APP_SECTION, "development.enable_logging", FALSE); - logging_initialized = TRUE; - } - return logging_enabled; + if (!logging_initialized) + { + logging_enabled = mc_config_get_bool (mc_main_config, + CONFIG_APP_SECTION, "development.enable_logging", + FALSE); + logging_initialized = TRUE; + } + return logging_enabled; } -/*** public functions **************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ void -mc_log(const char *fmt, ...) +mc_log (const char *fmt, ...) { - va_list args; - FILE *f; - char *logfilename; + va_list args; + FILE *f; + char *logfilename; - if (is_logging_enabled()) { - va_start(args, fmt); - logfilename = g_strdup_printf("%s/%s/log", home_dir, MC_USERCONF_DIR); - if (logfilename != NULL) { - f = fopen (logfilename, "a"); - if (f != NULL) { - (void)vfprintf(f, fmt, args); - (void)fclose(f); - } - g_free(logfilename); - va_end(args); - } - } + if (is_logging_enabled ()) + { + va_start (args, fmt); + logfilename = g_strdup_printf ("%s/%s/log", home_dir, MC_USERCONF_DIR); + if (logfilename != NULL) + { + f = fopen (logfilename, "a"); + if (f != NULL) + { + (void) vfprintf (f, fmt, args); + (void) fclose (f); + } + g_free (logfilename); + va_end (args); + } + } } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/logging.h b/lib/logging.h index cad3c01d0..9270ca90e 100644 --- a/lib/logging.h +++ b/lib/logging.h @@ -1,4 +1,3 @@ - /** \file logging.h * \brief Header: provides a log file to ease tracing the program */ @@ -11,9 +10,20 @@ events into a central log file that can be used for debugging. */ +/*** typedefs(not structures) and defined constants **********************************************/ + #define mc_log_mark() mc_log("%s:%d\n",__FILE__,__LINE__) -extern void mc_log(const char *, ...) - __attribute__((__format__(__printf__,1,2))); +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +extern void mc_log (const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); + +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/mcconfig.h b/lib/mcconfig.h index e2fad45ef..65fdd3c56 100644 --- a/lib/mcconfig.h +++ b/lib/mcconfig.h @@ -9,7 +9,8 @@ /*** structures declarations (and typedefs of structures)***************/ -typedef struct mc_config_struct { +typedef struct mc_config_struct +{ GKeyFile *handle; gchar *ini_path; } mc_config_t; @@ -35,10 +36,9 @@ gboolean mc_config_has_group (mc_config_t *, const char *); gboolean mc_config_read_file (mc_config_t *, const gchar *); -gboolean mc_config_save_file (mc_config_t * config, GError **error); +gboolean mc_config_save_file (mc_config_t * config, GError ** error); -gboolean mc_config_save_to_file (mc_config_t * config, const gchar * filename, - GError **error); +gboolean mc_config_save_to_file (mc_config_t * config, const gchar * filename, GError ** error); /* mcconfig/get.c: */ @@ -64,26 +64,21 @@ int *mc_config_get_int_list (mc_config_t *, const gchar *, const gchar *, gsize /* mcconfig/set.c: */ -void - mc_config_direct_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *); +void mc_config_direct_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *); -void - mc_config_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *); +void mc_config_set_string (mc_config_t *, const gchar *, const gchar *, const gchar *); -void - mc_config_set_bool (mc_config_t *, const gchar *, const gchar *, gboolean); +void mc_config_set_bool (mc_config_t *, const gchar *, const gchar *, gboolean); void mc_config_set_int (mc_config_t *, const gchar *, const gchar *, int); void - mc_config_set_string_list (mc_config_t *, const gchar *, - const gchar *, const gchar * const[], gsize); +mc_config_set_string_list (mc_config_t *, const gchar *, + const gchar *, const gchar * const[], gsize); -void - mc_config_set_bool_list (mc_config_t *, const gchar *, const gchar *, gboolean[], gsize); +void mc_config_set_bool_list (mc_config_t *, const gchar *, const gchar *, gboolean[], gsize); -void - mc_config_set_int_list (mc_config_t *, const gchar *, const gchar *, int[], gsize); +void mc_config_set_int_list (mc_config_t *, const gchar *, const gchar *, int[], gsize); /* mcconfig/dialog.c: */ diff --git a/lib/mcconfig/get.c b/lib/mcconfig/get.c index f7c468e05..4afc4dd88 100644 --- a/lib/mcconfig/get.c +++ b/lib/mcconfig/get.c @@ -44,15 +44,15 @@ mc_config_get_groups (mc_config_t * mc_config, gsize * len) if (!mc_config) { - ret = g_try_malloc0 (sizeof (gchar **)); - if (len != NULL) - *len=0; - return ret; + ret = g_try_malloc0 (sizeof (gchar **)); + if (len != NULL) + *len = 0; + return ret; } ret = g_key_file_get_groups (mc_config->handle, len); if (ret == NULL) { - ret = g_try_malloc0 (sizeof (gchar **)); + ret = g_try_malloc0 (sizeof (gchar **)); } return ret; } @@ -66,15 +66,15 @@ mc_config_get_keys (mc_config_t * mc_config, const gchar * group, gsize * len) if (!mc_config || !group) { - ret = g_try_malloc0 (sizeof (gchar **)); - if (len != NULL) - *len=0; - return ret; + ret = g_try_malloc0 (sizeof (gchar **)); + if (len != NULL) + *len = 0; + return ret; } ret = g_key_file_get_keys (mc_config->handle, group, len, NULL); if (ret == NULL) { - ret = g_try_malloc0 (sizeof (gchar **)); + ret = g_try_malloc0 (sizeof (gchar **)); } return ret; } @@ -83,26 +83,26 @@ mc_config_get_keys (mc_config_t * mc_config, const gchar * group, gsize * len) gchar * mc_config_get_string (mc_config_t * mc_config, const gchar * group, - const gchar * param, const gchar * def) + const gchar * param, const gchar * def) { GIConv conv; GString *buffer; gchar *ret; - const char *_system_codepage = str_detect_termencoding(); + const char *_system_codepage = str_detect_termencoding (); if (!mc_config || !group || !param) - return def ? g_strdup (def) : NULL; + return def ? g_strdup (def) : NULL; - if (! mc_config_has_param(mc_config, group, param)) + if (!mc_config_has_param (mc_config, group, param)) { - mc_config_set_string (mc_config, group, param, def ? def : ""); - return def ? g_strdup (def) : NULL; + mc_config_set_string (mc_config, group, param, def ? def : ""); + return def ? g_strdup (def) : NULL; } ret = g_key_file_get_string (mc_config->handle, group, param, NULL); if (!ret) - ret = def ? g_strdup (def) : NULL; + ret = def ? g_strdup (def) : NULL; if (str_isutf8 (_system_codepage)) return ret; @@ -115,38 +115,38 @@ mc_config_get_string (mc_config_t * mc_config, const gchar * group, if (str_convert (conv, ret, buffer) == ESTR_FAILURE) { - g_string_free(buffer, TRUE); + g_string_free (buffer, TRUE); str_close_conv (conv); return ret; } str_close_conv (conv); - g_free(ret); + g_free (ret); - return g_string_free(buffer, FALSE); + return g_string_free (buffer, FALSE); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ gchar * mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group, - const gchar * param, const gchar * def) + const gchar * param, const gchar * def) { gchar *ret; if (!mc_config || !group || !param) - return def ? g_strdup (def) : NULL; + return def ? g_strdup (def) : NULL; - if (! mc_config_has_param(mc_config, group, param)) + if (!mc_config_has_param (mc_config, group, param)) { - mc_config_set_string (mc_config, group, param, def ? def : ""); - return def ? g_strdup (def) : NULL; + mc_config_set_string (mc_config, group, param, def ? def : ""); + return def ? g_strdup (def) : NULL; } ret = g_key_file_get_string (mc_config->handle, group, param, NULL); if (!ret) - ret = def ? g_strdup (def) : NULL; + ret = def ? g_strdup (def) : NULL; return ret; } @@ -154,16 +154,15 @@ mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group, /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ gboolean -mc_config_get_bool (mc_config_t * mc_config, const gchar * group, - const gchar * param, gboolean def) +mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def) { if (!mc_config || !group || !param) - return def; + return def; - if (! mc_config_has_param(mc_config, group, param)) + if (!mc_config_has_param (mc_config, group, param)) { - mc_config_set_bool (mc_config, group, param, def); - return def; + mc_config_set_bool (mc_config, group, param, def); + return def; } return g_key_file_get_boolean (mc_config->handle, group, param, NULL); @@ -172,16 +171,15 @@ mc_config_get_bool (mc_config_t * mc_config, const gchar * group, /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ int -mc_config_get_int (mc_config_t * mc_config, const gchar * group, - const gchar * param, int def) +mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def) { if (!mc_config || !group || !param) - return def; + return def; - if (! mc_config_has_param(mc_config, group, param)) + if (!mc_config_has_param (mc_config, group, param)) { - mc_config_set_int (mc_config, group, param, def); - return def; + mc_config_set_int (mc_config, group, param, def); + return def; } return g_key_file_get_integer (mc_config->handle, group, param, NULL); @@ -192,39 +190,36 @@ mc_config_get_int (mc_config_t * mc_config, const gchar * group, gchar ** mc_config_get_string_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, gsize * length) + const gchar * param, gsize * length) { if (!mc_config || !group || !param) - return NULL; + return NULL; - return g_key_file_get_string_list (mc_config->handle, group, param, - length, NULL); + return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ gboolean * mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, gsize * length) + const gchar * param, gsize * length) { if (!mc_config || !group || !param) - return NULL; + return NULL; - return g_key_file_get_boolean_list (mc_config->handle, group, param, - length, NULL); + return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ int * mc_config_get_int_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, gsize * length) + const gchar * param, gsize * length) { if (!mc_config || !group || !param) - return NULL; + return NULL; - return g_key_file_get_integer_list (mc_config->handle, group, param, - length, NULL); + return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ diff --git a/lib/mcconfig/set.c b/lib/mcconfig/set.c index daa2f7025..1139d9138 100644 --- a/lib/mcconfig/set.c +++ b/lib/mcconfig/set.c @@ -37,33 +37,33 @@ extern int utf8_display; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ static gchar * -mc_config_normalize_before_save(const gchar * value) +mc_config_normalize_before_save (const gchar * value) { GIConv conv; GString *buffer; if (utf8_display) { - buffer = g_string_new(value); + buffer = g_string_new (value); } else { conv = str_crt_conv_to ("UTF-8"); if (conv == INVALID_CONV) - return g_strdup(value); + return g_strdup (value); buffer = g_string_new (""); if (str_convert (conv, value, buffer) == ESTR_FAILURE) { - g_string_free(buffer, TRUE); - buffer = g_string_new(value); + g_string_free (buffer, TRUE); + buffer = g_string_new (value); } str_close_conv (conv); } - return g_string_free(buffer, FALSE); + return g_string_free (buffer, FALSE); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ @@ -72,46 +72,46 @@ mc_config_normalize_before_save(const gchar * value) void mc_config_direct_set_string (mc_config_t * mc_config, const gchar * group, - const gchar * param, const gchar * value) + const gchar * param, const gchar * value) { gchar *buffer; if (!mc_config || !group || !param || !value) - return; + return; - buffer = mc_config_normalize_before_save(value); + buffer = mc_config_normalize_before_save (value); g_key_file_set_value (mc_config->handle, group, param, buffer); - g_free(buffer); + g_free (buffer); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ void mc_config_set_string (mc_config_t * mc_config, const gchar * group, - const gchar * param, const gchar * value) + const gchar * param, const gchar * value) { gchar *buffer; if (!mc_config || !group || !param || !value) - return; + return; - buffer = mc_config_normalize_before_save(value); + buffer = mc_config_normalize_before_save (value); g_key_file_set_string (mc_config->handle, group, param, buffer); - g_free(buffer); + g_free (buffer); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ void mc_config_set_bool (mc_config_t * mc_config, const gchar * group, - const gchar * param, gboolean value) + const gchar * param, gboolean value) { - if (!mc_config || !group || !param ) - return; + if (!mc_config || !group || !param) + return; g_key_file_set_boolean (mc_config->handle, group, param, value); } @@ -119,11 +119,10 @@ mc_config_set_bool (mc_config_t * mc_config, const gchar * group, /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ void -mc_config_set_int (mc_config_t * mc_config, const gchar * group, - const gchar * param, int value) +mc_config_set_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int value) { - if (!mc_config || !group || !param ) - return; + if (!mc_config || !group || !param) + return; g_key_file_set_integer (mc_config->handle, group, param, value); } @@ -133,14 +132,12 @@ mc_config_set_int (mc_config_t * mc_config, const gchar * group, void mc_config_set_string_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, const gchar * const value[], - gsize length) + const gchar * param, const gchar * const value[], gsize length) { if (!mc_config || !group || !param || !value || length == 0) - return; + return; - g_key_file_set_string_list (mc_config->handle, group, param, value, - length); + g_key_file_set_string_list (mc_config->handle, group, param, value, length); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ @@ -148,26 +145,24 @@ mc_config_set_string_list (mc_config_t * mc_config, const gchar * group, void mc_config_set_bool_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, gboolean value[], gsize length) + const gchar * param, gboolean value[], gsize length) { if (!mc_config || !group || !param || !value || length == 0) - return; + return; - g_key_file_set_boolean_list (mc_config->handle, group, param, value, - length); + g_key_file_set_boolean_list (mc_config->handle, group, param, value, length); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ void mc_config_set_int_list (mc_config_t * mc_config, const gchar * group, - const gchar * param, int value[], gsize length) + const gchar * param, int value[], gsize length) { if (!mc_config || !group || !param || !value || length == 0) - return; + return; - g_key_file_set_integer_list (mc_config->handle, group, param, value, - length); + g_key_file_set_integer_list (mc_config->handle, group, param, value, length); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ diff --git a/lib/search.h b/lib/search.h index 1e1068860..5770c646e 100644 --- a/lib/search.h +++ b/lib/search.h @@ -3,12 +3,12 @@ #include -#include "lib/global.h" /* */ +#include "lib/global.h" /* */ #include #ifdef SEARCH_TYPE_PCRE -# include +#include #endif /*** typedefs(not structures) and defined constants **********************************************/ @@ -18,14 +18,15 @@ typedef int (*mc_search_fn) (const void *user_data, gsize char_offset); #define MC_SEARCH__NUM_REPLACE_ARGS 64 #ifdef SEARCH_TYPE_GLIB -# define mc_search_matchinfo_t GMatchInfo +#define mc_search_matchinfo_t GMatchInfo #else -# define mc_search_matchinfo_t pcre_extra +#define mc_search_matchinfo_t pcre_extra #endif /*** enums ***************************************************************************************/ -typedef enum { +typedef enum +{ MC_SEARCH_E_OK, MC_SEARCH_E_INPUT, MC_SEARCH_E_REGEX_COMPILE, @@ -34,14 +35,16 @@ typedef enum { MC_SEARCH_E_NOTFOUND } mc_search_error_t; -typedef enum { +typedef enum +{ MC_SEARCH_T_NORMAL, MC_SEARCH_T_REGEX, MC_SEARCH_T_HEX, MC_SEARCH_T_GLOB } mc_search_type_t; -typedef enum { +typedef enum +{ MC_SEARCH_CB_OK = 0, MC_SEARCH_CB_INVALID = -1, MC_SEARCH_CB_ABORT = -2, @@ -51,9 +54,10 @@ typedef enum { /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_search_struct { +typedef struct mc_search_struct +{ -/* public input data */ + /* public input data */ /* search in all charsets */ gboolean is_all_charsets; @@ -80,7 +84,7 @@ typedef struct mc_search_struct { mc_search_type_t search_type; -/* public output data */ + /* public output data */ /* some data for normal */ off_t normal_offset; @@ -94,7 +98,7 @@ typedef struct mc_search_struct { int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2]; #endif /* SEARCH_TYPE_PCRE */ -/* private data */ + /* private data */ /* prepared conditions */ GPtrArray *conditions; @@ -109,7 +113,8 @@ typedef struct mc_search_struct { } mc_search_t; -typedef struct mc_search_type_str_struct { +typedef struct mc_search_type_str_struct +{ const char *str; mc_search_type_t type; } mc_search_type_str_t; @@ -129,14 +134,14 @@ gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize st gboolean mc_search_is_type_avail (mc_search_type_t); -const mc_search_type_str_t *mc_search_types_list_get (size_t *num); +const mc_search_type_str_t *mc_search_types_list_get (size_t * num); GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str); char *mc_search_prepare_replace_str2 (mc_search_t *, char *); gboolean mc_search_is_fixed_search_str (mc_search_t *); -gchar **mc_search_get_types_strings_array (size_t *num); +gchar **mc_search_get_types_strings_array (size_t * num); gboolean mc_search (const gchar *, const gchar *, mc_search_type_t); diff --git a/lib/search/internal.h b/lib/search/internal.h index edb5e9f72..2ccd5e3a2 100644 --- a/lib/search/internal.h +++ b/lib/search/internal.h @@ -11,7 +11,8 @@ /*** enums ***************************************************************************************/ -typedef enum { +typedef enum +{ COND__NOT_FOUND, COND__NOT_ALL_FOUND, COND__FOUND_CHAR, @@ -22,7 +23,8 @@ typedef enum { /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_search_cond_struct { +typedef struct mc_search_cond_struct +{ GString *str; GString *upper; GString *lower; @@ -32,12 +34,12 @@ typedef struct mc_search_cond_struct { /*** global variables defined in .c file *********************************************************/ -extern const char * STR_E_NOTFOUND; -extern const char * STR_E_UNKNOWN_TYPE; -extern const char * STR_E_RPL_NOT_EQ_TO_FOUND; -extern const char * STR_E_RPL_INVALID_TOKEN; -/*** declarations of public functions ************************************************************/ +extern const char *STR_E_NOTFOUND; +extern const char *STR_E_UNKNOWN_TYPE; +extern const char *STR_E_RPL_NOT_EQ_TO_FOUND; +extern const char *STR_E_RPL_INVALID_TOKEN; +/*** declarations of public functions ************************************************************/ /* search/lib.c : */ @@ -83,4 +85,6 @@ gboolean mc_search__run_hex (mc_search_t *, const void *, gsize, gsize, gsize *) GString *mc_search_hex_prepare_replace_str (mc_search_t *, GString *); +/*** inline functions ****************************************************************************/ + #endif diff --git a/lib/search/lib.c b/lib/search/lib.c index e5a3f23e0..c205f6fcb 100644 --- a/lib/search/lib.c +++ b/lib/search/lib.c @@ -39,10 +39,11 @@ /*** global variables ****************************************************************************/ -const char * STR_E_NOTFOUND = N_("Search string not found"); -const char * STR_E_UNKNOWN_TYPE = N_("Not implemented yet"); -const char * STR_E_RPL_NOT_EQ_TO_FOUND = N_("Num of replace tokens not equal to num of found tokens"); -const char * STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d"); +const char *STR_E_NOTFOUND = N_("Search string not found"); +const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet"); +const char *STR_E_RPL_NOT_EQ_TO_FOUND = +N_("Num of replace tokens not equal to num of found tokens"); +const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d"); /*** file scope macro definitions ****************************************************************/ @@ -62,13 +63,15 @@ mc_search__recode_str (const char *str, gsize str_len, gsize bytes_read; GIConv conv; - if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from)) { + if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from)) + { *bytes_written = str_len; return g_strndup (str, str_len); } conv = g_iconv_open (charset_to, charset_from); - if (conv == INVALID_CONV) { + if (conv == INVALID_CONV) + { *bytes_written = str_len; return g_strndup (str, str_len); } @@ -76,7 +79,8 @@ mc_search__recode_str (const char *str, gsize str_len, ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL); g_iconv_close (conv); - if (ret == NULL) { + if (ret == NULL) + { *bytes_written = str_len; return g_strndup (str, str_len); } @@ -118,7 +122,8 @@ mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len, converted_str2 = mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len); #endif - if (just_letters) { + if (just_letters) + { if (str_isalnum (converted_str) && !str_isdigit (converted_str)) *just_letters = TRUE; else @@ -250,7 +255,7 @@ mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len /* --------------------------------------------------------------------------------------------- */ gchar ** -mc_search_get_types_strings_array (size_t *num) +mc_search_get_types_strings_array (size_t * num) { gchar **ret; int lc_index; @@ -263,14 +268,12 @@ mc_search_get_types_strings_array (size_t *num) if (ret == NULL) return NULL; - for (lc_index = 0, type_str = types_str; - type_str->str != NULL; - type_str++, lc_index++) + for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++) ret[lc_index] = g_strdup (type_str->str); /* don't count last NULL item */ if (num != NULL) - *num = (size_t) lc_index; + *num = (size_t) lc_index; return ret; } diff --git a/lib/search/regex.c b/lib/search/regex.c index 757fc6624..77dd4cee5 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -58,7 +58,8 @@ typedef enum /*** file scope functions ************************************************************************/ static gboolean -mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str, gsize * offset) +mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str, + gsize * offset) { char *tmp_regex_str; gsize spec_chr_len; @@ -194,7 +195,7 @@ mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * st /* --------------------------------------------------------------------------------------------- */ static GString * -mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr) +mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr) { GString *accumulator, *spec_char, *ret_str; gsize loop; @@ -219,7 +220,8 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *ast mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator); while (loop < astr->len && !(astr->str[loop] == ']' - && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))) + && !strutils_is_char_escaped (astr->str, + &(astr->str[loop])))) { g_string_append_c (ret_str, astr->str[loop]); loop++; @@ -317,7 +319,7 @@ mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len) gsize loop; for (loop = 0; loop < len - 1; loop++) { - if (str[loop] == '\\' && g_ascii_isdigit(str[loop + 1]) ) + if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1])) { if (strutils_is_char_escaped (str, &str[loop])) continue; @@ -418,7 +420,7 @@ mc_search_regex__process_replace_str (const GString * replace_str, const gsize c return -1; } - if ( g_ascii_isdigit(*(curr_str + 1))) + if (g_ascii_isdigit (*(curr_str + 1))) { ret = g_ascii_digit_value (*(curr_str + 1)); *skip_len = 2; /* \\ and one digit */ @@ -452,6 +454,7 @@ mc_search_regex__process_replace_str (const GString * replace_str, const gsize c } return ret; } + static void mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len, replace_transform_type_t * replace_flags) diff --git a/lib/search/search.c b/lib/search/search.c index 895df2184..30dcdb80a 100644 --- a/lib/search/search.c +++ b/lib/search/search.c @@ -66,7 +66,8 @@ mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str, mc_search_cond->str = g_string_new_len (str, str_len); mc_search_cond->charset = g_strdup (charset); - switch (lc_mc_search->search_type) { + switch (lc_mc_search->search_type) + { case MC_SEARCH_T_GLOB: mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond); break; @@ -117,7 +118,8 @@ mc_search__conditions_free (GPtrArray * array) gsize loop1; mc_search_cond_t *lc_mc_search; - for (loop1 = 0; loop1 < array->len; loop1++) { + for (loop1 = 0; loop1 < array->len; loop1++) + { lc_mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1); mc_search__cond_struct_free (lc_mc_search); } @@ -137,7 +139,8 @@ mc_search_new (const gchar * original, gsize str_len) if (!original) return NULL; - if ((gssize) str_len == -1) { + if ((gssize) str_len == -1) + { str_len = strlen (original); if (str_len == 0) return NULL; @@ -184,30 +187,37 @@ mc_search_prepare (mc_search_t * lc_mc_search) GPtrArray *ret; ret = g_ptr_array_new (); #ifdef HAVE_CHARSET - if (lc_mc_search->is_all_charsets) { + if (lc_mc_search->is_all_charsets) + { gsize loop1, recoded_str_len; gchar *buffer; - for (loop1 = 0; loop1 < codepages->len; loop1++) { + for (loop1 = 0; loop1 < codepages->len; loop1++) + { const char *id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id; - if (!g_ascii_strcasecmp (id, cp_source)) { + if (!g_ascii_strcasecmp (id, cp_source)) + { g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original, - lc_mc_search->original_len, cp_source)); + lc_mc_search->original_len, + cp_source)); continue; } buffer = - mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len, cp_source, - id, &recoded_str_len); + mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len, + cp_source, id, &recoded_str_len); g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, recoded_str_len, id)); g_free (buffer); } - } else { + } + else + { g_ptr_array_add (ret, - (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original, + (gpointer) mc_search__cond_struct_new (lc_mc_search, + lc_mc_search->original, lc_mc_search->original_len, cp_source)); } @@ -232,13 +242,15 @@ mc_search_run (mc_search_t * lc_mc_search, const void *user_data, if (!lc_mc_search) return FALSE; - if (!mc_search_is_type_avail (lc_mc_search->search_type)) { + if (!mc_search_is_type_avail (lc_mc_search->search_type)) + { lc_mc_search->error = MC_SEARCH_E_INPUT; lc_mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE)); return FALSE; } #ifdef SEARCH_TYPE_GLIB - if (lc_mc_search->regex_match_info) { + if (lc_mc_search->regex_match_info) + { g_match_info_free (lc_mc_search->regex_match_info); lc_mc_search->regex_match_info = NULL; } @@ -252,7 +264,8 @@ mc_search_run (mc_search_t * lc_mc_search, const void *user_data, return FALSE; - switch (lc_mc_search->search_type) { + switch (lc_mc_search->search_type) + { case MC_SEARCH_T_NORMAL: ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len); break; @@ -276,7 +289,8 @@ mc_search_run (mc_search_t * lc_mc_search, const void *user_data, gboolean mc_search_is_type_avail (mc_search_type_t search_type) { - switch (search_type) { + switch (search_type) + { case MC_SEARCH_T_GLOB: case MC_SEARCH_T_NORMAL: case MC_SEARCH_T_REGEX: @@ -291,11 +305,11 @@ mc_search_is_type_avail (mc_search_type_t search_type) /* --------------------------------------------------------------------------------------------- */ const mc_search_type_str_t * -mc_search_types_list_get (size_t *num) +mc_search_types_list_get (size_t * num) { /* don't count last NULL item */ if (num != NULL) - *num = sizeof (mc_search__list_types) / sizeof (mc_search__list_types[0]) - 1; + *num = sizeof (mc_search__list_types) / sizeof (mc_search__list_types[0]) - 1; return mc_search__list_types; } @@ -313,7 +327,8 @@ mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str if (replace_str == NULL || replace_str->str == NULL || replace_str->len == 0) return g_string_new (""); - switch (lc_mc_search->search_type) { + switch (lc_mc_search->search_type) + { case MC_SEARCH_T_REGEX: ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str); break; @@ -354,7 +369,8 @@ mc_search_is_fixed_search_str (mc_search_t * lc_mc_search) { if (lc_mc_search == NULL) return FALSE; - switch (lc_mc_search->search_type) { + switch (lc_mc_search->search_type) + { case MC_SEARCH_T_REGEX: case MC_SEARCH_T_GLOB: return FALSE; diff --git a/lib/skin.h b/lib/skin.h index dc3147b90..91a85ecd3 100644 --- a/lib/skin.h +++ b/lib/skin.h @@ -12,7 +12,7 @@ /* Beware! When using Slang with color, not all the indexes are free. See color-slang.h (A_*) */ -/* cache often used colors*/ +/* cache often used colors */ #define DEFAULT_COLOR mc_skin_color__cache[0] #define NORMAL_COLOR mc_skin_color__cache[1] #define MARKED_COLOR mc_skin_color__cache[2] @@ -102,7 +102,8 @@ /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_skin_struct { +typedef struct mc_skin_struct +{ gchar *name; gchar *description; mc_config_t *config; @@ -123,6 +124,6 @@ int mc_skin_color_get (const gchar *, const gchar *); void mc_skin_lines_parse_ini_file (mc_skin_t *); -gchar *mc_skin_get(const gchar *, const gchar *, const gchar *); +gchar *mc_skin_get (const gchar *, const gchar *, const gchar *); #endif /* MC_SKIN_H */ diff --git a/lib/skin/colors.c b/lib/skin/colors.c index cb7780b67..be8993df0 100644 --- a/lib/skin/colors.c +++ b/lib/skin/colors.c @@ -203,7 +203,7 @@ mc_skin_color_cache_init (void) MARKED_COLOR = mc_skin_color_get ("core", "marked"); SELECTED_COLOR = mc_skin_color_get ("core", "selected"); MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect"); - DISABLED_COLOR = mc_skin_color_get ("core", "disabled"); + DISABLED_COLOR = mc_skin_color_get ("core", "disabled"); REVERSE_COLOR = mc_skin_color_get ("core", "reverse"); HEADER_COLOR = mc_skin_color_get ("core", "header"); COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark"); diff --git a/lib/skin/common.c b/lib/skin/common.c index c128005e0..a7d1f1e6a 100644 --- a/lib/skin/common.c +++ b/lib/skin/common.c @@ -85,8 +85,7 @@ mc_skin_reinit (void) mc_skin_deinit (); mc_skin__default.name = mc_skin_get_default_name (); mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, - mc_skin_hash_destroy_value); + g_free, mc_skin_hash_destroy_value); } /* --------------------------------------------------------------------------------------------- */ @@ -97,7 +96,8 @@ mc_skin_try_to_load_default (void) mc_skin_reinit (); g_free (mc_skin__default.name); mc_skin__default.name = g_strdup ("default"); - if (!mc_skin_ini_file_load (&mc_skin__default)) { + if (!mc_skin_ini_file_load (&mc_skin__default)) + { mc_skin_reinit (); mc_skin_set_hardcoded_skin (&mc_skin__default); } @@ -116,10 +116,10 @@ mc_skin_init (GError ** error) mc_skin__default.name = mc_skin_get_default_name (); mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, - mc_skin_hash_destroy_value); + g_free, mc_skin_hash_destroy_value); - if (!mc_skin_ini_file_load (&mc_skin__default)) { + if (!mc_skin_ini_file_load (&mc_skin__default)) + { *error = g_error_new (MC_ERROR, 0, _("Unable to load '%s' skin.\nDefault skin has been loaded"), mc_skin__default.name); @@ -129,7 +129,8 @@ mc_skin_init (GError ** error) } mc_skin_colors_old_configure (&mc_skin__default); - if (!mc_skin_ini_file_parse (&mc_skin__default)) { + if (!mc_skin_ini_file_parse (&mc_skin__default)) + { if (*error == NULL) *error = g_error_new (MC_ERROR, 0, _("Unable to parse '%s' skin.\nDefault skin has been loaded"), @@ -166,12 +167,13 @@ mc_skin_deinit (void) /* --------------------------------------------------------------------------------------------- */ gchar * -mc_skin_get(const gchar *group, const gchar *key, const gchar *default_value) +mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value) { - if (mc_args__ugly_line_drawing) { - return g_strdup(default_value); + if (mc_args__ugly_line_drawing) + { + return g_strdup (default_value); } - return mc_config_get_string(mc_skin__default.config, group, key, default_value); + return mc_config_get_string (mc_skin__default.config, group, key, default_value); } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/skin/ini-file.c b/lib/skin/ini-file.c index 5c9725c7b..da75f6fcc 100644 --- a/lib/skin/ini-file.c +++ b/lib/skin/ini-file.c @@ -51,7 +51,8 @@ mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir char *file_name, *file_name2; file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL); - if (exist_file (file_name)) { + if (exist_file (file_name)) + { mc_skin->config = mc_config_init (file_name); g_free (file_name); return (mc_skin->config != NULL); @@ -62,7 +63,8 @@ mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL); g_free (file_name2); - if (exist_file (file_name)) { + if (exist_file (file_name)) + { mc_skin->config = mc_config_init (file_name); g_free (file_name); return (mc_skin->config != NULL); @@ -84,7 +86,8 @@ mc_skin_ini_file_load (mc_skin_t * mc_skin) if (file_name == NULL) return FALSE; - if (strcmp (file_name, mc_skin->name) != 0) { + if (strcmp (file_name, mc_skin->name) != 0) + { g_free (file_name); if (!g_path_is_absolute (mc_skin->name)) return FALSE; @@ -95,7 +98,8 @@ mc_skin_ini_file_load (mc_skin_t * mc_skin) /* ~/.mc/skins/ */ user_home_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR); - if (mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir)) { + if (mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir)) + { g_free (user_home_dir); return TRUE; } diff --git a/lib/skin/internal.h b/lib/skin/internal.h index 801d9926a..a75ef90ea 100644 --- a/lib/skin/internal.h +++ b/lib/skin/internal.h @@ -10,7 +10,8 @@ /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct mc_skin_color_struct { +typedef struct mc_skin_color_struct +{ gchar *fgcolor; gchar *bgcolor; int pair_index; @@ -35,4 +36,5 @@ void mc_skin_hardcoded_blackwhite_colors (mc_skin_t *); void mc_skin_colors_old_configure (mc_skin_t *); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/strescape.h b/lib/strescape.h index cdc14829a..930b77d7f 100644 --- a/lib/strescape.h +++ b/lib/strescape.h @@ -3,7 +3,7 @@ #include -#include "lib/global.h" /* */ +#include "lib/global.h" /* */ /*** typedefs(not structures) and defined constants **********************************************/ diff --git a/lib/strutil.h b/lib/strutil.h index 2c5252d48..6412591af 100644 --- a/lib/strutil.h +++ b/lib/strutil.h @@ -1,7 +1,7 @@ #ifndef MC_STRUTIL_H #define MC_STRUTIL_H -#include "lib/global.h" /* include glib.h */ +#include "lib/global.h" /* include glib.h */ /* Header file for strutil.c, strutilascii.c, strutil8bit.c, strutilutf8.c. * There are two sort of functions: @@ -12,7 +12,7 @@ * (implemented separately in strutilascii.c, strutil8bit.c, strutilutf8.c) * documentation is made for UTF-8 version of functions. */ - + /* invalid strings * function, that works with invalid strings are marked with "I" * in documentation @@ -20,7 +20,7 @@ * are displayed as questionmarks, I-maked comparing functions try to keep * the original value of these bytes. */ - + /* combining characters * displaynig: all handled as zero with characters, expect combing character * at the begin of string, this character has with one (space add before), @@ -36,43 +36,107 @@ * decompose form. (used in do_search (screen.c)) */ +/*** typedefs(not structures) and defined constants **********************************************/ + +#define IS_FIT(x) ((x) & 0x0010) +#define MAKE_FIT(x) ((x) | 0x0010) +#define HIDE_FIT(x) ((x) & 0x000f) + +#define INVALID_CONV ((GIConv) (-1)) + +/*** enums ***************************************************************************************/ + /* results of conversion function */ -typedef enum { -/* success means, that convertion has been finished successully - */ +typedef enum +{ + /* success means, that convertion has been finished successully + */ ESTR_SUCCESS = 0, -/* problem means, that not every characters was successfully converted (They are - * replaced with questionmark). So is impossible convert string back. - */ + /* problem means, that not every characters was successfully converted (They are + * replaced with questionmark). So is impossible convert string back. + */ ESTR_PROBLEM = 1, -/* failure means, that conversion is not possible (example: wrong encoding - * of input string) - */ + /* failure means, that conversion is not possible (example: wrong encoding + * of input string) + */ ESTR_FAILURE = 2 } estr_t; /* alignment strings on terminal */ -typedef enum { - J_LEFT = 0x01, - J_RIGHT = 0x02, - J_CENTER = 0x03, +typedef enum +{ + J_LEFT = 0x01, + J_RIGHT = 0x02, + J_CENTER = 0x03, /* if there is enough space for string on terminal, * string is centered otherwise is aligned to left */ - J_CENTER_LEFT = 0x04, + J_CENTER_LEFT = 0x04, /* fit alignment, if string is to long, is truncated with '~' */ - J_LEFT_FIT = 0x11, - J_RIGHT_FIT = 0x12, - J_CENTER_FIT = 0x13, - J_CENTER_LEFT_FIT = 0x14 + J_LEFT_FIT = 0x11, + J_RIGHT_FIT = 0x12, + J_CENTER_FIT = 0x13, + J_CENTER_LEFT_FIT = 0x14 } align_crt_t; -#define IS_FIT(x) ((x) & 0x0010) -#define MAKE_FIT(x) ((x) | 0x0010) -#define HIDE_FIT(x) ((x) & 0x000f) -#define INVALID_CONV ((GIConv) (-1)) +/*** structures declarations (and typedefs of structures)*****************************************/ + +/* all functions in str_class must be defined for every encoding */ +struct str_class +{ + gchar *(*conv_gerror_message) (GError * error, const char *def_msg); + /*I*/ estr_t (*vfs_convert_to) (GIConv coder, const char *string, int size, GString * buffer); + /*I*/ void (*insert_replace_char) (GString * buffer); + int (*is_valid_string) (const char *); + /*I*/ int (*is_valid_char) (const char *, size_t); + /*I*/ void (*cnext_char) (const char **); + void (*cprev_char) (const char **); + void (*cnext_char_safe) (const char **); + /*I*/ void (*cprev_char_safe) (const char **); + /*I*/ int (*cnext_noncomb_char) (const char **text); + /*I*/ int (*cprev_noncomb_char) (const char **text, const char *begin); + /*I*/ int (*isspace) (const char *); + /*I*/ int (*ispunct) (const char *); + /*I*/ int (*isalnum) (const char *); + /*I*/ int (*isdigit) (const char *); + /*I*/ int (*isprint) (const char *); + /*I*/ int (*iscombiningmark) (const char *); + /*I*/ int (*length) (const char *); + /*I*/ int (*length2) (const char *, int); + /*I*/ int (*length_noncomb) (const char *); + /*I*/ int (*toupper) (const char *, char **, size_t *); + int (*tolower) (const char *, char **, size_t *); + void (*fix_string) (char *); + /*I*/ const char *(*term_form) (const char *); + /*I*/ const char *(*fit_to_term) (const char *, int, align_crt_t); + /*I*/ const char *(*term_trim) (const char *text, int width); + /*I*/ void (*msg_term_size) (const char *, int *, int *); + /*I*/ const char *(*term_substring) (const char *, int, int); + /*I*/ int (*term_width1) (const char *); + /*I*/ int (*term_width2) (const char *, size_t); + /*I*/ int (*term_char_width) (const char *); + /*I*/ const char *(*trunc) (const char *, int); + /*I*/ int (*offset_to_pos) (const char *, size_t); + /*I*/ int (*column_to_pos) (const char *, size_t); + /*I*/ char *(*create_search_needle) (const char *, int); + void (*release_search_needle) (char *, int); + const char *(*search_first) (const char *, const char *, int); + const char *(*search_last) (const char *, const char *, int); + int (*compare) (const char *, const char *); + /*I*/ int (*ncompare) (const char *, const char *); + /*I*/ int (*casecmp) (const char *, const char *); + /*I*/ int (*ncasecmp) (const char *, const char *); + /*I*/ int (*prefix) (const char *, const char *); + /*I*/ int (*caseprefix) (const char *, const char *); + /*I*/ char *(*create_key) (const char *text, int case_sen); + /*I*/ char *(*create_key_for_filename) (const char *text, int case_sen); + /*I*/ int (*key_collate) (const char *t1, const char *t2, int case_sen); + /*I*/ void (*release_key) (char *key, int case_sen); + /*I*/}; + +/*** global variables defined in .c file *********************************************************/ /* standard convertors */ extern GIConv str_cnv_to_term; @@ -80,58 +144,7 @@ extern GIConv str_cnv_from_term; /* from terminal encoding to terminal encoding */ extern GIConv str_cnv_not_convert; -/* all functions in str_class must be defined for every encoding */ -struct str_class { - gchar *(*conv_gerror_message) (GError *error, const char *def_msg); /*I*/ - estr_t (*vfs_convert_to) (GIConv coder, const char *string, - int size, GString *buffer); /*I*/ - void (*insert_replace_char) (GString *buffer); - int (*is_valid_string) (const char *); /*I*/ - int (*is_valid_char) (const char *, size_t); /*I*/ - void (*cnext_char) (const char **); - void (*cprev_char) (const char **); - void (*cnext_char_safe) (const char **); /*I*/ - void (*cprev_char_safe) (const char **); /*I*/ - int (*cnext_noncomb_char) (const char **text); /*I*/ - int (*cprev_noncomb_char) (const char **text, const char *begin); /*I*/ - int (*isspace) (const char *); /*I*/ - int (*ispunct) (const char *); /*I*/ - int (*isalnum) (const char *); /*I*/ - int (*isdigit) (const char *); /*I*/ - int (*isprint) (const char *); /*I*/ - int (*iscombiningmark) (const char *); /*I*/ - int (*length) (const char *); /*I*/ - int (*length2) (const char *, int); /*I*/ - int (*length_noncomb) (const char *); /*I*/ - int (*toupper) (const char *, char **, size_t *); - int (*tolower) (const char *, char **, size_t *); - void (*fix_string) (char *); /*I*/ - const char *(*term_form) (const char *); /*I*/ - const char *(*fit_to_term) (const char *, int, align_crt_t); /*I*/ - const char *(*term_trim) (const char *text, int width); /*I*/ - void (*msg_term_size) (const char *, int *, int *); /*I*/ - const char *(*term_substring) (const char *, int, int); /*I*/ - int (*term_width1) (const char *); /*I*/ - int (*term_width2) (const char *, size_t); /*I*/ - int (*term_char_width) (const char *); /*I*/ - const char *(*trunc) (const char *, int); /*I*/ - int (*offset_to_pos) (const char *, size_t); /*I*/ - int (*column_to_pos) (const char *, size_t); /*I*/ - char *(*create_search_needle) (const char *, int); - void (*release_search_needle) (char *, int); - const char *(*search_first) (const char *, const char *, int); - const char *(*search_last) (const char *, const char *, int); - int (*compare) (const char *, const char *); /*I*/ - int (*ncompare) (const char *, const char *); /*I*/ - int (*casecmp) (const char *, const char *); /*I*/ - int (*ncasecmp) (const char *, const char *); /*I*/ - int (*prefix) (const char *, const char *); /*I*/ - int (*caseprefix) (const char *, const char *); /*I*/ - char *(*create_key) (const char *text, int case_sen); /*I*/ - char *(*create_key_for_filename) (const char *text, int case_sen); /*I*/ - int (*key_collate) (const char *t1, const char *t2, int case_sen); /*I*/ - void (*release_key) (char *key, int case_sen); /*I*/ -}; +/*** declarations of public functions ************************************************************/ struct str_class str_utf8_init (void); struct str_class str_8bit_init (void); @@ -159,7 +172,7 @@ void str_close_conv (GIConv); /* convert string using coder, result of conversion is appended at end of buffer * return ESTR_SUCCESS if there was no problem. * otherwise return ESTR_PROBLEM or ESTR_FAILURE - */ + */ estr_t str_convert (GIConv, const char *, GString *); estr_t str_nconvert (GIConv, const char *, int, GString *); @@ -168,7 +181,7 @@ estr_t str_nconvert (GIConv, const char *, int, GString *); * return new allocated null-terminated string, which is need to be freed * I */ -gchar *str_conv_gerror_message (GError *error, const char *def_msg); +gchar *str_conv_gerror_message (GError * error, const char *def_msg); /* return only ESTR_SUCCESS or ESTR_FAILURE, because vfs must be able to convert * result to original string. (so no replace with questionmark) @@ -185,8 +198,7 @@ estr_t str_vfs_convert_to (GIConv, const char *, int, GString *); /* printf functin for str_buffer, append result of printf at the end of buffer */ -void -str_printf (GString *, const char *, ...); +void str_printf (GString *, const char *, ...); /* add standard replacement character in terminal encoding */ @@ -209,8 +221,8 @@ void str_uninit_strings (void); * ESTR_PROBLEM if ch contains only part of characters, * ESTR_FAILURE if conversion is not possible */ -estr_t str_translate_char (GIConv conv, const char *ch, size_t ch_size, - char *output, size_t out_size); +estr_t str_translate_char (GIConv conv, const char *ch, size_t ch_size, + char *output, size_t out_size); /* test, if text is valid in terminal encoding * I @@ -223,7 +235,7 @@ int str_is_valid_string (const char *text); * multibyte character * I */ -int str_is_valid_char (const char *ch, size_t size); +int str_is_valid_char (const char *ch, size_t size); /* return next characters after text, do not call on the end of string */ @@ -324,23 +336,23 @@ int str_iscombiningmark (const char *ch); * decrase remain by size of returned characters * if out is not big enough, do nothing */ -int str_toupper (const char *ch, char **out, size_t *remain); +int str_toupper (const char *ch, char **out, size_t * remain); /* write upper from of fisrt characters in ch into out * decrase remain by size of returned characters * if out is not big enough, do nothing */ -int str_tolower (const char *ch, char **out, size_t *remain); +int str_tolower (const char *ch, char **out, size_t * remain); /* return length of text in characters * I */ -int str_length (const char* text); +int str_length (const char *text); /* return length of text in characters, limit to size * I */ -int str_length2 (const char* text, int size); +int str_length2 (const char *text, int size); /* return length of one char * I @@ -350,13 +362,13 @@ int str_length_char (const char *); /* return length of text in characters, count only noncombining characters * I */ -int str_length_noncomb (const char* text); +int str_length_noncomb (const char *text); /* replace all invalid characters in text with questionmark * after return, text is valid string in terminal encoding * I */ -void str_fix_string (char* text); +void str_fix_string (char *text); /* replace all invalid characters in text with questionmark * replace all unprintable characters with '.' @@ -364,7 +376,7 @@ void str_fix_string (char* text); * returned string do not need to be freed * I */ -const char *str_term_form (const char *text); +const char *str_term_form (const char *text); /* like str_term_form, but text can be alignment to width * alignment is specified in just_mode (J_LEFT, J_LEFT_FIT, ...) @@ -389,7 +401,7 @@ void str_msg_term_size (const char *text, int *lines, int *columns); * start - column (position) on terminal, where substring begin * result is completed with spaces to width * I - */ + */ const char *str_term_substring (const char *text, int start, int width); /* return width, that will be text occupied on terminal @@ -412,7 +424,7 @@ int str_term_char_width (const char *text); /* convert position in characters to position in bytes * I */ -int str_offset_to_pos (const char* text, size_t length); +int str_offset_to_pos (const char *text, size_t length); /* convert position on terminal to position in characters * I @@ -508,5 +520,8 @@ int str_isutf8 (const char *codeset_name); const char *str_detect_termencoding (void); -int str_verscmp(const char *s1, const char *s2); -#endif /* MC_STRUTIL_H*/ +int str_verscmp (const char *s1, const char *s2); + +/*** inline functions ****************************************************************************/ + +#endif /* MC_STRUTIL_H */ diff --git a/lib/strutil/strutil.c b/lib/strutil/strutil.c index 717325c3e..9172cad4a 100644 --- a/lib/strutil/strutil.c +++ b/lib/strutil/strutil.c @@ -35,12 +35,12 @@ /*names, that are used for utf-8 */ static const char *str_utf8_encodings[] = { - "utf-8", - "utf8", - NULL + "utf-8", + "utf8", + NULL }; -/* standard 8bit encodings, no wide or multibytes characters*/ +/* standard 8bit encodings, no wide or multibytes characters */ static const char *str_8bit_encodings[] = { "cp-1251", "cp1251", @@ -60,16 +60,16 @@ static const char *str_8bit_encodings[] = { NULL }; -/* terminal encoding*/ +/* terminal encoding */ static char *codeset = NULL; -/* function for encoding specific operations*/ +/* function for encoding specific operations */ static struct str_class used_class; GIConv str_cnv_to_term; GIConv str_cnv_from_term; GIConv str_cnv_not_convert; -/* if enc is same encoding like on terminal*/ +/* if enc is same encoding like on terminal */ static int str_test_not_convert (const char *enc) { @@ -79,28 +79,27 @@ str_test_not_convert (const char *enc) GIConv str_crt_conv_to (const char *to_enc) { - return (!str_test_not_convert (to_enc)) - ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert; + return (!str_test_not_convert (to_enc)) ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert; } GIConv str_crt_conv_from (const char *from_enc) { return (!str_test_not_convert (from_enc)) - ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert; + ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert; } void str_close_conv (GIConv conv) { if (conv != str_cnv_not_convert) - g_iconv_close (conv); + g_iconv_close (conv); } static estr_t _str_convert (GIConv coder, const char *string, int size, GString * buffer) { - estr_t state = ESTR_SUCCESS; + estr_t state = ESTR_SUCCESS; gchar *tmp_buff = NULL; gssize left; gsize bytes_read = 0; @@ -109,26 +108,26 @@ _str_convert (GIConv coder, const char *string, int size, GString * buffer) errno = 0; if (coder == INVALID_CONV) - return ESTR_FAILURE; + return ESTR_FAILURE; if (string == NULL || buffer == NULL) - return ESTR_FAILURE; + return ESTR_FAILURE; -/* - if (! used_class.is_valid_string (string)) - { - return ESTR_FAILURE; - } -*/ + /* + if (! used_class.is_valid_string (string)) + { + return ESTR_FAILURE; + } + */ if (size < 0) { - size = strlen (string); + size = strlen (string); } else { - left = strlen (string); - if (left < size) - size = left; + left = strlen (string); + if (left < size) + size = left; } left = size; @@ -136,100 +135,96 @@ _str_convert (GIConv coder, const char *string, int size, GString * buffer) while (left) { - tmp_buff = g_convert_with_iconv ((const gchar *) string, - left, - coder, - &bytes_read, - &bytes_written, &error); - if (error) - { - int code = error->code; + tmp_buff = g_convert_with_iconv ((const gchar *) string, + left, coder, &bytes_read, &bytes_written, &error); + if (error) + { + int code = error->code; - g_error_free (error); - error = NULL; + g_error_free (error); + error = NULL; - switch (code) - { - case G_CONVERT_ERROR_NO_CONVERSION: - /* Conversion between the requested character sets is not supported. */ - tmp_buff = g_strnfill (strlen (string), '?'); - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - return ESTR_FAILURE; + switch (code) + { + case G_CONVERT_ERROR_NO_CONVERSION: + /* Conversion between the requested character sets is not supported. */ + tmp_buff = g_strnfill (strlen (string), '?'); + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + return ESTR_FAILURE; - case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: - /* Invalid byte sequence in conversion input. */ - if ((tmp_buff == NULL) && (bytes_read != 0)) - /* recode valid byte sequence */ - tmp_buff = g_convert_with_iconv ((const gchar *) string, - bytes_read, - coder, NULL, NULL, NULL); + case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: + /* Invalid byte sequence in conversion input. */ + if ((tmp_buff == NULL) && (bytes_read != 0)) + /* recode valid byte sequence */ + tmp_buff = g_convert_with_iconv ((const gchar *) string, + bytes_read, coder, NULL, NULL, NULL); - if (tmp_buff != NULL) - { - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - } + if (tmp_buff != NULL) + { + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + } - if ((int)bytes_read < left) - { - string += bytes_read + 1; - size -= (bytes_read + 1); - left -= (bytes_read + 1); - g_string_append_c (buffer, *(string-1)); - } - else - { - return ESTR_PROBLEM; - } - state = ESTR_PROBLEM; - break; + if ((int) bytes_read < left) + { + string += bytes_read + 1; + size -= (bytes_read + 1); + left -= (bytes_read + 1); + g_string_append_c (buffer, *(string - 1)); + } + else + { + return ESTR_PROBLEM; + } + state = ESTR_PROBLEM; + break; - case G_CONVERT_ERROR_PARTIAL_INPUT: - /* Partial character sequence at end of input. */ - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - if ((int)bytes_read < left) - { - left = left - bytes_read; - tmp_buff = g_strnfill (left, '?'); - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - } - return ESTR_PROBLEM; + case G_CONVERT_ERROR_PARTIAL_INPUT: + /* Partial character sequence at end of input. */ + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + if ((int) bytes_read < left) + { + left = left - bytes_read; + tmp_buff = g_strnfill (left, '?'); + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + } + return ESTR_PROBLEM; - case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */ - case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */ - case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */ - default: - g_free (tmp_buff); - return ESTR_FAILURE; - } - } - else - { - if (tmp_buff != NULL) - { - if (*tmp_buff) - { - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - string += bytes_read; - left -= bytes_read; - } - else - { - g_free (tmp_buff); - g_string_append (buffer, string); - return state; - } - } - else - { - g_string_append (buffer, string); - return ESTR_PROBLEM; - } - } + case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */ + case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */ + case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */ + default: + g_free (tmp_buff); + return ESTR_FAILURE; + } + } + else + { + if (tmp_buff != NULL) + { + if (*tmp_buff) + { + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + string += bytes_read; + left -= bytes_read; + } + else + { + g_free (tmp_buff); + g_string_append (buffer, string); + return state; + } + } + else + { + g_string_append (buffer, string); + return ESTR_PROBLEM; + } + } } return state; } @@ -247,7 +242,7 @@ str_nconvert (GIConv coder, const char *string, int size, GString * buffer) } gchar * -str_conv_gerror_message (GError *error, const char *def_msg) +str_conv_gerror_message (GError * error, const char *def_msg) { return used_class.conv_gerror_message (error, def_msg); } @@ -259,18 +254,17 @@ str_vfs_convert_from (GIConv coder, const char *string, GString * buffer) if (coder == str_cnv_not_convert) { - g_string_append (buffer, string != NULL ? string : ""); - result = ESTR_SUCCESS; + g_string_append (buffer, string != NULL ? string : ""); + result = ESTR_SUCCESS; } else - result = _str_convert (coder, string, -1, buffer); + result = _str_convert (coder, string, -1, buffer); return result; } estr_t -str_vfs_convert_to (GIConv coder, const char *string, int size, - GString * buffer) +str_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { return used_class.vfs_convert_to (coder, string, size, buffer); } @@ -284,10 +278,10 @@ str_printf (GString * buffer, const char *format, ...) g_string_append_vprintf (buffer, format, ap); #else { - gchar *tmp; - tmp = g_strdup_vprintf (format, ap); - g_string_append (buffer, tmp); - g_free(tmp); + gchar *tmp; + tmp = g_strdup_vprintf (format, ap); + g_string_append (buffer, tmp); + g_free (tmp); } #endif va_end (ap); @@ -300,8 +294,7 @@ str_insert_replace_char (GString * buffer) } estr_t -str_translate_char (GIConv conv, const char *keys, size_t ch_size, - char *output, size_t out_size) +str_translate_char (GIConv conv, const char *keys, size_t ch_size, char *output, size_t out_size) { size_t left; size_t cnv; @@ -310,10 +303,13 @@ str_translate_char (GIConv conv, const char *keys, size_t ch_size, left = (ch_size == (size_t) (-1)) ? strlen (keys) : ch_size; - cnv = g_iconv (conv, (gchar **) &keys, &left, &output, &out_size); - if (cnv == (size_t)(-1)) { + cnv = g_iconv (conv, (gchar **) & keys, &left, &output, &out_size); + if (cnv == (size_t) (-1)) + { return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE; - } else { + } + else + { output[0] = '\0'; return ESTR_SUCCESS; } @@ -331,13 +327,12 @@ str_test_encoding_class (const char *encoding, const char **table) { int t; int result = 0; - if ( encoding == NULL ) + if (encoding == NULL) return result; for (t = 0; table[t] != NULL; t++) { - result += (g_ascii_strncasecmp (encoding, table[t], - strlen (table[t])) == 0); + result += (g_ascii_strncasecmp (encoding, table[t], strlen (table[t])) == 0); } return result; } @@ -347,15 +342,15 @@ str_choose_str_functions () { if (str_test_encoding_class (codeset, str_utf8_encodings)) { - used_class = str_utf8_init (); + used_class = str_utf8_init (); } else if (str_test_encoding_class (codeset, str_8bit_encodings)) { - used_class = str_8bit_init (); + used_class = str_8bit_init (); } else { - used_class = str_ascii_init (); + used_class = str_ascii_init (); } } @@ -365,7 +360,7 @@ str_isutf8 (const char *codeset_name) int result = 0; if (str_test_encoding_class (codeset_name, str_utf8_encodings)) { - result = 1; + result = 1; } return result; } @@ -373,26 +368,24 @@ str_isutf8 (const char *codeset_name) void str_init_strings (const char *termenc) { - codeset = g_strdup ((termenc != NULL) - ? termenc - : str_detect_termencoding ()); + codeset = g_strdup ((termenc != NULL) ? termenc : str_detect_termencoding ()); str_cnv_not_convert = g_iconv_open (codeset, codeset); if (str_cnv_not_convert == INVALID_CONV) { - if (termenc != NULL) - { - g_free (codeset); - codeset = g_strdup (str_detect_termencoding ()); - str_cnv_not_convert = g_iconv_open (codeset, codeset); - } + if (termenc != NULL) + { + g_free (codeset); + codeset = g_strdup (str_detect_termencoding ()); + str_cnv_not_convert = g_iconv_open (codeset, codeset); + } - if (str_cnv_not_convert == INVALID_CONV) - { - g_free (codeset); - codeset = g_strdup ("ascii"); - str_cnv_not_convert = g_iconv_open (codeset, codeset); - } + if (str_cnv_not_convert == INVALID_CONV) + { + g_free (codeset); + codeset = g_strdup ("ascii"); + str_cnv_not_convert = g_iconv_open (codeset, codeset); + } } str_cnv_to_term = str_cnv_not_convert; @@ -405,7 +398,7 @@ void str_uninit_strings (void) { if (str_cnv_not_convert != INVALID_CONV) - g_iconv_close (str_cnv_not_convert); + g_iconv_close (str_cnv_not_convert); g_free (codeset); } @@ -450,7 +443,7 @@ str_get_next_char (char *text) const char * str_cget_next_char (const char *text) { - used_class.cnext_char(&text); + used_class.cnext_char (&text); return text; } @@ -607,7 +600,7 @@ str_length (const char *text) int str_length_char (const char *text) { - return str_cget_next_char_safe (text)-text; + return str_cget_next_char_safe (text) - text; } int diff --git a/lib/strutil/strutil8bit.c b/lib/strutil/strutil8bit.c index 8f3cb7099..b98636daf 100644 --- a/lib/strutil/strutil8bit.c +++ b/lib/strutil/strutil8bit.c @@ -52,24 +52,21 @@ static inline int char_##func_name(char c) \ return func_name((int)(unsigned char)c); \ } -DECLARE_CTYPE_WRAPPER(isalnum) -DECLARE_CTYPE_WRAPPER(isalpha) -DECLARE_CTYPE_WRAPPER(isascii) -DECLARE_CTYPE_WRAPPER(isblank) -DECLARE_CTYPE_WRAPPER(iscntrl) -DECLARE_CTYPE_WRAPPER(isdigit) -DECLARE_CTYPE_WRAPPER(isgraph) -DECLARE_CTYPE_WRAPPER(islower) -DECLARE_CTYPE_WRAPPER(isprint) -DECLARE_CTYPE_WRAPPER(ispunct) -DECLARE_CTYPE_WRAPPER(isspace) -DECLARE_CTYPE_WRAPPER(isupper) -DECLARE_CTYPE_WRAPPER(isxdigit) -DECLARE_CTYPE_WRAPPER(toupper) -DECLARE_CTYPE_WRAPPER(tolower) - -static void -str_8bit_insert_replace_char (GString * buffer) +DECLARE_CTYPE_WRAPPER (isalnum) +DECLARE_CTYPE_WRAPPER (isalpha) +DECLARE_CTYPE_WRAPPER (isascii) +DECLARE_CTYPE_WRAPPER (isblank) +DECLARE_CTYPE_WRAPPER (iscntrl) +DECLARE_CTYPE_WRAPPER (isdigit) +DECLARE_CTYPE_WRAPPER (isgraph) +DECLARE_CTYPE_WRAPPER (islower) +DECLARE_CTYPE_WRAPPER (isprint) +DECLARE_CTYPE_WRAPPER (ispunct) +DECLARE_CTYPE_WRAPPER (isspace) +DECLARE_CTYPE_WRAPPER (isupper) +DECLARE_CTYPE_WRAPPER (isxdigit) DECLARE_CTYPE_WRAPPER (toupper) DECLARE_CTYPE_WRAPPER (tolower) + static void + str_8bit_insert_replace_char (GString * buffer) { g_string_append_c (buffer, replch); } @@ -106,11 +103,11 @@ str_8bit_cnext_noncomb_char (const char **text) { if (*text[0] != '\0') { - (*text)++; - return 1; + (*text)++; + return 1; } else - return 0; + return 0; } static int @@ -118,11 +115,11 @@ str_8bit_cprev_noncomb_char (const char **text, const char *begin) { if ((*text) != begin) { - (*text)--; - return 1; + (*text)--; + return 1; } else - return 0; + return 0; } static int @@ -166,7 +163,7 @@ static int str_8bit_toupper (const char *text, char **out, size_t * remain) { if (*remain <= 1) - return 0; + return 0; (*out)[0] = char_toupper (text[0]); (*out)++; (*remain)--; @@ -177,7 +174,7 @@ static int str_8bit_tolower (const char *text, char **out, size_t * remain) { if (*remain <= 1) - return 0; + return 0; (*out)[0] = char_tolower (text[0]); (*out)++; (*remain)--; @@ -193,11 +190,11 @@ str_8bit_length (const char *text) static int str_8bit_length2 (const char *text, int size) { - return (size >= 0) ? min (strlen (text), (gsize)size) : strlen (text); + return (size >= 0) ? min (strlen (text), (gsize) size) : strlen (text); } static gchar * -str_8bit_conv_gerror_message (GError *error, const char *def_msg) +str_8bit_conv_gerror_message (GError * error, const char *def_msg) { GIConv conv; gchar *ret; @@ -207,15 +204,19 @@ str_8bit_conv_gerror_message (GError *error, const char *def_msg) if (conv == INVALID_CONV) ret = g_strdup (def_msg != NULL ? def_msg : ""); - else { + else + { GString *buf; buf = g_string_new (""); - if (str_convert (conv, error->message, buf) != ESTR_FAILURE) { + if (str_convert (conv, error->message, buf) != ESTR_FAILURE) + { ret = buf->str; g_string_free (buf, FALSE); - } else { + } + else + { ret = g_strdup (def_msg != NULL ? def_msg : ""); g_string_free (buf, TRUE); } @@ -227,18 +228,17 @@ str_8bit_conv_gerror_message (GError *error, const char *def_msg) } static estr_t -str_8bit_vfs_convert_to (GIConv coder, const char *string, - int size, GString * buffer) +str_8bit_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { estr_t result; if (coder == str_cnv_not_convert) { - g_string_append_len (buffer, string, size); - result = ESTR_SUCCESS; + g_string_append_len (buffer, string, size); + result = ESTR_SUCCESS; } else - result = str_nconvert (coder, (char *) string, size, buffer); + result = str_nconvert (coder, (char *) string, size, buffer); return result; } @@ -259,7 +259,7 @@ str_8bit_term_form (const char *text) for (; pos < length && remain > 1; pos++, actual++, remain--) { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; } actual[0] = '\0'; @@ -280,85 +280,83 @@ str_8bit_fit_to_term (const char *text, int width, align_crt_t just_mode) actual = result; remain = sizeof (result); - if ((int)length <= width) + if ((int) length <= width) { - ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER_LEFT: - case J_CENTER: - ident = (width - length) / 2; - break; - case J_RIGHT: - ident = width - length; - break; - } + ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER_LEFT: + case J_CENTER: + ident = (width - length) / 2; + break; + case J_RIGHT: + ident = width - length; + break; + } - if ((int)remain <= ident) - goto finally; - memset (actual, ' ', ident); - actual += ident; - remain -= ident; + if ((int) remain <= ident) + goto finally; + memset (actual, ' ', ident); + actual += ident; + remain -= ident; - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } - if (width - length - ident > 0) - { - if (remain <= width - length - ident) - goto finally; - memset (actual, ' ', width - length - ident); - actual += width - length - ident; - remain -= width - length - ident; - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } + if (width - length - ident > 0) + { + if (remain <= width - length - ident) + goto finally; + memset (actual, ' ', width - length - ident); + actual += width - length - ident; + remain -= width - length - ident; + } } else { - if (IS_FIT (just_mode)) - { - for (; pos + 1 <= (gsize)width / 2 && remain > 1; - actual++, pos++, remain--) - { + if (IS_FIT (just_mode)) + { + for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--) + { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } - if (remain <= 1) - goto finally; - actual[0] = '~'; - actual++; - remain--; + if (remain <= 1) + goto finally; + actual[0] = '~'; + actual++; + remain--; - pos += length - width + 1; + pos += length - width + 1; - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } - } - else - { - ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER: - ident = (length - width) / 2; - break; - case J_RIGHT: - ident = length - width; - break; - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } + } + else + { + ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER: + ident = (length - width) / 2; + break; + case J_RIGHT: + ident = length - width; + break; + } - pos += ident; - for (; pos < (gsize)(ident + width) && remain > 1; - pos++, actual++, remain--) - { + pos += ident; + for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--) + { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } - } + } } finally: actual[0] = '\0'; @@ -378,34 +376,34 @@ str_8bit_term_trim (const char *text, int width) actual = result; remain = sizeof (result); - if (width < (int)length) + if (width < (int) length) { - if (width <= 3) - { - memset (actual, '.', width); - actual += width; - remain -= width; - } - else - { - memset (actual, '.', 3); - actual += 3; - remain -= 3; + if (width <= 3) + { + memset (actual, '.', width); + actual += width; + remain -= width; + } + else + { + memset (actual, '.', 3); + actual += 3; + remain -= 3; - pos += length - width + 3; + pos += length - width + 3; - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } + } } else { - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } } actual[0] = '\0'; @@ -415,8 +413,7 @@ str_8bit_term_trim (const char *text, int width) static int str_8bit_term_width2 (const char *text, size_t length) { - return (length != (size_t) (-1)) - ? min (strlen (text), length) : strlen (text); + return (length != (size_t) (-1)) ? min (strlen (text), length) : strlen (text); } static int @@ -443,26 +440,26 @@ str_8bit_msg_term_size (const char *text, int *lines, int *columns) (*lines) = 1; (*columns) = 0; - tmp = g_strdup ((char *)text); + tmp = g_strdup ((char *) text); p = tmp; for (;;) { - q = strchr (p, '\n'); - if (q != NULL) - { - c = q[0]; - q[0] = '\0'; - } + q = strchr (p, '\n'); + if (q != NULL) + { + c = q[0]; + q[0] = '\0'; + } - width = str_8bit_term_width1 (p); - if (width > (*columns)) - (*columns) = width; + width = str_8bit_term_width1 (p); + if (width > (*columns)) + (*columns) = width; - if (q == NULL) - break; - q[0] = c; - p = q + 1; - (*lines)++; + if (q == NULL) + break; + q[0] = c; + p = q + 1; + (*lines)++; } g_free (tmp); } @@ -480,20 +477,19 @@ str_8bit_term_substring (const char *text, int start, int width) remain = sizeof (result); length = strlen (text); - if (start < (int)length) + if (start < (int) length) { - pos += start; - for (; pos < length && width > 0 && remain > 1; - pos++, width--, actual++, remain--) - { + pos += start; + for (; pos < length && width > 0 && remain > 1; pos++, width--, actual++, remain--) + { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } } for (; width > 0 && remain > 1; actual++, remain--, width--) { - actual[0] = ' '; + actual[0] = ' '; } actual[0] = '\0'; @@ -513,32 +509,32 @@ str_8bit_trunc (const char *text, int width) remain = sizeof (result); length = strlen (text); - if ((int)length > width) + if ((int) length > width) { - for (; pos + 1 <= (gsize)width / 2 && remain > 1; actual++, pos++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } - if (remain <= 1) - goto finally; - actual[0] = '~'; - actual++; - remain--; + if (remain <= 1) + goto finally; + actual[0] = '~'; + actual++; + remain--; - pos += length - width + 1; + pos += length - width + 1; - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } } else { - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; - } + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = char_isprint (text[pos]) ? text[pos] : '.'; + } } finally: @@ -557,7 +553,7 @@ static int str_8bit_column_to_pos (const char *text, size_t pos) { (void) text; - return (int)pos; + return (int) pos; } static char * @@ -604,14 +600,14 @@ str_8bit_search_first (const char *text, const char *search, int case_sen) match = g_strstr_len (fold_text, -1, fold_search); if (match != NULL) { - offsset = match - fold_text; - match = text + offsset; + offsset = match - fold_text; + match = text + offsset; } if (!case_sen) { - g_free (fold_text); - g_free (fold_search); + g_free (fold_text); + g_free (fold_search); } return match; @@ -631,14 +627,14 @@ str_8bit_search_last (const char *text, const char *search, int case_sen) match = g_strrstr_len (fold_text, -1, fold_search); if (match != NULL) { - offsset = match - fold_text; - match = text + offsset; + offsset = match - fold_text; + match = text + offsset; } if (!case_sen) { - g_free (fold_text); - g_free (fold_search); + g_free (fold_text); + g_free (fold_search); } return match; @@ -677,15 +673,15 @@ str_8bit_casecmp (const char *s1, const char *s2) /* According to A. Cox, some platforms have islower's that * don't work right on non-uppercase */ - c1 = isupper ((guchar) *s1) ? tolower ((guchar) *s1) : *s1; - c2 = isupper ((guchar) *s2) ? tolower ((guchar) *s2) : *s2; + c1 = isupper ((guchar) * s1) ? tolower ((guchar) * s1) : *s1; + c2 = isupper ((guchar) * s2) ? tolower ((guchar) * s2) : *s2; if (c1 != c2) return (c1 - c2); s1++; s2++; } - return (((gint)(guchar) *s1) - ((gint)(guchar) *s2)); + return (((gint) (guchar) * s1) - ((gint) (guchar) * s2)); #endif } @@ -712,8 +708,8 @@ str_8bit_ncasecmp (const char *s1, const char *s2) /* According to A. Cox, some platforms have islower's that * don't work right on non-uppercase */ - c1 = isupper ((guchar) *s1) ? tolower ((guchar) *s1) : *s1; - c2 = isupper ((guchar) *s2) ? tolower ((guchar) *s2) : *s2; + c1 = isupper ((guchar) * s1) ? tolower ((guchar) * s1) : *s1; + c2 = isupper ((guchar) * s2) ? tolower ((guchar) * s2) : *s2; if (c1 != c2) return (c1 - c2); s1++; @@ -721,7 +717,7 @@ str_8bit_ncasecmp (const char *s1, const char *s2) } if (n != 0) - return (((gint) (guchar) *s1) - ((gint) (guchar) *s2)); + return (((gint) (guchar) * s1) - ((gint) (guchar) * s2)); else return 0; #endif @@ -732,7 +728,7 @@ str_8bit_prefix (const char *text, const char *prefix) { int result; for (result = 0; text[result] != '\0' && prefix[result] != '\0' - && text[result] == prefix[result]; result++); + && text[result] == prefix[result]; result++); return result; } @@ -741,7 +737,7 @@ str_8bit_caseprefix (const char *text, const char *prefix) { int result; for (result = 0; text[result] != '\0' && prefix[result] != '\0' - && char_toupper (text[result]) == char_toupper (prefix[result]); result++); + && char_toupper (text[result]) == char_toupper (prefix[result]); result++); return result; } @@ -763,16 +759,16 @@ static int str_8bit_key_collate (const char *t1, const char *t2, int case_sen) { if (case_sen) - return strcmp (t1, t2); + return strcmp (t1, t2); else - return strcoll (t1, t2); + return strcoll (t1, t2); } static void str_8bit_release_key (char *key, int case_sen) { if (!case_sen) - g_free (key); + g_free (key); } struct str_class diff --git a/lib/strutil/strutilascii.c b/lib/strutil/strutilascii.c index b8dd02846..ab11a17b7 100644 --- a/lib/strutil/strutilascii.c +++ b/lib/strutil/strutilascii.c @@ -74,11 +74,11 @@ str_ascii_cnext_noncomb_char (const char **text) { if (*text[0] != '\0') { - (*text)++; - return 1; + (*text)++; + return 1; } else - return 0; + return 0; } static int @@ -86,11 +86,11 @@ str_ascii_cprev_noncomb_char (const char **text, const char *begin) { if ((*text) != begin) { - (*text)--; - return 1; + (*text)--; + return 1; } else - return 0; + return 0; } static int @@ -134,7 +134,7 @@ static int str_ascii_toupper (const char *text, char **out, size_t * remain) { if (*remain <= 1) - return 0; + return 0; (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]); (*out)++; (*remain)--; @@ -145,7 +145,7 @@ static int str_ascii_tolower (const char *text, char **out, size_t * remain) { if (*remain <= 1) - return 0; + return 0; (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]); (*out)++; (*remain)--; @@ -165,18 +165,17 @@ str_ascii_length2 (const char *text, int size) } static gchar * -str_ascii_conv_gerror_message (GError *error, const char *def_msg) +str_ascii_conv_gerror_message (GError * error, const char *def_msg) { /* the same as str_utf8_conv_gerror_message() */ if ((error != NULL) && (error->message != NULL)) - return g_strdup (error->message); + return g_strdup (error->message); return g_strdup (def_msg != NULL ? def_msg : ""); } static estr_t -str_ascii_vfs_convert_to (GIConv coder, const char *string, - int size, GString * buffer) +str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { (void) coder; g_string_append_len (buffer, string, size); @@ -200,8 +199,8 @@ str_ascii_term_form (const char *text) /* go throw all characters and check, if they are ascii and printable */ for (; pos < length && remain > 1; pos++, actual++, remain--) { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; } actual[0] = '\0'; @@ -222,101 +221,93 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode) actual = result; remain = sizeof (result); - if ((int)length <= width) + if ((int) length <= width) { - ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER_LEFT: - case J_CENTER: - ident = (width - length) / 2; - break; - case J_RIGHT: - ident = width - length; - break; - } + ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER_LEFT: + case J_CENTER: + ident = (width - length) / 2; + break; + case J_RIGHT: + ident = width - length; + break; + } - /* add space before text */ - if ((int)remain <= ident) - goto finally; - memset (actual, ' ', ident); - actual += ident; - remain -= ident; + /* add space before text */ + if ((int) remain <= ident) + goto finally; + memset (actual, ' ', ident); + actual += ident; + remain -= ident; - /* copy all characters */ - for (; pos < (gsize)length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + /* copy all characters */ + for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } - /* add space after text */ - if (width - length - ident > 0) - { - if (remain <= width - length - ident) - goto finally; - memset (actual, ' ', width - length - ident); - actual += width - length - ident; - remain -= width - length - ident; - } + /* add space after text */ + if (width - length - ident > 0) + { + if (remain <= width - length - ident) + goto finally; + memset (actual, ' ', width - length - ident); + actual += width - length - ident; + remain -= width - length - ident; + } } else { - if (IS_FIT (just_mode)) - { - /* copy prefix of text, that is not wider than width / 2 */ - for (; pos + 1 <= (gsize)width / 2 && remain > 1; - actual++, pos++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) - ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) - ? actual[0] : '.'; - } + if (IS_FIT (just_mode)) + { + /* copy prefix of text, that is not wider than width / 2 */ + for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } - if (remain <= 1) - goto finally; - actual[0] = '~'; - actual++; - remain--; + if (remain <= 1) + goto finally; + actual[0] = '~'; + actual++; + remain--; - pos += length - width + 1; + pos += length - width + 1; - /* copy suffix of text */ - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) - ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) - ? actual[0] : '.'; - } - } - else - { - ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER: - ident = (length - width) / 2; - break; - case J_RIGHT: - ident = length - width; - break; - } + /* copy suffix of text */ + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } + } + else + { + ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER: + ident = (length - width) / 2; + break; + case J_RIGHT: + ident = length - width; + break; + } - /* copy substring text, substring start from ident and take width - * characters from text */ - pos += ident; - for (; pos < (gsize)(ident + width) && remain > 1; - pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) - ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) - ? actual[0] : '.'; - } + /* copy substring text, substring start from ident and take width + * characters from text */ + pos += ident; + for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } - } + } } finally: actual[0] = '\0'; @@ -336,40 +327,38 @@ str_ascii_term_trim (const char *text, int width) actual = result; remain = sizeof (result); - if (width < (int)length) + if (width < (int) length) { - if (width <= 3) - { - memset (actual, '.', width); - actual += width; - remain -= width; - } - else - { - memset (actual, '.', 3); - actual += 3; - remain -= 3; + if (width <= 3) + { + memset (actual, '.', width); + actual += width; + remain -= width; + } + else + { + memset (actual, '.', 3); + actual += 3; + remain -= 3; - pos += length - width + 3; + pos += length - width + 3; - /* copy suffix of text */ - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) - ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) - ? actual[0] : '.'; - } - } + /* copy suffix of text */ + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } + } } else { - /* copy all characters */ - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + /* copy all characters */ + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } } actual[0] = '\0'; @@ -379,8 +368,7 @@ str_ascii_term_trim (const char *text, int width) static int str_ascii_term_width2 (const char *text, size_t length) { - return (length != (size_t) (-1)) - ? min (strlen (text), length) : strlen (text); + return (length != (size_t) (-1)) ? min (strlen (text), length) : strlen (text); } static int @@ -412,22 +400,22 @@ str_ascii_msg_term_size (const char *text, int *lines, int *columns) for (;;) { - q = strchr (p, '\n'); - if (q != NULL) - { - c = q[0]; - q[0] = '\0'; - } + q = strchr (p, '\n'); + if (q != NULL) + { + c = q[0]; + q[0] = '\0'; + } - width = str_ascii_term_width1 (p); - if (width > (*columns)) - (*columns) = width; + width = str_ascii_term_width1 (p); + if (width > (*columns)) + (*columns) = width; - if (q == NULL) - break; - q[0] = c; - p = q + 1; - (*lines)++; + if (q == NULL) + break; + q[0] = c; + p = q + 1; + (*lines)++; } g_free (tmp); } @@ -445,23 +433,22 @@ str_ascii_term_substring (const char *text, int start, int width) remain = sizeof (result); length = strlen (text); - if (start < (int)length) + if (start < (int) length) { - pos += start; - /* copy at most width characters from text from start */ - for (; pos < length && width > 0 && remain > 1; - pos++, width--, actual++, remain--) - { + pos += start; + /* copy at most width characters from text from start */ + for (; pos < length && width > 0 && remain > 1; pos++, width--, actual++, remain--) + { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } } /* if text is shorter then width, add space to the end */ for (; width > 0 && remain > 1; actual++, remain--, width--) { - actual[0] = ' '; + actual[0] = ' '; } actual[0] = '\0'; @@ -481,38 +468,38 @@ str_ascii_trunc (const char *text, int width) remain = sizeof (result); length = strlen (text); - if ((int)length > width) + if ((int) length > width) { - /* copy prefix of text */ - for (; pos + 1 <= (gsize)width / 2 && remain > 1; actual++, pos++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + /* copy prefix of text */ + for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } - if (remain <= 1) - goto finally; - actual[0] = '~'; - actual++; - remain--; + if (remain <= 1) + goto finally; + actual[0] = '~'; + actual++; + remain--; - pos += length - width + 1; + pos += length - width + 1; - /* copy suffix of text */ - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + /* copy suffix of text */ + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } } else { - /* copy all characters */ - for (; pos < length && remain > 1; pos++, actual++, remain--) - { - actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; - actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; - } + /* copy all characters */ + for (; pos < length && remain > 1; pos++, actual++, remain--) + { + actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?'; + actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.'; + } } finally: @@ -531,7 +518,7 @@ static int str_ascii_column_to_pos (const char *text, size_t pos) { (void) text; - return (int)pos; + return (int) pos; } static char * @@ -563,14 +550,14 @@ str_ascii_search_first (const char *text, const char *search, int case_sen) match = g_strstr_len (fold_text, -1, fold_search); if (match != NULL) { - offset = match - fold_text; - match = text + offset; + offset = match - fold_text; + match = text + offset; } if (!case_sen) { - g_free (fold_text); - g_free (fold_search); + g_free (fold_text); + g_free (fold_search); } return match; @@ -590,14 +577,14 @@ str_ascii_search_last (const char *text, const char *search, int case_sen) match = g_strrstr_len (fold_text, -1, fold_search); if (match != NULL) { - offset = match - fold_text; - match = text + offset; + offset = match - fold_text; + match = text + offset; } if (!case_sen) { - g_free (fold_text); - g_free (fold_search); + g_free (fold_text); + g_free (fold_search); } return match; @@ -632,7 +619,7 @@ str_ascii_fix_string (char *text) { for (; text[0] != '\0'; text++) { - text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?'; + text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?'; } } @@ -661,7 +648,7 @@ str_ascii_prefix (const char *text, const char *prefix) { int result; for (result = 0; text[result] != '\0' && prefix[result] != '\0' - && text[result] == prefix[result]; result++); + && text[result] == prefix[result]; result++); return result; } @@ -670,8 +657,7 @@ str_ascii_caseprefix (const char *text, const char *prefix) { int result; for (result = 0; text[result] != '\0' && prefix[result] != '\0' - && g_ascii_toupper (text[result]) == - g_ascii_toupper (prefix[result]); result++); + && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++); return result; } diff --git a/lib/strutil/strutilutf8.c b/lib/strutil/strutilutf8.c index 3f4b83692..69dd7c89a 100644 --- a/lib/strutil/strutilutf8.c +++ b/lib/strutil/strutilutf8.c @@ -42,8 +42,7 @@ str_unichar_iscombiningmark (gunichar uni) { int type = g_unichar_type (uni); return (type == G_UNICODE_COMBINING_MARK) - || (type == G_UNICODE_ENCLOSING_MARK) - || (type == G_UNICODE_NON_SPACING_MARK); + || (type == G_UNICODE_ENCLOSING_MARK) || (type == G_UNICODE_NON_SPACING_MARK); } static void @@ -64,11 +63,11 @@ str_utf8_is_valid_char (const char *ch, size_t size) switch (g_utf8_get_char_validated (ch, size)) { case (gunichar) (-2): - return -2; + return -2; case (gunichar) (-1): - return -1; + return -1; default: - return 1; + return 1; } } @@ -88,9 +87,9 @@ static void str_utf8_cnext_char_safe (const char **text) { if (str_utf8_is_valid_char (*text, -1) == 1) - (*text) = g_utf8_next_char (*text); + (*text) = g_utf8_next_char (*text); else - (*text)++; + (*text)++; } static void @@ -100,9 +99,9 @@ str_utf8_cprev_char_safe (const char **text) const char *t = result; str_utf8_cnext_char_safe (&t); if (t == *text) - (*text) = result; + (*text) = result; else - (*text)--; + (*text)--; } static void @@ -112,16 +111,16 @@ str_utf8_fix_string (char *text) while (text[0] != '\0') { - uni = g_utf8_get_char_validated (text, -1); - if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) - { - text = g_utf8_next_char (text); - } - else - { - text[0] = '?'; - text++; - } + uni = g_utf8_get_char_validated (text, -1); + if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) + { + text = g_utf8_next_char (text); + } + else + { + text[0] = '?'; + text++; + } } } @@ -173,10 +172,10 @@ str_utf8_cnext_noncomb_char (const char **text) int count = 0; while ((*text)[0] != '\0') { - str_utf8_cnext_char_safe (text); - count++; - if (!str_utf8_iscombiningmark (*text)) - break; + str_utf8_cnext_char_safe (text); + count++; + if (!str_utf8_iscombiningmark (*text)) + break; } return count; } @@ -187,10 +186,10 @@ str_utf8_cprev_noncomb_char (const char **text, const char *begin) int count = 0; while ((*text) != begin) { - str_utf8_cprev_char_safe (text); - count++; - if (!str_utf8_iscombiningmark (*text)) - break; + str_utf8_cprev_char_safe (text); + count++; + if (!str_utf8_iscombiningmark (*text)) + break; } return count; } @@ -203,12 +202,12 @@ str_utf8_toupper (const char *text, char **out, size_t * remain) uni = g_utf8_get_char_validated (text, -1); if (uni == (gunichar) (-1) || uni == (gunichar) (-2)) - return 0; + return 0; uni = g_unichar_toupper (uni); left = g_unichar_to_utf8 (uni, NULL); if (left >= *remain) - return 0; + return 0; left = g_unichar_to_utf8 (uni, *out); (*out) += left; @@ -224,12 +223,12 @@ str_utf8_tolower (const char *text, char **out, size_t * remain) uni = g_utf8_get_char_validated (text, -1); if (uni == (gunichar) (-1) || uni == (gunichar) (-2)) - return 0; + return 0; uni = g_unichar_tolower (uni); left = g_unichar_to_utf8 (uni, NULL); if (left >= *remain) - return 0; + return 0; left = g_unichar_to_utf8 (uni, *out); (*out) += left; @@ -247,24 +246,24 @@ str_utf8_length (const char *text) start = text; while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) - { - result += g_utf8_strlen (start, end - start); - } - result++; - start = end + 1; + if (start != end) + { + result += g_utf8_strlen (start, end - start); + } + result++; + start = end + 1; } if (start == text) { - result = g_utf8_strlen (text, -1); + result = g_utf8_strlen (text, -1); } else { - if (start[0] != '\0' && start != end) - { - result += g_utf8_strlen (start, end - start); - } + if (start[0] != '\0' && start != end) + { + result += g_utf8_strlen (start, end - start); + } } return result; @@ -280,26 +279,26 @@ str_utf8_length2 (const char *text, int size) start = text; while (!g_utf8_validate (start, -1, &end) && start[0] != '\0' && size > 0) { - if (start != end) - { - result += g_utf8_strlen (start, min (end - start, size)); - size -= end - start; - } - result += (size > 0); - size--; - start = end + 1; + if (start != end) + { + result += g_utf8_strlen (start, min (end - start, size)); + size -= end - start; + } + result += (size > 0); + size--; + start = end + 1; } if (start == text) { - result = g_utf8_strlen (text, size); + result = g_utf8_strlen (text, size); } else { - if (start[0] != '\0' && start != end && size > 0) - { - result += g_utf8_strlen (start, min (end - start, size)); - } + if (start[0] != '\0' && start != end && size > 0) + { + result += g_utf8_strlen (start, min (end - start, size)); + } } return result; @@ -313,45 +312,45 @@ str_utf8_length_noncomb (const char *text) while (t[0] != '\0') { - str_utf8_cnext_noncomb_char (&t); - result++; + str_utf8_cnext_noncomb_char (&t); + result++; } return result; } + /* -static void -str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer) -{ - char *next = g_utf8_next_char (*string); - (*left) -= next - (*string); - (*string) = next; - g_string_append_c (buffer, '?'); -} -*/ + static void + str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer) + { + char *next = g_utf8_next_char (*string); + (*left) -= next - (*string); + (*string) = next; + g_string_append_c (buffer, '?'); + } + */ static gchar * -str_utf8_conv_gerror_message (GError *error, const char *def_msg) +str_utf8_conv_gerror_message (GError * error, const char *def_msg) { if ((error != NULL) && (error->message != NULL)) - return g_strdup (error->message); + return g_strdup (error->message); return g_strdup (def_msg != NULL ? def_msg : ""); } static estr_t -str_utf8_vfs_convert_to (GIConv coder, const char *string, - int size, GString * buffer) +str_utf8_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { estr_t result; if (coder == str_cnv_not_convert) { - g_string_append_len (buffer, string, size); - result = ESTR_SUCCESS; + g_string_append_len (buffer, string, size); + result = ESTR_SUCCESS; } else - result = str_nconvert (coder, (char *) string, size, buffer); + result = str_nconvert (coder, (char *) string, size, buffer); return result; } @@ -382,43 +381,56 @@ str_utf8_make_make_term_form (const char *text, size_t length) * add space at begin in this case */ if (length != 0 && text[0] != '\0') { - uni = g_utf8_get_char_validated (text, -1); - if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) - { - if (str_unichar_iscombiningmark (uni)) - { - actual[0] = ' '; - actual++; - result.width++; - result.compose = 1; - } - } - } - - while (length != 0 && text[0] != '\0') { uni = g_utf8_get_char_validated (text, -1); - if ((uni != (gunichar)(-1)) && (uni != (gunichar)(-2))) { - if (g_unichar_isprint(uni)) { + if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) + { + if (str_unichar_iscombiningmark (uni)) + { + actual[0] = ' '; + actual++; + result.width++; + result.compose = 1; + } + } + } + + while (length != 0 && text[0] != '\0') + { + uni = g_utf8_get_char_validated (text, -1); + if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) + { + if (g_unichar_isprint (uni)) + { left = g_unichar_to_utf8 (uni, actual); - actual+= left; - if (!str_unichar_iscombiningmark (uni)) { + actual += left; + if (!str_unichar_iscombiningmark (uni)) + { result.width++; - if (g_unichar_iswide(uni)) result.width++; - } else result.compose = 1; - } else { + if (g_unichar_iswide (uni)) + result.width++; + } + else + result.compose = 1; + } + else + { actual[0] = '.'; actual++; result.width++; } text = g_utf8_next_char (text); - } else { + } + else + { text++; - /*actual[0] = '?';*/ + /*actual[0] = '?'; */ memcpy (actual, replch, strlen (replch)); - actual+= strlen (replch); + actual += strlen (replch); result.width++; } - if (length != (size_t) (-1)) length--; } + if (length != (size_t) (-1)) + length--; + } actual[0] = '\0'; return &result; @@ -434,15 +446,13 @@ str_utf8_term_form (const char *text) pre_form = str_utf8_make_make_term_form (text, (size_t) (-1)); if (pre_form->compose) { - composed = - g_utf8_normalize (pre_form->text, -1, - G_NORMALIZE_DEFAULT_COMPOSE); - g_strlcpy (result, composed, sizeof (result)); - g_free (composed); + composed = g_utf8_normalize (pre_form->text, -1, G_NORMALIZE_DEFAULT_COMPOSE); + g_strlcpy (result, composed, sizeof (result)); + g_free (composed); } else { - g_strlcpy (result, pre_form->text, sizeof (result)); + g_strlcpy (result, pre_form->text, sizeof (result)); } return result; } @@ -467,15 +477,15 @@ utf8_tool_copy_chars_to_end (struct utf8_tool *tool) while (tool->cheked[0] != '\0') { - uni = g_utf8_get_char (tool->cheked); - tool->compose |= str_unichar_iscombiningmark (uni); - left = g_unichar_to_utf8 (uni, NULL); - if (tool->remain <= left) - return 0; - left = g_unichar_to_utf8 (uni, tool->actual); - tool->actual += left; - tool->remain -= left; - tool->cheked = g_utf8_next_char (tool->cheked); + uni = g_utf8_get_char (tool->cheked); + tool->compose |= str_unichar_iscombiningmark (uni); + left = g_unichar_to_utf8 (uni, NULL); + if (tool->remain <= left) + return 0; + left = g_unichar_to_utf8 (uni, tool->actual); + tool->actual += left; + tool->remain -= left; + tool->cheked = g_utf8_next_char (tool->cheked); } return 1; } @@ -493,29 +503,29 @@ utf8_tool_copy_chars_to (struct utf8_tool *tool, int to_ident) while (tool->cheked[0] != '\0') { - uni = g_utf8_get_char (tool->cheked); - if (!str_unichar_iscombiningmark (uni)) - { - w = 1; - if (g_unichar_iswide (uni)) - w++; - if (tool->ident + w > to_ident) - return 1; - } - else - { - w = 0; - tool->compose = 1; - } + uni = g_utf8_get_char (tool->cheked); + if (!str_unichar_iscombiningmark (uni)) + { + w = 1; + if (g_unichar_iswide (uni)) + w++; + if (tool->ident + w > to_ident) + return 1; + } + else + { + w = 0; + tool->compose = 1; + } - left = g_unichar_to_utf8 (uni, NULL); - if (tool->remain <= left) - return 0; - left = g_unichar_to_utf8 (uni, tool->actual); - tool->actual += left; - tool->remain -= left; - tool->cheked = g_utf8_next_char (tool->cheked); - tool->ident += w; + left = g_unichar_to_utf8 (uni, NULL); + if (tool->remain <= left) + return 0; + left = g_unichar_to_utf8 (uni, tool->actual); + tool->actual += left; + tool->remain -= left; + tool->cheked = g_utf8_next_char (tool->cheked); + tool->ident += w; } return 1; } @@ -525,9 +535,9 @@ static int utf8_tool_insert_space (struct utf8_tool *tool, int count) { if (count <= 0) - return 1; + return 1; if (tool->remain <= (gsize) count) - return 0; + return 0; memset (tool->actual, ' ', count); tool->actual += count; tool->remain -= count; @@ -539,7 +549,7 @@ static int utf8_tool_insert_char (struct utf8_tool *tool, char ch) { if (tool->remain <= 1) - return 0; + return 0; tool->actual[0] = ch; tool->actual++; tool->remain--; @@ -555,20 +565,20 @@ utf8_tool_skip_chars_to (struct utf8_tool *tool, int to_ident) while (to_ident > tool->ident && tool->cheked[0] != '\0') { - uni = g_utf8_get_char (tool->cheked); - if (!str_unichar_iscombiningmark (uni)) - { - tool->ident++; - if (g_unichar_iswide (uni)) - tool->ident++; - } - tool->cheked = g_utf8_next_char (tool->cheked); + uni = g_utf8_get_char (tool->cheked); + if (!str_unichar_iscombiningmark (uni)) + { + tool->ident++; + if (g_unichar_iswide (uni)) + tool->ident++; + } + tool->cheked = g_utf8_next_char (tool->cheked); } uni = g_utf8_get_char (tool->cheked); while (str_unichar_iscombiningmark (uni)) { - tool->cheked = g_utf8_next_char (tool->cheked); - uni = g_utf8_get_char (tool->cheked); + tool->cheked = g_utf8_next_char (tool->cheked); + uni = g_utf8_get_char (tool->cheked); } return 1; } @@ -576,8 +586,7 @@ utf8_tool_skip_chars_to (struct utf8_tool *tool, int to_ident) static void utf8_tool_compose (char *buffer, size_t size) { - char *composed = - g_utf8_normalize (buffer, -1, G_NORMALIZE_DEFAULT_COMPOSE); + char *composed = g_utf8_normalize (buffer, -1, G_NORMALIZE_DEFAULT_COMPOSE); g_strlcpy (buffer, composed, size); g_free (composed); } @@ -596,62 +605,60 @@ str_utf8_fit_to_term (const char *text, int width, align_crt_t just_mode) tool.remain = sizeof (result); tool.compose = 0; - if (pre_form->width <= (gsize)width) + if (pre_form->width <= (gsize) width) { - tool.ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER_LEFT: - case J_CENTER: - tool.ident = (width - pre_form->width) / 2; - break; - case J_RIGHT: - tool.ident = width - pre_form->width; - break; - } + tool.ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER_LEFT: + case J_CENTER: + tool.ident = (width - pre_form->width) / 2; + break; + case J_RIGHT: + tool.ident = width - pre_form->width; + break; + } - utf8_tool_insert_space (&tool, tool.ident); - utf8_tool_copy_chars_to_end (&tool); - utf8_tool_insert_space (&tool, width - pre_form->width - tool.ident); + utf8_tool_insert_space (&tool, tool.ident); + utf8_tool_copy_chars_to_end (&tool); + utf8_tool_insert_space (&tool, width - pre_form->width - tool.ident); } else { - if (IS_FIT (just_mode)) - { - tool.ident = 0; - utf8_tool_copy_chars_to (&tool, width / 2); - utf8_tool_insert_char (&tool, '~'); + if (IS_FIT (just_mode)) + { + tool.ident = 0; + utf8_tool_copy_chars_to (&tool, width / 2); + utf8_tool_insert_char (&tool, '~'); - tool.ident = 0; - utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1); - utf8_tool_copy_chars_to_end (&tool); - utf8_tool_insert_space (&tool, - width - (pre_form->width - tool.ident + - 1)); - } - else - { - tool.ident = 0; - switch (HIDE_FIT (just_mode)) - { - case J_CENTER: - tool.ident = (width - pre_form->width) / 2; - break; - case J_RIGHT: - tool.ident = width - pre_form->width; - break; - } + tool.ident = 0; + utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1); + utf8_tool_copy_chars_to_end (&tool); + utf8_tool_insert_space (&tool, width - (pre_form->width - tool.ident + 1)); + } + else + { + tool.ident = 0; + switch (HIDE_FIT (just_mode)) + { + case J_CENTER: + tool.ident = (width - pre_form->width) / 2; + break; + case J_RIGHT: + tool.ident = width - pre_form->width; + break; + } - utf8_tool_skip_chars_to (&tool, 0); - utf8_tool_insert_space (&tool, tool.ident); - utf8_tool_copy_chars_to (&tool, width); - utf8_tool_insert_space (&tool, width - tool.ident); - } + utf8_tool_skip_chars_to (&tool, 0); + utf8_tool_insert_space (&tool, tool.ident); + utf8_tool_copy_chars_to (&tool, width); + utf8_tool_insert_space (&tool, width - tool.ident); + } } tool.actual[0] = '\0'; if (tool.compose) - utf8_tool_compose (result, sizeof (result)); + utf8_tool_compose (result, sizeof (result)); return result; } @@ -669,33 +676,33 @@ str_utf8_term_trim (const char *text, int width) tool.remain = sizeof (result); tool.compose = 0; - if ((gsize)width < pre_form->width) + if ((gsize) width < pre_form->width) { - if (width <= 3) - { - memset (tool.actual, '.', width); - tool.actual += width; - tool.remain -= width; - } - else - { - memset (tool.actual, '.', 3); - tool.actual += 3; - tool.remain -= 3; + if (width <= 3) + { + memset (tool.actual, '.', width); + tool.actual += width; + tool.remain -= width; + } + else + { + memset (tool.actual, '.', 3); + tool.actual += 3; + tool.remain -= 3; - tool.ident = 0; - utf8_tool_skip_chars_to (&tool, pre_form->width - width + 3); - utf8_tool_copy_chars_to_end (&tool); - } + tool.ident = 0; + utf8_tool_skip_chars_to (&tool, pre_form->width - width + 3); + utf8_tool_copy_chars_to_end (&tool); + } } else { - utf8_tool_copy_chars_to_end (&tool); + utf8_tool_copy_chars_to_end (&tool); } tool.actual[0] = '\0'; if (tool.compose) - utf8_tool_compose (result, sizeof (result)); + utf8_tool_compose (result, sizeof (result)); return result; } @@ -718,8 +725,7 @@ static int str_utf8_term_char_width (const char *text) { gunichar uni = g_utf8_get_char_validated (text, -1); - return (str_unichar_iscombiningmark (uni)) ? 0 - : ((g_unichar_iswide (uni)) ? 2 : 1); + return (str_unichar_iscombiningmark (uni)) ? 0 : ((g_unichar_iswide (uni)) ? 2 : 1); } static void @@ -737,22 +743,22 @@ str_utf8_msg_term_size (const char *text, int *lines, int *columns) p = tmp; for (;;) { - q = strchr (p, '\n'); - if (q != NULL) - { - c = q[0]; - q[0] = '\0'; - } + q = strchr (p, '\n'); + if (q != NULL) + { + c = q[0]; + q[0] = '\0'; + } - width = str_utf8_term_width1 (p); - if (width > (*columns)) - (*columns) = width; + width = str_utf8_term_width1 (p); + if (width > (*columns)) + (*columns) = width; - if (q == NULL) - break; - q[0] = c; - p = q + 1; - (*lines)++; + if (q == NULL) + break; + q[0] = c; + p = q + 1; + (*lines)++; } g_free (tmp); } @@ -774,7 +780,7 @@ str_utf8_term_substring (const char *text, int start, int width) tool.ident = -start; utf8_tool_skip_chars_to (&tool, 0); if (tool.ident < 0) - tool.ident = 0; + tool.ident = 0; utf8_tool_insert_space (&tool, tool.ident); utf8_tool_copy_chars_to (&tool, width); @@ -782,7 +788,7 @@ str_utf8_term_substring (const char *text, int start, int width) tool.actual[0] = '\0'; if (tool.compose) - utf8_tool_compose (result, sizeof (result)); + utf8_tool_compose (result, sizeof (result)); return result; } @@ -800,24 +806,24 @@ str_utf8_trunc (const char *text, int width) tool.remain = sizeof (result); tool.compose = 0; - if (pre_form->width > (gsize)width) + if (pre_form->width > (gsize) width) { - tool.ident = 0; - utf8_tool_copy_chars_to (&tool, width / 2); - utf8_tool_insert_char (&tool, '~'); + tool.ident = 0; + utf8_tool_copy_chars_to (&tool, width / 2); + utf8_tool_insert_char (&tool, '~'); - tool.ident = 0; - utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1); - utf8_tool_copy_chars_to_end (&tool); + tool.ident = 0; + utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1); + utf8_tool_copy_chars_to_end (&tool); } else { - utf8_tool_copy_chars_to_end (&tool); + utf8_tool_copy_chars_to_end (&tool); } tool.actual[0] = '\0'; if (tool.compose) - utf8_tool_compose (result, sizeof (result)); + utf8_tool_compose (result, sizeof (result)); return result; } @@ -825,16 +831,16 @@ static int str_utf8_offset_to_pos (const char *text, size_t length) { if (str_utf8_is_valid_string (text)) - return g_utf8_offset_to_pointer (text, length) - text; + return g_utf8_offset_to_pointer (text, length) - text; else { - int result; - GString *buffer = g_string_new (text); + int result; + GString *buffer = g_string_new (text); - str_utf8_fix_string (buffer->str); - result = g_utf8_offset_to_pointer (buffer->str, length) - buffer->str; - g_string_free (buffer, TRUE); - return result; + str_utf8_fix_string (buffer->str); + result = g_utf8_offset_to_pointer (buffer->str, length) - buffer->str; + g_string_free (buffer, TRUE); + return result; } } @@ -850,33 +856,33 @@ str_utf8_column_to_pos (const char *text, size_t pos) while (text[0] != '\0') { - uni = g_utf8_get_char_validated (text, 6); - if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) - { - if (g_unichar_isprint (uni)) - { - if (!str_unichar_iscombiningmark (uni)) - { - width++; - if (g_unichar_iswide (uni)) - width++; - } - } - else - { - width++; - } - text = g_utf8_next_char (text); - } - else - { - text++; - width++; - } - if ((gsize)width > pos) - return result; + uni = g_utf8_get_char_validated (text, 6); + if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2))) + { + if (g_unichar_isprint (uni)) + { + if (!str_unichar_iscombiningmark (uni)) + { + width++; + if (g_unichar_iswide (uni)) + width++; + } + } + else + { + width++; + } + text = g_utf8_next_char (text); + } + else + { + text++; + width++; + } + if ((gsize) width > pos) + return result; - result++; + result++; } return result; @@ -887,20 +893,20 @@ str_utf8_create_search_needle (const char *needle, int case_sen) { if (needle != NULL) { - if (case_sen) - { - return g_utf8_normalize (needle, -1, G_NORMALIZE_ALL); - } - else - { - char *fold = g_utf8_casefold (needle, -1); - char *result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - g_free (fold); - return result; - } + if (case_sen) + { + return g_utf8_normalize (needle, -1, G_NORMALIZE_ALL); + } + else + { + char *fold = g_utf8_casefold (needle, -1); + char *result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_free (fold); + return result; + } } else - return NULL; + return NULL; } static void @@ -908,7 +914,7 @@ str_utf8_release_search_needle (char *needle, int case_sen) { (void) case_sen; if (needle != NULL) - g_free (needle); + g_free (needle); } static const char * @@ -926,32 +932,32 @@ str_utf8_search_first (const char *text, const char *search, int case_sen) match = deco_text; do { - match = g_strstr_len (match, -1, search); - if (match != NULL) - { - if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) && - !str_utf8_iscombiningmark (match + strlen (search))) - { + match = g_strstr_len (match, -1, search); + if (match != NULL) + { + if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) && + !str_utf8_iscombiningmark (match + strlen (search))) + { - result = text; - m = deco_text; - while (m < match) - { - str_utf8_cnext_noncomb_char (&m); - str_utf8_cnext_noncomb_char (&result); - } - } - else - { - str_utf8_cnext_char (&match); - } - } + result = text; + m = deco_text; + while (m < match) + { + str_utf8_cnext_noncomb_char (&m); + str_utf8_cnext_noncomb_char (&result); + } + } + else + { + str_utf8_cnext_char (&match); + } + } } while (match != NULL && result == NULL); g_free (deco_text); if (!case_sen) - g_free (fold_text); + g_free (fold_text); return result; } @@ -970,32 +976,32 @@ str_utf8_search_last (const char *text, const char *search, int case_sen) do { - match = g_strrstr_len (deco_text, -1, search); - if (match != NULL) - { - if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) && - !str_utf8_iscombiningmark (match + strlen (search))) - { + match = g_strrstr_len (deco_text, -1, search); + if (match != NULL) + { + if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) && + !str_utf8_iscombiningmark (match + strlen (search))) + { - result = text; - m = deco_text; - while (m < match) - { - str_utf8_cnext_noncomb_char (&m); - str_utf8_cnext_noncomb_char (&result); - } - } - else - { - match[0] = '\0'; - } - } + result = text; + m = deco_text; + while (m < match) + { + str_utf8_cnext_noncomb_char (&m); + str_utf8_cnext_noncomb_char (&result); + } + } + else + { + match[0] = '\0'; + } + } } while (match != NULL && result == NULL); g_free (deco_text); if (!case_sen) - g_free (fold_text); + g_free (fold_text); return result; } @@ -1012,29 +1018,29 @@ str_utf8_normalize (const char *text) start = text; while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) - { - tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); - g_string_append (fixed, tmp); - g_free (tmp); - } - g_string_append_c (fixed, end[0]); - start = end + 1; + if (start != end) + { + tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + } + g_string_append_c (fixed, end[0]); + start = end + 1; } if (start == text) { - result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); + result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); } else { - if (start[0] != '\0' && start != end) - { - tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); - g_string_append (fixed, tmp); - g_free (tmp); - } - result = g_strdup (fixed->str); + if (start[0] != '\0' && start != end) + { + tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + } + result = g_strdup (fixed->str); } g_string_free (fixed, TRUE); @@ -1053,35 +1059,35 @@ str_utf8_casefold_normalize (const char *text) start = text; while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) - { - fold = g_utf8_casefold (start, end - start); - tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - g_string_append (fixed, tmp); - g_free (tmp); - g_free (fold); - } - g_string_append_c (fixed, end[0]); - start = end + 1; + if (start != end) + { + fold = g_utf8_casefold (start, end - start); + tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + g_free (fold); + } + g_string_append_c (fixed, end[0]); + start = end + 1; } if (start == text) { - fold = g_utf8_casefold (text, -1); - result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - g_free (fold); + fold = g_utf8_casefold (text, -1); + result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_free (fold); } else { - if (start[0] != '\0' && start != end) - { - fold = g_utf8_casefold (start, end - start); - tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - g_string_append (fixed, tmp); - g_free (tmp); - g_free (fold); - } - result = g_strdup (fixed->str); + if (start[0] != '\0' && start != end) + { + fold = g_utf8_casefold (start, end - start); + tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + g_free (fold); + } + result = g_strdup (fixed->str); } g_string_free (fixed, TRUE); @@ -1169,14 +1175,14 @@ str_utf8_prefix (const char *text, const char *prefix) while (nt[0] != '\0' && np[0] != '\0') { - str_utf8_cnext_char_safe (&nnt); - str_utf8_cnext_char_safe (&nnp); - if (nnt - nt != nnp - np) - break; - if (strncmp (nt, np, nnt - nt) != 0) - break; - nt = nnt; - np = nnp; + str_utf8_cnext_char_safe (&nnt); + str_utf8_cnext_char_safe (&nnp); + if (nnt - nt != nnp - np) + break; + if (strncmp (nt, np, nnt - nt) != 0) + break; + nt = nnt; + np = nnp; } result = np - p; @@ -1200,14 +1206,14 @@ str_utf8_caseprefix (const char *text, const char *prefix) while (nt[0] != '\0' && np[0] != '\0') { - str_utf8_cnext_char_safe (&nnt); - str_utf8_cnext_char_safe (&nnp); - if (nnt - nt != nnp - np) - break; - if (strncmp (nt, np, nnt - nt) != 0) - break; - nt = nnt; - np = nnp; + str_utf8_cnext_char_safe (&nnt); + str_utf8_cnext_char_safe (&nnp); + if (nnt - nt != nnp - np) + break; + if (strncmp (nt, np, nnt - nt) != 0) + break; + nt = nnt; + np = nnp; } result = np - p; @@ -1220,13 +1226,16 @@ str_utf8_caseprefix (const char *text, const char *prefix) static char * str_utf8_create_key_gen (const char *text, int case_sen, - gchar * (*keygen) (const gchar *text, gssize size)) + gchar * (*keygen) (const gchar * text, gssize size)) { char *result; - - if (case_sen) { + + if (case_sen) + { result = str_utf8_normalize (text); - } else { + } + else + { gboolean dot; GString *fixed; const char *start, *end; @@ -1299,8 +1308,7 @@ str_utf8_create_key (const char *text, int case_sen) static char * str_utf8_create_key_for_filename (const char *text, int case_sen) { - return str_utf8_create_key_gen (text, case_sen, - g_utf8_collate_key_for_filename); + return str_utf8_create_key_gen (text, case_sen, g_utf8_collate_key_for_filename); } #endif @@ -1318,7 +1326,7 @@ str_utf8_release_key (char *key, int case_sen) g_free (key); } -struct str_class +struct str_class str_utf8_init (void) { struct str_class result; diff --git a/lib/strutil/strverscmp.c b/lib/strutil/strverscmp.c index 91a94f0cc..bfae037f9 100644 --- a/lib/strutil/strverscmp.c +++ b/lib/strutil/strverscmp.c @@ -26,7 +26,7 @@ #include "lib/strutil.h" /* states: S_N: normal, S_I: comparing integral part, S_F: comparing - fractionnal parts, S_Z: idem but with leading Zeroes only */ + fractionnal parts, S_Z: idem but with leading Zeroes only */ #define S_N 0x0 #define S_I 0x4 #define S_F 0x8 @@ -40,78 +40,77 @@ /* Compare S1 and S2 as strings holding indices/version numbers, returning less than, equal to or greater than zero if S1 is less than, equal to or greater than S2 (for more info, see the texinfo doc). -*/ + */ -int str_verscmp (const char *s1, const char *s2) +int +str_verscmp (const char *s1, const char *s2) { #ifdef HAVE_STRVERSCMP - return strverscmp(s1, s2); + return strverscmp (s1, s2); #else /* HAVE_STRVERSCMP */ - unsigned char *p1 = (unsigned char *) s1; - unsigned char *p2 = (unsigned char *) s2; - unsigned char c1, c2; - int state; - int diff; + unsigned char *p1 = (unsigned char *) s1; + unsigned char *p2 = (unsigned char *) s2; + unsigned char c1, c2; + int state; + int diff; - /* Symbol(s) 0 [1-9] others (padding) - Transition (10) 0 (01) d (00) x (11) - */ - static const unsigned int next_state[] = - { - /* state x d 0 - */ - /* S_N */ S_N, S_I, S_Z, S_N, - /* S_I */ S_N, S_I, S_I, S_I, - /* S_F */ S_N, S_F, S_F, S_F, - /* S_Z */ S_N, S_F, S_Z, S_Z - }; + /* Symbol(s) 0 [1-9] others (padding) + Transition (10) 0 (01) d (00) x (11) - */ + static const unsigned int next_state[] = { + /* state x d 0 - */ + /* S_N */ S_N, S_I, S_Z, S_N, + /* S_I */ S_N, S_I, S_I, S_I, + /* S_F */ S_N, S_F, S_F, S_F, + /* S_Z */ S_N, S_F, S_Z, S_Z + }; - static const int result_type[] = - { - /* state x/x x/d x/0 x/- d/x d/d d/0 d/- - 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */ + static const int result_type[] = { + /* state x/x x/d x/0 x/- d/x d/d d/0 d/- + 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */ - /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, - CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, - /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP, - +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP, - /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, - CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, - /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP, - -1, CMP, CMP, CMP - }; + /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, + CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, + /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP, + +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP, + /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, + CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, + /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP, + -1, CMP, CMP, CMP + }; - if (p1 == p2) - return 0; + if (p1 == p2) + return 0; - c1 = *p1++; - c2 = *p2++; - /* Hint: '0' is a digit too. */ - state = S_N | ((c1 == '0') + (isdigit (c1) != 0)); + c1 = *p1++; + c2 = *p2++; + /* Hint: '0' is a digit too. */ + state = S_N | ((c1 == '0') + (isdigit (c1) != 0)); - while ((diff = c1 - c2) == 0 && c1 != '\0') + while ((diff = c1 - c2) == 0 && c1 != '\0') { - state = next_state[state]; - c1 = *p1++; - c2 = *p2++; - state |= (c1 == '0') + (isdigit (c1) != 0); + state = next_state[state]; + c1 = *p1++; + c2 = *p2++; + state |= (c1 == '0') + (isdigit (c1) != 0); } - state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))]; + state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))]; - switch (state) - { + switch (state) + { case CMP: - return diff; + return diff; case LEN: - while (isdigit (*p1++)) - if (!isdigit (*p2++)) - return 1; + while (isdigit (*p1++)) + if (!isdigit (*p2++)) + return 1; - return isdigit (*p2) ? -1 : diff; + return isdigit (*p2) ? -1 : diff; default: - return state; - } + return state; + } #endif /* HAVE_STRVERSCMP */ } diff --git a/lib/timefmt.h b/lib/timefmt.h index e80791b6e..c7dcc3da3 100644 --- a/lib/timefmt.h +++ b/lib/timefmt.h @@ -3,35 +3,48 @@ * \brief Header: time formating macroses */ -#ifndef __UTIL_TIMEFMT_H -#define __UTIL_TIMEFMT_H +#ifndef MC__UTIL_TIMEFMT_H +#define MC__UTIL_TIMEFMT_H #include #include -#define INVALID_TIME_TEXT "(invalid)" +/*** typedefs(not structures) and defined constants **********************************************/ + + +#define INVALID_TIME_TEXT "(invalid)" /* safe localtime formatting - strftime()-using version */ -#define FMT_LOCALTIME(buffer, bufsize, fmt, when) \ - { \ - struct tm *whentm; \ - whentm = localtime(&when); \ - if (whentm == NULL) \ - { \ - strncpy(buffer, INVALID_TIME_TEXT, bufsize); \ - buffer[bufsize-1] = 0; \ - } \ - else \ - { \ - strftime(buffer, bufsize, fmt, whentm); \ - } \ - } \ +#define FMT_LOCALTIME(buffer, bufsize, fmt, when) \ + { \ + struct tm *whentm; \ + whentm = localtime(&when); \ + if (whentm == NULL) \ + { \ + strncpy(buffer, INVALID_TIME_TEXT, bufsize); \ + buffer[bufsize-1] = 0; \ + } \ + else \ + { \ + strftime(buffer, bufsize, fmt, whentm); \ + } \ + } \ -#define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt) \ - { \ - time_t __current_time; \ - time(&__current_time); \ - FMT_LOCALTIME(buffer,bufsize,fmt,__current_time); \ +#define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt) \ + { \ + time_t __current_time; \ + time(&__current_time); \ + FMT_LOCALTIME(buffer,bufsize,fmt,__current_time); \ } -#endif /* !__UTIL_H */ +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ + +#endif /* !__UTIL_H */ diff --git a/lib/tty/color-internal.c b/lib/tty/color-internal.c index f5778c8a6..d1ebe2e24 100644 --- a/lib/tty/color-internal.c +++ b/lib/tty/color-internal.c @@ -1,11 +1,11 @@ /* Internal stuff of color setup Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc. - + Written by: Andrew Borodin , 2009. Slava Zanko , 2009. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -28,7 +28,7 @@ #include /* strcmp */ -#include "color.h" /* colors and attributes */ +#include "color.h" /* colors and attributes */ #include "color-internal.h" /*** global variables ****************************************************************************/ @@ -39,7 +39,8 @@ gboolean mc_tty_color_disable; /*** file scope type declarations ****************************************************************/ -typedef struct mc_tty_color_table_struct { +typedef struct mc_tty_color_table_struct +{ const char *name; int value; } mc_tty_color_table_t; @@ -47,30 +48,30 @@ typedef struct mc_tty_color_table_struct { /*** file scope variables ************************************************************************/ mc_tty_color_table_t const color_table[] = { - { "black", COLOR_BLACK }, - { "gray", COLOR_BLACK | A_BOLD }, - { "red", COLOR_RED }, - { "brightred", COLOR_RED | A_BOLD }, - { "green", COLOR_GREEN }, - { "brightgreen", COLOR_GREEN | A_BOLD }, - { "brown", COLOR_YELLOW }, - { "yellow", COLOR_YELLOW | A_BOLD }, - { "blue", COLOR_BLUE }, - { "brightblue", COLOR_BLUE | A_BOLD }, - { "magenta", COLOR_MAGENTA }, - { "brightmagenta", COLOR_MAGENTA | A_BOLD }, - { "cyan", COLOR_CYAN }, - { "brightcyan", COLOR_CYAN | A_BOLD }, - { "lightgray", COLOR_WHITE }, - { "white", COLOR_WHITE | A_BOLD }, - { "default", -1 }, /* default color of the terminal */ + {"black", COLOR_BLACK}, + {"gray", COLOR_BLACK | A_BOLD}, + {"red", COLOR_RED}, + {"brightred", COLOR_RED | A_BOLD}, + {"green", COLOR_GREEN}, + {"brightgreen", COLOR_GREEN | A_BOLD}, + {"brown", COLOR_YELLOW}, + {"yellow", COLOR_YELLOW | A_BOLD}, + {"blue", COLOR_BLUE}, + {"brightblue", COLOR_BLUE | A_BOLD}, + {"magenta", COLOR_MAGENTA}, + {"brightmagenta", COLOR_MAGENTA | A_BOLD}, + {"cyan", COLOR_CYAN}, + {"brightcyan", COLOR_CYAN | A_BOLD}, + {"lightgray", COLOR_WHITE}, + {"white", COLOR_WHITE | A_BOLD}, + {"default", -1}, /* default color of the terminal */ /* special colors */ - { "A_REVERSE", SPEC_A_REVERSE }, - { "A_BOLD", SPEC_A_BOLD}, - { "A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE }, - { "A_UNDERLINE", SPEC_A_UNDERLINE }, + {"A_REVERSE", SPEC_A_REVERSE}, + {"A_BOLD", SPEC_A_BOLD}, + {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE}, + {"A_UNDERLINE", SPEC_A_UNDERLINE}, /* End of list */ - { NULL, 0} + {NULL, 0} }; /*** file scope functions ************************************************************************/ diff --git a/lib/tty/color-internal.h b/lib/tty/color-internal.h index 609d850c5..bce25f8b0 100644 --- a/lib/tty/color-internal.h +++ b/lib/tty/color-internal.h @@ -3,21 +3,33 @@ * \brief Header: Internal stuff of color setup */ -#ifndef MC_COLOR_INTERNAL_H -#define MC_COLOR_INTERNAL_H +#ifndef MC__COLOR_INTERNAL_H +#define MC__COLOR_INTERNAL_H #include /* size_t */ #include "lib/global.h" #ifdef HAVE_SLANG -# include "tty-slang.h" +#include "tty-slang.h" #else -# include "tty-ncurses.h" +#include "tty-ncurses.h" #endif /* HAVE_SLANG */ -extern gboolean use_colors; -extern gboolean mc_tty_color_disable; +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/* *INDENT-OFF* */ +typedef enum { + SPEC_A_REVERSE = -100, + SPEC_A_BOLD = -101, + SPEC_A_BOLD_REVERSE = -102, + SPEC_A_UNDERLINE = -103 +} tty_special_color_t; +/* *INDENT-ON* */ + +/*** structures declarations (and typedefs of structures)*****************************************/ typedef struct mc_color_pair_struct { @@ -29,14 +41,12 @@ typedef struct mc_color_pair_struct gboolean is_temp; } tty_color_pair_t; -/* *INDENT-OFF* */ -typedef enum { - SPEC_A_REVERSE = -100, - SPEC_A_BOLD = -101, - SPEC_A_BOLD_REVERSE = -102, - SPEC_A_UNDERLINE = -103 -} tty_special_color_t; -/* *INDENT-ON* */ +/*** global variables defined in .c file *********************************************************/ + +extern gboolean use_colors; +extern gboolean mc_tty_color_disable; + +/*** declarations of public functions ************************************************************/ const char *tty_color_get_valid_name (const char *); int tty_color_get_index_by_name (const char *); @@ -46,4 +56,5 @@ void tty_color_deinit_lib (void); void tty_color_try_alloc_pair_lib (tty_color_pair_t *); +/*** inline functions ****************************************************************************/ #endif /* MC_COLOR_INTERNAL_H */ diff --git a/lib/tty/color-ncurses.c b/lib/tty/color-ncurses.c index 750e7a1f3..24d0e69b9 100644 --- a/lib/tty/color-ncurses.c +++ b/lib/tty/color-ncurses.c @@ -1,10 +1,10 @@ /* Color setup for NCurses screen library Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc. - + Written by: Andrew Borodin , 2009. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -33,7 +33,7 @@ #include "lib/global.h" #include "tty-ncurses.h" -#include "color.h" /* variables */ +#include "color.h" /* variables */ #include "color-internal.h" /*** global variables ****************************************************************************/ @@ -67,7 +67,8 @@ mc_tty_color_save_attr_lib (int color_pair, int color_attr) return color_attr; key = g_try_new (int, 1); - if (key == NULL) { + if (key == NULL) + { g_free (attr); return color_attr; } @@ -88,7 +89,7 @@ color_get_attr (int color_pair) int *fnd = NULL; if (mc_tty_color_color_pair_attrs != NULL) - fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair); + fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair); return (fnd != NULL) ? *fnd : 0; } @@ -100,10 +101,10 @@ mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair, { if (has_colors () && !mc_tty_color_disable) init_pair (mc_color_pair->pair_index, - mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1); + mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1); else init_pair (mc_color_pair->pair_index, - mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2); + mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2); } /* --------------------------------------------------------------------------------------------- */ @@ -115,7 +116,8 @@ tty_color_init_lib (gboolean disable, gboolean force) { (void) force; - if (has_colors () && !disable) { + if (has_colors () && !disable) + { use_colors = TRUE; start_color (); use_default_colors (); @@ -139,8 +141,10 @@ tty_color_deinit_lib (void) void tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair) { - if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) { - switch (mc_color_pair->ifg) { + if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) + { + switch (mc_color_pair->ifg) + { case SPEC_A_REVERSE: mc_tty_color_pair_init_special (mc_color_pair, COLOR_BLACK, COLOR_WHITE, @@ -162,7 +166,9 @@ tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair) COLOR_WHITE, COLOR_BLACK, A_UNDERLINE); break; } - } else { + } + else + { int mask_fg = (mc_color_pair->ifg == -1) ? mc_color_pair->ifg : 0xff; int mask_bg = (mc_color_pair->ibg == -1) ? mc_color_pair->ibg : 0xff; diff --git a/lib/tty/color-slang.c b/lib/tty/color-slang.c index 279c26239..c3f84a787 100644 --- a/lib/tty/color-slang.c +++ b/lib/tty/color-slang.c @@ -1,10 +1,10 @@ /* Color setup for S_Lang screen library Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc. - + Written by: Andrew Borodin , 2009. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -33,10 +33,21 @@ #include "lib/global.h" #include "tty-slang.h" -#include "color.h" /* variables */ +#include "color.h" /* variables */ #include "color-internal.h" -#include "src/setup.h" /* color_terminal_string */ +#include "src/setup.h" /* color_terminal_string */ + +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static int has_colors (gboolean disable, gboolean force) @@ -46,7 +57,8 @@ has_colors (gboolean disable, gboolean force) if (force || (getenv ("COLORTERM") != NULL)) SLtt_Use_Ansi_Colors = 1; - if (!mc_tty_color_disable) { + if (!mc_tty_color_disable) + { const char *terminal = getenv ("TERM"); const size_t len = strlen (terminal); @@ -55,13 +67,15 @@ has_colors (gboolean disable, gboolean force) size_t i; /* check color_terminal_string */ - while (*cts != '\0') { + while (*cts != '\0') + { while (*cts == ' ' || *cts == '\t') cts++; s = cts; i = 0; - while (*cts != '\0' && *cts != ',') { + while (*cts != '\0' && *cts != ',') + { cts++; i++; } @@ -76,43 +90,62 @@ has_colors (gboolean disable, gboolean force) return SLtt_Use_Ansi_Colors; } +/* --------------------------------------------------------------------------------------------- */ + static void mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair, const char *fg1, const char *bg1, const char *fg2, const char *bg2, SLtt_Char_Type mask) { - if (SLtt_Use_Ansi_Colors != 0) { - if (!mc_tty_color_disable) { + if (SLtt_Use_Ansi_Colors != 0) + { + if (!mc_tty_color_disable) + { SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1); - } else { + } + else + { SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2); } - } else { + } + else + { SLtt_set_mono (mc_color_pair->pair_index, NULL, mask); } } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void tty_color_init_lib (gboolean disable, gboolean force) { /* FIXME: if S-Lang is used, has_colors() must be called regardless of whether we are interested in its result */ - if (has_colors (disable, force) && !disable) { + if (has_colors (disable, force) && !disable) + { use_colors = TRUE; } } +/* --------------------------------------------------------------------------------------------- */ + void tty_color_deinit_lib (void) { } +/* --------------------------------------------------------------------------------------------- */ + void tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair) { const char *fg, *bg; - if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) { - switch (mc_color_pair->ifg) { + if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) + { + switch (mc_color_pair->ifg) + { case SPEC_A_REVERSE: mc_tty_color_pair_init_special (mc_color_pair, "black", "white", "black", "lightgray", SLTT_REV_MASK); @@ -132,28 +165,40 @@ tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair) "white", "black", "white", "black", SLTT_ULINE_MASK); break; } - } else { + } + else + { fg = (mc_color_pair->cfg) ? mc_color_pair->cfg : "default"; bg = (mc_color_pair->cbg) ? mc_color_pair->cbg : "default"; SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg); } } +/* --------------------------------------------------------------------------------------------- */ + void tty_setcolor (int color) { SLsmg_set_color (color); } -/* Set colorpair by index, don't interpret S-Lang "emulated attributes" */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Set colorpair by index, don't interpret S-Lang "emulated attributes" + */ + void tty_lowlevel_setcolor (int color) { SLsmg_set_color (color & 0x7F); } +/* --------------------------------------------------------------------------------------------- */ + void tty_set_normal_attrs (void) { SLsmg_normal_video (); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/color-slang.h b/lib/tty/color-slang.h index b3c22e691..39013b9f3 100644 --- a/lib/tty/color-slang.h +++ b/lib/tty/color-slang.h @@ -3,12 +3,25 @@ * \brief Header: S-Lang-specific color setup */ -#ifndef MC_COLOR_SLANG_H -#define MC_COLOR_SLANG_H +#ifndef MC__COLOR_SLANG_H +#define MC__COLOR_SLANG_H -#include "tty-slang.h" /* S-Lang headers */ +#include "tty-slang.h" /* S-Lang headers */ -enum { +/*** typedefs(not structures) and defined constants **********************************************/ + +/* When using Slang with color, we have all the indexes free but + * those defined here (A_BOLD, A_UNDERLINE, A_REVERSE, A_BOLD_REVERSE) + */ + +#ifndef A_BOLD +#define A_BOLD SLTT_BOLD_MASK +#endif /* A_BOLD */ + +/*** enums ***************************************************************************************/ + +enum +{ COLOR_BLACK = 0, COLOR_RED, COLOR_GREEN, @@ -19,12 +32,11 @@ enum { COLOR_WHITE }; -/* When using Slang with color, we have all the indexes free but - * those defined here (A_BOLD, A_UNDERLINE, A_REVERSE, A_BOLD_REVERSE) - */ +/*** structures declarations (and typedefs of structures)*****************************************/ -#ifndef A_BOLD -#define A_BOLD SLTT_BOLD_MASK -#endif /* A_BOLD */ +/*** global variables defined in .c file *********************************************************/ +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ #endif /* MC_COLOR_SLANG_H */ diff --git a/lib/tty/color.h b/lib/tty/color.h index c204d0a2b..002ed0c75 100644 --- a/lib/tty/color.h +++ b/lib/tty/color.h @@ -1,4 +1,3 @@ - /** \file color.h * \brief Header: color setup * @@ -9,20 +8,32 @@ * */ -#ifndef MC_COLOR_H -#define MC_COLOR_H +#ifndef MC__COLOR_H +#define MC__COLOR_H -#include "lib/global.h" /* glib.h */ +#include "lib/global.h" /* glib.h */ #ifdef HAVE_SLANG -# include "color-slang.h" +#include "color-slang.h" #else -# include "tty-ncurses.h" +#include "tty-ncurses.h" #endif +/*** typedefs(not structures) and defined constants **********************************************/ + +#define ALLOC_COLOR_PAIR_INDEX 1 + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + /* colors specified on the command line: they override any other setting */ extern char *command_line_colors; +/*** declarations of public functions ************************************************************/ + void tty_init_colors (gboolean disable, gboolean force); void tty_colors_done (void); @@ -39,6 +50,5 @@ void tty_set_normal_attrs (void); void tty_color_set_defaults (const char *, const char *); -#define ALLOC_COLOR_PAIR_INDEX 1 - +/*** inline functions ****************************************************************************/ #endif /* MC_COLOR_H */ diff --git a/lib/tty/key.c b/lib/tty/key.c index c85b38dd0..b0274fca4 100644 --- a/lib/tty/key.c +++ b/lib/tty/key.c @@ -4,9 +4,9 @@ 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. Written by: 1994, 1995 Miguel de Icaza. - 1994, 1995 Janne Kukonlehto. - 1995 Jakub Jelinek. - 1997 Norbert Warmuth + 1994, 1995 Janne Kukonlehto. + 1995 Jakub Jelinek. + 1997 Norbert Warmuth This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -38,18 +38,18 @@ #include #include "lib/global.h" -#include "lib/strutil.h" /* str_casecmp */ +#include "lib/strutil.h" /* str_casecmp */ #include "lib/vfs/mc-vfs/vfs.h" #include "tty.h" -#include "tty-internal.h" /* mouse_enabled */ +#include "tty-internal.h" /* mouse_enabled */ #include "mouse.h" #include "key.h" -#include "win.h" /* xterm_flag */ +#include "win.h" /* xterm_flag */ #include "src/main.h" -#include "src/layout.h" /* winch_flag, mc_refresh() */ +#include "src/layout.h" /* winch_flag, mc_refresh() */ #include "src/consaver/cons.saver.h" @@ -59,9 +59,9 @@ #ifdef __linux__ #if defined(__GLIBC__) && (__GLIBC__ < 2) -# include /* TIOCLINUX */ +#include /* TIOCLINUX */ #else -# include +#include #endif #include #endif /* __linux__ */ @@ -77,7 +77,7 @@ #include #endif /* __QNXNTO__ */ -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\. and M-- and keypad + / - */ @@ -94,137 +94,138 @@ int use_8th_bit_as_meta = 0; */ const key_code_name_t key_name_conv_tab[] = { /* KEY_F(0) is not here, since we are mapping it to f10, so there is no reason - to define f0 as well. Also, it makes Learn keys a bunch of problems :( */ - { KEY_F (1), "f1", N_("Function key 1"), "F1" }, - { KEY_F (2), "f2", N_("Function key 2"), "F2" }, - { KEY_F (3), "f3", N_("Function key 3"), "F3" }, - { KEY_F (4), "f4", N_("Function key 4"), "F4" }, - { KEY_F (5), "f5", N_("Function key 5"), "F5" }, - { KEY_F (6), "f6", N_("Function key 6"), "F6" }, - { KEY_F (7), "f7", N_("Function key 7"), "F7" }, - { KEY_F (8), "f8", N_("Function key 8"), "F8" }, - { KEY_F (9), "f9", N_("Function key 9"), "F9" }, - { KEY_F (10), "f10", N_("Function key 10"), "F10" }, - { KEY_F (11), "f11", N_("Function key 11"), "F11" }, - { KEY_F (12), "f12", N_("Function key 12"), "F12" }, - { KEY_F (13), "f13", N_("Function key 13"), "F13" }, - { KEY_F (14), "f14", N_("Function key 14"), "F14" }, - { KEY_F (15), "f15", N_("Function key 15"), "F15" }, - { KEY_F (16), "f16", N_("Function key 16"), "F16" }, - { KEY_F (17), "f17", N_("Function key 17"), "F17" }, - { KEY_F (18), "f18", N_("Function key 18"), "F18" }, - { KEY_F (19), "f19", N_("Function key 19"), "F19" }, - { KEY_F (20), "f20", N_("Function key 20"), "F20" }, - { KEY_BACKSPACE, "backspace", N_("Backspace key"), "Backspace" }, - { KEY_END, "end", N_("End key"), "End" }, - { KEY_UP, "up", N_("Up arrow key"), "Up" }, - { KEY_DOWN, "down", N_("Down arrow key"), "Down" }, - { KEY_LEFT, "left", N_("Left arrow key"), "Left" }, - { KEY_RIGHT, "right", N_("Right arrow key"), "Right" }, - { KEY_HOME, "home", N_("Home key"), "Home" }, - { KEY_NPAGE, "pgdn", N_("Page Down key"), "PgDn" }, - { KEY_PPAGE, "pgup", N_("Page Up key"), "PgUp" }, - { KEY_IC, "insert", N_("Insert key"), "Ins" }, - { KEY_DC, "delete", N_("Delete key"), "Del" }, - { ALT ('\t'), "complete", N_("Completion/M-tab"), "Meta-Tab" }, - { KEY_KP_ADD, "kpplus", N_("+ on keypad"), "+" }, - { KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-" }, - { (int) '/', "kpslash", N_("Slash on keypad"), "/" }, - { KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*" }, + to define f0 as well. Also, it makes Learn keys a bunch of problems :( */ + {KEY_F (1), "f1", N_("Function key 1"), "F1"}, + {KEY_F (2), "f2", N_("Function key 2"), "F2"}, + {KEY_F (3), "f3", N_("Function key 3"), "F3"}, + {KEY_F (4), "f4", N_("Function key 4"), "F4"}, + {KEY_F (5), "f5", N_("Function key 5"), "F5"}, + {KEY_F (6), "f6", N_("Function key 6"), "F6"}, + {KEY_F (7), "f7", N_("Function key 7"), "F7"}, + {KEY_F (8), "f8", N_("Function key 8"), "F8"}, + {KEY_F (9), "f9", N_("Function key 9"), "F9"}, + {KEY_F (10), "f10", N_("Function key 10"), "F10"}, + {KEY_F (11), "f11", N_("Function key 11"), "F11"}, + {KEY_F (12), "f12", N_("Function key 12"), "F12"}, + {KEY_F (13), "f13", N_("Function key 13"), "F13"}, + {KEY_F (14), "f14", N_("Function key 14"), "F14"}, + {KEY_F (15), "f15", N_("Function key 15"), "F15"}, + {KEY_F (16), "f16", N_("Function key 16"), "F16"}, + {KEY_F (17), "f17", N_("Function key 17"), "F17"}, + {KEY_F (18), "f18", N_("Function key 18"), "F18"}, + {KEY_F (19), "f19", N_("Function key 19"), "F19"}, + {KEY_F (20), "f20", N_("Function key 20"), "F20"}, + {KEY_BACKSPACE, "backspace", N_("Backspace key"), "Backspace"}, + {KEY_END, "end", N_("End key"), "End"}, + {KEY_UP, "up", N_("Up arrow key"), "Up"}, + {KEY_DOWN, "down", N_("Down arrow key"), "Down"}, + {KEY_LEFT, "left", N_("Left arrow key"), "Left"}, + {KEY_RIGHT, "right", N_("Right arrow key"), "Right"}, + {KEY_HOME, "home", N_("Home key"), "Home"}, + {KEY_NPAGE, "pgdn", N_("Page Down key"), "PgDn"}, + {KEY_PPAGE, "pgup", N_("Page Up key"), "PgUp"}, + {KEY_IC, "insert", N_("Insert key"), "Ins"}, + {KEY_DC, "delete", N_("Delete key"), "Del"}, + {ALT ('\t'), "complete", N_("Completion/M-tab"), "Meta-Tab"}, + {KEY_KP_ADD, "kpplus", N_("+ on keypad"), "+"}, + {KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-"}, + {(int) '/', "kpslash", N_("Slash on keypad"), "/"}, + {KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*"}, /* From here on, these won't be shown in Learn keys (no space) */ - { ESC_CHAR, "escape", N_("Escape key"), "Esc" }, - { KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left" }, - { KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right" }, - { KEY_UP, "kpup", N_("Up arrow keypad"), "Up" }, - { KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down" }, - { KEY_HOME, "kphome", N_("Home on keypad"), "Home" }, - { KEY_END, "kpend", N_("End on keypad"), "End" }, - { KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn" }, - { KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp" }, - { KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins" }, - { KEY_DC, "kpdelete", N_("Delete on keypad"), "Del" }, - { (int) '\n', "kpenter", N_("Enter on keypad"), "Enter" }, - { KEY_F (21), "f21", N_("Function key 21"), "F21" }, - { KEY_F (22), "f22", N_("Function key 22"), "F22" }, - { KEY_F (23), "f23", N_("Function key 23"), "F23" }, - { KEY_F (24), "f24", N_("Function key 24"), "F24" }, - { KEY_A1, "a1", N_("A1 key"), "A1" }, - { KEY_C1, "c1", N_("C1 key"), "C1" }, + {ESC_CHAR, "escape", N_("Escape key"), "Esc"}, + {KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left"}, + {KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right"}, + {KEY_UP, "kpup", N_("Up arrow keypad"), "Up"}, + {KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down"}, + {KEY_HOME, "kphome", N_("Home on keypad"), "Home"}, + {KEY_END, "kpend", N_("End on keypad"), "End"}, + {KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn"}, + {KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp"}, + {KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins"}, + {KEY_DC, "kpdelete", N_("Delete on keypad"), "Del"}, + {(int) '\n', "kpenter", N_("Enter on keypad"), "Enter"}, + {KEY_F (21), "f21", N_("Function key 21"), "F21"}, + {KEY_F (22), "f22", N_("Function key 22"), "F22"}, + {KEY_F (23), "f23", N_("Function key 23"), "F23"}, + {KEY_F (24), "f24", N_("Function key 24"), "F24"}, + {KEY_A1, "a1", N_("A1 key"), "A1"}, + {KEY_C1, "c1", N_("C1 key"), "C1"}, /* Alternative label */ - { ESC_CHAR, "esc", N_("Escape key"), "Esc" }, - { KEY_BACKSPACE, "bs", N_("Backspace key"), "Bakspace" }, - { KEY_IC, "ins", N_("Insert key"), "Ins" }, - { KEY_DC, "del", N_("Delete key"), "Del" }, - { (int) '+', "plus", N_("Plus"), "+" }, - { (int) '-', "minus", N_("Minus"), "-" }, - { (int) '*', "asterisk", N_("Asterisk"), "*" }, - { (int) '.', "dot", N_("Dot"), "." }, - { (int) '<', "lt", N_("Less than"), "<" }, - { (int) '>', "gt", N_("Great than"), ">" }, - { (int) '=', "equal", N_("Equal"), "=" }, - { (int) ',', "comma", N_("Comma"), "," }, - { (int) '\'', "apostrophe", N_("Apostrophe"), "\'" }, - { (int) ':', "colon", N_("Colon"), ":" }, - { (int) '!', "exclamation", N_("Exclamation mark"), "!" }, - { (int) '?', "question", N_("Question mark"), "?" }, - { (int) '&', "ampersand", N_("Ampersand"), "&" }, - { (int) '$', "dollar", N_("Dollar sign"), "$" }, - { (int) '"', "quota", N_("Quotation mark"), "\"" }, - { (int) '^', "caret", N_("Caret"), "^" }, - { (int) '~', "tilda", N_("Tilda"), "~" }, - { (int) '`', "prime", N_("Prime"), "`" }, - { (int) '_', "underline", N_("Underline"), "_" }, - { (int) '_', "understrike", N_("Understrike"), "_" }, - { (int) '|', "pipe", N_("Pipe"), "|" }, - { (int) '(', "lparenthesis", N_("Left parenthesis"), "(" }, - { (int) ')', "rparenthesis", N_("Right parenthesis"), ")" }, - { (int) '[', "lbracket", N_("Left bracket"), "[" }, - { (int) ']', "rbracket", N_("Right bracket"), "]" }, - { (int) '{', "lbrace", N_("Left brace"), "{" }, - { (int) '}', "rbrace", N_("Right brace"), "}" }, - { (int) '\n', "enter", N_("Enter"), "Enter" }, - { (int) '\t', "tab", N_("Tab key"), "Tab" }, - { (int) ' ', "space", N_("Space key"), "Space" }, - { (int) '/', "slash", N_("Slash key"), "/" }, - { (int) '\\', "backslash", N_("Backslash key"), "\\" }, - { (int) '#', "number", N_("Number sign #"), "#" }, - { (int) '#', "hash", N_("Number sign #"), "#" }, + {ESC_CHAR, "esc", N_("Escape key"), "Esc"}, + {KEY_BACKSPACE, "bs", N_("Backspace key"), "Bakspace"}, + {KEY_IC, "ins", N_("Insert key"), "Ins"}, + {KEY_DC, "del", N_("Delete key"), "Del"}, + {(int) '+', "plus", N_("Plus"), "+"}, + {(int) '-', "minus", N_("Minus"), "-"}, + {(int) '*', "asterisk", N_("Asterisk"), "*"}, + {(int) '.', "dot", N_("Dot"), "."}, + {(int) '<', "lt", N_("Less than"), "<"}, + {(int) '>', "gt", N_("Great than"), ">"}, + {(int) '=', "equal", N_("Equal"), "="}, + {(int) ',', "comma", N_("Comma"), ","}, + {(int) '\'', "apostrophe", N_("Apostrophe"), "\'"}, + {(int) ':', "colon", N_("Colon"), ":"}, + {(int) '!', "exclamation", N_("Exclamation mark"), "!"}, + {(int) '?', "question", N_("Question mark"), "?"}, + {(int) '&', "ampersand", N_("Ampersand"), "&"}, + {(int) '$', "dollar", N_("Dollar sign"), "$"}, + {(int) '"', "quota", N_("Quotation mark"), "\""}, + {(int) '^', "caret", N_("Caret"), "^"}, + {(int) '~', "tilda", N_("Tilda"), "~"}, + {(int) '`', "prime", N_("Prime"), "`"}, + {(int) '_', "underline", N_("Underline"), "_"}, + {(int) '_', "understrike", N_("Understrike"), "_"}, + {(int) '|', "pipe", N_("Pipe"), "|"}, + {(int) '(', "lparenthesis", N_("Left parenthesis"), "("}, + {(int) ')', "rparenthesis", N_("Right parenthesis"), ")"}, + {(int) '[', "lbracket", N_("Left bracket"), "["}, + {(int) ']', "rbracket", N_("Right bracket"), "]"}, + {(int) '{', "lbrace", N_("Left brace"), "{"}, + {(int) '}', "rbrace", N_("Right brace"), "}"}, + {(int) '\n', "enter", N_("Enter"), "Enter"}, + {(int) '\t', "tab", N_("Tab key"), "Tab"}, + {(int) ' ', "space", N_("Space key"), "Space"}, + {(int) '/', "slash", N_("Slash key"), "/"}, + {(int) '\\', "backslash", N_("Backslash key"), "\\"}, + {(int) '#', "number", N_("Number sign #"), "#"}, + {(int) '#', "hash", N_("Number sign #"), "#"}, /* TRANSLATORS: Please translate as in "at sign" (@). */ - { (int) '@', "at", N_("At sign"), "@" }, + {(int) '@', "at", N_("At sign"), "@"}, /* meta keys */ - { KEY_M_CTRL, "control", N_("Ctrl"), "C" }, - { KEY_M_CTRL, "ctrl", N_("Ctrl"), "C" }, - { KEY_M_ALT, "meta", N_("Alt"), "M" }, - { KEY_M_ALT, "alt", N_("Alt"), "M" }, - { KEY_M_ALT, "ralt", N_("Alt"), "M" }, - { KEY_M_SHIFT, "shift", N_("Shift"), "S" }, + {KEY_M_CTRL, "control", N_("Ctrl"), "C"}, + {KEY_M_CTRL, "ctrl", N_("Ctrl"), "C"}, + {KEY_M_ALT, "meta", N_("Alt"), "M"}, + {KEY_M_ALT, "alt", N_("Alt"), "M"}, + {KEY_M_ALT, "ralt", N_("Alt"), "M"}, + {KEY_M_SHIFT, "shift", N_("Shift"), "S"}, - { 0, NULL, NULL, NULL } + {0, NULL, NULL, NULL} }; +/*** file scope macro definitions ****************************************************************/ -/*** file scope macro definitions **************************************/ - -#define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *) NULL)) -#define DIF_TIME(t1, t2) ((t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000) +#define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *) NULL)) +#define DIF_TIME(t1, t2) ((t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000) /* The maximum sequence length (32 + null terminator) */ #define SEQ_BUFFER_LEN 33 -/*** file scope type declarations **************************************/ +/*** file scope type declarations ****************************************************************/ /* Linux console keyboard modifiers */ -typedef enum { +typedef enum +{ SHIFT_PRESSED = (1 << 0), ALTR_PRESSED = (1 << 1), CONTROL_PRESSED = (1 << 2), ALTL_PRESSED = (1 << 3) } mod_pressed_t; -typedef struct key_def { +typedef struct key_def +{ char ch; /* Holds the matching char code */ int code; /* The code returned, valid if child == NULL */ struct key_def *next; @@ -234,14 +235,16 @@ typedef struct key_def { Escape */ } key_def; -typedef struct { +typedef struct +{ int code; const char *seq; int action; } key_define_t; /* File descriptor monitoring add/remove routines */ -typedef struct SelectList { +typedef struct SelectList +{ int fd; select_fn callback; void *info; @@ -254,7 +257,7 @@ typedef int (*ph_ov_f) (void *); typedef int (*ph_pqc_f) (unsigned short, PhCursorInfo_t *); #endif -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ static key_define_t mc_default_keys[] = { {ESC_CHAR, ESC_STR, MCKEY_ESCAPE}, @@ -495,7 +498,7 @@ static key_define_t qansi_key_defines[] = { }; /* timeout for old_esc_mode in usec */ -int old_esc_mode_timeout = 1000000; /* settable via env */ +int old_esc_mode_timeout = 1000000; /* settable via env */ /* This holds all the key definitions */ static key_def *keys = NULL; @@ -521,17 +524,26 @@ static Display *x11_display; static Window x11_window; #endif /* HAVE_TEXTMODE_X11_SUPPORT */ -/*** file scope functions **********************************************/ +static const size_t key_name_conv_tab_size = sizeof (key_name_conv_tab) / + sizeof (key_name_conv_tab[0]) - 1; + +static const key_code_name_t *key_name_conv_tab_sorted[sizeof (key_name_conv_tab) / + sizeof (key_name_conv_tab[0]) - 1]; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static int add_selects (fd_set * select_set) { int top_fd = 0; - if (disabled_channels == 0) { + if (disabled_channels == 0) + { SelectList *p; - for (p = select_list; p != NULL; p = p->next) { + for (p = select_list; p != NULL; p = p->next) + { FD_SET (p->fd, select_set); if (p->fd > top_fd) top_fd = p->fd; @@ -541,28 +553,36 @@ add_selects (fd_set * select_set) return top_fd; } +/* --------------------------------------------------------------------------------------------- */ + static void check_selects (fd_set * select_set) { - if (disabled_channels == 0) { + if (disabled_channels == 0) + { gboolean retry; - do { + do + { SelectList *p; retry = FALSE; for (p = select_list; p; p = p->next) - if (FD_ISSET (p->fd, select_set)) { + if (FD_ISSET (p->fd, select_set)) + { FD_CLR (p->fd, select_set); (*p->callback) (p->fd, p->info); retry = TRUE; break; } - } while (retry); + } + while (retry); } } +/* --------------------------------------------------------------------------------------------- */ /* If set timeout is set, then we wait 0.1 seconds, else, we block */ + static void try_channels (int set_timeout) { @@ -572,20 +592,23 @@ try_channels (int set_timeout) int v; int maxfdp; - while (1) { + while (1) + { FD_ZERO (&select_set); FD_SET (input_fd, &select_set); /* Add stdin */ maxfdp = max (add_selects (&select_set), input_fd); timeptr = NULL; - if (set_timeout) { + if (set_timeout) + { time_out.tv_sec = 0; time_out.tv_usec = 100000; timeptr = &time_out; } v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr); - if (v > 0) { + if (v > 0) + { check_selects (&select_set); if (FD_ISSET (input_fd, &select_set)) break; @@ -593,12 +616,15 @@ try_channels (int set_timeout) } } +/* --------------------------------------------------------------------------------------------- */ + static key_def * create_sequence (const char *seq, int code, int action) { key_def *base, *p, *attach; - for (base = attach = NULL; *seq; seq++) { + for (base = attach = NULL; *seq; seq++) + { p = g_new (key_def, 1); if (base == NULL) base = p; @@ -617,6 +643,8 @@ create_sequence (const char *seq, int code, int action) return base; } +/* --------------------------------------------------------------------------------------------- */ + static void define_sequences (const key_define_t * kd) { @@ -626,11 +654,14 @@ define_sequences (const key_define_t * kd) define_sequence (kd[i].code, kd[i].seq, kd[i].action); } +/* --------------------------------------------------------------------------------------------- */ + static void init_key_x11 (void) { #ifdef HAVE_TEXTMODE_X11_SUPPORT - if (getenv ("DISPLAY") != NULL) { + if (getenv ("DISPLAY") != NULL) + { x11_display = mc_XOpenDisplay (0); if (x11_display != NULL) @@ -639,7 +670,9 @@ init_key_x11 (void) #endif /* HAVE_TEXTMODE_X11_SUPPORT */ } +/* --------------------------------------------------------------------------------------------- */ /* Workaround for System V Curses vt100 bug */ + static int getch_with_delay (void) { @@ -647,7 +680,8 @@ getch_with_delay (void) /* This routine could be used on systems without mouse support, so we need to do the select check :-( */ - while (1) { + while (1) + { if (pending_keys == NULL) try_channels (0); @@ -662,6 +696,8 @@ getch_with_delay (void) return c; } +/* --------------------------------------------------------------------------------------------- */ + static void xmouse_get_event (Gpm_Event * ev) { @@ -680,41 +716,55 @@ xmouse_get_event (Gpm_Event * ev) /* There seems to be no way of knowing which button was released */ /* So we assume all the buttons were released */ - if (btn == 3) { - if (last_btn != 0) { - if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0) { + if (btn == 3) + { + if (last_btn != 0) + { + if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0) + { /* FIXME: DIRTY HACK */ /* don't generate GPM_UP after mouse wheel */ /* need for menu event handling */ ev->type = 0; tv1.tv_sec = 0; tv1.tv_usec = 0; - } else { + } + else + { ev->type = GPM_UP | (GPM_SINGLE << clicks); GET_TIME (tv1); } ev->buttons = 0; last_btn = 0; clicks = 0; - } else { + } + else + { /* Bogus event, maybe mouse wheel */ ev->type = 0; } - } else { - if (btn >= 32 && btn <= 34) { + } + else + { + if (btn >= 32 && btn <= 34) + { btn -= 32; ev->type = GPM_DRAG; - } else + } + else ev->type = GPM_DOWN; GET_TIME (tv2); - if (tv1.tv_sec && (DIF_TIME (tv1, tv2) < double_click_speed)) { + if (tv1.tv_sec && (DIF_TIME (tv1, tv2) < double_click_speed)) + { clicks++; clicks %= 3; - } else + } + else clicks = 0; - switch (btn) { + switch (btn) + { case 0: ev->buttons = GPM_B_LEFT; break; @@ -746,13 +796,15 @@ xmouse_get_event (Gpm_Event * ev) ev->y = tty_lowlevel_getch () - 32; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Get modifier state (shift, alt, ctrl) for the last key pressed. * We are assuming that the state didn't change since the key press. * This is only correct if get_modifier() is called very fast after * the input was received, so that the user didn't release the * modifier keys yet. */ + static int get_modifier (void) { @@ -765,7 +817,8 @@ get_modifier (void) #endif /* __QNXNTO__ */ #ifdef HAVE_TEXTMODE_X11_SUPPORT - if (x11_window != 0) { + if (x11_window != 0) + { Window root, child; int root_x, root_y; int win_x, win_y; @@ -783,20 +836,24 @@ get_modifier (void) #endif /* HAVE_TEXTMODE_X11_SUPPORT */ #ifdef __QNXNTO__ - if (in_photon == 0) { + if (in_photon == 0) + { /* First time here, let's load Photon library and attach to Photon */ in_photon = -1; - if (getenv ("PHOTON2_PATH") != NULL) { + if (getenv ("PHOTON2_PATH") != NULL) + { /* QNX 6.x has no support for RTLD_LAZY */ void *ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW); - if (ph_handle != NULL) { + if (ph_handle != NULL) + { ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach"); ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup"); ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor"); - if ((ph_attach != NULL) && (ph_input_group != NULL) - && (ph_query_cursor != NULL)) { - if ((*ph_attach) (0, 0)) { /* Attached */ + if ((ph_attach != NULL) && (ph_input_group != NULL) && (ph_query_cursor != NULL)) + { + if ((*ph_attach) (0, 0)) + { /* Attached */ ph_ig = (*ph_input_group) (0); in_photon = 1; } @@ -806,7 +863,8 @@ get_modifier (void) } /* We do not have Photon running. Assume we are in text console or xterm */ - if (in_photon == -1) { + if (in_photon == -1) + { if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (int), NULL) == -1) return 0; shift_ext_status = mod_status & 0xffffff00UL; @@ -815,10 +873,11 @@ get_modifier (void) result |= KEY_M_ALT; if (mod_status & _LINESTATUS_CON_CTRL) result |= KEY_M_CTRL; - if ((mod_status & _LINESTATUS_CON_SHIFT) - || (shift_ext_status & 0x00000800UL)) + if ((mod_status & _LINESTATUS_CON_SHIFT) || (shift_ext_status & 0x00000800UL)) result |= KEY_M_SHIFT; - } else { + } + else + { (*ph_query_cursor) (ph_ig, &cursor_info); if (cursor_info.key_mods & 0x04) result |= KEY_M_ALT; @@ -848,6 +907,8 @@ get_modifier (void) return result; } +/* --------------------------------------------------------------------------------------------- */ + static gboolean push_char (int c) { @@ -856,7 +917,8 @@ push_char (int c) if (seq_append == NULL) seq_append = seq_buffer; - if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2])) { + if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2])) + { *(seq_append++) = c; *seq_append = 0; ret = TRUE; @@ -865,7 +927,9 @@ push_char (int c) return ret; } +/* --------------------------------------------------------------------------------------------- */ /* Apply corrections for the keycode generated in get_key_code() */ + static int correct_key_code (int code) { @@ -880,7 +944,8 @@ correct_key_code (int code) * Add key modifiers directly from X11 or OS. * Ordinary characters only get modifiers from sequences. */ - if (c < 32 || c >= 256) { + if (c < 32 || c >= 256) + { mod |= get_modifier (); } @@ -893,7 +958,8 @@ correct_key_code (int code) c = '\t'; /* Convert Shift+Tab and Ctrl+Tab to Back Tab */ - if ((c == '\t') && (mod & (KEY_M_SHIFT | KEY_M_CTRL))) { + if ((c == '\t') && (mod & (KEY_M_SHIFT | KEY_M_CTRL))) + { c = KEY_BTAB; mod = 0; } @@ -908,26 +974,32 @@ correct_key_code (int code) * XCTRL macro should be used. In some cases, we are interested, * e.g. to distinguish Ctrl-Enter from Enter. */ - if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n') { + if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n') + { mod |= KEY_M_CTRL; } #ifdef __QNXNTO__ qmod = get_modifier (); - if ((c == 127) && (mod == 0)) { /* Add Ctrl/Alt/Shift-BackSpace */ + if ((c == 127) && (mod == 0)) + { /* Add Ctrl/Alt/Shift-BackSpace */ mod |= get_modifier (); c = KEY_BACKSPACE; } - if ((c == '0') && (mod == 0)) { /* Add Shift-Insert on key pad */ - if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) { + if ((c == '0') && (mod == 0)) + { /* Add Shift-Insert on key pad */ + if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) + { mod = KEY_M_SHIFT; c = KEY_IC; } } - if ((c == '.') && (mod == 0)) { /* Add Shift-Del on key pad */ - if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) { + if ((c == '.') && (mod == 0)) + { /* Add Shift-Del on key pad */ + if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) + { mod = KEY_M_SHIFT; c = KEY_DC; } @@ -935,39 +1007,46 @@ correct_key_code (int code) #endif /* __QNXNTO__ */ /* Unrecognized 0177 is delete (preserve Ctrl) */ - if (c == 0177) { + if (c == 0177) + { c = KEY_BACKSPACE; } /* Unrecognized Ctrl-d is delete */ - if (c == (31 & 'd')) { + if (c == (31 & 'd')) + { c = KEY_DC; mod &= ~KEY_M_CTRL; } /* Unrecognized Ctrl-h is backspace */ - if (c == (31 & 'h')) { + if (c == (31 & 'h')) + { c = KEY_BACKSPACE; mod &= ~KEY_M_CTRL; } /* Shift+BackSpace is backspace */ - if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT)) { + if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT)) + { mod &= ~KEY_M_SHIFT; } /* Convert Shift+Fn to F(n+10) */ - if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT)) { + if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT)) + { c += 10; } /* Remove Shift information from function keys */ - if (c >= KEY_F (1) && c <= KEY_F (20)) { + if (c >= KEY_F (1) && c <= KEY_F (20)) + { mod &= ~KEY_M_SHIFT; } if (!alternate_plus_minus) - switch (c) { + switch (c) + { case KEY_KP_ADD: c = '+'; break; @@ -982,6 +1061,8 @@ correct_key_code (int code) return (mod | c); } +/* --------------------------------------------------------------------------------------------- */ + static int xgetch_second (void) { @@ -1000,47 +1081,128 @@ xgetch_second (void) return c; } +/* --------------------------------------------------------------------------------------------- */ + static void learn_store_key (char *buffer, char **p, int c) { if (*p - buffer > 253) return; - if (c == ESC_CHAR) { + if (c == ESC_CHAR) + { *(*p)++ = '\\'; *(*p)++ = 'e'; - } else if (c < ' ') { + } + else if (c < ' ') + { *(*p)++ = '^'; *(*p)++ = c + 'a' - 1; - } else if (c == '^') { + } + else if (c == '^') + { *(*p)++ = '^'; *(*p)++ = '^'; - } else + } + else *(*p)++ = (char) c; } +/* --------------------------------------------------------------------------------------------- */ + static void k_dispose (key_def * k) { - if (k != NULL) { + if (k != NULL) + { k_dispose (k->child); k_dispose (k->next); g_free (k); } } +/* --------------------------------------------------------------------------------------------- */ + static void s_dispose (SelectList * sel) { - if (sel != NULL) { + if (sel != NULL) + { s_dispose (sel->next); g_free (sel); } } -/*** public functions **************************************************/ +/* --------------------------------------------------------------------------------------------- */ +static int +key_code_name_comparator (const void *p1, const void *p2) +{ + const key_code_name_t *n1 = *(const key_code_name_t **) p1; + const key_code_name_t *n2 = *(const key_code_name_t **) p2; + + return str_casecmp (n1->name, n2->name); +} + +/* --------------------------------------------------------------------------------------------- */ + +static inline void +sort_key_name_conv_tab (void) +{ + static gboolean has_been_sorted = FALSE; + + if (!has_been_sorted) + { + size_t i; + for (i = 0; i < key_name_conv_tab_size; i++) + key_name_conv_tab_sorted[i] = &key_name_conv_tab[i]; + + qsort (key_name_conv_tab_sorted, + key_name_conv_tab_size, sizeof (key_name_conv_tab_sorted[0]), + &key_code_name_comparator); + has_been_sorted = TRUE; + } +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +lookup_keyname (const char *name, int *idx) +{ + if (name[0] != '\0') + { + const key_code_name_t key = { 0, name, NULL, NULL }; + const key_code_name_t *keyp = &key; + key_code_name_t **res; + + if (name[1] == '\0') + { + *idx = -1; + return (int) name[0]; + } + + sort_key_name_conv_tab (); + + res = bsearch (&keyp, key_name_conv_tab_sorted, + key_name_conv_tab_size, + sizeof (key_name_conv_tab_sorted[0]), key_code_name_comparator); + + if (res != NULL) + { + *idx = (int) (res - (key_code_name_t **) key_name_conv_tab_sorted); + return (*res)->code; + } + } + + *idx = -1; + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /* This has to be called before init_slang or whatever routine calls any define_sequence */ + void init_key (void) { @@ -1062,7 +1224,8 @@ init_key (void) load_xtra_key_defines (); #ifdef __QNX__ - if ((term != NULL) && (strncmp (term, "qnx", 3) == 0)) { + if ((term != NULL) && (strncmp (term, "qnx", 3) == 0)) + { /* Modify the default value of use_8th_bit_as_meta: we would * like to provide a working mc for a newbie who knows nothing * about [Options|Display bits|Full 8 bits input]... @@ -1088,7 +1251,11 @@ init_key (void) define_sequences (qansi_key_defines); } -/* This has to be called after SLang_init_tty/slint_init */ +/* --------------------------------------------------------------------------------------------- */ +/** + * This has to be called after SLang_init_tty/slint_init + */ + void init_key_input_fd (void) { @@ -1097,6 +1264,8 @@ init_key_input_fd (void) #endif } +/* --------------------------------------------------------------------------------------------- */ + void done_key (void) { @@ -1109,6 +1278,8 @@ done_key (void) #endif } +/* --------------------------------------------------------------------------------------------- */ + void add_select_channel (int fd, select_fn callback, void *info) { @@ -1122,6 +1293,8 @@ add_select_channel (int fd, select_fn callback, void *info) select_list = new; } +/* --------------------------------------------------------------------------------------------- */ + void delete_select_channel (int fd) { @@ -1130,7 +1303,8 @@ delete_select_channel (int fd) SelectList *p_next; while (p != NULL) - if (p->fd == fd) { + if (p->fd == fd) + { p_next = p->next; if (p_prev != NULL) @@ -1140,12 +1314,16 @@ delete_select_channel (int fd) g_free (p); p = p_next; - } else { + } + else + { p_prev = p; p = p->next; } } +/* --------------------------------------------------------------------------------------------- */ + void channels_up (void) { @@ -1154,74 +1332,19 @@ channels_up (void) disabled_channels--; } +/* --------------------------------------------------------------------------------------------- */ + void channels_down (void) { disabled_channels++; } -static const size_t key_name_conv_tab_size = sizeof (key_name_conv_tab) / - sizeof (key_name_conv_tab[0]) - 1; -static const key_code_name_t *key_name_conv_tab_sorted[sizeof (key_name_conv_tab) / - sizeof (key_name_conv_tab[0]) - 1]; +/* --------------------------------------------------------------------------------------------- */ +/** + * Return the code associated with the symbolic name keyname + */ -static int -key_code_name_comparator (const void *p1, const void *p2) -{ - const key_code_name_t *n1 = *(const key_code_name_t **) p1; - const key_code_name_t *n2 = *(const key_code_name_t **) p2; - - return str_casecmp (n1->name, n2->name); -} - -static inline void -sort_key_name_conv_tab (void) -{ - static gboolean has_been_sorted = FALSE; - - if (!has_been_sorted) { - size_t i; - for (i = 0; i < key_name_conv_tab_size; i++) - key_name_conv_tab_sorted[i] = &key_name_conv_tab[i]; - - qsort (key_name_conv_tab_sorted, - key_name_conv_tab_size, sizeof (key_name_conv_tab_sorted[0]), - &key_code_name_comparator); - has_been_sorted = TRUE; - } -} - -static int -lookup_keyname (const char *name, int *idx) -{ - if (name[0] != '\0') { - const key_code_name_t key = { 0, name, NULL, NULL }; - const key_code_name_t *keyp = &key; - key_code_name_t **res; - - if (name[1] == '\0') { - *idx = -1; - return (int) name[0]; - } - - sort_key_name_conv_tab (); - - res = bsearch (&keyp, key_name_conv_tab_sorted, - key_name_conv_tab_size, - sizeof (key_name_conv_tab_sorted[0]), - key_code_name_comparator); - - if (res != NULL) { - *idx = (int) (res - (key_code_name_t **) key_name_conv_tab_sorted); - return (*res)->code; - } - } - - *idx = -1; - return 0; -} - -/* Return the code associated with the symbolic name keyname */ long lookup_key (const char *name, char **label) { @@ -1235,84 +1358,97 @@ lookup_key (const char *name, char **label) int use_shift = -1; if (name == NULL) - return 0; + return 0; name = g_strstrip (g_strdup (name)); p = lc_keys = g_strsplit_set (name, "-+ ", -1); g_free ((char *) name); - while ((p != NULL) && (*p != NULL)) { - if ((*p)[0] != '\0') { - int idx; + while ((p != NULL) && (*p != NULL)) + { + if ((*p)[0] != '\0') + { + int idx; - key = lookup_keyname (g_strstrip (*p), &idx); + key = lookup_keyname (g_strstrip (*p), &idx); - if (key == KEY_M_ALT) - use_meta = idx; - else if (key == KEY_M_CTRL) - use_ctrl = idx; - else if (key == KEY_M_SHIFT) - use_shift = idx; - else { - k = key; - lc_index = idx; - break; - } - } + if (key == KEY_M_ALT) + use_meta = idx; + else if (key == KEY_M_CTRL) + use_ctrl = idx; + else if (key == KEY_M_SHIFT) + use_shift = idx; + else + { + k = key; + lc_index = idx; + break; + } + } - p++; + p++; } g_strfreev (lc_keys); /* output */ if (k <= 0) - return 0; + return 0; - if (label != NULL) { - GString *s; + if (label != NULL) + { + GString *s; - s = g_string_new (""); + s = g_string_new (""); - if (use_meta != -1) { - g_string_append (s, key_name_conv_tab_sorted[use_meta]->shortcut); - g_string_append_c (s, '-'); - } - if (use_ctrl != -1) { - g_string_append (s, key_name_conv_tab_sorted[use_ctrl]->shortcut); - g_string_append_c (s, '-'); - } - if (use_shift != -1) { - if (k < 127) - g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k)); - else { - g_string_append (s, key_name_conv_tab_sorted[use_shift]->shortcut); - g_string_append_c (s, '-'); - g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); - } - } else if (k < 128) { - if ((k >= 'A') || (lc_index < 0) - || (key_name_conv_tab_sorted[lc_index]->shortcut == NULL)) - g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k)); - else - g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); - } else if ((lc_index != -1) && (key_name_conv_tab_sorted[lc_index]->shortcut != NULL)) - g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); - else - g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key)); + if (use_meta != -1) + { + g_string_append (s, key_name_conv_tab_sorted[use_meta]->shortcut); + g_string_append_c (s, '-'); + } + if (use_ctrl != -1) + { + g_string_append (s, key_name_conv_tab_sorted[use_ctrl]->shortcut); + g_string_append_c (s, '-'); + } + if (use_shift != -1) + { + if (k < 127) + g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k)); + else + { + g_string_append (s, key_name_conv_tab_sorted[use_shift]->shortcut); + g_string_append_c (s, '-'); + g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); + } + } + else if (k < 128) + { + if ((k >= 'A') || (lc_index < 0) + || (key_name_conv_tab_sorted[lc_index]->shortcut == NULL)) + g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k)); + else + g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); + } + else if ((lc_index != -1) && (key_name_conv_tab_sorted[lc_index]->shortcut != NULL)) + g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut); + else + g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key)); - *label = g_string_free (s, FALSE); + *label = g_string_free (s, FALSE); } - if (use_shift != -1) { - if (k < 127 && k > 31 ) - k = g_ascii_toupper ((gchar) k); - else - k |= KEY_M_SHIFT; + if (use_shift != -1) + { + if (k < 127 && k > 31) + k = g_ascii_toupper ((gchar) k); + else + k |= KEY_M_SHIFT; } - if (use_ctrl != -1) { + if (use_ctrl != -1) + { if (k < 256) k = XCTRL (k); else @@ -1325,10 +1461,12 @@ lookup_key (const char *name, char **label) return (long) k; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Return TRUE on success, FALSE on error. * An error happens if SEQ is a beginning of an existing longer sequence. */ + gboolean define_sequence (int code, const char *seq, int action) { @@ -1338,11 +1476,14 @@ define_sequence (int code, const char *seq, int action) return FALSE; for (base = keys; (base != NULL) && (*seq != '\0');) - if (*seq == base->ch) { - if (base->child == 0) { + if (*seq == base->ch) + { + if (base->child == 0) + { if (*(seq + 1) != '\0') base->child = create_sequence (seq + 1, code, action); - else { + else + { /* The sequence matches an existing one. */ base->code = code; base->action = action; @@ -1352,16 +1493,20 @@ define_sequence (int code, const char *seq, int action) base = base->child; seq++; - } else { + } + else + { if (base->next) base = base->next; - else { + else + { base->next = create_sequence (seq, code, action); return TRUE; } } - if (*seq == '\0') { + if (*seq == '\0') + { /* Attempt to redefine a sequence with a shorter sequence. */ return FALSE; } @@ -1370,7 +1515,8 @@ define_sequence (int code, const char *seq, int action) return TRUE; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Check if we are idle, i.e. there are no pending keyboard or mouse * events. Return 1 is idle, 0 is there are pending events. */ @@ -1385,7 +1531,8 @@ is_idle (void) FD_SET (input_fd, &select_set); maxfdp = input_fd; #ifdef HAVE_LIBGPM - if (mouse_enabled && (use_mouse_p == MOUSE_GPM) && (gpm_fd > 0)) { + if (mouse_enabled && (use_mouse_p == MOUSE_GPM) && (gpm_fd > 0)) + { FD_SET (gpm_fd, &select_set); maxfdp = max (maxfdp, gpm_fd); } @@ -1395,6 +1542,8 @@ is_idle (void) return (select (maxfdp + 1, &select_set, 0, 0, &time_out) <= 0); } +/* --------------------------------------------------------------------------------------------- */ + int get_key_code (int no_delay) { @@ -1403,20 +1552,24 @@ get_key_code (int no_delay) static struct timeval esctime = { -1, -1 }; static int lastnodelay = -1; - if (no_delay != lastnodelay) { + if (no_delay != lastnodelay) + { this = NULL; lastnodelay = no_delay; } pend_send: - if (pending_keys != NULL) { + if (pending_keys != NULL) + { int d = *pending_keys++; check_pend: - if (*pending_keys == 0) { + if (*pending_keys == 0) + { pending_keys = NULL; seq_append = NULL; } - if ((d == ESC_CHAR) && (pending_keys != NULL)) { + if ((d == ESC_CHAR) && (pending_keys != NULL)) + { d = ALT (*pending_keys++); goto check_pend; } @@ -1435,10 +1588,13 @@ get_key_code (int no_delay) if (c == KEY_RESIZE) goto nodelay_try_again; #endif - if (no_delay) { + if (no_delay) + { tty_nodelay (FALSE); - if (c == -1) { - if (this != NULL && parent != NULL && parent->action == MCKEY_ESCAPE && old_esc_mode) { + if (c == -1) + { + if (this != NULL && parent != NULL && parent->action == MCKEY_ESCAPE && old_esc_mode) + { struct timeval current, time_out; if (esctime.tv_sec == -1) @@ -1446,7 +1602,8 @@ get_key_code (int no_delay) GET_TIME (current); time_out.tv_sec = old_esc_mode_timeout / 1000000 + esctime.tv_sec; time_out.tv_usec = old_esc_mode_timeout % 1000000 + esctime.tv_usec; - if (time_out.tv_usec > 1000000) { + if (time_out.tv_usec > 1000000) + { time_out.tv_usec -= 1000000; time_out.tv_sec++; } @@ -1460,11 +1617,14 @@ get_key_code (int no_delay) } return -1; } - } else if (c == -1) { + } + else if (c == -1) + { /* Maybe we got an incomplete match. This we do only in delay mode, since otherwise tty_lowlevel_getch can return -1 at any time. */ - if (seq_append != NULL) { + if (seq_append != NULL) + { pending_keys = seq_buffer; goto pend_send; } @@ -1473,11 +1633,13 @@ get_key_code (int no_delay) } /* Search the key on the root */ - if (!no_delay || this == NULL) { + if (!no_delay || this == NULL) + { this = keys; parent = NULL; - if ((c > 127 && c < 256) && use_8th_bit_as_meta) { + if ((c > 127 && c < 256) && use_8th_bit_as_meta) + { c &= 0x7f; /* The first sequence defined starts with esc */ @@ -1485,19 +1647,26 @@ get_key_code (int no_delay) this = keys->child; } } - while (this != NULL) { - if (c == this->ch) { - if (this->child) { - if (!push_char (c)) { + while (this != NULL) + { + if (c == this->ch) + { + if (this->child) + { + if (!push_char (c)) + { pending_keys = seq_buffer; goto pend_send; } parent = this; this = this->child; - if (parent->action == MCKEY_ESCAPE && old_esc_mode) { - if (no_delay) { + if (parent->action == MCKEY_ESCAPE && old_esc_mode) + { + if (no_delay) + { GET_TIME (esctime); - if (this == NULL) { + if (this == NULL) + { /* Shouldn't happen */ fputs ("Internal error\n", stderr); exit (EXIT_FAILURE); @@ -1506,17 +1675,22 @@ get_key_code (int no_delay) } esctime.tv_sec = -1; c = xgetch_second (); - if (c == -1) { + if (c == -1) + { pending_keys = seq_append = NULL; this = NULL; return ESC_CHAR; } - } else { + } + else + { if (no_delay) goto nodelay_try_again; c = tty_lowlevel_getch (); } - } else { + } + else + { /* We got a complete match, return and reset search */ int code; @@ -1525,11 +1699,15 @@ get_key_code (int no_delay) this = NULL; return correct_key_code (code); } - } else { + } + else + { if (this->next != NULL) this = this->next; - else { - if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) { + else + { + if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) + { /* Convert escape-digits to F-keys */ if (g_ascii_isdigit (c)) c = KEY_F (c - '0'); @@ -1555,10 +1733,12 @@ get_key_code (int no_delay) return correct_key_code (c); } +/* --------------------------------------------------------------------------------------------- */ /* Returns a character read from stdin with appropriate interpretation */ /* Also takes care of generated mouse events */ /* Returns EV_MOUSE if it is a mouse event */ /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */ + int tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) { @@ -1571,10 +1751,12 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) struct timeval *time_addr = NULL; static int dirty = 3; - if ((dirty == 3) || is_idle ()) { + if ((dirty == 3) || is_idle ()) + { mc_refresh (); dirty = 1; - } else + } + else dirty++; vfs_timeout_handler (); @@ -1583,14 +1765,16 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) a suitable position for the mouse, so we can't use show_mouse_pointer on it. */ - if (event->x > 0) { + if (event->x > 0) + { show_mouse_pointer (event->x, event->y); if (!redo_event) event->x = -1; } /* Repeat if using mouse */ - while (pending_keys == NULL) { + while (pending_keys == NULL) + { int maxfdp; fd_set select_set; @@ -1599,8 +1783,10 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) maxfdp = max (add_selects (&select_set), input_fd); #ifdef HAVE_LIBGPM - if (mouse_enabled && (use_mouse_p == MOUSE_GPM)) { - if (gpm_fd < 0) { + if (mouse_enabled && (use_mouse_p == MOUSE_GPM)) + { + if (gpm_fd < 0) + { /* Connection to gpm broken, possibly gpm has died */ mouse_enabled = FALSE; use_mouse_p = MOUSE_NONE; @@ -1612,18 +1798,22 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) } #endif - if (redo_event) { + if (redo_event) + { time_out.tv_usec = mou_auto_repeat * 1000; time_out.tv_sec = 0; time_addr = &time_out; - } else { + } + else + { int seconds; seconds = vfs_timeouts (); time_addr = NULL; - if (seconds != 0) { + if (seconds != 0) + { /* the timeout could be improved and actually be * the number of seconds until the next vfs entry * timeouts in the stamp list. @@ -1635,7 +1825,8 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) } } - if (!block || winch_flag) { + if (!block || winch_flag) + { time_addr = &time_out; time_out.tv_sec = 0; time_out.tv_usec = 0; @@ -1650,7 +1841,8 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) * !block -> we did not block in the select call * else -> 10 second timeout to check the vfs status. */ - if (flag == 0) { + if (flag == 0) + { if (redo_event) return EV_MOUSE; if (!block || winch_flag) @@ -1666,7 +1858,8 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) break; #ifdef HAVE_LIBGPM if (mouse_enabled && use_mouse_p == MOUSE_GPM - && gpm_fd > 0 && FD_ISSET (gpm_fd, &select_set)) { + && gpm_fd > 0 && FD_ISSET (gpm_fd, &select_set)) + { Gpm_GetEvent (&ev); Gpm_FitEvent (&ev); *event = ev; @@ -1690,7 +1883,8 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) #ifdef KEY_MOUSE || c == KEY_MOUSE #endif /* KEY_MOUSE */ - )) { + )) + { /* Mouse event */ xmouse_get_event (event); return (event->type != 0) ? EV_MOUSE : EV_NONE; @@ -1699,7 +1893,9 @@ tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block) return c; } +/* --------------------------------------------------------------------------------------------- */ /* Returns a key press, mouse events are discarded */ + int tty_getch (void) { @@ -1711,10 +1907,12 @@ tty_getch (void) return key; } +/* --------------------------------------------------------------------------------------------- */ + char * learn_key (void) { -/* LEARN_TIMEOUT in usec */ + /* LEARN_TIMEOUT in usec */ #define LEARN_TIMEOUT 200000 fd_set Read_FD_Set; @@ -1731,23 +1929,28 @@ learn_key (void) learn_store_key (buffer, &p, c); GET_TIME (endtime); endtime.tv_usec += LEARN_TIMEOUT; - if (endtime.tv_usec > 1000000) { + if (endtime.tv_usec > 1000000) + { endtime.tv_usec -= 1000000; endtime.tv_sec++; } tty_nodelay (TRUE); - for (;;) { - while ((c = tty_lowlevel_getch ()) == -1) { + for (;;) + { + while ((c = tty_lowlevel_getch ()) == -1) + { GET_TIME (time_out); time_out.tv_usec = endtime.tv_usec - time_out.tv_usec; if (time_out.tv_usec < 0) time_out.tv_sec++; time_out.tv_sec = endtime.tv_sec - time_out.tv_sec; - if (time_out.tv_sec >= 0 && time_out.tv_usec > 0) { + if (time_out.tv_sec >= 0 && time_out.tv_usec > 0) + { FD_ZERO (&Read_FD_Set); FD_SET (input_fd, &Read_FD_Set); select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out); - } else + } + else break; } if (c == -1) @@ -1761,23 +1964,31 @@ learn_key (void) #undef LEARN_TIMEOUT } +/* --------------------------------------------------------------------------------------------- */ /* xterm and linux console only: set keypad to numeric or application mode. Only in application keypad mode it's possible to distinguish - the '+' key and the '+' on the keypad ('*' and '-' ditto)*/ + the '+' key and the '+' on the keypad ('*' and '-' ditto) */ + void numeric_keypad_mode (void) { - if (console_flag || xterm_flag) { + if (console_flag || xterm_flag) + { fputs ("\033>", stdout); fflush (stdout); } } +/* --------------------------------------------------------------------------------------------- */ + void application_keypad_mode (void) { - if (console_flag || xterm_flag) { + if (console_flag || xterm_flag) + { fputs ("\033=", stdout); fflush (stdout); } } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/key.h b/lib/tty/key.h index 10b25b6e4..91b8290da 100644 --- a/lib/tty/key.h +++ b/lib/tty/key.h @@ -1,39 +1,14 @@ - /** \file key.h * \brief Header: keyboard support routines */ -#ifndef MC_KEY_H -#define MC_KEY_H +#ifndef MC__KEY_H +#define MC__KEY_H -#include "lib/global.h" /* */ +#include "lib/global.h" /* */ +#include "tty.h" /* KEY_F macro */ -#include "tty.h" /* KEY_F macro */ - -gboolean define_sequence (int code, const char *seq, int action); - -void init_key (void); -void init_key_input_fd (void); -void done_key (void); - -long lookup_key (const char *name, char **label); - -typedef struct { - int code; - const char *name; - const char *longname; - const char *shortcut; -} key_code_name_t; - -extern const key_code_name_t key_name_conv_tab[]; - -extern int old_esc_mode_timeout; - -/* mouse support */ -struct Gpm_Event; -int tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block); -gboolean is_idle (void); -int tty_getch (void); +/*** typedefs(not structures) and defined constants **********************************************/ /* Possible return values from tty_get_event: */ #define EV_MOUSE -2 @@ -48,12 +23,57 @@ int tty_getch (void); #define KEY_M_CTRL 0x4000 #define KEY_M_MASK 0x7000 +#define XCTRL(x) (KEY_M_CTRL | ((x) & 0x1F)) +#define ALT(x) (KEY_M_ALT | (unsigned int)(x)) + +/* To define sequences and return codes */ +#define MCKEY_NOACTION 0 +#define MCKEY_ESCAPE 1 + +/* Return code for the mouse sequence */ +#define MCKEY_MOUSE -2 + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +typedef struct +{ + int code; + const char *name; + const char *longname; + const char *shortcut; +} key_code_name_t; + +struct Gpm_Event; + +/*** global variables defined in .c file *********************************************************/ + +extern const key_code_name_t key_name_conv_tab[]; + +extern int old_esc_mode_timeout; + extern int alternate_plus_minus; extern int double_click_speed; extern int old_esc_mode; extern int use_8th_bit_as_meta; extern int mou_auto_repeat; +/*** declarations of public functions ************************************************************/ + +gboolean define_sequence (int code, const char *seq, int action); + +void init_key (void); +void init_key_input_fd (void); +void done_key (void); + +long lookup_key (const char *name, char **label); + +/* mouse support */ +int tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block); +gboolean is_idle (void); +int tty_getch (void); + /* While waiting for input, the program can select on more than one file */ typedef int (*select_fn) (int fd, void *info); @@ -66,23 +86,6 @@ void remove_select_channel (int fd); void channels_up (void); void channels_down (void); -#define XCTRL(x) (KEY_M_CTRL | ((x) & 0x1F)) -#define ALT(x) (KEY_M_ALT | (unsigned int)(x)) - -static inline gboolean -is_abort_char (int c) -{ - return ((c == ESC_CHAR) || (c == KEY_F (10))); -} - -/* To define sequences and return codes */ -#define MCKEY_NOACTION 0 -#define MCKEY_ESCAPE 1 - -/* Return code for the mouse sequence */ -#define MCKEY_MOUSE -2 - - /* internally used in key.c, defined in keyxtra.c */ void load_xtra_key_defines (void); @@ -96,4 +99,12 @@ int get_key_code (int nodelay); void numeric_keypad_mode (void); void application_keypad_mode (void); +/*** inline functions ****************************************************************************/ + +static inline gboolean +is_abort_char (int c) +{ + return ((c == ESC_CHAR) || (c == KEY_F (10))); +} + #endif /* MC_KEY_H */ diff --git a/lib/tty/keyxdef.c b/lib/tty/keyxdef.c index 832da7c72..94fa74c4a 100644 --- a/lib/tty/keyxdef.c +++ b/lib/tty/keyxdef.c @@ -5,12 +5,12 @@ Copyright (C) 1998, 2000, 2001, 2005, 2007 Free Software Foundation, Inc. Written by: 1998, Gyorgy Tamasi - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -44,7 +44,7 @@ #include "lib/global.h" #include "tty.h" -#include "mouse.h" /* required before key.h */ +#include "mouse.h" /* required before key.h */ #include "key.h" #if defined (__QNX__) && !defined (__QNXNTO__) @@ -91,6 +91,11 @@ * field name in the QNX terminfo strings struct */ +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + + #define Key_backspace __TK("kbs", "kb", 55, _ky_backspace ) #define Key_catab __TK("ktbc", "ka", 56, _ky_catab ) #define Key_clear __TK("kclr", "kC", 57, _ky_clear ) @@ -283,26 +288,43 @@ #define Key_alt_enter Key_ctl_enter /* map ALT-ENTER to CTRL-ENTER */ #ifdef __USE_QNX_TI - -/* OS/implementation specific key-define struct */ -typedef const struct qnx_key_define_s { - int mc_code; - int str_idx; -} qnx_key_define_t; - /* define current xtra_key_define_t (enable OS/implementation) */ #define xtra_key_define_t qnx_key_define_t - #endif /* __USE_QNX_TI */ - #endif /* HAVE_QNX_KEYS */ #ifdef xtra_key_define_t - #ifndef FORCE_BASE_KEY_DEFS #define FORCE_BASE_KEY_DEFS 0 #endif +#endif /* xtra_key_define_t */ + +#ifdef HAVE_QNX_KEYS +#ifdef __USE_QNX_TI +#define __CT (__cur_term) +#define __QTISOFFS(_qtisx) (((charoffset*)(&__CT->_strs))[_qtisx]) +#define __QTISSTR(_qtisx) (&__CT->_strtab[0]+__QTISOFFS(_qtisx)) +#endif /* __USE_QNX_TI */ +#endif /* HAVE_QNX_KEYS */ + +/*** file scope type declarations ****************************************************************/ + +#ifdef HAVE_QNX_KEYS +#ifdef __USE_QNX_TI +/* OS/implementation specific key-define struct */ +typedef const struct qnx_key_define_s +{ + int mc_code; + int str_idx; +} qnx_key_define_t; +#endif /* __USE_QNX_TI */ +#endif /* HAVE_QNX_KEYS */ + +/*** file scope variables ************************************************************************/ + + +#ifdef xtra_key_define_t /* general key define table */ xtra_key_define_t xtra_key_defines[] = { @@ -372,21 +394,22 @@ xtra_key_define_t xtra_key_defines[] = { #endif /* xtra_key_define_t */ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ #ifdef HAVE_QNX_KEYS - #ifdef __USE_QNX_TI - -#define __CT (__cur_term) -#define __QTISOFFS(_qtisx) (((charoffset*)(&__CT->_strs))[_qtisx]) -#define __QTISSTR(_qtisx) (&__CT->_strtab[0]+__QTISOFFS(_qtisx)) - void load_qnx_key_defines (void) { static int _qnx_keys_defined = 0; - if (!_qnx_keys_defined) { + if (!_qnx_keys_defined) + { int idx, str_idx; int term_setup_ok; @@ -394,10 +417,13 @@ load_qnx_key_defines (void) if (term_setup_ok != 1) return; - for (idx = 0; idx < sizeof (xtra_key_defines) / sizeof (xtra_key_defines[0]); idx++) { + for (idx = 0; idx < sizeof (xtra_key_defines) / sizeof (xtra_key_defines[0]); idx++) + { str_idx = xtra_key_defines[idx].str_idx; - if (__QTISOFFS (str_idx)) { - if (*__QTISSTR (str_idx)) { + if (__QTISOFFS (str_idx)) + { + if (*__QTISSTR (str_idx)) + { define_sequence (xtra_key_defines[idx].mc_code, __QTISSTR (str_idx), MCKEY_NOACTION); } @@ -406,13 +432,12 @@ load_qnx_key_defines (void) _qnx_keys_defined = 1; } } - #endif /* __USE_QNX_TI */ - #endif /* HAVE_QNX_KEYS */ - +/* --------------------------------------------------------------------------------------------- */ /* called from key.c/init_key() */ + void load_xtra_key_defines (void) { @@ -420,3 +445,5 @@ load_xtra_key_defines (void) load_qnx_key_defines (); #endif } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/mouse.c b/lib/tty/mouse.c index 1469211e7..a34e9f1c5 100644 --- a/lib/tty/mouse.c +++ b/lib/tty/mouse.c @@ -1,15 +1,15 @@ /* Mouse managing Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc. - + Written by: Andrew Borodin , 2009. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -34,13 +34,28 @@ #include "lib/global.h" #include "tty.h" -#include "tty-internal.h" /* mouse_enabled */ +#include "tty-internal.h" /* mouse_enabled */ #include "mouse.h" -#include "key.h" /* define sequence */ +#include "key.h" /* define sequence */ + +/*** global variables ****************************************************************************/ gboolean mouse_enabled = FALSE; const char *xmouse_seq; +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void show_mouse_pointer (int x, int y) { @@ -53,10 +68,13 @@ show_mouse_pointer (int x, int y) #endif /* HAVE_LIBGPM */ } +/* --------------------------------------------------------------------------------------------- */ + void init_mouse (void) { - switch (use_mouse_p) { + switch (use_mouse_p) + { #ifdef HAVE_LIBGPM case MOUSE_NONE: use_mouse_p = MOUSE_GPM; @@ -75,31 +93,35 @@ init_mouse (void) enable_mouse (); } +/* --------------------------------------------------------------------------------------------- */ + void enable_mouse (void) { if (mouse_enabled) return; - switch (use_mouse_p) { + switch (use_mouse_p) + { #ifdef HAVE_LIBGPM case MOUSE_GPM: - { - int mouse_d; - Gpm_Connect conn; + { + int mouse_d; + Gpm_Connect conn; - conn.eventMask = ~GPM_MOVE; - conn.defaultMask = GPM_MOVE; - conn.minMod = 0; - conn.maxMod = 0; + conn.eventMask = ~GPM_MOVE; + conn.defaultMask = GPM_MOVE; + conn.minMod = 0; + conn.maxMod = 0; - mouse_d = Gpm_Open (&conn, 0); - if (mouse_d == -1) { - use_mouse_p = MOUSE_NONE; - return; + mouse_d = Gpm_Open (&conn, 0); + if (mouse_d == -1) + { + use_mouse_p = MOUSE_NONE; + return; + } + mouse_enabled = 1; } - mouse_enabled = 1; - } break; #endif /* HAVE_LIBGPM */ @@ -130,6 +152,8 @@ enable_mouse (void) } } +/* --------------------------------------------------------------------------------------------- */ + void disable_mouse (void) { @@ -138,7 +162,8 @@ disable_mouse (void) mouse_enabled = FALSE; - switch (use_mouse_p) { + switch (use_mouse_p) + { #ifdef HAVE_LIBGPM case MOUSE_GPM: Gpm_Close (); @@ -166,3 +191,5 @@ disable_mouse (void) break; } } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/mouse.h b/lib/tty/mouse.h index 6d2c2b9fe..5a66b9376 100644 --- a/lib/tty/mouse.h +++ b/lib/tty/mouse.h @@ -5,33 +5,53 @@ * Events received by clients of this library have their coordinates 0 based */ -#ifndef MC_MOUSE_H -#define MC_MOUSE_H +#ifndef MC__MOUSE_H +#define MC__MOUSE_H #ifdef HAVE_LIBGPM - /* GPM mouse support include file */ #include +#endif /* !HAVE_LIBGPM */ -#else +/*** typedefs(not structures) and defined constants **********************************************/ + +#ifndef HAVE_LIBGPM /* Equivalent definitions for non-GPM mouse support */ /* These lines are modified version from the lines appearing in the */ /* gpm.h include file of the Linux General Purpose Mouse server */ -#define GPM_B_LEFT (1 << 2) +#define GPM_B_LEFT (1 << 2) #define GPM_B_MIDDLE (1 << 1) #define GPM_B_RIGHT (1 << 0) +#define GPM_BARE_EVENTS(ev) ((ev)&0xF) +#endif /* !HAVE_LIBGPM */ + +/* Mouse wheel events */ +#ifndef GPM_B_DOWN +#define GPM_B_DOWN (1 << 5) +#endif + +#ifndef GPM_B_UP +#define GPM_B_UP (1 << 4) +#endif + +/* Mouse callback */ +typedef int (*mouse_h) (Gpm_Event *, void *); + +/*** enums ***************************************************************************************/ + +#ifndef HAVE_LIBGPM /* Xterm mouse support supports only GPM_DOWN and GPM_UP */ /* If you use others make sure your code also works without them */ -enum Gpm_Etype { +enum Gpm_Etype +{ GPM_MOVE = 1, GPM_DRAG = 2, /* exactly one in four is active at a time */ GPM_DOWN = 4, GPM_UP = 8, -#define GPM_BARE_EVENTS(ev) ((ev)&0xF) GPM_SINGLE = 16, /* at most one in three is set */ GPM_DOUBLE = 32, @@ -41,24 +61,15 @@ enum Gpm_Etype { GPM_HARD = 256 /* if set in the defaultMask, force an already used event to pass over to another handler */ }; - -typedef struct Gpm_Event { - int buttons, x, y; - enum Gpm_Etype type; -} Gpm_Event; - #endif /* !HAVE_LIBGPM */ -/* General (i.e. both for xterm and gpm) mouse support definitions */ - /* Constants returned from the mouse callback */ -enum { MOU_NORMAL, MOU_REPEAT }; - -/* Mouse callback */ -typedef int (*mouse_h) (Gpm_Event *, void *); +enum +{ MOU_NORMAL, MOU_REPEAT }; /* Type of mouse support */ -typedef enum { +typedef enum +{ MOUSE_NONE, /* Not detected yet */ MOUSE_DISABLED, /* Explicitly disabled by -d */ MOUSE_GPM, /* Support using GPM on Linux */ @@ -67,25 +78,34 @@ typedef enum { MOUSE_XTERM_BUTTON_EVENT_TRACKING } Mouse_Type; +/*** structures declarations (and typedefs of structures)*****************************************/ + +#ifndef HAVE_LIBGPM +typedef struct Gpm_Event +{ + int buttons, x, y; + enum Gpm_Etype type; +} Gpm_Event; +#endif /* !HAVE_LIBGPM */ + + +/*** global variables defined in .c file *********************************************************/ + /* Type of the currently used mouse */ extern Mouse_Type use_mouse_p; /* String indicating that a mouse event has occured, usually "\E[M" */ extern const char *xmouse_seq; +/*** declarations of public functions ************************************************************/ + +/* General (i.e. both for xterm and gpm) mouse support definitions */ + void init_mouse (void); void enable_mouse (void); void disable_mouse (void); -/* Mouse wheel events */ -#ifndef GPM_B_DOWN -#define GPM_B_DOWN (1 << 5) -#endif - -#ifndef GPM_B_UP -#define GPM_B_UP (1 << 4) -#endif - void show_mouse_pointer (int x, int y); +/*** inline functions ****************************************************************************/ #endif /* MC_MOUSE_H */ diff --git a/lib/tty/tty-internal.h b/lib/tty/tty-internal.h index ffd6bfc21..4ab3c5de0 100644 --- a/lib/tty/tty-internal.h +++ b/lib/tty/tty-internal.h @@ -3,21 +3,29 @@ * \brief Header: internal suff of the terminal controlling library */ -#ifndef MC_TTY_INTERNAL_H -#define MC_TTY_INTERNAL_H +#ifndef MC__TTY_INTERNAL_H +#define MC__TTY_INTERNAL_H -#include "lib/global.h" /* include */ +#include "lib/global.h" /* include */ + +/*** typedefs(not structures) and defined constants **********************************************/ /* Taken from S-Lang's slutty.c */ -#ifdef ultrix /* Ultrix gets _POSIX_VDISABLE wrong! */ -# define NULL_VALUE -1 +#ifdef ultrix /* Ultrix gets _POSIX_VDISABLE wrong! */ +#define NULL_VALUE -1 #else -# ifdef _POSIX_VDISABLE -# define NULL_VALUE _POSIX_VDISABLE -# else -# define NULL_VALUE 255 -# endif +#ifdef _POSIX_VDISABLE +#define NULL_VALUE _POSIX_VDISABLE +#else +#define NULL_VALUE 255 #endif +#endif + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ /* If true lines are shown by spaces */ extern gboolean slow_tty; @@ -28,6 +36,9 @@ extern gboolean ugly_line_drawing; /* The mouse is currently: TRUE - enabled, FALSE - disabled */ extern gboolean mouse_enabled; +/*** declarations of public functions ************************************************************/ + char *mc_tty_normalize_from_utf8 (const char *); +/*** inline functions ****************************************************************************/ #endif /* MC_TTY_INTERNAL_H */ diff --git a/lib/tty/tty-ncurses.c b/lib/tty/tty-ncurses.c index b7a521385..10a7c492b 100644 --- a/lib/tty/tty-ncurses.c +++ b/lib/tty/tty-ncurses.c @@ -42,7 +42,7 @@ #include "src/main.h" #ifndef WANT_TERM_H -# define WANT_TERM_H +#define WANT_TERM_H #endif #include "tty-internal.h" /* slow_tty */ @@ -53,30 +53,31 @@ /* include at last !!! */ #ifdef WANT_TERM_H #ifdef HAVE_NCURSES_TERM_H -# include +#include #else -# include +#include #endif /* HAVE_NCURSES_TERM_H */ #endif /* WANT_TERM_H */ -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ -/*** file scope macro definitions **************************************/ +/*** file scope macro definitions ****************************************************************/ #if defined(_AIX) && !defined(CTRL) -# define CTRL(x) ((x) & 0x1f) +#define CTRL(x) ((x) & 0x1f) #endif -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ -/*** file scope type declarations **************************************/ +/*** file scope type declarations ****************************************************************/ -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ -/*** file scope functions **********************************************/ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */ -/*** public functions **************************************************/ +/*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ int @@ -137,7 +138,6 @@ mc_tty_normalize_lines_char (const char *ch) /* --------------------------------------------------------------------------------------------- */ - void tty_init (gboolean slow, gboolean ugly_lines) { @@ -174,24 +174,32 @@ tty_init (gboolean slow, gboolean ugly_lines) nodelay (stdscr, FALSE); } +/* --------------------------------------------------------------------------------------------- */ + void tty_shutdown (void) { endwin (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_reset_prog_mode (void) { reset_prog_mode (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_reset_shell_mode (void) { reset_shell_mode (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_raw_mode (void) { @@ -199,6 +207,8 @@ tty_raw_mode (void) cbreak (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_noraw_mode (void) { @@ -206,67 +216,89 @@ tty_noraw_mode (void) noraw (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_noecho (void) { noecho (); } +/* --------------------------------------------------------------------------------------------- */ + int tty_flush_input (void) { return flushinp (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_keypad (gboolean set) { keypad (stdscr, (bool) set); } +/* --------------------------------------------------------------------------------------------- */ + void tty_nodelay (gboolean set) { nodelay (stdscr, (bool) set); } +/* --------------------------------------------------------------------------------------------- */ + int tty_baudrate (void) { return baudrate (); } +/* --------------------------------------------------------------------------------------------- */ + int tty_lowlevel_getch (void) { return getch (); } +/* --------------------------------------------------------------------------------------------- */ + int tty_reset_screen (void) { return endwin (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_touch_screen (void) { touchwin (stdscr); } +/* --------------------------------------------------------------------------------------------- */ + void tty_gotoyx (int y, int x) { move (y, x); } +/* --------------------------------------------------------------------------------------------- */ + void tty_getyx (int *py, int *px) { getyx (stdscr, *py, *px); } +/* --------------------------------------------------------------------------------------------- */ /* if x < 0 or y < 0, draw line starting from current position */ + void tty_draw_hline (int y, int x, int ch, int len) { @@ -278,7 +310,9 @@ tty_draw_hline (int y, int x, int ch, int len) hline (ch, len); } +/* --------------------------------------------------------------------------------------------- */ /* if x < 0 or y < 0, draw line starting from current position */ + void tty_draw_vline (int y, int x, int ch, int len) { @@ -290,6 +324,8 @@ tty_draw_vline (int y, int x, int ch, int len) vline (ch, len); } +/* --------------------------------------------------------------------------------------------- */ + void tty_fill_region (int y, int x, int rows, int cols, unsigned char ch) { @@ -329,24 +365,32 @@ tty_fill_region (int y, int x, int rows, int cols, unsigned char ch) move (y, x); } +/* --------------------------------------------------------------------------------------------- */ + void tty_set_alt_charset (gboolean alt_charset) { (void) alt_charset; } +/* --------------------------------------------------------------------------------------------- */ + void tty_display_8bit (gboolean what) { meta (stdscr, (int) what); } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_char (int c) { addch (c); } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_anychar (int c) { @@ -369,6 +413,8 @@ tty_print_anychar (int c) } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_alt_char (int c, gboolean single) { @@ -394,12 +440,16 @@ tty_print_alt_char (int c, gboolean single) addch (c); } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_string (const char *s) { addstr (str_term_form (s)); } +/* --------------------------------------------------------------------------------------------- */ + void tty_printf (const char *fmt, ...) { @@ -410,6 +460,8 @@ tty_printf (const char *fmt, ...) va_end (args); } +/* --------------------------------------------------------------------------------------------- */ + char * tty_tgetstr (const char *cap) { @@ -417,6 +469,8 @@ tty_tgetstr (const char *cap) return tgetstr ((char *) cap, &unused); } +/* --------------------------------------------------------------------------------------------- */ + void tty_refresh (void) { @@ -424,6 +478,8 @@ tty_refresh (void) doupdate (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_setup_sigwinch (void (*handler) (int)) { @@ -439,8 +495,12 @@ tty_setup_sigwinch (void (*handler) (int)) #endif /* SIGWINCH */ } +/* --------------------------------------------------------------------------------------------- */ + void tty_beep (void) { beep (); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/tty-ncurses.h b/lib/tty/tty-ncurses.h index c45141a82..50f0597ec 100644 --- a/lib/tty/tty-ncurses.h +++ b/lib/tty/tty-ncurses.h @@ -1,25 +1,37 @@ -#ifndef MC_TTY_NCURSES_H -#define MC_TTY_NCURSES_H +#ifndef MC__TTY_NCURSES_H +#define MC__TTY_NCURSES_H #ifdef USE_NCURSES -# ifdef HAVE_NCURSES_CURSES_H -# include -# elif HAVE_NCURSES_NCURSES_H -# include -# elif HAVE_NCURSESW_CURSES_H -# include -# elif HAVE_NCURSES_HCURSES_H -# include -# elif HAVE_NCURSES_H -# include -# else -# include -# endif +#ifdef HAVE_NCURSES_CURSES_H +#include +#elif HAVE_NCURSES_NCURSES_H +#include +#elif HAVE_NCURSESW_CURSES_H +#include +#elif HAVE_NCURSES_HCURSES_H +#include +#elif HAVE_NCURSES_H +#include +#else +#include +#endif #endif /* USE_NCURSES */ #ifdef USE_NCURSESW -# include +#include #endif /* USE_NCURSESW */ +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ + #endif /* MC_TTY_NCURSES_H */ diff --git a/lib/tty/tty-slang.c b/lib/tty/tty-slang.c index 1ec971b6f..b91f96508 100644 --- a/lib/tty/tty-slang.c +++ b/lib/tty/tty-slang.c @@ -51,14 +51,14 @@ #include "win.h" -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ extern int reset_hp_softkeys; -/*** file scope macro definitions **************************************/ +/*** file scope macro definitions ****************************************************************/ #ifndef SA_RESTART -# define SA_RESTART 0 +#define SA_RESTART 0 #endif #ifndef SLTT_MAX_SCREEN_COLS @@ -69,9 +69,9 @@ extern int reset_hp_softkeys; #define SLTT_MAX_SCREEN_ROWS 512 #endif -/*** file scope type declarations **************************************/ +/*** file scope type declarations ****************************************************************/ -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ /* Various saved termios settings that we control here */ static struct termios boot_mode; @@ -126,7 +126,8 @@ static const struct /* *INDENT-ON* */ }; -/*** file scope functions **********************************************/ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /* HP Terminals have capabilities (pfkey, pfloc, pfx) to program function keys. elm 2.4pl15 invoked with the -K option utilizes these softkeys and the @@ -164,6 +165,8 @@ slang_reset_softkeys (void) } } +/* --------------------------------------------------------------------------------------------- */ + static void do_define_key (int code, const char *strcap) { @@ -174,6 +177,8 @@ do_define_key (int code, const char *strcap) define_sequence (code, seq, MCKEY_NOACTION); } +/* --------------------------------------------------------------------------------------------- */ + static void load_terminfo_keys (void) { @@ -184,7 +189,7 @@ load_terminfo_keys (void) } /* --------------------------------------------------------------------------------------------- */ -/*** public functions **************************************************/ +/*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ int @@ -233,6 +238,7 @@ mc_tty_normalize_lines_char (const char *str) } /* --------------------------------------------------------------------------------------------- */ + void tty_init (gboolean slow, gboolean ugly_lines) { @@ -297,6 +303,8 @@ tty_init (gboolean slow, gboolean ugly_lines) tty_nodelay (FALSE); } +/* --------------------------------------------------------------------------------------------- */ + void tty_shutdown (void) { @@ -318,7 +326,9 @@ tty_shutdown (void) } } +/* --------------------------------------------------------------------------------------------- */ /* Done each time we come back from done mode */ + void tty_reset_prog_mode (void) { @@ -327,35 +337,47 @@ tty_reset_prog_mode (void) SLsmg_touch_lines (0, LINES); } +/* --------------------------------------------------------------------------------------------- */ /* Called each time we want to shutdown slang screen manager */ + void tty_reset_shell_mode (void) { tcsetattr (SLang_TT_Read_FD, TCSANOW, &boot_mode); } +/* --------------------------------------------------------------------------------------------- */ + void tty_raw_mode (void) { tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode); } +/* --------------------------------------------------------------------------------------------- */ + void tty_noraw_mode (void) { } +/* --------------------------------------------------------------------------------------------- */ + void tty_noecho (void) { } +/* --------------------------------------------------------------------------------------------- */ + int tty_flush_input (void) { return 0; /* OK */ } +/* --------------------------------------------------------------------------------------------- */ + void tty_keypad (gboolean set) { @@ -368,18 +390,24 @@ tty_keypad (gboolean set) slang_reset_softkeys (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_nodelay (gboolean set) { no_slang_delay = set; } +/* --------------------------------------------------------------------------------------------- */ + int tty_baudrate (void) { return SLang_TT_Baud_Rate; } +/* --------------------------------------------------------------------------------------------- */ + int tty_lowlevel_getch (void) { @@ -400,6 +428,8 @@ tty_lowlevel_getch (void) return c; } +/* --------------------------------------------------------------------------------------------- */ + int tty_reset_screen (void) { @@ -407,18 +437,24 @@ tty_reset_screen (void) return 0; /* OK */ } +/* --------------------------------------------------------------------------------------------- */ + void tty_touch_screen (void) { SLsmg_touch_lines (0, LINES); } +/* --------------------------------------------------------------------------------------------- */ + void tty_gotoyx (int y, int x) { SLsmg_gotorc (y, x); } +/* --------------------------------------------------------------------------------------------- */ + void tty_getyx (int *py, int *px) { @@ -426,7 +462,9 @@ tty_getyx (int *py, int *px) *px = SLsmg_get_column (); } +/* --------------------------------------------------------------------------------------------- */ /* if x < 0 or y < 0, draw line staring from current position */ + void tty_draw_hline (int y, int x, int ch, int len) { @@ -453,7 +491,9 @@ tty_draw_hline (int y, int x, int ch, int len) SLsmg_gotorc (y, x); } +/* --------------------------------------------------------------------------------------------- */ /* if x < 0 or y < 0, draw line staring from current position */ + void tty_draw_vline (int y, int x, int ch, int len) { @@ -488,30 +528,40 @@ tty_draw_vline (int y, int x, int ch, int len) SLsmg_gotorc (y, x); } +/* --------------------------------------------------------------------------------------------- */ + void tty_fill_region (int y, int x, int rows, int cols, unsigned char ch) { SLsmg_fill_region (y, x, rows, cols, ch); } +/* --------------------------------------------------------------------------------------------- */ + void tty_set_alt_charset (gboolean alt_charset) { SLsmg_set_char_set ((int) alt_charset); } +/* --------------------------------------------------------------------------------------------- */ + void tty_display_8bit (gboolean what) { SLsmg_Display_Eight_Bit = what ? 128 : 160; } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_char (int c) { SLsmg_write_char ((SLwchar_Type) ((unsigned int) c)); } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_alt_char (int c, gboolean single) { @@ -553,6 +603,8 @@ tty_print_alt_char (int c, gboolean single) #undef DRAW } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_anychar (int c) { @@ -578,12 +630,16 @@ tty_print_anychar (int c) } } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_string (const char *s) { SLsmg_write_string ((char *) str_term_form (s)); } +/* --------------------------------------------------------------------------------------------- */ + void tty_printf (const char *fmt, ...) { @@ -594,18 +650,24 @@ tty_printf (const char *fmt, ...) va_end (args); } +/* --------------------------------------------------------------------------------------------- */ + char * tty_tgetstr (const char *cap) { return SLtt_tgetstr ((char *) cap); } +/* --------------------------------------------------------------------------------------------- */ + void tty_refresh (void) { SLsmg_refresh (); } +/* --------------------------------------------------------------------------------------------- */ + void tty_setup_sigwinch (void (*handler) (int)) { @@ -621,8 +683,12 @@ tty_setup_sigwinch (void (*handler) (int)) #endif /* SIGWINCH */ } +/* --------------------------------------------------------------------------------------------- */ + void tty_beep (void) { SLtt_beep (); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/tty-slang.h b/lib/tty/tty-slang.h index 086b3ab43..620fd6b85 100644 --- a/lib/tty/tty-slang.h +++ b/lib/tty/tty-slang.h @@ -1,33 +1,46 @@ -#ifndef MC_TTY_SLANG_H -#define MC_TTY_SLANG_H +#ifndef MC__TTY_SLANG_H +#define MC__TTY_SLANG_H #ifdef HAVE_SLANG_SLANG_H -# include +#include #else -# include +#include #endif /* HAVE_SLANG_SLANG_H */ -enum { +/*** typedefs(not structures) and defined constants **********************************************/ + +#define KEY_F(x) (1000 + x) + +#define ACS_VLINE SLSMG_VLINE_CHAR +#define ACS_HLINE SLSMG_HLINE_CHAR +#define ACS_LTEE SLSMG_LTEE_CHAR +#define ACS_RTEE SLSMG_RTEE_CHAR +#define ACS_ULCORNER SLSMG_ULCORN_CHAR +#define ACS_LLCORNER SLSMG_LLCORN_CHAR +#define ACS_URCORNER SLSMG_URCORN_CHAR +#define ACS_LRCORNER SLSMG_LRCORN_CHAR +#define ACS_PLUS SLSMG_PLUS_CHAR + +#define COLS SLtt_Screen_Cols +#define LINES SLtt_Screen_Rows + +/*** enums ***************************************************************************************/ + +enum +{ KEY_BACKSPACE = 400, KEY_END, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_A1, KEY_C1, KEY_NPAGE, KEY_PPAGE, KEY_IC, KEY_ENTER, KEY_DC, KEY_SCANCEL, KEY_BTAB }; -#define KEY_F(x) (1000 + x) +/*** structures declarations (and typedefs of structures)*****************************************/ -#define ACS_VLINE SLSMG_VLINE_CHAR -#define ACS_HLINE SLSMG_HLINE_CHAR -#define ACS_LTEE SLSMG_LTEE_CHAR -#define ACS_RTEE SLSMG_RTEE_CHAR -#define ACS_ULCORNER SLSMG_ULCORN_CHAR -#define ACS_LLCORNER SLSMG_LLCORN_CHAR -#define ACS_URCORNER SLSMG_URCORN_CHAR -#define ACS_LRCORNER SLSMG_LRCORN_CHAR -#define ACS_PLUS SLSMG_PLUS_CHAR +/*** global variables defined in .c file *********************************************************/ -#define COLS SLtt_Screen_Cols -#define LINES SLtt_Screen_Rows +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ #endif /* MC_TTY_SLANG_H */ diff --git a/lib/tty/tty.c b/lib/tty/tty.c index 3354a5614..dc7ca6c7b 100644 --- a/lib/tty/tty.c +++ b/lib/tty/tty.c @@ -41,7 +41,7 @@ #include "tty-internal.h" -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ /* If true program softkeys (HP terminals only) on startup and after every command ran in the subshell to the description found in the termcap/terminfo @@ -56,15 +56,16 @@ gboolean ugly_line_drawing = FALSE; int mc_tty_frm[MC_TTY_FRM_MAX]; -/*** file scope macro definitions **************************************/ +/*** file scope macro definitions ****************************************************************/ -/*** file scope type declarations **************************************/ +/*** file scope type declarations ****************************************************************/ -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ static volatile sig_atomic_t got_interrupt = 0; -/*** file scope functions **********************************************/ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static void sigintr_handler (int signo) @@ -73,7 +74,9 @@ sigintr_handler (int signo) got_interrupt = 1; } -/*** public functions **************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ extern gboolean tty_is_slow (void) @@ -81,6 +84,8 @@ tty_is_slow (void) return slow_tty; } +/* --------------------------------------------------------------------------------------------- */ + extern void tty_start_interrupt_key (void) { @@ -92,6 +97,8 @@ tty_start_interrupt_key (void) sigaction (SIGINT, &act, NULL); } +/* --------------------------------------------------------------------------------------------- */ + extern void tty_enable_interrupt_key (void) { @@ -104,6 +111,8 @@ tty_enable_interrupt_key (void) got_interrupt = 0; } +/* --------------------------------------------------------------------------------------------- */ + extern void tty_disable_interrupt_key (void) { @@ -115,6 +124,8 @@ tty_disable_interrupt_key (void) sigaction (SIGINT, &act, NULL); } +/* --------------------------------------------------------------------------------------------- */ + extern gboolean tty_got_interrupt (void) { @@ -125,18 +136,24 @@ tty_got_interrupt (void) return rv; } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_one_hline (gboolean single) { tty_print_alt_char (ACS_HLINE, single); } +/* --------------------------------------------------------------------------------------------- */ + void tty_print_one_vline (gboolean single) { tty_print_alt_char (ACS_VLINE, single); } +/* --------------------------------------------------------------------------------------------- */ + void tty_draw_box (int y, int x, int ys, int xs, gboolean single) { @@ -157,6 +174,8 @@ tty_draw_box (int y, int x, int ys, int xs, gboolean single) tty_print_alt_char (ACS_LRCORNER, single); } +/* --------------------------------------------------------------------------------------------- */ + char * mc_tty_normalize_from_utf8 (const char *str) { @@ -183,3 +202,5 @@ mc_tty_normalize_from_utf8 (const char *str) return g_string_free (buffer, FALSE); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/tty.h b/lib/tty/tty.h index c5abd502b..521aad64e 100644 --- a/lib/tty/tty.h +++ b/lib/tty/tty.h @@ -8,17 +8,68 @@ * files small. */ -#ifndef MC_TTY_H -#define MC_TTY_H +#ifndef MC__TTY_H +#define MC__TTY_H #include "lib/global.h" /* include */ #ifdef HAVE_SLANG -# include "tty-slang.h" +#include "tty-slang.h" #else -# include "tty-ncurses.h" +#include "tty-ncurses.h" #endif +/*** typedefs(not structures) and defined constants **********************************************/ + +#define KEY_KP_ADD 4001 +#define KEY_KP_SUBTRACT 4002 +#define KEY_KP_MULTIPLY 4003 + +/*** enums ***************************************************************************************/ + +typedef enum +{ + /* single lines */ + MC_TTY_FRM_VERT, + MC_TTY_FRM_HORIZ, + MC_TTY_FRM_LEFTTOP, + MC_TTY_FRM_RIGHTTOP, + MC_TTY_FRM_LEFTBOTTOM, + MC_TTY_FRM_RIGHTBOTTOM, + MC_TTY_FRM_TOPMIDDLE, + MC_TTY_FRM_BOTTOMMIDDLE, + MC_TTY_FRM_LEFTMIDDLE, + MC_TTY_FRM_RIGHTMIDDLE, + MC_TTY_FRM_CROSS, + + /* double lines */ + MC_TTY_FRM_DVERT, + MC_TTY_FRM_DHORIZ, + MC_TTY_FRM_DLEFTTOP, + MC_TTY_FRM_DRIGHTTOP, + MC_TTY_FRM_DLEFTBOTTOM, + MC_TTY_FRM_DRIGHTBOTTOM, + MC_TTY_FRM_DTOPMIDDLE, + MC_TTY_FRM_DBOTTOMMIDDLE, + MC_TTY_FRM_DLEFTMIDDLE, + MC_TTY_FRM_DRIGHTMIDDLE, + + MC_TTY_FRM_MAX +} mc_tty_frm_t; + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +extern int mc_tty_frm[]; + +extern char *tty_tgetstr (const char *name); + +extern void tty_beep (void); + +/*** declarations of public functions ************************************************************/ + + /* {{{ Input }}} */ extern int reset_hp_softkeys; @@ -78,49 +129,10 @@ extern void tty_draw_vline (int y, int x, int ch, int len); extern void tty_draw_box (int y, int x, int rows, int cols, gboolean single); extern void tty_fill_region (int y, int x, int rows, int cols, unsigned char ch); -extern int mc_tty_frm[]; - -typedef enum -{ - /* single lines */ - MC_TTY_FRM_VERT, - MC_TTY_FRM_HORIZ, - MC_TTY_FRM_LEFTTOP, - MC_TTY_FRM_RIGHTTOP, - MC_TTY_FRM_LEFTBOTTOM, - MC_TTY_FRM_RIGHTBOTTOM, - MC_TTY_FRM_TOPMIDDLE, - MC_TTY_FRM_BOTTOMMIDDLE, - MC_TTY_FRM_LEFTMIDDLE, - MC_TTY_FRM_RIGHTMIDDLE, - MC_TTY_FRM_CROSS, - - /* double lines */ - MC_TTY_FRM_DVERT, - MC_TTY_FRM_DHORIZ, - MC_TTY_FRM_DLEFTTOP, - MC_TTY_FRM_DRIGHTTOP, - MC_TTY_FRM_DLEFTBOTTOM, - MC_TTY_FRM_DRIGHTBOTTOM, - MC_TTY_FRM_DTOPMIDDLE, - MC_TTY_FRM_DBOTTOMMIDDLE, - MC_TTY_FRM_DLEFTMIDDLE, - MC_TTY_FRM_DRIGHTMIDDLE, - - MC_TTY_FRM_MAX -} mc_tty_frm_t; - -extern char *tty_tgetstr (const char *name); - -extern void tty_beep (void); - -#define KEY_KP_ADD 4001 -#define KEY_KP_SUBTRACT 4002 -#define KEY_KP_MULTIPLY 4003 - extern void tty_refresh (void); extern void tty_setup_sigwinch (void (*handler) (int)); extern int mc_tty_normalize_lines_char (const char *); +/*** inline functions ****************************************************************************/ #endif /* MC_TTY_H */ diff --git a/lib/tty/win.c b/lib/tty/win.c index 0a31bc93a..3f88e4a13 100644 --- a/lib/tty/win.c +++ b/lib/tty/win.c @@ -1,10 +1,10 @@ /* Terminal management xterm and rxvt support Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009 Free Software Foundation, Inc. - + Written by: Andrew Borodin , 2009. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -33,11 +33,11 @@ #include #include "lib/global.h" -#include "tty.h" /* tty_gotoyx, tty_print_char */ +#include "tty.h" /* tty_gotoyx, tty_print_char */ #include "win.h" -#include "src/consaver/cons.saver.h" /* console_flag */ +#include "src/consaver/cons.saver.h" /* console_flag */ -/*** global variables **************************************************/ +/*** global variables ****************************************************************************/ /* This flag is set by xterm detection routine in function main() */ /* It is used by function view_other_cmd() */ @@ -45,15 +45,16 @@ int xterm_flag = 0; extern int keybar_visible; -/*** file scope macro definitions **************************************/ +/*** file scope macro definitions ****************************************************************/ -/*** file scope type declarations **************************************/ +/*** file scope type declarations ****************************************************************/ -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ static gboolean rxvt_extensions = FALSE; -/*** file scope functions **********************************************/ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ /* my own wierd protocol base 16 - paul */ static int @@ -71,6 +72,8 @@ rxvt_getc (void) return r; } +/* --------------------------------------------------------------------------------------------- */ + static int anything_ready (void) { @@ -84,26 +87,34 @@ anything_ready (void) return select (1, &fds, 0, 0, &tv); } -/*** public functions **************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ void do_enter_ca_mode (void) { - if (xterm_flag) { + if (xterm_flag) + { fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h"); fflush (stdout); } } +/* --------------------------------------------------------------------------------------------- */ + void do_exit_ca_mode (void) { - if (xterm_flag) { + if (xterm_flag) + { fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m"); fflush (stdout); } } +/* --------------------------------------------------------------------------------------------- */ + void show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) { @@ -115,20 +126,22 @@ show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) while (anything_ready ()) tty_lowlevel_getch (); -/* my own wierd protocol base 26 - paul */ + /* my own wierd protocol base 26 - paul */ printf ("\033CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A', (y2 % 26) + 'A'); bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */ j = 0; k = g_malloc (bytes); - for (;;) { + for (;;) + { int c; c = rxvt_getc (); if (c < 0) break; if (j < bytes) k[j++] = c; - for (cols = 1;; cols++) { + for (cols = 1;; cols++) + { c = rxvt_getc (); if (c < 0) break; @@ -136,7 +149,8 @@ show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) k[j++] = c; } } - for (i = 0; i < j; i++) { + for (i = 0; i < j; i++) + { if ((i % cols) == 0) tty_gotoyx (starty + (i / cols), 0); tty_print_char (is_printable (k[i]) ? k[i] : ' '); @@ -144,12 +158,15 @@ show_rxvt_contents (int starty, unsigned char y1, unsigned char y2) g_free (k); } +/* --------------------------------------------------------------------------------------------- */ + gboolean look_for_rxvt_extensions (void) { static gboolean been_called = FALSE; - if (!been_called) { + if (!been_called) + { const char *e = getenv ("RXVT_EXT"); rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0)); been_called = TRUE; @@ -160,3 +177,5 @@ look_for_rxvt_extensions (void) return rxvt_extensions; } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/win.h b/lib/tty/win.h index 1b9b00183..bca052bfb 100644 --- a/lib/tty/win.h +++ b/lib/tty/win.h @@ -1,19 +1,29 @@ - /** \file win.h * \brief Header: X terminal management: xterm and rxvt */ -#ifndef MC_WIN_H -#define MC_WIN_H +#ifndef MC__WIN_H +#define MC__WIN_H -#include "lib/global.h" /* */ +#include "lib/global.h" /* */ + +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ extern int xterm_flag; +/*** declarations of public functions ************************************************************/ + void do_enter_ca_mode (void); void do_exit_ca_mode (void); void show_rxvt_contents (int starty, unsigned char y1, unsigned char y2); gboolean look_for_rxvt_extensions (void); +/*** inline functions ****************************************************************************/ #endif /* MC_WIN_H */ diff --git a/lib/tty/x11conn.c b/lib/tty/x11conn.c index 18de0570c..024763975 100644 --- a/lib/tty/x11conn.c +++ b/lib/tty/x11conn.c @@ -36,40 +36,39 @@ typedef int dummy; /* C99 forbids empty compilation unit */ #include #include #ifdef HAVE_GMODULE -# include +#include #endif #include "lib/global.h" #include "x11conn.h" -/*** file scope type declarations **************************************/ +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + +#ifndef HAVE_GMODULE +#define func_XOpenDisplay XOpenDisplay +#define func_XCloseDisplay XCloseDisplay +#define func_XSetErrorHandler XSetErrorHandler +#define func_XSetIOErrorHandler XSetIOErrorHandler +#define func_XQueryPointer XQueryPointer +#endif + +/*** file scope type declarations ****************************************************************/ typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *); typedef int (*mc_XIOErrorHandler_callback) (Display *); -/*** file scope variables **********************************************/ +/*** file scope variables ************************************************************************/ #ifdef HAVE_GMODULE - static Display *(*func_XOpenDisplay) (_Xconst char *); static int (*func_XCloseDisplay) (Display *); -static mc_XErrorHandler_callback (*func_XSetErrorHandler) - (mc_XErrorHandler_callback); -static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) - (mc_XIOErrorHandler_callback); +static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback); +static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback); static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *, int *, int *, int *, int *, unsigned int *); - static GModule *x11_module; - -#else - -#define func_XOpenDisplay XOpenDisplay -#define func_XCloseDisplay XCloseDisplay -#define func_XSetErrorHandler XSetErrorHandler -#define func_XSetIOErrorHandler XSetIOErrorHandler -#define func_XQueryPointer XQueryPointer - #endif static gboolean handlers_installed = FALSE; @@ -82,7 +81,8 @@ static gboolean lost_connection = FALSE; static jmp_buf x11_exception; /* FIXME: get a better name */ static gboolean longjmp_allowed = FALSE; -/*** file private functions ********************************************/ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static int x_io_error_handler (Display * dpy) @@ -90,13 +90,16 @@ x_io_error_handler (Display * dpy) (void) dpy; lost_connection = TRUE; - if (longjmp_allowed) { + if (longjmp_allowed) + { longjmp_allowed = FALSE; longjmp (x11_exception, 1); } return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int x_error_handler (Display * dpy, XErrorEvent * ee) { @@ -105,6 +108,8 @@ x_error_handler (Display * dpy, XErrorEvent * ee) return x_io_error_handler (dpy); } +/* --------------------------------------------------------------------------------------------- */ + static void install_error_handlers (void) { @@ -116,6 +121,8 @@ install_error_handlers (void) handlers_installed = TRUE; } +/* --------------------------------------------------------------------------------------------- */ + static gboolean x11_available (void) { @@ -167,15 +174,19 @@ x11_available (void) #endif } -/*** public functions **************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ Display * mc_XOpenDisplay (const char *displayname) { Display *retval; - if (x11_available ()) { - if (setjmp (x11_exception) == 0) { + if (x11_available ()) + { + if (setjmp (x11_exception) == 0) + { longjmp_allowed = TRUE; retval = func_XOpenDisplay (displayname); longjmp_allowed = FALSE; @@ -185,13 +196,17 @@ mc_XOpenDisplay (const char *displayname) return NULL; } +/* --------------------------------------------------------------------------------------------- */ + int mc_XCloseDisplay (Display * display) { int retval; - if (x11_available ()) { - if (setjmp (x11_exception) == 0) { + if (x11_available ()) + { + if (setjmp (x11_exception) == 0) + { longjmp_allowed = TRUE; retval = func_XCloseDisplay (display); longjmp_allowed = FALSE; @@ -201,6 +216,8 @@ mc_XCloseDisplay (Display * display) return 0; } +/* --------------------------------------------------------------------------------------------- */ + Bool mc_XQueryPointer (Display * display, Window win, Window * root_return, Window * child_return, int *root_x_return, int *root_y_return, @@ -208,8 +225,10 @@ mc_XQueryPointer (Display * display, Window win, Window * root_return, { Bool retval; - if (x11_available ()) { - if (setjmp (x11_exception) == 0) { + if (x11_available ()) + { + if (setjmp (x11_exception) == 0) + { longjmp_allowed = TRUE; retval = func_XQueryPointer (display, win, root_return, child_return, root_x_return, root_y_return, @@ -228,4 +247,6 @@ mc_XQueryPointer (Display * display, Window win, Window * root_return, return False; } +/* --------------------------------------------------------------------------------------------- */ + #endif /* HAVE_TEXTMODE_X11_SUPPORT */ diff --git a/lib/tty/x11conn.h b/lib/tty/x11conn.h index 3f9a3c507..774a800fd 100644 --- a/lib/tty/x11conn.h +++ b/lib/tty/x11conn.h @@ -1,12 +1,11 @@ - /** \file x11conn.h * \brief Header: X11 support * \warning This code uses setjmp() and longjmp(). Before you modify _anything_ here, * please read the relevant sections of the C standard. */ -#ifndef MC_X11CONN_H -#define MC_X11CONN_H +#ifndef MC__X11CONN_H +#define MC__X11CONN_H /* This module provides support for some X11 functions. The functions @@ -15,14 +14,25 @@ error or a connection error, all further traffic to the X server will be suppressed, and the functions will return reasonable default values. -*/ + */ #include +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + extern Display *mc_XOpenDisplay (const char *); extern int mc_XCloseDisplay (Display *); extern Bool mc_XQueryPointer (Display *, Window, Window *, Window *, int *, int *, int *, int *, unsigned int *); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/unixcompat.h b/lib/unixcompat.h index 9ad6e318e..8021c6048 100644 --- a/lib/unixcompat.h +++ b/lib/unixcompat.h @@ -1,4 +1,3 @@ - /** \file unixcompat.h * \brief Header: collects differences between the various Unix * @@ -12,29 +11,43 @@ #ifndef MC_UNIXCOMPAT_H #define MC_UNIXCOMPAT_H -#include /* BSD */ +#include /* BSD */ #ifdef HAVE_SYS_MKDEV_H -# include /* Solaris 9 */ +#include /* Solaris 9 */ #endif #if defined(_AIX) && defined(HAVE_SYS_SYSMACROS_H) -# include /* AIX */ +#include /* AIX */ #endif #if defined(_AIX) -# include /* AIX for tm */ +#include /* AIX for tm */ #endif +/*** typedefs(not structures) and defined constants **********************************************/ + #ifndef major -# warning major() is undefined. Device numbers will not be shown correctly. -# define major(devnum) (((devnum) >> 8) & 0xff) -#endif -#ifndef minor -# warning minor() is undefined. Device numbers will not be shown correctly. -# define minor(devnum) (((devnum) & 0xff)) -#endif -#ifndef makedev -# warning makedev() is undefined. Device numbers will not be shown correctly. -# define makedev(major,minor) ((((major) & 0xff) << 8) | ((minor) & 0xff)) +#warning major() is undefined. Device numbers will not be shown correctly. +#define major(devnum) (((devnum) >> 8) & 0xff) #endif +#ifndef minor +#warning minor() is undefined. Device numbers will not be shown correctly. +#define minor(devnum) (((devnum) & 0xff)) +#endif + +#ifndef makedev +#warning makedev() is undefined. Device numbers will not be shown correctly. +#define makedev(major,minor) ((((major) & 0xff) << 8) | ((minor) & 0xff)) +#endif + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ + #endif diff --git a/lib/util.c b/lib/util.c index d9bc91355..6ecbdf484 100644 --- a/lib/util.c +++ b/lib/util.c @@ -55,11 +55,33 @@ #include "src/main.h" /* eight_bit_clean */ #endif +/*** global variables ****************************************************************************/ + int easy_patterns = 1; char *user_recent_timeformat = NULL; /* time format string for recent dates */ char *user_old_timeformat = NULL; /* time format string for older dates */ +/*** file scope macro definitions ****************************************************************/ + +#define ismode(n,m) ((n & m) == m) + +/* Number of attempts to create a temporary file */ +#ifndef TMP_MAX +#define TMP_MAX 16384 +#endif /* !TMP_MAX */ + +#define TMP_SUFFIX ".tmp" + +#define ASCII_A (0x40 + 1) +#define ASCII_Z (0x40 + 26) +#define ASCII_a (0x60 + 1) +#define ASCII_z (0x60 + 26) + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + /* * Cache variable for the i18n_checktimelength function, * initially set to a clearly invalid value to show that @@ -67,6 +89,143 @@ char *user_old_timeformat = NULL; /* time format string for older dates */ */ static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1; +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +static inline int +is_7bit_printable (unsigned char c) +{ + return (c > 31 && c < 127); +} + +/* --------------------------------------------------------------------------------------------- */ + +static inline int +is_iso_printable (unsigned char c) +{ + return ((c > 31 && c < 127) || c >= 160); +} + +/* --------------------------------------------------------------------------------------------- */ + +static inline int +is_8bit_printable (unsigned char c) +{ + /* "Full 8 bits output" doesn't work on xterm */ + if (xterm_flag) + return is_iso_printable (c); + + return (c > 31 && c != 127 && c != 155); +} + +/* --------------------------------------------------------------------------------------------- */ + +static char * +resolve_symlinks (const char *path) +{ + char *buf, *buf2, *q, *r, c; + int len; + struct stat mybuf; + const char *p; + + if (*path != PATH_SEP) + return NULL; + r = buf = g_malloc (MC_MAXPATHLEN); + buf2 = g_malloc (MC_MAXPATHLEN); + *r++ = PATH_SEP; + *r = 0; + p = path; + for (;;) + { + q = strchr (p + 1, PATH_SEP); + if (!q) + { + q = strchr (p + 1, 0); + if (q == p + 1) + break; + } + c = *q; + *q = 0; + if (mc_lstat (path, &mybuf) < 0) + { + g_free (buf); + g_free (buf2); + *q = c; + return NULL; + } + if (!S_ISLNK (mybuf.st_mode)) + strcpy (r, p + 1); + else + { + len = mc_readlink (path, buf2, MC_MAXPATHLEN - 1); + if (len < 0) + { + g_free (buf); + g_free (buf2); + *q = c; + return NULL; + } + buf2[len] = 0; + if (*buf2 == PATH_SEP) + strcpy (buf, buf2); + else + strcpy (r, buf2); + } + canonicalize_pathname (buf); + r = strchr (buf, 0); + if (!*r || *(r - 1) != PATH_SEP) + { + *r++ = PATH_SEP; + *r = 0; + } + *q = c; + p = q; + if (!c) + break; + } + if (!*buf) + strcpy (buf, PATH_SEP_STR); + else if (*(r - 1) == PATH_SEP && r != buf + 1) + *(r - 1) = 0; + g_free (buf2); + return buf; +} + +/* --------------------------------------------------------------------------------------------- */ + +static gboolean +mc_util_write_backup_content (const char *from_file_name, const char *to_file_name) +{ + FILE *backup_fd; + char *contents; + gsize length; + gboolean ret1 = TRUE; + + if (!g_file_get_contents (from_file_name, &contents, &length, NULL)) + return FALSE; + + backup_fd = fopen (to_file_name, "w"); + if (backup_fd == NULL) + { + g_free (contents); + return FALSE; + } + + if (fwrite ((const void *) contents, 1, length, backup_fd) != length) + ret1 = FALSE; + { + int ret2; + ret2 = fflush (backup_fd); + ret2 = fclose (backup_fd); + } + g_free (contents); + return ret1; +} + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + extern void str_replace (char *s, char from, char to) { @@ -77,27 +236,7 @@ str_replace (char *s, char from, char to) } } -static inline int -is_7bit_printable (unsigned char c) -{ - return (c > 31 && c < 127); -} - -static inline int -is_iso_printable (unsigned char c) -{ - return ((c > 31 && c < 127) || c >= 160); -} - -static inline int -is_8bit_printable (unsigned char c) -{ - /* "Full 8 bits output" doesn't work on xterm */ - if (xterm_flag) - return is_iso_printable (c); - - return (c > 31 && c != 127 && c != 155); -} +/* --------------------------------------------------------------------------------------------- */ int is_printable (int c) @@ -121,7 +260,12 @@ is_printable (int c) #endif /* !HAVE_CHARSET */ } -/* Calculates the message dimensions (lines and columns) */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Calculates the message dimensions (lines and columns) + * + */ + void msglen (const char *text, int *lines, int *columns) { @@ -148,11 +292,13 @@ msglen (const char *text, int *lines, int *columns) *columns = ncolumns; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * 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) { @@ -185,7 +331,8 @@ trim (const char *s, char *d, int len) return d; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * 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. @@ -247,6 +394,8 @@ name_quote (const char *s, int quote_percent) return ret; } +/* --------------------------------------------------------------------------------------------- */ + char * fake_name_quote (const char *s, int quote_percent) { @@ -254,22 +403,26 @@ fake_name_quote (const char *s, int quote_percent) return g_strdup (s); } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * 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. */ + const char * name_trunc (const char *txt, size_t trunc_len) { return str_trunc (txt, trunc_len); } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * path_trunc() is the same as name_trunc() above but * it deletes possible password from path for security * reasons. */ + const char * path_trunc (const char *path, size_t trunc_len) { @@ -281,6 +434,8 @@ path_trunc (const char *path, size_t trunc_len) return ret; } +/* --------------------------------------------------------------------------------------------- */ + const char * size_trunc (double size, gboolean use_si) { @@ -302,6 +457,8 @@ size_trunc (double size, gboolean use_si) return x; } +/* --------------------------------------------------------------------------------------------- */ + const char * size_trunc_sep (double size, gboolean use_si) { @@ -331,7 +488,8 @@ size_trunc_sep (double size, gboolean use_si) return d; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Print file SIZE to BUFFER, but don't exceed LEN characters, * not including trailing 0. BUFFER should be at least LEN+1 long. * This function is called for every file on panels, so avoid @@ -340,6 +498,7 @@ size_trunc_sep (double size, gboolean use_si) * Units: size units (filesystem sizes are 1K blocks) * 0=bytes, 1=Kbytes, 2=Mbytes, etc. */ + void size_trunc_len (char *buffer, unsigned int len, off_t size, int units, gboolean use_si) { @@ -403,6 +562,8 @@ size_trunc_len (char *buffer, unsigned int len, off_t size, int units, gboolean } } +/* --------------------------------------------------------------------------------------------- */ + int is_exe (mode_t mode) { @@ -411,7 +572,7 @@ is_exe (mode_t mode) return 0; } -#define ismode(n,m) ((n & m) == m) +/* --------------------------------------------------------------------------------------------- */ const char * string_perm (mode_t mode_bits) @@ -468,15 +629,18 @@ string_perm (mode_t mode_bits) return mode; } -/* p: string which might contain an url with a password (this parameter is - modified in place). - has_prefix = 0: The first parameter is an url without a prefix - (user[:pass]@]machine[:port][remote-dir). Delete - the password. - has_prefix = 1: Search p for known url prefixes. If found delete - the password from the url. - Caveat: only the first url is found +/* --------------------------------------------------------------------------------------------- */ +/** + * p: string which might contain an url with a password (this parameter is + * modified in place). + * has_prefix = 0: The first parameter is an url without a prefix + * (user[:pass]@]machine[:port][remote-dir). Delete + * the password. + * has_prefix = 1: Search p for known url prefixes. If found delete + * the password from the url. + * Caveat: only the first url is found */ + char * strip_password (char *p, int has_prefix) { @@ -536,6 +700,8 @@ strip_password (char *p, int has_prefix) return (result); } +/* --------------------------------------------------------------------------------------------- */ + const char * strip_home_and_password (const char *dir) { @@ -558,6 +724,8 @@ strip_home_and_password (const char *dir) return newdir; } +/* --------------------------------------------------------------------------------------------- */ + const char * extension (const char *filename) { @@ -565,12 +733,16 @@ extension (const char *filename) return (d != NULL) ? d + 1 : ""; } +/* --------------------------------------------------------------------------------------------- */ + int exist_file (const char *name) { return access (name, R_OK) == 0; } +/* --------------------------------------------------------------------------------------------- */ + int check_for_default (const char *default_file, const char *file) { @@ -593,7 +765,7 @@ check_for_default (const char *default_file, const char *file) return 0; } - +/* --------------------------------------------------------------------------------------------- */ char * load_file (const char *filename) @@ -627,6 +799,8 @@ load_file (const char *filename) } } +/* --------------------------------------------------------------------------------------------- */ + char * load_mc_home_file (const char *from, const char *filename, char **allocated_filename) { @@ -670,8 +844,11 @@ load_mc_home_file (const char *from, const char *filename, char **allocated_file return data; } -/* Check strftime() results. Some systems (i.e. Solaris) have different - short-month and month name sizes for different locales */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Check strftime() results. Some systems (i.e. Solaris) have different + * short-month and month name sizes for different locales + */ size_t i18n_checktimelength (void) { @@ -695,10 +872,11 @@ i18n_checktimelength (void) lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10; /* Loop through all months to find out the longest one */ - for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++) { - strftime (buf, sizeof(buf) - 1, user_recent_timeformat, lt); + for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++) + { + strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt); length = max ((size_t) str_term_width1 (buf), length); - strftime (buf, sizeof(buf) - 1, user_old_timeformat, lt); + strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt); length = max ((size_t) str_term_width1 (buf), length); } @@ -715,6 +893,8 @@ i18n_checktimelength (void) return i18n_timelength_cache; } +/* --------------------------------------------------------------------------------------------- */ + const char * file_date (time_t when) { @@ -740,6 +920,8 @@ file_date (time_t when) return timebuf; } +/* --------------------------------------------------------------------------------------------- */ + const char * extract_line (const char *s, const char *top) { @@ -752,7 +934,11 @@ extract_line (const char *s, const char *top) return tmp_line; } -/* The basename routine */ +/* --------------------------------------------------------------------------------------------- */ +/** + * The basename routine + */ + const char * x_basename (const char *s) { @@ -760,6 +946,7 @@ x_basename (const char *s) return ((where = strrchr (s, PATH_SEP))) ? where + 1 : s; } +/* --------------------------------------------------------------------------------------------- */ const char * unix_error_string (int error_num) @@ -774,6 +961,8 @@ unix_error_string (int error_num) return buffer; } +/* --------------------------------------------------------------------------------------------- */ + const char * skip_separators (const char *s) { @@ -786,6 +975,8 @@ skip_separators (const char *s) return su; } +/* --------------------------------------------------------------------------------------------- */ + const char * skip_numbers (const char *s) { @@ -798,7 +989,9 @@ skip_numbers (const char *s) return su; } -/* Remove all control sequences from the argument string. We define +/* --------------------------------------------------------------------------------------------- */ +/** + * Remove all control sequences from the argument string. We define * "control sequence", in a sort of pidgin BNF, as follows: * * control-seq = Esc non-'[' @@ -883,6 +1076,8 @@ strip_ctrl_codes (char *s) return s; } +/* --------------------------------------------------------------------------------------------- */ + enum compression_type get_compression_type (int fd, const char *name) { @@ -961,6 +1156,8 @@ get_compression_type (int fd, const char *name) return COMPRESSION_NONE; } +/* --------------------------------------------------------------------------------------------- */ + const char * decompress_extension (int type) { @@ -982,7 +1179,9 @@ decompress_extension (int type) return 0; } +/* --------------------------------------------------------------------------------------------- */ /* Hooks */ + void add_hook (Hook ** hook_list, void (*hook_fn) (void *), void *data) { @@ -995,6 +1194,8 @@ add_hook (Hook ** hook_list, void (*hook_fn) (void *), void *data) *hook_list = new_hook; } +/* --------------------------------------------------------------------------------------------- */ + void execute_hooks (Hook * hook_list) { @@ -1026,6 +1227,8 @@ execute_hooks (Hook * hook_list) } } +/* --------------------------------------------------------------------------------------------- */ + void delete_hook (Hook ** hook_list, void (*hook_fn) (void *)) { @@ -1044,6 +1247,8 @@ delete_hook (Hook ** hook_list, void (*hook_fn) (void *)) *hook_list = new_list; } +/* --------------------------------------------------------------------------------------------- */ + int hook_present (Hook * hook_list, void (*hook_fn) (void *)) { @@ -1055,6 +1260,8 @@ hook_present (Hook * hook_list, void (*hook_fn) (void *)) return 0; } +/* --------------------------------------------------------------------------------------------- */ + void wipe_password (char *passwd) { @@ -1067,8 +1274,12 @@ wipe_password (char *passwd) g_free (passwd); } -/* Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key */ -/* Returns a newly allocated string */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key + * @returns a newly allocated string + */ + char * convert_controls (const char *p) { @@ -1114,108 +1325,12 @@ convert_controls (const char *p) return valcopy; } -static char * -resolve_symlinks (const char *path) -{ - char *buf, *buf2, *q, *r, c; - int len; - struct stat mybuf; - const char *p; +/* --------------------------------------------------------------------------------------------- */ +/** + * Finds out a relative path from first to second, i.e. goes as many .. + * as needed up in first and then goes down using second + */ - if (*path != PATH_SEP) - return NULL; - r = buf = g_malloc (MC_MAXPATHLEN); - buf2 = g_malloc (MC_MAXPATHLEN); - *r++ = PATH_SEP; - *r = 0; - p = path; - for (;;) - { - q = strchr (p + 1, PATH_SEP); - if (!q) - { - q = strchr (p + 1, 0); - if (q == p + 1) - break; - } - c = *q; - *q = 0; - if (mc_lstat (path, &mybuf) < 0) - { - g_free (buf); - g_free (buf2); - *q = c; - return NULL; - } - if (!S_ISLNK (mybuf.st_mode)) - strcpy (r, p + 1); - else - { - len = mc_readlink (path, buf2, MC_MAXPATHLEN - 1); - if (len < 0) - { - g_free (buf); - g_free (buf2); - *q = c; - return NULL; - } - buf2[len] = 0; - if (*buf2 == PATH_SEP) - strcpy (buf, buf2); - else - strcpy (r, buf2); - } - canonicalize_pathname (buf); - r = strchr (buf, 0); - if (!*r || *(r - 1) != PATH_SEP) - { - *r++ = PATH_SEP; - *r = 0; - } - *q = c; - p = q; - if (!c) - break; - } - if (!*buf) - strcpy (buf, PATH_SEP_STR); - else if (*(r - 1) == PATH_SEP && r != buf + 1) - *(r - 1) = 0; - g_free (buf2); - return buf; -} - -static gboolean -mc_util_write_backup_content (const char *from_file_name, const char *to_file_name) -{ - FILE *backup_fd; - char *contents; - gsize length; - gboolean ret1 = TRUE; - - if (!g_file_get_contents (from_file_name, &contents, &length, NULL)) - return FALSE; - - backup_fd = fopen (to_file_name, "w"); - if (backup_fd == NULL) - { - g_free (contents); - return FALSE; - } - - if (fwrite ((const void *) contents, 1, length, backup_fd) != length) - ret1 = FALSE; - { - int ret2; - ret2 = fflush (backup_fd); - ret2 = fclose (backup_fd); - } - g_free (contents); - return ret1; -} - -/* Finds out a relative path from first to second, i.e. goes as many .. - * as needed up in first and then goes down using second */ char * diff_two_paths (const char *first, const char *second) { @@ -1283,7 +1398,11 @@ diff_two_paths (const char *first, const char *second) return buf; } -/* If filename is NULL, then we just append PATH_SEP to the dir */ +/* --------------------------------------------------------------------------------------------- */ +/** + * If filename is NULL, then we just append PATH_SEP to the dir + */ + char * concat_dir_and_file (const char *dir, const char *file) { @@ -1295,7 +1414,11 @@ concat_dir_and_file (const char *dir, const char *file) return g_strconcat (dir, PATH_SEP_STR, file, (char *) NULL); } -/* Append text to GList, remove all entries with the same text */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Append text to GList, remove all entries with the same text + */ + GList * list_append_unique (GList * list, char *text) { @@ -1329,13 +1452,8 @@ list_append_unique (GList * list, char *text) return list; } +/* --------------------------------------------------------------------------------------------- */ /* Following code heavily borrows from libiberty, mkstemps.c */ - -/* Number of attempts to create a temporary file */ -#ifndef TMP_MAX -#define TMP_MAX 16384 -#endif /* !TMP_MAX */ - /* * Arguments: * pname (output) - pointer to the name of the temp file (needs g_free). @@ -1347,6 +1465,7 @@ list_append_unique (GList * list, char *text) * Result: * handle of the open file or -1 if couldn't open any. */ + int mc_mkstemps (char **pname, const char *prefix, const char *suffix) { @@ -1415,12 +1534,15 @@ mc_mkstemps (char **pname, const char *prefix, const char *suffix) return -1; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Read and restore position for the given filename. * If there is no stored data, return line 1 and col 0. */ + void -load_file_position (const char *filename, long *line, long *column, off_t * offset, GArray **bookmarks) +load_file_position (const char *filename, long *line, long *column, off_t * offset, + GArray ** bookmarks) { char *fn; FILE *f; @@ -1503,10 +1625,13 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs fclose (f); } -/* Save position for the given file */ -#define TMP_SUFFIX ".tmp" +/* --------------------------------------------------------------------------------------------- */ +/** + * Save position for the given file + */ + void -save_file_position (const char *filename, long line, long column, off_t offset, GArray *bookmarks) +save_file_position (const char *filename, long line, long column, off_t offset, GArray * bookmarks) { static size_t filepos_max_saved_entries = 0; char *fn, *tmp_fn; @@ -1556,7 +1681,8 @@ save_file_position (const char *filename, long line, long column, off_t offset, i = 1; while (fgets (buf, sizeof (buf), tmp_f) != NULL) { - if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && strchr (&buf[len + 1], ' ') == NULL) + if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 + && strchr (&buf[len + 1], ' ') == NULL) continue; fprintf (f, "%s", buf); @@ -1579,7 +1705,8 @@ save_file_position (const char *filename, long line, long column, off_t offset, g_array_free (bookmarks, TRUE); } -#undef TMP_SUFFIX +/* --------------------------------------------------------------------------------------------- */ + extern const char * cstrcasestr (const char *haystack, const char *needle) { @@ -1589,22 +1716,23 @@ cstrcasestr (const char *haystack, const char *needle) return result; } +/* --------------------------------------------------------------------------------------------- */ + const char * cstrstr (const char *haystack, const char *needle) { return strstr (haystack, needle); } +/* --------------------------------------------------------------------------------------------- */ + extern char * str_unconst (const char *s) { return (char *) s; } -#define ASCII_A (0x40 + 1) -#define ASCII_Z (0x40 + 26) -#define ASCII_a (0x60 + 1) -#define ASCII_z (0x60 + 26) +/* --------------------------------------------------------------------------------------------- */ extern int ascii_alpha_to_cntrl (int ch) @@ -1616,6 +1744,8 @@ ascii_alpha_to_cntrl (int ch) return ch; } +/* --------------------------------------------------------------------------------------------- */ + const char * Q_ (const char *s) { @@ -1626,6 +1756,7 @@ Q_ (const char *s) return (sep != NULL) ? sep + 1 : result; } +/* --------------------------------------------------------------------------------------------- */ gboolean mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix) @@ -1657,6 +1788,8 @@ mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffi return ret; } +/* --------------------------------------------------------------------------------------------- */ + gboolean mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix) { @@ -1673,6 +1806,8 @@ mc_util_restore_from_backup_if_possible (const char *file_name, const char *back return ret; } +/* --------------------------------------------------------------------------------------------- */ + gboolean mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix) { @@ -1689,8 +1824,12 @@ mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suf return TRUE; } -/* partly taken from dcigettext.c, returns "" for default locale */ -/* value should be freed by calling function g_free() */ +/* --------------------------------------------------------------------------------------------- */ +/** + * partly taken from dcigettext.c, returns "" for default locale + * value should be freed by calling function g_free() + */ + char * guess_message_value (void) { @@ -1722,3 +1861,5 @@ guess_message_value (void) return g_strdup (locale); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/util.h b/lib/util.h index a690963d6..4ff30a81b 100644 --- a/lib/util.h +++ b/lib/util.h @@ -1,4 +1,3 @@ - /** \file util.h * \brief Header: various utilities */ @@ -14,9 +13,119 @@ #include #include + +/*** typedefs(not structures) and defined constants **********************************************/ + +#define MAX_I18NTIMELENGTH 20 +#define MIN_I18NTIMELENGTH 10 +#define STD_I18NTIMELENGTH 12 + +#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 + +#define MAX_SAVED_BOOKMARKS 10 + +/* OS specific defines */ +#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 + +/* 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 + +/* usage: str_cmp ("foo", !=, "bar") */ +#define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0) + +#define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start)) + +#define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0) + +/*** enums ***************************************************************************************/ + +/* Matching */ +enum +{ + match_file, /* match a filename, use easy_patterns */ + match_normal, /* match pattern, use easy_patterns */ + match_regex /* match pattern, force using regex */ +}; + +/* 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_LZMA, + COMPRESSION_XZ +}; + +/*** structures declarations (and typedefs of structures)*****************************************/ + +typedef struct hook +{ + void (*hook_fn) (void *); + void *hook_data; + struct hook *next; +} Hook; + +/*** global variables defined in .c file *********************************************************/ + extern char *user_recent_timeformat; /* time format string for recent dates */ extern char *user_old_timeformat; /* time format string for older dates */ +extern int easy_patterns; + +extern struct sigaction startup_handler; + +/*** declarations of public functions ************************************************************/ + /* 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 @@ -111,9 +220,6 @@ void init_uid_gid_cache (void); char *get_group (int); char *get_owner (int); -#define MAX_I18NTIMELENGTH 20 -#define MIN_I18NTIMELENGTH 10 -#define STD_I18NTIMELENGTH 12 size_t i18n_checktimelength (void); const char *file_date (time_t); @@ -126,16 +232,6 @@ int check_for_default (const char *default_file, const char *file); /* Returns a copy of *s until a \n is found and is below top */ const char *extract_line (const char *s, const char *top); -/* Matching */ -enum -{ - match_file, /* match a filename, use easy_patterns */ - match_normal, /* match pattern, use easy_patterns */ - match_regex /* match pattern, force using regex */ -}; - -extern int easy_patterns; - /* Error pipes */ void open_error_pipe (void); void check_error_pipe (void); @@ -144,21 +240,10 @@ int close_error_pipe (int error, const char *text); /* Process spawning */ int my_system (int flags, const char *shell, const char *command); void save_stop_handler (void); -extern struct sigaction startup_handler; /* Tilde expansion */ char *tilde_expand (const char *); -/* 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; void custom_canonicalize_pathname (char *, CANON_PATH_FLAGS); void canonicalize_pathname (char *); @@ -170,38 +255,12 @@ int my_rmdir (const char *s); const char *mc_tmpdir (void); int mc_mkstemps (char **pname, const char *prefix, const char *suffix); -#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 - #ifdef HAVE_REALPATH #define mc_realpath realpath #else char *mc_realpath (const char *path, char *resolved_path); #endif -enum compression_type -{ - COMPRESSION_NONE, - COMPRESSION_GZIP, - COMPRESSION_BZIP, - COMPRESSION_BZIP2, - COMPRESSION_LZMA, - COMPRESSION_XZ -}; - /* 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 *); @@ -209,12 +268,6 @@ const char *decompress_extension (int type); /* 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); @@ -224,7 +277,6 @@ int hook_present (Hook * hook_list, void (*hook_fn) (void *)); GList *list_append_unique (GList * list, char *text); /* Position saving and restoring */ -#define MAX_SAVED_BOOKMARKS 10 /* Load position for the given filename */ void load_file_position (const char *filename, long *line, long *column, off_t * offset, GArray **bookmarks); @@ -233,39 +285,6 @@ void save_file_position (const char *filename, long line, long column, off_t off GArray *bookmarks); -/* OS specific defines */ -#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 - -/* 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 - -/* usage: str_cmp ("foo", !=, "bar") */ -#define str_cmp(a,rel,b) (strcmp ((a), (b)) rel 0) - /* if ch is in [A-Za-z], returns the corresponding control character, * else returns the argument. */ extern int ascii_alpha_to_cntrl (int ch); @@ -273,7 +292,14 @@ extern int ascii_alpha_to_cntrl (int ch); #undef Q_ const char *Q_ (const char *s); -#define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start)) +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); + + +/*** inline functions **************************************************/ /* * strcpy is unsafe on overlapping memory areas, so define memmove-alike @@ -298,12 +324,5 @@ str_move (char *dest, const char *src) return (char *) memmove (dest, src, n); } -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); - -#define MC_PTR_FREE(ptr) do { g_free (ptr); (ptr) = NULL; } while (0) #endif /* MC_UTIL_H */ diff --git a/lib/utilunix.c b/lib/utilunix.c index 806ad70a5..f3992f6fa 100644 --- a/lib/utilunix.c +++ b/lib/utilunix.c @@ -42,7 +42,10 @@ #include #include #ifdef HAVE_SYS_IOCTL_H -# include +#include +#endif +#ifdef HAVE_GET_PROCESS_STATS +#include #endif #include #include @@ -57,20 +60,38 @@ #include "src/charsets.h" #endif +/*** global variables ****************************************************************************/ + struct sigaction startup_handler; +/*** file scope macro definitions ****************************************************************/ + #define UID_CACHE_SIZE 200 #define GID_CACHE_SIZE 30 +/* Pipes are guaranteed to be able to hold at least 4096 bytes */ +/* More than that would be unportable */ +#define MAX_PIPE_SIZE 4096 + +/*** file scope type declarations ****************************************************************/ + typedef struct { int index; char *string; } int_cache; +/*** file scope variables ************************************************************************/ + static int_cache uid_cache[UID_CACHE_SIZE]; static int_cache gid_cache[GID_CACHE_SIZE]; +static int error_pipe[2]; /* File descriptors of error pipe */ +static int old_error; /* File descriptor of old standard error */ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + static char * i_cache_match (int id, int_cache * cache, int size) { @@ -82,6 +103,8 @@ i_cache_match (int id, int_cache * cache, int size) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static void i_cache_add (int id, int_cache * cache, int size, char *text, int *last) { @@ -91,6 +114,10 @@ i_cache_add (int id, int_cache * cache, int size, char *text, int *last) *last = ((*last) + 1) % size; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + char * get_owner (int uid) { @@ -116,6 +143,8 @@ get_owner (int uid) } } +/* --------------------------------------------------------------------------------------------- */ + char * get_group (int gid) { @@ -141,15 +170,19 @@ get_group (int gid) } } +/* --------------------------------------------------------------------------------------------- */ /* Since ncurses uses a handler that automatically refreshes the */ /* screen after a SIGCONT, and we don't want this behavior when */ /* spawning a child, we save the original handler here */ + void save_stop_handler (void) { sigaction (SIGTSTP, NULL, &startup_handler); } +/* --------------------------------------------------------------------------------------------- */ + int my_system (int flags, const char *shell, const char *command) { @@ -213,7 +246,7 @@ my_system (int flags, const char *shell, const char *command) { if (waitpid (pid, &status, 0) > 0) { - status = WEXITSTATUS(status); + status = WEXITSTATUS (status); break; } if (errno != EINTR) @@ -231,10 +264,12 @@ my_system (int flags, const char *shell, const char *command) } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Perform tilde expansion if possible. * Always return a newly allocated string, even if it's unchanged. */ + char * tilde_expand (const char *directory) { @@ -276,13 +311,15 @@ tilde_expand (const char *directory) return g_strconcat (passwd->pw_dir, PATH_SEP_STR, q, (char *) NULL); } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Return the directory where mc should keep its temporary files. * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER" * When called the first time, the directory is created if needed. * The first call should be done early, since we are using fprintf() * and not message() to report possible problems. */ + const char * mc_tmpdir (void) { @@ -384,17 +421,13 @@ mc_tmpdir (void) return tmpdir; } +/* --------------------------------------------------------------------------------------------- */ +/** + * Creates a pipe to hold standard error for a later analysis. + * The pipe can hold 4096 bytes. Make sure no more is written + * or a deadlock might occur. + */ -/* Pipes are guaranteed to be able to hold at least 4096 bytes */ -/* More than that would be unportable */ -#define MAX_PIPE_SIZE 4096 - -static int error_pipe[2]; /* File descriptors of error pipe */ -static int old_error; /* File descriptor of old standard error */ - -/* Creates a pipe to hold standard error for a later analysis. */ -/* The pipe can hold 4096 bytes. Make sure no more is written */ -/* or a deadlock might occur. */ void open_error_pipe (void) { @@ -436,11 +469,13 @@ open_error_pipe (void) error_pipe[1] = -1; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Returns true if an error was displayed * error: -1 - ignore errors, 0 - display warning, 1 - display error * text is prepended to the error message from the pipe */ + int close_error_pipe (int error, const char *text) { @@ -489,7 +524,8 @@ close_error_pipe (int error, const char *text) return 1; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Canonicalize path, and return a new path. Do everything in place. * The new path differs from path in: * Multiple `/'s are collapsed to a single `/'. @@ -499,6 +535,7 @@ close_error_pipe (int error, const char *text) * portions of the path. * Well formed UNC paths are modified only in the local part. */ + void custom_canonicalize_pathname (char *path, CANON_PATH_FLAGS flags) { @@ -662,7 +699,7 @@ custom_canonicalize_pathname (char *path, CANON_PATH_FLAGS flags) s[0] = 0; #if HAVE_CHARSET else if ((strncmp (s, VFS_ENCODING_PREFIX, enc_prefix_len) == 0) - && (is_supported_encoding (s + enc_prefix_len))) + && (is_supported_encoding (s + enc_prefix_len))) { /* special case: remove encoding */ s[0] = '.'; @@ -689,15 +726,17 @@ custom_canonicalize_pathname (char *path, CANON_PATH_FLAGS flags) } } +/* --------------------------------------------------------------------------------------------- */ + void canonicalize_pathname (char *path) { custom_canonicalize_pathname (path, CANON_PATH_ALL); } -#ifdef HAVE_GET_PROCESS_STATS -# include +/* --------------------------------------------------------------------------------------------- */ +#ifdef HAVE_GET_PROCESS_STATS int gettimeofday (struct timeval *tp, void *tzp) { @@ -705,6 +744,8 @@ gettimeofday (struct timeval *tp, void *tzp) } #endif /* HAVE_GET_PROCESS_STATS */ +/* --------------------------------------------------------------------------------------------- */ + #ifndef HAVE_REALPATH char * mc_realpath (const char *path, char *resolved_path) @@ -847,7 +888,12 @@ mc_realpath (const char *path, char *resolved_path) } #endif /* HAVE_REALPATH */ -/* Return the index of the permissions triplet */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Return the index of the permissions triplet + * + */ + int get_user_permissions (struct stat *st) { @@ -894,3 +940,5 @@ get_user_permissions (struct stat *st) return 2; } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/cpio.c b/lib/vfs/mc-vfs/cpio.c index f1ee87d1c..a05ac21b9 100644 --- a/lib/vfs/mc-vfs/cpio.c +++ b/lib/vfs/mc-vfs/cpio.c @@ -1,7 +1,7 @@ /* Virtual File System: GNU Tar file system. Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. - + Written by: 2000 Jan Hudec This program is free software; you can redistribute it and/or @@ -39,73 +39,9 @@ #include "xdirentry.h" #include "gc.h" /* vfs_rmstamp */ -enum { - STATUS_START, - STATUS_OK, - STATUS_TRAIL, - STATUS_FAIL, - STATUS_EOF -}; +/*** global variables ****************************************************************************/ -enum { - CPIO_UNKNOWN = 0, /* Not determined yet */ - CPIO_BIN, /* Binary format */ - CPIO_BINRE, /* Binary format, reverse endianity */ - CPIO_OLDC, /* Old ASCII format */ - CPIO_NEWC, /* New ASCII format */ - CPIO_CRC /* New ASCII format + CRC */ -}; - -static struct vfs_class vfs_cpiofs_ops; - -struct old_cpio_header -{ - unsigned short c_magic; - short c_dev; - unsigned short c_ino; - unsigned short c_mode; - unsigned short c_uid; - unsigned short c_gid; - unsigned short c_nlink; - short c_rdev; - unsigned short c_mtimes[2]; - unsigned short c_namesize; - unsigned short c_filesizes[2]; -}; - -struct new_cpio_header -{ - unsigned short c_magic; - unsigned long c_ino; - unsigned long c_mode; - unsigned long c_uid; - unsigned long c_gid; - unsigned long c_nlink; - unsigned long c_mtime; - unsigned long c_filesize; - long c_dev; - long c_devmin; - long c_rdev; - long c_rdevmin; - unsigned long c_namesize; - unsigned long c_chksum; -}; - -struct defer_inode { - struct defer_inode *next; - unsigned long inumber; - unsigned short device; - struct vfs_s_inode *inode; -}; - -/* FIXME: should be off_t instead of int. */ -static int cpio_position; - -static int cpio_find_head(struct vfs_class *me, struct vfs_s_super *super); -static ssize_t cpio_read_bin_head(struct vfs_class *me, struct vfs_s_super *super); -static ssize_t cpio_read_oldc_head(struct vfs_class *me, struct vfs_s_super *super); -static ssize_t cpio_read_crc_head(struct vfs_class *me, struct vfs_s_super *super); -static ssize_t cpio_read(void *fh, char *buffer, size_t count); +/*** file scope macro definitions ****************************************************************/ #define CPIO_POS(super) cpio_position /* If some time reentrancy should be needed change it to */ @@ -114,84 +50,183 @@ static ssize_t cpio_read(void *fh, char *buffer, size_t count); #define CPIO_SEEK_SET(super, where) mc_lseek((super)->u.arch.fd, CPIO_POS(super) = (where), SEEK_SET) #define CPIO_SEEK_CUR(super, where) mc_lseek((super)->u.arch.fd, CPIO_POS(super) += (where), SEEK_SET) +#define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */ +#define SEEKBACK CPIO_SEEK_CUR(super, ptr - top) +#define RETURN(x) return(super->u.arch.type = (x)) +#define TYPEIS(x) ((super->u.arch.type == CPIO_UNKNOWN) || (super->u.arch.type == (x))) + +#define HEAD_LENGTH (26) + +/*** file scope type declarations ****************************************************************/ + +enum +{ + STATUS_START, + STATUS_OK, + STATUS_TRAIL, + STATUS_FAIL, + STATUS_EOF +}; + +enum +{ + CPIO_UNKNOWN = 0, /* Not determined yet */ + CPIO_BIN, /* Binary format */ + CPIO_BINRE, /* Binary format, reverse endianity */ + CPIO_OLDC, /* Old ASCII format */ + CPIO_NEWC, /* New ASCII format */ + CPIO_CRC /* New ASCII format + CRC */ +}; + +struct old_cpio_header +{ + unsigned short c_magic; + short c_dev; + unsigned short c_ino; + unsigned short c_mode; + unsigned short c_uid; + unsigned short c_gid; + unsigned short c_nlink; + short c_rdev; + unsigned short c_mtimes[2]; + unsigned short c_namesize; + unsigned short c_filesizes[2]; +}; + +struct new_cpio_header +{ + unsigned short c_magic; + unsigned long c_ino; + unsigned long c_mode; + unsigned long c_uid; + unsigned long c_gid; + unsigned long c_nlink; + unsigned long c_mtime; + unsigned long c_filesize; + long c_dev; + long c_devmin; + long c_rdev; + long c_rdevmin; + unsigned long c_namesize; + unsigned long c_chksum; +}; + +struct defer_inode +{ + struct defer_inode *next; + unsigned long inumber; + unsigned short device; + struct vfs_s_inode *inode; +}; + +/*** file scope variables ************************************************************************/ + +static struct vfs_class vfs_cpiofs_ops; + +/* FIXME: should be off_t instead of int. */ +static int cpio_position; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +static int cpio_find_head (struct vfs_class *me, struct vfs_s_super *super); +static ssize_t cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super); +static ssize_t cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super); +static ssize_t cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super); +static ssize_t cpio_read (void *fh, char *buffer, size_t count); + +/* --------------------------------------------------------------------------------------------- */ + static struct defer_inode * cpio_defer_find (struct defer_inode *l, struct defer_inode *i) { while (l && (l->inumber != i->inumber || l->device != i->device)) - l = l->next; + l = l->next; return l; } -static int cpio_skip_padding(struct vfs_s_super *super) +/* --------------------------------------------------------------------------------------------- */ + +static int +cpio_skip_padding (struct vfs_s_super *super) { - switch(super->u.arch.type) { + switch (super->u.arch.type) + { case CPIO_BIN: case CPIO_BINRE: - return CPIO_SEEK_CUR(super, (2 - (CPIO_POS(super) % 2)) % 2); + return CPIO_SEEK_CUR (super, (2 - (CPIO_POS (super) % 2)) % 2); case CPIO_NEWC: case CPIO_CRC: - return CPIO_SEEK_CUR(super, (4 - (CPIO_POS(super) % 4)) % 4); + return CPIO_SEEK_CUR (super, (4 - (CPIO_POS (super) % 4)) % 4); case CPIO_OLDC: - return CPIO_POS(super); + return CPIO_POS (super); default: - g_assert_not_reached(); - return 42; /* & the compiler is happy :-) */ + g_assert_not_reached (); + return 42; /* & the compiler is happy :-) */ } } -static void cpio_free_archive(struct vfs_class *me, struct vfs_s_super *super) +/* --------------------------------------------------------------------------------------------- */ + +static void +cpio_free_archive (struct vfs_class *me, struct vfs_s_super *super) { struct defer_inode *l, *lnext; (void) me; - if(super->u.arch.fd != -1) - mc_close(super->u.arch.fd); - super->u.arch.fd = -1; - for (l = super->u.arch.deferred; l; l = lnext) { - lnext = l->next; - g_free (l); + if (super->u.arch.fd != -1) + mc_close (super->u.arch.fd); + super->u.arch.fd = -1; + for (l = super->u.arch.deferred; l; l = lnext) + { + lnext = l->next; + g_free (l); } super->u.arch.deferred = NULL; } +/* --------------------------------------------------------------------------------------------- */ + static int -cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, - const char *name) +cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const char *name) { int fd, type; mode_t mode; struct vfs_s_inode *root; fd = mc_open (name, O_RDONLY); - if (fd == -1) { - message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), name); - return -1; + if (fd == -1) + { + message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), name); + return -1; } super->name = g_strdup (name); - super->u.arch.fd = -1; /* for now */ + super->u.arch.fd = -1; /* for now */ mc_stat (name, &(super->u.arch.st)); super->u.arch.type = CPIO_UNKNOWN; type = get_compression_type (fd, name); - if (type != COMPRESSION_NONE) { - char *s; + if (type != COMPRESSION_NONE) + { + char *s; - mc_close (fd); - s = g_strconcat (name, decompress_extension (type), (char *) NULL); - fd = mc_open (s, O_RDONLY); - if (fd == -1) { - message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s); - g_free (s); - return -1; - } - g_free (s); + mc_close (fd); + s = g_strconcat (name, decompress_extension (type), (char *) NULL); + fd = mc_open (s, O_RDONLY); + if (fd == -1) + { + message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s); + g_free (s); + return -1; + } + g_free (s); } super->u.arch.fd = fd; mode = super->u.arch.st.st_mode & 07777; - mode |= (mode & 0444) >> 2; /* set eXec where Read is */ + mode |= (mode & 0444) >> 2; /* set eXec where Read is */ mode |= S_IFDIR; root = vfs_s_new_inode (me, super, &(super->u.arch.st)); @@ -207,30 +242,33 @@ cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, return fd; } -static ssize_t cpio_read_head(struct vfs_class *me, struct vfs_s_super *super) +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +cpio_read_head (struct vfs_class *me, struct vfs_s_super *super) { - switch(cpio_find_head(me, super)) { + switch (cpio_find_head (me, super)) + { case CPIO_UNKNOWN: - return -1; + return -1; case CPIO_BIN: case CPIO_BINRE: - return cpio_read_bin_head(me, super); + return cpio_read_bin_head (me, super); case CPIO_OLDC: - return cpio_read_oldc_head(me, super); + return cpio_read_oldc_head (me, super); case CPIO_NEWC: case CPIO_CRC: - return cpio_read_crc_head(me, super); + return cpio_read_crc_head (me, super); default: - g_assert_not_reached(); - return 42; /* & the compiler is happy :-) */ + g_assert_not_reached (); + return 42; /* & the compiler is happy :-) */ } } -#define MAGIC_LENGTH (6) /* How many bytes we have to read ahead */ -#define SEEKBACK CPIO_SEEK_CUR(super, ptr - top) -#define RETURN(x) return(super->u.arch.type = (x)) -#define TYPEIS(x) ((super->u.arch.type == CPIO_UNKNOWN) || (super->u.arch.type == (x))) -static int cpio_find_head(struct vfs_class *me, struct vfs_s_super *super) +/* --------------------------------------------------------------------------------------------- */ + +static int +cpio_find_head (struct vfs_class *me, struct vfs_s_super *super) { char buf[256]; int ptr = 0; @@ -239,49 +277,68 @@ static int cpio_find_head(struct vfs_class *me, struct vfs_s_super *super) top = mc_read (super->u.arch.fd, buf, 256); if (top > 0) - CPIO_POS (super) += top; - for(;;) { - if(ptr + MAGIC_LENGTH >= top) { - if(top > 128) { - memmove(buf, buf + top - 128, 128); - ptr -= top - 128; - top = 128; - } - tmp = mc_read (super->u.arch.fd, buf, top); - if (tmp == 0 || tmp == -1) { - message (D_ERROR, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name); - cpio_free_archive(me, super); - return CPIO_UNKNOWN; - } - top += tmp; - } - if(TYPEIS(CPIO_BIN) && ((*(unsigned short *)(buf + ptr)) == 070707)) { - SEEKBACK; RETURN(CPIO_BIN); - } else if(TYPEIS(CPIO_BINRE) && ((*(unsigned short *)(buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT(070707))) { - SEEKBACK; RETURN(CPIO_BINRE); - } else if(TYPEIS(CPIO_OLDC) && (!strncmp(buf + ptr, "070707", 6))) { - SEEKBACK; RETURN(CPIO_OLDC); - } else if(TYPEIS(CPIO_NEWC) && (!strncmp(buf + ptr, "070701", 6))) { - SEEKBACK; RETURN(CPIO_NEWC); - } else if(TYPEIS(CPIO_CRC) && (!strncmp(buf + ptr, "070702", 6))) { - SEEKBACK; RETURN(CPIO_CRC); - }; - ptr++; + CPIO_POS (super) += top; + for (;;) + { + if (ptr + MAGIC_LENGTH >= top) + { + if (top > 128) + { + memmove (buf, buf + top - 128, 128); + ptr -= top - 128; + top = 128; + } + tmp = mc_read (super->u.arch.fd, buf, top); + if (tmp == 0 || tmp == -1) + { + message (D_ERROR, MSG_ERROR, _("Premature end of cpio archive\n%s"), super->name); + cpio_free_archive (me, super); + return CPIO_UNKNOWN; + } + top += tmp; + } + if (TYPEIS (CPIO_BIN) && ((*(unsigned short *) (buf + ptr)) == 070707)) + { + SEEKBACK; + RETURN (CPIO_BIN); + } + else if (TYPEIS (CPIO_BINRE) + && ((*(unsigned short *) (buf + ptr)) == GUINT16_SWAP_LE_BE_CONSTANT (070707))) + { + SEEKBACK; + RETURN (CPIO_BINRE); + } + else if (TYPEIS (CPIO_OLDC) && (!strncmp (buf + ptr, "070707", 6))) + { + SEEKBACK; + RETURN (CPIO_OLDC); + } + else if (TYPEIS (CPIO_NEWC) && (!strncmp (buf + ptr, "070701", 6))) + { + SEEKBACK; + RETURN (CPIO_NEWC); + } + else if (TYPEIS (CPIO_CRC) && (!strncmp (buf + ptr, "070702", 6))) + { + SEEKBACK; + RETURN (CPIO_CRC); + }; + ptr++; } } -#undef RETURN -#undef SEEKBACK + +/* --------------------------------------------------------------------------------------------- */ static int -cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, - struct stat *st, char *name) +cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, struct stat *st, char *name) { struct vfs_s_inode *inode = NULL; struct vfs_s_inode *root = super->root; struct vfs_s_entry *entry = NULL; char *tn; - switch (st->st_mode & S_IFMT) { /* For case of HP/UX archives */ + switch (st->st_mode & S_IFMT) + { /* For case of HP/UX archives */ case S_IFCHR: case S_IFBLK: #ifdef S_IFSOCK @@ -293,162 +350,181 @@ cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, #ifdef S_IFNAM case S_IFNAM: #endif - if ((st->st_size != 0) && (st->st_rdev == 0x0001)) { - /* FIXME: representation of major/minor differs between */ - /* different operating systems. */ - st->st_rdev = (unsigned) st->st_size; - st->st_size = 0; - } - break; + if ((st->st_size != 0) && (st->st_rdev == 0x0001)) + { + /* FIXME: representation of major/minor differs between */ + /* different operating systems. */ + st->st_rdev = (unsigned) st->st_size; + st->st_size = 0; + } + break; default: - break; + break; } if ((st->st_nlink > 1) - && ((super->u.arch.type == CPIO_NEWC) - || (super->u.arch.type == CPIO_CRC))) { /* For case of hardlinked files */ - struct defer_inode i, *l; - i.inumber = st->st_ino; - i.device = st->st_dev; - i.inode = NULL; + && ((super->u.arch.type == CPIO_NEWC) || (super->u.arch.type == CPIO_CRC))) + { /* For case of hardlinked files */ + struct defer_inode i, *l; + i.inumber = st->st_ino; + i.device = st->st_dev; + i.inode = NULL; - l = cpio_defer_find (super->u.arch.deferred, &i); - if (l != NULL) { - inode = l->inode; - if (inode->st.st_size != 0 && st->st_size != 0 - && (inode->st.st_size != st->st_size)) { - message (D_ERROR, MSG_ERROR, - _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"), - name, super->name); - inode = NULL; - } else if (inode->st.st_size == 0) - inode->st.st_size = st->st_size; - } + l = cpio_defer_find (super->u.arch.deferred, &i); + if (l != NULL) + { + inode = l->inode; + if (inode->st.st_size != 0 && st->st_size != 0 && (inode->st.st_size != st->st_size)) + { + message (D_ERROR, MSG_ERROR, + _("Inconsistent hardlinks of\n%s\nin cpio archive\n%s"), + name, super->name); + inode = NULL; + } + else if (inode->st.st_size == 0) + inode->st.st_size = st->st_size; + } } /* remove trailing slashes */ for (tn = name + strlen (name) - 1; tn >= name && *tn == PATH_SEP; tn--) - *tn = '\0'; + *tn = '\0'; tn = strrchr (name, PATH_SEP); if (tn == NULL) - tn = name; - else if (tn == name + 1) { - /* started with "./" -- directory in the root of archive */ - tn++; - } else { - *tn = '\0'; - root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR); - *tn = PATH_SEP; - tn++; + tn = name; + else if (tn == name + 1) + { + /* started with "./" -- directory in the root of archive */ + tn++; + } + else + { + *tn = '\0'; + root = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_MKDIR); + *tn = PATH_SEP; + tn++; } - entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */ + entry = MEDATA->find_entry (me, root, tn, LINK_FOLLOW, FL_NONE); /* In case entry is already there */ - if (entry != NULL) { - /* This shouldn't happen! (well, it can happen if there is a record for a - file and than a record for a directory it is in; cpio would die with - 'No such file or directory' is such case) */ + if (entry != NULL) + { + /* This shouldn't happen! (well, it can happen if there is a record for a + file and than a record for a directory it is in; cpio would die with + 'No such file or directory' is such case) */ - if (!S_ISDIR (entry->ino->st.st_mode)) { - /* This can be considered archive inconsistency */ - message (D_ERROR, MSG_ERROR, - _("%s contains duplicate entries! Skipping!"), - super->name); - } else { - entry->ino->st.st_mode = st->st_mode; - entry->ino->st.st_uid = st->st_uid; - entry->ino->st.st_gid = st->st_gid; - entry->ino->st.st_atime = st->st_atime; - entry->ino->st.st_mtime = st->st_mtime; - entry->ino->st.st_ctime = st->st_ctime; - } + if (!S_ISDIR (entry->ino->st.st_mode)) + { + /* This can be considered archive inconsistency */ + message (D_ERROR, MSG_ERROR, + _("%s contains duplicate entries! Skipping!"), super->name); + } + else + { + entry->ino->st.st_mode = st->st_mode; + entry->ino->st.st_uid = st->st_uid; + entry->ino->st.st_gid = st->st_gid; + entry->ino->st.st_atime = st->st_atime; + entry->ino->st.st_mtime = st->st_mtime; + entry->ino->st.st_ctime = st->st_ctime; + } - g_free (name); - } else { /* !entry */ - if (inode == NULL) { - inode = vfs_s_new_inode (me, super, st); - if ((st->st_nlink > 0) - && ((super->u.arch.type == CPIO_NEWC) - || (super->u.arch.type == CPIO_CRC))) { - /* For case of hardlinked files */ - struct defer_inode *i; - i = g_new (struct defer_inode, 1); - i->inumber = st->st_ino; - i->device = st->st_dev; - i->inode = inode; - i->next = super->u.arch.deferred; - super->u.arch.deferred = i; - } - } + g_free (name); + } + else + { /* !entry */ + if (inode == NULL) + { + inode = vfs_s_new_inode (me, super, st); + if ((st->st_nlink > 0) + && ((super->u.arch.type == CPIO_NEWC) || (super->u.arch.type == CPIO_CRC))) + { + /* For case of hardlinked files */ + struct defer_inode *i; + i = g_new (struct defer_inode, 1); + i->inumber = st->st_ino; + i->device = st->st_dev; + i->inode = inode; + i->next = super->u.arch.deferred; + super->u.arch.deferred = i; + } + } - if (st->st_size != 0) - inode->data_offset = CPIO_POS (super); + if (st->st_size != 0) + inode->data_offset = CPIO_POS (super); - entry = vfs_s_new_entry (me, tn, inode); - vfs_s_insert_entry (me, root, entry); + entry = vfs_s_new_entry (me, tn, inode); + vfs_s_insert_entry (me, root, entry); - g_free (name); + g_free (name); - if (!S_ISLNK (st->st_mode)) - CPIO_SEEK_CUR (super, st->st_size); - else { - inode->linkname = g_malloc (st->st_size + 1); + if (!S_ISLNK (st->st_mode)) + CPIO_SEEK_CUR (super, st->st_size); + else + { + inode->linkname = g_malloc (st->st_size + 1); - if (mc_read (super->u.arch.fd, inode->linkname, st->st_size) < st->st_size) { - inode->linkname[0] = '\0'; - return STATUS_EOF; - } + if (mc_read (super->u.arch.fd, inode->linkname, st->st_size) < st->st_size) + { + inode->linkname[0] = '\0'; + return STATUS_EOF; + } - inode->linkname[st->st_size] = '\0'; /* Linkname stored without terminating \0 !!! */ - CPIO_POS (super) += st->st_size; - cpio_skip_padding (super); - } - } /* !entry */ + inode->linkname[st->st_size] = '\0'; /* Linkname stored without terminating \0 !!! */ + CPIO_POS (super) += st->st_size; + cpio_skip_padding (super); + } + } /* !entry */ return STATUS_OK; } -#define HEAD_LENGTH (26) -static ssize_t cpio_read_bin_head(struct vfs_class *me, struct vfs_s_super *super) +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super) { - union { - struct old_cpio_header buf; - short shorts[HEAD_LENGTH >> 1]; + union + { + struct old_cpio_header buf; + short shorts[HEAD_LENGTH >> 1]; } u; ssize_t len; char *name; struct stat st; - len = mc_read (super->u.arch.fd, (char *)&u.buf, HEAD_LENGTH); + len = mc_read (super->u.arch.fd, (char *) &u.buf, HEAD_LENGTH); if (len < HEAD_LENGTH) - return STATUS_EOF; - CPIO_POS(super) += len; - if(super->u.arch.type == CPIO_BINRE) { - int i; - for(i = 0; i < (HEAD_LENGTH >> 1); i++) - u.shorts[i] = GUINT16_SWAP_LE_BE_CONSTANT(u.shorts[i]); + return STATUS_EOF; + CPIO_POS (super) += len; + if (super->u.arch.type == CPIO_BINRE) + { + int i; + for (i = 0; i < (HEAD_LENGTH >> 1); i++) + u.shorts[i] = GUINT16_SWAP_LE_BE_CONSTANT (u.shorts[i]); } - if (u.buf.c_magic != 070707 || - u.buf.c_namesize == 0 || u.buf.c_namesize > MC_MAXPATHLEN) { - message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), - super->name); - return STATUS_FAIL; + if (u.buf.c_magic != 070707 || u.buf.c_namesize == 0 || u.buf.c_namesize > MC_MAXPATHLEN) + { + message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name); + return STATUS_FAIL; } - name = g_malloc(u.buf.c_namesize); + name = g_malloc (u.buf.c_namesize); len = mc_read (super->u.arch.fd, name, u.buf.c_namesize); - if (len < u.buf.c_namesize) { - g_free(name); - return STATUS_EOF; + if (len < u.buf.c_namesize) + { + g_free (name); + return STATUS_EOF; } name[u.buf.c_namesize - 1] = '\0'; - CPIO_POS(super) += len; - cpio_skip_padding(super); + CPIO_POS (super) += len; + cpio_skip_padding (super); - if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */ - g_free(name); - return STATUS_TRAIL; + if (!strcmp ("TRAILER!!!", name)) + { /* We got to the last record */ + g_free (name); + return STATUS_TRAIL; } st.st_dev = u.buf.c_dev; @@ -461,53 +537,60 @@ static ssize_t cpio_read_bin_head(struct vfs_class *me, struct vfs_s_super *supe st.st_size = (u.buf.c_filesizes[0] << 16) | u.buf.c_filesizes[1]; st.st_atime = st.st_mtime = st.st_ctime = (u.buf.c_mtimes[0] << 16) | u.buf.c_mtimes[1]; - return cpio_create_entry(me, super, &st, name); + return cpio_create_entry (me, super, &st, name); } -#undef HEAD_LENGTH +/* --------------------------------------------------------------------------------------------- */ + +#undef HEAD_LENGTH #define HEAD_LENGTH (76) -static ssize_t cpio_read_oldc_head(struct vfs_class *me, struct vfs_s_super *super) + +static ssize_t +cpio_read_oldc_head (struct vfs_class *me, struct vfs_s_super *super) { struct new_cpio_header hd; - union { - struct stat st; - char buf[HEAD_LENGTH + 1]; + union + { + struct stat st; + char buf[HEAD_LENGTH + 1]; } u; ssize_t len; char *name; if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH) - return STATUS_EOF; + return STATUS_EOF; CPIO_POS (super) += HEAD_LENGTH; u.buf[HEAD_LENGTH] = 0; if (sscanf (u.buf, "070707%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo", - (unsigned long *)&hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid, - &hd.c_nlink, (unsigned long *)&hd.c_rdev, &hd.c_mtime, - &hd.c_namesize, &hd.c_filesize) < 10) { - message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), - super->name); - return STATUS_FAIL; + (unsigned long *) &hd.c_dev, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid, + &hd.c_nlink, (unsigned long *) &hd.c_rdev, &hd.c_mtime, + &hd.c_namesize, &hd.c_filesize) < 10) + { + message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name); + return STATUS_FAIL; } - if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) { - message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), - super->name); - return STATUS_FAIL; + if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) + { + message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name); + return STATUS_FAIL; } - name = g_malloc(hd.c_namesize); + name = g_malloc (hd.c_namesize); len = mc_read (super->u.arch.fd, name, hd.c_namesize); - if ((len == -1) || ((unsigned long) len < hd.c_namesize)) { - g_free (name); - return STATUS_EOF; + if ((len == -1) || ((unsigned long) len < hd.c_namesize)) + { + g_free (name); + return STATUS_EOF; } name[hd.c_namesize - 1] = '\0'; - CPIO_POS(super) += len; - cpio_skip_padding(super); + CPIO_POS (super) += len; + cpio_skip_padding (super); - if(!strcmp("TRAILER!!!", name)) { /* We got to the last record */ - g_free(name); - return STATUS_TRAIL; + if (!strcmp ("TRAILER!!!", name)) + { /* We got to the last record */ + g_free (name); + return STATUS_TRAIL; } u.st.st_dev = hd.c_dev; @@ -522,61 +605,67 @@ static ssize_t cpio_read_oldc_head(struct vfs_class *me, struct vfs_s_super *sup return cpio_create_entry (me, super, &u.st, name); } -#undef HEAD_LENGTH +/* --------------------------------------------------------------------------------------------- */ + +#undef HEAD_LENGTH #define HEAD_LENGTH (110) + static ssize_t cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super) { struct new_cpio_header hd; - union { - struct stat st; - char buf[HEAD_LENGTH + 1]; + union + { + struct stat st; + char buf[HEAD_LENGTH + 1]; } u; ssize_t len; char *name; if (mc_read (super->u.arch.fd, u.buf, HEAD_LENGTH) != HEAD_LENGTH) - return STATUS_EOF; + return STATUS_EOF; CPIO_POS (super) += HEAD_LENGTH; u.buf[HEAD_LENGTH] = '\0'; if (sscanf (u.buf, "%6ho%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx", - &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid, - &hd.c_nlink, &hd.c_mtime, &hd.c_filesize, - (unsigned long *)&hd.c_dev, (unsigned long *)&hd.c_devmin, - (unsigned long *)&hd.c_rdev, (unsigned long *)&hd.c_rdevmin, - &hd.c_namesize, &hd.c_chksum) < 14) { - message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), - super->name); - return STATUS_FAIL; + &hd.c_magic, &hd.c_ino, &hd.c_mode, &hd.c_uid, &hd.c_gid, + &hd.c_nlink, &hd.c_mtime, &hd.c_filesize, + (unsigned long *) &hd.c_dev, (unsigned long *) &hd.c_devmin, + (unsigned long *) &hd.c_rdev, (unsigned long *) &hd.c_rdevmin, + &hd.c_namesize, &hd.c_chksum) < 14) + { + message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name); + return STATUS_FAIL; } - if((super->u.arch.type == CPIO_NEWC && hd.c_magic != 070701) || - (super->u.arch.type == CPIO_CRC && hd.c_magic != 070702)) - return STATUS_FAIL; + if ((super->u.arch.type == CPIO_NEWC && hd.c_magic != 070701) || + (super->u.arch.type == CPIO_CRC && hd.c_magic != 070702)) + return STATUS_FAIL; - if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) { - message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), - super->name); - return STATUS_FAIL; + if (hd.c_namesize == 0 || hd.c_namesize > MC_MAXPATHLEN) + { + message (D_ERROR, MSG_ERROR, _("Corrupted cpio header encountered in\n%s"), super->name); + return STATUS_FAIL; } - name = g_malloc(hd.c_namesize); + name = g_malloc (hd.c_namesize); len = mc_read (super->u.arch.fd, name, hd.c_namesize); - if ((len == -1) || ((unsigned long) len < hd.c_namesize)) { - g_free (name); - return STATUS_EOF; + if ((len == -1) || ((unsigned long) len < hd.c_namesize)) + { + g_free (name); + return STATUS_EOF; } name[hd.c_namesize - 1] = '\0'; - CPIO_POS(super) += len; - cpio_skip_padding(super); + CPIO_POS (super) += len; + cpio_skip_padding (super); - if (strcmp ("TRAILER!!!", name) == 0) { /* We got to the last record */ - g_free(name); - return STATUS_TRAIL; + if (strcmp ("TRAILER!!!", name) == 0) + { /* We got to the last record */ + g_free (name); + return STATUS_TRAIL; } u.st.st_dev = makedev (hd.c_dev, hd.c_devmin); @@ -592,38 +681,42 @@ cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super) return cpio_create_entry (me, super, &u.st, name); } -/* Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */ +/* --------------------------------------------------------------------------------------------- */ +/** Need to CPIO_SEEK_CUR to skip the file at the end of add entry!!!! */ static int -cpio_open_archive (struct vfs_class *me, struct vfs_s_super *super, - const char *name, char *op) +cpio_open_archive (struct vfs_class *me, struct vfs_s_super *super, const char *name, char *op) { int status = STATUS_START; (void) op; if (cpio_open_cpio_file (me, super, name) == -1) - return -1; + return -1; - for (;;) { - status = cpio_read_head (me, super); + for (;;) + { + status = cpio_read_head (me, super); - switch (status) { - case STATUS_EOF: - message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"), name); - return 0; - case STATUS_OK: - continue; - case STATUS_TRAIL: - break; - } - break; + switch (status) + { + case STATUS_EOF: + message (D_ERROR, MSG_ERROR, _("Unexpected end of file\n%s"), name); + return 0; + case STATUS_OK: + continue; + case STATUS_TRAIL: + break; + } + break; } return 0; } -/* Remaining functions are exactly same as for tarfs (and were in fact just copied) */ +/* --------------------------------------------------------------------------------------------- */ +/** Remaining functions are exactly same as for tarfs (and were in fact just copied) */ + static void * cpio_super_check (struct vfs_class *me, const char *archive_name, char *op) { @@ -633,45 +726,51 @@ cpio_super_check (struct vfs_class *me, const char *archive_name, char *op) (void) op; if (mc_stat (archive_name, &sb)) - return NULL; + return NULL; return &sb; } +/* --------------------------------------------------------------------------------------------- */ + static int cpio_super_same (struct vfs_class *me, struct vfs_s_super *parc, - const char *archive_name, char *op, void *cookie) + const char *archive_name, char *op, void *cookie) { - struct stat *archive_stat = cookie; /* stat of main archive */ + struct stat *archive_stat = cookie; /* stat of main archive */ (void) me; (void) op; if (strcmp (parc->name, archive_name)) - return 0; + return 0; /* Has the cached archive been changed on the disk? */ - if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) { - /* Yes, reload! */ - (*vfs_cpiofs_ops.free) ((vfsid) parc); - vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc); - return 2; + if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) + { + /* Yes, reload! */ + (*vfs_cpiofs_ops.free) ((vfsid) parc); + vfs_rmstamp (&vfs_cpiofs_ops, (vfsid) parc); + return 2; } /* Hasn't been modified, give it a new timeout */ vfs_stamp (&vfs_cpiofs_ops, (vfsid) parc); return 1; } -static ssize_t cpio_read(void *fh, char *buffer, size_t count) +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +cpio_read (void *fh, char *buffer, size_t count) { off_t begin = FH->ino->data_offset; int fd = FH_SUPER->u.arch.fd; struct vfs_class *me = FH_SUPER->me; ssize_t res; - if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != - begin + FH->pos) ERRNOR (EIO, -1); + if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != begin + FH->pos) + ERRNOR (EIO, -1); - count = MIN(count, (size_t)(FH->ino->st.st_size - FH->pos)); + count = MIN (count, (size_t) (FH->ino->st.st_size - FH->pos)); res = mc_read (fd, buffer, count); if (res == -1) @@ -681,15 +780,23 @@ static ssize_t cpio_read(void *fh, char *buffer, size_t count) return res; } -static int cpio_fh_open(struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) +/* --------------------------------------------------------------------------------------------- */ + +static int +cpio_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) { (void) fh; (void) mode; - if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1); + if ((flags & O_ACCMODE) != O_RDONLY) + ERRNOR (EROFS, -1); return 0; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void init_cpiofs (void) { @@ -709,3 +816,5 @@ init_cpiofs (void) vfs_cpiofs_ops.setctl = NULL; vfs_register_class (&vfs_cpiofs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/direntry.c b/lib/vfs/mc-vfs/direntry.c index 0b18c2d37..f55fbe21b 100644 --- a/lib/vfs/mc-vfs/direntry.c +++ b/lib/vfs/mc-vfs/direntry.c @@ -50,51 +50,26 @@ #include "xdirentry.h" #include "gc.h" /* vfs_rmstamp */ +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + #define CALL(x) if (MEDATA->x) MEDATA->x +/*** file scope type declarations ****************************************************************/ + +struct dirhandle +{ + struct vfs_s_entry *cur; + struct vfs_s_inode *dir; +}; + +/*** file scope variables ************************************************************************/ + static volatile int total_inodes = 0, total_entries = 0; -struct vfs_s_inode * -vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat) -{ - struct vfs_s_inode *ino; - - ino = g_try_new0 (struct vfs_s_inode, 1); - if (ino == NULL) - return NULL; - - if (initstat) - ino->st = *initstat; - ino->super = super; - ino->st.st_nlink = 0; - ino->st.st_ino = MEDATA->inode_counter++; - ino->st.st_dev = MEDATA->rdev; - - super->ino_usage++; - total_inodes++; - - CALL (init_inode) (me, ino); - - return ino; -} - -struct vfs_s_entry * -vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode) -{ - struct vfs_s_entry *entry; - - entry = g_new0 (struct vfs_s_entry, 1); - total_entries++; - - if (name) - entry->name = g_strdup (name); - - entry->ino = inode; - entry->ino->ent = entry; - CALL (init_entry) (me, entry); - - return entry; -} +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static void vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino) @@ -125,86 +100,9 @@ vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino) ino->st.st_nlink--; } -void -vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent) -{ - if (ent->prevp) - { - /* It is possible that we are deleting freshly created entry */ - *ent->prevp = ent->next; - if (ent->next) - ent->next->prevp = ent->prevp; - } - - g_free (ent->name); - ent->name = NULL; - - if (ent->ino) - { - ent->ino->ent = NULL; - vfs_s_free_inode (me, ent->ino); - ent->ino = NULL; - } - - total_entries--; - g_free (ent); -} - -void -vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent) -{ - struct vfs_s_entry **ep; - - (void) me; - - for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next)) - ; - ent->prevp = ep; - ent->next = NULL; - ent->dir = dir; - *ep = ent; - - ent->ino->st.st_nlink++; -} - -struct stat * -vfs_s_default_stat (struct vfs_class *me, mode_t mode) -{ - static struct stat st; - int myumask; - - (void) me; - - myumask = umask (022); - umask (myumask); - mode &= ~myumask; - - st.st_mode = mode; - st.st_ino = 0; - st.st_dev = 0; - st.st_rdev = 0; - st.st_uid = getuid (); - st.st_gid = getgid (); - st.st_size = 0; - st.st_mtime = st.st_atime = st.st_ctime = time (NULL); - - return &st; -} - -struct vfs_s_entry * -vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent, - mode_t mode) -{ - struct vfs_s_inode *inode; - struct stat *st; - - st = vfs_s_default_stat (me, mode); - inode = vfs_s_new_inode (me, parent->super, st); - - return vfs_s_new_entry (me, name, inode); -} - +/* --------------------------------------------------------------------------------------------- */ /* We were asked to create entries automagically */ + static struct vfs_s_entry * vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags) { @@ -222,7 +120,9 @@ vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int f return res; } +/* --------------------------------------------------------------------------------------------- */ /* If the entry is a symlink, find the entry for its target */ + static struct vfs_s_entry * vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow) { @@ -260,10 +160,12 @@ vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int foll return target; } +/* --------------------------------------------------------------------------------------------- */ /* * Follow > 0: follow links, serves as loop protect, * == -1: do not follow links */ + static struct vfs_s_entry * vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root, const char *a_path, int follow, int flags) @@ -314,6 +216,8 @@ vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root, return NULL; } +/* --------------------------------------------------------------------------------------------- */ + static void split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save) { @@ -337,6 +241,8 @@ split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char } } +/* --------------------------------------------------------------------------------------------- */ + static struct vfs_s_entry * vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root, const char *a_path, int follow, int flags) @@ -409,19 +315,7 @@ vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root, return ent; } -struct vfs_s_inode * -vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super, - const char *path, int follow, int flags) -{ - struct vfs_s_entry *ent; - - if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0')) - return super->root; - - ent = (MEDATA->find_entry) (me, super->root, path, follow, flags); - return (ent != NULL) ? ent->ino : NULL; -} - +/* --------------------------------------------------------------------------------------------- */ /* Ook, these were functions around directory entries / inodes */ /* -------------------------------- superblock games -------------------------- */ @@ -435,6 +329,8 @@ vfs_s_new_super (struct vfs_class *me) return super; } +/* --------------------------------------------------------------------------------------------- */ + static void vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super) { @@ -446,6 +342,8 @@ vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super) MEDATA->supers = super; } +/* --------------------------------------------------------------------------------------------- */ + static void vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super) { @@ -477,6 +375,651 @@ vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super) g_free (super); } +/* --------------------------------------------------------------------------------------------- */ +/** + * Dissect the path and create corresponding superblock. + * The result should be freed. + */ + +static char * +vfs_s_get_path (struct vfs_class *me, const char *inname, struct vfs_s_super **archive, int flags) +{ + char *buf, *retval; + + buf = g_strdup (inname); + retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags)); + g_free (buf); + return retval; +} + +/* --------------------------------------------------------------------------------------------- */ +/* Support of archives */ +/* ------------------------ readdir & friends ----------------------------- */ + +static struct vfs_s_inode * +vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags) +{ + struct vfs_s_super *super; + struct vfs_s_inode *ino; + char *q; + + if (!(q = vfs_s_get_path (me, name, &super, 0))) + return NULL; + + ino = + vfs_s_find_inode (me, super, q, + flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW); + if ((!ino) && (!*q)) + /* We are asking about / directory of ftp server: assume it exists */ + ino = + vfs_s_find_inode (me, super, q, + flags & FL_FOLLOW ? LINK_FOLLOW : + LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW)); + g_free (q); + return ino; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void * +vfs_s_opendir (struct vfs_class *me, const char *dirname) +{ + struct vfs_s_inode *dir; + struct dirhandle *info; + + dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW); + if (!dir) + return NULL; + if (!S_ISDIR (dir->st.st_mode)) + ERRNOR (ENOTDIR, NULL); + + dir->st.st_nlink++; +#if 0 + if (!dir->subdir) /* This can actually happen if we allow empty directories */ + ERRNOR (EAGAIN, NULL); +#endif + info = g_new (struct dirhandle, 1); + info->cur = dir->subdir; + info->dir = dir; + + return info; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void * +vfs_s_readdir (void *data) +{ + static union vfs_dirent dir; + struct dirhandle *info = (struct dirhandle *) data; + + if (!(info->cur)) + return NULL; + + if (info->cur->name) + { + g_strlcpy (dir.dent.d_name, info->cur->name, MC_MAXPATHLEN); + } + else + { + vfs_die ("Null in structure-cannot happen"); + } + + compute_namelen (&dir.dent); + info->cur = info->cur->next; + + return (void *) &dir; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_closedir (void *data) +{ + struct dirhandle *info = (struct dirhandle *) data; + struct vfs_s_inode *dir = info->dir; + + vfs_s_free_inode (dir->super->me, dir); + g_free (data); + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_chdir (struct vfs_class *me, const char *path) +{ + void *data; + + data = vfs_s_opendir (me, path); + if (data == NULL) + return -1; + vfs_s_closedir (data); + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ +/* --------------------------- stat and friends ---------------------------- */ + +static int +vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag) +{ + struct vfs_s_inode *ino; + + ino = vfs_s_inode_from_path (me, path, flag); + if (ino == NULL) + return -1; + *buf = ino->st; + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf) +{ + return vfs_s_internal_stat (me, path, buf, FL_FOLLOW); +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf) +{ + return vfs_s_internal_stat (me, path, buf, FL_NONE); +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_fstat (void *fh, struct stat *buf) +{ + *buf = FH->ino->st; + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) +{ + struct vfs_s_inode *ino; + size_t len; + + ino = vfs_s_inode_from_path (me, path, 0); + if (!ino) + return -1; + + if (!S_ISLNK (ino->st.st_mode)) + ERRNOR (EINVAL, -1); + + if (ino->linkname == NULL) + ERRNOR (EFAULT, -1); + + len = strlen (ino->linkname); + if (size < len) + len = size; + /* readlink() does not append a NUL character to buf */ + memcpy (buf, ino->linkname, len); + return len; +} + +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +vfs_s_read (void *fh, char *buffer, size_t count) +{ + int n; + struct vfs_class *me = FH_SUPER->me; + + if (FH->linear == LS_LINEAR_PREOPEN) + { + if (!MEDATA->linear_start (me, FH, FH->pos)) + return -1; + } + + if (FH->linear == LS_LINEAR_CLOSED) + vfs_die ("linear_start() did not set linear_state!"); + + if (FH->linear == LS_LINEAR_OPEN) + return MEDATA->linear_read (me, FH, buffer, count); + + if (FH->handle != -1) + { + n = read (FH->handle, buffer, count); + if (n < 0) + me->verrno = errno; + return n; + } + vfs_die ("vfs_s_read: This should not happen\n"); + return -1; +} + +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +vfs_s_write (void *fh, const char *buffer, size_t count) +{ + int n; + struct vfs_class *me = FH_SUPER->me; + + if (FH->linear) + vfs_die ("no writing to linear files, please"); + + FH->changed = 1; + if (FH->handle != -1) + { + n = write (FH->handle, buffer, count); + if (n < 0) + me->verrno = errno; + return n; + } + vfs_die ("vfs_s_write: This should not happen\n"); + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static off_t +vfs_s_lseek (void *fh, off_t offset, int whence) +{ + off_t size = FH->ino->st.st_size; + + if (FH->linear == LS_LINEAR_OPEN) + vfs_die ("cannot lseek() after linear_read!"); + + if (FH->handle != -1) + { /* If we have local file opened, we want to work with it */ + off_t retval = lseek (FH->handle, offset, whence); + if (retval == -1) + FH->ino->super->me->verrno = errno; + return retval; + } + + switch (whence) + { + case SEEK_CUR: + offset += FH->pos; + break; + case SEEK_END: + offset += size; + break; + } + if (offset < 0) + FH->pos = 0; + else if (offset < size) + FH->pos = offset; + else + FH->pos = size; + return FH->pos; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_close (void *fh) +{ + int res = 0; + struct vfs_class *me = FH_SUPER->me; + + FH_SUPER->fd_usage--; + if (!FH_SUPER->fd_usage) + vfs_stamp_create (me, FH_SUPER); + + if (FH->linear == LS_LINEAR_OPEN) + MEDATA->linear_close (me, fh); + if (MEDATA->fh_close) + res = MEDATA->fh_close (me, fh); + if (FH->changed && MEDATA->file_store) + { + char *s = vfs_s_fullpath (me, FH->ino); + if (!s) + res = -1; + else + { + res = MEDATA->file_store (me, fh, s, FH->ino->localname); + g_free (s); + } + vfs_s_invalidate (me, FH_SUPER); + } + if (FH->handle != -1) + close (FH->handle); + + vfs_s_free_inode (me, FH->ino); + g_free (fh); + return res; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +vfs_s_print_stats (const char *fs_name, const char *action, + const char *file_name, off_t have, off_t need) +{ + static const char *i18n_percent_transf_format = NULL; + static const char *i18n_transf_format = NULL; + + if (i18n_percent_transf_format == NULL) + { + i18n_percent_transf_format = _("%s: %s: %s %3d%% (%ju bytes transferred)"); + i18n_transf_format = _("%s: %s: %s %ju bytes transferred"); + } + + if (need) + print_vfs_message (i18n_percent_transf_format, fs_name, action, + file_name, (int) ((double) have * 100 / need), (uintmax_t) have); + else + print_vfs_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have); +} + +/* --------------------------------------------------------------------------------------------- */ +/* ------------------------------- mc support ---------------------------- */ + +static void +vfs_s_fill_names (struct vfs_class *me, fill_names_f func) +{ + struct vfs_s_super *a = MEDATA->supers; + char *name; + + while (a) + { + name = g_strconcat (a->name, "#", me->prefix, "/", + /* a->current_dir->name, */ (char *) NULL); + (*func) (name); + g_free (name); + a = a->next; + } +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_ferrno (struct vfs_class *me) +{ + return me->verrno; +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Get local copy of the given file. We reuse the existing file cache + * for remote filesystems. Archives use standard VFS facilities. + */ + +static char * +vfs_s_getlocalcopy (struct vfs_class *me, const char *path) +{ + struct vfs_s_fh *fh; + char *local = NULL; + + fh = vfs_s_open (me, path, O_RDONLY, 0); + + if (fh != NULL) + { + if ((fh->ino != NULL) && (fh->ino->localname != NULL)) + local = g_strdup (fh->ino->localname); + + vfs_s_close (fh); + } + + return local; +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Return the local copy. Since we are using our cache, we do nothing - + * the cache will be removed when the archive is closed. + */ + +static int +vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed) +{ + (void) me; + (void) path; + (void) local; + (void) has_changed; + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) +{ + switch (ctlop) + { + case VFS_SETCTL_STALE_DATA: + { + struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0); + + if (ino == NULL) + return 0; + if (arg) + ino->super->want_stale = 1; + else + { + ino->super->want_stale = 0; + vfs_s_invalidate (me, ino->super); + } + return 1; + } + case VFS_SETCTL_LOGFILE: + MEDATA->logfile = fopen ((char *) arg, "w"); + return 1; + case VFS_SETCTL_FLUSH: + MEDATA->flush = 1; + return 1; + } + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ +/* ----------------------------- Stamping support -------------------------- */ + +static vfsid +vfs_s_getid (struct vfs_class *me, const char *path) +{ + struct vfs_s_super *archive = NULL; + char *p; + + p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN); + if (p == NULL) + return NULL; + g_free (p); + return (vfsid) archive; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_nothingisopen (vfsid id) +{ + (void) id; + /* Our data structures should survive free of superblock at any time */ + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +vfs_s_free (vfsid id) +{ + vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id); +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino) +{ + struct timeval tim; + + if (MEDATA->flush) + { + MEDATA->flush = 0; + return 0; + } + + gettimeofday (&tim, NULL); + if (tim.tv_sec < ino->timestamp.tv_sec) + return 1; + return 0; +} + + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +struct vfs_s_inode * +vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat) +{ + struct vfs_s_inode *ino; + + ino = g_try_new0 (struct vfs_s_inode, 1); + if (ino == NULL) + return NULL; + + if (initstat) + ino->st = *initstat; + ino->super = super; + ino->st.st_nlink = 0; + ino->st.st_ino = MEDATA->inode_counter++; + ino->st.st_dev = MEDATA->rdev; + + super->ino_usage++; + total_inodes++; + + CALL (init_inode) (me, ino); + + return ino; +} + +/* --------------------------------------------------------------------------------------------- */ + +struct vfs_s_entry * +vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode) +{ + struct vfs_s_entry *entry; + + entry = g_new0 (struct vfs_s_entry, 1); + total_entries++; + + if (name) + entry->name = g_strdup (name); + + entry->ino = inode; + entry->ino->ent = entry; + CALL (init_entry) (me, entry); + + return entry; +} + + +/* --------------------------------------------------------------------------------------------- */ + +void +vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent) +{ + if (ent->prevp) + { + /* It is possible that we are deleting freshly created entry */ + *ent->prevp = ent->next; + if (ent->next) + ent->next->prevp = ent->prevp; + } + + g_free (ent->name); + ent->name = NULL; + + if (ent->ino) + { + ent->ino->ent = NULL; + vfs_s_free_inode (me, ent->ino); + ent->ino = NULL; + } + + total_entries--; + g_free (ent); +} + +/* --------------------------------------------------------------------------------------------- */ + +void +vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent) +{ + struct vfs_s_entry **ep; + + (void) me; + + for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next)) + ; + ent->prevp = ep; + ent->next = NULL; + ent->dir = dir; + *ep = ent; + + ent->ino->st.st_nlink++; +} + +/* --------------------------------------------------------------------------------------------- */ + +struct stat * +vfs_s_default_stat (struct vfs_class *me, mode_t mode) +{ + static struct stat st; + int myumask; + + (void) me; + + myumask = umask (022); + umask (myumask); + mode &= ~myumask; + + st.st_mode = mode; + st.st_ino = 0; + st.st_dev = 0; + st.st_rdev = 0; + st.st_uid = getuid (); + st.st_gid = getgid (); + st.st_size = 0; + st.st_mtime = st.st_atime = st.st_ctime = time (NULL); + + return &st; +} + +/* --------------------------------------------------------------------------------------------- */ + +struct vfs_s_entry * +vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent, + mode_t mode) +{ + struct vfs_s_inode *inode; + struct stat *st; + + st = vfs_s_default_stat (me, mode); + inode = vfs_s_new_inode (me, parent->super, st); + + return vfs_s_new_entry (me, name, inode); +} + +/* --------------------------------------------------------------------------------------------- */ + +struct vfs_s_inode * +vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super, + const char *path, int follow, int flags) +{ + struct vfs_s_entry *ent; + + if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0')) + return super->root; + + ent = (MEDATA->find_entry) (me, super->root, path, follow, flags); + return (ent != NULL) ? ent->ino : NULL; +} + +/* --------------------------------------------------------------------------------------------- */ +/* Ook, these were functions around directory entries / inodes */ +/* -------------------------------- superblock games -------------------------- */ /* * Dissect the path and create corresponding superblock. Note that inname @@ -539,21 +1082,7 @@ vfs_s_get_path_mangle (struct vfs_class *me, char *inname, struct vfs_s_super ** return retval; } - -/* - * Dissect the path and create corresponding superblock. - * The result should be freed. - */ -static char * -vfs_s_get_path (struct vfs_class *me, const char *inname, struct vfs_s_super **archive, int flags) -{ - char *buf, *retval; - - buf = g_strdup (inname); - retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags)); - g_free (buf); - return retval; -} +/* --------------------------------------------------------------------------------------------- */ void vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super) @@ -565,6 +1094,8 @@ vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super) } } +/* --------------------------------------------------------------------------------------------- */ + char * vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino) { @@ -595,166 +1126,9 @@ vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino) return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL); } -/* Support of archives */ -/* ------------------------ readdir & friends ----------------------------- */ - -static struct vfs_s_inode * -vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags) -{ - struct vfs_s_super *super; - struct vfs_s_inode *ino; - char *q; - - if (!(q = vfs_s_get_path (me, name, &super, 0))) - return NULL; - - ino = - vfs_s_find_inode (me, super, q, - flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW); - if ((!ino) && (!*q)) - /* We are asking about / directory of ftp server: assume it exists */ - ino = - vfs_s_find_inode (me, super, q, - flags & FL_FOLLOW ? LINK_FOLLOW : - LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW)); - g_free (q); - return ino; -} - -struct dirhandle -{ - struct vfs_s_entry *cur; - struct vfs_s_inode *dir; -}; - -static void * -vfs_s_opendir (struct vfs_class *me, const char *dirname) -{ - struct vfs_s_inode *dir; - struct dirhandle *info; - - dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW); - if (!dir) - return NULL; - if (!S_ISDIR (dir->st.st_mode)) - ERRNOR (ENOTDIR, NULL); - - dir->st.st_nlink++; -#if 0 - if (!dir->subdir) /* This can actually happen if we allow empty directories */ - ERRNOR (EAGAIN, NULL); -#endif - info = g_new (struct dirhandle, 1); - info->cur = dir->subdir; - info->dir = dir; - - return info; -} - -static void * -vfs_s_readdir (void *data) -{ - static union vfs_dirent dir; - struct dirhandle *info = (struct dirhandle *) data; - - if (!(info->cur)) - return NULL; - - if (info->cur->name) - { - g_strlcpy (dir.dent.d_name, info->cur->name, MC_MAXPATHLEN); - } - else - { - vfs_die ("Null in structure-cannot happen"); - } - - compute_namelen (&dir.dent); - info->cur = info->cur->next; - - return (void *) &dir; -} - -static int -vfs_s_closedir (void *data) -{ - struct dirhandle *info = (struct dirhandle *) data; - struct vfs_s_inode *dir = info->dir; - - vfs_s_free_inode (dir->super->me, dir); - g_free (data); - return 0; -} - -static int -vfs_s_chdir (struct vfs_class *me, const char *path) -{ - void *data; - - data = vfs_s_opendir (me, path); - if (data == NULL) - return -1; - vfs_s_closedir (data); - return 0; -} - +/* --------------------------------------------------------------------------------------------- */ /* --------------------------- stat and friends ---------------------------- */ -static int -vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag) -{ - struct vfs_s_inode *ino; - - ino = vfs_s_inode_from_path (me, path, flag); - if (ino == NULL) - return -1; - *buf = ino->st; - return 0; -} - -static int -vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf) -{ - return vfs_s_internal_stat (me, path, buf, FL_FOLLOW); -} - -static int -vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf) -{ - return vfs_s_internal_stat (me, path, buf, FL_NONE); -} - -static int -vfs_s_fstat (void *fh, struct stat *buf) -{ - *buf = FH->ino->st; - return 0; -} - -static int -vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) -{ - struct vfs_s_inode *ino; - size_t len; - - ino = vfs_s_inode_from_path (me, path, 0); - if (!ino) - return -1; - - if (!S_ISLNK (ino->st.st_mode)) - ERRNOR (EINVAL, -1); - - if (ino->linkname == NULL) - ERRNOR (EFAULT, -1); - - len = strlen (ino->linkname); - if (size < len) - len = size; - /* readlink() does not append a NUL character to buf */ - memcpy (buf, ino->linkname, len); - return len; -} - void * vfs_s_open (struct vfs_class *me, const char *file, int flags, mode_t mode) { @@ -848,143 +1222,7 @@ vfs_s_open (struct vfs_class *me, const char *file, int flags, mode_t mode) return fh; } -static ssize_t -vfs_s_read (void *fh, char *buffer, size_t count) -{ - int n; - struct vfs_class *me = FH_SUPER->me; - - if (FH->linear == LS_LINEAR_PREOPEN) - { - if (!MEDATA->linear_start (me, FH, FH->pos)) - return -1; - } - - if (FH->linear == LS_LINEAR_CLOSED) - vfs_die ("linear_start() did not set linear_state!"); - - if (FH->linear == LS_LINEAR_OPEN) - return MEDATA->linear_read (me, FH, buffer, count); - - if (FH->handle != -1) - { - n = read (FH->handle, buffer, count); - if (n < 0) - me->verrno = errno; - return n; - } - vfs_die ("vfs_s_read: This should not happen\n"); - return -1; -} - -static ssize_t -vfs_s_write (void *fh, const char *buffer, size_t count) -{ - int n; - struct vfs_class *me = FH_SUPER->me; - - if (FH->linear) - vfs_die ("no writing to linear files, please"); - - FH->changed = 1; - if (FH->handle != -1) - { - n = write (FH->handle, buffer, count); - if (n < 0) - me->verrno = errno; - return n; - } - vfs_die ("vfs_s_write: This should not happen\n"); - return 0; -} - -static off_t -vfs_s_lseek (void *fh, off_t offset, int whence) -{ - off_t size = FH->ino->st.st_size; - - if (FH->linear == LS_LINEAR_OPEN) - vfs_die ("cannot lseek() after linear_read!"); - - if (FH->handle != -1) - { /* If we have local file opened, we want to work with it */ - off_t retval = lseek (FH->handle, offset, whence); - if (retval == -1) - FH->ino->super->me->verrno = errno; - return retval; - } - - switch (whence) - { - case SEEK_CUR: - offset += FH->pos; - break; - case SEEK_END: - offset += size; - break; - } - if (offset < 0) - FH->pos = 0; - else if (offset < size) - FH->pos = offset; - else - FH->pos = size; - return FH->pos; -} - -static int -vfs_s_close (void *fh) -{ - int res = 0; - struct vfs_class *me = FH_SUPER->me; - - FH_SUPER->fd_usage--; - if (!FH_SUPER->fd_usage) - vfs_stamp_create (me, FH_SUPER); - - if (FH->linear == LS_LINEAR_OPEN) - MEDATA->linear_close (me, fh); - if (MEDATA->fh_close) - res = MEDATA->fh_close (me, fh); - if (FH->changed && MEDATA->file_store) - { - char *s = vfs_s_fullpath (me, FH->ino); - if (!s) - res = -1; - else - { - res = MEDATA->file_store (me, fh, s, FH->ino->localname); - g_free (s); - } - vfs_s_invalidate (me, FH_SUPER); - } - if (FH->handle != -1) - close (FH->handle); - - vfs_s_free_inode (me, FH->ino); - g_free (fh); - return res; -} - -static void -vfs_s_print_stats (const char *fs_name, const char *action, - const char *file_name, off_t have, off_t need) -{ - static const char *i18n_percent_transf_format = NULL; - static const char *i18n_transf_format = NULL; - - if (i18n_percent_transf_format == NULL) - { - i18n_percent_transf_format = _("%s: %s: %s %3d%% (%ju bytes transferred)"); - i18n_transf_format = _("%s: %s: %s %ju bytes transferred"); - } - - if (need) - print_vfs_message (i18n_percent_transf_format, fs_name, action, - file_name, (int) ((double) have * 100 / need), (uintmax_t) have); - else - print_vfs_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have); -} +/* --------------------------------------------------------------------------------------------- */ int vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino) @@ -1053,144 +1291,9 @@ vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino) return -1; } -/* ------------------------------- mc support ---------------------------- */ - -static void -vfs_s_fill_names (struct vfs_class *me, fill_names_f func) -{ - struct vfs_s_super *a = MEDATA->supers; - char *name; - - while (a) - { - name = g_strconcat (a->name, "#", me->prefix, "/", - /* a->current_dir->name, */ (char *) NULL); - (*func) (name); - g_free (name); - a = a->next; - } -} - -static int -vfs_s_ferrno (struct vfs_class *me) -{ - return me->verrno; -} - -/* - * Get local copy of the given file. We reuse the existing file cache - * for remote filesystems. Archives use standard VFS facilities. - */ -static char * -vfs_s_getlocalcopy (struct vfs_class *me, const char *path) -{ - struct vfs_s_fh *fh; - char *local = NULL; - - fh = vfs_s_open (me, path, O_RDONLY, 0); - - if (fh != NULL) - { - if ((fh->ino != NULL) && (fh->ino->localname != NULL)) - local = g_strdup (fh->ino->localname); - - vfs_s_close (fh); - } - - return local; -} - -/* - * Return the local copy. Since we are using our cache, we do nothing - - * the cache will be removed when the archive is closed. - */ -static int -vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed) -{ - (void) me; - (void) path; - (void) local; - (void) has_changed; - return 0; -} - -static int -vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) -{ - switch (ctlop) - { - case VFS_SETCTL_STALE_DATA: - { - struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0); - - if (ino == NULL) - return 0; - if (arg) - ino->super->want_stale = 1; - else - { - ino->super->want_stale = 0; - vfs_s_invalidate (me, ino->super); - } - return 1; - } - case VFS_SETCTL_LOGFILE: - MEDATA->logfile = fopen ((char *) arg, "w"); - return 1; - case VFS_SETCTL_FLUSH: - MEDATA->flush = 1; - return 1; - } - return 0; -} - - +/* --------------------------------------------------------------------------------------------- */ /* ----------------------------- Stamping support -------------------------- */ -static vfsid -vfs_s_getid (struct vfs_class *me, const char *path) -{ - struct vfs_s_super *archive = NULL; - char *p; - - p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN); - if (p == NULL) - return NULL; - g_free (p); - return (vfsid) archive; -} - -static int -vfs_s_nothingisopen (vfsid id) -{ - (void) id; - /* Our data structures should survive free of superblock at any time */ - return 1; -} - -static void -vfs_s_free (vfsid id) -{ - vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id); -} - -static int -vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino) -{ - struct timeval tim; - - if (MEDATA->flush) - { - MEDATA->flush = 0; - return 0; - } - - gettimeofday (&tim, NULL); - if (tim.tv_sec < ino->timestamp.tv_sec) - return 1; - return 0; -} - /* Initialize one of our subclasses - fill common functions */ void vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub) @@ -1231,7 +1334,9 @@ vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub) sub->dir_uptodate = vfs_s_dir_uptodate; } -/* Find VFS id for given directory name */ +/* --------------------------------------------------------------------------------------------- */ +/** Find VFS id for given directory name */ + vfsid vfs_getid (struct vfs_class *vclass, const char *dir) { @@ -1247,6 +1352,7 @@ vfs_getid (struct vfs_class *vclass, const char *dir) return id; } +/* --------------------------------------------------------------------------------------------- */ /* ----------- Utility functions for networked filesystems -------------- */ #ifdef ENABLE_VFS_NET @@ -1273,6 +1379,8 @@ vfs_s_select_on_two (int fd1, int fd2) return -1; } +/* --------------------------------------------------------------------------------------------- */ + int vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term) { @@ -1315,6 +1423,8 @@ vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char ter return 0; } +/* --------------------------------------------------------------------------------------------- */ + int vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd) { @@ -1348,3 +1458,5 @@ vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int return 0; } #endif /* ENABLE_VFS_NET */ + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/extfs.c b/lib/vfs/mc-vfs/extfs.c index d01656775..1137e1a84 100644 --- a/lib/vfs/mc-vfs/extfs.c +++ b/lib/vfs/mc-vfs/extfs.c @@ -57,9 +57,19 @@ #include "utilvfs.h" #include "gc.h" /* vfs_rmstamp */ +/*** global variables ****************************************************************************/ + +GArray *extfs_plugins = NULL; + +/*** file scope macro definitions ****************************************************************/ + #undef ERRNOR #define ERRNOR(x,y) do { my_errno = x; return y; } while(0) +#define RECORDSIZE 512 + +/*** file scope type declarations ****************************************************************/ + struct inode { nlink_t nlink; @@ -116,19 +126,24 @@ typedef struct gboolean need_archive; } extfs_plugin_info_t; +/*** file scope variables ************************************************************************/ + static gboolean errloop; static gboolean notadir; +static struct vfs_class vfs_extfs_ops; +static struct archive *first_archive = NULL; +static int my_errno = 0; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + static void extfs_remove_entry (struct entry *e); static void extfs_free (vfsid id); static void extfs_free_entry (struct entry *e); static struct entry *extfs_resolve_symlinks_int (struct entry *entry, GSList * list); -static struct vfs_class vfs_extfs_ops; -static struct archive *first_archive = NULL; -static int my_errno = 0; - -GArray *extfs_plugins = NULL; +/* --------------------------------------------------------------------------------------------- */ static void extfs_make_dots (struct entry *ent) @@ -164,6 +179,8 @@ extfs_make_dots (struct entry *ent) } } +/* --------------------------------------------------------------------------------------------- */ + static struct entry * extfs_generate_entry (struct archive *archive, const char *name, struct entry *parentry, mode_t mode) @@ -208,6 +225,8 @@ extfs_generate_entry (struct archive *archive, return entry; } +/* --------------------------------------------------------------------------------------------- */ + static struct entry * extfs_find_entry_int (struct entry *dir, char *name, GSList * list, gboolean make_dirs, gboolean make_file) @@ -291,6 +310,8 @@ extfs_find_entry_int (struct entry *dir, char *name, GSList * list, return pent; } +/* --------------------------------------------------------------------------------------------- */ + static struct entry * extfs_find_entry (struct entry *dir, char *name, gboolean make_dirs, gboolean make_file) { @@ -310,6 +331,8 @@ extfs_find_entry (struct entry *dir, char *name, gboolean make_dirs, gboolean ma return res; } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_fill_names (struct vfs_class *me, fill_names_f func) { @@ -330,6 +353,8 @@ extfs_fill_names (struct vfs_class *me, fill_names_f func) } } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_free_archive (struct archive *archive) { @@ -347,6 +372,8 @@ extfs_free_archive (struct archive *archive) g_free (archive); } +/* --------------------------------------------------------------------------------------------- */ + static FILE * extfs_open_archive (int fstype, const char *name, struct archive **pparc) { @@ -432,10 +459,12 @@ extfs_open_archive (int fstype, const char *name, struct archive **pparc) return result; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Main loop for reading an archive. * Return 0 on success, -1 on error. */ + static int extfs_read_archive (int fstype, const char *name, struct archive **pparc) { @@ -579,6 +608,8 @@ extfs_read_archive (int fstype, const char *name, struct archive **pparc) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_which (struct vfs_class *me, const char *path) { @@ -602,10 +633,12 @@ extfs_which (struct vfs_class *me, const char *path) return -1; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Dissect the path and create corresponding superblock. Note that inname * can be changed and the result may point inside the original string. */ + static char * extfs_get_path_mangle (struct vfs_class *me, char *inname, struct archive **archive, gboolean do_not_open) @@ -649,10 +682,12 @@ extfs_get_path_mangle (struct vfs_class *me, char *inname, struct archive **arch return local; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Dissect the path and create corresponding superblock. * The result should be freed. */ + static char * extfs_get_path (struct vfs_class *me, const char *inname, struct archive **archive, gboolean do_not_open) @@ -666,7 +701,9 @@ extfs_get_path (struct vfs_class *me, const char *inname, return res2; } +/* --------------------------------------------------------------------------------------------- */ /* Return allocated path (without leading slash) inside the archive */ + static char * extfs_get_path_from_entry (struct entry *entry) { @@ -685,6 +722,8 @@ extfs_get_path_from_entry (struct entry *entry) return g_string_free (localpath, FALSE); } +/* --------------------------------------------------------------------------------------------- */ + static struct entry * extfs_resolve_symlinks_int (struct entry *entry, GSList * list) { @@ -713,6 +752,8 @@ extfs_resolve_symlinks_int (struct entry *entry, GSList * list) return pent; } +/* --------------------------------------------------------------------------------------------- */ + static struct entry * extfs_resolve_symlinks (struct entry *entry) { @@ -731,6 +772,8 @@ extfs_resolve_symlinks (struct entry *entry) return res; } +/* --------------------------------------------------------------------------------------------- */ + static const char * extfs_get_archive_name (struct archive *archive) { @@ -747,7 +790,9 @@ extfs_get_archive_name (struct archive *archive) return archive_name; } -/* Don't pass localname as NULL */ +/* --------------------------------------------------------------------------------------------- */ +/** Don't pass localname as NULL */ + static int extfs_cmd (const char *str_extfs_cmd, struct archive *archive, struct entry *entry, const char *localname) @@ -780,6 +825,8 @@ extfs_cmd (const char *str_extfs_cmd, struct archive *archive, return retval; } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_run (struct vfs_class *me, const char *file) { @@ -803,6 +850,8 @@ extfs_run (struct vfs_class *me, const char *file) g_free (cmd); } +/* --------------------------------------------------------------------------------------------- */ + static void * extfs_open (struct vfs_class *me, const char *file, int flags, mode_t mode) { @@ -879,6 +928,8 @@ extfs_open (struct vfs_class *me, const char *file, int flags, mode_t mode) return extfs_info; } +/* --------------------------------------------------------------------------------------------- */ + static ssize_t extfs_read (void *data, char *buffer, size_t count) { @@ -887,6 +938,8 @@ extfs_read (void *data, char *buffer, size_t count) return read (file->local_handle, buffer, count); } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_close (void *data) { @@ -921,6 +974,8 @@ extfs_close (void *data) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_errno (struct vfs_class *me) { @@ -928,6 +983,8 @@ extfs_errno (struct vfs_class *me) return my_errno; } +/* --------------------------------------------------------------------------------------------- */ + static void * extfs_opendir (struct vfs_class *me, const char *dirname) { @@ -956,6 +1013,8 @@ extfs_opendir (struct vfs_class *me, const char *dirname) return info; } +/* --------------------------------------------------------------------------------------------- */ + static void * extfs_readdir (void *data) { @@ -973,6 +1032,8 @@ extfs_readdir (void *data) return (void *) &dir; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_closedir (void *data) { @@ -980,7 +1041,7 @@ extfs_closedir (void *data) return 0; } -#define RECORDSIZE 512 +/* --------------------------------------------------------------------------------------------- */ static void extfs_stat_move (struct stat *buf, const struct inode *inode) @@ -1006,6 +1067,8 @@ extfs_stat_move (struct stat *buf, const struct inode *inode) buf->st_ctime = inode->ctime; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, gboolean resolve) { @@ -1035,18 +1098,24 @@ extfs_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, g return result; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_stat (struct vfs_class *me, const char *path, struct stat *buf) { return extfs_internal_stat (me, path, buf, TRUE); } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) { return extfs_internal_stat (me, path, buf, FALSE); } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_fstat (void *data, struct stat *buf) { @@ -1056,6 +1125,8 @@ extfs_fstat (void *data, struct stat *buf) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) { @@ -1089,6 +1160,8 @@ extfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) return result; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { @@ -1099,6 +1172,8 @@ extfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_chmod (struct vfs_class *me, const char *path, int mode) { @@ -1108,6 +1183,8 @@ extfs_chmod (struct vfs_class *me, const char *path, int mode) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static ssize_t extfs_write (void *data, const char *buf, size_t nbyte) { @@ -1117,6 +1194,8 @@ extfs_write (void *data, const char *buf, size_t nbyte) return write (file->local_handle, buf, nbyte); } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_unlink (struct vfs_class *me, const char *file) { @@ -1153,6 +1232,8 @@ extfs_unlink (struct vfs_class *me, const char *file) return result; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_mkdir (struct vfs_class *me, const char *path, mode_t mode) { @@ -1198,6 +1279,8 @@ extfs_mkdir (struct vfs_class *me, const char *path, mode_t mode) return result; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_rmdir (struct vfs_class *me, const char *path) { @@ -1235,6 +1318,8 @@ extfs_rmdir (struct vfs_class *me, const char *path) return result; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_chdir (struct vfs_class *me, const char *path) { @@ -1257,6 +1342,8 @@ extfs_chdir (struct vfs_class *me, const char *path) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static off_t extfs_lseek (void *data, off_t offset, int whence) { @@ -1265,6 +1352,8 @@ extfs_lseek (void *data, off_t offset, int whence) return lseek (file->local_handle, offset, whence); } +/* --------------------------------------------------------------------------------------------- */ + static vfsid extfs_getid (struct vfs_class *me, const char *path) { @@ -1278,12 +1367,16 @@ extfs_getid (struct vfs_class *me, const char *path) return (vfsid) archive; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_nothingisopen (vfsid id) { return (((struct archive *) id)->fd_usage <= 0); } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_remove_entry (struct entry *e) { @@ -1327,6 +1420,8 @@ extfs_remove_entry (struct entry *e) g_free (e); } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_free_entry (struct entry *e) { @@ -1355,6 +1450,8 @@ extfs_free_entry (struct entry *e) g_free (e); } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_free (vfsid id) { @@ -1377,6 +1474,8 @@ extfs_free (vfsid id) extfs_free_archive (archive); } +/* --------------------------------------------------------------------------------------------- */ + static char * extfs_getlocalcopy (struct vfs_class *me, const char *path) { @@ -1397,6 +1496,8 @@ extfs_getlocalcopy (struct vfs_class *me, const char *path) return p; } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed) { @@ -1422,6 +1523,8 @@ extfs_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, } } +/* --------------------------------------------------------------------------------------------- */ + static gboolean extfs_get_plugins (const char *where, gboolean silent) { @@ -1520,6 +1623,7 @@ extfs_get_plugins (const char *where, gboolean silent) return TRUE; } +/* --------------------------------------------------------------------------------------------- */ static int extfs_init (struct vfs_class *me) @@ -1539,6 +1643,8 @@ extfs_init (struct vfs_class *me) return (d1 || d2 ? 1 : 0); } +/* --------------------------------------------------------------------------------------------- */ + static void extfs_done (struct vfs_class *me) { @@ -1564,6 +1670,8 @@ extfs_done (struct vfs_class *me) g_array_free (extfs_plugins, TRUE); } +/* --------------------------------------------------------------------------------------------- */ + static int extfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) { @@ -1577,6 +1685,10 @@ extfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) return 0; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void init_extfs (void) { @@ -1612,3 +1724,5 @@ init_extfs (void) vfs_extfs_ops.setctl = extfs_setctl; vfs_register_class (&vfs_extfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/fish.c b/lib/vfs/mc-vfs/fish.c index ff120a48d..25ba59d4d 100644 --- a/lib/vfs/mc-vfs/fish.c +++ b/lib/vfs/mc-vfs/fish.c @@ -6,9 +6,9 @@ Written by: 1998 Pavel Machek Spaces fix: 2000 Michal Svec - 2010 Andrew Borodin - 2010 Slava Zanko - 2010 Ilia Maslakov + 2010 Andrew Borodin + 2010 Slava Zanko + 2010 Ilia Maslakov Derived from ftpfs.c. @@ -76,8 +76,12 @@ #include "fishdef.h" #include "src/execute.h" /* pre_exec, post_exec */ +/*** global variables ****************************************************************************/ + int fish_directory_timeout = 900; +/*** file scope macro definitions ****************************************************************/ + #define DO_RESOLVE_SYMLINK 1 #define DO_OPEN 2 #define DO_FREE_RESOURCE 4 @@ -111,10 +115,33 @@ int fish_directory_timeout = 900; #define FISH_HAVE_DATE_MDYT 32 #define FISH_HAVE_TAIL 64 +#define SUP super->u.fish + +#define PREFIX \ + char buf[BUF_LARGE]; \ + const char *crpath; \ + char *rpath, *mpath; \ + struct vfs_s_super *super; \ + mpath = g_strdup (path); \ + crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \ + if (crpath == NULL) \ + { \ + g_free (mpath); \ + return -1; \ + } \ + rpath = strutils_shell_escape (crpath); \ + g_free (mpath); + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + static char reply_str[80]; static struct vfs_class vfs_fish_ops; +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static char * fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content) @@ -132,7 +159,8 @@ fish_load_script_from_file (const char *hostname, const char *script_name, const /* 2nd: scan system dir */ if (scr_content == NULL) { - scr_filename = g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL); + scr_filename = + g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL); g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL); g_free (scr_filename); } @@ -143,6 +171,8 @@ fish_load_script_from_file (const char *hostname, const char *script_name, const return g_strdup (def_content); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_decode_reply (char *s, int was_garbage) { @@ -157,7 +187,9 @@ fish_decode_reply (char *s, int was_garbage) return code / 100; } +/* --------------------------------------------------------------------------------------------- */ /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */ + static int fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len) { @@ -184,7 +216,7 @@ fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len } } -#define SUP super->u.fish +/* --------------------------------------------------------------------------------------------- */ static int fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...) @@ -222,6 +254,8 @@ fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, c return COMPLETE; } +/* --------------------------------------------------------------------------------------------- */ + static void fish_free_archive (struct vfs_class *me, struct vfs_s_super *super) { @@ -254,6 +288,8 @@ fish_free_archive (struct vfs_class *me, struct vfs_s_super *super) g_free (SUP.scr_env); } +/* --------------------------------------------------------------------------------------------- */ + static void fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[]) { @@ -291,6 +327,8 @@ fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[]) } } +/* --------------------------------------------------------------------------------------------- */ + static char * fish_set_env (int flags) { @@ -323,6 +361,8 @@ fish_set_env (int flags) return g_string_free (tmp, FALSE); } +/* --------------------------------------------------------------------------------------------- */ + static gboolean fish_info (struct vfs_class *me, struct vfs_s_super *super) { @@ -345,7 +385,9 @@ fish_info (struct vfs_class *me, struct vfs_s_super *super) } +/* --------------------------------------------------------------------------------------------- */ /* The returned directory should always contain a trailing slash */ + static char * fish_getcwd (struct vfs_class *me, struct vfs_s_super *super) { @@ -355,6 +397,8 @@ fish_getcwd (struct vfs_class *me, struct vfs_s_super *super) } +/* --------------------------------------------------------------------------------------------- */ + static void fish_open_archive_pipeopen (struct vfs_s_super *super) { @@ -399,6 +443,8 @@ fish_open_archive_pipeopen (struct vfs_s_super *super) fish_pipeopen (super, xsh, argv); } +/* --------------------------------------------------------------------------------------------- */ + static gboolean fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super) { @@ -443,6 +489,8 @@ fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super) return TRUE; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super) { @@ -499,6 +547,8 @@ fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_open_archive (struct vfs_class *me, struct vfs_s_super *super, const char *archive_name, char *op) @@ -530,7 +580,8 @@ fish_open_archive (struct vfs_class *me, struct vfs_s_super *super, SUP.scr_rmdir = fish_load_script_from_file (host, FISH_RMDIR_FILE, FISH_RMDIR_DEF_CONTENT); SUP.scr_ln = fish_load_script_from_file (host, FISH_LN_FILE, FISH_LN_DEF_CONTENT); SUP.scr_mv = fish_load_script_from_file (host, FISH_MV_FILE, FISH_MV_DEF_CONTENT); - SUP.scr_hardlink = fish_load_script_from_file (host, FISH_HARDLINK_FILE, FISH_HARDLINK_DEF_CONTENT); + SUP.scr_hardlink = + fish_load_script_from_file (host, FISH_HARDLINK_FILE, FISH_HARDLINK_DEF_CONTENT); SUP.scr_get = fish_load_script_from_file (host, FISH_GET_FILE, FISH_GET_DEF_CONTENT); SUP.scr_send = fish_load_script_from_file (host, FISH_SEND_FILE, FISH_SEND_DEF_CONTENT); SUP.scr_append = fish_load_script_from_file (host, FISH_APPEND_FILE, FISH_APPEND_DEF_CONTENT); @@ -538,6 +589,8 @@ fish_open_archive (struct vfs_class *me, struct vfs_s_super *super, return fish_open_archive_int (me, super); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_archive_same (struct vfs_class *me, struct vfs_s_super *super, const char *archive_name, char *op, void *cookie) @@ -567,6 +620,8 @@ fish_archive_same (struct vfs_class *me, struct vfs_s_super *super, return result; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path) { @@ -772,6 +827,8 @@ fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path) return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname) { @@ -892,6 +949,8 @@ fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *lo return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset) { @@ -934,6 +993,8 @@ fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset) return 1; } +/* --------------------------------------------------------------------------------------------- */ + static void fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh) { @@ -961,6 +1022,8 @@ fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh) print_vfs_message (_("Aborted transfer would be successful.")); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t len) { @@ -985,6 +1048,8 @@ fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t l ERRNOR (errno, n); } +/* --------------------------------------------------------------------------------------------- */ + static void fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh) { @@ -992,6 +1057,8 @@ fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh) fish_linear_abort (me, fh); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_ctl (void *fh, int ctlop, void *arg) { @@ -1022,6 +1089,8 @@ fish_ctl (void *fh, int ctlop, void *arg) #endif } +/* --------------------------------------------------------------------------------------------- */ + static int fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags) { @@ -1036,20 +1105,7 @@ fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char * return 0; } -#define PREFIX \ - char buf[BUF_LARGE]; \ - const char *crpath; \ - char *rpath, *mpath; \ - struct vfs_s_super *super; \ - mpath = g_strdup (path); \ - crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \ - if (crpath == NULL) \ - { \ - g_free (mpath); \ - return -1; \ - } \ - rpath = strutils_shell_escape (crpath); \ - g_free (mpath); +/* --------------------------------------------------------------------------------------------- */ static int fish_rename (struct vfs_class *me, const char *path1, const char *path2) @@ -1085,9 +1141,11 @@ fish_rename (struct vfs_class *me, const char *path1, const char *path2) g_free (shell_commands); g_free (rpath1); g_free (rpath2); - return fish_send_command(me, super2, buf, OPT_FLUSH); + return fish_send_command (me, super2, buf, OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_link (struct vfs_class *me, const char *path1, const char *path2) { @@ -1126,6 +1184,8 @@ fish_link (struct vfs_class *me, const char *path1, const char *path2) } +/* --------------------------------------------------------------------------------------------- */ + static int fish_symlink (struct vfs_class *me, const char *setto, const char *path) { @@ -1156,19 +1216,23 @@ fish_symlink (struct vfs_class *me, const char *setto, const char *path) return fish_send_command (me, super, buf, OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_chmod (struct vfs_class *me, const char *path, int mode) { gchar *shell_commands = NULL; PREFIX - shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n", - SUP.scr_chmod, (char *) NULL); + shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n", + SUP.scr_chmod, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath, mode & 07777); g_free (shell_commands); g_free (rpath); return fish_send_command (me, super, buf, OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { @@ -1190,9 +1254,9 @@ fish_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) gchar *shell_commands = NULL; PREFIX - shell_commands = g_strconcat (SUP.scr_env, - "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n", - SUP.scr_chown, (char *) NULL); + shell_commands = g_strconcat (SUP.scr_env, + "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n", + SUP.scr_chown, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup); g_free (shell_commands); fish_send_command (me, super, buf, OPT_FLUSH); @@ -1203,12 +1267,14 @@ fish_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) } } +/* --------------------------------------------------------------------------------------------- */ + static int fish_unlink (struct vfs_class *me, const char *path) { gchar *shell_commands = NULL; PREFIX - shell_commands = + shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_unlink, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath); g_free (shell_commands); @@ -1216,12 +1282,15 @@ fish_unlink (struct vfs_class *me, const char *path) return fish_send_command (me, super, buf, OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_exists (struct vfs_class *me, const char *path) { gchar *shell_commands = NULL; PREFIX - shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_exists, (char *) NULL); + shell_commands = + g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_exists, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath); g_free (shell_commands); g_free (rpath); @@ -1230,14 +1299,15 @@ fish_exists (struct vfs_class *me, const char *path) } +/* --------------------------------------------------------------------------------------------- */ + static int fish_mkdir (struct vfs_class *me, const char *path, mode_t mode) { gchar *shell_commands = NULL; int ret_code; - PREFIX - (void) mode; + PREFIX (void) mode; shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_mkdir, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath); @@ -1256,18 +1326,23 @@ fish_mkdir (struct vfs_class *me, const char *path, mode_t mode) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int fish_rmdir (struct vfs_class *me, const char *path) { gchar *shell_commands = NULL; PREFIX - shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_rmdir, (char *) NULL); + shell_commands = + g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_rmdir, (char *) NULL); g_snprintf (buf, sizeof (buf), shell_commands, rpath); g_free (shell_commands); g_free (rpath); return fish_send_command (me, super, buf, OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) { @@ -1296,6 +1371,8 @@ fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static void fish_fill_names (struct vfs_class *me, fill_names_f func) { @@ -1332,6 +1409,8 @@ fish_fill_names (struct vfs_class *me, fill_names_f func) } } +/* --------------------------------------------------------------------------------------------- */ + static void * fish_open (struct vfs_class *me, const char *file, int flags, mode_t mode) { @@ -1343,6 +1422,10 @@ fish_open (struct vfs_class *me, const char *file, int flags, mode_t mode) return vfs_s_open (me, file, flags, mode); } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void init_fish (void) { @@ -1377,3 +1460,5 @@ init_fish (void) vfs_fish_ops.ctl = fish_ctl; vfs_register_class (&vfs_fish_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/fish.h b/lib/vfs/mc-vfs/fish.h index bae43ca04..d015af76f 100644 --- a/lib/vfs/mc-vfs/fish.h +++ b/lib/vfs/mc-vfs/fish.h @@ -6,9 +6,21 @@ */ -#ifndef MC_VFS_FISH_H -#define MC_VFS_FISH_H +#ifndef MC__VFS_FISH_H +#define MC__VFS_FISH_H + +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ extern int fish_directory_timeout; +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ + #endif diff --git a/lib/vfs/mc-vfs/fishdef.h b/lib/vfs/mc-vfs/fishdef.h index 12fd15bf5..8ae1bd4dc 100644 --- a/lib/vfs/mc-vfs/fishdef.h +++ b/lib/vfs/mc-vfs/fishdef.h @@ -4,8 +4,10 @@ * \brief Header: FISH script defaults */ -#ifndef MC_FISH_DEF_H -#define MC_FISH_DEF_H +#ifndef MC__FISH_DEF_H +#define MC__FISH_DEF_H + +/*** typedefs(not structures) and defined constants **********************************************/ /* default 'ls' script */ #define FISH_LS_DEF_CONTENT "" \ @@ -205,5 +207,13 @@ "echo $res\n" \ "echo \"### 200\"\n" +/*** enums ***************************************************************************************/ +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/vfs/mc-vfs/ftpfs.c b/lib/vfs/mc-vfs/ftpfs.c index d0aed4f21..08f63f88f 100644 --- a/lib/vfs/mc-vfs/ftpfs.c +++ b/lib/vfs/mc-vfs/ftpfs.c @@ -3,12 +3,12 @@ 2006, 2007 Free Software Foundation, Inc. Written by: - 1995 Ching Hui - 1995 Jakub Jelinek - 1995, 1996, 1997 Miguel de Icaza - 1997 Norbert Warmuth - 1998 Pavel Machek - 2010 Yury V. Zaytsev + 1995 Ching Hui + 1995 Jakub Jelinek + 1995, 1996, 1997 Miguel de Icaza + 1997 Norbert Warmuth + 1998 Pavel Machek + 2010 Yury V. Zaytsev This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License @@ -103,33 +103,7 @@ What to do with this? #include "ftpfs.h" -#ifndef MAXHOSTNAMELEN -# define MAXHOSTNAMELEN 64 -#endif - -#define UPLOAD_ZERO_LENGTH_FILE -#define SUP super->u.ftp -#define FH_SOCK fh->u.ftp.sock - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -/* for uclibc < 0.9.29 */ -#ifndef AI_ADDRCONFIG -#define AI_ADDRCONFIG 0x0020 -#endif - -#define RFC_AUTODETECT 0 -#define RFC_DARING 1 -#define RFC_STRICT 2 - -#ifndef HAVE_SOCKLEN_T -typedef int socklen_t; -#endif - -static int ftpfs_errno; -static int code; +/*** global variables ****************************************************************************/ /* Delay to retry a connection */ int ftpfs_retry_seconds = 30; @@ -140,8 +114,8 @@ int ftpfs_use_passive_connections_over_proxy = 0; /* Method used to get directory listings: * 1: try 'LIST -la ', if it fails - * fall back to CWD ; LIST - * 0: always use CWD ; LIST + * fall back to CWD ; LIST + * 0: always use CWD ; LIST */ int ftpfs_use_unix_list_options = 1; @@ -163,18 +137,92 @@ int ftpfs_always_use_proxy = 0; int ftpfs_ignore_chattr_errors = 1; -#ifdef FIXME_LATER_ALIGATOR -static struct linklist *connections_list; +/*** file scope macro definitions ****************************************************************/ + +#ifndef MAXHOSTNAMELEN +#define MAXHOSTNAMELEN 64 #endif +#define UPLOAD_ZERO_LENGTH_FILE +#define SUP super->u.ftp +#define FH_SOCK fh->u.ftp.sock + +#ifndef INADDR_NONE +#define INADDR_NONE 0xffffffff +#endif + +/* for uclibc < 0.9.29 */ +#ifndef AI_ADDRCONFIG +#define AI_ADDRCONFIG 0x0020 +#endif + +#define RFC_AUTODETECT 0 +#define RFC_DARING 1 +#define RFC_STRICT 2 + /* ftpfs_command wait_flag: */ #define NONE 0x00 #define WAIT_REPLY 0x01 #define WANT_STRING 0x02 + +#define FTP_COMMAND_PORT 21 + +/* some defines only used by ftpfs_changetype */ +/* These two are valid values for the second parameter */ +#define TYPE_ASCII 0 +#define TYPE_BINARY 1 + +/* This one is only used to initialize bucket->isbinary, don't use it as + second parameter to ftpfs_changetype. */ +#define TYPE_UNKNOWN -1 + +#define ABORT_TIMEOUT 5 +/*** file scope type declarations ****************************************************************/ + +#ifndef HAVE_SOCKLEN_T +typedef int socklen_t; +#endif + +/* This should match the keywords[] array below */ +typedef enum +{ + NETRC_NONE = 0, + NETRC_DEFAULT, + NETRC_MACHINE, + NETRC_LOGIN, + NETRC_PASSWORD, + NETRC_PASSWD, + NETRC_ACCOUNT, + NETRC_MACDEF, + NETRC_UNKNOWN +} keyword_t; + +/*** file scope variables ************************************************************************/ + +static int ftpfs_errno; +static int code; + +#ifdef FIXME_LATER_ALIGATOR +static struct linklist *connections_list; +#endif + static char reply_str[80]; static struct vfs_class vfs_ftpfs_ops; +static struct no_proxy_entry +{ + char *domain; + void *next; +} *no_proxy; + +static char buffer[BUF_MEDIUM]; +static char *netrc; +static const char *netrcp; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + /* char *ftpfs_translate_path (struct ftpfs_connection *bucket, char *remote_path) Translate a Unix path, i.e. MC's internal path representation (e.g. /somedir/somefile) to a path valid for the remote server. Every path @@ -198,6 +246,8 @@ static int ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super, const char *netrcpass); static int ftpfs_netrc_lookup (const char *host, char **login, char **pass); +/* --------------------------------------------------------------------------------------------- */ + static char * ftpfs_translate_path (struct vfs_class *me, struct vfs_s_super *super, const char *remote_path) { @@ -240,8 +290,8 @@ ftpfs_translate_path (struct vfs_class *me, struct vfs_s_super *super, const cha } } -/* Extract the hostname and username from the path */ - +/* --------------------------------------------------------------------------------------------- */ +/** Extract the hostname and username from the path */ /* * path is in the form: [user@]hostname:port/remote-dir, e.g.: * ftp://sunsite.unc.edu/pub/linux @@ -253,8 +303,6 @@ ftpfs_translate_path (struct vfs_class *me, struct vfs_s_super *super, const cha * */ -#define FTP_COMMAND_PORT 21 - static void ftpfs_split_url (char *path, char **host, char **user, int *port, char **pass) { @@ -291,7 +339,9 @@ ftpfs_split_url (char *path, char **host, char **user, int *port, char **pass) g_free (p); } +/* --------------------------------------------------------------------------------------------- */ /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */ + static int ftpfs_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len) { @@ -337,6 +387,8 @@ ftpfs_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_le } } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_reconnect (struct vfs_class *me, struct vfs_s_super *super) { @@ -360,6 +412,8 @@ ftpfs_reconnect (struct vfs_class *me, struct vfs_s_super *super) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...) @@ -449,6 +503,8 @@ ftpfs_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, return COMPLETE; } +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_free_archive (struct vfs_class *me, struct vfs_s_super *super) { @@ -464,14 +520,7 @@ ftpfs_free_archive (struct vfs_class *me, struct vfs_s_super *super) g_free (SUP.password); } -/* some defines only used by ftpfs_changetype */ -/* These two are valid values for the second parameter */ -#define TYPE_ASCII 0 -#define TYPE_BINARY 1 - -/* This one is only used to initialize bucket->isbinary, don't use it as - second parameter to ftpfs_changetype. */ -#define TYPE_UNKNOWN -1 +/* --------------------------------------------------------------------------------------------- */ static int ftpfs_changetype (struct vfs_class *me, struct vfs_s_super *super, int binary) @@ -485,7 +534,9 @@ ftpfs_changetype (struct vfs_class *me, struct vfs_s_super *super, int binary) return binary; } +/* --------------------------------------------------------------------------------------------- */ /* This routine logs the user in */ + static int ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super, const char *netrcpass) { @@ -598,11 +649,7 @@ ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super, const char ERRNOR (EPERM, 0); } -static struct no_proxy_entry -{ - char *domain; - void *next; -} *no_proxy; +/* --------------------------------------------------------------------------------------------- */ static void ftpfs_load_no_proxy_list (void) @@ -627,7 +674,7 @@ ftpfs_load_no_proxy_list (void) while (fgets (s, sizeof (s), npf) != NULL) { p = strchr (s, '\n'); - if (p == NULL) /* skip bogus entries */ + if (p == NULL) /* skip bogus entries */ { while ((c = fgetc (npf)) != EOF && c != '\n') ; @@ -641,7 +688,7 @@ ftpfs_load_no_proxy_list (void) np = g_new (struct no_proxy_entry, 1); np->domain = g_strdup (s); - np->next = NULL; + np->next = NULL; if (no_proxy) current->next = np; else @@ -654,7 +701,9 @@ ftpfs_load_no_proxy_list (void) g_free (mc_file); } +/* --------------------------------------------------------------------------------------------- */ /* Return 1 if FTP proxy should be used for this host, 0 otherwise */ + static int ftpfs_check_proxy (const char *host) { @@ -698,6 +747,8 @@ ftpfs_check_proxy (const char *host) return 1; } +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_get_proxy_host_and_port (const char *proxy, char **host, int *port) { @@ -708,6 +759,8 @@ ftpfs_get_proxy_host_and_port (const char *proxy, char **host, int *port) g_free (dir); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super) { @@ -820,6 +873,8 @@ ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super) return my_socket; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_open_archive_int (struct vfs_class *me, struct vfs_s_super *super) { @@ -882,6 +937,8 @@ ftpfs_open_archive_int (struct vfs_class *me, struct vfs_s_super *super) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_open_archive (struct vfs_class *me, struct vfs_s_super *super, const char *archive_name, char *op) @@ -911,6 +968,8 @@ ftpfs_open_archive (struct vfs_class *me, struct vfs_s_super *super, return ftpfs_open_archive_int (me, super); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_archive_same (struct vfs_class *me, struct vfs_s_super *super, const char *archive_name, char *op, void *cookie) @@ -932,7 +991,9 @@ ftpfs_archive_same (struct vfs_class *me, struct vfs_s_super *super, return port; } +/* --------------------------------------------------------------------------------------------- */ /* The returned directory should always contain a trailing slash */ + static char * ftpfs_get_current_directory (struct vfs_class *me, struct vfs_s_super *super) { @@ -981,10 +1042,12 @@ ftpfs_get_current_directory (struct vfs_class *me, struct vfs_s_super *super) return NULL; } +/* --------------------------------------------------------------------------------------------- */ /* Setup Passive PASV FTP connection */ + static int ftpfs_setup_passive_pasv (struct vfs_class *me, struct vfs_s_super *super, - int my_socket, struct sockaddr_storage *sa, socklen_t *salen) + int my_socket, struct sockaddr_storage *sa, socklen_t * salen) { char *c; char n[6]; @@ -1019,10 +1082,12 @@ ftpfs_setup_passive_pasv (struct vfs_class *me, struct vfs_s_super *super, return 1; } +/* --------------------------------------------------------------------------------------------- */ /* Setup Passive EPSV FTP connection */ + static int ftpfs_setup_passive_epsv (struct vfs_class *me, struct vfs_s_super *super, - int my_socket, struct sockaddr_storage *sa, socklen_t *salen) + int my_socket, struct sockaddr_storage *sa, socklen_t * salen) { char *c; int port; @@ -1060,10 +1125,12 @@ ftpfs_setup_passive_epsv (struct vfs_class *me, struct vfs_s_super *super, return 1; } +/* --------------------------------------------------------------------------------------------- */ /* Setup Passive ftp connection, we use it for source routed connections */ + static int ftpfs_setup_passive (struct vfs_class *me, struct vfs_s_super *super, - int my_socket, struct sockaddr_storage *sa, socklen_t *salen) + int my_socket, struct sockaddr_storage *sa, socklen_t * salen) { /* It's IPV4, so try PASV first, some servers and ALGs get confused by EPSV */ if (sa->ss_family == AF_INET) @@ -1083,7 +1150,9 @@ ftpfs_setup_passive (struct vfs_class *me, struct vfs_s_super *super, return 1; } +/* --------------------------------------------------------------------------------------------- */ /* Setup Active PORT or EPRT FTP connection */ + static int ftpfs_setup_active (struct vfs_class *me, struct vfs_s_super *super, struct sockaddr_storage data_addr, socklen_t data_addrlen) @@ -1102,7 +1171,7 @@ ftpfs_setup_active (struct vfs_class *me, struct vfs_s_super *super, af = FTP_INET6; port = ((struct sockaddr_in6 *) &data_addr)->sin6_port; break; - /* Not implemented */ + /* Not implemented */ default: return 0; } @@ -1153,10 +1222,12 @@ ftpfs_setup_active (struct vfs_class *me, struct vfs_s_super *super, return 0; } +/* --------------------------------------------------------------------------------------------- */ /* Initialize a socket for FTP DATA connection */ + static int ftpfs_init_data_socket (struct vfs_class *me, struct vfs_s_super *super, - struct sockaddr_storage *data_addr, socklen_t *data_addrlen) + struct sockaddr_storage *data_addr, socklen_t * data_addrlen) { int result; @@ -1190,11 +1261,14 @@ ftpfs_init_data_socket (struct vfs_class *me, struct vfs_s_super *super, { print_vfs_message (_("ftpfs: could not create socket: %s"), unix_error_string (errno)); return -1; - } else + } + else return result; } +/* --------------------------------------------------------------------------------------------- */ /* Initialize FTP DATA connection */ + static int ftpfs_initconn (struct vfs_class *me, struct vfs_s_super *super) { @@ -1247,19 +1321,21 @@ ftpfs_initconn (struct vfs_class *me, struct vfs_s_super *super) /* Restore the initial value of use_passive_connection (for subsequent retries) */ SUP.use_passive_connection = SUP.proxy ? ftpfs_use_passive_connections_over_proxy : - ftpfs_use_passive_connections; + ftpfs_use_passive_connections; ftpfs_errno = EIO; return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_open_data_connection (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, const char *remote, int isbinary, int reget) { struct sockaddr_storage from; int s, j, data; - socklen_t fromlen = sizeof(from); + socklen_t fromlen = sizeof (from); s = ftpfs_initconn (me, super); if (s == -1) @@ -1304,7 +1380,8 @@ ftpfs_open_data_connection (struct vfs_class *me, struct vfs_s_super *super, con return data; } -#define ABORT_TIMEOUT 5 +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh) { @@ -1359,6 +1436,8 @@ ftpfs_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh) ftpfs_get_reply (me, SUP.sock, NULL, 0); } +/* --------------------------------------------------------------------------------------------- */ + #if 0 static void resolve_symlink_without_ls_options (struct vfs_class *me, struct vfs_s_super *super, @@ -1435,6 +1514,8 @@ resolve_symlink_without_ls_options (struct vfs_class *me, struct vfs_s_super *su dir->symlink_status = FTPFS_RESOLVED_SYMLINKS; } +/* --------------------------------------------------------------------------------------------- */ + static void resolve_symlink_with_ls_options (struct vfs_class *me, struct vfs_s_super *super, struct vfs_s_inode *dir) @@ -1526,6 +1607,8 @@ resolve_symlink_with_ls_options (struct vfs_class *me, struct vfs_s_super *super ftpfs_get_reply (me, SUP.sock, NULL, 0); } +/* --------------------------------------------------------------------------------------------- */ + static void resolve_symlink (struct vfs_class *me, struct vfs_s_super *super, struct vfs_s_inode *dir) { @@ -1538,6 +1621,8 @@ resolve_symlink (struct vfs_class *me, struct vfs_s_super *super, struct vfs_s_i } #endif +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path) { @@ -1668,6 +1753,8 @@ ftpfs_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path ERRNOR (EACCES, -1); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname) { @@ -1740,7 +1827,7 @@ ftpfs_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *l n_read -= n_written; } print_vfs_message (_("ftpfs: storing file %ju (%ju)"), - (uintmax_t) n_stored, (uintmax_t) s.st_size); + (uintmax_t) n_stored, (uintmax_t) s.st_size); } tty_disable_interrupt_key (); close (sock); @@ -1756,6 +1843,8 @@ ftpfs_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *l return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset) { @@ -1773,6 +1862,8 @@ ftpfs_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset) return 1; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t len) { @@ -1801,6 +1892,8 @@ ftpfs_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t ERRNOR (errno, n); } +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_linear_close (struct vfs_class *me, struct vfs_s_fh *fh) { @@ -1808,6 +1901,8 @@ ftpfs_linear_close (struct vfs_class *me, struct vfs_s_fh *fh) ftpfs_linear_abort (me, fh); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_ctl (void *fh, int ctlop, void *arg) { @@ -1834,6 +1929,8 @@ ftpfs_ctl (void *fh, int ctlop, void *arg) } } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_send_command (struct vfs_class *me, const char *filename, const char *cmd, int flags) { @@ -1867,24 +1964,7 @@ ftpfs_send_command (struct vfs_class *me, const char *filename, const char *cmd, return 0; } -/* This routine is called as the last step in load_setup */ -void -ftpfs_init_passwd (void) -{ - ftpfs_anonymous_passwd = load_anon_passwd (); - if (ftpfs_anonymous_passwd) - return; - - /* If there is no anonymous ftp password specified - * then we'll just use anonymous@ - * We don't send any other thing because: - * - We want to remain anonymous - * - We want to stop SPAM - * - We don't want to let ftp sites to discriminate by the user, - * host or country. - */ - ftpfs_anonymous_passwd = g_strdup ("anonymous@"); -} +/* --------------------------------------------------------------------------------------------- */ static int ftpfs_chmod (struct vfs_class *me, const char *path, int mode) @@ -1899,6 +1979,8 @@ ftpfs_chmod (struct vfs_class *me, const char *path, int mode) return ftpfs_ignore_chattr_errors ? 0 : ret; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { @@ -1916,12 +1998,16 @@ ftpfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) #endif } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_unlink (struct vfs_class *me, const char *path) { return ftpfs_send_command (me, path, "DELE /%s", OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + /* Return 1 if path is the same directory as the one we are in now */ static int ftpfs_is_same_dir (struct vfs_class *me, struct vfs_s_super *super, const char *path) @@ -1935,6 +2021,8 @@ ftpfs_is_same_dir (struct vfs_class *me, struct vfs_s_super *super, const char * return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_chdir_internal (struct vfs_class *me, struct vfs_s_super *super, const char *remote_path) { @@ -1961,6 +2049,8 @@ ftpfs_chdir_internal (struct vfs_class *me, struct vfs_s_super *super, const cha return r; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_rename (struct vfs_class *me, const char *path1, const char *path2) { @@ -1968,6 +2058,8 @@ ftpfs_rename (struct vfs_class *me, const char *path1, const char *path2) return ftpfs_send_command (me, path2, "RNTO /%s", OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_mkdir (struct vfs_class *me, const char *path, mode_t mode) { @@ -1976,12 +2068,16 @@ ftpfs_mkdir (struct vfs_class *me, const char *path, mode_t mode) return ftpfs_send_command (me, path, "MKD /%s", OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_rmdir (struct vfs_class *me, const char *path) { return ftpfs_send_command (me, path, "RMD /%s", OPT_FLUSH); } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) { @@ -2048,6 +2144,8 @@ ftpfs_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_fh_close (struct vfs_class *me, struct vfs_s_fh *fh) { @@ -2066,6 +2164,8 @@ ftpfs_fh_close (struct vfs_class *me, struct vfs_s_fh *fh) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_done (struct vfs_class *me) { @@ -2084,6 +2184,8 @@ ftpfs_done (struct vfs_class *me) g_free (ftpfs_proxy_host); } +/* --------------------------------------------------------------------------------------------- */ + static void ftpfs_fill_names (struct vfs_class *me, fill_names_f func) { @@ -2099,23 +2201,7 @@ ftpfs_fill_names (struct vfs_class *me, fill_names_f func) } } -static char buffer[BUF_MEDIUM]; -static char *netrc; -static const char *netrcp; - -/* This should match the keywords[] array below */ -typedef enum -{ - NETRC_NONE = 0, - NETRC_DEFAULT, - NETRC_MACHINE, - NETRC_LOGIN, - NETRC_PASSWORD, - NETRC_PASSWD, - NETRC_ACCOUNT, - NETRC_MACDEF, - NETRC_UNKNOWN -} keyword_t; +/* --------------------------------------------------------------------------------------------- */ static keyword_t ftpfs_netrc_next (void) @@ -2172,6 +2258,8 @@ ftpfs_netrc_next (void) return NETRC_UNKNOWN; } +/* --------------------------------------------------------------------------------------------- */ + static int ftpfs_netrc_bad_mode (const char *netrcname) { @@ -2191,10 +2279,12 @@ ftpfs_netrc_bad_mode (const char *netrcname) return 0; } +/* --------------------------------------------------------------------------------------------- */ /* Scan .netrc until we find matching "machine" or "default" * domain is used for additional matching * No search is done after "default" in compliance with "man netrc" * Return 0 if found, -1 otherwise */ + static int ftpfs_find_machine (const char *host, const char *domain) { @@ -2255,9 +2345,11 @@ ftpfs_find_machine (const char *host, const char *domain) return -1; } +/* --------------------------------------------------------------------------------------------- */ /* Extract login and password from .netrc for the host. * pass may be NULL. * Returns 0 for success, -1 for error */ + static int ftpfs_netrc_lookup (const char *host, char **login, char **pass) { @@ -2405,6 +2497,31 @@ ftpfs_netrc_lookup (const char *host, char **login, char **pass) return 0; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/** This routine is called as the last step in load_setup */ +void +ftpfs_init_passwd (void) +{ + ftpfs_anonymous_passwd = load_anon_passwd (); + if (ftpfs_anonymous_passwd) + return; + + /* If there is no anonymous ftp password specified + * then we'll just use anonymous@ + * We don't send any other thing because: + * - We want to remain anonymous + * - We want to stop SPAM + * - We don't want to let ftp sites to discriminate by the user, + * host or country. + */ + ftpfs_anonymous_passwd = g_strdup ("anonymous@"); +} + +/* --------------------------------------------------------------------------------------------- */ + void init_ftpfs (void) { @@ -2439,3 +2556,5 @@ init_ftpfs (void) vfs_ftpfs_ops.ctl = ftpfs_ctl; vfs_register_class (&vfs_ftpfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/ftpfs.h b/lib/vfs/mc-vfs/ftpfs.h index 9f1750293..f76253ea5 100644 --- a/lib/vfs/mc-vfs/ftpfs.h +++ b/lib/vfs/mc-vfs/ftpfs.h @@ -1,11 +1,24 @@ - /** * \file * \brief Header: Virtual File System: FTP file system */ -#ifndef MC_VFS_FTPFS_H -#define MC_VFS_FTPFS_H +#ifndef MC__VFS_FTPFS_H +#define MC__VFS_FTPFS_H + +/*** typedefs(not structures) and defined constants **********************************************/ + +#define FTP_INET 1 +#define FTP_INET6 2 + +#define OPT_FLUSH 1 +#define OPT_IGNORE_ERROR 2 + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ extern int ftpfs_use_netrc; extern char *ftpfs_anonymous_passwd; @@ -20,13 +33,10 @@ extern int ftpfs_use_passive_connections_over_proxy; extern int ftpfs_use_unix_list_options; extern int ftpfs_first_cd_then_ls; +/*** declarations of public functions ************************************************************/ + void ftpfs_init_passwd (void); void init_ftpfs (void); -#define FTP_INET 1 -#define FTP_INET6 2 - -#define OPT_FLUSH 1 -#define OPT_IGNORE_ERROR 2 - +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/vfs/mc-vfs/gc.c b/lib/vfs/mc-vfs/gc.c index 9f03fec40..2f08618c4 100644 --- a/lib/vfs/mc-vfs/gc.c +++ b/lib/vfs/mc-vfs/gc.c @@ -2,9 +2,9 @@ Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc. Written by: 1995 Miguel de Icaza - 1995 Jakub Jelinek - 1998 Pavel Machek - 2003 Pavel Roskin + 1995 Jakub Jelinek + 1998 Pavel Machek + 2003 Pavel Roskin This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License @@ -34,59 +34,89 @@ #include #include -#include /* For atol() */ +#include /* For atol() */ #include #include #include #include -#include /* is_digit() */ -#include /* gettimeofday() */ +#include /* is_digit() */ +#include /* gettimeofday() */ #include "lib/global.h" -#include "src/panel.h" /* current_panel */ -#include "src/layout.h" /* get_current_type(), get_other_type() */ +#include "src/panel.h" /* current_panel */ +#include "src/layout.h" /* get_current_type(), get_other_type() */ #include "vfs-impl.h" #include "utilvfs.h" #include "gc.h" -int vfs_timeout = 60; /* VFS timeout in seconds */ +/*** global variables ****************************************************************************/ + +int vfs_timeout = 60; /* VFS timeout in seconds */ + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ static struct vfs_stamping *stamps; +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + static void vfs_addstamp (struct vfs_class *v, vfsid id) { - if (!(v->flags & VFSF_LOCAL) && id != NULL) { - struct vfs_stamping *stamp; - struct vfs_stamping *last_stamp = NULL; + if (!(v->flags & VFSF_LOCAL) && id != NULL) + { + struct vfs_stamping *stamp; + struct vfs_stamping *last_stamp = NULL; - for (stamp = stamps; stamp != NULL; stamp = stamp->next) { - if (stamp->v == v && stamp->id == id) { - gettimeofday (&(stamp->time), NULL); - return; - } - last_stamp = stamp; - } - stamp = g_new (struct vfs_stamping, 1); - stamp->v = v; - stamp->id = id; + for (stamp = stamps; stamp != NULL; stamp = stamp->next) + { + if (stamp->v == v && stamp->id == id) + { + gettimeofday (&(stamp->time), NULL); + return; + } + last_stamp = stamp; + } + stamp = g_new (struct vfs_stamping, 1); + stamp->v = v; + stamp->id = id; - gettimeofday (&(stamp->time), NULL); - stamp->next = 0; + gettimeofday (&(stamp->time), NULL); + stamp->next = 0; - if (stamps) { - /* Add to the end */ - last_stamp->next = stamp; - } else { - /* Add first element */ - stamps = stamp; - } + if (stamps) + { + /* Add to the end */ + last_stamp->next = stamp; + } + else + { + /* Add first element */ + stamps = stamp; + } } } +/* --------------------------------------------------------------------------------------------- */ +/** Compare two timeval structures. Return 0 is t1 is less than t2. */ + +static inline int +timeoutcmp (struct timeval *t1, struct timeval *t2) +{ + return ((t1->tv_sec < t2->tv_sec) + || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec))); +} + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ void vfs_stamp (struct vfs_class *v, vfsid id) @@ -94,32 +124,38 @@ vfs_stamp (struct vfs_class *v, vfsid id) struct vfs_stamping *stamp; for (stamp = stamps; stamp != NULL; stamp = stamp->next) - if (stamp->v == v && stamp->id == id) { - gettimeofday (&(stamp->time), NULL); - return; - } + if (stamp->v == v && stamp->id == id) + { + gettimeofday (&(stamp->time), NULL); + return; + } } +/* --------------------------------------------------------------------------------------------- */ void vfs_rmstamp (struct vfs_class *v, vfsid id) { struct vfs_stamping *stamp, *st1; - for (stamp = stamps, st1 = NULL; stamp != NULL; - st1 = stamp, stamp = stamp->next) - if (stamp->v == v && stamp->id == id) { - if (st1 == NULL) { - stamps = stamp->next; - } else { - st1->next = stamp->next; - } - g_free (stamp); + for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next) + if (stamp->v == v && stamp->id == id) + { + if (st1 == NULL) + { + stamps = stamp->next; + } + else + { + st1->next = stamp->next; + } + g_free (stamp); - return; - } + return; + } } +/* --------------------------------------------------------------------------------------------- */ void vfs_stamp_path (const char *path) @@ -132,10 +168,11 @@ vfs_stamp_path (const char *path) vfs_addstamp (vfs, id); } - -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Create a new timestamp item by VFS class and VFS id. */ + void vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid) { @@ -148,54 +185,53 @@ vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid) same, it's possible that all three are different -- Norbert */ if (!current_panel) - return; + return; nvfs = vfs_get_class (vfs_get_current_dir ()); nvfsid = vfs_getid (nvfs, vfs_get_current_dir ()); vfs_rmstamp (nvfs, nvfsid); - if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL) { - return; + if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL) + { + return; } - if (get_current_type () == view_listing) { - n2vfs = vfs_get_class (current_panel->cwd); - n2vfsid = vfs_getid (n2vfs, current_panel->cwd); - if (n2vfs == oldvfs && n2vfsid == oldvfsid) - return; - } else { - n2vfs = NULL; - n2vfsid = NULL; + if (get_current_type () == view_listing) + { + n2vfs = vfs_get_class (current_panel->cwd); + n2vfsid = vfs_getid (n2vfs, current_panel->cwd); + if (n2vfs == oldvfs && n2vfsid == oldvfsid) + return; + } + else + { + n2vfs = NULL; + n2vfsid = NULL; } - if (get_other_type () == view_listing) { - n3vfs = vfs_get_class (other_panel->cwd); - n3vfsid = vfs_getid (n3vfs, other_panel->cwd); - if (n3vfs == oldvfs && n3vfsid == oldvfsid) - return; - } else { - n3vfs = NULL; - n3vfsid = NULL; + if (get_other_type () == view_listing) + { + n3vfs = vfs_get_class (other_panel->cwd); + n3vfsid = vfs_getid (n3vfs, other_panel->cwd); + if (n3vfs == oldvfs && n3vfsid == oldvfsid) + return; + } + else + { + n3vfs = NULL; + n3vfsid = NULL; } if (!oldvfs->nothingisopen || !(*oldvfs->nothingisopen) (oldvfsid)) - return; + return; vfs_addstamp (oldvfs, oldvfsid); } -/* Compare two timeval structures. Return 0 is t1 is less than t2. */ -static inline int -timeoutcmp (struct timeval *t1, struct timeval *t2) -{ - return ((t1->tv_sec < t2->tv_sec) - || ((t1->tv_sec == t2->tv_sec) - && (t1->tv_usec <= t2->tv_usec))); -} - - -/* This is called from timeout handler with now = 0, or can be called +/* --------------------------------------------------------------------------------------------- */ +/** This is called from timeout handler with now = 0, or can be called with now = 1 to force freeing all filesystems that are not in use */ + void vfs_expire (int now) { @@ -206,37 +242,42 @@ vfs_expire (int now) /* Avoid recursive invocation, e.g. when one of the free functions calls message */ if (locked) - return; + return; locked = 1; gettimeofday (&lc_time, NULL); lc_time.tv_sec -= vfs_timeout; - for (stamp = stamps; stamp != NULL;) { - if (now || (timeoutcmp (&stamp->time, &lc_time))) { - st = stamp->next; - if (stamp->v->free) - (*stamp->v->free) (stamp->id); - vfs_rmstamp (stamp->v, stamp->id); - stamp = st; - } else - stamp = stamp->next; + for (stamp = stamps; stamp != NULL;) + { + if (now || (timeoutcmp (&stamp->time, &lc_time))) + { + st = stamp->next; + if (stamp->v->free) + (*stamp->v->free) (stamp->id); + vfs_rmstamp (stamp->v, stamp->id); + stamp = st; + } + else + stamp = stamp->next; } locked = 0; } - +/* --------------------------------------------------------------------------------------------- */ /* * Return the number of seconds remaining to the vfs timeout. * FIXME: The code should be improved to actually return the number of * seconds until the next item times out. */ + int vfs_timeouts () { return stamps ? 10 : 0; } +/* --------------------------------------------------------------------------------------------- */ void vfs_timeout_handler (void) @@ -244,6 +285,7 @@ vfs_timeout_handler (void) vfs_expire (0); } +/* --------------------------------------------------------------------------------------------- */ void vfs_release_path (const char *dir) @@ -256,21 +298,25 @@ vfs_release_path (const char *dir) vfs_stamp_create (oldvfs, oldvfsid); } - +/* --------------------------------------------------------------------------------------------- */ /* Free all data */ + void vfs_gc_done (void) { struct vfs_stamping *stamp, *st; - for (stamp = stamps, stamps = 0; stamp != NULL;) { - if (stamp->v->free) - (*stamp->v->free) (stamp->id); - st = stamp->next; - g_free (stamp); - stamp = st; + for (stamp = stamps, stamps = 0; stamp != NULL;) + { + if (stamp->v->free) + (*stamp->v->free) (stamp->id); + st = stamp->next; + g_free (stamp); + stamp = st; } if (stamps) - vfs_rmstamp (stamps->v, stamps->id); + vfs_rmstamp (stamps->v, stamps->id); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/gc.h b/lib/vfs/mc-vfs/gc.h index aab68d9d9..8d6262d72 100644 --- a/lib/vfs/mc-vfs/gc.h +++ b/lib/vfs/mc-vfs/gc.h @@ -1,14 +1,19 @@ - /** * \file * \brief Header: Virtual File System: garbage collection code */ -#ifndef MC_VFS_GC_H -#define MC_VFS_GC_H +#ifndef MC__VFS_GC_H +#define MC__VFS_GC_H #include "vfs-impl.h" +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + struct vfs_stamping { struct vfs_class *v; @@ -17,10 +22,15 @@ struct vfs_stamping struct timeval time; }; +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + void vfs_stamp (struct vfs_class *vclass, vfsid id); void vfs_rmstamp (struct vfs_class *vclass, vfsid id); void vfs_stamp_create (struct vfs_class *vclass, vfsid id); vfsid vfs_getid (struct vfs_class *vclass, const char *dir); void vfs_gc_done (void); +/*** inline functions ****************************************************************************/ #endif /* MC_VFS_GC_H */ diff --git a/lib/vfs/mc-vfs/local.c b/lib/vfs/mc-vfs/local.c index 283c492d8..ec19502b9 100644 --- a/lib/vfs/mc-vfs/local.c +++ b/lib/vfs/mc-vfs/local.c @@ -19,13 +19,27 @@ #include "local.h" +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +static struct vfs_class vfs_local_ops; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + /** * Note: Some of this functions are not static. This has rather good * reason: exactly same functions would have to appear in sfs.c. This * saves both computer's memory and my work. */ -static struct vfs_class vfs_local_ops; + +/* --------------------------------------------------------------------------------------------- */ static void * local_open (struct vfs_class *me, const char *file, int flags, mode_t mode) @@ -37,7 +51,7 @@ local_open (struct vfs_class *me, const char *file, int flags, mode_t mode) fd = open (file, NO_LINEAR (flags), mode); if (fd == -1) - return 0; + return 0; local_info = g_new (int, 1); *local_info = fd; @@ -45,45 +59,7 @@ local_open (struct vfs_class *me, const char *file, int flags, mode_t mode) return local_info; } -ssize_t -local_read (void *data, char *buffer, size_t count) -{ - int n; - - if (!data) - return -1; - - while ((n = read (*((int *) data), buffer, count)) == -1){ -#ifdef EAGAIN - if (errno == EAGAIN) continue; -#endif -#ifdef EINTR - if (errno == EINTR) continue; -#endif - return -1; - } - return n; -} - -int -local_close (void *data) -{ - int fd; - - if (!data) - return -1; - - fd = *(int *) data; - g_free (data); - return close (fd); -} - -int -local_errno (struct vfs_class *me) -{ - (void) me; - return errno; -} +/* --------------------------------------------------------------------------------------------- */ static void * local_opendir (struct vfs_class *me, const char *dirname) @@ -95,30 +71,36 @@ local_opendir (struct vfs_class *me, const char *dirname) dir = opendir (dirname); if (!dir) - return 0; + return 0; local_info = (DIR **) g_new (DIR *, 1); *local_info = dir; - + return local_info; } +/* --------------------------------------------------------------------------------------------- */ + static void * local_readdir (void *data) { return readdir (*(DIR **) data); } +/* --------------------------------------------------------------------------------------------- */ + static int local_closedir (void *data) { int i; - i = closedir (* (DIR **) data); + i = closedir (*(DIR **) data); g_free (data); return i; } +/* --------------------------------------------------------------------------------------------- */ + static int local_stat (struct vfs_class *me, const char *path, struct stat *buf) { @@ -127,24 +109,21 @@ local_stat (struct vfs_class *me, const char *path, struct stat *buf) return stat (path, buf); } +/* --------------------------------------------------------------------------------------------- */ + static int local_lstat (struct vfs_class *me, const char *path, struct stat *buf) { (void) me; #ifndef HAVE_STATLSTAT - return lstat (path,buf); + return lstat (path, buf); #else return statlstat (path, buf); #endif } -int -local_fstat (void *data, struct stat *buf) -{ - /* FIXME: avoid type cast */ - return fstat (*((int *) data), buf); -} +/* --------------------------------------------------------------------------------------------- */ static int local_chmod (struct vfs_class *me, const char *path, int mode) @@ -154,6 +133,8 @@ local_chmod (struct vfs_class *me, const char *path, int mode) return chmod (path, mode); } +/* --------------------------------------------------------------------------------------------- */ + static int local_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { @@ -162,6 +143,8 @@ local_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) return chown (path, owner, group); } +/* --------------------------------------------------------------------------------------------- */ + static int local_utime (struct vfs_class *me, const char *path, struct utimbuf *times) { @@ -170,6 +153,8 @@ local_utime (struct vfs_class *me, const char *path, struct utimbuf *times) return utime (path, times); } +/* --------------------------------------------------------------------------------------------- */ + static int local_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) { @@ -178,6 +163,8 @@ local_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) return readlink (path, buf, size); } +/* --------------------------------------------------------------------------------------------- */ + static int local_unlink (struct vfs_class *me, const char *path) { @@ -186,6 +173,8 @@ local_unlink (struct vfs_class *me, const char *path) return unlink (path); } +/* --------------------------------------------------------------------------------------------- */ + static int local_symlink (struct vfs_class *me, const char *n1, const char *n2) { @@ -194,6 +183,8 @@ local_symlink (struct vfs_class *me, const char *n1, const char *n2) return symlink (n1, n2); } +/* --------------------------------------------------------------------------------------------- */ + static ssize_t local_write (void *data, const char *buf, size_t nbyte) { @@ -201,21 +192,26 @@ local_write (void *data, const char *buf, size_t nbyte) int n; if (!data) - return -1; + return -1; - fd = * (int *) data; - while ((n = write (fd, buf, nbyte)) == -1){ + fd = *(int *) data; + while ((n = write (fd, buf, nbyte)) == -1) + { #ifdef EAGAIN - if (errno == EAGAIN) continue; + if (errno == EAGAIN) + continue; #endif #ifdef EINTR - if (errno == EINTR) continue; + if (errno == EINTR) + continue; #endif - break; + break; } return n; } +/* --------------------------------------------------------------------------------------------- */ + static int local_rename (struct vfs_class *me, const char *a, const char *b) { @@ -224,6 +220,8 @@ local_rename (struct vfs_class *me, const char *a, const char *b) return rename (a, b); } +/* --------------------------------------------------------------------------------------------- */ + static int local_chdir (struct vfs_class *me, const char *path) { @@ -232,13 +230,7 @@ local_chdir (struct vfs_class *me, const char *path) return chdir (path); } -off_t -local_lseek (void *data, off_t offset, int whence) -{ - int fd = * (int *) data; - - return lseek (fd, offset, whence); -} +/* --------------------------------------------------------------------------------------------- */ static int local_mknod (struct vfs_class *me, const char *path, mode_t mode, dev_t dev) @@ -248,6 +240,8 @@ local_mknod (struct vfs_class *me, const char *path, mode_t mode, dev_t dev) return mknod (path, mode, dev); } +/* --------------------------------------------------------------------------------------------- */ + static int local_link (struct vfs_class *me, const char *p1, const char *p2) { @@ -256,6 +250,8 @@ local_link (struct vfs_class *me, const char *p1, const char *p2) return link (p1, p2); } +/* --------------------------------------------------------------------------------------------- */ + static int local_mkdir (struct vfs_class *me, const char *path, mode_t mode) { @@ -264,6 +260,8 @@ local_mkdir (struct vfs_class *me, const char *path, mode_t mode) return mkdir (path, mode); } +/* --------------------------------------------------------------------------------------------- */ + static int local_rmdir (struct vfs_class *me, const char *path) { @@ -272,6 +270,8 @@ local_rmdir (struct vfs_class *me, const char *path) return rmdir (path); } +/* --------------------------------------------------------------------------------------------- */ + static char * local_getlocalcopy (struct vfs_class *me, const char *path) { @@ -280,9 +280,10 @@ local_getlocalcopy (struct vfs_class *me, const char *path) return g_strdup (path); } +/* --------------------------------------------------------------------------------------------- */ + static int -local_ungetlocalcopy (struct vfs_class *me, const char *path, - const char *local, int has_changed) +local_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed) { (void) me; (void) path; @@ -292,15 +293,89 @@ local_ungetlocalcopy (struct vfs_class *me, const char *path, return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int local_which (struct vfs_class *me, const char *path) { (void) me; (void) path; - return 0; /* Every path which other systems do not like is expected to be ours */ + return 0; /* Every path which other systems do not like is expected to be ours */ } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +ssize_t +local_read (void *data, char *buffer, size_t count) +{ + int n; + + if (!data) + return -1; + + while ((n = read (*((int *) data), buffer, count)) == -1) + { +#ifdef EAGAIN + if (errno == EAGAIN) + continue; +#endif +#ifdef EINTR + if (errno == EINTR) + continue; +#endif + return -1; + } + return n; +} + +/* --------------------------------------------------------------------------------------------- */ + +int +local_close (void *data) +{ + int fd; + + if (!data) + return -1; + + fd = *(int *) data; + g_free (data); + return close (fd); +} + +/* --------------------------------------------------------------------------------------------- */ + +int +local_errno (struct vfs_class *me) +{ + (void) me; + return errno; +} + +/* --------------------------------------------------------------------------------------------- */ + +int +local_fstat (void *data, struct stat *buf) +{ + /* FIXME: avoid type cast */ + return fstat (*((int *) data), buf); +} + +/* --------------------------------------------------------------------------------------------- */ + +off_t +local_lseek (void *data, off_t offset, int whence) +{ + int fd = *(int *) data; + + return lseek (fd, offset, whence); +} + +/* --------------------------------------------------------------------------------------------- */ + void init_localfs (void) { @@ -335,3 +410,5 @@ init_localfs (void) vfs_local_ops.rmdir = local_rmdir; vfs_register_class (&vfs_local_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/local.h b/lib/vfs/mc-vfs/local.h index eccfe6644..1002b273d 100644 --- a/lib/vfs/mc-vfs/local.h +++ b/lib/vfs/mc-vfs/local.h @@ -1,14 +1,23 @@ - /** * \file * \brief Header: local FS */ -#ifndef MC_VFS_LOCAL_H -#define MC_VFS_LOCAL_H +#ifndef MC__VFS_LOCAL_H +#define MC__VFS_LOCAL_H #include "vfs-impl.h" +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + extern void init_localfs (void); /* these functions are used by other filesystems, so they are @@ -19,4 +28,5 @@ extern int local_fstat (void *data, struct stat *buf); extern int local_errno (struct vfs_class *me); extern off_t local_lseek (void *data, off_t offset, int whence); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/vfs/mc-vfs/netutil.c b/lib/vfs/mc-vfs/netutil.c index 9e88b2b9d..d1d867478 100644 --- a/lib/vfs/mc-vfs/netutil.c +++ b/lib/vfs/mc-vfs/netutil.c @@ -1,13 +1,13 @@ /* Network utilities for the Midnight Commander Virtual File System. - + Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2005, 2007 Free Software Foundation, Inc. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -31,8 +31,19 @@ #include "netutil.h" +/*** global variables ****************************************************************************/ + volatile sig_atomic_t got_sigpipe = 0; +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + static void sig_pipe (int unused) { @@ -40,6 +51,10 @@ sig_pipe (int unused) got_sigpipe = 1; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void tcp_init (void) { @@ -57,3 +72,5 @@ tcp_init (void) initialized = TRUE; } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/netutil.h b/lib/vfs/mc-vfs/netutil.h index 3aac0db51..18fdbc283 100644 --- a/lib/vfs/mc-vfs/netutil.h +++ b/lib/vfs/mc-vfs/netutil.h @@ -4,12 +4,23 @@ * \brief Header: Virtual File System: Network utilities */ -#ifndef MC_VFS_NETUTIL_H -#define MC_VFS_NETUTIL_H +#ifndef MC__VFS_NETUTIL_H +#define MC__VFS_NETUTIL_H #include +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ extern volatile sig_atomic_t got_sigpipe; + +/*** declarations of public functions ************************************************************/ + void tcp_init (void); +/*** inline functions ****************************************************************************/ #endif /* MC_VFS_NETUTIL_H */ diff --git a/lib/vfs/mc-vfs/sfs.c b/lib/vfs/mc-vfs/sfs.c index 6985b0650..108881606 100644 --- a/lib/vfs/mc-vfs/sfs.c +++ b/lib/vfs/mc-vfs/sfs.c @@ -43,33 +43,64 @@ #include "lib/global.h" -#include "src/wtools.h" /* D_ERROR, D_NORMAL */ -#include "src/main.h" /* mc_home */ -#include "src/execute.h" /* EXECUTE_AS_SHELL */ +#include "src/wtools.h" /* D_ERROR, D_NORMAL */ +#include "src/main.h" /* mc_home */ +#include "src/execute.h" /* EXECUTE_AS_SHELL */ #include "vfs-impl.h" #include "utilvfs.h" #include "local.h" -#include "gc.h" /* vfs_stamp_create */ +#include "gc.h" /* vfs_stamp_create */ -struct cachedfile { - char *name, *cache; - struct cachedfile *next; -}; +/*** global variables ****************************************************************************/ -static struct cachedfile *head; -static struct vfs_class vfs_sfs_ops; +/*** file scope macro definitions ****************************************************************/ #define MAXFS 32 -static int sfs_no = 0; -static char *sfs_prefix[ MAXFS ]; -static char *sfs_command[ MAXFS ]; -static int sfs_flags[ MAXFS ]; + #define F_1 1 #define F_2 2 #define F_NOLOCALCOPY 4 #define F_FULLMATCH 8 +#define COPY_CHAR \ + if ((size_t) (t-pad) > sizeof(pad)) { \ + g_free (pqname); \ + return -1; \ + } \ + else \ + *t++ = *s; + +#define COPY_STRING(a) \ + if ((t-pad)+strlen(a)>sizeof(pad)) { \ + g_free (pqname); \ + return -1; \ + } else { \ + strcpy (t, a); \ + t+= strlen(a); \ + } + +/*** file scope type declarations ****************************************************************/ + +struct cachedfile +{ + char *name, *cache; + struct cachedfile *next; +}; + +/*** file scope variables ************************************************************************/ + +static struct cachedfile *head; +static struct vfs_class vfs_sfs_ops; + +static int sfs_no = 0; +static char *sfs_prefix[MAXFS]; +static char *sfs_command[MAXFS]; +static int sfs_flags[MAXFS]; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + static int sfs_vfmake (struct vfs_class *me, const char *name, char *cache) { @@ -78,91 +109,88 @@ sfs_vfmake (struct vfs_class *me, const char *name, char *cache) char pad[10240]; char *s, *t = pad; int was_percent = 0; - char *pname; /* name of parent archive */ - char *pqname; /* name of parent archive, quoted */ + char *pname; /* name of parent archive */ + char *pqname; /* name of parent archive, quoted */ pname = g_strdup (name); vfs_split (pname, &inpath, &op); w = (*me->which) (me, op); if (w == -1) - vfs_die ("This cannot happen... Hopefully.\n"); + vfs_die ("This cannot happen... Hopefully.\n"); - if (!(sfs_flags[w] & F_1) && strcmp (pname, "/")) { - g_free (pname); - return -1; + if (!(sfs_flags[w] & F_1) && strcmp (pname, "/")) + { + g_free (pname); + return -1; } /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */ - if (!(sfs_flags[w] & F_NOLOCALCOPY)) { - s = mc_getlocalcopy (pname); - if (!s) { - g_free (pname); - return -1; - } - pqname = name_quote (s, 0); - g_free (s); - } else { - pqname = name_quote (pname, 0); + if (!(sfs_flags[w] & F_NOLOCALCOPY)) + { + s = mc_getlocalcopy (pname); + if (!s) + { + g_free (pname); + return -1; + } + pqname = name_quote (s, 0); + g_free (s); + } + else + { + pqname = name_quote (pname, 0); } g_free (pname); -#define COPY_CHAR \ - if ((size_t) (t-pad) > sizeof(pad)) { \ - g_free (pqname); \ - return -1; \ - } \ - else \ - *t++ = *s; -#define COPY_STRING(a) \ - if ((t-pad)+strlen(a)>sizeof(pad)) { \ - g_free (pqname); \ - return -1; \ - } else { \ - strcpy (t, a); \ - t+= strlen(a); \ - } + for (s = sfs_command[w]; *s; s++) + { + if (was_percent) + { - for (s = sfs_command[w]; *s; s++) { - if (was_percent) { + const char *ptr = NULL; + was_percent = 0; - const char *ptr = NULL; - was_percent = 0; - - switch (*s) { - case '1': - ptr = pqname; - break; - case '2': - ptr = op + strlen (sfs_prefix[w]); - break; - case '3': - ptr = cache; - break; - case '%': - COPY_CHAR; - continue; - } - COPY_STRING (ptr); - } else { - if (*s == '%') - was_percent = 1; - else - COPY_CHAR; - } + switch (*s) + { + case '1': + ptr = pqname; + break; + case '2': + ptr = op + strlen (sfs_prefix[w]); + break; + case '3': + ptr = cache; + break; + case '%': + COPY_CHAR; + continue; + } + COPY_STRING (ptr); + } + else + { + if (*s == '%') + was_percent = 1; + else + COPY_CHAR; + } } g_free (pqname); open_error_pipe (); - if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) { - close_error_pipe (D_ERROR, NULL); - return -1; + if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) + { + close_error_pipe (D_ERROR, NULL); + return -1; } close_error_pipe (D_NORMAL, NULL); - return 0; /* OK */ + return 0; /* OK */ } +/* --------------------------------------------------------------------------------------------- */ + static const char * sfs_redirect (struct vfs_class *me, const char *name) { @@ -170,32 +198,36 @@ sfs_redirect (struct vfs_class *me, const char *name) char *cache; int handle; - while (cur) { - if (!strcmp (name, cur->name)) { - vfs_stamp (&vfs_sfs_ops, cur); - return cur->cache; - } - cur = cur->next; + while (cur) + { + if (!strcmp (name, cur->name)) + { + vfs_stamp (&vfs_sfs_ops, cur); + return cur->cache; + } + cur = cur->next; } handle = vfs_mkstemps (&cache, "sfs", name); - if (handle == -1) { - return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US"; + if (handle == -1) + { + return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US"; } close (handle); - if (!sfs_vfmake (me, name, cache)) { - cur = g_new (struct cachedfile, 1); - cur->name = g_strdup (name); - cur->cache = cache; - cur->next = head; - head = cur; + if (!sfs_vfmake (me, name, cache)) + { + cur = g_new (struct cachedfile, 1); + cur->name = g_strdup (name); + cur->cache = cache; + cur->next = head; + head = cur; - vfs_stamp_create (&vfs_sfs_ops, head); + vfs_stamp_create (&vfs_sfs_ops, head); - return cache; + return cache; } unlink (cache); @@ -203,6 +235,8 @@ sfs_redirect (struct vfs_class *me, const char *name) return "/I_MUST_NOT_EXIST"; } +/* --------------------------------------------------------------------------------------------- */ + static void * sfs_open (struct vfs_class *me, const char *path, int flags, mode_t mode) { @@ -212,7 +246,7 @@ sfs_open (struct vfs_class *me, const char *path, int flags, mode_t mode) path = sfs_redirect (me, path); fd = open (path, NO_LINEAR (flags), mode); if (fd == -1) - return 0; + return 0; sfs_info = g_new (int, 1); *sfs_info = fd; @@ -220,13 +254,19 @@ sfs_open (struct vfs_class *me, const char *path, int flags, mode_t mode) return sfs_info; } -static int sfs_stat (struct vfs_class *me, const char *path, struct stat *buf) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_stat (struct vfs_class *me, const char *path, struct stat *buf) { path = sfs_redirect (me, path); return stat (path, buf); } -static int sfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) { path = sfs_redirect (me, path); #ifndef HAVE_STATLSTAT @@ -236,24 +276,35 @@ static int sfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) #endif } -static int sfs_chmod (struct vfs_class *me, const char *path, int mode) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_chmod (struct vfs_class *me, const char *path, int mode) { path = sfs_redirect (me, path); return chmod (path, mode); } -static int sfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { path = sfs_redirect (me, path); return chown (path, owner, group); } -static int sfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times) { path = sfs_redirect (me, path); return utime (path, times); } +/* --------------------------------------------------------------------------------------------- */ + static int sfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) { @@ -261,6 +312,8 @@ sfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) return readlink (path, buf, size); } +/* --------------------------------------------------------------------------------------------- */ + static vfsid sfs_getid (struct vfs_class *me, const char *path) { @@ -268,49 +321,60 @@ sfs_getid (struct vfs_class *me, const char *path) (void) me; - while (cur) { - if (!strcmp (path, cur->name)) - break; - cur = cur->next; + while (cur) + { + if (!strcmp (path, cur->name)) + break; + cur = cur->next; } return (vfsid) cur; } -static void sfs_free (vfsid id) +/* --------------------------------------------------------------------------------------------- */ + +static void +sfs_free (vfsid id) { struct cachedfile *which = (struct cachedfile *) id; struct cachedfile *cur, *prev; for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next) - ; + ; if (!cur) - vfs_die( "Free of thing which is unknown to me\n" ); + vfs_die ("Free of thing which is unknown to me\n"); unlink (cur->cache); if (prev) - prev->next = cur->next; + prev->next = cur->next; else - head = cur->next; + head = cur->next; g_free (cur->cache); g_free (cur->name); g_free (cur); } -static void sfs_fill_names (struct vfs_class *me, fill_names_f func) +/* --------------------------------------------------------------------------------------------- */ + +static void +sfs_fill_names (struct vfs_class *me, fill_names_f func) { struct cachedfile *cur = head; (void) me; - while (cur){ - (*func)(cur->name); - cur = cur->next; + while (cur) + { + (*func) (cur->name); + cur = cur->next; } } -static int sfs_nothingisopen (vfsid id) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_nothingisopen (vfsid id) { /* FIXME: Investigate whether have to guard this like in the other VFSs (see fd_usage in extfs) -- Norbert */ @@ -318,6 +382,8 @@ static int sfs_nothingisopen (vfsid id) return 1; } +/* --------------------------------------------------------------------------------------------- */ + static char * sfs_getlocalcopy (struct vfs_class *me, const char *path) { @@ -325,9 +391,10 @@ sfs_getlocalcopy (struct vfs_class *me, const char *path) return g_strdup (path); } +/* --------------------------------------------------------------------------------------------- */ + static int -sfs_ungetlocalcopy (struct vfs_class *me, const char *path, - const char *local, int has_changed) +sfs_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed) { (void) me; (void) path; @@ -336,7 +403,10 @@ sfs_ungetlocalcopy (struct vfs_class *me, const char *path, return 0; } -static int sfs_init (struct vfs_class *me) +/* --------------------------------------------------------------------------------------------- */ + +static int +sfs_init (struct vfs_class *me) { char *mc_sfsini; FILE *cfg; @@ -347,67 +417,80 @@ static int sfs_init (struct vfs_class *me) mc_sfsini = g_build_filename (mc_home, "sfs.ini", (char *) NULL); cfg = fopen (mc_sfsini, "r"); - if (cfg == NULL) { - fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini); - g_free (mc_sfsini); - return 0; + if (cfg == NULL) + { + fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini); + g_free (mc_sfsini); + return 0; } g_free (mc_sfsini); sfs_no = 0; - while (sfs_no < MAXFS && fgets (key, sizeof (key), cfg)) { - char *c, *semi = NULL, flags = 0; + while (sfs_no < MAXFS && fgets (key, sizeof (key), cfg)) + { + char *c, *semi = NULL, flags = 0; - if (*key == '#' || *key == '\n') - continue; + if (*key == '#' || *key == '\n') + continue; - for (c = key; *c; c++) - if ((*c == ':') || (*c == '/')){ - semi = c; - if (*c == '/'){ - *c = 0; - flags |= F_FULLMATCH; - } - break; - } + for (c = key; *c; c++) + if ((*c == ':') || (*c == '/')) + { + semi = c; + if (*c == '/') + { + *c = 0; + flags |= F_FULLMATCH; + } + break; + } - if (!semi){ - invalid_line: - fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"), - "sfs.ini", key); - continue; - } + if (!semi) + { + invalid_line: + fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"), "sfs.ini", key); + continue; + } - c = semi + 1; - while (*c && (*c != ' ') && (*c != '\t')) { - switch (*c) { - case '1': flags |= F_1; break; - case '2': flags |= F_2; break; - case 'R': flags |= F_NOLOCALCOPY; break; - default: - fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"), - *c, "sfs.ini", key); - } - c++; - } - if (!*c) - goto invalid_line; + c = semi + 1; + while (*c && (*c != ' ') && (*c != '\t')) + { + switch (*c) + { + case '1': + flags |= F_1; + break; + case '2': + flags |= F_2; + break; + case 'R': + flags |= F_NOLOCALCOPY; + break; + default: + fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"), *c, "sfs.ini", key); + } + c++; + } + if (!*c) + goto invalid_line; - c++; - *(semi+1) = 0; - semi = strchr (c, '\n'); - if (semi != NULL) - *semi = 0; + c++; + *(semi + 1) = 0; + semi = strchr (c, '\n'); + if (semi != NULL) + *semi = 0; - sfs_prefix [sfs_no] = g_strdup (key); - sfs_command [sfs_no] = g_strdup (c); - sfs_flags [sfs_no] = flags; - sfs_no++; + sfs_prefix[sfs_no] = g_strdup (key); + sfs_command[sfs_no] = g_strdup (c); + sfs_flags[sfs_no] = flags; + sfs_no++; } fclose (cfg); return 1; } +/* --------------------------------------------------------------------------------------------- */ + static void sfs_done (struct vfs_class *me) { @@ -415,14 +498,17 @@ sfs_done (struct vfs_class *me) (void) me; - for (i = 0; i < sfs_no; i++){ - g_free (sfs_prefix [i]); - g_free (sfs_command [i]); - sfs_prefix [i] = sfs_command [i] = NULL; + for (i = 0; i < sfs_no; i++) + { + g_free (sfs_prefix[i]); + g_free (sfs_command[i]); + sfs_prefix[i] = sfs_command[i] = NULL; } sfs_no = 0; } +/* --------------------------------------------------------------------------------------------- */ + static int sfs_which (struct vfs_class *me, const char *path) { @@ -431,16 +517,21 @@ sfs_which (struct vfs_class *me, const char *path) (void) me; for (i = 0; i < sfs_no; i++) - if (sfs_flags [i] & F_FULLMATCH) { - if (!strcmp (path, sfs_prefix [i])) - return i; - } else - if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i]))) - return i; + if (sfs_flags[i] & F_FULLMATCH) + { + if (!strcmp (path, sfs_prefix[i])) + return i; + } + else if (!strncmp (path, sfs_prefix[i], strlen (sfs_prefix[i]))) + return i; return -1; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void init_sfs (void) { @@ -468,3 +559,5 @@ init_sfs (void) vfs_sfs_ops.ungetlocalcopy = sfs_ungetlocalcopy; vfs_register_class (&vfs_sfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/smbfs.c b/lib/vfs/mc-vfs/smbfs.c index 709f2596f..bcacca233 100644 --- a/lib/vfs/mc-vfs/smbfs.c +++ b/lib/vfs/mc-vfs/smbfs.c @@ -4,7 +4,7 @@ Free Software Foundation, Inc. Written by Wayne Roberts , 1997 - Andrew V. Samoilov 2002, 2003 + Andrew V. Samoilov 2002, 2003 This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License @@ -35,20 +35,20 @@ #include #include -#undef USE_NCURSES /* Don't include *curses.h */ +#undef USE_NCURSES /* Don't include *curses.h */ #undef USE_NCURSESW #include #include "lib/global.h" -#include "src/wtools.h" /* message() */ +#include "src/wtools.h" /* message() */ -#undef PACKAGE_BUGREPORT -#undef PACKAGE_NAME -#undef PACKAGE_STRING -#undef PACKAGE_TARNAME -#undef PACKAGE_VERSION +#undef PACKAGE_BUGREPORT +#undef PACKAGE_NAME +#undef PACKAGE_STRING +#undef PACKAGE_TARNAME +#undef PACKAGE_VERSION #include "samba/include/config.h" /* don't load crap in "samba/include/includes.h" we don't use and which @@ -64,20 +64,72 @@ #include "utilvfs.h" #include "smbfs.h" +/*** global variables ****************************************************************************/ + +extern int DEBUGLEVEL; +extern pstring myhostname; +extern struct in_addr ipzero; +extern pstring global_myname; +extern pstring debugf; +extern FILE *dbf; + +/*** file scope macro definitions ****************************************************************/ + #define SMBFS_MAX_CONNECTIONS 16 -static const char * const IPC = "IPC$"; -static const char * const URL_HEADER = "/#smb:"; -#define HEADER_LEN 6 + +#define HEADER_LEN 6 + +#define CNV_LANG(s) dos_to_unix(s,False) +#define GNAL_VNC(s) unix_to_dos(s,False) + +/* Extract the hostname and username from the path */ +/* path is in the form: [user@]hostname/share/remote-dir */ +#define smbfs_get_host_and_username(path, host, user, port, pass) \ + vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0) + +#define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */ + +/*** file scope type declarations ****************************************************************/ + +typedef struct _smbfs_connection smbfs_connection; + +typedef struct +{ + struct cli_state *cli; + int fnum; + off_t nread; + uint16 attr; +} smbfs_handle; + +typedef struct dir_entry +{ + char *text; + struct dir_entry *next; + struct stat my_stat; + int merrno; +} dir_entry; + +typedef struct +{ + gboolean server_list; + char *dirname; + char *path; /* the dir originally passed to smbfs_opendir */ + smbfs_connection *conn; + dir_entry *entries; + dir_entry *current; +} opendir_info; + +/*** file scope variables ************************************************************************/ + +static const char *const IPC = "IPC$"; +static const char *const URL_HEADER = "/#smb:"; static int my_errno; static uint32 err; /* stuff that is same with each connection */ -extern int DEBUGLEVEL; -extern pstring myhostname; -extern struct in_addr ipzero; + static mode_t myumask = 0755; -extern pstring global_myname; static int smbfs_open_connections = 0; static gboolean got_user = FALSE; static gboolean got_pass = FALSE; @@ -85,50 +137,56 @@ static pstring password; static pstring username; static struct vfs_class vfs_smbfs_ops; -static struct _smbfs_connection { - struct cli_state *cli; - struct in_addr dest_ip; - BOOL have_ip; - char *host; /* server name */ - char *service; /* share name */ - char *domain; - char *user; - char *home; - char *password; - int port; - int name_type; - time_t last_use; -} smbfs_connections [SMBFS_MAX_CONNECTIONS]; +static struct _smbfs_connection +{ + struct cli_state *cli; + struct in_addr dest_ip; + BOOL have_ip; + char *host; /* server name */ + char *service; /* share name */ + char *domain; + char *user; + char *home; + char *password; + int port; + int name_type; + time_t last_use; +} smbfs_connections[SMBFS_MAX_CONNECTIONS]; /* unique to each connection */ -/* modifies *share */ -static struct cli_state * smbfs_do_connect (const char *server, char *share); - -typedef struct _smbfs_connection smbfs_connection; static smbfs_connection *current_bucket; -typedef struct { - struct cli_state *cli; - int fnum; - off_t nread; - uint16 attr; -} smbfs_handle; - static GSList *auth_list; +static opendir_info *previous_info, *current_info, *current_share_info, *current_server_info; + +static gboolean first_direntry; + +/* stat a single file, smbfs_get_remote_stat callback */ +static dir_entry *single_entry; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/* modifies *share */ +static struct cli_state *smbfs_do_connect (const char *server, char *share); + +/* --------------------------------------------------------------------------------------------- */ /* this function allows you to write: * char *s = g_strdup("hello, world"); * s = free_after(g_strconcat(s, s, (char *)0), s); */ + static inline char * free_after (char *result, char *string_to_free) { - g_free(string_to_free); - return result; + g_free (string_to_free); + return result; } +/* --------------------------------------------------------------------------------------------- */ -static void +static void smbfs_auth_free (struct smb_authinfo const *a) { g_free (a->host); @@ -138,21 +196,26 @@ smbfs_auth_free (struct smb_authinfo const *a) wipe_password (a->password); } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_auth_free_all (void) { - if (auth_list) { - g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0); + if (auth_list) + { + g_slist_foreach (auth_list, (GFunc) smbfs_auth_free, 0); g_slist_free (auth_list); auth_list = 0; } } +/* --------------------------------------------------------------------------------------------- */ + static gint smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b) { - struct smb_authinfo const *a = (struct smb_authinfo const *)_a; - struct smb_authinfo const *b = (struct smb_authinfo const *)_b; + struct smb_authinfo const *a = (struct smb_authinfo const *) _a; + struct smb_authinfo const *b = (struct smb_authinfo const *) _b; if (!a->host || !a->share || !b->host || !b->share) return 1; @@ -163,11 +226,13 @@ smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static gint smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b) { - struct smb_authinfo const *a = (struct smb_authinfo const *)_a; - struct smb_authinfo const *b = (struct smb_authinfo const *)_b; + struct smb_authinfo const *a = (struct smb_authinfo const *) _a; + struct smb_authinfo const *b = (struct smb_authinfo const *) _b; if (!a->host || !b->host) return 1; @@ -178,18 +243,22 @@ smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_auth_add (const char *host, const char *share, const char *domain, - const char *user, const char *pass) + const char *user, const char *pass) { struct smb_authinfo *auth; auth = vfs_smb_authinfo_new (host, share, domain, user, pass); if (auth != NULL) - auth_list = g_slist_prepend (auth_list, auth); + auth_list = g_slist_prepend (auth_list, auth); } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_auth_remove (const char *host, const char *share) { @@ -199,9 +268,7 @@ smbfs_auth_remove (const char *host, const char *share) data.host = g_strdup (host); data.share = g_strdup (share); - list = g_slist_find_custom (auth_list, - &data, - smbfs_auth_cmp_host_and_share); + list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share); g_free (data.host); g_free (data.share); if (!list) @@ -211,19 +278,22 @@ smbfs_auth_remove (const char *host, const char *share) smbfs_auth_free (auth); } +/* --------------------------------------------------------------------------------------------- */ /* Set authentication information in bucket. Return 1 if successful, else 0 */ /* Information in auth_list overrides user if pass is NULL. */ /* bucket->host and bucket->service must be valid. */ + static int -smbfs_bucket_set_authinfo (smbfs_connection *bucket, - const char *domain, const char *user, const char *pass, - int fallback_to_host) +smbfs_bucket_set_authinfo (smbfs_connection * bucket, + const char *domain, const char *user, const char *pass, + int fallback_to_host) { struct smb_authinfo data; struct smb_authinfo *auth; GSList *list; - if (domain && user && pass) { + if (domain && user && pass) + { g_free (bucket->domain); g_free (bucket->user); g_free (bucket->password); @@ -231,8 +301,7 @@ smbfs_bucket_set_authinfo (smbfs_connection *bucket, bucket->user = g_strdup (user); bucket->password = g_strdup (pass); smbfs_auth_remove (bucket->host, bucket->service); - smbfs_auth_add (bucket->host, bucket->service, - domain, user, pass); + smbfs_auth_add (bucket->host, bucket->service, domain, user, pass); return 1; } @@ -241,7 +310,8 @@ smbfs_bucket_set_authinfo (smbfs_connection *bucket, list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share); if (!list && fallback_to_host) list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host); - if (list) { + if (list) + { auth = list->data; bucket->domain = g_strdup (auth->domain); bucket->user = g_strdup (auth->user); @@ -249,18 +319,18 @@ smbfs_bucket_set_authinfo (smbfs_connection *bucket, return 1; } - if (got_pass) { + if (got_pass) + { bucket->domain = g_strdup (lp_workgroup ()); bucket->user = g_strdup (got_user ? username : user); bucket->password = g_strdup (password); return 1; } - auth = vfs_smb_get_authinfo (bucket->host, - bucket->service, - (domain ? domain : lp_workgroup ()), - user); - if (auth) { + auth = vfs_smb_get_authinfo (bucket->host, + bucket->service, (domain ? domain : lp_workgroup ()), user); + if (auth) + { g_free (bucket->domain); g_free (bucket->user); g_free (bucket->password); @@ -274,32 +344,37 @@ smbfs_bucket_set_authinfo (smbfs_connection *bucket, return 0; } +/* --------------------------------------------------------------------------------------------- */ + void smbfs_set_debug (int arg) { DEBUGLEVEL = arg; } -extern pstring debugf; -extern FILE *dbf; +/* --------------------------------------------------------------------------------------------- */ void smbfs_set_debugf (const char *filename) { - if (DEBUGLEVEL > 0) { - FILE *outfile = fopen (filename, "w"); - if (outfile) { - setup_logging ("", True); /* No needs for timestamp for each message */ - dbf = outfile; - setbuf (dbf, NULL); - pstrcpy (debugf, filename); - } + if (DEBUGLEVEL > 0) + { + FILE *outfile = fopen (filename, "w"); + if (outfile) + { + setup_logging ("", True); /* No needs for timestamp for each message */ + dbf = outfile; + setbuf (dbf, NULL); + pstrcpy (debugf, filename); + } } } +/* --------------------------------------------------------------------------------------------- */ /********************** The callbacks ******************************/ + static int -smbfs_init (struct vfs_class * me) +smbfs_init (struct vfs_class *me) { const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf"; @@ -311,10 +386,10 @@ smbfs_init (struct vfs_class * me) DEBUG (3, ("smbfs_init(%s)\n", me->name)); if (!get_myname (myhostname, NULL)) - DEBUG (0, ("Failed to get my hostname.\n")); + DEBUG (0, ("Failed to get my hostname.\n")); if (!lp_load (servicesf, True, False, False)) - DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf)); + DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf)); codepage_initialise (lp_client_code_page ()); @@ -324,29 +399,33 @@ smbfs_init (struct vfs_class * me) umask (myumask); myumask = ~myumask; - if (getenv ("USER")) { - char *p; + if (getenv ("USER")) + { + char *p; - pstrcpy (username, getenv ("USER")); - got_user = TRUE; - DEBUG (3, ("smbfs_init(): $USER:%s\n", username)); - if ((p = strchr (username, '%'))) { - *p = 0; - pstrcpy (password, p + 1); - got_pass = TRUE; - memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password)); - DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n", - username, password)); - } - strupper (username); + pstrcpy (username, getenv ("USER")); + got_user = TRUE; + DEBUG (3, ("smbfs_init(): $USER:%s\n", username)); + if ((p = strchr (username, '%'))) + { + *p = 0; + pstrcpy (password, p + 1); + got_pass = TRUE; + memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password)); + DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n", username, password)); + } + strupper (username); } - if (getenv ("PASSWD")) { - pstrcpy (password, getenv ("PASSWD")); - got_pass = TRUE; + if (getenv ("PASSWD")) + { + pstrcpy (password, getenv ("PASSWD")); + got_pass = TRUE; } return 1; } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_fill_names (struct vfs_class *me, fill_names_f func) { @@ -355,50 +434,55 @@ smbfs_fill_names (struct vfs_class *me, fill_names_f func) (void) me; - for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) { - if (smbfs_connections [i].cli) { - path = g_strconcat (URL_HEADER, - smbfs_connections[i].user, "@", - smbfs_connections[i].host, - "/", smbfs_connections[i].service, (char *) NULL); - (*func)(path); - g_free (path); - } + for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) + { + if (smbfs_connections[i].cli) + { + path = g_strconcat (URL_HEADER, + smbfs_connections[i].user, "@", + smbfs_connections[i].host, + "/", smbfs_connections[i].service, (char *) NULL); + (*func) (path); + g_free (path); + } } } -#define CNV_LANG(s) dos_to_unix(s,False) -#define GNAL_VNC(s) unix_to_dos(s,False) +/* --------------------------------------------------------------------------------------------- */ /* does same as do_get() in client.c */ /* called from vfs.c:1080, count = buffer size */ + static ssize_t smbfs_read (void *data, char *buffer, size_t count) { smbfs_handle *info = (smbfs_handle *) data; ssize_t n; - DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%zu)\n", - info->fnum, (int)info->nread, count)); - n = cli_read(info->cli, info->fnum, buffer, info->nread, count); - if (n > 0) - info->nread += n; - return n; + DEBUG (3, ("smbfs_read(fnum:%d, nread:%d, count:%zu)\n", info->fnum, (int) info->nread, count)); + n = cli_read (info->cli, info->fnum, buffer, info->nread, count); + if (n > 0) + info->nread += n; + return n; } +/* --------------------------------------------------------------------------------------------- */ + static ssize_t smbfs_write (void *data, const char *buf, size_t nbyte) { smbfs_handle *info = (smbfs_handle *) data; ssize_t n; - DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%zu)\n", - info->fnum, (int)info->nread, nbyte)); - n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte); - if (n > 0) - info->nread += n; + DEBUG (3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%zu)\n", + info->fnum, (int) info->nread, nbyte)); + n = cli_write (info->cli, info->fnum, 0, buf, info->nread, nbyte); + if (n > 0) + info->nread += n; return n; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_close (void *data) { @@ -408,51 +492,33 @@ smbfs_close (void *data) /* FIXME: Why too different cli have the same outbuf * if file is copied to share */ - if (info->cli->outbuf == NULL) { - my_errno = EINVAL; - return -1; + if (info->cli->outbuf == NULL) + { + my_errno = EINVAL; + return -1; } #if 0 /* if imlementing archive_level: add rname to smbfs_handle */ - if (archive_level >= 2 && (inf->attr & aARCH)) { - cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0); + if (archive_level >= 2 && (inf->attr & aARCH)) + { + cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0); } #endif return (cli_close (info->cli, info->fnum) == True) ? 0 : -1; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_errno (struct vfs_class *me) { (void) me; - DEBUG(3, ("smbfs_errno: %s\n", unix_error_string (my_errno))); + DEBUG (3, ("smbfs_errno: %s\n", unix_error_string (my_errno))); return my_errno; } -typedef struct dir_entry { - char *text; - struct dir_entry *next; - struct stat my_stat; - int merrno; -} dir_entry; - -typedef struct { - gboolean server_list; - char *dirname; - char *path; /* the dir originally passed to smbfs_opendir */ - smbfs_connection *conn; - dir_entry *entries; - dir_entry *current; -} opendir_info; - -static opendir_info - *previous_info, - *current_info, - *current_share_info, - *current_server_info; - -static gboolean first_direntry; +/* --------------------------------------------------------------------------------------------- */ static dir_entry * smbfs_new_dir_entry (const char *name) @@ -462,11 +528,14 @@ smbfs_new_dir_entry (const char *name) new_entry = g_new0 (dir_entry, 1); new_entry->text = dos_to_unix (g_strdup (name), 1); - if (first_direntry) { - current_info->entries = new_entry; - first_direntry = FALSE; - } else { - current_info->current->next = new_entry; + if (first_direntry) + { + current_info->entries = new_entry; + first_direntry = FALSE; + } + else + { + current_info->current->next = new_entry; } current_info->current = new_entry; new_entry->my_stat.st_ino = inode_counter++; @@ -474,7 +543,9 @@ smbfs_new_dir_entry (const char *name) return new_entry; } +/* --------------------------------------------------------------------------------------------- */ /* browse for shares on server */ + static void smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state) { @@ -483,41 +554,43 @@ smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void (void) state; - switch (type) { + switch (type) + { case STYPE_DISKTREE: - typestr = "Disk"; - /* show this as dir */ - new_entry->my_stat.st_mode = - (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | - S_IXOTH) & myumask; - break; + typestr = "Disk"; + /* show this as dir */ + new_entry->my_stat.st_mode = + (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH) & myumask; + break; case STYPE_PRINTQ: - typestr = "Printer"; - break; + typestr = "Printer"; + break; case STYPE_DEVICE: - typestr = "Device"; - break; + typestr = "Device"; + break; case STYPE_IPC: - typestr = "IPC"; - break; + typestr = "IPC"; + break; } DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment)); } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry) { dir_entry *new_entry = (dir_entry *) entry; - time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */ + time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */ (void) mask; -#if 0 /* I want to see dot files */ +#if 0 /* I want to see dot files */ if (finfo->mode & aHIDDEN) - return; /* don't bother with hidden files, "~$" screws up mc */ + return; /* don't bother with hidden files, "~$" screws up mc */ #endif if (!entry) - new_entry = smbfs_new_dir_entry (finfo->name); + new_entry = smbfs_new_dir_entry (finfo->name); new_entry->my_stat.st_size = finfo->size; new_entry->my_stat.st_mtime = finfo->mtime; @@ -526,30 +599,31 @@ smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry) new_entry->my_stat.st_uid = finfo->uid; new_entry->my_stat.st_gid = finfo->gid; - new_entry->my_stat.st_mode = /* rw-rw-rw */ - S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; + new_entry->my_stat.st_mode = /* rw-rw-rw */ + S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; -/* if (finfo->mode & aVOLID); nothing similar in real world */ + /* if (finfo->mode & aVOLID); nothing similar in real world */ if (finfo->mode & aDIR) - new_entry->my_stat.st_mode |= /* drwxrwxrwx */ - S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; + new_entry->my_stat.st_mode |= /* drwxrwxrwx */ + S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; else - new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */ -/* if (finfo->mode & aARCH); DOS archive */ -/* if (finfo->mode & aHIDDEN); like a dot file? */ -/* if (finfo->mode & aSYSTEM); like a kernel? */ + new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */ + /* if (finfo->mode & aARCH); DOS archive */ + /* if (finfo->mode & aHIDDEN); like a dot file? */ + /* if (finfo->mode & aSYSTEM); like a kernel? */ if (finfo->mode & aRONLY) - new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); + new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); new_entry->my_stat.st_mode &= myumask; DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s", - CNV_LANG (finfo->name), - attrib_string (finfo->mode), - (double) finfo->size, - asctime (LocalTime (&t)))); + CNV_LANG (finfo->name), + attrib_string (finfo->mode), + (double) finfo->size, asctime (LocalTime (&t)))); } +/* --------------------------------------------------------------------------------------------- */ /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */ + static char * smbfs_convert_path (const char *remote_file, gboolean trailing_asterik) { @@ -557,30 +631,32 @@ smbfs_convert_path (const char *remote_file, gboolean trailing_asterik) char *result; my_remote = remote_file; - if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */ - my_remote += HEADER_LEN; - if (*my_remote == '/') /* from server browsing */ - my_remote++; - p = strchr(my_remote, '/'); - if (p) - my_remote = p+1; /* advance to end of server name */ - } + if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) + { /* if passed directly */ + my_remote += HEADER_LEN; + if (*my_remote == '/') /* from server browsing */ + my_remote++; + p = strchr (my_remote, '/'); + if (p) + my_remote = p + 1; /* advance to end of server name */ + } if (*my_remote == '/') - my_remote++; /* strip off leading '/' */ - p = strchr(my_remote, '/'); + my_remote++; /* strip off leading '/' */ + p = strchr (my_remote, '/'); if (p) - my_remote = p; /* strip off share/service name */ + my_remote = p; /* strip off share/service name */ /* create remote filename as understood by smb clientgen */ result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL); - unix_to_dos (result, /* inplace = */ 1); /* code page conversion */ - str_replace(result, '/', '\\'); + unix_to_dos (result, /* inplace = */ 1); /* code page conversion */ + str_replace (result, '/', '\\'); return result; } +/* --------------------------------------------------------------------------------------------- */ + static void -smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment, - void *state) +smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment, void *state) { dir_entry *new_entry = smbfs_new_dir_entry (name); @@ -589,134 +665,153 @@ smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment, /* show this as dir */ new_entry->my_stat.st_mode = - (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | - S_IXOTH) & myumask; + (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH) & myumask; DEBUG (3, ("\t%-16.16s %s\n", name, comment)); } -static BOOL -smbfs_reconnect(smbfs_connection *conn, int *retries) -{ - char *host; - DEBUG(3, ("RECONNECT\n")); - - if (*(conn->host) == 0) - host = g_strdup(conn->cli->desthost); /* server browsing */ - else - host = g_strdup(conn->host); - - cli_shutdown(conn->cli); - - if (!(conn->cli = smbfs_do_connect(host, conn->service))) { - message (D_ERROR, MSG_ERROR, - _("reconnect to %s failed"), conn->host); - g_free(host); - return False; - } - g_free(host); - if (++(*retries) == 2) - return False; - return True; -} +/* --------------------------------------------------------------------------------------------- */ static BOOL -smbfs_send(struct cli_state *cli) +smbfs_reconnect (smbfs_connection * conn, int *retries) { - size_t len; - size_t nwritten=0; - ssize_t ret; + char *host; + DEBUG (3, ("RECONNECT\n")); - len = smb_len(cli->outbuf) + 4; + if (*(conn->host) == 0) + host = g_strdup (conn->cli->desthost); /* server browsing */ + else + host = g_strdup (conn->host); - while (nwritten < len) { - ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten); - if (ret <= 0) { - if (errno == EPIPE) - return False; - } else - nwritten += ret; - } - - return True; + cli_shutdown (conn->cli); + + if (!(conn->cli = smbfs_do_connect (host, conn->service))) + { + message (D_ERROR, MSG_ERROR, _("reconnect to %s failed"), conn->host); + g_free (host); + return False; + } + g_free (host); + if (++(*retries) == 2) + return False; + return True; } +/* --------------------------------------------------------------------------------------------- */ + +static BOOL +smbfs_send (struct cli_state *cli) +{ + size_t len; + size_t nwritten = 0; + ssize_t ret; + + len = smb_len (cli->outbuf) + 4; + + while (nwritten < len) + { + ret = write_socket (cli->fd, cli->outbuf + nwritten, len - nwritten); + if (ret <= 0) + { + if (errno == EPIPE) + return False; + } + else + nwritten += ret; + } + + return True; +} + +/* --------------------------------------------------------------------------------------------- */ /**************************************************************************** See if server has cut us off by checking for EPIPE when writing. Taken from cli_chkpath() ****************************************************************************/ + static BOOL -smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only) +smbfs_chkpath (struct cli_state *cli, const char *path, BOOL send_only) { - fstring path2; - char *p; - - fstrcpy(path2,path); - unix_to_dos (path2, 1); - trim_string(path2,NULL,"\\"); - if (!*path2) *path2 = '\\'; - - memset(cli->outbuf,'\0',smb_size); - set_message(cli->outbuf,0,4 + strlen(path2),True); - SCVAL(cli->outbuf,smb_com,SMBchkpth); - SSVAL(cli->outbuf,smb_tid,cli->cnum); + fstring path2; + char *p; - cli->rap_error = 0; - cli->nt_error = 0; - SSVAL(cli->outbuf,smb_pid,cli->pid); - SSVAL(cli->outbuf,smb_uid,cli->vuid); - SSVAL(cli->outbuf,smb_mid,cli->mid); - if (cli->protocol > PROTOCOL_CORE) { - SCVAL(cli->outbuf,smb_flg,0x8); - SSVAL(cli->outbuf,smb_flg2,0x1); - } - - p = smb_buf(cli->outbuf); - *p++ = 4; - fstrcpy(p,path2); + fstrcpy (path2, path); + unix_to_dos (path2, 1); + trim_string (path2, NULL, "\\"); + if (!*path2) + *path2 = '\\'; - if (!smbfs_send(cli)) { - DEBUG(3, ("smbfs_chkpath: couldnt send\n")); - return False; - } - if (send_only) { - client_receive_smb(cli->fd, cli->inbuf, cli->timeout); - DEBUG(3, ("smbfs_chkpath: send only OK\n")); - return True; /* just testing for EPIPE */ - } - if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) { - DEBUG(3, ("smbfs_chkpath: receive error\n")); - return False; - } - if ((my_errno = cli_error(cli, NULL, NULL, NULL))) { - if (my_errno == 20 || my_errno == 13) - return True; /* ignore if 'not a directory' error */ - DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", unix_error_string (my_errno))); - return False; - } + memset (cli->outbuf, '\0', smb_size); + set_message (cli->outbuf, 0, 4 + strlen (path2), True); + SCVAL (cli->outbuf, smb_com, SMBchkpth); + SSVAL (cli->outbuf, smb_tid, cli->cnum); - return True; + cli->rap_error = 0; + cli->nt_error = 0; + SSVAL (cli->outbuf, smb_pid, cli->pid); + SSVAL (cli->outbuf, smb_uid, cli->vuid); + SSVAL (cli->outbuf, smb_mid, cli->mid); + if (cli->protocol > PROTOCOL_CORE) + { + SCVAL (cli->outbuf, smb_flg, 0x8); + SSVAL (cli->outbuf, smb_flg2, 0x1); + } + + p = smb_buf (cli->outbuf); + *p++ = 4; + fstrcpy (p, path2); + + if (!smbfs_send (cli)) + { + DEBUG (3, ("smbfs_chkpath: couldnt send\n")); + return False; + } + if (send_only) + { + client_receive_smb (cli->fd, cli->inbuf, cli->timeout); + DEBUG (3, ("smbfs_chkpath: send only OK\n")); + return True; /* just testing for EPIPE */ + } + if (!client_receive_smb (cli->fd, cli->inbuf, cli->timeout)) + { + DEBUG (3, ("smbfs_chkpath: receive error\n")); + return False; + } + if ((my_errno = cli_error (cli, NULL, NULL, NULL))) + { + if (my_errno == 20 || my_errno == 13) + return True; /* ignore if 'not a directory' error */ + DEBUG (3, ("smbfs_chkpath: cli_error: %s\n", unix_error_string (my_errno))); + return False; + } + + return True; } +/* --------------------------------------------------------------------------------------------- */ + #if 1 static int smbfs_fs (const char *text) { - const char *p = text; - int count = 0; - - while ((p = strchr(p, '/')) != NULL) { - count++; - p++; - } - if (count == 1) - return strlen(text); - return count; + const char *p = text; + int count = 0; + + while ((p = strchr (p, '/')) != NULL) + { + count++; + p++; + } + if (count == 1) + return strlen (text); + return count; } #endif +/* --------------------------------------------------------------------------------------------- */ + static int -smbfs_loaddir (opendir_info *smbfs_info) +smbfs_loaddir (opendir_info * smbfs_info) { uint16 attribute = aDIR | aSYSTEM | aHIDDEN; int servlen = strlen (smbfs_info->conn->service); @@ -726,82 +821,86 @@ smbfs_loaddir (opendir_info *smbfs_info) DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname)); first_direntry = TRUE; - if (current_info) { - DEBUG (3, - ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname, - current_info->dirname)); - /* if new desired dir is longer than cached in current_info */ - if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) { - DEBUG (3, ("saving to previous_info\n")); - previous_info = current_info; - } + if (current_info) + { + DEBUG (3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname, current_info->dirname)); + /* if new desired dir is longer than cached in current_info */ + if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) + { + DEBUG (3, ("saving to previous_info\n")); + previous_info = current_info; + } } current_info = smbfs_info; - if (strcmp (info_dirname, "/") == 0) { - if (!strcmp (smbfs_info->path, URL_HEADER)) { - DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC)); - /* browse for servers */ - if (!cli_NetServerEnum - (smbfs_info->conn->cli, smbfs_info->conn->domain, - SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL)) - return 0; - else - current_server_info = smbfs_info; - smbfs_info->server_list = TRUE; - } else { - /* browse for shares */ - if (cli_RNetShareEnum - (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1) - return 0; - else - current_share_info = smbfs_info; - } - goto done; + if (strcmp (info_dirname, "/") == 0) + { + if (!strcmp (smbfs_info->path, URL_HEADER)) + { + DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC)); + /* browse for servers */ + if (!cli_NetServerEnum + (smbfs_info->conn->cli, smbfs_info->conn->domain, + SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL)) + return 0; + else + current_server_info = smbfs_info; + smbfs_info->server_list = TRUE; + } + else + { + /* browse for shares */ + if (cli_RNetShareEnum (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1) + return 0; + else + current_share_info = smbfs_info; + } + goto done; } /* do regular directory listing */ - if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) { - /* strip share name from dir */ - my_dirname = g_strdup (info_dirname + servlen); - *my_dirname = '/'; - my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname); - } else - my_dirname = smbfs_convert_path (info_dirname, TRUE); + if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) + { + /* strip share name from dir */ + my_dirname = g_strdup (info_dirname + servlen); + *my_dirname = '/'; + my_dirname = free_after (smbfs_convert_path (my_dirname, TRUE), my_dirname); + } + else + my_dirname = smbfs_convert_path (info_dirname, TRUE); DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service)); - DEBUG (6, - ("smbfs_loaddir: cli->share: %s\n", - smbfs_info->conn->cli->share)); - DEBUG (6, - ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname)); + DEBUG (6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share)); + DEBUG (6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname)); /* do file listing: cli_list returns number of files */ - if (cli_list - (smbfs_info->conn->cli, my_dirname, attribute, - smbfs_loaddir_helper, NULL) < 0) { - /* cli_list returns -1 if directory empty or cannot read socket */ - my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL); - g_free (my_dirname); - return 0; + if (cli_list (smbfs_info->conn->cli, my_dirname, attribute, smbfs_loaddir_helper, NULL) < 0) + { + /* cli_list returns -1 if directory empty or cannot read socket */ + my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL); + g_free (my_dirname); + return 0; } if (*(my_dirname) == 0) - smbfs_info->dirname = smbfs_info->conn->service; + smbfs_info->dirname = smbfs_info->conn->service; g_free (my_dirname); -/* do_dskattr(); */ + /* do_dskattr(); */ done: -/* current_info->parent = smbfs_info->dirname; */ + /* current_info->parent = smbfs_info->dirname; */ smbfs_info->current = smbfs_info->entries; - return 1; /* 1 = ok */ + return 1; /* 1 = ok */ } -#ifdef SMBFS_FREE_DIR +/* --------------------------------------------------------------------------------------------- */ + +#ifdef SMBFS_FREE_DIR static void -smbfs_free_dir (dir_entry *de) +smbfs_free_dir (dir_entry * de) { - if (!de) return; + if (!de) + return; smbfs_free_dir (de->next); g_free (de->text); @@ -809,279 +908,309 @@ smbfs_free_dir (dir_entry *de) } #endif - +/* --------------------------------------------------------------------------------------------- */ /* The readdir routine loads the complete directory */ /* It's too slow to ask the server each time */ /* It now also sends the complete lstat information for each file */ + static void * -smbfs_readdir(void *info) +smbfs_readdir (void *info) { static union vfs_dirent smbfs_readdir_data; static char *const dirent_dest = smbfs_readdir_data.dent.d_name; opendir_info *smbfs_info = (opendir_info *) info; - DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname)); + DEBUG (4, ("smbfs_readdir(%s)\n", smbfs_info->dirname)); if (!smbfs_info->entries) - if (!smbfs_loaddir(smbfs_info)) - return NULL; + if (!smbfs_loaddir (smbfs_info)) + return NULL; - if (smbfs_info->current == 0) { /* reached end of dir entries */ - DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n")); + if (smbfs_info->current == 0) + { /* reached end of dir entries */ + DEBUG (3, ("smbfs_readdir: smbfs_info->current = 0\n")); #ifdef SMBFS_FREE_DIR - smbfs_free_dir(smbfs_info->entries); - smbfs_info->entries = 0; + smbfs_free_dir (smbfs_info->entries); + smbfs_info->entries = 0; #endif - return NULL; + return NULL; } - g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN); + g_strlcpy (dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN); smbfs_info->current = smbfs_info->current->next; - compute_namelen(&smbfs_readdir_data.dent); + compute_namelen (&smbfs_readdir_data.dent); return &smbfs_readdir_data; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_closedir (void *info) { opendir_info *smbfs_info = (opendir_info *) info; -/* dir_entry *p, *q; */ + /* dir_entry *p, *q; */ - DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname)); - /* CLOSE HERE */ - -/* for (p = smbfs_info->entries; p;){ - q = p; - p = p->next; - g_free (q->text); - g_free (q); - } - g_free (info); */ + DEBUG (3, ("smbfs_closedir(%s)\n", smbfs_info->dirname)); + /* CLOSE HERE */ + + /* for (p = smbfs_info->entries; p;){ + q = p; + p = p->next; + g_free (q->text); + g_free (q); + } + g_free (info); */ return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_chmod (struct vfs_class *me, const char *path, int mode) { - (void) me; + (void) me; - DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode)); -/* my_errno = EOPNOTSUPP; - return -1; */ /* cannot chmod on smb filesystem */ - return 0; /* make mc happy */ + DEBUG (3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode)); + /* my_errno = EOPNOTSUPP; + return -1; *//* cannot chmod on smb filesystem */ + return 0; /* make mc happy */ } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group) { - (void) me; + (void) me; - DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group)); - my_errno = EOPNOTSUPP; /* ready for your labotomy? */ - return -1; + DEBUG (3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group)); + my_errno = EOPNOTSUPP; /* ready for your labotomy? */ + return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times) { - (void) me; - (void) times; + (void) me; + (void) times; - DEBUG(3, ("smbfs_utime(path:%s)\n", path)); - my_errno = EOPNOTSUPP; - return -1; + DEBUG (3, ("smbfs_utime(path:%s)\n", path)); + my_errno = EOPNOTSUPP; + return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size) { - (void) me; + (void) me; - DEBUG (3, - ("smbfs_readlink(path:%s, buf:%s, size:%zu)\n", path, buf, size)); - my_errno = EOPNOTSUPP; - return -1; /* no symlinks on smb filesystem? */ + DEBUG (3, ("smbfs_readlink(path:%s, buf:%s, size:%zu)\n", path, buf, size)); + my_errno = EOPNOTSUPP; + return -1; /* no symlinks on smb filesystem? */ } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2) { - (void) me; + (void) me; - DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2)); - my_errno = EOPNOTSUPP; - return -1; /* no symlinks on smb filesystem? */ + DEBUG (3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2)); + my_errno = EOPNOTSUPP; + return -1; /* no symlinks on smb filesystem? */ } -/* Extract the hostname and username from the path */ -/* path is in the form: [user@]hostname/share/remote-dir */ -#define smbfs_get_host_and_username(path, host, user, port, pass) \ - vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0) - -/***************************************************** - return a connection to a SMB server - current_bucket needs to be set before calling +/* --------------------------------------------------------------------------------------------- */ +/***************************************************** + return a connection to a SMB server + current_bucket needs to be set before calling *******************************************************/ + static struct cli_state * smbfs_do_connect (const char *server, char *share) { - struct cli_state *c; - struct nmb_name called, calling; - struct in_addr ip; + struct cli_state *c; + struct nmb_name called, calling; + struct in_addr ip; - DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share)); - if (*share == '\\') { - server = share+2; - share = strchr(server,'\\'); - if (!share) return NULL; - *share = 0; - share++; - } - - make_nmb_name(&calling, global_myname, 0x0); - make_nmb_name(&called , server, current_bucket->name_type); - - for (;;) { - - ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero; - - /* have to open a new connection */ - if (!(c = cli_initialise(NULL))) { - my_errno = ENOMEM; - return NULL; - } - - pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */ - pwd_set_cleartext(&(c->pwd), current_bucket->password); - - if ((cli_set_port(c, current_bucket->port) == 0) || - !cli_connect(c, server, &ip)) { - DEBUG(1, ("Connection to %s failed\n", server)); - break; - } - - if (!cli_session_request(c, &calling, &called)) { - my_errno = cli_error(c, NULL, &err, NULL); - DEBUG(1, ("session request to %s failed\n", called.name)); - cli_shutdown(c); - if (strcmp(called.name, "*SMBSERVER")) { - make_nmb_name(&called , "*SMBSERVER", 0x20); - continue; - } - return NULL; - } - - DEBUG(3, (" session request ok\n")); - - if (!cli_negprot(c)) { - DEBUG(1, ("protocol negotiation failed\n")); - break; - } - - if (!cli_session_setup(c, current_bucket->user, - current_bucket->password, strlen(current_bucket->password), - current_bucket->password, strlen(current_bucket->password), - current_bucket->domain)) { - DEBUG(1,("session setup failed: %s\n", cli_errstr(c))); - smbfs_auth_remove (server, share); - break; - } - - if (*c->server_domain || *c->server_os || *c->server_type) - DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n", - c->server_domain,c->server_os,c->server_type)); - - DEBUG(3, (" session setup ok\n")); - - if (!cli_send_tconX(c, share, "?????", - current_bucket->password, strlen(current_bucket->password)+1)) { - DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c))); - break; - } - - DEBUG(3, (" tconx ok\n")); - - my_errno = 0; - return c; + DEBUG (3, ("smbfs_do_connect(%s, %s)\n", server, share)); + if (*share == '\\') + { + server = share + 2; + share = strchr (server, '\\'); + if (!share) + return NULL; + *share = 0; + share++; } - my_errno = cli_error(c, NULL, &err, NULL); - cli_shutdown(c); + make_nmb_name (&calling, global_myname, 0x0); + make_nmb_name (&called, server, current_bucket->name_type); + + for (;;) + { + + ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero; + + /* have to open a new connection */ + if (!(c = cli_initialise (NULL))) + { + my_errno = ENOMEM; + return NULL; + } + + pwd_init (&(c->pwd)); /* should be moved into cli_initialise()? */ + pwd_set_cleartext (&(c->pwd), current_bucket->password); + + if ((cli_set_port (c, current_bucket->port) == 0) || !cli_connect (c, server, &ip)) + { + DEBUG (1, ("Connection to %s failed\n", server)); + break; + } + + if (!cli_session_request (c, &calling, &called)) + { + my_errno = cli_error (c, NULL, &err, NULL); + DEBUG (1, ("session request to %s failed\n", called.name)); + cli_shutdown (c); + if (strcmp (called.name, "*SMBSERVER")) + { + make_nmb_name (&called, "*SMBSERVER", 0x20); + continue; + } + return NULL; + } + + DEBUG (3, (" session request ok\n")); + + if (!cli_negprot (c)) + { + DEBUG (1, ("protocol negotiation failed\n")); + break; + } + + if (!cli_session_setup (c, current_bucket->user, + current_bucket->password, strlen (current_bucket->password), + current_bucket->password, strlen (current_bucket->password), + current_bucket->domain)) + { + DEBUG (1, ("session setup failed: %s\n", cli_errstr (c))); + smbfs_auth_remove (server, share); + break; + } + + if (*c->server_domain || *c->server_os || *c->server_type) + DEBUG (5, ("Domain=[%s] OS=[%s] Server=[%s]\n", + c->server_domain, c->server_os, c->server_type)); + + DEBUG (3, (" session setup ok\n")); + + if (!cli_send_tconX (c, share, "?????", + current_bucket->password, strlen (current_bucket->password) + 1)) + { + DEBUG (1, ("%s: tree connect failed: %s\n", share, cli_errstr (c))); + break; + } + + DEBUG (3, (" tconx ok\n")); + + my_errno = 0; + return c; + } + + my_errno = cli_error (c, NULL, &err, NULL); + cli_shutdown (c); return NULL; - + } +/* --------------------------------------------------------------------------------------------- */ + static int -smbfs_get_master_browser(char **host) +smbfs_get_master_browser (char **host) { - static char so_broadcast[] = "SO_BROADCAST"; - int count; - struct in_addr *ip_list, bcast_addr; + static char so_broadcast[] = "SO_BROADCAST"; + int count; + struct in_addr *ip_list, bcast_addr; - /* does port = 137 for win95 master browser? */ - int fd= open_socket_in( SOCK_DGRAM, 0, 3, - interpret_addr(lp_socket_address()), True ); - if (fd == -1) - return 0; - set_socket_options(fd, so_broadcast); - ip_list = iface_bcast(ipzero); - bcast_addr = *ip_list; - if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True, - True, bcast_addr, &count, NULL))) { - if (!count) - return 0; - /* just return first master browser */ - *host = g_strdup(inet_ntoa(ip_list[0])); - return 1; - } - return 0; + /* does port = 137 for win95 master browser? */ + int fd = open_socket_in (SOCK_DGRAM, 0, 3, + interpret_addr (lp_socket_address ()), True); + if (fd == -1) + return 0; + set_socket_options (fd, so_broadcast); + ip_list = iface_bcast (ipzero); + bcast_addr = *ip_list; + if ((ip_list = name_query (fd, "\01\02__MSBROWSE__\02", 1, True, + True, bcast_addr, &count, NULL))) + { + if (!count) + return 0; + /* just return first master browser */ + *host = g_strdup (inet_ntoa (ip_list[0])); + return 1; + } + return 0; } -static void -smbfs_free_bucket (smbfs_connection *bucket) +/* --------------------------------------------------------------------------------------------- */ + +static void +smbfs_free_bucket (smbfs_connection * bucket) { - g_free (bucket->host); - g_free (bucket->service); - g_free (bucket->domain); - g_free (bucket->user); - wipe_password (bucket->password); - g_free (bucket->home); - memset (bucket, 0, sizeof (smbfs_connection)); + g_free (bucket->host); + g_free (bucket->service); + g_free (bucket->domain); + g_free (bucket->user); + wipe_password (bucket->password); + g_free (bucket->home); + memset (bucket, 0, sizeof (smbfs_connection)); } +/* --------------------------------------------------------------------------------------------- */ + static smbfs_connection * smbfs_get_free_bucket (void) { int i; - - for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) - if (!smbfs_connections [i].cli) return &smbfs_connections [i]; - { /* search for most dormant connection */ - int oldest = 0; /* index */ - time_t oldest_time = smbfs_connections[0].last_use; - for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) { - if (smbfs_connections[i].last_use < oldest_time) { - oldest_time = smbfs_connections[i].last_use; - oldest = i; - } - } - cli_shutdown(smbfs_connections[oldest].cli); - smbfs_free_bucket (&smbfs_connections[oldest]); - return &smbfs_connections[oldest]; - } + for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) + if (!smbfs_connections[i].cli) + return &smbfs_connections[i]; + + { /* search for most dormant connection */ + int oldest = 0; /* index */ + time_t oldest_time = smbfs_connections[0].last_use; + for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) + { + if (smbfs_connections[i].last_use < oldest_time) + { + oldest_time = smbfs_connections[i].last_use; + oldest = i; + } + } + cli_shutdown (smbfs_connections[oldest].cli); + smbfs_free_bucket (&smbfs_connections[oldest]); + return &smbfs_connections[oldest]; + } /* This can't happend, since we have checked for max connections before */ - vfs_die("Internal error: smbfs_get_free_bucket"); - return 0; /* shut up, stupid gcc */ + vfs_die ("Internal error: smbfs_get_free_bucket"); + return 0; /* shut up, stupid gcc */ } +/* --------------------------------------------------------------------------------------------- */ /* This routine keeps track of open connections */ /* Returns a connected socket to host */ + static smbfs_connection * -smbfs_open_link (char *host, char *path, const char *user, int *port, - char *this_pass) +smbfs_open_link (char *host, char *path, const char *user, int *port, char *this_pass) { int i; smbfs_connection *bucket; @@ -1090,48 +1219,52 @@ smbfs_open_link (char *host, char *path, const char *user, int *port, DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path)); - if (strcmp (host, path) == 0) /* if host & path are same: */ - pstrcpy (service, IPC); /* setup for browse */ - else { /* get share name from path, path starts with server name */ - char *p; - if ((p = strchr (path, '/'))) /* get share aka */ - pstrcpy (service, ++p); /* service name from path */ - else - pstrcpy (service, ""); - /* now check for trailing directory/filenames */ - p = strchr (service, '/'); - if (p) - *p = 0; /* cut off dir/files: sharename only */ - if (!*service) - pstrcpy (service, IPC); /* setup for browse */ - DEBUG (6, ("smbfs_open_link: service from path:%s\n", service)); + if (strcmp (host, path) == 0) /* if host & path are same: */ + pstrcpy (service, IPC); /* setup for browse */ + else + { /* get share name from path, path starts with server name */ + char *p; + if ((p = strchr (path, '/'))) /* get share aka */ + pstrcpy (service, ++p); /* service name from path */ + else + pstrcpy (service, ""); + /* now check for trailing directory/filenames */ + p = strchr (service, '/'); + if (p) + *p = 0; /* cut off dir/files: sharename only */ + if (!*service) + pstrcpy (service, IPC); /* setup for browse */ + DEBUG (6, ("smbfs_open_link: service from path:%s\n", service)); } if (got_user) - user = username; /* global from getenv */ + user = username; /* global from getenv */ /* Is the link actually open? */ - for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) { - if (!smbfs_connections[i].cli) - continue; - if ((strcmp (host, smbfs_connections[i].host) == 0) && - (strcmp (user, smbfs_connections[i].user) == 0) && - (strcmp (service, smbfs_connections[i].service) == 0)) { - int retries = 0; - BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/')); - /* check if this connection has died */ - while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) { - if (!smbfs_reconnect (&smbfs_connections[i], &retries)) - return 0; - } - DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i)); - current_bucket = &smbfs_connections[i]; - smbfs_connections[i].last_use = time (NULL); - return &smbfs_connections[i]; - } - /* connection not found, find if we have ip for new connection */ - if (strcmp (host, smbfs_connections[i].host) == 0) - dest_ip = &smbfs_connections[i].cli->dest_ip; + for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) + { + if (!smbfs_connections[i].cli) + continue; + if ((strcmp (host, smbfs_connections[i].host) == 0) && + (strcmp (user, smbfs_connections[i].user) == 0) && + (strcmp (service, smbfs_connections[i].service) == 0)) + { + int retries = 0; + BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/')); + /* check if this connection has died */ + while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) + { + if (!smbfs_reconnect (&smbfs_connections[i], &retries)) + return 0; + } + DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i)); + current_bucket = &smbfs_connections[i]; + smbfs_connections[i].last_use = time (NULL); + return &smbfs_connections[i]; + } + /* connection not found, find if we have ip for new connection */ + if (strcmp (host, smbfs_connections[i].host) == 0) + dest_ip = &smbfs_connections[i].cli->dest_ip; } /* make new connection */ @@ -1140,47 +1273,52 @@ smbfs_open_link (char *host, char *path, const char *user, int *port, bucket->home = 0; bucket->port = *port; bucket->have_ip = False; - if (dest_ip) { - bucket->have_ip = True; - bucket->dest_ip = *dest_ip; + if (dest_ip) + { + bucket->have_ip = True; + bucket->dest_ip = *dest_ip; } current_bucket = bucket; bucket->user = g_strdup (user); bucket->service = g_strdup (service); - if (!(*host)) { /* if blank host name, browse for servers */ - if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */ - return 0; /* could not find master browser? */ - g_free (host); - bucket->host = g_strdup (""); /* blank host means master browser */ - } else - bucket->host = g_strdup (host); + if (!(*host)) + { /* if blank host name, browse for servers */ + if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */ + return 0; /* could not find master browser? */ + g_free (host); + bucket->host = g_strdup (""); /* blank host means master browser */ + } + else + bucket->host = g_strdup (host); - if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */ - user, this_pass, 1)) - return 0; + if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */ + user, this_pass, 1)) + return 0; /* connect to share */ - while (!(bucket->cli = smbfs_do_connect (host, service))) { + while (!(bucket->cli = smbfs_do_connect (host, service))) + { - if (my_errno != EPERM) - return 0; - message (D_ERROR, MSG_ERROR, _("Authentication failed")); + if (my_errno != EPERM) + return 0; + message (D_ERROR, MSG_ERROR, _("Authentication failed")); - /* authentication failed, try again */ - smbfs_auth_remove (bucket->host, bucket->service); - if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0)) - return 0; + /* authentication failed, try again */ + smbfs_auth_remove (bucket->host, bucket->service); + if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0)) + return 0; } smbfs_open_connections++; - DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n", - smbfs_open_connections)); + DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n", smbfs_open_connections)); return bucket; } +/* --------------------------------------------------------------------------------------------- */ + static char * smbfs_get_path (smbfs_connection ** sc, const char *path) { @@ -1189,71 +1327,75 @@ smbfs_get_path (smbfs_connection ** sc, const char *path) DEBUG (3, ("smbfs_get_path(%s)\n", path)); if (strncmp (path, URL_HEADER, HEADER_LEN)) - return NULL; + return NULL; path += HEADER_LEN; - if (*path == '/') /* '/' leading server name */ - path++; /* probably came from server browsing */ + if (*path == '/') /* '/' leading server name */ + path++; /* probably came from server browsing */ - if ((remote_path = - smbfs_get_host_and_username (&path, &host, &user, &port, &pass))) - if ((*sc = - smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) { - g_free (remote_path); - remote_path = NULL; - } + if ((remote_path = smbfs_get_host_and_username (&path, &host, &user, &port, &pass))) + if ((*sc = smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) + { + g_free (remote_path); + remote_path = NULL; + } g_free (host); g_free (user); if (pass) - wipe_password (pass); + wipe_password (pass); if (!remote_path) - return NULL; + return NULL; /* NOTE: tildes are deprecated. See ftpfs.c */ { - int f = !strcmp (remote_path, "/~"); - if (f || !strncmp (remote_path, "/~/", 3)) { - char *s; - s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f); - g_free (remote_path); - return s; - } + int f = !strcmp (remote_path, "/~"); + if (f || !strncmp (remote_path, "/~/", 3)) + { + char *s; + s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f); + g_free (remote_path); + return s; + } } return remote_path; } +/* --------------------------------------------------------------------------------------------- */ + #if 0 static int is_error (int result, int errno_num) { if (!(result == -1)) - return my_errno = 0; - else - my_errno = errno_num; + return my_errno = 0; + else + my_errno = errno_num; return 1; } #endif +/* --------------------------------------------------------------------------------------------- */ + static void * smbfs_opendir (struct vfs_class *me, const char *dirname) { opendir_info *smbfs_info; - smbfs_connection *sc; - char *remote_dir; + smbfs_connection *sc; + char *remote_dir; (void) me; - DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname)); + DEBUG (3, ("smbfs_opendir(dirname:%s)\n", dirname)); if (!(remote_dir = smbfs_get_path (&sc, dirname))) - return NULL; + return NULL; /* FIXME: where freed? */ smbfs_info = g_new (opendir_info, 1); - smbfs_info->server_list = FALSE; - smbfs_info->path = g_strdup(dirname); /* keep original */ - smbfs_info->dirname = remote_dir; + smbfs_info->server_list = FALSE; + smbfs_info->path = g_strdup (dirname); /* keep original */ + smbfs_info->dirname = remote_dir; smbfs_info->conn = sc; smbfs_info->entries = 0; smbfs_info->current = 0; @@ -1261,6 +1403,8 @@ smbfs_opendir (struct vfs_class *me, const char *dirname) return smbfs_info; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf) { @@ -1270,83 +1414,92 @@ smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *b (void) server_url; if ((p = strrchr (path, '/'))) - path = p + 1; /* advance until last '/' */ + path = p + 1; /* advance until last '/' */ - if (!current_info->entries) { - if (!smbfs_loaddir (current_info)) /* browse host */ - return -1; + if (!current_info->entries) + { + if (!smbfs_loaddir (current_info)) /* browse host */ + return -1; } - if (current_info->server_list == True) { - dentry = current_info->entries; - DEBUG (4, ("fake stat for SERVER \"%s\"\n", path)); - while (dentry) { - if (strcmp (dentry->text, path) == 0) { - DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n", - dentry->text, (int)dentry->my_stat.st_mode)); - memcpy (buf, &dentry->my_stat, sizeof (struct stat)); - return 0; - } - dentry = dentry->next; - } + if (current_info->server_list == True) + { + dentry = current_info->entries; + DEBUG (4, ("fake stat for SERVER \"%s\"\n", path)); + while (dentry) + { + if (strcmp (dentry->text, path) == 0) + { + DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n", + dentry->text, (int) dentry->my_stat.st_mode)); + memcpy (buf, &dentry->my_stat, sizeof (struct stat)); + return 0; + } + dentry = dentry->next; + } } my_errno = ENOENT; return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf) { dir_entry *dentry; if (strlen (path) < strlen (server_url)) - return -1; + return -1; - if (!current_share_info) { /* Server was not stat()ed */ - /* Make sure there is such share at server */ - smbfs_connection *sc; - char *p; - p = smbfs_get_path (&sc, path); - g_free (p); - if (p) { - memset (buf, 0, sizeof (*buf)); - /* show this as dir */ - buf->st_mode = - (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | - S_IXOTH) & myumask; - return 0; - } - return -1; + if (!current_share_info) + { /* Server was not stat()ed */ + /* Make sure there is such share at server */ + smbfs_connection *sc; + char *p; + p = smbfs_get_path (&sc, path); + g_free (p); + if (p) + { + memset (buf, 0, sizeof (*buf)); + /* show this as dir */ + buf->st_mode = + (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH) & myumask; + return 0; + } + return -1; } - path += strlen (server_url); /* we only want share name */ + path += strlen (server_url); /* we only want share name */ path++; - if (*path == '/') /* '/' leading server name */ - path++; /* probably came from server browsing */ + if (*path == '/') /* '/' leading server name */ + path++; /* probably came from server browsing */ - if (!current_share_info->entries) { - if (!smbfs_loaddir (current_share_info)) /* browse host */ - return -1; + if (!current_share_info->entries) + { + if (!smbfs_loaddir (current_share_info)) /* browse host */ + return -1; } dentry = current_share_info->entries; DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url)); - while (dentry) { - if (strcmp (dentry->text, path) == 0) { - DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n", - dentry->text, (int) dentry->my_stat.st_mode)); - memcpy (buf, &dentry->my_stat, sizeof (struct stat)); - return 0; - } - dentry = dentry->next; + while (dentry) + { + if (strcmp (dentry->text, path) == 0) + { + DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n", + dentry->text, (int) dentry->my_stat.st_mode)); + memcpy (buf, &dentry->my_stat, sizeof (struct stat)); + return 0; + } + dentry = dentry->next; } my_errno = ENOENT; return -1; } -/* stat a single file, smbfs_get_remote_stat callback */ -static dir_entry *single_entry; - +/* --------------------------------------------------------------------------------------------- */ /* stat a single file */ + static int smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf) { @@ -1357,7 +1510,7 @@ smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf mypath = smbfs_convert_path (path, FALSE); -#if 0 /* single_entry is never free()d now. And only my_stat is used */ +#if 0 /* single_entry is never free()d now. And only my_stat is used */ single_entry = g_new (dir_entry, 1); single_entry->text = dos_to_unix (g_strdup (finfo->name), 1); @@ -1365,39 +1518,44 @@ smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf single_entry->next = 0; #endif if (!single_entry) - single_entry = g_new0 (dir_entry, 1); + single_entry = g_new0 (dir_entry, 1); - if (cli_list - (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) { - my_errno = ENOENT; - g_free (mypath); - return -1; /* cli_list returns number of files */ + if (cli_list (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) + { + my_errno = ENOENT; + g_free (mypath); + return -1; /* cli_list returns number of files */ } memcpy (buf, &single_entry->my_stat, sizeof (struct stat)); -/* don't free here, use for smbfs_fstat() */ -/* g_free(single_entry->text); - g_free(single_entry); */ + /* don't free here, use for smbfs_fstat() */ + /* g_free(single_entry->text); + g_free(single_entry); */ g_free (mypath); return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int -smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf) +smbfs_search_dir_entry (dir_entry * dentry, const char *text, struct stat *buf) { - while (dentry) { - if (strcmp(text, dentry->text) == 0) { - memcpy(buf, &dentry->my_stat, sizeof(struct stat)); - memcpy(&single_entry->my_stat, &dentry->my_stat, - sizeof(struct stat)); - return 0; - } - dentry = dentry->next; - } - return -1; + while (dentry) + { + if (strcmp (text, dentry->text) == 0) + { + memcpy (buf, &dentry->my_stat, sizeof (struct stat)); + memcpy (&single_entry->my_stat, &dentry->my_stat, sizeof (struct stat)); + return 0; + } + dentry = dentry->next; + } + return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf) { @@ -1407,87 +1565,93 @@ smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf) #endif const char *mypath = path; - mypath++; /* cut off leading '/' */ + mypath++; /* cut off leading '/' */ if ((p = strrchr (mypath, '/'))) - mypath = p + 1; /* advance until last file/dir name */ + mypath = p + 1; /* advance until last file/dir name */ DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n", - mypath, current_info->dirname)); + mypath, current_info->dirname)); #if 0 - if (!dentry) { - DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", - current_info->dirname, path)); - return -1; + if (!dentry) + { + DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", + current_info->dirname, path)); + return -1; } #endif - if (!single_entry) /* when found, this will be written too */ - single_entry = g_new (dir_entry, 1); - if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) { - return 0; + if (!single_entry) /* when found, this will be written too */ + single_entry = g_new (dir_entry, 1); + if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) + { + return 0; } /* now try to identify mypath as PARENT dir */ { - char *mdp; - char *mydir; - mdp = mydir = g_strdup (current_info->dirname); - if ((p = strrchr (mydir, '/'))) - *p = 0; /* advance util last '/' */ - if ((p = strrchr (mydir, '/'))) - mydir = p + 1; /* advance util last '/' */ - if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */ - memset (buf, 0, sizeof (struct stat)); - buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; - memcpy (&single_entry->my_stat, buf, sizeof (struct stat)); - g_free (mdp); - DEBUG (1, (" PARENT:found in %s\n", current_info->dirname)); - return 0; - } - g_free (mdp); + char *mdp; + char *mydir; + mdp = mydir = g_strdup (current_info->dirname); + if ((p = strrchr (mydir, '/'))) + *p = 0; /* advance util last '/' */ + if ((p = strrchr (mydir, '/'))) + mydir = p + 1; /* advance util last '/' */ + if (strcmp (mydir, mypath) == 0) + { /* fake a stat for ".." */ + memset (buf, 0, sizeof (struct stat)); + buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; + memcpy (&single_entry->my_stat, buf, sizeof (struct stat)); + g_free (mdp); + DEBUG (1, (" PARENT:found in %s\n", current_info->dirname)); + return 0; + } + g_free (mdp); } /* now try to identify as CURRENT dir? */ { - char *dnp = current_info->dirname; - DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n", - mypath, current_info->dirname)); - if (*dnp == '/') - dnp++; - else { - return -1; - } - if (strcmp (mypath, dnp) == 0) { - memset (buf, 0, sizeof (struct stat)); - buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; - memcpy (&single_entry->my_stat, buf, sizeof (struct stat)); - DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname)); - return 0; - } + char *dnp = current_info->dirname; + DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n", + mypath, current_info->dirname)); + if (*dnp == '/') + dnp++; + else + { + return -1; + } + if (strcmp (mypath, dnp) == 0) + { + memset (buf, 0, sizeof (struct stat)); + buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; + memcpy (&single_entry->my_stat, buf, sizeof (struct stat)); + DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname)); + return 0; + } } - DEBUG (3, ("'%s' not found in current_info '%s'\n", path, - current_info->dirname)); + DEBUG (3, ("'%s' not found in current_info '%s'\n", path, current_info->dirname)); /* try to find this in the PREVIOUS listing */ - if (previous_info) { - if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0) - return 0; - DEBUG (3, ("'%s' not found in previous_info '%s'\n", path, - previous_info->dirname)); + if (previous_info) + { + if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0) + return 0; + DEBUG (3, ("'%s' not found in previous_info '%s'\n", path, previous_info->dirname)); } /* try to find this in the SHARE listing */ - if (current_share_info) { - if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0) - return 0; - DEBUG (3, ("'%s' not found in share_info '%s'\n", path, - current_share_info->dirname)); + if (current_share_info) + { + if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0) + return 0; + DEBUG (3, ("'%s' not found in share_info '%s'\n", path, current_share_info->dirname)); } /* try to find this in the SERVER listing */ - if (current_server_info) { - if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0) - return 0; - DEBUG (3, ("'%s' not found in server_info '%s'\n", path, - current_server_info->dirname)); + if (current_server_info) + { + if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0) + return 0; + DEBUG (3, ("'%s' not found in server_info '%s'\n", path, current_server_info->dirname)); } /* nothing found. get stat file info from server */ return smbfs_get_remote_stat (sc, path, buf); } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_chdir (struct vfs_class *me, const char *path) { @@ -1498,36 +1662,40 @@ smbfs_chdir (struct vfs_class *me, const char *path) DEBUG (3, ("smbfs_chdir(path:%s)\n", path)); if (!(remote_dir = smbfs_get_path (&sc, path))) - return -1; + return -1; g_free (remote_dir); return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_loaddir_by_name (struct vfs_class *me, const char *path) { - void *info; - char *mypath, *p; + void *info; + char *mypath, *p; - mypath = g_strdup(path); - p = strrchr(mypath, '/'); + mypath = g_strdup (path); + p = strrchr (mypath, '/'); - if (p > mypath) - *p = 0; - DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath)); - smbfs_chdir(me, mypath); - info = smbfs_opendir (me, mypath); - g_free(mypath); - if (!info) - return -1; - smbfs_readdir(info); - smbfs_loaddir(info); - return 0; + if (p > mypath) + *p = 0; + DEBUG (6, ("smbfs_loaddir_by_name(%s)\n", mypath)); + smbfs_chdir (me, mypath); + info = smbfs_opendir (me, mypath); + g_free (mypath); + if (!info) + return -1; + smbfs_readdir (info); + smbfs_loaddir (info); + return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int -smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf) +smbfs_stat (struct vfs_class *me, const char *path, struct stat *buf) { smbfs_connection *sc; pstring server_url; @@ -1536,98 +1704,107 @@ smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf) DEBUG (3, ("smbfs_stat(path:%s)\n", path)); - if (!current_info) { - DEBUG (1, ("current_info = NULL: ")); - if (smbfs_loaddir_by_name (me, path) < 0) - return -1; + if (!current_info) + { + DEBUG (1, ("current_info = NULL: ")); + if (smbfs_loaddir_by_name (me, path) < 0) + return -1; } /* check if stating server */ p = path; - if (strncmp (p, URL_HEADER, HEADER_LEN)) { - DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n", - p, URL_HEADER, HEADER_LEN)); - return -1; + if (strncmp (p, URL_HEADER, HEADER_LEN)) + { + DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n", p, URL_HEADER, HEADER_LEN)); + return -1; } p += HEADER_LEN; if (*p == '/') - p++; + p++; - pp = strchr (p, '/'); /* advance past next '/' */ + pp = strchr (p, '/'); /* advance past next '/' */ at = strchr (p, '@'); pstrcpy (server_url, URL_HEADER); - if (at && at < pp) { /* user@server */ - char *z = &(server_url[sizeof (server_url) - 1]); - const char *s = p; + if (at && at < pp) + { /* user@server */ + char *z = &(server_url[sizeof (server_url) - 1]); + const char *s = p; - at = &(server_url [HEADER_LEN]) + (at - p + 1); - if (z > at) - z = at; - at = &(server_url [HEADER_LEN]); - while (at < z) - *at++ = *s++; - *z = 0; + at = &(server_url[HEADER_LEN]) + (at - p + 1); + if (z > at) + z = at; + at = &(server_url[HEADER_LEN]); + while (at < z) + *at++ = *s++; + *z = 0; } pstrcat (server_url, current_bucket->host); - if (!pp) { - if (!current_info->server_list) { - if (smbfs_loaddir_by_name (me, path) < 0) - return -1; - } - return smbfs_fake_server_stat (server_url, path, buf); + if (!pp) + { + if (!current_info->server_list) + { + if (smbfs_loaddir_by_name (me, path) < 0) + return -1; + } + return smbfs_fake_server_stat (server_url, path, buf); } - if (!strchr (++pp, '/')) { - return smbfs_fake_share_stat (server_url, path, buf); + if (!strchr (++pp, '/')) + { + return smbfs_fake_share_stat (server_url, path, buf); } /* stating inside share at this point */ - if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */ - return -1; + if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */ + return -1; { - int hostlen = strlen (current_bucket->host); - char *ppp = service + strlen (service) - hostlen; - char *sp = server_url + strlen (server_url) - hostlen; + int hostlen = strlen (current_bucket->host); + char *ppp = service + strlen (service) - hostlen; + char *sp = server_url + strlen (server_url) - hostlen; - if (strcmp (sp, ppp) == 0) { - /* make server name appear as directory */ - DEBUG (1, ("smbfs_stat: showing server as directory\n")); - memset (buf, 0, sizeof (struct stat)); - buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; - g_free (service); - return 0; - } + if (strcmp (sp, ppp) == 0) + { + /* make server name appear as directory */ + DEBUG (1, ("smbfs_stat: showing server as directory\n")); + memset (buf, 0, sizeof (struct stat)); + buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask; + g_free (service); + return 0; + } } /* check if current_info is in share requested */ p = service; pp = strchr (p, '/'); - if (pp) { - p = ++pp; /* advance past server name */ - pp = strchr (p, '/'); + if (pp) + { + p = ++pp; /* advance past server name */ + pp = strchr (p, '/'); } if (pp) - *pp = 0; /* cut off everthing after service name */ + *pp = 0; /* cut off everthing after service name */ else - p = IPC; /* browsing for services */ + p = IPC; /* browsing for services */ pp = current_info->dirname; if (*pp == '/') - pp++; - if (strncmp (p, pp, strlen (p)) != 0) { - DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp)); - if (smbfs_loaddir_by_name (me, path) < 0) { - g_free (service); - return -1; - } - DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname)); + pp++; + if (strncmp (p, pp, strlen (p)) != 0) + { + DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp)); + if (smbfs_loaddir_by_name (me, path) < 0) + { + g_free (service); + return -1; + } + DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname)); } g_free (service); /* stat dirs & files under shares now */ return smbfs_get_stat_info (sc, path, buf); } -#define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */ +/* --------------------------------------------------------------------------------------------- */ static off_t smbfs_lseek (void *data, off_t offset, int whence) @@ -1636,44 +1813,49 @@ smbfs_lseek (void *data, off_t offset, int whence) size_t size; DEBUG (3, - ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n", - (int) info->nread, (int) offset, whence)); + ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n", + (int) info->nread, (int) offset, whence)); - switch (whence) { + switch (whence) + { case SEEK_SET: - info->nread = offset; - break; + info->nread = offset; + break; case SEEK_CUR: - info->nread += offset; - break; + info->nread += offset; + break; case SEEK_END: - if (!cli_qfileinfo (info->cli, info->fnum, - NULL, &size, NULL, NULL, NULL, - NULL, NULL) && - !cli_getattrE (info->cli, info->fnum, - NULL, &size, NULL, NULL, NULL)) { - errno = EINVAL; - return -1; - } - info->nread = size + offset; - break; + if (!cli_qfileinfo (info->cli, info->fnum, + NULL, &size, NULL, NULL, NULL, + NULL, NULL) && + !cli_getattrE (info->cli, info->fnum, NULL, &size, NULL, NULL, NULL)) + { + errno = EINVAL; + return -1; + } + info->nread = size + offset; + break; } return info->nread; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_mknod (struct vfs_class *me, const char *path, mode_t mode, dev_t dev) { (void) me; - DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%u)\n", path, mode, (unsigned int)dev)); - my_errno = EOPNOTSUPP; + DEBUG (3, ("smbfs_mknod(path:%s, mode:%d, dev:%u)\n", path, mode, (unsigned int) dev)); + my_errno = EOPNOTSUPP; return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int -smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode) +smbfs_mkdir (struct vfs_class *me, const char *path, mode_t mode) { smbfs_connection *sc; char *remote_file; @@ -1683,48 +1865,54 @@ smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode) DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode)); if ((remote_file = smbfs_get_path (&sc, path)) == 0) - return -1; + return -1; g_free (remote_file); cpath = smbfs_convert_path (path, FALSE); - if (!cli_mkdir (sc->cli, cpath)) { - my_errno = cli_error (sc->cli, NULL, &err, NULL); - message (D_ERROR, MSG_ERROR, _("Error %s creating directory %s"), - cli_errstr (sc->cli), CNV_LANG (cpath)); - g_free (cpath); - return -1; + if (!cli_mkdir (sc->cli, cpath)) + { + my_errno = cli_error (sc->cli, NULL, &err, NULL); + message (D_ERROR, MSG_ERROR, _("Error %s creating directory %s"), + cli_errstr (sc->cli), CNV_LANG (cpath)); + g_free (cpath); + return -1; } g_free (cpath); return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_rmdir (struct vfs_class *me, const char *path) { - smbfs_connection *sc; - char *remote_file; - char *cpath; + smbfs_connection *sc; + char *remote_file; + char *cpath; - (void) me; + (void) me; - DEBUG(3, ("smbfs_rmdir(path:%s)\n", path)); - if ((remote_file = smbfs_get_path (&sc, path)) == 0) - return -1; - g_free (remote_file); - cpath = smbfs_convert_path (path, FALSE); + DEBUG (3, ("smbfs_rmdir(path:%s)\n", path)); + if ((remote_file = smbfs_get_path (&sc, path)) == 0) + return -1; + g_free (remote_file); + cpath = smbfs_convert_path (path, FALSE); - if (!cli_rmdir(sc->cli, cpath)) { - my_errno = cli_error(sc->cli, NULL, &err, NULL); - message (D_ERROR, MSG_ERROR, _("Error %s removing directory %s"), - cli_errstr(sc->cli), CNV_LANG(cpath)); - g_free (cpath); - return -1; - } + if (!cli_rmdir (sc->cli, cpath)) + { + my_errno = cli_error (sc->cli, NULL, &err, NULL); + message (D_ERROR, MSG_ERROR, _("Error %s removing directory %s"), + cli_errstr (sc->cli), CNV_LANG (cpath)); + g_free (cpath); + return -1; + } - g_free (cpath); - return 0; + g_free (cpath); + return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_link (struct vfs_class *me, const char *p1, const char *p2) { @@ -1735,6 +1923,8 @@ smbfs_link (struct vfs_class *me, const char *p1, const char *p2) return -1; } +/* --------------------------------------------------------------------------------------------- */ + static void smbfs_free (vfsid id) { @@ -1742,9 +1932,11 @@ smbfs_free (vfsid id) smbfs_auth_free_all (); } +/* --------------------------------------------------------------------------------------------- */ /* Gives up on a socket and reopens the connection, the child own the socket * now */ + static void smbfs_forget (const char *path) { @@ -1752,38 +1944,42 @@ smbfs_forget (const char *path) int port; if (strncmp (path, URL_HEADER, HEADER_LEN)) - return; + return; DEBUG (3, ("smbfs_forget(path:%s)\n", path)); path += 6; if (path[0] == '/' && path[1] == '/') - path += 2; + path += 2; - if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) { - size_t i; + if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) + { + size_t i; - g_free (p); + g_free (p); - for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) { - if (smbfs_connections[i].cli - && (strcmp (host, smbfs_connections[i].host) == 0) - && (strcmp (user, smbfs_connections[i].user) == 0) - && (port == smbfs_connections[i].port)) { + for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) + { + if (smbfs_connections[i].cli + && (strcmp (host, smbfs_connections[i].host) == 0) + && (strcmp (user, smbfs_connections[i].user) == 0) + && (port == smbfs_connections[i].port)) + { - /* close socket: the child owns it now */ - cli_shutdown (smbfs_connections[i].cli); + /* close socket: the child owns it now */ + cli_shutdown (smbfs_connections[i].cli); - /* reopen the connection */ - smbfs_connections[i].cli = - smbfs_do_connect (host, smbfs_connections[i].service); - } - } + /* reopen the connection */ + smbfs_connections[i].cli = smbfs_do_connect (host, smbfs_connections[i].service); + } + } } g_free (host); g_free (user); } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) { @@ -1791,100 +1987,105 @@ smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg) (void) arg; DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop)); - switch (ctlop) { + switch (ctlop) + { case VFS_SETCTL_FORGET: - smbfs_forget (path); - return 0; + smbfs_forget (path); + return 0; } return 0; } +/* --------------------------------------------------------------------------------------------- */ static smbfs_handle * -smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, mode_t mode) +smbfs_open_readwrite (smbfs_handle * remote_handle, char *rname, int flags, mode_t mode) { size_t size; (void) mode; - if (flags & O_TRUNC) /* if it exists truncate to zero */ - DEBUG (3, ("smbfs_open: O_TRUNC\n")); + if (flags & O_TRUNC) /* if it exists truncate to zero */ + DEBUG (3, ("smbfs_open: O_TRUNC\n")); remote_handle->fnum = -#if 1 /* Don't play with flags, it is cli_open() headache */ - cli_open (remote_handle->cli, rname, flags, DENY_NONE); -#else /* What's a reasons to has this code ? */ - cli_open (remote_handle->cli, rname, ((flags & O_CREAT) - || (flags == - (O_WRONLY | O_APPEND))) ? - flags : O_RDONLY, DENY_NONE); +#if 1 /* Don't play with flags, it is cli_open() headache */ + cli_open (remote_handle->cli, rname, flags, DENY_NONE); +#else /* What's a reasons to has this code ? */ + cli_open (remote_handle->cli, rname, ((flags & O_CREAT) + || (flags == + (O_WRONLY | O_APPEND))) ? + flags : O_RDONLY, DENY_NONE); #endif - if (remote_handle->fnum == -1) { - message (D_ERROR, MSG_ERROR, _("%s opening remote file %s"), - cli_errstr (remote_handle->cli), CNV_LANG (rname)); - DEBUG (1, ("smbfs_open(rname:%s) error:%s\n", - rname, cli_errstr (remote_handle->cli))); - my_errno = cli_error (remote_handle->cli, NULL, &err, NULL); - return NULL; + if (remote_handle->fnum == -1) + { + message (D_ERROR, MSG_ERROR, _("%s opening remote file %s"), + cli_errstr (remote_handle->cli), CNV_LANG (rname)); + DEBUG (1, ("smbfs_open(rname:%s) error:%s\n", rname, cli_errstr (remote_handle->cli))); + my_errno = cli_error (remote_handle->cli, NULL, &err, NULL); + return NULL; } if (flags & O_CREAT) - return remote_handle; + return remote_handle; if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum, - &remote_handle->attr, &size, NULL, NULL, NULL, NULL, - NULL) - && !cli_getattrE (remote_handle->cli, remote_handle->fnum, - &remote_handle->attr, &size, NULL, NULL, NULL)) { - message (D_ERROR, MSG_ERROR, "getattrib: %s", - cli_errstr (remote_handle->cli)); - DEBUG (1, - ("smbfs_open(rname:%s) getattrib:%s\n", rname, - cli_errstr (remote_handle->cli))); - my_errno = cli_error (remote_handle->cli, NULL, &err, NULL); - cli_close (remote_handle->cli, remote_handle->fnum); - return NULL; + &remote_handle->attr, &size, NULL, NULL, NULL, NULL, + NULL) + && !cli_getattrE (remote_handle->cli, remote_handle->fnum, + &remote_handle->attr, &size, NULL, NULL, NULL)) + { + message (D_ERROR, MSG_ERROR, "getattrib: %s", cli_errstr (remote_handle->cli)); + DEBUG (1, ("smbfs_open(rname:%s) getattrib:%s\n", rname, cli_errstr (remote_handle->cli))); + my_errno = cli_error (remote_handle->cli, NULL, &err, NULL); + cli_close (remote_handle->cli, remote_handle->fnum); + return NULL; } - if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */ - && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) { - cli_close (remote_handle->cli, remote_handle->fnum); - return NULL; + if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */ + && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) + { + cli_close (remote_handle->cli, remote_handle->fnum); + return NULL; } return remote_handle; } +/* --------------------------------------------------------------------------------------------- */ + static void * smbfs_open (struct vfs_class *me, const char *file, int flags, mode_t mode) { char *remote_file; void *ret; - smbfs_connection *sc; - smbfs_handle *remote_handle; + smbfs_connection *sc; + smbfs_handle *remote_handle; (void) me; - DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode)); + DEBUG (3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode)); if (!(remote_file = smbfs_get_path (&sc, file))) - return 0; + return 0; - remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file); + remote_file = free_after (smbfs_convert_path (remote_file, FALSE), remote_file); - remote_handle = g_new (smbfs_handle, 2); - remote_handle->cli = sc->cli; - remote_handle->nread = 0; + remote_handle = g_new (smbfs_handle, 2); + remote_handle->cli = sc->cli; + remote_handle->nread = 0; ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode); g_free (remote_file); if (!ret) - g_free (remote_handle); + g_free (remote_handle); return ret; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_unlink (struct vfs_class *me, const char *path) { @@ -1894,20 +2095,23 @@ smbfs_unlink (struct vfs_class *me, const char *path) (void) me; if ((remote_file = smbfs_get_path (&sc, path)) == 0) - return -1; + return -1; - remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file); + remote_file = free_after (smbfs_convert_path (remote_file, FALSE), remote_file); - if (!cli_unlink(sc->cli, remote_file)) { - message (D_ERROR, MSG_ERROR, _("%s removing remote file %s"), - cli_errstr(sc->cli), CNV_LANG(remote_file)); - g_free (remote_file); - return -1; - } + if (!cli_unlink (sc->cli, remote_file)) + { + message (D_ERROR, MSG_ERROR, _("%s removing remote file %s"), + cli_errstr (sc->cli), CNV_LANG (remote_file)); + g_free (remote_file); + return -1; + } g_free (remote_file); return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_rename (struct vfs_class *me, const char *a, const char *b) { @@ -1918,46 +2122,54 @@ smbfs_rename (struct vfs_class *me, const char *a, const char *b) (void) me; if ((ra = smbfs_get_path (&sc, a)) == 0) - return -1; - - if ((rb = smbfs_get_path (&sc, b)) == 0) { - g_free (ra); - return -1; + return -1; + + if ((rb = smbfs_get_path (&sc, b)) == 0) + { + g_free (ra); + return -1; } ra = free_after (smbfs_convert_path (ra, FALSE), ra); rb = free_after (smbfs_convert_path (rb, FALSE), rb); - retval = cli_rename(sc->cli, ra, rb); + retval = cli_rename (sc->cli, ra, rb); g_free (ra); g_free (rb); - if (!retval) { - message (D_ERROR, MSG_ERROR, _("%s renaming files\n"), - cli_errstr(sc->cli)); - return -1; - } + if (!retval) + { + message (D_ERROR, MSG_ERROR, _("%s renaming files\n"), cli_errstr (sc->cli)); + return -1; + } return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int smbfs_fstat (void *data, struct stat *buf) { - smbfs_handle *remote_handle = (smbfs_handle *)data; + smbfs_handle *remote_handle = (smbfs_handle *) data; - DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum)); + DEBUG (3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum)); - /* use left over from previous smbfs_get_remote_stat, if available */ - if (single_entry) - memcpy(buf, &single_entry->my_stat, sizeof(struct stat)); - else { /* single_entry not set up: bug */ - my_errno = EFAULT; - return -EFAULT; - } - return 0; + /* use left over from previous smbfs_get_remote_stat, if available */ + if (single_entry) + memcpy (buf, &single_entry->my_stat, sizeof (struct stat)); + else + { /* single_entry not set up: bug */ + my_errno = EFAULT; + return -EFAULT; + } + return 0; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + smb_authinfo * vfs_smb_authinfo_new (const char *host, const char *share, const char *domain, const char *user, const char *pass) @@ -1968,20 +2180,22 @@ vfs_smb_authinfo_new (const char *host, const char *share, const char *domain, if (auth != NULL) { - auth->host = g_strdup (host); - auth->share = g_strdup (share); - auth->domain = g_strdup (domain); - auth->user = g_strdup (user); - auth->password = g_strdup (pass); + auth->host = g_strdup (host); + auth->share = g_strdup (share); + auth->domain = g_strdup (domain); + auth->user = g_strdup (user); + auth->password = g_strdup (pass); } return auth; } +/* --------------------------------------------------------------------------------------------- */ + void init_smbfs (void) { - tcp_init(); + tcp_init (); vfs_smbfs_ops.name = "smbfs"; vfs_smbfs_ops.prefix = "smb:"; @@ -2016,3 +2230,5 @@ init_smbfs (void) vfs_smbfs_ops.setctl = smbfs_setctl; vfs_register_class (&vfs_smbfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/smbfs.h b/lib/vfs/mc-vfs/smbfs.h index b8686ebcb..d8a9d430d 100644 --- a/lib/vfs/mc-vfs/smbfs.h +++ b/lib/vfs/mc-vfs/smbfs.h @@ -4,12 +4,14 @@ * \brief Header: Virtual File System: smb file system */ -#ifndef MC_VFS_SMBFS_H -#define MC_VFS_SMBFS_H +#ifndef MC__VFS_SMBFS_H +#define MC__VFS_SMBFS_H -void init_smbfs (void); -void smbfs_set_debug (int arg); -void smbfs_set_debugf (const char *filename); +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ typedef struct smb_authinfo { @@ -20,16 +22,24 @@ typedef struct smb_authinfo char *password; } smb_authinfo; + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + + + +void init_smbfs (void); +void smbfs_set_debug (int arg); +void smbfs_set_debugf (const char *filename); + smb_authinfo *vfs_smb_authinfo_new (const char *host, const char *share, - const char *domain, - const char *user, - const char *pass); + const char *domain, const char *user, const char *pass); /* src/boxes.c */ smb_authinfo *vfs_smb_get_authinfo (const char *host, - const char *share, - const char *domain, - const char *user); + const char *share, const char *domain, const char *user); +/*** inline functions ****************************************************************************/ #endif /* MC_VFS_SMBFS_H */ diff --git a/lib/vfs/mc-vfs/tar.c b/lib/vfs/mc-vfs/tar.c index 2f4bed829..59e4fe343 100644 --- a/lib/vfs/mc-vfs/tar.c +++ b/lib/vfs/mc-vfs/tar.c @@ -1,7 +1,7 @@ /* Virtual File System: GNU Tar file system. Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. - + Written by: 1995 Jakub Jelinek Rewritten by: 1998 Pavel Machek @@ -42,22 +42,16 @@ #include "lib/global.h" -#include "src/wtools.h" /* message() */ +#include "src/wtools.h" /* message() */ #include "vfs-impl.h" #include "utilvfs.h" #include "xdirentry.h" -#include "gc.h" /* vfs_rmstamp */ +#include "gc.h" /* vfs_rmstamp */ -static struct vfs_class vfs_tarfs_ops; +/*** global variables ****************************************************************************/ -enum { - TAR_UNKNOWN = 0, - TAR_V7, - TAR_USTAR, - TAR_POSIX, - TAR_GNU -}; +/*** file scope macro definitions ****************************************************************/ /* * Header block on tape. @@ -67,174 +61,216 @@ enum { * A "record" is a piece of info that we care about. * Typically many "record"s fit into a "block". */ -#define RECORDSIZE 512 -#define NAMSIZ 100 -#define PREFIX_SIZE 155 -#define TUNMLEN 32 -#define TGNMLEN 32 +#define RECORDSIZE 512 +#define NAMSIZ 100 +#define PREFIX_SIZE 155 +#define TUNMLEN 32 +#define TGNMLEN 32 #define SPARSE_EXT_HDR 21 -#define SPARSE_IN_HDR 4 - -struct sparse { - char offset[12]; - char numbytes[12]; -}; - -struct sp_array { - int offset; - int numbytes; -}; - -union record { - char charptr[RECORDSIZE]; - struct header { - char arch_name[NAMSIZ]; - char mode[8]; - char uid[8]; - char gid[8]; - char size[12]; - char mtime[12]; - char chksum[8]; - char linkflag; - char arch_linkname[NAMSIZ]; - char magic[8]; - char uname[TUNMLEN]; - char gname[TGNMLEN]; - char devmajor[8]; - char devminor[8]; - /* The following bytes of the tar header record were originally unused. - - Archives following the ustar specification use almost all of those - bytes to support pathnames of 256 characters in length. - - GNU tar archives use the "unused" space to support incremental - archives and sparse files. */ - union unused { - char prefix[PREFIX_SIZE]; - /* GNU extensions to the ustar (POSIX.1-1988) archive format. */ - struct oldgnu { - char atime[12]; - char ctime[12]; - char offset[12]; - char longnames[4]; - char pad; - struct sparse sp[SPARSE_IN_HDR]; - char isextended; - char realsize[12]; /* true size of the sparse file */ - } oldgnu; - } unused; - } header; - struct extended_header { - struct sparse sp[21]; - char isextended; - } ext_hdr; -}; +#define SPARSE_IN_HDR 4 /* The checksum field is filled with this while the checksum is computed. */ -#define CHKBLANKS " " /* 8 blanks, no null */ +#define CHKBLANKS " " /* 8 blanks, no null */ /* The magic field is filled with this if uname and gname are valid. */ -#define TMAGIC "ustar" /* ustar and a null */ -#define OLDGNU_MAGIC "ustar " /* 7 chars and a null */ +#define TMAGIC "ustar" /* ustar and a null */ +#define OLDGNU_MAGIC "ustar " /* 7 chars and a null */ /* The linkflag defines the type of file */ -#define LF_OLDNORMAL '\0' /* Normal disk file, Unix compat */ -#define LF_NORMAL '0' /* Normal disk file */ -#define LF_LINK '1' /* Link to previously dumped file */ -#define LF_SYMLINK '2' /* Symbolic link */ -#define LF_CHR '3' /* Character special file */ -#define LF_BLK '4' /* Block special file */ -#define LF_DIR '5' /* Directory */ -#define LF_FIFO '6' /* FIFO special file */ -#define LF_CONTIG '7' /* Contiguous file */ -#define LF_EXTHDR 'x' /* pax Extended Header */ -#define LF_GLOBAL_EXTHDR 'g' /* pax Global Extended Header */ +#define LF_OLDNORMAL '\0' /* Normal disk file, Unix compat */ +#define LF_NORMAL '0' /* Normal disk file */ +#define LF_LINK '1' /* Link to previously dumped file */ +#define LF_SYMLINK '2' /* Symbolic link */ +#define LF_CHR '3' /* Character special file */ +#define LF_BLK '4' /* Block special file */ +#define LF_DIR '5' /* Directory */ +#define LF_FIFO '6' /* FIFO special file */ +#define LF_CONTIG '7' /* Contiguous file */ +#define LF_EXTHDR 'x' /* pax Extended Header */ +#define LF_GLOBAL_EXTHDR 'g' /* pax Global Extended Header */ /* Further link types may be defined later. */ /* Note that the standards committee allows only capital A through capital Z for user-defined expansion. This means that defining something as, say '8' is a *bad* idea. */ -#define LF_DUMPDIR 'D' /* This is a dir entry that contains - the names of files that were in - the dir at the time the dump - was made */ -#define LF_LONGLINK 'K' /* Identifies the NEXT file on the tape - as having a long linkname */ -#define LF_LONGNAME 'L' /* Identifies the NEXT file on the tape - as having a long name. */ -#define LF_MULTIVOL 'M' /* This is the continuation - of a file that began on another - volume */ -#define LF_NAMES 'N' /* For storing filenames that didn't - fit in 100 characters */ -#define LF_SPARSE 'S' /* This is for sparse files */ -#define LF_VOLHDR 'V' /* This file is a tape/volume header */ +#define LF_DUMPDIR 'D' /* This is a dir entry that contains + the names of files that were in + the dir at the time the dump + was made */ +#define LF_LONGLINK 'K' /* Identifies the NEXT file on the tape + as having a long linkname */ +#define LF_LONGNAME 'L' /* Identifies the NEXT file on the tape + as having a long name. */ +#define LF_MULTIVOL 'M' /* This is the continuation + of a file that began on another + volume */ +#define LF_NAMES 'N' /* For storing filenames that didn't + fit in 100 characters */ +#define LF_SPARSE 'S' /* This is for sparse files */ +#define LF_VOLHDR 'V' /* This file is a tape/volume header */ /* Ignore it on extraction */ /* * Exit codes from the "tar" program */ -#define EX_SUCCESS 0 /* success! */ -#define EX_ARGSBAD 1 /* invalid args */ -#define EX_BADFILE 2 /* invalid filename */ -#define EX_BADARCH 3 /* bad archive */ -#define EX_SYSTEM 4 /* system gave unexpected error */ -#define EX_BADVOL 5 /* Special error code means - Tape volume doesn't match the one - specified on the command line */ +#define EX_SUCCESS 0 /* success! */ +#define EX_ARGSBAD 1 /* invalid args */ +#define EX_BADFILE 2 /* invalid filename */ +#define EX_BADARCH 3 /* bad archive */ +#define EX_SYSTEM 4 /* system gave unexpected error */ +#define EX_BADVOL 5 /* Special error code means + Tape volume doesn't match the one + specified on the command line */ -#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) +#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) -/* - * Quick and dirty octal conversion. - * - * Result is -1 if the field is invalid (all blank, or nonoctal). - */ -static long tar_from_oct (int digs, char *where) +/*** file scope type declarations ****************************************************************/ + +enum { - register long value; + TAR_UNKNOWN = 0, + TAR_V7, + TAR_USTAR, + TAR_POSIX, + TAR_GNU +}; - while (isspace ((unsigned char) *where)) { /* Skip spaces */ - where++; - if (--digs <= 0) - return -1; /* All blank field */ - } - value = 0; - while (digs > 0 && isodigit (*where)) { /* Scan till nonoctal */ - value = (value << 3) | (*where++ - '0'); - --digs; - } - - if (digs > 0 && *where && !isspace ((unsigned char) *where)) - return -1; /* Ended on non-space/nul */ - - return value; -} - -static void tar_free_archive (struct vfs_class *me, struct vfs_s_super *archive) +struct sparse { - (void) me; + char offset[12]; + char numbytes[12]; +}; - if (archive->u.arch.fd != -1) - mc_close(archive->u.arch.fd); -} +struct sp_array +{ + int offset; + int numbytes; +}; + +union record +{ + char charptr[RECORDSIZE]; + struct header + { + char arch_name[NAMSIZ]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char linkflag; + char arch_linkname[NAMSIZ]; + char magic[8]; + char uname[TUNMLEN]; + char gname[TGNMLEN]; + char devmajor[8]; + char devminor[8]; + /* The following bytes of the tar header record were originally unused. + + Archives following the ustar specification use almost all of those + bytes to support pathnames of 256 characters in length. + + GNU tar archives use the "unused" space to support incremental + archives and sparse files. */ + union unused + { + char prefix[PREFIX_SIZE]; + /* GNU extensions to the ustar (POSIX.1-1988) archive format. */ + struct oldgnu + { + char atime[12]; + char ctime[12]; + char offset[12]; + char longnames[4]; + char pad; + struct sparse sp[SPARSE_IN_HDR]; + char isextended; + char realsize[12]; /* true size of the sparse file */ + } oldgnu; + } unused; + } header; + struct extended_header + { + struct sparse sp[21]; + char isextended; + } ext_hdr; +}; + +typedef enum +{ + STATUS_BADCHECKSUM, + STATUS_SUCCESS, + STATUS_EOFMARK, + STATUS_EOF +} ReadStatus; + +/*** file scope variables ************************************************************************/ + +static struct vfs_class vfs_tarfs_ops; /* As we open one archive at a time, it is safe to have this static */ static int current_tar_position = 0; +static union record rec_buf; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/** + * Quick and dirty octal conversion. + * + * Result is -1 if the field is invalid (all blank, or nonoctal). + */ +static long +tar_from_oct (int digs, char *where) +{ + register long value; + + while (isspace ((unsigned char) *where)) + { /* Skip spaces */ + where++; + if (--digs <= 0) + return -1; /* All blank field */ + } + value = 0; + while (digs > 0 && isodigit (*where)) + { /* Scan till nonoctal */ + value = (value << 3) | (*where++ - '0'); + --digs; + } + + if (digs > 0 && *where && !isspace ((unsigned char) *where)) + return -1; /* Ended on non-space/nul */ + + return value; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +tar_free_archive (struct vfs_class *me, struct vfs_s_super *archive) +{ + (void) me; + + if (archive->u.arch.fd != -1) + mc_close (archive->u.arch.fd); +} + +/* --------------------------------------------------------------------------------------------- */ + /* Returns fd of the open tar file */ static int -tar_open_archive_int (struct vfs_class *me, const char *name, - struct vfs_s_super *archive) +tar_open_archive_int (struct vfs_class *me, const char *name, struct vfs_s_super *archive) { int result, type; mode_t mode; struct vfs_s_inode *root; result = mc_open (name, O_RDONLY); - if (result == -1) { - message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), name); - ERRNOR (ENOENT, -1); + if (result == -1) + { + message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), name); + ERRNOR (ENOENT, -1); } archive->name = g_strdup (name); @@ -245,26 +281,27 @@ tar_open_archive_int (struct vfs_class *me, const char *name, /* Find out the method to handle this tar file */ type = get_compression_type (result, name); mc_lseek (result, 0, SEEK_SET); - if (type != COMPRESSION_NONE) { - char *s; - mc_close (result); - s = g_strconcat (archive->name, decompress_extension (type), (char *) NULL); - result = mc_open (s, O_RDONLY); - if (result == -1) - message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), s); - g_free (s); - if (result == -1) - ERRNOR (ENOENT, -1); + if (type != COMPRESSION_NONE) + { + char *s; + mc_close (result); + s = g_strconcat (archive->name, decompress_extension (type), (char *) NULL); + result = mc_open (s, O_RDONLY); + if (result == -1) + message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), s); + g_free (s); + if (result == -1) + ERRNOR (ENOENT, -1); } archive->u.arch.fd = result; mode = archive->u.arch.st.st_mode & 07777; if (mode & 0400) - mode |= 0100; + mode |= 0100; if (mode & 0040) - mode |= 0010; + mode |= 0010; if (mode & 0004) - mode |= 0001; + mode |= 0001; mode |= S_IFDIR; root = vfs_s_new_inode (me, archive, &archive->u.arch.st); @@ -278,7 +315,7 @@ tar_open_archive_int (struct vfs_class *me, const char *name, return result; } -static union record rec_buf; +/* --------------------------------------------------------------------------------------------- */ static union record * tar_get_next_record (struct vfs_s_super *archive, int tard) @@ -289,12 +326,15 @@ tar_get_next_record (struct vfs_s_super *archive, int tard) n = mc_read (tard, rec_buf.charptr, RECORDSIZE); if (n != RECORDSIZE) - return NULL; /* An error has occurred */ + return NULL; /* An error has occurred */ current_tar_position += RECORDSIZE; return &rec_buf; } -static void tar_skip_n_records (struct vfs_s_super *archive, int tard, int n) +/* --------------------------------------------------------------------------------------------- */ + +static void +tar_skip_n_records (struct vfs_s_super *archive, int tard, int n) { (void) archive; @@ -302,9 +342,10 @@ static void tar_skip_n_records (struct vfs_s_super *archive, int tard, int n) current_tar_position += n * RECORDSIZE; } +/* --------------------------------------------------------------------------------------------- */ + static void -tar_fill_stat (struct vfs_s_super *archive, struct stat *st, union record *header, - size_t h_size) +tar_fill_stat (struct vfs_s_super *archive, struct stat *st, union record *header, size_t h_size) { st->st_mode = tar_from_oct (8, header->header.mode); @@ -313,74 +354,74 @@ tar_fill_stat (struct vfs_s_super *archive, struct stat *st, union record *heade * know about the other modes but I think I cause no new * problem when I adjust them, too. -- Norbert. */ - if (header->header.linkflag == LF_DIR) { - st->st_mode |= S_IFDIR; - } else if (header->header.linkflag == LF_SYMLINK) { - st->st_mode |= S_IFLNK; - } else if (header->header.linkflag == LF_CHR) { - st->st_mode |= S_IFCHR; - } else if (header->header.linkflag == LF_BLK) { - st->st_mode |= S_IFBLK; - } else if (header->header.linkflag == LF_FIFO) { - st->st_mode |= S_IFIFO; - } else - st->st_mode |= S_IFREG; + if (header->header.linkflag == LF_DIR) + { + st->st_mode |= S_IFDIR; + } + else if (header->header.linkflag == LF_SYMLINK) + { + st->st_mode |= S_IFLNK; + } + else if (header->header.linkflag == LF_CHR) + { + st->st_mode |= S_IFCHR; + } + else if (header->header.linkflag == LF_BLK) + { + st->st_mode |= S_IFBLK; + } + else if (header->header.linkflag == LF_FIFO) + { + st->st_mode |= S_IFIFO; + } + else + st->st_mode |= S_IFREG; st->st_rdev = 0; - switch (archive->u.arch.type) { + switch (archive->u.arch.type) + { case TAR_USTAR: case TAR_POSIX: case TAR_GNU: - st->st_uid = - *header->header.uname ? vfs_finduid (header->header. - uname) : tar_from_oct (8, - header-> - header. - uid); - st->st_gid = - *header->header.gname ? vfs_findgid (header->header. - gname) : tar_from_oct (8, - header-> - header. - gid); - switch (header->header.linkflag) { - case LF_BLK: - case LF_CHR: - st->st_rdev = - (tar_from_oct (8, header->header.devmajor) << 8) | - tar_from_oct (8, header->header.devminor); - } + st->st_uid = + *header->header.uname ? vfs_finduid (header->header.uname) : tar_from_oct (8, + header->header. + uid); + st->st_gid = + *header->header.gname ? vfs_findgid (header->header.gname) : tar_from_oct (8, + header->header. + gid); + switch (header->header.linkflag) + { + case LF_BLK: + case LF_CHR: + st->st_rdev = + (tar_from_oct (8, header->header.devmajor) << 8) | + tar_from_oct (8, header->header.devminor); + } default: - st->st_uid = tar_from_oct (8, header->header.uid); - st->st_gid = tar_from_oct (8, header->header.gid); + st->st_uid = tar_from_oct (8, header->header.uid); + st->st_gid = tar_from_oct (8, header->header.gid); } st->st_size = h_size; st->st_mtime = tar_from_oct (1 + 12, header->header.mtime); st->st_atime = 0; st->st_ctime = 0; - if (archive->u.arch.type == TAR_GNU) { - st->st_atime = tar_from_oct (1 + 12, - header->header.unused.oldgnu.atime); - st->st_ctime = tar_from_oct (1 + 12, - header->header.unused.oldgnu.ctime); + if (archive->u.arch.type == TAR_GNU) + { + st->st_atime = tar_from_oct (1 + 12, header->header.unused.oldgnu.atime); + st->st_ctime = tar_from_oct (1 + 12, header->header.unused.oldgnu.ctime); } } - -typedef enum { - STATUS_BADCHECKSUM, - STATUS_SUCCESS, - STATUS_EOFMARK, - STATUS_EOF -} ReadStatus; -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Return 1 for success, 0 if the checksum is bad, EOF on eof, * 2 for a record full of zeros (EOF marker). * */ static ReadStatus -tar_read_header (struct vfs_class *me, struct vfs_s_super *archive, - int tard, size_t *h_size) +tar_read_header (struct vfs_class *me, struct vfs_s_super *archive, int tard, size_t * h_size) { register int i; register long sum, signed_sum, recsum; @@ -392,26 +433,28 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive, header = tar_get_next_record (archive, tard); if (NULL == header) - return STATUS_EOF; + return STATUS_EOF; recsum = tar_from_oct (8, header->header.chksum); sum = 0; signed_sum = 0; p = header->charptr; - for (i = sizeof (*header); --i >= 0;) { - /* - * We can't use unsigned char here because of old compilers, - * e.g. V7. - */ - signed_sum += *p; - sum += 0xFF & *p++; + for (i = sizeof (*header); --i >= 0;) + { + /* + * We can't use unsigned char here because of old compilers, + * e.g. V7. + */ + signed_sum += *p; + sum += 0xFF & *p++; } /* Adjust checksum to count the "chksum" field as blanks. */ - for (i = sizeof (header->header.chksum); --i >= 0;) { - sum -= 0xFF & header->header.chksum[i]; - signed_sum -= (char) header->header.chksum[i]; + for (i = sizeof (header->header.chksum); --i >= 0;) + { + sum -= 0xFF & header->header.chksum[i]; + signed_sum -= (char) header->header.chksum[i]; } sum += ' ' * sizeof header->header.chksum; signed_sum += ' ' * sizeof header->header.chksum; @@ -421,240 +464,257 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive, * for the 8 blanks we faked for the checksum field. */ if (sum == 8 * ' ') - return STATUS_EOFMARK; + return STATUS_EOFMARK; if (sum != recsum && signed_sum != recsum) - return STATUS_BADCHECKSUM; + return STATUS_BADCHECKSUM; /* * Try to determine the archive format. */ - if (archive->u.arch.type == TAR_UNKNOWN) { - if (!strcmp (header->header.magic, TMAGIC)) { - if (header->header.linkflag == LF_GLOBAL_EXTHDR) - archive->u.arch.type = TAR_POSIX; - else - archive->u.arch.type = TAR_USTAR; - } else if (!strcmp (header->header.magic, OLDGNU_MAGIC)) { - archive->u.arch.type = TAR_GNU; - } + if (archive->u.arch.type == TAR_UNKNOWN) + { + if (!strcmp (header->header.magic, TMAGIC)) + { + if (header->header.linkflag == LF_GLOBAL_EXTHDR) + archive->u.arch.type = TAR_POSIX; + else + archive->u.arch.type = TAR_USTAR; + } + else if (!strcmp (header->header.magic, OLDGNU_MAGIC)) + { + archive->u.arch.type = TAR_GNU; + } } /* * linkflag on BSDI tar (pax) always '\000' */ - if (header->header.linkflag == '\000') { - if (header->header.arch_name[NAMSIZ - 1] != '\0') - i = NAMSIZ; - else - i = strlen (header->header.arch_name); + if (header->header.linkflag == '\000') + { + if (header->header.arch_name[NAMSIZ - 1] != '\0') + i = NAMSIZ; + else + i = strlen (header->header.arch_name); - if (i && header->header.arch_name[i - 1] == '/') - header->header.linkflag = LF_DIR; + if (i && header->header.arch_name[i - 1] == '/') + header->header.linkflag = LF_DIR; } /* * Good record. Decode file size and return. */ - if (header->header.linkflag == LF_LINK - || header->header.linkflag == LF_DIR) - *h_size = 0; /* Links 0 size on tape */ + if (header->header.linkflag == LF_LINK || header->header.linkflag == LF_DIR) + *h_size = 0; /* Links 0 size on tape */ else - *h_size = tar_from_oct (1 + 12, header->header.size); + *h_size = tar_from_oct (1 + 12, header->header.size); /* * Skip over directory snapshot info records that * are stored in incremental tar archives. */ - if (header->header.linkflag == LF_DUMPDIR) { - if (archive->u.arch.type == TAR_UNKNOWN) - archive->u.arch.type = TAR_GNU; - return STATUS_SUCCESS; + if (header->header.linkflag == LF_DUMPDIR) + { + if (archive->u.arch.type == TAR_UNKNOWN) + archive->u.arch.type = TAR_GNU; + return STATUS_SUCCESS; } /* * Skip over pax extended header and global extended * header records. */ - if (header->header.linkflag == LF_EXTHDR || - header->header.linkflag == LF_GLOBAL_EXTHDR) { - if (archive->u.arch.type == TAR_UNKNOWN) - archive->u.arch.type = TAR_POSIX; - return STATUS_SUCCESS; + if (header->header.linkflag == LF_EXTHDR || header->header.linkflag == LF_GLOBAL_EXTHDR) + { + if (archive->u.arch.type == TAR_UNKNOWN) + archive->u.arch.type = TAR_POSIX; + return STATUS_SUCCESS; } - if (header->header.linkflag == LF_LONGNAME - || header->header.linkflag == LF_LONGLINK) { - char **longp; - char *bp, *data; - int size, written; + if (header->header.linkflag == LF_LONGNAME || header->header.linkflag == LF_LONGLINK) + { + char **longp; + char *bp, *data; + int size, written; - if (archive->u.arch.type == TAR_UNKNOWN) - archive->u.arch.type = TAR_GNU; + if (archive->u.arch.type == TAR_UNKNOWN) + archive->u.arch.type = TAR_GNU; - if (*h_size > MC_MAXPATHLEN) { - message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); - return STATUS_BADCHECKSUM; - } + if (*h_size > MC_MAXPATHLEN) + { + message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); + return STATUS_BADCHECKSUM; + } - longp = ((header->header.linkflag == LF_LONGNAME) - ? &next_long_name : &next_long_link); + longp = ((header->header.linkflag == LF_LONGNAME) ? &next_long_name : &next_long_link); - g_free (*longp); - bp = *longp = g_malloc (*h_size + 1); + g_free (*longp); + bp = *longp = g_malloc (*h_size + 1); - for (size = *h_size; size > 0; size -= written) { - data = tar_get_next_record (archive, tard)->charptr; - if (data == NULL) { - g_free (*longp); - *longp = NULL; - message (D_ERROR, MSG_ERROR, - _("Unexpected EOF on archive file")); - return STATUS_BADCHECKSUM; - } - written = RECORDSIZE; - if (written > size) - written = size; + for (size = *h_size; size > 0; size -= written) + { + data = tar_get_next_record (archive, tard)->charptr; + if (data == NULL) + { + g_free (*longp); + *longp = NULL; + message (D_ERROR, MSG_ERROR, _("Unexpected EOF on archive file")); + return STATUS_BADCHECKSUM; + } + written = RECORDSIZE; + if (written > size) + written = size; - memcpy (bp, data, written); - bp += written; - } + memcpy (bp, data, written); + bp += written; + } - if (bp - *longp == MC_MAXPATHLEN && bp[-1] != '\0') { - g_free (*longp); - *longp = NULL; - message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); - return STATUS_BADCHECKSUM; - } - *bp = 0; - goto recurse; - } else { - struct stat st; - struct vfs_s_entry *entry; - struct vfs_s_inode *inode = NULL, *parent; - long data_position; - char *q; - int len; - char *current_file_name, *current_link_name; + if (bp - *longp == MC_MAXPATHLEN && bp[-1] != '\0') + { + g_free (*longp); + *longp = NULL; + message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); + return STATUS_BADCHECKSUM; + } + *bp = 0; + goto recurse; + } + else + { + struct stat st; + struct vfs_s_entry *entry; + struct vfs_s_inode *inode = NULL, *parent; + long data_position; + char *q; + int len; + char *current_file_name, *current_link_name; - current_link_name = - (next_long_link ? next_long_link : - g_strndup (header->header.arch_linkname, NAMSIZ)); - len = strlen (current_link_name); - if (len > 1 && current_link_name[len - 1] == '/') - current_link_name[len - 1] = 0; + current_link_name = + (next_long_link ? next_long_link : g_strndup (header->header.arch_linkname, NAMSIZ)); + len = strlen (current_link_name); + if (len > 1 && current_link_name[len - 1] == '/') + current_link_name[len - 1] = 0; - current_file_name = NULL; - switch (archive->u.arch.type) { - case TAR_USTAR: - case TAR_POSIX: - /* The ustar archive format supports pathnames of upto 256 - * characters in length. This is achieved by concatenating - * the contents of the `prefix' and `arch_name' fields like - * this: - * - * prefix + path_separator + arch_name - * - * If the `prefix' field contains an empty string i.e. its - * first characters is '\0' the prefix field is ignored. - */ - if (header->header.unused.prefix[0] != '\0') { - char *temp_name, *temp_prefix; + current_file_name = NULL; + switch (archive->u.arch.type) + { + case TAR_USTAR: + case TAR_POSIX: + /* The ustar archive format supports pathnames of upto 256 + * characters in length. This is achieved by concatenating + * the contents of the `prefix' and `arch_name' fields like + * this: + * + * prefix + path_separator + arch_name + * + * If the `prefix' field contains an empty string i.e. its + * first characters is '\0' the prefix field is ignored. + */ + if (header->header.unused.prefix[0] != '\0') + { + char *temp_name, *temp_prefix; - temp_name = g_strndup (header->header.arch_name, NAMSIZ); - temp_prefix = g_strndup (header->header.unused.prefix, - PREFIX_SIZE); - current_file_name = g_strconcat (temp_prefix, PATH_SEP_STR, - temp_name, (char *) NULL); - g_free (temp_name); - g_free (temp_prefix); - } - break; - case TAR_GNU: - if (next_long_name != NULL) - current_file_name = next_long_name; - break; - default: - break; - } + temp_name = g_strndup (header->header.arch_name, NAMSIZ); + temp_prefix = g_strndup (header->header.unused.prefix, PREFIX_SIZE); + current_file_name = g_strconcat (temp_prefix, PATH_SEP_STR, + temp_name, (char *) NULL); + g_free (temp_name); + g_free (temp_prefix); + } + break; + case TAR_GNU: + if (next_long_name != NULL) + current_file_name = next_long_name; + break; + default: + break; + } - if (current_file_name == NULL) - current_file_name = g_strndup (header->header.arch_name, NAMSIZ); + if (current_file_name == NULL) + current_file_name = g_strndup (header->header.arch_name, NAMSIZ); - canonicalize_pathname (current_file_name); - len = strlen (current_file_name); + canonicalize_pathname (current_file_name); + len = strlen (current_file_name); - data_position = current_tar_position; + data_position = current_tar_position; - p = strrchr (current_file_name, '/'); - if (p == NULL) { - p = current_file_name; - q = current_file_name + len; /* "" */ - } else { - *(p++) = 0; - q = current_file_name; - } + p = strrchr (current_file_name, '/'); + if (p == NULL) + { + p = current_file_name; + q = current_file_name + len; /* "" */ + } + else + { + *(p++) = 0; + q = current_file_name; + } - parent = - vfs_s_find_inode (me, archive, q, LINK_NO_FOLLOW, FL_MKDIR); - if (parent == NULL) { - message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); - return STATUS_BADCHECKSUM; - } + parent = vfs_s_find_inode (me, archive, q, LINK_NO_FOLLOW, FL_MKDIR); + if (parent == NULL) + { + message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); + return STATUS_BADCHECKSUM; + } - if (header->header.linkflag == LF_LINK) { - inode = - vfs_s_find_inode (me, archive, current_link_name, - LINK_NO_FOLLOW, 0); - if (inode == NULL) { - message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); - } else { - entry = vfs_s_new_entry (me, p, inode); - vfs_s_insert_entry (me, parent, entry); - g_free (current_link_name); - goto done; - } - } + if (header->header.linkflag == LF_LINK) + { + inode = vfs_s_find_inode (me, archive, current_link_name, LINK_NO_FOLLOW, 0); + if (inode == NULL) + { + message (D_ERROR, MSG_ERROR, _("Inconsistent tar archive")); + } + else + { + entry = vfs_s_new_entry (me, p, inode); + vfs_s_insert_entry (me, parent, entry); + g_free (current_link_name); + goto done; + } + } - tar_fill_stat (archive, &st, header, *h_size); - if (S_ISDIR(st.st_mode)) { + tar_fill_stat (archive, &st, header, *h_size); + if (S_ISDIR (st.st_mode)) + { entry = MEDATA->find_entry (me, parent, p, LINK_NO_FOLLOW, FL_NONE); if (entry) goto done; } - inode = vfs_s_new_inode (me, archive, &st); + inode = vfs_s_new_inode (me, archive, &st); - inode->data_offset = data_position; - if (*current_link_name) { - inode->linkname = current_link_name; - } else if (current_link_name != next_long_link) { - g_free (current_link_name); - } - entry = vfs_s_new_entry (me, p, inode); + inode->data_offset = data_position; + if (*current_link_name) + { + inode->linkname = current_link_name; + } + else if (current_link_name != next_long_link) + { + g_free (current_link_name); + } + entry = vfs_s_new_entry (me, p, inode); - vfs_s_insert_entry (me, parent, entry); - g_free (current_file_name); + vfs_s_insert_entry (me, parent, entry); + g_free (current_file_name); done: - next_long_link = next_long_name = NULL; + next_long_link = next_long_name = NULL; - if (archive->u.arch.type == TAR_GNU && - header->header.unused.oldgnu.isextended) { - while (tar_get_next_record (archive, tard)->ext_hdr. - isextended); - inode->data_offset = current_tar_position; - } - return STATUS_SUCCESS; + if (archive->u.arch.type == TAR_GNU && header->header.unused.oldgnu.isextended) + { + while (tar_get_next_record (archive, tard)->ext_hdr.isextended); + inode->data_offset = current_tar_position; + } + return STATUS_SUCCESS; } } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Main loop for reading an archive. * Returns 0 on success, -1 on error. */ static int -tar_open_archive (struct vfs_class *me, struct vfs_s_super *archive, - const char *name, char *op) +tar_open_archive (struct vfs_class *me, struct vfs_s_super *archive, const char *name, char *op) { /* Initial status at start of archive */ ReadStatus status = STATUS_EOFMARK; @@ -667,63 +727,63 @@ tar_open_archive (struct vfs_class *me, struct vfs_s_super *archive, /* Open for reading */ tard = tar_open_archive_int (me, name, archive); if (tard == -1) - return -1; + return -1; - for (;;) { - size_t h_size; + for (;;) + { + size_t h_size; - prev_status = status; - status = tar_read_header (me, archive, tard, &h_size); + prev_status = status; + status = tar_read_header (me, archive, tard, &h_size); - switch (status) { + switch (status) + { - case STATUS_SUCCESS: - tar_skip_n_records (archive, tard, - (h_size + RECORDSIZE - - 1) / RECORDSIZE); - continue; + case STATUS_SUCCESS: + tar_skip_n_records (archive, tard, (h_size + RECORDSIZE - 1) / RECORDSIZE); + continue; - /* - * Invalid header: - * - * If the previous header was good, tell them - * that we are skipping bad ones. - */ - case STATUS_BADCHECKSUM: - switch (prev_status) { + /* + * Invalid header: + * + * If the previous header was good, tell them + * that we are skipping bad ones. + */ + case STATUS_BADCHECKSUM: + switch (prev_status) + { - /* Error on first record */ - case STATUS_EOFMARK: - message (D_ERROR, MSG_ERROR, - _ - ("%s\ndoesn't look like a tar archive."), - name); - /* FALL THRU */ + /* Error on first record */ + case STATUS_EOFMARK: + message (D_ERROR, MSG_ERROR, _("%s\ndoesn't look like a tar archive."), name); + /* FALL THRU */ - /* Error after header rec */ - case STATUS_SUCCESS: - /* Error after error */ + /* Error after header rec */ + case STATUS_SUCCESS: + /* Error after error */ - case STATUS_BADCHECKSUM: - return -1; + case STATUS_BADCHECKSUM: + return -1; - case STATUS_EOF: - return 0; - } + case STATUS_EOF: + return 0; + } - /* Record of zeroes */ - case STATUS_EOFMARK: - status = prev_status; /* If error after 0's */ - /* FALL THRU */ + /* Record of zeroes */ + case STATUS_EOFMARK: + status = prev_status; /* If error after 0's */ + /* FALL THRU */ - case STATUS_EOF: /* End of archive */ - break; - } - break; + case STATUS_EOF: /* End of archive */ + break; + } + break; }; return 0; } +/* --------------------------------------------------------------------------------------------- */ + static void * tar_super_check (struct vfs_class *me, const char *archive_name, char *op) { @@ -733,45 +793,51 @@ tar_super_check (struct vfs_class *me, const char *archive_name, char *op) (void) op; if (mc_stat (archive_name, &stat_buf)) - return NULL; + return NULL; return &stat_buf; } +/* --------------------------------------------------------------------------------------------- */ + static int tar_super_same (struct vfs_class *me, struct vfs_s_super *parc, - const char *archive_name, char *op, void *cookie) + const char *archive_name, char *op, void *cookie) { - struct stat *archive_stat = cookie; /* stat of main archive */ + struct stat *archive_stat = cookie; /* stat of main archive */ (void) me; (void) op; if (strcmp (parc->name, archive_name)) - return 0; + return 0; /* Has the cached archive been changed on the disk? */ - if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) { - /* Yes, reload! */ - (*vfs_tarfs_ops.free) ((vfsid) parc); - vfs_rmstamp (&vfs_tarfs_ops, (vfsid) parc); - return 2; + if (parc->u.arch.st.st_mtime < archive_stat->st_mtime) + { + /* Yes, reload! */ + (*vfs_tarfs_ops.free) ((vfsid) parc); + vfs_rmstamp (&vfs_tarfs_ops, (vfsid) parc); + return 2; } /* Hasn't been modified, give it a new timeout */ vfs_stamp (&vfs_tarfs_ops, (vfsid) parc); return 1; } -static ssize_t tar_read (void *fh, char *buffer, size_t count) +/* --------------------------------------------------------------------------------------------- */ + +static ssize_t +tar_read (void *fh, char *buffer, size_t count) { off_t begin = FH->ino->data_offset; int fd = FH_SUPER->u.arch.fd; struct vfs_class *me = FH_SUPER->me; ssize_t res; - if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != - begin + FH->pos) ERRNOR (EIO, -1); + if (mc_lseek (fd, begin + FH->pos, SEEK_SET) != begin + FH->pos) + ERRNOR (EIO, -1); - count = MIN (count, (size_t)(FH->ino->st.st_size - FH->pos)); + count = MIN (count, (size_t) (FH->ino->st.st_size - FH->pos)); res = mc_read (fd, buffer, count); if (res == -1) @@ -781,15 +847,23 @@ static ssize_t tar_read (void *fh, char *buffer, size_t count) return res; } -static int tar_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) +/* --------------------------------------------------------------------------------------------- */ + +static int +tar_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode) { (void) fh; (void) mode; - if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, -1); + if ((flags & O_ACCMODE) != O_RDONLY) + ERRNOR (EROFS, -1); return 0; } +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + void init_tarfs (void) { @@ -809,3 +883,5 @@ init_tarfs (void) vfs_tarfs_ops.setctl = NULL; vfs_register_class (&vfs_tarfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/undelfs.c b/lib/vfs/mc-vfs/undelfs.c index 55232a70f..6f3bfd505 100644 --- a/lib/vfs/mc-vfs/undelfs.c +++ b/lib/vfs/mc-vfs/undelfs.c @@ -64,6 +64,17 @@ #include "utilvfs.h" #include "vfs-impl.h" +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + +/* To generate the . and .. entries use -2 */ +#define READDIR_PTR_INIT 0 + +#define undelfs_stat undelfs_lstat + +/*** file scope type declarations ****************************************************************/ + struct deleted_info { ext2_ino_t ino; @@ -84,6 +95,25 @@ struct lsdel_struct int bad_blocks; }; +typedef struct +{ + int f_index; /* file index into delarray */ + char *buf; + int error_code; /* */ + off_t pos; /* file position */ + off_t current; /* used to determine current position in itereate */ + gboolean finished; + ext2_ino_t inode; + int bytes_read; + off_t size; + + /* Used by undelfs_read: */ + char *dest_buffer; /* destination buffer */ + size_t count; /* bytes to read */ +} undelfs_file; + +/*** file scope variables ************************************************************************/ + /* We only allow one opened ext2fs */ static char *ext2_fname; static ext2_filsys fs = NULL; @@ -96,8 +126,8 @@ static int readdir_ptr; static int undelfs_usage; static struct vfs_class vfs_undelfs_ops; -/* To generate the . and .. entries use -2 */ -#define READDIR_PTR_INIT 0 +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ static void undelfs_shutdown (void) @@ -113,6 +143,8 @@ undelfs_shutdown (void) block_buf = NULL; } +/* --------------------------------------------------------------------------------------------- */ + static void undelfs_get_path (const char *dirname, char **fsname, char **file) { @@ -137,7 +169,7 @@ undelfs_get_path (const char *dirname, char **fsname, char **file) p = dirname + strlen (dirname); #if 0 - /* Strip trailing ./ + /* Strip trailing ./ */ if (p - dirname > 2 && *(p - 1) == '/' && *(p - 2) == '.') *(p = p - 2) = 0; @@ -162,6 +194,8 @@ undelfs_get_path (const char *dirname, char **fsname, char **file) return; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *private) { @@ -181,10 +215,12 @@ undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *priva return 0; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Load information about deleted files. * Don't abort if there is not enough memory - load as much as we can. */ + static int undelfs_loaddel (void) { @@ -291,24 +327,7 @@ undelfs_loaddel (void) return 0; } - -/* - * This function overrides com_err() from libcom_err library. - * It is used in libext2fs to report errors. - */ -void -com_err (const char *whoami, long err_code, const char *fmt, ...) -{ - va_list ap; - char *str; - - va_start (ap, fmt); - str = g_strdup_vprintf (fmt, ap); - va_end (ap); - - message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code); - g_free (str); -} +/* --------------------------------------------------------------------------------------------- */ static void * undelfs_opendir (struct vfs_class *me, const char *dirname) @@ -364,6 +383,7 @@ undelfs_opendir (struct vfs_class *me, const char *dirname) return 0; } +/* --------------------------------------------------------------------------------------------- */ static void * undelfs_readdir (void *vfs_info) @@ -390,6 +410,8 @@ undelfs_readdir (void *vfs_info) return &undelfs_readdir_data; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_closedir (void *vfs_info) { @@ -397,24 +419,9 @@ undelfs_closedir (void *vfs_info) return 0; } -typedef struct -{ - int f_index; /* file index into delarray */ - char *buf; - int error_code; /* */ - off_t pos; /* file position */ - off_t current; /* used to determine current position in itereate */ - gboolean finished; - ext2_ino_t inode; - int bytes_read; - off_t size; - - /* Used by undelfs_read: */ - char *dest_buffer; /* destination buffer */ - size_t count; /* bytes to read */ -} undelfs_file; - +/* --------------------------------------------------------------------------------------------- */ /* We do not support lseek */ + static void * undelfs_open (struct vfs_class *me, const char *fname, int flags, mode_t mode) { @@ -474,6 +481,8 @@ undelfs_open (struct vfs_class *me, const char *fname, int flags, mode_t mode) return p; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_close (void *vfs_info) { @@ -484,6 +493,8 @@ undelfs_close (void *vfs_info) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private) { @@ -554,6 +565,8 @@ undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *pr return 0; } +/* --------------------------------------------------------------------------------------------- */ + static ssize_t undelfs_read (void *vfs_info, char *buffer, size_t count) { @@ -581,6 +594,8 @@ undelfs_read (void *vfs_info, char *buffer, size_t count) return p->dest_buffer - buffer; } +/* --------------------------------------------------------------------------------------------- */ + static long undelfs_getindex (char *path) { @@ -595,6 +610,8 @@ undelfs_getindex (char *path) return -1; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_stat_int (int inode_index, struct stat *buf) { @@ -611,6 +628,8 @@ undelfs_stat_int (int inode_index, struct stat *buf) return 0; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) { @@ -652,7 +671,7 @@ undelfs_lstat (struct vfs_class *me, const char *path, struct stat *buf) return undelfs_stat_int (inode_index, buf); } -#define undelfs_stat undelfs_lstat +/* --------------------------------------------------------------------------------------------- */ static int undelfs_fstat (void *vfs_info, struct stat *buf) @@ -662,6 +681,8 @@ undelfs_fstat (void *vfs_info, struct stat *buf) return undelfs_stat_int (p->f_index, buf); } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_chdir (struct vfs_class *me, const char *path) { @@ -690,6 +711,8 @@ undelfs_chdir (struct vfs_class *me, const char *path) return 0; } +/* --------------------------------------------------------------------------------------------- */ + /* this has to stay here for now: vfs layer does not know how to emulate it */ static off_t undelfs_lseek (void *vfs_info, off_t offset, int whence) @@ -701,6 +724,8 @@ undelfs_lseek (void *vfs_info, off_t offset, int whence) return -1; } +/* --------------------------------------------------------------------------------------------- */ + static vfsid undelfs_getid (struct vfs_class *me, const char *path) { @@ -716,6 +741,8 @@ undelfs_getid (struct vfs_class *me, const char *path) return (vfsid) fs; } +/* --------------------------------------------------------------------------------------------- */ + static int undelfs_nothingisopen (vfsid id) { @@ -724,6 +751,8 @@ undelfs_nothingisopen (vfsid id) return !undelfs_usage; } +/* --------------------------------------------------------------------------------------------- */ + static void undelfs_free (vfsid id) { @@ -732,7 +761,9 @@ undelfs_free (vfsid id) undelfs_shutdown (); } -#ifdef ENABLE_NLS +/* --------------------------------------------------------------------------------------------- */ + +#ifdef ENABLE_NLS static int undelfs_init (struct vfs_class *me) { @@ -742,9 +773,33 @@ undelfs_init (struct vfs_class *me) return 1; } #else -#define undelfs_init NULL +#define undelfs_init NULL #endif +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/** + * This function overrides com_err() from libcom_err library. + * It is used in libext2fs to report errors. + */ + +void +com_err (const char *whoami, long err_code, const char *fmt, ...) +{ + va_list ap; + char *str; + + va_start (ap, fmt); + str = g_strdup_vprintf (fmt, ap); + va_end (ap); + + message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code); + g_free (str); +} + +/* --------------------------------------------------------------------------------------------- */ + void init_undelfs (void) { @@ -767,3 +822,5 @@ init_undelfs (void) vfs_undelfs_ops.free = undelfs_free; vfs_register_class (&vfs_undelfs_ops); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/utilvfs.c b/lib/vfs/mc-vfs/utilvfs.c index 9c70bee24..823af2f44 100644 --- a/lib/vfs/mc-vfs/utilvfs.c +++ b/lib/vfs/mc-vfs/utilvfs.c @@ -3,12 +3,12 @@ Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. Copyright (C) 1995, 1996 Miguel de Icaza - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -38,36 +38,15 @@ #include "lib/global.h" #include "lib/unixcompat.h" -#include "src/wtools.h" /* message() */ +#include "src/wtools.h" /* message() */ #include "src/history.h" #include "vfs-impl.h" #include "utilvfs.h" -/** Get current username - * - * @return g_malloc()ed string with the name of the currently logged in - * user ("anonymous" if uid is not registered in the system) - */ -char * -vfs_get_local_username(void) -{ - struct passwd * p_i; +/*** global variables ****************************************************************************/ - p_i = getpwuid (geteuid ()); - - return (p_i && p_i->pw_name) - ? g_strdup (p_i->pw_name) - : g_strdup ("anonymous"); /* Unknown UID, strange */ -} - -/* - * Look up a user or group name from a uid/gid, maintaining a cache. - * FIXME, for now it's a one-entry cache. - * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup. - * This file should be modified for non-unix systems to do something - * reasonable. - */ +/*** file scope macro definitions ****************************************************************/ #ifndef TUNMLEN #define TUNMLEN 256 @@ -76,8 +55,211 @@ vfs_get_local_username(void) #define TGNMLEN 256 #endif -#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid ) -#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid ) +#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid ) +#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid ) + +/* Parsing code is used by ftpfs, fish and extfs */ +#define MAXCOLS 30 + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +static char *columns[MAXCOLS]; /* Points to the string in column n */ +static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */ + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +static int +is_num (int idx) +{ + char *column = columns[idx]; + + if (!column || column[0] < '0' || column[0] > '9') + return 0; + + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ +/* Return 1 for MM-DD-YY and MM-DD-YYYY */ + +static int +is_dos_date (const char *str) +{ + int len; + + if (!str) + return 0; + + len = strlen (str); + if (len != 8 && len != 10) + return 0; + + if (str[2] != str[5]) + return 0; + + if (!strchr ("\\-/", (int) str[2])) + return 0; + + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +is_week (const char *str, struct tm *tim) +{ + static const char *week = "SunMonTueWedThuFriSat"; + const char *pos; + + if (!str) + return 0; + + pos = strstr (week, str); + if (pos != NULL) + { + if (tim != NULL) + tim->tm_wday = (pos - week) / 3; + return 1; + } + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +is_month (const char *str, struct tm *tim) +{ + static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec"; + const char *pos; + + if (!str) + return 0; + + pos = strstr (month, str); + if (pos != NULL) + { + if (tim != NULL) + tim->tm_mon = (pos - month) / 3; + return 1; + } + return 0; +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Check for possible locale's abbreviated month name (Jan..Dec). + * Any 3 bytes long string without digit, control and punctuation characters. + * isalpha() is locale specific, so it cannot be used if current + * locale is "C" and ftp server use Cyrillic. + * NB: It is assumed there are no whitespaces in month. + */ +static int +is_localized_month (const char *month) +{ + int i = 0; + + if (!month) + return 0; + + while ((i < 3) && *month && !isdigit ((unsigned char) *month) + && !iscntrl ((unsigned char) *month) && !ispunct ((unsigned char) *month)) + { + i++; + month++; + } + return ((i == 3) && (*month == 0)); +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +is_time (const char *str, struct tm *tim) +{ + const char *p, *p2; + + if (str == NULL) + return 0; + + p = strchr (str, ':'); + p2 = strrchr (str, ':'); + if (p != NULL && p2 != NULL) + { + if (p != p2) + { + if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3) + return 0; + } + else + { + if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2) + return 0; + } + } + else + return 0; + + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +is_year (char *str, struct tm *tim) +{ + long year; + + if (!str) + return 0; + + if (strchr (str, ':')) + return 0; + + if (strlen (str) != 4) + return 0; + + if (sscanf (str, "%ld", &year) != 1) + return 0; + + if (year < 1900 || year > 3000) + return 0; + + tim->tm_year = (int) (year - 1900); + + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/** Get current username + * + * @return g_malloc()ed string with the name of the currently logged in + * user ("anonymous" if uid is not registered in the system) + */ + +char * +vfs_get_local_username (void) +{ + struct passwd *p_i; + + p_i = getpwuid (geteuid ()); + + return (p_i && p_i->pw_name) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous"); /* Unknown UID, strange */ +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Look up a user or group name from a uid/gid, maintaining a cache. + * FIXME, for now it's a one-entry cache. + * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup. + * This file should be modified for non-unix systems to do something + * reasonable. + */ + +/* --------------------------------------------------------------------------------------------- */ int vfs_finduid (const char *uname) @@ -88,19 +270,25 @@ vfs_finduid (const char *uname) struct passwd *pw; - if (uname[0] != saveuname[0] /* Quick test w/o proc call */ - ||0 != strncmp (uname, saveuname, TUNMLEN)) { - g_strlcpy (saveuname, uname, TUNMLEN); - pw = getpwnam (uname); - if (pw) { - saveuid = pw->pw_uid; - } else { - saveuid = myuid; - } + if (uname[0] != saveuname[0] /* Quick test w/o proc call */ + || 0 != strncmp (uname, saveuname, TUNMLEN)) + { + g_strlcpy (saveuname, uname, TUNMLEN); + pw = getpwnam (uname); + if (pw) + { + saveuid = pw->pw_uid; + } + else + { + saveuid = myuid; + } } return saveuid; } +/* --------------------------------------------------------------------------------------------- */ + int vfs_findgid (const char *gname) { @@ -110,25 +298,31 @@ vfs_findgid (const char *gname) struct group *gr; - if (gname[0] != savegname[0] /* Quick test w/o proc call */ - ||0 != strncmp (gname, savegname, TUNMLEN)) { - g_strlcpy (savegname, gname, TUNMLEN); - gr = getgrnam (gname); - if (gr) { - savegid = gr->gr_gid; - } else { - savegid = mygid; - } + if (gname[0] != savegname[0] /* Quick test w/o proc call */ + || 0 != strncmp (gname, savegname, TUNMLEN)) + { + g_strlcpy (savegname, gname, TUNMLEN); + gr = getgrnam (gname); + if (gr) + { + savegid = gr->gr_gid; + } + else + { + savegid = mygid; + } } return savegid; } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Create a temporary file with a name resembling the original. * This is needed e.g. for local copies requested by extfs. * Some extfs scripts may look at the extension. * We also protect stupid scripts agains dangerous names. */ + int vfs_mkstemps (char **pname, const char *prefix, const char *param_basename) { @@ -140,23 +334,24 @@ vfs_mkstemps (char **pname, const char *prefix, const char *param_basename) /* Strip directories */ p = strrchr (param_basename, PATH_SEP); if (!p) - p = param_basename; + p = param_basename; else - p++; + p++; /* Protection against very long names */ shift = strlen (p) - (MC_MAXPATHLEN - 16); if (shift > 0) - p += shift; + p += shift; suffix = g_malloc (MC_MAXPATHLEN); /* Protection against unusual characters */ q = suffix; - while (*p && (*p != '#')) { - if (strchr (".-_@", *p) || isalnum ((unsigned char) *p)) - *q++ = *p; - p++; + while (*p && (*p != '#')) + { + if (strchr (".-_@", *p) || isalnum ((unsigned char) *p)) + *q++ = *p; + p++; } *q = 0; @@ -165,6 +360,7 @@ vfs_mkstemps (char **pname, const char *prefix, const char *param_basename) return fd; } +/* --------------------------------------------------------------------------------------------- */ /** Extract the hostname and username from the path * * Format of the path is [user@]hostname:port/remote-dir, e.g.: @@ -190,106 +386,120 @@ vfs_mkstemps (char **pname, const char *prefix, const char *param_basename) * Return value is a g_malloc()ed string with the pathname relative to the * host. */ + char * vfs_split_url (const char *path, char **host, char **user, int *port, - char **pass, int default_port, enum VFS_URL_FLAGS flags) + char **pass, int default_port, enum VFS_URL_FLAGS flags) { char *dir, *colon, *inner_colon, *at, *rest; char *retval; - char * const pcopy = g_strdup (path); + char *const pcopy = g_strdup (path); const char *pend = pcopy + strlen (pcopy); if (pass) - *pass = NULL; + *pass = NULL; *port = default_port; *user = NULL; retval = NULL; dir = pcopy; - if (!(flags & URL_NOSLASH)) { - /* locate path component */ - while (*dir != PATH_SEP && *dir) - dir++; - if (*dir) { - retval = g_strdup (dir); - *dir = 0; - } else - retval = g_strdup (PATH_SEP_STR); + if (!(flags & URL_NOSLASH)) + { + /* locate path component */ + while (*dir != PATH_SEP && *dir) + dir++; + if (*dir) + { + retval = g_strdup (dir); + *dir = 0; + } + else + retval = g_strdup (PATH_SEP_STR); } /* search for any possible user */ at = strrchr (pcopy, '@'); /* We have a username */ - if (at) { - *at = 0; - inner_colon = strchr (pcopy, ':'); - if (inner_colon) { - *inner_colon = 0; - inner_colon++; - if (pass) - *pass = g_strdup (inner_colon); - } - if (*pcopy != 0) - *user = g_strdup (pcopy); + if (at) + { + *at = 0; + inner_colon = strchr (pcopy, ':'); + if (inner_colon) + { + *inner_colon = 0; + inner_colon++; + if (pass) + *pass = g_strdup (inner_colon); + } + if (*pcopy != 0) + *user = g_strdup (pcopy); - if (pend == at + 1) - rest = at; - else - rest = at + 1; - } else - rest = pcopy; + if (pend == at + 1) + rest = at; + else + rest = at + 1; + } + else + rest = pcopy; if (!*user && !(flags & URL_USE_ANONYMOUS)) - *user = vfs_get_local_username(); + *user = vfs_get_local_username (); /* Check if the host comes with a port spec, if so, chop it */ - if ('[' == *rest) { - colon = strchr (++rest, ']'); - if (colon) { - colon[0] = '\0'; - colon[1] = '\0'; - colon++; - } else { - g_free (pcopy); - g_free (retval); - *host = NULL; - *port = 0; - return NULL; - } - } else - colon = strchr (rest, ':'); + if ('[' == *rest) + { + colon = strchr (++rest, ']'); + if (colon) + { + colon[0] = '\0'; + colon[1] = '\0'; + colon++; + } + else + { + g_free (pcopy); + g_free (retval); + *host = NULL; + *port = 0; + return NULL; + } + } + else + colon = strchr (rest, ':'); - if (colon) { - *colon = 0; - if (sscanf (colon + 1, "%d", port) == 1) { - if (*port <= 0 || *port >= 65536) - *port = default_port; - } else { - while (*(++colon)) { - switch (*colon) { - case 'C': - *port = 1; - break; - case 'r': - *port = 2; - break; - } - } - } + if (colon) + { + *colon = 0; + if (sscanf (colon + 1, "%d", port) == 1) + { + if (*port <= 0 || *port >= 65536) + *port = default_port; + } + else + { + while (*(++colon)) + { + switch (*colon) + { + case 'C': + *port = 1; + break; + case 'r': + *port = 2; + break; + } + } + } } if (host) - *host = g_strdup (rest); + *host = g_strdup (rest); g_free (pcopy); return retval; } -/* Parsing code is used by ftpfs, fish and extfs */ -#define MAXCOLS 30 - -static char *columns[MAXCOLS]; /* Points to the string in column n */ -static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */ +/* --------------------------------------------------------------------------------------------- */ int vfs_split_text (char *p) @@ -299,193 +509,79 @@ vfs_split_text (char *p) memset (columns, 0, sizeof (columns)); - for (numcols = 0; *p && numcols < MAXCOLS; numcols++) { - while (*p == ' ' || *p == '\r' || *p == '\n') { - *p = 0; - p++; - } - columns[numcols] = p; - column_ptr[numcols] = p - original; - while (*p && *p != ' ' && *p != '\r' && *p != '\n') - p++; + for (numcols = 0; *p && numcols < MAXCOLS; numcols++) + { + while (*p == ' ' || *p == '\r' || *p == '\n') + { + *p = 0; + p++; + } + columns[numcols] = p; + column_ptr[numcols] = p - original; + while (*p && *p != ' ' && *p != '\r' && *p != '\n') + p++; } return numcols; } -static int -is_num (int idx) -{ - char *column = columns[idx]; - - if (!column || column[0] < '0' || column[0] > '9') - return 0; - - return 1; -} - -/* Return 1 for MM-DD-YY and MM-DD-YYYY */ -static int -is_dos_date (const char *str) -{ - int len; - - if (!str) - return 0; - - len = strlen (str); - if (len != 8 && len != 10) - return 0; - - if (str[2] != str[5]) - return 0; - - if (!strchr ("\\-/", (int) str[2])) - return 0; - - return 1; -} - -static int -is_week (const char *str, struct tm *tim) -{ - static const char *week = "SunMonTueWedThuFriSat"; - const char *pos; - - if (!str) - return 0; - - pos = strstr (week, str); - if (pos != NULL) { - if (tim != NULL) - tim->tm_wday = (pos - week) / 3; - return 1; - } - return 0; -} - -static int -is_month (const char *str, struct tm *tim) -{ - static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec"; - const char *pos; - - if (!str) - return 0; - - pos = strstr (month, str); - if (pos != NULL) { - if (tim != NULL) - tim->tm_mon = (pos - month) / 3; - return 1; - } - return 0; -} - -/* - * Check for possible locale's abbreviated month name (Jan..Dec). - * Any 3 bytes long string without digit, control and punctuation characters. - * isalpha() is locale specific, so it cannot be used if current - * locale is "C" and ftp server use Cyrillic. - * NB: It is assumed there are no whitespaces in month. - */ -static int -is_localized_month (const char *month) -{ - int i = 0; - - if (!month) - return 0; - - while ((i < 3) && *month && !isdigit ((unsigned char) *month) - && !iscntrl ((unsigned char) *month) - && !ispunct ((unsigned char) *month)) { - i++; - month++; - } - return ((i == 3) && (*month == 0)); -} - -static int -is_time (const char *str, struct tm *tim) -{ - const char *p, *p2; - - if (str == NULL) - return 0; - - p = strchr (str, ':'); - p2 = strrchr (str, ':'); - if (p != NULL && p2 != NULL) { - if (p != p2) { - if (sscanf - (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, - &tim->tm_sec) != 3) - return 0; - } else { - if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2) - return 0; - } - } else - return 0; - - return 1; -} - -static int -is_year (char *str, struct tm *tim) -{ - long year; - - if (!str) - return 0; - - if (strchr (str, ':')) - return 0; - - if (strlen (str) != 4) - return 0; - - if (sscanf (str, "%ld", &year) != 1) - return 0; - - if (year < 1900 || year > 3000) - return 0; - - tim->tm_year = (int) (year - 1900); - - return 1; -} +/* --------------------------------------------------------------------------------------------- */ gboolean -vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type) +vfs_parse_filetype (const char *s, size_t * ret_skipped, mode_t * ret_type) { mode_t type; - switch (*s) { - case 'd': type = S_IFDIR; break; - case 'b': type = S_IFBLK; break; - case 'c': type = S_IFCHR; break; - case 'l': type = S_IFLNK; break; + switch (*s) + { + case 'd': + type = S_IFDIR; + break; + case 'b': + type = S_IFBLK; + break; + case 'c': + type = S_IFCHR; + break; + case 'l': + type = S_IFLNK; + break; #ifdef S_IFSOCK - case 's': type = S_IFSOCK; break; + case 's': + type = S_IFSOCK; + break; #else - case 's': type = S_IFIFO; break; + case 's': + type = S_IFIFO; + break; #endif -#ifdef S_IFDOOR /* Solaris door */ - case 'D': type = S_IFDOOR; break; +#ifdef S_IFDOOR /* Solaris door */ + case 'D': + type = S_IFDOOR; + break; #else - case 'D': type = S_IFIFO; break; + case 'D': + type = S_IFIFO; + break; #endif - case 'p': type = S_IFIFO; break; -#ifdef S_IFNAM /* Special named files */ - case 'n': type = S_IFNAM; break; + case 'p': + type = S_IFIFO; + break; +#ifdef S_IFNAM /* Special named files */ + case 'n': + type = S_IFNAM; + break; #else - case 'n': type = S_IFREG; break; + case 'n': + type = S_IFREG; + break; #endif - case 'm': /* Don't know what these are :-) */ - case '-': - case '?': type = S_IFREG; break; - default: return FALSE; + case 'm': /* Don't know what these are :-) */ + case '-': + case '?': + type = S_IFREG; + break; + default: + return FALSE; } *ret_type = type; @@ -493,8 +589,10 @@ vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type) return TRUE; } +/* --------------------------------------------------------------------------------------------- */ + gboolean -vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms) +vfs_parse_fileperms (const char *s, size_t * ret_skipped, mode_t * ret_perms) { const char *p; mode_t perms; @@ -502,60 +600,120 @@ vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms) p = s; perms = 0; - switch (*p++) { - case '-': break; - case 'r': perms |= S_IRUSR; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'r': + perms |= S_IRUSR; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'w': perms |= S_IWUSR; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'w': + perms |= S_IWUSR; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'S': perms |= S_ISUID; break; - case 's': perms |= S_IXUSR | S_ISUID; break; - case 'x': perms |= S_IXUSR; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'S': + perms |= S_ISUID; + break; + case 's': + perms |= S_IXUSR | S_ISUID; + break; + case 'x': + perms |= S_IXUSR; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'r': perms |= S_IRGRP; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'r': + perms |= S_IRGRP; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'w': perms |= S_IWGRP; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'w': + perms |= S_IWGRP; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'S': perms |= S_ISGID; break; - case 'l': perms |= S_ISGID; break; /* found on Solaris */ - case 's': perms |= S_IXGRP | S_ISGID; break; - case 'x': perms |= S_IXGRP; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'S': + perms |= S_ISGID; + break; + case 'l': + perms |= S_ISGID; + break; /* found on Solaris */ + case 's': + perms |= S_IXGRP | S_ISGID; + break; + case 'x': + perms |= S_IXGRP; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'r': perms |= S_IROTH; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'r': + perms |= S_IROTH; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'w': perms |= S_IWOTH; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'w': + perms |= S_IWOTH; + break; + default: + return FALSE; } - switch (*p++) { - case '-': break; - case 'T': perms |= S_ISVTX; break; - case 't': perms |= S_IXOTH | S_ISVTX; break; - case 'x': perms |= S_IXOTH; break; - default: return FALSE; + switch (*p++) + { + case '-': + break; + case 'T': + perms |= S_ISVTX; + break; + case 't': + perms |= S_IXOTH | S_ISVTX; + break; + case 'x': + perms |= S_IXOTH; + break; + default: + return FALSE; } - if (*p == '+') { /* ACLs on Solaris, HP-UX and others */ - p++; + if (*p == '+') + { /* ACLs on Solaris, HP-UX and others */ + p++; } *ret_skipped = p - s; @@ -563,9 +721,10 @@ vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms) return TRUE; } +/* --------------------------------------------------------------------------------------------- */ + gboolean -vfs_parse_filemode (const char *s, size_t *ret_skipped, - mode_t *ret_mode) +vfs_parse_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode) { const char *p; mode_t type, perms; @@ -574,11 +733,11 @@ vfs_parse_filemode (const char *s, size_t *ret_skipped, p = s; if (!vfs_parse_filetype (p, &skipped, &type)) - return FALSE; + return FALSE; p += skipped; if (!vfs_parse_fileperms (p, &skipped, &perms)) - return FALSE; + return FALSE; p += skipped; *ret_skipped = p - s; @@ -586,9 +745,10 @@ vfs_parse_filemode (const char *s, size_t *ret_skipped, return TRUE; } +/* --------------------------------------------------------------------------------------------- */ + gboolean -vfs_parse_raw_filemode (const char *s, size_t *ret_skipped, - mode_t *ret_mode) +vfs_parse_raw_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode) { const char *p; mode_t remote_type = 0, local_type, perms = 0; @@ -596,47 +756,52 @@ vfs_parse_raw_filemode (const char *s, size_t *ret_skipped, p = s; /* isoctal */ - while(*p >= '0' && *p <= '7') + while (*p >= '0' && *p <= '7') { - perms *= 010; - perms += (*p - '0'); - ++p; + perms *= 010; + perms += (*p - '0'); + ++p; } if (*p++ != ' ') - return FALSE; + return FALSE; - while(*p >= '0' && *p <= '7') + while (*p >= '0' && *p <= '7') { - remote_type *= 010; - remote_type += (*p - '0'); - ++p; + remote_type *= 010; + remote_type += (*p - '0'); + ++p; } if (*p++ != ' ') - return FALSE; + return FALSE; /* generated with: - $ perl -e 'use Fcntl ":mode"; - my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG); - foreach $t (@modes) { printf ("%o\n", $t); };' - TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os) - (see vfs_parse_filetype) + $ perl -e 'use Fcntl ":mode"; + my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG); + foreach $t (@modes) { printf ("%o\n", $t); };' + TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os) + (see vfs_parse_filetype) */ switch (remote_type) { - case 020000: - local_type = S_IFCHR; break; - case 040000: - local_type = S_IFDIR; break; - case 060000: - local_type = S_IFBLK; break; - case 0120000: - local_type = S_IFLNK; break; - case 0100000: - default: /* don't know what is it */ - local_type = S_IFREG; break; + case 020000: + local_type = S_IFCHR; + break; + case 040000: + local_type = S_IFDIR; + break; + case 060000: + local_type = S_IFBLK; + break; + case 0120000: + local_type = S_IFLNK; + break; + case 0100000: + default: /* don't know what is it */ + local_type = S_IFREG; + break; } *ret_skipped = p - s; @@ -644,15 +809,17 @@ vfs_parse_raw_filemode (const char *s, size_t *ret_skipped, return TRUE; } -/* This function parses from idx in the columns[] array */ +/* --------------------------------------------------------------------------------------------- */ +/** This function parses from idx in the columns[] array */ + int -vfs_parse_filedate (int idx, time_t *t) +vfs_parse_filedate (int idx, time_t * t) { char *p; struct tm tim; int d[3]; int got_year = 0; - int l10n = 0; /* Locale's abbreviated month name */ + int l10n = 0; /* Locale's abbreviated month name */ time_t current_time; struct tm *local_time; @@ -666,71 +833,81 @@ vfs_parse_filedate (int idx, time_t *t) tim.tm_hour = 0; tim.tm_min = 0; tim.tm_sec = 0; - tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */ + tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */ p = columns[idx++]; /* We eat weekday name in case of extfs */ if (is_week (p, &tim)) - p = columns[idx++]; + p = columns[idx++]; /* Month name */ - if (is_month (p, &tim)) { - /* And we expect, it followed by day number */ - if (is_num (idx)) - tim.tm_mday = (int) atol (columns[idx++]); - else - return 0; /* No day */ + if (is_month (p, &tim)) + { + /* And we expect, it followed by day number */ + if (is_num (idx)) + tim.tm_mday = (int) atol (columns[idx++]); + else + return 0; /* No day */ - } else { - /* We expect: - 3 fields max or we'll see oddities with certain file names. - So both year and time is not allowed. - Mon DD hh:mm[:ss] - Mon DD YYYY - But in case of extfs we allow these date formats: - MM-DD-YY hh:mm[:ss] - where Mon is Jan-Dec, DD, MM, YY two digit day, month, year, - YYYY four digit year, hh, mm, ss two digit hour, minute or second. */ + } + else + { + /* We expect: + 3 fields max or we'll see oddities with certain file names. + So both year and time is not allowed. + Mon DD hh:mm[:ss] + Mon DD YYYY + But in case of extfs we allow these date formats: + MM-DD-YY hh:mm[:ss] + where Mon is Jan-Dec, DD, MM, YY two digit day, month, year, + YYYY four digit year, hh, mm, ss two digit hour, minute or second. */ - /* Special case with MM-DD-YY or MM-DD-YYYY */ - if (is_dos_date (p)) { - p[2] = p[5] = '-'; + /* Special case with MM-DD-YY or MM-DD-YYYY */ + if (is_dos_date (p)) + { + p[2] = p[5] = '-'; - if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) { - /* Months are zero based */ - if (d[0] > 0) - d[0]--; + if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) + { + /* Months are zero based */ + if (d[0] > 0) + d[0]--; - if (d[2] > 1900) { - d[2] -= 1900; - } else { - /* Y2K madness */ - if (d[2] < 70) - d[2] += 100; - } + if (d[2] > 1900) + { + d[2] -= 1900; + } + else + { + /* Y2K madness */ + if (d[2] < 70) + d[2] += 100; + } - tim.tm_mon = d[0]; - tim.tm_mday = d[1]; - tim.tm_year = d[2]; - got_year = 1; - } else - return 0; /* sscanf failed */ - } else { - /* Locale's abbreviated month name followed by day number */ - if (is_localized_month (p) && (is_num (idx++))) - l10n = 1; - else - return 0; /* unsupported format */ - } + tim.tm_mon = d[0]; + tim.tm_mday = d[1]; + tim.tm_year = d[2]; + got_year = 1; + } + else + return 0; /* sscanf failed */ + } + else + { + /* Locale's abbreviated month name followed by day number */ + if (is_localized_month (p) && (is_num (idx++))) + l10n = 1; + else + return 0; /* unsupported format */ + } } /* Here we expect to find time or year */ - if (is_num (idx) && (is_time (columns[idx], &tim) - || (got_year = is_year (columns[idx], &tim)))) - idx++; + if (is_num (idx) && (is_time (columns[idx], &tim) || (got_year = is_year (columns[idx], &tim)))) + idx++; else - return 0; /* Neither time nor date */ + return 0; /* Neither time nor date */ /* * If the date is less than 6 months in the past, it is shown without year @@ -739,20 +916,20 @@ vfs_parse_filedate (int idx, time_t *t) * to represent them at all */ if (!got_year && local_time->tm_mon < 6 - && local_time->tm_mon < tim.tm_mon - && tim.tm_mon - local_time->tm_mon >= 6) + && local_time->tm_mon < tim.tm_mon && tim.tm_mon - local_time->tm_mon >= 6) - tim.tm_year--; + tim.tm_year--; *t = mktime (&tim); if (l10n || (*t < 0)) - *t = 0; + *t = 0; return idx; } +/* --------------------------------------------------------------------------------------------- */ + int -vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, - char **linkname) +vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, char **linkname) { int idx, idx2, num_cols; int i; @@ -762,33 +939,34 @@ vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, size_t skipped; if (strncmp (p, "total", 5) == 0) - return 0; + return 0; if (!vfs_parse_filetype (p, &skipped, &s->st_mode)) - goto error; + goto error; p += skipped; - if (*p == ' ') /* Notwell 4 */ - p++; - if (*p == '[') { - if (strlen (p) <= 8 || p[8] != ']') - goto error; - /* Should parse here the Notwell permissions :) */ - if (S_ISDIR (s->st_mode)) - s->st_mode |= - (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP - | S_IXOTH); - else - s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR); - p += 9; - } else { - size_t lc_skipped; - mode_t perms; + if (*p == ' ') /* Notwell 4 */ + p++; + if (*p == '[') + { + if (strlen (p) <= 8 || p[8] != ']') + goto error; + /* Should parse here the Notwell permissions :) */ + if (S_ISDIR (s->st_mode)) + s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH); + else + s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR); + p += 9; + } + else + { + size_t lc_skipped; + mode_t perms; - if (!vfs_parse_fileperms (p, &lc_skipped, &perms)) - goto error; - p += lc_skipped; - s->st_mode |= perms; + if (!vfs_parse_fileperms (p, &lc_skipped, &perms)) + goto error; + p += lc_skipped; + s->st_mode |= perms; } p_copy = g_strdup (p); @@ -796,75 +974,79 @@ vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, s->st_nlink = atol (columns[0]); if (s->st_nlink <= 0) - goto error; + goto error; if (!is_num (1)) - s->st_uid = vfs_finduid (columns[1]); + s->st_uid = vfs_finduid (columns[1]); else - s->st_uid = (uid_t) atol (columns[1]); + s->st_uid = (uid_t) atol (columns[1]); /* Mhm, the ls -lg did not produce a group field */ for (idx = 3; idx <= 5; idx++) - if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL) - || is_dos_date (columns[idx]) - || is_localized_month (columns[idx])) - break; + if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL) + || is_dos_date (columns[idx]) || is_localized_month (columns[idx])) + break; - if (idx == 6 - || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode))) - goto error; + if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode))) + goto error; /* We don't have gid */ - if (idx == 3 - || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)))) - idx2 = 2; - else { - /* We have gid field */ - if (is_num (2)) - s->st_gid = (gid_t) atol (columns[2]); - else - s->st_gid = vfs_findgid (columns[2]); - idx2 = 3; + if (idx == 3 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)))) + idx2 = 2; + else + { + /* We have gid field */ + if (is_num (2)) + s->st_gid = (gid_t) atol (columns[2]); + else + s->st_gid = vfs_findgid (columns[2]); + idx2 = 3; } /* This is device */ - if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) { - int maj, min; + if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) + { + int maj, min; - /* Corner case: there is no whitespace(s) between maj & min */ - if (!is_num (idx2) && idx2 == 2) { - if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2) - goto error; - } else { - if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1) - goto error; + /* Corner case: there is no whitespace(s) between maj & min */ + if (!is_num (idx2) && idx2 == 2) + { + if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2) + goto error; + } + else + { + if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1) + goto error; - if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1) - goto error; - } + if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1) + goto error; + } #ifdef HAVE_STRUCT_STAT_ST_RDEV - s->st_rdev = makedev (maj, min); + s->st_rdev = makedev (maj, min); #endif - s->st_size = 0; + s->st_size = 0; - } else { - /* Common file size */ - if (!is_num (idx2)) - goto error; + } + else + { + /* Common file size */ + if (!is_num (idx2)) + goto error; #ifdef HAVE_ATOLL - s->st_size = (off_t) atoll (columns[idx2]); + s->st_size = (off_t) atoll (columns[idx2]); #else - s->st_size = (off_t) atof (columns[idx2]); + s->st_size = (off_t) atof (columns[idx2]); #endif #ifdef HAVE_STRUCT_STAT_ST_RDEV - s->st_rdev = 0; + s->st_rdev = 0; #endif } idx = vfs_parse_filedate (idx, &s->st_mtime); if (!idx) - goto error; + goto error; /* Use resulting time value */ s->st_atime = s->st_ctime = s->st_mtime; /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */ @@ -876,45 +1058,51 @@ vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, #endif for (i = idx + 1, idx2 = 0; i < num_cols; i++) - if (strcmp (columns[i], "->") == 0) { - idx2 = i; - break; - } + if (strcmp (columns[i], "->") == 0) + { + idx2 = i; + break; + } - if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */ - &&idx2) { + if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */ + && idx2) + { - if (filename) { - *filename = - g_strndup (p + column_ptr[idx], - column_ptr[idx2] - column_ptr[idx] - 1); - } - if (linkname) { - t = g_strdup (p + column_ptr[idx2 + 1]); - *linkname = t; - } - } else { - /* Extract the filename from the string copy, not from the columns - * this way we have a chance of entering hidden directories like ". ." - */ - if (filename) { - /* - * filename = g_strdup (columns [idx++]); - */ + if (filename) + { + *filename = g_strndup (p + column_ptr[idx], column_ptr[idx2] - column_ptr[idx] - 1); + } + if (linkname) + { + t = g_strdup (p + column_ptr[idx2 + 1]); + *linkname = t; + } + } + else + { + /* Extract the filename from the string copy, not from the columns + * this way we have a chance of entering hidden directories like ". ." + */ + if (filename) + { + /* + * filename = g_strdup (columns [idx++]); + */ - t = g_strdup (p + column_ptr[idx]); - *filename = t; - } - if (linkname) - *linkname = NULL; + t = g_strdup (p + column_ptr[idx]); + *filename = t; + } + if (linkname) + *linkname = NULL; } - if (t) { - int p2 = strlen (t); - if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n')) - t[p2] = 0; - if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n')) - t[p2] = 0; + if (t) + { + int p2 = strlen (t); + if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n')) + t[p2] = 0; + if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n')) + t[p2] = 0; } g_free (p_copy); @@ -922,20 +1110,22 @@ vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, error: { - static int errorcount = 0; + static int errorcount = 0; - if (++errorcount < 5) { - message (D_ERROR, _("Cannot parse:"), "%s", - (p_copy && *p_copy) ? p_copy : line); - } else if (errorcount == 5) - message (D_ERROR, MSG_ERROR, - _("More parsing errors will be ignored.")); + if (++errorcount < 5) + { + message (D_ERROR, _("Cannot parse:"), "%s", (p_copy && *p_copy) ? p_copy : line); + } + else if (errorcount == 5) + message (D_ERROR, MSG_ERROR, _("More parsing errors will be ignored.")); } g_free (p_copy); return 0; } +/* --------------------------------------------------------------------------------------------- */ + void vfs_die (const char *m) { @@ -943,8 +1133,12 @@ vfs_die (const char *m) exit (EXIT_FAILURE); } +/* --------------------------------------------------------------------------------------------- */ + char * vfs_get_password (const char *msg) { return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD); } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/utilvfs.h b/lib/vfs/mc-vfs/utilvfs.h index f7876e993..0c1159d05 100644 --- a/lib/vfs/mc-vfs/utilvfs.h +++ b/lib/vfs/mc-vfs/utilvfs.h @@ -13,40 +13,47 @@ #include "lib/global.h" +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + /** Bit flags for vfs_split_url() * * Modify parsing parameters according to flag meaning. * @see vfs_split_url() */ -enum VFS_URL_FLAGS { +enum VFS_URL_FLAGS +{ URL_USE_ANONYMOUS = 1, /**< if set, empty *user will contain NULL instead of current */ - URL_NOSLASH = 2 /**< if set, 'proto://' part in url is not searched */ + URL_NOSLASH = 2 /**< if set, 'proto://' part in url is not searched */ }; +/*** structures declarations (and typedefs of structures)*****************************************/ + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + int vfs_finduid (const char *name); int vfs_findgid (const char *name); char *vfs_split_url (const char *path, char **host, char **user, int *port, - char **pass, int default_port, enum VFS_URL_FLAGS flags); + char **pass, int default_port, enum VFS_URL_FLAGS flags); int vfs_split_text (char *p); int vfs_mkstemps (char **pname, const char *prefix, const char *basename); void vfs_die (const char *msg); char *vfs_get_password (const char *msg); -char * vfs_get_local_username(void); +char *vfs_get_local_username (void); -gboolean vfs_parse_filetype (const char *s, size_t *ret_skipped, - mode_t *ret_type); -gboolean vfs_parse_fileperms (const char *s, size_t *ret_skipped, - mode_t *ret_perms); -gboolean vfs_parse_filemode (const char *s, size_t *ret_skipped, - mode_t *ret_mode); -gboolean vfs_parse_raw_filemode (const char *s, size_t *ret_skipped, - mode_t *ret_mode); +gboolean vfs_parse_filetype (const char *s, size_t * ret_skipped, mode_t * ret_type); +gboolean vfs_parse_fileperms (const char *s, size_t * ret_skipped, mode_t * ret_perms); +gboolean vfs_parse_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode); +gboolean vfs_parse_raw_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode); -int vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, - char **linkname); -int vfs_parse_filedate (int idx, time_t *t); +int vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, char **linkname); +int vfs_parse_filedate (int idx, time_t * t); +/*** inline functions ****************************************************************************/ #endif diff --git a/lib/vfs/mc-vfs/vfs-impl.h b/lib/vfs/mc-vfs/vfs-impl.h index 17a0170bc..235aa4acc 100644 --- a/lib/vfs/mc-vfs/vfs-impl.h +++ b/lib/vfs/mc-vfs/vfs-impl.h @@ -4,8 +4,8 @@ * \brief Header: VFS implemntation (?) */ -#ifndef MC_VFS_IMPL_H -#define MC_VFS_IMPL_H +#ifndef MC__VFS_IMPL_H +#define MC__VFS_IMPL_H #include #include @@ -16,90 +16,100 @@ #include #include "vfs.h" -#include "lib/fs.h" /* MC_MAXPATHLEN */ +#include "lib/fs.h" /* MC_MAXPATHLEN */ + +/*** typedefs(not structures) and defined constants **********************************************/ typedef void *vfsid; + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + struct vfs_stamping; -struct vfs_class { +struct vfs_class +{ struct vfs_class *next; - const char *name; /* "FIles over SHell" */ + const char *name; /* "FIles over SHell" */ vfs_class_flags_t flags; - const char *prefix; /* "fish:" */ - void *data; /* this is for filesystem's own use */ - int verrno; /* can't use errno because glibc2 might define errno as function */ + const char *prefix; /* "fish:" */ + void *data; /* this is for filesystem's own use */ + int verrno; /* can't use errno because glibc2 might define errno as function */ - int (*init) (struct vfs_class *me); - void (*done) (struct vfs_class *me); + int (*init) (struct vfs_class * me); + void (*done) (struct vfs_class * me); /** * The fill_names method shall call the callback function for every * filesystem name that this vfs module supports. */ - void (*fill_names) (struct vfs_class *me, fill_names_f); + void (*fill_names) (struct vfs_class * me, fill_names_f); /** * The which() method shall return the index of the vfs subsystem * or -1 if this vfs cannot handle the given pathname. */ - int (*which) (struct vfs_class *me, const char *path); + int (*which) (struct vfs_class * me, const char *path); - void *(*open) (struct vfs_class *me, const char *fname, int flags, - mode_t mode); + void *(*open) (struct vfs_class * me, const char *fname, int flags, mode_t mode); int (*close) (void *vfs_info); - ssize_t (*read) (void *vfs_info, char *buffer, size_t count); - ssize_t (*write) (void *vfs_info, const char *buf, size_t count); + ssize_t (*read) (void *vfs_info, char *buffer, size_t count); + ssize_t (*write) (void *vfs_info, const char *buf, size_t count); - void *(*opendir) (struct vfs_class *me, const char *dirname); + void *(*opendir) (struct vfs_class * me, const char *dirname); void *(*readdir) (void *vfs_info); int (*closedir) (void *vfs_info); - int (*stat) (struct vfs_class *me, const char *path, struct stat * buf); - int (*lstat) (struct vfs_class *me, const char *path, struct stat * buf); + int (*stat) (struct vfs_class * me, const char *path, struct stat * buf); + int (*lstat) (struct vfs_class * me, const char *path, struct stat * buf); int (*fstat) (void *vfs_info, struct stat * buf); - int (*chmod) (struct vfs_class *me, const char *path, int mode); - int (*chown) (struct vfs_class *me, const char *path, uid_t owner, gid_t group); - int (*utime) (struct vfs_class *me, const char *path, - struct utimbuf * times); + int (*chmod) (struct vfs_class * me, const char *path, int mode); + int (*chown) (struct vfs_class * me, const char *path, uid_t owner, gid_t group); + int (*utime) (struct vfs_class * me, const char *path, struct utimbuf * times); - int (*readlink) (struct vfs_class *me, const char *path, char *buf, - size_t size); - int (*symlink) (struct vfs_class *me, const char *n1, const char *n2); - int (*link) (struct vfs_class *me, const char *p1, const char *p2); - int (*unlink) (struct vfs_class *me, const char *path); - int (*rename) (struct vfs_class *me, const char *p1, const char *p2); - int (*chdir) (struct vfs_class *me, const char *path); - int (*ferrno) (struct vfs_class *me); - off_t (*lseek) (void *vfs_info, off_t offset, int whence); - int (*mknod) (struct vfs_class *me, const char *path, mode_t mode, dev_t dev); + int (*readlink) (struct vfs_class * me, const char *path, char *buf, size_t size); + int (*symlink) (struct vfs_class * me, const char *n1, const char *n2); + int (*link) (struct vfs_class * me, const char *p1, const char *p2); + int (*unlink) (struct vfs_class * me, const char *path); + int (*rename) (struct vfs_class * me, const char *p1, const char *p2); + int (*chdir) (struct vfs_class * me, const char *path); + int (*ferrno) (struct vfs_class * me); + off_t (*lseek) (void *vfs_info, off_t offset, int whence); + int (*mknod) (struct vfs_class * me, const char *path, mode_t mode, dev_t dev); - vfsid (*getid) (struct vfs_class *me, const char *path); + vfsid (*getid) (struct vfs_class * me, const char *path); int (*nothingisopen) (vfsid id); void (*free) (vfsid id); - char *(*getlocalcopy) (struct vfs_class *me, const char *filename); - int (*ungetlocalcopy) (struct vfs_class *me, const char *filename, - const char *local, int has_changed); + char *(*getlocalcopy) (struct vfs_class * me, const char *filename); + int (*ungetlocalcopy) (struct vfs_class * me, const char *filename, + const char *local, int has_changed); - int (*mkdir) (struct vfs_class *me, const char *path, mode_t mode); - int (*rmdir) (struct vfs_class *me, const char *path); + int (*mkdir) (struct vfs_class * me, const char *path, mode_t mode); + int (*rmdir) (struct vfs_class * me, const char *path); int (*ctl) (void *vfs_info, int ctlop, void *arg); - int (*setctl) (struct vfs_class *me, const char *path, int ctlop, - void *arg); + int (*setctl) (struct vfs_class * me, const char *path, int ctlop, void *arg); }; /* * This union is used to ensure that there is enough space for the * filename (d_name) when the dirent structure is created. */ -union vfs_dirent { +union vfs_dirent +{ struct dirent dent; - char _extra_buffer[offsetof(struct dirent, d_name) + MC_MAXPATHLEN + 1]; + char _extra_buffer[offsetof (struct dirent, d_name) + MC_MAXPATHLEN + 1]; }; +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + + /* Register a file system class */ int vfs_register_class (struct vfs_class *vfs); @@ -133,4 +143,5 @@ void init_ftpfs (void); void init_fish (void); #endif +/*** inline functions ****************************************************************************/ #endif /* MC_VFS_IMPL_H */ diff --git a/lib/vfs/mc-vfs/vfs.c b/lib/vfs/mc-vfs/vfs.c index 55af34d15..5a0e5b3ca 100644 --- a/lib/vfs/mc-vfs/vfs.c +++ b/lib/vfs/mc-vfs/vfs.c @@ -75,13 +75,19 @@ #include "local.h" +/*** global variables ****************************************************************************/ + +/*** file scope macro definitions ****************************************************************/ + #if defined(_AIX) && !defined(NAME_MAX) -# define NAME_MAX FILENAME_MAX +#define NAME_MAX FILENAME_MAX #endif -/** They keep track of the current directory */ -static struct vfs_class *current_vfs; -static char *current_dir; +#define VFS_FIRST_HANDLE 100 + +#define ISSLASH(a) (!a || (a == '/')) + +/*** file scope type declarations ****************************************************************/ struct vfs_openfile { @@ -96,15 +102,50 @@ struct vfs_dirinfo GIConv converter; }; +/*** file scope variables ************************************************************************/ + +/** They keep track of the current directory */ +static struct vfs_class *current_vfs; +static char *current_dir; static GPtrArray *vfs_openfiles; static long vfs_free_handle_list = -1; -#define VFS_FIRST_HANDLE 100 static struct vfs_class *localfs_class; static GString *vfs_str_buffer; -/** Create new VFS handle and put it to the list */ +static struct vfs_class *vfs_list; + +static struct dirent *mc_readdir_result = NULL; + +static const struct +{ + const char *name; + size_t name_len; + const char *substitute; +} url_table[] = +{ + /* *INDENT-OFF* */ +#ifdef ENABLE_VFS_FTP + { "ftp://", 6, "/#ftp:" }, +#endif +#ifdef ENABLE_VFS_FISH + { "sh://", 5, "/#sh:" }, + { "ssh://", 6, "/#sh:" }, +#endif +#ifdef ENABLE_VFS_SMB + { "smb://", 6, "/#smb:" }, +#endif + { "a:", 2, "/#a" } + /* *INDENT-ON* */ +}; + +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ +/** + * Create new VFS handle and put it to the list + */ + static int vfs_new_handle (struct vfs_class *vclass, void *fsinfo) { @@ -132,7 +173,9 @@ vfs_new_handle (struct vfs_class *vclass, void *fsinfo) return h->handle; } +/* --------------------------------------------------------------------------------------------- */ /** Find VFS class by file handle */ + static struct vfs_class * vfs_op (int handle) { @@ -150,7 +193,9 @@ vfs_op (int handle) return h->vclass; } +/* --------------------------------------------------------------------------------------------- */ /** Find private file data by file handle */ + static void * vfs_info (int handle) { @@ -168,7 +213,9 @@ vfs_info (int handle) return h->fsinfo; } +/* --------------------------------------------------------------------------------------------- */ /** Free open file data for given file handle */ + static void vfs_free_handle (int handle) { @@ -185,20 +232,8 @@ vfs_free_handle (int handle) } } -static struct vfs_class *vfs_list; -int -vfs_register_class (struct vfs_class *vfs) -{ - if (vfs->init != NULL) /* vfs has own initialization function */ - if (!(*vfs->init) (vfs)) /* but it failed */ - return 0; - - vfs->next = vfs_list; - vfs_list = vfs; - - return 1; -} +/* --------------------------------------------------------------------------------------------- */ /** Return VFS class for the given prefix */ static struct vfs_class * @@ -221,10 +256,330 @@ vfs_prefix_to_class (char *prefix) return NULL; } +/* --------------------------------------------------------------------------------------------- */ + +static gboolean +path_magic (const char *path) +{ + struct stat buf; + + return (stat (path, &buf) != 0); +} + +/* --------------------------------------------------------------------------------------------- */ + +static struct vfs_class * +_vfs_get_class (char *path) +{ + char *semi; + char *slash; + struct vfs_class *ret; + + g_return_val_if_fail (path, NULL); + + semi = strrchr (path, '#'); + if (semi == NULL || !path_magic (path)) + return NULL; + + slash = strchr (semi, PATH_SEP); + *semi = '\0'; + if (slash != NULL) + *slash = '\0'; + + ret = vfs_prefix_to_class (semi + 1); + + if (slash != NULL) + *slash = PATH_SEP; + if (ret == NULL) + ret = _vfs_get_class (path); + + *semi = '#'; + return ret; +} + +/* --------------------------------------------------------------------------------------------- */ +/* now used only by vfs_translate_path, but could be used in other vfs + * plugin to automatic detect encoding + * path - path to translate + * size - how many bytes from path translate + * defcnv - convertor, that is used as default, when path does not contain any + * #enc: subtring + * buffer - used to store result of translation + */ + +static estr_t +_vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer) +{ + const char *semi; + const char *slash; + estr_t state = ESTR_SUCCESS; + + if (size == 0) + return ESTR_SUCCESS; + + size = (size > 0) ? size : (signed int) strlen (path); + + /* try found /#enc: */ + semi = g_strrstr_len (path, size, PATH_SEP_STR VFS_ENCODING_PREFIX); + if (semi != NULL) + { + char encoding[16]; + GIConv coder = INVALID_CONV; + int ms; + + /* first must be translated part before /#enc: */ + ms = semi - path; + + state = _vfs_translate_path (path, ms, defcnv, buffer); + + if (state != ESTR_SUCCESS) + return state; + + /* now can be translated part after #enc: */ + semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */ + slash = strchr (semi, PATH_SEP); + /* ignore slashes after size; */ + if (slash - path >= size) + slash = NULL; + + ms = (slash != NULL) ? slash - semi : (int) strlen (semi); + ms = min ((unsigned int) ms, sizeof (encoding) - 1); + /* limit encoding size (ms) to path size (size) */ + if (semi + ms > path + size) + ms = path + size - semi; + memcpy (encoding, semi, ms); + encoding[ms] = '\0'; + +#if HAVE_CHARSET + if (is_supported_encoding (encoding)) + coder = str_crt_conv_to (encoding); +#endif + + if (coder != INVALID_CONV) + { + if (slash != NULL) + state = str_vfs_convert_to (coder, slash, path + size - slash, buffer); + else if (buffer->len == 0) + g_string_append_c (buffer, PATH_SEP); + str_close_conv (coder); + return state; + } + + errno = EINVAL; + state = ESTR_FAILURE; + } + else + { + /* path can be translated whole at once */ + state = str_vfs_convert_to (defcnv, path, size, buffer); + } + + return state; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +ferrno (struct vfs_class *vfs) +{ + return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN; + /* Hope that error message is obscure enough ;-) */ +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Return current directory. If it's local, reread the current directory + * from the OS. You must g_strdup() whatever this function returns. + */ + +static const char * +_vfs_get_cwd (void) +{ + char *trans; + + trans = vfs_translate_path_n (current_dir); + + if (_vfs_get_class (trans) == NULL) + { + const char *encoding = vfs_get_encoding (current_dir); + + if (encoding == NULL) + { + char *tmp; + + tmp = g_get_current_dir (); + if (tmp != NULL) + { /* One of the directories in the path is not readable */ + estr_t state; + char *sys_cwd; + + g_string_set_size (vfs_str_buffer, 0); + state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer); + g_free (tmp); + + sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL; + if (sys_cwd != NULL) + { + struct stat my_stat, my_stat2; + /* Check if it is O.K. to use the current_dir */ + if (cd_symlinks + && mc_stat (sys_cwd, &my_stat) == 0 + && mc_stat (current_dir, &my_stat2) == 0 + && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev) + g_free (sys_cwd); + else + { + g_free (current_dir); + current_dir = sys_cwd; + } + } + } + } + } + + g_free (trans); + return current_dir; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +vfs_setup_wd (void) +{ + current_dir = g_strdup (PATH_SEP_STR); + _vfs_get_cwd (); + + if (strlen (current_dir) > MC_MAXPATHLEN - 2) + vfs_die ("Current dir too long.\n"); + + current_vfs = vfs_get_class (current_dir); +} + +/* --------------------------------------------------------------------------------------------- */ + +static char * +mc_def_getlocalcopy (const char *filename) +{ + char *tmp; + int fdin, fdout; + ssize_t i; + char buffer[8192]; + struct stat mystat; + + fdin = mc_open (filename, O_RDONLY | O_LINEAR); + if (fdin == -1) + return NULL; + + fdout = vfs_mkstemps (&tmp, "vfs", filename); + + if (fdout == -1) + goto fail; + while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) + { + if (write (fdout, buffer, i) != i) + goto fail; + } + if (i == -1) + goto fail; + i = mc_close (fdin); + fdin = -1; + if (i == -1) + goto fail; + if (close (fdout) == -1) + { + fdout = -1; + goto fail; + } + + if (mc_stat (filename, &mystat) != -1) + chmod (tmp, mystat.st_mode); + + return tmp; + + fail: + if (fdout != -1) + close (fdout); + if (fdin != -1) + mc_close (fdin); + g_free (tmp); + return NULL; +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename, + const char *local, int has_changed) +{ + int fdin = -1, fdout = -1; + if (has_changed) + { + char buffer[8192]; + ssize_t i; + + if (!vfs->write) + goto failed; + + fdin = open (local, O_RDONLY); + if (fdin == -1) + goto failed; + fdout = mc_open (filename, O_WRONLY | O_TRUNC); + if (fdout == -1) + goto failed; + while ((i = read (fdin, buffer, sizeof (buffer))) > 0) + if (mc_write (fdout, buffer, (size_t) i) != i) + goto failed; + if (i == -1) + goto failed; + + if (close (fdin) == -1) + { + fdin = -1; + goto failed; + } + fdin = -1; + if (mc_close (fdout) == -1) + { + fdout = -1; + goto failed; + } + } + unlink (local); + return 0; + + failed: + message (D_ERROR, _("Changes to file lost"), "%s", filename); + if (fdout != -1) + mc_close (fdout); + if (fdin != -1) + close (fdin); + unlink (local); + return -1; +} + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +int +vfs_register_class (struct vfs_class *vfs) +{ + if (vfs->init != NULL) /* vfs has own initialization function */ + if (!(*vfs->init) (vfs)) /* but it failed */ + return 0; + + vfs->next = vfs_list; + vfs_list = vfs; + + return 1; +} + +/* --------------------------------------------------------------------------------------------- */ /** Strip known vfs suffixes from a filename (possible improvement: strip * suffix from last path component). * \return a malloced string which has to be freed. */ + char * vfs_strip_suffix_from_filename (const char *filename) { @@ -259,13 +614,7 @@ vfs_strip_suffix_from_filename (const char *filename) return p; } -static gboolean -path_magic (const char *path) -{ - struct stat buf; - - return (stat (path, &buf) != 0); -} +/* --------------------------------------------------------------------------------------------- */ /** * Splits path extracting vfs part. @@ -278,6 +627,7 @@ path_magic (const char *path) * What is left in path is p1. You still want to g_free(path), you DON'T * want to free neither *inpath nor *op */ + struct vfs_class * vfs_split (char *path, char **inpath, char **op) { @@ -322,34 +672,7 @@ vfs_split (char *path, char **inpath, char **op) return ret; } -static struct vfs_class * -_vfs_get_class (char *path) -{ - char *semi; - char *slash; - struct vfs_class *ret; - - g_return_val_if_fail (path, NULL); - - semi = strrchr (path, '#'); - if (semi == NULL || !path_magic (path)) - return NULL; - - slash = strchr (semi, PATH_SEP); - *semi = '\0'; - if (slash != NULL) - *slash = '\0'; - - ret = vfs_prefix_to_class (semi + 1); - - if (slash != NULL) - *slash = PATH_SEP; - if (ret == NULL) - ret = _vfs_get_class (path); - - *semi = '#'; - return ret; -} +/* --------------------------------------------------------------------------------------------- */ struct vfs_class * vfs_get_class (const char *pathname) @@ -366,6 +689,8 @@ vfs_get_class (const char *pathname) return vfs; } +/* --------------------------------------------------------------------------------------------- */ + const char * vfs_get_encoding (const char *path) { @@ -380,7 +705,7 @@ vfs_get_encoding (const char *path) if (semi != NULL) { - semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */ + semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */ slash = strchr (semi, PATH_SEP); if (slash != NULL) slash[0] = '\0'; @@ -396,83 +721,7 @@ vfs_get_encoding (const char *path) } } -/* now used only by vfs_translate_path, but could be used in other vfs - * plugin to automatic detect encoding - * path - path to translate - * size - how many bytes from path translate - * defcnv - convertor, that is used as default, when path does not contain any - * #enc: subtring - * buffer - used to store result of translation - */ -static estr_t -_vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer) -{ - const char *semi; - const char *slash; - estr_t state = ESTR_SUCCESS; - - if (size == 0) - return ESTR_SUCCESS; - - size = (size > 0) ? size : (signed int) strlen (path); - - /* try found /#enc: */ - semi = g_strrstr_len (path, size, PATH_SEP_STR VFS_ENCODING_PREFIX); - if (semi != NULL) - { - char encoding[16]; - GIConv coder = INVALID_CONV; - int ms; - - /* first must be translated part before /#enc: */ - ms = semi - path; - - state = _vfs_translate_path (path, ms, defcnv, buffer); - - if (state != ESTR_SUCCESS) - return state; - - /* now can be translated part after #enc: */ - semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */ - slash = strchr (semi, PATH_SEP); - /* ignore slashes after size; */ - if (slash - path >= size) - slash = NULL; - - ms = (slash != NULL) ? slash - semi : (int) strlen (semi); - ms = min ((unsigned int) ms, sizeof (encoding) - 1); - /* limit encoding size (ms) to path size (size) */ - if (semi + ms > path + size) - ms = path + size - semi; - memcpy (encoding, semi, ms); - encoding[ms] = '\0'; - -#if HAVE_CHARSET - if (is_supported_encoding (encoding)) - coder = str_crt_conv_to (encoding); -#endif - - if (coder != INVALID_CONV) - { - if (slash != NULL) - state = str_vfs_convert_to (coder, slash, path + size - slash, buffer); - else if (buffer->len == 0) - g_string_append_c (buffer, PATH_SEP); - str_close_conv (coder); - return state; - } - - errno = EINVAL; - state = ESTR_FAILURE; - } - else - { - /* path can be translated whole at once */ - state = str_vfs_convert_to (defcnv, path, size, buffer); - } - - return state; -} +/* --------------------------------------------------------------------------------------------- */ char * vfs_translate_path (const char *path) @@ -488,6 +737,8 @@ vfs_translate_path (const char *path) return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL; } +/* --------------------------------------------------------------------------------------------- */ + char * vfs_translate_path_n (const char *path) { @@ -497,6 +748,8 @@ vfs_translate_path_n (const char *path) return (result != NULL) ? g_strdup (result) : NULL; } +/* --------------------------------------------------------------------------------------------- */ + char * vfs_canon_and_translate (const char *path) { @@ -511,12 +764,7 @@ vfs_canon_and_translate (const char *path) return result; } -static int -ferrno (struct vfs_class *vfs) -{ - return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN; - /* Hope that error message is obscure enough ;-) */ -} +/* --------------------------------------------------------------------------------------------- */ int mc_open (const char *filename, int flags, ...) @@ -548,7 +796,7 @@ mc_open (const char *filename, int flags, ...) return -1; } - info = vfs->open (vfs, file, flags, mode); /* open must be supported */ + info = vfs->open (vfs, file, flags, mode); /* open must be supported */ g_free (file); if (info == NULL) { @@ -559,6 +807,7 @@ mc_open (const char *filename, int flags, ...) return vfs_new_handle (vfs, info); } +/* --------------------------------------------------------------------------------------------- */ #define MC_NAMEOP(name, inarg, callarg) \ int mc_##name inarg \ @@ -580,14 +829,16 @@ int mc_##name inarg \ return result; \ } -MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode)) -MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group)) -MC_NAMEOP (utime, (const char *path, struct utimbuf * times), (vfs, mpath, times)) -MC_NAMEOP (readlink, (const char *path, char *buf, size_t bufsiz), (vfs, mpath, buf, bufsiz)) -MC_NAMEOP (unlink, (const char *path), (vfs, mpath)) -MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode)) -MC_NAMEOP (rmdir, (const char *path), (vfs, mpath)) -MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev)) +MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode)); +MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group)); +MC_NAMEOP (utime, (const char *path, struct utimbuf * times), (vfs, mpath, times)); +MC_NAMEOP (readlink, (const char *path, char *buf, size_t bufsiz), (vfs, mpath, buf, bufsiz)); +MC_NAMEOP (unlink, (const char *path), (vfs, mpath)); +MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode)); +MC_NAMEOP (rmdir, (const char *path), (vfs, mpath)); +MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev)); + +/* --------------------------------------------------------------------------------------------- */ int mc_symlink (const char *name1, const char *path) @@ -623,6 +874,8 @@ mc_symlink (const char *name1, const char *path) return -1; } +/* --------------------------------------------------------------------------------------------- */ + #define MC_HANDLEOP(name, inarg, callarg) \ ssize_t mc_##name inarg \ { \ @@ -639,8 +892,10 @@ ssize_t mc_##name inarg \ return result; \ } -MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_info (handle), buffer, count)) -MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_info (handle), buf, nbyte)) +MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_info (handle), buffer, count)); +MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_info (handle), buf, nbyte)); + +/* --------------------------------------------------------------------------------------------- */ #define MC_RENAMEOP(name) \ int mc_##name (const char *fname1, const char *fname2) \ @@ -672,11 +927,11 @@ int mc_##name (const char *fname1, const char *fname2) \ return result; \ } -MC_RENAMEOP (link) -MC_RENAMEOP (rename) +MC_RENAMEOP (link) MC_RENAMEOP (rename); +/* --------------------------------------------------------------------------------------------- */ -int -mc_ctl (int handle, int ctlop, void *arg) + int + mc_ctl (int handle, int ctlop, void *arg) { struct vfs_class *vfs = vfs_op (handle); @@ -686,6 +941,8 @@ mc_ctl (int handle, int ctlop, void *arg) return vfs->ctl ? (*vfs->ctl) (vfs_info (handle), ctlop, arg) : 0; } +/* --------------------------------------------------------------------------------------------- */ + int mc_setctl (const char *path, int ctlop, void *arg) { @@ -707,6 +964,8 @@ mc_setctl (const char *path, int ctlop, void *arg) return result; } +/* --------------------------------------------------------------------------------------------- */ + int mc_close (int handle) { @@ -733,6 +992,8 @@ mc_close (int handle) return result; } +/* --------------------------------------------------------------------------------------------- */ + DIR * mc_opendir (const char *dirname) { @@ -782,7 +1043,7 @@ mc_opendir (const char *dirname) } } -static struct dirent *mc_readdir_result = NULL; +/* --------------------------------------------------------------------------------------------- */ struct dirent * mc_readdir (DIR * dirp) @@ -835,6 +1096,8 @@ mc_readdir (DIR * dirp) return (entry != NULL) ? mc_readdir_result : NULL; } +/* --------------------------------------------------------------------------------------------- */ + int mc_closedir (DIR * dirp) { @@ -859,6 +1122,8 @@ mc_closedir (DIR * dirp) return result; } +/* --------------------------------------------------------------------------------------------- */ + int mc_stat (const char *filename, struct stat *buf) { @@ -888,6 +1153,8 @@ mc_stat (const char *filename, struct stat *buf) return result; } +/* --------------------------------------------------------------------------------------------- */ + int mc_lstat (const char *filename, struct stat *buf) { @@ -914,6 +1181,8 @@ mc_lstat (const char *filename, struct stat *buf) return result; } +/* --------------------------------------------------------------------------------------------- */ + int mc_fstat (int handle, struct stat *buf) { @@ -933,75 +1202,12 @@ mc_fstat (int handle, struct stat *buf) return result; } -/** - * Return current directory. If it's local, reread the current directory - * from the OS. You must g_strdup() whatever this function returns. - */ -static const char * -_vfs_get_cwd (void) -{ - char *trans; - - trans = vfs_translate_path_n (current_dir); - - if (_vfs_get_class (trans) == NULL) - { - const char *encoding = vfs_get_encoding (current_dir); - - if (encoding == NULL) - { - char *tmp; - - tmp = g_get_current_dir (); - if (tmp != NULL) - { /* One of the directories in the path is not readable */ - estr_t state; - char *sys_cwd; - - g_string_set_size (vfs_str_buffer, 0); - state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer); - g_free (tmp); - - sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL; - if (sys_cwd != NULL) - { - struct stat my_stat, my_stat2; - /* Check if it is O.K. to use the current_dir */ - if (cd_symlinks - && mc_stat (sys_cwd, &my_stat) == 0 - && mc_stat (current_dir, &my_stat2) == 0 - && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev) - g_free (sys_cwd); - else - { - g_free (current_dir); - current_dir = sys_cwd; - } - } - } - } - } - - g_free (trans); - return current_dir; -} - -static void -vfs_setup_wd (void) -{ - current_dir = g_strdup (PATH_SEP_STR); - _vfs_get_cwd (); - - if (strlen (current_dir) > MC_MAXPATHLEN - 2) - vfs_die ("Current dir too long.\n"); - - current_vfs = vfs_get_class (current_dir); -} - +/* --------------------------------------------------------------------------------------------- */ /** * Return current directory. If it's local, reread the current directory * from the OS. Put directory to the provided buffer. */ + char * mc_get_current_wd (char *buffer, size_t size) { @@ -1011,15 +1217,19 @@ mc_get_current_wd (char *buffer, size_t size) return buffer; } +/* --------------------------------------------------------------------------------------------- */ /** * Return current directory without any OS calls. */ + char * vfs_get_current_dir (void) { return current_dir; } +/* --------------------------------------------------------------------------------------------- */ + off_t mc_lseek (int fd, off_t offset, int whence) { @@ -1039,12 +1249,11 @@ mc_lseek (int fd, off_t offset, int whence) return result; } +/* --------------------------------------------------------------------------------------------- */ /** * remove //, /./ and /../ */ -#define ISSLASH(a) (!a || (a == '/')) - char * vfs_canon (const char *path) { @@ -1074,10 +1283,12 @@ vfs_canon (const char *path) } } +/* --------------------------------------------------------------------------------------------- */ /** * VFS chdir. * Return 0 on success, -1 on failure. */ + int mc_chdir (const char *path) { @@ -1139,14 +1350,18 @@ mc_chdir (const char *path) } } +/* --------------------------------------------------------------------------------------------- */ /* Return TRUE is the current VFS class is local */ + gboolean vfs_current_is_local (void) { return (current_vfs->flags & VFSF_LOCAL) != 0; } +/* --------------------------------------------------------------------------------------------- */ /* Return flags of the VFS class of the given filename */ + vfs_class_flags_t vfs_file_class_flags (const char *filename) { @@ -1162,53 +1377,7 @@ vfs_file_class_flags (const char *filename) return vfs->flags; } -static char * -mc_def_getlocalcopy (const char *filename) -{ - char *tmp; - int fdin, fdout; - ssize_t i; - char buffer[8192]; - struct stat mystat; - - fdin = mc_open (filename, O_RDONLY | O_LINEAR); - if (fdin == -1) - return NULL; - - fdout = vfs_mkstemps (&tmp, "vfs", filename); - - if (fdout == -1) - goto fail; - while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) - { - if (write (fdout, buffer, i) != i) - goto fail; - } - if (i == -1) - goto fail; - i = mc_close (fdin); - fdin = -1; - if (i == -1) - goto fail; - if (close (fdout) == -1) - { - fdout = -1; - goto fail; - } - - if (mc_stat (filename, &mystat) != -1) - chmod (tmp, mystat.st_mode); - - return tmp; - - fail: - if (fdout != -1) - close (fdout); - if (fdin != -1) - mc_close (fdin); - g_free (tmp); - return NULL; -} +/* --------------------------------------------------------------------------------------------- */ char * mc_getlocalcopy (const char *pathname) @@ -1222,8 +1391,7 @@ mc_getlocalcopy (const char *pathname) struct vfs_class *vfs = vfs_get_class (path); result = vfs->getlocalcopy != NULL ? - vfs->getlocalcopy (vfs, path) : - mc_def_getlocalcopy (path); + vfs->getlocalcopy (vfs, path) : mc_def_getlocalcopy (path); g_free (path); if (result == NULL) errno = ferrno (vfs); @@ -1232,55 +1400,7 @@ mc_getlocalcopy (const char *pathname) return result; } -static int -mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename, - const char *local, int has_changed) -{ - int fdin = -1, fdout = -1; - if (has_changed) - { - char buffer[8192]; - ssize_t i; - - if (!vfs->write) - goto failed; - - fdin = open (local, O_RDONLY); - if (fdin == -1) - goto failed; - fdout = mc_open (filename, O_WRONLY | O_TRUNC); - if (fdout == -1) - goto failed; - while ((i = read (fdin, buffer, sizeof (buffer))) > 0) - if (mc_write (fdout, buffer, (size_t) i) != i) - goto failed; - if (i == -1) - goto failed; - - if (close (fdin) == -1) - { - fdin = -1; - goto failed; - } - fdin = -1; - if (mc_close (fdout) == -1) - { - fdout = -1; - goto failed; - } - } - unlink (local); - return 0; - - failed: - message (D_ERROR, _("Changes to file lost"), "%s", filename); - if (fdout != -1) - mc_close (fdout); - if (fdin != -1) - close (fdin); - unlink (local); - return -1; -} +/* --------------------------------------------------------------------------------------------- */ int mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed) @@ -1302,6 +1422,7 @@ mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed) return return_value; } +/* --------------------------------------------------------------------------------------------- */ void vfs_init (void) @@ -1344,6 +1465,8 @@ vfs_init (void) vfs_setup_wd (); } +/* --------------------------------------------------------------------------------------------- */ + void vfs_shut (void) { @@ -1362,10 +1485,12 @@ vfs_shut (void) g_free (mc_readdir_result); } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * These ones grab information from the VFS * and handles them to an upper layer */ + void vfs_fill_names (fill_names_f func) { @@ -1376,33 +1501,12 @@ vfs_fill_names (fill_names_f func) (*vfs->fill_names) (vfs, func); } -/* +/* --------------------------------------------------------------------------------------------- */ +/** * Returns vfs path corresponding to given url. If passed string is * not recognized as url, g_strdup(url) is returned. */ -static const struct -{ - const char *name; - size_t name_len; - const char *substitute; -} url_table[] = -{ - /* *INDENT-OFF* */ -#ifdef ENABLE_VFS_FTP - { "ftp://", 6, "/#ftp:" }, -#endif -#ifdef ENABLE_VFS_FISH - { "sh://", 5, "/#sh:" }, - { "ssh://", 6, "/#sh:" }, -#endif -#ifdef ENABLE_VFS_SMB - { "smb://", 6, "/#smb:" }, -#endif - { "a:", 2, "/#a" } - /* *INDENT-ON* */ -}; - char * vfs_translate_url (const char *url) { @@ -1416,8 +1520,12 @@ vfs_translate_url (const char *url) return g_strdup (url); } +/* --------------------------------------------------------------------------------------------- */ + gboolean vfs_file_is_local (const char *filename) { return (vfs_file_class_flags (filename) & VFSF_LOCAL) != 0; } + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/mc-vfs/vfs.h b/lib/vfs/mc-vfs/vfs.h index 53cf602d9..3d236ee25 100644 --- a/lib/vfs/mc-vfs/vfs.h +++ b/lib/vfs/mc-vfs/vfs.h @@ -4,8 +4,8 @@ * \brief Header: Virtual File System switch code */ -#ifndef MC_VFS_VFS_H -#define MC_VFS_VFS_H +#ifndef MC__VFS_VFS_H +#define MC__VFS_VFS_H #include #include @@ -14,18 +14,7 @@ #include "lib/global.h" -struct vfs_class; - -/* Flags of VFS classes */ -typedef enum -{ - VFSF_UNKNOWN = 0, - VFSF_LOCAL = 1 << 0, /* Class is local (not virtual) filesystem */ - VFSF_NOLINKS = 1 << 1 /* Hard links not supported */ -} vfs_class_flags_t; - -void vfs_init (void); -void vfs_shut (void); +/*** typedefs(not structures) and defined constants **********************************************/ #if defined (ENABLE_VFS_FTP) || defined (ENABLE_VFS_FISH) || defined (ENABLE_VFS_SMB) #define ENABLE_VFS_NET 1 @@ -37,14 +26,114 @@ void vfs_shut (void); * See also: * vfs_fill_names(). */ + +#define VFS_ENCODING_PREFIX "#enc:" + +#define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY) +/* Midnight commander code should _not_ use other flags than those + listed above and O_APPEND */ + +#if (O_ALL & O_APPEND) +#warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz" +#define O_LINEAR 0 +#define IS_LINEAR(a) 0 +#define NO_LINEAR(a) a +#else +#define O_LINEAR O_APPEND +#define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */ +#define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a)) +#endif + +/* O_LINEAR is strange beast, be careful. If you open file asserting + * O_RDONLY | O_LINEAR, you promise: + * + * a) to read file linearly from beginning to the end + * b) not to open another file before you close this one + * (this will likely go away in future) + * as a special gift, you may + * c) lseek() immediately after open(), giving ftpfs chance to + * reget. Be warned that this lseek() can fail, and you _have_ + * to handle that gratefully. + * + * O_LINEAR allows filesystems not to create temporary file in some + * cases (ftp transfer). -- pavel@ucw.cz + */ + +/* And now some defines for our errors. */ + +#ifdef ENOSYS +#define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */ +#else +#define E_NOTSUPP EFAULT /* Does this happen? */ +#endif + +#ifdef ENOMSG +#define E_UNKNOWN ENOMSG /* if we do not know what error happened */ +#else +#define E_UNKNOWN EIO /* if we do not know what error happened */ +#endif + +#ifdef EREMOTEIO +#define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */ +#else +#define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */ +#endif + +#ifdef EPROTO +#define E_PROTO EPROTO /* if other side fails to follow protocol */ +#else +#define E_PROTO EIO +#endif + typedef void (*fill_names_f) (const char *); +/*** enums ***************************************************************************************/ + +/* Flags of VFS classes */ +typedef enum +{ + VFSF_UNKNOWN = 0, + VFSF_LOCAL = 1 << 0, /* Class is local (not virtual) filesystem */ + VFSF_NOLINKS = 1 << 1 /* Hard links not supported */ +} vfs_class_flags_t; + +/* Operations for mc_ctl - on open file */ +enum +{ + VFS_CTL_IS_NOTREADY +}; + +/* Operations for mc_setctl - on path */ +enum +{ + VFS_SETCTL_FORGET, + VFS_SETCTL_RUN, + VFS_SETCTL_LOGFILE, + VFS_SETCTL_FLUSH, /* invalidate directory cache */ + + /* Setting this makes vfs layer give out potentially incorrect data, + but it also makes some operations much faster. Use with caution. */ + VFS_SETCTL_STALE_DATA +}; + +/*** structures declarations (and typedefs of structures)*****************************************/ + +struct vfs_class; + +/*** global variables defined in .c file *********************************************************/ + extern int vfs_timeout; #ifdef ENABLE_VFS_NET extern int use_netrc; #endif +/*** declarations of public functions ************************************************************/ + +void vfs_init (void); +void vfs_shut (void); + + void vfs_timeout_handler (void); int vfs_timeouts (void); void vfs_expire (int now); @@ -86,7 +175,6 @@ char *vfs_translate_url (const char *url); struct vfs_class *vfs_get_class (const char *path); vfs_class_flags_t vfs_file_class_flags (const char *filename); -#define VFS_ENCODING_PREFIX "#enc:" /* return encoding after last #enc: or NULL, if part does not contain #enc: * return static buffer */ const char *vfs_get_encoding (const char *path); @@ -109,77 +197,5 @@ char *vfs_get_current_dir (void); * return static buffer */ char *vfs_translate_path (const char *path); -/* Operations for mc_ctl - on open file */ -enum { - VFS_CTL_IS_NOTREADY -}; - -/* Operations for mc_setctl - on path */ -enum { - VFS_SETCTL_FORGET, - VFS_SETCTL_RUN, - VFS_SETCTL_LOGFILE, - VFS_SETCTL_FLUSH, /* invalidate directory cache */ - - /* Setting this makes vfs layer give out potentially incorrect data, - but it also makes some operations much faster. Use with caution. */ - VFS_SETCTL_STALE_DATA -}; - -#define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY) -/* Midnight commander code should _not_ use other flags than those - listed above and O_APPEND */ - -#if (O_ALL & O_APPEND) - #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz" -#define O_LINEAR 0 -#define IS_LINEAR(a) 0 -#define NO_LINEAR(a) a -#else -#define O_LINEAR O_APPEND -#define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */ -#define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a)) -#endif - -/* O_LINEAR is strange beast, be careful. If you open file asserting - * O_RDONLY | O_LINEAR, you promise: - * - * a) to read file linearly from beginning to the end - * b) not to open another file before you close this one - * (this will likely go away in future) - * as a special gift, you may - * c) lseek() immediately after open(), giving ftpfs chance to - * reget. Be warned that this lseek() can fail, and you _have_ - * to handle that gratefully. - * - * O_LINEAR allows filesystems not to create temporary file in some - * cases (ftp transfer). -- pavel@ucw.cz - */ - -/* And now some defines for our errors. */ - -#ifdef ENOSYS -#define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */ -#else -#define E_NOTSUPP EFAULT /* Does this happen? */ -#endif - -#ifdef ENOMSG -#define E_UNKNOWN ENOMSG /* if we do not know what error happened */ -#else -#define E_UNKNOWN EIO /* if we do not know what error happened */ -#endif - -#ifdef EREMOTEIO -#define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */ -#else -#define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */ -#endif - -#ifdef EPROTO -#define E_PROTO EPROTO /* if other side fails to follow protocol */ -#else -#define E_PROTO EIO -#endif - +/*** inline functions ****************************************************************************/ #endif /* MC_VFS_VFS_H */ diff --git a/lib/vfs/mc-vfs/xdirentry.h b/lib/vfs/mc-vfs/xdirentry.h index 323b7feab..9e87f1f36 100644 --- a/lib/vfs/mc-vfs/xdirentry.h +++ b/lib/vfs/mc-vfs/xdirentry.h @@ -5,12 +5,14 @@ */ -#ifndef MC_VFS_XDIRENTRY_H -#define MC_VFS_XDIRENTRY_H +#ifndef MC__VFS_XDIRENTRY_H +#define MC__VFS_XDIRENTRY_H #include #include +/*** typedefs(not structures) and defined constants **********************************************/ + #define LINK_FOLLOW 15 #define LINK_NO_FOLLOW -1 @@ -31,6 +33,21 @@ #define VFS_S_REMOTE 1 #define VFS_S_READONLY 2 +#define ERRNOR(a, b) do { me->verrno = a; return b; } while (0) + +#define MEDATA ((struct vfs_s_subclass *) me->data) + +#define FH ((struct vfs_s_fh *) fh) +#define FH_SUPER FH->ino->super + +#define LS_NOT_LINEAR 0 +#define LS_LINEAR_CLOSED 1 +#define LS_LINEAR_OPEN 2 +#define LS_LINEAR_PREOPEN 3 + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ /* Single connection or archive */ struct vfs_s_super @@ -70,7 +87,7 @@ struct vfs_s_super int host_flags; char *scr_env; } fish; -#endif /* ENABLE_VFS_FISH */ +#endif /* ENABLE_VFS_FISH */ #ifdef ENABLE_VFS_FTP struct { @@ -80,29 +97,29 @@ struct vfs_s_super char *password; int port; - char *proxy; /* proxy server, NULL if no proxy */ + char *proxy; /* proxy server, NULL if no proxy */ int failed_on_login; /* used to pass the failure reason to upper levels */ int use_passive_connection; int remote_is_amiga; /* No leading slash allowed for AmiTCP (Amiga) */ int isbinary; - int cwd_deferred; /* current_directory was changed but CWD command hasn't - been sent yet */ - int strict; /* ftp server doesn't understand - * "LIST -la "; use "CWD "/ - * "LIST" instead - */ + int cwd_deferred; /* current_directory was changed but CWD command hasn't + been sent yet */ + int strict; /* ftp server doesn't understand + * "LIST -la "; use "CWD "/ + * "LIST" instead + */ int ctl_connection_busy; } ftp; -#endif /* ENABLE_VFS_FTP */ +#endif /* ENABLE_VFS_FTP */ #if defined(ENABLE_VFS_CPIO) || defined(ENABLE_VFS_TAR) struct { int fd; struct stat st; - int type; /* Type of the archive */ - struct defer_inode *deferred; /* List of inodes for which another entries may appear */ + int type; /* Type of the archive */ + struct defer_inode *deferred; /* List of inodes for which another entries may appear */ } arch; -#endif /* ENABLE_VFS_CPIO || ENABLE_VFS_TAR */ +#endif /* ENABLE_VFS_CPIO || ENABLE_VFS_TAR */ } u; }; @@ -149,13 +166,13 @@ struct vfs_s_fh off_t got, total; int append; } fish; -#endif /* ENABLE_VFS_FISH */ +#endif /* ENABLE_VFS_FISH */ #ifdef ENABLE_VFS_FTP struct { int sock, append; } ftp; -#endif /* ENABLE_VFS_FTP */ +#endif /* ENABLE_VFS_FTP */ } u; }; @@ -198,6 +215,9 @@ struct vfs_s_subclass void (*linear_close) (struct vfs_class * me, struct vfs_s_fh * fh); }; +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ /* entries and inodes */ struct vfs_s_inode *vfs_s_new_inode (struct vfs_class *me, @@ -229,16 +249,5 @@ int vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, /* misc */ int vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino); -#define ERRNOR(a, b) do { me->verrno = a; return b; } while (0) - -#define MEDATA ((struct vfs_s_subclass *) me->data) - -#define FH ((struct vfs_s_fh *) fh) -#define FH_SUPER FH->ino->super - -#define LS_NOT_LINEAR 0 -#define LS_LINEAR_CLOSED 1 -#define LS_LINEAR_OPEN 2 -#define LS_LINEAR_PREOPEN 3 - +/*** inline functions ****************************************************************************/ #endif diff --git a/src/cmddef.h b/src/cmddef.h index d539d5445..b6a95ca42 100644 --- a/src/cmddef.h +++ b/src/cmddef.h @@ -11,194 +11,194 @@ */ /* special commands */ -#define CK_Insert_Char -1 -#define CK_Ignore_Key 0 +#define CK_Insert_Char -1 +#define CK_Ignore_Key 0 /* cursor movements */ -#define CK_BackSpace 1 -#define CK_Delete 2 -#define CK_Enter 3 -#define CK_Page_Up 4 -#define CK_Page_Down 5 -#define CK_Left 6 -#define CK_Right 7 -#define CK_Word_Left 8 -#define CK_Word_Right 9 -#define CK_Up 10 -#define CK_Down 11 -#define CK_Home 12 -#define CK_End 13 -#define CK_Tab 14 -#define CK_Undo 15 -#define CK_Beginning_Of_Text 16 -#define CK_End_Of_Text 17 -#define CK_Scroll_Up 18 -#define CK_Scroll_Down 19 -#define CK_Return 20 -#define CK_Begin_Page 21 -#define CK_End_Page 22 -#define CK_Delete_Word_Left 23 -#define CK_Delete_Word_Right 24 -#define CK_Paragraph_Up 25 -#define CK_Paragraph_Down 26 +#define CK_BackSpace 1 +#define CK_Delete 2 +#define CK_Enter 3 +#define CK_Page_Up 4 +#define CK_Page_Down 5 +#define CK_Left 6 +#define CK_Right 7 +#define CK_Word_Left 8 +#define CK_Word_Right 9 +#define CK_Up 10 +#define CK_Down 11 +#define CK_Home 12 +#define CK_End 13 +#define CK_Tab 14 +#define CK_Undo 15 +#define CK_Beginning_Of_Text 16 +#define CK_End_Of_Text 17 +#define CK_Scroll_Up 18 +#define CK_Scroll_Down 19 +#define CK_Return 20 +#define CK_Begin_Page 21 +#define CK_End_Page 22 +#define CK_Delete_Word_Left 23 +#define CK_Delete_Word_Right 24 +#define CK_Paragraph_Up 25 +#define CK_Paragraph_Down 26 /* file commands */ -#define CK_Save 101 -#define CK_Load 102 -#define CK_New 103 -#define CK_Save_As 104 -#define CK_Load_Prev_File 111 -#define CK_Load_Next_File 112 -#define CK_Load_Syntax_File 121 -#define CK_Load_Menu_File 122 -#define CK_Menu 123 +#define CK_Save 101 +#define CK_Load 102 +#define CK_New 103 +#define CK_Save_As 104 +#define CK_Load_Prev_File 111 +#define CK_Load_Next_File 112 +#define CK_Load_Syntax_File 121 +#define CK_Load_Menu_File 122 +#define CK_Menu 123 /* block commands */ -#define CK_Mark 201 -#define CK_Copy 202 -#define CK_Move 203 -#define CK_Remove 204 -#define CK_Unmark 206 -#define CK_Save_Block 207 -#define CK_Column_Mark 208 -#define CK_Shift_Block_Left 211 -#define CK_Shift_Block_Right 212 -#define CK_Mark_All 213 +#define CK_Mark 201 +#define CK_Copy 202 +#define CK_Move 203 +#define CK_Remove 204 +#define CK_Unmark 206 +#define CK_Save_Block 207 +#define CK_Column_Mark 208 +#define CK_Shift_Block_Left 211 +#define CK_Shift_Block_Right 212 +#define CK_Mark_All 213 /* search and replace */ -#define CK_Find 301 -#define CK_Find_Again 302 -#define CK_Replace 303 -#define CK_Replace_Again 304 -#define CK_Complete_Word 305 +#define CK_Find 301 +#define CK_Find_Again 302 +#define CK_Replace 303 +#define CK_Replace_Again 304 +#define CK_Complete_Word 305 #if 0 /* debugger commands */ -#define CK_Debug_Start 350 -#define CK_Debug_Stop 351 -#define CK_Debug_Toggle_Break 352 -#define CK_Debug_Clear 353 -#define CK_Debug_Next 354 -#define CK_Debug_Step 355 -#define CK_Debug_Back_Trace 356 -#define CK_Debug_Continue 357 -#define CK_Debug_Enter_Command 358 -#define CK_Debug_Until_Curser 359 +#define CK_Debug_Start 350 +#define CK_Debug_Stop 351 +#define CK_Debug_Toggle_Break 352 +#define CK_Debug_Clear 353 +#define CK_Debug_Next 354 +#define CK_Debug_Step 355 +#define CK_Debug_Back_Trace 356 +#define CK_Debug_Continue 357 +#define CK_Debug_Enter_Command 358 +#define CK_Debug_Until_Curser 359 #endif /* misc */ -#define CK_Insert_File 401 -#define CK_Quit 402 +#define CK_Insert_File 401 +#define CK_Quit 402 #define CK_Toggle_Insert 403 -#define CK_Help 404 -#define CK_Date 405 -#define CK_Refresh 406 -#define CK_Goto 407 -#define CK_Delete_Line 408 -#define CK_Delete_To_Line_End 409 -#define CK_Delete_To_Line_Begin 410 -#define CK_Man_Page 411 -#define CK_Sort 412 -#define CK_Mail 413 -#define CK_Cancel 414 -#define CK_Complete 415 -#define CK_Paragraph_Format 416 -#define CK_Util 417 -#define CK_Type_Load_Python 418 -#define CK_Find_File 419 -#define CK_Ctags 420 -#define CK_Match_Bracket 421 -#define CK_Terminal 422 -#define CK_Terminal_App 423 -#define CK_ExtCmd 424 -#define CK_User_Menu 425 -#define CK_Find_Definition 426 -#define CK_Edit_Options 427 -#define CK_Edit_Save_Mode 428 -#define CK_Choose_Syntax 429 -#define CK_About 430 +#define CK_Help 404 +#define CK_Date 405 +#define CK_Refresh 406 +#define CK_Goto 407 +#define CK_Delete_Line 408 +#define CK_Delete_To_Line_End 409 +#define CK_Delete_To_Line_Begin 410 +#define CK_Man_Page 411 +#define CK_Sort 412 +#define CK_Mail 413 +#define CK_Cancel 414 +#define CK_Complete 415 +#define CK_Paragraph_Format 416 +#define CK_Util 417 +#define CK_Type_Load_Python 418 +#define CK_Find_File 419 +#define CK_Ctags 420 +#define CK_Match_Bracket 421 +#define CK_Terminal 422 +#define CK_Terminal_App 423 +#define CK_ExtCmd 424 +#define CK_User_Menu 425 +#define CK_Find_Definition 426 +#define CK_Edit_Options 427 +#define CK_Edit_Save_Mode 428 +#define CK_Choose_Syntax 429 +#define CK_About 430 #if 0 /* application control */ -#define CK_Save_Desktop 451 -#define CK_New_Window 452 -#define CK_Cycle 453 -#define CK_Save_And_Quit 455 -#define CK_Run_Another 456 -#define CK_Check_Save_And_Quit 457 -#define CK_Maximize 458 +#define CK_Save_Desktop 451 +#define CK_New_Window 452 +#define CK_Cycle 453 +#define CK_Save_And_Quit 455 +#define CK_Run_Another 456 +#define CK_Check_Save_And_Quit 457 +#define CK_Maximize 458 #endif -#define CK_Toggle_Show_Margin 460 -#define CK_Toggle_Tab_TWS 470 -#define CK_Toggle_Syntax 480 -#define CK_Toggle_Line_State 490 +#define CK_Toggle_Show_Margin 460 +#define CK_Toggle_Tab_TWS 470 +#define CK_Toggle_Syntax 480 +#define CK_Toggle_Line_State 490 /* macro */ -#define CK_Begin_Record_Macro 501 -#define CK_End_Record_Macro 502 -#define CK_Delete_Macro 503 +#define CK_Begin_Record_Macro 501 +#define CK_End_Record_Macro 502 +#define CK_Delete_Macro 503 /* book mark */ -#define CK_Toggle_Bookmark 550 -#define CK_Flush_Bookmarks 551 -#define CK_Next_Bookmark 552 -#define CK_Prev_Bookmark 553 +#define CK_Toggle_Bookmark 550 +#define CK_Flush_Bookmarks 551 +#define CK_Next_Bookmark 552 +#define CK_Prev_Bookmark 553 /* highlight commands */ -#define CK_Page_Up_Highlight 604 -#define CK_Page_Down_Highlight 605 -#define CK_Left_Highlight 606 -#define CK_Right_Highlight 607 -#define CK_Word_Left_Highlight 608 -#define CK_Word_Right_Highlight 609 -#define CK_Up_Highlight 610 -#define CK_Down_Highlight 611 -#define CK_Home_Highlight 612 -#define CK_End_Highlight 613 -#define CK_Beginning_Of_Text_Highlight 614 -#define CK_End_Of_Text_Highlight 615 -#define CK_Begin_Page_Highlight 616 -#define CK_End_Page_Highlight 617 -#define CK_Scroll_Up_Highlight 618 -#define CK_Scroll_Down_Highlight 619 -#define CK_Paragraph_Up_Highlight 620 -#define CK_Paragraph_Down_Highlight 621 +#define CK_Page_Up_Highlight 604 +#define CK_Page_Down_Highlight 605 +#define CK_Left_Highlight 606 +#define CK_Right_Highlight 607 +#define CK_Word_Left_Highlight 608 +#define CK_Word_Right_Highlight 609 +#define CK_Up_Highlight 610 +#define CK_Down_Highlight 611 +#define CK_Home_Highlight 612 +#define CK_End_Highlight 613 +#define CK_Beginning_Of_Text_Highlight 614 +#define CK_End_Of_Text_Highlight 615 +#define CK_Begin_Page_Highlight 616 +#define CK_End_Page_Highlight 617 +#define CK_Scroll_Up_Highlight 618 +#define CK_Scroll_Down_Highlight 619 +#define CK_Paragraph_Up_Highlight 620 +#define CK_Paragraph_Down_Highlight 621 /* alt highlight commands */ -#define CK_Page_Up_Alt_Highlight 654 -#define CK_Page_Down_Alt_Highlight 655 -#define CK_Left_Alt_Highlight 656 -#define CK_Right_Alt_Highlight 657 -#define CK_Word_Left_Alt_Highlight 658 -#define CK_Word_Right_Alt_Highlight 659 -#define CK_Up_Alt_Highlight 660 -#define CK_Down_Alt_Highlight 661 -#define CK_Home_Alt_Highlight 662 -#define CK_End_Alt_Highlight 663 -#define CK_Beginning_Of_Text_Alt_Highlight 664 -#define CK_End_Of_Text_Alt_Highlight 665 -#define CK_Begin_Page_Alt_Highlight 666 -#define CK_End_Page_Alt_Highlight 667 -#define CK_Scroll_Up_Alt_Highlight 668 -#define CK_Scroll_Down_Alt_Highlight 669 -#define CK_Paragraph_Up_Alt_Highlight 670 -#define CK_Paragraph_Down_Alt_Highlight 671 +#define CK_Page_Up_Alt_Highlight 654 +#define CK_Page_Down_Alt_Highlight 655 +#define CK_Left_Alt_Highlight 656 +#define CK_Right_Alt_Highlight 657 +#define CK_Word_Left_Alt_Highlight 658 +#define CK_Word_Right_Alt_Highlight 659 +#define CK_Up_Alt_Highlight 660 +#define CK_Down_Alt_Highlight 661 +#define CK_Home_Alt_Highlight 662 +#define CK_End_Alt_Highlight 663 +#define CK_Beginning_Of_Text_Alt_Highlight 664 +#define CK_End_Of_Text_Alt_Highlight 665 +#define CK_Begin_Page_Alt_Highlight 666 +#define CK_End_Page_Alt_Highlight 667 +#define CK_Scroll_Up_Alt_Highlight 668 +#define CK_Scroll_Down_Alt_Highlight 669 +#define CK_Paragraph_Up_Alt_Highlight 670 +#define CK_Paragraph_Down_Alt_Highlight 671 /* X clipboard operations */ -#define CK_XStore 701 -#define CK_XCut 702 -#define CK_XPaste 703 -#define CK_Selection_History 704 +#define CK_XStore 701 +#define CK_XCut 702 +#define CK_XPaste 703 +#define CK_Selection_History 704 -#define CK_Shell 801 +#define CK_Shell 801 /* C-x or similar */ -#define CK_Ext_Mode 820 +#define CK_Ext_Mode 820 -#define CK_Insert_Literal 851 -#define CK_Execute_Macro 852 -#define CK_Begin_End_Macro 853 +#define CK_Insert_Literal 851 +#define CK_Execute_Macro 852 +#define CK_Begin_End_Macro 853 /* help */ #define CK_HelpHelp 1001 @@ -503,15 +503,15 @@ TODO: bring up a viewer to display the error message instead of inserting it into the text, which is annoying. */ -#define CK_Pipe_Block(i) (1000+(i)) +#define CK_Pipe_Block(i) (1000+(i)) #define SHELL_COMMANDS_i {"/edit.indent.rc", "/edit.spell.rc", /* and so on */ 0 } -#define CK_Macro(i) (2000+(i)) -#define CK_Last_Macro CK_Macro(0x7FFF) +#define CK_Macro(i) (2000+(i)) +#define CK_Last_Macro CK_Macro(0x7FFF) #else -#define CK_User_Command(i) ((i) | (1 << 16)) -#define IS_USER_COMMAND(i) ((i) & (1 << 16)) -#define CK_Macro(i) ((i) | (1 << 17)) -#define IS_MACRO_COMMAND(i) ((i) & (1 << 17)) +#define CK_User_Command(i) ((i) | (1 << 16)) +#define IS_USER_COMMAND(i) ((i) & (1 << 16)) +#define CK_Macro(i) ((i) | (1 << 17)) +#define IS_MACRO_COMMAND(i) ((i) & (1 << 17)) -#endif /* MC_CMD_DEF_H */ +#endif /* MC_CMD_DEF_H */ diff --git a/src/cons.handler.c b/src/cons.handler.c index cee2503d4..2a2eded19 100644 --- a/src/cons.handler.c +++ b/src/cons.handler.c @@ -264,9 +264,11 @@ set_attr (unsigned attr) color_map[tc & 7], color_map[bc & 7]); } -#define cursor_to(x, y) do { \ - printf("\x1B[%d;%df", (y) + 1, (x) + 1); \ - fflush(stdout); \ +#define cursor_to(x, y) \ +do \ +{ \ + printf("\x1B[%d;%df", (y) + 1, (x) + 1); \ + fflush(stdout); \ } while (0) static void diff --git a/src/diffviewer/ydiff.c b/src/diffviewer/ydiff.c index 5d2d1a4dc..df638fd33 100644 --- a/src/diffviewer/ydiff.c +++ b/src/diffviewer/ydiff.c @@ -72,27 +72,28 @@ do { \ } \ } while (0) -#define FILE_READ_BUF 4096 -#define FILE_FLAG_TEMP (1 << 0) +#define FILE_READ_BUF 4096 +#define FILE_FLAG_TEMP (1 << 0) #define OPTX 56 #define OPTY 17 -#define ADD_CH '+' -#define DEL_CH '-' -#define CHG_CH '*' -#define EQU_CH ' ' +#define ADD_CH '+' +#define DEL_CH '-' +#define CHG_CH '*' +#define EQU_CH ' ' -#define HDIFF_ENABLE 1 -#define HDIFF_MINCTX 5 -#define HDIFF_DEPTH 10 - -#define FILE_DIRTY(fs) \ - do { \ - (fs)->pos = 0; \ - (fs)->len = 0; \ - } while (0) +#define HDIFF_ENABLE 1 +#define HDIFF_MINCTX 5 +#define HDIFF_DEPTH 10 +#define FILE_DIRTY(fs) \ +do \ +{ \ + (fs)->pos = 0; \ + (fs)->len = 0; \ +} \ +while (0) /*** file scope type declarations ****************************************************************/ @@ -3306,34 +3307,42 @@ diff_view (const char *file1, const char *file2, const char *label1, const char /* --------------------------------------------------------------------------------------------- */ -#define GET_FILE_AND_STAMP(n) \ - do { \ - use_copy##n = 0; \ - real_file##n = file##n; \ - if (!vfs_file_is_local(file##n)) { \ - real_file##n = mc_getlocalcopy(file##n); \ - if (real_file##n != NULL) { \ - use_copy##n = 1; \ - if (mc_stat(real_file##n, &st##n) != 0) { \ - use_copy##n = -1; \ - } \ - } \ - } \ - } while (0) -#define UNGET_FILE(n) \ - do { \ - if (use_copy##n) { \ - int changed = 0; \ - if (use_copy##n > 0) { \ - time_t mtime = st##n.st_mtime; \ - if (mc_stat(real_file##n, &st##n) == 0) { \ - changed = (mtime != st##n.st_mtime); \ - } \ - } \ - mc_ungetlocalcopy(file##n, real_file##n, changed); \ - g_free(real_file##n); \ - } \ - } while (0) +#define GET_FILE_AND_STAMP(n) \ +do \ +{ \ + use_copy##n = 0; \ + real_file##n = file##n; \ + if (!vfs_file_is_local (file##n)) \ + { \ + real_file##n = mc_getlocalcopy (file##n); \ + if (real_file##n != NULL) \ + { \ + use_copy##n = 1; \ + if (mc_stat (real_file##n, &st##n) != 0) \ + use_copy##n = -1; \ + } \ + } \ +} \ +while (0) + +#define UNGET_FILE(n) \ +do \ +{ \ + if (use_copy##n) \ + { \ + int changed = 0; \ + if (use_copy##n > 0) \ + { \ + time_t mtime; \ + mtime = st##n.st_mtime; \ + if (mc_stat (real_file##n, &st##n) == 0) \ + changed = (mtime != st##n.st_mtime); \ + } \ + mc_ungetlocalcopy (file##n, real_file##n, changed); \ + g_free (real_file##n); \ + } \ +} \ +while (0) void dview_diff_cmd (void) diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 1f41b25b1..dd5aa34c8 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -95,26 +95,26 @@ #define START_STACK_SIZE 32 /* Some codes that may be pushed onto or returned from the undo stack */ -#define CURS_LEFT 601 -#define CURS_RIGHT 602 -#define DELCHAR 603 -#define BACKSPACE 604 -#define STACK_BOTTOM 605 -#define CURS_LEFT_LOTS 606 -#define CURS_RIGHT_LOTS 607 -#define COLUMN_ON 608 -#define COLUMN_OFF 609 -#define MARK_1 1000 -#define MARK_2 700000000 -#define KEY_PRESS 1400000000 +#define CURS_LEFT 601 +#define CURS_RIGHT 602 +#define DELCHAR 603 +#define BACKSPACE 604 +#define STACK_BOTTOM 605 +#define CURS_LEFT_LOTS 606 +#define CURS_RIGHT_LOTS 607 +#define COLUMN_ON 608 +#define COLUMN_OFF 609 +#define MARK_1 1000 +#define MARK_2 700000000 +#define KEY_PRESS 1400000000 /* Tabs spaces: (sofar only HALF_TAB_SIZE is used: */ -#define TAB_SIZE option_tab_spacing -#define HALF_TAB_SIZE ((int) option_tab_spacing / 2) +#define TAB_SIZE option_tab_spacing +#define HALF_TAB_SIZE ((int) option_tab_spacing / 2) /* max count stack files */ #define MAX_HISTORY_MOVETO 50 -#define LINE_STATE_WIDTH 8 +#define LINE_STATE_WIDTH 8 /* search/replace options */ typedef struct edit_search_options_t diff --git a/src/editor/etags.h b/src/editor/etags.h index ec9804187..48d635b48 100644 --- a/src/editor/etags.h +++ b/src/editor/etags.h @@ -4,8 +4,8 @@ #include /* size_t */ #include "lib/global.h" /* include */ -#define MAX_WIDTH_DEF_DIALOG 60 /* max width def dialog */ -#define MAX_DEFINITIONS 60 /* count found entries show */ +#define MAX_WIDTH_DEF_DIALOG 60 /* max width def dialog */ +#define MAX_DEFINITIONS 60 /* count found entries show */ #define SHORT_DEF_LEN 30 #define LONG_DEF_LEN 40 #define LINE_DEF_LEN 16 diff --git a/src/execute.h b/src/execute.h index 87b8f4fc1..79e820d50 100644 --- a/src/execute.h +++ b/src/execute.h @@ -7,9 +7,9 @@ #define MC_EXECUTE_H /* flags for shell_execute */ -#define EXECUTE_INTERNAL (1 << 0) -#define EXECUTE_AS_SHELL (1 << 2) -#define EXECUTE_HIDE (1 << 3) +#define EXECUTE_INTERNAL (1 << 0) +#define EXECUTE_AS_SHELL (1 << 2) +#define EXECUTE_HIDE (1 << 3) /* Execute functions that use the shell to execute */ void shell_execute (const char *command, int flags); diff --git a/src/help.h b/src/help.h index 3d7bbc218..73ebd29d1 100644 --- a/src/help.h +++ b/src/help.h @@ -30,7 +30,7 @@ #define MC_HELP_H /* Markers used in the help files */ -#define CHAR_LINK_START '\01' /* Ctrl-A */ +#define CHAR_LINK_START '\01' /* Ctrl-A */ #define CHAR_LINK_POINTER '\02' /* Ctrl-B */ #define CHAR_LINK_END '\03' /* Ctrl-C */ #define CHAR_NODE_END '\04' /* Ctrl-D */ diff --git a/src/history.h b/src/history.h index e29a70939..ebc91a128 100644 --- a/src/history.h +++ b/src/history.h @@ -3,46 +3,46 @@ * \brief Header: defines history section names */ -#ifndef __MC_HISTORY_H -#define __MC_HISTORY_H +#ifndef MC_HISTORY_H +#define MC_HISTORY_H /* history section names */ -#define MC_HISTORY_EDIT_SAVE_AS "mc.edit.save-as" -#define MC_HISTORY_EDIT_LOAD "mc.edit.load" -#define MC_HISTORY_EDIT_SAVE_BLOCK "mc.edit.save-block" -#define MC_HISTORY_EDIT_INSERT_FILE "mc.edit.insert-file" -#define MC_HISTORY_EDIT_GOTO_LINE "mc.edit.goto-line" -#define MC_HISTORY_EDIT_SORT "mc.edit.sort" -#define MC_HISTORY_EDIT_PASTE_EXTCMD "mc.edit.paste-extcmd" +#define MC_HISTORY_EDIT_SAVE_AS "mc.edit.save-as" +#define MC_HISTORY_EDIT_LOAD "mc.edit.load" +#define MC_HISTORY_EDIT_SAVE_BLOCK "mc.edit.save-block" +#define MC_HISTORY_EDIT_INSERT_FILE "mc.edit.insert-file" +#define MC_HISTORY_EDIT_GOTO_LINE "mc.edit.goto-line" +#define MC_HISTORY_EDIT_SORT "mc.edit.sort" +#define MC_HISTORY_EDIT_PASTE_EXTCMD "mc.edit.paste-extcmd" -#define MC_HISTORY_FM_VIEW_FILE "mc.fm.view-file" -#define MC_HISTORY_FM_MKDIR "mc.fm.mkdir" -#define MC_HISTORY_FM_LINK "mc.fm.link" -#define MC_HISTORY_FM_EDIT_LINK "mc.fm.edit-link" -#define MC_HISTORY_FM_TREE_COPY "mc.fm.tree-copy" -#define MC_HISTORY_FM_TREE_MOVE "mc.fm.tree-move" -#define MC_HISTORY_FM_PANELIZE_ADD "mc.fm.panelize.add" -#define MC_HISTORY_FM_FILTERED_VIEW "mc.fm.filtered-view" -#define MC_HISTORY_FM_PANEL_FILTER "mc.fm.panel-filter" -#define MC_HISTORY_FM_MENU_EXEC_PARAM "mc.fm.menu.exec.parameter" +#define MC_HISTORY_FM_VIEW_FILE "mc.fm.view-file" +#define MC_HISTORY_FM_MKDIR "mc.fm.mkdir" +#define MC_HISTORY_FM_LINK "mc.fm.link" +#define MC_HISTORY_FM_EDIT_LINK "mc.fm.edit-link" +#define MC_HISTORY_FM_TREE_COPY "mc.fm.tree-copy" +#define MC_HISTORY_FM_TREE_MOVE "mc.fm.tree-move" +#define MC_HISTORY_FM_PANELIZE_ADD "mc.fm.panelize.add" +#define MC_HISTORY_FM_FILTERED_VIEW "mc.fm.filtered-view" +#define MC_HISTORY_FM_PANEL_FILTER "mc.fm.panel-filter" +#define MC_HISTORY_FM_MENU_EXEC_PARAM "mc.fm.menu.exec.parameter" -#define MC_HISTORY_ESC_TIMEOUT "mc.esc.timeout" +#define MC_HISTORY_ESC_TIMEOUT "mc.esc.timeout" -#define MC_HISTORY_VIEW_GOTO "mc.view.goto" -#define MC_HISTORY_VIEW_GOTO_LINE "mc.view.goto-line" -#define MC_HISTORY_VIEW_GOTO_ADDR "mc.view.goto-addr" -#define MC_HISTORY_VIEW_SEARCH_REGEX "mc.view.search.regex" +#define MC_HISTORY_VIEW_GOTO "mc.view.goto" +#define MC_HISTORY_VIEW_GOTO_LINE "mc.view.goto-line" +#define MC_HISTORY_VIEW_GOTO_ADDR "mc.view.goto-addr" +#define MC_HISTORY_VIEW_SEARCH_REGEX "mc.view.search.regex" -#define MC_HISTORY_FTPFS_ACCOUNT "mc.vfs.ftp.account" -#define MC_HISTORY_VFS_PASSWORD "mc.vfs.password" +#define MC_HISTORY_FTPFS_ACCOUNT "mc.vfs.ftp.account" +#define MC_HISTORY_VFS_PASSWORD "mc.vfs.password" -#define MC_HISTORY_EXT_PARAMETER "mc.ext.parameter" +#define MC_HISTORY_EXT_PARAMETER "mc.ext.parameter" -#define MC_HISTORY_HOTLIST_ADD "mc.hotlist.add" +#define MC_HISTORY_HOTLIST_ADD "mc.hotlist.add" -#define MC_HISTORY_SHARED_SEARCH "mc.shared.search" +#define MC_HISTORY_SHARED_SEARCH "mc.shared.search" -#define MC_HISTORY_YDIFF_GOTO_LINE "mc.ydiff.goto-line" +#define MC_HISTORY_YDIFF_GOTO_LINE "mc.ydiff.goto-line" -#endif +#endif /* MC_HISTORY_H */ diff --git a/src/hotlist.c b/src/hotlist.c index 188161141..438c2cc2f 100644 --- a/src/hotlist.c +++ b/src/hotlist.c @@ -65,22 +65,22 @@ #define UY 2 #define BX UX -#define BY (LINES - 6) +#define BY (LINES - 6) -#define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but)) +#define BUTTONS (sizeof(hotlist_but)/sizeof(struct _hotlist_but)) #define LABELS 3 -#define B_ADD_CURRENT B_USER +#define B_ADD_CURRENT B_USER #define B_REMOVE (B_USER + 1) -#define B_NEW_GROUP (B_USER + 2) -#define B_NEW_ENTRY (B_USER + 3) -#define B_UP_GROUP (B_USER + 4) -#define B_INSERT (B_USER + 5) -#define B_APPEND (B_USER + 6) -#define B_MOVE (B_USER + 7) +#define B_NEW_GROUP (B_USER + 2) +#define B_NEW_ENTRY (B_USER + 3) +#define B_UP_GROUP (B_USER + 4) +#define B_INSERT (B_USER + 5) +#define B_APPEND (B_USER + 6) +#define B_MOVE (B_USER + 7) #ifdef ENABLE_VFS -#define B_FREE_ALL_VFS (B_USER + 8) -#define B_REFRESH_VFS (B_USER + 9) +#define B_FREE_ALL_VFS (B_USER + 8) +#define B_REFRESH_VFS (B_USER + 9) #endif int hotlist_has_dot_dot = 1; @@ -232,13 +232,15 @@ update_path_name (void) dlg_redraw (dlg); } -#define CHECK_BUFFER \ -do { \ - int i; \ -\ - if ((i = strlen (current->label) + 3) > buflen) { \ +#define CHECK_BUFFER \ +do \ +{ \ + size_t i; \ + i = strlen (current->label); \ + if (i + 3 > buflen) { \ g_free (buf); \ - buf = g_malloc (buflen = 1024 * (i/1024 + 1)); \ + buflen = 1024 * (i/1024 + 1); \ + buf = g_malloc (buflen); \ } \ buf[0] = '\0'; \ } while (0) @@ -933,9 +935,9 @@ static int add_new_entry_input (const char *header, const char *text1, const char *text2, const char *help, char **r1, char **r2) { -#define RELATIVE_Y_BUTTONS 4 -#define RELATIVE_Y_LABEL_PTH 3 -#define RELATIVE_Y_INPUT_PTH 4 +#define RELATIVE_Y_BUTTONS 4 +#define RELATIVE_Y_LABEL_PTH 3 +#define RELATIVE_Y_INPUT_PTH 4 QuickWidget quick_widgets[] = { /* 0 */ QUICK_BUTTON (55, 80, RELATIVE_Y_BUTTONS, 0, N_("&Cancel"), B_CANCEL, NULL), @@ -1278,11 +1280,11 @@ load_group (struct hotlist *grp) #define TKN_ENTRY 1 #define TKN_STRING 2 #define TKN_URL 3 -#define TKN_ENDGROUP 4 -#define TKN_COMMENT 5 -#define TKN_EOL 125 -#define TKN_EOF 126 -#define TKN_UNKNOWN 127 +#define TKN_ENDGROUP 4 +#define TKN_COMMENT 5 +#define TKN_EOL 125 +#define TKN_EOF 126 +#define TKN_UNKNOWN 127 static GString *tkn_buf = NULL; @@ -1384,19 +1386,21 @@ hot_next_token (void) return ret; } -#define SKIP_TO_EOL { \ -int _tkn; \ -while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \ +#define SKIP_TO_EOL \ +{ \ + int _tkn; \ + while ((_tkn = hot_next_token ()) != TKN_EOF && _tkn != TKN_EOL) ; \ } #define CHECK_TOKEN(_TKN_) \ tkn = hot_next_token (); \ -if (tkn != _TKN_) { \ +if (tkn != _TKN_) \ +{ \ hotlist_state.readonly = 1; \ hotlist_state.file_error = 1; \ while (tkn != TKN_EOL && tkn != TKN_EOF) \ tkn = hot_next_token (); \ -break; \ + break; \ } static void diff --git a/src/hotlist.h b/src/hotlist.h index c051519c6..c4a1bb7aa 100644 --- a/src/hotlist.h +++ b/src/hotlist.h @@ -6,9 +6,9 @@ #ifndef MC_HOTLIST_H #define MC_HOTLIST_H -#define LIST_VFSLIST 0x01 -#define LIST_HOTLIST 0x02 -#define LIST_MOVELIST 0x04 +#define LIST_VFSLIST 0x01 +#define LIST_HOTLIST 0x02 +#define LIST_MOVELIST 0x04 void add2hotlist_cmd (void); char *hotlist_cmd (int list_vfs); diff --git a/src/keybind.h b/src/keybind.h index d0da37d44..fc624ef8b 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -17,7 +17,7 @@ typedef struct key_config_t { } key_config_t; /* The global keymaps are of this type */ -#define KEYMAP_SHORTCUT_LENGTH 32 /* FIXME: is 32 bytes enough for shortcut? */ +#define KEYMAP_SHORTCUT_LENGTH 32 /* FIXME: is 32 bytes enough for shortcut? */ typedef struct global_keymap_t { long key; unsigned long command; diff --git a/src/learn.c b/src/learn.c index 12023b98b..2fb94fc9a 100644 --- a/src/learn.c +++ b/src/learn.c @@ -49,15 +49,15 @@ #include "learn.h" #include "wtools.h" -#define UX 4 -#define UY 3 +#define UX 4 +#define UY 3 -#define BY UY + 17 +#define BY UY + 17 -#define ROWS 13 -#define COLSHIFT 23 +#define ROWS 13 +#define COLSHIFT 23 -#define BUTTONS 2 +#define BUTTONS 2 static struct { int ret_cmd, flags, y, x; diff --git a/src/listmode.c b/src/listmode.c index b39830c39..429e8c92a 100644 --- a/src/listmode.c +++ b/src/listmode.c @@ -51,14 +51,14 @@ #include "main.h" #include "listmode.h" -#define UX 5 -#define UY 2 +#define UX 5 +#define UY 2 -#define BX 5 -#define BY 18 +#define BX 5 +#define BY 18 -#define B_ADD B_USER -#define B_REMOVE (B_USER + 1) +#define B_ADD B_USER +#define B_REMOVE (B_USER + 1) static WListbox *l_listmode; diff --git a/src/panel.h b/src/panel.h index db9dc712b..7af34c249 100644 --- a/src/panel.h +++ b/src/panel.h @@ -17,7 +17,7 @@ #define selection(p) (&(p->dir.list[p->selected])) #define DEFAULT_USER_FORMAT "half type name | size | perm" -#define LIST_TYPES 4 +#define LIST_TYPES 4 enum list_types { list_full, /* Name, size, perm/date */ @@ -116,9 +116,9 @@ void panel_set_sort_order (WPanel *panel, const panel_field_t *sort_order); void panel_re_sort (WPanel *panel); void panel_change_encoding (WPanel * panel); -#define UP_OPTIMIZE 0 -#define UP_RELOAD 1 -#define UP_ONLY_CURRENT 2 +#define UP_OPTIMIZE 0 +#define UP_RELOAD 1 +#define UP_ONLY_CURRENT 2 #define UP_KEEPSEL ((char *) -1) diff --git a/src/panelize.c b/src/panelize.c index 3c944bea1..077343f66 100644 --- a/src/panelize.c +++ b/src/panelize.c @@ -51,16 +51,16 @@ #include "panelize.h" #include "history.h" -#define UX 5 -#define UY 2 +#define UX 5 +#define UY 2 -#define BX 5 -#define BY 18 +#define BX 5 +#define BY 18 -#define BUTTONS 4 -#define LABELS 3 -#define B_ADD B_USER -#define B_REMOVE (B_USER + 1) +#define BUTTONS 4 +#define LABELS 3 +#define B_ADD B_USER +#define B_REMOVE (B_USER + 1) static WListbox *l_panelize; static Dlg_head *panelize_dlg; diff --git a/src/selcodepage.h b/src/selcodepage.h index c828b07db..d2563982f 100644 --- a/src/selcodepage.h +++ b/src/selcodepage.h @@ -13,15 +13,15 @@ gboolean do_set_codepage (int); gboolean do_select_codepage (void); /* some results of select_charset() */ -#define SELECT_CHARSET_CANCEL -2 +#define SELECT_CHARSET_CANCEL -2 /* select_charset() returns this value if dialog has been canceled */ -#define SELECT_CHARSET_OTHER_8BIT -1 +#define SELECT_CHARSET_OTHER_8BIT -1 /* select_charset() returns this value if seldisplay == TRUE * and the last item has been selected. Last item is "Other 8 bits" */ -#define SELECT_CHARSET_NO_TRANSLATE -1 +#define SELECT_CHARSET_NO_TRANSLATE -1 /* select_charset() returns this value if seldisplay == FALSE * and the 1st item has been selected. 1st item is "No translation" */ /* In other cases select_charset() returns non-negative value * which is number of codepage in codepage list */ -#endif /* MC_SELCODEPAGE_H */ +#endif /* MC_SELCODEPAGE_H */ diff --git a/src/viewer/growbuf.c b/src/viewer/growbuf.c index 8be2ff551..623199e6d 100644 --- a/src/viewer/growbuf.c +++ b/src/viewer/growbuf.c @@ -46,7 +46,7 @@ #include "internal.h" /* Block size for reading files in parts */ -#define VIEW_PAGE_SIZE ((size_t) 8192) +#define VIEW_PAGE_SIZE ((size_t) 8192) /*** global variables ****************************************************************************/ diff --git a/src/widget.h b/src/widget.h index 2bcd13987..91c54eb42 100644 --- a/src/widget.h +++ b/src/widget.h @@ -58,10 +58,10 @@ typedef struct WButton { int action; /* what to do when pressed */ int selected; /* button state */ -#define HIDDEN_BUTTON 0 -#define NARROW_BUTTON 1 -#define NORMAL_BUTTON 2 -#define DEFPUSH_BUTTON 3 +#define HIDDEN_BUTTON 0 +#define NARROW_BUTTON 1 +#define NORMAL_BUTTON 2 +#define DEFPUSH_BUTTON 3 unsigned int flags; /* button flags */ struct hotkey_t text; /* text of button, contain hotkey too */ int hotpos; /* offset hot KEY char in text */ @@ -79,8 +79,8 @@ typedef struct WRadio { typedef struct WCheck { Widget widget; -#define C_BOOL 0x0001 -#define C_CHANGE 0x0002 +#define C_BOOL 0x0001 +#define C_CHANGE 0x0002 unsigned int state; /* check button state */ struct hotkey_t text; /* text of check button */ } WCheck; @@ -177,7 +177,7 @@ struct WListbox { }; /* number of bttons in buttonbar */ -#define BUTTONBAR_LABELS_NUM 10 +#define BUTTONBAR_LABELS_NUM 10 typedef struct WButtonBar { Widget widget;