]> Sergey Matveev's repositories - nnn.git/commitdiff
Support archiving with default utils
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 27 Jul 2019 06:44:49 +0000 (12:14 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 27 Jul 2019 06:44:49 +0000 (12:14 +0530)
src/nnn.c

index 305d54fb94bf92754f558f99720dfdfb49f17f4d..4bc38390f4d649b17c78a1810848e38524ff7699 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
 #define EXEC_ARGS_MAX 8
 #define SCROLLOFF 3
 #define LONG_SIZE sizeof(ulong)
+#define ARCHIVE_CMD_LEN 12
 
 /* Program return codes */
 #define _SUCCESS 0
@@ -474,6 +475,7 @@ static int spawn(char *file, char *arg1, char *arg2, const char *dir, uchar flag
 static int (*nftw_fn)(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
 static int dentfind(const char *fname, int n);
 static void move_cursor(int target, int ignore_scrolloff);
+static bool getutil(const char *util);
 
 /* Functions */
 
@@ -727,6 +729,20 @@ static size_t xstrlcpy(char *dest, const char *src, size_t n)
        return len;
 }
 
+static bool is_suffix(const char *str, const char *suffix)
+{
+       if (!str || !suffix)
+               return FALSE;
+
+       size_t lenstr = strlen(str);
+       size_t lensuffix = strlen(suffix);
+
+       if (lensuffix > lenstr)
+               return FALSE;
+
+       return (xstrcmp(str + (lenstr - lensuffix), suffix) == 0);
+}
+
 /*
  * The poor man's implementation of memrchr(3).
  * We are only looking for '/' in this program.
@@ -1263,6 +1279,18 @@ finish:
        return ret;
 }
 
+static void get_archive_cmd(char *cmd, char *archive)
+{
+       if (getutil(utils[ATOOL]))
+               xstrlcpy(cmd, "atool -a", ARCHIVE_CMD_LEN);
+       else if (getutil(utils[BSDTAR]))
+               xstrlcpy(cmd, "bsdtar -cvf", ARCHIVE_CMD_LEN);
+       else if (is_suffix(archive, ".zip"))
+               xstrlcpy(cmd, "zip -r", ARCHIVE_CMD_LEN);
+       else
+               xstrlcpy(cmd, "tar -cvf", ARCHIVE_CMD_LEN);
+}
+
 static void archive_selection(const char *cmd, const char *archive, const char *curpath)
 {
        char *buf = (char *)malloc(CMD_LEN_MAX * sizeof(char));
@@ -4141,14 +4169,9 @@ nochange:
                        switch (sel) {
                        case SEL_ARCHIVE:
                        {
-                               char cmd[] = "bsdtar -cf";
+                               char cmd[ARCHIVE_CMD_LEN];
 
-                               if (getutil(utils[ATOOL]))
-                                       xstrlcpy(cmd, "atool -a", 10);
-                               else if (!getutil(utils[BSDTAR])) {
-                                       printwait(messages[UTIL_MISSING], &presel);
-                                       goto nochange;
-                               }
+                               get_archive_cmd(cmd, tmp);
 
                                (r == 'y' || r == 'Y') ? archive_selection(cmd, tmp, path)
                                                       : spawn(cmd, tmp, dents[cur].name,