Re: [PATCH v4 08/16] reorganize indexing of multipart/signed and multipart/encrypted
[notmuch-archives.git] / ba / c02dea3908e9a4e44e4bfdfbc6d892046277fb
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
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.261\r
10 X-Spam-Level: \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
13  autolearn=disabled\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
31 MIME-Version: 1.0\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
36 Precedence: list\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
47 \r
48 One implementation how to do this. Output of the "tests".\r
49 \r
50 $ sh expand-tilde.c\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
52 $\r
53 $ ./expand-tilde\r
54 \r
55 input '~',  expanded '/home/too',  rest ''\r
56 input '~root',  expanded '/root',  rest ''\r
57 \r
58 $ HOME=/a/b/c/ ./expand-tilde '~' '~'/ "~$USER" "~$USER/" '~'root '~'root/ '~none' '~none/'\r
59 \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
68 \r
69 $ HOME= ./expand-tilde '~' '~'/ "~$USER" "~$USER/"\r
70 \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
75 \r
76 $ ( unset HOME; ./expand-tilde '~' '~'/ "~$USER" "~$USER/" )\r
77 \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
82 ---\r
83  util/expand-tilde.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
84  1 file changed, 102 insertions(+)\r
85  create mode 100644 util/expand-tilde.c\r
86 \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
90 --- /dev/null\r
91 +++ b/util/expand-tilde.c\r
92 @@ -0,0 +1,102 @@\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
105 + exit $?\r
106 + */\r
107 +#endif\r
108 +/*\r
109 + * $ expand-tilde.c $\r
110 + *\r
111 + * Author: Tomi Ollila -- too ät iki piste fi\r
112 + *\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
115 + *\r
116 + * *** Public Domain ***\r
117 + */\r
118 +\r
119 +#include <stdlib.h>\r
120 +#include <sys/types.h>\r
121 +#include <pwd.h>\r
122 +\r
123 +/* USER_NAME_MAX_LENGTH is not usually defined (publicly in standard paths). */\r
124 +\r
125 +#ifndef USER_NAME_MAX_LENGTH\r
126 +#define USER_NAME_MAX_LENGTH 32\r
127 +#endif\r
128 +\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
134 + */\r
135 +const char * expand_tilde(const char * path, const char ** rest)\r
136 +{\r
137 +    if (path == NULL || path[0] != '~') {\r
138 +        *rest = path;\r
139 +        return NULL;\r
140 +    }\r
141 +\r
142 +    if (path[1] == '/' || path[1] == '\0') {\r
143 +        const char * home = getenv ("HOME");\r
144 +        *rest = home? path + 1: path;\r
145 +        return home;\r
146 +    }\r
147 +\r
148 +    char username[USER_NAME_MAX_LENGTH + 1];\r
149 +    int namepos = 0;\r
150 +    const char * pathp = path + 1;\r
151 +\r
152 +    while (1) {\r
153 +        username[namepos++] = pathp++[0];\r
154 +        if (pathp[0] == '/' || pathp[0] == '\0')\r
155 +            break;\r
156 +        if (namepos >= USER_NAME_MAX_LENGTH) {\r
157 +            *rest = path;\r
158 +            return NULL;\r
159 +        }\r
160 +    }\r
161 +    username[namepos] = '\0';\r
162 +\r
163 +    struct passwd * pw = getpwnam (username);\r
164 +    if (pw == NULL) {\r
165 +        *rest = path;\r
166 +        return NULL;\r
167 +    }\r
168 +    *rest = path + 1 + namepos;\r
169 +    return pw->pw_dir;\r
170 + }\r
171 +\r
172 +#include <stdio.h>\r
173 +\r
174 +static void print_expanded(const char * path)\r
175 +{\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
180 +}\r
181 +\r
182 +int main(int argc, char * argv[])\r
183 +{\r
184 +    printf("\n");\r
185 +    if (argc < 2) {\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
188 +    } else {\r
189 +        for (int i = 1; i < argc; i++)\r
190 +            print_expanded(argv[i]);\r
191 +    }\r
192 +    printf("\n");\r
193 +    return 0;\r
194 +}\r
195 -- \r
196 2.8.2\r
197 \r