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

时间:2015-05-10 03:24:37

标签: c

我为一个简单的shell编写了一个小程序,作为学习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)

你能帮我解决一下吗?代码是:

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

程序应该像一个小shell,它能够接受任意命令并执行它们(不使用系统调用)。你能救我吗?

1 个答案:

答案 0 :(得分:1)

问题是由i的值在顶级0循环开始时未重置为while引起的。

添加行

  i = 0;

之前

  printf("miniShell>> ");                    

一切都应该好。