目录修改的分段错误?

时间:2010-10-15 16:40:55

标签: c segmentation-fault

这是我的代码:

/*
 * main.c
 *
 *  Created on: 15 Oct 2010
 *      Author: mohit
 */

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

void print_usage();

int main(int argc, char *argv[])
{
    int i;
    char *directory;
    char *name;

    //Iterate through command line arguments
    for (i = 1; i < argc; i++)
    {
    if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
    {
        memcpy(directory, argv[i + 1], sizeof (argv[i + 1]));
        continue;
    }

    else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
    {
        memcpy(name, argv[i + 1], sizeof (argv[i + 1]));
        continue;
    }
}

if (!name)
{
    perror("\n\nIncorrect Usage! Name was not provided!\n\n");
    print_usage();
}
if (directory)
    chdir(directory);

printf("\nCreating %s.theme directory...\n", name);
mkdir(strcat(name, ".theme"), "a+rw");
printf("Created %s.theme...\n", name);

printf("Entering %s.theme...\n", name);
chdir(strcat(name, ".theme"));

printf("Creating Icons directory...\n");
mkdir("Icons", "a+rw");
printf("Created Icons...\n");

printf("Creating Bundles directory...\n");
mkdir("Bundles", "a+rw");
printf("Created Bundles...\n");

printf("Creating UIImages directory...\n");
mkdir("UIImages", "a+rw");
printf("Created UIImages...\n");

printf("Creating Folder directory...\n");
mkdir("Folder", "a+rw");
printf("Created Folder...\n");

return 0;
}

void print_usage()
{
printf("\n\nUsage: wbt-create [--directory directory] --name theme_name\n");
printf("\n\n\t wbt-create [-d directory] -n theme_name\n");
}

3 个答案:

答案 0 :(得分:2)

argv中的参数将在程序运行时保持分配状态。因此,您可以避免memcpy(以及内存分配)并简单地分配指针。示例如下:

...
int main(int argc, char *argv[])
{
    int i;
    char *directory;
    char *name;

    //Iterate through command line arguments
    for (i = 1; i < argc; i++)
    {
    if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
    {
        directory = argv[i + 1];
        continue;
    }

    else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
    {
        name = argv[i + 1];
        continue;
    }
}
...

但是,我强烈建议使用getopt。它是一个相当标准的库,可以很好地处理这些事情。

答案 1 :(得分:1)

char *directory;
char *name;

那些是声明指针,但没有为它们分配任何内存。当您执行memcpy()将数据复制到这些指针时,您将获得Seg错误。

您可以在堆栈上声明char数组,例如:

char directory[100];

或者你必须使用malloc()来自己分配内存。

答案 2 :(得分:1)

变化:

int main(int argc, char *argv[])
{
    int i;
    char *directory;
    char *name;

    //Iterate through command line arguments
    for (i = 1; i < argc; i++)
    {
        if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
        {
            memcpy(directory, argv[i + 1], sizeof (argv[i + 1]));
            continue;
        }

        else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
        {
            memcpy(name, argv[i + 1], sizeof (argv[i + 1]));
            continue;
        }
    }
    ...

为:

#include <limits.h>

int main(int argc, char *argv[])
{
    int i;
    char directory[PATH_MAX];
    char name[PATH_MAX];

    //Iterate through command line arguments
    for (i = 1; i < argc; i++)
    {
        if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--directory"))
        {
            strcpy(directory, argv[i + 1]);
            continue;
        }

        else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name"))
        {
            strcpy(name, argv[i + 1]);
            continue;
        }
    }
    ...