1 Return-Path: <too@guru-group.fi>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by arlo.cworth.org (Postfix) with ESMTP id 22AAF6DE0318
\r
6 for <notmuch@notmuchmail.org>; Sat, 14 May 2016 04:33:27 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
11 X-Spam-Status: No, score=0.261 tagged_above=-999 required=5 tests=[AWL=0.270,
\r
12 HEADER_FROM_DIFFERENT_DOMAINS=0.001, T_RP_MATCHES_RCVD=-0.01]
\r
14 Received: from arlo.cworth.org ([127.0.0.1])
\r
15 by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id js0IYzTJJOZ9 for <notmuch@notmuchmail.org>;
\r
17 Sat, 14 May 2016 04:33:18 -0700 (PDT)
\r
18 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])
\r
19 by arlo.cworth.org (Postfix) with ESMTP id 056586DE02B2
\r
20 for <notmuch@notmuchmail.org>; Sat, 14 May 2016 04:33:17 -0700 (PDT)
\r
21 Received: by guru.guru-group.fi (Postfix, from userid 501)
\r
22 id 1A2D110008D; Sat, 14 May 2016 14:33:10 +0300 (EEST)
\r
23 From: Tomi Ollila <tomi.ollila@iki.fi>
\r
24 To: notmuch@notmuchmail.org
\r
25 Cc: bijan@chokoufe.com,
\r
26 Tomi Ollila <tomi.ollila@iki.fi>
\r
27 Subject: [RFC PATCH] RFC: expand_tilde () for potential future path expansion
\r
28 Date: Sat, 14 May 2016 14:33:08 +0300
\r
29 Message-Id: <1463225588-28059-1-git-send-email-tomi.ollila@iki.fi>
\r
30 X-Mailer: git-send-email 2.8.2
\r
32 Content-Type: text/plain; charset=UTF-8
\r
33 Content-Transfer-Encoding: 8bit
\r
34 X-BeenThere: notmuch@notmuchmail.org
\r
35 X-Mailman-Version: 2.1.20
\r
37 List-Id: "Use and development of the notmuch mail system."
\r
38 <notmuch.notmuchmail.org>
\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
40 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
42 List-Post: <mailto:notmuch@notmuchmail.org>
\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
45 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
46 X-List-Received-Date: Sat, 14 May 2016 11:33:27 -0000
\r
48 One implementation how to do this. Output of the "tests".
\r
51 + 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
55 input '~', expanded '/home/too', rest ''
\r
56 input '~root', expanded '/root', rest ''
\r
58 $ HOME=/a/b/c/ ./expand-tilde '~' '~'/ "~$USER" "~$USER/" '~'root '~'root/ '~none' '~none/'
\r
60 input '~', expanded '/a/b/c/', rest ''
\r
61 input '~/', expanded '/a/b/c/', rest '/'
\r
62 input '~too', expanded '/home/too', rest ''
\r
63 input '~too/', expanded '/home/too', rest '/'
\r
64 input '~root', expanded '/root', rest ''
\r
65 input '~root/', expanded '/root', rest '/'
\r
66 input '~none', expanded '(null)', rest '~none'
\r
67 input '~none/', expanded '(null)', rest '~none/'
\r
69 $ HOME= ./expand-tilde '~' '~'/ "~$USER" "~$USER/"
\r
71 input '~', expanded '', rest ''
\r
72 input '~/', expanded '', rest '/'
\r
73 input '~too', expanded '/home/too', rest ''
\r
74 input '~too/', expanded '/home/too', rest '/'
\r
76 $ ( unset HOME; ./expand-tilde '~' '~'/ "~$USER" "~$USER/" )
\r
78 input '~', expanded '(null)', rest '~'
\r
79 input '~/', expanded '(null)', rest '~/'
\r
80 input '~too', expanded '/home/too', rest ''
\r
81 input '~too/', expanded '/home/too', rest '/'
\r
83 util/expand-tilde.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
84 1 file changed, 102 insertions(+)
\r
85 create mode 100644 util/expand-tilde.c
\r
87 diff --git a/util/expand-tilde.c b/util/expand-tilde.c
\r
88 new file mode 100644
\r
89 index 0000000..e3097e6
\r
91 +++ b/util/expand-tilde.c
\r
93 +#if 0 /* -*- mode: c; c-file-style: "stroustrup"; tab-width: 8; -*-
\r
94 + set -eu; trg=`exec basename "$0" .c`; rm -f "$trg"
\r
95 + WARN="-Wall -Wstrict-prototypes -Winit-self -Wformat=2" # -pedantic
\r
96 + WARN="$WARN -Wcast-align -Wpointer-arith " # -Wfloat-equal #-Werror
\r
97 + WARN="$WARN -Wextra -Wwrite-strings -Wcast-qual -Wshadow" # -Wconversion
\r
98 + WARN="$WARN -Wmissing-include-dirs -Wundef -Wbad-function-cast -Wlogical-op"
\r
99 + WARN="$WARN -Waggregate-return -Wold-style-definition"
\r
100 + WARN="$WARN -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls"
\r
101 + WARN="$WARN -Wnested-externs -Winline -Wvla -Woverlength-strings -Wpadded"
\r
102 + case ${1-} in '') set x -O2; shift; esac
\r
103 + #case ${1-} in '') set x -ggdb; shift; esac
\r
104 + set -x; exec ${CC:-gcc} -std=c99 $WARN "$@" -o "$trg" "$0"
\r
109 + * $ expand-tilde.c $
\r
111 + * Author: Tomi Ollila -- too ät iki piste fi
\r
113 + * Created: Sat 14 May 2016 13:10:35 EEST too
\r
114 + * Last modified: Sat 14 May 2016 14:22:10 +0300 too
\r
116 + * *** Public Domain ***
\r
119 +#include <stdlib.h>
\r
120 +#include <sys/types.h>
\r
123 +/* USER_NAME_MAX_LENGTH is not usually defined (publicly in standard paths). */
\r
125 +#ifndef USER_NAME_MAX_LENGTH
\r
126 +#define USER_NAME_MAX_LENGTH 32
\r
129 +static // for -Wmissing-prototypes
\r
130 +/* This is modeled after expand-file-name in emacs sources (fileio.c);
\r
131 + * simplified and using constant buffer for user name.
\r
132 + * neither emacs nor this implementation has restrictions in username chars
\r
133 + * ...we may be looking forward for multibyte usernames...
\r
135 +const char * expand_tilde(const char * path, const char ** rest)
\r
137 + if (path == NULL || path[0] != '~') {
\r
142 + if (path[1] == '/' || path[1] == '\0') {
\r
143 + const char * home = getenv ("HOME");
\r
144 + *rest = home? path + 1: path;
\r
148 + char username[USER_NAME_MAX_LENGTH + 1];
\r
150 + const char * pathp = path + 1;
\r
153 + username[namepos++] = pathp++[0];
\r
154 + if (pathp[0] == '/' || pathp[0] == '\0')
\r
156 + if (namepos >= USER_NAME_MAX_LENGTH) {
\r
161 + username[namepos] = '\0';
\r
163 + struct passwd * pw = getpwnam (username);
\r
164 + if (pw == NULL) {
\r
168 + *rest = path + 1 + namepos;
\r
169 + return pw->pw_dir;
\r
172 +#include <stdio.h>
\r
174 +static void print_expanded(const char * path)
\r
176 + const char * rest;
\r
177 + const char * expanded = expand_tilde(path, &rest);
\r
178 + printf("input '%s', expanded '%s', rest '%s'\n",
\r
179 + path, expanded, rest);
\r
182 +int main(int argc, char * argv[])
\r
186 + print_expanded("~\0-- this something not to be seen in output --");
\r
187 + print_expanded("~root\0-- this something not to be seen in output --");
\r
189 + for (int i = 1; i < argc; i++)
\r
190 + print_expanded(argv[i]);
\r