简体   繁体   English

将fts(3)的输出写入文件

[英]Write output of fts(3) to file

I am developing a file fuzzer that mutates music files and feeds them to iTunes. 我正在开发一种文件模糊处理程序,可以使音乐文件变异并将其馈送到iTunes。 I already developed code to mutate the music file, but I want to use more than one music file to increase code coverage. 我已经开发了使音乐文件变异的代码,但是我想使用多个音乐文件来增加代码覆盖率。 So I want to traverse my iTunes library and load the file paths into a buffer or into a file; 因此,我想遍历我的iTunes库并将文件路径加载到缓冲区或文件中; either way will suffice. 任一种方法都足够。 I have this code here that segfaults on the fprintf function. 我这里有这段代码,对fprintf函数进行了fprintf

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <fts.h>

    int compare (const FTSENT**, const FTSENT**);

int main(void)
{   
FILE * musicPaths;
musicPaths = fopen("Users/NahNig/music_paths", "wb");
char *p1 = "/Users/NahNig/Music/iTunes/iTunes Media/Music";
char *p2 = NULL;
char *path[1];
path[0] = p1;
path[1] = p2;
FTS* file_system = NULL;
FTSENT* child =    NULL;
FTSENT* parent =   NULL;

file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);

if (NULL != file_system)
{
    while( (parent = fts_read(file_system)) != NULL)
    {
        child = fts_children(file_system,0);

        if (errno != 0)
        {
            perror("fts_children");
        }

        while ((NULL != child)
            && (NULL != child->fts_link))
        {
            child = child->fts_link;
            fprintf(musicPaths, "%s%s\n", child->fts_path, child->fts_name);
            }
        }
        fts_close(file_system);
    }

    return 0;
}


int compare(const FTSENT** one, const FTSENT** two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}

I have tried using sscanf and fprintf to write child to a buffer and fwrite to try and write to a file; 我曾尝试使用sscanf和fprintf将子级写入缓冲区,然后使用fwrite尝试写入文件。 none of them have been working. 他们都没有工作。 What I found on Google also didn't help much, so any help would be greatly appreciated. 我在Google上发现的内容也无济于事,因此不胜感激。

You didn't test the return value from fopen() , which is NULL because you missed the first / from /Users/NahNig/music_paths . 您没有测试fopen()的返回值为NULL,因为您错过了/Users/NahNig/music_paths的第一个/ When you use the null pointer, the program crashes. 当您使用空指针时,程序崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM