10f66f451Sopenharmony_ci/* setfattr.c - Write POSIX extended attributes.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2016 Android Open Source Project.
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * No standard
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciUSE_SETFATTR(NEWTOY(setfattr, "hn:|v:x:|[!xv]", TOYFLAG_USR|TOYFLAG_BIN))
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ciconfig SETFATTR
100f66f451Sopenharmony_ci  bool "setfattr"
110f66f451Sopenharmony_ci  default y
120f66f451Sopenharmony_ci  help
130f66f451Sopenharmony_ci    usage: setfattr [-h] [-x|-n NAME] [-v VALUE] FILE...
140f66f451Sopenharmony_ci
150f66f451Sopenharmony_ci    Write POSIX extended attributes.
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci    -h	Do not dereference symlink
180f66f451Sopenharmony_ci    -n	Set given attribute
190f66f451Sopenharmony_ci    -x	Remove given attribute
200f66f451Sopenharmony_ci    -v	Set value for attribute -n (default is empty)
210f66f451Sopenharmony_ci*/
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_ci#define FOR_setfattr
240f66f451Sopenharmony_ci#include "toys.h"
250f66f451Sopenharmony_ci
260f66f451Sopenharmony_ciGLOBALS(
270f66f451Sopenharmony_ci  char *x, *v, *n;
280f66f451Sopenharmony_ci)
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_civoid setfattr_main(void)
310f66f451Sopenharmony_ci{
320f66f451Sopenharmony_ci  int h = toys.optflags & FLAG_h, rc;
330f66f451Sopenharmony_ci  char **s;
340f66f451Sopenharmony_ci
350f66f451Sopenharmony_ci  for (s=toys.optargs; *s; s++) {
360f66f451Sopenharmony_ci    if (TT.x) rc = (h?lremovexattr:removexattr)(*s, TT.x);
370f66f451Sopenharmony_ci    else rc = (h?lsetxattr:setxattr)(*s, TT.n, TT.v, TT.v?strlen(TT.v):0, 0);
380f66f451Sopenharmony_ci
390f66f451Sopenharmony_ci    if (rc) perror_msg("%s", *s);
400f66f451Sopenharmony_ci  }
410f66f451Sopenharmony_ci}
42