tags:

views:

738

answers:

5

On Fedora Core 7, I'm writing some code that relies on ARG_MAX. However, even if I #include <limits.h>, the constant is still not defined. My investigations show that it's present in <sys/linux/limits.h>, but this is supposed to be portable across Win32/Mac/Linux, so directly including it isn't an option. What's going on here?

A: 

Sorry to answer a question with a question, but why do you need ARG_MAX?

Christian Oudard
A: 

I'm assuming the filename that are missing in your post are limits.h and linux/limits.h. I don't know why they are missing, but I'm also wondering why your code needs them. I can think of no good reason. The value on any normal operating system these days is high enough not to worry about it.

Leon Timmermans
`<` was probably unescaped at first (and the site thought he wanted an HTML tag).
jnylen
+6  A: 

The reason it's not in limits.h is that it's not a quantity giving the limits of the value range of an integral type based on bit width on the current architecture. That's the role assigned to limits.h by the ISO standard.

The value in which you're interested is not hardware-bound in practice and can vary from platform to platform and perhaps system build to system build.

The correct thing to do is to call sysconf and ask it for "ARG_MAX" or "_POSIX_ARG_MAX". I think that's the POSIX-compliant solution anyway.

Acc. to my documentation, you include one or both of unistd.h or limits.h based on what values you're requesting.

One other point: many implementations of the exec family of functions return E2BIG or a similar value if you try to call them with an oversized environment. This is one of the defined conditions under which exec can actually return.

Thomas Kammeyer
A: 

The answer to the non-answers is that I'm maintaining code and don't have the authority or inclination to go rewriting it in a different way. The people who wrote it are pretty darn smart.

Josh Matthews
A: 

So there is never a need to modify any parameters in any way ever?

I can think of a perfect example why you would need to adjust the ARG_MAX values:

if [ -n "$FILE_EX" ]; then
 cd $SOME_DIR
   for FILE_I in *;do
    echo "$FILE_I" >> $logdir/inbound.log
   done
 cd -
fi

This works fine in most cases, except when the paths/filenames exceed the ARG_MAX (which it easily can if you are working with long filenames and lots of them), then you suddenly have a broken script and are left with and ugly warning message that makes you want to tear your hair out.