2011-05-21 13:11:23 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
2011-01-02 17:45:16 +01:00
|
|
|
/*
|
2011-08-30 21:02:49 +02:00
|
|
|
**
|
2016-12-08 02:55:28 +01:00
|
|
|
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** 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 3 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
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2011-01-09 13:55:25 +01:00
|
|
|
#if HAVE_CONFIG_H
|
2010-10-09 11:44:48 +02:00
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
|
|
|
#include "mu-util.h"
|
2010-09-22 23:03:04 +02:00
|
|
|
#define _XOPEN_SOURCE 500
|
2012-11-08 21:19:28 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_WORDEXP_H
|
2009-11-29 23:03:03 +01:00
|
|
|
#include <wordexp.h> /* for shell-style globbing */
|
2012-11-08 21:20:31 +01:00
|
|
|
#endif /*HAVE_WORDEXP_H*/
|
2012-11-08 21:19:28 +01:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
#include <stdlib.h>
|
2010-09-22 23:03:04 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
#include <string.h>
|
2010-09-08 23:12:06 +02:00
|
|
|
#include <locale.h> /* for setlocale() */
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
#include <fcntl.h>
|
2009-12-09 18:53:30 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-09-08 23:12:06 +02:00
|
|
|
#include <glib-object.h>
|
2010-01-04 19:15:49 +01:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <errno.h>
|
2009-12-09 18:53:30 +01:00
|
|
|
|
2011-06-01 20:52:58 +02:00
|
|
|
#include <langinfo.h>
|
|
|
|
|
2012-11-08 21:19:28 +01:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
static char*
|
|
|
|
do_wordexp (const char *path)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2012-11-08 21:19:28 +01:00
|
|
|
#ifdef HAVE_WORDEXP_H
|
2009-11-29 23:03:03 +01:00
|
|
|
wordexp_t wexp;
|
|
|
|
char *dir;
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
if (!path) {
|
2015-04-22 20:06:31 +02:00
|
|
|
/* g_debug ("%s: path is empty", __func__); */
|
2010-10-09 10:56:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
if (wordexp (path, &wexp, 0) != 0) {
|
2015-04-22 20:06:31 +02:00
|
|
|
/* g_debug ("%s: expansion failed for %s", __func__, path); */
|
2010-07-26 11:12:43 +02:00
|
|
|
return NULL;
|
2010-10-09 10:56:55 +02:00
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
/* we just pick the first one */
|
2010-10-09 10:56:55 +02:00
|
|
|
dir = g_strdup (wexp.we_wordv[0]);
|
2009-11-29 23:03:03 +01:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
/* strangely, below seems to lead to a crash on MacOS (BSD);
|
2010-07-30 19:56:50 +02:00
|
|
|
so we have to allow for a tiny leak here on that
|
|
|
|
platform... maybe instead of __APPLE__ it should be
|
2013-08-25 19:05:57 +02:00
|
|
|
__BSD__?
|
|
|
|
|
|
|
|
Hmmm., cannot reproduce that crash anymore, so commenting
|
|
|
|
it out for now...
|
|
|
|
*/
|
|
|
|
/* #ifndef __APPLE__ */
|
2010-07-30 19:56:50 +02:00
|
|
|
wordfree (&wexp);
|
2013-08-25 19:05:57 +02:00
|
|
|
/* #endif /\*__APPLE__*\/ */
|
2010-10-09 10:56:55 +02:00
|
|
|
return dir;
|
|
|
|
|
2012-11-08 21:19:28 +01:00
|
|
|
# else /*!HAVE_WORDEXP_H*/
|
|
|
|
/* E.g. OpenBSD does not have wordexp.h, so we ignore it */
|
|
|
|
return path ? g_strdup (path) : NULL;
|
|
|
|
#endif /*HAVE_WORDEXP_H*/
|
|
|
|
}
|
2010-10-09 10:56:55 +02:00
|
|
|
|
2011-07-20 23:35:14 +02:00
|
|
|
|
2010-10-09 11:44:48 +02:00
|
|
|
/* note, the g_debugs are commented out because this function may be
|
|
|
|
* called before the log handler is installed. */
|
2010-10-09 10:56:55 +02:00
|
|
|
char*
|
|
|
|
mu_util_dir_expand (const char *path)
|
|
|
|
{
|
|
|
|
char *dir;
|
|
|
|
char resolved[PATH_MAX + 1];
|
2010-12-04 18:08:23 +01:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
g_return_val_if_fail (path, NULL);
|
|
|
|
|
|
|
|
dir = do_wordexp (path);
|
|
|
|
if (!dir)
|
|
|
|
return NULL; /* error */
|
2010-12-04 18:14:31 +01:00
|
|
|
|
|
|
|
/* don't try realpath if the dir does not exist */
|
|
|
|
if (access (dir, F_OK) != 0)
|
|
|
|
return dir;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-12-04 18:08:23 +01:00
|
|
|
/* now resolve any symlinks, .. etc. */
|
|
|
|
if (realpath (dir, resolved) == NULL) {
|
2012-01-19 18:24:46 +01:00
|
|
|
/* g_debug ("%s: could not get realpath for '%s': %s", */
|
2015-04-22 20:06:31 +02:00
|
|
|
/* __func__, dir, strerror(errno)); */
|
2010-09-22 23:03:04 +02:00
|
|
|
g_free (dir);
|
|
|
|
return NULL;
|
2011-08-30 21:02:49 +02:00
|
|
|
} else
|
2010-09-22 23:03:04 +02:00
|
|
|
g_free (dir);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-10-09 10:56:55 +02:00
|
|
|
return g_strdup (resolved);
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2012-04-01 12:08:02 +02:00
|
|
|
GQuark
|
|
|
|
mu_util_error_quark (void)
|
|
|
|
{
|
|
|
|
static GQuark error_domain = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY(error_domain == 0))
|
|
|
|
error_domain = g_quark_from_static_string
|
|
|
|
("mu-error-quark");
|
|
|
|
|
|
|
|
return error_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-10 23:44:03 +01:00
|
|
|
const char*
|
|
|
|
mu_util_cache_dir (void)
|
|
|
|
{
|
|
|
|
static char cachedir [PATH_MAX];
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2020-01-07 22:15:22 +01:00
|
|
|
g_snprintf (cachedir, sizeof(cachedir), "%s%cmu-%u",
|
2011-01-10 23:44:03 +01:00
|
|
|
g_get_tmp_dir(), G_DIR_SEPARATOR,
|
|
|
|
getuid());
|
|
|
|
|
|
|
|
return cachedir;
|
|
|
|
}
|
|
|
|
|
2011-01-09 17:55:54 +01:00
|
|
|
|
2010-01-06 00:18:24 +01:00
|
|
|
gboolean
|
2010-02-03 20:06:31 +01:00
|
|
|
mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable)
|
2009-12-09 18:53:30 +01:00
|
|
|
{
|
2010-09-26 16:29:54 +02:00
|
|
|
int mode;
|
2009-12-09 18:53:30 +01:00
|
|
|
struct stat statbuf;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
|
|
|
if (!path)
|
2010-01-06 00:18:24 +01:00
|
|
|
return FALSE;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-01-06 00:18:24 +01:00
|
|
|
mode = F_OK | (readable ? R_OK : 0) | (writeable ? W_OK : 0);
|
2009-12-09 18:53:30 +01:00
|
|
|
|
2010-01-06 00:18:24 +01:00
|
|
|
if (access (path, mode) != 0) {
|
2012-01-19 18:24:46 +01:00
|
|
|
/* g_debug ("Cannot access %s: %s", path, strerror (errno)); */
|
2010-01-06 00:18:24 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat (path, &statbuf) != 0) {
|
2012-01-19 18:24:46 +01:00
|
|
|
/* g_debug ("Cannot stat %s: %s", path, strerror (errno)); */
|
2010-01-06 00:18:24 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-01-25 12:27:02 +01:00
|
|
|
return S_ISDIR(statbuf.st_mode) ? TRUE: FALSE;
|
2009-12-09 18:53:30 +01:00
|
|
|
}
|
|
|
|
|
2010-01-06 00:18:24 +01:00
|
|
|
|
2009-12-09 18:53:30 +01:00
|
|
|
gchar*
|
|
|
|
mu_util_guess_maildir (void)
|
|
|
|
{
|
2016-10-25 19:34:08 +02:00
|
|
|
const gchar *mdir1, *home;
|
|
|
|
|
|
|
|
/* first, try MAILDIR */
|
|
|
|
mdir1 = g_getenv ("MAILDIR");
|
|
|
|
|
|
|
|
if (mdir1 && mu_util_check_dir (mdir1, TRUE, FALSE))
|
|
|
|
return g_strdup (mdir1);
|
|
|
|
|
|
|
|
/* then, try <home>/Maildir */
|
|
|
|
home = g_get_home_dir();
|
|
|
|
if (home) {
|
|
|
|
char *mdir2;
|
|
|
|
mdir2 = g_strdup_printf ("%s%cMaildir",
|
|
|
|
home, G_DIR_SEPARATOR);
|
|
|
|
if (mu_util_check_dir (mdir2, TRUE, FALSE))
|
|
|
|
return mdir2;
|
|
|
|
g_free (mdir2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nope; nothing found */
|
|
|
|
return NULL;
|
2009-12-09 18:53:30 +01:00
|
|
|
}
|
2010-01-04 19:15:49 +01:00
|
|
|
|
|
|
|
gboolean
|
2011-08-09 06:53:50 +02:00
|
|
|
mu_util_create_dir_maybe (const gchar *path, mode_t mode, gboolean nowarn)
|
2010-01-04 19:15:49 +01:00
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-01-04 19:15:49 +01:00
|
|
|
g_return_val_if_fail (path, FALSE);
|
|
|
|
|
2011-08-30 21:02:49 +02:00
|
|
|
/* if it exists, it must be a readable dir */
|
2010-01-04 19:15:49 +01:00
|
|
|
if (stat (path, &statbuf) == 0) {
|
|
|
|
if ((!S_ISDIR(statbuf.st_mode)) ||
|
|
|
|
(access (path, W_OK|R_OK) != 0)) {
|
2011-08-09 06:53:50 +02:00
|
|
|
if (!nowarn)
|
|
|
|
g_warning ("not a read-writable"
|
|
|
|
"directory: %s", path);
|
2010-01-04 19:15:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
}
|
|
|
|
|
2011-01-10 23:44:03 +01:00
|
|
|
if (g_mkdir_with_parents (path, mode) != 0) {
|
2011-08-09 06:53:50 +02:00
|
|
|
if (!nowarn)
|
|
|
|
g_warning ("failed to create %s: %s",
|
|
|
|
path, strerror(errno));
|
2010-01-04 19:15:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2010-02-03 20:06:31 +01:00
|
|
|
|
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
int
|
2011-01-09 13:55:25 +01:00
|
|
|
mu_util_create_writeable_fd (const char* path, mode_t mode,
|
2010-08-29 21:13:06 +02:00
|
|
|
gboolean overwrite)
|
2010-08-22 21:50:19 +02:00
|
|
|
{
|
|
|
|
errno = 0; /* clear! */
|
2011-01-09 13:55:25 +01:00
|
|
|
g_return_val_if_fail (path, -1);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
if (overwrite)
|
2011-05-21 13:11:23 +02:00
|
|
|
return open (path, O_WRONLY|O_CREAT|O_TRUNC, mode);
|
2010-08-22 21:50:19 +02:00
|
|
|
else
|
2011-05-21 13:11:23 +02:00
|
|
|
return open (path, O_WRONLY|O_CREAT|O_EXCL, mode);
|
2010-08-22 21:50:19 +02:00
|
|
|
}
|
2010-08-29 21:13:06 +02:00
|
|
|
|
2011-01-10 23:44:03 +01:00
|
|
|
|
2011-01-09 13:55:25 +01:00
|
|
|
gboolean
|
2011-01-11 22:21:24 +01:00
|
|
|
mu_util_is_local_file (const char* path)
|
|
|
|
{
|
|
|
|
/* if it starts with file:// it's a local file (for the
|
|
|
|
* purposes of this function -- if it's on a remote FS it's
|
|
|
|
* still considered local) */
|
|
|
|
if (g_ascii_strncasecmp ("file://", path, strlen("file://")) == 0)
|
|
|
|
return TRUE;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-01-11 22:21:24 +01:00
|
|
|
if (access (path, R_OK) == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2011-08-30 21:02:49 +02:00
|
|
|
return FALSE;
|
2011-01-11 22:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 11:11:43 +02:00
|
|
|
gboolean
|
|
|
|
mu_util_supports (MuFeature feature)
|
|
|
|
{
|
|
|
|
/* check for Guile support */
|
|
|
|
#ifndef BUILD_GUILE
|
|
|
|
if (feature & MU_FEATURE_GUILE)
|
|
|
|
return FALSE;
|
|
|
|
#endif /*BUILD_GUILE*/
|
|
|
|
|
|
|
|
/* check for Gnuplot */
|
|
|
|
if (feature & MU_FEATURE_GNUPLOT)
|
|
|
|
if (!mu_util_program_in_path ("gnuplot"))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_program_in_path (const char *prog)
|
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
|
|
|
|
g_return_val_if_fail (prog, FALSE);
|
|
|
|
|
|
|
|
path = g_find_program_in_path (prog);
|
|
|
|
g_free (path);
|
|
|
|
|
|
|
|
return (path != NULL) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:11:23 +01:00
|
|
|
|
2016-12-08 02:55:28 +01:00
|
|
|
/*
|
|
|
|
* Set the child to a group leader to avoid being killed when the
|
|
|
|
* parent group is killed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
maybe_setsid (G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
#if HAVE_SETSID
|
|
|
|
setsid();
|
|
|
|
#endif /*HAVE_SETSID*/
|
|
|
|
}
|
2012-01-22 23:11:23 +01:00
|
|
|
|
2011-01-11 22:21:24 +01:00
|
|
|
gboolean
|
2012-04-15 18:59:53 +02:00
|
|
|
mu_util_play (const char *path, gboolean allow_local, gboolean allow_remote,
|
|
|
|
GError **err)
|
2011-01-09 13:55:25 +01:00
|
|
|
{
|
|
|
|
gboolean rv;
|
2012-04-15 18:59:53 +02:00
|
|
|
const gchar *argv[3];
|
|
|
|
const char *prog;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-01-09 13:55:25 +01:00
|
|
|
g_return_val_if_fail (path, FALSE);
|
2011-01-11 22:21:24 +01:00
|
|
|
g_return_val_if_fail (mu_util_is_local_file (path) || allow_remote,
|
|
|
|
FALSE);
|
|
|
|
g_return_val_if_fail (!mu_util_is_local_file (path) || allow_local,
|
|
|
|
FALSE);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2012-01-22 23:11:23 +01:00
|
|
|
prog = g_getenv ("MU_PLAY_PROGRAM");
|
|
|
|
if (!prog) {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
prog = "open";
|
|
|
|
#else
|
|
|
|
prog = "xdg-open";
|
|
|
|
#endif /*!__APPLE__*/
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2012-10-18 11:11:43 +02:00
|
|
|
if (!mu_util_program_in_path (prog)) {
|
|
|
|
mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_EXECUTE,
|
|
|
|
"cannot find '%s' in path", prog);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-04-15 18:59:53 +02:00
|
|
|
argv[0] = prog;
|
|
|
|
argv[1] = path;
|
|
|
|
argv[2] = NULL;
|
2012-03-07 23:12:06 +01:00
|
|
|
|
2012-01-22 23:11:23 +01:00
|
|
|
err = NULL;
|
2012-10-18 11:11:43 +02:00
|
|
|
rv = g_spawn_async (NULL, (gchar**)&argv, NULL,
|
2016-12-08 02:55:28 +01:00
|
|
|
G_SPAWN_SEARCH_PATH, maybe_setsid,
|
|
|
|
NULL, NULL, err);
|
2011-01-09 13:55:25 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
unsigned char
|
2020-05-26 18:07:56 +02:00
|
|
|
mu_util_get_dtype (const char *path, gboolean use_lstat)
|
2010-12-11 12:52:03 +01:00
|
|
|
{
|
2020-05-26 18:07:56 +02:00
|
|
|
int res;
|
2010-12-11 12:52:03 +01:00
|
|
|
struct stat statbuf;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
g_return_val_if_fail (path, DT_UNKNOWN);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2020-05-26 18:07:56 +02:00
|
|
|
if (use_lstat)
|
|
|
|
res = lstat (path, &statbuf);
|
|
|
|
else
|
|
|
|
res = stat (path, &statbuf);
|
|
|
|
|
|
|
|
if (res != 0) {
|
|
|
|
g_warning ("%sstat failed on %s: %s",
|
|
|
|
use_lstat ? "l" : "", path, strerror(errno));
|
2010-12-11 12:52:03 +01:00
|
|
|
return DT_UNKNOWN;
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
/* we only care about dirs, regular files and links */
|
|
|
|
if (S_ISREG (statbuf.st_mode))
|
|
|
|
return DT_REG;
|
|
|
|
else if (S_ISDIR (statbuf.st_mode))
|
|
|
|
return DT_DIR;
|
|
|
|
else if (S_ISLNK (statbuf.st_mode))
|
|
|
|
return DT_LNK;
|
|
|
|
|
|
|
|
return DT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2010-08-29 21:13:06 +02:00
|
|
|
|
2020-05-26 18:07:56 +02:00
|
|
|
|
2011-06-01 20:52:58 +02:00
|
|
|
gboolean
|
|
|
|
mu_util_locale_is_utf8 (void)
|
|
|
|
{
|
|
|
|
const gchar *dummy;
|
|
|
|
static int is_utf8 = -1;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
|
|
|
if (G_UNLIKELY(is_utf8 == -1))
|
2011-06-01 20:52:58 +02:00
|
|
|
is_utf8 = g_get_charset(&dummy) ? 1 : 0;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-06-01 20:52:58 +02:00
|
|
|
return is_utf8 ? TRUE : FALSE;
|
|
|
|
}
|
2011-05-31 22:17:09 +02:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_fputs_encoded (const char *str, FILE *stream)
|
|
|
|
{
|
2016-11-16 19:23:03 +01:00
|
|
|
int rv;
|
|
|
|
char *conv;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
g_return_val_if_fail (stream, FALSE);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:23:50 +02:00
|
|
|
/* g_get_charset return TRUE when the locale is UTF8 */
|
2011-08-30 21:02:49 +02:00
|
|
|
if (mu_util_locale_is_utf8())
|
2013-09-07 17:39:00 +02:00
|
|
|
return fputs (str, stream) == EOF ? FALSE : TRUE;
|
|
|
|
|
2016-11-16 19:23:03 +01:00
|
|
|
/* charset is _not_ utf8, so we need to convert it */
|
2013-09-07 17:39:00 +02:00
|
|
|
conv = NULL;
|
|
|
|
if (g_utf8_validate (str, -1, NULL))
|
2016-11-16 19:23:03 +01:00
|
|
|
conv = g_locale_from_utf8 (str, -1, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
/* conversion failed; this happens because is some cases GMime may gives
|
|
|
|
* us non-UTF-8 strings from e.g. wrongly encoded message-subjects; if
|
|
|
|
* so, we escape the string */
|
|
|
|
conv = conv ? conv : g_strescape (str, "\n\t");
|
|
|
|
rv = conv ? fputs (conv, stream) : EOF;
|
2013-09-07 17:39:00 +02:00
|
|
|
g_free (conv);
|
2013-08-14 20:59:31 +02:00
|
|
|
|
2012-06-26 12:11:50 +02:00
|
|
|
return (rv == EOF) ? FALSE : TRUE;
|
2011-05-31 22:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-29 19:51:29 +01:00
|
|
|
|
2012-05-22 09:19:49 +02:00
|
|
|
gboolean
|
2012-04-13 20:58:58 +02:00
|
|
|
mu_util_g_set_error (GError **err, MuError errcode, const char *frm, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
/* don't bother with NULL errors, or errors already set */
|
|
|
|
if (!err || *err)
|
2012-05-22 15:49:57 +02:00
|
|
|
return FALSE;
|
2012-04-13 20:58:58 +02:00
|
|
|
|
|
|
|
msg = NULL;
|
|
|
|
va_start (ap, frm);
|
|
|
|
g_vasprintf (&msg, frm, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, errcode, "%s", msg);
|
|
|
|
|
|
|
|
g_free (msg);
|
2012-05-22 09:19:49 +02:00
|
|
|
|
|
|
|
return FALSE;
|
2012-04-13 20:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-11 17:33:31 +01:00
|
|
|
|
2019-12-30 21:28:53 +01:00
|
|
|
__attribute__((format(printf, 2, 0))) static gboolean
|
2011-05-31 22:17:09 +02:00
|
|
|
print_args (FILE *stream, const char *frm, va_list args)
|
|
|
|
{
|
|
|
|
gchar *str;
|
|
|
|
gboolean rv;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
str = g_strdup_vprintf (frm, args);
|
|
|
|
|
|
|
|
rv = mu_util_fputs_encoded (str, stream);
|
|
|
|
|
|
|
|
g_free (str);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_print_encoded (const char *frm, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (frm, FALSE);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
va_start (args, frm);
|
|
|
|
rv = print_args (stdout, frm, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_util_printerr_encoded (const char *frm, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean rv;
|
|
|
|
|
|
|
|
g_return_val_if_fail (frm, FALSE);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
va_start (args, frm);
|
|
|
|
rv = print_args (stderr, frm, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2012-09-13 21:56:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_util_read_password (const char *prompt)
|
|
|
|
{
|
2012-09-14 11:16:53 +02:00
|
|
|
char *pass;
|
2012-09-13 21:56:08 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (prompt, NULL);
|
|
|
|
|
|
|
|
/* note: getpass is obsolete; replace with something better */
|
|
|
|
|
2012-09-14 11:16:53 +02:00
|
|
|
pass = getpass (prompt); /* returns static mem, don't free */
|
|
|
|
if (!pass) {
|
2012-09-13 21:56:08 +02:00
|
|
|
if (errno)
|
|
|
|
g_warning ("error: %s", strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:16:53 +02:00
|
|
|
return g_strdup (pass);
|
2012-09-13 21:56:08 +02:00
|
|
|
}
|
2020-05-27 23:37:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Pick g_canonicalize_file name from glib >= 2.58 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_canonicalize_filename:
|
|
|
|
* @filename: (type filename): the name of the file
|
|
|
|
* @relative_to: (type filename) (nullable): the relative directory, or %NULL
|
|
|
|
* to use the current working directory
|
|
|
|
*
|
|
|
|
* Gets the canonical file name from @filename. All triple slashes are turned into
|
|
|
|
* single slashes, and all `..` and `.`s resolved against @relative_to.
|
|
|
|
*
|
|
|
|
* Symlinks are not followed, and the returned path is guaranteed to be absolute.
|
|
|
|
*
|
|
|
|
* If @filename is an absolute path, @relative_to is ignored. Otherwise,
|
|
|
|
* @relative_to will be prepended to @filename to make it absolute. @relative_to
|
|
|
|
* must be an absolute path, or %NULL. If @relative_to is %NULL, it'll fallback
|
|
|
|
* to g_get_current_dir().
|
|
|
|
*
|
|
|
|
* This function never fails, and will canonicalize file paths even if they don't
|
|
|
|
* exist.
|
|
|
|
*
|
|
|
|
* No file system I/O is done.
|
|
|
|
*
|
|
|
|
* Returns: (type filename) (transfer full): a newly allocated string with the
|
|
|
|
* canonical file path
|
|
|
|
* Since: 2.58
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
mu_canonicalize_filename (const gchar *filename,
|
2020-06-27 16:00:57 +02:00
|
|
|
const gchar *relative_to)
|
2020-05-27 23:37:29 +02:00
|
|
|
{
|
|
|
|
gchar *canon, *start, *p, *q;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (relative_to == NULL || g_path_is_absolute (relative_to), NULL);
|
|
|
|
|
|
|
|
if (!g_path_is_absolute (filename))
|
|
|
|
{
|
|
|
|
gchar *cwd_allocated = NULL;
|
|
|
|
const gchar *cwd;
|
|
|
|
|
|
|
|
if (relative_to != NULL)
|
|
|
|
cwd = relative_to;
|
|
|
|
else
|
|
|
|
cwd = cwd_allocated = g_get_current_dir ();
|
|
|
|
|
|
|
|
canon = g_build_filename (cwd, filename, NULL);
|
|
|
|
g_free (cwd_allocated);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
canon = g_strdup (filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = (char *)g_path_skip_root (canon);
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
{
|
|
|
|
/* This shouldn't really happen, as g_get_current_dir() should
|
|
|
|
return an absolute pathname, but bug 573843 shows this is
|
|
|
|
not always happening */
|
|
|
|
g_free (canon);
|
|
|
|
return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* POSIX allows double slashes at the start to
|
|
|
|
* mean something special (as does windows too).
|
|
|
|
* So, "//" != "/", but more than two slashes
|
|
|
|
* is treated as "/".
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
for (p = start - 1;
|
|
|
|
(p >= canon) &&
|
|
|
|
G_IS_DIR_SEPARATOR (*p);
|
|
|
|
p--)
|
|
|
|
i++;
|
|
|
|
if (i > 2)
|
|
|
|
{
|
|
|
|
i -= 1;
|
|
|
|
start -= i;
|
|
|
|
memmove (start, start+i, strlen (start+i) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we're using the canonical dir separator */
|
|
|
|
p++;
|
|
|
|
while (p < start && G_IS_DIR_SEPARATOR (*p))
|
|
|
|
*p++ = G_DIR_SEPARATOR;
|
|
|
|
|
|
|
|
p = start;
|
|
|
|
while (*p != 0)
|
|
|
|
{
|
|
|
|
if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
|
|
|
|
{
|
|
|
|
memmove (p, p+1, strlen (p+1)+1);
|
|
|
|
}
|
|
|
|
else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
|
|
|
|
{
|
|
|
|
q = p + 2;
|
|
|
|
/* Skip previous separator */
|
|
|
|
p = p - 2;
|
|
|
|
if (p < start)
|
|
|
|
p = start;
|
|
|
|
while (p > start && !G_IS_DIR_SEPARATOR (*p))
|
|
|
|
p--;
|
|
|
|
if (G_IS_DIR_SEPARATOR (*p))
|
|
|
|
*p++ = G_DIR_SEPARATOR;
|
|
|
|
memmove (p, q, strlen (q)+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Skip until next separator */
|
|
|
|
while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p != 0)
|
|
|
|
{
|
|
|
|
/* Canonicalize one separator */
|
|
|
|
*p++ = G_DIR_SEPARATOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove additional separators */
|
|
|
|
q = p;
|
|
|
|
while (*q && G_IS_DIR_SEPARATOR (*q))
|
|
|
|
q++;
|
|
|
|
|
|
|
|
if (p != q)
|
|
|
|
memmove (p, q, strlen (q) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove trailing slashes */
|
|
|
|
if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
|
|
|
|
*(p-1) = 0;
|
|
|
|
|
|
|
|
return canon;
|
|
|
|
}
|