make m_dupstr return NULL if the string was empty.
[apps/madmutt.git] / complete.c
index b0c96cf..55df906 100644 (file)
@@ -20,7 +20,9 @@
 #include "nntp.h"
 #endif
 
-#include "lib/str.h"
+#include <lib-lib/str.h>
+#include <lib-lib/file.h>
+#include "lib/debug.h"
 
 #include <dirent.h>
 #include <string.h>
@@ -47,7 +49,7 @@ int mutt_complete (char *s, size_t slen)
   char imap_path[LONG_STRING];
 #endif
 
-  dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
+  debug_print (2, ("completing %s\n", s));
 
 #ifdef USE_NNTP
   if (option (OPTNEWS)) {
@@ -59,7 +61,7 @@ int mutt_complete (char *s, size_t slen)
      * special case to handle when there is no filepart yet.
      * find the first subscribed newsgroup
      */
-    if ((len = safe_strlen (filepart)) == 0) {
+    if ((len = m_strlen(filepart)) == 0) {
       for (; l; l = l->next) {
         NNTP_DATA *data = (NNTP_DATA *) l->data;
 
@@ -76,7 +78,7 @@ int mutt_complete (char *s, size_t slen)
       NNTP_DATA *data = (NNTP_DATA *) l->data;
 
       if (data && data->subscribed &&
-          safe_strncmp (data->group, filepart, len) == 0) {
+          str_ncmp (data->group, filepart, len) == 0) {
         if (init) {
           for (i = 0; filepart[i] && data->group[i]; i++) {
             if (filepart[i] != data->group[i]) {
@@ -102,12 +104,8 @@ int mutt_complete (char *s, size_t slen)
 #ifdef USE_IMAP
   /* we can use '/' as a delimiter, imap_complete rewrites it */
   if (*s == '=' || *s == '+' || *s == '!') {
-    if (*s == '!')
-      p = NONULL (Spoolfile);
-    else
-      p = NONULL (Maildir);
-
-    mutt_concat_path (imap_path, p, s + 1, sizeof (imap_path));
+      const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
+      mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
   }
   else
     strfcpy (imap_path, s, sizeof (imap_path));
@@ -127,7 +125,7 @@ int mutt_complete (char *s, size_t slen)
       char buf[_POSIX_PATH_MAX];
 
       *p++ = 0;
-      mutt_concat_path (buf, exp_dirpart, s + 1, sizeof (buf));
+      mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
       strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
       snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
       strfcpy (dirpart, buf, sizeof (dirpart));
@@ -149,7 +147,7 @@ int mutt_complete (char *s, size_t slen)
       else {
         *p = 0;
         len = (size_t) (p - s);
-        strncpy (dirpart, s, len);
+        memcpy(dirpart, s, len);
         dirpart[len] = 0;
         p++;
         strfcpy (filepart, p, sizeof (filepart));
@@ -167,9 +165,7 @@ int mutt_complete (char *s, size_t slen)
   }
 
   if (dirp == NULL) {
-    dprint (1,
-            (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart,
-             strerror (errno), errno));
+    debug_print (1, ("%s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
     return (-1);
   }
 
@@ -177,10 +173,10 @@ int mutt_complete (char *s, size_t slen)
    * special case to handle when there is no filepart yet.  find the first
    * file/directory which is not ``.'' or ``..''
    */
-  if ((len = safe_strlen (filepart)) == 0) {
+  if ((len = m_strlen(filepart)) == 0) {
     while ((de = readdir (dirp)) != NULL) {
-      if (safe_strcmp (".", de->d_name) != 0
-          && safe_strcmp ("..", de->d_name) != 0) {
+      if (m_strcmp(".", de->d_name) != 0
+          && m_strcmp("..", de->d_name) != 0) {
         strfcpy (filepart, de->d_name, sizeof (filepart));
         init++;
         break;
@@ -189,7 +185,7 @@ int mutt_complete (char *s, size_t slen)
   }
 
   while ((de = readdir (dirp)) != NULL) {
-    if (safe_strncmp (de->d_name, filepart, len) == 0) {
+    if (str_ncmp (de->d_name, filepart, len) == 0) {
       if (init) {
         for (i = 0; filepart[i] && de->d_name[i]; i++) {
           if (filepart[i] != de->d_name[i]) {
@@ -208,14 +204,14 @@ int mutt_complete (char *s, size_t slen)
         /* check to see if it is a directory */
         if (dirpart[0]) {
           strfcpy (buf, exp_dirpart, sizeof (buf));
-          strfcpy (buf + safe_strlen (buf), "/", sizeof (buf) - safe_strlen (buf));
+          strfcpy (buf + m_strlen(buf), "/", sizeof (buf) - m_strlen(buf));
         }
         else
           buf[0] = 0;
-        strfcpy (buf + safe_strlen (buf), filepart, sizeof (buf) - safe_strlen (buf));
+        strfcpy (buf + m_strlen(buf), filepart, sizeof (buf) - m_strlen(buf));
         if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
-          strfcpy (filepart + safe_strlen (filepart), "/",
-                   sizeof (filepart) - safe_strlen (filepart));
+          strfcpy (filepart + m_strlen(filepart), "/",
+                   sizeof (filepart) - m_strlen(filepart));
         init = 1;
       }
     }
@@ -224,10 +220,10 @@ int mutt_complete (char *s, size_t slen)
 
   if (dirpart[0]) {
     strfcpy (s, dirpart, slen);
-    if (safe_strcmp ("/", dirpart) != 0 && dirpart[0] != '='
+    if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
         && dirpart[0] != '+')
-      strfcpy (s + safe_strlen (s), "/", slen - safe_strlen (s));
-    strfcpy (s + safe_strlen (s), filepart, slen - safe_strlen (s));
+      strfcpy (s + m_strlen(s), "/", slen - m_strlen(s));
+    strfcpy (s + m_strlen(s), filepart, slen - m_strlen(s));
   }
   else
     strfcpy (s, filepart, slen);