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
61 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
63 /* Wrappers for C library function that take pathname arguments. */
64 # include <glib/gstdio.h>
66 /* why is this sometimes undefined !? */
68 typedef gint64 goffset
;
69 #define G_MINOFFSET G_MININT64
70 #define G_MAXOFFSET G_MAXINT64
73 #ifndef BIG_ENDIAN_HOST
74 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
75 #define BIG_ENDIAN_HOST 1
79 #define CHDIR_RETURN_IF_FAIL(dir) \
81 if (change_dir(dir) < 0) return; \
84 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
86 if (change_dir(dir) < 0) return val; \
89 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
91 if (change_dir(dir) < 0) { \
97 #define MAX_ALLOCA_MEM_SIZE 102400
99 #define Xalloca(ptr, size, iffail) \
101 size_t __size = size; \
103 if (__size > MAX_ALLOCA_MEM_SIZE) { \
104 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
107 if ((ptr = alloca(__size)) == NULL) { \
108 g_warning("can't allocate memory"); \
113 #define Xstrdup_a(ptr, str, iffail) \
116 size_t __size = strlen(str); \
118 if (__size > MAX_ALLOCA_MEM_SIZE) { \
119 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
122 if ((__tmp = alloca(__size + 1)) == NULL) { \
123 g_warning("can't allocate memory"); \
126 strcpy(__tmp, str); \
131 #define Xstrndup_a(ptr, str, len, iffail) \
134 size_t __size = len; \
136 if (__size > MAX_ALLOCA_MEM_SIZE) { \
137 g_warning("%" G_GSIZE_FORMAT "bytes exceeds max alloca size '%d'", __size, MAX_ALLOCA_MEM_SIZE); \
140 if ((__tmp = alloca(__size + 1)) == NULL) { \
141 g_warning("can't allocate memory"); \
144 memcpy(__tmp, str, __size); \
145 __tmp[__size] = '\0'; \
151 #define Xstrcat_a(ptr, str1, str2, iffail) \
156 len1 = strlen(str1); \
157 len2 = strlen(str2); \
158 if (len1 + len2 > MAX_ALLOCA_MEM_SIZE) { \
159 g_warning("%" G_GSIZE_FORMAT " bytes exceeds max alloca size '%d'", len1 + len2, MAX_ALLOCA_MEM_SIZE); \
162 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
163 g_warning("can't allocate memory"); \
166 memcpy(__tmp, str1, len1); \
167 memcpy(__tmp + len1, str2, len2 + 1); \
173 #define AUTORELEASE_STR(str, iffail) \
176 Xstrdup_a(__str, str, iffail); \
181 #define FILE_OP_ERROR(file, func) \
183 g_printerr("%s: ", file); \
188 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
190 /* from NetworkManager */
191 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
192 #define print_backtrace() \
195 void *_call_stack[512]; \
196 int _call_stack_size; \
198 _call_stack_size = backtrace (_call_stack, \
199 G_N_ELEMENTS (_call_stack)); \
200 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
201 if (_symbols != NULL) \
205 g_print ("traceback:\n"); \
206 while (_i < _call_stack_size) \
208 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
216 #define print_backtrace() \
224 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
226 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
233 #define cm_return_if_fail(expr) G_STMT_START { \
235 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
243 #define MIN(a, b) ((a) < (b) ? (a) : (b))
246 #define MAX(a, b) ((a) > (b) ? (a) : (b))
253 typedef gpointer (*GNodeMapFunc
) (gpointer nodedata
, gpointer data
);
255 /* debug functions */
256 void debug_set_mode (gboolean mode
);
257 gboolean
debug_get_mode (void);
260 #define debug_print(format, ...) debug_print_real(__FILE__, __LINE__, format __VA_OPT__(,) __VA_ARGS__)
262 #define debug_print \
263 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
267 /* for macro expansion */
269 #define Xstr(x) Str(x)
271 /* String utilities. */
273 void list_free_strings_full (GList
*list
);
274 void slist_free_strings_full (GSList
*list
);
276 void hash_free_strings (GHashTable
*table
);
278 gint
str_case_equal (gconstpointer v
,
280 guint
str_case_hash (gconstpointer key
);
282 /* number-string conversion */
283 gint
to_number (const gchar
*nstr
);
284 gchar
*itos_buf (gchar
*nstr
,
286 gchar
*itos (gint n
);
287 gchar
*to_human_readable (goffset size
);
289 /* alternative string functions */
290 gint
path_cmp (const gchar
*s1
,
292 gchar
*strretchomp (gchar
*str
);
293 gchar
*strtailchomp (gchar
*str
,
295 gchar
*strcrchomp (gchar
*str
);
296 gchar
*strcrlftrunc (gchar
*str
);
297 #ifndef HAVE_STRCASESTR
298 gchar
*strcasestr (const gchar
*haystack
,
299 const gchar
*needle
);
300 #endif /* HAVE_STRCASESTR */
301 gchar
*strncasestr (const gchar
*haystack
,
303 const gchar
*needle
);
304 gpointer
my_memmem (gconstpointer haystack
,
306 gconstpointer needle
,
308 gchar
*strncpy2 (gchar
*dest
,
312 gboolean
is_next_nonascii (const gchar
*s
);
313 gint
get_next_word_len (const gchar
*s
);
315 /* functions for string parsing */
316 gint
subject_compare (const gchar
*s1
,
318 gint
subject_compare_for_sort (const gchar
*s1
,
320 void trim_subject (gchar
*str
);
321 void eliminate_parenthesis (gchar
*str
,
324 void extract_parenthesis (gchar
*str
,
328 void extract_quote (gchar
*str
,
330 gchar
*escape_internal_quotes (gchar
*str
,
332 void eliminate_address_comment (gchar
*str
);
333 gchar
*strchr_with_skip_quote (const gchar
*str
,
336 void extract_address (gchar
*str
);
337 void extract_list_id_str (gchar
*str
);
339 GSList
*address_list_append (GSList
*addr_list
,
341 GSList
*address_list_append_with_comments(GSList
*addr_list
,
343 GSList
*references_list_prepend (GSList
*msgid_list
,
345 GSList
*references_list_append (GSList
*msgid_list
,
347 GSList
*newsgroup_list_append (GSList
*group_list
,
350 GList
*add_history (GList
*list
,
353 void remove_return (gchar
*str
);
354 void remove_space (gchar
*str
);
355 void unfold_line (gchar
*str
);
356 void subst_char (gchar
*str
,
359 void subst_chars (gchar
*str
,
362 void subst_for_filename (gchar
*str
);
363 void subst_for_shellsafe_filename (gchar
*str
);
364 gboolean
is_ascii_str (const gchar
*str
);
365 gint
get_quote_level (const gchar
*str
,
366 const gchar
*quote_chars
);
367 gint
check_line_length (const gchar
*str
,
371 gchar
**strsplit_with_quote (const gchar
*str
,
375 gchar
*get_abbrev_newsgroup_name (const gchar
*group
,
377 gchar
*trim_string (const gchar
*str
,
380 GList
*uri_list_extract_filenames (const gchar
*uri_list
);
381 gboolean
is_uri_string (const gchar
*str
);
382 gchar
*get_uri_path (const gchar
*uri
);
383 gint
get_uri_len (const gchar
*str
);
384 void decode_uri (gchar
*decoded_uri
,
385 const gchar
*encoded_uri
);
386 void decode_uri_with_plus (gchar
*decoded_uri
,
387 const gchar
*encoded_uri
,
389 gint
scan_mailto_url (const gchar
*mailto
,
399 /* return static strings */
400 const gchar
*get_home_dir (void);
401 const gchar
*get_rc_dir (void);
402 void set_rc_dir (const gchar
*dir
);
403 gboolean
rc_dir_is_alt (void);
404 const gchar
*get_mail_base_dir (void);
405 const gchar
*get_news_cache_dir (void);
406 const gchar
*get_imap_cache_dir (void);
407 const gchar
*get_mime_tmp_dir (void);
408 const gchar
*get_template_dir (void);
409 const gchar
*get_plugin_dir (void);
410 const gchar
*get_tmp_dir (void);
411 const gchar
*get_locale_dir (void);
412 gchar
*get_tmp_file (void);
413 const gchar
*get_domain_name (void);
414 gboolean
is_numeric_host_address (const gchar
*hostaddress
);
415 const gchar
*get_desktop_file(void);
417 const gchar
*w32_get_themes_dir (void);
418 const gchar
*w32_get_cert_file (void);
420 /* file / directory handling */
421 off_t
get_file_size (const gchar
*file
);
422 time_t get_file_mtime (const gchar
*file
);
424 gboolean
file_exist (const gchar
*file
,
425 gboolean allow_fifo
);
426 gboolean
is_relative_filename (const gchar
*file
);
427 gboolean
is_dir_exist (const gchar
*dir
);
428 gboolean
is_file_entry_exist (const gchar
*file
);
429 gboolean
is_file_entry_regular(const gchar
*file
);
431 #define is_file_exist(file) file_exist(file, FALSE)
432 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
434 gint
change_dir (const gchar
*dir
);
435 gint
make_dir (const gchar
*dir
);
436 gint
make_dir_hier (const gchar
*dir
);
437 gint
remove_all_files (const gchar
*dir
);
438 gint
remove_numbered_files (const gchar
*dir
,
441 gint
remove_numbered_files_not_in_list(const gchar
*dir
,
443 gint
remove_all_numbered_files (const gchar
*dir
);
444 gint
remove_dir_recursive (const gchar
*dir
);
445 gchar
*canonicalize_str (const gchar
*str
);
446 gchar
*normalize_newlines (const gchar
*str
);
448 gchar
*get_outgoing_rfc2822_str (FILE *fp
);
450 char *fgets_crlf(char *buf
, int size
, FILE *stream
);
452 /* process execution */
453 gint
execute_command_line (const gchar
*cmdline
,
455 const gchar
*working_directory
);
456 gchar
*get_command_output (const gchar
*cmdline
);
457 FILE *get_command_output_stream (const gchar
*cmdline
);
459 /* open URI with external browser */
460 gint
open_uri(const gchar
*uri
, const gchar
*cmdline
);
461 /* open file with text editor */
462 gint
open_txt_editor(const gchar
*filepath
, const gchar
*cmdline
);
465 time_t remote_tzoffset_sec (const gchar
*zone
);
466 time_t tzoffset_sec (time_t *now
);
467 gchar
*tzoffset (time_t *now
);
468 void get_rfc822_date (gchar
*buf
,
470 void get_rfc822_date_hide_tz (gchar
*buf
,
473 size_t fast_strftime (gchar
*buf
,
480 void debug_print_real (const char *file
, int line
, const gchar
*format
, ...) G_GNUC_PRINTF(3, 4);
482 void debug_print_real (const gchar
*format
, ...) G_GNUC_PRINTF(1, 2);
484 const char * debug_srcname (const char *file
);
486 /* subject threading */
487 void * subject_table_lookup(GHashTable
*subject_table
, gchar
* subject
);
488 void subject_table_insert(GHashTable
*subject_table
, gchar
* subject
,
490 void subject_table_remove(GHashTable
*subject_table
, gchar
* subject
);
491 void utils_free_regex(void);
492 gint
subject_get_prefix_length (const gchar
*subject
);
494 /* quoting recognition */
495 const gchar
* line_has_quote_char (const gchar
*str
,
496 const gchar
*quote_chars
);
498 gint
g_int_compare (gconstpointer a
, gconstpointer b
);
500 gchar
*generate_mime_boundary (const gchar
*prefix
);
502 gint
quote_cmd_argument(gchar
* result
, guint size
,
504 GNode
*g_node_map(GNode
*node
, GNodeMapFunc func
, gpointer data
);
506 gboolean
get_hex_value(guchar
*out
, gchar c1
, gchar c2
);
507 void get_hex_str(gchar
*out
, guchar ch
);
509 /* auto pointer for containers that support GType system */
511 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
512 typedef struct AutoPointer GAuto
;
513 GType
g_auto_pointer_register (void);
514 GAuto
*g_auto_pointer_new (gpointer pointer
);
515 GAuto
*g_auto_pointer_new_with_free (gpointer p
,
517 gpointer
g_auto_pointer_get_ptr (GAuto
*auto_ptr
);
518 GAuto
*g_auto_pointer_copy (GAuto
*auto_ptr
);
519 void g_auto_pointer_free (GAuto
*auto_ptr
);
520 gboolean
get_uri_part (const gchar
*start
,
521 const gchar
*scanpos
,
525 gchar
*make_uri_string (const gchar
*bp
,
527 gboolean
get_email_part (const gchar
*start
,
528 const gchar
*scanpos
,
532 gchar
*make_email_string(const gchar
*bp
,
534 gchar
*make_http_string (const gchar
*bp
,
537 gchar
*mailcap_get_command_for_type(const gchar
*type
,
538 const gchar
*file_to_open
);
539 void mailcap_update_default (const gchar
*type
,
540 const gchar
*command
);
542 gboolean
file_is_email(const gchar
*filename
);
543 gboolean
sc_g_list_bigger(GList
*list
, gint max
);
544 gboolean
sc_g_slist_bigger(GSList
*list
, gint max
);
546 int cm_canonicalize_filename(const gchar
*filename
, gchar
**canonical_name
);
548 guchar
*g_base64_decode_zero(const gchar
*text
, gsize
*out_len
);
550 gboolean
get_random_bytes(void *buf
, size_t count
);
556 gboolean
get_serverportfp_from_filename(const gchar
*str
, gchar
**server
, gchar
**port
, gchar
**fp
);
559 gchar
*win32_debug_log_path(void);
562 #endif /* __UTILS_H__ */