目录修改的分段错误?

发布于 2024-09-27 15:10:56 字数 1666 浏览 0 评论 0原文

这是我的代码:

/*
 * 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");
}

Here is my code:

/*
 * 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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘑菇王子 2024-10-04 15:10:56

当程序运行时,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 。它是一个相当标准的库,并且可以很好地处理此类事情。

Arguments in argv will stay allocated while your program is running. Therefore, you can avoid the memcpy (and the allocation of memory) and simply assign the pointers. Example below:

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

However, I would highly recommend the use of getopt. It's a fairly standard library and handles these kind of things quite well.

表情可笑 2024-10-04 15:10:56
char *directory;
char *name;

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

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

char directory[100];

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

char *directory;
char *name;

Those are declaring pointers, but not allocating any memory for them. When you do the memcpy() to copy data to those pointers you will get the Seg fault.

You could declare char arrays on the stack instead, something like:

char directory[100];

or you will have to use malloc() to allocate the memory yourself.

反话 2024-10-04 15:10:56

更改:

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

Change:

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

to:

#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;
        }
    }
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文