URL: https://www.opennet.me/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 8242
[ Назад ]

Исходное сообщение
"Ошибка 'Segmentation fault'"

Отправлено extira , 27-Апр-09 04:00 
При работе программы вылетает ошибка Segmentation fault

Если в switch идти по варианту 'd' вылетает в промежутке между printf("TEST4\n") и printf("TREST"). TEST4 выводит а TREST уже нет.
Если в switch идти по варианту 'f' то вылетает сразуже даже не печатая test10.

Я даже не знаю в какую сторону копать.

ЗЫ. Закоменчено конечноже все это не было.

Вот код:
int main (int argc, char *argv[])
{
    if(argc!=3||((argv[1][0]!='d'||argv[1][0]!='f')&& argv[1][1]!=0))
    {
        printf("The program %s takes 2 arguments:\n\t 1 -  \"d\" - make matrix of all included elements in directory\n\t\t\"f\" - make matrix of element\n\t 2 -  full path to element\n", argv[0]);
        return 0;
    }
    
    struct stat info;
    struct filerights *fileinfo=NULL;    
    int ret;
    DIR *directory=NULL;
    struct dirent *entry=NULL;
    struct dirent *entry1=NULL;
    int i=0, j=0;
    size_t path, name;
    char *fullpath=NULL;
    int ii=0;
    struct passwd **users;
    int k=0, l=0;
    
    switch (argv[1][0])
    {
        case 'd':
            directory=opendir(argv[2]);
            if(directory==NULL)
            {
                perror("ERROR:");
                return 0;
            }
            while (entry = readdir(directory))
            {
                if(entry == NULL)
                {
                    perror("ERROR1:");
                }
                else
                {
                    i=i+1;
                }
            }
            if(closedir(directory)==(-1))
            {
                perror("ERROR");
            }
            fileinfo=malloc(i*sizeof(struct filerights));
            if(fileinfo==NULL)
            {
                perror("ERROR:");
            }
            directory=opendir(argv[2]);
            if(directory==NULL)
            {
                perror("ERROR:");
            }
            path=strlen(argv[2]);
//            printf("%d\n",i);
            for(j=0;j<i;j++)
            {
//                printf("%d\n",j);
                entry1=readdir(directory);
                if(entry1==NULL)
                {
                    perror("ERROR");
                    printf("End");
                    return 0;
                }
                else
                {
//                    printf("working %d\n", j);
                }
//                printf("%s\n", entry1->d_name);
                name=strlen(entry1->d_name);
                fullpath=malloc(path+name);
                if(fullpath==NULL)
                {
                    perror("ERROR");
                }
                if(strcpy(fullpath,argv[2])==NULL)
                {
                    perror("ERROR");
                }
                if(strcat(fullpath,entry1->d_name)==NULL)
                {
                    perror("ERROR");
                }
//                printf("%s\n", fullpath);

                if(!stat(fullpath, &info))
                {
//                    printf("testI");
                    if(strcpy(fileinfo[j].name, fullpath)==NULL)
                    {
                        perror("ERROR");
                    }
//                    printf("%s\n", fileinfo[j].name);
                    fileinfo[j].uid=info.st_uid;
                    fileinfo[j].gid=info.st_gid;
                
                    if((info.st_mode|1)==info.st_mode)
                    {
                        fileinfo[j].execute_other=1;
                    }
                    else
                    {
                        fileinfo[j].execute_other=0;
                    }
                    if((info.st_mode|2)==info.st_mode)
                    {
                        fileinfo[j].write_other=1;
                    }
                    else
                    {
                        fileinfo[j].write_other=0;
                    }
                    if((info.st_mode|4)==info.st_mode)
                    {
                        fileinfo[j].read_other=1;
                    }
                    else
                    {
                        fileinfo[j].read_other=0;
                    }
                    if((info.st_mode|8)==info.st_mode)
                    {
                        fileinfo[j].execute_group=1;
                    }
                    else
                    {
                        fileinfo[j].execute_group=0;
                    }
                    if((info.st_mode|16)==info.st_mode)
                    {
                        fileinfo[j].write_group=1;
                    }
                    else
                    {
                        fileinfo[j].write_group=0;
                    }
                    if((info.st_mode|32)==info.st_mode)
                    {
                        fileinfo[j].read_group=1;
                    }
                    else
                    {
                        fileinfo[j].read_group=0;
                    }
                    if((info.st_mode|64)==info.st_mode)
                    {
                        fileinfo[j].execute_owner=1;
                    }
                    else
                    {
                        fileinfo[j].execute_owner=0;
                    }
                    if((info.st_mode|128)==info.st_mode)
                    {
                        fileinfo[j].write_owner=1;
                    }
                    else
                    {
                        fileinfo[j].write_owner=0;
                    }
                    if((info.st_mode|256)==info.st_mode)
                    {
                        fileinfo[j].read_owner=1;
                    }
                    else
                    {
                        fileinfo[j].read_owner=0;
                    }
                    if((info.st_mode|512)==info.st_mode)
                    {
                        fileinfo[j].sticky_bit=1;
                    }
                    else
                    {
                        fileinfo[j].sticky_bit=0;
                    }
                    if((info.st_mode|1024)==info.st_mode)
                    {
                        fileinfo[j].set_gid=1;
                    }
                    else
                    {
                        fileinfo[j].set_gid=0;
                    }
                    if((info.st_mode|2048)==info.st_mode)
                    {
                        fileinfo[j].set_uid=1;
                    }
                    else
                    {
                        fileinfo[j].set_uid=0;
                    }
                }
                else
                {
                    perror("ERROR:");
                    return 0;
                }
                free(fullpath);
            }
//            printf("TEST\n");
            closedir(directory);
//            free(fullpath);
//            printf("TEST1\n");
//            printf("TEST2\n");
//            printf("TEST3\n");
//            printf("TEST4\n");    
            break;
        case 'f':
//            printf("test10");
            i=1;
//            printf("test1");
            fileinfo=malloc((size_t)sizeof(struct filerights));
//            printf("test2");
            if(!stat(argv[2], &info))
            {
                strcpy(fileinfo->name, argv[2]);
                fileinfo->uid=info.st_uid;
                fileinfo->gid=info.st_gid;
                
                if((info.st_mode|1)==info.st_mode)
                {
                    fileinfo->execute_other=1;
                }
                else
                {
                    fileinfo->execute_other=0;
                }
                if((info.st_mode|2)==info.st_mode)
                {
                    fileinfo->write_other=1;
                }
                else
                {
                    fileinfo->write_other=0;
                }
                if((info.st_mode|4)==info.st_mode)
                {
                    fileinfo->read_other=1;
                }
                else
                {
                    fileinfo->read_other=0;
                }
                if((info.st_mode|8)==info.st_mode)
                {
                    fileinfo->execute_group=1;
                }
                else
                {
                    fileinfo->execute_group=0;
                }
                if((info.st_mode|16)==info.st_mode)
                {
                    fileinfo->write_group=1;
                }
                else
                {
                    fileinfo->write_group=0;
                }
                if((info.st_mode|32)==info.st_mode)
                {
                    fileinfo->read_group=1;
                }
                else
                {
                    fileinfo->read_group=0;
                }
                if((info.st_mode|64)==info.st_mode)
                {
                    fileinfo->execute_owner=1;
                }
                else
                {
                    fileinfo->execute_owner=0;
                }
                if((info.st_mode|128)==info.st_mode)
                {
                    fileinfo->write_owner=1;
                }
                else
                {
                    fileinfo->write_owner=0;
                }
                if((info.st_mode|256)==info.st_mode)
                {
                    fileinfo->read_owner=1;
                }
                else
                {
                    fileinfo->read_owner=0;
                }
                if((info.st_mode|512)==info.st_mode)
                {
                    fileinfo->sticky_bit=1;
                }
                else
                {
                    fileinfo->sticky_bit=0;
                }
                if((info.st_mode|1024)==info.st_mode)
                {
                    fileinfo->set_gid=1;
                }
                else
                {
                    fileinfo->set_gid=0;
                }
                if((info.st_mode|2048)==info.st_mode)
                {
                    fileinfo->set_uid=1;
                }
                else
                {
                    fileinfo->set_uid=0;
                }
            }
            else
            {
                perror("ERROR:");
                return 0;
            }
            break;
    }
//    printf("TREST");
    
//    printf("test3");

    setpwent();
.............................


Содержание

Сообщения в этом обсуждении
"Ошибка 'Segmentation fault'"
Отправлено Andy_zzzzzz , 27-Апр-09 14:38 
А ті собери с -g и настрой создание сore файлов , а потом через gdb глянь - где у тебя проблемка

"Ошибка 'Segmentation fault'"
Отправлено sas , 30-Апр-09 10:46 
Доброго времени суток,

Проблема (как минимум одна, т.к. я не смотрел код подробно):

                fullpath=malloc(path+name);
                if(fullpath==NULL)
                {
                    perror("ERROR");
                }
                if(strcpy(fullpath,argv[2])==NULL)

Забыли выделить место для '\0'.

Посмотрите код на предмет подобных проблем.

Удачи
--- sas

>[оверквотинг удален]
>    return 0;
>   }
>   break;
> }
>// printf("TREST");
>
>// printf("test3");
>
> setpwent();
>.............................