2 * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Jérôme Duval, jerome.duval@free.fr
7 * Axel Dörfler, axeld@pinc-software.de
10 /*! Remove an attribute from a file */
23 extern const char *__progname
;
24 const char *kProgramName
= __progname
;
32 printf("usage: %s [-P] [-p] attr filename1 [filename2...]\n"
33 "\t'attr' is the name of an attribute of the file\n"
34 "\t-P : Don't resolve links\n"
35 "\tIf '-p' is specified, 'attr' is regarded as a pattern.\n", kProgramName
);
42 open_attr_dir(const char* /*path*/)
44 return fs_fopen_attr_dir(gCurrentFile
);
49 stat_attr(const char* /*attribute*/, struct stat
* stat
)
51 memset(stat
, 0, sizeof(struct stat
));
52 stat
->st_mode
= S_IFREG
;
58 remove_attribute(int fd
, const char* attribute
, bool isPattern
)
61 return fs_remove_attr(fd
, attribute
);
64 memset(&glob
, 0, sizeof(glob_t
));
66 glob
.gl_closedir
= (void (*)(void *))fs_close_attr_dir
;
67 glob
.gl_readdir
= (dirent
* (*)(void*))fs_read_attr_dir
;
68 glob
.gl_opendir
= open_attr_dir
;
69 glob
.gl_lstat
= stat_attr
;
70 glob
.gl_stat
= stat_attr
;
72 // for open_attr_dir():
75 int result
= ::glob(attribute
, GLOB_ALTDIRFUNC
, NULL
, &glob
);
83 for (int i
= 0; i
< glob
.gl_pathc
; i
++) {
84 if (fs_remove_attr(fd
, glob
.gl_pathv
[i
]) != 0)
88 return error
? -1 : 0;
93 main(int argc
, const char **argv
)
95 bool isPattern
= false;
96 bool resolveLinks
= true;
101 const char *arg
= *argv
;
105 if (!strcmp(arg
, "-P"))
106 resolveLinks
= false;
107 else if (!strcmp(arg
, "-p"))
111 // Make sure we have the minimum number of arguments.
115 for (int32 i
= 1; i
< argc
; i
++) {
116 int fd
= open(argv
[i
], O_RDONLY
| (resolveLinks
? 0 : O_NOTRAVERSE
));
118 fprintf(stderr
, "%s: can\'t open file %s to remove attribute: %s\n",
119 kProgramName
, argv
[i
], strerror(errno
));
123 if (remove_attribute(fd
, argv
[0], isPattern
) != B_OK
) {
124 fprintf(stderr
, "%s: error removing attribute %s from %s: %s\n",
125 kProgramName
, argv
[0], argv
[i
], strerror(errno
));