仅当逐步通过调试器(gdb)时程序才不会出现段错误

时间:2018-07-13 11:12:49

标签: c gdb

我编写的该程序仅在我不中断程序运行的情况下,才自动打开网络的自动配置逻辑段。当通过带有断点的调试器运行时,它运行得很好。在没有断点的情况下通过调试器运行时,它会出现段错误。该程序中有我不注意的错误吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define COMLEN 64
#define BUFSIZE 128

const char *program_name;

typedef enum errors
{
    SUCCESS = 0,
    MEM_ERROR,
    FILE_ERROR,
    USAGE_ERROR,
} Error;

void terminal_error(Error e)
{
    switch (e)
    {
        case MEM_ERROR:
            fputs("Memory error!\n", stderr);
            break;
        case FILE_ERROR:
            fputs("File error!\n", stderr);
            break;
        case USAGE_ERROR:
            fprintf(stderr, "Incorrect usage. Try %s [y|n]\n", program_name);
            break;
        default:
            fputs("Unknown error!\n", stderr);
            break;
    }
    exit(e);
}

Error read_file(FILE *fp, char **buffer)
{

    long len;
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if ((*buffer = malloc(len+1)) == NULL)
        return MEM_ERROR;
    if (fread(*buffer, 1, len, fp) != len)
        return FILE_ERROR;
    buffer[len+1] = '\0';
    return SUCCESS;
}

Error get_interface(char *name)
{
    const char *cmd = "netsh wlan show networks";
    FILE *fp;
    // open pipe to parse output from command and run command
    if ((fp = popen(cmd, "rb")) == NULL)
        return FILE_ERROR;
    // read the piped output into buffer
    char *buffer = NULL;
    Error res;
    res = read_file(fp, &buffer);
    if (res != SUCCESS)
        return res;
    fclose(fp);
    // parse the buffer
    // needle delimits the start of the interface name in the command
    // end_needle delimits the end of the interface name
    const char *needle = "Interface name : "; 
    const char *end_needle = " \r\nThere";
    // end will point to end_needle (end of interface name)
    char *end = NULL;
    // name points to one-past the end of the needle (start of interface
    // name)
    name = strstr(buffer, needle);
    name += strlen(needle);
    end = strstr(buffer, end_needle);
    *end = '\0'; // terminates the interface name
    return SUCCESS;
}

int main(int argc, const char *argv[])
{
    const char *template = "netsh wlan set autoconfig enabled=%s interface=\"%s\"";
    char interface_name[BUFSIZE] = {0};
    program_name = argv[0];
    Error res = SUCCESS;
    // get name of wireless interfacew
    res = get_interface(interface_name);
    if (res != SUCCESS)
        terminal_error(res);
    char *op;
    if (argc > 1)
        // arguments were provided
    {
        if (strcmp(argv[1], "y") == 0)
            op = "yes";
        else if (strcmp(argv[1], "n") == 0)
            op = "no";
        else
            terminal_error(USAGE_ERROR);
        char command[COMLEN];
        sprintf(command, template, op, interface_name);
        system(command);
    }
    else
        // no arguments were provided
    {
        system("netsh wlan show settings");
    }
    return 0;

}

0 个答案:

没有答案