diff options
author | Kevin McCarthy <kevin@8t8.us> | 2019-09-19 18:52:24 -0700 |
---|---|---|
committer | Kevin McCarthy <kevin@8t8.us> | 2019-09-19 19:14:03 -0700 |
commit | 192a6d0ebe7d303b5a340a5334ada58bd5e509a6 (patch) | |
tree | e4deaa91bcb4cea9065460fbb1fae14bbd002c74 | |
parent | a4b53e19ef471efc1938348d1623b811354f1c93 (diff) |
Convert mutt_complete() to use the buffer pool.
Add helper functions mutt_buffer_substrcpy() and
mutt_buffer_concatn_path().
Remove mutt_concatn_path() because mutt_complete() was the only
caller.
-rw-r--r-- | buffer.c | 6 | ||||
-rw-r--r-- | buffer.h | 1 | ||||
-rw-r--r-- | complete.c | 135 | ||||
-rw-r--r-- | imap/imap.c | 2 | ||||
-rw-r--r-- | imap/imap.h | 2 | ||||
-rw-r--r-- | lib.c | 46 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | muttlib.c | 16 | ||||
-rw-r--r-- | protos.h | 2 |
9 files changed, 103 insertions, 108 deletions
@@ -208,6 +208,12 @@ void mutt_buffer_strcpy_n (BUFFER *buf, const char *s, size_t len) mutt_buffer_addstr_n (buf, s, len); } +void mutt_buffer_substrcpy (BUFFER *buf, const char *beg, const char *end) +{ + mutt_buffer_clear (buf); + if (end > beg) + mutt_buffer_strcpy_n (buf, beg, end - beg); +} static void increase_buffer_pool (void) { @@ -46,6 +46,7 @@ void mutt_buffer_fix_dptr (BUFFER *); int mutt_buffer_printf (BUFFER*, const char*, ...); void mutt_buffer_strcpy (BUFFER *, const char *); void mutt_buffer_strcpy_n (BUFFER *, const char *, size_t); +void mutt_buffer_substrcpy (BUFFER *buf, const char *beg, const char *end); /* These append to the buffer. */ int mutt_buffer_add_printf (BUFFER*, const char*, ...); @@ -44,13 +44,18 @@ int mutt_complete (char *s, size_t slen) struct dirent *de; int i ,init=0; size_t len; - char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX]; - char filepart[_POSIX_PATH_MAX]; + BUFFER *dirpart = NULL; + BUFFER *exp_dirpart = NULL; + BUFFER *filepart = NULL; + BUFFER *buf = NULL; + #ifdef USE_IMAP - char imap_path[LONG_STRING]; + BUFFER *imap_path = NULL; + int rc; dprint (2, (debugfile, "mutt_complete: completing %s\n", s)); + imap_path = mutt_buffer_pool_get (); /* we can use '/' as a delimiter, imap_complete rewrites it */ if (*s == '=' || *s == '+' || *s == '!') { @@ -59,38 +64,45 @@ int mutt_complete (char *s, size_t slen) else p = NONULL (Maildir); - mutt_concat_path (imap_path, p, s+1, sizeof (imap_path)); + mutt_buffer_concat_path (imap_path, p, s+1); } else - strfcpy (imap_path, s, sizeof(imap_path)); + mutt_buffer_strcpy (imap_path, s); - if (mx_is_imap (imap_path)) - return imap_complete (s, slen, imap_path); + if (mx_is_imap (mutt_b2s (imap_path))) + { + rc = imap_complete (s, slen, mutt_b2s (imap_path)); + mutt_buffer_pool_release (&imap_path); + return rc; + } + + mutt_buffer_pool_release (&imap_path); #endif + dirpart = mutt_buffer_pool_get (); + exp_dirpart = mutt_buffer_pool_get (); + filepart = mutt_buffer_pool_get (); + buf = mutt_buffer_pool_get (); + if (*s == '=' || *s == '+' || *s == '!') { - dirpart[0] = *s; - dirpart[1] = 0; + mutt_buffer_addch (dirpart, *s); if (*s == '!') - strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart)); + mutt_buffer_strcpy (exp_dirpart, NONULL (Spoolfile)); else - strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart)); + mutt_buffer_strcpy (exp_dirpart, NONULL (Maildir)); if ((p = strrchr (s, '/'))) { - char buf[_POSIX_PATH_MAX]; - if (mutt_concatn_path (buf, sizeof(buf), exp_dirpart, strlen(exp_dirpart), - s + 1, (size_t)(p - s - 1)) == NULL) - { - return -1; - } - strfcpy (exp_dirpart, buf, sizeof (exp_dirpart)); - mutt_substrcpy(dirpart, s, p+1, sizeof(dirpart)); - strfcpy (filepart, p + 1, sizeof (filepart)); + mutt_buffer_concatn_path (buf, + mutt_b2s (exp_dirpart), mutt_buffer_len (exp_dirpart), + s + 1, (size_t)(p - s - 1)); + mutt_buffer_strcpy (exp_dirpart, mutt_b2s (buf)); + mutt_buffer_substrcpy (dirpart, s, p+1); + mutt_buffer_strcpy (filepart, p + 1); } else - strfcpy (filepart, s + 1, sizeof (filepart)); - dirp = opendir (exp_dirpart); + mutt_buffer_strcpy (filepart, s + 1); + dirp = opendir (mutt_b2s (exp_dirpart)); } else { @@ -99,46 +111,45 @@ int mutt_complete (char *s, size_t slen) if (p == s) /* absolute path */ { p = s + 1; - strfcpy (dirpart, "/", sizeof (dirpart)); - exp_dirpart[0] = 0; - strfcpy (filepart, p, sizeof (filepart)); - dirp = opendir (dirpart); + mutt_buffer_strcpy (dirpart, "/"); + mutt_buffer_strcpy (filepart, p); + dirp = opendir (mutt_b2s (dirpart)); } else { - mutt_substrcpy(dirpart, s, p, sizeof(dirpart)); - strfcpy (filepart, p + 1, sizeof (filepart)); - strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart)); - mutt_expand_path (exp_dirpart, sizeof (exp_dirpart)); - dirp = opendir (exp_dirpart); + mutt_buffer_substrcpy (dirpart, s, p); + mutt_buffer_strcpy (filepart, p + 1); + mutt_buffer_strcpy (exp_dirpart, mutt_b2s (dirpart)); + mutt_buffer_expand_path (exp_dirpart); + dirp = opendir (mutt_b2s (exp_dirpart)); } } else { /* no directory name, so assume current directory. */ - dirpart[0] = 0; - strfcpy (filepart, s, sizeof (filepart)); + mutt_buffer_strcpy (filepart, s); dirp = opendir ("."); } } if (dirp == NULL) { - dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno)); - return (-1); + dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", + mutt_b2s (exp_dirpart), strerror (errno), errno)); + goto cleanup; } /* * special case to handle when there is no filepart yet. find the first * file/directory which is not ``.'' or ``..'' */ - if ((len = mutt_strlen (filepart)) == 0) + if ((len = mutt_buffer_len (filepart)) == 0) { while ((de = readdir (dirp)) != NULL) { if (mutt_strcmp (".", de->d_name) != 0 && mutt_strcmp ("..", de->d_name) != 0) { - strfcpy (filepart, de->d_name, sizeof (filepart)); + mutt_buffer_strcpy (filepart, de->d_name); init++; break; } @@ -147,54 +158,60 @@ int mutt_complete (char *s, size_t slen) while ((de = readdir (dirp)) != NULL) { - if (mutt_strncmp (de->d_name, filepart, len) == 0) + if (mutt_strncmp (de->d_name, mutt_b2s (filepart), len) == 0) { if (init) { - for (i=0; filepart[i] && de->d_name[i]; i++) + char *fpch; + + for (i=0, fpch = filepart->data; *fpch && de->d_name[i]; i++, fpch++) { - if (filepart[i] != de->d_name[i]) - { - filepart[i] = 0; + if (*fpch != de->d_name[i]) break; - } } - filepart[i] = 0; + *fpch = 0; + mutt_buffer_fix_dptr (filepart); } else { - char buf[_POSIX_PATH_MAX]; struct stat st; - strfcpy (filepart, de->d_name, sizeof(filepart)); + mutt_buffer_strcpy (filepart, de->d_name); /* check to see if it is a directory */ - if (dirpart[0]) + if (mutt_buffer_len (dirpart)) { - strfcpy (buf, exp_dirpart, sizeof (buf)); - strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf)); + mutt_buffer_strcpy (buf, mutt_b2s (exp_dirpart)); + mutt_buffer_addch (buf, '/'); } else - buf[0] = 0; - strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf)); - if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR)) - strfcpy (filepart + strlen (filepart), "/", - sizeof (filepart) - strlen (filepart)); + mutt_buffer_clear (buf); + mutt_buffer_addstr (buf, mutt_b2s (filepart)); + if (stat (mutt_b2s (buf), &st) != -1 && (st.st_mode & S_IFDIR)) + mutt_buffer_addch (filepart, '/'); init = 1; } } } closedir (dirp); - if (dirpart[0]) + if (mutt_buffer_len (dirpart)) { - strfcpy (s, dirpart, slen); - if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '=' && dirpart[0] != '+') + strfcpy (s, mutt_b2s (dirpart), slen); + if (mutt_strcmp ("/", mutt_b2s (dirpart)) != 0 && + mutt_b2s (dirpart)[0] != '=' && + mutt_b2s (dirpart)[0] != '+') strfcpy (s + strlen (s), "/", slen - strlen (s)); - strfcpy (s + strlen (s), filepart, slen - strlen (s)); + strfcpy (s + strlen (s), mutt_b2s (filepart), slen - strlen (s)); } else - strfcpy (s, filepart, slen); + strfcpy (s, mutt_b2s (filepart), slen); + +cleanup: + mutt_buffer_pool_release (&dirpart); + mutt_buffer_pool_release (&exp_dirpart); + mutt_buffer_pool_release (&filepart); + mutt_buffer_pool_release (&buf); return (init ? 0 : -1); } diff --git a/imap/imap.c b/imap/imap.c index e583a328..ccba3a38 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -2147,7 +2147,7 @@ imap_complete_hosts (char *dest, size_t len) /* imap_complete: given a partial IMAP folder path, return a string which * adds as much to the path as is unique */ -int imap_complete(char* dest, size_t dlen, char* path) +int imap_complete(char* dest, size_t dlen, const char* path) { IMAP_DATA* idata; char list[LONG_STRING]; diff --git a/imap/imap.h b/imap/imap.h index f8565195..2a3d4fc0 100644 --- a/imap/imap.h +++ b/imap/imap.h @@ -41,7 +41,7 @@ int imap_buffy_check (int force, int check_stats); int imap_status (const char *path, int queue); int imap_search (CONTEXT* ctx, const pattern_t* pat); int imap_subscribe (char *path, int subscribe); -int imap_complete (char* dest, size_t dlen, char* path); +int imap_complete (char* dest, size_t dlen, const char* path); int imap_fast_trash (CONTEXT* ctx, char* dest); void imap_allow_reopen (CONTEXT *ctx); @@ -780,52 +780,6 @@ void mutt_remove_trailing_ws (char *s) *p = 0; } -/* - * Write the concatened pathname (dir + "/" + fname) into dst. - * The slash is omitted when dir or fname is of 0 length. - * Returns NULL on error or a pointer to dst otherwise. - */ -char *mutt_concatn_path (char *dst, size_t dstlen, - const char *dir, size_t dirlen, const char *fname, size_t fnamelen) -{ - size_t req; - size_t offset = 0; - - if (dstlen == 0) - return NULL; /* probably should not mask errors like this */ - - /* size check */ - req = dirlen + fnamelen + 1; /* +1 for the trailing nul */ - if (dirlen && fnamelen) - req++; /* when both components are non-nul, we add a "/" in between */ - if (req > dstlen) /* check for condition where the dst length is too short */ - { - /* Two options here: - * 1) assert(0) or return NULL to signal error - * 2) copy as much of the path as will fit - * It doesn't appear that the return value is actually checked anywhere mutt_concat_path() - * is called, so we should just copy set dst to nul and let the calling function fail later. - */ - dst[0] = 0; /* safe since we bail out early if dstlen == 0 */ - return NULL; - } - - if (dirlen) /* when dir is not empty */ - { - memcpy(dst, dir, dirlen); - offset = dirlen; - if (fnamelen) - dst[offset++] = '/'; - } - if (fnamelen) /* when fname is not empty */ - { - memcpy(dst + offset, fname, fnamelen); - offset += fnamelen; - } - dst[offset] = 0; - return dst; -} - char *mutt_concat_path (char *d, const char *dir, const char *fname, size_t l) { const char *fmt = "%s/%s"; @@ -169,7 +169,6 @@ void mutt_debug (FILE *, const char *, ...); /* The actual library functions. */ -char *mutt_concatn_path (char *, size_t, const char *, size_t, const char *, size_t); char *mutt_concat_path (char *, const char *, const char *, size_t); char *mutt_read_line (char *, size_t *, FILE *, int *, int); char *mutt_skip_whitespace (char *); @@ -1275,6 +1275,22 @@ void mutt_buffer_concat_path (BUFFER *d, const char *dir, const char *fname) mutt_buffer_printf (d, fmt, dir, fname); } +/* + * Write the concatened pathname (dir + "/" + fname) into dst. + * The slash is omitted when dir or fname is of 0 length. + */ +void mutt_buffer_concatn_path (BUFFER *dst, const char *dir, size_t dirlen, + const char *fname, size_t fnamelen) +{ + mutt_buffer_clear (dst); + if (dirlen) + mutt_buffer_addstr_n (dst, dir, dirlen); + if (dirlen && fnamelen) + mutt_buffer_addch (dst, '/'); + if (fnamelen) + mutt_buffer_addstr_n (dst, fname, fnamelen); +} + const char *mutt_getcwd (BUFFER *cwd) { char *retval; @@ -175,6 +175,8 @@ int mutt_bounce_message (FILE *fp, HEADER *, ADDRESS *); void mutt_break_thread (HEADER *); void mutt_browser_cleanup (void); void mutt_buffer_concat_path (BUFFER *, const char *, const char *); +void mutt_buffer_concatn_path (BUFFER *dst, const char *dir, size_t dirlen, + const char *fname, size_t fnamelen); #define mutt_buffer_quote_filename(a,b) _mutt_buffer_quote_filename (a, b, 1); void _mutt_buffer_quote_filename (BUFFER *, const char *, int); void mutt_buffer_sanitize_filename (BUFFER *d, const char *f, short slash); |