Thread: Stat() Program

  1. #1
    Registered User
    Join Date
    Aug 2001

    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

    #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
    		if ( nl != NULL ) 
    		  *nl = '\0';   // remove the end of line 
        if( stat( file, &fileattrib ) != 0 ) {
    		    printf("Access Permissions of file: %s\n ",file);
    			  printf( "%d\n", fileattrib.st_mode );
    			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

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10: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, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM