NO

Author Topic: Line length for system() function...  (Read 9965 times)

CommonTater

  • Guest
Line length for system() function...
« on: October 10, 2012, 11:27:45 PM »
Does anyone know what the maximum line length is for the system() and wsystem() functions?
 
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Line length for system() function...
« Reply #1 on: October 11, 2012, 05:15:47 AM »
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article...

Ralf

CommonTater

  • Guest
Re: Line length for system() function...
« Reply #2 on: October 11, 2012, 05:25:10 AM »
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article...
Ralf

Hi Ralf ... I would assume the same, but I thought I should make sure before I write a ton of complicated software just to have it crash and burn on the one function.  The help file doesn't say anything about line length...

I know from today's tests that it's at least 256 characters (MAX_PATH) ...
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Line length for system() function...
« Reply #3 on: October 11, 2012, 09:12:59 AM »
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article...
Ralf

Hi Ralf ... I would assume the same, but I thought I should make sure before I write a ton of complicated software just to have it crash and burn on the one function.  The help file doesn't say anything about line length...

I know from today's tests that it's at least 256 characters (MAX_PATH) ...
 
I don't think I ever had in the 36 years that I am now programming a need for such (as in maximum of command line length) a large command line string, on any OS... ;-)

Well, I am certain that I have used a few years back (on Windows XP) strings in excess of 256 bytes though.

MAX_PATH doesn't really apply here, as the system() command is feeding the command interpreter (cmd.exe in case of Windows XP and newer) and just a simply copy instructions could have in excess of 2xMAX_PATH characters (source and target file/pathname)...  ;)

Ralf

CommonTater

  • Guest
Re: Line length for system() function...
« Reply #4 on: October 11, 2012, 01:45:18 PM »
I don't think I ever had in the 36 years that I am now programming a need for such (as in maximum of command line length) a large command line string, on any OS... ;-)

I've had a couple of pretty long ones over the years.  As you point out a simple copy command can hit MAX_PATH * 2. 

What I was wondering was if there was an artificial limit imposed by C in general or Pelles C in particular.   I guess I will have to kludge up a bit of code to torture it and see when it breaks...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Line length for system() function...
« Reply #5 on: October 11, 2012, 02:54:08 PM »
What cmd.exe tells to you ?
Code: [Select]
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
#define MAXLEN 8191
char cmd[MAXLEN+1];
strcpy(cmd, "echo ");
memset(&cmd[5], 'x', MAXLEN);
cmd[MAXLEN] = 0;
system(cmd);
return 0;
}
In Win7 8160 was the limit.
May the source be with you

CommonTater

  • Guest
Re: Line length for system() function...
« Reply #6 on: October 11, 2012, 06:55:08 PM »
What cmd.exe tells to you ?
Code: [Select]
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
#define MAXLEN 8191
   char cmd[MAXLEN+1];
   strcpy(cmd, "echo ");
   memset(&cmd[5], 'x', MAXLEN);
   cmd[MAXLEN] = 0;
   system(cmd);
   return 0;
}
In Win7 8160 was the limit.

Thanks Timo ... I just tried a similar test, using ever growing strings... and got 8158 before the shell errored off.

I'm guessing the result is the limit of the CMD.EXE command line, including the executable name...

c:\windows\system32\cmd.exe /c  == 31 characters

8191 - 31 = 8160

I wasn't expecting that...
« Last Edit: October 11, 2012, 07:16:31 PM by CommonTater »

CommonTater

  • Guest
Re: Line length for system() function...
« Reply #7 on: October 11, 2012, 07:22:59 PM »
Further testing ...
This expands it to 32k when running executables but is still limited to 8k when running CMD.EXE /c
 
Code: [Select]

WCHAR gWorkDir[MAX_PATH + 1];
 
// execute a program and commandline then return the errorlevel
DWORD Shell(PWCHAR Program, PWCHAR CommandLine)
  {
    PROCESS_INFORMATION pi = {0};       // handles from create process
    STARTUPINFO         si = {0};       // for create process
    DWORD               err;
    WCHAR cmd[MAX_PATH];   
   
    // precondition startup info
    si.cb = sizeof(si);
 
    // find the executable
    if (! SearchPath(NULL,Program,NULL,MAX_PATH,cmd,NULL))
      return -1;
 
    // run the command
    if (! CreateProcess(cmd, CommandLine, NULL, NULL, 0, CREATE_UNICODE_ENVIRONMENT,
                        NULL, gWorkDir, &si, &pi))
      return -1;
 
    // wait for completion
    WaitForSingleObject(pi.hthread, INFINITE);     
    GetExitCodeThread(pi.hThread, &err);
 
    // cleanup
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
 
    return err;
  }
 


(EDIT :  included a more real world version of the function
              added WaitForSingleObject() )
« Last Edit: November 09, 2012, 04:05:23 AM by CommonTater »

CommonTater

  • Guest
Re: Line length for system() function...
« Reply #8 on: November 08, 2012, 10:19:20 PM »
A quick update... working more with this, the limits of the function in my previous message are 8160 characters when using "cmd /c ...." and ~32700 when running an executable.