Chapter Contents

Previous

Next
system

system



Execute a System Command

Portability: ISO/ANSI C conforming, UNIX compatible, POSIX.1 conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
DIAGNOSTICS
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdlib.h>

int system(const char *cmd);


DESCRIPTION

The system function executes a character string as a command by passing it to an operating-system-defined command processor. The cmd argument is a pointer to the command string, which consists of two parts: a prefix that contains an optional command type followed by a colon and a command.

Note:    The case of the command name or its arguments or both may be significant, depending on the operating system and the command being executed.  [cautionend]


RETURN VALUE

The system function returns an integer status code indicating the success or failure of the command. The status code for a successful command is normally 0. If called with a NULL argument, system returns 1 to indicate that command processing is available.

In the 370 implementation, the status code is normally the return code (or completion code) of the invoked program or command, unless an error is detected by the system function itself.

Certain status codes returned by system have special significance. These codes are given symbolic names by the header file <lclib.h> . Here are the names of these codes and their meanings:
SYS_ATTN indicates the command was terminated by attention.
SYS_ABTM indicates the command was abnormally terminated.
SYS_CUNK indicates an unknown command.
SYS_CSYN indicates a command syntax error.
SYS_INF indicates a system interface failure.
SYS_TNAC indicates the specified command environment was not active.
SYS_TSYN indicates a command-type syntax error.
SYS_TUNK indicates an unknown command type.
SYS_CHILD indicates a problem creating an USS child process.

Some of these codes are meaningful only for one operating system, and the situations in which they are returned are dependent on the operating system.


CAUTION

If the TSO: prefix is used with the system function, unpredictable results may occur under MVS/XA when running in 31-bit addressing mode and TSO/E is not installed. (In this case, TSO commands cannot be invoked with the system function.)

Use system with the TSO: prefix in 31-bit mode programs only if TSO/E is installed.


DIAGNOSTICS

Errors in the command string can generate library messages, operating-system messages, or both.


PORTABILITY

The format of the command string and the meaning of the status code for system is completely system dependent, and it is unlikely that a program calling system can be moved to another system without modification.

system is a POSIX.2 function.


IMPLEMENTATION

OS/390

Under OS/390, system can use a prefix of PGM: , TSO: or SH: . If the prefix is omitted, SH: is assumed if the program was compiled with the posix option; otherwise, PGM: is assumed. For programs compiled with the posix option, the argument to system is always assumed to be a shell command, even if it appears to have an explicit prefix. To use the PGM: or TSO: prefix in a posix -compiled program, you must precede the prefix with "//" , for example, system("//PGM:IEFBR14") . The "//" prefix is recognized whether or not a program is compiled with the posix option to enable you to write subroutines that call system and can be used in both posix -compiled and non- posix -compiled programs. The effect of using the PGM: and TSO: prefixes follows:

It is recommended that a program that calls system using the TSO: prefix be executed as a TSO command. When a C program calls system to execute a TSO command, the library must locate TSO interface information, which is readily available to programs that have been called as commands. For programs that are not called as commands, this information must be extracted from unprotected system-control blocks. Because these control blocks are unprotected, it is possible for a malfunctioning TSO program (either the C program or one executed earlier) to overlay this information. Any attempt to execute a TSO command using the corrupted control blocks may result in a program ABEND, involuntary logoff, or incorrect results.

Under TSO, if an attention interrupt occurs during a call to system , the called program or command is immediately terminated.

When running with TSO/E Release 1.3 or higher under MVS/XA or MVS/ESA, system uses the TSO service routine IKJEFTSR to invoke TSO commands, CLISTs, or EXECs. This interface enables an unauthorized program to call commands that require authorization.

The IKJEFTSR interface is sensitive to TSO release and maintenance levels, and it can behave differently from release to release. You should be aware of these points:

USS

In addition to its use for invoking OS/390 load modules and TSO commands, you can use the system function to invoke shell commands if USS is installed and available.

Note:    If a program uses any USS features, such as HFS files or POSIX signals, you should not use the system function prefixes PGM: or TSO: to invoke another program that also uses USS. USS will treat both programs as comprising a single process, which can cause confusing behavior in file access, signal handling, and other areas. In general, use the system function with the SH: prefix or the oeattach function to invoke one USS application from another.  [cautionend]

If the system function is successful at calling a shell command, its return value is the exit status code of the shell, which can be interpreted by the <wait.h> macros such as WEXITSTATUS and WTERMSIG . If no child process can be created, system returns -1, which is given the symbolic name SYS_CHLD in <lclib.h> .

CMS

Under CMS, system can use a prefix of CMS: , CP: , SUBSET: , or XEDIT: . If the prefix is omitted, CMS: is assumed. The prefix can also be preceded by "//" for OS/390 compatibility. These paragraphs describe the effects of each of these prefixes:

If the command prefix is unknown (that is, it is not one of the prefixes listed here), it is treated as the name of a subcommand environment. A subcommand environment is a program that has been named with the CMS SUBCOM function. If the subcommand environment is active, the command is transferred (with the CMS command search function) to the subcommand environment. If the subcommand environment is not active, system returns SYS_TNAC . For more information about subcommand environments, refer to the appropriate IBM publication.


EXAMPLE

This example creates a new PDS named EXAMPLE.OUTPUT and writes member README. If the PDS already exists, it is not changed.

#include <stdlib.h>
#include <stdio.h>

main()
{
   int sysrc;
   FILE *readme;

   puts("Calling the TSO allocate command to create EXAMPLE.OUTPUT");
   sysrc = system("tso: allocate da(example.output) new sp(1 1) tr "
                  "dir(1)");       /* Allocate example.output NEW. */
   if (sysrc != 0){
         puts("Unable to allocate EXAMPLE.OUTPUT. File probably "
              "already exists.");
         exit(EXIT_FAILURE);
   }
   readme = fopen("tso:example.output(readme)", "w");
   fputs("This file was created by a SAS/C example program.\n",
          readme);
   fclose(readme);
   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

fork , oslink , popen


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.