You are viewing a single comment's thread from:

RE: C Program to Run External Command using System (Synchronous)

in #programming4 years ago

Do not forget to mention that system 1) fork the shell (/bin/sh on nix systems) and 2) it can be a security risk and shouldn't be used lightheartly...

An interesting fact about sprintf is that it returns the number of bytes actually written. Hence the concatenation loop could look like:

      for (int i = 1; i < argc; ++i) {
                s += sprintf(buf + s, "%s ", argv[i]);
        }

An extra space will be written at the end of the buffer, and there's no check for buffer overflow (another security risk), as in your version.