int ug_rename (const gchar *old_filename, const gchar *new_filename) { if (g_get_filename_charsets (NULL)) return g_rename (old_filename, new_filename); else { gchar *cp_old_filename = g_filename_from_utf8 (old_filename, -1, NULL, NULL, NULL); gchar *cp_new_filename = g_filename_from_utf8 (new_filename, -1, NULL, NULL, NULL); int save_errno; int retval; if (cp_old_filename == NULL || cp_new_filename == NULL) { g_free (cp_old_filename); g_free (cp_new_filename); errno = EINVAL; return -1; } retval = g_rename (cp_old_filename, cp_new_filename); save_errno = errno; g_free (cp_old_filename); g_free (cp_new_filename); errno = save_errno; return retval; } }
void path_global_init(void) { const char *charset = NULL; charset = config_get_string(CONF_FS_CHARSET, NULL); if (charset == NULL) { #ifndef G_OS_WIN32 const gchar **encodings; g_get_filename_charsets(&encodings); if (encodings[0] != NULL && *encodings[0] != '\0') charset = encodings[0]; #else /* G_OS_WIN32 */ /* Glib claims that file system encoding is always utf-8 * on native Win32 (i.e. not Cygwin). * However this is true only if <gstdio.h> helpers are used. * MPD uses regular <stdio.h> functions. * Those functions use encoding determined by GetACP(). */ char win_charset[13]; sprintf(win_charset, "cp%u", GetACP()); charset = win_charset; #endif } if (charset) { path_set_fs_charset(charset); } else { g_message("setting filesystem charset to ISO-8859-1"); path_set_fs_charset("ISO-8859-1"); } }
/** * ticonv_gfe_to_zfe: * @model: a calculator model taken in #CalcModel. * @src: the name of variable to convert from GLib filename encoding * * This function converts a locale dependent filename into a * valid ZIP filename. * Example: 'foobar' => foobar, 'alpha' => _alpha_. * * Return value: %dst as a newly allocated string. **/ TIEXPORT4 char* TICALL ticonv_gfe_to_zfe(CalcModel model, const char *src_) { char *src, *p; char *dst, *q; // This conversion is needed and works only if the filename charset // is UTF-8. Otherwise, the equivalent conversion is done in // ticonv_utf16_to_gfe. if (!g_get_filename_charsets(NULL)) return g_strdup(src_); p = src = (char *)src_; q = dst = g_malloc0(18*strlen(src)+1); while(*p) { if((*p & 0xFF) == 0xCE) { const char *str; p++; switch(*p & 0xff) { case 0xbc: str = "_mu_"; break; case 0xb1: str = "_alpha_"; break; case 0xb2: str = "_beta_"; break; case 0x93: str = "_GAMMA_"; break; case 0xb3: str = "_gamma_"; break; case 0x94: str = "_DELTA_"; break; case 0xb4: str = "_delta_"; break; case 0xb5: str = "_epsilon_";break; case 0xb6: str = "_zeta_"; break; case 0xb8: str = "_theta_"; break; case 0xbb: str = "_lambda_"; break; case 0xbe: str = "_ksi_"; break; case 0xa0: str = "_PI_"; break; case 0xc0: str = "_pi_"; break; case 0xc1: str = "_rho_"; break; case 0xa3: str = "_SIGMA_"; break; case 0xc3: str = "_sigma_"; break; case 0xc4: str = "_tau_"; break; case 0xd5: str = "_PHI_"; break; case 0xa8: str = "_PSI_"; break; case 0xa9: str = "_OMEGA_"; break; case 0xc9: str = "_omega_"; break; default: str = ""; break; } memcpy(q, str, strlen(str) + 1); q += strlen(str); p++; } else *q++ = *p++; } *q = '\0'; return dst; }
static gboolean gtkGetFilenameCharset(const gchar **filename_charset) { const gchar **charsets = NULL; gboolean is_utf8 = FALSE; #if GTK_CHECK_VERSION(2, 6, 0) is_utf8 = g_get_filename_charsets (&charsets); #endif if (filename_charset && charsets) *filename_charset = charsets[0]; return is_utf8; }
int ug_delete_dir_recursive (const gchar *utf8_dir) { GDir* dir; gchar* name; gchar* path; gchar* locale_dir; gboolean is_dir; if (g_get_filename_charsets (NULL)) { locale_dir = NULL; dir = g_dir_open (utf8_dir, 0, NULL); } else { locale_dir = g_filename_from_utf8 (utf8_dir, -1, NULL, NULL, NULL); dir = g_dir_open (locale_dir, 0, NULL); } if (dir == NULL) { g_free (locale_dir); return -1; } for (;;) { name = (gchar*) g_dir_read_name (dir); if (name == NULL) break; if (locale_dir == NULL) { path = g_build_filename (utf8_dir, name, NULL); is_dir = g_file_test (path, G_FILE_TEST_IS_DIR); } else { name = g_build_filename (locale_dir, name, NULL); path = g_filename_to_utf8 (name, -1, NULL, NULL, NULL); is_dir = g_file_test (name, G_FILE_TEST_IS_DIR); g_free (name); } // delete file or directory if (is_dir) ug_delete_dir_recursive (path); else ug_delete_file (path); g_free (path); } g_free (locale_dir); g_dir_close (dir); ug_delete_dir (utf8_dir); return 0; }
void ug_attachment_sync (void) { GDir* dir; guint stamp; gchar* path; gchar* name; gchar* locale_dir; if (g_get_filename_charsets (NULL)) { locale_dir = NULL; dir = g_dir_open (attachment_dir, 0, NULL); } else { locale_dir = g_filename_from_utf8 (attachment_dir, -1, NULL, NULL, NULL); dir = g_dir_open (locale_dir, 0, NULL); } if (dir == NULL) { g_free (locale_dir); return; } for (;;) { name = (gchar*) g_dir_read_name (dir); if (name == NULL) break; stamp = strtoul (name, NULL, 16); if (g_hash_table_lookup (attachment_hash, GUINT_TO_POINTER (stamp))) continue; // delete directory if (locale_dir == NULL) path = g_build_filename (attachment_dir, name, NULL); else { name = g_build_filename (locale_dir, name, NULL); path = g_filename_to_utf8 (name, -1, NULL, NULL, NULL); g_free (name); } ug_delete_dir_recursive (path); g_free (path); } g_free (locale_dir); g_dir_close (dir); }
int ug_unlink (const gchar *filename) { if (g_get_filename_charsets (NULL)) return g_unlink (filename); else { gchar *cp_filename = g_filename_from_utf8 (filename, -1, NULL, NULL, NULL); int save_errno; int retval; if (cp_filename == NULL) { errno = EINVAL; return -1; } retval = g_unlink (cp_filename); save_errno = errno; g_free (cp_filename); errno = save_errno; return retval; } }
int ug_delete_dir (const gchar *dir_utf8) { if (g_get_filename_charsets (NULL)) return g_rmdir (dir_utf8); else { gchar *cp_filename = g_filename_from_utf8 (dir_utf8, -1, NULL, NULL, NULL); int save_errno; int retval; if (cp_filename == NULL) { errno = EINVAL; return -1; } retval = g_rmdir (cp_filename); save_errno = errno; g_free (cp_filename); errno = save_errno; return retval; } }
/* {{{ proto array Glib::filenameGetCharsets() Determines the preferred character sets used for filenames. Returns an array. The first element is a boolean indicating if the filename encoding is UTF-8. The second element is the filename encoding. The final element is an array of character sets used when trying to generate a displayable representation of a filename. */ PHP_METHOD(Glib, filenameGetCharsets) { const gchar **charsets; gboolean utf8; zval *charset_array; int i; if (zend_parse_parameters_none() == FAILURE) { return; } array_init(return_value); MAKE_STD_ZVAL(charset_array); array_init(charset_array); utf8 = g_get_filename_charsets(&charsets); add_next_index_bool(return_value, utf8); add_next_index_string(return_value, (char *)charsets[0], 1); for (i = 1; charsets[i]; i++) { add_next_index_string(charset_array, (char *)charsets[i], 1); } add_next_index_zval(return_value, charset_array); }
/** @brief parse commandline options and store results for later use @param argc no. of commandline arguments, as supplied to main() @param argv array of commanline arguments, as supplied to main() @return */ void e2_cl_option_process (gint argc, gchar *argv[]) { gchar *freeme; gint c; #ifdef DEBUG_MESSAGES gint d = 0; #endif static struct option long_options[] = { {"one", required_argument, NULL, '1'}, {"two", required_argument, NULL, '2'}, {"share-config", required_argument, NULL, 'a'}, {"config", required_argument, NULL, 'c'}, #ifdef DEBUG_MESSAGES {"debug", required_argument, NULL, 'd'}, #endif {"encoding", required_argument, NULL, 'e'}, {"fallback-encoding", required_argument, NULL, 'f'}, {"trash", required_argument, NULL, 't'}, {"ignore-problems", no_argument, NULL, 'i'}, {"log-all", no_argument, NULL, 'l'}, {"daemon", no_argument, NULL, 'm'}, {"original", no_argument, NULL, 'o'}, {"run-at-start", required_argument, NULL, 'r'}, {"set-option", required_argument, NULL, 's'}, {"user", no_argument, NULL, 'u'}, #ifdef DEBUG_MESSAGES {"verbose", no_argument, NULL, 'x'}, #endif {"build", no_argument, NULL, 'b'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {NULL, no_argument, NULL, 0} }; opterr = 0; //no option-parse error messages //non-string non-zero-default values need to be set before the options are parsed #ifdef DEBUG_MESSAGES e2_cl_options.debug_level = E2_DEBUG_LEVEL; #endif //default to no logging when detailed messaging happening e2_cl_options.suppress_gtk_log = TRUE; //(E2_DEBUG_LEVEL > 2); #ifdef DEBUG_MESSAGES # define CHAR_D "d:" # define CHAR_X "x" #else # define CHAR_D # define CHAR_X #endif const gchar *pattern = "1:2:a:c:"CHAR_D"e:f:t:ilmor:s:u"CHAR_X"bhv"; while ((c = getopt_long (argc, argv, pattern, long_options, NULL)) != -1) { #ifdef DEBUG_MESSAGES d++; #endif if (optarg != NULL && *optarg != '\0') { switch (c) { case '1': printd (DEBUG, "setting pane 1 startup path '%s'", optarg); //no encoding conversion until encoding choices are certain e2_cl_options.pane1_path = _e2_cl_option_get_path (optarg); break; case '2': g_free (e2_cl_options.pane2_path); printd (DEBUG, "setting pane 2 startup path '%s'", optarg); //no encoding conversion yet e2_cl_options.pane2_path = _e2_cl_option_get_path (optarg); break; case 'a': g_free (e2_cl_options.sharedconfig_dir); //no encoding conversion yet e2_cl_options.sharedconfig_dir = _e2_cl_option_get_path (optarg); break; case 'c': g_free (e2_cl_options.config_dir); //no encoding conversion yet e2_cl_options.config_dir = _e2_cl_option_get_path (optarg); break; #ifdef DEBUG_MESSAGES case 'd': printd (DEBUG, "setting debug level '%s'", optarg); { gchar *end; e2_cl_options.debug_level = g_ascii_strtoull (optarg, &end, 10); if (end == optarg) { e2_cl_options.debug_level = E2_DEBUG_LEVEL; printd (WARN, "failed setting debug level '%s'", optarg); } // e2_cl_options.suppress_gtk_log = // (e2_cl_options.debug_level > 2); } break; #endif case 'e': g_free (e2_cl_options.encoding); e2_cl_options.encoding = g_strdup (optarg); break; case 'f': g_free (e2_cl_options.fallback_encoding); e2_cl_options.fallback_encoding = g_strdup (optarg); break; case 'r': e2_cl_options.startup_commands = g_slist_append (e2_cl_options.startup_commands, g_strdup (optarg)); break; case 's': e2_cl_options.option_overrides = g_list_append (e2_cl_options.option_overrides, g_strdup (optarg)); break; case 't': g_free (e2_cl_options.trash_dir); //no encoding conversion yet e2_cl_options.trash_dir = _e2_cl_option_get_path (optarg); break; default: printd (ERROR, "unknown option with code: 0%o and value: %s", c, optarg); break; } } else //no argument, or blank { switch (c) { case 'i': e2_cl_options.ignore_problems = TRUE; break; case 'l': e2_cl_options.suppress_gtk_log = FALSE; case 'm': e2_cl_options.detached = TRUE; break; case 'o': e2_cl_options.original = TRUE; break; case 'u': e2_cl_options.session_user = TRUE; break; #ifdef DEBUG_MESSAGES case 'x': e2_cl_options.verbose = TRUE; break; #endif case 'b': _e2_cl_option_print_build (); exit (0); break; case 'h': _e2_cl_option_print_help (); exit (0); break; case 'v': _e2_cl_option_print_version (); exit (0); break; case '?': printd (ERROR, "unknown option"); //ignore exit (0); break; default: printd (ERROR, "unknown option with code: 0%o", c); break; } } } if (optind < argc) { gchar *path = _e2_cl_option_get_path (argv[optind]); #ifdef E2_VFS VPATH ddata = { path, NULL }; if (e2_fs_path_exists (&ddata)) #else if (e2_fs_path_exists (path)) #endif { e2_cl_options.force_path = TRUE; } else { //maybe the path applies to a file, not a directory freeme = path; path = g_path_get_dirname (path); g_free (freeme); #ifdef E2_VFS ddata.path = path; if (e2_fs_path_exists (&ddata)) #else if (e2_fs_path_exists (path)) #endif { e2_cl_options.force_path = TRUE; } } if (e2_cl_options.force_path) { #ifdef DEBUG_MESSAGES d++; #endif optind++; g_free (e2_cl_options.pane2_path); e2_cl_options.pane2_path = path; } if (optind < argc) { g_free (cwd); g_free (path); e2_cl_option_clear (); printf (_("Startup options must begin with \"-\" or \"--\", or be a valid file-system path\n")); exit (1); //FIXME do a full clean exit } } printd (DEBUG, "parsed %d command line options", d); g_free (cwd); //encodings may be needed for other-option conversions const gchar **encodings; g_get_filename_charsets (&encodings); if (e2_cl_options.encoding == NULL) { if (encodings[0] != NULL && *encodings[0] != '\0') e2_cl_options.encoding = g_strdup (encodings[0]); else e2_cl_options.encoding = g_strdup ("ISO-8859-15"); } if (e2_cl_options.fallback_encoding == NULL) { if (encodings[1] != NULL && *encodings[1] != '\0') e2_cl_options.fallback_encoding = g_strdup (encodings[1]); else if (strcmp (e2_cl_options.encoding,"ISO-8859-1") == 0 || strcmp (e2_cl_options.encoding,"ISO-8859-15") == 0) e2_cl_options.fallback_encoding = g_strdup ("C"); else e2_cl_options.fallback_encoding = g_strdup ("ISO-8859-15"); } //now we setup any missing default strings gboolean subdir; const gchar *usedir; const gchar *homedir = g_getenv ("HOME"); if (e2_cl_options.config_dir != NULL && *e2_cl_options.config_dir != '\0') { usedir = e2_cl_options.config_dir; subdir = FALSE; } else { usedir = g_getenv ("XDG_CONFIG_HOME"); if (usedir == NULL || *usedir == '\0') usedir = g_get_user_config_dir (); subdir = TRUE; } //check dir is suitable (su etc may not adjust variable) if (usedir != NULL && *usedir != '\0') { if (homedir != NULL && *homedir != '\0') { if (!g_str_has_prefix (usedir, homedir)) usedir = NULL; } else usedir = NULL; } if (usedir == NULL || *usedir == '\0') freeme = g_build_filename (g_get_home_dir (), ".config", BINNAME, NULL); else if (subdir) freeme = g_build_filename (usedir, BINNAME, NULL); else { freeme = (gchar*)usedir; printd (DEBUG, "setting config directory '%s'", freeme); } g_free (e2_cl_options.config_dir); //if any //#ifdef E2_FILES_UTF8ONLY // e2_cl_options.config_dir = freeme; //#else e2_cl_options.config_dir = e2_utf8_filename_from_locale (freeme); if (freeme != usedir) g_free (freeme); //#endif if (e2_cl_options.sharedconfig_dir != NULL) { //minimal sanity check #ifdef E2_VFS VPATH data = { e2_cl_options.sharedconfig_dir, NULL }; //local config data only if (e2_fs_is_dir3 (&data E2_ERR_NONE()) && (!e2_fs_access (&data, R_OK | X_OK E2_ERR_NONE()))) #else if (e2_fs_is_dir3 (e2_cl_options.sharedconfig_dir E2_ERR_NONE()) && (!e2_fs_access (e2_cl_options.sharedconfig_dir, R_OK E2_ERR_NONE()))) #endif { freeme = e2_cl_options.config_dir; e2_cl_options.config_dir = e2_utf8_filename_from_locale (freeme); g_free (freeme); } else { g_free (e2_cl_options.config_dir); e2_cl_options.config_dir = NULL; } } //setup local default trash dir (any others done elsewhere) if (e2_cl_options.trash_dir != NULL && *e2_cl_options.trash_dir != '\0') usedir = e2_cl_options.trash_dir; else { usedir = g_getenv ("XDG_DATA_HOME"); if (usedir == NULL || *usedir == '\0') usedir = g_get_user_data_dir (); } if (usedir != NULL && *usedir != '\0') { if (homedir != NULL && *homedir != '\0') { if (!g_str_has_prefix (usedir, homedir)) usedir = NULL; } else usedir = NULL; } if (usedir == NULL || *usedir == '\0') freeme = g_build_filename (g_get_home_dir (), ".local", "share", "Trash", NULL); else { freeme = g_build_filename (usedir, "Trash", NULL); printd (DEBUG, "setting trash directory '%s'", freeme); } g_free (e2_cl_options.trash_dir); //if any //#ifdef E2_FILES_UTF8ONLY // e2_cl_options.trash_dir = freeme; //#else e2_cl_options.trash_dir = e2_utf8_filename_from_locale (freeme); g_free (freeme); //#endif }
int main(int argc, char *argv[]) { EventdCoreContext *context; gchar *runtime_dir = NULL; gchar *control_socket = NULL; gchar **binds = NULL; gboolean take_over_socket = FALSE; gboolean enable_relay = TRUE; gboolean enable_sd_modules = TRUE; gchar *config_dir = NULL; gboolean daemonize = FALSE; gboolean print_paths = FALSE; gboolean print_version = FALSE; EventdReturnCode retval = EVENTD_RETURN_CODE_OK; GError *error = NULL; GOptionContext *option_context = NULL; #ifdef EVENTD_DEBUG g_setenv("G_MESSAGES_DEBUG", "all", FALSE); #endif /* EVENTD_DEBUG */ setlocale(LC_ALL, ""); #ifdef ENABLE_NLS bindtextdomain(GETTEXT_PACKAGE, EVENTD_LOCALEDIR); bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); #endif /* ENABLE_NLS */ if ( ! g_get_filename_charsets(NULL) ) { g_warning(PACKAGE_NAME " does not support non-UTF-8 filename encoding"); return EVENTD_RETURN_CODE_FILESYSTEM_ENCODING_ERROR; } if ( G_UNLIKELY(! g_module_supported()) ) { g_warning("No loadable module support"); return EVENTD_RETURN_CODE_NO_MODULE_SUPPORT_ERROR; } #ifdef EVENTD_DEBUG setvbuf(stdout, NULL, _IOLBF, 0); const gchar *debug_log_filename = g_getenv("EVENTD_DEBUG_LOG_FILENAME"); GDataOutputStream *debug_stream = NULL; if ( debug_log_filename != NULL ) { GFile *debug_log; debug_log = g_file_new_for_path(debug_log_filename); GError *error = NULL; GFileOutputStream *debug_log_stream; debug_log_stream = g_file_append_to(debug_log, G_FILE_CREATE_NONE, NULL, &error); if ( debug_log_stream == NULL ) { g_warning("Couldn't open debug log file: %s", error->message); g_clear_error(&error); } else { debug_stream = g_data_output_stream_new(G_OUTPUT_STREAM(debug_log_stream)); g_object_unref(debug_log_stream); g_log_set_default_handler(_eventd_core_debug_log_handler, debug_stream); } g_object_unref(debug_log); } #endif /* EVENTD_DEBUG */ context = g_new0(EventdCoreContext, 1); context->interface.get_sockets = eventd_core_get_sockets; context->interface.push_event = eventd_core_push_event; #ifdef G_OS_UNIX context->system_mode = ( g_getenv("XDG_RUNTIME_DIR") == NULL ); if ( context->system_mode ) g_setenv("XDG_RUNTIME_DIR", "/run", TRUE); #endif /* G_OS_UNIX */ GOptionEntry entries[] = { { "private-socket", 'i', 0, G_OPTION_ARG_FILENAME, &control_socket, "Socket to listen for internal control", "<socket>" }, { "listen", 'l', 0, G_OPTION_ARG_STRING_ARRAY, &binds, "Add a socket to listen to", "<socket>" }, { "take-over", 't', GIO_UNIX_OPTION_FLAG, G_OPTION_ARG_NONE, &take_over_socket, "Take over socket", NULL }, { "no-relay", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &enable_relay, "Disable the relay feature", NULL }, { "no-service-discovery", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &enable_sd_modules, "Disable the service discovery feature", NULL }, { "config-dir", 'c', 0, G_OPTION_ARG_FILENAME, &config_dir, "Additionnal configuration directory", "<directory>" }, { "daemonize", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &daemonize, NULL, NULL }, { "paths", 'P', 0, G_OPTION_ARG_NONE, &print_paths, "Print search paths", NULL }, { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version, "Print version", NULL }, { .long_name = NULL } };