2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 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/>.
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 */
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 typedef gboolean (*StrFindFunc) (const gchar *haystack,
291 const gchar *needle);
293 gboolean str_find (const gchar *haystack,
294 const gchar *needle);
295 gboolean str_case_find (const gchar *haystack,
296 const gchar *needle);
297 /* number-string conversion */
298 gint to_number (const gchar *nstr);
299 gchar *itos_buf (gchar *nstr,
301 gchar *itos (gint n);
302 gchar *to_human_readable (goffset size);
304 /* alternative string functions */
305 gint strcmp2 (const gchar *s1,
307 gchar *strstr2 (const gchar *s1,
309 gint path_cmp (const gchar *s1,
311 gchar *strretchomp (gchar *str);
312 gchar *strtailchomp (gchar *str,
314 gchar *strcrchomp (gchar *str);
315 gint file_strip_crs (const gchar *file);
316 gchar *strcasestr (const gchar *haystack,
317 const gchar *needle);
318 gpointer my_memmem (gconstpointer haystack,
320 gconstpointer needle,
322 gchar *strncpy2 (gchar *dest,
326 gboolean is_next_nonascii (const gchar *s);
327 gint get_next_word_len (const gchar *s);
329 /* functions for string parsing */
330 gint subject_compare (const gchar *s1,
332 gint subject_compare_for_sort (const gchar *s1,
334 void trim_subject (gchar *str);
335 void eliminate_parenthesis (gchar *str,
338 void extract_parenthesis (gchar *str,
342 void extract_quote (gchar *str,
344 void eliminate_address_comment (gchar *str);
345 gchar *strchr_with_skip_quote (const gchar *str,
348 void extract_address (gchar *str);
349 void extract_list_id_str (gchar *str);
351 GSList *address_list_append (GSList *addr_list,
353 GSList *address_list_append_with_comments(GSList *addr_list,
355 GSList *references_list_prepend (GSList *msgid_list,
357 GSList *references_list_append (GSList *msgid_list,
359 GSList *newsgroup_list_append (GSList *group_list,
362 GList *add_history (GList *list,
365 void remove_return (gchar *str);
366 void remove_space (gchar *str);
367 void unfold_line (gchar *str);
368 void subst_char (gchar *str,
371 void subst_chars (gchar *str,
374 void subst_for_filename (gchar *str);
375 void subst_for_shellsafe_filename (gchar *str);
376 gboolean is_ascii_str (const gchar *str);
377 gint get_quote_level (const gchar *str,
378 const gchar *quote_chars);
379 gint check_line_length (const gchar *str,
383 gchar **strsplit_with_quote (const gchar *str,
387 gchar *get_abbrev_newsgroup_name (const gchar *group,
389 gchar *trim_string (const gchar *str,
392 GList *uri_list_extract_filenames (const gchar *uri_list);
393 gboolean is_uri_string (const gchar *str);
394 gchar *get_uri_path (const gchar *uri);
395 gint get_uri_len (const gchar *str);
396 void decode_uri (gchar *decoded_uri,
397 const gchar *encoded_uri);
398 void decode_uri_with_plus (gchar *decoded_uri,
399 const gchar *encoded_uri,
401 gint scan_mailto_url (const gchar *mailto,
410 /* return static strings */
411 const gchar *get_home_dir (void);
412 const gchar *get_rc_dir (void);
413 void set_rc_dir (const gchar *dir);
414 gboolean rc_dir_is_alt (void);
415 const gchar *get_mail_base_dir (void);
416 const gchar *get_news_cache_dir (void);
417 const gchar *get_imap_cache_dir (void);
418 const gchar *get_mime_tmp_dir (void);
419 const gchar *get_template_dir (void);
420 const gchar *get_plugin_dir (void);
421 const gchar *get_tmp_dir (void);
422 const gchar *get_locale_dir (void);
423 gchar *get_tmp_file (void);
424 const gchar *get_domain_name (void);
425 const gchar *get_desktop_file(void);
427 const gchar *get_themes_dir (void);
428 const gchar *get_cert_file (void);
430 /* file / directory handling */
431 off_t get_file_size (const gchar *file);
432 off_t get_file_size_as_crlf (const gchar *file);
434 time_t get_file_mtime (const gchar *file);
436 gboolean file_exist (const gchar *file,
437 gboolean allow_fifo);
438 gboolean is_relative_filename (const gchar *file);
439 gboolean is_dir_exist (const gchar *dir);
440 gboolean is_file_entry_exist (const gchar *file);
441 gboolean dirent_is_regular_file (struct dirent *d);
443 #define is_file_exist(file) file_exist(file, FALSE)
444 #define is_file_or_fifo_exist(file) file_exist(file, TRUE)
446 gint change_dir (const gchar *dir);
447 gint make_dir (const gchar *dir);
448 gint make_dir_hier (const gchar *dir);
449 gint remove_all_files (const gchar *dir);
450 gint remove_numbered_files (const gchar *dir,
453 gint remove_numbered_files_not_in_list(const gchar *dir,
455 gint remove_all_numbered_files (const gchar *dir);
456 gint remove_dir_recursive (const gchar *dir);
457 gint append_file (const gchar *src,
459 gboolean keep_backup);
460 gint rename_force (const gchar *oldpath,
461 const gchar *newpath);
462 gint copy_file (const gchar *src,
464 gboolean keep_backup);
465 gint move_file (const gchar *src,
468 gint copy_dir (const gchar *src,
470 gint copy_file_part_to_fp (FILE *fp,
474 gint copy_file_part (FILE *fp,
479 gchar *canonicalize_str (const gchar *str);
480 gint canonicalize_file (const gchar *src,
482 gint canonicalize_file_replace (const gchar *file);
484 gchar *normalize_newlines (const gchar *str);
486 gchar *get_outgoing_rfc2822_str (FILE *fp);
488 gint change_file_mode_rw (FILE *fp,
490 FILE *my_tmpfile (void);
491 FILE *get_tmpfile_in_dir (const gchar *dir,
493 FILE *str_open_as_stream (const gchar *str);
494 gint str_write_to_file (const gchar *str,
496 gchar *file_read_to_str (const gchar *file);
497 gchar *file_read_stream_to_str (FILE *fp);
498 gchar *file_read_to_str_no_recode(const gchar *file);
499 gchar *file_read_stream_to_str_no_recode(FILE *fp);
501 char *fgets_crlf(char *buf, int size, FILE *stream);
503 /* process execution */
504 gint execute_command_line (const gchar *cmdline,
506 gchar *get_command_output (const gchar *cmdline);
508 /* open URI with external browser */
509 gint open_uri(const gchar *uri, const gchar *cmdline);
510 /* open file with text editor */
511 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
514 time_t remote_tzoffset_sec (const gchar *zone);
515 time_t tzoffset_sec (time_t *now);
516 gchar *tzoffset (time_t *now);
517 void get_rfc822_date (gchar *buf,
520 size_t fast_strftime (gchar *buf,
526 void debug_print_real (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
527 const char * debug_srcname (const char *file);
529 /* subject threading */
530 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
531 void subject_table_insert(GHashTable *subject_table, gchar * subject,
533 void subject_table_remove(GHashTable *subject_table, gchar * subject);
534 void utils_free_regex(void);
535 gint subject_get_prefix_length (const gchar *subject);
537 /* quoting recognition */
538 const gchar * line_has_quote_char (const gchar *str,
539 const gchar *quote_chars);
541 gint g_int_compare (gconstpointer a, gconstpointer b);
543 gchar *generate_msgid (gchar *buf, gint len, gchar *user_addr);
544 gchar *generate_mime_boundary (const gchar *prefix);
546 gint quote_cmd_argument(gchar * result, guint size,
548 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
550 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
551 void get_hex_str(gchar *out, guchar ch);
553 /* auto pointer for containers that support GType system */
555 #define G_TYPE_AUTO_POINTER g_auto_pointer_register()
556 typedef struct AutoPointer GAuto;
557 GType g_auto_pointer_register (void);
558 GAuto *g_auto_pointer_new (gpointer pointer);
559 GAuto *g_auto_pointer_new_with_free (gpointer p,
561 gpointer g_auto_pointer_get_ptr (GAuto *auto_ptr);
562 GAuto *g_auto_pointer_copy (GAuto *auto_ptr);
563 void g_auto_pointer_free (GAuto *auto_ptr);
564 void replace_returns (gchar *str);
565 gboolean get_uri_part (const gchar *start,
566 const gchar *scanpos,
570 gchar *make_uri_string (const gchar *bp,
572 gboolean get_email_part (const gchar *start,
573 const gchar *scanpos,
577 gchar *make_email_string(const gchar *bp,
579 gchar *make_http_string (const gchar *bp,
582 gchar *mailcap_get_command_for_type(const gchar *type,
583 const gchar *file_to_open);
584 void mailcap_update_default (const gchar *type,
585 const gchar *command);
587 gboolean file_is_email(const gchar *filename);
588 gboolean sc_g_list_bigger(GList *list, gint max);
589 gboolean sc_g_slist_bigger(GSList *list, gint max);
591 int claws_unlink(const gchar *filename);
596 #endif /* __UTILS_H__ */