Stat() Program

This is a discussion on Stat() Program within the C Programming forums, part of the General Programming Boards category; Ok I have to write a program that uses the linux stat() function, it is supposed to read in a ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    27

    Stat() Program

    Ok I have to write a program that uses the linux stat() function, it is supposed to read in a filename, were from were it will determine access permissions, it will end once the user enter a blank filename i.e '/0' or enter.

    Can anyone see any probs, as i am unable to yet test it.
    thanks for any help

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/stat.h>
    
    #define BUFFER 10
    
    struct stat fileattrib;
    
    char file[BUFFER], *nl;
    
    int main(int argc, char *argv[])
    {
      // Get file name
      printf("Enter Filename....\n");
    	while (gets(file) != NULL) // Loop getting filename while not null
    	{
    		*nl = strchr( buff, '\n' ); //Search out the null entry
    
    		if ((buf[0] == '\n') // if a null is a 0 then exit
    		break;
    
    		if ( nl != NULL ) 
    		  *nl = '\0';   // remove the end of line 
    
        if( stat( file, &fileattrib ) != 0 ) {
    			{
    		    printf("Access Permissions of file: %s\n ",file);
    				printf("----------------------------\n");
    			  printf( "%d\n", fileattrib.st_mode );
    		  }
    		else
    			printf("File Error Message = %s\n", strerror(errno));
    
    		printf("Enter Filename....\n"); // for next filename input
    		}
    }

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Re: Stat() Program

    > while (gets(file) != NULL)
    You should use scanf() instead. Most people agree that gets() is dangerous.


    > *nl = strchr( buff, '\n' );
    I think you wanted this to be:

    nl = strchr(buf, '\n');

    > if ( nl != NULL )

    I think you wanted this to be:

    if (*nl != NULL)


    Mind you that I just parsed through and looked as I leaped. Those are the things I noticed.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    I see you've managed to butcher my program by using gets instead of fgets.
    my program

    As a result, you've introduced a number of bugs

    I mean, all you had to add was
    printf( "%d\n", fileattrib.st_mode );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Soz salem, got mixed up, i thought fgets was for getting information from a file, silly me.
    Soz mate
    -ali

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21