diff --git a/README.md b/README.md index 28dabe2..40a1ad1 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ Exemplary configuration might be as follows: * `format-coverfile = "^(cover|folder)\.jpg$"` -Instalation ------------ +Installation +------------ $ autoreconf --install $ mkdir build && cd build diff --git a/src/cache.c b/src/cache.c index e08bb57..2df2f5c 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1,6 +1,6 @@ /* * cmusfm - cache.c - * Copyright (c) 2014 Arkadiusz Bokowy + * Copyright (c) 2014-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -104,7 +104,7 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) { sb_tinf->artist, sb_tinf->album, sb_tinf->album_artist, sb_tinf->track_number, sb_tinf->track, sb_tinf->duration); - if ((f = fopen(get_cmusfm_cache_file(), "a")) == NULL) + if ((f = fopen(cmusfm_cache_file, "a")) == NULL) return; record = get_cache_record(sb_tinf); @@ -118,7 +118,6 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) { void cmusfm_cache_submit(scrobbler_session_t *sbs) { char rd_buff[4096]; - char *fname; FILE *f; scrobbler_trackinfo_t sb_tinf; struct cmusfm_cache_record *record; @@ -127,8 +126,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { debug("cache submit"); - fname = get_cmusfm_cache_file(); - if ((f = fopen(fname, "r")) == NULL) + if ((f = fopen(cmusfm_cache_file, "r")) == NULL) return; // read file until EOF @@ -200,12 +198,10 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { fclose(f); // remove cache file - unlink(fname); + unlink(cmusfm_cache_file); } -// Helper function for retrieving cmusfm cache file. +/* Helper function for retrieving cmusfm cache file. */ char *get_cmusfm_cache_file(void) { - static char fname[128]; - sprintf(fname, "%s/" CACHE_FNAME, get_cmus_home_dir()); - return fname; + return get_cmus_home_file(CACHE_FNAME); } diff --git a/src/cmusfm.h b/src/cmusfm.h index 5e17966..9328b04 100644 --- a/src/cmusfm.h +++ b/src/cmusfm.h @@ -1,6 +1,6 @@ /* * cmusfm - cmusfm.h - * Copyright (c) 2010-2014 Arkadiusz Bokowy + * Copyright (c) 2010-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -30,14 +30,17 @@ #define CACHE_FNAME "cmusfm.cache" -// time delay (in seconds) between login attempts to the Last.fm -// scrobbling service after a submit failure +/* time delay (in seconds) between login attempts to the Last.fm + * scrobbling service after a submit failure */ #define SERVICE_RETRY_DELAY 60 * 30 -// global variable definitions +/* global variable definitions */ extern unsigned char SC_api_key[16]; extern unsigned char SC_secret[16]; +extern const char *cmusfm_cache_file; +extern const char *cmusfm_config_file; +extern const char *cmusfm_socket_file; extern struct cmusfm_config config; @@ -71,6 +74,7 @@ struct format_match { char *get_cmus_home_dir(void); +char *get_cmus_home_file(const char *file); #ifdef ENABLE_LIBNOTIFY char *get_album_cover_file(const char *location, const char *format); #endif diff --git a/src/config.c b/src/config.c index 1528572..39e94a6 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,6 @@ /* * cmusfm - config.c - * Copyright (c) 2014 Arkadiusz Bokowy + * Copyright (c) 2014-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -24,10 +24,10 @@ #include "config.h" -#include -#include #include #include +#include +#include #include #ifdef HAVE_SYS_INOTIFY_H #include @@ -156,17 +156,15 @@ int cmusfm_config_write(const char *fname, struct cmusfm_config *conf) { } #ifdef HAVE_SYS_INOTIFY_H -// Add the cmusfm configuration file into the inotify watch stack. File -// is watched for modification, and after event is triggered it will be -// automatically removed from the stack. +/* Add the cmusfm configuration file into the inotify watch stack. File + * is watched for modification, and after event is triggered it will be + * automatically removed from the stack. */ int cmusfm_config_add_watch(int fd) { - return inotify_add_watch(fd, get_cmusfm_config_file(), IN_MODIFY | IN_ONESHOT); + return inotify_add_watch(fd, cmusfm_config_file, IN_MODIFY | IN_ONESHOT); } #endif -// Helper function for retrieving cmusfm configuration file. +/* Helper function for retrieving cmusfm configuration file. */ char *get_cmusfm_config_file(void) { - static char fname[128]; - sprintf(fname, "%s/" CONFIG_FNAME, get_cmus_home_dir()); - return fname; + return get_cmus_home_file(CONFIG_FNAME); } diff --git a/src/main.c b/src/main.c index b6f6974..6a1b9a5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* * cmusfm - main.c - * Copyright (c) 2010-2014 Arkadiusz Bokowy + * Copyright (c) 2010-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -22,23 +22,29 @@ #include "../config.h" #endif +#include "cmusfm.h" #include #include #include -#include "cmusfm.h" +#include "cache.h" #include "config.h" #include "debug.h" #include "server.h" -// Last.fm API key for cmusfm +/* Last.fm API key for cmusfm */ unsigned char SC_api_key[16] = {0x67, 0x08, 0x2e, 0x45, 0xda, 0xb1, 0xf6, 0x43, 0x3d, 0xa7, 0x2a, 0x00, 0xe3, 0xbc, 0x03, 0x7a}; unsigned char SC_secret[16] = {0x02, 0xfc, 0xbc, 0x90, 0x34, 0x1a, 0x01, 0xf2, 0x1c, 0x3b, 0xfc, 0x05, 0xb6, 0x36, 0xe3, 0xae}; -// Global configuration structure +/* Global cmusfm file location variables */ +const char *cmusfm_cache_file = NULL; +const char *cmusfm_config_file = NULL; +const char *cmusfm_socket_file = NULL; + +/* Global configuration structure */ struct cmusfm_config config; @@ -102,15 +108,13 @@ static int cmusfm_initialization() { scrobbler_session_t *sbs; struct cmusfm_config conf; int fetch_session_key; - char *conf_fname; char yesno[8], *ptr; fetch_session_key = 1; - conf_fname = get_cmusfm_config_file(); sbs = scrobbler_initialize(SC_api_key, SC_secret); // try to read previous configuration - if (cmusfm_config_read(conf_fname, &conf) == 0) { + if (cmusfm_config_read(cmusfm_config_file, &conf) == 0) { printf("Checking previous session (user: %s) ...", conf.user_name); fflush(stdout); scrobbler_set_session_key_str(sbs, conf.session_key); @@ -135,8 +139,8 @@ static int cmusfm_initialization() { } scrobbler_free(sbs); - if (cmusfm_config_write(conf_fname, &conf) != 0) - printf("Error: unable to write file: %s\n", conf_fname); + if (cmusfm_config_write(cmusfm_config_file, &conf) != 0) + printf("Error: unable to write file: %s\n", cmusfm_config_file); return 0; } @@ -153,11 +157,16 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } + /* setup global variables - file locations */ + cmusfm_cache_file = get_cmusfm_cache_file(); + cmusfm_config_file = get_cmusfm_config_file(); + cmusfm_socket_file = get_cmusfm_socket_file(); + if (argc == 2 && strcmp(argv[1], "init") == 0) return cmusfm_initialization(); - if (cmusfm_config_read(get_cmusfm_config_file(), &config) == -1) { - perror("error: unable to read config file"); + if (cmusfm_config_read(cmusfm_config_file, &config) == -1) { + perror("error: config read"); return EXIT_FAILURE; } diff --git a/src/server.c b/src/server.c index 9f4ae2d..967ed0e 100644 --- a/src/server.c +++ b/src/server.c @@ -1,6 +1,6 @@ /* * cmusfm - server.c - * Copyright (c) 2010-2014 Arkadiusz Bokowy + * Copyright (c) 2010-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -24,14 +24,14 @@ #include "server.h" -#include -#include -#include -#include #include #include #include #include +#include +#include +#include +#include #include #include #ifdef HAVE_SYS_INOTIFY_H @@ -47,22 +47,22 @@ #endif -// Helper function for artist name retrieval. +/* Helper function for artist name retrieval. */ static char *get_sock_data_artist(struct sock_data_tag *dt) { return (char *)(dt + 1); } -// Helper function for album name retrieval. +/* Helper function for album name retrieval. */ static char *get_sock_data_album(struct sock_data_tag *dt) { return &((char *)(dt + 1))[dt->alboff]; } -// Helper function for track name retrieval. +/* Helper function for track name retrieval. */ static char *get_sock_data_track(struct sock_data_tag *dt) { return &((char *)(dt + 1))[dt->titoff]; } -// Helper function for location retrieval. +/* Helper function for location retrieval. */ static char *get_sock_data_location(struct sock_data_tag *dt) { return &((char *)(dt + 1))[dt->locoff]; } @@ -270,7 +270,7 @@ void cmusfm_server_start(void) { memset(&sock_a, 0, sizeof(sock_a)); sock_a.sun_family = AF_UNIX; - strcpy(sock_a.sun_path, get_cmusfm_socket_file()); + strcpy(sock_a.sun_path, cmusfm_socket_file); pfds[0].fd = socket(PF_UNIX, SOCK_STREAM, 0); // check if behind the socket there is already an active server instance @@ -329,7 +329,7 @@ void cmusfm_server_start(void) { // to us, simply read out the inotify file descriptor read(pfds[2].fd, &inot_even, sizeof(inot_even)); debug("inotify event occurred: %x", inot_even.mask); - cmusfm_config_read(get_cmusfm_config_file(), &config); + cmusfm_config_read(cmusfm_config_file, &config); cmusfm_config_add_watch(pfds[2].fd); } #endif @@ -435,7 +435,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { // connect to the communication socket memset(&sock_a, 0, sizeof(sock_a)); - strcpy(sock_a.sun_path, get_cmusfm_socket_file()); + strcpy(sock_a.sun_path, cmusfm_socket_file); sock_a.sun_family = AF_UNIX; sock = socket(PF_UNIX, SOCK_STREAM, 0); if (connect(sock, (struct sockaddr *)(&sock_a), sizeof(sock_a)) == -1) { @@ -450,9 +450,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { return close(sock); } -// Helper function for retrieving cmusfm server socket file. +/* Helper function for retrieving cmusfm server socket file. */ char *get_cmusfm_socket_file(void) { - static char fname[128]; - sprintf(fname, "%s/" SOCKET_FNAME, get_cmus_home_dir()); - return fname; + return get_cmus_home_file(SOCKET_FNAME); } diff --git a/src/utils.c b/src/utils.c index c48aa9a..ec76911 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,6 +1,6 @@ /* * cmusfm - utils.c - * Copyright (c) 2014 Arkadiusz Bokowy + * Copyright (c) 2014-2015 Arkadiusz Bokowy * * This file is a part of a cmusfm. * @@ -22,33 +22,57 @@ #include "../config.h" #endif +#include "cmusfm.h" + +#include #include #include #include -#include #ifdef ENABLE_LIBNOTIFY #include #include #endif -#include "cmusfm.h" #include "debug.h" -// Helper function for retrieving cmus configuration home path. +/* Helper function for retrieving cmus configuration home path. */ char *get_cmus_home_dir(void) { - static char fname[128]; - char *xdg_config; + const char cmusdir[] = "/cmus"; + char *fullpath; + char *tmp; + + /* get our file path in the XDG configuration directory */ + if ((tmp = getenv("XDG_CONFIG_HOME")) != NULL) { + fullpath = malloc(strlen(tmp) + sizeof(cmusdir)); + sprintf(fullpath, "%s%s", tmp, cmusdir); + return fullpath; + } + + if ((tmp = getenv("HOME")) != NULL) { + fullpath = malloc(strlen(tmp) + 8 + sizeof(cmusdir)); + sprintf(fullpath, "%s/.config%s", tmp, cmusdir); + return fullpath; + } + + /* semi failproof return */ + return strdup(cmusdir); +} + +/* Helper function for retrieving a path for given file inside the cmus + * configuration home path. */ +char *get_cmus_home_file(const char *file) { + + char *home; + char *fullpath; - // get XDG config directory or fall-back to the default - xdg_config = getenv("XDG_CONFIG_HOME"); - if (xdg_config) - strcpy(fname, xdg_config); - else - sprintf(fname, "%s/.config", getenv("HOME")); + home = get_cmus_home_dir(); + fullpath = malloc(strlen(home) + 1 + strlen(file) + 1); + sprintf(fullpath, "%s/%s", home, file); - return strcat(fname, "/cmus"); + free(home); + return fullpath; } #ifdef ENABLE_LIBNOTIFY