Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH 4/4] attr: Use descriptive variables for argv and allow setting values with embedded nuls



---
 Src/Modules/attr.c |   63 ++++++++++++++++++++++++++-------------------------
 1 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/Src/Modules/attr.c b/Src/Modules/attr.c
index bb30ebb..88ebb16 100644
--- a/Src/Modules/attr.c
+++ b/Src/Modules/attr.c
@@ -99,26 +99,26 @@ bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func))
 {
     int ret = 0;
     int list_len, val_len, attr_len, slen;
-    char *value;
+    char *value, *file = argv[0], *attr = argv[1], *param = argv[2];
     int symlink = OPT_ISSET(ops, 'h');

-    unmetafy(*argv, &slen);
-    unmetafy(*(argv+1), NULL);
-    list_len = xlistxattr(*argv, NULL, 0, symlink);
+    unmetafy(file, &slen);
+    unmetafy(attr, NULL);
+    list_len = xlistxattr(file, NULL, 0, symlink);
     if (list_len > 0) {
-        val_len = xgetxattr(*argv, *(argv+1), NULL, 0, symlink);
+        val_len = xgetxattr(file, attr, NULL, 0, symlink);
         if (val_len == 0) {
-            if (*(argv+2))
-                unsetparam(*(argv+2));
+            if (param)
+                unsetparam(param);
             return 0;
         }
         if (val_len > 0) {
             value = (char *)zalloc(val_len+1);
-            attr_len = xgetxattr(*argv, *(argv+1), value, val_len, symlink);
+            attr_len = xgetxattr(file, attr, value, val_len, symlink);
             if (attr_len > 0 && attr_len <= val_len) {
                 value[attr_len] = '\0';
-                if (*(argv+2))
-                    setsparam(*(argv+2), metafy(value, attr_len, META_DUP));
+                if (param)
+                    setsparam(param, metafy(value, attr_len, META_DUP));
                 else
                     printf("%s\n", value);
             }
@@ -126,7 +126,7 @@ bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func))
         }
     }
     if (list_len < 0 || val_len < 0 || attr_len < 0)  {
-        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
+        zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
         ret = 1 + (attr_len > val_len);
     }
     return ret;
@@ -135,14 +135,15 @@ bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func))
 static int
 bin_setattr(char *nam, char **argv, Options ops, UNUSED(int func))
 {
-    int ret = 0, slen;
+    int ret = 0, slen, vlen;
     int symlink = OPT_ISSET(ops, 'h');
+    char *file = argv[0], *attr = argv[1], *value = argv[2];

-    unmetafy(*argv, &slen);
-    unmetafy(*(argv+1), NULL);
-    unmetafy(*(argv+2), NULL);
-    if (xsetxattr(*argv, *(argv+1), *(argv+2), strlen(*(argv+2)), 0, symlink)) {
-        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
+    unmetafy(file, &slen);
+    unmetafy(attr, NULL);
+    unmetafy(value, &vlen);
+    if (xsetxattr(file, attr, value, vlen, 0, symlink)) {
+        zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
         ret = 1;
     }
     return ret;
@@ -153,12 +154,12 @@ bin_delattr(char *nam, char **argv, Options ops, UNUSED(int func))
 {
     int ret = 0, slen;
     int symlink = OPT_ISSET(ops, 'h');
-    char *file = *argv;
+    char *file = argv[0], **attr = &argv[1];

     unmetafy(file, &slen);
-    while (*++argv) {
-        unmetafy(*argv, NULL);
-        if (xremovexattr(file, *argv, symlink)) {
+    while (*++attr) {
+        unmetafy(*attr, NULL);
+        if (xremovexattr(file, *attr, symlink)) {
             zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
             ret = 1;
             break;
@@ -172,24 +173,24 @@ bin_listattr(char *nam, char **argv, Options ops, UNUSED(int func))
 {
     int ret = 0;
     int val_len, list_len, slen;
-    char *value;
+    char *value, *file = argv[0], *param = argv[1];
     int symlink = OPT_ISSET(ops, 'h');

-    unmetafy(*argv, &slen);
-    val_len = xlistxattr(*argv, NULL, 0, symlink);
+    unmetafy(file, &slen);
+    val_len = xlistxattr(file, NULL, 0, symlink);
     if (val_len == 0) {
-        if (*(argv+1))
-            unsetparam(*(argv+1));
+        if (param)
+            unsetparam(param);
         return 0;
     }
     if (val_len > 0) {
         value = (char *)zalloc(val_len+1);
-        list_len = xlistxattr(*argv, value, val_len, symlink);
+        list_len = xlistxattr(file, value, val_len, symlink);
         if (list_len > 0 && list_len <= val_len) {
             char *p = value;
-            if (*(argv+1)) {
+            if (param) {
                 if (strlen(value) + 1 == list_len)
-                    setsparam(*(argv+1), metafy(value, list_len-1, META_DUP));
+                    setsparam(param, metafy(value, list_len-1, META_DUP));
                 else {
                     int arrlen = 0;
                     char **array = NULL, **arrptr = NULL;
@@ -204,7 +205,7 @@ bin_listattr(char *nam, char **argv, Options ops, UNUSED(int func))
                         *arrptr++ = metafy(p, -1, META_DUP);
                         p += strlen(p) + 1;
                     }
-                    setaparam(*(argv+1), array);
+                    setaparam(param, array);
                 }
             } else while (p < &value[list_len]) {
                 printf("%s\n", p);
@@ -214,7 +215,7 @@ bin_listattr(char *nam, char **argv, Options ops, UNUSED(int func))
         zfree(value, val_len+1);
     }
     if (val_len < 0 || list_len < 0) {
-        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
+        zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
         ret = 1 + (list_len > val_len);
     }
     return ret;
--
1.6.5



Messages sorted by: Reverse Date, Date, Thread, Author