简体   繁体   English

字符数组和指针如何使用strtok和strcmp

[英]Character arrays, and pointers how to use strtok and strcmp

I'm writing a linux shell for my operating systems class. 我正在为我的操作系统类编写一个Linux shell。 I've knocked out the majority of it but I'm stuck on simple string comparisons. 我已经淘汰了大部分,但我坚持进行简单的字符串比较。 I've everything I can think of. 我有我能想到的一切。 strcmp should take in \\0 terminated strings and return 0 for equal but that doesn't seem to be working and even stepping through the array and checking each char isn't working either. strcmp应该接受\\ 0终止的字符串,并返回0等于等号,但这似乎不起作用,甚至单步执行数组并检查每个字符是否也不起作用。 I currently have cmd[0] in the strcmp I know thats not right it needs to be null terminated but I've tried using strcpy and strcat \\0 to another string. 我目前在strcmp中有cmd [0],我知道这是不正确的,它需要以null终止,但是我尝试将strcpy和strcat \\ 0用于另一个字符串。 If someone could point out my mistake it would be much appreciated. 如果有人指出我的错误,将不胜感激。

//Matthew Spiers
//CSC306

#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

void ckCmd(char dir[]);
int main(){

    pid_t pid;
    char cdstr[4] = "cd";
    char str[50];
    char *cmd[3];
    char *pstr;
    char temp[50];
    char dir[50] = "/bin/";
    while(1){
        pid = fork();
        if(pid < 0){
            fprintf(stdout, "Fork Failed");
        }
        else if(pid == 0){
            fprintf(stdout, "\e[36m306.SH>\e[0m");
            fgets(str, 50, stdin);  
            for(int i =0; i<50; i++){
                if(str[i] == '\n'){
                    str[i] = '\0';
                }
            }
            strcpy(temp, str); // Make a copy of original string
            cmd[0] = strtok(str, " ");
            for(int i =1; i<3; i++){
                cmd[i] = strtok(NULL, " ");
            }
            strcat(dir, cmd[0]);

            cout << cmd[0];

                    pstr = strtok(temp, " ");  //pull out only first token
            //Change Directory
            if(!strcmp(pstr, "cd")){ //if first token is cd
                //either branch to a routine just change directory
                //ie branch and change directory
            }
            //ckCmd(temp);

            execlp(dir, cmd[0], cmd[1], cmd[2], NULL);
            _exit(0);
        }
        else{
            wait(NULL);
        }
    }
}

void ckCmd(char str[]){
    char *p;
    p = strtok(str, " ");
    if(p[0] == 'c'){
        chdir("new");
    }
}

    enter code here

strtok is not reentrant/thread-safe! strtok不是可重入的/线程安全的! You should use the RETURN-value from strtok: 您应该使用strtok的RETURN值:

p = strtok(str, " ");
    if(p[0] == 'c'){

cmd[0] = strtok(str, " ");
...
if(!strcmp(cmd[0], "cd")){

If p/cmd[0] is NULL, it will crash. 如果p / cmd [0]为NULL,它将崩溃。

What exactly isn't working? 到底什么不起作用? Can you show a smaller code sample? 您可以显示一个较小的代码示例吗?

The line: 该行:

strcat(dir, cmd[0]);

Are you aware that dir is the target here and not cmd[0] ? 您是否知道dir是这里的目标而不是cmd[0]

The line: !strcmp(cmd[0], "cd") by itself is correct, but it's unclear what exactly you're trying to do. 该行: !strcmp(cmd[0], "cd")本身是正确的,但尚不清楚您到底要做什么。 Could you comment each group of line with your intentions? 您能按照自己的意图评论每一行吗?


Update: I suggest that you're trying too many things at once. 更新:我建议您一次尝试太多事情。 To home in on your problem I recommend the following steps: 为了解决您的问题,我建议您执行以下步骤:

  1. Understand how strtok works. 了解strtok工作原理。 It isn't very hard - read its manual and then try it in a separate file with some strings. 这不是很难-阅读其手册,然后在带有一些字符串的单独文件中尝试。 This should give you a good idea. 这应该给您一个好主意。
  2. Break parts of your code out and provide them with pre-set input (not from the user and without the forking). 将您的代码部分分解出来,并提供给他们预设的输入(不是来自用户的,也不是来自分叉的)。 See where the behavior is as expected and where it drifts off. 查看行为在预期中的位置以及漂移的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM