Thread: stat() returning same values all the time

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  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, 07:56 AM
  5. Passing And Returning Values
    By neo6132 in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 10:24 PM