From d58e5061ad49a3778dd1295e28810ad1f6e44cf6 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Tue, 3 Mar 2009 15:04:15 +0000 Subject: Mikael Magnusson: 26670: add zsh/attr module --- Src/Modules/.distfiles | 2 + Src/Modules/attr.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++ Src/Modules/attr.mdd | 12 ++++ 3 files changed, 188 insertions(+) create mode 100644 Src/Modules/attr.c create mode 100644 Src/Modules/attr.mdd (limited to 'Src') diff --git a/Src/Modules/.distfiles b/Src/Modules/.distfiles index 40d3114ca..9231cecb3 100644 --- a/Src/Modules/.distfiles +++ b/Src/Modules/.distfiles @@ -2,6 +2,8 @@ DISTFILES_SRC=' .cvsignore .distfiles .exrc +attr.mdd +attr.c cap.mdd cap.c clone.mdd diff --git a/Src/Modules/attr.c b/Src/Modules/attr.c new file mode 100644 index 000000000..3355d5c6b --- /dev/null +++ b/Src/Modules/attr.c @@ -0,0 +1,174 @@ +/* + * attr.c - extended attributes (xattr) manipulation + * + * This file is part of zsh, the Z shell. + * + * Copyright (c) 2009 Mikael Magnusson + * All rights reserved. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and to distribute modified versions of this software for any + * purpose, provided that the above copyright notice and the following + * two paragraphs appear in all copies of this software. + * + * In no event shall Mikael Magnusson or the Zsh Development Group be liable + * to any party for direct, indirect, special, incidental, or consequential + * damages arising out of the use of this software and its documentation, + * even if Andrew Main and the Zsh Development Group have been advised of + * the possibility of such damage. + * + * Mikael Magnusson and the Zsh Development Group specifically disclaim any + * warranties, including, but not limited to, the implied warranties of + * merchantability and fitness for a particular purpose. The software + * provided hereunder is on an "as is" basis, and Mikael Magnusson and the + * Zsh Development Group have no obligation to provide maintenance, + * support, updates, enhancements, or modifications. + * + */ + +#include "attr.mdh" +#include "attr.pro" + +#include +#include + +static int +bin_getattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) +{ + int ret = 0; + int len; + char value[256]; + + unmetafy(*argv, NULL); + unmetafy(*(argv+1), NULL); + if (listxattr(*argv, NULL, 0) > 0) { + if (0 < (len = getxattr(*argv, *(argv+1), value, 255))) { + if (len < 256) { + value[len] = '\0'; + if (*(argv+2)) + setsparam(*(argv+2), metafy(value, len, META_DUP)); + else + printf("%s\n", value); + } + } else { + zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno); + ret = 1; + } + } + return ret; +} + +static int +bin_setattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) +{ + int ret = 0; + + unmetafy(*argv, NULL); + unmetafy(*(argv+1), NULL); + unmetafy(*(argv+2), NULL); + if (setxattr(*argv, *(argv+1), *(argv+2), strlen(*(argv+2)), 0)) { + zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno); + ret = 1; + } + return ret; +} + +static int +bin_delattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) +{ + int ret = 0; + + unmetafy(*argv, NULL); + unmetafy(*(argv+1), NULL); + if (removexattr(*argv, *(argv+1))) { + zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno); + ret = 1; + } + return ret; +} + +static int +bin_listattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) +{ + int ret = 0; + int len, i = 1; + char value[256]; + + unmetafy(*argv, NULL); + if (0 < (len = listxattr(*argv, value, 256))) { + if (len < 256) { + char *p = value; + if (*(argv+1)) + setsparam(*(argv+1), metafy(value, len, META_DUP)); + else while (p < &value[len]) { + printf("%s\n", p); + p += strlen(p) + 1; + } + } + } else { + zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno); + ret = 1; + } + return ret; +} + +/* module paraphernalia */ + +static struct builtin bintab[] = { + BUILTIN("zgetattr", 0, bin_getattr, 2, 3, 0, NULL, NULL), + BUILTIN("zsetattr", 0, bin_setattr, 3, 3, 0, NULL, NULL), + BUILTIN("zdelattr", 0, bin_delattr, 2, 2, 0, NULL, NULL), + BUILTIN("zlistattr", 0, bin_listattr, 1, 2, 0, NULL, NULL), +}; + +static struct features module_features = { + bintab, sizeof(bintab)/sizeof(*bintab), + NULL, 0, + NULL, 0, + NULL, 0, + 0 +}; + +/**/ +int +setup_(UNUSED(Module m)) +{ + return 0; +} + +/**/ +int +features_(Module m, char ***features) +{ + *features = featuresarray(m, &module_features); + return 0; +} + +/**/ +int +enables_(Module m, int **enables) +{ + return handlefeatures(m, &module_features, enables); +} + +/**/ +int +boot_(UNUSED(Module m)) +{ + return 0; +} + +/**/ +int +cleanup_(Module m) +{ + return setfeatureenables(m, &module_features, NULL); +} + +/**/ +int +finish_(UNUSED(Module m)) +{ + return 0; +} diff --git a/Src/Modules/attr.mdd b/Src/Modules/attr.mdd new file mode 100644 index 000000000..52f3e2420 --- /dev/null +++ b/Src/Modules/attr.mdd @@ -0,0 +1,12 @@ +name=zsh/attr +link='if test "x$ac_cv_func_getxattr" = xyes && test "x$ac_cv_header_sys_xattr_h" = xyes; then + echo dynamic +else + echo no +fi +' +load=no + +autofeatures="b:zgetattr b:zsetattr b:zdelattr" + +objects="attr.o" -- cgit 1.4.1