1 // $Id: showattr.c,v 1.1.4.1 2003/11/18 22:31:10 ensc Exp $
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on showattr.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <sys/ioctl.h>
35 // Patch to help compile this utility on unpatched kernel source
36 #ifndef EXT2_IUNLINK_FL
37 /* Set both bits for backward compatibility */
38 #define EXT2_IUNLINK_FL 0x08008000
41 #ifndef EXT2_BARRIER_FL
42 #define EXT2_BARRIER_FL 0x04000000
46 Get the extended attributes of a file
48 static int getext2flags (const char *fname, long *flags)
51 int fd = open (fname,O_RDONLY);
53 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
56 ret = ioctl (fd,EXT2_IOC_GETFLAGS,flags);
59 fprintf (stderr,"Can't get ext2 flags on file %s (%s)\n"
60 ,fname,strerror(errno));
67 Set the extended attributes of a file
69 static int setext2flags (const char *fname, long flags)
72 int fd = open (fname,O_RDONLY);
74 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
76 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
79 fprintf (stderr,"Can't set ext2 flags on file %s (%s)\n"
80 ,fname,strerror(errno));
87 int main (int argc, char *argv[])
92 ,"showattr file ...\n"
94 "Presents extended file attribute.\n"
96 "setattr --immutable --immulink file ...\n"
98 "Sets the extended file attributes.\n"
100 "These utilities exist as an interim until lsattr and\n"
101 "chattr are updated.\n"
103 }else if (strstr(argv[0],"showattr")!=NULL){
105 for (i=1; i<argc; i++){
107 ret = getext2flags (argv[i],&flags);
111 printf ("%s\t%08lx\n",argv[i],flags);
114 }else if (strstr(argv[0],"setattr")!=NULL){
115 long flags, add_flags = 0, remove_flags = 0;
118 for (i=1; i<argc; i++){
119 const char *arg = argv[i];
120 if (strncmp(arg,"--",2)==0){
121 if (strcmp(arg,"--immutable")==0){
122 add_flags |= EXT2_IMMUTABLE_FL;
123 }else if (strcmp(arg,"--!immutable")==0 || strcmp(arg,"--~immutable")==0){
124 remove_flags |= EXT2_IMMUTABLE_FL;
125 }else if (strcmp(arg,"--immulink")==0){
126 add_flags |= EXT2_IUNLINK_FL;
127 }else if (strcmp(arg,"--!immulink")==0 || strcmp(arg,"--~immulink")==0){
128 remove_flags |= EXT2_IUNLINK_FL;
129 }else if (strcmp(arg,"--barrier")==0){
130 add_flags |= EXT2_BARRIER_FL;
131 }else if (strcmp(arg,"--!barrier")==0 || strcmp(arg,"--~barrier")==0){
132 remove_flags |= EXT2_BARRIER_FL;
134 fprintf (stderr,"Invalid option %s\n",arg);
139 ret = getext2flags (arg,&flags);
144 flags &= ~remove_flags;
145 ret = setext2flags (arg,flags);