From: Michal Nazarewicz <[email protected]>
If $MPD_PORT is not a number, i.e. does not start with a digit,
attempt to resolve it using getservbyname, i.e. by reading the
/etc/services database.
---
src/settings.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/src/settings.c b/src/settings.c
index f2799df..d8e9247 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -29,7 +29,10 @@
#include <mpd/settings.h>
#include "config.h"
+#include <arpa/inet.h>
#include <assert.h>
+#include <ctype.h>
+#include <netdb.h>
#include <string.h>
#include <stdlib.h>
@@ -100,6 +103,21 @@ mpd_check_host(const char *host, char **password_r)
}
/**
+ * Parse port number (which can be a service name).
+ */
+static unsigned
+mpd_parse_port(const char *str)
+{
+ if (!*str)
+ return 0;
+ if (isdigit(str[0]))
+ return atoi(str);
+
+ struct servent *servent = getservbyname(str, "tcp");
+ return servent ? ntohs(servent->s_port) : 0;
+}
+
+/**
* Parses the port specification. If not specified (0), it attempts
* to load it from the environment variable MPD_PORT.
*/
@@ -109,7 +127,7 @@ mpd_check_port(unsigned port)
if (port == 0) {
const char *env_port = getenv("MPD_PORT");
if (env_port != NULL)
- port = atoi(env_port);
+ port = mpd_parse_port(env_port);
}
return port;
--
2.2.0.rc0.207.ga3a616c
_______________________________________________
mpd-devel mailing list
[email protected]
http://mailman.blarg.de/listinfo/mpd-devel