2005-01-24 [paul] 1.0.0cvs12.1
[claws.git] / src / common / utils.c
index 4a2115461f189ad1256c4be1c12f8f39717a98e6..348a9bac5406c8d30f4e5c51f1c36d219c152d40 100644 (file)
@@ -42,6 +42,7 @@
 #include <dirent.h>
 #include <time.h>
 #include <regex.h>
+#include <sys/utsname.h>
 
 #include "intl.h"
 #include "utils.h"
@@ -113,7 +114,7 @@ void hash_free_value_mem(GHashTable *table)
 
 gint str_case_equal(gconstpointer v, gconstpointer v2)
 {
-       return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
+       return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
 }
 
 guint str_case_hash(gconstpointer key)
@@ -143,6 +144,26 @@ void ptr_array_free_strings(GPtrArray *array)
        }
 }
 
+gboolean str_find(const gchar *haystack, const gchar *needle)
+{
+       return strstr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_case_find(const gchar *haystack, const gchar *needle)
+{
+       return strcasestr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_find_equal(const gchar *haystack, const gchar *needle)
+{
+       return strcmp(haystack, needle) == 0;
+}
+
+gboolean str_case_find_equal(const gchar *haystack, const gchar *needle)
+{
+       return strcasecmp(haystack, needle) == 0;
+}
+
 gint to_number(const gchar *nstr)
 {
        register const guchar *p;
@@ -277,7 +298,7 @@ gchar *strcasestr(const gchar *haystack, const gchar *needle)
                return NULL;
 
        while (haystack_len >= needle_len) {
-               if (!strncasecmp(haystack, needle, needle_len))
+               if (!g_ascii_strncasecmp(haystack, needle, needle_len))
                        return (gchar *)haystack;
                else {
                        haystack++;
@@ -288,6 +309,29 @@ gchar *strcasestr(const gchar *haystack, const gchar *needle)
        return NULL;
 }
 
+gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
+                  gconstpointer needle, size_t needlelen)
+{
+       const gchar *haystack_ = (const gchar *)haystack;
+       const gchar *needle_ = (const gchar *)needle;
+       const gchar *haystack_cur = (const gchar *)haystack;
+
+       if (needlelen == 1)
+               return memchr(haystack_, *needle_, haystacklen);
+
+       while ((haystack_cur = memchr(haystack_cur, *needle_, haystacklen))
+              != NULL) {
+               if (haystacklen - (haystack_cur - haystack_) < needlelen)
+                       break;
+               if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
+                       return (gpointer)haystack_cur;
+               else
+                       haystack_cur++;
+       }
+
+       return NULL;
+}
+
 /* Copy no more than N characters of SRC to DEST, with NULL terminating.  */
 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
 {
@@ -571,12 +615,12 @@ gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
        trim_subject_for_sort(str1);
        trim_subject_for_sort(str2);
 
-       return strcasecmp(str1, str2);
+       return g_utf8_collate(str1, str2);
 }
 
 void trim_subject_for_compare(gchar *str)
 {
-       guchar *srcp;
+       gchar *srcp;
 
        eliminate_parenthesis(str, '[', ']');
        eliminate_parenthesis(str, '(', ')');
@@ -589,7 +633,7 @@ void trim_subject_for_compare(gchar *str)
 
 void trim_subject_for_sort(gchar *str)
 {
-       guchar *srcp;
+       gchar *srcp;
 
        g_strstrip(str);
 
@@ -1037,7 +1081,7 @@ void subst_chars(gchar *str, gchar *orig, gchar subst)
 
 void subst_for_filename(gchar *str)
 {
-       subst_chars(str, " \t\r\n\"/\\", '_');
+       subst_chars(str, " \t\r\n\"'/\\", '_');
 }
 
 void subst_for_shellsafe_filename(gchar *str)
@@ -1449,21 +1493,6 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
        return result;
 }
 
-#define HEX_TO_INT(val, hex) \
-{ \
-       gchar c = hex; \
- \
-       if ('0' <= c && c <= '9') { \
-               val = c - '0'; \
-       } else if ('a' <= c && c <= 'f') { \
-               val = c - 'a' + 10; \
-       } else if ('A' <= c && c <= 'F') { \
-               val = c - 'A' + 10; \
-       } else { \
-               val = 0; \
-       } \
-}
-
 /* Converts two-digit hexadecimal to decimal.  Used for unescaping escaped 
  * characters
  */
@@ -1498,19 +1527,19 @@ static gint axtoi(const gchar *hexstr)
 
 gboolean is_uri_string(const gchar *str)
 {
-       return (g_strncasecmp(str, "http://", 7) == 0 ||
-               g_strncasecmp(str, "https://", 8) == 0 ||
-               g_strncasecmp(str, "ftp://", 6) == 0 ||
-               g_strncasecmp(str, "www.", 4) == 0);
+       return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
+               g_ascii_strncasecmp(str, "https://", 8) == 0 ||
+               g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
+               g_ascii_strncasecmp(str, "www.", 4) == 0);
 }
 
 gchar *get_uri_path(const gchar *uri)
 {
-       if (g_strncasecmp(uri, "http://", 7) == 0)
+       if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
                return (gchar *)(uri + 7);
-       else if (g_strncasecmp(uri, "https://", 8) == 0)
+       else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
                return (gchar *)(uri + 8);
-       else if (g_strncasecmp(uri, "ftp://", 6) == 0)
+       else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
                return (gchar *)(uri + 6);
        else
                return (gchar *)uri;
@@ -1586,15 +1615,15 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
 
                if (*value == '\0') continue;
 
-               if (cc && !*cc && !g_strcasecmp(field, "cc")) {
+               if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) {
                        *cc = g_strdup(value);
-               } else if (bcc && !*bcc && !g_strcasecmp(field, "bcc")) {
+               } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) {
                        *bcc = g_strdup(value);
                } else if (subject && !*subject &&
-                          !g_strcasecmp(field, "subject")) {
+                          !g_ascii_strcasecmp(field, "subject")) {
                        *subject = g_malloc(strlen(value) + 1);
                        decode_uri(*subject, value);
-               } else if (body && !*body && !g_strcasecmp(field, "body")) {
+               } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
                        *body = g_malloc(strlen(value) + 1);
                        decode_uri(*body, value);
                }
@@ -1738,17 +1767,16 @@ const gchar *get_domain_name(void)
        static gchar *domain_name = NULL;
 
        if (!domain_name) {
-               gchar buf[128] = "";
                struct hostent *hp;
+               struct utsname uts;
 
-               if (gethostname(buf, sizeof(buf)) < 0) {
+               if (uname(&uts) < 0) {
                        perror("gethostname");
                        domain_name = "unknown";
                } else {
-                       buf[sizeof(buf) - 1] = '\0';
-                       if ((hp = my_gethostbyname(buf)) == NULL) {
+                       if ((hp = my_gethostbyname(uts.nodename)) == NULL) {
                                perror("gethostbyname");
-                               domain_name = g_strdup(buf);
+                               domain_name = g_strdup(uts.nodename);
                        } else {
                                domain_name = g_strdup(hp->h_name);
                        }
@@ -2013,7 +2041,7 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        DIR *dp;
        struct dirent *d;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
 
        prev_dir = g_get_current_dir();
 
@@ -2030,8 +2058,8 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        }
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0 && first <= fileno && fileno <= last) {
+               file_no = to_number(d->d_name);
+               if (file_no > 0 && first <= file_no && file_no <= last) {
                        if (is_dir_exist(d->d_name))
                                continue;
                        if (unlink(d->d_name) < 0)
@@ -2057,7 +2085,7 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
        DIR *dp;
        struct dirent *d;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
 
        prev_dir = g_get_current_dir();
 
@@ -2074,9 +2102,9 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
        }
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
-                       debug_print("removing unwanted file %d from %s\n", fileno, dir);
+               file_no = to_number(d->d_name);
+               if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
+                       debug_print("removing unwanted file %d from %s\n", file_no, dir);
                        if (is_dir_exist(d->d_name))
                                continue;
                        if (unlink(d->d_name) < 0)
@@ -2108,7 +2136,7 @@ gint remove_expired_files(const gchar *dir, guint hours)
        struct dirent *d;
        struct stat s;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
        time_t mtime, now, expire_time;
 
        prev_dir = g_get_current_dir();
@@ -2129,8 +2157,8 @@ gint remove_expired_files(const gchar *dir, guint hours)
        expire_time = hours * 60 * 60;
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0) {
+               file_no = to_number(d->d_name);
+               if (file_no > 0) {
                        if (stat(d->d_name, &s) < 0) {
                                FILE_OP_ERROR(d->d_name, "stat");
                                continue;
@@ -2485,29 +2513,17 @@ gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
        return 0;
 }
 
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
 {
-       FILE *dest_fp;
        gint n_read;
        gint bytes_left, to_read;
        gchar buf[BUFSIZ];
-       gboolean err = FALSE;
 
        if (fseek(fp, offset, SEEK_SET) < 0) {
                perror("fseek");
                return -1;
        }
 
-       if ((dest_fp = fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "fopen");
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode\n");
-       }
-
        bytes_left = length;
        to_read = MIN(bytes_left, sizeof(buf));
 
@@ -2515,9 +2531,6 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
                if (n_read < to_read && ferror(fp))
                        break;
                if (fwrite(buf, n_read, 1, dest_fp) < 1) {
-                       g_warning("writing to %s failed.\n", dest);
-                       fclose(dest_fp);
-                       unlink(dest);
                        return -1;
                }
                bytes_left -= n_read;
@@ -2528,14 +2541,37 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 
        if (ferror(fp)) {
                perror("fread");
-               err = TRUE;
+               return -1;
        }
-       if (fclose(dest_fp) == EOF) {
+
+       return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+       FILE *dest_fp;
+       gboolean err = FALSE;
+
+       if ((dest_fp = fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "fopen");
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode\n");
+       }
+
+       if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
+               err = TRUE;
+
+       if (!err && fclose(dest_fp) == EOF) {
                FILE_OP_ERROR(dest, "fclose");
                err = TRUE;
        }
 
        if (err) {
+               g_warning("writing to %s failed.\n", dest);
                unlink(dest);
                return -1;
        }
@@ -2782,7 +2818,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
        /* output header part */
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
-               if (!g_strncasecmp(buf, "Bcc:", 4)) {
+               if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
                        gint next;
 
                        for (;;) {
@@ -2856,7 +2892,7 @@ gchar *generate_mime_boundary(const gchar *prefix)
         * doesn't do any harm.
         */
        for (i = 0; i < sizeof(buf_uniq) - 1; i++)
-               buf_uniq[i] = tbl[(random() ^ pid) % (sizeof(tbl) - 1)];
+               buf_uniq[i] = tbl[(rand() ^ pid) % (sizeof(tbl) - 1)];
        buf_uniq[i] = '\0';
 
        get_rfc822_date(buf_date, sizeof(buf_date));
@@ -2864,7 +2900,7 @@ gchar *generate_mime_boundary(const gchar *prefix)
        subst_char(buf_date, ',', '_');
        subst_char(buf_date, ':', '_');
 
-       return g_strdup_printf("%s=_%s_%s", prefix ? prefix : "Multipart",
+       return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
                               buf_date, buf_uniq);
 }
 
@@ -2892,6 +2928,8 @@ FILE *my_tmpfile(void)
        tmpdir = get_tmp_dir();
        tmplen = strlen(tmpdir);
        progname = g_get_prgname();
+       if (progname == NULL)
+               progname = "sylpheed-claws";
        proglen = strlen(progname);
        Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
                return tmpfile());
@@ -3051,6 +3089,7 @@ gchar *file_read_stream_to_str(FILE *fp)
 gint execute_async(gchar *const argv[])
 {
        pid_t pid;
+       gint status;
 
        if ((pid = fork()) < 0) {
                perror("fork");
@@ -3075,14 +3114,18 @@ gint execute_async(gchar *const argv[])
                _exit(0);
        }
 
-       waitpid(pid, NULL, 0);
+       waitpid(pid, &status, 0);
 
-       return 0;
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       else
+               return -1;
 }
 
 gint execute_sync(gchar *const argv[])
 {
        pid_t pid;
+       gint status;
 
        if ((pid = fork()) < 0) {
                perror("fork");
@@ -3096,9 +3139,12 @@ gint execute_sync(gchar *const argv[])
                _exit(1);
        }
 
-       waitpid(pid, NULL, 0);
+       waitpid(pid, &status, 0);
 
-       return 0;
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       else
+               return -1;
 }
 
 gint execute_command_line(const gchar *cmdline, gboolean async)
@@ -3106,12 +3152,15 @@ gint execute_command_line(const gchar *cmdline, gboolean async)
        gchar **argv;
        gint ret;
 
+       debug_print("executing: %s\n", cmdline);
+
        argv = strsplit_with_quote(cmdline, " ", 0);
 
        if (async)
                ret = execute_async(argv);
        else
                ret = execute_sync(argv);
+
        g_strfreev(argv);
 
        return ret;
@@ -3208,11 +3257,12 @@ gint open_uri(const gchar *uri, const gchar *cmdline)
                g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
        else {
                if (cmdline)
-                       g_warning("Open URI command line is invalid: `%s'",
+                       g_warning("Open URI command line is invalid "
+                                 "(there must be only one '%%s'): %s",
                                  cmdline);
                g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
        }
-       
+
        execute_command_line(buf, TRUE);
 
        return 0;
@@ -3242,7 +3292,7 @@ time_t remote_tzoffset_sec(const gchar *zone)
                remoteoffset = 0;
        } else if (strlen(zone3) == 3) {
                for (p = ustzstr; *p != '\0'; p += 3) {
-                       if (!strncasecmp(p, zone3, 3)) {
+                       if (!g_ascii_strncasecmp(p, zone3, 3)) {
                                iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
                                remoteoffset = iustz * 3600;
                                break;
@@ -3458,7 +3508,7 @@ int subject_get_prefix_length(const gchar *subject)
                for (n = 0; n < PREFIXES; n++)
                        /* Terminate each prefix regexpression by a
                         * "\ ?" (zero or ONE space), and OR them */
-                       g_string_sprintfa(s, "(%s\\ ?)%s",
+                       g_string_append_printf(s, "(%s\\ ?)%s",
                                          prefixes[n],
                                          n < PREFIXES - 1 ? 
                                          "|" : "");
@@ -3504,7 +3554,7 @@ gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
        const char *str1 = gptr1;
        const char *str2 = gptr2;
 
-       return !strcasecmp(str1, str2);
+       return !g_utf8_collate(str1, str2);
 }
 
 gint g_int_compare(gconstpointer a, gconstpointer b)
@@ -3512,9 +3562,8 @@ gint g_int_compare(gconstpointer a, gconstpointer b)
        return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
 }
 
-gchar *generate_msgid(const gchar *address, gchar *buf, gint len)
+gchar *generate_msgid(gchar *buf, gint len)
 {
-       /* steal from compose.c::compose_generate_msgid() */
        struct tm *lt;
        time_t t;
        gchar *addr;
@@ -3522,26 +3571,18 @@ gchar *generate_msgid(const gchar *address, gchar *buf, gint len)
        t = time(NULL);
        lt = localtime(&t);
 
-       if (address && *address) {
-               if (strchr(address, '@'))
-                       addr = g_strdup(address);
-               else
-                       addr = g_strconcat(address, "@", get_domain_name(), NULL);
-       } else
-               addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
-                                  NULL);
+       addr = g_strconcat("@", get_domain_name(), NULL);
 
-       g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
+       g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x%s",
                   lt->tm_year + 1900, lt->tm_mon + 1,
                   lt->tm_mday, lt->tm_hour,
                   lt->tm_min, lt->tm_sec,
-                  (guint)random(), addr);
+                  (guint) rand(), addr);
 
        g_free(addr);
        return buf;
 }
 
-
 /*
    quote_cmd_argument()
    
@@ -3643,3 +3684,240 @@ GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
 
        return root;
 }
+
+#define HEX_TO_INT(val, hex)                   \
+{                                              \
+       gchar c = hex;                          \
+                                               \
+       if ('0' <= c && c <= '9') {             \
+               val = c - '0';                  \
+       } else if ('a' <= c && c <= 'f') {      \
+               val = c - 'a' + 10;             \
+       } else if ('A' <= c && c <= 'F') {      \
+               val = c - 'A' + 10;             \
+       } else {                                \
+               val = -1;                       \
+       }                                       \
+}
+
+gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
+{
+       gint hi, lo;
+
+       HEX_TO_INT(hi, c1);
+       HEX_TO_INT(lo, c2);
+
+       if (hi == -1 || lo == -1)
+               return FALSE;
+
+       *out = (hi << 4) + lo;
+       return TRUE;
+}
+
+#define INT_TO_HEX(hex, val)           \
+{                                      \
+       if ((val) < 10)                 \
+               hex = '0' + (val);      \
+       else                            \
+               hex = 'A' + (val) - 10; \
+}
+
+void get_hex_str(gchar *out, guchar ch)
+{
+       gchar hex;
+
+       INT_TO_HEX(hex, ch >> 4);
+       *out++ = hex;
+       INT_TO_HEX(hex, ch & 0x0f);
+       *out++ = hex;
+}
+
+#undef REF_DEBUG
+#ifndef REF_DEBUG
+#define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
+#else
+#define G_PRINT_REF g_print
+#endif
+
+/*!
+ *\brief       Register ref counted pointer. It is based on GBoxed, so should
+ *             work with anything that uses the GType system. The semantics
+ *             are similar to a C++ auto pointer, with the exception that
+ *             C doesn't have automatic closure (calling destructors) when 
+ *             exiting a block scope.
+ *             Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
+ *             function directly.
+ *
+ *\return      GType A GType type.
+ */
+GType g_auto_pointer_register(void)
+{
+       static GType auto_pointer_type;
+       if (!auto_pointer_type)
+               auto_pointer_type =
+                       g_boxed_type_register_static
+                               ("G_TYPE_AUTO_POINTER",
+                                (GBoxedCopyFunc) g_auto_pointer_copy,
+                                (GBoxedFreeFunc) g_auto_pointer_free);
+       return auto_pointer_type;                                                    
+}
+
+/*!
+ *\brief       Structure with g_new() allocated pointer guarded by the
+ *             auto pointer
+ */
+typedef struct AutoPointerRef {
+       void          (*free) (gpointer);
+       gpointer        pointer;
+       glong           cnt;
+} AutoPointerRef;
+
+/*!
+ *\brief       The auto pointer opaque structure that references the
+ *             pointer guard block.
+ */
+typedef struct AutoPointer {
+       AutoPointerRef *ref;
+       gpointer        ptr; /*!< access to protected pointer */
+} AutoPointer;
+
+/*!
+ *\brief       Creates an auto pointer for a g_new()ed pointer. Example:
+ *
+ *\code        
+ *
+ *             ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
+ *             ... when assigning, copying and freeing storage elements
+ *
+ *             gtk_list_store_new(N_S_COLUMNS, 
+ *                                G_TYPE_AUTO_POINTER,
+ *                                -1);
+ *
+ *
+ *             Template *precious_data = g_new0(Template, 1);
+ *             g_pointer protect = g_auto_pointer_new(precious_data);
+ *
+ *             gtk_list_store_set(container, &iter,
+ *                                S_DATA, protect,
+ *                                -1);
+ *
+ *             ... the gtk_list_store has copied the pointer and 
+ *             ... incremented its reference count, we should free
+ *             ... the auto pointer (in C++ a destructor would do
+ *             ... this for us when leaving block scope)
+ * 
+ *             g_auto_pointer_free(protect);
+ *
+ *             ... gtk_list_store_set() now manages the data. When
+ *             ... *explicitly* requesting a pointer from the list 
+ *             ... store, don't forget you get a copy that should be 
+ *             ... freed with g_auto_pointer_free() eventually.
+ *
+ *\endcode
+ *
+ *\param       pointer Pointer to be guarded.
+ *
+ *\return      GAuto * Pointer that should be used in containers with
+ *             GType support.
+ */
+GAuto *g_auto_pointer_new(gpointer p)
+{
+       AutoPointerRef *ref;
+       AutoPointer    *ptr;
+       
+       if (p == NULL) 
+               return NULL;
+
+       ref = g_new0(AutoPointerRef, 1);
+       ptr = g_new0(AutoPointer, 1);
+
+       ref->pointer = p;
+       ref->free = g_free;
+       ref->cnt = 1;
+
+       ptr->ref = ref;
+       ptr->ptr = p;
+
+       G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
+
+       return ptr;
+}
+
+/*!
+ *\brief       Allocate an autopointer using the passed \a free function to
+ *             free the guarded pointer
+ */
+GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
+{
+       AutoPointer *aptr;
+       
+       if (p == NULL)
+               return NULL;
+
+       aptr = g_auto_pointer_new(p);
+       aptr->ref->free = free_;
+       return aptr; 
+}
+
+gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
+{
+       if (auto_ptr == NULL) 
+               return NULL;
+       return ((AutoPointer *) auto_ptr)->ptr; 
+}
+
+/*!
+ *\brief       Copies an auto pointer by. It's mostly not necessary
+ *             to call this function directly, unless you copy/assign
+ *             the guarded pointer.
+ *
+ *\param       auto_ptr Auto pointer returned by previous call to 
+ *             g_auto_pointer_new_XXX()
+ *
+ *\return      gpointer An auto pointer
+ */
+GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
+{
+       AutoPointer     *ptr;
+       AutoPointerRef  *ref;
+       AutoPointer     *newp;
+
+       if (auto_ptr == NULL) 
+               return NULL;
+
+       ptr = auto_ptr;
+       ref = ptr->ref;
+       newp = g_new0(AutoPointer, 1);
+
+       newp->ref = ref;
+       newp->ptr = ref->pointer;
+       ++(ref->cnt);
+       
+       G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+
+       return newp;
+}
+
+/*!
+ *\brief       Free an auto pointer
+ */
+void g_auto_pointer_free(GAuto *auto_ptr)
+{
+       AutoPointer     *ptr;
+       AutoPointerRef  *ref;
+       
+       if (auto_ptr == NULL)
+               return;
+
+       ptr = auto_ptr;
+       ref = ptr->ref;
+
+       if (--(ref->cnt) == 0) {
+               G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+               ref->free(ref->pointer);
+               g_free(ref);
+       } else
+               G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+       g_free(ptr);            
+}
+