From a543eed112b4167c410a2fb75d672df9df926a1a Mon Sep 17 00:00:00 2001
From: lostd <lostd@2f30.org>
Date: Tue, 7 Oct 2014 14:32:03 +0000
Subject: [PATCH] Simplify associations and handle README files

---
 noice.c | 55 +++++++++++++++++++++++--------------------------------
 1 file changed, 23 insertions(+), 32 deletions(-)

diff --git a/noice.c b/noice.c
index 549eaa36..655ac39b 100644
--- a/noice.c
+++ b/noice.c
@@ -50,36 +50,34 @@ struct assoc {
 	char *ext; /* Extension */
 	char *bin; /* Program */
 } assocs[] = {
-	{ "avi", "mplayer" },
-	{ "mp4", "mplayer" },
-	{ "mkv", "mplayer" },
-	{ "mp3", "mplayer" },
-	{ "ogg", "mplayer" },
-	{ "srt", "less" },
-	{ "txt", "less" },
+	{ ".avi", "mplayer" },
+	{ ".mp4", "mplayer" },
+	{ ".mkv", "mplayer" },
+	{ ".mp3", "mplayer" },
+	{ ".ogg", "mplayer" },
+	{ ".srt", "less" },
+	{ ".txt", "less" },
+	{ "README", "less" },
 };
 
 char *
-extension(char *file)
-{
-	char *dot;
-
-	dot = strrchr(file, '.');
-	if (dot == NULL || dot == file)
-		return NULL;
-	else
-		return dot + 1;
-}
-
-char *
-openwith(char *ext)
+openwith(char *file)
 {
+	char *ext = NULL;
+	char *bin = NULL;
 	int i;
 
+	ext = strrchr(file, '.');
+	if (ext == NULL)
+		ext = file;
+	DPRINTF_S(ext);
+
 	for (i = 0; i < LEN(assocs); i++)
-		if (strncmp(assocs[i].ext, ext, strlen(ext)) == 0)
-			return assocs[i].bin;
-	return NULL;
+		if (strncmp(assocs[i].ext, ext, strlen(ext) + 1) == 0)
+			bin = assocs[i].bin;
+	DPRINTF_S(bin);
+
+	return bin;
 }
 
 int
@@ -327,7 +325,7 @@ nochange:
 		if (ret == 3) {
 			char *name, *file = NULL;
 			char *newpath;
-			char *ext, *bin;
+			char *bin;
 			pid_t pid;
 
 			/* Cannot descend in empty directories */
@@ -357,18 +355,11 @@ nochange:
 				DPRINTF_S(file);
 
 				/* Open with */
-				ext = extension(name);
-				if (ext == NULL) {
-					printwarn("invalid extension\n");
-					goto nochange;
-				}
-				bin = openwith(ext);
+				bin = openwith(name);
 				if (bin == NULL) {
 					printwarn("no association\n");
 					goto nochange;
 				}
-				DPRINTF_S(ext);
-				DPRINTF_S(bin);
 
 				exitcurses();
 
-- 
2.51.0