Commit 715e36a0 715e36a0fc50d2a2af1616d600009633b0be092a by Alain Magloire

* mailbox/misc.c: Removed, _cpystr() function move in mutil.c

	and rename to util_cpystr().
	* mailbox/include/misc.h: Removed.
	* mailbox/mutil.c (util_cpystr): New function, move
	the _cpystr() from misc.c and move here.
	* mailbox/Makefile.am: Removed misc.c in dependencies.
	* mailbox/include/Makefile.am: Removed misc.h in dependencies.
	* mailbox/address.c: Use util_cpystr() instead of _cpystr().
	Added <mailutils/mutil.h> prototype.
	* mailbox/auth.c: Likewised.
	* mailbox/url.c: Likewised.
	* mailbox/folder.c: Likewised
1 parent d3841c3b
...@@ -42,7 +42,6 @@ md5-rsa.c \ ...@@ -42,7 +42,6 @@ md5-rsa.c \
42 message.c \ 42 message.c \
43 memory_stream.c \ 43 memory_stream.c \
44 mime.c \ 44 mime.c \
45 misc.c \
46 monitor.c \ 45 monitor.c \
47 mutil.c \ 46 mutil.c \
48 muerror.c \ 47 muerror.c \
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
28 #include <errno.h> 28 #include <errno.h>
29 29
30 #include <mailutils/parse822.h> 30 #include <mailutils/parse822.h>
31 #include <misc.h> 31 #include <mailutils/mutil.h>
32 #include <address0.h> 32 #include <address0.h>
33 33
34 /* Get email address from rfc822 address. */ 34 /* Get email address from rfc822 address. */
...@@ -103,7 +103,7 @@ address_get_personal (address_t addr, size_t no, char *buf, size_t len, ...@@ -103,7 +103,7 @@ address_get_personal (address_t addr, size_t no, char *buf, size_t len,
103 { 103 {
104 if (j == no) 104 if (j == no)
105 { 105 {
106 i = _cpystr (buf, addr->personal, len); 106 i = util_cpystr (buf, addr->personal, len);
107 status = 0; 107 status = 0;
108 break; 108 break;
109 } 109 }
...@@ -125,7 +125,7 @@ address_get_comments (address_t addr, size_t no, char *buf, size_t len, ...@@ -125,7 +125,7 @@ address_get_comments (address_t addr, size_t no, char *buf, size_t len,
125 { 125 {
126 if (j == no) 126 if (j == no)
127 { 127 {
128 i = _cpystr (buf, addr->comments, len); 128 i = util_cpystr (buf, addr->comments, len);
129 if (n) 129 if (n)
130 *n = i; 130 *n = i;
131 status = 0; 131 status = 0;
...@@ -146,7 +146,7 @@ address_get_email (address_t addr, size_t no, char *buf, size_t len, size_t *n) ...@@ -146,7 +146,7 @@ address_get_email (address_t addr, size_t no, char *buf, size_t len, size_t *n)
146 { 146 {
147 if (j == no) 147 if (j == no)
148 { 148 {
149 i = _cpystr (buf, addr->email, len); 149 i = util_cpystr (buf, addr->email, len);
150 if (n) 150 if (n)
151 *n = i; 151 *n = i;
152 status = 0; 152 status = 0;
...@@ -167,7 +167,7 @@ address_get_local_part (address_t addr, size_t no, char *buf, size_t len, size_t ...@@ -167,7 +167,7 @@ address_get_local_part (address_t addr, size_t no, char *buf, size_t len, size_t
167 { 167 {
168 if (j == no) 168 if (j == no)
169 { 169 {
170 i = _cpystr (buf, addr->local_part, len); 170 i = util_cpystr (buf, addr->local_part, len);
171 if (n) 171 if (n)
172 *n = i; 172 *n = i;
173 status = 0; 173 status = 0;
...@@ -188,7 +188,7 @@ address_get_domain (address_t addr, size_t no, char *buf, size_t len, size_t *n) ...@@ -188,7 +188,7 @@ address_get_domain (address_t addr, size_t no, char *buf, size_t len, size_t *n)
188 { 188 {
189 if (j == no) 189 if (j == no)
190 { 190 {
191 i = _cpystr (buf, addr->domain, len); 191 i = util_cpystr (buf, addr->domain, len);
192 if (n) 192 if (n)
193 *n = i; 193 *n = i;
194 status = 0; 194 status = 0;
...@@ -209,7 +209,7 @@ address_get_route (address_t addr, size_t no, char *buf, size_t len, size_t *n) ...@@ -209,7 +209,7 @@ address_get_route (address_t addr, size_t no, char *buf, size_t len, size_t *n)
209 { 209 {
210 if (j == no) 210 if (j == no)
211 { 211 {
212 i = _cpystr (buf, addr->route, len); 212 i = util_cpystr (buf, addr->route, len);
213 if (n) 213 if (n)
214 *n = i; 214 *n = i;
215 status = 0; 215 status = 0;
...@@ -251,7 +251,7 @@ address_to_string (address_t addr, char *buf, size_t len, size_t *n) ...@@ -251,7 +251,7 @@ address_to_string (address_t addr, char *buf, size_t len, size_t *n)
251 return EINVAL; 251 return EINVAL;
252 if (buf) 252 if (buf)
253 *buf = '\0'; 253 *buf = '\0';
254 i = _cpystr (buf, addr->addr, len); 254 i = util_cpystr (buf, addr->addr, len);
255 if (n) 255 if (n)
256 *n = i; 256 *n = i;
257 return 0; 257 return 0;
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by 5 it under the terms of the GNU General Library Public License as published by
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
26 #include <termios.h> 26 #include <termios.h>
27 27
28 #include <mailutils/mailbox.h> 28 #include <mailutils/mailbox.h>
29 #include <mailutils/mutil.h>
29 #include <auth0.h> 30 #include <auth0.h>
30 #include <misc.h>
31 31
32 static void 32 static void
33 echo_off(struct termios *stored_settings) 33 echo_off(struct termios *stored_settings)
...@@ -134,7 +134,7 @@ ticket_get_type (ticket_t ticket, char *type, size_t len, size_t *pwriten) ...@@ -134,7 +134,7 @@ ticket_get_type (ticket_t ticket, char *type, size_t len, size_t *pwriten)
134 size_t n; 134 size_t n;
135 if (ticket == NULL || type == NULL) 135 if (ticket == NULL || type == NULL)
136 return EINVAL; 136 return EINVAL;
137 n = _cpystr (type, ticket->type, len); 137 n = util_cpystr (type, ticket->type, len);
138 if (pwriten) 138 if (pwriten)
139 *pwriten = n; 139 *pwriten = n;
140 return 0; 140 return 0;
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
28 #include <mailutils/list.h> 28 #include <mailutils/list.h>
29 #include <mailutils/monitor.h> 29 #include <mailutils/monitor.h>
30 30
31 #include <misc.h>
32 #include <folder0.h> 31 #include <folder0.h>
33 32
34 /* Internal folder list. */ 33 /* Internal folder list. */
......
...@@ -15,7 +15,6 @@ mailbox0.h \ ...@@ -15,7 +15,6 @@ mailbox0.h \
15 mailer0.h \ 15 mailer0.h \
16 message0.h \ 16 message0.h \
17 mime0.h \ 17 mime0.h \
18 misc.h \
19 monitor0.h \ 18 monitor0.h \
20 observer0.h \ 19 observer0.h \
21 property0.h \ 20 property0.h \
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MISC_H
19 #define _MISC_H
20
21 #ifdef DMALLOC
22 # include <dmalloc.h>
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <sys/types.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #ifndef __P
36 # if __STDC__
37 # define __P(x) x
38 # else
39 # define __P(x)
40 # endif
41 #endif
42
43 extern size_t _cpystr __P ((char *dst, const char *src, size_t size));
44
45 #ifdef __cplusplus
46 }
47 #endif
48
49 #endif /* _MISC_H */
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <misc.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29
30 /* Smart strncpy that always add the null and returns the number of bytes
31 written. */
32 size_t
33 _cpystr (char *dst, const char *src, size_t size)
34 {
35 size_t len = src ? strlen (src) : 0 ;
36 if (dst == NULL || size == 0)
37 return len;
38 if (len >= size)
39 len = size - 1;
40 memcpy (dst, src, len);
41 dst[len] = '\0';
42 return len;
43 }
...@@ -66,10 +66,10 @@ size_t mu_hexstr2ul(unsigned long* ul, const char* hex, size_t len) ...@@ -66,10 +66,10 @@ size_t mu_hexstr2ul(unsigned long* ul, const char* hex, size_t len)
66 } 66 }
67 67
68 /* Convert struct tm into time_t, taking into account timezone offset. 68 /* Convert struct tm into time_t, taking into account timezone offset.
69 69
70 mktime() always treats tm as if it was localtime, so convert it 70 mktime() always treats tm as if it was localtime, so convert it
71 to UTC, then adjust by the tm's real timezone, if it is known. 71 to UTC, then adjust by the tm's real timezone, if it is known.
72 72
73 NOTE: 1. mktime converts localtime struct tm to *time_t in UTC* 73 NOTE: 1. mktime converts localtime struct tm to *time_t in UTC*
74 2. adding mu_utc_offset() compensates for the localtime 74 2. adding mu_utc_offset() compensates for the localtime
75 corrections in mktime(), i.e. it yields the time_t in 75 corrections in mktime(), i.e. it yields the time_t in
...@@ -80,7 +80,7 @@ time_t ...@@ -80,7 +80,7 @@ time_t
80 mu_tm2time (struct tm *timeptr, mu_timezone* tz) 80 mu_tm2time (struct tm *timeptr, mu_timezone* tz)
81 { 81 {
82 int offset = tz ? tz->utc_offset : 0; 82 int offset = tz ? tz->utc_offset : 0;
83 83
84 return mktime(timeptr) + mu_utc_offset() - offset; 84 return mktime(timeptr) + mu_utc_offset() - offset;
85 } 85 }
86 86
...@@ -249,7 +249,7 @@ mu_get_homedir (void) ...@@ -249,7 +249,7 @@ mu_get_homedir (void)
249 if (!homedir) 249 if (!homedir)
250 { 250 {
251 struct passwd *pwd; 251 struct passwd *pwd;
252 252
253 pwd = getpwuid(getuid()); 253 pwd = getpwuid(getuid());
254 if (!pwd) 254 if (!pwd)
255 return NULL; 255 return NULL;
...@@ -264,7 +264,7 @@ char * ...@@ -264,7 +264,7 @@ char *
264 mu_tilde_expansion (const char *ref, const char *delim, const char *homedir) 264 mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
265 { 265 {
266 char *p = strdup (ref); 266 char *p = strdup (ref);
267 267
268 if (*p == '~') 268 if (*p == '~')
269 { 269 {
270 p++; 270 p++;
...@@ -309,3 +309,18 @@ mu_tilde_expansion (const char *ref, const char *delim, const char *homedir) ...@@ -309,3 +309,18 @@ mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
309 } 309 }
310 return p; 310 return p;
311 } 311 }
312
313 /* Smart strncpy that always add the null and returns the number of bytes
314 written. */
315 size_t
316 util_cpystr (char *dst, const char *src, size_t size)
317 {
318 size_t len = src ? strlen (src) : 0 ;
319 if (dst == NULL || size == 0)
320 return len;
321 if (len >= size)
322 len = size - 1;
323 memcpy (dst, src, len);
324 dst[len] = '\0';
325 return len;
326 }
......
...@@ -28,9 +28,7 @@ ...@@ -28,9 +28,7 @@
28 #endif 28 #endif
29 29
30 #include <mailutils/mutil.h> 30 #include <mailutils/mutil.h>
31 #include <mailutils/registrar.h>
32 31
33 #include <misc.h>
34 #include <url0.h> 32 #include <url0.h>
35 33
36 #ifndef EPARSE 34 #ifndef EPARSE
...@@ -351,7 +349,7 @@ url_get_scheme (const url_t url, char *scheme, size_t len, size_t *n) ...@@ -351,7 +349,7 @@ url_get_scheme (const url_t url, char *scheme, size_t len, size_t *n)
351 return EINVAL; 349 return EINVAL;
352 if (url->_get_scheme) 350 if (url->_get_scheme)
353 return url->_get_scheme (url, scheme, len, n); 351 return url->_get_scheme (url, scheme, len, n);
354 i = _cpystr (scheme, url->scheme, len); 352 i = util_cpystr (scheme, url->scheme, len);
355 if (n) 353 if (n)
356 *n = i; 354 *n = i;
357 return 0; 355 return 0;
...@@ -365,7 +363,7 @@ url_get_user (const url_t url, char *user, size_t len, size_t *n) ...@@ -365,7 +363,7 @@ url_get_user (const url_t url, char *user, size_t len, size_t *n)
365 return EINVAL; 363 return EINVAL;
366 if (url->_get_user) 364 if (url->_get_user)
367 return url->_get_user (url, user, len, n); 365 return url->_get_user (url, user, len, n);
368 i = _cpystr (user, url->user, len); 366 i = util_cpystr (user, url->user, len);
369 if (n) 367 if (n)
370 *n = i; 368 *n = i;
371 return 0; 369 return 0;
...@@ -381,7 +379,7 @@ url_get_passwd (const url_t url, char *passwd, size_t len, size_t *n) ...@@ -381,7 +379,7 @@ url_get_passwd (const url_t url, char *passwd, size_t len, size_t *n)
381 return EINVAL; 379 return EINVAL;
382 if (url->_get_passwd) 380 if (url->_get_passwd)
383 return url->_get_passwd (url, passwd, len, n); 381 return url->_get_passwd (url, passwd, len, n);
384 i = _cpystr (passwd, url->passwd, len); 382 i = util_cpystr (passwd, url->passwd, len);
385 if (n) 383 if (n)
386 *n = i; 384 *n = i;
387 return 0; 385 return 0;
...@@ -395,7 +393,7 @@ url_get_auth (const url_t url, char *auth, size_t len, size_t *n) ...@@ -395,7 +393,7 @@ url_get_auth (const url_t url, char *auth, size_t len, size_t *n)
395 return EINVAL; 393 return EINVAL;
396 if (url->_get_auth) 394 if (url->_get_auth)
397 return url->_get_auth (url, auth, len, n); 395 return url->_get_auth (url, auth, len, n);
398 i = _cpystr (auth, url->auth, len); 396 i = util_cpystr (auth, url->auth, len);
399 if (n) 397 if (n)
400 *n = i; 398 *n = i;
401 return 0; 399 return 0;
...@@ -409,7 +407,7 @@ url_get_host (const url_t url, char *host, size_t len, size_t *n) ...@@ -409,7 +407,7 @@ url_get_host (const url_t url, char *host, size_t len, size_t *n)
409 return EINVAL; 407 return EINVAL;
410 if (url->_get_host) 408 if (url->_get_host)
411 return url->_get_host (url, host, len, n); 409 return url->_get_host (url, host, len, n);
412 i = _cpystr (host, url->host, len); 410 i = util_cpystr (host, url->host, len);
413 if (n) 411 if (n)
414 *n = i; 412 *n = i;
415 return 0; 413 return 0;
...@@ -434,7 +432,7 @@ url_get_path (const url_t url, char *path, size_t len, size_t *n) ...@@ -434,7 +432,7 @@ url_get_path (const url_t url, char *path, size_t len, size_t *n)
434 return EINVAL; 432 return EINVAL;
435 if (url->_get_path) 433 if (url->_get_path)
436 return url->_get_path (url, path, len, n); 434 return url->_get_path (url, path, len, n);
437 i = _cpystr(path, url->path, len); 435 i = util_cpystr(path, url->path, len);
438 if (n) 436 if (n)
439 *n = i; 437 *n = i;
440 return 0; 438 return 0;
...@@ -448,7 +446,7 @@ url_get_query (const url_t url, char *query, size_t len, size_t *n) ...@@ -448,7 +446,7 @@ url_get_query (const url_t url, char *query, size_t len, size_t *n)
448 return EINVAL; 446 return EINVAL;
449 if (url->_get_query) 447 if (url->_get_query)
450 return url->_get_query (url, query, len, n); 448 return url->_get_query (url, query, len, n);
451 i = _cpystr(query, url->query, len); 449 i = util_cpystr(query, url->query, len);
452 if (n) 450 if (n)
453 *n = i; 451 *n = i;
454 return 0; 452 return 0;
......