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 #ifndef BIG_ENDIAN_HOST
78 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
79 #define BIG_ENDIAN_HOST 1
83 #define CHDIR_RETURN_IF_FAIL(dir) \
85 if (change_dir(dir) < 0) return; \
88 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
90 if (change_dir(dir) < 0) return val; \
93 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
95 if (change_dir(dir) < 0) { \
101 #define Xalloca(ptr, size, iffail) \
103 if ((ptr = alloca(size)) == NULL) { \
104 g_warning("can't allocate memory"); \
109 #define Xstrdup_a(ptr, str, iffail) \
113 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
114 g_warning("can't allocate memory"); \
117 strcpy(__tmp, str); \
122 #define Xstrndup_a(ptr, str, len, iffail) \
126 if ((__tmp = alloca(len + 1)) == NULL) { \
127 g_warning("can't allocate memory"); \
130 memcpy(__tmp, str, len); \
137 #define Xstrcat_a(ptr, str1, str2, iffail) \
142 len1 = strlen(str1); \
143 len2 = strlen(str2); \
144 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
145 g_warning("can't allocate memory"); \
148 memcpy(__tmp, str1, len1); \
149 memcpy(__tmp + len1, str2, len2 + 1); \
155 #define AUTORELEASE_STR(str, iffail) \
158 Xstrdup_a(__str, str, iffail); \
163 #define FILE_OP_ERROR(file, func) \
165 g_printerr("%s: ", file); \
170 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
172 /* from NetworkManager */
173 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
174 #define print_backtrace() \
177 void *_call_stack[512]; \
178 int _call_stack_size; \
180 _call_stack_size = backtrace (_call_stack, \
181 G_N_ELEMENTS (_call_stack)); \
182 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
183 if (_symbols != NULL) \
187 g_print ("traceback:\n"); \
188 while (_i < _call_stack_size) \
190 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
198 #define print_backtrace() \
206 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
208 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
215 #define cm_return_if_fail(expr) G_STMT_START { \
217 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
225 #define MIN(a, b) ((a) < (b) ? (a) : (b))
228 #define MAX(a, b) ((a) > (b) ? (a) : (b))
235 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
237 /* debug functions */
238 void debug_set_mode (gboolean mode);
239 gboolean debug_get_mode (void);
242 #define debug_print \
243 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
246 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
247 #define debug_print \
248 debug_print_real("%s:%d:", __FILE__, __LINE__), \
252 /* for macro expansion */
254 #define Xstr(x) Str(x)
257 /* System related stuff. */
259 gboolean superuser_p (void);
261 /* List utilities. */
263 GSList *slist_copy_deep (GSList *list,
266 /* String utilities. */
268 void list_free_strings_full (GList *list);
269 void slist_free_strings_full (GSList *list);
271 void hash_free_strings (GHashTable *table);
273 gint str_case_equal (gconstpointer v,
275 guint str_case_hash (gconstpointer key);
277 void ptr_array_free_strings (GPtrArray *array);
279 /* number-string conversion */
280 gint to_number (const gchar *nstr);
281 gchar *itos_buf (gchar *nstr,
283 gchar *itos (gint n);
284 gchar *to_human_readable (goffset size);
286 /* alternative string functions */
287 gint strcmp2 (const gchar *s1,
289 gchar *strstr2 (const gchar *s1,
291 gint path_cmp (const gchar *s1,
293 gchar *strretchomp (gchar *str);
294 gchar *strtailchomp (gchar *str,
296 gchar *strcrchomp (gchar *str);
297 gint file_strip_crs (const gchar *file);
298 gchar *strcasestr (const gchar *haystack,
299 const gchar *needle);
300 gchar *strncasestr (const gchar *haystack,
302 const gchar *needle);
303 gpointer my_memmem (gconstpointer haystack,
305 gconstpointer needle,
307 gchar *strncpy2 (gchar *dest,
311 gboolean is_next_nonascii (const gchar *s);
312 gint get_next_word_len (const gchar *s);
314 /* functions for string parsing */
315 gint subject_compare (const gchar *s1,
317 gint subject_compare_for_sort (const gchar *s1,
319 void trim_subject (gchar *str);
320 void eliminate_parenthesis (gchar *str,
323 void extract_parenthesis (gchar *str,
327 void extract_quote (gchar *str,
329 gchar *escape_internal_quotes (gchar *str,
331 void eliminate_address_comment (gchar *str);
332 gchar *strchr_with_skip_quote (const gchar *str,
335 void extract_address (gchar *str);
336 void extract_list_id_str (gchar *str);
338 GSList *address_list_append (GSList *addr_list,
340 GSList *address_list_append_with_comments(GSList *addr_list,
342 GSList *references_list_prepend (GSList *msgid_list,
344 GSList *references_list_append (GSList *msgid_list,
346 GSList *newsgroup_list_append (GSList *group_list,
349 GList *add_history (GList *list,
352 void remove_return (gchar *str);
353 void remove_space (gchar *str);
354 void unfold_line (gchar *str);
355 void subst_char (gchar *str,
358 void subst_chars (gchar *str,
361 void subst_for_filename (gchar *str);
362 void subst_for_shellsafe_filename (gchar *str);
363 gboolean is_ascii_str (const gchar *str);
364 gint get_quote_level (const gchar *str,
365 const gchar *quote_chars);
366 gint check_line_length (const gchar *str,
370 gchar **strsplit_with_quote (const gchar *str,
374 gchar *get_abbrev_newsgroup_name (const gchar *group,
376 gchar *trim_string (const gchar *str,
379 GList *uri_list_extract_filenames (const gchar *uri_list);
380 gboolean is_uri_string (const gchar *str);
381 gchar *get_uri_path (const gchar *uri);
382 gint get_uri_len (const gchar *str);
383 void decode_uri (gchar *decoded_uri,
384 const gchar *encoded_uri);
385 void decode_uri_with_plus (gchar *decoded_uri,
386 const gchar *encoded_uri,
388 gint scan_mailto_url (const gchar *mailto,
398 /* return static strings */
399 const gchar *get_home_dir (void);
400 const gchar *get_rc_dir (void);
401 void set_rc_dir (const gchar *dir);
402 gboolean rc_dir_is_alt (void);
403 const gchar *get_mail_base_dir (void);
404 const gchar *get_news_cache_dir (void);
405 const gchar *get_imap_cache_dir (void);
406 const gchar *get_mime_tmp_dir (void);
407 const gchar *get_template_dir (void);
408 const gchar *get_plugin_dir (void);
409 const gchar *get_tmp_dir (void);
410 const gchar *get_locale_dir (void);
411 gchar *get_tmp_file (void);
412 const gchar *get_domain_name (void);
413 const gchar *get_desktop_file(void);
415 const gchar *w32_get_themes_dir (void);
416 const gchar *w32_get_cert_file (void);
418 /* file / directory handling */
419 off_t get_file_size (const gchar *file);
420 time_t get_file_mtime (const gchar *file);
422 gboolean file_exist (const gchar *file,
423 gboolean allow_fifo);
424 gboolean is_relative_filename (const gchar *file);
425 gboolean is_dir_exist (const gchar *dir);
426 gboolean is_file_entry_exist (const gchar *file);
427 gboolean dirent_is_regular_file (struct dirent *d);
429 #define is_file_exist(file) file_exist(file, FALSE)
430 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
432 gint change_dir (const gchar *dir);
433 gint make_dir (const gchar *dir);
434 gint make_dir_hier (const gchar *dir);
435 gint remove_all_files (const gchar *dir);
436 gint remove_numbered_files (const gchar *dir,
439 gint remove_numbered_files_not_in_list(const gchar *dir,
441 gint remove_all_numbered_files (const gchar *dir);
442 gint remove_dir_recursive (const gchar *dir);
443 gint append_file (const gchar *src,
445 gboolean keep_backup);
446 gint rename_force (const gchar *oldpath,
447 const gchar *newpath);
448 gint copy_file (const gchar *src,
450 gboolean keep_backup);
451 gint move_file (const gchar *src,
454 gint copy_dir (const gchar *src,
456 gint copy_file_part_to_fp (FILE *fp,
460 gint copy_file_part (FILE *fp,
465 gchar *canonicalize_str (const gchar *str);
466 gint canonicalize_file (const gchar *src,
468 gint canonicalize_file_replace (const gchar *file);
470 gchar *normalize_newlines (const gchar *str);
472 gchar *get_outgoing_rfc2822_str (FILE *fp);
474 gint change_file_mode_rw (FILE *fp,
476 FILE *my_tmpfile (void);
477 FILE *get_tmpfile_in_dir (const gchar *dir,
479 FILE *str_open_as_stream (const gchar *str);
480 gint str_write_to_file (const gchar *str,
482 gchar *file_read_to_str (const gchar *file);
483 gchar *file_read_stream_to_str (FILE *fp);
484 gchar *file_read_to_str_no_recode(const gchar *file);
485 gchar *file_read_stream_to_str_no_recode(FILE *fp);
487 char *fgets_crlf(char *buf, int size, FILE *stream);
489 /* process execution */
490 gint execute_command_line (const gchar *cmdline,
492 const gchar *working_directory);
493 gchar *get_command_output (const gchar *cmdline);
495 /* open URI with external browser */
496 gint open_uri(const gchar *uri, const gchar *cmdline);
497 /* open file with text editor */
498 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
501 time_t remote_tzoffset_sec (const gchar *zone);
502 time_t tzoffset_sec (time_t *now);
503 gchar *tzoffset (time_t *now);
504 void get_rfc822_date (gchar *buf,
506 void get_rfc822_date_hide_tz (gchar *buf,
509 size_t fast_strftime (gchar *buf,
515 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
516 const char * debug_srcname (const char *file);
518 /* subject threading */
519 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
520 void subject_table_insert(GHashTable *subject_table, gchar * subject,
522 void subject_table_remove(GHashTable *subject_table, gchar * subject);
523 void utils_free_regex(void);
524 gint subject_get_prefix_length (const gchar *subject);
526 /* quoting recognition */
527 const gchar * line_has_quote_char (const gchar *str,
528 const gchar *quote_chars);
530 gint g_int_compare (gconstpointer a, gconstpointer b);
532 gchar *generate_mime_boundary (const gchar *prefix);
534 gint quote_cmd_argument(gchar * result, guint size,
536 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
538 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
539 void get_hex_str(gchar *out, guchar ch);
541 /* auto pointer for containers that support GType system */
543 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
544 typedef struct AutoPointer GAuto;
545 GType g_auto_pointer_register (void);
546 GAuto *g_auto_pointer_new (gpointer pointer);
547 GAuto *g_auto_pointer_new_with_free (gpointer p,
549 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
550 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
551 void g_auto_pointer_free (GAuto *auto_ptr);
552 void replace_returns (gchar *str);
553 gboolean get_uri_part (const gchar *start,
554 const gchar *scanpos,
558 gchar *make_uri_string (const gchar *bp,
560 gboolean get_email_part (const gchar *start,
561 const gchar *scanpos,
565 gchar *make_email_string(const gchar *bp,
567 gchar *make_http_string (const gchar *bp,
570 gchar *mailcap_get_command_for_type(const gchar *type,
571 const gchar *file_to_open);
572 void mailcap_update_default (const gchar *type,
573 const gchar *command);
575 gboolean file_is_email(const gchar *filename);
576 gboolean sc_g_list_bigger(GList *list, gint max);
577 gboolean sc_g_slist_bigger(GSList *list, gint max);
579 int claws_unlink(const gchar *filename);
581 GMutex *cm_mutex_new(void);
582 void cm_mutex_free(GMutex *mutex);
584 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
586 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
588 #if !GLIB_CHECK_VERSION(2, 30, 0)
589 gchar *g_utf8_substring (const gchar *p,
591 glong end_pos) G_GNUC_MALLOC;
594 gboolean get_random_bytes(void *buf, size_t count);
600 #endif /* __UTILS_H__ */