Thread: Unable to List Files Attributes on EXT2

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Unhappy Unable to List Files Attributes on EXT2

    The code is well commented, the compiler isn't pointing anything really great, so I'm kinda lost, it is an exercise on a book I'm reading...
    Code:
    /*check flags.c - display info on ext2 extended attributes*/
    
    /* For each file name on the command line, display information on that 
    file's ext2 attributes */
    
    #include<errno.h>
    #include<fcntl.h>
    #include<linux/ext2_fs.h>
    #include<stdio.h>
    #include<sys/ioctl.h>
    #include<unistd.h>
    
    int main(int argc, char **argv){
    	
    	char **filename = argv + 1;
    	int fd;
    	int flags;
    	
    /*Iterate over each file name on the command line. The last pointer in argv[]
    	is NULL, so this while() loop is legal*/
    	while (*filename){
    
    /*Unlike normal attributes, ext2 attributes can only be queried if 
    we have a file descriptor (a file name is not sufficient). We 
    dont need write acces to query the ext2 attributes, so O_RDONLY is ok*/
    		
    		fd = open(*filename, O_RDONLY);
    			if (fd < 0) {
    				fprintf(stderr,"cannot open %s: %s\n", *filename, 
    						strerror(errno));
    				return 1;
    			}
    /*This gets the attributes, and puts them into flags*/
    				
    			if (ioctl(fd, EXT2_IOC_GETFLAGS, &flags)){
    				fprintf(stderr,"ioctl failed on %s: %s\n", *filename, 
    						strerror(errno));
    				return 1;
    			}
    			
    			printf("%s:", *filename++);
    
    /*Check for each attribute, and display a message for each one wich is returned 
    on*/
    			if (flags & EXT2_APPEND_FL) printf("Append");
    			if (flags & EXT2_IMMUTABLE_FL) printf("Immutable");
    			if (flags & EXT2_SYNC_FL) printf("Sync");
    			if (flags & EXT2_NODUMP_FL) printf("No dump");
    			
    			printf("\n");
    			close(fd);			
    	}
    	
    	return 0;
    }
    The problem is simple, the program does nothing...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you supplying any filenames on the command line (i.e.: flags file1 file2 file3)?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by itsme86
    Are you supplying any filenames on the command line (i.e.: flags file1 file2 file3)?
    yes, but I now I thing I might be doing it wrong in Anjuta (the IDE I'm using) could you plase give an example of call to the program to check one file? I would be very gratefull!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't just hit the "run" button in the IDE to get it to display anything. Compile it, then go to a shell (command line prompt, whatever you want to call it) and type in: flags file where flags is the name of the program and file is the name of the file.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Well I think I'm doing right, but I got no output anyways:
    imanewbie@blah:~/Projects/Junk Code$ ./checkflags random.c
    random.c:

    I've called the program checkgflags.

    Woops I might figured out now:

    EXECUTING:
    /home/imanewbie/Projects/Junk Code/checkflags flags senha.c
    ----------------------------------------------
    cannot open flags: No such file or directory

    ----------------------------------------------
    Program exited successfully with errcode (1)
    Press the Enter key to close this terminal ...


    No idea about what is wrong
    Last edited by Maragato; 08-10-2004 at 02:33 PM.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Quote Originally Posted by Maragato
    cannot open flags: No such file or directory
    It is trying to open flags as a file, it is not finding it.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    EXECUTING:
    /home/imanewbie/Projects/Junk Code/checkflags checkflags.c
    ----------------------------------------------
    checkflags.c:

    ----------------------------------------------
    Program exited successfully with errcode (0)
    Press the Enter key to close this terminal ...


    Suggestions?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Program exited successfully
    What's the problem exactly?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Hammer
    >>Program exited successfully
    What's the problem exactly?
    The program exited (didn't crash) but also did'nt displayed the info I was waiting for:

    Code:
    /*check flags.c - display info on ext2 extended attributes*/
    
    /* For each file name on the command line, display information on that 
    file's ext2 attributes */
    
    #include<errno.h>
    #include<fcntl.h>
    #include<linux/ext2_fs.h>
    #include<stdio.h>
    #include<sys/ioctl.h>
    #include<unistd.h>
    
    int main(int argc, char **argv){
    	
    	char **filename = argv + 1;
    	int fd;
    	int flags;
    	
    /*Iterate over each file name on the command line. The last pointer in argv[]
    	is NULL, so this while() loop is legal*/
    	while (*filename){
    
    /*Unlike normal attributes, ext2 attributes can only be queried if 
    we have a file descriptor (a file name is not sufficient). We 
    dont need write acces to query the ext2 attributes, so O_RDONLY is ok*/
    		
    		fd = open(*filename, O_RDONLY);
    			if (fd < 0) {
    				fprintf(stderr,"cannot open %s: %s\n", *filename, 
    						strerror(errno));
    				return 1;
    			}
    /*This gets the attributes, and puts them into flags*/
    				
    			if (ioctl(fd, EXT2_IOC_GETFLAGS, &flags)){
    				fprintf(stderr,"ioctl failed on %s: %s\n", *filename, 
    						strerror(errno));
    				return 1;
    			}
    			
    			printf("%s:", *filename++);
    
    /*Check for each attribute, and display a message for each one wich is returned 
    on*/
    			if (flags & EXT2_APPEND_FL) printf("Append");
    			if (flags & EXT2_IMMUTABLE_FL) printf("Immutable");
    			if (flags & EXT2_SYNC_FL) printf("Sync");
    			if (flags & EXT2_NODUMP_FL) printf("No dump");
    			
    			printf("\n");
    			close(fd);			
    	}
    	
    	return 0;
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you pass it any parameters?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Hammer
    Did you pass it any parameters?
    I think this piece of code should do it:
    Code:
    /*This gets the attributes, and puts them into flags*/
    				
    			if (ioctl(fd, EXT2_IOC_GETFLAGS, &flags)){
    				fprintf(stderr,"ioctl failed on %s: %s\n", *filename, 
    						strerror(errno));
    				return 1;
    			}

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, they mean, are you calling your program correctly? You see, unless you supply command line arguments, then there will be nothing for filename to point at. IE:
    Code:
    ./myfunction arg1 arg2 arg3
    Those arguments would end up being stored in argc. Unless you call your program correctly, there will be nothing for your pointer to point at. Here, add this to your code:
    Code:
    int main(int argc, char **argv){
    	
    	char **filename = argv + 1;
    	int fd;
    	int flags;
    	
    	if( argc < 2 )
    	{
    		printf("Nope, you didn't call your program correctly.\n"
    		printf("Next time try some command line parameters, eh?\n");
    		exit( 0 );
    	}
    
    /*Iterate over each file name on the command line. The last pointer in argv[]
    	is NULL, so this while() loop is legal*/
    	while (*filename){
    You see, this part of your code only works if you supply command line parameters. If you don't, well...
    Quote Originally Posted by Maragato
    The problem is simple, the program does nothing...
    Quote Originally Posted by Maragato
    The program exited (didn't crash) but also did'nt displayed the info I was waiting for:
    But then, you've already been told that:
    Quote Originally Posted by itsme86
    Are you supplying any filenames on the command line (i.e.: flags file1 file2 file3)?
    Quote Originally Posted by Hammer
    Did you pass it any parameters?
    I mean really, we can only point out the obvious so many times before it ceases to be amusing. And when it ceases to be amusing, I cease replying.



    Quzah.
    [edit]Tidying up color tags.[/edit]
    Last edited by quzah; 08-11-2004 at 08:45 AM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Well I added you piece of code to check for the parameters but it goes trought without printing the error advices... But still not working.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Depends what you mean by not working.
    The code you posted a couple of posts ago works just fine - it prints a list of filenames passed on the command line.
    As for printing the flags, well that really depends on finding files with those attributes set.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Depends what you mean by not working.
    The code you posted a couple of posts ago works just fine - it prints a list of filenames passed on the command line.
    As for printing the flags, well that really depends on finding files with those attributes set.
    Salem, I've read on Google about passing the parameters on command line, but the code didn't worked the way they told me to call it, could you please, just type the way I must call it on shell?

    MyUser@blah:~here follows the call that I don't know how to do?
    Thx a lot by the attention.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM