You didn't test the return value from fopen()
, which is NULL because you missed the first /
from /Users/NahNig/music_paths
. When you use the null pointer, the program crashes.
Write output of fts(3) to file
Pergunta
I am developing a file fuzzer that mutates music files and feeds them to 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; either way will suffice. I have this code here that segfaults on the fprintf
function.
#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; none of them have been working. What I found on Google also didn't help much, so any help would be greatly appreciated.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow