Using chdir to change working directory in c

Ryan picture Ryan · Oct 24, 2011 · Viewed 10.2k times · Source

I'm fairly new to C,and seem to have hit a wall. Am I passing this argument to change working directory whenever someone types in cd directory correctly? If I don't include a directory, it defaults to reporting the present working directory, but I can't seem to get the directory to change when given an argument.

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

#define MAX_BUFFER 1024                        // max line buffer
#define MAX_ARGS 64                            // max # args
#define SEPARATORS " \t\n"                     // token sparators

extern char **environ;
/*******************************************************************/

int main (int argc, char ** argv)
{
char linebuf[MAX_BUFFER];                  // line buffer
char cmndbuf[MAX_BUFFER];                  // command buffer
char * args[MAX_ARGS];                     // pointers to arg strings
char ** arg;                               // working pointer thru args
char * prompt = "==>" ;                    // shell prompt

// keep reading input until "quit" command or eof of redirected input 

while (!feof(stdin)) { 

// get command line from input


    fputs (prompt, stdout);                // write prompt
    fflush(stdout);
    if (fgets(linebuf, MAX_BUFFER, stdin )) { // read a line

// tokenize the input into args array

        arg = args;
        *arg++ = strtok(linebuf,SEPARATORS);   // tokenize input
        while ((*arg++ = strtok(NULL,SEPARATORS)));
                                           // last entry will be NULL 

        if (args[0]) {                     // if there's anything there

            cmndbuf[0] = 0;                // set zero-length command string

// check for internal/external command 

            if (!strcmp(args[0],"clr")) {  // "clr" command
                strcpy(cmndbuf, "clear");
            } else
            if (!strcmp(args[0],"cd"))
            {
                int ret;
                if (!args[1])
                    strcpy(cmndbuf, "pwd");
                ret = chdir(args[1]);
                strcpy(cmndbuf, "pwd");


            }else
            if (!strcmp(args[0],"dir")) {  // "dir" command
                strcpy(cmndbuf, "ls -al ");
                if (!args[1])
                    args[1] = ".";         // if no arg set current directory
                strcat(cmndbuf, args[1]);
            } else
            if (!strcmp(args[0],"environ")) { // "environ" command
                char ** envstr = environ;
                while (*envstr) {            // print out environment
                    printf("%s\n",*envstr);
                    envstr++;
                }                            // (no entry in cmndbuf)
            } else
            if (!strcmp(args[0],"quit")) {   // "quit" command
                break;
            } else {                         // pass command on to OS shell
                int i = 1;
                strcpy(cmndbuf, args[0]);
                while (args[i]) {
                    strcat(cmndbuf, " ");
                    strcat(cmndbuf, args[i++]);
                }
            }

// pass any command onto OS

            if (cmndbuf[0])
                system(cmndbuf);
        }
    }
}
return 0; 
}

Thanks.

Answer

Keith Thompson picture Keith Thompson · Oct 24, 2011

I just compiled and ran your program, and it worked.

$ gcc c.c -o c
$ ./c
==>cd
/home/kst
==>cd /tmp
/tmp
==>cd
/tmp
==>cd /        
/
==>quit
$