C Program to Run External Command using System (Synchronous) by justyy

View this thread on steempeak.com
· @justyy ·
$27.68
C Program to Run External Command using System (Synchronous)
We can use the <em>system</em> function to run an external command. The method invokes the command <a href="https://helloacm.com/how-to-async-and-await-in-c11/" title="How to Async and Await in C++11?">synchronously</a> and return the status code. Given the following C program, we first concatenate all command line paramters into a string/char buffer of size 256. And then we invoke the command string using system function from stdlib header. And return the status code as the main function.

This is just a wrapper of the command. You can echo $? to see the status code:

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

int main(int argc, char* argv[]) {
    if (argc == 0) {
        return 0;
    }
    char cmd[255];
    int s = 0;
    for (int i = 1; i < argc; ++ i) {
        strcpy(cmd + s, argv[i]);        
        s += strlen(argv[i]);
        cmd[s ++] = ' ';
    }
    cmd[s] = '\0';
    printf("Running `%s` ...\n", cmd);
    int ret = system(cmd);
    printf("Return Code: %d\n", ret);
    return ret;
}
```

Compiling the above C source e.g. run.c
```
$ gcc run.c -o run
```

And then run:

```
$ ./run echo HelloACM.com
Running `echo HelloACM.com ` ...
HelloACM.com
Return Code: 0
$ echo $?
0
```

--EOF (<a title='The Ultimate Computing & Technology Blog' href='https://helloacm.com'>The Ultimate Computing & Technology Blog</a>) -- 


Reposted to [Blog](https://helloacm.com/c-program-to-run-external-command-using-system-synchronous/)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
---------------
## NEW! [Following my Trail (Upvote or/and Downvote)](https://steemit.com/witness-category/@justyy/following-my-trail-upvote-or-and-downvote-by-authorizing-posting-key-to-me)


Follow me for topics of *Algorithms, Blockchain and Cloud.*
I am @justyy - a Steem Witness
[https://steemyy.com](https://steemyy.com)

#### My contributions
- [Video Downloader](https://weibomiaopai.com/download-video-parser.php)
- [Steem Blockchain Tools](https://steemyy.com)
- [Free Cryptos API](https://steemit.com/witness-category/@justyy/serverless-api-to-query-the-cryptos-and-fiat)
- [VPS Database](https://anothervps.com/vps-database/)
- [Computing Technology Blog](https://helloacm.com)
- [A few useless tools](https://helloacm.com/tools/)
- [And some other online software/tools](https://steakovercooked.com/Software.Home)
- [Merge Files/Videos](https://slowapi.com/merge-videos/)
- [LOGO Turtle Programming Chrome Extension](https://chrome.google.com/webstore/detail/logo-turtle-graphics/dcoeaobaokbccdcnadncifmconllpihp)
- [Teaching Kids Programming - Youtube Channel](https://www.youtube.com/channel/UCK2ugmUOoYne1qQfSAph4nQ?view_as=subscriber) and [All Contents](https://github.com/DoctorLai/Teaching-Kids-Programming/blob/main/README.md)

#### Delegation Service
[Important Update of Delegation Service!](https://steemit.com/witness-category/@justyy/important-update-of-delegation-service)

- Delegate 1000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=1000)
- Delegate 5000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=5000)
- Delegate 10000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=10000)

#### Support me
If you like my work, please: 
- [Buy Me a Coffee](https://justyy.com/out/buymeacoffee), Thanks!
- [Become my Sponsor](https://github.com/sponsors/DoctorLai), Thanks!
- Voting for me:
https://steemit.com/~witnesses type in **justyy** and click ***VOTE***
https://steemyy.com/images/vote-for-justyy.jpg


1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
2. Vote @justyy as Witness:  https://steemyy.com/witness-voting/?witness=justyy&amp;action=approve
3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&amp;action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses


-------------
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 148 others
👎  , ,
properties (23)
post_id92,645,128
authorjustyy
permlinkc-program-to-run-external-command-using-system-synchronous
categoryprogramming
json_metadata{"tags":["programming","blog","sct","whalepower","upfundme","zzan","palnet","tutorial"],"users":["justyy"],"image":["https:\/\/steemyy.com\/images\/vote-for-justyy.jpg"],"links":["https:\/\/helloacm.com\/how-to-async-and-await-in-c11\/","https:\/\/helloacm.com","https:\/\/helloacm.com\/c-program-to-run-external-command-using-system-synchronous\/","https:\/\/steemit.com\/witness-category\/@justyy\/following-my-trail-upvote-or-and-downvote-by-authorizing-posting-key-to-me","https:\/\/steemyy.com","https:\/\/weibomiaopai.com\/download-video-parser.php","https:\/\/steemit.com\/witness-category\/@justyy\/serverless-api-to-query-the-cryptos-and-fiat","https:\/\/anothervps.com\/vps-database\/","https:\/\/helloacm.com\/tools\/","https:\/\/steakovercooked.com\/Software.Home","https:\/\/slowapi.com\/merge-videos\/","https:\/\/chrome.google.com\/webstore\/detail\/logo-turtle-graphics\/dcoeaobaokbccdcnadncifmconllpihp","https:\/\/www.youtube.com\/channel\/UCK2ugmUOoYne1qQfSAph4nQ?view_as=subscriber","https:\/\/github.com\/DoctorLai\/Teaching-Kids-Programming\/blob\/main\/README.md","https:\/\/steemit.com\/witness-category\/@justyy\/important-update-of-delegation-service","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=1000","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=5000","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=10000","https:\/\/justyy.com\/out\/buymeacoffee","https:\/\/github.com\/sponsors\/DoctorLai","https:\/\/steemit.com\/~witnesses","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&amp;action=approve","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&amp;action=proxy"],"app":"steemit\/0.2","format":"markdown"}
created2021-07-16 06:43:39
last_update2021-07-16 06:43:39
depth0
children2
net_rshares46,754,931,787,561
last_payout2021-07-23 06:43:39
cashout_time1969-12-31 23:59:59
total_payout_value13.898 SBD
curator_payout_value13.786 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,963
author_reputation2,052,211,997,492,585
root_title"C Program to Run External Command using System (Synchronous)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (215)
@xinta ·
$0.15
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.
```
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id92,680,540
authorxinta
permlinkqwew1o
categoryprogramming
json_metadata{"app":"steemit\/0.2"}
created2021-07-17 23:03:24
last_update2021-07-17 23:03:24
depth1
children1
net_rshares446,086,626,205
last_payout2021-07-24 23:03:24
cashout_time1969-12-31 23:59:59
total_payout_value0.078 SBD
curator_payout_value0.076 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length551
author_reputation2,154,434,690,031
root_title"C Program to Run External Command using System (Synchronous)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (42)
@justyy ·
Nice. thanks!
properties (22)
post_id92,692,617
authorjustyy
permlinkqwg1vd
categoryprogramming
json_metadata{"app":"steemit\/0.2"}
created2021-07-18 14:06:51
last_update2021-07-18 14:06:51
depth2
children0
net_rshares0
last_payout2021-07-25 14:06:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length13
author_reputation2,052,211,997,492,585
root_title"C Program to Run External Command using System (Synchronous)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000