diff -ruN vdr-1.4.7-orig/Makefile vdr-1.4.7-darwin-2/Makefile
--- vdr-1.4.7-orig/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -8,6 +8,8 @@
 
 .DELETE_ON_ERROR:
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 CC       ?= gcc
 CFLAGS   ?= -g -O2 -Wall
 
@@ -17,8 +19,14 @@
 LSIDIR   = ./libsi
 MANDIR   = /usr/local/man
 BINDIR   = /usr/local/bin
-LIBS     = -ljpeg -lpthread -ldl -lcap
-INCLUDES =
+ifeq ($(APPLE_DARWIN), 1)
+    LIBS     = -ljpeg -lpthread -ldl
+    INCLUDES = -I/sw/include
+    LIBDIRS += -L/sw/lib
+else
+    LIBS     = -ljpeg -lpthread -ldl -lcap
+    INCLUDES =
+endif
 
 PLUGINDIR= ./PLUGINS
 PLUGINLIBDIR= $(PLUGINDIR)/lib
@@ -32,12 +40,21 @@
 
 SILIB    = $(LSIDIR)/libsi.a
 
+ifeq ($(APPLE_DARWIN), 1)
+OBJS = audio.o channels.o ci.o config.o cutter.o device.o diseqc.o dvbdevice.o dvbosd.o\
+       dvbplayer.o dvbspu.o eit.o eitscan.o epg.o filter.o font.o i18n.o interface.o keys.o\
+       lirc.o darwinutils.o menu.o menuitems.o nit.o osdbase.o osd.o pat.o player.o plugin.o rcu.o\
+       receiver.o recorder.o recording.o remote.o remux.o ringbuffer.o sdt.o sections.o\
+       skinclassic.o skins.o skinsttng.o sources.o spu.o status.o svdrp.o themes.o thread.o\
+       timers.o tools.o transfer.o vdr.o videodir.o
+else
 OBJS = audio.o channels.o ci.o config.o cutter.o device.o diseqc.o dvbdevice.o dvbosd.o\
        dvbplayer.o dvbspu.o eit.o eitscan.o epg.o filter.o font.o i18n.o interface.o keys.o\
        lirc.o menu.o menuitems.o nit.o osdbase.o osd.o pat.o player.o plugin.o rcu.o\
        receiver.o recorder.o recording.o remote.o remux.o ringbuffer.o sdt.o sections.o\
        skinclassic.o skins.o skinsttng.o sources.o spu.o status.o svdrp.o themes.o thread.o\
        timers.o tools.o transfer.o vdr.o videodir.o
+endif
 
 FIXFONT_ISO8859_1 = -adobe-courier-bold-r-normal--25-*-100-100-m-*-iso8859-1
 OSDFONT_ISO8859_1 = -adobe-helvetica-medium-r-normal--23-*-100-100-p-*-iso8859-1
@@ -115,7 +132,11 @@
 # The main program:
 
 vdr: $(OBJS) $(SILIB)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) -dynamic $(OBJS) $(NCURSESLIB) $(LIBS) $(LIBDIRS) $(SILIB) -o vdr
+else
 	$(CXX) $(CXXFLAGS) -rdynamic $(OBJS) $(NCURSESLIB) $(LIBS) $(LIBDIRS) $(SILIB) -o vdr
+endif
 
 # The font files:
 
diff -ruN vdr-1.4.7-orig/PLUGINS/src/hello/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/hello/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/hello/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/hello/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = hello
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +72,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/osddemo/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/osddemo/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/osddemo/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/osddemo/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = osddemo
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +72,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/servicedemo/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/servicedemo/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/servicedemo/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/servicedemo/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -11,6 +11,8 @@
 PLUGIN1 = svccli
 PLUGIN2 = svcsvr
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN1).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -18,7 +20,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -68,12 +74,22 @@
 all: libvdr-$(PLUGIN1).so libvdr-$(PLUGIN2).so
 
 libvdr-$(PLUGIN1).so: $(PLUGIN1).o
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(PLUGIN1).o -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(PLUGIN1).o -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 libvdr-$(PLUGIN2).so: $(PLUGIN2).o
-	$(CXX) $(CXXFLAGS) -shared $(PLUGIN2).o -o $@
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(PLUGIN2).o -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
+	$(CXX) $(CXXFLAGS) -shared $(PLUGIN1).o -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/skincurses/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/skincurses/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/skincurses/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/skincurses/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = skincurses
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,12 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +73,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -lncurses -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -lncurses -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/sky/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/sky/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/sky/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/sky/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = sky
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +72,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/sky/sky.c vdr-1.4.7-darwin-2/PLUGINS/src/sky/sky.c
--- vdr-1.4.7-orig/PLUGINS/src/sky/sky.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/sky/sky.c	2009-02-11 21:48:53.000000000 +0200
@@ -14,6 +14,9 @@
 #include <vdr/device.h>
 #include <vdr/plugin.h>
 #include <vdr/sources.h>
+#ifdef __APPLE__
+#include <vdr/darwinutils.h>
+#endif
 
 static const char *VERSION        = "0.3.5";
 static const char *DESCRIPTION    = "Sky Digibox interface";
@@ -33,7 +36,11 @@
 bool cSkyChannel::Parse(const char *s)
 {
   char *id = NULL;
+#ifdef __APPLE__
+  if (2 == sscanf_darwin(s, "%a[^:]:%d", &id, &digiboxChannelNumber))
+#else
   if (2 == sscanf(s, "%a[^:]:%d", &id, &digiboxChannelNumber))
+#endif
      channelID = tChannelID::FromString(id);
   free(id);
   return digiboxChannelNumber && channelID.Valid();
diff -ruN vdr-1.4.7-orig/PLUGINS/src/status/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/status/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/status/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/status/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = status
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +72,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/PLUGINS/src/svdrpdemo/Makefile vdr-1.4.7-darwin-2/PLUGINS/src/svdrpdemo/Makefile
--- vdr-1.4.7-orig/PLUGINS/src/svdrpdemo/Makefile	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/PLUGINS/src/svdrpdemo/Makefile	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,8 @@
 #
 PLUGIN = svdrpdemo
 
+APPLE_DARWIN = $(shell gcc -dumpmachine | grep -q 'apple-darwin' && echo "1" || echo "0")
+
 ### The version number of this plugin (taken from the main source file):
 
 VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g')
@@ -16,7 +18,11 @@
 ### The C++ compiler and options:
 
 CXX      ?= g++
-CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+ifeq ($(APPLE_DARWIN), 1)
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual -fno-common -bundle -flat_namespace -undefined suppress
+else
+    CXXFLAGS ?= -fPIC -g -O2 -Wall -Woverloaded-virtual
+endif
 
 ### The directory environment:
 
@@ -66,8 +72,13 @@
 all: libvdr-$(PLUGIN).so
 
 libvdr-$(PLUGIN).so: $(OBJS)
+ifeq ($(APPLE_DARWIN), 1)
+	$(CXX) $(CXXFLAGS) $(OBJS) -o $@
+	@cp $@ $(LIBDIR)/$@.$(APIVERSION)
+else
 	$(CXX) $(CXXFLAGS) -shared $(OBJS) -o $@
 	@cp --remove-destination $@ $(LIBDIR)/$@.$(APIVERSION)
+endif
 
 dist: clean
 	@-rm -rf $(TMPDIR)/$(ARCHIVE)
diff -ruN vdr-1.4.7-orig/channels.c vdr-1.4.7-darwin-2/channels.c
--- vdr-1.4.7-orig/channels.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/channels.c	2009-02-11 21:48:53.000000000 +0200
@@ -13,6 +13,9 @@
 #include "device.h"
 #include "epg.h"
 #include "timers.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // IMPORTANT NOTE: in the 'sscanf()' calls there is a blank after the '%d'
 // format characters in order to allow any number of blanks after a numeric
@@ -134,7 +137,11 @@
   int tid;
   int sid;
   int rid = 0;
+#ifdef __APPLE__
+  int fields = sscanf_darwin(s, "%a[^-]-%d-%d-%d-%d", &sourcebuf, &nid, &tid, &sid, &rid);
+#else
   int fields = sscanf(s, "%a[^-]-%d-%d-%d-%d", &sourcebuf, &nid, &tid, &sid, &rid);
+#endif
   if (fields == 4 || fields == 5) {
      int source = cSource::FromString(sourcebuf);
      free(sourcebuf);
@@ -706,7 +713,11 @@
      char *vpidbuf = NULL;
      char *apidbuf = NULL;
      char *caidbuf = NULL;
+#ifdef __APPLE__
+     int fields = sscanf_darwin(s, "%a[^:]:%d :%a[^:]:%a[^:] :%d :%a[^:]:%a[^:]:%d :%a[^:]:%d :%d :%d :%d ", &namebuf, &frequency, &parambuf, &sourcebuf, &srate, &vpidbuf, &apidbuf, &tpid, &caidbuf, &sid, &nid, &tid, &rid);
+#else
      int fields = sscanf(s, "%a[^:]:%d :%a[^:]:%a[^:] :%d :%a[^:]:%a[^:]:%d :%a[^:]:%d :%d :%d :%d ", &namebuf, &frequency, &parambuf, &sourcebuf, &srate, &vpidbuf, &apidbuf, &tpid, &caidbuf, &sid, &nid, &tid, &rid);
+#endif
      if (fields >= 9) {
         if (fields == 9) {
            // allow reading of old format
diff -ruN vdr-1.4.7-orig/ci.c vdr-1.4.7-darwin-2/ci.c
--- vdr-1.4.7-orig/ci.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/ci.c	2009-02-11 21:48:53.000000000 +0200
@@ -9,8 +9,13 @@
 
 #include "ci.h"
 #include <ctype.h>
-#include <linux/dvb/ca.h>
+#ifdef __APPLE__
+#include <sys/malloc.h>
+#include <stdlib.h>
+#else
 #include <malloc.h>
+#endif
+#include <linux/dvb/ca.h>
 #include <netinet/in.h>
 #include <poll.h>
 #include <string.h>
diff -ruN vdr-1.4.7-orig/darwinutils.c vdr-1.4.7-darwin-2/darwinutils.c
--- vdr-1.4.7-orig/darwinutils.c	1970-01-01 02:00:00.000000000 +0200
+++ vdr-1.4.7-darwin-2/darwinutils.c	2009-02-11 21:48:53.000000000 +0200
@@ -0,0 +1,597 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <machine/limits.h>
+#include <errno.h>
+#include <unistd.h>
+#include "darwinutils.h"
+#include "tools.h"
+#include <sys/param.h>
+
+# define __canonicalize_file_name_darwin canonicalize_file_name_darwin
+#  define __getcwd getcwd
+# define __readlink readlink
+
+char *strchrnul_darwin(const char *s, int c_in)
+{
+  char c = c_in;
+  while(*s && (*s != c))
+    s++;
+
+  return (char *) s;
+}
+
+char *strndup_darwin(const char *str, size_t len)
+{
+  size_t t;
+  char *dest;
+  
+  t = strlen(str);
+  if(len < t)
+  {
+    t = len;
+  }
+  
+  dest = (char *)calloc(t + 1, 1);
+  
+  if (NULL != dest)
+  {
+    memcpy(dest, str, t);
+    dest[t] = '\0';
+  }
+
+  return dest;
+}
+
+ssize_t getdelim_darwin(char **lineptr, size_t *n, int delimiter, FILE *fp)
+{
+  ssize_t result = 0;
+  size_t cur_len = 0;
+
+  if(lineptr == NULL || n == NULL || fp == NULL)
+  {
+    errno = EINVAL;
+    return -1;
+  }
+
+  flockfile (fp);
+
+  if(*lineptr == NULL || *n == 0)
+  {
+    *n = 120;
+    *lineptr = (char *) malloc (*n);
+    if (*lineptr == NULL)
+    {
+      result = -1;
+      goto unlock_return;
+	}
+  }
+
+  for(;;)
+  {
+    int i;
+    i = getc (fp);
+    if (i == EOF)
+    {
+      result = -1;
+      break;
+    }
+
+    if(cur_len + 1 >= *n)
+	{
+      size_t needed_max =
+      SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
+      size_t needed = 2 * *n + 1;
+      char *new_lineptr;
+
+      if(needed_max < needed)
+        needed = needed_max;
+      if(cur_len + 1 >= needed)
+	  {
+	    result = -1;
+	    goto unlock_return;
+	  }
+
+	  new_lineptr = (char *)realloc(*lineptr, needed);
+      if(new_lineptr == NULL)
+      {
+        result = -1;
+        goto unlock_return;
+      }
+
+      *lineptr = new_lineptr;
+      *n = needed;
+    }
+
+    (*lineptr)[cur_len] = i;
+    cur_len++;
+
+    if (i == delimiter)
+      break;
+  }
+  (*lineptr)[cur_len] = '\0';
+  result = cur_len ? cur_len : result;
+
+  unlock_return:
+  funlockfile (fp);
+  return result;
+}
+
+ssize_t getline_darwin(char **lineptr, size_t *n, FILE *stream)
+{
+  return getdelim_darwin(lineptr, n, '\n', stream);
+}
+
+char * __realpath_darwin(const char *name, char *resolved)
+{
+  char *rpath, *dest, *extra_buf = NULL;
+  const char *start, *end, *rpath_limit;
+  long int path_max;
+  int num_links = 0;
+
+  if(name == NULL)
+  {
+    __set_errno (EINVAL);
+    return NULL;
+  }
+
+  if(name[0] == '\0')
+  {
+    __set_errno (ENOENT);
+    return NULL;
+  }
+
+  path_max = PATH_MAX;
+
+  if(resolved == NULL)
+  {
+    rpath = (char*)malloc (path_max);
+    if (rpath == NULL)
+      return NULL;
+  }
+  else
+    rpath = resolved;
+  rpath_limit = rpath + path_max;
+
+  if(name[0] != '/')
+  {
+    if(!__getcwd (rpath, path_max))
+    {
+      rpath[0] = '\0';
+      goto error;
+    }
+    dest = strchr (rpath, '\0');
+  }
+  else
+  {
+    rpath[0] = '/';
+    dest = rpath + 1;
+  }
+
+  for (start = end = name; *start; start = end)
+  {
+    struct stat st;
+    int n;
+
+    while(*start == '/')
+      ++start;
+
+    for(end = start; *end && *end != '/'; ++end)
+      ;
+
+    if(end - start == 0)
+      break;
+    else if(end - start == 1 && start[0] == '.')
+      ;
+    else if(end - start == 2 && start[0] == '.' && start[1] == '.')
+    {
+      if (dest > rpath + 1)
+        while ((--dest)[-1] != '/')
+          ;
+    }
+    else
+    {
+      size_t new_size;
+
+      if(dest[-1] != '/')
+        *dest++ = '/';
+
+      if(dest + (end - start) >= rpath_limit)
+      {
+        ptrdiff_t dest_offset = dest - rpath;
+        char *new_rpath;
+
+        if(resolved)
+        {
+          __set_errno(ENAMETOOLONG);
+          if(dest > rpath + 1)
+            dest--;
+          *dest = '\0';
+          goto error;
+        }
+        new_size = rpath_limit - rpath;
+        if(end - start + 1 > path_max)
+          new_size += end - start + 1;
+        else
+          new_size += path_max;
+        new_rpath = (char *) realloc (rpath, new_size);
+        if(new_rpath == NULL)
+          goto error;
+        rpath = new_rpath;
+        rpath_limit = rpath + new_size;
+
+        dest = rpath + dest_offset;
+      }
+
+      memcpy (dest, start, end - start);
+      dest += end - start;
+      *dest = '\0';
+
+      if(lstat (rpath, &st) < 0)
+        goto error;
+
+      if(S_ISLNK(st.st_mode))
+      {
+        char *buf = (char*)__alloca (path_max);
+        size_t len;
+        
+        if(++num_links > MAXSYMLINKS)
+        {
+          __set_errno(ELOOP);
+          goto error;
+        }
+
+        n = __readlink(rpath, buf, path_max);
+        if (n < 0)
+          goto error;
+        buf[n] = '\0';
+
+        if(!extra_buf)
+          extra_buf = (char*)__alloca(path_max);
+
+        len = strlen (end);
+        if((long int) (n + len) >= path_max)
+        {
+          __set_errno (ENAMETOOLONG);
+          goto error;
+        }
+
+        memmove(&extra_buf[n], end, len + 1);
+        name = end = (char*)memcpy(extra_buf, buf, n);
+
+        if(buf[0] == '/')
+          dest = rpath + 1;
+        else if (dest > rpath + 1)
+          while ((--dest)[-1] != '/');
+      }
+    }
+  }
+  if(dest > rpath + 1 && dest[-1] == '/')
+    --dest;
+  *dest = '\0';
+
+  return resolved ? (char*)memcpy (resolved, rpath, dest - rpath + 1) : rpath;
+
+  error:
+  if(resolved)
+    strcpy (resolved, rpath);
+  else
+    free (rpath);
+  return NULL;
+}
+
+
+char *__canonicalize_file_name_darwin(const char *name)
+{
+  return __realpath_darwin(name, NULL);
+}
+
+int sscanf_darwin(const char *str, const char *format, ...)
+{
+  va_list list;
+  const char *p;
+  char *s, *c;
+  char **a;
+  int **d;
+  unsigned int **x, **u;
+  int pos = 0, width = 0, count = 0;
+  char temp[1];
+  
+  va_start( list, format );
+  
+  for( p = format ; *p ; ++p )
+  {
+    width=0;
+    if( str[pos]==' ')
+    {
+      pos++;
+    }
+
+    if( *p != '%' )
+    {
+    }
+    else
+    {
+      switch( *++p )
+      {
+
+        case '0':
+          if(str[pos]=='0')
+            pos++;
+            p++;
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+        case '8':
+        case '9':
+        {
+          temp[0]=*p;
+          width = atoi(temp);
+          switch( *++p )
+          {
+            case 'X':
+            {
+              x = va_arg(list, unsigned int**);
+              read_x_w(str, x, width, &pos);
+              count++;
+              p++;
+              break;
+            }
+
+            case 's':
+            {
+              //string
+              s = va_arg(list, char *);
+              read_s_w(str, s, width, &pos);
+              count++;
+              pos++;
+              p++;
+              break;
+            }
+            default:
+            ;
+          }
+          break;
+          // length
+        }
+        case 's':
+        {
+          //string
+          p++;
+          break;
+        }
+
+        case 'c':
+        {
+          //char
+          c = va_arg(list, char*);
+          read_c(str, c, *++p, &pos);
+          pos++;
+          count++;
+          break;
+        }
+
+
+        case 'a':
+        {
+          //string with alloc
+          if(*(p+=2)=='^')
+          {
+            if(*(p+1)!='\n')
+            {
+              a = va_arg(list, char**);
+              read_a(str, a, *++p, &pos);
+              p++;
+              count++;
+              pos++;
+            }
+            else
+            {
+              a = va_arg(list, char**);
+              read_a(str, a, *++p, &pos);
+              p++;
+              count++;
+              pos++;
+            }
+          }
+          break;
+        }
+
+        case 'd':
+        {
+          //int
+          d = va_arg(list, int**);
+          if(*(p+1)==' ')
+            read_d(str, d, *(p+=2), &pos);
+          else
+            read_d(str, d, *++p, &pos);
+          pos++;
+          count++;
+          p--;
+          break;
+        }
+
+        case 'u':
+        {
+          //int
+          u = va_arg(list, unsigned int**);
+          if(*(p+1)==' ')
+            read_u(str, u, *(p+=2), &pos);
+          else
+            read_u(str, u, *++p, &pos);
+          count++;
+          pos++;
+          p--;
+          break;
+        }
+
+        case 'X':
+        {
+          //hex
+          x = va_arg(list, unsigned int**);
+          read_x(str, x, *++p, &pos);
+          count++;
+          pos++;
+          break;
+        }
+        
+        default:
+          break;
+      }
+    }
+  }
+  va_end( list );
+  return count;
+  
+}
+
+void read_a(const char *str, char **ptr, const char delimiter, int *position)
+{
+  int i=0, j=0;
+  int start = *position;
+  char *temp;
+  
+  while(str[*position]!=delimiter&&str[*position]!='\0')
+  {
+    (*position)++;
+    i++;
+  }
+  temp = (char*)calloc(i+1, sizeof(char) );
+  for( j=0;j<i;j++)
+  {
+    temp[j]=str[start];
+    start++;
+  }
+  temp[j]='\0';
+  *ptr = temp;
+}
+
+
+void read_d(const char *str, int **ptr, const char delimiter, int *position)
+{
+
+  int i=0, j=0;
+  int start = *position;
+  char *temp;
+
+  while(str[*position]!=delimiter&&str[*position]!='\0'&&str[*position]!=' ')
+  {
+    (*position)++;
+    i++;
+  }
+  temp = (char*)calloc(i+1, sizeof(char) );
+  for( j=0;j<i;j++)
+  {
+    temp[j]=str[start];
+    start++;
+  }
+  temp[j]='\0';
+  *ptr = (int*)atoi(temp);
+}
+
+
+void read_x(const char *str, unsigned int **ptr, const char delimiter, int 
+*position)
+{
+
+  int i=0, j=0;
+  int start = *position;
+  char *temp;
+  unsigned int *pointer;
+  pointer = (unsigned int*)ptr;
+
+  while(str[*position]!=delimiter&&str[*position]!='\0'&&str[*position]!=' ')
+  {
+    (*position)++;
+    i++;
+  }
+  temp = (char*)calloc(i+1, sizeof(char) );
+  for( j=0;j<i;j++)
+  {
+    temp[j]=str[start];
+    start++;
+  }
+  temp[j]='\0';
+  sscanf(temp, "%X", pointer);
+  free(temp);
+}
+
+void read_x_w(const char *str, unsigned int **ptr, int width, int *position)
+{
+  int j=0;
+  char *temp;
+  unsigned int *pointer;
+  pointer = (unsigned int*)ptr;
+
+  temp = (char*)calloc(width, sizeof(char) );
+  for( j=0;j<width;j++)
+  {
+    temp[j]=str[(*position)];
+    (*position)++;
+  }
+  temp[j]='\0';
+  sscanf(temp, "%X", pointer);
+  free(temp);
+}
+
+void read_s_w(const char *str, char *ptr, int width, int *position)
+{
+
+  int j=0;
+  int start = *position;
+
+  while(str[*position]!='\0'&&j<width&&str[*position]!=' ')
+  {
+    ptr[j]=str[start];
+    start++;
+    (*position)++;
+    j++;
+  }
+  ptr[j]='\0';
+}
+
+void read_c(const char *str, char *ptr, const char delimiter, int *position)
+{
+  int i=0, j=0;
+  int start = *position;
+  char *temp;
+  
+  while(str[*position]!=delimiter&&str[*position]!='\0')
+  {
+    (*position)++;
+    i++;
+  }
+  temp = (char*)calloc(i+1, sizeof(char) );
+  for( j=0;j<i;j++)
+  {
+    temp[j]=str[start];
+    start++;
+  }
+  *ptr = temp[0];
+}
+
+void read_u(const char *str, unsigned int **ptr, const char delimiter, int *position)
+{
+
+  int i=0, j=0;
+  int start = *position;
+  char *temp;
+
+  while(str[*position]!=delimiter&&str[*position]!='\0'&&str[*position]!=' ')
+  {
+    (*position)++;
+    i++;
+  }
+  temp = (char*)calloc(i+1, sizeof(char) );
+  for( j=0;j<i;j++)
+  {
+    temp[j]=str[start];
+    start++;
+  }
+  temp[j]='\0';
+  *ptr = (unsigned int*)atoi(temp);
+}
diff -ruN vdr-1.4.7-orig/darwinutils.h vdr-1.4.7-darwin-2/darwinutils.h
--- vdr-1.4.7-orig/darwinutils.h	1970-01-01 02:00:00.000000000 +0200
+++ vdr-1.4.7-darwin-2/darwinutils.h	2009-02-11 21:48:53.000000000 +0200
@@ -0,0 +1,23 @@
+#define SIZE_MAX ((size_t) -1)
+#define __set_errno(e) errno = (e)
+
+inline int max(int a,int b) { return a > b ? a : b; };
+inline int min(int a,int b) { return a > b ? b : a; };
+
+extern char *strchrnul_darwin (const char *s, int c_in);
+char *strndup_darwin(const char *str, size_t len);
+ssize_t getdelim_darwin (char **lineptr, size_t *n, int delimiter, FILE *stream);
+ssize_t getline_darwin (char **lineptr, size_t *n, FILE *stream);
+
+char *__realpath_darwin (const char *name, char *resolved);
+char *canonicalize_file_name_darwin (const char *);
+
+int sscanf_darwin(const char *str, const char *format, ...);
+
+void read_a(const char *str, char **ptr, const char delimiter, int *position);
+void read_d(const char *str, int **ptr, const char delimiter, int *position);
+void read_x(const char *str, unsigned int **ptr, const char delimiter, int *position);
+void read_x_w(const char *str, unsigned int **ptr, int width, int *position);
+void read_s_w(const char *str, char *ptr, int width, int *position);
+void read_c(const char *str, char *ptr, const char delimiter, int *position);
+void read_u(const char *str, unsigned int **ptr, const char delimiter, int *position);
diff -ruN vdr-1.4.7-orig/diseqc.c vdr-1.4.7-darwin-2/diseqc.c
--- vdr-1.4.7-orig/diseqc.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/diseqc.c	2009-02-11 21:48:53.000000000 +0200
@@ -11,6 +11,9 @@
 #include <ctype.h>
 #include "sources.h"
 #include "thread.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // -- cDiseqc ----------------------------------------------------------------
 
@@ -30,7 +33,11 @@
 {
   bool result = false;
   char *sourcebuf = NULL;
+#ifdef __APPLE__
+  int fields = sscanf_darwin(s, "%a[^ ] %d %c %d %a[^\n]", &sourcebuf, &slof, &polarization, &lof, &commands);
+#else
   int fields = sscanf(s, "%a[^ ] %d %c %d %a[^\n]", &sourcebuf, &slof, &polarization, &lof, &commands);
+#endif
   if (fields == 4)
      commands = NULL; //XXX Apparently sscanf() doesn't work correctly if the last %a argument results in an empty string
   if (4 <= fields && fields <= 5) {
diff -ruN vdr-1.4.7-orig/dvbdevice.c vdr-1.4.7-darwin-2/dvbdevice.c
--- vdr-1.4.7-orig/dvbdevice.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/dvbdevice.c	2009-02-11 21:48:53.000000000 +0200
@@ -10,7 +10,11 @@
 #include "dvbdevice.h"
 #include <errno.h>
 #include <limits.h>
+#ifdef __APPLE__
+#include <linux/videodev_darwin.h>
+#else
 #include <linux/videodev.h>
+#endif
 #include <linux/dvb/audio.h>
 #include <linux/dvb/dmx.h>
 #include <linux/dvb/frontend.h>
diff -ruN vdr-1.4.7-orig/eit.c vdr-1.4.7-darwin-2/eit.c
--- vdr-1.4.7-orig/eit.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/eit.c	2009-02-11 21:48:53.000000000 +0200
@@ -298,7 +298,10 @@
      if (abs(diff - lastDiff) < 3) {
         isyslog("System Time = %s (%ld)", *TimeToString(loctim), loctim);
         isyslog("Local Time  = %s (%ld)", *TimeToString(sattim), sattim);
+//stime not defined in darwin
+#ifndef __APPLE__
         if (stime(&sattim) < 0)
+#endif
            esyslog("ERROR while setting system time: %m");
         }
      lastDiff = diff;
diff -ruN vdr-1.4.7-orig/epg.c vdr-1.4.7-darwin-2/epg.c
--- vdr-1.4.7-orig/epg.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/epg.c	2009-02-11 21:48:53.000000000 +0200
@@ -15,6 +15,16 @@
 #include <time.h>
 #include "libsi/si.h"
 #include "timers.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#ifdef __MAC_OS_X_VERSION_10_5
+#ifdef __i386__
+#include <i386/limits.h>
+#else
+#include <ppc/limits.h>
+#endif
+#endif
+#endif
 
 #define RUNNINGSTATUSTIMEOUT 30 // seconds before the running status is considered unknown
 
@@ -30,7 +40,11 @@
 bool tComponent::FromString(const char *s)
 {
   unsigned int Stream, Type;
+#ifdef __APPLE__
+  int n = sscanf_darwin(s, "%X %02X %7s %a[^\n]", &Stream, &Type, language, &description); // 7 = MAXLANGCODE2 - 1
+#else
   int n = sscanf(s, "%X %02X %7s %a[^\n]", &Stream, &Type, language, &description); // 7 = MAXLANGCODE2 - 1
+#endif
   if (n != 4 || isempty(description)) {
      free(description);
      description = NULL;
diff -ruN vdr-1.4.7-orig/i18n.h vdr-1.4.7-darwin-2/i18n.h
--- vdr-1.4.7-orig/i18n.h	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/i18n.h	2009-02-11 21:48:53.000000000 +0200
@@ -12,6 +12,10 @@
 
 #include <stdio.h>
 
+#ifdef __APPLE__
+# define __attribute_format_arg__(x) /* Ignore */
+#endif
+
 const int I18nNumLanguages = 21;
 
 typedef const char *tI18nPhrase[I18nNumLanguages];
diff -ruN vdr-1.4.7-orig/libsi/headers.h vdr-1.4.7-darwin-2/libsi/headers.h
--- vdr-1.4.7-orig/libsi/headers.h	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/libsi/headers.h	2009-02-11 21:48:53.000000000 +0200
@@ -17,7 +17,11 @@
 #ifndef LIBSI_HEADERS_H
 #define LIBSI_HEADERS_H
 
+#ifdef __APPLE__
+#include <machine/endian.h>
+#else
 #include <endian.h>
+#endif
 
 namespace SI {
 
diff -ruN vdr-1.4.7-orig/menu.c vdr-1.4.7-darwin-2/menu.c
--- vdr-1.4.7-orig/menu.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/menu.c	2009-02-11 21:48:53.000000000 +0200
@@ -28,6 +28,9 @@
 #include "timers.h"
 #include "transfer.h"
 #include "videodir.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 #define MAXWAIT4EPGINFO   3 // seconds
 #define MODETIMEOUT       3 // seconds
@@ -1620,7 +1623,11 @@
         int l = p ? p - s : strlen(s);
         cOsdItem *item = new cOsdItem;
         item->SetSelectable(false);
+#ifdef __APPLE__
+        item->SetText(strndup_darwin(s, l), false);
+#else
         item->SetText(strndup(s, l), false);
+#endif
         Add(item);
         s = p ? p + 1 : p;
         }
diff -ruN vdr-1.4.7-orig/menuitems.h vdr-1.4.7-darwin-2/menuitems.h
--- vdr-1.4.7-orig/menuitems.h	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/menuitems.h	2009-02-11 21:48:53.000000000 +0200
@@ -11,6 +11,15 @@
 #define __MENUITEMS_H
 
 #include "osdbase.h"
+#ifdef __APPLE__
+#ifdef __MAC_OS_X_VERSION_10_5
+#ifdef __i386__
+#include <i386/limits.h>
+#else
+#include <ppc/limits.h>
+#endif
+#endif
+#endif
 
 extern const char *FileNameChars;
 
diff -ruN vdr-1.4.7-orig/pat.c vdr-1.4.7-darwin-2/pat.c
--- vdr-1.4.7-orig/pat.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/pat.c	2009-02-11 21:48:53.000000000 +0200
@@ -8,7 +8,11 @@
  */
 
 #include "pat.h"
+#ifdef __APPLE__
+#include <stdlib.h>
+#else
 #include <malloc.h>
+#endif
 #include "channels.h"
 #include "libsi/section.h"
 #include "libsi/descriptor.h"
diff -ruN vdr-1.4.7-orig/recording.c vdr-1.4.7-darwin-2/recording.c
--- vdr-1.4.7-orig/recording.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/recording.c	2009-02-11 21:48:53.000000000 +0200
@@ -1181,7 +1181,11 @@
               delta = buf.st_size % sizeof(tIndex);
               if (delta) {
                  delta = sizeof(tIndex) - delta;
+#ifdef __APPLE__
+                 esyslog("ERROR: invalid file size (%ld) in '%s'", (long int)buf.st_size, fileName);
+#else
                  esyslog("ERROR: invalid file size (%ld) in '%s'", buf.st_size, fileName);
+#endif
                  }
               last = (buf.st_size + delta) / sizeof(tIndex) - 1;
               if (!Record && last >= 0) {
diff -ruN vdr-1.4.7-orig/remote.c vdr-1.4.7-darwin-2/remote.c
--- vdr-1.4.7-orig/remote.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/remote.c	2009-02-11 21:49:27.000000000 +0200
@@ -119,7 +119,11 @@
 bool cRemote::Put(uint64_t Code, bool Repeat, bool Release)
 {
   char buffer[32];
+#ifndef __i386__
+  snprintf(buffer, sizeof(buffer), "%0llX", Code);
+#else
   snprintf(buffer, sizeof(buffer), "%016LX", Code);
+#endif
   return Put(buffer, Repeat, Release);
 }
 
@@ -350,5 +354,8 @@
                  Put(KBDKEY(func));
               }
            }
+#ifdef __APPLE__
+        usleep(5000);
+#endif
         }
 }
diff -ruN vdr-1.4.7-orig/sections.c vdr-1.4.7-darwin-2/sections.c
--- vdr-1.4.7-orig/sections.c	2005-09-18 14:30:29.000000000 +0300
+++ vdr-1.4.7-darwin-2/sections.c	2009-02-11 21:49:12.000000000 +0200
@@ -221,5 +221,8 @@
                   }
                }
            }
+#ifdef __APPLE__
+        usleep(5000);
+#endif
         }
 }
diff -ruN vdr-1.4.7-orig/skins.c vdr-1.4.7-darwin-2/skins.c
--- vdr-1.4.7-orig/skins.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/skins.c	2009-02-11 21:48:53.000000000 +0200
@@ -10,6 +10,9 @@
 #include "skins.h"
 #include "interface.h"
 #include "status.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // --- cSkinQueuedMessage ----------------------------------------------------
 
@@ -93,10 +96,18 @@
      return NULL;
   static char buffer[1000];
   const char *a = s;
+#ifdef __APPLE__
+  const char *b = strchrnul_darwin(a, '\t');
+#else
   const char *b = strchrnul(a, '\t');
+#endif
   while (*b && Tab-- > 0) {
         a = b + 1;
+#ifdef __APPLE__
+        b = strchrnul_darwin(a, '\t');
+#else
         b = strchrnul(a, '\t');
+#endif
         }
   if (!*b)
      return (Tab <= 0) ? a : NULL;
diff -ruN vdr-1.4.7-orig/sources.c vdr-1.4.7-darwin-2/sources.c
--- vdr-1.4.7-orig/sources.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/sources.c	2009-02-11 21:48:53.000000000 +0200
@@ -9,6 +9,9 @@
 
 #include "sources.h"
 #include <ctype.h>
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // -- cSource ----------------------------------------------------------------
 
@@ -26,7 +29,11 @@
 bool cSource::Parse(const char *s)
 {
   char *codeBuf = NULL;
+#ifdef __APPLE__
+  if (2 == sscanf_darwin(s, "%a[^ ] %a[^\n]", &codeBuf, &description))
+#else
   if (2 == sscanf(s, "%a[^ ] %a[^\n]", &codeBuf, &description))
+#endif
      code = FromString(codeBuf);
   free(codeBuf);
   return code != stNone && description && *description;
diff -ruN vdr-1.4.7-orig/svdrp.c vdr-1.4.7-darwin-2/svdrp.c
--- vdr-1.4.7-orig/svdrp.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/svdrp.c	2009-02-11 21:48:53.000000000 +0200
@@ -470,7 +470,11 @@
   if (*Option) {
      int n = -1;
      int d = 0;
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         int o = strtol(Option, NULL, 10);
         if (o >= 1 && o <= Channels.MaxNumber())
            n = o;
@@ -540,7 +544,11 @@
 void cSVDRP::CmdDELC(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         if (!Channels.BeingEdited()) {
            cChannel *channel = Channels.GetByNumber(strtol(Option, NULL, 10));
            if (channel) {
@@ -587,7 +595,11 @@
 void cSVDRP::CmdDELR(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
         if (recording) {
            cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
@@ -615,7 +627,11 @@
 void cSVDRP::CmdDELT(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         if (!Timers.BeingEdited()) {
            cTimer *timer = Timers.Get(strtol(Option, NULL, 10) - 1);
            if (timer) {
@@ -644,7 +660,11 @@
 void cSVDRP::CmdEDIT(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
         if (recording) {
            cMarks Marks;
@@ -705,7 +725,11 @@
            p = strtok_r(NULL, delim, &strtok_next);
            }
         if (p) {
+#ifdef __APPLE__
+           if (isnumber_darwin(p))
+#else
            if (isnumber(p))
+#endif
               Quality = atoi(p);
            else {
               Reply(501, "Invalid quality \"%s\"", p);
@@ -715,14 +739,22 @@
         }
      // image size:
      if ((p = strtok_r(NULL, delim, &strtok_next)) != NULL) {
+#ifdef __APPLE__
+        if (isnumber_darwin(p))
+#else
         if (isnumber(p))
+#endif
            SizeX = atoi(p);
         else {
            Reply(501, "Invalid sizex \"%s\"", p);
            return;
            }
         if ((p = strtok_r(NULL, delim, &strtok_next)) != NULL) {
+#ifdef __APPLE__
+           if (isnumber_darwin(p))
+#else
            if (isnumber(p))
+#endif
               SizeY = atoi(p);
            else {
               Reply(501, "Invalid sizey \"%s\"", p);
@@ -860,7 +892,11 @@
 void cSVDRP::CmdLSTC(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         cChannel *channel = Channels.GetByNumber(strtol(Option, NULL, 10));
         if (channel)
            Reply(250, "%d %s", channel->Number(), *channel->ToText());
@@ -928,7 +964,11 @@
               else if (strcasecmp(p, "AT") == 0) {
                  DumpMode = dmAtTime;
                  if ((p = strtok_r(NULL, delim, &strtok_next)) != NULL) {
+#ifdef __APPLE__
+                    if (isnumber_darwin(p))
+#else
                     if (isnumber(p))
+#endif
                        AtTime = strtol(p, NULL, 10);
                     else {
                        Reply(501, "Invalid time");
@@ -942,7 +982,11 @@
                  }
               else if (!Schedule) {
                  cChannel* Channel = NULL;
+#ifdef __APPLE__
+                 if (isnumber_darwin(p))
+#else
                  if (isnumber(p))
+#endif
                     Channel = Channels.GetByNumber(strtol(Option, NULL, 10));
                  else
                     Channel = Channels.GetByChannelID(tChannelID::FromString(Option));
@@ -993,7 +1037,11 @@
 {
   bool recordings = Recordings.Update(true);
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option)) {
+#else
      if (isnumber(Option)) {
+#endif
         cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
         if (recording) {
            FILE *f = fdopen(file, "w");
@@ -1034,7 +1082,11 @@
      char *strtok_next;
      char *p = strtok_r(buf, delim, &strtok_next);
      while (p) {
+#ifdef __APPLE__
+           if (isnumber_darwin(p))
+#else
            if (isnumber(p))
+#endif
               Number = strtol(p, NULL, 10);
            else if (strcasecmp(p, "ID") == 0)
               Id = true;
@@ -1287,7 +1339,11 @@
            option++;
      char c = *option;
      *option = 0;
+#ifdef __APPLE__
+     if (isnumber_darwin(num)) {
+#else
      if (isnumber(num)) {
+#endif
         cRecording *recording = Recordings.Get(strtol(num, NULL, 10) - 1);
         if (recording) {
            if (c)
@@ -1462,7 +1518,11 @@
 void cSVDRP::CmdVOLU(const char *Option)
 {
   if (*Option) {
+#ifdef __APPLE__
+     if (isnumber_darwin(Option))
+#else
      if (isnumber(Option))
+#endif
         cDevice::PrimaryDevice()->SetVolume(strtol(Option, NULL, 10), true);
      else if (strcmp(Option, "+") == 0)
         cDevice::PrimaryDevice()->SetVolume(VOLUMEDELTA);
diff -ruN vdr-1.4.7-orig/themes.c vdr-1.4.7-darwin-2/themes.c
--- vdr-1.4.7-orig/themes.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/themes.c	2009-02-11 21:48:53.000000000 +0200
@@ -12,6 +12,9 @@
 #include <string.h>
 #include "config.h"
 #include "tools.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // --- cTheme ----------------------------------------------------------------
 
@@ -50,7 +53,11 @@
                     // FileName is ok
                     if (SetName) {
                        free(name);
+#ifdef __APPLE__
+                       name = strndup_darwin(n, e - n);
+#else
                        name = strndup(n, e - n);
+#endif
                        }
                     }
                  else
diff -ruN vdr-1.4.7-orig/thread.c vdr-1.4.7-darwin-2/thread.c
--- vdr-1.4.7-orig/thread.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/thread.c	2009-02-11 21:48:53.000000000 +0200
@@ -9,8 +9,16 @@
 
 #include "thread.h"
 #include <errno.h>
+#ifdef __APPLE__
+#include <stdlib.h>
+#include <pthread.h>
+#ifdef __MAC_OS_X_VERSION_10_5
+#include <signal.h>
+#endif
+#else
 #include <linux/unistd.h>
 #include <malloc.h>
+#endif
 #include <stdarg.h>
 #include <sys/resource.h>
 #include <sys/syscall.h>
@@ -140,7 +148,9 @@
 {
   pthread_rwlockattr_t attr;
   pthread_rwlockattr_init(&attr);
+#ifndef __APPLE__
   pthread_rwlockattr_setkind_np(&attr, PreferWriter ? PTHREAD_RWLOCK_PREFER_WRITER_NP : PTHREAD_RWLOCK_PREFER_READER_NP);
+#endif
   pthread_rwlock_init(&rwlock, &attr);
 }
 
@@ -158,9 +168,15 @@
         TimeoutMs = 0;
      }
   if (Write)
+#ifdef __APPLE__
+     Result = TimeoutMs ? pthread_rwlock_wrlock(&rwlock) : pthread_rwlock_wrlock(&rwlock);
+  else
+     Result = TimeoutMs ? pthread_rwlock_rdlock(&rwlock) : pthread_rwlock_rdlock(&rwlock);
+#else
      Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock);
   else
      Result = TimeoutMs ? pthread_rwlock_timedrdlock(&rwlock, &abstime) : pthread_rwlock_rdlock(&rwlock);
+#endif
   return Result == 0;
 }
 
@@ -176,7 +192,11 @@
   locked = 0;
   pthread_mutexattr_t attr;
   pthread_mutexattr_init(&attr);
+#ifdef __APPLE__
+  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+#else
   pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
+#endif
   pthread_mutex_init(&mutex, &attr);
 }
 
@@ -318,7 +338,11 @@
 
 tThreadId cThread::ThreadId(void)
 {
+#ifdef __APPLE__
+  return -ENOSYS;
+#else
   return syscall(__NR_gettid);
+#endif
 }
 
 void cThread::SetMainThreadId(void)
diff -ruN vdr-1.4.7-orig/timers.c vdr-1.4.7-darwin-2/timers.c
--- vdr-1.4.7-orig/timers.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/timers.c	2009-02-11 21:48:53.000000000 +0200
@@ -14,6 +14,9 @@
 #include "i18n.h"
 #include "libsi/si.h"
 #include "remote.h"
+#ifdef __APPLE__
+#include "darwinutils.h"
+#endif
 
 // IMPORTANT NOTE: in the 'sscanf()' calls there is a blank after the '%d'
 // format characters in order to allow any number of blanks after a numeric
@@ -269,7 +272,11 @@
      s = s2;
      }
   bool result = false;
+#ifdef __APPLE__
+  if (8 <= sscanf_darwin(s, "%u :%a[^:]:%a[^:]:%d :%d :%d :%d :%a[^:\n]:%a[^\n]", &flags, &channelbuffer, &daybuffer, &start, &stop, &priority, &lifetime, &filebuffer, &aux)) {
+#else
   if (8 <= sscanf(s, "%u :%a[^:]:%a[^:]:%d :%d :%d :%d :%a[^:\n]:%a[^\n]", &flags, &channelbuffer, &daybuffer, &start, &stop, &priority, &lifetime, &filebuffer, &aux)) {
+#endif
      ClrFlags(tfRecording);
      if (aux && !*skipspace(aux)) {
         free(aux);
@@ -279,7 +286,11 @@
      result = ParseDay(daybuffer, day, weekdays);
      strn0cpy(file, filebuffer, MaxFileName);
      strreplace(file, '|', ':');
+#ifdef __APPLE__
+     if (isnumber_darwin(channelbuffer))
+#else
      if (isnumber(channelbuffer))
+#endif
         channel = Channels.GetByNumber(atoi(channelbuffer));
      else
         channel = Channels.GetByChannelID(tChannelID::FromString(channelbuffer), true, true);
diff -ruN vdr-1.4.7-orig/tools.c vdr-1.4.7-darwin-2/tools.c
--- vdr-1.4.7-orig/tools.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/tools.c	2009-02-11 21:48:53.000000000 +0200
@@ -21,7 +21,12 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <sys/time.h>
+#ifdef __APPLE__
+#include "darwinutils.h"
+#include <sys/mount.h>
+#else
 #include <sys/vfs.h>
+#endif
 #include <time.h>
 #include <unistd.h>
 #include <utime.h>
@@ -257,7 +262,11 @@
   return strlen(buf);
 }
 
+#ifdef __APPLE__
+bool isnumber_darwin(const char *s)
+#else
 bool isnumber(const char *s)
+#endif
 {
   if (!*s)
      return false;
@@ -481,7 +490,11 @@
 {
   if (!FileName)
      return NULL;
+#ifdef __APPLE__
+  char *TargetName = canonicalize_file_name_darwin(FileName);
+#else
   char *TargetName = canonicalize_file_name(FileName);
+#endif
   if (!TargetName) {
      if (errno == ENOENT) // file doesn't exist
         TargetName = strdup(FileName);
@@ -506,7 +519,11 @@
          int f = open(buf, O_WRONLY | O_CREAT, DEFFILEMODE);
          // O_SYNC doesn't work on all file systems
          if (f >= 0) {
+#ifdef __APPLE__
+            if (fsync(f) < 0)
+#else
             if (fdatasync(f) < 0)
+#endif
                LOG_ERROR_STR(buf);
             close(f);
             remove(buf);
@@ -819,7 +836,11 @@
 
 char *cReadLine::Read(FILE *f)
 {
+#ifdef __APPLE__
+  int n = getline_darwin(&buffer, &size, f);
+#else
   int n = getline(&buffer, &size, f);
+#endif
   if (n > 0) {
      n--;
      if (buffer[n] == '\n') {
@@ -1079,9 +1100,11 @@
   readahead = KILOBYTE(128);
   written = 0;
   totwritten = 0;
+#ifndef __APPLE__
   if (fd >= 0)
      posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM); // we could use POSIX_FADV_SEQUENTIAL, but we do our own readahead, disabling the kernel one.
 #endif
+#endif
   return fd;
 }
 
@@ -1090,8 +1113,12 @@
 #ifdef USE_FADVISE
   if (fd >= 0) {
      if (totwritten)    // if we wrote anything make sure the data has hit the disk before
+#ifdef __APPLE__
+        fsync(fd);  // calling fadvise, as this is our last chance to un-cache it.
+#else
         fdatasync(fd);  // calling fadvise, as this is our last chance to un-cache it.
      posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+#endif
      }
 #endif
   int OldFd = fd;
@@ -1115,7 +1142,11 @@
 int cUnbufferedFile::FadviseDrop(off_t Offset, off_t Len)
 {
   // rounding up the window to make sure that not PAGE_SIZE-aligned data gets freed.
+#ifndef __APPLE__
   return posix_fadvise(fd, Offset - (FADVGRAN - 1), Len + (FADVGRAN - 1) * 2, POSIX_FADV_DONTNEED);
+#else
+  return 0; // returning all is fine also on darwin
+#endif
 }
 
 off_t cUnbufferedFile::Seek(off_t Offset, int Whence)
@@ -1152,7 +1183,9 @@
            // 1/2 of the previously requested area. This avoids calling
            // fadvise() after every read() call.
            if (ahead - curpos < (off_t)(readahead / 2)) {
+#ifndef __APPLE__
               posix_fadvise(fd, curpos, readahead, POSIX_FADV_WILLNEED);
+#endif
               ahead = curpos + readahead;
               cachedend = max(cachedend, ahead);
               }
@@ -1203,8 +1236,10 @@
               //    last (partial) page might be skipped, writeback will start only after
               //    second call; the third call will still include this page and finally
               //    drop it from cache.
+#ifndef __APPLE__
               off_t headdrop = min(begin, WRITE_BUFFER * 2L);
               posix_fadvise(fd, begin - headdrop, lastpos - begin + headdrop, POSIX_FADV_DONTNEED);
+#endif
               }
            begin = lastpos = curpos;
            totwritten += written;
@@ -1222,8 +1257,10 @@
               // kind of write gathering enabled), but the syncs cause (io) load..
               // Uncomment the next line if you think you need them.
               //fdatasync(fd);
+#ifndef __APPLE__
               off_t headdrop = min(curpos - totwritten, totwritten * 2L);
               posix_fadvise(fd, curpos - totwritten - headdrop, totwritten + headdrop, POSIX_FADV_DONTNEED);
+#endif
               totwritten = 0;
               }
            }
diff -ruN vdr-1.4.7-orig/tools.h vdr-1.4.7-darwin-2/tools.h
--- vdr-1.4.7-orig/tools.h	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/tools.h	2009-02-11 21:48:53.000000000 +0200
@@ -21,6 +21,9 @@
 #include <syslog.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#ifdef __APPLE__
+#include <sys/syslimits.h>
+#endif
 
 typedef unsigned char uchar;
 #define uint64 uint64_t // for compatibility - TODO remove in version 1.5
@@ -28,8 +31,15 @@
 extern int SysLogLevel;
 
 #define esyslog(a...) void( (SysLogLevel > 0) ? syslog_with_tid(LOG_ERR,   a) : void() )
+// Actually not needed, but is set because of default syslogd configuration doesn't
+// write info and debug messages to syslog. Better solution is configure the syslogd
+#ifdef __APPLE__
+#define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_ERR,  a) : void() )
+#define dsyslog(a...) void( (SysLogLevel > 2) ? syslog_with_tid(LOG_ERR, a) : void() )
+#else
 #define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_INFO,  a) : void() )
 #define dsyslog(a...) void( (SysLogLevel > 2) ? syslog_with_tid(LOG_DEBUG, a) : void() )
+#endif
 
 #define LOG_ERROR         esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
 #define LOG_ERROR_STR(s)  esyslog("ERROR: %s: %m", s)
@@ -105,7 +115,11 @@
 bool endswith(const char *s, const char *p);
 bool isempty(const char *s);
 int numdigits(int n);
+#ifdef __APPLE__
+bool isnumber_darwin(const char *s);
+#else
 bool isnumber(const char *s);
+#endif
 cString itoa(int n);
 cString AddDirectory(const char *DirName, const char *FileName);
 int FreeDiskSpaceMB(const char *Directory, int *UsedMB = NULL);
diff -ruN vdr-1.4.7-orig/vdr.c vdr-1.4.7-darwin-2/vdr.c
--- vdr-1.4.7-orig/vdr.c	2008-04-21 22:21:02.000000000 +0300
+++ vdr-1.4.7-darwin-2/vdr.c	2009-02-11 21:48:53.000000000 +0200
@@ -31,8 +31,10 @@
 #include <pwd.h>
 #include <signal.h>
 #include <stdlib.h>
+#ifndef __APPLE__
 #include <sys/capability.h>
 #include <sys/prctl.h>
+#endif
 #include <termios.h>
 #include <unistd.h>
 #include "audio.h"
@@ -103,10 +105,12 @@
         fprintf(stderr, "vdr: cannot set user id %u: %s\n", (unsigned int)user->pw_uid, strerror(errno));
         return false;
         }
+#ifndef __APPLE__
      if (prctl(PR_SET_DUMPABLE, 2, 0, 0, 0) < 0) {
         fprintf(stderr, "vdr: warning - cannot set dumpable: %s\n", strerror(errno));
         // always non-fatal, and will not work with kernel < 2.6.13
         }
+#endif
      }
   return true;
 }
@@ -114,6 +118,7 @@
 static bool SetCapSysTime(void)
 {
   // drop all capabilities except cap_sys_time
+#ifndef __APPLE__
   cap_t caps = cap_from_text("= cap_sys_time=ep");
   if (!caps) {
      fprintf(stderr, "vdr: cap_from_text failed: %s\n", strerror(errno));
@@ -125,16 +130,19 @@
      return false;
      }
   cap_free(caps);
+#endif
   return true;
 }
 
 static bool SetKeepCaps(bool On)
 {
+#ifndef __APPLE__
   // set keeping capabilities during setuid() on/off
   if (prctl(PR_SET_KEEPCAPS, On ? 1 : 0, 0, 0, 0) != 0) {
      fprintf(stderr, "vdr: prctl failed\n");
      return false;
      }
+#endif
   return true;
 }
 
@@ -244,7 +252,11 @@
           case 'c': ConfigDirectory = optarg;
                     break;
           case 'd': DaemonMode = true; break;
+#ifdef __APPLE__
+          case 'D': if (isnumber_darwin(optarg)) {
+#else
           case 'D': if (isnumber(optarg)) {
+#endif
                        int n = atoi(optarg);
                        if (0 <= n && n < MAXDEVICES) {
                           cDevice::SetUseDevice(n);
@@ -264,13 +276,21 @@
                       char *p = strchr(optarg, '.');
                       if (p)
                          *p = 0;
+#ifdef __APPLE__
+                      if (isnumber_darwin(optarg)) {
+#else
                       if (isnumber(optarg)) {
+#endif
                          int l = atoi(optarg);
                          if (0 <= l && l <= 3) {
                             SysLogLevel = l;
                             if (!p)
                                break;
+#ifdef __APPLE__
+                            if (isnumber_darwin(p + 1)) {
+#else
                             if (isnumber(p + 1)) {
+#endif
                                int l = atoi(p + 1);
                                if (0 <= l && l <= 7) {
                                   int targets[] = { LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
@@ -301,7 +321,11 @@
           case 'n' | 0x100:
                     UseKbd = false;
                     break;
+#ifdef __APPLE__
+          case 'p': if (isnumber_darwin(optarg))
+#else
           case 'p': if (isnumber(optarg))
+#endif
                        SVDRPport = atoi(optarg);
                     else {
                        fprintf(stderr, "vdr: invalid port number: %s\n", optarg);
@@ -335,7 +359,11 @@
                     while (optarg && *optarg && optarg[strlen(optarg) - 1] == '/')
                           optarg[strlen(optarg) - 1] = 0;
                     break;
+#ifdef __APPLE__
+          case 'w': if (isnumber_darwin(optarg)) { int t = atoi(optarg);
+#else
           case 'w': if (isnumber(optarg)) { int t = atoi(optarg);
+#endif
                        if (t >= 0) {
                           WatchdogTimeout = t;
                           break;
@@ -479,9 +507,12 @@
      }
   else if (Terminal) {
      // Claim new controlling terminal
+     // Not needed on darwin
+#ifndef __APPLE__
      stdin  = freopen(Terminal, "r", stdin);
      stdout = freopen(Terminal, "w", stdout);
      stderr = freopen(Terminal, "w", stderr);
+#endif
      HasStdin = true;
      }
 

