Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>system() execute dos command</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 system()                Execute DOS Command

 #include   <process.h>                  Required for declarations only
 #include   <stdlib.h>                   Use either process.h or stdlib.h

 int        system(string);
 const char *string;                     Command to be executed

    system() passes 'string' to the command interpreter COMMAND.COM or
    CMD.EXE in OS/2 to be executed as if it were typed in at the DOS
    prompt.  If 'string' is NULL, system() looks to see if COMMAND.COM
    (or CMD.EXE in OS/2) is present.

    Returns:    If 'string' is not NULL, 0 is returned if the command is
                successfully executed.  -1 is returned on error, and
                'errno' (defined in <stdlib.h>) is set to one of the
                following values (defined in <errno.h>):

                    E2BIG      The argument list for the command exceeds
                               128 bytes, or the space for the
                               environment information exceeds 32K.
                    ENOENT     COMMAND.COM or CMD.EXE cannot be found.
                    ENOEXEC    COMMAND.COM or CMD.EXE is not an
                               executable file.
                    ENOMEM     Not enough memory, corrupted memory, or
                               invalid block exists, indicating that
                               the process making the call was not
                               allocated properly.

                If 'string' is NULL, and COMMAND.COM or CMD.EXE  is
                found, a nonzero value is returned.  If COMMAND.COM or
                CMD.EXE is not found, 'errno' is set to:

                    ENOENT    COMMAND.COM or CMD.EXE cannot be found.

      Notes:    system() locates COMMAND.COM or CMD.EXE by using the
                COMSPEC and PATH environment variables.

 Compatibility: MSC version 4.0 does not allow a NULL value for 'string'.

   -------------------------------- Example ---------------------------------

    The following statements send a directory listing to the file
    "dir.txt":

           #include <process.h>     /* for 'system' */
           #include <stdio.h>       /* for 'printf' */
           #include <stdlib.h>      /* for 'errno' */
           #include <errno.h>       /* for 'ENOENT' and 'ENOMEM' */

           main()
           {
               int result;

               result = system("dir >> dir.txt");
               if (result == -1)
                   if (errno == ENOENT)
                       printf("can't find COMMAND.COM\n");
                   else if (errno == ENOMEM)
                       printf("not enough memory\n");
                   else
                       printf("error #%d executing system
                               command\n",errno);
           }


See Also: exec...() spawn...()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson