2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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
27 #include "claws-features.h"
35 #include <glib-object.h>
40 #include <sys/types.h>
50 /* The Hurd doesn't have this limit */
59 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
61 /* Wrappers for C library function that take pathname arguments. */
62 # include <glib/gstdio.h>
64 /* why is this sometimes undefined !? */
66 typedef gint64 goffset;
67 #define G_MINOFFSET G_MININT64
68 #define G_MAXOFFSET G_MAXINT64
71 #ifndef HAVE_U32_TYPEDEF
72 #undef u32 /* maybe there is a macro with this name */
74 #define HAVE_U32_TYPEDEF
77 #if !GLIB_CHECK_VERSION(2, 26, 0)
78 #define g_base64_decode(t,l) g_base64_decode_wa((t),(l))
79 guchar *g_base64_decode_wa(const gchar *text, gsize *out_len);
82 #if !GLIB_CHECK_VERSION(2, 25, 0)
84 typedef _g_stat_struct GStatBuf;
86 typedef struct stat GStatBuf;
90 #ifndef BIG_ENDIAN_HOST
91 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
92 #define BIG_ENDIAN_HOST 1
96 #define CHDIR_RETURN_IF_FAIL(dir) \
98 if (change_dir(dir) < 0) return; \
101 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
103 if (change_dir(dir) < 0) return val; \
106 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
108 if (change_dir(dir) < 0) { \
114 #define Xalloca(ptr, size, iffail) \
116 if ((ptr = alloca(size)) == NULL) { \
117 g_warning("can't allocate memory"); \
122 #define Xstrdup_a(ptr, str, iffail) \
126 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
127 g_warning("can't allocate memory"); \
130 strcpy(__tmp, str); \
135 #define Xstrndup_a(ptr, str, len, iffail) \
139 if ((__tmp = alloca(len + 1)) == NULL) { \
140 g_warning("can't allocate memory"); \
143 strncpy(__tmp, str, len); \
150 #define Xstrcat_a(ptr, str1, str2, iffail) \
155 len1 = strlen(str1); \
156 len2 = strlen(str2); \
157 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
158 g_warning("can't allocate memory"); \
161 memcpy(__tmp, str1, len1); \
162 memcpy(__tmp + len1, str2, len2 + 1); \
168 #define AUTORELEASE_STR(str, iffail) \
171 Xstrdup_a(__str, str, iffail); \
176 #define FILE_OP_ERROR(file, func) \
178 g_printerr("%s: ", file); \
183 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
185 /* from NetworkManager */
186 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
187 #define print_backtrace() \
190 void *_call_stack[512]; \
191 int _call_stack_size; \
193 _call_stack_size = backtrace (_call_stack, \
194 G_N_ELEMENTS (_call_stack)); \
195 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
196 if (_symbols != NULL) \
200 g_print ("traceback:\n"); \
201 while (_i < _call_stack_size) \
203 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
211 #define print_backtrace() \
219 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
221 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
228 #define cm_return_if_fail(expr) G_STMT_START { \
230 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
238 #define MIN(a, b) ((a) < (b) ? (a) : (b))
241 #define MAX(a, b) ((a) > (b) ? (a) : (b))
248 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
250 /* debug functions */
251 void debug_set_mode (gboolean mode);
252 gboolean debug_get_mode (void);
255 #define debug_print \
256 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
259 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
260 #define debug_print \
261 debug_print_real("%s:%d:", __FILE__, __LINE__), \
265 /* for macro expansion */
267 #define Xstr(x) Str(x)
270 /* System related stuff. */
272 gboolean superuser_p (void);
274 /* List utilities. */
276 GSList *slist_copy_deep (GSList *list,
279 /* String utilities. */
281 void list_free_strings (GList *list);
282 void slist_free_strings (GSList *list);
283 void slist_free_strings_full (GSList *list);
285 void hash_free_strings (GHashTable *table);
287 gint str_case_equal (gconstpointer v,
289 guint str_case_hash (gconstpointer key);
291 void ptr_array_free_strings (GPtrArray *array);
293 /* number-string conversion */
294 gint to_number (const gchar *nstr);
295 gchar *itos_buf (gchar *nstr,
297 gchar *itos (gint n);
298 gchar *to_human_readable (goffset size);
300 /* alternative string functions */
301 gint strcmp2 (const gchar *s1,
303 gchar *strstr2 (const gchar *s1,
305 gint path_cmp (const gchar *s1,
307 gchar *strretchomp (gchar *str);
308 gchar *strtailchomp (gchar *str,
310 gchar *strcrchomp (gchar *str);
311 gint file_strip_crs (const gchar *file);
312 gchar *strcasestr (const gchar *haystack,
313 const gchar *needle);
314 gchar *strncasestr (const gchar *haystack,
316 const gchar *needle);
317 gpointer my_memmem (gconstpointer haystack,
319 gconstpointer needle,
321 gchar *strncpy2 (gchar *dest,
325 gboolean is_next_nonascii (const gchar *s);
326 gint get_next_word_len (const gchar *s);
328 /* functions for string parsing */
329 gint subject_compare (const gchar *s1,
331 gint subject_compare_for_sort (const gchar *s1,
333 void trim_subject (gchar *str);
334 void eliminate_parenthesis (gchar *str,
337 void extract_parenthesis (gchar *str,
341 void extract_quote (gchar *str,
343 gchar *escape_internal_quotes (gchar *str,
345 void eliminate_address_comment (gchar *str);
346 gchar *strchr_with_skip_quote (const gchar *str,
349 void extract_address (gchar *str);
350 void extract_list_id_str (gchar *str);
352 GSList *address_list_append (GSList *addr_list,
354 GSList *address_list_append_with_comments(GSList *addr_list,
356 GSList *references_list_prepend (GSList *msgid_list,
358 GSList *references_list_append (GSList *msgid_list,
360 GSList *newsgroup_list_append (GSList *group_list,
363 GList *add_history (GList *list,
366 void remove_return (gchar *str);
367 void remove_space (gchar *str);
368 void unfold_line (gchar *str);
369 void subst_char (gchar *str,
372 void subst_chars (gchar *str,
375 void subst_for_filename (gchar *str);
376 void subst_for_shellsafe_filename (gchar *str);
377 gboolean is_ascii_str (const gchar *str);
378 gint get_quote_level (const gchar *str,
379 const gchar *quote_chars);
380 gint check_line_length (const gchar *str,
384 gchar **strsplit_with_quote (const gchar *str,
388 gchar *get_abbrev_newsgroup_name (const gchar *group,
390 gchar *trim_string (const gchar *str,
393 GList *uri_list_extract_filenames (const gchar *uri_list);
394 gboolean is_uri_string (const gchar *str);
395 gchar *get_uri_path (const gchar *uri);
396 gint get_uri_len (const gchar *str);
397 void decode_uri (gchar *decoded_uri,
398 const gchar *encoded_uri);
399 void decode_uri_with_plus (gchar *decoded_uri,
400 const gchar *encoded_uri,
402 gint scan_mailto_url (const gchar *mailto,
412 /* return static strings */
413 const gchar *get_home_dir (void);
414 const gchar *get_rc_dir (void);
415 void set_rc_dir (const gchar *dir);
416 gboolean rc_dir_is_alt (void);
417 const gchar *get_mail_base_dir (void);
418 const gchar *get_news_cache_dir (void);
419 const gchar *get_imap_cache_dir (void);
420 const gchar *get_mime_tmp_dir (void);
421 const gchar *get_template_dir (void);
422 const gchar *get_plugin_dir (void);
423 const gchar *get_tmp_dir (void);
424 const gchar *get_locale_dir (void);
425 gchar *get_tmp_file (void);
426 const gchar *get_domain_name (void);
427 const gchar *get_desktop_file(void);
429 const gchar *w32_get_themes_dir (void);
430 const gchar *w32_get_cert_file (void);
432 /* file / directory handling */
433 off_t get_file_size (const gchar *file);
434 time_t get_file_mtime (const gchar *file);
436 gboolean file_exist (const gchar *file,
437 gboolean allow_fifo);
438 gboolean is_relative_filename (const gchar *file);
439 gboolean is_dir_exist (const gchar *dir);
440 gboolean is_file_entry_exist (const gchar *file);
441 gboolean dirent_is_regular_file (struct dirent *d);
443 #define is_file_exist(file) file_exist(file, FALSE)
444 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
446 gint change_dir (const gchar *dir);
447 gint make_dir (const gchar *dir);
448 gint make_dir_hier (const gchar *dir);
449 gint remove_all_files (const gchar *dir);
450 gint remove_numbered_files (const gchar *dir,
453 gint remove_numbered_files_not_in_list(const gchar *dir,
455 gint remove_all_numbered_files (const gchar *dir);
456 gint remove_dir_recursive (const gchar *dir);
457 gint append_file (const gchar *src,
459 gboolean keep_backup);
460 gint rename_force (const gchar *oldpath,
461 const gchar *newpath);
462 gint copy_file (const gchar *src,
464 gboolean keep_backup);
465 gint move_file (const gchar *src,
468 gint copy_dir (const gchar *src,
470 gint copy_file_part_to_fp (FILE *fp,
474 gint copy_file_part (FILE *fp,
479 gchar *canonicalize_str (const gchar *str);
480 gint canonicalize_file (const gchar *src,
482 gint canonicalize_file_replace (const gchar *file);
484 gchar *normalize_newlines (const gchar *str);
486 gchar *get_outgoing_rfc2822_str (FILE *fp);
488 gint change_file_mode_rw (FILE *fp,
490 FILE *my_tmpfile (void);
491 FILE *get_tmpfile_in_dir (const gchar *dir,
493 FILE *str_open_as_stream (const gchar *str);
494 gint str_write_to_file (const gchar *str,
496 gchar *file_read_to_str (const gchar *file);
497 gchar *file_read_stream_to_str (FILE *fp);
498 gchar *file_read_to_str_no_recode(const gchar *file);
499 gchar *file_read_stream_to_str_no_recode(FILE *fp);
501 char *fgets_crlf(char *buf, int size, FILE *stream);
503 /* process execution */
504 gint execute_command_line (const gchar *cmdline,
506 const gchar *working_directory);
507 gchar *get_command_output (const gchar *cmdline);
509 /* open URI with external browser */
510 gint open_uri(const gchar *uri, const gchar *cmdline);
511 /* open file with text editor */
512 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
515 time_t remote_tzoffset_sec (const gchar *zone);
516 time_t tzoffset_sec (time_t *now);
517 gchar *tzoffset (time_t *now);
518 void get_rfc822_date (gchar *buf,
520 void get_rfc822_date_hide_tz (gchar *buf,
523 size_t fast_strftime (gchar *buf,
529 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
530 const char * debug_srcname (const char *file);
532 /* subject threading */
533 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
534 void subject_table_insert(GHashTable *subject_table, gchar * subject,
536 void subject_table_remove(GHashTable *subject_table, gchar * subject);
537 void utils_free_regex(void);
538 gint subject_get_prefix_length (const gchar *subject);
540 /* quoting recognition */
541 const gchar * line_has_quote_char (const gchar *str,
542 const gchar *quote_chars);
544 gint g_int_compare (gconstpointer a, gconstpointer b);
546 gchar *generate_mime_boundary (const gchar *prefix);
548 gint quote_cmd_argument(gchar * result, guint size,
550 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
552 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
553 void get_hex_str(gchar *out, guchar ch);
555 /* auto pointer for containers that support GType system */
557 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
558 typedef struct AutoPointer GAuto;
559 GType g_auto_pointer_register (void);
560 GAuto *g_auto_pointer_new (gpointer pointer);
561 GAuto *g_auto_pointer_new_with_free (gpointer p,
563 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
564 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
565 void g_auto_pointer_free (GAuto *auto_ptr);
566 void replace_returns (gchar *str);
567 gboolean get_uri_part (const gchar *start,
568 const gchar *scanpos,
572 gchar *make_uri_string (const gchar *bp,
574 gboolean get_email_part (const gchar *start,
575 const gchar *scanpos,
579 gchar *make_email_string(const gchar *bp,
581 gchar *make_http_string (const gchar *bp,
584 gchar *mailcap_get_command_for_type(const gchar *type,
585 const gchar *file_to_open);
586 void mailcap_update_default (const gchar *type,
587 const gchar *command);
589 gboolean file_is_email(const gchar *filename);
590 gboolean sc_g_list_bigger(GList *list, gint max);
591 gboolean sc_g_slist_bigger(GSList *list, gint max);
593 int claws_unlink(const gchar *filename);
595 GMutex *cm_mutex_new(void);
596 void cm_mutex_free(GMutex *mutex);
598 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
600 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
602 #if !GLIB_CHECK_VERSION(2, 30, 0)
603 gchar *g_utf8_substring (const gchar *p,
605 glong end_pos) G_GNUC_MALLOC;
608 gboolean get_random_bytes(void *buf, size_t count);
614 #endif /* __UTILS_H__ */