Removing old svn keywords.
[monky] / src / libmpdclient.c
index 47b5021..6e2b261 100644 (file)
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
 
+#include "conky.h"
 #include "libmpdclient.h"
 
 #include <errno.h>
 #include <ctype.h>
 #include <sys/types.h>
-#include <stdio.h>
 #include <sys/param.h>
-#include <string.h>
 #include <unistd.h>
-#include <stdlib.h>
 #include <fcntl.h>
 #include <limits.h>
 
@@ -149,6 +149,9 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port,
 
        for (res = addrinfo; res; res = res->ai_next) {
                /* create socket */
+               if (connection->sock > -1) {
+                       closesocket(connection->sock);
+               }
                connection->sock = socket(res->ai_family, SOCK_STREAM,
                        res->ai_protocol);
                if (connection->sock < 0) {
@@ -187,27 +190,38 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port,
 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
                float timeout)
 {
-       struct hostent *he;
+       struct hostent he, *he_res = 0;
+       int he_errno;
+       char hostbuff[2048];
        struct sockaddr *dest;
        int destlen;
        struct sockaddr_in sin;
 
-       if (!(he = gethostbyname(host))) {
+#ifdef HAVE_GETHOSTBYNAME_R
+               if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) {      // get the host info
+               snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
+                       "%s ('%s')", hstrerror(h_errno), host);
+               connection->error = MPD_ERROR_UNKHOST;
+               return -1;
+       }
+#else /* HAVE_GETHOSTBYNAME_R */
+       if (!(he_res = gethostbyname(host))) {
                snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
                        "host \"%s\" not found", host);
                connection->error = MPD_ERROR_UNKHOST;
                return -1;
        }
+#endif /* HAVE_GETHOSTBYNAME_R */
 
        memset(&sin, 0, sizeof(struct sockaddr_in));
-       /* dest.sin_family = he->h_addrtype; */
+       /* dest.sin_family = he_res->h_addrtype; */
        sin.sin_family = AF_INET;
        sin.sin_port = htons(port);
 
-       switch (he->h_addrtype) {
+       switch (he_res->h_addrtype) {
                case AF_INET:
-                       memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr,
-                               he->h_length);
+                       memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr,
+                               he_res->h_length);
                        dest = (struct sockaddr *) &sin;
                        destlen = sizeof(struct sockaddr_in);
                        break;
@@ -218,6 +232,9 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port,
                        break;
        }
 
+       if (connection->sock > -1) {
+               closesocket(connection->sock);
+       }
        if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
                strcpy(connection->errorStr, "problems creating socket");
                connection->error = MPD_ERROR_SYSTEM;
@@ -238,7 +255,7 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port,
 }
 #endif /* !MPD_HAVE_GAI */
 
-char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
+const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
        "Artist",
        "Album",
        "Title",
@@ -282,8 +299,8 @@ static mpd_ReturnElement *mpd_newReturnElement(const char *name,
 {
        mpd_ReturnElement *ret = malloc(sizeof(mpd_ReturnElement));
 
-       ret->name = strdup(name);
-       ret->value = strdup(value);
+       ret->name = strndup(name, text_buffer_size);
+       ret->value = strndup(value, text_buffer_size);
 
        return ret;
 }
@@ -354,6 +371,7 @@ mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
        connection->commandList = 0;
        connection->listOks = 0;
        connection->doneListOk = 0;
+       connection->sock = -1;
        connection->returnElement = NULL;
        connection->request = NULL;
 
@@ -403,7 +421,7 @@ mpd_Connection *mpd_newConnection(const char *host, int port, float timeout)
        }
 
        *rt = '\0';
-       output = strdup(connection->buffer);
+       output = strndup(connection->buffer, text_buffer_size);
        strcpy(connection->buffer, rt + 1);
        connection->buflen = strlen(connection->buffer);
 
@@ -435,12 +453,12 @@ void mpd_closeConnection(mpd_Connection *connection)
        WSACleanup();
 }
 
-static void mpd_executeCommand(mpd_Connection *connection, char *command)
+static void mpd_executeCommand(mpd_Connection *connection, const char *command)
 {
        int ret;
        struct timeval tv;
        fd_set fds;
-       char *commandPtr = command;
+       const char *commandPtr = command;
        int commandLen = strlen(command);
 
        if (!connection->doneProcessing && !connection->commandList) {
@@ -756,7 +774,7 @@ mpd_Status *mpd_getStatus(mpd_Connection *connection)
                                status->totalTime = atoi(tok + 1);
                        }
                } else if (strcmp(re->name, "error") == 0) {
-                       status->error = strdup(re->value);
+                       status->error = strndup(re->value, text_buffer_size);
                } else if (strcmp(re->name, "xfade") == 0) {
                        status->crossfade = atoi(re->value);
                } else if (strcmp(re->name, "updating_db") == 0) {
@@ -1006,37 +1024,37 @@ mpd_Song *mpd_songDup(mpd_Song *song)
        mpd_Song *ret = mpd_newSong();
 
        if (song->file) {
-               ret->file = strdup(song->file);
+               ret->file = strndup(song->file, text_buffer_size);
        }
        if (song->artist) {
-               ret->artist = strdup(song->artist);
+               ret->artist = strndup(song->artist, text_buffer_size);
        }
        if (song->album) {
-               ret->album = strdup(song->album);
+               ret->album = strndup(song->album, text_buffer_size);
        }
        if (song->title) {
-               ret->title = strdup(song->title);
+               ret->title = strndup(song->title, text_buffer_size);
        }
        if (song->track) {
-               ret->track = strdup(song->track);
+               ret->track = strndup(song->track, text_buffer_size);
        }
        if (song->name) {
-               ret->name = strdup(song->name);
+               ret->name = strndup(song->name, text_buffer_size);
        }
        if (song->date) {
-               ret->date = strdup(song->date);
+               ret->date = strndup(song->date, text_buffer_size);
        }
        if (song->genre) {
-               ret->genre = strdup(song->genre);
+               ret->genre = strndup(song->genre, text_buffer_size);
        }
        if (song->composer) {
-               ret->composer = strdup(song->composer);
+               ret->composer = strndup(song->composer, text_buffer_size);
        }
        if (song->disc) {
-               ret->disc = strdup(song->disc);
+               ret->disc = strndup(song->disc, text_buffer_size);
        }
        if (song->comment) {
-               ret->comment = strdup(song->comment);
+               ret->comment = strndup(song->comment, text_buffer_size);
        }
        ret->time = song->time;
        ret->pos = song->pos;
@@ -1078,7 +1096,7 @@ mpd_Directory *mpd_directoryDup(mpd_Directory *directory)
        mpd_Directory *ret = mpd_newDirectory();
 
        if (directory->path) {
-               ret->path = strdup(directory->path);
+               ret->path = strndup(directory->path, text_buffer_size);
        }
 
        return ret;
@@ -1116,7 +1134,7 @@ mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist)
        mpd_PlaylistFile *ret = mpd_newPlaylistFile();
 
        if (playlist->path) {
-               ret->path = strdup(playlist->path);
+               ret->path = strndup(playlist->path, text_buffer_size);
        }
 
        return ret;
@@ -1178,19 +1196,19 @@ mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
                        entity = mpd_newInfoEntity();
                        entity->type = MPD_INFO_ENTITY_TYPE_SONG;
                        entity->info.song = mpd_newSong();
-                       entity->info.song->file = strdup(connection->returnElement->value);
+                       entity->info.song->file = strndup(connection->returnElement->value, text_buffer_size);
                } else if (strcmp(connection->returnElement->name, "directory") == 0) {
                        entity = mpd_newInfoEntity();
                        entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
                        entity->info.directory = mpd_newDirectory();
                        entity->info.directory->path =
-                               strdup(connection->returnElement->value);
+                               strndup(connection->returnElement->value, text_buffer_size);
                } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
                        entity = mpd_newInfoEntity();
                        entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
                        entity->info.playlistFile = mpd_newPlaylistFile();
                        entity->info.playlistFile->path =
-                               strdup(connection->returnElement->value);
+                               strndup(connection->returnElement->value, text_buffer_size);
                } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
                        entity = mpd_newInfoEntity();
                        entity->type = MPD_INFO_ENTITY_TYPE_SONG;
@@ -1222,19 +1240,19 @@ mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
                if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && strlen(re->value)) {
                        if (!entity->info.song->artist
                                        && strcmp(re->name, "Artist") == 0) {
-                               entity->info.song->artist = strdup(re->value);
+                               entity->info.song->artist = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->album
                                        && strcmp(re->name, "Album") == 0) {
-                               entity->info.song->album = strdup(re->value);
+                               entity->info.song->album = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->title
                                        && strcmp(re->name, "Title") == 0) {
-                               entity->info.song->title = strdup(re->value);
+                               entity->info.song->title = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->track
                                        && strcmp(re->name, "Track") == 0) {
-                               entity->info.song->track = strdup(re->value);
+                               entity->info.song->track = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->name
                                        && strcmp(re->name, "Name") == 0) {
-                               entity->info.song->name = strdup(re->value);
+                               entity->info.song->name = strndup(re->value, text_buffer_size);
                        } else if (entity->info.song->time == MPD_SONG_NO_TIME
                                        && strcmp(re->name, "Time") == 0) {
                                entity->info.song->time = atoi(re->value);
@@ -1246,22 +1264,22 @@ mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection)
                                entity->info.song->id = atoi(re->value);
                        } else if (!entity->info.song->date
                                        && strcmp(re->name, "Date") == 0) {
-                               entity->info.song->date = strdup(re->value);
+                               entity->info.song->date = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->genre
                                        && strcmp(re->name, "Genre") == 0) {
-                               entity->info.song->genre = strdup(re->value);
+                               entity->info.song->genre = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->composer
                                        && strcmp(re->name, "Composer") == 0) {
-                               entity->info.song->composer = strdup(re->value);
+                               entity->info.song->composer = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->performer
                                        && strcmp(re->name, "Performer") == 0) {
-                               entity->info.song->performer = strdup(re->value);
+                               entity->info.song->performer = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->disc
                                        && strcmp(re->name, "Disc") == 0) {
-                               entity->info.song->disc = strdup(re->value);
+                               entity->info.song->disc = strndup(re->value, text_buffer_size);
                        } else if (!entity->info.song->comment
                                        && strcmp(re->name, "Comment") == 0) {
-                               entity->info.song->comment = strdup(re->value);
+                               entity->info.song->comment = strndup(re->value, text_buffer_size);
                        }
                } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
                } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
@@ -1286,7 +1304,7 @@ static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
                mpd_ReturnElement *re = connection->returnElement;
 
                if (strcmp(re->name, name) == 0) {
-                       return strdup(re->value);
+                       return strndup(re->value, text_buffer_size);
                }
                mpd_getNextReturnElement(connection);
        }
@@ -1640,22 +1658,22 @@ void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2)
        free(string);
 }
 
-void mpd_sendSeekCommand(mpd_Connection *connection, int song, int time)
+void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time)
 {
        int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
        char *string = malloc(len);
 
-       snprintf(string, len, "seek \"%i\" \"%i\"\n", song, time);
+       snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
        mpd_sendInfoCommand(connection, string);
        free(string);
 }
 
-void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int time)
+void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time)
 {
        int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
        char *string = malloc(len);
 
-       snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, time);
+       snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
        mpd_sendInfoCommand(connection, string);
        free(string);
 }
@@ -1823,7 +1841,7 @@ mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection)
                        }
                        output->id = atoi(re->value);
                } else if (strcmp(re->name, "outputname") == 0) {
-                       output->name = strdup(re->value);
+                       output->name = strndup(re->value, text_buffer_size);
                } else if (strcmp(re->name, "outputenabled") == 0) {
                        output->enabled = atoi(re->value);
                }
@@ -1911,9 +1929,9 @@ void mpd_startSearch(mpd_Connection *connection, int exact)
        }
 
        if (exact) {
-               connection->request = strdup("find");
+               connection->request = strndup("find", text_buffer_size);
        } else {
-               connection->request = strdup("search");
+               connection->request = strndup("search", text_buffer_size);
        }
 }
 
@@ -1925,7 +1943,7 @@ void mpd_startStatsSearch(mpd_Connection *connection)
                return;
        }
 
-       connection->request = strdup("count");
+       connection->request = strndup("count", text_buffer_size);
 }
 
 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
@@ -1937,15 +1955,15 @@ void mpd_startPlaylistSearch(mpd_Connection *connection, int exact)
        }
 
        if (exact) {
-               connection->request = strdup("playlistfind");
+               connection->request = strndup("playlistfind", text_buffer_size);
        } else {
-               connection->request = strdup("playlistsearch");
+               connection->request = strndup("playlistsearch", text_buffer_size);
        }
 }
 
 void mpd_startFieldSearch(mpd_Connection *connection, int type)
 {
-       char *strtype;
+       const char *strtype;
        int len;
 
        if (connection->request) {
@@ -1972,7 +1990,7 @@ void mpd_startFieldSearch(mpd_Connection *connection, int type)
 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
                const char *name)
 {
-       char *strtype;
+       const char *strtype;
        char *arg;
        int len;
        char *string;
@@ -1995,7 +2013,7 @@ void mpd_addConstraintSearch(mpd_Connection *connection, int type,
                return;
        }
 
-       string = strdup(connection->request);
+       string = strndup(connection->request, text_buffer_size);
        strtype = mpdTagItemKeys[type];
        arg = mpd_sanitizeArg(name);