Commit 5815d9f5 5815d9f59b60f583a16db4ef267199ed903c861e by Wojciech Polak

(rfc2047_decode_wrapper): Fixed reading a language environment variable.

1 parent 86573c3b
Showing 1 changed file with 32 additions and 28 deletions
...@@ -92,13 +92,16 @@ static const char *capa[] = { ...@@ -92,13 +92,16 @@ static const char *capa[] = {
92 NULL 92 NULL
93 }; 93 };
94 94
95 static void 95 static char *
96 from_rfc2047_decode (char *buf, size_t buflen) 96 rfc2047_decode_wrapper (char *buf, size_t buflen)
97 { 97 {
98 char locale[32];
98 char *charset = NULL; 99 char *charset = NULL;
99 char *tmp; 100 char *tmp;
100 int rc; 101 int rc;
101 102
103 memset (locale, 0, sizeof (locale));
104
102 /* Try to deduce the charset from LC_ALL or LANG variables */ 105 /* Try to deduce the charset from LC_ALL or LANG variables */
103 106
104 tmp = getenv ("LC_ALL"); 107 tmp = getenv ("LC_ALL");
...@@ -107,11 +110,13 @@ from_rfc2047_decode (char *buf, size_t buflen) ...@@ -107,11 +110,13 @@ from_rfc2047_decode (char *buf, size_t buflen)
107 110
108 if (tmp) 111 if (tmp)
109 { 112 {
110 char *sp; 113 char *sp = NULL;
111 char *lang; 114 char *lang;
112 char *terr; 115 char *terr;
113 116
114 lang = strtok_r (tmp, "_", &sp); 117 strncpy (locale, tmp, sizeof (locale) - 1);
118
119 lang = strtok_r (locale, "_", &sp);
115 terr = strtok_r (NULL, ".", &sp); 120 terr = strtok_r (NULL, ".", &sp);
116 charset = strtok_r (NULL, "@", &sp); 121 charset = strtok_r (NULL, "@", &sp);
117 122
...@@ -120,7 +125,7 @@ from_rfc2047_decode (char *buf, size_t buflen) ...@@ -120,7 +125,7 @@ from_rfc2047_decode (char *buf, size_t buflen)
120 } 125 }
121 126
122 if (!charset) 127 if (!charset)
123 return; 128 return strdup (buf);
124 129
125 rc = rfc2047_decode (charset, buf, &tmp); 130 rc = rfc2047_decode (charset, buf, &tmp);
126 if (rc) 131 if (rc)
...@@ -128,12 +133,10 @@ from_rfc2047_decode (char *buf, size_t buflen) ...@@ -128,12 +133,10 @@ from_rfc2047_decode (char *buf, size_t buflen)
128 if (debug) 133 if (debug)
129 mu_error (_("Can't decode line `%s': %s"), 134 mu_error (_("Can't decode line `%s': %s"),
130 buf, mu_strerror (rc)); 135 buf, mu_strerror (rc));
136 return strdup (buf);
131 } 137 }
132 else 138
133 { 139 return tmp;
134 strncpy (buf, tmp, buflen - 1);
135 free (tmp);
136 }
137 } 140 }
138 141
139 int 142 int
...@@ -143,9 +146,7 @@ main (int argc, char **argv) ...@@ -143,9 +146,7 @@ main (int argc, char **argv)
143 size_t i; 146 size_t i;
144 size_t count = 0; 147 size_t count = 0;
145 char *mailbox_name = NULL; 148 char *mailbox_name = NULL;
146 /* Arbitrary limits. A better approach would be to allocate 149 char *buf;
147 as we go along but it is not worth the trouble. */
148 char buf[128];
149 char personal[128]; 150 char personal[128];
150 int status; 151 int status;
151 152
...@@ -204,36 +205,39 @@ main (int argc, char **argv) ...@@ -204,36 +205,39 @@ main (int argc, char **argv)
204 exit (2); 205 exit (2);
205 } 206 }
206 207
207 header_get_value (hdr, MU_HEADER_FROM, buf, sizeof (buf), &len); 208 status = header_aget_value (hdr, MU_HEADER_FROM, &buf);
208 if (len != 0) 209 if (status == 0)
209 { 210 {
210 address_t address = NULL; 211 address_t address = NULL;
211 len = 0;
212 212
213 from_rfc2047_decode (buf, sizeof (buf)); 213 char *s = rfc2047_decode_wrapper (buf, strlen (buf));
214 address_create (&address, buf); 214 address_create (&address, s);
215 address_get_personal (address, 1, personal, 215 free (s);
216 sizeof (personal), &len); 216
217 len = 0;
218 address_get_personal (address, 1, personal, sizeof (personal), &len);
217 printf ("%s\t", (len != 0) ? personal : buf); 219 printf ("%s\t", (len != 0) ? personal : buf);
218 address_destroy (&address); 220 address_destroy (&address);
219 } 221 }
220 else 222 else
221 { 223 {
222 status = header_get_value (hdr, MU_HEADER_TO, buf, 224 status = header_aget_value (hdr, MU_HEADER_TO, &buf);
223 sizeof (buf), &len);
224 if (status == 0) 225 if (status == 0)
225 { 226 {
226 from_rfc2047_decode (buf, sizeof (buf)); 227 char *s = rfc2047_decode_wrapper (buf, strlen (buf));
227 printf ("%s\t", buf); 228 printf ("%s\t", s);
229 free (s);
228 } 230 }
229 } 231 }
232 free (buf);
230 233
231 status = header_get_value_unfold (hdr, MU_HEADER_SUBJECT, 234 status = header_aget_value_unfold (hdr, MU_HEADER_SUBJECT, &buf);
232 buf, sizeof (buf), NULL);
233 if (status == 0) 235 if (status == 0)
234 { 236 {
235 from_rfc2047_decode (buf, sizeof (buf)); 237 char *s = rfc2047_decode_wrapper (buf, strlen (buf));
236 printf ("%s\n", buf); 238 printf ("%s\n", s);
239 free (s);
240 free (buf);
237 } 241 }
238 } 242 }
239 243
......