Skip to content
Advertisement

Changing working directories in Linux shell in a C program

My goal is to write a C program that is like a basic shell for Linux. I have everything working except changing working directories. I have tried the system() for input strings for cd and nothing happened. I also tried chdir("tokened string") and also no luck. Anyone have any ideas? This is part of my code:

JavaScript

Is it possible to do this? Or is this a simple case of something to do with the child shell not being able to do certain things?

Edit: Code above is complete.

Advertisement

Answer

fgets() leaves the trailing 'n' character in cmdstr.

If you type cd foo, you’ll call chdir("foon") rather than chdir("foo").

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement