Executing a shell command could be done by calling the system function which executes a shell command.
This is obviously not safe as someone can abuse the program and execute attacker-specific os commands.
The the man page of system suggests using the exec(3) function:
Do not use system() from a privileged program (a set-user-ID or set-group-ID program, or a program with capabilities) because strange values for some environment variables might be used to subvert system integrity. For example, PATH could be manipulated so that an arbitrary program is executed with privilege. Use the exec(3) family of functions instead, but not execlp(3) or execvp(3) (which also use the PATH environment variable to search for an executable).
Although I don't really understand the difference. Yes, the privileges might work differently, but we still executing os commands and it is still vulnerable, isn't it ? am I missing something ?
How can one execute a system command safely without worrying it will be abused ?