2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
24 #include "claws-features.h"
32 #include <glib-object.h>
37 #include <sys/types.h>
51 #if GLIB_CHECK_VERSION (2, 13, 2)
52 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
55 /* Wrappers for C library function that take pathname arguments. */
56 #if GLIB_CHECK_VERSION(2, 6, 0)
57 # include <glib/gstdio.h>
61 #define g_rename rename
65 #define g_unlink unlink
66 #define g_remove remove
69 #define g_freopen freopen
71 #endif /* GLIB_CHECK_VERSION */
73 #if !GLIB_CHECK_VERSION(2, 7, 0)
79 gint g_chdir (const gchar *path);
80 gint g_chmod (const gchar *path,
82 #endif /* G_OS_UNIX */
84 #endif /* !GLIB_CHECK_VERSION */
86 /* why is this sometimes undefined !? */
88 typedef gint64 goffset;
89 #define G_MINOFFSET G_MININT64
90 #define G_MAXOFFSET G_MAXINT64
93 #ifndef HAVE_U32_TYPEDEF
94 #undef u32 /* maybe there is a macro with this name */
96 #define HAVE_U32_TYPEDEF
99 #ifndef BIG_ENDIAN_HOST
100 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
101 #define BIG_ENDIAN_HOST 1
105 #define CHDIR_RETURN_IF_FAIL(dir) \
107 if (change_dir(dir) < 0) return; \
110 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
112 if (change_dir(dir) < 0) return val; \
115 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
117 if (change_dir(dir) < 0) { \
123 #define Xalloca(ptr, size, iffail) \
125 if ((ptr = alloca(size)) == NULL) { \
126 g_warning("can't allocate memory\n"); \
131 #define Xstrdup_a(ptr, str, iffail) \
135 if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
136 g_warning("can't allocate memory\n"); \
139 strcpy(__tmp, str); \
144 #define Xstrndup_a(ptr, str, len, iffail) \
148 if ((__tmp = alloca(len + 1)) == NULL) { \
149 g_warning("can't allocate memory\n"); \
152 strncpy(__tmp, str, len); \
159 #define Xstrcat_a(ptr, str1, str2, iffail) \
164 len1 = strlen(str1); \
165 len2 = strlen(str2); \
166 if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
167 g_warning("can't allocate memory\n"); \
170 memcpy(__tmp, str1, len1); \
171 memcpy(__tmp + len1, str2, len2 + 1); \
177 #define AUTORELEASE_STR(str, iffail) \
180 Xstrdup_a(__str, str, iffail); \
185 #define FILE_OP_ERROR(file, func) \
187 g_printerr("%s: ", file); \
192 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
194 /* from NetworkManager */
195 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
196 #define print_backtrace() \
199 void *_call_stack[512]; \
200 int _call_stack_size; \
202 _call_stack_size = backtrace (_call_stack, \
203 G_N_ELEMENTS (_call_stack)); \
204 _symbols = backtrace_symbols (_call_stack, _call_stack_size); \
205 if (_symbols != NULL) \
209 g_print ("traceback:\n"); \
210 while (_i < _call_stack_size) \
212 g_print ("%d:\t%s\n", _i, _symbols[_i]); \
220 #define print_backtrace() \
228 #define cm_return_val_if_fail(expr,val) G_STMT_START { \
230 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
237 #define cm_return_if_fail(expr) G_STMT_START { \
239 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
250 typedef gpointer (*GNodeMapFunc) (gpointer nodedata, gpointer data);
252 /* debug functions */
253 void debug_set_mode (gboolean mode);
254 gboolean debug_get_mode (void);
257 #define debug_print \
258 debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
261 /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
262 #define debug_print \
263 debug_print_real("%s:%d:", __FILE__, __LINE__), \
267 /* for macro expansion */
269 #define Xstr(x) Str(x)
272 /* System related stuff. */
274 gboolean superuser_p (void);
277 /* String utilities. */
279 void list_free_strings (GList *list);
280 void slist_free_strings (GSList *list);
282 void hash_free_strings (GHashTable *table);
284 gint str_case_equal (gconstpointer v,
286 guint str_case_hash (gconstpointer key);
288 void ptr_array_free_strings (GPtrArray *array);
290 /* number-string conversion */
291 gint to_number (const gchar *nstr);
292 gchar *itos_buf (gchar *nstr,
294 gchar *itos (gint n);
295 gchar *to_human_readable (goffset size);
297 /* alternative string functions */
298 gint strcmp2 (const gchar *s1,
300 gchar *strstr2 (const gchar *s1,
302 gint path_cmp (const gchar *s1,
304 gchar *strretchomp (gchar *str);
305 gchar *strtailchomp (gchar *str,
307 gchar *strcrchomp (gchar *str);
308 gint file_strip_crs (const gchar *file);
309 gchar *strcasestr (const gchar *haystack,
310 const gchar *needle);
311 gpointer my_memmem (gconstpointer haystack,
313 gconstpointer needle,
315 gchar *strncpy2 (gchar *dest,
319 gboolean is_next_nonascii (const gchar *s);
320 gint get_next_word_len (const gchar *s);
322 /* functions for string parsing */
323 gint subject_compare (const gchar *s1,
325 gint subject_compare_for_sort (const gchar *s1,
327 void trim_subject (gchar *str);
328 void eliminate_parenthesis (gchar *str,
331 void extract_parenthesis (gchar *str,
335 void extract_quote (gchar *str,
337 void eliminate_address_comment (gchar *str);
338 gchar *strchr_with_skip_quote (const gchar *str,
341 void extract_address (gchar *str);
342 void extract_list_id_str (gchar *str);
344 GSList *address_list_append (GSList *addr_list,
346 GSList *address_list_append_with_comments(GSList *addr_list,
348 GSList *references_list_prepend (GSList *msgid_list,
350 GSList *references_list_append (GSList *msgid_list,
352 GSList *newsgroup_list_append (GSList *group_list,
355 GList *add_history (GList *list,
358 void remove_return (gchar *str);
359 void remove_space (gchar *str);
360 void unfold_line (gchar *str);
361 void subst_char (gchar *str,
364 void subst_chars (gchar *str,
367 void subst_for_filename (gchar *str);
368 void subst_for_shellsafe_filename (gchar *str);
369 gboolean is_ascii_str (const gchar *str);
370 gint get_quote_level (const gchar *str,
371 const gchar *quote_chars);
372 gint check_line_length (const gchar *str,
376 gchar **strsplit_with_quote (const gchar *str,
380 gchar *get_abbrev_newsgroup_name (const gchar *group,
382 gchar *trim_string (const gchar *str,
385 GList *uri_list_extract_filenames (const gchar *uri_list);
386 gboolean is_uri_string (const gchar *str);
387 gchar *get_uri_path (const gchar *uri);
388 gint get_uri_len (const gchar *str);
389 void decode_uri (gchar *decoded_uri,
390 const gchar *encoded_uri);
391 void decode_uri_with_plus (gchar *decoded_uri,
392 const gchar *encoded_uri,
394 gint scan_mailto_url (const gchar *mailto,
404 /* return static strings */
405 const gchar *get_home_dir (void);
406 const gchar *get_rc_dir (void);
407 void set_rc_dir (const gchar *dir);
408 gboolean rc_dir_is_alt (void);
409 const gchar *get_mail_base_dir (void);
410 const gchar *get_news_cache_dir (void);
411 const gchar *get_imap_cache_dir (void);
412 const gchar *get_mime_tmp_dir (void);
413 const gchar *get_template_dir (void);
414 const gchar *get_plugin_dir (void);
415 const gchar *get_tmp_dir (void);
416 const gchar *get_locale_dir (void);
417 gchar *get_tmp_file (void);
418 const gchar *get_domain_name (void);
419 const gchar *get_desktop_file(void);
421 const gchar *get_themes_dir (void);
422 const gchar *get_cert_file (void);
424 /* file / directory handling */
425 off_t get_file_size (const gchar *file);
426 off_t get_file_size_as_crlf (const gchar *file);
428 time_t get_file_mtime (const gchar *file);
430 gboolean file_exist (const gchar *file,
431 gboolean allow_fifo);
432 gboolean is_relative_filename (const gchar *file);
433 gboolean is_dir_exist (const gchar *dir);
434 gboolean is_file_entry_exist (const gchar *file);
435 gboolean dirent_is_regular_file (struct dirent *d);
437 #define is_file_exist(file) file_exist(file, FALSE)
438 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
440 gint change_dir (const gchar *dir);
441 gint make_dir (const gchar *dir);
442 gint make_dir_hier (const gchar *dir);
443 gint remove_all_files (const gchar *dir);
444 gint remove_numbered_files (const gchar *dir,
447 gint remove_numbered_files_not_in_list(const gchar *dir,
449 gint remove_all_numbered_files (const gchar *dir);
450 gint remove_dir_recursive (const gchar *dir);
451 gint append_file (const gchar *src,
453 gboolean keep_backup);
454 gint rename_force (const gchar *oldpath,
455 const gchar *newpath);
456 gint copy_file (const gchar *src,
458 gboolean keep_backup);
459 gint move_file (const gchar *src,
462 gint copy_dir (const gchar *src,
464 gint copy_file_part_to_fp (FILE *fp,
468 gint copy_file_part (FILE *fp,
473 gchar *canonicalize_str (const gchar *str);
474 gint canonicalize_file (const gchar *src,
476 gint canonicalize_file_replace (const gchar *file);
478 gchar *normalize_newlines (const gchar *str);
480 gchar *get_outgoing_rfc2822_str (FILE *fp);
482 gint change_file_mode_rw (FILE *fp,
484 FILE *my_tmpfile (void);
485 FILE *get_tmpfile_in_dir (const gchar *dir,
487 FILE *str_open_as_stream (const gchar *str);
488 gint str_write_to_file (const gchar *str,
490 gchar *file_read_to_str (const gchar *file);
491 gchar *file_read_stream_to_str (FILE *fp);
492 gchar *file_read_to_str_no_recode(const gchar *file);
493 gchar *file_read_stream_to_str_no_recode(FILE *fp);
495 char *fgets_crlf(char *buf, int size, FILE *stream);
497 /* process execution */
498 gint execute_command_line (const gchar *cmdline,
500 gchar *get_command_output (const gchar *cmdline);
502 /* open URI with external browser */
503 gint open_uri(const gchar *uri, const gchar *cmdline);
504 /* open file with text editor */
505 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
508 time_t remote_tzoffset_sec (const gchar *zone);
509 time_t tzoffset_sec (time_t *now);
510 gchar *tzoffset (time_t *now);
511 void get_rfc822_date (gchar *buf,
514 size_t fast_strftime (gchar *buf,
520 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
521 const char * debug_srcname (const char *file);
523 /* subject threading */
524 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
525 void subject_table_insert(GHashTable *subject_table, gchar * subject,
527 void subject_table_remove(GHashTable *subject_table, gchar * subject);
528 void utils_free_regex(void);
529 gint subject_get_prefix_length (const gchar *subject);
531 /* quoting recognition */
532 const gchar * line_has_quote_char (const gchar *str,
533 const gchar *quote_chars);
535 gint g_int_compare (gconstpointer a, gconstpointer b);
537 gchar *generate_msgid (gchar *buf, gint len, gchar *user_addr);
538 gchar *generate_mime_boundary (const gchar *prefix);
540 gint quote_cmd_argument(gchar * result, guint size,
542 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
544 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
545 void get_hex_str(gchar *out, guchar ch);
547 /* auto pointer for containers that support GType system */
549 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
550 typedef struct AutoPointer GAuto;
551 GType g_auto_pointer_register (void);
552 GAuto *g_auto_pointer_new (gpointer pointer);
553 GAuto *g_auto_pointer_new_with_free (gpointer p,
555 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
556 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
557 void g_auto_pointer_free (GAuto *auto_ptr);
558 void replace_returns (gchar *str);
559 gboolean get_uri_part (const gchar *start,
560 const gchar *scanpos,
564 gchar *make_uri_string (const gchar *bp,
566 gboolean get_email_part (const gchar *start,
567 const gchar *scanpos,
571 gchar *make_email_string(const gchar *bp,
573 gchar *make_http_string (const gchar *bp,
576 gchar *mailcap_get_command_for_type(const gchar *type,
577 const gchar *file_to_open);
578 void mailcap_update_default (const gchar *type,
579 const gchar *command);
581 gboolean file_is_email(const gchar *filename);
582 gboolean sc_g_list_bigger(GList *list, gint max);
583 gboolean sc_g_slist_bigger(GSList *list, gint max);
585 int claws_unlink(const gchar *filename);
590 #endif /* __UTILS_H__ */