stat() returning same values all the time

This is a discussion on stat() returning same values all the time within the C Programming forums, part of the General Programming Boards category; Hi, when i use stat the values that are returned are identical regardless of what file is passed I currently ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    stat() returning same values all the time

    Hi,

    when i use stat the values that are returned are identical regardless of what file is passed

    I currently have a script that when the user uses RM it calls the c code called 'delete' with the filename as a variable. The main value i'm after is the filesize which is always printed as 5658608 bytes. Does anyone have any suggestions what i'm doing wrong as i can't see anything that would cause this.

    Thanks

    Here is the script:
    Code:
    #!/bin/bash
    mv $@ $HOME/.Trash
    delete $@
    Here is the delete.c code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
     	struct stat sb;
    	stat(argv[1], &sb);
    	
    	printf("%d\n", sb.st_size);
    	return 0;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    The st_size field is an off_t. For all we know that quantity is 64-bit. Make sure you are printing it properly.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You also didn't even bother to check the return value of stat. Perhaps it's failing?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    It seems the call to stat was failing. The problem i've encountered now is that the call will always fail unless i'm in the directory in which the delete code is in. The small script in the 1st post is named rm so that all this information is displayed prior to deleting.

    The script is currently in /usr/bin so when i call for example "rm /home/dave/test.txt" from /usr/bin i get the correct values. However calling rm test.txt from the /home/dave directory comes up with a file not found.

    I don't really know how to fix this so that regardless of where i call my rm script from it will display the proper information. If anyone can suggest anything it'd be greatly appreciated.

    I've attached my new code for delete.c incase something is wrong in there.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
      	const char delimiters[] = ".";
        	char *ext;
    	mystat(argv[1], 0);
    	return 0;
    }
    
    int mystat(char *file, int fd)
    {
    	int err;
    	struct stat st;
    	if (file != NULL)
    		err = stat(file, &st);
    	else
    		err = fstat(fd, &st);
    	if (err < 0)
    	{
    		perror(file);
    		return -1;
    	}
    	if (file != NULL)
    		printf("%s:\n", file);
    	else
    	printf("%d:\n", fd);
    	printf("dev     = %d\n", st.st_dev);
    	printf("ino     = %d\n", st.st_ino);
    	printf("mode    = \\0%010lo %s\n", st.st_mode);
    	printf("nlink   = %d\n", st.st_nlink);
    	printf("uid     = %d\n", st.st_uid);
    	printf("gid     = %d\n", st.st_gid);
    	printf("size    = %lld\n", st.st_size);
    	printf("blksize = %ld\n", st.st_blksize);
    	return 0;
    }

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    You could very likely have a bug in your script; hard to say without seeing it [edit: I'm blind. Sorry.]

    As for the code, turn up warnings on your compiler. If you're using gcc, you should always be using at least the -Wall flag. It will catch many problems in your code.

    One thing that -Wall won't catch in your code is the fact that you're using argv[1] without checking argc. It's possible that argv[1] doesn't exist and thus accessing it will cause undefined behavior.
    Last edited by cas; 02-24-2008 at 10:50 PM. Reason: Apparently I cannot read...

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I think you're going to have to parse the parm in your script to determine if a path has been specified. If so, use it. If not, get the current working directory and prefix it to the file name.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by pamplemoose View Post
    Hi,

    when i use stat the values that are returned are identical regardless of what file is passed

    Here is the script:
    Code:
    #!/bin/bash
    mv $@ $HOME/.Trash
    delete $@
    Do you see now what you are doing wrong? It's not your C program, but any program would fail, as the scripts file parameters are no longer in their location...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  2. Convert Values Into Time And Kbps Or Mbps
    By amitmistry_petl in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2007, 06:48 AM
  3. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  5. Passing And Returning Values
    By neo6132 in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 11:24 PM

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