Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does system() exist?

Tags:

c

Many papers and such mention that calls to 'system()' are unsafe and unportable. I do not dispute their arguments.

I have noticed, though, that many Unix utilities have a C library equivalent. If not, the source is available for a wide variety of these tools.

While many papers and such recommend against goto, there are those who can make an argument for its use, and there are simple reasons why it's in C at all.

So, why do we need system()? How much existing code relies on it that can't easily be changed?

like image 401
Orion Avatar asked Jan 28 '23 14:01

Orion


2 Answers

sarcastic answer Because if it didn't exist people would ask why that functionality didn't exist...

better answer
Many of the system functionality is not part of the 'C' standard but are part of say the Linux spec and Windows most likely has some equivalent. So if you're writing an app that will only be used on Linux environments then using these functions is not an issue, and as such is actually useful. If you're writing an application that can run on both Linux and Windows (or others) these calls become problematic because they may not be portable between system. The key (imo) is that you are simply aware of the issues/concerns and program accordingly (e.g. use appropriate #ifdef's to protect the code etc...)

like image 86
Jim M. Avatar answered Jan 30 '23 02:01

Jim M.


The closest thing to an official "why" answer you're likely to find is the C89 Rationale. 4.10.4.5 The system function reads:

The system function allows a program to suspend its execution temporarily in order to run another program to completion.

Information may be passed to the called program in three ways: through command-line argument strings, through the environment, and (most portably) through data files. Before calling the system function, the calling program should close all such data files.

Information may be returned from the called program in two ways: through the implementation-defined return value (in many implementations, the termination status code which is the argument to the exit function is returned by the implementation to the caller as the value returned by the system function), and (most portably) through data files.

If the environment is interactive, information may also be exchanged with users of interactive devices.

Some implementations offer built-in programs called "commands" (for example, date) which may provide useful information to an application program via the system function. The Standard does not attempt to characterize such commands, and their use is not portable.

On the other hand, the use of the system function is portable, provided the implementation supports the capability. The Standard permits the application to ascertain this by calling the system function with a null pointer argument. Whether more levels of nesting are supported can also be ascertained this way; assuming more than one such level is obviously dangerous.

Aside from that, I would say mainly for historical reasons. In the early days of Unix and C, system was a convenient library function that fulfilled a need that several interactive programs needed: as mentioned above, "suspend[ing] its execution temporarily in order to run another program". It's not well-designed or suitable for any serious tasks (the POSIX requirements for it make it fundamentally non-thread-safe, it doesn't admit asynchronous events to be handled by the calling program while the other program is running, etc.) and its use is error-prone (safe construction of command string is difficult) and non-portable (because the particular form of command strings is implementation-defined, though POSIX defines this for POSIX-conforming implementations).

If C were being designed today, it almost certainly would not include system, and would either leave this type of functionality entirely to the implementation and its library extensions, or would specify something more akin to posix_spawn and related interfaces.

like image 33
R.. GitHub STOP HELPING ICE Avatar answered Jan 30 '23 04:01

R.. GitHub STOP HELPING ICE