Search code examples
cterminalminix

Weird directory created after executing mkdir after fork in Minix (USER=root)


I'm busy coding a simple terminal in C on Minix 3.1. Simple commands such as touch a.txtor date is working perfectly. But when I make a directory such as mkdir test it creates a directory called USER=root and when I try create another one it says USER=root: already exists. I am logged in as the root. I am using execvp() in C to execute the terminal commands.

ls gives me this:

(null): HOME=/root: No such file or directory
(null): PAGER=more: No such file or directory
(null): LOGNAME=root: No such file or directory
(null): TERM=minix: No such file or directory
(null): PATH=/root/bin:/usr/local/bin:/vin:/usr/bin: No such file or directory
(null): SHELL=/bin/sh: No such file or directory
(null): TZ=GMT0: No such file or directory
(null): EDITOR=vi: No such file or directory
USER=root:

Does anyone know what this actually means and how I can fix this. I have tried everything I could think of in my code to try fix this problem but nothing has worked.

Thanks for any helpFirst part of my code

Second part of my code Last part of my code

Im calling executeCommand from another function which just returns the status to loopShell


Solution

  • You're creating the the argument list to execvp incorrectly. You're returning an array allocated on the stack (an array with automatic storage duration). Once the the function that created it returns the array is unallocated and the pointer to it is no longer valid. As simple quick fix would be to make the array static so that it remains allocated after the function returns. Eg:

    static char *tokens[SPLIT_SIZE];
    

    Another mistake in your program is trying to use free on objects not allocated with malloc, calloc or realloc.