简体   繁体   English

为什么我的C程序第二次崩溃?

[英]Why is my C program crashing the second time?

I've written a small program for a simple shell as an axercise to learn C. Unfortunaltely, the program crashes the second time it is called. 我已经写了一个小程序,用简单的外壳作为练习C的焦虑症。不幸的是,该程序在第二次调用时崩溃。

$ ./a.out 
miniShell>> ls
ls
a.out       digenv.c~      miniShell.c~       test
digenv      digenv.c.orig  miniShell.c.orig   testshell.c
digenv2.c   digenv.old.c   README.md          testshell.c~
digenv2.c~  LICENSE    smallshell.c       testshell.c.orig
digenv.c    miniShell.c    smallshell.c.orig
Child exited
miniShell>> ls
ls
Segmentation fault (core dumped)

Can you help me troubleshoot it? 您能帮我解决问题吗? The code is: 代码是:

#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int mystrcmp(char const *, char const *);


void err_syserr(char *fmt, ...)
{
    int errnum = errno;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    if (errnum != 0)
        fprintf(stderr, "(%d: %s)\n", errnum, strerror(errnum));
    exit(EXIT_FAILURE);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_LEN 1024
#define BUFFERSIZE 1024
int main() {
    char line[BUFFER_LEN];  
    char* argv[100];        
    char* path= "/bin/";    
    char progpath[20];      
    int argc;               
    size_t length;
    char *token;
    int i=0;
    int pid;
    while(1) {

        printf("miniShell>> ");                    

        if(!fgets(line, BUFFER_LEN, stdin)) { 
            break;                                
        }
        length = strlen(line);
        if (line[length - 1] == '\n') {
            line[length - 1] = '\0';
        }
        if(strcmp(line, "exit")==0) {           
            break;
        }


        token = strtok(line," ");

        while(token!=NULL) {
            argv[i]=token;
            token = strtok(NULL," ");
            i++;
        }
        argv[i]=NULL;                     

        argc=i;                           
        for(i=0; i<argc; i++) {
            printf("%s\n", argv[i]);      
        }
        strcpy(progpath, path);           
        strcat(progpath, argv[0]);            

        for(i=0; i<strlen(progpath); i++) {   
            if(progpath[i]=='\n') {
                progpath[i]='\0';
            }
        }
        pid= fork();              

        if(pid==0) {              
            execvp(progpath,argv);
            fprintf(stderr, "Child process could not do execvp\n");

        } else {                  
            wait(NULL);
            printf("Child exited\n");
        }

    }
return (0);
}

int mystrcmp(char const *p, char const *q)
{
    int i = 0;
    for(i = 0; q[i]; i++)
    {
        if(p[i] != q[i])
            return -1;
    }
    return 0;
}

int cd(char *pth) {
    char path[BUFFERSIZE];
    char cwd[BUFFERSIZE];
    char * return_value;
    int other_return;
    strcpy(path,pth);

    if(pth[0] != '/')
    {  
        return_value = getcwd(cwd,sizeof(cwd));
        strcat(cwd,"/");
        strcat(cwd,path);
        other_return = chdir(cwd);
    } else { 
        other_return = chdir(pth);
    }
    printf("Spawned foreground process: %d\n", getpid());
    return 0;
}

The program is supposed to behave like a small shell that is able to take arbitrary commands and execute them (without using the system call). 该程序应该表现得像一个小型外壳,可以接受任意命令并执行它们(无需使用系统调用)。 Can you help me? 你能帮助我吗?

The problem is caused by the value of i not being reset to 0 at the start of the top level while loop. 问题是由于在顶级while循环开始时i的值未重置为0引起的。

Add the line 添加行

  i = 0;

just before 就在之前

  printf("miniShell>> ");                    

and all should be well. 一切都会好起来的。

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

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