add some list functions.
authorPierre Habouzit <madcoder@debian.org>
Thu, 2 Nov 2006 21:15:37 +0000 (22:15 +0100)
committerPierre Habouzit <madcoder@debian.org>
Thu, 2 Nov 2006 21:15:37 +0000 (22:15 +0100)
fix compilation warnings.
add cflags.
add ssizeof

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
lib-lib/Makefile.am
lib-lib/file.c
lib-lib/list.h
lib-lib/mem.h

index 3545ecf..d7dd815 100644 (file)
@@ -5,3 +5,4 @@ liblib_a_SOURCES = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h \
 
 noinst_HEADERS   = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h
 
 
 noinst_HEADERS   = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h
 
+-include ../cflags.mk
index 793ac10..6442667 100644 (file)
@@ -107,11 +107,11 @@ int safe_symlink(const char *oldpath, const char *newpath)
             return -1;
 
         len = m_strcat(abs_oldpath, sizeof(abs_oldpath), "/");
             return -1;
 
         len = m_strcat(abs_oldpath, sizeof(abs_oldpath), "/");
-        if (len >= sizeof(abs_oldpath))
+        if (len >= ssizeof(abs_oldpath))
             return -1;
 
         len += m_strcpy(abs_oldpath + len, sizeof(abs_oldpath) - len, oldpath);
             return -1;
 
         len += m_strcpy(abs_oldpath + len, sizeof(abs_oldpath) - len, oldpath);
-        if (len >= sizeof(abs_oldpath))
+        if (len >= ssizeof(abs_oldpath))
             return -1;
 
         if (symlink (abs_oldpath, newpath) < 0)
             return -1;
 
         if (symlink (abs_oldpath, newpath) < 0)
@@ -211,8 +211,8 @@ void mutt_unlink(const char *s)
 
             p_clear(buf, sizeof(buf));
             while (sb.st_size > 0) {
 
             p_clear(buf, sizeof(buf));
             while (sb.st_size > 0) {
-                fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f);
-                sb.st_size -= MIN(sizeof(buf), sb.st_size);
+                fwrite(buf, 1, MIN(ssizeof(buf), sb.st_size), f);
+                sb.st_size -= MIN(ssizeof(buf), sb.st_size);
             }
             fclose (f);
         }
             }
             fclose (f);
         }
@@ -310,7 +310,7 @@ char *mutt_read_line(char *s, size_t *size, FILE * fp, int *line)
 int mutt_copy_stream(FILE *fin, FILE *fout)
 {
     char buf[BUFSIZ];
 int mutt_copy_stream(FILE *fin, FILE *fout)
 {
     char buf[BUFSIZ];
-    ssize_t l;
+    size_t l;
 
     while ((l = fread(buf, 1, sizeof (buf), fin)) > 0) {
         if (fwrite(buf, 1, l, fout) != l)
 
     while ((l = fread(buf, 1, sizeof (buf), fin)) > 0) {
         if (fwrite(buf, 1, l, fout) != l)
@@ -325,7 +325,7 @@ int mutt_copy_bytes(FILE *in, FILE *out, size_t size)
     char buf[BUFSIZ];
 
     while (size > 0) {
     char buf[BUFSIZ];
 
     while (size > 0) {
-        ssize_t chunk = MIN(size, sizeof(buf));
+        size_t chunk = MIN(size, sizeof(buf));
 
         if ((chunk = fread(buf, 1, chunk, in)) < 1)
             break;
 
         if ((chunk = fread(buf, 1, chunk, in)) < 1)
             break;
@@ -354,12 +354,16 @@ const char *mutt_basename(const char *f)
 char *
 mutt_concat_path(char *d, ssize_t n, const char *dir, const char *fname)
 {
 char *
 mutt_concat_path(char *d, ssize_t n, const char *dir, const char *fname)
 {
-    const char *fmt = "%s/%s";
+    int pos;
 
 
-    if (!*fname || (*dir && dir[m_strlen(dir) - 1] == '/'))
-        fmt = "%s%s";
+    pos = m_strcpy(d, n, dir);
+    if (pos >= n - 1)
+        return d;
 
 
-    snprintf(d, n, fmt, dir, fname);
+    if (pos && d[pos - 1] != '/')
+        d[pos++] = '/';
+
+    m_strcpy(d + pos, n - pos, fname);
     return d;
 }
 
     return d;
 }
 
@@ -385,7 +389,7 @@ void mutt_sanitize_filename(char *s, short slash)
 /* FIXME: API is very wrong */
 ssize_t mutt_quote_filename(char *d, ssize_t l, const char *s)
 {
 /* FIXME: API is very wrong */
 ssize_t mutt_quote_filename(char *d, ssize_t l, const char *s)
 {
-    size_t i, j = 0;
+    ssize_t i, j = 0;
 
     if (!s) {
         *d = '\0';
 
     if (!s) {
         *d = '\0';
index 1a915b3..2e13fca 100644 (file)
@@ -22,8 +22,8 @@
  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
  */
 
  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
  */
 
-#ifndef _MUTT_LIST_H
-#define _MUTT_LIST_H
+#ifndef MUTT_LIB_LIB_LIST_H
+#define MUTT_LIB_LIB_LIST_H
 
 typedef struct list_t {
     char *data;
 
 typedef struct list_t {
     char *data;
@@ -42,4 +42,38 @@ static inline LIST *mutt_add_list(LIST *head, const char *data) {
     return mutt_add_list_n(head, data, len ? len + 1 : 0);
 }
 
     return mutt_add_list_n(head, data, len ? len + 1 : 0);
 }
 
-#endif /* !_MUTT_LIST_H */
+#define DO_SLIST(type, prefix)                                               \
+    static inline type *prefix##_list_pop(type **list) {                     \
+        if (*list) {                                                         \
+            type *res = *list;                                               \
+            *list = res->next;                                               \
+            res->next = NULL;                                                \
+            return res;                                                      \
+        }                                                                    \
+        return NULL;                                                         \
+    }                                                                        \
+    static inline void prefix##_list_push(type **list, type *item) {         \
+        item->next = *list;                                                  \
+        *list = item;                                                        \
+    }                                                                        \
+                                                                             \
+    static inline type **prefix##_list_init(type **list) {                   \
+        *list = NULL;                                                        \
+        return list;                                                         \
+    }                                                                        \
+    static inline void prefix##_list_wipe(type **list, int del) {            \
+        if (del) {                                                           \
+            while (*list) {                                                  \
+                type *item = prefix##_list_pop(list);                        \
+                prefix##_delete(&item);                                      \
+            }                                                                \
+        } else {                                                             \
+            *list = NULL;                                                    \
+        }                                                                    \
+    }                                                                        \
+
+
+
+
+
+#endif /* MUTT_LIB_LIB_LIST_H */
index db5ef49..b3bcd62 100644 (file)
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <unistd.h>
 
 #include <string.h>
 #include <unistd.h>
 
+#define ssizeof(foo)            (ssize_t)sizeof(foo)
+
 #define p_new(type, count)      ((type *)xmalloc(sizeof(type) * (count)))
 #define p_clear(p, count)       ((void)memset((p), 0, sizeof(*(p)) * (count)))
 #define p_dup(p, count)         xmemdup((p), sizeof(*(p)) * (count))
 #define p_new(type, count)      ((type *)xmalloc(sizeof(type) * (count)))
 #define p_clear(p, count)       ((void)memset((p), 0, sizeof(*(p)) * (count)))
 #define p_dup(p, count)         xmemdup((p), sizeof(*(p)) * (count))