2 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2002 Poul-Henning Kamp.
4 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
7 * This software was developed for the FreeBSD Project by Poul-Henning
8 * Kamp and Network Associates Laboratories, the Security Research Division
9 * of Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The names of the authors may not be used to endorse or promote
21 * products derived from this software without specific prior written
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/types.h>
42 #include <sys/extattr.h>
54 static enum { EADUNNO
, EAGET
, EASET
, EARM
, EALS
} what
= EADUNNO
;
62 fprintf(stderr
, "usage: getextattr [-fhqsx] attrnamespace");
63 fprintf(stderr
, " attrname filename ...\n");
66 fprintf(stderr
, "usage: setextattr [-fhnq] attrnamespace");
67 fprintf(stderr
, " attrname attrvalue filename ...\n");
68 fprintf(stderr
, " or setextattr -i [-fhnq] attrnamespace");
69 fprintf(stderr
, " attrname filename ...\n");
72 fprintf(stderr
, "usage: rmextattr [-fhq] attrnamespace");
73 fprintf(stderr
, " attrname filename ...\n");
76 fprintf(stderr
, "usage: lsextattr [-fhq] attrnamespace");
77 fprintf(stderr
, " filename ...\n");
81 fprintf(stderr
, "usage: (getextattr|lsextattr|rmextattr");
82 fprintf(stderr
, "|setextattr)\n");
88 mkbuf(char **buf
, int *oldlen
, int newlen
)
91 if (*oldlen
>= newlen
)
95 *buf
= malloc(newlen
);
103 main(int argc
, char *argv
[])
105 #define STDIN_BUF_SZ 1024
106 char stdin_data
[STDIN_BUF_SZ
];
109 const char *options
, *attrname
;
112 int ch
, error
, i
, arg_counter
, attrnamespace
, minargc
;
118 struct sbuf
*attrvalue
= NULL
;
120 int flag_nofollow
= 0;
123 int flag_from_stdin
= 0;
127 p
= basename(argv
[0]);
130 if (!strcmp(p
, "getextattr")) {
134 } else if (!strcmp(p
, "setextattr")) {
138 } else if (!strcmp(p
, "rmextattr")) {
142 } else if (!strcmp(p
, "lsextattr")) {
150 while ((ch
= getopt(argc
, argv
, options
)) != -1) {
182 if (what
== EASET
&& flag_from_stdin
== 0)
188 error
= extattr_string_to_namespace(argv
[0], &attrnamespace
);
190 err(-1, "%s", argv
[0]);
200 attrvalue
= sbuf_new_auto();
201 if (flag_from_stdin
) {
202 while ((error
= read(0, stdin_data
, STDIN_BUF_SZ
)) > 0)
203 sbuf_bcat(attrvalue
, stdin_data
, error
);
205 sbuf_cpy(attrvalue
, argv
[0]);
208 sbuf_finish(attrvalue
);
211 for (arg_counter
= 0; arg_counter
< argc
; arg_counter
++) {
215 error
= extattr_delete_link(argv
[arg_counter
],
216 attrnamespace
, attrname
);
218 error
= extattr_delete_file(argv
[arg_counter
],
219 attrnamespace
, attrname
);
224 len
= sbuf_len(attrvalue
) + flag_null
;
226 ret
= extattr_set_link(argv
[arg_counter
],
227 attrnamespace
, attrname
,
228 sbuf_data(attrvalue
), len
);
230 ret
= extattr_set_file(argv
[arg_counter
],
231 attrnamespace
, attrname
,
232 sbuf_data(attrvalue
), len
);
234 if ((size_t)ret
!= len
&& !count_quiet
) {
235 warnx("Set %zd bytes of %zu for %s",
243 ret
= extattr_list_link(argv
[arg_counter
],
244 attrnamespace
, NULL
, 0);
246 ret
= extattr_list_file(argv
[arg_counter
],
247 attrnamespace
, NULL
, 0);
250 mkbuf(&buf
, &buflen
, ret
);
252 ret
= extattr_list_link(argv
[arg_counter
],
253 attrnamespace
, buf
, buflen
);
255 ret
= extattr_list_file(argv
[arg_counter
],
256 attrnamespace
, buf
, buflen
);
260 printf("%s\t", argv
[arg_counter
]);
261 for (i
= 0; i
< ret
; i
+= ch
+ 1) {
262 /* The attribute name length is unsigned. */
263 ch
= (unsigned char)buf
[i
];
264 printf("%s%*.*s", i
? "\t" : "",
265 ch
, ch
, buf
+ i
+ 1);
267 if (!count_quiet
|| ret
> 0)
272 ret
= extattr_get_link(argv
[arg_counter
],
273 attrnamespace
, attrname
, NULL
, 0);
275 ret
= extattr_get_file(argv
[arg_counter
],
276 attrnamespace
, attrname
, NULL
, 0);
279 mkbuf(&buf
, &buflen
, ret
);
281 ret
= extattr_get_link(argv
[arg_counter
],
282 attrnamespace
, attrname
, buf
, buflen
);
284 ret
= extattr_get_file(argv
[arg_counter
],
285 attrnamespace
, attrname
, buf
, buflen
);
289 printf("%s\t", argv
[arg_counter
]);
291 mkbuf(&visbuf
, &visbuflen
, ret
* 4 + 1);
292 strvisx(visbuf
, buf
, ret
,
293 VIS_SAFE
| VIS_WHITE
);
294 printf("\"%s\"", visbuf
);
295 } else if (flag_hex
) {
296 for (i
= 0; i
< ret
; i
++)
297 printf("%s%02x", i
? " " : "",
298 (unsigned char)buf
[i
]);
300 fwrite(buf
, ret
, 1, stdout
);
309 warn("%s: failed", argv
[arg_counter
]);