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
|
|
|
**
|
2013-03-30 10:32:07 +01:00
|
|
|
** Copyright (C) 2008-2013 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) {
|
2010-12-04 18:08:23 +01:00
|
|
|
/* g_debug ("%s: path is empty", __FUNCTION__); */
|
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) {
|
2010-10-09 11:44:48 +02:00
|
|
|
/* g_debug ("%s: expansion failed for %s", __FUNCTION__, 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", */
|
|
|
|
/* __FUNCTION__, 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
|
|
|
}
|
|
|
|
|
2011-01-09 17:55:54 +01:00
|
|
|
|
|
|
|
char*
|
|
|
|
mu_util_create_tmpdir (void)
|
|
|
|
{
|
|
|
|
gchar *dirname;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-06-01 20:52:58 +02:00
|
|
|
dirname = g_strdup_printf ("%s%cmu-%d%c%x",
|
|
|
|
g_get_tmp_dir(),
|
2011-01-09 17:55:54 +01:00
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
getuid(),
|
|
|
|
G_DIR_SEPARATOR,
|
|
|
|
(int)random()*getpid()*(int)time(NULL));
|
|
|
|
|
2011-08-09 06:53:50 +02:00
|
|
|
if (!mu_util_create_dir_maybe (dirname, 0700, FALSE)) {
|
2011-01-09 17:55:54 +01:00
|
|
|
g_free (dirname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-01-10 23:44:03 +01:00
|
|
|
snprintf (cachedir, sizeof(cachedir), "%s%cmu-%u",
|
|
|
|
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)
|
|
|
|
{
|
2012-03-19 19:53:18 +01: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
|
|
|
|
|
|
|
|
2010-11-02 20:12:18 +01:00
|
|
|
gchar*
|
|
|
|
mu_util_guess_mu_homedir (void)
|
|
|
|
{
|
2012-03-19 19:53:18 +01:00
|
|
|
const char* home;
|
2010-11-02 20:12:18 +01:00
|
|
|
|
2012-03-19 19:53:18 +01:00
|
|
|
/* g_get_home_dir use /etc/passwd, not $HOME; this is better,
|
|
|
|
* as HOME may be wrong when using 'sudo' etc.*/
|
|
|
|
home = g_get_home_dir ();
|
2010-11-02 20:12:18 +01:00
|
|
|
|
2012-03-19 19:53:18 +01:00
|
|
|
if (!home) {
|
|
|
|
MU_WRITE_LOG ("failed to determine homedir");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2012-03-19 19:53:18 +01:00
|
|
|
return g_strdup_printf ("%s%c%s", home ? home : ".",
|
|
|
|
G_DIR_SEPARATOR, ".mu");
|
2010-11-02 20:12:18 +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
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_util_str_from_strv (const gchar **params)
|
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
int i;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
g_return_val_if_fail (params, NULL);
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
if (!params[0])
|
|
|
|
return g_strdup ("");
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
str = g_string_sized_new (64); /* just a guess */
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
for (i = 0; params[i]; ++i) {
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2011-11-23 23:12:29 +01:00
|
|
|
if (i > 0)
|
2010-02-03 20:06:31 +01:00
|
|
|
g_string_append_c (str, ' ');
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
g_string_append (str, params[i]);
|
2011-08-30 21:02:49 +02:00
|
|
|
}
|
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
return g_string_free (str, FALSE);
|
|
|
|
}
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2011-11-23 23:12:29 +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
|
|
|
|
|
|
|
|
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,
|
|
|
|
G_SPAWN_SEARCH_PATH, NULL, NULL, NULL,
|
2012-04-15 18:59:53 +02:00
|
|
|
err);
|
2011-01-09 13:55:25 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
unsigned char
|
|
|
|
mu_util_get_dtype_with_lstat (const char *path)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
if (lstat (path, &statbuf) != 0) {
|
2010-12-12 17:32:37 +01:00
|
|
|
g_warning ("stat failed on %s: %s", 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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int rv;
|
2013-09-07 17:39:00 +02:00
|
|
|
unsigned bytes;
|
|
|
|
char *conv;
|
2011-08-30 21:02:49 +02:00
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
g_return_val_if_fail (str, FALSE);
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* charset is _not_ utf8, so we actually have to convert
|
|
|
|
* it
|
|
|
|
*/
|
|
|
|
conv = NULL;
|
|
|
|
if (g_utf8_validate (str, -1, NULL))
|
|
|
|
/* it _seems_ that on the bsds, the final err param
|
|
|
|
* may receive garbage... so we don't use it */
|
|
|
|
conv = g_locale_from_utf8
|
|
|
|
(str, -1, (gsize*)&bytes, 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
|
|
|
|
*/
|
|
|
|
if (!conv)
|
|
|
|
conv = g_strescape (str, "\n\t");
|
|
|
|
|
|
|
|
rv = conv ? fputs (conv, stream) : EOF;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-31 22:17:09 +02:00
|
|
|
static gboolean
|
|
|
|
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
|
|
|
}
|
2012-12-25 15:45:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_util_get_hash (const char* str)
|
|
|
|
{
|
|
|
|
unsigned djbhash, bkdrhash, bkdrseed;
|
|
|
|
unsigned u;
|
|
|
|
static char hex[18];
|
|
|
|
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
|
|
|
|
djbhash = 5381;
|
|
|
|
bkdrhash = 0;
|
|
|
|
bkdrseed = 1313;
|
|
|
|
|
|
|
|
for(u = 0; str[u]; ++u) {
|
|
|
|
djbhash = ((djbhash << 5) + djbhash) + str[u];
|
|
|
|
bkdrhash = bkdrhash * bkdrseed + str[u];
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash);
|
|
|
|
|
|
|
|
return hex;
|
|
|
|
}
|