2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The code of the g_utf8_substring function below is owned by
19 * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20 * and is got from GLIB 2.30
28 #include "claws-features.h"
36 #include <glib-object.h>
41 #include <sys/types.h>
51 /* The Hurd doesn't have this limit */
60 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
62 /* Wrappers for C library function that take pathname arguments. */
63 # include <glib/gstdio.h>
65 /* why is this sometimes undefined !? */
67 typedef gint64 goffset
;
68 #define G_MINOFFSET G_MININT64
69 #define G_MAXOFFSET G_MAXINT64
72 #ifndef HAVE_U32_TYPEDEF
73 #undef u32 /* maybe there is a macro with this name */
75 #define HAVE_U32_TYPEDEF
78 #if !GLIB_CHECK_VERSION(2, 26, 0)
79 #define g_base64_decode(t,l) g_base64_decode_wa((t),(l))
80 guchar
*g_base64_decode_wa(const gchar
*text
, gsize
*out_len
);
83 #if !GLIB_CHECK_VERSION(2, 25, 0)
85 typedef _g_stat_struct GStatBuf
;
87 typedef struct stat GStatBuf
;
91 #ifndef BIG_ENDIAN_HOST
92 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
93 #define BIG_ENDIAN_HOST 1
97 #define CHDIR_RETURN_IF_FAIL(dir) \
99 if (change_dir(dir) < 0) return; \
102 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
104 if (change_dir(dir) < 0) return val; \
107 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
109 if (change_dir(dir) < 0) { \
115 #define Xalloca(ptr, size, iffail) \
117 if ((ptr = alloca(size)) == NULL) { \
118 g_warning("can't allocate memory"); \
123 #define Xstrdup_a(ptr, str, iffail) \
127 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
128 g_warning("can't allocate memory"); \
131 strcpy(__tmp, str); \
136 #define Xstrndup_a(ptr, str, len, iffail) \
140 if ((__tmp = alloca(len + 1)) == NULL) { \
141 g_warning("can't allocate memory"); \
144 strncpy(__tmp, str, len); \
151 #define Xstrcat_a(ptr, str1, str2, iffail) \
156 len1 = strlen(str1); \
157 len2 = strlen(str2); \
158 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
159 g_warning("can't allocate memory"); \
162 memcpy(__tmp, str1, len1); \
163 memcpy(__tmp + len1, str2, len2 + 1); \
169 #define AUTORELEASE_STR(str, iffail) \
172 Xstrdup_a(__str, str, iffail); \
177 #define FILE_OP_ERROR(file, func) \
179 g_printerr("%s: ", file); \
184 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
186 /* from NetworkManager */
187 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
188 #define print_backtrace() \
191 void *_call_stack[512]; \
192 int _call_stack_size; \
194 _call_stack_size = backtrace (_call_stack, \
195 G_N_ELEMENTS (_call_stack)); \
196 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
197 if (_symbols != NULL) \
201 g_print ("traceback:\n"); \
202 while (_i < _call_stack_size) \
204 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
212 #define print_backtrace() \
220 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
222 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
229 #define cm_return_if_fail(expr) G_STMT_START { \
231 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
239 #define MIN(a, b) ((a) < (b) ? (a) : (b))
242 #define MAX(a, b) ((a) > (b) ? (a) : (b))
249 typedef gpointer (*GNodeMapFunc
) (gpointer nodedata
, gpointer data
);
251 /* debug functions */
252 void debug_set_mode (gboolean mode
);
253 gboolean
debug_get_mode (void);
256 #define debug_print \
257 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
260 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
261 #define debug_print \
262 debug_print_real("%s:%d:", __FILE__, __LINE__), \
266 /* for macro expansion */
268 #define Xstr(x) Str(x)
271 /* System related stuff. */
273 gboolean
superuser_p (void);
275 /* List utilities. */
277 GSList
*slist_copy_deep (GSList
*list
,
280 /* String utilities. */
282 void list_free_strings (GList
*list
);
283 void slist_free_strings (GSList
*list
);
284 void slist_free_strings_full (GSList
*list
);
286 void hash_free_strings (GHashTable
*table
);
288 gint
str_case_equal (gconstpointer v
,
290 guint
str_case_hash (gconstpointer key
);
292 void ptr_array_free_strings (GPtrArray
*array
);
294 /* number-string conversion */
295 gint
to_number (const gchar
*nstr
);
296 gchar
*itos_buf (gchar
*nstr
,
298 gchar
*itos (gint n
);
299 gchar
*to_human_readable (goffset size
);
301 /* alternative string functions */
302 gint
strcmp2 (const gchar
*s1
,
304 gchar
*strstr2 (const gchar
*s1
,
306 gint
path_cmp (const gchar
*s1
,
308 gchar
*strretchomp (gchar
*str
);
309 gchar
*strtailchomp (gchar
*str
,
311 gchar
*strcrchomp (gchar
*str
);
312 gint
file_strip_crs (const gchar
*file
);
313 gchar
*strcasestr (const gchar
*haystack
,
314 const gchar
*needle
);
315 gchar
*strncasestr (const gchar
*haystack
,
317 const gchar
*needle
);
318 gpointer
my_memmem (gconstpointer haystack
,
320 gconstpointer needle
,
322 gchar
*strncpy2 (gchar
*dest
,
326 gboolean
is_next_nonascii (const gchar
*s
);
327 gint
get_next_word_len (const gchar
*s
);
329 /* functions for string parsing */
330 gint
subject_compare (const gchar
*s1
,
332 gint
subject_compare_for_sort (const gchar
*s1
,
334 void trim_subject (gchar
*str
);
335 void eliminate_parenthesis (gchar
*str
,
338 void extract_parenthesis (gchar
*str
,
342 void extract_quote (gchar
*str
,
344 gchar
*escape_internal_quotes (gchar
*str
,
346 void eliminate_address_comment (gchar
*str
);
347 gchar
*strchr_with_skip_quote (const gchar
*str
,
350 void extract_address (gchar
*str
);
351 void extract_list_id_str (gchar
*str
);
353 GSList
*address_list_append (GSList
*addr_list
,
355 GSList
*address_list_append_with_comments(GSList
*addr_list
,
357 GSList
*references_list_prepend (GSList
*msgid_list
,
359 GSList
*references_list_append (GSList
*msgid_list
,
361 GSList
*newsgroup_list_append (GSList
*group_list
,
364 GList
*add_history (GList
*list
,
367 void remove_return (gchar
*str
);
368 void remove_space (gchar
*str
);
369 void unfold_line (gchar
*str
);
370 void subst_char (gchar
*str
,
373 void subst_chars (gchar
*str
,
376 void subst_for_filename (gchar
*str
);
377 void subst_for_shellsafe_filename (gchar
*str
);
378 gboolean
is_ascii_str (const gchar
*str
);
379 gint
get_quote_level (const gchar
*str
,
380 const gchar
*quote_chars
);
381 gint
check_line_length (const gchar
*str
,
385 gchar
**strsplit_with_quote (const gchar
*str
,
389 gchar
*get_abbrev_newsgroup_name (const gchar
*group
,
391 gchar
*trim_string (const gchar
*str
,
394 GList
*uri_list_extract_filenames (const gchar
*uri_list
);
395 gboolean
is_uri_string (const gchar
*str
);
396 gchar
*get_uri_path (const gchar
*uri
);
397 gint
get_uri_len (const gchar
*str
);
398 void decode_uri (gchar
*decoded_uri
,
399 const gchar
*encoded_uri
);
400 void decode_uri_with_plus (gchar
*decoded_uri
,
401 const gchar
*encoded_uri
,
403 gint
scan_mailto_url (const gchar
*mailto
,
413 /* return static strings */
414 const gchar
*get_home_dir (void);
415 const gchar
*get_rc_dir (void);
416 void set_rc_dir (const gchar
*dir
);
417 gboolean
rc_dir_is_alt (void);
418 const gchar
*get_mail_base_dir (void);
419 const gchar
*get_news_cache_dir (void);
420 const gchar
*get_imap_cache_dir (void);
421 const gchar
*get_mime_tmp_dir (void);
422 const gchar
*get_template_dir (void);
423 const gchar
*get_plugin_dir (void);
424 const gchar
*get_tmp_dir (void);
425 const gchar
*get_locale_dir (void);
426 gchar
*get_tmp_file (void);
427 const gchar
*get_domain_name (void);
428 const gchar
*get_desktop_file(void);
430 const gchar
*w32_get_themes_dir (void);
431 const gchar
*w32_get_cert_file (void);
433 /* file / directory handling */
434 off_t
get_file_size (const gchar
*file
);
435 off_t
get_file_size_as_crlf (const gchar
*file
);
437 time_t get_file_mtime (const gchar
*file
);
439 gboolean
file_exist (const gchar
*file
,
440 gboolean allow_fifo
);
441 gboolean
is_relative_filename (const gchar
*file
);
442 gboolean
is_dir_exist (const gchar
*dir
);
443 gboolean
is_file_entry_exist (const gchar
*file
);
444 gboolean
dirent_is_regular_file (struct dirent
*d
);
446 #define is_file_exist(file) file_exist(file, FALSE)
447 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
449 gint
change_dir (const gchar
*dir
);
450 gint
make_dir (const gchar
*dir
);
451 gint
make_dir_hier (const gchar
*dir
);
452 gint
remove_all_files (const gchar
*dir
);
453 gint
remove_numbered_files (const gchar
*dir
,
456 gint
remove_numbered_files_not_in_list(const gchar
*dir
,
458 gint
remove_all_numbered_files (const gchar
*dir
);
459 gint
remove_dir_recursive (const gchar
*dir
);
460 gint
append_file (const gchar
*src
,
462 gboolean keep_backup
);
463 gint
rename_force (const gchar
*oldpath
,
464 const gchar
*newpath
);
465 gint
copy_file (const gchar
*src
,
467 gboolean keep_backup
);
468 gint
move_file (const gchar
*src
,
471 gint
copy_dir (const gchar
*src
,
473 gint
copy_file_part_to_fp (FILE *fp
,
477 gint
copy_file_part (FILE *fp
,
482 gchar
*canonicalize_str (const gchar
*str
);
483 gint
canonicalize_file (const gchar
*src
,
485 gint
canonicalize_file_replace (const gchar
*file
);
487 gchar
*normalize_newlines (const gchar
*str
);
489 gchar
*get_outgoing_rfc2822_str (FILE *fp
);
491 gint
change_file_mode_rw (FILE *fp
,
493 FILE *my_tmpfile (void);
494 FILE *get_tmpfile_in_dir (const gchar
*dir
,
496 FILE *str_open_as_stream (const gchar
*str
);
497 gint
str_write_to_file (const gchar
*str
,
499 gchar
*file_read_to_str (const gchar
*file
);
500 gchar
*file_read_stream_to_str (FILE *fp
);
501 gchar
*file_read_to_str_no_recode(const gchar
*file
);
502 gchar
*file_read_stream_to_str_no_recode(FILE *fp
);
504 char *fgets_crlf(char *buf
, int size
, FILE *stream
);
506 /* process execution */
507 gint
execute_command_line (const gchar
*cmdline
,
509 const gchar
*working_directory
);
510 gchar
*get_command_output (const gchar
*cmdline
);
512 /* open URI with external browser */
513 gint
open_uri(const gchar
*uri
, const gchar
*cmdline
);
514 /* open file with text editor */
515 gint
open_txt_editor(const gchar
*filepath
, const gchar
*cmdline
);
518 time_t remote_tzoffset_sec (const gchar
*zone
);
519 time_t tzoffset_sec (time_t *now
);
520 gchar
*tzoffset (time_t *now
);
521 void get_rfc822_date (gchar
*buf
,
523 void get_rfc822_date_hide_tz (gchar
*buf
,
526 size_t fast_strftime (gchar
*buf
,
532 void debug_print_real (const gchar
*format
, ...) G_GNUC_PRINTF(1, 2);
533 const char * debug_srcname (const char *file
);
535 /* subject threading */
536 void * subject_table_lookup(GHashTable
*subject_table
, gchar
* subject
);
537 void subject_table_insert(GHashTable
*subject_table
, gchar
* subject
,
539 void subject_table_remove(GHashTable
*subject_table
, gchar
* subject
);
540 void utils_free_regex(void);
541 gint
subject_get_prefix_length (const gchar
*subject
);
543 /* quoting recognition */
544 const gchar
* line_has_quote_char (const gchar
*str
,
545 const gchar
*quote_chars
);
547 gint
g_int_compare (gconstpointer a
, gconstpointer b
);
549 gchar
*generate_msgid (gchar
*buf
, gint len
, gchar
*user_addr
);
550 gchar
*generate_mime_boundary (const gchar
*prefix
);
552 gint
quote_cmd_argument(gchar
* result
, guint size
,
554 GNode
*g_node_map(GNode
*node
, GNodeMapFunc func
, gpointer data
);
556 gboolean
get_hex_value(guchar
*out
, gchar c1
, gchar c2
);
557 void get_hex_str(gchar
*out
, guchar ch
);
559 /* auto pointer for containers that support GType system */
561 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
562 typedef struct AutoPointer GAuto
;
563 GType
g_auto_pointer_register (void);
564 GAuto
*g_auto_pointer_new (gpointer pointer
);
565 GAuto
*g_auto_pointer_new_with_free (gpointer p
,
567 gpointer
g_auto_pointer_get_ptr (GAuto
*auto_ptr
);
568 GAuto
*g_auto_pointer_copy (GAuto
*auto_ptr
);
569 void g_auto_pointer_free (GAuto
*auto_ptr
);
570 void replace_returns (gchar
*str
);
571 gboolean
get_uri_part (const gchar
*start
,
572 const gchar
*scanpos
,
576 gchar
*make_uri_string (const gchar
*bp
,
578 gboolean
get_email_part (const gchar
*start
,
579 const gchar
*scanpos
,
583 gchar
*make_email_string(const gchar
*bp
,
585 gchar
*make_http_string (const gchar
*bp
,
588 gchar
*mailcap_get_command_for_type(const gchar
*type
,
589 const gchar
*file_to_open
);
590 void mailcap_update_default (const gchar
*type
,
591 const gchar
*command
);
593 gboolean
file_is_email(const gchar
*filename
);
594 gboolean
sc_g_list_bigger(GList
*list
, gint max
);
595 gboolean
sc_g_slist_bigger(GSList
*list
, gint max
);
597 int claws_unlink(const gchar
*filename
);
599 GMutex
*cm_mutex_new(void);
600 void cm_mutex_free(GMutex
*mutex
);
602 int cm_canonicalize_filename(const gchar
*filename
, gchar
**canonical_name
);
604 guchar
*g_base64_decode_zero(const gchar
*text
, gsize
*out_len
);
606 #if !GLIB_CHECK_VERSION(2, 30, 0)
607 gchar
*g_utf8_substring (const gchar
*p
,
609 glong end_pos
) G_GNUC_MALLOC
;
612 gboolean
get_random_bytes(void *buf
, size_t count
);
618 #endif /* __UTILS_H__ */