--- /dev/null
+Return-Path: <too@guru-group.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 22AAF6DE0318\r
+ for <notmuch@notmuchmail.org>; Sat, 14 May 2016 04:33:27 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.261\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.261 tagged_above=-999 required=5 tests=[AWL=0.270, \r
+ HEADER_FROM_DIFFERENT_DOMAINS=0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id js0IYzTJJOZ9 for <notmuch@notmuchmail.org>;\r
+ Sat, 14 May 2016 04:33:18 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 056586DE02B2\r
+ for <notmuch@notmuchmail.org>; Sat, 14 May 2016 04:33:17 -0700 (PDT)\r
+Received: by guru.guru-group.fi (Postfix, from userid 501)\r
+ id 1A2D110008D; Sat, 14 May 2016 14:33:10 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: notmuch@notmuchmail.org\r
+Cc: bijan@chokoufe.com,\r
+ Tomi Ollila <tomi.ollila@iki.fi>\r
+Subject: [RFC PATCH] RFC: expand_tilde () for potential future path expansion\r
+Date: Sat, 14 May 2016 14:33:08 +0300\r
+Message-Id: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi>\r
+X-Mailer: git-send-email 2.8.2\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 14 May 2016 11:33:27 -0000\r
+\r
+One implementation how to do this. Output of the "tests".\r
+\r
+$ sh expand-tilde.c\r
++ exec gcc -std=c99 -Wall -Wstrict-prototypes -Winit-self -Wformat=2 -Wcast-align -Wpointer-arith -Wextra -Wwrite-strings -Wcast-qual -Wshadow -Wmissing-include-dirs -Wundef -Wbad-function-cast -Wlogical-op -Waggregate-return -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wvla -Woverlength-strings -Wpadded -O2 -o expand-tilde expand-tilde.c\r
+$\r
+$ ./expand-tilde\r
+\r
+input '~', expanded '/home/too', rest ''\r
+input '~root', expanded '/root', rest ''\r
+\r
+$ HOME=/a/b/c/ ./expand-tilde '~' '~'/ "~$USER" "~$USER/" '~'root '~'root/ '~none' '~none/'\r
+\r
+input '~', expanded '/a/b/c/', rest ''\r
+input '~/', expanded '/a/b/c/', rest '/'\r
+input '~too', expanded '/home/too', rest ''\r
+input '~too/', expanded '/home/too', rest '/'\r
+input '~root', expanded '/root', rest ''\r
+input '~root/', expanded '/root', rest '/'\r
+input '~none', expanded '(null)', rest '~none'\r
+input '~none/', expanded '(null)', rest '~none/'\r
+\r
+$ HOME= ./expand-tilde '~' '~'/ "~$USER" "~$USER/"\r
+\r
+input '~', expanded '', rest ''\r
+input '~/', expanded '', rest '/'\r
+input '~too', expanded '/home/too', rest ''\r
+input '~too/', expanded '/home/too', rest '/'\r
+\r
+$ ( unset HOME; ./expand-tilde '~' '~'/ "~$USER" "~$USER/" )\r
+\r
+input '~', expanded '(null)', rest '~'\r
+input '~/', expanded '(null)', rest '~/'\r
+input '~too', expanded '/home/too', rest ''\r
+input '~too/', expanded '/home/too', rest '/'\r
+---\r
+ util/expand-tilde.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 102 insertions(+)\r
+ create mode 100644 util/expand-tilde.c\r
+\r
+diff --git a/util/expand-tilde.c b/util/expand-tilde.c\r
+new file mode 100644\r
+index 0000000..e3097e6\r
+--- /dev/null\r
++++ b/util/expand-tilde.c\r
+@@ -0,0 +1,102 @@\r
++#if 0 /* -*- mode: c; c-file-style: "stroustrup"; tab-width: 8; -*-\r
++ set -eu; trg=`exec basename "$0" .c`; rm -f "$trg"\r
++ WARN="-Wall -Wstrict-prototypes -Winit-self -Wformat=2" # -pedantic\r
++ WARN="$WARN -Wcast-align -Wpointer-arith " # -Wfloat-equal #-Werror\r
++ WARN="$WARN -Wextra -Wwrite-strings -Wcast-qual -Wshadow" # -Wconversion\r
++ WARN="$WARN -Wmissing-include-dirs -Wundef -Wbad-function-cast -Wlogical-op"\r
++ WARN="$WARN -Waggregate-return -Wold-style-definition"\r
++ WARN="$WARN -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls"\r
++ WARN="$WARN -Wnested-externs -Winline -Wvla -Woverlength-strings -Wpadded"\r
++ case ${1-} in '') set x -O2; shift; esac\r
++ #case ${1-} in '') set x -ggdb; shift; esac\r
++ set -x; exec ${CC:-gcc} -std=c99 $WARN "$@" -o "$trg" "$0"\r
++ exit $?\r
++ */\r
++#endif\r
++/*\r
++ * $ expand-tilde.c $\r
++ *\r
++ * Author: Tomi Ollila -- too ät iki piste fi\r
++ *\r
++ * Created: Sat 14 May 2016 13:10:35 EEST too\r
++ * Last modified: Sat 14 May 2016 14:22:10 +0300 too\r
++ *\r
++ * *** Public Domain ***\r
++ */\r
++\r
++#include <stdlib.h>\r
++#include <sys/types.h>\r
++#include <pwd.h>\r
++\r
++/* USER_NAME_MAX_LENGTH is not usually defined (publicly in standard paths). */\r
++\r
++#ifndef USER_NAME_MAX_LENGTH\r
++#define USER_NAME_MAX_LENGTH 32\r
++#endif\r
++\r
++static // for -Wmissing-prototypes\r
++/* This is modeled after expand-file-name in emacs sources (fileio.c);\r
++ * simplified and using constant buffer for user name.\r
++ * neither emacs nor this implementation has restrictions in username chars\r
++ * ...we may be looking forward for multibyte usernames...\r
++ */\r
++const char * expand_tilde(const char * path, const char ** rest)\r
++{\r
++ if (path == NULL || path[0] != '~') {\r
++ *rest = path;\r
++ return NULL;\r
++ }\r
++\r
++ if (path[1] == '/' || path[1] == '\0') {\r
++ const char * home = getenv ("HOME");\r
++ *rest = home? path + 1: path;\r
++ return home;\r
++ }\r
++\r
++ char username[USER_NAME_MAX_LENGTH + 1];\r
++ int namepos = 0;\r
++ const char * pathp = path + 1;\r
++\r
++ while (1) {\r
++ username[namepos++] = pathp++[0];\r
++ if (pathp[0] == '/' || pathp[0] == '\0')\r
++ break;\r
++ if (namepos >= USER_NAME_MAX_LENGTH) {\r
++ *rest = path;\r
++ return NULL;\r
++ }\r
++ }\r
++ username[namepos] = '\0';\r
++\r
++ struct passwd * pw = getpwnam (username);\r
++ if (pw == NULL) {\r
++ *rest = path;\r
++ return NULL;\r
++ }\r
++ *rest = path + 1 + namepos;\r
++ return pw->pw_dir;\r
++ }\r
++\r
++#include <stdio.h>\r
++\r
++static void print_expanded(const char * path)\r
++{\r
++ const char * rest;\r
++ const char * expanded = expand_tilde(path, &rest);\r
++ printf("input '%s', expanded '%s', rest '%s'\n",\r
++ path, expanded, rest);\r
++}\r
++\r
++int main(int argc, char * argv[])\r
++{\r
++ printf("\n");\r
++ if (argc < 2) {\r
++ print_expanded("~\0-- this something not to be seen in output --");\r
++ print_expanded("~root\0-- this something not to be seen in output --");\r
++ } else {\r
++ for (int i = 1; i < argc; i++)\r
++ print_expanded(argv[i]);\r
++ }\r
++ printf("\n");\r
++ return 0;\r
++}\r
+-- \r
+2.8.2\r
+\r