From b50a669d711877259b4c2092485767f14815c72d Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Wed, 9 Jan 2013 17:06:15 +0000 Subject: [PATCH 1/2] Check for basename(3) error. --- playlist.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/playlist.c b/playlist.c index ceac2fc..5c5d632 100644 --- a/playlist.c +++ b/playlist.c @@ -203,18 +203,21 @@ playlist * playlist_load(const char *filename, meta_info **db, int ndb) { meta_info *mi, **mit; - FILE *fin; - char *period; - char entry[PATH_MAX + 1]; + FILE *fin; + char *name, *period; + char entry[PATH_MAX + 1]; /* open file */ if ((fin = fopen(filename, "r")) == NULL) err(1, "playlist_load: failed to open playlist '%s'", filename); + if ((name = basename(filename)) == NULL) + err(1, "%s: basename(3) failed", __FUNCTION__); + /* create playlist and setup */ playlist *p = playlist_new(); p->filename = strdup(filename); - p->name = strdup(basename(p->filename)); + p->name = strdup(name); if (p->filename == NULL || p->name == NULL) err(1, "playlist_load: failed to allocate info for playlist '%s'", filename); From 2d8c242b7326a4d8f00190d10d59ab1baeee4c07 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Wed, 9 Jan 2013 17:06:40 +0000 Subject: [PATCH 2/2] Pass the file and name when creating a playlist. --- commands.c | 6 ++---- medialib.c | 12 ++---------- playlist.c | 30 +++++++++++++----------------- playlist.h | 2 +- 4 files changed, 18 insertions(+), 32 deletions(-) diff --git a/commands.c b/commands.c index e310fca..0d0c8ae 100644 --- a/commands.c +++ b/commands.c @@ -413,11 +413,8 @@ cmd_new(int argc, char *argv[]) } /* create & setup playlist */ - p = playlist_new(); + p = playlist_new(filename, name); p->needs_saving = true; - p->filename = filename; - if ((p->name = strdup(name)) == NULL) - err(1, "cmd_new: strdup(3) failed"); /* add playlist to media library and update ui */ medialib_playlist_add(p); @@ -429,6 +426,7 @@ cmd_new(int argc, char *argv[]) paint_library(); paint_message("playlist \"%s\" added", name); + free(filename); return 0; } diff --git a/medialib.c b/medialib.c index 35cfd2e..864df53 100644 --- a/medialib.c +++ b/medialib.c @@ -38,16 +38,8 @@ medialib_load(const char *db_file, const char *playlist_dir) err(1, "failed to strdup db file and playlist dir in medialib_init"); /* setup pseudo-playlists */ - mdb.library = playlist_new(); - mdb.library->filename = NULL; - mdb.library->name = strdup("--LIBRARY--"); - - mdb.filter_results = playlist_new(); - mdb.filter_results->filename = NULL; - mdb.filter_results->name = strdup("--FILTER--"); - - if (mdb.library->name == NULL || mdb.filter_results->name == NULL) - err(1, "failed to strdup pseudo-names in medialib_load"); + mdb.library = playlist_new(NULL, "--LIBRARY--"); + mdb.filter_results = playlist_new(NULL, "--FILTER--"); /* load the actual database */ medialib_db_load(db_file); diff --git a/playlist.c b/playlist.c index 5c5d632..d5a56d3 100644 --- a/playlist.c +++ b/playlist.c @@ -37,7 +37,7 @@ playlist_increase_capacity(playlist *p) * structure must be free(2)'d using playlist_free(). */ playlist * -playlist_new(void) +playlist_new(const char *filename, const char *name) { playlist *p; @@ -55,6 +55,15 @@ playlist_new(void) p->hist_present = -1; p->needs_saving = false; + if (filename != NULL) { + if ((p->filename = strdup(filename)) == NULL) + err(1, "%s: strdup(3) failed", __FUNCTION__); + } + if (name != NULL) { + if ((p->name = strdup(name)) == NULL) + err(1, "%s: strdup(3) failed", __FUNCTION__); + } + return p; } @@ -81,19 +90,10 @@ playlist_dup(const playlist *original, const char *filename, int i; /* create new playlist and copy simple members */ - newplist = playlist_new(); + newplist = playlist_new(filename, name); newplist->nfiles = original->nfiles; newplist->capacity = original->nfiles; - if (name != NULL) { - if ((newplist->name = strdup(name)) == NULL) - err(1, "playlist_dup: strdup name failed"); - } - if (filename != NULL) { - if ((newplist->filename = strdup(filename)) == NULL) - err(1, "playlist_dup: strdup filename failed"); - } - /* copy all of the files */ newplist->files = calloc(original->nfiles, sizeof(meta_info*)); if (newplist->files == NULL) @@ -215,11 +215,7 @@ playlist_load(const char *filename, meta_info **db, int ndb) err(1, "%s: basename(3) failed", __FUNCTION__); /* create playlist and setup */ - playlist *p = playlist_new(); - p->filename = strdup(filename); - p->name = strdup(name); - if (p->filename == NULL || p->name == NULL) - err(1, "playlist_load: failed to allocate info for playlist '%s'", filename); + playlist *p = playlist_new(filename, name); /* hack to remove '.playlist' from name */ period = strrchr(p->name, '.'); @@ -312,7 +308,7 @@ playlist_filter(const playlist *p, bool m) if (!mi_query_isset()) return NULL; - results = playlist_new(); + results = playlist_new(NULL, NULL); for (i = 0; i < p->nfiles; i++) { if (mi_match(p->files[i])) { if (m) playlist_files_append(results, &(p->files[i]), 1, false); diff --git a/playlist.h b/playlist.h index 60c06a0..979fa62 100644 --- a/playlist.h +++ b/playlist.h @@ -81,7 +81,7 @@ typedef struct { */ /* create/destroy/duplicate playlist structs */ -playlist *playlist_new(void); +playlist *playlist_new(const char *filename, const char *name); void playlist_free(playlist *p); playlist *playlist_dup(const playlist *original, const char *filename, const char* name);