2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 The Claws Mail Team and Hiroyuki Yamamoto
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>
49 /* The Hurd doesn't have these limits */
54 #define HOST_NAME_MAX 256
57 /* Handling Base64 content in procmime and prefs_customheader */
58 #define B64_LINE_SIZE 57
64 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
66 /* Wrappers for C library function that take pathname arguments. */
67 # include <glib/gstdio.h>
69 /* why is this sometimes undefined !? */
71 typedef gint64 goffset
;
72 #define G_MINOFFSET G_MININT64
73 #define G_MAXOFFSET G_MAXINT64
76 #ifndef BIG_ENDIAN_HOST
77 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
78 #define BIG_ENDIAN_HOST 1
82 #define CHDIR_RETURN_IF_FAIL(dir) \
84 if (change_dir(dir) < 0) return; \
87 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
89 if (change_dir(dir) < 0) return val; \
92 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
94 if (change_dir(dir) < 0) { \
100 #define MAX_ALLOCA_MEM_SIZE 102400
102 #define Xalloca(ptr, size, iffail) \
104 size_t __size = size; \
106 if (__size > MAX_ALLOCA_MEM_SIZE) { \
107 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
110 if ((ptr = alloca(__size)) == NULL) { \
111 g_warning("can't allocate memory"); \
116 #define Xstrdup_a(ptr, str, iffail) \
119 size_t __size = strlen(str); \
121 if (__size > MAX_ALLOCA_MEM_SIZE) { \
122 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
125 if ((__tmp = alloca(__size + 1)) == NULL) { \
126 g_warning("can't allocate memory"); \
129 strcpy(__tmp, str); \
134 #define Xstrndup_a(ptr, str, len, iffail) \
137 size_t __size = len; \
139 if (__size > MAX_ALLOCA_MEM_SIZE) { \
140 g_warning("%" G_GSIZE_FORMAT "bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
143 if ((__tmp = alloca(__size + 1)) == NULL) { \
144 g_warning("can't allocate memory"); \
147 memcpy(__tmp, str, __size); \
148 __tmp[__size] = '\0'; \
154 #define Xstrcat_a(ptr, str1, str2, iffail) \
159 len1 = strlen(str1); \
160 len2 = strlen(str2); \
161 if (len1 + len2 > MAX_ALLOCA_MEM_SIZE) { \
162 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", len1 + len2, MAX_ALLOCA_MEM_SIZE); \
165 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
166 g_warning("can't allocate memory"); \
169 memcpy(__tmp, str1, len1); \
170 memcpy(__tmp + len1, str2, len2 + 1); \
176 #define AUTORELEASE_STR(str, iffail) \
179 Xstrdup_a(__str, str, iffail); \
184 #define FILE_OP_ERROR(file, func) \
186 g_printerr("%s: ", file); \
191 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
193 /* from NetworkManager */
194 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
195 #define print_backtrace() \
198 void *_call_stack[512]; \
199 int _call_stack_size; \
201 _call_stack_size = backtrace (_call_stack, \
202 G_N_ELEMENTS (_call_stack)); \
203 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
204 if (_symbols != NULL) \
208 g_print ("traceback:\n"); \
209 while (_i < _call_stack_size) \
211 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
219 #define print_backtrace() \
227 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
229 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
236 #define cm_return_if_fail(expr) G_STMT_START { \
238 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
246 #define MIN(a, b) ((a) < (b) ? (a) : (b))
249 #define MAX(a, b) ((a) > (b) ? (a) : (b))
256 typedef gpointer (*GNodeMapFunc
) (gpointer nodedata
, gpointer data
);
258 /* debug functions */
259 void debug_set_mode (gboolean mode
);
260 gboolean
debug_get_mode (void);
263 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format __VA_OPT__(,) __VA_ARGS__)
265 #define debug_print \
266 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
270 /* for macro expansion */
272 #define Xstr(x) Str(x)
274 /* String utilities. */
276 void list_free_strings_full (GList
*list
);
277 void slist_free_strings_full (GSList
*list
);
279 void hash_free_strings (GHashTable
*table
);
281 gint
str_case_equal (gconstpointer v
,
283 guint
str_case_hash (gconstpointer key
);
285 /* number-string conversion */
286 gint
to_number (const gchar
*nstr
);
287 gchar
*itos_buf (gchar
*nstr
,
289 gchar
*itos (gint n
);
290 gchar
*to_human_readable (goffset size
);
292 /* alternative string functions */
293 gint
path_cmp (const gchar
*s1
,
295 gchar
*strretchomp (gchar
*str
);
296 gchar
*strtailchomp (gchar
*str
,
298 gchar
*strcrchomp (gchar
*str
);
299 gchar
*strcrlftrunc (gchar
*str
);
300 #ifndef HAVE_STRCASESTR
301 gchar
*strcasestr (const gchar
*haystack
,
302 const gchar
*needle
);
303 #endif /* HAVE_STRCASESTR */
304 gchar
*strncasestr (const gchar
*haystack
,
306 const gchar
*needle
);
307 gpointer
my_memmem (gconstpointer haystack
,
309 gconstpointer needle
,
311 gchar
*strncpy2 (gchar
*dest
,
315 gboolean
is_next_nonascii (const gchar
*s
);
316 gint
get_next_word_len (const gchar
*s
);
318 /* functions for string parsing */
319 gint
subject_compare (const gchar
*s1
,
321 gint
subject_compare_for_sort (const gchar
*s1
,
323 void trim_subject (gchar
*str
);
324 void eliminate_parenthesis (gchar
*str
,
327 void extract_parenthesis (gchar
*str
,
331 void extract_quote (gchar
*str
,
333 gchar
*escape_internal_quotes (gchar
*str
,
335 void eliminate_address_comment (gchar
*str
);
336 gchar
*strchr_with_skip_quote (const gchar
*str
,
339 void extract_address (gchar
*str
);
340 void extract_list_id_str (gchar
*str
);
342 GSList
*address_list_append (GSList
*addr_list
,
344 GSList
*address_list_append_with_comments(GSList
*addr_list
,
346 GSList
*references_list_prepend (GSList
*msgid_list
,
348 GSList
*references_list_append (GSList
*msgid_list
,
350 GSList
*newsgroup_list_append (GSList
*group_list
,
353 GList
*add_history (GList
*list
,
356 void remove_return (gchar
*str
);
357 void remove_space (gchar
*str
);
358 void unfold_line (gchar
*str
);
359 void subst_char (gchar
*str
,
362 void subst_chars (gchar
*str
,
365 void subst_for_filename (gchar
*str
);
366 void subst_for_shellsafe_filename (gchar
*str
);
367 gboolean
is_ascii_str (const gchar
*str
);
368 gint
get_quote_level (const gchar
*str
,
369 const gchar
*quote_chars
);
370 gint
check_line_length (const gchar
*str
,
374 gchar
**strsplit_with_quote (const gchar
*str
,
378 gchar
*get_abbrev_newsgroup_name (const gchar
*group
,
380 gchar
*trim_string (const gchar
*str
,
383 GList
*uri_list_extract_filenames (const gchar
*uri_list
);
384 gboolean
is_uri_string (const gchar
*str
);
385 gchar
*get_uri_path (const gchar
*uri
);
386 gint
get_uri_len (const gchar
*str
);
387 void decode_uri (gchar
*decoded_uri
,
388 const gchar
*encoded_uri
);
389 void decode_uri_with_plus (gchar
*decoded_uri
,
390 const gchar
*encoded_uri
,
392 gint
scan_mailto_url (const gchar
*mailto
,
402 /* return static strings */
403 const gchar
*get_home_dir (void);
404 const gchar
*get_rc_dir (void);
405 void set_rc_dir (const gchar
*dir
);
406 gboolean
rc_dir_is_alt (void);
407 const gchar
*get_mail_base_dir (void);
408 const gchar
*get_news_cache_dir (void);
409 const gchar
*get_imap_cache_dir (void);
410 const gchar
*get_mime_tmp_dir (void);
411 const gchar
*get_template_dir (void);
412 const gchar
*get_plugin_dir (void);
413 const gchar
*get_tmp_dir (void);
414 const gchar
*get_locale_dir (void);
415 gchar
*get_tmp_file (void);
416 const gchar
*get_domain_name (void);
417 gboolean
is_numeric_host_address (const gchar
*hostaddress
);
418 const gchar
*get_desktop_file(void);
420 const gchar
*w32_get_themes_dir (void);
421 const gchar
*w32_get_cert_file (void);
423 /* file / directory handling */
424 off_t
get_file_size (const gchar
*file
);
425 time_t get_file_mtime (const gchar
*file
);
427 gboolean
file_exist (const gchar
*file
,
428 gboolean allow_fifo
);
429 gboolean
is_relative_filename (const gchar
*file
);
430 gboolean
is_dir_exist (const gchar
*dir
);
431 gboolean
is_file_entry_exist (const gchar
*file
);
432 gboolean
is_file_entry_regular(const gchar
*file
);
434 #define is_file_exist(file) file_exist(file, FALSE)
435 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
437 gint
change_dir (const gchar
*dir
);
438 gint
make_dir (const gchar
*dir
);
439 gint
make_dir_hier (const gchar
*dir
);
440 gint
remove_all_files (const gchar
*dir
);
441 gint
remove_numbered_files (const gchar
*dir
,
444 gint
remove_numbered_files_not_in_list(const gchar
*dir
,
446 gint
remove_all_numbered_files (const gchar
*dir
);
447 gint
remove_dir_recursive (const gchar
*dir
);
448 gchar
*canonicalize_str (const gchar
*str
);
449 gchar
*normalize_newlines (const gchar
*str
);
451 gchar
*get_outgoing_rfc2822_str (FILE *fp
);
453 char *fgets_crlf(char *buf
, int size
, FILE *stream
);
455 /* process execution */
456 gint
execute_command_line (const gchar
*cmdline
,
458 const gchar
*working_directory
);
459 gchar
*get_command_output (const gchar
*cmdline
);
460 FILE *get_command_output_stream (const gchar
*cmdline
);
462 /* open URI with external browser */
463 gint
open_uri(const gchar
*uri
, const gchar
*cmdline
);
464 /* open file with text editor */
465 gint
open_txt_editor(const gchar
*filepath
, const gchar
*cmdline
);
468 time_t remote_tzoffset_sec (const gchar
*zone
);
469 time_t tzoffset_sec (time_t *now
);
470 gchar
*tzoffset (time_t *now
);
471 void get_rfc822_date (gchar
*buf
,
473 void get_rfc822_date_hide_tz (gchar
*buf
,
476 size_t fast_strftime (gchar
*buf
,
483 void debug_print_real (const char *file
, int line
, const gchar
*format
, ...) G_GNUC_PRINTF(3, 4);
485 void debug_print_real (const gchar
*format
, ...) G_GNUC_PRINTF(1, 2);
487 const char * debug_srcname (const char *file
);
489 /* subject threading */
490 void * subject_table_lookup(GHashTable
*subject_table
, gchar
* subject
);
491 void subject_table_insert(GHashTable
*subject_table
, gchar
* subject
,
493 void subject_table_remove(GHashTable
*subject_table
, gchar
* subject
);
494 void utils_free_regex(void);
495 gint
subject_get_prefix_length (const gchar
*subject
);
497 /* quoting recognition */
498 const gchar
* line_has_quote_char (const gchar
*str
,
499 const gchar
*quote_chars
);
501 gint
g_int_compare (gconstpointer a
, gconstpointer b
);
503 gchar
*generate_mime_boundary (const gchar
*prefix
);
505 gint
quote_cmd_argument(gchar
* result
, guint size
,
507 GNode
*g_node_map(GNode
*node
, GNodeMapFunc func
, gpointer data
);
509 gboolean
get_hex_value(guchar
*out
, gchar c1
, gchar c2
);
510 void get_hex_str(gchar
*out
, guchar ch
);
512 /* auto pointer for containers that support GType system */
514 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
515 typedef struct AutoPointer GAuto
;
516 GType
g_auto_pointer_register (void);
517 GAuto
*g_auto_pointer_new (gpointer pointer
);
518 GAuto
*g_auto_pointer_new_with_free (gpointer p
,
520 gpointer
g_auto_pointer_get_ptr (GAuto
*auto_ptr
);
521 GAuto
*g_auto_pointer_copy (GAuto
*auto_ptr
);
522 void g_auto_pointer_free (GAuto
*auto_ptr
);
523 gboolean
get_uri_part (const gchar
*start
,
524 const gchar
*scanpos
,
528 gchar
*make_uri_string (const gchar
*bp
,
530 gboolean
get_email_part (const gchar
*start
,
531 const gchar
*scanpos
,
535 gchar
*make_email_string(const gchar
*bp
,
537 gchar
*make_http_string (const gchar
*bp
,
540 gchar
*mailcap_get_command_for_type(const gchar
*type
,
541 const gchar
*file_to_open
);
542 void mailcap_update_default (const gchar
*type
,
543 const gchar
*command
);
545 gboolean
file_is_email(const gchar
*filename
);
546 gboolean
sc_g_list_bigger(GList
*list
, gint max
);
547 gboolean
sc_g_slist_bigger(GSList
*list
, gint max
);
549 int cm_canonicalize_filename(const gchar
*filename
, gchar
**canonical_name
);
551 guchar
*g_base64_decode_zero(const gchar
*text
, gsize
*out_len
);
553 gboolean
get_random_bytes(void *buf
, size_t count
);
559 gboolean
get_serverportfp_from_filename(const gchar
*str
, gchar
**server
, gchar
**port
, gchar
**fp
);
562 gchar
*win32_debug_log_path(void);
565 #endif /* __UTILS_H__ */