return value of executable

This is a discussion on return value of executable within the Linux Programming forums, part of the Platform Specific Boards category; I actually have a two part question. I'm trying to develop a script that will recurse through a directory tree ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Cool return value of executable

    I actually have a two part question. I'm trying to develop a script that will recurse through a directory tree and collect the regular file sizes to give me the total size of the tree. I've already gotten st_size from sys/stat.h to work via intmax_t from stdint.h to work in another script, but for whatever reason the only way it will produce a correct, non-zero result in the current app is to include it in a printf statement -- ie, printf ("%d", (intmax_t)statstruct.st_size) works where "bytes = (intmax_t)statstruct.st_size" does not (bizarrely), meaning I cannot manipulate or collect an integer.

    I thought of using the final return value of a seperate program (that already works) but of course system() returns a pid, not the actual return value. So my first question is: Can I do that somehow anyway (without using a pipe)?

    My other question: is there a better way than st_size/intmax_t to get a file size? And why does it work so strangely?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What's 'bytes'?

    As per the man page, they print the size with
    Code:
    printf(" %9jd", (intmax_t)statbuf.st_size);
    A portable way to get the file size is to open it, seek to the end with fseek() and use ftell(). Although I'd say stick with stat() in this case.

    As for executing the process, I'd say fork() it and use pipes .

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by MK27 View Post
    I actually have a two part question. I'm trying to develop a script that will recurse through a directory tree and collect the regular file sizes to give me the total size of the tree. I've already gotten st_size from sys/stat.h to work via intmax_t from stdint.h to work in another script, but for whatever reason the only way it will produce a correct, non-zero result in the current app is to include it in a printf statement -- ie, printf ("%d", (intmax_t)statstruct.st_size) works where "bytes = (intmax_t)statstruct.st_size" does not (bizarrely), meaning I cannot manipulate or collect an integer.

    I thought of using the final return value of a seperate program (that already works) but of course system() returns a pid, not the actual return value. So my first question is: Can I do that somehow anyway (without using a pipe)?

    My other question: is there a better way than st_size/intmax_t to get a file size? And why does it work so strangely?
    What wrong with using du ?
    You can or use executable as is or use it's code as example

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Thanks for the fseek tip, it may come down to that if it's not too slow, or else the a pipe. Because stat in this circumstance is useless. Think why:

    "bytes" would be an int variable. Let's call it "filesize" instead. So in one script this works:

    int filesize = (intmax_t)statbuf.st_size;

    Which gives an integer that can be accumulated and eventually divided by 1024 to give us the total Kb in a directory. Unfortunately, in my current script the plain equation will not produce a value, ONLY the printf statement:

    printf ("%d", (intmax_t)statbuf.st_size);

    Even if I switch these two lines and change nothing else! Clearly there is some explanation outside my very limited knowledge of C. Unless I can feed the printf output into an int buffer, it's useless (can I?).

    As for du: sure, also I could do this in perl easy or a lil' shellscript with ls -s. But i'm trying to learn C (either because i feel some necessity or else find sudoku too frustrating) and it would seem to me that if you can't write a script to recurse a tree and add up file sizes, then all that time spent decyphering "hello world!" was wasted.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think your problem with printing the value is that intmax_t is (at least) 64 bits long, so it's not printable with normal %d. There should be no problem with using it to accumulate your data, as long as you print it with the correct format specifier (e.g. "%lld").

    Using fseek() will not work on files bigger than 4GB, since ftell only returns 32-bit value -> not the correct size. And it's little point in using portable functions to find the size when you have unportable functions to read the directory and traverse it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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