C编程 - 本地时间内存泄漏()

时间:2014-03-26 18:17:21

标签: c shell localtime

您好我有一个创建shell的基本实现的任务,并且分配的一部分要求打印日期/时间以及提示。

日期和时间应采用dd / mm HH / MM格式。

我可以得到那个显示但是当我运行像ls这样的命令时,它不起作用,但当我删除日期时间打印功能命令工作时,我似乎无法在互联网上找到任何帮助我所以我以为我会在这里发帖

这是我的代码:

comp20200Shell.c

#include "comp20200Shell_header.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int main(void)
{
    bool end_program = false;
    size_t length = 0;
    ssize_t read;
    char* input = NULL;

    while(end_program != true)
    {
        if(signal(SIGINT, sigintHandler) == SIG_ERR)
        {
            fprintf(stderr, "An error occured while setting a signal handler\n");
            exit(EXIT_FAILURE);
        }

        printf("\x5b");
        return_time();
        printf("\x5d %s", PROMPT);

        read = getline(&input, &length, stdin);

        remove_trailing_newline(input);

        if(strcmp(input, "exit") == 0 || read == EOF)
        {
            if(read == EOF)
            {
                putchar('\n');
            }
            return(0);
        }

        if(process_input(&input) == -1)
        {
            fprintf(stderr, "Command not found\n");
        }
    }

    return(0);
}   

void sigintHandler(int sig_num)
{
    if(signal(SIGINT, sigintHandler) == SIG_ERR)
    {
        fprintf(stderr, "An error occured while setting a signal handler\n");
        exit(EXIT_FAILURE);
    }
    fflush(stdout);
    return;
}

comp20200Shell_header.h

#include <ctype.h>
#include <string.h>

#define PROMPT "# "

void remove_trailing_newline(char *input);
void sigintHandler(int sig_num);
int process_input(char** input);
void return_time(void);

process_input.c

#include "comp20200Shell_header.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int process_input(char** input)
{
    pid_t child_pid;
    int child_status;
    char** argument = malloc(sizeof(char*));
    int count = 0;
    char* temp = strtok(*input, " ");
    while(temp != NULL)
    {
        argument[count] = temp;
        count ++;
        argument = realloc(argument, (count+1) * sizeof(char *));
        temp = strtok(NULL, " ");
    }

    child_pid = fork();
    if(child_pid == 0)
    {
        execvp(argument[0], argument);
        return(-1);
    }
    else
    {
        wait(&child_status);;
    }

    return(0);
}

remove_trailing_newline.c

#include "comp20200Shell_header.h"
#include <stdio.h>

void remove_trailing_newline(char* input)
{
    int index;
    for(index = 0; input[index] != '\0'; index++)
    {
        if(input[index] == '\n')
        {
            input[index] = '\0';
        }
    }

    return;
}

time_now.c

#include "comp20200Shell_header.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void return_time(void)
{
    time_t now;
    struct tm *timestamp;
    char time_now[12];

    now = time(NULL);

    timestamp = localtime(&now);
    strftime(time_now, sizeof(time_now), "%d/%m %H:%M", timestamp);
    printf("%s", time_now);

    return;
}

这是我运行命令时得到的结果:

[26/03 18:04] # ls
ls: cannot access p��: No such file or directory
[26/03 18:04] # 

0 个答案:

没有答案