2

I am creating a C++ console application and want to check whether the command processor is available:

if (!system(NULL))  //check to see if command processor is available
{
    fprintf(stderr, "Error: Command processor not available\n");
    exit(EXIT_FAILURE);
}

My question is, can a console app exist or work without having a command processor attached to it? If when a console is created, it is always paired somehow with a command processor, then the code above would be redundant. Otherwise, the console window will appear, the program will run normally, but I just can't use system().

  • 6
    This appears to be an **XY Problem**. See: [**What is the XY problem?**](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) What is it you are trying to do to begin with. You may not need `system` at all. C++ generally provides everything you need. – David C. Rankin Dec 19 '18 at 09:25
  • 1
    @DavidC.Rankin It is true, the way I phrased the question makes it seem that it's an XY problem. I am using system("cls") and have found other alternatives, but I want to understand better how console apps work. – Konstantinos P. Dec 19 '18 at 09:30
  • 1
    Well, `"cls"` is more of a cludge than a tool. Yes it works, but if you are wanting to do text console manipulation, you really need a library like `ncurses`, etc.. I avoid the use of `system` like the plague. (it has is convenience uses -- but those should be view as "few and far between") – David C. Rankin Dec 19 '18 at 09:33
  • Thanks for the answer. I just edited the question so it's more focused. My question is on the command processor rather than whether `system` should be used or not. – Konstantinos P. Dec 19 '18 at 09:47
  • 1
    what OS? cls is a Windows-only command, linux misses it (some distrs have macro which emulates the behaviour). Thing is windows cannot missing command processor that way, "system" there is a compatibility wrapper around CreateProcess to call command processor." – Swift - Friday Pie Dec 19 '18 at 09:48
  • Windows. So like, can a console app work without having a command processor attached to it? – Konstantinos P. Dec 19 '18 at 09:53
  • 1
    Console application may work without opening a visible command console. You have to have command processor to execute a console application. The `system` call executes a "shell command" on the running operation system and is completely os dependent. Are you asking about C? About why windows `system` call opens new console window? If windows needs console window to run a "console" applications? What is a console application? Command processor is used to process a command, console window is used to display the result. – KamilCuk Dec 19 '18 at 10:20
  • 2
    The command processor is merely another program that also happens to use a console. Cmd.exe on Windows. Otherwise indistinguishable from any program that was built to avoid GUI features. That the system() function has special knowledge of it is wonkiness that goes back to early Unix. Checking if it is "available" is pretty pointless, it is always available and if not then your user would have a hard time getting your program started in the first place and have decided that reinstalling the OS is warranted. – Hans Passant Dec 19 '18 at 10:32
  • A command processor is not attached to anything. It is either available to your program or not. "Console" is not something the C++ standard mentions so we cannot talk about it in standard terms. If talking in OS-apecific terms, then yes, most operating systems that have consoles do let you set up things in a way that makes the command processor unavailable to your console application. – n. m. could be an AI Dec 19 '18 at 10:32
  • @DavidC.Rankin It's a reasonable question asking a specific thing about a specific feature – Lightness Races in Orbit Dec 19 '18 at 10:43

1 Answers1

0

If you have a console application, then you're going to be running your program from inside a command processor. Therefore, a command processor is available in the context of your program.

It is probably possible with some trickery to "hide" said processor from your process, but I don't know how to do so on any OS.

More generally, though, since C++ is an abstract "multi-purpose" language, in theory it is conceivable that for some given program P in some environment E a command processor is not available, but in practice I can't think of an example. Perhaps when, say, writing a kernel module?

Overall I would probably not bother with this check unless you already had reason to believe that there may be something "unusual" about the execution environment — you need to handle errors generated by your "real" system calls anyway.

Still, can't hurt.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055