Commit 4cf5d04c authored by Eric Wong's avatar Eric Wong

interface/connection malloc reductions from mpd-ke

This patch massively reduces the amount of heap allocations at the interface/command layer. Most commands with minimal output should not allocate memory from the heap at all. Things like repeatedly polling status, currentsong, and volume changes should be faster as a result, and more importantly, not a source of memory fragmentation. These changes should be safe in that there's no way for a remote-client to corrupt memory or otherwise do bad stuff to MPD, but an extra set of eyes to review would be good. Of course there's never any warranty :) No longer do we use FILE * structures in the interface, which means we don't have to allocate any new memory for most connections. Now, before you go on about losing the buffering that FILE * +implies+, remember that myfprintf() never took advantage of any of the stdio buffering features. To reduce the diff and make bugs easier to spot in the diff, I've kept myfprintf in places where we write to files (and not network interfaces). Expect myfprintf to go away entirely soon (we'll use fprintf for writing regular files). git-svn-id: https://svn.musicpd.org/mpd/trunk@4483 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 4d5b8509
...@@ -35,6 +35,7 @@ mpd_headers = \ ...@@ -35,6 +35,7 @@ mpd_headers = \
dbUtils.h \ dbUtils.h \
decode.h \ decode.h \
directory.h \ directory.h \
gcc.h \
inputPlugin.h \ inputPlugin.h \
inputPlugins/_flac_common.h \ inputPlugins/_flac_common.h \
inputPlugins/_ogg_common.h \ inputPlugins/_ogg_common.h \
...@@ -61,6 +62,7 @@ mpd_headers = \ ...@@ -61,6 +62,7 @@ mpd_headers = \
replayGain.h \ replayGain.h \
signal_check.h \ signal_check.h \
sig_handlers.h \ sig_handlers.h \
sllist.h \
song.h \ song.h \
stats.h \ stats.h \
tag.h \ tag.h \
...@@ -107,6 +109,7 @@ mpd_SOURCES = \ ...@@ -107,6 +109,7 @@ mpd_SOURCES = \
replayGain.c \ replayGain.c \
sig_handlers.c \ sig_handlers.c \
signal_check.c \ signal_check.c \
sllist.c \
song.c \ song.c \
stats.c \ stats.c \
tag.c \ tag.c \
......
...@@ -423,10 +423,10 @@ void sendMetadataToAudioDevice(MpdTag * tag) ...@@ -423,10 +423,10 @@ void sendMetadataToAudioDevice(MpdTag * tag)
} }
} }
int enableAudioDevice(FILE * fp, int device) int enableAudioDevice(int fd, int device)
{ {
if (device < 0 || device >= audioOutputArraySize) { if (device < 0 || device >= audioOutputArraySize) {
commandError(fp, ACK_ERROR_ARG, "audio output device id %i " commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
"doesn't exist\n", device); "doesn't exist\n", device);
return -1; return -1;
} }
...@@ -436,10 +436,10 @@ int enableAudioDevice(FILE * fp, int device) ...@@ -436,10 +436,10 @@ int enableAudioDevice(FILE * fp, int device)
return 0; return 0;
} }
int disableAudioDevice(FILE * fp, int device) int disableAudioDevice(int fd, int device)
{ {
if (device < 0 || device >= audioOutputArraySize) { if (device < 0 || device >= audioOutputArraySize) {
commandError(fp, ACK_ERROR_ARG, "audio output device id %i " commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
"doesn't exist\n", device); "doesn't exist\n", device);
return -1; return -1;
} }
...@@ -449,15 +449,16 @@ int disableAudioDevice(FILE * fp, int device) ...@@ -449,15 +449,16 @@ int disableAudioDevice(FILE * fp, int device)
return 0; return 0;
} }
void printAudioDevices(FILE * fp) void printAudioDevices(int fd)
{ {
int i; int i;
for (i = 0; i < audioOutputArraySize; i++) { for (i = 0; i < audioOutputArraySize; i++) {
myfprintf(fp, "outputid: %i\n", i); fdprintf(fd,
myfprintf(fp, "outputname: %s\n", audioOutputArray[i]->name); "outputid: %i\noutputname: %s\noutputenabled: %i\n",
myfprintf(fp, "outputenabled: %i\n", i,
(int)pdAudioDevicesEnabled[i]); audioOutputArray[i]->name,
(int)pdAudioDevicesEnabled[i]);
} }
} }
......
...@@ -69,11 +69,11 @@ void sendMetadataToAudioDevice(MpdTag * tag); ...@@ -69,11 +69,11 @@ void sendMetadataToAudioDevice(MpdTag * tag);
/* these functions are called in the main parent process while the child /* these functions are called in the main parent process while the child
process is busy playing to the audio */ process is busy playing to the audio */
int enableAudioDevice(FILE * fp, int device); int enableAudioDevice(int fd, int device);
int disableAudioDevice(FILE * fp, int device); int disableAudioDevice(int fd, int device);
void printAudioDevices(FILE * fp); void printAudioDevices(int fd);
void readAudioDevicesState(); void readAudioDevicesState();
......
...@@ -111,9 +111,9 @@ ...@@ -111,9 +111,9 @@
typedef struct _CommandEntry CommandEntry; typedef struct _CommandEntry CommandEntry;
typedef int (*CommandHandlerFunction) (FILE *, int *, int, char **); typedef int (*CommandHandlerFunction) (int, int *, int, char **);
typedef int (*CommandListHandlerFunction) typedef int (*CommandListHandlerFunction)
(FILE *, int *, int, char **, ListNode *, CommandEntry *); (int, int *, int, char **, struct strnode *, CommandEntry *);
/* if min: -1 don't check args * /* if min: -1 don't check args *
* if max: -1 no max args */ * if max: -1 no max args */
...@@ -131,7 +131,7 @@ int command_listNum = 0; ...@@ -131,7 +131,7 @@ int command_listNum = 0;
static CommandEntry *getCommandEntryFromString(char *string, int *permission); static CommandEntry *getCommandEntryFromString(char *string, int *permission);
List *commandList; static List *commandList;
CommandEntry *newCommandEntry(void) CommandEntry *newCommandEntry(void)
{ {
...@@ -163,14 +163,14 @@ static void addCommand(char *name, ...@@ -163,14 +163,14 @@ static void addCommand(char *name,
insertInList(commandList, cmd->cmd, cmd); insertInList(commandList, cmd->cmd, cmd);
} }
static int handleUrlHandlers(FILE * fp, int *permission, int argc, static int handleUrlHandlers(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return printRemoteUrlHandlers(fp); return printRemoteUrlHandlers(fd);
} }
static int handlePlay(FILE * fp, int *permission, int argc, static int handlePlay(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int song = -1; int song = -1;
char *test; char *test;
...@@ -178,16 +178,16 @@ static int handlePlay(FILE * fp, int *permission, int argc, ...@@ -178,16 +178,16 @@ static int handlePlay(FILE * fp, int *permission, int argc,
if (argc == 2) { if (argc == 2) {
song = strtol(argv[1], &test, 10); song = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
} }
return playPlaylist(fp, song, 0); return playPlaylist(fd, song, 0);
} }
static int handlePlayId(FILE * fp, int *permission, int argc, static int handlePlayId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int id = -1; int id = -1;
char *test; char *test;
...@@ -195,49 +195,49 @@ static int handlePlayId(FILE * fp, int *permission, int argc, ...@@ -195,49 +195,49 @@ static int handlePlayId(FILE * fp, int *permission, int argc,
if (argc == 2) { if (argc == 2) {
id = strtol(argv[1], &test, 10); id = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
} }
return playPlaylistById(fp, id, 0); return playPlaylistById(fd, id, 0);
} }
static int handleStop(FILE * fp, int *permission, int argc, static int handleStop(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return stopPlaylist(fp); return stopPlaylist(fd);
} }
static int handleCurrentSong(FILE * fp, int *permission, int argc, static int handleCurrentSong(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int song = getPlaylistCurrentSong(); int song = getPlaylistCurrentSong();
if (song >= 0) { if (song >= 0) {
return playlistInfo(fp, song); return playlistInfo(fd, song);
} else } else
return 0; return 0;
} }
static int handlePause(FILE * fp, int *permission, static int handlePause(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
if (argc == 2) { if (argc == 2) {
char *test; char *test;
int pause = strtol(argv[1], &test, 10); int pause = strtol(argv[1], &test, 10);
if (*test != '\0' || (pause != 0 && pause != 1)) { if (*test != '\0' || (pause != 0 && pause != 1)) {
commandError(fp, ACK_ERROR_ARG, "\"%s\" is not 0 or 1", commandError(fd, ACK_ERROR_ARG, "\"%s\" is not 0 or 1",
argv[1]); argv[1]);
return -1; return -1;
} }
return playerSetPause(fp, pause); return playerSetPause(fd, pause);
} }
return playerPause(fp); return playerPause(fd);
} }
static int commandStatus(FILE * fp, int *permission, int argc, static int commandStatus(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
char *state = NULL; char *state = NULL;
int updateJobId; int updateJobId;
...@@ -257,204 +257,204 @@ static int commandStatus(FILE * fp, int *permission, int argc, ...@@ -257,204 +257,204 @@ static int commandStatus(FILE * fp, int *permission, int argc,
break; break;
} }
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_VOLUME, getVolumeLevel()); fdprintf(fd, "%s: %i\n", COMMAND_STATUS_VOLUME, getVolumeLevel());
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_REPEAT, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_REPEAT,
getPlaylistRepeatStatus()); getPlaylistRepeatStatus());
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_RANDOM, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_RANDOM,
getPlaylistRandomStatus()); getPlaylistRandomStatus());
myfprintf(fp, "%s: %li\n", COMMAND_STATUS_PLAYLIST, fdprintf(fd, "%s: %li\n", COMMAND_STATUS_PLAYLIST,
getPlaylistVersion()); getPlaylistVersion());
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_PLAYLIST_LENGTH, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_PLAYLIST_LENGTH,
getPlaylistLength()); getPlaylistLength());
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_CROSSFADE, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_CROSSFADE,
(int)(getPlayerCrossFade() + 0.5)); (int)(getPlayerCrossFade() + 0.5));
myfprintf(fp, "%s: %s\n", COMMAND_STATUS_STATE, state); fdprintf(fd, "%s: %s\n", COMMAND_STATUS_STATE, state);
song = getPlaylistCurrentSong(); song = getPlaylistCurrentSong();
if (song >= 0) { if (song >= 0) {
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_SONG, song); fdprintf(fd, "%s: %i\n", COMMAND_STATUS_SONG, song);
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_SONGID, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_SONGID,
getPlaylistSongId(song)); getPlaylistSongId(song));
} }
if (getPlayerState() != PLAYER_STATE_STOP) { if (getPlayerState() != PLAYER_STATE_STOP) {
myfprintf(fp, "%s: %i:%i\n", COMMAND_STATUS_TIME, fdprintf(fd, "%s: %i:%i\n", COMMAND_STATUS_TIME,
getPlayerElapsedTime(), getPlayerTotalTime()); getPlayerElapsedTime(), getPlayerTotalTime());
myfprintf(fp, "%s: %li\n", COMMAND_STATUS_BITRATE, fdprintf(fd, "%s: %li\n", COMMAND_STATUS_BITRATE,
getPlayerBitRate(), getPlayerTotalTime()); getPlayerBitRate(), getPlayerTotalTime());
myfprintf(fp, "%s: %u:%i:%i\n", COMMAND_STATUS_AUDIO, fdprintf(fd, "%s: %u:%i:%i\n", COMMAND_STATUS_AUDIO,
getPlayerSampleRate(), getPlayerBits(), getPlayerSampleRate(), getPlayerBits(),
getPlayerChannels()); getPlayerChannels());
} }
if ((updateJobId = isUpdatingDB())) { if ((updateJobId = isUpdatingDB())) {
myfprintf(fp, "%s: %i\n", COMMAND_STATUS_UPDATING_DB, fdprintf(fd, "%s: %i\n", COMMAND_STATUS_UPDATING_DB,
updateJobId); updateJobId);
} }
if (getPlayerError() != PLAYER_ERROR_NOERROR) { if (getPlayerError() != PLAYER_ERROR_NOERROR) {
myfprintf(fp, "%s: %s\n", COMMAND_STATUS_ERROR, fdprintf(fd, "%s: %s\n", COMMAND_STATUS_ERROR,
getPlayerErrorStr()); getPlayerErrorStr());
} }
return 0; return 0;
} }
static int handleKill(FILE * fp, int *permission, int argc, static int handleKill(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return COMMAND_RETURN_KILL; return COMMAND_RETURN_KILL;
} }
static int handleClose(FILE * fp, int *permission, int argc, static int handleClose(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return COMMAND_RETURN_CLOSE; return COMMAND_RETURN_CLOSE;
} }
static int handleAdd(FILE * fp, int *permission, int argc, static int handleAdd(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
char *path = argv[1]; char *path = argv[1];
if (isRemoteUrl(path)) if (isRemoteUrl(path))
return addToPlaylist(fp, path, 0); return addToPlaylist(fd, path, 0);
return addAllIn(fp, path); return addAllIn(fd, path);
} }
static int handleAddId(FILE * fp, int *permission, int argc, static int handleAddId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return addToPlaylist(fp, argv[1], 1); return addToPlaylist(fd, argv[1], 1);
} }
static int handleDelete(FILE * fp, int *permission, int argc, static int handleDelete(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int song; int song;
char *test; char *test;
song = strtol(argv[1], &test, 10); song = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
return deleteFromPlaylist(fp, song); return deleteFromPlaylist(fd, song);
} }
static int handleDeleteId(FILE * fp, int *permission, int argc, static int handleDeleteId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int id; int id;
char *test; char *test;
id = strtol(argv[1], &test, 10); id = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
return deleteFromPlaylistById(fp, id); return deleteFromPlaylistById(fd, id);
} }
static int handlePlaylist(FILE * fp, int *permission, int argc, static int handlePlaylist(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return showPlaylist(fp); return showPlaylist(fd);
} }
static int handleShuffle(FILE * fp, int *permission, int argc, static int handleShuffle(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return shufflePlaylist(fp); return shufflePlaylist(fd);
} }
static int handleClear(FILE * fp, int *permission, int argc, static int handleClear(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return clearPlaylist(fp); return clearPlaylist(fd);
} }
static int handleSave(FILE * fp, int *permission, int argc, static int handleSave(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return savePlaylist(fp, argv[1]); return savePlaylist(fd, argv[1]);
} }
static int handleLoad(FILE * fp, int *permission, int argc, static int handleLoad(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return loadPlaylist(fp, argv[1]); return loadPlaylist(fd, argv[1]);
} }
static int handleListPlaylist(FILE * fp, int *permission, int argc, static int handleListPlaylist(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return PlaylistInfo(fp, argv[1], 0); return PlaylistInfo(fd, argv[1], 0);
} }
static int handleListPlaylistInfo(FILE * fp, int *permission, static int handleListPlaylistInfo(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
return PlaylistInfo(fp, argv[1], 1); return PlaylistInfo(fd, argv[1], 1);
} }
static int handleLsInfo(FILE * fp, int *permission, int argc, static int handleLsInfo(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
if (argc == 1) { if (argc == 1) {
if (printDirectoryInfo(fp, NULL) < 0) if (printDirectoryInfo(fd, NULL) < 0)
return -1; return -1;
else else
return lsPlaylists(fp, ""); return lsPlaylists(fd, "");
} else { } else {
if (printDirectoryInfo(fp, argv[1]) < 0) if (printDirectoryInfo(fd, argv[1]) < 0)
return -1; return -1;
else else
return lsPlaylists(fp, argv[1]); return lsPlaylists(fd, argv[1]);
} }
} }
static int handleRm(FILE * fp, int *permission, int argc, static int handleRm(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return deletePlaylist(fp, argv[1]); return deletePlaylist(fd, argv[1]);
} }
static int handlePlaylistChanges(FILE * fp, int *permission, static int handlePlaylistChanges(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
unsigned long version; unsigned long version;
char *test; char *test;
version = strtoul(argv[1], &test, 10); version = strtoul(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need a positive integer", commandError(fd, ACK_ERROR_ARG, "need a positive integer",
NULL); NULL);
return -1; return -1;
} }
return playlistChanges(fp, version); return playlistChanges(fd, version);
} }
static int handlePlaylistChangesPosId(FILE * fp, int *permission, static int handlePlaylistChangesPosId(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
unsigned long version; unsigned long version;
char *test; char *test;
version = strtoul(argv[1], &test, 10); version = strtoul(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need a positive integer", commandError(fd, ACK_ERROR_ARG, "need a positive integer",
NULL); NULL);
return -1; return -1;
} }
return playlistChangesPosId(fp, version); return playlistChangesPosId(fd, version);
} }
static int handlePlaylistInfo(FILE * fp, int *permission, static int handlePlaylistInfo(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
int song = -1; int song = -1;
char *test; char *test;
...@@ -462,16 +462,16 @@ static int handlePlaylistInfo(FILE * fp, int *permission, ...@@ -462,16 +462,16 @@ static int handlePlaylistInfo(FILE * fp, int *permission,
if (argc == 2) { if (argc == 2) {
song = strtol(argv[1], &test, 10); song = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
} }
return playlistInfo(fp, song); return playlistInfo(fd, song);
} }
static int handlePlaylistId(FILE * fp, int *permission, static int handlePlaylistId(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
int id = -1; int id = -1;
char *test; char *test;
...@@ -479,16 +479,16 @@ static int handlePlaylistId(FILE * fp, int *permission, ...@@ -479,16 +479,16 @@ static int handlePlaylistId(FILE * fp, int *permission,
if (argc == 2) { if (argc == 2) {
id = strtol(argv[1], &test, 10); id = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"need a positive integer", NULL); "need a positive integer", NULL);
return -1; return -1;
} }
} }
return playlistId(fp, id); return playlistId(fd, id);
} }
static int handleFind(FILE * fp, int *permission, int argc, static int handleFind(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int ret; int ret;
...@@ -498,19 +498,19 @@ static int handleFind(FILE * fp, int *permission, int argc, ...@@ -498,19 +498,19 @@ static int handleFind(FILE * fp, int *permission, int argc,
&items); &items);
if (numItems <= 0) { if (numItems <= 0) {
commandError(fp, ACK_ERROR_ARG, "incorrect arguments", NULL); commandError(fd, ACK_ERROR_ARG, "incorrect arguments", NULL);
return -1; return -1;
} }
ret = findSongsIn(fp, NULL, numItems, items); ret = findSongsIn(fd, NULL, numItems, items);
freeLocateTagItemArray(numItems, items); freeLocateTagItemArray(numItems, items);
return ret; return ret;
} }
static int handleSearch(FILE * fp, int *permission, int argc, static int handleSearch(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int ret; int ret;
...@@ -520,26 +520,26 @@ static int handleSearch(FILE * fp, int *permission, int argc, ...@@ -520,26 +520,26 @@ static int handleSearch(FILE * fp, int *permission, int argc,
&items); &items);
if (numItems <= 0) { if (numItems <= 0) {
commandError(fp, ACK_ERROR_ARG, "incorrect arguments", NULL); commandError(fd, ACK_ERROR_ARG, "incorrect arguments", NULL);
return -1; return -1;
} }
ret = searchForSongsIn(fp, NULL, numItems, items); ret = searchForSongsIn(fd, NULL, numItems, items);
freeLocateTagItemArray(numItems, items); freeLocateTagItemArray(numItems, items);
return ret; return ret;
} }
static int listHandleUpdate(FILE * fp, static int listHandleUpdate(int fd,
int *permission, int *permission,
int argc, int argc,
char **argv, char *argv[],
ListNode * commandNode, CommandEntry * cmd) struct strnode *cmdnode, CommandEntry *cmd)
{ {
static List *pathList = NULL; static List *pathList = NULL;
CommandEntry *nextCmd = NULL; CommandEntry *nextCmd = NULL;
ListNode *nextNode = commandNode->nextNode;; struct strnode *next = cmdnode->next;
if (!pathList) if (!pathList)
pathList = makeList(NULL, 1); pathList = makeList(NULL, 1);
...@@ -549,13 +549,11 @@ static int listHandleUpdate(FILE * fp, ...@@ -549,13 +549,11 @@ static int listHandleUpdate(FILE * fp,
else else
insertInList(pathList, "", NULL); insertInList(pathList, "", NULL);
if (nextNode) { if(next)
nextCmd = getCommandEntryFromString((void *)nextNode->data, nextCmd = getCommandEntryFromString(next->data, permission);
permission);
}
if (cmd != nextCmd) { if (cmd != nextCmd) {
int ret = updateInit(fp, pathList); int ret = updateInit(fd, pathList);
freeList(pathList); freeList(pathList);
pathList = NULL; pathList = NULL;
return ret; return ret;
...@@ -564,113 +562,113 @@ static int listHandleUpdate(FILE * fp, ...@@ -564,113 +562,113 @@ static int listHandleUpdate(FILE * fp,
return 0; return 0;
} }
static int handleUpdate(FILE * fp, int *permission, int argc, static int handleUpdate(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
if (argc == 2) { if (argc == 2) {
int ret; int ret;
List *pathList = makeList(NULL, 1); List *pathList = makeList(NULL, 1);
insertInList(pathList, argv[1], NULL); insertInList(pathList, argv[1], NULL);
ret = updateInit(fp, pathList); ret = updateInit(fd, pathList);
freeList(pathList); freeList(pathList);
return ret; return ret;
} }
return updateInit(fp, NULL); return updateInit(fd, NULL);
} }
static int handleNext(FILE * fp, int *permission, int argc, static int handleNext(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return nextSongInPlaylist(fp); return nextSongInPlaylist(fd);
} }
static int handlePrevious(FILE * fp, int *permission, int argc, static int handlePrevious(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return previousSongInPlaylist(fp); return previousSongInPlaylist(fd);
} }
static int handleListAll(FILE * fp, int *permission, int argc, static int handleListAll(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
char *directory = NULL; char *directory = NULL;
if (argc == 2) if (argc == 2)
directory = argv[1]; directory = argv[1];
return printAllIn(fp, directory); return printAllIn(fd, directory);
} }
static int handleVolume(FILE * fp, int *permission, int argc, static int handleVolume(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int change; int change;
char *test; char *test;
change = strtol(argv[1], &test, 10); change = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need an integer", NULL); commandError(fd, ACK_ERROR_ARG, "need an integer", NULL);
return -1; return -1;
} }
return changeVolumeLevel(fp, change, 1); return changeVolumeLevel(fd, change, 1);
} }
static int handleSetVol(FILE * fp, int *permission, int argc, static int handleSetVol(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int level; int level;
char *test; char *test;
level = strtol(argv[1], &test, 10); level = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need an integer", NULL); commandError(fd, ACK_ERROR_ARG, "need an integer", NULL);
return -1; return -1;
} }
return changeVolumeLevel(fp, level, 0); return changeVolumeLevel(fd, level, 0);
} }
static int handleRepeat(FILE * fp, int *permission, int argc, static int handleRepeat(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int status; int status;
char *test; char *test;
status = strtol(argv[1], &test, 10); status = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need an integer", NULL); commandError(fd, ACK_ERROR_ARG, "need an integer", NULL);
return -1; return -1;
} }
return setPlaylistRepeatStatus(fp, status); return setPlaylistRepeatStatus(fd, status);
} }
static int handleRandom(FILE * fp, int *permission, int argc, static int handleRandom(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int status; int status;
char *test; char *test;
status = strtol(argv[1], &test, 10); status = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "need an integer", NULL); commandError(fd, ACK_ERROR_ARG, "need an integer", NULL);
return -1; return -1;
} }
return setPlaylistRandomStatus(fp, status); return setPlaylistRandomStatus(fd, status);
} }
static int handleStats(FILE * fp, int *permission, int argc, static int handleStats(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return printStats(fp); return printStats(fd);
} }
static int handleClearError(FILE * fp, int *permission, int argc, static int handleClearError(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
clearPlayerError(); clearPlayerError();
return 0; return 0;
} }
static int handleList(FILE * fp, int *permission, int argc, static int handleList(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int numConditionals = 0; int numConditionals = 0;
LocateTagItem *conditionals = NULL; LocateTagItem *conditionals = NULL;
...@@ -678,7 +676,7 @@ static int handleList(FILE * fp, int *permission, int argc, ...@@ -678,7 +676,7 @@ static int handleList(FILE * fp, int *permission, int argc,
int ret; int ret;
if (tagType < 0) { if (tagType < 0) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not known", argv[1]); "\"%s\" is not known", argv[1]);
return -1; return -1;
} }
...@@ -686,7 +684,7 @@ static int handleList(FILE * fp, int *permission, int argc, ...@@ -686,7 +684,7 @@ static int handleList(FILE * fp, int *permission, int argc,
/* for compatibility with < 0.12.0 */ /* for compatibility with < 0.12.0 */
if (argc == 3) { if (argc == 3) {
if (tagType != TAG_ITEM_ALBUM) { if (tagType != TAG_ITEM_ALBUM) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"should be \"%s\" for 3 arguments", "should be \"%s\" for 3 arguments",
mpdTagItemKeys[TAG_ITEM_ALBUM]); mpdTagItemKeys[TAG_ITEM_ALBUM]);
return -1; return -1;
...@@ -701,13 +699,13 @@ static int handleList(FILE * fp, int *permission, int argc, ...@@ -701,13 +699,13 @@ static int handleList(FILE * fp, int *permission, int argc,
&conditionals); &conditionals);
if (numConditionals < 0) { if (numConditionals < 0) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"not able to parse args", NULL); "not able to parse args", NULL);
return -1; return -1;
} }
} }
ret = listAllUniqueTags(fp, tagType, numConditionals, conditionals); ret = listAllUniqueTags(fd, tagType, numConditionals, conditionals);
if (conditionals) if (conditionals)
freeLocateTagItemArray(numConditionals, conditionals); freeLocateTagItemArray(numConditionals, conditionals);
...@@ -715,8 +713,8 @@ static int handleList(FILE * fp, int *permission, int argc, ...@@ -715,8 +713,8 @@ static int handleList(FILE * fp, int *permission, int argc,
return ret; return ret;
} }
static int handleMove(FILE * fp, int *permission, int argc, static int handleMove(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int from; int from;
int to; int to;
...@@ -724,21 +722,21 @@ static int handleMove(FILE * fp, int *permission, int argc, ...@@ -724,21 +722,21 @@ static int handleMove(FILE * fp, int *permission, int argc,
from = strtol(argv[1], &test, 10); from = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
to = strtol(argv[2], &test, 10); to = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[2]); "\"%s\" is not a integer", argv[2]);
return -1; return -1;
} }
return moveSongInPlaylist(fp, from, to); return moveSongInPlaylist(fd, from, to);
} }
static int handleMoveId(FILE * fp, int *permission, int argc, static int handleMoveId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int id; int id;
int to; int to;
...@@ -746,21 +744,21 @@ static int handleMoveId(FILE * fp, int *permission, int argc, ...@@ -746,21 +744,21 @@ static int handleMoveId(FILE * fp, int *permission, int argc,
id = strtol(argv[1], &test, 10); id = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
to = strtol(argv[2], &test, 10); to = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[2]); "\"%s\" is not a integer", argv[2]);
return -1; return -1;
} }
return moveSongInPlaylistById(fp, id, to); return moveSongInPlaylistById(fd, id, to);
} }
static int handleSwap(FILE * fp, int *permission, int argc, static int handleSwap(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int song1; int song1;
int song2; int song2;
...@@ -768,21 +766,21 @@ static int handleSwap(FILE * fp, int *permission, int argc, ...@@ -768,21 +766,21 @@ static int handleSwap(FILE * fp, int *permission, int argc,
song1 = strtol(argv[1], &test, 10); song1 = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
song2 = strtol(argv[2], &test, 10); song2 = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "\"%s\" is not a integer", commandError(fd, ACK_ERROR_ARG, "\"%s\" is not a integer",
argv[2]); argv[2]);
return -1; return -1;
} }
return swapSongsInPlaylist(fp, song1, song2); return swapSongsInPlaylist(fd, song1, song2);
} }
static int handleSwapId(FILE * fp, int *permission, int argc, static int handleSwapId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int id1; int id1;
int id2; int id2;
...@@ -790,21 +788,21 @@ static int handleSwapId(FILE * fp, int *permission, int argc, ...@@ -790,21 +788,21 @@ static int handleSwapId(FILE * fp, int *permission, int argc,
id1 = strtol(argv[1], &test, 10); id1 = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
id2 = strtol(argv[2], &test, 10); id2 = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, "\"%s\" is not a integer", commandError(fd, ACK_ERROR_ARG, "\"%s\" is not a integer",
argv[2]); argv[2]);
return -1; return -1;
} }
return swapSongsInPlaylistById(fp, id1, id2); return swapSongsInPlaylistById(fd, id1, id2);
} }
static int handleSeek(FILE * fp, int *permission, int argc, static int handleSeek(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int song; int song;
int time; int time;
...@@ -812,21 +810,21 @@ static int handleSeek(FILE * fp, int *permission, int argc, ...@@ -812,21 +810,21 @@ static int handleSeek(FILE * fp, int *permission, int argc,
song = strtol(argv[1], &test, 10); song = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
time = strtol(argv[2], &test, 10); time = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[2]); "\"%s\" is not a integer", argv[2]);
return -1; return -1;
} }
return seekSongInPlaylist(fp, song, time); return seekSongInPlaylist(fd, song, time);
} }
static int handleSeekId(FILE * fp, int *permission, int argc, static int handleSeekId(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int id; int id;
int time; int time;
...@@ -834,40 +832,40 @@ static int handleSeekId(FILE * fp, int *permission, int argc, ...@@ -834,40 +832,40 @@ static int handleSeekId(FILE * fp, int *permission, int argc,
id = strtol(argv[1], &test, 10); id = strtol(argv[1], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[1]); "\"%s\" is not a integer", argv[1]);
return -1; return -1;
} }
time = strtol(argv[2], &test, 10); time = strtol(argv[2], &test, 10);
if (*test != '\0') { if (*test != '\0') {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer", argv[2]); "\"%s\" is not a integer", argv[2]);
return -1; return -1;
} }
return seekSongInPlaylistById(fp, id, time); return seekSongInPlaylistById(fd, id, time);
} }
static int handleListAllInfo(FILE * fp, int *permission, int argc, static int handleListAllInfo(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
char *directory = NULL; char *directory = NULL;
if (argc == 2) if (argc == 2)
directory = argv[1]; directory = argv[1];
return printInfoForAllIn(fp, directory); return printInfoForAllIn(fd, directory);
} }
static int handlePing(FILE * fp, int *permission, int argc, static int handlePing(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
return 0; return 0;
} }
static int handlePassword(FILE * fp, int *permission, int argc, static int handlePassword(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
if (getPermissionFromPassword(argv[1], permission) < 0) { if (getPermissionFromPassword(argv[1], permission) < 0) {
commandError(fp, ACK_ERROR_PASSWORD, "incorrect password", commandError(fd, ACK_ERROR_PASSWORD, "incorrect password",
NULL); NULL);
return -1; return -1;
} }
...@@ -875,15 +873,15 @@ static int handlePassword(FILE * fp, int *permission, int argc, ...@@ -875,15 +873,15 @@ static int handlePassword(FILE * fp, int *permission, int argc,
return 0; return 0;
} }
static int handleCrossfade(FILE * fp, int *permission, int argc, static int handleCrossfade(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int time; int time;
char *test; char *test;
time = strtol(argv[1], &test, 10); time = strtol(argv[1], &test, 10);
if (*test != '\0' || time < 0) { if (*test != '\0' || time < 0) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer >= 0", argv[1]); "\"%s\" is not a integer >= 0", argv[1]);
return -1; return -1;
} }
...@@ -893,49 +891,49 @@ static int handleCrossfade(FILE * fp, int *permission, int argc, ...@@ -893,49 +891,49 @@ static int handleCrossfade(FILE * fp, int *permission, int argc,
return 0; return 0;
} }
static int handleEnableDevice(FILE * fp, int *permission, int argc, static int handleEnableDevice(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
int device; int device;
char *test; char *test;
device = strtol(argv[1], &test, 10); device = strtol(argv[1], &test, 10);
if (*test != '\0' || device < 0) { if (*test != '\0' || device < 0) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer >= 0", argv[1]); "\"%s\" is not a integer >= 0", argv[1]);
return -1; return -1;
} }
return enableAudioDevice(fp, device); return enableAudioDevice(fd, device);
} }
static int handleDisableDevice(FILE * fp, int *permission, static int handleDisableDevice(int fd, int *permission,
int argc, char **argv) int argc, char *argv[])
{ {
int device; int device;
char *test; char *test;
device = strtol(argv[1], &test, 10); device = strtol(argv[1], &test, 10);
if (*test != '\0' || device < 0) { if (*test != '\0' || device < 0) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"\"%s\" is not a integer >= 0", argv[1]); "\"%s\" is not a integer >= 0", argv[1]);
return -1; return -1;
} }
return disableAudioDevice(fp, device); return disableAudioDevice(fd, device);
} }
static int handleDevices(FILE * fp, int *permission, int argc, static int handleDevices(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
printAudioDevices(fp); printAudioDevices(fd);
return 0; return 0;
} }
/* don't be fooled, this is the command handler for "commands" command */ /* don't be fooled, this is the command handler for "commands" command */
static int handleCommands(FILE * fp, int *permission, int argc, static int handleCommands(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
ListNode *node = commandList->firstNode; ListNode *node = commandList->firstNode;
CommandEntry *cmd; CommandEntry *cmd;
...@@ -943,7 +941,7 @@ static int handleCommands(FILE * fp, int *permission, int argc, ...@@ -943,7 +941,7 @@ static int handleCommands(FILE * fp, int *permission, int argc,
while (node != NULL) { while (node != NULL) {
cmd = (CommandEntry *) node->data; cmd = (CommandEntry *) node->data;
if (cmd->reqPermission == (*permission & cmd->reqPermission)) { if (cmd->reqPermission == (*permission & cmd->reqPermission)) {
myfprintf(fp, "command: %s\n", cmd->cmd); fdprintf(fd, "command: %s\n", cmd->cmd);
} }
node = node->nextNode; node = node->nextNode;
...@@ -952,8 +950,8 @@ static int handleCommands(FILE * fp, int *permission, int argc, ...@@ -952,8 +950,8 @@ static int handleCommands(FILE * fp, int *permission, int argc,
return 0; return 0;
} }
static int handleNotcommands(FILE * fp, int *permission, int argc, static int handleNotcommands(int fd, int *permission, int argc,
char **argv) char *argv[])
{ {
ListNode *node = commandList->firstNode; ListNode *node = commandList->firstNode;
CommandEntry *cmd; CommandEntry *cmd;
...@@ -962,7 +960,7 @@ static int handleNotcommands(FILE * fp, int *permission, int argc, ...@@ -962,7 +960,7 @@ static int handleNotcommands(FILE * fp, int *permission, int argc,
cmd = (CommandEntry *) node->data; cmd = (CommandEntry *) node->data;
if (cmd->reqPermission != (*permission & cmd->reqPermission)) { if (cmd->reqPermission != (*permission & cmd->reqPermission)) {
myfprintf(fp, "command: %s\n", cmd->cmd); fdprintf(fd, "command: %s\n", cmd->cmd);
} }
node = node->nextNode; node = node->nextNode;
...@@ -1065,15 +1063,15 @@ void finishCommands(void) ...@@ -1065,15 +1063,15 @@ void finishCommands(void)
freeList(commandList); freeList(commandList);
} }
static int checkArgcAndPermission(CommandEntry * cmd, FILE * fp, static int checkArgcAndPermission(CommandEntry * cmd, int fd,
int permission, int argc, char **argv) int permission, int argc, char *argv[])
{ {
int min = cmd->min + 1; int min = cmd->min + 1;
int max = cmd->max + 1; int max = cmd->max + 1;
if (cmd->reqPermission != (permission & cmd->reqPermission)) { if (cmd->reqPermission != (permission & cmd->reqPermission)) {
if (fp) { if (fd) {
commandError(fp, ACK_ERROR_PERMISSION, commandError(fd, ACK_ERROR_PERMISSION,
"you don't have permission for \"%s\"", "you don't have permission for \"%s\"",
cmd->cmd); cmd->cmd);
} }
...@@ -1084,22 +1082,22 @@ static int checkArgcAndPermission(CommandEntry * cmd, FILE * fp, ...@@ -1084,22 +1082,22 @@ static int checkArgcAndPermission(CommandEntry * cmd, FILE * fp,
return 0; return 0;
if (min == max && max != argc) { if (min == max && max != argc) {
if (fp) { if (fd) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"wrong number of arguments for \"%s\"", "wrong number of arguments for \"%s\"",
argv[0]); argv[0]);
} }
return -1; return -1;
} else if (argc < min) { } else if (argc < min) {
if (fp) { if (fd) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"too few arguments for \"%s\"", "too few arguments for \"%s\"",
argv[0]); argv[0]);
} }
return -1; return -1;
} else if (argc > max && max /* != 0 */ ) { } else if (argc > max && max /* != 0 */ ) {
if (fp) { if (fd) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"too many arguments for \"%s\"", "too many arguments for \"%s\"",
argv[0]); argv[0]);
} }
...@@ -1108,11 +1106,10 @@ static int checkArgcAndPermission(CommandEntry * cmd, FILE * fp, ...@@ -1108,11 +1106,10 @@ static int checkArgcAndPermission(CommandEntry * cmd, FILE * fp,
return 0; return 0;
} }
static CommandEntry *getCommandEntryAndCheckArgcAndPermission(FILE * fp, static CommandEntry *getCommandEntryAndCheckArgcAndPermission(int fd,
int *permission, int *permission,
int int argc,
argc, char *argv[])
char **argv)
{ {
static char unknown[] = ""; static char unknown[] = "";
CommandEntry *cmd; CommandEntry *cmd;
...@@ -1123,8 +1120,8 @@ static CommandEntry *getCommandEntryAndCheckArgcAndPermission(FILE * fp, ...@@ -1123,8 +1120,8 @@ static CommandEntry *getCommandEntryAndCheckArgcAndPermission(FILE * fp,
return NULL; return NULL;
if (!findInList(commandList, argv[0], (void *)&cmd)) { if (!findInList(commandList, argv[0], (void *)&cmd)) {
if (fp) { if (fd) {
commandError(fp, ACK_ERROR_UNKNOWN, commandError(fd, ACK_ERROR_UNKNOWN,
"unknown command \"%s\"", argv[0]); "unknown command \"%s\"", argv[0]);
} }
return NULL; return NULL;
...@@ -1132,7 +1129,7 @@ static CommandEntry *getCommandEntryAndCheckArgcAndPermission(FILE * fp, ...@@ -1132,7 +1129,7 @@ static CommandEntry *getCommandEntryAndCheckArgcAndPermission(FILE * fp,
current_command = cmd->cmd; current_command = cmd->cmd;
if (checkArgcAndPermission(cmd, fp, *permission, argc, if (checkArgcAndPermission(cmd, fd, *permission, argc,
argv) < 0) { argv) < 0) {
return NULL; return NULL;
} }
...@@ -1149,14 +1146,14 @@ static CommandEntry *getCommandEntryFromString(char *string, int *permission) ...@@ -1149,14 +1146,14 @@ static CommandEntry *getCommandEntryFromString(char *string, int *permission)
if (0 == argc) if (0 == argc)
return NULL; return NULL;
cmd = getCommandEntryAndCheckArgcAndPermission(NULL, permission, cmd = getCommandEntryAndCheckArgcAndPermission(0, permission,
argc, argv); argc, argv);
return cmd; return cmd;
} }
static int processCommandInternal(FILE * fp, int *permission, static int processCommandInternal(int fd, int *permission,
char *commandString, ListNode * commandNode) char *commandString, struct strnode *cmdnode)
{ {
int argc; int argc;
char *argv[COMMAND_ARGV_MAX] = { 0 }; char *argv[COMMAND_ARGV_MAX] = { 0 };
...@@ -1168,13 +1165,13 @@ static int processCommandInternal(FILE * fp, int *permission, ...@@ -1168,13 +1165,13 @@ static int processCommandInternal(FILE * fp, int *permission,
if (argc == 0) if (argc == 0)
return 0; return 0;
if ((cmd = getCommandEntryAndCheckArgcAndPermission(fp, permission, if ((cmd = getCommandEntryAndCheckArgcAndPermission(fd, permission,
argc, argv))) { argc, argv))) {
if (NULL == commandNode || NULL == cmd->listHandler) { if (!cmdnode || !cmd->listHandler) {
ret = cmd->handler(fp, permission, argc, argv); ret = cmd->handler(fd, permission, argc, argv);
} else { } else {
ret = cmd->listHandler(fp, permission, argc, argv, ret = cmd->listHandler(fd, permission, argc, argv,
commandNode, cmd); cmdnode, cmd);
} }
} }
...@@ -1183,37 +1180,32 @@ static int processCommandInternal(FILE * fp, int *permission, ...@@ -1183,37 +1180,32 @@ static int processCommandInternal(FILE * fp, int *permission,
return ret; return ret;
} }
int processListOfCommands(FILE * fp, int *permission, int *expired, int processListOfCommands(int fd, int *permission, int *expired,
int listOK, List * list) int listOK, struct strnode *list)
{ {
ListNode *node = list->firstNode; struct strnode *cur = list;
ListNode *tempNode;
int ret = 0; int ret = 0;
command_listNum = 0; command_listNum = 0;
while (node != NULL) { while (cur) {
DEBUG("processListOfCommands: process command \"%s\"\n", DEBUG("processListOfCommands: process command \"%s\"\n",
node->data); cur->data);
ret = processCommandInternal(fp, permission, (char *)node->data, ret = processCommandInternal(fd, permission, cur->data, cur);
node);
DEBUG("processListOfCommands: command returned %i\n", ret); DEBUG("processListOfCommands: command returned %i\n", ret);
tempNode = node->nextNode;
deleteNodeFromList(list, node);
node = tempNode;
if (ret != 0 || (*expired) != 0) if (ret != 0 || (*expired) != 0)
node = NULL; goto out;
else if (listOK) else if (listOK)
myfprintf(fp, "list_OK\n"); fdprintf(fd, "list_OK\n");
command_listNum++; command_listNum++;
cur = cur->next;
} }
out:
command_listNum = 0; command_listNum = 0;
return ret; return ret;
} }
int processCommand(FILE * fp, int *permission, char *commandString) int processCommand(int fd, int *permission, char *commandString)
{ {
return processCommandInternal(fp, permission, commandString, NULL); return processCommandInternal(fd, permission, commandString, NULL);
} }
...@@ -25,7 +25,9 @@ ...@@ -25,7 +25,9 @@
#include "myfprintf.h" #include "myfprintf.h"
#include "log.h" #include "log.h"
#include "ack.h" #include "ack.h"
#include "sllist.h"
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#define COMMAND_RETURN_KILL 10 #define COMMAND_RETURN_KILL 10
...@@ -35,30 +37,29 @@ ...@@ -35,30 +37,29 @@
extern char *current_command; extern char *current_command;
extern int command_listNum; extern int command_listNum;
int processListOfCommands(FILE * fp, int *permission, int *expired, int processListOfCommands(int fd, int *permission, int *expired,
int listOK, List * list); int listOK, struct strnode *list);
int processCommand(FILE * fp, int *permission, char *commandString); int processCommand(int fd, int *permission, char *commandString);
void initCommands(); void initCommands();
void finishCommands(); void finishCommands();
#define commandSuccess(fp) myfprintf(fp, "OK\n") #define commandSuccess(fd) fdprintf(fd, "OK\n")
#define commandError(fp, error, format, ... ) \ #define commandError(fd, error, format, ... ) do \
{\ {\
if(current_command) { \ if (current_command) { \
myfprintf(fp, "ACK [%i@%i] {%s} " format "\n", \ fdprintf(fd, "ACK [%i@%i] {%s} " format "\n", \
(int)error, command_listNum, \ (int)error, command_listNum, \
current_command, __VA_ARGS__); \ current_command, __VA_ARGS__); \
current_command = NULL; \ current_command = NULL; \
} \ } \
else { \ else { \
myfprintf(stderr, "ACK [%i@%i] " format "\n", \ fdprintf(STDERR_FILENO, "ACK [%i@%i] " format "\n", \
(int)error, command_listNum, \ (int)error, command_listNum, \
__VA_ARGS__); \ __VA_ARGS__); \
} \ } \
} } while (0)
#endif #endif
...@@ -134,7 +134,7 @@ void freeLocateTagItem(LocateTagItem * item) ...@@ -134,7 +134,7 @@ void freeLocateTagItem(LocateTagItem * item)
free(item); free(item);
} }
static int countSongsInDirectory(FILE * fp, Directory * directory, void *data) static int countSongsInDirectory(int fd, Directory * directory, void *data)
{ {
int *count = (int *)data; int *count = (int *)data;
...@@ -143,18 +143,18 @@ static int countSongsInDirectory(FILE * fp, Directory * directory, void *data) ...@@ -143,18 +143,18 @@ static int countSongsInDirectory(FILE * fp, Directory * directory, void *data)
return 0; return 0;
} }
static int printDirectoryInDirectory(FILE * fp, Directory * directory, static int printDirectoryInDirectory(int fd, Directory * directory,
void *data) void *data)
{ {
if (directory->path) { if (directory->path) {
myfprintf(fp, "directory: %s\n", getDirectoryPath(directory)); fdprintf(fd, "directory: %s\n", getDirectoryPath(directory));
} }
return 0; return 0;
} }
static int printSongInDirectory(FILE * fp, Song * song, void *data) static int printSongInDirectory(int fd, Song * song, void *data)
{ {
printSongUrl(fp, song); printSongUrl(fd, song);
return 0; return 0;
} }
...@@ -192,7 +192,7 @@ static int strstrSearchTag(Song * song, int type, char *str) ...@@ -192,7 +192,7 @@ static int strstrSearchTag(Song * song, int type, char *str)
return ret; return ret;
} }
static int searchInDirectory(FILE * fp, Song * song, void *data) static int searchInDirectory(int fd, Song * song, void *data)
{ {
LocateTagItemArray *array = data; LocateTagItemArray *array = data;
int i; int i;
...@@ -204,12 +204,12 @@ static int searchInDirectory(FILE * fp, Song * song, void *data) ...@@ -204,12 +204,12 @@ static int searchInDirectory(FILE * fp, Song * song, void *data)
} }
} }
printSongInfo(fp, song); printSongInfo(fd, song);
return 0; return 0;
} }
int searchForSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items) int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
{ {
int ret = -1; int ret = -1;
int i; int i;
...@@ -225,7 +225,7 @@ int searchForSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items) ...@@ -225,7 +225,7 @@ int searchForSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items)
array.numItems = numItems; array.numItems = numItems;
array.items = items; array.items = items;
ret = traverseAllIn(fp, name, searchInDirectory, NULL, &array); ret = traverseAllIn(fd, name, searchInDirectory, NULL, &array);
for (i = 0; i < numItems; i++) { for (i = 0; i < numItems; i++) {
free(items[i].needle); free(items[i].needle);
...@@ -260,7 +260,7 @@ static int tagItemFoundAndMatches(Song * song, int type, char *str) ...@@ -260,7 +260,7 @@ static int tagItemFoundAndMatches(Song * song, int type, char *str)
return 0; return 0;
} }
static int findInDirectory(FILE * fp, Song * song, void *data) static int findInDirectory(int fd, Song * song, void *data)
{ {
LocateTagItemArray *array = data; LocateTagItemArray *array = data;
int i; int i;
...@@ -272,43 +272,43 @@ static int findInDirectory(FILE * fp, Song * song, void *data) ...@@ -272,43 +272,43 @@ static int findInDirectory(FILE * fp, Song * song, void *data)
} }
} }
printSongInfo(fp, song); printSongInfo(fd, song);
return 0; return 0;
} }
int findSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items) int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
{ {
LocateTagItemArray array; LocateTagItemArray array;
array.numItems = numItems; array.numItems = numItems;
array.items = items; array.items = items;
return traverseAllIn(fp, name, findInDirectory, NULL, (void *)&array); return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array);
} }
int printAllIn(FILE * fp, char *name) int printAllIn(int fd, char *name)
{ {
return traverseAllIn(fp, name, printSongInDirectory, return traverseAllIn(fd, name, printSongInDirectory,
printDirectoryInDirectory, NULL); printDirectoryInDirectory, NULL);
} }
static int directoryAddSongToPlaylist(FILE * fp, Song * song, void *data) static int directoryAddSongToPlaylist(int fd, Song * song, void *data)
{ {
return addSongToPlaylist(fp, song, 0); return addSongToPlaylist(fd, song, 0);
} }
int addAllIn(FILE * fp, char *name) int addAllIn(int fd, char *name)
{ {
return traverseAllIn(fp, name, directoryAddSongToPlaylist, NULL, NULL); return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
} }
static int directoryPrintSongInfo(FILE * fp, Song * song, void *data) static int directoryPrintSongInfo(int fd, Song * song, void *data)
{ {
return printSongInfo(fp, song); return printSongInfo(fd, song);
} }
static int sumSongTime(FILE * fp, Song * song, void *data) static int sumSongTime(int fd, Song * song, void *data)
{ {
unsigned long *time = (unsigned long *)data; unsigned long *time = (unsigned long *)data;
...@@ -318,28 +318,28 @@ static int sumSongTime(FILE * fp, Song * song, void *data) ...@@ -318,28 +318,28 @@ static int sumSongTime(FILE * fp, Song * song, void *data)
return 0; return 0;
} }
int printInfoForAllIn(FILE * fp, char *name) int printInfoForAllIn(int fd, char *name)
{ {
return traverseAllIn(fp, name, directoryPrintSongInfo, return traverseAllIn(fd, name, directoryPrintSongInfo,
printDirectoryInDirectory, NULL); printDirectoryInDirectory, NULL);
} }
int countSongsIn(FILE * fp, char *name) int countSongsIn(int fd, char *name)
{ {
int count = 0; int count = 0;
void *ptr = (void *)&count; void *ptr = (void *)&count;
traverseAllIn(fp, name, NULL, countSongsInDirectory, ptr); traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
return count; return count;
} }
unsigned long sumSongTimesIn(FILE * fp, char *name) unsigned long sumSongTimesIn(int fd, char *name)
{ {
unsigned long dbPlayTime = 0; unsigned long dbPlayTime = 0;
void *ptr = (void *)&dbPlayTime; void *ptr = (void *)&dbPlayTime;
traverseAllIn(fp, name, sumSongTime, NULL, ptr); traverseAllIn(fd, name, sumSongTime, NULL, ptr);
return dbPlayTime; return dbPlayTime;
} }
...@@ -361,13 +361,13 @@ static void freeListCommandItem(ListCommandItem * item) ...@@ -361,13 +361,13 @@ static void freeListCommandItem(ListCommandItem * item)
free(item); free(item);
} }
static void visitTag(FILE * fp, Song * song, int tagType) static void visitTag(int fd, Song * song, int tagType)
{ {
int i; int i;
MpdTag *tag = song->tag; MpdTag *tag = song->tag;
if (tagType == LOCATE_TAG_FILE_TYPE) { if (tagType == LOCATE_TAG_FILE_TYPE) {
printSongUrl(fp, song); printSongUrl(fd, song);
return; return;
} }
...@@ -381,7 +381,7 @@ static void visitTag(FILE * fp, Song * song, int tagType) ...@@ -381,7 +381,7 @@ static void visitTag(FILE * fp, Song * song, int tagType)
} }
} }
static int listUniqueTagsInDirectory(FILE * fp, Song * song, void *data) static int listUniqueTagsInDirectory(int fd, Song * song, void *data)
{ {
ListCommandItem *item = data; ListCommandItem *item = data;
int i; int i;
...@@ -393,12 +393,12 @@ static int listUniqueTagsInDirectory(FILE * fp, Song * song, void *data) ...@@ -393,12 +393,12 @@ static int listUniqueTagsInDirectory(FILE * fp, Song * song, void *data)
} }
} }
visitTag(fp, song, item->tagType); visitTag(fd, song, item->tagType);
return 0; return 0;
} }
int listAllUniqueTags(FILE * fp, int type, int numConditionals, int listAllUniqueTags(int fd, int type, int numConditionals,
LocateTagItem * conditionals) LocateTagItem * conditionals)
{ {
int ret; int ret;
...@@ -409,11 +409,11 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals, ...@@ -409,11 +409,11 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals,
resetVisitedFlagsInTagTracker(type); resetVisitedFlagsInTagTracker(type);
} }
ret = traverseAllIn(fp, NULL, listUniqueTagsInDirectory, NULL, ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
(void *)item); (void *)item);
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) { if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
printVisitedInTagTracker(fp, type); printVisitedInTagTracker(fd, type);
} }
freeListCommandItem(item); freeListCommandItem(item);
...@@ -421,7 +421,7 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals, ...@@ -421,7 +421,7 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals,
return ret; return ret;
} }
static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir, static int sumSavedFilenameMemoryInDirectory(int fd, Directory * dir,
void *data) void *data)
{ {
int *sum = data; int *sum = data;
...@@ -435,7 +435,7 @@ static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir, ...@@ -435,7 +435,7 @@ static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir,
return 0; return 0;
} }
static int sumSavedFilenameMemoryInSong(FILE * fp, Song * song, void *data) static int sumSavedFilenameMemoryInSong(int fd, Song * song, void *data)
{ {
int *sum = data; int *sum = data;
...@@ -448,7 +448,7 @@ void printSavedMemoryFromFilenames(void) ...@@ -448,7 +448,7 @@ void printSavedMemoryFromFilenames(void)
{ {
int sum = 0; int sum = 0;
traverseAllIn(stderr, NULL, sumSavedFilenameMemoryInSong, traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
sumSavedFilenameMemoryInDirectory, (void *)&sum); sumSavedFilenameMemoryInDirectory, (void *)&sum);
DEBUG("saved memory from filenames: %i\n", sum); DEBUG("saved memory from filenames: %i\n", sum);
......
...@@ -43,22 +43,22 @@ void freeLocateTagItemArray(int count, LocateTagItem * array); ...@@ -43,22 +43,22 @@ void freeLocateTagItemArray(int count, LocateTagItem * array);
void freeLocateTagItem(LocateTagItem * item); void freeLocateTagItem(LocateTagItem * item);
int printAllIn(FILE * fp, char *name); int printAllIn(int fd, char *name);
int addAllIn(FILE * fp, char *name); int addAllIn(int fd, char *name);
int printInfoForAllIn(FILE * fp, char *name); int printInfoForAllIn(int fd, char *name);
int searchForSongsIn(FILE * fp, char *name, int numItems, int searchForSongsIn(int fd, char *name, int numItems,
LocateTagItem * items); LocateTagItem * items);
int findSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items); int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items);
int countSongsIn(FILE * fp, char *name); int countSongsIn(int fd, char *name);
unsigned long sumSongTimesIn(FILE * fp, char *name); unsigned long sumSongTimesIn(int fd, char *name);
int listAllUniqueTags(FILE * fp, int type, int numConditiionals, int listAllUniqueTags(int fd, int type, int numConditiionals,
LocateTagItem * conditionals); LocateTagItem * conditionals);
void printSavedMemoryFromFilenames(); void printSavedMemoryFromFilenames();
......
...@@ -159,10 +159,10 @@ void readDirectoryDBIfUpdateIsFinished() ...@@ -159,10 +159,10 @@ void readDirectoryDBIfUpdateIsFinished()
} }
} }
int updateInit(FILE * fp, List * pathList) int updateInit(int fd, List * pathList)
{ {
if (directory_updatePid > 0) { if (directory_updatePid > 0) {
commandError(fp, ACK_ERROR_UPDATE_ALREADY, "already updating", commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating",
NULL); NULL);
return -1; return -1;
} }
...@@ -216,7 +216,7 @@ int updateInit(FILE * fp, List * pathList) ...@@ -216,7 +216,7 @@ int updateInit(FILE * fp, List * pathList)
} else if (directory_updatePid < 0) { } else if (directory_updatePid < 0) {
unblockSignals(); unblockSignals();
ERROR("updateInit: Problems forking()'ing\n"); ERROR("updateInit: Problems forking()'ing\n");
commandError(fp, ACK_ERROR_SYSTEM, commandError(fd, ACK_ERROR_SYSTEM,
"problems trying to update", NULL); "problems trying to update", NULL);
directory_updatePid = 0; directory_updatePid = 0;
return -1; return -1;
...@@ -228,7 +228,7 @@ int updateInit(FILE * fp, List * pathList) ...@@ -228,7 +228,7 @@ int updateInit(FILE * fp, List * pathList)
directory_updateJobId = 1; directory_updateJobId = 1;
DEBUG("updateInit: fork()'d update child for update job id %i\n", DEBUG("updateInit: fork()'d update child for update job id %i\n",
(int)directory_updateJobId); (int)directory_updateJobId);
myfprintf(fp, "updating_db: %i\n", (int)directory_updateJobId); fdprintf(fd, "updating_db: %i\n", (int)directory_updateJobId);
return 0; return 0;
} }
...@@ -871,33 +871,33 @@ static Directory *getDirectory(char *name) ...@@ -871,33 +871,33 @@ static Directory *getDirectory(char *name)
return getSubDirectory(mp3rootDirectory, name, &shortname); return getSubDirectory(mp3rootDirectory, name, &shortname);
} }
static int printDirectoryList(FILE * fp, DirectoryList * directoryList) static int printDirectoryList(int fd, DirectoryList * directoryList)
{ {
ListNode *node = directoryList->firstNode; ListNode *node = directoryList->firstNode;
Directory *directory; Directory *directory;
while (node != NULL) { while (node != NULL) {
directory = (Directory *) node->data; directory = (Directory *) node->data;
myfprintf(fp, "%s%s\n", DIRECTORY_DIR, fdprintf(fd, "%s%s\n", DIRECTORY_DIR,
getDirectoryPath(directory)); getDirectoryPath(directory));
node = node->nextNode; node = node->nextNode;
} }
return 0; return 0;
} }
int printDirectoryInfo(FILE * fp, char *name) int printDirectoryInfo(int fd, char *name)
{ {
Directory *directory; Directory *directory;
if ((directory = getDirectory(name)) == NULL) { if ((directory = getDirectory(name)) == NULL) {
commandError(fp, ACK_ERROR_NO_EXIST, "directory not found", commandError(fd, ACK_ERROR_NO_EXIST, "directory not found",
NULL); NULL);
return -1; return -1;
} }
printDirectoryList(fp, directory->subDirectories); printDirectoryList(fd, directory->subDirectories);
printSongInfoFromList(fp, directory->songs); printSongInfoFromList(fd, directory->songs);
return 0; return 0;
} }
...@@ -1208,8 +1208,8 @@ int readDirectoryDB() ...@@ -1208,8 +1208,8 @@ int readDirectoryDB()
readDirectoryInfo(fp, mp3rootDirectory); readDirectoryInfo(fp, mp3rootDirectory);
while (fclose(fp) && errno == EINTR) ; while (fclose(fp) && errno == EINTR) ;
stats.numberOfSongs = countSongsIn(stderr, NULL); stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(stderr, NULL); stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
if (stat(dbFile, &st) == 0) if (stat(dbFile, &st) == 0)
directory_dbModTime = st.st_mtime; directory_dbModTime = st.st_mtime;
...@@ -1237,10 +1237,10 @@ void updateMp3Directory() ...@@ -1237,10 +1237,10 @@ void updateMp3Directory()
return; return;
} }
static int traverseAllInSubDirectory(FILE * fp, Directory * directory, static int traverseAllInSubDirectory(int fd, Directory * directory,
int (*forEachSong) (FILE *, Song *, int (*forEachSong) (int, Song *,
void *), void *),
int (*forEachDir) (FILE *, Directory *, int (*forEachDir) (int, Directory *,
void *), void *data) void *), void *data)
{ {
ListNode *node = directory->songs->firstNode; ListNode *node = directory->songs->firstNode;
...@@ -1249,7 +1249,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory, ...@@ -1249,7 +1249,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
int errFlag = 0; int errFlag = 0;
if (forEachDir) { if (forEachDir) {
errFlag = forEachDir(fp, directory, data); errFlag = forEachDir(fd, directory, data);
if (errFlag) if (errFlag)
return errFlag; return errFlag;
} }
...@@ -1257,7 +1257,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory, ...@@ -1257,7 +1257,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
if (forEachSong) { if (forEachSong) {
while (node != NULL && !errFlag) { while (node != NULL && !errFlag) {
song = (Song *) node->data; song = (Song *) node->data;
errFlag = forEachSong(fp, song, data); errFlag = forEachSong(fd, song, data);
node = node->nextNode; node = node->nextNode;
} }
if (errFlag) if (errFlag)
...@@ -1268,7 +1268,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory, ...@@ -1268,7 +1268,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
while (node != NULL && !errFlag) { while (node != NULL && !errFlag) {
dir = (Directory *) node->data; dir = (Directory *) node->data;
errFlag = traverseAllInSubDirectory(fp, dir, forEachSong, errFlag = traverseAllInSubDirectory(fd, dir, forEachSong,
forEachDir, data); forEachDir, data);
node = node->nextNode; node = node->nextNode;
} }
...@@ -1276,23 +1276,23 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory, ...@@ -1276,23 +1276,23 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
return errFlag; return errFlag;
} }
int traverseAllIn(FILE * fp, char *name, int traverseAllIn(int fd, char *name,
int (*forEachSong) (FILE *, Song *, void *), int (*forEachSong) (int, Song *, void *),
int (*forEachDir) (FILE *, Directory *, void *), void *data) int (*forEachDir) (int, Directory *, void *), void *data)
{ {
Directory *directory; Directory *directory;
if ((directory = getDirectory(name)) == NULL) { if ((directory = getDirectory(name)) == NULL) {
Song *song; Song *song;
if ((song = getSongFromDB(name)) && forEachSong) { if ((song = getSongFromDB(name)) && forEachSong) {
return forEachSong(fp, song, data); return forEachSong(fd, song, data);
} }
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found", NULL); "directory or file not found", NULL);
return -1; return -1;
} }
return traverseAllInSubDirectory(fp, directory, forEachSong, forEachDir, return traverseAllInSubDirectory(fd, directory, forEachSong, forEachDir,
data); data);
} }
...@@ -1315,8 +1315,8 @@ void initMp3Directory() ...@@ -1315,8 +1315,8 @@ void initMp3Directory()
mp3rootDirectory = newDirectory(NULL, NULL); mp3rootDirectory = newDirectory(NULL, NULL);
exploreDirectory(mp3rootDirectory); exploreDirectory(mp3rootDirectory);
freeAllDirectoryStats(mp3rootDirectory); freeAllDirectoryStats(mp3rootDirectory);
stats.numberOfSongs = countSongsIn(stderr, NULL); stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(stderr, NULL); stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
if (stat(getDbFile(), &st) == 0) if (stat(getDbFile(), &st) == 0)
directory_dbModTime = st.st_mtime; directory_dbModTime = st.st_mtime;
......
...@@ -45,13 +45,13 @@ int isUpdatingDB(); ...@@ -45,13 +45,13 @@ int isUpdatingDB();
void directory_sigChldHandler(int pid, int status); void directory_sigChldHandler(int pid, int status);
int updateInit(FILE * fp, List * pathList); int updateInit(int fd, List * pathList);
void initMp3Directory(); void initMp3Directory();
void closeMp3Directory(); void closeMp3Directory();
int printDirectoryInfo(FILE * fp, char *dirname); int printDirectoryInfo(int fd, char *dirname);
int checkDirectoryDB(); int checkDirectoryDB();
...@@ -65,9 +65,9 @@ Song *getSongFromDB(char *file); ...@@ -65,9 +65,9 @@ Song *getSongFromDB(char *file);
time_t getDbModTime(); time_t getDbModTime();
int traverseAllIn(FILE * fp, char *name, int traverseAllIn(int fd, char *name,
int (*forEachSong) (FILE *, Song *, void *), int (*forEachSong) (int, Song *, void *),
int (*forEachDir) (FILE *, Directory *, void *), void *data); int (*forEachDir) (int, Directory *, void *), void *data);
#define getDirectoryPath(dir) ((dir && dir->path) ? dir->path : "") #define getDirectoryPath(dir) ((dir && dir->path) ? dir->path : "")
......
#ifndef MPD_GCC_H
#define MPD_GCC_H
/* this allows us to take advantage of special gcc features while still
* allowing other compilers to compile:
*
* example taken from: http://rlove.org/log/2005102601
*/
/* disabled (0) until I fix all the warnings :) */
#if (0 && __GNUC__ >= 3)
# define mpd_const __attribute__ ((const))
# define mpd_deprecated __attribute__ ((deprecated))
# define mpd_malloc __attribute__ ((malloc))
# define mpd_must_check __attribute__ ((warn_unused_result))
# define mpd_noreturn __attribute__ ((noreturn))
# define mpd_packed __attribute__ ((packed))
/* these are very useful for type checking */
# define mpd_printf __attribute__ ((format(printf,1,2)))
# define mpd_fprintf __attribute__ ((format(printf,2,3)))
# define mpd_fprintf_ __attribute__ ((format(printf,3,4)))
# define mpd_pure __attribute__ ((pure))
# define mpd_scanf __attribute__ ((format(scanf,1,2)))
# define mpd_unused __attribute__ ((unused))
# define mpd_used __attribute__ ((used))
/* # define inline inline __attribute__ ((always_inline)) */
# define mpd_noinline __attribute__ ((noinline))
# define likely(x) __builtin_expect (!!(x), 1)
# define unlikely(x) __builtin_expect (!!(x), 0)
#else
# define mpd_const
# define mpd_deprecated
# define mpd_malloc
# define mpd_must_check
# define mpd_noreturn
# define mpd_packed
# define mpd_printf
# define mpd_fprintf
# define mpd_fprintf_
# define mpd_pure
# define mpd_scanf
# define mpd_unused
# define mpd_used
/* # define inline */
# define mpd_noinline
# define likely(x) (x)
# define unlikely(x) (x)
#endif
#endif /* MPD_GCC_H */
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include "listen.h" #include "listen.h"
#include "playlist.h" #include "playlist.h"
#include "permission.h" #include "permission.h"
#include "sig_handlers.h" #include "sllist.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#define GREETING "OK MPD" #define GREETING "OK MPD " VERSION "\n"
#define INTERFACE_MAX_BUFFER_LENGTH (40960) #define INTERFACE_MAX_BUFFER_LENGTH (40960)
#define INTERFACE_LIST_MODE_BEGIN "command_list_begin" #define INTERFACE_LIST_MODE_BEGIN "command_list_begin"
...@@ -61,26 +61,36 @@ static size_t interface_max_command_list_size = ...@@ -61,26 +61,36 @@ static size_t interface_max_command_list_size =
static size_t interface_max_output_buffer_size = static size_t interface_max_output_buffer_size =
INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT; INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT;
/* maybe make conf option for this, or... 32 might be good enough */
static long int interface_list_cache_size = 32;
/* shared globally between all interfaces: */
static struct strnode *list_cache = NULL;
static struct strnode *list_cache_head = NULL;
static struct strnode *list_cache_tail = NULL;
typedef struct _Interface { typedef struct _Interface {
char buffer[INTERFACE_MAX_BUFFER_LENGTH]; char buffer[INTERFACE_MAX_BUFFER_LENGTH];
int bufferLength; int bufferLength;
int bufferPos; int bufferPos;
int fd; /* file descriptor */ int fd; /* file descriptor */
FILE *fp; /* file pointer */
int open; /* open/used */
int permission; int permission;
time_t lastTime; time_t lastTime;
List *commandList; /* for when in list mode */ struct strnode *cmd_list; /* for when in list mode */
int commandListOK; /* print OK after each command execution */ struct strnode *cmd_list_tail; /* for when in list mode */
size_t commandListSize; /* mem commandList consumes */ int cmd_list_OK; /* print OK after each command execution */
List *bufferList; /* for output if client is slow */ int cmd_list_size; /* mem cmd_list consumes */
size_t outputBufferSize; /* mem bufferList consumes */ int cmd_list_dup; /* has the cmd_list been copied to private space? */
struct sllnode *deferred_send; /* for output if client is slow */
int deferred_bytes; /* mem deferred_send consumes */
int expired; /* set whether this interface should be closed on next int expired; /* set whether this interface should be closed on next
check of old interfaces */ check of old interfaces */
int num; /* interface number */ int num; /* interface number */
char *outBuffer;
int outBuflen; char *send_buf;
int outBufSize; int send_buf_used; /* bytes used this instance */
int send_buf_size; /* bytes usable this instance */
int send_buf_alloc; /* bytes actually allocated */
} Interface; } Interface;
static Interface *interfaces = NULL; static Interface *interfaces = NULL;
...@@ -89,66 +99,150 @@ static void flushInterfaceBuffer(Interface * interface); ...@@ -89,66 +99,150 @@ static void flushInterfaceBuffer(Interface * interface);
static void printInterfaceOutBuffer(Interface * interface); static void printInterfaceOutBuffer(Interface * interface);
#ifdef SO_SNDBUF
static int get_default_snd_buf_size(Interface * interface)
{
int new_size;
socklen_t sockOptLen = sizeof(int);
if (getsockopt(interface->fd, SOL_SOCKET, SO_SNDBUF,
(char *)&new_size, &sockOptLen) < 0) {
DEBUG("problem getting sockets send buffer size\n");
return INTERFACE_DEFAULT_OUT_BUFFER_SIZE;
}
if (new_size > 0)
return new_size;
DEBUG("sockets send buffer size is not positive\n");
return INTERFACE_DEFAULT_OUT_BUFFER_SIZE;
}
#else /* !SO_SNDBUF */
static int get_default_snd_buf_size(Interface * interface)
{
return INTERFACE_DEFAULT_OUT_BUFFER_SIZE;
}
#endif /* !SO_SNDBUF */
static void set_send_buf_size(Interface * interface)
{
int new_size = get_default_snd_buf_size(interface);
if (interface->send_buf_size != new_size) {
interface->send_buf_size = new_size;
/* don't resize to get smaller, only bigger */
if (interface->send_buf_alloc < new_size) {
if (interface->send_buf)
free(interface->send_buf);
interface->send_buf = malloc(new_size);
interface->send_buf_alloc = new_size;
}
}
}
static void openInterface(Interface * interface, int fd) static void openInterface(Interface * interface, int fd)
{ {
int flags; int flags;
assert(interface->open == 0); assert(interface->fd < 0);
interface->cmd_list_size = 0;
interface->cmd_list_dup = 0;
interface->cmd_list_OK = -1;
interface->bufferLength = 0; interface->bufferLength = 0;
interface->bufferPos = 0; interface->bufferPos = 0;
interface->fd = fd; interface->fd = fd;
/* fcntl(interface->fd,F_SETOWN,(int)getpid()); */
while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ; while ((flags = fcntl(fd, F_GETFL)) < 0 && errno == EINTR) ;
flags |= O_NONBLOCK; while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 && errno == EINTR) ;
while (fcntl(interface->fd, F_SETFL, flags) < 0 && errno == EINTR) ;
while ((interface->fp = fdopen(fd, "rw")) == NULL && errno == EINTR) ;
interface->open = 1;
interface->lastTime = time(NULL); interface->lastTime = time(NULL);
interface->commandList = NULL; interface->cmd_list = NULL;
interface->bufferList = NULL; interface->cmd_list_tail = NULL;
interface->deferred_send = NULL;
interface->expired = 0; interface->expired = 0;
interface->outputBufferSize = 0; interface->deferred_bytes = 0;
interface->outBuflen = 0; interface->send_buf_used = 0;
interface->permission = getDefaultPermissions(); interface->permission = getDefaultPermissions();
set_send_buf_size(interface);
interface->outBufSize = INTERFACE_DEFAULT_OUT_BUFFER_SIZE; xwrite(fd, GREETING, strlen(GREETING));
#ifdef SO_SNDBUF }
{
int getSize; static void free_cmd_list(struct strnode *list)
unsigned int sockOptLen = sizeof(int); {
struct strnode *tmp = list;
if (getsockopt(interface->fd, SOL_SOCKET, SO_SNDBUF,
(char *)&getSize, &sockOptLen) < 0) { while (tmp) {
DEBUG("problem getting sockets send buffer size\n"); struct strnode *next = tmp->next;
} else if (getSize <= 0) { if (tmp >= list_cache_head && tmp <= list_cache_tail) {
DEBUG("sockets send buffer size is not positive\n"); /* inside list_cache[] array */
tmp->data = NULL;
tmp->next = NULL;
} else } else
interface->outBufSize = getSize; free(tmp);
tmp = next;
} }
#endif }
interface->outBuffer = malloc(interface->outBufSize);
myfprintf(interface->fp, "%s %s\n", GREETING, VERSION); static void cmd_list_clone(Interface * interface)
printInterfaceOutBuffer(interface); {
struct strnode *new = dup_strlist(interface->cmd_list);
free_cmd_list(interface->cmd_list);
interface->cmd_list = new;
interface->cmd_list_dup = 1;
/* new tail */
while (new && new->next)
new = new->next;
interface->cmd_list_tail = new;
} }
static void closeInterface(Interface * interface) static void new_cmd_list_ptr(Interface * interface, char *s, const int size)
{ {
if (!interface->open) int i;
return; struct strnode *new;
if (!interface->cmd_list_dup) {
for (i = interface_list_cache_size - 1; i >= 0; --i) {
if (list_cache[i].data)
continue;
new = &(list_cache[i]);
new->data = s;
/* implied in free_cmd_list() and init: */
/* last->next->next = NULL; */
goto out;
}
}
interface->open = 0; /* allocate from the heap */
new = interface->cmd_list_dup ? new_strnode_dup(s, size)
: new_strnode(s);
out:
if (interface->cmd_list) {
interface->cmd_list_tail->next = new;
interface->cmd_list_tail = new;
} else
interface->cmd_list = interface->cmd_list_tail = new;
}
while (fclose(interface->fp) && errno == EINTR) ; static void closeInterface(Interface * interface)
{
struct sllnode *buf;
if (interface->fd < 0)
return;
xclose(interface->fd);
interface->fd = -1;
if (interface->commandList) if (interface->cmd_list) {
freeList(interface->commandList); free_cmd_list(interface->cmd_list);
if (interface->bufferList) interface->cmd_list = NULL;
freeList(interface->bufferList); }
free(interface->outBuffer); if ((buf = interface->deferred_send)) {
do {
struct sllnode *prev = buf;
buf = buf->next;
free(prev);
} while (buf);
interface->deferred_send = NULL;
}
SECURE("interface %i: closed\n", interface->num); SECURE("interface %i: closed\n", interface->num);
} }
...@@ -157,11 +251,12 @@ void openAInterface(int fd, struct sockaddr *addr) ...@@ -157,11 +251,12 @@ void openAInterface(int fd, struct sockaddr *addr)
{ {
int i; int i;
for (i = 0; i < interface_max_connections && interfaces[i].open; i++) ; for (i = 0; i < interface_max_connections
&& interfaces[i].fd >= 0; i++) /* nothing */ ;
if (i == interface_max_connections) { if (i == interface_max_connections) {
ERROR("Max Connections Reached!\n"); ERROR("Max Connections Reached!\n");
while (close(fd) && errno == EINTR) ; xclose(fd);
} else { } else {
SECURE("interface %i: opened from ", i); SECURE("interface %i: opened from ", i);
switch (addr->sa_family) { switch (addr->sa_family) {
...@@ -207,74 +302,60 @@ static int processLineOfInput(Interface * interface) ...@@ -207,74 +302,60 @@ static int processLineOfInput(Interface * interface)
int ret = 1; int ret = 1;
char *line = interface->buffer + interface->bufferPos; char *line = interface->buffer + interface->bufferPos;
if (interface->bufferLength - interface->bufferPos > 1) { if (interface->cmd_list_OK >= 0) {
if (interface->buffer[interface->bufferLength - 2] == '\r') {
interface->buffer[interface->bufferLength - 2] = '\0';
}
}
if (interface->commandList) {
if (strcmp(line, INTERFACE_LIST_MODE_END) == 0) { if (strcmp(line, INTERFACE_LIST_MODE_END) == 0) {
DEBUG("interface %i: process command " DEBUG("interface %i: process command "
"list\n", interface->num); "list\n", interface->num);
ret = processListOfCommands(interface->fp, ret = processListOfCommands(interface->fd,
&(interface->permission), &(interface->permission),
&(interface->expired), &(interface->expired),
interface->commandListOK, interface->cmd_list_OK,
interface->commandList); interface->cmd_list);
DEBUG("interface %i: process command " DEBUG("interface %i: process command "
"list returned %i\n", interface->num, ret); "list returned %i\n", interface->num, ret);
if (ret == 0) if (ret == 0)
commandSuccess(interface->fp); commandSuccess(interface->fd);
else if (ret == COMMAND_RETURN_CLOSE else if (ret == COMMAND_RETURN_CLOSE
|| interface->expired) { || interface->expired)
closeInterface(interface); closeInterface(interface);
}
printInterfaceOutBuffer(interface);
freeList(interface->commandList); printInterfaceOutBuffer(interface);
interface->commandList = NULL; free_cmd_list(interface->cmd_list);
interface->cmd_list = NULL;
interface->cmd_list_OK = -1;
} else { } else {
interface->commandListSize += sizeof(ListNode); size_t len = strlen(line) + 1;
interface->commandListSize += strlen(line) + 1; interface->cmd_list_size += len;
if (interface->commandListSize > if (interface->cmd_list_size >
interface_max_command_list_size) { interface_max_command_list_size) {
ERROR("interface %i: command " ERROR("interface %i: command "
"list size (%lli) is " "list size (%i) is "
"larger than the max " "larger than the max "
"(%lli)\n", "(%i)\n",
interface->num, interface->num,
(long long)interface-> interface->cmd_list_size,
commandListSize, (long long)
interface_max_command_list_size); interface_max_command_list_size);
closeInterface(interface); closeInterface(interface);
ret = COMMAND_RETURN_CLOSE; ret = COMMAND_RETURN_CLOSE;
} else { } else
insertInListWithoutKey(interface->commandList, new_cmd_list_ptr(interface, line, len);
strdup(line));
}
} }
} else { } else {
if (strcmp(line, INTERFACE_LIST_MODE_BEGIN) == 0) { if (strcmp(line, INTERFACE_LIST_MODE_BEGIN) == 0) {
interface->commandList = makeList(free, 1); interface->cmd_list_OK = 0;
interface->commandListSize = sizeof(List);
interface->commandListOK = 0;
ret = 1; ret = 1;
} else if (strcmp(line, INTERFACE_LIST_OK_MODE_BEGIN) == 0) { } else if (strcmp(line, INTERFACE_LIST_OK_MODE_BEGIN) == 0) {
interface->commandList = makeList(free, 1); interface->cmd_list_OK = 1;
interface->commandListSize = sizeof(List);
interface->commandListOK = 1;
ret = 1; ret = 1;
} else { } else {
DEBUG("interface %i: process command \"%s\"\n", DEBUG("interface %i: process command \"%s\"\n",
interface->num, line); interface->num, line);
ret = processCommand(interface->fp, ret = processCommand(interface->fd,
&(interface->permission), line); &(interface->permission), line);
DEBUG("interface %i: command returned %i\n", DEBUG("interface %i: command returned %i\n",
interface->num, ret); interface->num, ret);
if (ret == 0) if (ret == 0)
commandSuccess(interface->fp); commandSuccess(interface->fd);
else if (ret == COMMAND_RETURN_CLOSE else if (ret == COMMAND_RETURN_CLOSE
|| interface->expired) { || interface->expired) {
closeInterface(interface); closeInterface(interface);
...@@ -289,12 +370,18 @@ static int processLineOfInput(Interface * interface) ...@@ -289,12 +370,18 @@ static int processLineOfInput(Interface * interface)
static int processBytesRead(Interface * interface, int bytesRead) static int processBytesRead(Interface * interface, int bytesRead)
{ {
int ret = 0; int ret = 0;
char *buf_tail = &(interface->buffer[interface->bufferLength - 1]);
while (bytesRead > 0) { while (bytesRead > 0) {
interface->bufferLength++; interface->bufferLength++;
bytesRead--; bytesRead--;
if (interface->buffer[interface->bufferLength - 1] == '\n') { buf_tail++;
interface->buffer[interface->bufferLength - 1] = '\0'; if (*buf_tail == '\n') {
*buf_tail = '\0';
if (interface->bufferLength - interface->bufferPos > 1) {
if (*(buf_tail - 1) == '\r')
*(buf_tail - 1) = '\0';
}
ret = processLineOfInput(interface); ret = processLineOfInput(interface);
interface->bufferPos = interface->bufferLength; interface->bufferPos = interface->bufferLength;
} }
...@@ -305,6 +392,9 @@ static int processBytesRead(Interface * interface, int bytesRead) ...@@ -305,6 +392,9 @@ static int processBytesRead(Interface * interface, int bytesRead)
closeInterface(interface); closeInterface(interface);
return 1; return 1;
} }
if (interface->cmd_list_OK >= 0 &&
!interface->cmd_list_dup)
cmd_list_clone(interface);
interface->bufferLength -= interface->bufferPos; interface->bufferLength -= interface->bufferPos;
memmove(interface->buffer, memmove(interface->buffer,
interface->buffer + interface->bufferPos, interface->buffer + interface->bufferPos,
...@@ -347,8 +437,8 @@ static void addInterfacesReadyToReadAndListenSocketToFdSet(fd_set * fds, ...@@ -347,8 +437,8 @@ static void addInterfacesReadyToReadAndListenSocketToFdSet(fd_set * fds,
addListenSocketsToFdSet(fds, fdmax); addListenSocketsToFdSet(fds, fdmax);
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open && !interfaces[i].expired if (interfaces[i].fd >= 0 && !interfaces[i].expired
&& !interfaces[i].bufferList) { && !interfaces[i].deferred_send) {
FD_SET(interfaces[i].fd, fds); FD_SET(interfaces[i].fd, fds);
if (*fdmax < interfaces[i].fd) if (*fdmax < interfaces[i].fd)
*fdmax = interfaces[i].fd; *fdmax = interfaces[i].fd;
...@@ -363,8 +453,8 @@ static void addInterfacesForBufferFlushToFdSet(fd_set * fds, int *fdmax) ...@@ -363,8 +453,8 @@ static void addInterfacesForBufferFlushToFdSet(fd_set * fds, int *fdmax)
FD_ZERO(fds); FD_ZERO(fds);
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open && !interfaces[i].expired if (interfaces[i].fd >= 0 && !interfaces[i].expired
&& interfaces[i].bufferList) { && interfaces[i].deferred_send) {
FD_SET(interfaces[i].fd, fds); FD_SET(interfaces[i].fd, fds);
if (*fdmax < interfaces[i].fd) if (*fdmax < interfaces[i].fd)
*fdmax = interfaces[i].fd; *fdmax = interfaces[i].fd;
...@@ -382,7 +472,7 @@ static void closeNextErroredInterface(void) ...@@ -382,7 +472,7 @@ static void closeNextErroredInterface(void)
tv.tv_usec = 0; tv.tv_usec = 0;
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open) { if (interfaces[i].fd >= 0) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(interfaces[i].fd, &fds); FD_SET(interfaces[i].fd, &fds);
if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) { if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) {
...@@ -424,7 +514,7 @@ int doIOForInterfaces(void) ...@@ -424,7 +514,7 @@ int doIOForInterfaces(void)
getConnections(&rfds); getConnections(&rfds);
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open if (interfaces[i].fd >= 0
&& FD_ISSET(interfaces[i].fd, &rfds)) { && FD_ISSET(interfaces[i].fd, &rfds)) {
if (COMMAND_RETURN_KILL == if (COMMAND_RETURN_KILL ==
interfaceReadInput(&(interfaces[i]))) { interfaceReadInput(&(interfaces[i]))) {
...@@ -432,7 +522,7 @@ int doIOForInterfaces(void) ...@@ -432,7 +522,7 @@ int doIOForInterfaces(void)
} }
interfaces[i].lastTime = time(NULL); interfaces[i].lastTime = time(NULL);
} }
if (interfaces[i].open if (interfaces[i].fd >= 0
&& FD_ISSET(interfaces[i].fd, &wfds)) { && FD_ISSET(interfaces[i].fd, &wfds)) {
flushInterfaceBuffer(&interfaces[i]); flushInterfaceBuffer(&interfaces[i]);
interfaces[i].lastTime = time(NULL); interfaces[i].lastTime = time(NULL);
...@@ -504,8 +594,15 @@ void initInterfaces(void) ...@@ -504,8 +594,15 @@ void initInterfaces(void)
interfaces = malloc(sizeof(Interface) * interface_max_connections); interfaces = malloc(sizeof(Interface) * interface_max_connections);
list_cache = calloc(interface_list_cache_size, sizeof(struct strnode));
list_cache_head = &(list_cache[0]);
list_cache_tail = &(list_cache[interface_list_cache_size - 1]);
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
interfaces[i].open = 0; interfaces[i].fd = -1;
interfaces[i].send_buf = NULL;
interfaces[i].send_buf_size = 0;
interfaces[i].send_buf_alloc = 0;
interfaces[i].num = i; interfaces[i].num = i;
} }
} }
...@@ -514,13 +611,13 @@ static void closeAllInterfaces(void) ...@@ -514,13 +611,13 @@ static void closeAllInterfaces(void)
{ {
int i; int i;
fflush(NULL);
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open) { if (interfaces[i].fd > 0)
closeInterface(&(interfaces[i])); closeInterface(&(interfaces[i]));
} if (interfaces[i].send_buf)
free(interfaces[i].send_buf);
} }
free(list_cache);
} }
void freeAllInterfaces(void) void freeAllInterfaces(void)
...@@ -537,7 +634,7 @@ void closeOldInterfaces(void) ...@@ -537,7 +634,7 @@ void closeOldInterfaces(void)
int i; int i;
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open) { if (interfaces[i].fd > 0) {
if (interfaces[i].expired) { if (interfaces[i].expired) {
DEBUG("interface %i: expired\n", i); DEBUG("interface %i: expired\n", i);
closeInterface(&(interfaces[i])); closeInterface(&(interfaces[i]));
...@@ -552,37 +649,45 @@ void closeOldInterfaces(void) ...@@ -552,37 +649,45 @@ void closeOldInterfaces(void)
static void flushInterfaceBuffer(Interface * interface) static void flushInterfaceBuffer(Interface * interface)
{ {
ListNode *node = NULL; struct sllnode *buf;
char *str;
int ret = 0; int ret = 0;
while ((node = interface->bufferList->firstNode)) { buf = interface->deferred_send;
str = (char *)node->data; while (buf) {
if ((ret = write(interface->fd, str, strlen(str))) < 0) ret = write(interface->fd, buf->data, buf->size);
if (ret < 0)
break; break;
else if (ret < strlen(str)) { else if (ret < buf->size) {
interface->outputBufferSize -= ret; interface->deferred_bytes -= ret;
str = strdup(&str[ret]); buf->data += ret;
free(node->data); buf->size -= ret;
node->data = str;
} else { } else {
interface->outputBufferSize -= strlen(str) + 1; struct sllnode *tmp = buf;
interface->outputBufferSize -= sizeof(ListNode); interface->deferred_bytes -= (buf->size +
deleteNodeFromList(interface->bufferList, node); sizeof(struct sllnode));
buf = buf->next;
free(tmp);
interface->deferred_send = buf;
} }
interface->lastTime = time(NULL); interface->lastTime = time(NULL);
} }
if (!interface->bufferList->firstNode) { if (!interface->deferred_send) {
DEBUG("interface %i: buffer empty\n", interface->num); DEBUG("interface %i: buffer empty %i\n", interface->num,
freeList(interface->bufferList); interface->deferred_bytes);
interface->bufferList = NULL; assert(interface->deferred_bytes == 0);
} else if (ret < 0 && errno != EAGAIN && errno != EINTR) { } else if (ret < 0 && errno != EAGAIN && errno != EINTR) {
/* cause interface to close */ /* cause interface to close */
DEBUG("interface %i: problems flushing buffer\n", DEBUG("interface %i: problems flushing buffer\n",
interface->num); interface->num);
freeList(interface->bufferList); buf = interface->deferred_send;
interface->bufferList = NULL; do {
struct sllnode *prev = buf;
buf = buf->next;
free(prev);
} while (buf);
interface->deferred_send = NULL;
interface->deferred_bytes = 0;
interface->expired = 1; interface->expired = 1;
} }
} }
...@@ -593,10 +698,12 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen) ...@@ -593,10 +698,12 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen)
int copylen; int copylen;
Interface *interface; Interface *interface;
assert(fd > 0);
if (i >= interface_max_connections || if (i >= interface_max_connections ||
!interfaces[i].open || interfaces[i].fd != fd) { interfaces[i].fd < 0 || interfaces[i].fd != fd) {
for (i = 0; i < interface_max_connections; i++) { for (i = 0; i < interface_max_connections; i++) {
if (interfaces[i].open && interfaces[i].fd == fd) if (interfaces[i].fd == fd)
break; break;
} }
if (i == interface_max_connections) if (i == interface_max_connections)
...@@ -610,17 +717,15 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen) ...@@ -610,17 +717,15 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen)
interface = interfaces + i; interface = interfaces + i;
while (buflen > 0 && !interface->expired) { while (buflen > 0 && !interface->expired) {
copylen = buflen > int left = interface->send_buf_size - interface->send_buf_used;
interface->outBufSize - interface->outBuflen ? copylen = buflen > left ? left : buflen;
interface->outBufSize - interface->outBuflen : buflen; memcpy(interface->send_buf + interface->send_buf_used, buffer,
memcpy(interface->outBuffer + interface->outBuflen, buffer,
copylen); copylen);
buflen -= copylen; buflen -= copylen;
interface->outBuflen += copylen; interface->send_buf_used += copylen;
buffer += copylen; buffer += copylen;
if (interface->outBuflen >= interface->outBufSize) { if (interface->send_buf_used >= interface->send_buf_size)
printInterfaceOutBuffer(interface); printInterfaceOutBuffer(interface);
}
} }
return 0; return 0;
...@@ -628,73 +733,63 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen) ...@@ -628,73 +733,63 @@ int interfacePrintWithFD(int fd, char *buffer, int buflen)
static void printInterfaceOutBuffer(Interface * interface) static void printInterfaceOutBuffer(Interface * interface)
{ {
char *buffer;
int ret; int ret;
struct sllnode *buf;
if (!interface->open || interface->expired || !interface->outBuflen) { if (interface->fd < 0 || interface->expired ||
!interface->send_buf_used)
return; return;
}
if (interface->bufferList) { if ((buf = interface->deferred_send)) {
interface->outputBufferSize += sizeof(ListNode); interface->deferred_bytes += sizeof(struct sllnode)
interface->outputBufferSize += interface->outBuflen + 1; + interface->send_buf_used;
if (interface->outputBufferSize > if (interface->deferred_bytes >
interface_max_output_buffer_size) { interface_max_output_buffer_size) {
ERROR("interface %i: output buffer size (%lli) is " ERROR("interface %i: output buffer size (%li) is "
"larger than the max (%lli)\n", "larger than the max (%li)\n",
interface->num, interface->num,
(long long)interface->outputBufferSize, (long)interface->deferred_bytes,
(long long)interface_max_output_buffer_size); (long)interface_max_output_buffer_size);
/* cause interface to close */ /* cause interface to close */
freeList(interface->bufferList);
interface->bufferList = NULL;
interface->expired = 1; interface->expired = 1;
do {
struct sllnode *prev = buf;
buf = buf->next;
free(prev);
} while (buf);
interface->deferred_send = NULL;
interface->deferred_bytes = 0;
} else { } else {
buffer = malloc(interface->outBuflen + 1); while (buf->next)
memcpy(buffer, interface->outBuffer, buf = buf->next;
interface->outBuflen); buf->next = new_sllnode(interface->send_buf,
buffer[interface->outBuflen] = '\0'; interface->send_buf_used);
insertInListWithoutKey(interface->bufferList,
(void *)buffer);
flushInterfaceBuffer(interface);
} }
} else { } else {
if ((ret = write(interface->fd, interface->outBuffer, if ((ret = write(interface->fd, interface->send_buf,
interface->outBuflen)) < 0) { interface->send_buf_used)) < 0) {
if (errno == EAGAIN || errno == EINTR) { if (errno == EAGAIN || errno == EINTR) {
buffer = malloc(interface->outBuflen + 1); interface->deferred_send =
memcpy(buffer, interface->outBuffer, new_sllnode(interface->send_buf,
interface->outBuflen); interface->send_buf_used);
buffer[interface->outBuflen] = '\0';
interface->bufferList = makeList(free, 1);
insertInListWithoutKey(interface->bufferList,
(void *)buffer);
} else { } else {
DEBUG("interface %i: problems writing\n", DEBUG("interface %i: problems writing\n",
interface->num); interface->num);
interface->expired = 1; interface->expired = 1;
return; return;
} }
} else if (ret < interface->outBuflen) { } else if (ret < interface->send_buf_used) {
buffer = malloc(interface->outBuflen - ret + 1); interface->deferred_send =
memcpy(buffer, interface->outBuffer + ret, new_sllnode(interface->send_buf + ret,
interface->outBuflen - ret); interface->send_buf_used - ret);
buffer[interface->outBuflen - ret] = '\0';
interface->bufferList = makeList(free, 1);
insertInListWithoutKey(interface->bufferList, buffer);
} }
/* if we needed to create buffer, initialize bufferSize info */ if (interface->deferred_send) {
if (interface->bufferList) {
DEBUG("interface %i: buffer created\n", interface->num); DEBUG("interface %i: buffer created\n", interface->num);
interface->outputBufferSize = sizeof(List); interface->deferred_bytes =
interface->outputBufferSize += sizeof(ListNode); interface->deferred_send->size
interface->outputBufferSize += strlen((char *) + sizeof(struct sllnode);
interface->
bufferList->
firstNode->data) +
1;
} }
} }
interface->outBuflen = 0; interface->send_buf_used = 0;
} }
...@@ -32,12 +32,12 @@ static char *remoteUrlPrefixes[] = { ...@@ -32,12 +32,12 @@ static char *remoteUrlPrefixes[] = {
NULL NULL
}; };
int printRemoteUrlHandlers(FILE * fp) int printRemoteUrlHandlers(int fd)
{ {
char **prefixes = remoteUrlPrefixes; char **prefixes = remoteUrlPrefixes;
while (*prefixes) { while (*prefixes) {
myfprintf(fp, "handler: %s\n", *prefixes); fdprintf(fd, "handler: %s\n", *prefixes);
prefixes++; prefixes++;
} }
...@@ -100,7 +100,7 @@ int isRemoteUrl(char *url) ...@@ -100,7 +100,7 @@ int isRemoteUrl(char *url)
return 0; return 0;
} }
int lsPlaylists(FILE * fp, char *utf8path) int lsPlaylists(int fd, char *utf8path)
{ {
DIR *dir; DIR *dir;
struct stat st; struct stat st;
...@@ -166,7 +166,7 @@ int lsPlaylists(FILE * fp, char *utf8path) ...@@ -166,7 +166,7 @@ int lsPlaylists(FILE * fp, char *utf8path)
node = list->firstNode; node = list->firstNode;
while (node != NULL) { while (node != NULL) {
if (!strchr(node->key, '\n')) { if (!strchr(node->key, '\n')) {
myfprintf(fp, "playlist: %s%s\n", dup, fdprintf(fd, "playlist: %s%s\n", dup,
node->key); node->key);
} }
node = node->nextNode; node = node->nextNode;
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
int lsPlaylists(FILE * fp, char *utf8path); int lsPlaylists(int fd, char *utf8path);
char *getSuffix(char *utf8file); char *getSuffix(char *utf8file);
...@@ -47,6 +47,6 @@ InputPlugin *hasMusicSuffix(char *utf8file, unsigned int next); ...@@ -47,6 +47,6 @@ InputPlugin *hasMusicSuffix(char *utf8file, unsigned int next);
InputPlugin *isMusic(char *utf8file, time_t * mtime, unsigned int next); InputPlugin *isMusic(char *utf8file, time_t * mtime, unsigned int next);
int printRemoteUrlHandlers(FILE * fp); int printRemoteUrlHandlers(int fd);
#endif #endif
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "path.h" #include "path.h"
#include "log.h" #include "log.h"
#include "conf.h" #include "conf.h"
#include "utils.h"
#include <stdarg.h> #include <stdarg.h>
#include <sys/param.h> #include <sys/param.h>
...@@ -38,28 +39,41 @@ static FILE *myfprintf_err; ...@@ -38,28 +39,41 @@ static FILE *myfprintf_err;
static char *myfprintf_outFilename; static char *myfprintf_outFilename;
static char *myfprintf_errFilename; static char *myfprintf_errFilename;
static void blockingWrite(int fd, char *string, int len) static void blockingWrite(const int fd, const char *string, size_t len)
{ {
int ret;
while (len) { while (len) {
ret = write(fd, string, len); size_t ret = xwrite(fd, string, len);
if (ret == 0) if (ret == len)
return; return;
if (ret < 0) { if (ret >= 0) {
switch (errno) { len -= ret;
case EAGAIN: string += ret;
case EINTR: continue;
continue;
default:
return;
}
} }
len -= ret; return; /* error */
string += ret;
} }
} }
void vfdprintf(const int fd, const char *fmt, va_list args)
{
static char buffer[BUFFER_LENGTH + 1];
char *buf = buffer;
size_t len;
vsnprintf(buf, BUFFER_LENGTH, fmt, args);
len = strlen(buf);
if (interfacePrintWithFD(fd, buf, len) < 0)
blockingWrite(fd, buf, len);
}
mpd_fprintf void fdprintf(const int fd, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfdprintf(fd, fmt, args);
va_end(args);
}
void myfprintfStdLogMode(FILE * out, FILE * err) void myfprintfStdLogMode(FILE * out, FILE * err)
{ {
myfprintf_stdLogMode = 1; myfprintf_stdLogMode = 1;
...@@ -69,38 +83,6 @@ void myfprintfStdLogMode(FILE * out, FILE * err) ...@@ -69,38 +83,6 @@ void myfprintfStdLogMode(FILE * out, FILE * err)
myfprintf_errFilename = getConfigParamValue(CONF_ERROR_FILE); myfprintf_errFilename = getConfigParamValue(CONF_ERROR_FILE);
} }
void myfprintf(FILE * fp, char *format, ...)
{
static char buffer[BUFFER_LENGTH + 1];
va_list arglist;
int fd = fileno(fp);
va_start(arglist, format);
if (fd == 1 || fd == 2) {
if (myfprintf_stdLogMode) {
time_t t = time(NULL);
if (fd == 1)
fp = myfprintf_out;
else
fp = myfprintf_err;
strftime(buffer, 14, "%b %e %R", localtime(&t));
blockingWrite(fd, buffer, strlen(buffer));
blockingWrite(fd, " : ", 3);
}
vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
blockingWrite(fd, buffer, strlen(buffer));
} else {
int len;
vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
len = strlen(buffer);
if (interfacePrintWithFD(fd, buffer, len) < 0) {
blockingWrite(fd, buffer, len);
}
}
va_end(arglist);
}
int myfprintfCloseAndOpenLogFile(void) int myfprintfCloseAndOpenLogFile(void)
{ {
if (myfprintf_stdLogMode) { if (myfprintf_stdLogMode) {
......
...@@ -20,12 +20,20 @@ ...@@ -20,12 +20,20 @@
#define MYFPRINTF_H #define MYFPRINTF_H
#include "../config.h" #include "../config.h"
#include "gcc.h"
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
void myfprintfStdLogMode(FILE * out, FILE * err); void myfprintfStdLogMode(FILE * out, FILE * err);
void myfprintf(FILE * fp, char *format, ...); mpd_fprintf void fdprintf(const int fd, const char *fmt, ...);
void vfdprintf(const int fd, const char *fmt, va_list arglist);
#define myfprintf(fp, ...) do { \
fprintf(fp, __VA_ARGS__); \
fflush(fp); \
} while (0)
int myfprintfCloseAndOpenLogFile(); int myfprintfCloseAndOpenLogFile();
......
...@@ -156,14 +156,11 @@ int playerInitReal() ...@@ -156,14 +156,11 @@ int playerInitReal()
return 0; return 0;
} }
int playerPlay(FILE * fp, Song * song) int playerPlay(int fd, Song * song)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (fp == NULL) if (playerStop(fd) < 0)
fp = stderr;
if (playerStop(fp) < 0)
return -1; return -1;
if (song->tag) if (song->tag)
...@@ -189,7 +186,7 @@ int playerPlay(FILE * fp, Song * song) ...@@ -189,7 +186,7 @@ int playerPlay(FILE * fp, Song * song)
return 0; return 0;
} }
int playerStop(FILE * fp) int playerStop(int fd)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
...@@ -219,7 +216,7 @@ void playerKill() ...@@ -219,7 +216,7 @@ void playerKill()
kill(pid, SIGTERM); kill(pid, SIGTERM);
} }
int playerPause(FILE * fp) int playerPause(int fd)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
...@@ -232,7 +229,7 @@ int playerPause(FILE * fp) ...@@ -232,7 +229,7 @@ int playerPause(FILE * fp)
return 0; return 0;
} }
int playerSetPause(FILE * fp, int pause) int playerSetPause(int fd, int pause)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
...@@ -242,11 +239,11 @@ int playerSetPause(FILE * fp, int pause) ...@@ -242,11 +239,11 @@ int playerSetPause(FILE * fp, int pause)
switch (pc->state) { switch (pc->state) {
case PLAYER_STATE_PLAY: case PLAYER_STATE_PLAY:
if (pause) if (pause)
playerPause(fp); playerPause(fd);
break; break;
case PLAYER_STATE_PAUSE: case PLAYER_STATE_PAUSE:
if (!pause) if (!pause)
playerPause(fp); playerPause(fd);
break; break;
} }
...@@ -329,7 +326,7 @@ void playerCloseAudio() ...@@ -329,7 +326,7 @@ void playerCloseAudio()
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (getPlayerPid() > 0) { if (getPlayerPid() > 0) {
if (playerStop(stderr) < 0) if (playerStop(STDERR_FILENO) < 0)
return; return;
pc->closeAudio = 1; pc->closeAudio = 1;
} }
...@@ -393,12 +390,12 @@ void playerQueueUnlock() ...@@ -393,12 +390,12 @@ void playerQueueUnlock()
} }
} }
int playerSeek(FILE * fp, Song * song, float time) int playerSeek(int fd, Song * song, float time)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (pc->state == PLAYER_STATE_STOP) { if (pc->state == PLAYER_STATE_STOP) {
commandError(fp, ACK_ERROR_PLAYER_SYNC, commandError(fd, ACK_ERROR_PLAYER_SYNC,
"player not currently playing", NULL); "player not currently playing", NULL);
return -1; return -1;
} }
......
...@@ -93,13 +93,13 @@ int playerInitReal(); ...@@ -93,13 +93,13 @@ int playerInitReal();
void player_sigChldHandler(int pid, int status); void player_sigChldHandler(int pid, int status);
int playerPlay(FILE * fp, Song * song); int playerPlay(int fd, Song * song);
int playerSetPause(FILE * fp, int pause); int playerSetPause(int fd, int pause);
int playerPause(FILE * fp); int playerPause(int fd);
int playerStop(FILE * fp); int playerStop(int fd);
void playerCloseAudio(); void playerCloseAudio();
...@@ -131,7 +131,7 @@ void playerQueueLock(); ...@@ -131,7 +131,7 @@ void playerQueueLock();
void playerQueueUnlock(); void playerQueueUnlock();
int playerSeek(FILE * fp, Song * song, float time); int playerSeek(int fd, Song * song, float time);
void setPlayerCrossFade(float crossFadeInSeconds); void setPlayerCrossFade(float crossFadeInSeconds);
......
...@@ -89,7 +89,7 @@ static int playlist_noGoToNext = 0; ...@@ -89,7 +89,7 @@ static int playlist_noGoToNext = 0;
static int playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS; static int playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
static void swapOrder(int a, int b); static void swapOrder(int a, int b);
static int playPlaylistOrderNumber(FILE * fp, int orderNum); static int playPlaylistOrderNumber(int fd, int orderNum);
static void randomizeOrder(int start, int end); static void randomizeOrder(int start, int end);
char *getStateFile(void) char *getStateFile(void)
...@@ -223,11 +223,11 @@ void finishPlaylist(void) ...@@ -223,11 +223,11 @@ void finishPlaylist(void)
playlist.positionToId = NULL; playlist.positionToId = NULL;
} }
int clearPlaylist(FILE * fp) int clearPlaylist(int fd)
{ {
int i; int i;
if (stopPlaylist(fp) < 0) if (stopPlaylist(fd) < 0)
return -1; return -1;
for (i = 0; i < playlist.length; i++) { for (i = 0; i < playlist.length; i++) {
...@@ -245,12 +245,12 @@ int clearPlaylist(FILE * fp) ...@@ -245,12 +245,12 @@ int clearPlaylist(FILE * fp)
return 0; return 0;
} }
int showPlaylist(FILE * fp) int showPlaylist(int fd)
{ {
int i; int i;
for (i = 0; i < playlist.length; i++) { for (i = 0; i < playlist.length; i++) {
myfprintf(fp, "%i:%s\n", i, getSongUrl(playlist.songs[i])); fdprintf(fd, "%i:%s\n", i, getSongUrl(playlist.songs[i]));
} }
return 0; return 0;
...@@ -298,14 +298,14 @@ void savePlaylistState(void) ...@@ -298,14 +298,14 @@ void savePlaylistState(void)
myfprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE, myfprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
(int)(getPlayerCrossFade())); (int)(getPlayerCrossFade()));
myfprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); myfprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
showPlaylist(fp); showPlaylist(fileno(fp));
myfprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END); myfprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
while (fclose(fp) && errno == EINTR) ; while (fclose(fp) && errno == EINTR) ;
} }
} }
void loadPlaylistFromStateFile(FILE * fp, char *buffer, int state, int current, void loadPlaylistFromStateFile(FILE *fp, char *buffer, int state, int current,
int time) int time)
{ {
char *temp; char *temp;
...@@ -322,15 +322,17 @@ void loadPlaylistFromStateFile(FILE * fp, char *buffer, int state, int current, ...@@ -322,15 +322,17 @@ void loadPlaylistFromStateFile(FILE * fp, char *buffer, int state, int current,
ERROR("error parsing state file \"%s\"\n", stateFile); ERROR("error parsing state file \"%s\"\n", stateFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (addToPlaylist(stderr, temp, 0) == 0 && current == song) { if (!addToPlaylist(STDERR_FILENO, temp, 0) && current == song) {
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
playPlaylist(stderr, playlist.length - 1, 0); playPlaylist(STDERR_FILENO,
playlist.length - 1, 0);
} }
if (state == PLAYER_STATE_PAUSE) { if (state == PLAYER_STATE_PAUSE) {
playerPause(stderr); playerPause(STDERR_FILENO);
} }
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
seekSongInPlaylist(stderr, playlist.length - 1, seekSongInPlaylist(STDERR_FILENO,
playlist.length - 1,
time); time);
} }
} }
...@@ -402,9 +404,11 @@ void readPlaylistState(void) ...@@ -402,9 +404,11 @@ void readPlaylistState(void)
(buffer (buffer
[strlen(PLAYLIST_STATE_FILE_REPEAT)]), [strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) { "1") == 0) {
setPlaylistRepeatStatus(stderr, 1); setPlaylistRepeatStatus(STDERR_FILENO,
1);
} else } else
setPlaylistRepeatStatus(stderr, 0); setPlaylistRepeatStatus(STDERR_FILENO,
0);
} else } else
if (strncmp if (strncmp
(buffer, PLAYLIST_STATE_FILE_CROSSFADE, (buffer, PLAYLIST_STATE_FILE_CROSSFADE,
...@@ -423,9 +427,11 @@ void readPlaylistState(void) ...@@ -423,9 +427,11 @@ void readPlaylistState(void)
(buffer (buffer
[strlen(PLAYLIST_STATE_FILE_RANDOM)]), [strlen(PLAYLIST_STATE_FILE_RANDOM)]),
"1") == 0) { "1") == 0) {
setPlaylistRandomStatus(stderr, 1); setPlaylistRandomStatus(STDERR_FILENO,
1);
} else } else
setPlaylistRandomStatus(stderr, 0); setPlaylistRandomStatus(STDERR_FILENO,
0);
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT, } else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT,
strlen(PLAYLIST_STATE_FILE_CURRENT)) strlen(PLAYLIST_STATE_FILE_CURRENT))
== 0) { == 0) {
...@@ -454,14 +460,13 @@ void readPlaylistState(void) ...@@ -454,14 +460,13 @@ void readPlaylistState(void)
} }
} }
void printPlaylistSongInfo(FILE * fp, int song) void printPlaylistSongInfo(int fd, int song)
{ {
printSongInfo(fp, playlist.songs[song]); printSongInfo(fd, playlist.songs[song]);
myfprintf(fp, "Pos: %i\n", song); fdprintf(fd, "Pos: %i\nId: %i\n", song, playlist.positionToId[song]);
myfprintf(fp, "Id: %i\n", playlist.positionToId[song]);
} }
int playlistChanges(FILE * fp, mpd_uint32 version) int playlistChanges(int fd, mpd_uint32 version)
{ {
int i; int i;
...@@ -469,14 +474,14 @@ int playlistChanges(FILE * fp, mpd_uint32 version) ...@@ -469,14 +474,14 @@ int playlistChanges(FILE * fp, mpd_uint32 version)
if (version > playlist.version || if (version > playlist.version ||
playlist.songMod[i] >= version || playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) { playlist.songMod[i] == 0) {
printPlaylistSongInfo(fp, i); printPlaylistSongInfo(fd, i);
} }
} }
return 0; return 0;
} }
int playlistChangesPosId(FILE * fp, mpd_uint32 version) int playlistChangesPosId(int fd, mpd_uint32 version)
{ {
int i; int i;
...@@ -484,15 +489,15 @@ int playlistChangesPosId(FILE * fp, mpd_uint32 version) ...@@ -484,15 +489,15 @@ int playlistChangesPosId(FILE * fp, mpd_uint32 version)
if (version > playlist.version || if (version > playlist.version ||
playlist.songMod[i] >= version || playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) { playlist.songMod[i] == 0) {
myfprintf(fp, "cpos: %i\n", i); fdprintf(fd, "cpos: %i\nId: %i\n",
myfprintf(fp, "Id: %i\n", playlist.positionToId[i]); i, playlist.positionToId[i]);
} }
} }
return 0; return 0;
} }
int playlistInfo(FILE * fp, int song) int playlistInfo(int fd, int song)
{ {
int i; int i;
int begin = 0; int begin = 0;
...@@ -503,13 +508,13 @@ int playlistInfo(FILE * fp, int song) ...@@ -503,13 +508,13 @@ int playlistInfo(FILE * fp, int song)
end = song + 1; end = song + 1;
} }
if (song >= playlist.length) { if (song >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song); "song doesn't exist: \"%i\"", song);
return -1; return -1;
} }
for (i = begin; i < end; i++) for (i = begin; i < end; i++)
printPlaylistSongInfo(fp, i); printPlaylistSongInfo(fd, i);
return 0; return 0;
} }
...@@ -518,13 +523,13 @@ int playlistInfo(FILE * fp, int song) ...@@ -518,13 +523,13 @@ int playlistInfo(FILE * fp, int song)
if(id < 0 || id >= PLAYLIST_HASH_MULT*playlist_max_length || \ if(id < 0 || id >= PLAYLIST_HASH_MULT*playlist_max_length || \
playlist.idToPosition[id] == -1 ) \ playlist.idToPosition[id] == -1 ) \
{ \ { \
commandError(fp, ACK_ERROR_NO_EXIST, \ commandError(fd, ACK_ERROR_NO_EXIST, \
"song id doesn't exist: \"%i\"", id); \ "song id doesn't exist: \"%i\"", id); \
return -1; \ return -1; \
} \ } \
} }
int playlistId(FILE * fp, int id) int playlistId(int fd, int id)
{ {
int i; int i;
int begin = 0; int begin = 0;
...@@ -537,7 +542,7 @@ int playlistId(FILE * fp, int id) ...@@ -537,7 +542,7 @@ int playlistId(FILE * fp, int id)
} }
for (i = begin; i < end; i++) for (i = begin; i < end; i++)
printPlaylistSongInfo(fp, i); printPlaylistSongInfo(fd, i);
return 0; return 0;
} }
...@@ -642,7 +647,7 @@ void clearPlayerQueue(void) ...@@ -642,7 +647,7 @@ void clearPlayerQueue(void)
} }
} }
int addToPlaylist(FILE * fp, char *url, int printId) int addToPlaylist(int fd, char *url, int printId)
{ {
Song *song; Song *song;
...@@ -651,21 +656,21 @@ int addToPlaylist(FILE * fp, char *url, int printId) ...@@ -651,21 +656,21 @@ int addToPlaylist(FILE * fp, char *url, int printId)
if ((song = getSongFromDB(url))) { if ((song = getSongFromDB(url))) {
} else if (!(isValidRemoteUtf8Url(url) && } else if (!(isValidRemoteUtf8Url(url) &&
(song = newSong(url, SONG_TYPE_URL, NULL)))) { (song = newSong(url, SONG_TYPE_URL, NULL)))) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"\"%s\" is not in the music db or is " "\"%s\" is not in the music db or is "
"not a valid url", url); "not a valid url", url);
return -1; return -1;
} }
return addSongToPlaylist(fp, song, printId); return addSongToPlaylist(fd, song, printId);
} }
int addSongToPlaylist(FILE * fp, Song * song, int printId) int addSongToPlaylist(int fd, Song * song, int printId)
{ {
int id; int id;
if (playlist.length == playlist_max_length) { if (playlist.length == playlist_max_length) {
commandError(fp, ACK_ERROR_PLAYLIST_MAX, commandError(fd, ACK_ERROR_PLAYLIST_MAX,
"playlist is at the max size", NULL); "playlist is at the max size", NULL);
return -1; return -1;
} }
...@@ -707,23 +712,23 @@ int addSongToPlaylist(FILE * fp, Song * song, int printId) ...@@ -707,23 +712,23 @@ int addSongToPlaylist(FILE * fp, Song * song, int printId)
incrPlaylistVersion(); incrPlaylistVersion();
if (printId) if (printId)
myfprintf(fp, "Id: %i\n", id); fdprintf(fd, "Id: %i\n", id);
return 0; return 0;
} }
int swapSongsInPlaylist(FILE * fp, int song1, int song2) int swapSongsInPlaylist(int fd, int song1, int song2)
{ {
int queuedSong = -1; int queuedSong = -1;
int currentSong = -1; int currentSong = -1;
if (song1 < 0 || song1 >= playlist.length) { if (song1 < 0 || song1 >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song1); "song doesn't exist: \"%i\"", song1);
return -1; return -1;
} }
if (song2 < 0 || song2 >= playlist.length) { if (song2 < 0 || song2 >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song2); "song doesn't exist: \"%i\"", song2);
return -1; return -1;
} }
...@@ -768,12 +773,12 @@ int swapSongsInPlaylist(FILE * fp, int song1, int song2) ...@@ -768,12 +773,12 @@ int swapSongsInPlaylist(FILE * fp, int song1, int song2)
return 0; return 0;
} }
int swapSongsInPlaylistById(FILE * fp, int id1, int id2) int swapSongsInPlaylistById(int fd, int id1, int id2)
{ {
checkSongId(id1); checkSongId(id1);
checkSongId(id2); checkSongId(id2);
return swapSongsInPlaylist(fp, playlist.idToPosition[id1], return swapSongsInPlaylist(fd, playlist.idToPosition[id1],
playlist.idToPosition[id2]); playlist.idToPosition[id2]);
} }
...@@ -784,13 +789,13 @@ int swapSongsInPlaylistById(FILE * fp, int id1, int id2) ...@@ -784,13 +789,13 @@ int swapSongsInPlaylistById(FILE * fp, int id1, int id2)
playlist.songMod[to] = playlist.version; \ playlist.songMod[to] = playlist.version; \
} }
int deleteFromPlaylist(FILE * fp, int song) int deleteFromPlaylist(int fd, int song)
{ {
int i; int i;
int songOrder; int songOrder;
if (song < 0 || song >= playlist.length) { if (song < 0 || song >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song); "song doesn't exist: \"%i\"", song);
return -1; return -1;
} }
...@@ -837,9 +842,9 @@ int deleteFromPlaylist(FILE * fp, int song) ...@@ -837,9 +842,9 @@ int deleteFromPlaylist(FILE * fp, int song)
if (playlist_state != PLAYLIST_STATE_STOP if (playlist_state != PLAYLIST_STATE_STOP
&& playlist.current == songOrder) { && playlist.current == songOrder) {
/*if(playlist.current>=playlist.length) return playerStop(fp); /*if(playlist.current>=playlist.length) return playerStop(fd);
else return playPlaylistOrderNumber(fp,playlist.current); */ else return playPlaylistOrderNumber(fd,playlist.current); */
playerStop(stderr); playerStop(STDERR_FILENO);
playlist_noGoToNext = 1; playlist_noGoToNext = 1;
} }
...@@ -856,11 +861,11 @@ int deleteFromPlaylist(FILE * fp, int song) ...@@ -856,11 +861,11 @@ int deleteFromPlaylist(FILE * fp, int song)
return 0; return 0;
} }
int deleteFromPlaylistById(FILE * fp, int id) int deleteFromPlaylistById(int fd, int id)
{ {
checkSongId(id); checkSongId(id);
return deleteFromPlaylist(fp, playlist.idToPosition[id]); return deleteFromPlaylist(fd, playlist.idToPosition[id]);
} }
void deleteASongFromPlaylist(Song * song) void deleteASongFromPlaylist(Song * song)
...@@ -872,15 +877,15 @@ void deleteASongFromPlaylist(Song * song) ...@@ -872,15 +877,15 @@ void deleteASongFromPlaylist(Song * song)
for (i = 0; i < playlist.length; i++) { for (i = 0; i < playlist.length; i++) {
if (song == playlist.songs[i]) { if (song == playlist.songs[i]) {
deleteFromPlaylist(stderr, i); deleteFromPlaylist(STDERR_FILENO, i);
} }
} }
} }
int stopPlaylist(FILE * fp) int stopPlaylist(int fd)
{ {
DEBUG("playlist: stop\n"); DEBUG("playlist: stop\n");
if (playerStop(fp) < 0) if (playerStop(fd) < 0)
return -1; return -1;
playerCloseAudio(); playerCloseAudio();
playlist.queued = -1; playlist.queued = -1;
...@@ -891,10 +896,10 @@ int stopPlaylist(FILE * fp) ...@@ -891,10 +896,10 @@ int stopPlaylist(FILE * fp)
return 0; return 0;
} }
int playPlaylistOrderNumber(FILE * fp, int orderNum) int playPlaylistOrderNumber(int fd, int orderNum)
{ {
if (playerStop(fp) < 0) if (playerStop(fd) < 0)
return -1; return -1;
playlist_state = PLAYLIST_STATE_PLAY; playlist_state = PLAYLIST_STATE_PLAY;
...@@ -905,8 +910,8 @@ int playPlaylistOrderNumber(FILE * fp, int orderNum) ...@@ -905,8 +910,8 @@ int playPlaylistOrderNumber(FILE * fp, int orderNum)
DEBUG("playlist: play %i:\"%s\"\n", orderNum, DEBUG("playlist: play %i:\"%s\"\n", orderNum,
getSongUrl(playlist.songs[playlist.order[orderNum]])); getSongUrl(playlist.songs[playlist.order[orderNum]]));
if (playerPlay(fp, (playlist.songs[playlist.order[orderNum]])) < 0) { if (playerPlay(fd, (playlist.songs[playlist.order[orderNum]])) < 0) {
stopPlaylist(fp); stopPlaylist(fd);
return -1; return -1;
} else } else
playlist.current++; playlist.current++;
...@@ -916,7 +921,7 @@ int playPlaylistOrderNumber(FILE * fp, int orderNum) ...@@ -916,7 +921,7 @@ int playPlaylistOrderNumber(FILE * fp, int orderNum)
return 0; return 0;
} }
int playPlaylist(FILE * fp, int song, int stopOnError) int playPlaylist(int fd, int song, int stopOnError)
{ {
int i = song; int i = song;
...@@ -927,7 +932,7 @@ int playPlaylist(FILE * fp, int song, int stopOnError) ...@@ -927,7 +932,7 @@ int playPlaylist(FILE * fp, int song, int stopOnError)
return 0; return 0;
if (playlist_state == PLAYLIST_STATE_PLAY) { if (playlist_state == PLAYLIST_STATE_PLAY) {
return playerSetPause(fp, 0); return playerSetPause(fd, 0);
} }
if (playlist.current >= 0 && playlist.current < playlist.length) { if (playlist.current >= 0 && playlist.current < playlist.length) {
i = playlist.current; i = playlist.current;
...@@ -935,7 +940,7 @@ int playPlaylist(FILE * fp, int song, int stopOnError) ...@@ -935,7 +940,7 @@ int playPlaylist(FILE * fp, int song, int stopOnError)
i = 0; i = 0;
} }
} else if (song < 0 || song >= playlist.length) { } else if (song < 0 || song >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song); "song doesn't exist: \"%i\"", song);
return -1; return -1;
} }
...@@ -957,18 +962,18 @@ int playPlaylist(FILE * fp, int song, int stopOnError) ...@@ -957,18 +962,18 @@ int playPlaylist(FILE * fp, int song, int stopOnError)
playlist_stopOnError = stopOnError; playlist_stopOnError = stopOnError;
playlist_errorCount = 0; playlist_errorCount = 0;
return playPlaylistOrderNumber(fp, i); return playPlaylistOrderNumber(fd, i);
} }
int playPlaylistById(FILE * fp, int id, int stopOnError) int playPlaylistById(int fd, int id, int stopOnError)
{ {
if (id == -1) { if (id == -1) {
return playPlaylist(fp, id, stopOnError); return playPlaylist(fd, id, stopOnError);
} }
checkSongId(id); checkSongId(id);
return playPlaylist(fp, playlist.idToPosition[id], stopOnError); return playPlaylist(fd, playlist.idToPosition[id], stopOnError);
} }
void syncCurrentPlayerDecodeMetadata(void) void syncCurrentPlayerDecodeMetadata(void)
...@@ -1010,7 +1015,7 @@ void syncPlayerAndPlaylist(void) ...@@ -1010,7 +1015,7 @@ void syncPlayerAndPlaylist(void)
syncCurrentPlayerDecodeMetadata(); syncCurrentPlayerDecodeMetadata();
} }
int currentSongInPlaylist(FILE * fp) int currentSongInPlaylist(int fd)
{ {
if (playlist_state != PLAYLIST_STATE_PLAY) if (playlist_state != PLAYLIST_STATE_PLAY)
return 0; return 0;
...@@ -1020,14 +1025,14 @@ int currentSongInPlaylist(FILE * fp) ...@@ -1020,14 +1025,14 @@ int currentSongInPlaylist(FILE * fp)
syncPlaylistWithQueue(0); syncPlaylistWithQueue(0);
if (playlist.current >= 0 && playlist.current < playlist.length) { if (playlist.current >= 0 && playlist.current < playlist.length) {
return playPlaylistOrderNumber(fp, playlist.current); return playPlaylistOrderNumber(fd, playlist.current);
} else } else
return stopPlaylist(fp);; return stopPlaylist(fd);
return 0; return 0;
} }
int nextSongInPlaylist(FILE * fp) int nextSongInPlaylist(int fd)
{ {
if (playlist_state != PLAYLIST_STATE_PLAY) if (playlist_state != PLAYLIST_STATE_PLAY)
return 0; return 0;
...@@ -1037,14 +1042,14 @@ int nextSongInPlaylist(FILE * fp) ...@@ -1037,14 +1042,14 @@ int nextSongInPlaylist(FILE * fp)
playlist_stopOnError = 0; playlist_stopOnError = 0;
if (playlist.current < playlist.length - 1) { if (playlist.current < playlist.length - 1) {
return playPlaylistOrderNumber(fp, playlist.current + 1); return playPlaylistOrderNumber(fd, playlist.current + 1);
} else if (playlist.length && playlist.repeat) { } else if (playlist.length && playlist.repeat) {
if (playlist.random) if (playlist.random)
randomizeOrder(0, playlist.length - 1); randomizeOrder(0, playlist.length - 1);
return playPlaylistOrderNumber(fp, 0); return playPlaylistOrderNumber(fd, 0);
} else { } else {
incrPlaylistCurrent(); incrPlaylistCurrent();
return stopPlaylist(fp);; return stopPlaylist(fd);
} }
return 0; return 0;
...@@ -1065,11 +1070,11 @@ void playPlaylistIfPlayerStopped(void) ...@@ -1065,11 +1070,11 @@ void playPlaylistIfPlayerStopped(void)
|| error == PLAYER_ERROR_AUDIO || error == PLAYER_ERROR_AUDIO
|| error == PLAYER_ERROR_SYSTEM || error == PLAYER_ERROR_SYSTEM
|| playlist_errorCount >= playlist.length)) { || playlist_errorCount >= playlist.length)) {
stopPlaylist(stderr); stopPlaylist(STDERR_FILENO);
} else if (playlist_noGoToNext) } else if (playlist_noGoToNext)
currentSongInPlaylist(stderr); currentSongInPlaylist(STDERR_FILENO);
else else
nextSongInPlaylist(stderr); nextSongInPlaylist(STDERR_FILENO);
} }
} }
...@@ -1083,10 +1088,10 @@ int getPlaylistRandomStatus(void) ...@@ -1083,10 +1088,10 @@ int getPlaylistRandomStatus(void)
return playlist.random; return playlist.random;
} }
int setPlaylistRepeatStatus(FILE * fp, int status) int setPlaylistRepeatStatus(int fd, int status)
{ {
if (status != 0 && status != 1) { if (status != 0 && status != 1) {
commandError(fp, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status); commandError(fd, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status);
return -1; return -1;
} }
...@@ -1103,7 +1108,7 @@ int setPlaylistRepeatStatus(FILE * fp, int status) ...@@ -1103,7 +1108,7 @@ int setPlaylistRepeatStatus(FILE * fp, int status)
return 0; return 0;
} }
int moveSongInPlaylist(FILE * fp, int from, int to) int moveSongInPlaylist(int fd, int from, int to)
{ {
int i; int i;
Song *tmpSong; Song *tmpSong;
...@@ -1112,13 +1117,13 @@ int moveSongInPlaylist(FILE * fp, int from, int to) ...@@ -1112,13 +1117,13 @@ int moveSongInPlaylist(FILE * fp, int from, int to)
int currentSong = -1; int currentSong = -1;
if (from < 0 || from >= playlist.length) { if (from < 0 || from >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", from); "song doesn't exist: \"%i\"", from);
return -1; return -1;
} }
if (to < 0 || to >= playlist.length) { if (to < 0 || to >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", to); "song doesn't exist: \"%i\"", to);
return -1; return -1;
} }
...@@ -1176,11 +1181,11 @@ int moveSongInPlaylist(FILE * fp, int from, int to) ...@@ -1176,11 +1181,11 @@ int moveSongInPlaylist(FILE * fp, int from, int to)
return 0; return 0;
} }
int moveSongInPlaylistById(FILE * fp, int id1, int to) int moveSongInPlaylistById(int fd, int id1, int to)
{ {
checkSongId(id1); checkSongId(id1);
return moveSongInPlaylist(fp, playlist.idToPosition[id1], to); return moveSongInPlaylist(fd, playlist.idToPosition[id1], to);
} }
static void orderPlaylist(void) static void orderPlaylist(void)
...@@ -1238,12 +1243,12 @@ static void randomizeOrder(int start, int end) ...@@ -1238,12 +1243,12 @@ static void randomizeOrder(int start, int end)
} }
int setPlaylistRandomStatus(FILE * fp, int status) int setPlaylistRandomStatus(int fd, int status)
{ {
int statusWas = playlist.random; int statusWas = playlist.random;
if (status != 0 && status != 1) { if (status != 0 && status != 1) {
commandError(fp, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status); commandError(fd, ACK_ERROR_ARG, "\"%i\" is not 0 or 1", status);
return -1; return -1;
} }
...@@ -1268,7 +1273,7 @@ int setPlaylistRandomStatus(FILE * fp, int status) ...@@ -1268,7 +1273,7 @@ int setPlaylistRandomStatus(FILE * fp, int status)
return 0; return 0;
} }
int previousSongInPlaylist(FILE * fp) int previousSongInPlaylist(int fd)
{ {
static time_t lastTime = 0; static time_t lastTime = 0;
time_t diff = time(NULL) - lastTime; time_t diff = time(NULL) - lastTime;
...@@ -1281,22 +1286,22 @@ int previousSongInPlaylist(FILE * fp) ...@@ -1281,22 +1286,22 @@ int previousSongInPlaylist(FILE * fp)
syncPlaylistWithQueue(0); syncPlaylistWithQueue(0);
if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) { if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) {
return playPlaylistOrderNumber(fp, playlist.current); return playPlaylistOrderNumber(fd, playlist.current);
} else { } else {
if (playlist.current > 0) { if (playlist.current > 0) {
return playPlaylistOrderNumber(fp, return playPlaylistOrderNumber(fd,
playlist.current - 1); playlist.current - 1);
} else if (playlist.repeat) { } else if (playlist.repeat) {
return playPlaylistOrderNumber(fp, playlist.length - 1); return playPlaylistOrderNumber(fd, playlist.length - 1);
} else { } else {
return playPlaylistOrderNumber(fp, playlist.current); return playPlaylistOrderNumber(fd, playlist.current);
} }
} }
return 0; return 0;
} }
int shufflePlaylist(FILE * fp) int shufflePlaylist(int fd)
{ {
int i; int i;
int ri; int ri;
...@@ -1331,7 +1336,7 @@ int shufflePlaylist(FILE * fp) ...@@ -1331,7 +1336,7 @@ int shufflePlaylist(FILE * fp)
return 0; return 0;
} }
int deletePlaylist(FILE * fp, char *utf8file) int deletePlaylist(int fd, char *utf8file)
{ {
char *file = utf8ToFsCharset(utf8file); char *file = utf8ToFsCharset(utf8file);
char *rfile = malloc(strlen(file) + strlen(".") + char *rfile = malloc(strlen(file) + strlen(".") +
...@@ -1348,13 +1353,13 @@ int deletePlaylist(FILE * fp, char *utf8file) ...@@ -1348,13 +1353,13 @@ int deletePlaylist(FILE * fp, char *utf8file)
free(rfile); free(rfile);
else { else {
free(rfile); free(rfile);
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"playlist \"%s\" not found", utf8file); "playlist \"%s\" not found", utf8file);
return -1; return -1;
} }
if (unlink(actualFile) < 0) { if (unlink(actualFile) < 0) {
commandError(fp, ACK_ERROR_SYSTEM, commandError(fd, ACK_ERROR_SYSTEM,
"problems deleting file", NULL); "problems deleting file", NULL);
return -1; return -1;
} }
...@@ -1362,7 +1367,7 @@ int deletePlaylist(FILE * fp, char *utf8file) ...@@ -1362,7 +1367,7 @@ int deletePlaylist(FILE * fp, char *utf8file)
return 0; return 0;
} }
int savePlaylist(FILE * fp, char *utf8file) int savePlaylist(int fd, char *utf8file)
{ {
FILE *fileP; FILE *fileP;
int i; int i;
...@@ -1373,7 +1378,7 @@ int savePlaylist(FILE * fp, char *utf8file) ...@@ -1373,7 +1378,7 @@ int savePlaylist(FILE * fp, char *utf8file)
char *url; char *url;
if (strstr(utf8file, "/")) { if (strstr(utf8file, "/")) {
commandError(fp, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"cannot save \"%s\", saving playlists to " "cannot save \"%s\", saving playlists to "
"subdirectories is not supported", utf8file); "subdirectories is not supported", utf8file);
return -1; return -1;
...@@ -1395,14 +1400,14 @@ int savePlaylist(FILE * fp, char *utf8file) ...@@ -1395,14 +1400,14 @@ int savePlaylist(FILE * fp, char *utf8file)
free(rfile); free(rfile);
if (0 == stat(actualFile, &st)) { if (0 == stat(actualFile, &st)) {
commandError(fp, ACK_ERROR_EXIST, "a file or directory already " commandError(fd, ACK_ERROR_EXIST, "a file or directory already "
"exists with the name \"%s\"", utf8file); "exists with the name \"%s\"", utf8file);
return -1; return -1;
} }
while (!(fileP = fopen(actualFile, "w")) && errno == EINTR) ; while (!(fileP = fopen(actualFile, "w")) && errno == EINTR) ;
if (fileP == NULL) { if (fileP == NULL) {
commandError(fp, ACK_ERROR_SYSTEM, "problems opening file", commandError(fd, ACK_ERROR_SYSTEM, "problems opening file",
NULL); NULL);
return -1; return -1;
} }
...@@ -1445,12 +1450,12 @@ int getPlaylistLength(void) ...@@ -1445,12 +1450,12 @@ int getPlaylistLength(void)
return playlist.length; return playlist.length;
} }
int seekSongInPlaylist(FILE * fp, int song, float time) int seekSongInPlaylist(int fd, int song, float time)
{ {
int i = song; int i = song;
if (song < 0 || song >= playlist.length) { if (song < 0 || song >= playlist.length) {
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song); "song doesn't exist: \"%i\"", song);
return -1; return -1;
} }
...@@ -1468,22 +1473,22 @@ int seekSongInPlaylist(FILE * fp, int song, float time) ...@@ -1468,22 +1473,22 @@ int seekSongInPlaylist(FILE * fp, int song, float time)
clearPlayerQueue(); clearPlayerQueue();
unlockPlaylistInteraction(); unlockPlaylistInteraction();
} }
} else if (playPlaylistOrderNumber(fp, i) < 0) } else if (playPlaylistOrderNumber(fd, i) < 0)
return -1; return -1;
if (playlist.current != i) { if (playlist.current != i) {
if (playPlaylistOrderNumber(fp, i) < 0) if (playPlaylistOrderNumber(fd, i) < 0)
return -1; return -1;
} }
return playerSeek(fp, playlist.songs[playlist.order[i]], time); return playerSeek(fd, playlist.songs[playlist.order[i]], time);
} }
int seekSongInPlaylistById(FILE * fp, int id, float time) int seekSongInPlaylistById(int fd, int id, float time)
{ {
checkSongId(id); checkSongId(id);
return seekSongInPlaylist(fp, playlist.idToPosition[id], time); return seekSongInPlaylist(fd, playlist.idToPosition[id], time);
} }
int getPlaylistSongId(int song) int getPlaylistSongId(int song)
...@@ -1491,8 +1496,8 @@ int getPlaylistSongId(int song) ...@@ -1491,8 +1496,8 @@ int getPlaylistSongId(int song)
return playlist.positionToId[song]; return playlist.positionToId[song];
} }
static int PlaylistIterFunc(FILE * fp, char *utf8file, static int PlaylistIterFunc(int fd, char *utf8file,
void (*IterFunc) (FILE * fp, char *utf8_file, void (*IterFunc) (int fd, char *utf8_file,
char **errored_File)) char **errored_File))
{ {
FILE *fileP; FILE *fileP;
...@@ -1518,14 +1523,14 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file, ...@@ -1518,14 +1523,14 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file,
free(rfile); free(rfile);
else { else {
free(rfile); free(rfile);
commandError(fp, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"playlist \"%s\" not found", utf8file); "playlist \"%s\" not found", utf8file);
return -1; return -1;
} }
while (!(fileP = fopen(actualFile, "r")) && errno == EINTR) ; while (!(fileP = fopen(actualFile, "r")) && errno == EINTR) ;
if (fileP == NULL) { if (fileP == NULL) {
commandError(fp, ACK_ERROR_SYSTEM, commandError(fd, ACK_ERROR_SYSTEM,
"problems opening file \"%s\"", utf8file); "problems opening file \"%s\"", utf8file);
return -1; return -1;
} }
...@@ -1547,7 +1552,7 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file, ...@@ -1547,7 +1552,7 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file,
strncat(s, "/", MAXPATHLEN - parentlen); strncat(s, "/", MAXPATHLEN - parentlen);
strncat(s, temp, MAXPATHLEN - parentlen - 1); strncat(s, temp, MAXPATHLEN - parentlen - 1);
if (strlen(s) >= MAXPATHLEN) { if (strlen(s) >= MAXPATHLEN) {
commandError(fp, commandError(fd,
ACK_ERROR_PLAYLIST_LOAD, ACK_ERROR_PLAYLIST_LOAD,
"\"%s\" too long", temp); "\"%s\" too long", temp);
free(temp); free(temp);
...@@ -1568,12 +1573,12 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file, ...@@ -1568,12 +1573,12 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file,
* for our current IterFunction set * for our current IterFunction set
* but just in case, we copy to s */ * but just in case, we copy to s */
strcpy(s, temp); strcpy(s, temp);
IterFunc(fp, s, &erroredFile); IterFunc(fd, s, &erroredFile);
} }
free(temp); free(temp);
} else if (slength == MAXPATHLEN) { } else if (slength == MAXPATHLEN) {
s[slength] = '\0'; s[slength] = '\0';
commandError(fp, ACK_ERROR_PLAYLIST_LOAD, commandError(fd, ACK_ERROR_PLAYLIST_LOAD,
"line in \"%s\" is too long", utf8file); "line in \"%s\" is too long", utf8file);
ERROR("line \"%s\" in playlist \"%s\" is too long\n", ERROR("line \"%s\" in playlist \"%s\" is too long\n",
s, utf8file); s, utf8file);
...@@ -1588,7 +1593,7 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file, ...@@ -1588,7 +1593,7 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file,
while (fclose(fileP) && errno == EINTR) ; while (fclose(fileP) && errno == EINTR) ;
if (erroredFile) { if (erroredFile) {
commandError(fp, ACK_ERROR_PLAYLIST_LOAD, commandError(fd, ACK_ERROR_PLAYLIST_LOAD,
"can't add file \"%s\"", erroredFile); "can't add file \"%s\"", erroredFile);
free(erroredFile); free(erroredFile);
return -1; return -1;
...@@ -1597,25 +1602,25 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file, ...@@ -1597,25 +1602,25 @@ static int PlaylistIterFunc(FILE * fp, char *utf8file,
return 0; return 0;
} }
static void PlaylistInfoPrintInfo(FILE * fp, char *utf8file, char **erroredfile) static void PlaylistInfoPrintInfo(int fd, char *utf8file, char **erroredfile)
{ {
Song *song = getSongFromDB(utf8file); Song *song = getSongFromDB(utf8file);
if (song) { if (song) {
printSongInfo(fp, song); printSongInfo(fd, song);
} else { } else {
myfprintf(fp, "file: %s\n", utf8file); fdprintf(fd, "file: %s\n", utf8file);
} }
} }
static void PlaylistInfoPrint(FILE * fp, char *utf8file, char **erroredfile) static void PlaylistInfoPrint(int fd, char *utf8file, char **erroredfile)
{ {
myfprintf(fp, "file: %s\n", utf8file); fdprintf(fd, "file: %s\n", utf8file);
} }
static void PlaylistLoadIterFunc(FILE * fp, char *temp, char **erroredFile) static void PlaylistLoadIterFunc(int fd, char *temp, char **erroredFile)
{ {
if (!getSongFromDB(temp) && !isRemoteUrl(temp)) { if (!getSongFromDB(temp) && !isRemoteUrl(temp)) {
} else if ((addToPlaylist(stderr, temp, 0)) < 0) { } else if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
/* for windows compatibilit, convert slashes */ /* for windows compatibilit, convert slashes */
char *temp2 = strdup(temp); char *temp2 = strdup(temp);
char *p = temp2; char *p = temp2;
...@@ -1624,7 +1629,7 @@ static void PlaylistLoadIterFunc(FILE * fp, char *temp, char **erroredFile) ...@@ -1624,7 +1629,7 @@ static void PlaylistLoadIterFunc(FILE * fp, char *temp, char **erroredFile)
*p = '/'; *p = '/';
p++; p++;
} }
if ((addToPlaylist(stderr, temp2, 0)) < 0) { if ((addToPlaylist(STDERR_FILENO, temp2, 0)) < 0) {
if (!*erroredFile) { if (!*erroredFile) {
*erroredFile = strdup(temp); *erroredFile = strdup(temp);
} }
...@@ -1633,15 +1638,15 @@ static void PlaylistLoadIterFunc(FILE * fp, char *temp, char **erroredFile) ...@@ -1633,15 +1638,15 @@ static void PlaylistLoadIterFunc(FILE * fp, char *temp, char **erroredFile)
} }
} }
int PlaylistInfo(FILE * fp, char *utf8file, int detail) int PlaylistInfo(int fd, char *utf8file, int detail)
{ {
if (detail) { if (detail) {
return PlaylistIterFunc(fp, utf8file, PlaylistInfoPrintInfo); return PlaylistIterFunc(fd, utf8file, PlaylistInfoPrintInfo);
} }
return PlaylistIterFunc(fp, utf8file, PlaylistInfoPrint); return PlaylistIterFunc(fd, utf8file, PlaylistInfoPrint);
} }
int loadPlaylist(FILE * fp, char *utf8file) int loadPlaylist(int fd, char *utf8file)
{ {
return PlaylistIterFunc(fp, utf8file, PlaylistLoadIterFunc); return PlaylistIterFunc(fd, utf8file, PlaylistLoadIterFunc);
} }
...@@ -38,61 +38,61 @@ void readPlaylistState(); ...@@ -38,61 +38,61 @@ void readPlaylistState();
void savePlaylistState(); void savePlaylistState();
int clearPlaylist(FILE * fp); int clearPlaylist(int fd);
int addToPlaylist(FILE * fp, char *file, int printId); int addToPlaylist(int fd, char *file, int printId);
int addSongToPlaylist(FILE * fp, Song * song, int printId); int addSongToPlaylist(int fd, Song * song, int printId);
int showPlaylist(FILE * fp); int showPlaylist(int fd);
int deleteFromPlaylist(FILE * fp, int song); int deleteFromPlaylist(int fd, int song);
int deleteFromPlaylistById(FILE * fp, int song); int deleteFromPlaylistById(int fd, int song);
int playlistInfo(FILE * fp, int song); int playlistInfo(int fd, int song);
int playlistId(FILE * fp, int song); int playlistId(int fd, int song);
int stopPlaylist(FILE * fp); int stopPlaylist(int fd);
int playPlaylist(FILE * fp, int song, int stopOnError); int playPlaylist(int fd, int song, int stopOnError);
int playPlaylistById(FILE * fp, int song, int stopOnError); int playPlaylistById(int fd, int song, int stopOnError);
int nextSongInPlaylist(FILE * fp); int nextSongInPlaylist(int fd);
void syncPlayerAndPlaylist(); void syncPlayerAndPlaylist();
int previousSongInPlaylist(FILE * fp); int previousSongInPlaylist(int fd);
int shufflePlaylist(FILE * fp); int shufflePlaylist(int fd);
int savePlaylist(FILE * fp, char *utf8file); int savePlaylist(int fd, char *utf8file);
int deletePlaylist(FILE * fp, char *utf8file); int deletePlaylist(int fd, char *utf8file);
int deletePlaylistById(FILE * fp, char *utf8file); int deletePlaylistById(int fd, char *utf8file);
void deleteASongFromPlaylist(Song * song); void deleteASongFromPlaylist(Song * song);
int moveSongInPlaylist(FILE * fp, int from, int to); int moveSongInPlaylist(int fd, int from, int to);
int moveSongInPlaylistById(FILE * fp, int id, int to); int moveSongInPlaylistById(int fd, int id, int to);
int swapSongsInPlaylist(FILE * fp, int song1, int song2); int swapSongsInPlaylist(int fd, int song1, int song2);
int swapSongsInPlaylistById(FILE * fp, int id1, int id2); int swapSongsInPlaylistById(int fd, int id1, int id2);
int loadPlaylist(FILE * fp, char *utf8file); int loadPlaylist(int fd, char *utf8file);
int getPlaylistRepeatStatus(); int getPlaylistRepeatStatus();
int setPlaylistRepeatStatus(FILE * fp, int status); int setPlaylistRepeatStatus(int fd, int status);
int getPlaylistRandomStatus(); int getPlaylistRandomStatus();
int setPlaylistRandomStatus(FILE * fp, int status); int setPlaylistRandomStatus(int fd, int status);
int getPlaylistCurrentSong(); int getPlaylistCurrentSong();
...@@ -104,17 +104,17 @@ unsigned long getPlaylistVersion(); ...@@ -104,17 +104,17 @@ unsigned long getPlaylistVersion();
void playPlaylistIfPlayerStopped(); void playPlaylistIfPlayerStopped();
int seekSongInPlaylist(FILE * fp, int song, float time); int seekSongInPlaylist(int fd, int song, float time);
int seekSongInPlaylistById(FILE * fp, int id, float time); int seekSongInPlaylistById(int fd, int id, float time);
void playlistVersionChange(); void playlistVersionChange();
int playlistChanges(FILE * fp, mpd_uint32 version); int playlistChanges(int fd, mpd_uint32 version);
int playlistChangesPosId(FILE * fp, mpd_uint32 version); int playlistChangesPosId(int fd, mpd_uint32 version);
int PlaylistInfo(FILE * fp, char *utf8file, int detail); int PlaylistInfo(int fd, char *utf8file, int detail);
char *getStateFile(); char *getStateFile();
......
/* the Music Player Daemon (MPD)
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
* This project's homepage is: http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* a very simple singly-linked-list structure for queues/buffers */
#include "sllist.h"
#include "utils.h"
static void init_strnode(struct strnode *x, char *s)
{
x->data = s;
x->next = NULL;
}
struct strnode *new_strnode(char *s)
{
struct strnode *x = malloc(sizeof(struct strnode));
init_strnode(x, s);
return x;
}
struct strnode *new_strnode_dup(char *s, const size_t size)
{
struct strnode *x = malloc(sizeof(struct strnode) + size);
x->next = NULL;
x->data = ((void *)x + sizeof(struct strnode));
memcpy((void *)x->data, (void*)s, size);
return x;
}
struct sllnode *new_sllnode(void *s, const size_t size)
{
struct sllnode *x = malloc(sizeof(struct sllnode) + size);
x->next = NULL;
x->size = size;
x->data = ((void *)x + sizeof(struct sllnode));
memcpy(x->data, (void *)s, size);
return x;
}
struct strnode *dup_strlist(struct strnode *old)
{
struct strnode *tmp, *new, *cur;
tmp = old;
cur = new = new_strnode_dup(tmp->data, strlen(tmp->data) + 1);
tmp = tmp->next;
while (tmp) {
cur->next = new_strnode_dup(tmp->data, strlen(tmp->data) + 1);
cur = cur->next;
tmp = tmp->next;
}
return new;
}
/* a very simple singly-linked-list structure for queues/buffers */
#ifndef SLLIST_H
#define SLLIST_H
#include "utils.h"
/* just free the entire structure if it's free-able, the 'data' member
* should _NEVER_ be explicitly freed
*
* there's no free command, iterate through them yourself and just
* call free() on it iff you malloc'd them */
struct strnode {
struct strnode *next;
char *data;
};
struct sllnode {
struct sllnode *next;
void *data;
size_t size;
};
struct strnode *new_strnode(char *s);
struct strnode *new_strnode_dup(char *s, const size_t size);
struct strnode *dup_strlist(struct strnode *old);
struct sllnode *new_sllnode(void *s, const size_t size);
#endif /* SLLIST_H */
...@@ -134,32 +134,32 @@ void freeSongList(SongList * list) ...@@ -134,32 +134,32 @@ void freeSongList(SongList * list)
freeList(list); freeList(list);
} }
void printSongUrl(FILE * fp, Song * song) void printSongUrl(int fd, Song * song)
{ {
if (song->parentDir && song->parentDir->path) { if (song->parentDir && song->parentDir->path) {
myfprintf(fp, "%s%s/%s\n", SONG_FILE, fdprintf(fd, "%s%s/%s\n", SONG_FILE,
getDirectoryPath(song->parentDir), song->url); getDirectoryPath(song->parentDir), song->url);
} else { } else {
myfprintf(fp, "%s%s\n", SONG_FILE, song->url); fdprintf(fd, "%s%s\n", SONG_FILE, song->url);
} }
} }
int printSongInfo(FILE * fp, Song * song) int printSongInfo(int fd, Song * song)
{ {
printSongUrl(fp, song); printSongUrl(fd, song);
if (song->tag) if (song->tag)
printMpdTag(fp, song->tag); printMpdTag(fd, song->tag);
return 0; return 0;
} }
int printSongInfoFromList(FILE * fp, SongList * list) int printSongInfoFromList(int fd, SongList * list)
{ {
ListNode *tempNode = list->firstNode; ListNode *tempNode = list->firstNode;
while (tempNode != NULL) { while (tempNode != NULL) {
printSongInfo(fp, (Song *) tempNode->data); printSongInfo(fd, (Song *) tempNode->data);
tempNode = tempNode->nextNode; tempNode = tempNode->nextNode;
} }
...@@ -174,7 +174,7 @@ void writeSongInfoFromList(FILE * fp, SongList * list) ...@@ -174,7 +174,7 @@ void writeSongInfoFromList(FILE * fp, SongList * list)
while (tempNode != NULL) { while (tempNode != NULL) {
myfprintf(fp, "%s%s\n", SONG_KEY, tempNode->key); myfprintf(fp, "%s%s\n", SONG_KEY, tempNode->key);
printSongInfo(fp, (Song *) tempNode->data); printSongInfo(fileno(fp), (Song *) tempNode->data);
myfprintf(fp, "%s%li\n", SONG_MTIME, myfprintf(fp, "%s%li\n", SONG_MTIME,
(long)((Song *) tempNode->data)->mtime); (long)((Song *) tempNode->data)->mtime);
tempNode = tempNode->nextNode; tempNode = tempNode->nextNode;
......
...@@ -58,9 +58,9 @@ void freeSongList(SongList * list); ...@@ -58,9 +58,9 @@ void freeSongList(SongList * list);
Song *addSongToList(SongList * list, char *url, char *utf8path, Song *addSongToList(SongList * list, char *url, char *utf8path,
int songType, struct _Directory *parentDir); int songType, struct _Directory *parentDir);
int printSongInfo(FILE * fp, Song * song); int printSongInfo(int fd, Song * song);
int printSongInfoFromList(FILE * fp, SongList * list); int printSongInfoFromList(int fd, SongList * list);
void writeSongInfoFromList(FILE * fp, SongList * list); void writeSongInfoFromList(FILE * fp, SongList * list);
...@@ -69,7 +69,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, ...@@ -69,7 +69,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list,
int updateSongInfo(Song * song); int updateSongInfo(Song * song);
void printSongUrl(FILE * fp, Song * song); void printSongUrl(int fd, Song * song);
char *getSongUrl(Song * song); char *getSongUrl(Song * song);
......
...@@ -34,15 +34,15 @@ void initStats(void) ...@@ -34,15 +34,15 @@ void initStats(void)
stats.numberOfSongs = 0; stats.numberOfSongs = 0;
} }
int printStats(FILE * fp) int printStats(int fd)
{ {
myfprintf(fp, "artists: %li\n", getNumberOfTagItems(TAG_ITEM_ARTIST)); fdprintf(fd, "artists: %li\n", getNumberOfTagItems(TAG_ITEM_ARTIST));
myfprintf(fp, "albums: %li\n", getNumberOfTagItems(TAG_ITEM_ALBUM)); fdprintf(fd, "albums: %li\n", getNumberOfTagItems(TAG_ITEM_ALBUM));
myfprintf(fp, "songs: %i\n", stats.numberOfSongs); fdprintf(fd, "songs: %i\n", stats.numberOfSongs);
myfprintf(fp, "uptime: %li\n", time(NULL) - stats.daemonStart); fdprintf(fd, "uptime: %li\n", time(NULL) - stats.daemonStart);
myfprintf(fp, "playtime: %li\n", fdprintf(fd, "playtime: %li\n",
(long)(getPlayerTotalPlayTime() + 0.5)); (long)(getPlayerTotalPlayTime() + 0.5));
myfprintf(fp, "db_playtime: %li\n", stats.dbPlayTime); fdprintf(fd, "db_playtime: %li\n", stats.dbPlayTime);
myfprintf(fp, "db_update: %li\n", getDbModTime()); fdprintf(fd, "db_update: %li\n", getDbModTime());
return 0; return 0;
} }
...@@ -35,6 +35,6 @@ extern Stats stats; ...@@ -35,6 +35,6 @@ extern Stats stats;
void initStats(); void initStats();
int printStats(FILE * fp); int printStats(int fd);
#endif #endif
...@@ -121,15 +121,15 @@ void initTagConfig(void) ...@@ -121,15 +121,15 @@ void initTagConfig(void)
free(temp); free(temp);
} }
void printMpdTag(FILE * fp, MpdTag * tag) void printMpdTag(int fd, MpdTag * tag)
{ {
int i; int i;
if (tag->time >= 0) if (tag->time >= 0)
myfprintf(fp, "Time: %i\n", tag->time); fdprintf(fd, "Time: %i\n", tag->time);
for (i = 0; i < tag->numOfItems; i++) { for (i = 0; i < tag->numOfItems; i++) {
myfprintf(fp, "%s: %s\n", mpdTagItemKeys[tag->items[i].type], fdprintf(fd, "%s: %s\n", mpdTagItemKeys[tag->items[i].type],
tag->items[i].value); tag->items[i].value);
} }
} }
......
...@@ -78,7 +78,7 @@ void addItemToMpdTagWithLen(MpdTag * tag, int itemType, char *value, int len); ...@@ -78,7 +78,7 @@ void addItemToMpdTagWithLen(MpdTag * tag, int itemType, char *value, int len);
#define addItemToMpdTag(tag, itemType, value) \ #define addItemToMpdTag(tag, itemType, value) \
addItemToMpdTagWithLen(tag, itemType, value, strlen(value)) addItemToMpdTagWithLen(tag, itemType, value, strlen(value))
void printMpdTag(FILE * fp, MpdTag * tag); void printMpdTag(int fd, MpdTag * tag);
MpdTag *mpdTagDup(MpdTag * tag); MpdTag *mpdTagDup(MpdTag * tag);
......
...@@ -147,7 +147,7 @@ void visitInTagTracker(int type, char *str) ...@@ -147,7 +147,7 @@ void visitInTagTracker(int type, char *str)
((TagTrackerItem *) item)->visited = 1; ((TagTrackerItem *) item)->visited = 1;
} }
void printVisitedInTagTracker(FILE * fp, int type) void printVisitedInTagTracker(int fd, int type)
{ {
ListNode *node; ListNode *node;
TagTrackerItem *item; TagTrackerItem *item;
...@@ -160,8 +160,8 @@ void printVisitedInTagTracker(FILE * fp, int type) ...@@ -160,8 +160,8 @@ void printVisitedInTagTracker(FILE * fp, int type)
while (node) { while (node) {
item = node->data; item = node->data;
if (item->visited) { if (item->visited) {
myfprintf(fp, "%s: %s\n", mpdTagItemKeys[type], fdprintf(fd, "%s: %s\n", mpdTagItemKeys[type],
node->key); node->key);
} }
node = node->nextNode; node = node->nextNode;
} }
......
...@@ -33,6 +33,6 @@ void resetVisitedFlagsInTagTracker(int type); ...@@ -33,6 +33,6 @@ void resetVisitedFlagsInTagTracker(int type);
void visitInTagTracker(int type, char *str); void visitInTagTracker(int type, char *str);
void printVisitedInTagTracker(FILE * fp, int type); void printVisitedInTagTracker(int fd, int type);
#endif #endif
...@@ -21,7 +21,13 @@ ...@@ -21,7 +21,13 @@
#include "../config.h" #include "../config.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char *myFgets(char *buffer, int bufferSize, FILE * fp); char *myFgets(char *buffer, int bufferSize, FILE * fp);
...@@ -37,4 +43,39 @@ char *appendToString(char *dest, const char *src); ...@@ -37,4 +43,39 @@ char *appendToString(char *dest, const char *src);
unsigned long readLEuint32(const unsigned char *p); unsigned long readLEuint32(const unsigned char *p);
/* trivial functions, keep them inlined */
static inline int xopen(const char *path, int flags, mode_t mode)
{
int fd;
while(0>(fd = open(path,flags,mode)) && errno == EINTR);
return fd;
}
static inline void xclose(int fd)
{
while (close(fd) && errno == EINTR);
}
static inline ssize_t xread(int fd, void *buf, size_t len)
{
ssize_t nr;
while (1) {
nr = read(fd, buf, len);
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
continue;
return nr;
}
}
static inline ssize_t xwrite(int fd, const void *buf, size_t len)
{
ssize_t nr;
while (1) {
nr = write(fd, buf, len);
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
continue;
return nr;
}
}
#endif #endif
...@@ -169,7 +169,7 @@ static int getOssVolumeLevel(void) ...@@ -169,7 +169,7 @@ static int getOssVolumeLevel(void)
return left; return left;
} }
static int changeOssVolumeLevel(FILE * fp, int change, int rel) static int changeOssVolumeLevel(int fd, int change, int rel)
{ {
int current; int current;
int new; int new;
...@@ -177,7 +177,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel) ...@@ -177,7 +177,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel)
if (rel) { if (rel) {
if ((current = getOssVolumeLevel()) < 0) { if ((current = getOssVolumeLevel()) < 0) {
commandError(fp, ACK_ERROR_SYSTEM, commandError(fd, ACK_ERROR_SYSTEM,
"problem getting current volume", NULL); "problem getting current volume", NULL);
return -1; return -1;
} }
...@@ -198,7 +198,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel) ...@@ -198,7 +198,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel)
if (ioctl(volume_ossFd, MIXER_WRITE(volume_ossControl), &level) < 0) { if (ioctl(volume_ossFd, MIXER_WRITE(volume_ossControl), &level) < 0) {
closeOssMixer(); closeOssMixer();
commandError(fp, ACK_ERROR_SYSTEM, "problems setting volume", commandError(fd, ACK_ERROR_SYSTEM, "problems setting volume",
NULL); NULL);
return -1; return -1;
} }
...@@ -328,7 +328,7 @@ static int getAlsaVolumeLevel(void) ...@@ -328,7 +328,7 @@ static int getAlsaVolumeLevel(void)
return ret; return ret;
} }
static int changeAlsaVolumeLevel(FILE * fp, int change, int rel) static int changeAlsaVolumeLevel(int fd, int change, int rel)
{ {
float vol; float vol;
long level; long level;
...@@ -361,7 +361,7 @@ static int changeAlsaVolumeLevel(FILE * fp, int change, int rel) ...@@ -361,7 +361,7 @@ static int changeAlsaVolumeLevel(FILE * fp, int change, int rel)
if ((err = if ((err =
snd_mixer_selem_set_playback_volume_all(volume_alsaElem, snd_mixer_selem_set_playback_volume_all(volume_alsaElem,
level)) < 0) { level)) < 0) {
commandError(fp, ACK_ERROR_SYSTEM, "problems setting volume", commandError(fd, ACK_ERROR_SYSTEM, "problems setting volume",
NULL); NULL);
WARNING("problems setting alsa volume: %s\n", WARNING("problems setting alsa volume: %s\n",
snd_strerror(err)); snd_strerror(err));
...@@ -471,7 +471,7 @@ int getVolumeLevel(void) ...@@ -471,7 +471,7 @@ int getVolumeLevel(void)
} }
} }
static int changeSoftwareVolume(FILE * fp, int change, int rel) static int changeSoftwareVolume(int fd, int change, int rel)
{ {
int new = change; int new = change;
...@@ -499,19 +499,19 @@ static int changeSoftwareVolume(FILE * fp, int change, int rel) ...@@ -499,19 +499,19 @@ static int changeSoftwareVolume(FILE * fp, int change, int rel)
return 0; return 0;
} }
int changeVolumeLevel(FILE * fp, int change, int rel) int changeVolumeLevel(int fd, int change, int rel)
{ {
switch (volume_mixerType) { switch (volume_mixerType) {
#ifdef HAVE_ALSA #ifdef HAVE_ALSA
case VOLUME_MIXER_TYPE_ALSA: case VOLUME_MIXER_TYPE_ALSA:
return changeAlsaVolumeLevel(fp, change, rel); return changeAlsaVolumeLevel(fd, change, rel);
#endif #endif
#ifdef HAVE_OSS #ifdef HAVE_OSS
case VOLUME_MIXER_TYPE_OSS: case VOLUME_MIXER_TYPE_OSS:
return changeOssVolumeLevel(fp, change, rel); return changeOssVolumeLevel(fd, change, rel);
#endif #endif
case VOLUME_MIXER_TYPE_SOFTWARE: case VOLUME_MIXER_TYPE_SOFTWARE:
return changeSoftwareVolume(fp, change, rel); return changeSoftwareVolume(fd, change, rel);
default: default:
return 0; return 0;
break; break;
......
...@@ -35,6 +35,6 @@ void finishVolume(); ...@@ -35,6 +35,6 @@ void finishVolume();
int getVolumeLevel(); int getVolumeLevel();
int changeVolumeLevel(FILE * fp, int change, int rel); int changeVolumeLevel(int fd, int change, int rel);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment