2 * Copyright (c) 2000-2002,2004 Silicon Graphics, Inc.
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
20 #include <sys/param.h>
30 #include <attr/attributes.h>
33 #define SETOP 1 /* do a SET operation */
34 #define GETOP 2 /* do a GET operation */
35 #define REMOVEOP 3 /* do a REMOVE operation */
36 #define LISTOP 4 /* do a LIST operation */
38 #define BUFSIZE (60*1024) /* buffer size for LIST operations */
40 static char *progname;
46 "Usage: %s [-LRSq] -s attrname [-V attrvalue] pathname # set value\n"
47 " %s [-LRSq] -g attrname pathname # get value\n"
48 " %s [-LRSq] -r attrname pathname # remove attr\n"
49 " %s [-LRq] -l pathname # list attrs \n"
50 " -s reads a value from stdin and -g writes a value to stdout\n"),
51 progname, progname, progname, progname);
56 main(int argc, char **argv)
58 char *attrname, *attrvalue, *filename, *buffer;
59 int attrlength, attrflags;
60 int opflag, i, ch, error, follow, verbose, rootflag, secureflag;
63 attrlist_cursor_t cursor;
65 progname = basename(argv[0]);
67 setlocale(LC_CTYPE, "");
68 setlocale(LC_MESSAGES, "");
69 bindtextdomain(PACKAGE, LOCALEDIR);
73 * Pick up and validate the arguments.
76 follow = opflag = rootflag = secureflag = 0;
77 attrname = attrvalue = NULL;
78 while ((ch = getopt(argc, argv, "s:V:g:r:lqLRS")) != EOF) {
81 if ((opflag != 0) && (opflag != SETOP)) {
83 _("Only one of -s, -g, -r, or -l allowed\n"));
90 if ((opflag != 0) && (opflag != SETOP)) {
91 fprintf(stderr, _("-V only allowed with -s\n"));
100 _("Only one of -s, -g, -r, or -l allowed\n"));
109 _("Only one of -s, -g, -r, or -l allowed\n"));
118 _("Only one of -s, -g, -r, or -l allowed\n"));
136 fprintf(stderr, _("Unrecognized option: %c\n"),
142 if (optind != argc-1) {
143 fprintf(stderr, _("A filename to operate on is required\n"));
146 filename = argv[optind];
148 attrflags = ((!follow ? ATTR_DONTFOLLOW : 0) |
149 (secureflag ? ATTR_SECURE : 0) |
150 (rootflag ? ATTR_ROOT : 0));
152 * Break out into option-specific processing.
156 if (attrvalue == NULL) {
157 attrvalue = malloc(ATTR_MAX_VALUELEN);
158 if (attrvalue == NULL) {
163 fread(attrvalue, 1, ATTR_MAX_VALUELEN, stdin);
165 attrlength = strlen(attrvalue);
167 error = attr_set(filename, attrname, attrvalue,
168 attrlength, attrflags);
171 fprintf(stderr, _("Could not set \"%s\" for %s\n"),
176 printf(_("Attribute \"%s\" set to a %d byte value "
177 "for %s:\n"), attrname, attrlength, filename);
178 fwrite(attrvalue, 1, attrlength, stdout);
184 attrvalue = malloc(ATTR_MAX_VALUELEN);
185 if (attrvalue == NULL) {
189 attrlength = ATTR_MAX_VALUELEN;
190 error = attr_get(filename, attrname, attrvalue,
191 &attrlength, attrflags);
194 fprintf(stderr, _("Could not get \"%s\" for %s\n"),
199 printf(_("Attribute \"%s\" had a %d byte value "
200 "for %s:\n"), attrname, attrlength, filename);
202 fwrite(attrvalue, 1, attrlength, stdout);
209 error = attr_remove(filename, attrname, attrflags);
211 perror("attr_remove");
212 fprintf(stderr, _("Could not remove \"%s\" for %s\n"),
219 if ((buffer = malloc(BUFSIZE)) == NULL) {
223 bzero((char *)&cursor, sizeof(cursor));
225 error = attr_list(filename, buffer, BUFSIZE,
230 _("Could not list \"%s\" for %s\n"),
235 alist = (attrlist_t *)buffer;
236 for (i = 0; i < alist->al_count; i++) {
237 aep = (attrlist_ent_t *)&buffer[ alist->al_offset[i] ];
240 _("Attribute \"%s\" has a %d byte value for %s\n"),
241 aep->a_name, aep->a_valuelen,
244 printf("%s\n", aep->a_name);
247 } while (alist->al_more);
252 _("At least one of -s, -g, -r, or -l is required\n"));