Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id A82766DE01EA for ; Sat, 14 May 2016 08:41:57 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.64 X-Spam-Level: X-Spam-Status: No, score=-0.64 tagged_above=-999 required=5 tests=[AWL=0.180, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tyk-CA4XD4Uz for ; Sat, 14 May 2016 08:41:49 -0700 (PDT) Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by arlo.cworth.org (Postfix) with ESMTPS id 57B266DE01D0 for ; Sat, 14 May 2016 08:41:49 -0700 (PDT) Received: by mail-wm0-f67.google.com with SMTP id e201so9357533wme.2 for ; Sat, 14 May 2016 08:41:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=sx7Z6PMn9DIW+syuDyg25KYV7aVN/K0EK0QhqmON3k0=; b=hQoc1LuKRIibhGtS29Oz2WP70BQxF4Kldxs2Bkt8A1FKj3+EV5GfydSXsvVOAfeNw3 rE/uYbyFcU2RWZzvKnFPE4DZhR6Z0r56GpnMo8C9lFf6gLD1QGCk5o2mfoLJz/HRv5Lc 1HuHJJuxQ7kKsLMMZYxrFl+iytXxtc7JJxfCm8L0gaeWPSpzMIAl/n7xiCgwJ68J2dr/ ZzbUvQzmMmcCRoqL7vFIfcKtDlch2exYNefIfwYTCRcq8ddxTd3yX5K1d2Ibsfpozrrv /CIzaprksBxG5ZiYmoLKOQsye7UY81lH/RqRGWkioelhO26aS2EB6+tTJGJy0/XA1tyc T7UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=sx7Z6PMn9DIW+syuDyg25KYV7aVN/K0EK0QhqmON3k0=; b=jdvUPxOuC7cYKwdToY+9b1pM0DsvYJv2sIBkrTHyWF0xfR/sWNd8qBj2BNzsebf227 d4mIak+BhEl2bw1MpDfpq+vI9NWnrvI428OWjic1XRdND8HhtNcLGx2EG9Yft4NNwa0r 4Rzn+c0xCrFt3uq19xm+dIazjodD6TE2M6/EfUPkQMaVveQ4rkgB+vP2WW395xJM6OJp zNjdBrPzAk6MmqL0/BHIiMqB94q41X4N69UaPAuGNArvOcBpG2jw1mNn6OR3X5azQHsl 1Lf/7J/WRZdw7syulMKwMX212haA1tL9S1kvoILXL0XFaT3hnIOprk2JZcBh+IbxQLRA LAvA== X-Gm-Message-State: AOPr4FWVyZBtE7IpFZiGupOJf67PyR0YhVAkeFhQWRh+0eN7uWd6PoKlxu62auOI5v0rcQ== X-Received: by 10.28.211.10 with SMTP id k10mr9281155wmg.82.1463240507968; Sat, 14 May 2016 08:41:47 -0700 (PDT) Received: from localhost (ip5f5be327.dynamic.kabel-deutschland.de. [95.91.227.39]) by smtp.gmail.com with ESMTPSA id az2sm24241802wjc.6.2016.05.14.08.41.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 May 2016 08:41:47 -0700 (PDT) Date: Sat, 14 May 2016 17:41:46 +0200 From: Bijan Chokoufe Nejad To: notmuch@notmuchmail.org Subject: Re: [RFC PATCH] RFC: expand_tilde () for potential future path expansion Message-ID: <20160514154146.GB5583@bijan-TPX1Yoga> References: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi> User-Agent: Mutt/1.5.24 (2015-08-30) X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 May 2016 15:41:57 -0000 (Second Try: This time it should come from a mail adress of the mailing list) Woops. Now we did it twice. I have my implementation directly in notmuch-config.c which makes it harder to test and to reuse elsewhere (not sure that this is necessary though). I tested mine to work with ~ ~/ ~foo/.mail ~foo/ ~foo ~bar/.mail ~bar/ ~bar (where bar is not a user in my system but foo is) and all cases work fine. I send the patch and then we can discuss how to proceed. For me both versions look good enough though. On 16-05-14, Tomi Ollila wrote: > One implementation how to do this. Output of the "tests". > > $ sh expand-tilde.c > + 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 > $ > $ ./expand-tilde > > input '~', expanded '/home/too', rest '' > input '~root', expanded '/root', rest '' > > $ HOME=/a/b/c/ ./expand-tilde '~' '~'/ "~$USER" "~$USER/" '~'root '~'root/ '~none' '~none/' > > input '~', expanded '/a/b/c/', rest '' > input '~/', expanded '/a/b/c/', rest '/' > input '~too', expanded '/home/too', rest '' > input '~too/', expanded '/home/too', rest '/' > input '~root', expanded '/root', rest '' > input '~root/', expanded '/root', rest '/' > input '~none', expanded '(null)', rest '~none' > input '~none/', expanded '(null)', rest '~none/' > > $ HOME= ./expand-tilde '~' '~'/ "~$USER" "~$USER/" > > input '~', expanded '', rest '' > input '~/', expanded '', rest '/' > input '~too', expanded '/home/too', rest '' > input '~too/', expanded '/home/too', rest '/' > > $ ( unset HOME; ./expand-tilde '~' '~'/ "~$USER" "~$USER/" ) > > input '~', expanded '(null)', rest '~' > input '~/', expanded '(null)', rest '~/' > input '~too', expanded '/home/too', rest '' > input '~too/', expanded '/home/too', rest '/' > --- > util/expand-tilde.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 102 insertions(+) > create mode 100644 util/expand-tilde.c > > diff --git a/util/expand-tilde.c b/util/expand-tilde.c > new file mode 100644 > index 0000000..e3097e6 > --- /dev/null > +++ b/util/expand-tilde.c > @@ -0,0 +1,102 @@ > +#if 0 /* -*- mode: c; c-file-style: "stroustrup"; tab-width: 8; -*- > + set -eu; trg=`exec basename "$0" .c`; rm -f "$trg" > + WARN="-Wall -Wstrict-prototypes -Winit-self -Wformat=2" # -pedantic > + WARN="$WARN -Wcast-align -Wpointer-arith " # -Wfloat-equal #-Werror > + WARN="$WARN -Wextra -Wwrite-strings -Wcast-qual -Wshadow" # -Wconversion > + WARN="$WARN -Wmissing-include-dirs -Wundef -Wbad-function-cast -Wlogical-op" > + WARN="$WARN -Waggregate-return -Wold-style-definition" > + WARN="$WARN -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls" > + WARN="$WARN -Wnested-externs -Winline -Wvla -Woverlength-strings -Wpadded" > + case ${1-} in '') set x -O2; shift; esac > + #case ${1-} in '') set x -ggdb; shift; esac > + set -x; exec ${CC:-gcc} -std=c99 $WARN "$@" -o "$trg" "$0" > + exit $? > + */ > +#endif > +/* > + * $ expand-tilde.c $ > + * > + * Author: Tomi Ollila -- too ät iki piste fi > + * > + * Created: Sat 14 May 2016 13:10:35 EEST too > + * Last modified: Sat 14 May 2016 14:22:10 +0300 too > + * > + * *** Public Domain *** > + */ > + > +#include > +#include > +#include > + > +/* USER_NAME_MAX_LENGTH is not usually defined (publicly in standard paths). */ > + > +#ifndef USER_NAME_MAX_LENGTH > +#define USER_NAME_MAX_LENGTH 32 > +#endif > + > +static // for -Wmissing-prototypes > +/* This is modeled after expand-file-name in emacs sources (fileio.c); > + * simplified and using constant buffer for user name. > + * neither emacs nor this implementation has restrictions in username chars > + * ...we may be looking forward for multibyte usernames... > + */ > +const char * expand_tilde(const char * path, const char ** rest) > +{ > + if (path == NULL || path[0] != '~') { > + *rest = path; > + return NULL; > + } > + > + if (path[1] == '/' || path[1] == '\0') { > + const char * home = getenv ("HOME"); > + *rest = home? path + 1: path; > + return home; > + } > + > + char username[USER_NAME_MAX_LENGTH + 1]; > + int namepos = 0; > + const char * pathp = path + 1; > + > + while (1) { > + username[namepos++] = pathp++[0]; > + if (pathp[0] == '/' || pathp[0] == '\0') > + break; > + if (namepos >= USER_NAME_MAX_LENGTH) { > + *rest = path; > + return NULL; > + } > + } > + username[namepos] = '\0'; > + > + struct passwd * pw = getpwnam (username); > + if (pw == NULL) { > + *rest = path; > + return NULL; > + } > + *rest = path + 1 + namepos; > + return pw->pw_dir; > + } > + > +#include > + > +static void print_expanded(const char * path) > +{ > + const char * rest; > + const char * expanded = expand_tilde(path, &rest); > + printf("input '%s', expanded '%s', rest '%s'\n", > + path, expanded, rest); > +} > + > +int main(int argc, char * argv[]) > +{ > + printf("\n"); > + if (argc < 2) { > + print_expanded("~\0-- this something not to be seen in output --"); > + print_expanded("~root\0-- this something not to be seen in output --"); > + } else { > + for (int i = 1; i < argc; i++) > + print_expanded(argv[i]); > + } > + printf("\n"); > + return 0; > +} > -- > 2.8.2 >