{"id":33308,"date":"2023-02-06T10:19:42","date_gmt":"2023-02-06T04:49:42","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/"},"modified":"2023-02-06T10:19:42","modified_gmt":"2023-02-06T04:49:42","slug":"solved-create-linux-environment-in-c-using-execl-and-fork-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/","title":{"rendered":"[Solved] Create linux environment in C using execl and fork() [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-15354645\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"15354645\" data-parentid=\"15354588\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The line:<\/p>\n<pre><code>a[i] = strtok(s,\" \");\n<\/code><\/pre>\n<p>sets <code>a[i]<\/code> to a pointer within the string, which you&#8217;re modifying and which may eventually have the temporary NUL characters (from <code>strtok<\/code>) removed.<\/p>\n<p>You may want to try:<\/p>\n<pre><code>a[i] = strdup (strtok (s,\" \"));\n<\/code><\/pre>\n<p>instead.<\/p>\n<hr>\n<p>Let me explain further. Say we have the command (as it exists in memory):<\/p>\n<pre><code>ls -al xyzzy\\0\n<\/code><\/pre>\n<p>When you <code>strtok<\/code> it, it almost certainly gets changed to:<\/p>\n<pre><code>ls\\0-al xyzzy\\0\n^\n<\/code><\/pre>\n<p>and then it gives you <code>s<\/code> (indicated by <code>^<\/code>) returned from <code>strtok<\/code> and placed into <code>a[0]<\/code>. The next time through, it changes the string to:<\/p>\n<pre><code>ls -al\\0xyzzy\\0\n   ^\n<\/code><\/pre>\n<p>and gives you the address indicated by <code>^<\/code> and places that into <code>a[1]<\/code>. Unfortunately, <code>a[0]<\/code> now points to the string <code>\"ls -al\"<\/code><\/p>\n<p>Eventually, the string will return to it&#8217;s original state and your pointers, while pointing to the right <em>addresses,<\/em> won&#8217;t have strings null-terminated as you expect (except for the last one).<\/p>\n<p>So you&#8217;ll end up with:<\/p>\n<pre><code>a[0] = \"ls -al xyzzy\"\na[1] = \"-al xyzzy\"\na[2] = \"xyzzy\"\n<\/code><\/pre>\n<p>By using <code>strdup<\/code>, you make a <em>copy<\/em> of each string (at the time when the string is as you expect, a single word), which isn&#8217;t then modified by later operations in the string tokenising code.<\/p>\n<p>Remember, however, to free all those strings you&#8217;ve allocated when you&#8217;re done with them.<\/p>\n<p>You can use this code as a baseline:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main (void) {\n    char str[256], *word[20];\n    int i, num, len;\n\n    printf (\"Enter command: \");\n    fgets (str, sizeof (str), stdin);\n\n    num = 0;\n    word[num] = strtok (str, \" \");\n    while (word[num] != NULL) {\n        word[num] = strdup (word[num]);\n        len = strlen (word[num]);\n        if (strlen (word[num]) &gt; 0)\n            if (word[num][len-1] == '\\n')\n                word[num][len-1] = '\\0';\n        word[++num] = strtok (NULL, \" \");\n    }\n\n    for (i = 0; i &lt; num; i++) {\n        printf (\"%d: [%s]\\n\", i, (word[i] == NULL) ? \"&lt;&lt;null&gt;&gt;\" : word[i]);\n        free (word[i]);\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<p>and the following transcript shows it in action:<\/p>\n<pre><code>Enter command: ls -al xyzzy | grep -v plugh\n0: [ls]\n1: [-al]\n2: [xyzzy]\n3: [|]\n4: [grep]\n5: [-v]\n6: [plugh]\n<\/code><\/pre>\n<hr>\n<p>Additionally, the correct way to compare C strings is not:<\/p>\n<pre><code>if (a[0] = \"cd\")\n<\/code><\/pre>\n<p>since that compares <em>addresses,<\/em> which may be different even if the backing content is identical. You should instead use something like:<\/p>\n<pre><code>if (strcmp (a[0], \"cd\") == 0)\n<\/code><\/pre>\n<hr>\n<p>And one final thing, the <code>argv[]<\/code> array that you pass to <code>execvp<\/code> must have a final terminating NULL pointer. That&#8217;s part of the contract, so you&#8217;ll need to ensure that gets put in before calling <code>execvp<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Create linux environment in C using execl and fork() [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The line: a[i] = strtok(s,&#8221; &#8220;); sets a[i] to a pointer within the string, which you&#8217;re modifying and which may eventually have the temporary NUL characters (from strtok) removed. You may want to try: a[i] = strdup (strtok (s,&#8221; &#8220;)); instead. Let me explain further. Say we have the command (as it exists in &#8230; <a title=\"[Solved] Create linux environment in C using execl and fork() [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/\" aria-label=\"More on [Solved] Create linux environment in C using execl and fork() [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,446,1145,424,1043],"class_list":["post-33308","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-command","tag-exec","tag-linux","tag-shell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The line: a[i] = strtok(s,&quot; &quot;); sets a[i] to a pointer within the string, which you&#8217;re modifying and which may eventually have the temporary NUL characters (from strtok) removed. You may want to try: a[i] = strdup (strtok (s,&quot; &quot;)); instead. Let me explain further. Say we have the command (as it exists in ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-06T04:49:42+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Create linux environment in C using execl and fork() [closed]\",\"datePublished\":\"2023-02-06T04:49:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/\"},\"wordCount\":286,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"command\",\"exec\",\"linux\",\"shell\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/\",\"name\":\"[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-02-06T04:49:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Create linux environment in C using execl and fork() [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb","og_description":"[ad_1] The line: a[i] = strtok(s,\" \"); sets a[i] to a pointer within the string, which you&#8217;re modifying and which may eventually have the temporary NUL characters (from strtok) removed. You may want to try: a[i] = strdup (strtok (s,\" \")); instead. Let me explain further. Say we have the command (as it exists in ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/","og_site_name":"JassWeb","article_published_time":"2023-02-06T04:49:42+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Create linux environment in C using execl and fork() [closed]","datePublished":"2023-02-06T04:49:42+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/"},"wordCount":286,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","command","exec","linux","shell"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/","name":"[Solved] Create linux environment in C using execl and fork() [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-06T04:49:42+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-create-linux-environment-in-c-using-execl-and-fork-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Create linux environment in C using execl and fork() [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33308"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33308\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}