#include <dirent.h>
#include <time.h>
#include <regex.h>
+#include <sys/utsname.h>
#include "intl.h"
#include "utils.h"
gint str_case_equal(gconstpointer v, gconstpointer v2)
{
- return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
+ return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
}
guint str_case_hash(gconstpointer key)
}
}
+gboolean str_find(const gchar *haystack, const gchar *needle)
+{
+ return strstr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_case_find(const gchar *haystack, const gchar *needle)
+{
+ return strcasestr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_find_equal(const gchar *haystack, const gchar *needle)
+{
+ return strcmp(haystack, needle) == 0;
+}
+
+gboolean str_case_find_equal(const gchar *haystack, const gchar *needle)
+{
+ return strcasecmp(haystack, needle) == 0;
+}
+
gint to_number(const gchar *nstr)
{
register const guchar *p;
return NULL;
while (haystack_len >= needle_len) {
- if (!strncasecmp(haystack, needle, needle_len))
+ if (!g_ascii_strncasecmp(haystack, needle, needle_len))
return (gchar *)haystack;
else {
haystack++;
return NULL;
}
+gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
+ gconstpointer needle, size_t needlelen)
+{
+ const gchar *haystack_ = (const gchar *)haystack;
+ const gchar *needle_ = (const gchar *)needle;
+ const gchar *haystack_cur = (const gchar *)haystack;
+
+ if (needlelen == 1)
+ return memchr(haystack_, *needle_, haystacklen);
+
+ while ((haystack_cur = memchr(haystack_cur, *needle_, haystacklen))
+ != NULL) {
+ if (haystacklen - (haystack_cur - haystack_) < needlelen)
+ break;
+ if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
+ return (gpointer)haystack_cur;
+ else
+ haystack_cur++;
+ }
+
+ return NULL;
+}
+
/* Copy no more than N characters of SRC to DEST, with NULL terminating. */
gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
{
trim_subject_for_sort(str1);
trim_subject_for_sort(str2);
- return strcasecmp(str1, str2);
+ return g_utf8_collate(str1, str2);
}
void trim_subject_for_compare(gchar *str)
{
- guchar *srcp;
+ gchar *srcp;
eliminate_parenthesis(str, '[', ']');
eliminate_parenthesis(str, '(', ')');
void trim_subject_for_sort(gchar *str)
{
- guchar *srcp;
+ gchar *srcp;
g_strstrip(str);
void subst_for_filename(gchar *str)
{
- subst_chars(str, " \t\r\n\"/\\", '_');
+ subst_chars(str, " \t\r\n\"'/\\", '_');
}
void subst_for_shellsafe_filename(gchar *str)
return result;
}
-#define HEX_TO_INT(val, hex) \
-{ \
- gchar c = hex; \
- \
- if ('0' <= c && c <= '9') { \
- val = c - '0'; \
- } else if ('a' <= c && c <= 'f') { \
- val = c - 'a' + 10; \
- } else if ('A' <= c && c <= 'F') { \
- val = c - 'A' + 10; \
- } else { \
- val = 0; \
- } \
-}
-
/* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
* characters
*/
gboolean is_uri_string(const gchar *str)
{
- return (g_strncasecmp(str, "http://", 7) == 0 ||
- g_strncasecmp(str, "https://", 8) == 0 ||
- g_strncasecmp(str, "ftp://", 6) == 0 ||
- g_strncasecmp(str, "www.", 4) == 0);
+ return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
+ g_ascii_strncasecmp(str, "https://", 8) == 0 ||
+ g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
+ g_ascii_strncasecmp(str, "www.", 4) == 0);
}
gchar *get_uri_path(const gchar *uri)
{
- if (g_strncasecmp(uri, "http://", 7) == 0)
+ if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
return (gchar *)(uri + 7);
- else if (g_strncasecmp(uri, "https://", 8) == 0)
+ else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
return (gchar *)(uri + 8);
- else if (g_strncasecmp(uri, "ftp://", 6) == 0)
+ else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
return (gchar *)(uri + 6);
else
return (gchar *)uri;
if (*value == '\0') continue;
- if (cc && !*cc && !g_strcasecmp(field, "cc")) {
+ if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) {
*cc = g_strdup(value);
- } else if (bcc && !*bcc && !g_strcasecmp(field, "bcc")) {
+ } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) {
*bcc = g_strdup(value);
} else if (subject && !*subject &&
- !g_strcasecmp(field, "subject")) {
+ !g_ascii_strcasecmp(field, "subject")) {
*subject = g_malloc(strlen(value) + 1);
decode_uri(*subject, value);
- } else if (body && !*body && !g_strcasecmp(field, "body")) {
+ } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
*body = g_malloc(strlen(value) + 1);
decode_uri(*body, value);
}
static gchar *domain_name = NULL;
if (!domain_name) {
- gchar buf[128] = "";
struct hostent *hp;
+ struct utsname uts;
- if (gethostname(buf, sizeof(buf)) < 0) {
+ if (uname(&uts) < 0) {
perror("gethostname");
domain_name = "unknown";
} else {
- buf[sizeof(buf) - 1] = '\0';
- if ((hp = my_gethostbyname(buf)) == NULL) {
+ if ((hp = my_gethostbyname(uts.nodename)) == NULL) {
perror("gethostbyname");
- domain_name = g_strdup(buf);
+ domain_name = g_strdup(uts.nodename);
} else {
domain_name = g_strdup(hp->h_name);
}
DIR *dp;
struct dirent *d;
gchar *prev_dir;
- gint fileno;
+ gint file_no;
prev_dir = g_get_current_dir();
}
while ((d = readdir(dp)) != NULL) {
- fileno = to_number(d->d_name);
- if (fileno >= 0 && first <= fileno && fileno <= last) {
+ file_no = to_number(d->d_name);
+ if (file_no > 0 && first <= file_no && file_no <= last) {
if (is_dir_exist(d->d_name))
continue;
if (unlink(d->d_name) < 0)
DIR *dp;
struct dirent *d;
gchar *prev_dir;
- gint fileno;
+ gint file_no;
prev_dir = g_get_current_dir();
}
while ((d = readdir(dp)) != NULL) {
- fileno = to_number(d->d_name);
- if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
- debug_print("removing unwanted file %d from %s\n", fileno, dir);
+ file_no = to_number(d->d_name);
+ if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
+ debug_print("removing unwanted file %d from %s\n", file_no, dir);
if (is_dir_exist(d->d_name))
continue;
if (unlink(d->d_name) < 0)
struct dirent *d;
struct stat s;
gchar *prev_dir;
- gint fileno;
+ gint file_no;
time_t mtime, now, expire_time;
prev_dir = g_get_current_dir();
expire_time = hours * 60 * 60;
while ((d = readdir(dp)) != NULL) {
- fileno = to_number(d->d_name);
- if (fileno >= 0) {
+ file_no = to_number(d->d_name);
+ if (file_no > 0) {
if (stat(d->d_name, &s) < 0) {
FILE_OP_ERROR(d->d_name, "stat");
continue;
return 0;
}
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
{
- FILE *dest_fp;
gint n_read;
gint bytes_left, to_read;
gchar buf[BUFSIZ];
- gboolean err = FALSE;
if (fseek(fp, offset, SEEK_SET) < 0) {
perror("fseek");
return -1;
}
- if ((dest_fp = fopen(dest, "wb")) == NULL) {
- FILE_OP_ERROR(dest, "fopen");
- return -1;
- }
-
- if (change_file_mode_rw(dest_fp, dest) < 0) {
- FILE_OP_ERROR(dest, "chmod");
- g_warning("can't change file mode\n");
- }
-
bytes_left = length;
to_read = MIN(bytes_left, sizeof(buf));
if (n_read < to_read && ferror(fp))
break;
if (fwrite(buf, n_read, 1, dest_fp) < 1) {
- g_warning("writing to %s failed.\n", dest);
- fclose(dest_fp);
- unlink(dest);
return -1;
}
bytes_left -= n_read;
if (ferror(fp)) {
perror("fread");
- err = TRUE;
+ return -1;
}
- if (fclose(dest_fp) == EOF) {
+
+ return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+ FILE *dest_fp;
+ gboolean err = FALSE;
+
+ if ((dest_fp = fopen(dest, "wb")) == NULL) {
+ FILE_OP_ERROR(dest, "fopen");
+ return -1;
+ }
+
+ if (change_file_mode_rw(dest_fp, dest) < 0) {
+ FILE_OP_ERROR(dest, "chmod");
+ g_warning("can't change file mode\n");
+ }
+
+ if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
+ err = TRUE;
+
+ if (!err && fclose(dest_fp) == EOF) {
FILE_OP_ERROR(dest, "fclose");
err = TRUE;
}
if (err) {
+ g_warning("writing to %s failed.\n", dest);
unlink(dest);
return -1;
}
/* output header part */
while (fgets(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
- if (!g_strncasecmp(buf, "Bcc:", 4)) {
+ if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
gint next;
for (;;) {
* doesn't do any harm.
*/
for (i = 0; i < sizeof(buf_uniq) - 1; i++)
- buf_uniq[i] = tbl[(random() ^ pid) % (sizeof(tbl) - 1)];
+ buf_uniq[i] = tbl[(rand() ^ pid) % (sizeof(tbl) - 1)];
buf_uniq[i] = '\0';
get_rfc822_date(buf_date, sizeof(buf_date));
subst_char(buf_date, ',', '_');
subst_char(buf_date, ':', '_');
- return g_strdup_printf("%s=_%s_%s", prefix ? prefix : "Multipart",
+ return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
buf_date, buf_uniq);
}
tmpdir = get_tmp_dir();
tmplen = strlen(tmpdir);
progname = g_get_prgname();
+ if (progname == NULL)
+ progname = "sylpheed-claws";
proglen = strlen(progname);
Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
return tmpfile());
gint execute_async(gchar *const argv[])
{
pid_t pid;
+ gint status;
if ((pid = fork()) < 0) {
perror("fork");
_exit(0);
}
- waitpid(pid, NULL, 0);
+ waitpid(pid, &status, 0);
- return 0;
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ else
+ return -1;
}
gint execute_sync(gchar *const argv[])
{
pid_t pid;
+ gint status;
if ((pid = fork()) < 0) {
perror("fork");
_exit(1);
}
- waitpid(pid, NULL, 0);
+ waitpid(pid, &status, 0);
- return 0;
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ else
+ return -1;
}
gint execute_command_line(const gchar *cmdline, gboolean async)
gchar **argv;
gint ret;
+ debug_print("executing: %s\n", cmdline);
+
argv = strsplit_with_quote(cmdline, " ", 0);
if (async)
ret = execute_async(argv);
else
ret = execute_sync(argv);
+
g_strfreev(argv);
return ret;
g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
else {
if (cmdline)
- g_warning("Open URI command line is invalid: `%s'",
+ g_warning("Open URI command line is invalid "
+ "(there must be only one '%%s'): %s",
cmdline);
g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
}
-
+
execute_command_line(buf, TRUE);
return 0;
remoteoffset = 0;
} else if (strlen(zone3) == 3) {
for (p = ustzstr; *p != '\0'; p += 3) {
- if (!strncasecmp(p, zone3, 3)) {
+ if (!g_ascii_strncasecmp(p, zone3, 3)) {
iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
remoteoffset = iustz * 3600;
break;
for (n = 0; n < PREFIXES; n++)
/* Terminate each prefix regexpression by a
* "\ ?" (zero or ONE space), and OR them */
- g_string_sprintfa(s, "(%s\\ ?)%s",
+ g_string_append_printf(s, "(%s\\ ?)%s",
prefixes[n],
n < PREFIXES - 1 ?
"|" : "");
const char *str1 = gptr1;
const char *str2 = gptr2;
- return !strcasecmp(str1, str2);
+ return !g_utf8_collate(str1, str2);
}
gint g_int_compare(gconstpointer a, gconstpointer b)
return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
}
-gchar *generate_msgid(const gchar *address, gchar *buf, gint len)
+gchar *generate_msgid(gchar *buf, gint len)
{
- /* steal from compose.c::compose_generate_msgid() */
struct tm *lt;
time_t t;
gchar *addr;
t = time(NULL);
lt = localtime(&t);
- if (address && *address) {
- if (strchr(address, '@'))
- addr = g_strdup(address);
- else
- addr = g_strconcat(address, "@", get_domain_name(), NULL);
- } else
- addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
- NULL);
+ addr = g_strconcat("@", get_domain_name(), NULL);
- g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
+ g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x%s",
lt->tm_year + 1900, lt->tm_mon + 1,
lt->tm_mday, lt->tm_hour,
lt->tm_min, lt->tm_sec,
- (guint)random(), addr);
+ (guint) rand(), addr);
g_free(addr);
return buf;
}
-
/*
quote_cmd_argument()
return root;
}
+
+#define HEX_TO_INT(val, hex) \
+{ \
+ gchar c = hex; \
+ \
+ if ('0' <= c && c <= '9') { \
+ val = c - '0'; \
+ } else if ('a' <= c && c <= 'f') { \
+ val = c - 'a' + 10; \
+ } else if ('A' <= c && c <= 'F') { \
+ val = c - 'A' + 10; \
+ } else { \
+ val = -1; \
+ } \
+}
+
+gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
+{
+ gint hi, lo;
+
+ HEX_TO_INT(hi, c1);
+ HEX_TO_INT(lo, c2);
+
+ if (hi == -1 || lo == -1)
+ return FALSE;
+
+ *out = (hi << 4) + lo;
+ return TRUE;
+}
+
+#define INT_TO_HEX(hex, val) \
+{ \
+ if ((val) < 10) \
+ hex = '0' + (val); \
+ else \
+ hex = 'A' + (val) - 10; \
+}
+
+void get_hex_str(gchar *out, guchar ch)
+{
+ gchar hex;
+
+ INT_TO_HEX(hex, ch >> 4);
+ *out++ = hex;
+ INT_TO_HEX(hex, ch & 0x0f);
+ *out++ = hex;
+}
+
+#undef REF_DEBUG
+#ifndef REF_DEBUG
+#define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
+#else
+#define G_PRINT_REF g_print
+#endif
+
+/*!
+ *\brief Register ref counted pointer. It is based on GBoxed, so should
+ * work with anything that uses the GType system. The semantics
+ * are similar to a C++ auto pointer, with the exception that
+ * C doesn't have automatic closure (calling destructors) when
+ * exiting a block scope.
+ * Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
+ * function directly.
+ *
+ *\return GType A GType type.
+ */
+GType g_auto_pointer_register(void)
+{
+ static GType auto_pointer_type;
+ if (!auto_pointer_type)
+ auto_pointer_type =
+ g_boxed_type_register_static
+ ("G_TYPE_AUTO_POINTER",
+ (GBoxedCopyFunc) g_auto_pointer_copy,
+ (GBoxedFreeFunc) g_auto_pointer_free);
+ return auto_pointer_type;
+}
+
+/*!
+ *\brief Structure with g_new() allocated pointer guarded by the
+ * auto pointer
+ */
+typedef struct AutoPointerRef {
+ void (*free) (gpointer);
+ gpointer pointer;
+ glong cnt;
+} AutoPointerRef;
+
+/*!
+ *\brief The auto pointer opaque structure that references the
+ * pointer guard block.
+ */
+typedef struct AutoPointer {
+ AutoPointerRef *ref;
+ gpointer ptr; /*!< access to protected pointer */
+} AutoPointer;
+
+/*!
+ *\brief Creates an auto pointer for a g_new()ed pointer. Example:
+ *
+ *\code
+ *
+ * ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
+ * ... when assigning, copying and freeing storage elements
+ *
+ * gtk_list_store_new(N_S_COLUMNS,
+ * G_TYPE_AUTO_POINTER,
+ * -1);
+ *
+ *
+ * Template *precious_data = g_new0(Template, 1);
+ * g_pointer protect = g_auto_pointer_new(precious_data);
+ *
+ * gtk_list_store_set(container, &iter,
+ * S_DATA, protect,
+ * -1);
+ *
+ * ... the gtk_list_store has copied the pointer and
+ * ... incremented its reference count, we should free
+ * ... the auto pointer (in C++ a destructor would do
+ * ... this for us when leaving block scope)
+ *
+ * g_auto_pointer_free(protect);
+ *
+ * ... gtk_list_store_set() now manages the data. When
+ * ... *explicitly* requesting a pointer from the list
+ * ... store, don't forget you get a copy that should be
+ * ... freed with g_auto_pointer_free() eventually.
+ *
+ *\endcode
+ *
+ *\param pointer Pointer to be guarded.
+ *
+ *\return GAuto * Pointer that should be used in containers with
+ * GType support.
+ */
+GAuto *g_auto_pointer_new(gpointer p)
+{
+ AutoPointerRef *ref;
+ AutoPointer *ptr;
+
+ if (p == NULL)
+ return NULL;
+
+ ref = g_new0(AutoPointerRef, 1);
+ ptr = g_new0(AutoPointer, 1);
+
+ ref->pointer = p;
+ ref->free = g_free;
+ ref->cnt = 1;
+
+ ptr->ref = ref;
+ ptr->ptr = p;
+
+ G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
+
+ return ptr;
+}
+
+/*!
+ *\brief Allocate an autopointer using the passed \a free function to
+ * free the guarded pointer
+ */
+GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
+{
+ AutoPointer *aptr;
+
+ if (p == NULL)
+ return NULL;
+
+ aptr = g_auto_pointer_new(p);
+ aptr->ref->free = free_;
+ return aptr;
+}
+
+gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
+{
+ if (auto_ptr == NULL)
+ return NULL;
+ return ((AutoPointer *) auto_ptr)->ptr;
+}
+
+/*!
+ *\brief Copies an auto pointer by. It's mostly not necessary
+ * to call this function directly, unless you copy/assign
+ * the guarded pointer.
+ *
+ *\param auto_ptr Auto pointer returned by previous call to
+ * g_auto_pointer_new_XXX()
+ *
+ *\return gpointer An auto pointer
+ */
+GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
+{
+ AutoPointer *ptr;
+ AutoPointerRef *ref;
+ AutoPointer *newp;
+
+ if (auto_ptr == NULL)
+ return NULL;
+
+ ptr = auto_ptr;
+ ref = ptr->ref;
+ newp = g_new0(AutoPointer, 1);
+
+ newp->ref = ref;
+ newp->ptr = ref->pointer;
+ ++(ref->cnt);
+
+ G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+
+ return newp;
+}
+
+/*!
+ *\brief Free an auto pointer
+ */
+void g_auto_pointer_free(GAuto *auto_ptr)
+{
+ AutoPointer *ptr;
+ AutoPointerRef *ref;
+
+ if (auto_ptr == NULL)
+ return;
+
+ ptr = auto_ptr;
+ ref = ptr->ref;
+
+ if (--(ref->cnt) == 0) {
+ G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+ ref->free(ref->pointer);
+ g_free(ref);
+ } else
+ G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+ g_free(ptr);
+}
+