map

minimalist audio player
git clone https://git.porkepik.fr/map
Log | Files | Refs | README | LICENSE

commit b175835c93f64761b9a6b8867b57c195e827a56e
parent a31ac5398ee890c2825eb50b87e5764352d1f5e5
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Wed, 17 Jun 2020 23:46:10 +0200

fix error when folder argument did not end with a slash

Diffstat:
Mmap.c | 75++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 38 insertions(+), 37 deletions(-)

diff --git a/map.c b/map.c @@ -1,5 +1,6 @@ /* TODO - volume change + - fix indent in play() */ #include <dirent.h> @@ -128,36 +129,38 @@ static struct track * init_track(const char *curdir, const char *d_name) { size_t extension; - struct track *temp = malloc(sizeof(struct track)); + struct track *tmp = malloc(sizeof(struct track)); - temp->filename = malloc(strlen(curdir) + strlen(d_name) + 1); - if (!temp->filename) + tmp->filename = malloc(strlen(curdir) + strlen(d_name) + 2); + if (!tmp->filename) errx(1, "malloc failure"); - strcpy(temp->filename, curdir); - strcat(temp->filename, d_name); + strcpy(tmp->filename, curdir); + if (curdir[strlen(curdir) - 1] != '/') + strcat(tmp->filename, "/"); + strcat(tmp->filename, d_name); if (strend(d_name, ".mp3")) { - temp->open = mp3_open; - temp->decode = mp3_decode; - temp->seek = mp3_seek; - temp->close = mp3_close; - temp->format = "MP3"; + tmp->open = mp3_open; + tmp->decode = mp3_decode; + tmp->seek = mp3_seek; + tmp->close = mp3_close; + tmp->format = "MP3"; } else if (strend(d_name, ".flac")) { - temp->open = flac_open; - temp->decode = flac_decode; - temp->seek = flac_seek; - temp->close = flac_close; - temp->format = "FLAC"; + tmp->open = flac_open; + tmp->decode = flac_decode; + tmp->seek = flac_seek; + tmp->close = flac_close; + tmp->format = "FLAC"; } - temp->name = malloc(strlen(strrchr(temp->filename, '/'))); - if (!temp->name) + tmp->name = malloc(strlen(strrchr(tmp->filename, '/'))); + if (!tmp->name) errx(1, "malloc failure"); - strcpy(temp->name, strrchr(temp->filename, '/') + 1); - if ((extension = strrchr(temp->name, '.') - temp->name)) - temp->name[extension] = '\0'; + strcpy(tmp->name, strrchr(tmp->filename, '/') + 1); + if ((extension = strrchr(tmp->name, '.') - tmp->name)) + tmp->name[extension] = '\0'; - return temp; + return tmp; } static struct track ** @@ -209,7 +212,7 @@ play(struct player player) { int trackid = 0, cursorid = 1, toptrack, timersec; int gapless = 0; - long done, buf_size, tempbuf_len; + long done, buf_size, tmpbuf_len; long *buffer, *newbuf; time_t start, pausetime; struct track curtrack; @@ -258,7 +261,6 @@ next: break; /* select track under cursor*/ case ' ': - curtrack.close(); trackid = toptrack + cursorid - 1; goto endtrack; /* resume/pause */ @@ -282,19 +284,10 @@ next: break; /* quit */ case 'q': - curtrack.close(); trackid = ntracks; goto endtrack; } - if(get_term_size(&rows, &cols)) { - clear_term(); - print_playlist(player.tracks, 0); - cursorid = 1; - toptrack = 0; - print_info(player.curdir, curtrack); - } - if (pausetime) { print_pause(); usleep(10 * 1000); @@ -304,16 +297,16 @@ next: if (!gapless) { done = curtrack.decode(buffer, buf_size); if (trackid+1 < ntracks && done < buf_size / 2) { - tempbuf_len = done * 2; + tmpbuf_len = done * 2; gapless = 1; curtrack.close(); trackid++; goto next; } } else { - newbuf = malloc((buf_size - tempbuf_len) * sizeof(long)); - done = curtrack.decode(newbuf, buf_size - tempbuf_len); - memcpy(buffer + tempbuf_len, newbuf, (buf_size - tempbuf_len) * sizeof(long)); + newbuf = malloc((buf_size - tmpbuf_len) * sizeof(long)); + done = curtrack.decode(newbuf, buf_size - tmpbuf_len); + memcpy(buffer + tmpbuf_len, newbuf, done * 2 * sizeof(long)); free(newbuf); gapless = 0; } @@ -323,13 +316,21 @@ next: errx(1, "Error during decoding."); } + if(get_term_size(&rows, &cols)) { + clear_term(); + print_playlist(player.tracks, 0); + cursorid = 1; + toptrack = 0; + print_info(player.curdir, curtrack); + } + timersec = difftime(time(NULL), start); print_timer(timersec, curtrack.length); } while (done); - curtrack.close(); trackid++; endtrack: + curtrack.close(); free(buffer); } }