Re: [RFC PATCH] RFC: expand_tilde () for potential future path expansion
authorBijan Chokoufe Nejad <bijan.chokoufe@gmail.com>
Sat, 14 May 2016 15:41:46 +0000 (17:41 +0200)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 23:21:50 +0000 (16:21 -0700)
5a/a5026b997447734bd078be2e2d83e3e5d85e7d [new file with mode: 0644]

diff --git a/5a/a5026b997447734bd078be2e2d83e3e5d85e7d b/5a/a5026b997447734bd078be2e2d83e3e5d85e7d
new file mode 100644 (file)
index 0000000..d138b3c
--- /dev/null
@@ -0,0 +1,254 @@
+Return-Path: <bijan.chokoufe@gmail.com>\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 A82766DE01EA\r
+ for <notmuch@notmuchmail.org>; Sat, 14 May 2016 08:41:57 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.64\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.64 tagged_above=-999 required=5 tests=[AWL=0.180, \r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01,\r
+ RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] 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 tyk-CA4XD4Uz for <notmuch@notmuchmail.org>;\r
+ Sat, 14 May 2016 08:41:49 -0700 (PDT)\r
+Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com\r
+ [74.125.82.67]) by arlo.cworth.org (Postfix) with ESMTPS id 57B266DE01D0 for\r
+ <notmuch@notmuchmail.org>; Sat, 14 May 2016 08:41:49 -0700 (PDT)\r
+Received: by mail-wm0-f67.google.com with SMTP id e201so9357533wme.2\r
+ for <notmuch@notmuchmail.org>; Sat, 14 May 2016 08:41:49 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=date:from:to:subject:message-id:references:mime-version\r
+ :content-disposition:content-transfer-encoding:in-reply-to\r
+ :user-agent; bh=sx7Z6PMn9DIW+syuDyg25KYV7aVN/K0EK0QhqmON3k0=;\r
+ b=hQoc1LuKRIibhGtS29Oz2WP70BQxF4Kldxs2Bkt8A1FKj3+EV5GfydSXsvVOAfeNw3\r
+ rE/uYbyFcU2RWZzvKnFPE4DZhR6Z0r56GpnMo8C9lFf6gLD1QGCk5o2mfoLJz/HRv5Lc\r
+ 1HuHJJuxQ7kKsLMMZYxrFl+iytXxtc7JJxfCm8L0gaeWPSpzMIAl/n7xiCgwJ68J2dr/\r
+ ZzbUvQzmMmcCRoqL7vFIfcKtDlch2exYNefIfwYTCRcq8ddxTd3yX5K1d2Ibsfpozrrv\r
+ /CIzaprksBxG5ZiYmoLKOQsye7UY81lH/RqRGWkioelhO26aS2EB6+tTJGJy0/XA1tyc\r
+ T7UA==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:date:from:to:subject:message-id:references\r
+ :mime-version:content-disposition:content-transfer-encoding\r
+ :in-reply-to:user-agent;\r
+ bh=sx7Z6PMn9DIW+syuDyg25KYV7aVN/K0EK0QhqmON3k0=;\r
+ b=jdvUPxOuC7cYKwdToY+9b1pM0DsvYJv2sIBkrTHyWF0xfR/sWNd8qBj2BNzsebf227\r
+ d4mIak+BhEl2bw1MpDfpq+vI9NWnrvI428OWjic1XRdND8HhtNcLGx2EG9Yft4NNwa0r\r
+ 4Rzn+c0xCrFt3uq19xm+dIazjodD6TE2M6/EfUPkQMaVveQ4rkgB+vP2WW395xJM6OJp\r
+ zNjdBrPzAk6MmqL0/BHIiMqB94q41X4N69UaPAuGNArvOcBpG2jw1mNn6OR3X5azQHsl\r
+ 1Lf/7J/WRZdw7syulMKwMX212haA1tL9S1kvoILXL0XFaT3hnIOprk2JZcBh+IbxQLRA\r
+ LAvA==\r
+X-Gm-Message-State:\r
+ AOPr4FWVyZBtE7IpFZiGupOJf67PyR0YhVAkeFhQWRh+0eN7uWd6PoKlxu62auOI5v0rcQ==\r
+X-Received: by 10.28.211.10 with SMTP id k10mr9281155wmg.82.1463240507968;\r
+ Sat, 14 May 2016 08:41:47 -0700 (PDT)\r
+Received: from localhost (ip5f5be327.dynamic.kabel-deutschland.de.\r
+ [95.91.227.39])\r
+ by smtp.gmail.com with ESMTPSA id az2sm24241802wjc.6.2016.05.14.08.41.46\r
+ for <notmuch@notmuchmail.org>\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Sat, 14 May 2016 08:41:47 -0700 (PDT)\r
+Date: Sat, 14 May 2016 17:41:46 +0200\r
+From: Bijan Chokoufe Nejad <bijan.chokoufe@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: Re: [RFC PATCH] RFC: expand_tilde () for potential future path\r
+ expansion\r
+Message-ID: <20160514154146.GB5583@bijan-TPX1Yoga>\r
+References: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=iso-8859-1\r
+Content-Disposition: inline\r
+Content-Transfer-Encoding: 8bit\r
+In-Reply-To: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi>\r
+User-Agent: Mutt/1.5.24 (2015-08-30)\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 15:41:57 -0000\r
+\r
+(Second Try: This time it should come from a mail adress of the mailing list)\r
+\r
+Woops. Now we did it twice. I have my implementation directly in\r
+notmuch-config.c which makes it harder to test and to reuse elsewhere (not sure\r
+that this is necessary though).\r
+\r
+I tested mine to work with\r
+\r
+~\r
+~/\r
+~foo/.mail\r
+~foo/\r
+~foo\r
+~bar/.mail\r
+~bar/\r
+~bar\r
+\r
+(where bar is not a user in my system but foo is) and all cases work fine. I\r
+send the patch and then we can discuss how to proceed. For me both versions look\r
+good enough though.\r
+\r
+On 16-05-14, Tomi Ollila wrote:\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