OSDN Git Service

Clean up setfattr.
authorRob Landley <rob@landley.net>
Sun, 21 Aug 2016 01:20:06 +0000 (20:20 -0500)
committerRob Landley <rob@landley.net>
Sun, 21 Aug 2016 01:20:06 +0000 (20:20 -0500)
toys/pending/setfattr.c

index 9f17ce0..080991f 100644 (file)
@@ -4,21 +4,20 @@
  *
  * No standard
 
-USE_SETFATTR(NEWTOY(setfattr, "hn:v:x:[!xv]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_SETFATTR(NEWTOY(setfattr, "hn:|v:x:|[!xv]", TOYFLAG_USR|TOYFLAG_BIN))
 
 config SETFATTR
   bool "setfattr"
   default y
   help
-    usage: setfattr [-h] -n NAME [-v VALUE] FILE...
-    usage: setfattr [-h] -x NAME FILE...
+    usage: setfattr [-h] [-x|-n NAME] [-v VALUE] FILE...
 
     Write POSIX extended attributes.
 
-    -h Do not dereference symbolic links.
-    -n Set value of given attribute.
-    -x Remove value of given attribute.
-    -v Value to use with -n (default is empty).
+    -h Do not dereference symlink.
+    -n Set given attribute.
+    -x Remove given attribute.
+    -v Set value for attribute -n (default is empty).
 */
 
 #define FOR_setfattr
@@ -30,27 +29,19 @@ GLOBALS(
 
 static void do_setfattr(char *file)
 {
-  int (*setter)(const char *, const char *, const void *, size_t, int) =
-      setxattr;
-  int (*remover)(const char *, const char *) = removexattr;
-
-  if (toys.optflags&FLAG_h) {
-    setter = lsetxattr;
-    remover = lremovexattr;
-  }
+  int h = toys.optflags & FLAG_h;
 
   if (toys.optflags&FLAG_x) {
-    if (remover(file, TT.x)) perror_msg("removexattr failed");
-  } else {
-    if (setter(file, TT.n, TT.v, TT.v ? strlen(TT.v) : 0, 0))
+    if ((h ? lremovexattr : removexattr)(file, TT.x))
+      perror_msg("removexattr failed");
+  } else 
+    if ((h ? lsetxattr : setxattr)(file, TT.n, TT.v, TT.v?strlen(TT.v):0, 0))
       perror_msg("setxattr failed");
-  }
 }
 
 void setfattr_main(void)
 {
   char **s;
 
-  if (!(toys.optflags&(FLAG_n|FLAG_x))) error_exit("need 'n' or 'x'");
   for (s=toys.optargs; *s; s++) do_setfattr(*s);
 }