sript (Perl) test code needed

Oliver Fromme olli at lurza.secnetix.de
Thu Jun 30 10:06:42 GMT 2005


David O'Brien <obrien at freebsd.org> wrote:
 > I know this should be trivial to do in perl and maybe someone here has the
 > time to hack it up.  I want a small test script (actually some other
 > scripting language than perl preferred).
 > 
 > The script should run the following commands until an error is returned:
 > 
 >     ls
 >     ls .
 >     ls ./
 >     ls ./.
 >     ls ././
 >     ls ././.
 >     ls ./././
 >     etc...
 > 
 > and when the error occurs print out the length of the command line.
 > I want to get the bottom of the "command too long" issue that causes too
 > much trouble deploying Java on FreeBSD/AMD64.

This is pretty easy to do in plain /bin/sh.

   CMD="ls "
   while $CMD >/dev/null; do
           case "$CMD" in
                   *.)     CMD="$CMD/";;
                   *)      CMD="$CMD.";;
           esac
   done
   echo "length == ${#CMD}"

On my old 4-stable/i386 machine, it reports "File name too
long" when the length of the command reaches 1027 chars
(i.e. the path is 1024 chars, which is PATH_MAX).

When replacing "/" with " ", so that the commands become
"ls . . . . " etc., it runs a lot longer, until finally it
reports "argument list too long" when the command length
reaches 61344 chars.  Since my environment is 2079 chars,
and the " >/dev/null" is another 12 bytes, that adds up
to 65532 bytes.  ARG_MAX is 65536.  Don't ask me where the
remaining 4 chars go.  Maybe some safety margins in the
implementation, or alignment issues.

Best regards
   Oliver

PS:  I couldn't help writing the same in Python, too.  :-)
It reports the same result, of course.

   import os
   cmd = "ls "
   while os.system(cmd + " >/dev/null") == 0:
           cmd = cmd + "/."[len(cmd) % 2]
   print "length ==", len(cmd)

And in awk ...

   BEGIN {
       cmd = "ls "
       while (system(cmd " >/dev/null") == 0)
           cmd = cmd substr("/.", length(cmd) % 2 + 1, 1)
       print "length ==", length(cmd)
   }

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

(On the statement print "42 monkeys" + "1 snake":)  By the way,
both perl and Python get this wrong.  Perl gives 43 and Python
gives "42 monkeys1 snake", when the answer is clearly "41 monkeys
and 1 fat snake".        -- Jim Fulton


More information about the freebsd-amd64 mailing list