diff --git a/commands.c b/commands.c
index 2a791f6..8a1c06a 100644
--- a/commands.c
+++ b/commands.c
@@ -16,9 +16,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#define _POSIX_C_SOURCE 200112L /* for setenv(3) */
-#include <stdlib.h>
+#define _POSIX_C_SOURCE 200112L
 
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/wait.h>
diff --git a/image.c b/image.c
index 7774267..20c4ff3 100644
--- a/image.c
+++ b/image.c
@@ -16,6 +16,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <string.h>
 #include <unistd.h>
 
diff --git a/main.c b/main.c
index dedd86e..ebb6e7d 100644
--- a/main.c
+++ b/main.c
@@ -16,6 +16,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -430,8 +432,9 @@ int fncmp(const void *a, const void *b) {
 }
 
 int main(int argc, char **argv) {
-	int i, len, start;
+	int i, start;
 	size_t n;
+	ssize_t len;
 	char *filename;
 	struct stat fstats;
 	r_dir_t dir;
@@ -460,11 +463,13 @@ int main(int argc, char **argv) {
 	/* build file list: */
 	if (options->from_stdin) {
 		filename = NULL;
-		while ((len = getline(&filename, &n, stdin)) > 0) {
+		while ((len = get_line(&filename, &n, stdin)) > 0) {
 			if (filename[len-1] == '\n')
 				filename[len-1] = '\0';
 			check_add_file(filename);
 		}
+		if (filename)
+			free(filename);
 	} else {
 		for (i = 0; i < options->filecnt; i++) {
 			filename = options->filenames[i];
diff --git a/options.c b/options.c
index 9887a96..853a72f 100644
--- a/options.c
+++ b/options.c
@@ -16,12 +16,12 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#define _POSIX_C_SOURCE 2 /* for getopt(3) */
-#include <unistd.h>
+#define _POSIX_C_SOURCE 200112L
 
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <unistd.h>
 
 #include "options.h"
 #include "util.h"
diff --git a/thumbs.c b/thumbs.c
index 6315b8c..52597dd 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -16,12 +16,14 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <stdlib.h>
 #include <string.h>
-#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <utime.h>
 
 #include "thumbs.h"
 #include "util.h"
@@ -29,11 +31,6 @@
 #define _THUMBS_CONFIG
 #include "config.h"
 
-#ifdef __NetBSD__
-#define st_mtim st_mtimespec
-#define st_atim st_atimespec
-#endif
-
 #ifdef EXIF_SUPPORT
 void exif_auto_orientate(const fileinfo_t*);
 #endif
@@ -76,12 +73,8 @@ Imlib_Image* tns_cache_load(const char *filepath) {
 		return NULL;
 
 	if ((cfile = tns_cache_filepath(filepath))) {
-		if (!stat(cfile, &cstats) &&
-		    cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
-		    cstats.st_mtim.tv_nsec / 1000 == fstats.st_mtim.tv_nsec / 1000)
-		{
+		if (!stat(cfile, &cstats) && cstats.st_mtime == fstats.st_mtime)
 			im = imlib_load_image(cfile);
-		}
 		free(cfile);
 	}
 
@@ -91,7 +84,7 @@ Imlib_Image* tns_cache_load(const char *filepath) {
 void tns_cache_write(thumb_t *t, Bool force) {
 	char *cfile, *dirend;
 	struct stat cstats, fstats;
-	struct timeval times[2];
+	struct utimbuf times;
 	Imlib_Load_Error err = 0;
 
 	if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
@@ -101,10 +94,7 @@ void tns_cache_write(thumb_t *t, Bool force) {
 		return;
 
 	if ((cfile = tns_cache_filepath(t->file->path))) {
-		if (force || stat(cfile, &cstats) ||
-		    cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
-		    cstats.st_mtim.tv_nsec / 1000 != fstats.st_mtim.tv_nsec / 1000)
-		{
+		if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) {
 			if ((dirend = strrchr(cfile, '/'))) {
 				*dirend = '\0';
 				err = r_mkdir(cfile);
@@ -120,9 +110,9 @@ void tns_cache_write(thumb_t *t, Bool force) {
 			if (err) {
 				warn("could not cache thumbnail: %s", t->file->name);
 			} else {
-				TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
-				TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
-				utimes(cfile, times);
+				times.actime = fstats.st_atime;
+				times.modtime = fstats.st_mtime;
+				utime(cfile, &times);
 			}
 		}
 		free(cfile);
diff --git a/util.c b/util.c
index 8b65c27..bc7c2e1 100644
--- a/util.c
+++ b/util.c
@@ -16,6 +16,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -27,6 +29,7 @@
 #include "util.h"
 
 enum {
+	BUF_SIZE  = 1024,
 	DNAME_CNT = 512,
 	FNAME_LEN = 1024
 };
@@ -87,6 +90,39 @@ void die(const char* fmt, ...) {
 	exit(1);
 }
 
+ssize_t get_line(char **buf, size_t *n, FILE *stream) {
+	size_t len;
+	char *s;
+
+	if (!stream || feof(stream) || ferror(stream))
+		return -1;
+
+	if (!*buf || !*n) {
+		*n = BUF_SIZE;
+		*buf = (char*) s_malloc(*n);
+	}
+	s = *buf;
+
+	while (1) {
+		if (!fgets(s, *n - (s - *buf), stream))
+			return -1;
+		len = strlen(s);
+		if (feof(stream))
+			break;
+		if (len > 0 && s[len-1] == '\n')
+			break;
+		if (len + 1 == *n - (s - *buf)) {
+			*buf = (char*) s_realloc(*buf, 2 * *n);
+			s = *buf + *n - 1;
+			*n *= 2;
+		} else {
+			s += len;
+		}
+	}
+
+	return s - *buf + len;
+}
+
 void size_readable(float *size, const char **unit) {
 	const char *units[] = { "", "K", "M", "G" };
 	int i;
@@ -98,13 +134,9 @@ void size_readable(float *size, const char **unit) {
 
 char* absolute_path(const char *filename) {
 	size_t len;
-	char *path = NULL;
 	const char *basename;
-	char *dirname = NULL;
-	char *cwd = NULL;
-	char *twd = NULL;
-	char *dir;
-	char *s;
+	char *dir, *dirname = NULL, *path = NULL, *s;
+	char *cwd = NULL, *twd = NULL;
 
 	if (!filename || *filename == '\0' || *filename == '/')
 		return NULL;
diff --git a/util.h b/util.h
index 9d5dc1d..cc4da88 100644
--- a/util.h
+++ b/util.h
@@ -45,13 +45,6 @@
   (tv)->tv_usec += (t) % 1000 * 1000;   \
 }
 
-#ifndef TIMESPEC_TO_TIMEVAL
-#define TIMESPEC_TO_TIMEVAL(tv,ts) {    \
-  (tv)->tv_sec = (ts)->tv_sec;          \
-  (tv)->tv_usec = (ts)->tv_nsec / 1000; \
-}
-#endif
-
 typedef struct {
 	DIR *dir;
 	char *name;
@@ -69,6 +62,8 @@ char* s_strdup(char*);
 void warn(const char*, ...);
 void die(const char*, ...);
 
+ssize_t get_line(char**, size_t*, FILE*);
+
 void size_readable(float*, const char**);
 
 char* absolute_path(const char*);
diff --git a/window.c b/window.c
index 237f3f3..4a90516 100644
--- a/window.c
+++ b/window.c
@@ -16,6 +16,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <string.h>
 #include <X11/Xutil.h>
 #include <X11/cursorfont.h>