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 #ifndef BIG_ENDIAN_HOST
79 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
80 #define BIG_ENDIAN_HOST 1
84 #define CHDIR_RETURN_IF_FAIL(dir) \
86 if (change_dir(dir) < 0) return; \
89 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
91 if (change_dir(dir) < 0) return val; \
94 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
96 if (change_dir(dir) < 0) { \
102 #define Xalloca(ptr, size, iffail) \
104 if ((ptr = alloca(size)) == NULL) { \
105 g_warning("can't allocate memory\n"); \
110 #define Xstrdup_a(ptr, str, iffail) \
114 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
115 g_warning("can't allocate memory\n"); \
118 strcpy(__tmp, str); \
123 #define Xstrndup_a(ptr, str, len, iffail) \
127 if ((__tmp = alloca(len + 1)) == NULL) { \
128 g_warning("can't allocate memory\n"); \
131 strncpy(__tmp, str, len); \
138 #define Xstrcat_a(ptr, str1, str2, iffail) \
143 len1 = strlen(str1); \
144 len2 = strlen(str2); \
145 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
146 g_warning("can't allocate memory\n"); \
149 memcpy(__tmp, str1, len1); \
150 memcpy(__tmp + len1, str2, len2 + 1); \
156 #define AUTORELEASE_STR(str, iffail) \
159 Xstrdup_a(__str, str, iffail); \
164 #define FILE_OP_ERROR(file, func) \
166 g_printerr("%s: ", file); \
171 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
173 /* from NetworkManager */
174 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
175 #define print_backtrace() \
178 void *_call_stack[512]; \
179 int _call_stack_size; \
181 _call_stack_size = backtrace (_call_stack, \
182 G_N_ELEMENTS (_call_stack)); \
183 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
184 if (_symbols != NULL) \
188 g_print ("traceback:\n"); \
189 while (_i < _call_stack_size) \
191 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
199 #define print_backtrace() \
207 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
209 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
216 #define cm_return_if_fail(expr) G_STMT_START { \
218 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
229 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
231 /* debug functions */
232 void debug_set_mode (gboolean mode);
233 gboolean debug_get_mode (void);
236 #define debug_print \
237 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
240 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
241 #define debug_print \
242 debug_print_real("%s:%d:", __FILE__, __LINE__), \
246 /* for macro expansion */
248 #define Xstr(x) Str(x)
251 /* System related stuff. */
253 gboolean superuser_p (void);
255 /* List utilities. */
257 GSList *slist_copy_deep (GSList *list,
260 /* String utilities. */
262 void list_free_strings (GList *list);
263 void slist_free_strings (GSList *list);
264 void slist_free_strings_full (GSList *list);
266 void hash_free_strings (GHashTable *table);
268 gint str_case_equal (gconstpointer v,
270 guint str_case_hash (gconstpointer key);
272 void ptr_array_free_strings (GPtrArray *array);
274 /* number-string conversion */
275 gint to_number (const gchar *nstr);
276 gchar *itos_buf (gchar *nstr,
278 gchar *itos (gint n);
279 gchar *to_human_readable (goffset size);
281 /* alternative string functions */
282 gint strcmp2 (const gchar *s1,
284 gchar *strstr2 (const gchar *s1,
286 gint path_cmp (const gchar *s1,
288 gchar *strretchomp (gchar *str);
289 gchar *strtailchomp (gchar *str,
291 gchar *strcrchomp (gchar *str);
292 gint file_strip_crs (const gchar *file);
293 gchar *strcasestr (const gchar *haystack,
294 const gchar *needle);
295 gchar *strncasestr (const gchar *haystack,
297 const gchar *needle);
298 gpointer my_memmem (gconstpointer haystack,
300 gconstpointer needle,
302 gchar *strncpy2 (gchar *dest,
306 gboolean is_next_nonascii (const gchar *s);
307 gint get_next_word_len (const gchar *s);
309 /* functions for string parsing */
310 gint subject_compare (const gchar *s1,
312 gint subject_compare_for_sort (const gchar *s1,
314 void trim_subject (gchar *str);
315 void eliminate_parenthesis (gchar *str,
318 void extract_parenthesis (gchar *str,
322 void extract_quote (gchar *str,
324 gchar *escape_internal_quotes (gchar *str,
326 void eliminate_address_comment (gchar *str);
327 gchar *strchr_with_skip_quote (const gchar *str,
330 void extract_address (gchar *str);
331 void extract_list_id_str (gchar *str);
333 GSList *address_list_append (GSList *addr_list,
335 GSList *address_list_append_with_comments(GSList *addr_list,
337 GSList *references_list_prepend (GSList *msgid_list,
339 GSList *references_list_append (GSList *msgid_list,
341 GSList *newsgroup_list_append (GSList *group_list,
344 GList *add_history (GList *list,
347 void remove_return (gchar *str);
348 void remove_space (gchar *str);
349 void unfold_line (gchar *str);
350 void subst_char (gchar *str,
353 void subst_chars (gchar *str,
356 void subst_for_filename (gchar *str);
357 void subst_for_shellsafe_filename (gchar *str);
358 gboolean is_ascii_str (const gchar *str);
359 gint get_quote_level (const gchar *str,
360 const gchar *quote_chars);
361 gint check_line_length (const gchar *str,
365 gchar **strsplit_with_quote (const gchar *str,
369 gchar *get_abbrev_newsgroup_name (const gchar *group,
371 gchar *trim_string (const gchar *str,
374 GList *uri_list_extract_filenames (const gchar *uri_list);
375 gboolean is_uri_string (const gchar *str);
376 gchar *get_uri_path (const gchar *uri);
377 gint get_uri_len (const gchar *str);
378 void decode_uri (gchar *decoded_uri,
379 const gchar *encoded_uri);
380 void decode_uri_with_plus (gchar *decoded_uri,
381 const gchar *encoded_uri,
383 gint scan_mailto_url (const gchar *mailto,
393 /* return static strings */
394 const gchar *get_home_dir (void);
395 const gchar *get_rc_dir (void);
396 void set_rc_dir (const gchar *dir);
397 gboolean rc_dir_is_alt (void);
398 const gchar *get_mail_base_dir (void);
399 const gchar *get_news_cache_dir (void);
400 const gchar *get_imap_cache_dir (void);
401 const gchar *get_mime_tmp_dir (void);
402 const gchar *get_template_dir (void);
403 const gchar *get_plugin_dir (void);
404 const gchar *get_tmp_dir (void);
405 const gchar *get_locale_dir (void);
406 gchar *get_tmp_file (void);
407 const gchar *get_domain_name (void);
408 const gchar *get_desktop_file(void);
410 const gchar *get_themes_dir (void);
411 const gchar *get_cert_file (void);
413 /* file / directory handling */
414 off_t get_file_size (const gchar *file);
415 off_t get_file_size_as_crlf (const gchar *file);
417 time_t get_file_mtime (const gchar *file);
419 gboolean file_exist (const gchar *file,
420 gboolean allow_fifo);
421 gboolean is_relative_filename (const gchar *file);
422 gboolean is_dir_exist (const gchar *dir);
423 gboolean is_file_entry_exist (const gchar *file);
424 gboolean dirent_is_regular_file (struct dirent *d);
426 #define is_file_exist(file) file_exist(file, FALSE)
427 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
429 gint change_dir (const gchar *dir);
430 gint make_dir (const gchar *dir);
431 gint make_dir_hier (const gchar *dir);
432 gint remove_all_files (const gchar *dir);
433 gint remove_numbered_files (const gchar *dir,
436 gint remove_numbered_files_not_in_list(const gchar *dir,
438 gint remove_all_numbered_files (const gchar *dir);
439 gint remove_dir_recursive (const gchar *dir);
440 gint append_file (const gchar *src,
442 gboolean keep_backup);
443 gint rename_force (const gchar *oldpath,
444 const gchar *newpath);
445 gint copy_file (const gchar *src,
447 gboolean keep_backup);
448 gint move_file (const gchar *src,
451 gint copy_dir (const gchar *src,
453 gint copy_file_part_to_fp (FILE *fp,
457 gint copy_file_part (FILE *fp,
462 gchar *canonicalize_str (const gchar *str);
463 gint canonicalize_file (const gchar *src,
465 gint canonicalize_file_replace (const gchar *file);
467 gchar *normalize_newlines (const gchar *str);
469 gchar *get_outgoing_rfc2822_str (FILE *fp);
471 gint change_file_mode_rw (FILE *fp,
473 FILE *my_tmpfile (void);
474 FILE *get_tmpfile_in_dir (const gchar *dir,
476 FILE *str_open_as_stream (const gchar *str);
477 gint str_write_to_file (const gchar *str,
479 gchar *file_read_to_str (const gchar *file);
480 gchar *file_read_stream_to_str (FILE *fp);
481 gchar *file_read_to_str_no_recode(const gchar *file);
482 gchar *file_read_stream_to_str_no_recode(FILE *fp);
484 char *fgets_crlf(char *buf, int size, FILE *stream);
486 /* process execution */
487 gint execute_command_line (const gchar *cmdline,
489 gchar *get_command_output (const gchar *cmdline);
491 /* open URI with external browser */
492 gint open_uri(const gchar *uri, const gchar *cmdline);
493 /* open file with text editor */
494 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
497 time_t remote_tzoffset_sec (const gchar *zone);
498 time_t tzoffset_sec (time_t *now);
499 gchar *tzoffset (time_t *now);
500 void get_rfc822_date (gchar *buf,
503 size_t fast_strftime (gchar *buf,
509 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
510 const char * debug_srcname (const char *file);
512 /* subject threading */
513 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
514 void subject_table_insert(GHashTable *subject_table, gchar * subject,
516 void subject_table_remove(GHashTable *subject_table, gchar * subject);
517 void utils_free_regex(void);
518 gint subject_get_prefix_length (const gchar *subject);
520 /* quoting recognition */
521 const gchar * line_has_quote_char (const gchar *str,
522 const gchar *quote_chars);
524 gint g_int_compare (gconstpointer a, gconstpointer b);
526 gchar *generate_msgid (gchar *buf, gint len, gchar *user_addr);
527 gchar *generate_mime_boundary (const gchar *prefix);
529 gint quote_cmd_argument(gchar * result, guint size,
531 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
533 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
534 void get_hex_str(gchar *out, guchar ch);
536 /* auto pointer for containers that support GType system */
538 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
539 typedef struct AutoPointer GAuto;
540 GType g_auto_pointer_register (void);
541 GAuto *g_auto_pointer_new (gpointer pointer);
542 GAuto *g_auto_pointer_new_with_free (gpointer p,
544 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
545 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
546 void g_auto_pointer_free (GAuto *auto_ptr);
547 void replace_returns (gchar *str);
548 gboolean get_uri_part (const gchar *start,
549 const gchar *scanpos,
553 gchar *make_uri_string (const gchar *bp,
555 gboolean get_email_part (const gchar *start,
556 const gchar *scanpos,
560 gchar *make_email_string(const gchar *bp,
562 gchar *make_http_string (const gchar *bp,
565 gchar *mailcap_get_command_for_type(const gchar *type,
566 const gchar *file_to_open);
567 void mailcap_update_default (const gchar *type,
568 const gchar *command);
570 gboolean file_is_email(const gchar *filename);
571 gboolean sc_g_list_bigger(GList *list, gint max);
572 gboolean sc_g_slist_bigger(GSList *list, gint max);
574 int claws_unlink(const gchar *filename);
576 GMutex *cm_mutex_new(void);
577 void cm_mutex_free(GMutex *mutex);
579 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
581 #if !GLIB_CHECK_VERSION(2, 30, 0)
582 gchar *g_utf8_substring (const gchar *p,
584 glong end_pos) G_GNUC_MALLOC;
591 #endif /* __UTILS_H__ */