使用 chdir 更改 c 中的工作目录

发布于 2024-12-11 16:04:09 字数 3090 浏览 0 评论 0原文

我对 C 还很陌生,似乎碰壁了。每当有人正确输入 cd 目录时,我是否会传递此参数来更改工作目录?如果我不包含目录,它默认报告当前工作目录,但在给定参数时我似乎无法更改目录。

#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; 
}

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2024-12-18 16:04:09

我刚刚编译并运行了你的程序,它工作了。

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

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
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文