Lay off the ground of our very efficient event loop.
[apps/madmutt.git] / lib-lib / file.c
index 946d867..ed74aad 100644 (file)
@@ -70,7 +70,7 @@ int safe_open(const char *path, int flags)
         return -1;
     }
 
-    return (fd);
+    return fd;
 }
 
 
@@ -226,7 +226,7 @@ FILE *safe_fopen(const char *path, const char *mode)
         flags |= (mode[1] == '+' ? O_RDWR : O_WRONLY);
 
         if ((fd = safe_open(path, flags)) < 0)
-            return (NULL);
+            return NULL;
 
         return fdopen (fd, mode);
     }
@@ -364,6 +364,26 @@ const char *mutt_basename(const char *f)
     return p ? p + 1 : f;
 }
 
+ssize_t m_dirname(char *dst, ssize_t dlen, const char *path)
+{
+    ssize_t plen = m_strlen(path);
+
+    while (plen > 0 && path[plen - 1] == '/')
+        plen--;
+
+    while (plen > 0 && path[plen - 1] != '/')
+        plen--;
+
+    while (plen > 0 && path[plen - 1] == '/')
+        plen--;
+
+    if (plen)
+        return m_strncpy(dst, dlen, path, plen);
+
+    if (*path == '/')
+        return m_strcpy(dst, dlen, "/");
+    return m_strcpy(dst, dlen, ".");
+}
 
 char *
 mutt_concat_path(char *d, ssize_t n, const char *dir, const char *fname)