]> granicus.if.org Git - neomutt/commitdiff
lib: move function to file.c
authorRichard Russon <rich@flatcap.org>
Wed, 1 Nov 2017 02:50:26 +0000 (02:50 +0000)
committerRichard Russon <rich@flatcap.org>
Wed, 1 Nov 2017 02:50:26 +0000 (02:50 +0000)
init.c
lib/file.c
lib/file.h
mbox.c
mx.h

diff --git a/init.c b/init.c
index 2698412989f4ddd9c750a5c77770087569c39fae..989c3a05473ef5fdc7d56e83fd3db84314ca5dd1 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3106,47 +3106,6 @@ static int parse_set(struct Buffer *tmp, struct Buffer *s, unsigned long data,
  * avoid cyclic sourcing */
 static struct ListHead MuttrcStack = STAILQ_HEAD_INITIALIZER(MuttrcStack);
 
-/**
- * to_absolute_path - Convert relative filepath to an absolute path
- * @param path      Relative path
- * @param reference Absolute path that \a path is relative to
- * @retval true on success
- * @retval false otherwise
- *
- * Use POSIX functions to convert a path to absolute, relatively to another path
- * @note \a path should be at least of PATH_MAX length
- */
-static int to_absolute_path(char *path, const char *reference)
-{
-  const char *dirpath = NULL;
-  char abs_path[PATH_MAX];
-  int path_len;
-
-  /* if path is already absolute, don't do anything */
-  if ((strlen(path) > 1) && (path[0] == '/'))
-  {
-    return true;
-  }
-
-  dirpath = mutt_dirname(reference);
-  strfcpy(abs_path, dirpath, PATH_MAX);
-  safe_strncat(abs_path, sizeof(abs_path), "/", 1); /* append a / at the end of the path */
-
-  path_len = PATH_MAX - strlen(path);
-
-  safe_strncat(abs_path, sizeof(abs_path), path, path_len > 0 ? path_len : 0);
-
-  path = realpath(abs_path, path);
-
-  if (!path)
-  {
-    printf("Error: issue converting path to absolute (%s)", strerror(errno));
-    return false;
-  }
-
-  return true;
-}
-
 #define MAXERRS 128
 
 /**
@@ -4182,22 +4141,8 @@ void mutt_init(int skip_sys_rc, struct ListHead *commands)
   }
   else
   {
-    FILE *f = NULL;
-    char *c = NULL;
-
-    if ((f = safe_fopen(SYSCONFDIR "/nntpserver", "r")))
-    {
-      if (fgets(buffer, sizeof(buffer), f) == NULL)
-        buffer[0] = '\0';
-      p = buffer;
-      SKIPWS(p);
-      c = p;
-      while (*c && (*c != ' ') && (*c != '\t') && (*c != '\r') && (*c != '\n'))
-        c++;
-      *c = '\0';
-      NewsServer = safe_strdup(p);
-      fclose(f);
-    }
+    p = file_read_keyword(SYSCONFDIR "/nntpserver", buffer, sizeof(buffer));
+    NewsServer = safe_strdup(p);
   }
 #endif
 
index 7c9e4a6ca282b408d3e95d3e0d40bfd5f25c297a..63cce57f3288ec3daa80bb82de3e107ec514582d 100644 (file)
  *
  * Commonly used file/dir management routines.
  *
- * | Function                  | Description
- * | :------------------------ | :-----------------------------------------------------------
- * | mutt_basename()           | Find the last component for a pathname
- * | mutt_concatn_path()       | Concatenate directory and filename
- * | mutt_concat_path()        | Join a directory name and a filename
- * | mutt_copy_bytes()         | Copy some content from one file to another
- * | mutt_copy_stream()        | Copy the contents of one file into another
- * | mutt_decrease_mtime()     | Decrease a file's modification time by 1 second
- * | mutt_dirname()            | Return a path up to, but not including, the final '/'
- * | mutt_lock_file()          | (try to) lock a file
- * | mutt_mkdir()              | Recursively create directories
- * | mutt_quote_filename()     | Quote a filename to survive the shell's quoting rules
- * | mutt_read_line()          | Read a line from a file
- * | mutt_rmtree()             | Recursively remove a directory
- * | mutt_rename_file()        | Rename a file
+ * | Function                     | Description
+ * | :--------------------------- | :-----------------------------------------------------------
+ * | file_read_keyword()          | Read a keyword from a file
+ * | mbox_check_empty()           | Is the mailbox empty
+ * | mutt_basename()              | Find the last component for a pathname
+ * | mutt_concatn_path()          | Concatenate directory and filename
+ * | mutt_concat_path()           | Join a directory name and a filename
+ * | mutt_copy_bytes()            | Copy some content from one file to another
+ * | mutt_copy_stream()           | Copy the contents of one file into another
+ * | mutt_decrease_mtime()        | Decrease a file's modification time by 1 second
+ * | mutt_dirname()               | Return a path up to, but not including, the final '/'
+ * | mutt_lock_file()             | (try to) lock a file
+ * | mutt_mkdir()                 | Recursively create directories
+ * | mutt_quote_filename()        | Quote a filename to survive the shell's quoting rules
+ * | mutt_read_line()             | Read a line from a file
  * | mutt_regex_sanitize_string() | Escape any regex-magic characters in a string
- * | mutt_sanitize_filename()  | Replace unsafe characters in a filename
- * | mutt_set_mtime()          | Set the modification time of one file from another
- * | mutt_touch_atime()        | Set the access time to current time
- * | mutt_unlink()             | Delete a file, carefully
- * | mutt_unlink_empty()       | Delete a file if it's empty
- * | mutt_unlock_file()        | Unlock a file previously locked by mutt_lock_file()
- * | safe_fclose()             | Close a FILE handle (and NULL the pointer)
- * | safe_fopen()              | Call fopen() safely
- * | safe_fsync_close()        | Flush the data, before closing a file (and NULL the pointer)
- * | safe_open()               | Open a file
- * | safe_rename()             | NFS-safe renaming of files
- * | safe_symlink()            | Create a symlink
+ * | mutt_rename_file()           | Rename a file
+ * | mutt_rmtree()                | Recursively remove a directory
+ * | mutt_sanitize_filename()     | Replace unsafe characters in a filename
+ * | mutt_set_mtime()             | Set the modification time of one file from another
+ * | mutt_touch_atime()           | Set the access time to current time
+ * | mutt_unlink()                | Delete a file, carefully
+ * | mutt_unlink_empty()          | Delete a file if it's empty
+ * | mutt_unlock_file()           | Unlock a file previously locked by mutt_lock_file()
+ * | safe_fclose()                | Close a FILE handle (and NULL the pointer)
+ * | safe_fopen()                 | Call fopen() safely
+ * | safe_fsync_close()           | Flush the data, before closing a file (and NULL the pointer)
+ * | safe_open()                  | Open a file
+ * | safe_rename()                | NFS-safe renaming of files
+ * | safe_symlink()               | Create a symlink
+ * | to_absolute_path()           | Convert relative filepath to an absolute path
  */
 
 #include "config.h"
@@ -1163,3 +1166,98 @@ int mutt_rename_file(char *oldfile, char *newfile)
   mutt_unlink(oldfile);
   return 0;
 }
+
+/**
+ * to_absolute_path - Convert relative filepath to an absolute path
+ * @param path      Relative path
+ * @param reference Absolute path that \a path is relative to
+ * @retval true on success
+ * @retval false otherwise
+ *
+ * Use POSIX functions to convert a path to absolute, relatively to another path
+ * @note \a path should be at least of PATH_MAX length
+ */
+int to_absolute_path(char *path, const char *reference)
+{
+  const char *dirpath = NULL;
+  char abs_path[PATH_MAX];
+  int path_len;
+
+  /* if path is already absolute, don't do anything */
+  if ((strlen(path) > 1) && (path[0] == '/'))
+  {
+    return true;
+  }
+
+  dirpath = mutt_dirname(reference);
+  strfcpy(abs_path, dirpath, PATH_MAX);
+  safe_strncat(abs_path, sizeof(abs_path), "/", 1); /* append a / at the end of the path */
+
+  path_len = PATH_MAX - strlen(path);
+
+  safe_strncat(abs_path, sizeof(abs_path), path, path_len > 0 ? path_len : 0);
+
+  path = realpath(abs_path, path);
+
+  if (!path)
+  {
+    printf("Error: issue converting path to absolute (%s)", strerror(errno));
+    return false;
+  }
+
+  return true;
+}
+
+/**
+ * file_read_keyword - Read a keyword from a file
+ * @param file   File to read
+ * @param buffer Buffer to store the keyword
+ * @param buflen Length of the buffer
+ * @retval ptr Start of the keyword
+ *
+ * Read one line from the start of a file.
+ * Skip any leading whitespace and extract the first token.
+ */
+char *file_read_keyword(const char *file, char *buffer, size_t buflen)
+{
+  FILE *fp = NULL;
+  char *start = NULL;
+
+  fp = safe_fopen(file, "r");
+  if (!fp)
+    return NULL;
+
+  buffer = fgets(buffer, buflen, fp);
+  safe_fclose(&fp);
+
+  if (!buffer)
+    return NULL;
+
+  SKIPWS(buffer);
+  start = buffer;
+
+  while (*buffer && !isspace(*buffer))
+    buffer++;
+
+  *buffer = '\0';
+
+  return start;
+}
+
+/**
+ * mbox_check_empty - Is the mailbox empty
+ * @param path Path to mailbox
+ * @retval 1 mailbox is not empty
+ * @retval 0 mailbox is empty
+ * @retval -1 on error
+ */
+int mbox_check_empty(const char *path)
+{
+  struct stat st;
+
+  if (stat(path, &st) == -1)
+    return -1;
+
+  return ((st.st_size == 0));
+}
+
index 5e611b85941bc2dbf45fc02fafa840ee888d729a..3a37cb21593c543e0d2837e229ef43ec6edbd253 100644 (file)
@@ -33,6 +33,8 @@ struct stat;
 #define MUTT_CONT (1 << 0) /**< \-continuation */
 #define MUTT_EOL  (1 << 1) /**< don't strip `\n` / `\r\n` */
 
+char *      file_read_keyword(const char *file, char *buffer, size_t buflen);
+int         mbox_check_empty(const char *path);
 const char *mutt_basename(const char *f);
 char *      mutt_concatn_path(char *dst, size_t dstlen, const char *dir, size_t dirlen, const char *fname, size_t fnamelen);
 char *      mutt_concat_path(char *d, const char *dir, const char *fname, size_t l);
@@ -44,9 +46,9 @@ int         mutt_lock_file(const char *path, int fd, int excl, int timeout);
 int         mutt_mkdir(const char *path, mode_t mode);
 size_t      mutt_quote_filename(char *d, size_t l, const char *f);
 char *      mutt_read_line(char *s, size_t *size, FILE *fp, int *line, int flags);
-int         mutt_rmtree(const char *path);
 int         mutt_regex_sanitize_string(char *dest, size_t destlen, const char *src);
 int         mutt_rename_file(char *oldfile, char *newfile);
+int         mutt_rmtree(const char *path);
 void        mutt_sanitize_filename(char *f, short slash);
 void        mutt_set_mtime(const char *from, const char *to);
 void        mutt_touch_atime(int f);
@@ -59,5 +61,6 @@ int         safe_fsync_close(FILE **f);
 int         safe_open(const char *path, int flags);
 int         safe_rename(const char *src, const char *target);
 int         safe_symlink(const char *oldpath, const char *newpath);
+int         to_absolute_path(char *path, const char *reference);
 
 #endif /* _LIB_FILE_H */
diff --git a/mbox.c b/mbox.c
index 44b5ef07e334c19839cf191d6eed647ecaa601fe..d2441e740c99c9fddc984bdff9fe55b6eb5ddda2 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -1402,23 +1402,6 @@ bail: /* Come here in case of disaster */
   return rc;
 }
 
-/**
- * mbox_check_empty - Is the mailbox empty
- * @param path Path to mailbox
- * @retval 1 mailbox is not empty
- * @retval 0 mailbox is empty
- * @retval -1 on error
- */
-int mbox_check_empty(const char *path)
-{
-  struct stat st;
-
-  if (stat(path, &st) == -1)
-    return -1;
-
-  return ((st.st_size == 0));
-}
-
 struct MxOps mx_mbox_ops = {
   .open = mbox_open_mailbox,
   .open_append = mbox_open_mailbox_append,
diff --git a/mx.h b/mx.h
index f88dc8e22f28b137a6778f1b36789c03a29cad10..a950506a2e20ebbf357fe9af9e6a37ee10ea860e 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -87,7 +87,6 @@ WHERE short MboxType;
 
 #define MMDF_SEP "\001\001\001\001\n"
 
-int mbox_check_empty(const char *path);
 void mbox_reset_atime(struct Context *ctx, struct stat *st);
 
 int mh_check_empty(const char *path);