简单 Shell 的问题 - 返回状态始终为 1

问题描述 投票:0回答:0

我目前正在用 C 编写一个简单的 shell 程序,但我面临着代码返回状态的问题。问题是 shell 应该在成功执行命令后返回 0,但它总是返回 1,即使命令正确运行也是如此。

我已经提供了以下代码的相关部分:

  • 主要功能 main 函数处理 shell 程序的整体流程。 它反复提示用户输入,使用 exe_cmd 执行命令,并使用内存函数释放分配的内存。 我无法在这里确定退货状态问题的具体原因。
#include "shell.h"
/**
 * main - entry point
 *
 * Return: 0  if succ
 *
 */
int main(void)
{
    char *input;
    char **args;
    int stat;

    do
    {
        if (isatty(STDIN_FILENO))
            write(1, "ous&zoh$ ", 9); /*prompt*/
        input = read_line();
        if (input == NULL)
        {
            exit(EXIT_FAILURE);
        }
        args = break_line(input);
        if (args == NULL)
        {
            perror("Error");
            free(input);
            exit(EXIT_FAILURE);
        }
        stat = exe_cmd(args);
        memory(input, args);
    }
    while (stat != -1); /*Continue until -1 : run until user type exit*/

    return (0);
}
  • read_line函数

此函数读取用户的输入。 它按预期工作,似乎不会导致退货状态问题。

#include "shell.h"

/**
 * read_line - read input from user
 *
 * Return: buffer
 */
char *read_line()
{
    char *buffer = NULL;
    size_t len = 0;
    ssize_t char_read;

    char_read = getline(&buffer, &len, stdin);

    if (char_read == -1)
    {
        if (isatty(STDIN_FILENO))
            perror("error");
        if (buffer != NULL)
            free(buffer);
        return (NULL);
    }

    return (buffer);
}
  • exe_cmd函数

此函数执行作为参数传递的命令。 我已经彻底检查了这个函数,它似乎正确执行了命令。但是,这可能是退货状态问题的根源。

#include "shell.h"

/**
 * exe_cmd - executes cmd
 *
 * @args: arg for cmd
 *
 * Return: 0 if scc or -1 if error
 */
int exe_cmd(char **args)
{
    pid_t pid;
    int stat;
    int exe;
    int exit_stat;

    if (args == NULL || args[0] == NULL)
        return (0);

    if (_strcmp(args[0], "exit") == 0)
        return (-1);
    pid = fork();
    if (pid == 0)
    {
        exe = execvp(args[0], args);

        if (exe == -1)
        {
            perror("error");
            exit(EXIT_FAILURE);
        }
    } else if (pid > 0)
    {
        wait(&stat);
        if (WIFEXITED(stat))
        {
            exit_stat = WEXITSTATUS(stat);
            return (exit_stat);
        }
        else
        {
            perror("error");
            return (-1);
        }
    }
    else
    {
        perror("eroor");
        return (-1);
    }
    return (0);
}
  • 断线函数

该函数接受一行输入并将其分解为一个参数数组。 我怀疑这个问题可能与这个函数有关,因为它使用 _realloc 动态分配内存,但我无法查明确切的问题。

#include "shell.h"

/**
 * break_line - break a line into array
 *
 * @line: line that will user give
 *
 * Return: args
 */
char **break_line(char *line)
{
    char **args = NULL;
    char *token;
    size_t size = 0;
    size_t i;

    if (line == NULL)
        return (NULL);

    token = strtok(line, DEL);
    for (i = 0; token != NULL; i++)
    {
        if (i == size)
        {
            size = size + 10;
            args = _realloc(args, 8 * (size + 1), 8);
            if (args == NULL)
                return (NULL);
        }
        args[i] = token;
        token = strtok(NULL, DEL);
    }
    args[i] = NULL;
    return (args);
}
  • 记忆功能

该函数用于释放为输入行和参数分配的内存。 问题不太可能出在这个函数中,因为它的目的是内存管理。

#include "shell.h"

/**
 * memory - to free al memory allocated
 * @line : line to be free
 * @args: args to be free
 */


void memory(char *line, char **args)
{
    if (line != NULL)
        free(line);
    if (args != NULL)
        free(args);
}
  • _realloc 函数,我在断线函数中使用

该函数用于重新分配内存块。 它似乎工作正常,与退货状态问题没有直接关系。

#include <stdlib.h>
#include "shell.h"

/**
 * *_realloc - reallocates a memory block using malloc and free
 * @ptr: pointer to the memory previsouly allocated by malloc
 * @old_size: size of the allocated memory for ptr
 * @new_size: new size of the new memory block
 * Return: pointer to the newly allocated memory block
 */
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
    char *ptr1;
    char *old_ptr;
    unsigned int i;

    if (new_size == old_size)
        return (ptr);

    if (new_size == 0 && ptr)
    {
        free(ptr);
        return (NULL);
    }

    if (!ptr)
        return (malloc(new_size));

    ptr1 = malloc(new_size);
    if (!ptr1)
        return (NULL);

    old_ptr = ptr;

    if (new_size < old_size)
    {
        for (i = 0; i < new_size; i++)
            ptr1[i] = old_ptr[i];
    }

    if (new_size > old_size)
    {
        for (i = 0; i < old_size; i++)
            ptr1[i] = old_ptr[i];
    }

    free(ptr);
    return (ptr1);
}
  • 头文件
#ifndef SHELL_H
#define SHELL_H

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

#define DEL " \t\r\n\a"


char *read_line();
char **break_line(char *line);
int exe_cmd(char **args);
void memory(char *line, char **args);
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);

/*string.h*/
int _strcmp(char *s1, char *s2);

#endif

我正在寻求帮助以确定问题的根本原因。我怀疑这可能与 exe_cmd 函数中的返回状态处理方式或 break_line 函数中的动态内存分配有关。

任何解决问题和改进 shell 程序的见解或建议将不胜感激。预先感谢您的帮助!

c shell return status
© www.soinside.com 2019 - 2024. All rights reserved.