Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with using execvp()
    primarykey
    data
    text
    <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;string.h&gt; #include &lt;errno.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/wait.h&gt; extern int errno; void parseArgs(char *path, char *argv[]) { while (*path != '\0') { while (*path == ' ' || *path == '\t' || *path == '\n') { *path++ = '\0'; } *argv++ = path; while (*path != '\0' &amp;&amp; *path != ' ' &amp;&amp; *path != '\t' &amp;&amp; *path != '\n') { path++; } } *argv = '\0'; } int execArgs(char *argv[], int amp) { pid_t process; int check; if ((process = fork()) &lt; 0) { fprintf(stderr, "Forking child failed\n%s\n", strerror(errno)); errno = 0; return EXIT_FAILURE; } else if (process == 0) { if (execvp(*argv, argv) &lt; 0) { fprintf(stderr, "Execution failed\n%s\n", strerror(errno)); errno = 0; return EXIT_FAILURE; } } else { while (wait(&amp;check) != process) { //do nothing } } return EXIT_SUCCESS; } int main (void) { char path[1024]; char *argv[64]; //int amp = 0; while (1) { printf("[root@localhost]$ "); fgets(path, 1024, stdin); puts(""); parseArgs(path, argv); if (strcmp(argv[0], "exit") == 0) { return EXIT_SUCCESS; } execArgs(argv, 0); } } </code></pre> <p>I'm having an issue here. The problem seems to be a parse error, because whenever I try to <code>ls</code> a directory, I get <code>ls: cannot access : No such file or directory</code>. The odd thing is, if I go <code>ls ..</code> I get:</p> <pre><code>ls: cannot access : No such file or directory ..: cat cat.c cat.c~ m4 </code></pre> <h3>NOTE: The above are the contents of the dir one level up!</h3> <p>Furthermore - and I suspect the issues are linked - I cannot <code>cd</code> into directories, however I'm not 100% sure of this as it does not throw anything like directory does not exist; I just get a blank space (but my cwd does not update).</p> <p>Any help would be appreciated!</p> <p>For Reference, some test inputs</p> <pre><code>[thanasi@localhost m4]$ ./cli [root@localhost /home/thanasi/Systems Programming/m4]$ ls ls: cannot access : No such file or directory [root@localhost /home/thanasi/Systems Programming/m4]$ ls .. ls: cannot access : No such file or directory ..: cat cat.c cat.c~ m4 [root@localhost /home/thanasi/Systems Programming/m4]$ cd test [root@localhost /home/thanasi/Systems Programming/m4]$ cd /test /usr/bin/cd: line 2: cd: /test: No such file or directory [root@localhost /home/thanasi/Systems Programming/m4]$ ^C </code></pre> <p>And confirmation dir 'test' does exist:</p> <pre><code>[thanasi@localhost m4]$ ls -al total 36 drwxrwxr-x. 3 thanasi thanasi 4096 Mar 17 22:00 . drwxrwxr-x. 3 thanasi thanasi 4096 Mar 17 20:36 .. -rwxrwxr-x. 1 thanasi thanasi 13175 Mar 17 22:00 cli -rw-rw-r--. 1 thanasi thanasi 1222 Mar 17 22:00 cli.c -rw-rw-r--. 1 thanasi thanasi 1221 Mar 17 22:00 cli.c~ drwxr-xr-x. 2 root root 4096 Mar 17 21:32 test </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload