1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Auxiliary functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
44 #include <sys/utsname.h>
52 # include <sys/socket.h>
67 struct mem_chunk
*mc_prev
;
68 struct mem_chunk
*mc_next
;
78 struct mem_chunk
*p_c
;
82 #endif /* HAVE_DEBUG */
84 /* NYD, memory pool debug */
86 static ui32_t _nyd_curr
, _nyd_level
;
87 static struct nyd_info _nyd_infos
[NYD_CALLS_MAX
];
89 static size_t _mem_aall
, _mem_acur
, _mem_amax
,
90 _mem_mall
, _mem_mcur
, _mem_mmax
;
92 static struct mem_chunk
*_mem_list
, *_mem_free
;
95 /* {hold,rele}_all_sigs() */
96 static size_t _alls_depth
;
97 static sigset_t _alls_nset
, _alls_oset
;
99 /* {hold,rele}_sigs() */
100 static size_t _hold_sigdepth
;
101 static sigset_t _hold_nset
, _hold_oset
;
103 /* Create an ISO 6429 (ECMA-48/ANSI) terminal control escape sequence */
105 static char * _colour_iso6429(char const *wish
);
109 static void _nyd_print(struct nyd_info
*nip
);
114 _colour_iso6429(char const *wish
)
116 char const * const wish_orig
= wish
;
117 char *xwish
, *cp
, cfg
[3] = {0, 0, 0};
120 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
121 * value, ensure we have enough room for that */
123 size_t i
= strlen(wish
) +1;
124 xwish
= salloc(MAX(i
, sizeof("\033[1;30;40m")));
125 memcpy(xwish
, wish
, i
);
129 /* Iterate over the colour spec */
130 while ((cp
= n_strsep(&xwish
, ',', TRU1
)) != NULL
) {
131 char *y
, *x
= strchr(cp
, '=');
134 fprintf(stderr
, tr(527,
135 "Invalid colour specification \"%s\": >>> %s <<<\n"),
141 /* TODO convert the ft/fg/bg parser into a table-based one! */
142 if (!asccasecmp(cp
, "ft")) {
143 if (!asccasecmp(x
, "bold"))
145 else if (!asccasecmp(x
, "inverse"))
147 else if (!asccasecmp(x
, "underline"))
151 } else if (!asccasecmp(cp
, "fg")) {
154 } else if (!asccasecmp(cp
, "bg")) {
157 if (!asccasecmp(x
, "black"))
159 else if (!asccasecmp(x
, "blue"))
161 else if (!asccasecmp(x
, "green"))
163 else if (!asccasecmp(x
, "red"))
165 else if (!asccasecmp(x
, "brown"))
167 else if (!asccasecmp(x
, "magenta"))
169 else if (!asccasecmp(x
, "cyan"))
171 else if (!asccasecmp(x
, "white"))
179 /* Restore our salloc() buffer, create return value */
180 xwish
= UNCONST(wish
);
181 if (cfg
[0] || cfg
[1] || cfg
[2]) {
195 if (cfg
[0] || cfg
[1])
205 return UNCONST(wish
);
207 #endif /* HAVE_COLOUR */
211 _nyd_print(struct nyd_info
*nip
) /* XXX like SFSYS;no magics;jumps:lvl wrong */
214 union {int i
; size_t z
;} u
;
216 u
.i
= snprintf(buf
, sizeof buf
, "%c [%2u] %-25.25s %.16s:%-5u\n",
217 "=><"[(nip
->ni_chirp_line
>> 29) & 0x3], nip
->ni_level
, nip
->ni_fun
,
218 nip
->ni_file
, (nip
->ni_chirp_line
& 0x1FFFFFFFu
));
221 if (u
.z
> sizeof buf
)
222 u
.z
= sizeof buf
- 1; /* (Skip \0) */
223 write(STDERR_FILENO
, buf
, u
.z
);
229 panic(char const *format
, ...)
234 fprintf(stderr
, tr(1, "Panic: "));
236 va_start(ap
, format
);
237 vfprintf(stderr
, format
, ap
);
243 abort(); /* Was exit(EXIT_ERR); for a while, but no */
247 alert(char const *format
, ...)
252 fprintf(stderr
, tr(1, "Panic: "));
254 va_start(ap
, format
);
255 vfprintf(stderr
, format
, ap
);
264 safe_signal(int signum
, sighandler_type handler
)
266 struct sigaction nact
, oact
;
270 nact
.sa_handler
= handler
;
271 sigemptyset(&nact
.sa_mask
);
274 nact
.sa_flags
|= SA_RESTART
;
276 rv
= (sigaction(signum
, &nact
, &oact
) != 0) ? SIG_ERR
: oact
.sa_handler
;
285 if (_alls_depth
++ == 0) {
286 sigfillset(&_alls_nset
);
287 sigdelset(&_alls_nset
, SIGABRT
);
289 sigdelset(&_alls_nset
, SIGBUS
);
291 sigdelset(&_alls_nset
, SIGCHLD
);
292 sigdelset(&_alls_nset
, SIGFPE
);
293 sigdelset(&_alls_nset
, SIGILL
);
294 sigdelset(&_alls_nset
, SIGKILL
);
295 sigdelset(&_alls_nset
, SIGSEGV
);
296 sigdelset(&_alls_nset
, SIGSTOP
);
297 sigprocmask(SIG_BLOCK
, &_alls_nset
, &_alls_oset
);
306 if (--_alls_depth
== 0)
307 sigprocmask(SIG_SETMASK
, &_alls_oset
, (sigset_t
*)NULL
);
315 if (_hold_sigdepth
++ == 0) {
316 sigemptyset(&_hold_nset
);
317 sigaddset(&_hold_nset
, SIGHUP
);
318 sigaddset(&_hold_nset
, SIGINT
);
319 sigaddset(&_hold_nset
, SIGQUIT
);
320 sigprocmask(SIG_BLOCK
, &_hold_nset
, &_hold_oset
);
329 if (--_hold_sigdepth
== 0)
330 sigprocmask(SIG_SETMASK
, &_hold_oset
, NULL
);
336 _nyd_chirp(ui8_t act
, char const *file
, ui32_t line
, char const *fun
)
338 struct nyd_info
*nip
= _nyd_infos
;
340 if (_nyd_curr
!= NELEM(_nyd_infos
))
346 nip
->ni_chirp_line
= ((ui32_t
)(act
& 0x3) << 29) | (line
& 0x1FFFFFFFu
);
347 nip
->ni_level
= ((act
== 0) ? _nyd_level
348 : (act
== 1) ? ++_nyd_level
: _nyd_level
--);
352 _nyd_oncrash(int signo
)
354 struct sigaction xact
;
356 struct nyd_info
*nip
;
359 xact
.sa_handler
= SIG_DFL
;
360 sigemptyset(&xact
.sa_mask
);
362 sigaction(signo
, &xact
, NULL
);
364 fprintf(stderr
, "\n\nNYD: program dying due to signal %d:\n", signo
);
365 if (_nyd_infos
[NELEM(_nyd_infos
) - 1].ni_file
!= NULL
)
366 for (i
= _nyd_curr
, nip
= _nyd_infos
+ i
; i
< NELEM(_nyd_infos
); ++i
)
368 for (i
= 0, nip
= _nyd_infos
; i
< _nyd_curr
; ++i
)
372 sigaddset(&xset
, signo
);
373 sigprocmask(SIG_UNBLOCK
, &xset
, NULL
);
381 touch(struct message
*mp
)
384 mp
->m_flag
|= MTOUCH
;
385 if (!(mp
->m_flag
& MREAD
))
386 mp
->m_flag
|= MREAD
| MSTATUS
;
391 is_dir(char const *name
)
397 if (!stat(name
, &sbuf
))
398 rv
= (S_ISDIR(sbuf
.st_mode
) != 0);
404 argcount(char **argv
)
409 for (ap
= argv
; *ap
++ != NULL
;)
412 return (int)PTR2SIZE(ap
- argv
- 1);
422 if ((cp
= ok_vlook(screen
)) == NULL
|| (s
= atoi(cp
)) <= 0)
423 s
= scrnheight
- 4; /* XXX no magics */
434 cp
= ok_vlook(PAGER
);
435 if (cp
== NULL
|| *cp
== '\0')
442 paging_seems_sensible(void)
448 if (IS_TTY_SESSION() && (cp
= ok_vlook(crt
)) != NULL
)
449 rv
= (*cp
!= '\0') ? (size_t)atol(cp
) : (size_t)scrnheight
;
455 page_or_print(FILE *fp
, size_t lines
)
463 if ((rows
= paging_seems_sensible()) != 0 && lines
== 0) {
464 while ((c
= getc(fp
)) != EOF
)
465 if (c
== '\n' && ++lines
> rows
)
470 if (rows
!= 0 && lines
>= rows
)
471 run_command(get_pager(), 0, fileno(fp
), -1, NULL
, NULL
, NULL
);
473 while ((c
= getc(fp
)) != EOF
)
479 which_protocol(char const *name
)
485 enum protocol rv
= PROTO_UNKNOWN
;
488 if (name
[0] == '%' && name
[1] == ':')
490 for (cp
= name
; *cp
&& *cp
!= ':'; cp
++)
494 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
495 if (!strncmp(name
, "pop3://", 7)) {
499 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
501 } else if (!strncmp(name
, "pop3s://", 8)) {
502 #if defined HAVE_POP3 && defined HAVE_SSL
506 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
509 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
512 } else if (!strncmp(name
, "imap://", 7)) {
516 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
518 } else if (!strncmp(name
, "imaps://", 8)) {
519 #if defined HAVE_IMAP && defined HAVE_SSL
523 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
526 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
533 /* TODO This is the de facto maildir code and thus belongs into there!
534 * TODO and: we should have maildir:// and mbox:// pseudo-protos, instead of
535 * TODO or (more likely) in addition to *newfolders*) */
538 np
= ac_alloc((sz
= strlen(name
)) + 4 +1);
539 memcpy(np
, name
, sz
+ 1);
540 if (!stat(name
, &st
)) {
541 if (S_ISDIR(st
.st_mode
) &&
542 (strcpy(&np
[sz
], "/tmp"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
543 (strcpy(&np
[sz
], "/new"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
544 (strcpy(&np
[sz
], "/cur"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)))
546 } else if ((cp
= ok_vlook(newfolders
)) != NULL
&& !strcmp(cp
, "maildir"))
555 torek_hash(char const *name
)
557 /* Chris Torek's hash.
558 * NOTE: need to change *at least* create-okey-map.pl when changing the
563 while (*name
!= '\0') {
572 pjw(char const *cp
) /* TODO obsolete that -> torek_hash */
579 h
= (h
<< 4 & 0xffffffff) + (*cp
&0377);
580 if ((g
= h
& 0xf0000000) != 0) {
592 static ui32_t
const primes
[] = {
593 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521,
594 131071, 262139, 524287, 1048573, 2097143, 4194301,
595 8388593, 16777213, 33554393, 67108859, 134217689,
596 268435399, 536870909, 1073741789, 2147483647
599 ui32_t mprime
= 7, cutlim
;
603 cutlim
= (n
< 65536 ? n
<< 2 : (n
< 262144 ? n
<< 1 : n
));
605 for (i
= 0; i
< NELEM(primes
); i
++)
606 if ((mprime
= primes
[i
]) >= cutlim
)
608 if (i
== NELEM(primes
) && mprime
< n
)
615 expand_shell_escape(char const **s
, bool_t use_nail_extensions
)
623 if ((c
= *xs
& 0xFF) == '\0')
629 switch ((c
= *xs
& 0xFF)) {
631 case 'a': c
= '\a'; break;
632 case 'b': c
= '\b'; break;
633 case 'c': c
= PROMPT_STOP
; break;
634 case 'f': c
= '\f'; break;
635 case 'n': c
= '\n'; break;
636 case 'r': c
= '\r'; break;
637 case 't': c
= '\t'; break;
638 case 'v': c
= '\v'; break;
640 for (++xs
, c
= 0, n
= 4; --n
> 0 && octalchar(*xs
); ++xs
) {
645 /* S-nail extension for nice (get)prompt(()) support */
650 if (use_nail_extensions
) {
652 case '&': c
= ok_blook(bsdcompat
) ? '&' : '?'; break;
653 case '?': c
= exec_last_comm_error
? '1' : '0'; break;
654 case '$': c
= PROMPT_DOLLAR
; break;
655 case '@': c
= PROMPT_AT
; break;
661 /* A sole <backslash> at EOS is treated as-is! */
677 static char buf
[PROMPT_BUFFER_SIZE
];
683 if ((ccp
= ok_vlook(prompt
)) == NULL
|| *ccp
== '\0')
686 for (; PTRCMP(cp
, <, buf
+ sizeof(buf
) - 1); ++cp
) {
689 int c
= expand_shell_escape(&ccp
, TRU1
);
695 if (c
== 0 || c
== PROMPT_STOP
)
698 a
= (c
== PROMPT_DOLLAR
) ? account_name
: displayname
;
702 if (PTRCMP(cp
+ l
, >=, buf
+ sizeof(buf
) - 1))
716 nodename(int mayoverride
)
718 static char *hostname
;
724 struct addrinfo hints
, *res
;
726 struct hostent
*hent
;
731 if (mayoverride
&& (hn
= ok_vlook(hostname
)) != NULL
&& *hn
!= '\0') {
732 if (hostname
!= NULL
)
734 hostname
= sstrdup(hn
);
735 } else if (hostname
== NULL
) {
740 memset(&hints
, 0, sizeof hints
);
741 hints
.ai_family
= AF_UNSPEC
;
742 hints
.ai_socktype
= SOCK_DGRAM
; /* (dummy) */
743 hints
.ai_flags
= AI_CANONNAME
;
744 if (getaddrinfo(hn
, "0", &hints
, &res
) == 0) {
745 if (res
->ai_canonname
!= NULL
) {
746 size_t l
= strlen(res
->ai_canonname
) +1;
748 memcpy(hn
, res
->ai_canonname
, l
);
753 hent
= gethostbyname(hn
);
758 hostname
= sstrdup(hn
);
759 #if defined HAVE_SOCKETS && defined HAVE_IPV6
760 if (hn
!= ut
.nodename
)
769 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
771 #if defined HAVE_SMTP && defined HAVE_POP3 && defined HAVE_IMAP
774 #if defined HAVE_SMTP || defined HAVE_POP3 || defined HAVE_IMAP
782 memset(urlp
, 0, sizeof *urlp
);
783 urlp
->url_input
= data
;
784 urlp
->url_cproto
= cproto
;
786 /* Network protocol */
787 #define _protox(X,Y) \
788 urlp->url_portno = Y;\
789 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
790 urlp->url_proto[sizeof(X) -1] = '\0';\
791 urlp->url_proto_len = sizeof(X) -1;\
792 urlp->url_proto_xlen = sizeof(X "://") -1
793 #define __if(X,Y,Z) \
794 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
796 data += sizeof(X "://") -1;\
797 do { Z; } while (0);\
800 #define _if(X,Y) __if(X, Y, (void)0)
802 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
804 # define _ifs(X,Y) goto jeproto;
811 _if ("submission", 587)
822 _protox("pop3", 110);
831 _protox("imap", 143);
843 if (strstr(data
, "://") != NULL
) {
844 #if !defined __ALLPROTO || !defined HAVE_SSL
847 fprintf(stderr
, tr(574, "URL `proto://' prefix invalid: `%s'\n"),
853 /* User and password, I */
855 if ((cp
= UNCONST(last_at_before_slash(data
))) == NULL
)
858 urlp
->url_had_user
= TRU1
;
859 urlp
->url_user
.s
= savestrbuf(data
, urlp
->url_user
.l
= PTR2SIZE(cp
- data
));
862 /* And also have a password? */
863 if ((cp
= strchr(urlp
->url_user
.s
, ':')) != NULL
) {
864 x
= urlp
->url_user
.s
;
865 urlp
->url_user
.s
= savestrbuf(x
, urlp
->url_user
.l
= PTR2SIZE(cp
- x
));
866 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(++cp
));
867 urlp
->url_pass_enc
.l
= strlen(
868 urlp
->url_pass_enc
.s
= urlxenc(urlp
->url_pass
.s
));
871 /* Servername and port -- and possible path suffix */
873 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
877 urlp
->url_port
= x
= savestr(x
= cp
+ 1);
878 if ((x
= strchr(x
, '/')) != NULL
)
880 l
= strtol(urlp
->url_port
, &eptr
, 10);
881 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
882 fprintf(stderr
, tr(573, "URL with invalid port number: `%s'\n"),
886 urlp
->url_portno
= (ui16_t
)l
;
888 if ((x
= strchr(data
, '/')) != NULL
)
889 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
890 cp
= UNCONST(data
+ strlen(data
));
893 /* A (non-empty) path may only occur with IMAP */
894 if (x
!= NULL
&& x
[1] != '\0') {
895 if (cproto
!= CPROTO_IMAP
) {
896 fprintf(stderr
, tr(575, "URL protocol doesn't support paths: `%s'\n"),
900 urlp
->url_path
.l
= strlen(++x
);
901 urlp
->url_path
.s
= savestrbuf(x
, urlp
->url_path
.l
);
904 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
906 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
907 *cp
= lowerconv(*cp
);
912 struct str
*s
= &urlp
->url_hp
;
914 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
915 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
916 if (urlp
->url_port
!= NULL
) {
917 size_t j
= strlen(urlp
->url_port
);
919 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
927 * If there was no user in the URL, do we have *user-HOST* or *user*? */
928 if (!urlp
->url_had_user
) {
929 char const usrstr
[] = "user-";
930 size_t const usrlen
= sizeof(usrstr
) -1;
932 cp
= ac_alloc(usrlen
+ urlp
->url_hp
.l
+1);
933 memcpy(cp
, usrstr
, usrlen
);
934 memcpy(cp
+ usrlen
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
935 if ((urlp
->url_user
.s
= vok_vlook(cp
)) == NULL
) {
936 cp
[usrlen
- 1] = '\0';
937 if ((urlp
->url_user
.s
= vok_vlook(cp
)) == NULL
)
938 urlp
->url_user
.s
= UNCONST(myname
);
943 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(urlp
->url_user
.s
));
944 urlp
->url_user_enc
.l
= strlen(
945 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
));
948 if (urlp
->url_user_enc
.l
== 0)
949 urlp
->url_uhp
= urlp
->url_hp
;
951 struct str
*s
= &urlp
->url_uhp
;
952 size_t i
= urlp
->url_user_enc
.l
;
954 s
->s
= salloc(i
+ 1 + urlp
->url_hp
.l
+1);
956 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
959 memcpy(s
->s
+ i
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
965 if (urlp
->url_user_enc
.l
== 0)
966 urlp
->url_uh
= urlp
->url_host
;
968 struct str
*s
= &urlp
->url_uh
;
969 size_t i
= urlp
->url_user_enc
.l
;
971 s
->s
= salloc(i
+ 1 + urlp
->url_host
.l
+1);
973 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
976 memcpy(s
->s
+ i
, urlp
->url_host
.s
, urlp
->url_host
.l
+1);
977 i
+= urlp
->url_host
.l
;
981 /* Finally, for fun: .url_proto://.url_uhp[/.url_path] */
983 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_uhp
.l
) +
984 1 + urlp
->url_path
.l
+1);
986 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
987 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_uhp
.s
, urlp
->url_uhp
.l
+1);
988 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
990 if (urlp
->url_path
.l
== 0)
991 urlp
->url_puhp
= urlp
->url_puhpp
= ud
;
993 urlp
->url_puhp
= savestrbuf(ud
, i
);
994 urlp
->url_puhpp
= ud
;
997 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1002 #endif /* __ANYPROTO */
1011 lookup_password_for_token(char const *token
)
1018 var
= ac_alloc(tl
+ 9 +1);
1020 memcpy(var
, "password-", 9);
1021 memcpy(var
+ 9, token
, tl
);
1024 if ((cp
= vok_vlook(var
)) != NULL
)
1032 getrandstring(size_t length
)
1034 static unsigned char nodedigest
[16];
1048 data
= ac_alloc(length
);
1050 if ((fd
= open("/dev/urandom", O_RDONLY
)) == -1 ||
1051 length
!= (size_t)read(fd
, data
, length
)) {
1058 md5_update(&ctx
, (unsigned char*)cp
, strlen(cp
));
1059 md5_final(nodedigest
, &ctx
);
1061 /* In that case it's only used for boundaries and Message-Id:s so that
1062 * srand(3) should suffice */
1064 for (i
= 0; i
< sizeof(nodedigest
); ++i
)
1065 nodedigest
[i
] = (unsigned char)(cp
[i
% j
] ^ rand());
1068 for (i
= 0; i
< length
; i
++)
1069 data
[i
] = (char)((int)(255 * (rand() / (RAND_MAX
+ 1.0))) ^
1070 nodedigest
[i
% sizeof nodedigest
]);
1075 b64_encode_buf(&b64
, data
, length
, B64_SALLOC
);
1077 assert(length
< b64
.l
);
1078 b64
.s
[length
] = '\0';
1085 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1087 char const *cp
= vp
;
1091 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; i
++) {
1093 hex
[j
] = hexchar((cp
[i
] & 0xf0) >> 4);
1094 hex
[++j
] = hexchar(cp
[i
] & 0x0f);
1101 cram_md5_string(char const *user
, char const *pass
, char const *b64
)
1104 char digest
[16], *cp
;
1109 in
.s
= UNCONST(b64
);
1110 in
.l
= strlen(in
.s
);
1111 b64_decode(&out
, &in
, NULL
);
1112 assert(out
.s
!= NULL
);
1114 hmac_md5((unsigned char*)out
.s
, out
.l
, UNCONST(pass
), strlen(pass
), digest
);
1116 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1119 in
.l
= lu
+ MD5TOHEX_SIZE
+1;
1120 in
.s
= ac_alloc(lu
+ 1 + MD5TOHEX_SIZE
+1);
1121 memcpy(in
.s
, user
, lu
);
1123 memcpy(in
.s
+ lu
, cp
, MD5TOHEX_SIZE
);
1124 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1131 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1135 * This code is taken from
1137 * Network Working Group H. Krawczyk
1138 * Request for Comments: 2104 IBM
1139 * Category: Informational M. Bellare
1146 * HMAC: Keyed-Hashing for Message Authentication
1149 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1150 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1151 unsigned char tk
[16];
1155 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1160 md5_update(&tctx
, key
, key_len
);
1161 md5_final(tk
, &tctx
);
1167 /* the HMAC_MD5 transform looks like:
1169 * MD5(K XOR opad, MD5(K XOR ipad, text))
1171 * where K is an n byte key
1172 * ipad is the byte 0x36 repeated 64 times
1173 * opad is the byte 0x5c repeated 64 times
1174 * and text is the data being protected */
1176 /* start out by storing key in pads */
1177 memset(k_ipad
, 0, sizeof k_ipad
);
1178 memset(k_opad
, 0, sizeof k_opad
);
1179 memcpy(k_ipad
, key
, key_len
);
1180 memcpy(k_opad
, key
, key_len
);
1182 /* XOR key with ipad and opad values */
1183 for (i
=0; i
<64; i
++) {
1188 /* perform inner MD5 */
1189 md5_init(&context
); /* init context for 1st pass */
1190 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1191 md5_update(&context
, text
, text_len
); /* then text of datagram */
1192 md5_final(digest
, &context
); /* finish up 1st pass */
1194 /* perform outer MD5 */
1195 md5_init(&context
); /* init context for 2nd pass */
1196 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1197 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1198 md5_final(digest
, &context
); /* finish up 2nd pass */
1201 #endif /* HAVE_MD5 */
1204 makedir(char const *name
)
1207 enum okay rv
= STOP
;
1210 if (!mkdir(name
, 0700))
1214 if ((e
== EEXIST
|| e
== ENOSYS
) && !stat(name
, &st
) &&
1215 S_ISDIR(st
.st_mode
))
1224 cwget(struct cw
*cw
)
1226 enum okay rv
= STOP
;
1229 if ((cw
->cw_fd
= open(".", O_RDONLY
)) == -1)
1231 if (fchdir(cw
->cw_fd
) == -1) {
1242 cwret(struct cw
*cw
)
1244 enum okay rv
= STOP
;
1247 if (!fchdir(cw
->cw_fd
))
1254 cwrelse(struct cw
*cw
)
1261 #else /* !HAVE_FCHDIR */
1263 cwget(struct cw
*cw
)
1265 enum okay rv
= STOP
;
1268 if (getcwd(cw
->cw_wd
, sizeof cw
->cw_wd
) != NULL
&& !chdir(cw
->cw_wd
))
1275 cwret(struct cw
*cw
)
1277 enum okay rv
= STOP
;
1280 if (!chdir(cw
->cw_wd
))
1287 cwrelse(struct cw
*cw
)
1293 #endif /* !HAVE_FCHDIR */
1296 colalign(char const *cp
, int col
, int fill
, int *cols_decr_used_or_null
)
1298 int col_orig
= col
, n
, sz
;
1302 np
= nb
= salloc(mb_cur_max
* strlen(cp
) + col
+1);
1305 if (mb_cur_max
> 1) {
1308 if ((sz
= mbtowc(&wc
, cp
, mb_cur_max
)) == -1)
1310 else if ((n
= wcwidth(wc
)) == -1)
1318 if (sz
== 1 && spacechar(*cp
)) {
1326 if (fill
&& col
!= 0) {
1328 memmove(nb
+ col
, nb
, PTR2SIZE(np
- nb
));
1329 memset(nb
, ' ', col
);
1331 memset(np
, ' ', col
);
1337 if (cols_decr_used_or_null
!= NULL
)
1338 *cols_decr_used_or_null
-= col_orig
- col
;
1344 makeprint(struct str
const *in
, struct str
*out
)
1346 static int print_all_chars
= -1;
1348 char const *inp
, *maxp
;
1353 if (print_all_chars
== -1)
1354 print_all_chars
= ok_blook(print_all_chars
);
1357 out
->s
= outp
= smalloc(msz
);
1361 if (print_all_chars
) {
1363 memcpy(outp
, inp
, out
->l
);
1367 #ifdef HAVE_C90AMEND1
1368 if (mb_cur_max
> 1) {
1369 char mbb
[MB_LEN_MAX
+ 1];
1375 while (inp
< maxp
) {
1377 n
= mbtowc(&wc
, inp
, PTR2SIZE(maxp
- inp
));
1383 /* FIXME Why mbtowc() resetting here?
1384 * FIXME what about ISO 2022-JP plus -- those
1385 * FIXME will loose shifts, then!
1386 * FIXME THUS - we'd need special "known points"
1387 * FIXME to do so - say, after a newline!!
1388 * FIXME WE NEED TO CHANGE ALL USES +MBLEN! */
1389 mbtowc(&wc
, NULL
, mb_cur_max
);
1390 wc
= utf8
? 0xFFFD : '?';
1395 if (!iswprint(wc
) && wc
!= '\n' && wc
!= '\r' && wc
!= '\b' &&
1397 if ((wc
& ~(wchar_t)037) == 0)
1398 wc
= utf8
? 0x2400 | wc
: '?';
1399 else if (wc
== 0177)
1400 wc
= utf8
? 0x2421 : '?';
1402 wc
= utf8
? 0x2426 : '?';
1404 if ((n
= wctomb(mbb
, wc
)) <= 0)
1407 if (out
->l
>= msz
- 1) {
1408 dist
= outp
- out
->s
;
1409 out
->s
= srealloc(out
->s
, msz
+= 32);
1410 outp
= &out
->s
[dist
];
1412 for (i
= 0; i
< n
; ++i
)
1416 #endif /* C90AMEND1 */
1419 while (inp
< maxp
) {
1421 if (!isprint(c
) && c
!= '\n' && c
!= '\r' && c
!= '\b' && c
!= '\t')
1428 out
->s
[out
->l
] = '\0';
1433 prstr(char const *s
)
1441 makeprint(&in
, &out
);
1442 rp
= savestrbuf(out
.s
, out
.l
);
1449 prout(char const *s
, size_t sz
, FILE *fp
)
1457 makeprint(&in
, &out
);
1458 n
= fwrite(out
.s
, 1, out
.l
, fp
);
1465 putuc(int u
, int c
, FILE *fp
)
1471 #ifdef HAVE_C90AMEND1
1472 if (utf8
&& (u
& ~(wchar_t)0177)) {
1473 char mbb
[MB_LEN_MAX
];
1476 if ((n
= wctomb(mbb
, u
)) > 0) {
1478 for (i
= 0; i
< n
; ++i
)
1479 if (putc(mbb
[i
] & 0377, fp
) == EOF
) {
1484 rv
= (putc('\0', fp
) != EOF
);
1489 rv
= (putc(c
, fp
) != EOF
);
1496 colour_table_create(char const *pager_used
)
1498 union {char *cp
; char const *ccp
; void *vp
; struct colour_table
*ctp
;} u
;
1500 struct colour_table
*ct
;
1503 if (ok_blook(colour_disable
))
1506 /* If pager, check wether it is allowed to use colour */
1507 if (pager_used
!= NULL
) {
1510 if ((u
.cp
= ok_vlook(colour_pagers
)) == NULL
)
1511 u
.ccp
= COLOUR_PAGERS
;
1512 pager
= savestr(u
.cp
);
1514 while ((u
.cp
= n_strsep(&pager
, ',', TRU1
)) != NULL
)
1515 if (strstr(pager_used
, u
.cp
) != NULL
)
1520 /* $TERM is different in that we default to false unless whitelisted */
1522 char *term
, *okterms
;
1524 /* Don't use getenv(), but force copy-in into our own tables.. */
1525 if ((term
= _var_voklook("TERM")) == NULL
)
1527 if ((okterms
= ok_vlook(colour_terms
)) == NULL
)
1528 okterms
= UNCONST(COLOUR_TERMS
);
1529 okterms
= savestr(okterms
);
1532 while ((u
.cp
= n_strsep(&okterms
, ',', TRU1
)) != NULL
)
1533 if (!strncmp(u
.cp
, term
, i
))
1539 colour_table
= ct
= salloc(sizeof *ct
); /* XXX lex.c yet resets (FILTER!) */
1542 enum colourspec cspec
;
1545 {ok_v_colour_msginfo
, COLOURSPEC_MSGINFO
, COLOUR_MSGINFO
},
1546 {ok_v_colour_partinfo
, COLOURSPEC_PARTINFO
, COLOUR_PARTINFO
},
1547 {ok_v_colour_from_
, COLOURSPEC_FROM_
, COLOUR_FROM_
},
1548 {ok_v_colour_header
, COLOURSPEC_HEADER
, COLOUR_HEADER
},
1549 {ok_v_colour_uheader
, COLOURSPEC_UHEADER
, COLOUR_UHEADER
}
1552 for (i
= 0; i
< NELEM(map
); ++i
) {
1553 if ((u
.cp
= _var_oklook(map
[i
].okey
)) == NULL
)
1554 u
.ccp
= map
[i
].defval
;
1555 u
.cp
= _colour_iso6429(u
.ccp
);
1556 ct
->ct_csinfo
[map
[i
].cspec
].l
= strlen(u
.cp
);
1557 ct
->ct_csinfo
[map
[i
].cspec
].s
= u
.cp
;
1560 ct
->ct_csinfo
[COLOURSPEC_RESET
].l
= sizeof("\033[0m") -1;
1561 ct
->ct_csinfo
[COLOURSPEC_RESET
].s
= UNCONST("\033[0m");
1563 if ((u
.cp
= ok_vlook(colour_user_headers
)) == NULL
)
1564 u
.ccp
= COLOUR_USER_HEADERS
;
1565 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].l
= i
= strlen(u
.ccp
);
1566 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].s
= (i
== 0) ? NULL
: savestr(u
.ccp
);
1572 colour_put(FILE *fp
, enum colourspec cs
)
1575 if (colour_table
!= NULL
) {
1576 struct str
const *cp
= colour_get(cs
);
1578 fwrite(cp
->s
, cp
->l
, 1, fp
);
1584 colour_put_header(FILE *fp
, char const *name
)
1586 enum colourspec cs
= COLOURSPEC_HEADER
;
1587 struct str
const *uheads
;
1588 char *cp
, *cp_base
, *x
;
1592 if (colour_table
== NULL
)
1594 /* Normal header colours if there are no user headers */
1595 uheads
= colour_table
->ct_csinfo
+ COLOURSPEC_RESET
+ 1;
1596 if (uheads
->s
== NULL
)
1599 /* Iterate over all entries in the *colour-user-headers* list */
1600 cp
= ac_alloc(uheads
->l
+1);
1601 memcpy(cp
, uheads
->s
, uheads
->l
+1);
1603 namelen
= strlen(name
);
1604 while ((x
= n_strsep(&cp
, ',', TRU1
)) != NULL
) {
1605 size_t l
= (cp
!= NULL
) ? PTR2SIZE(cp
- x
) - 1 : strlen(x
);
1606 if (l
== namelen
&& !ascncasecmp(x
, name
, namelen
)) {
1607 cs
= COLOURSPEC_UHEADER
;
1619 colour_reset(FILE *fp
)
1622 if (colour_table
!= NULL
)
1623 fwrite("\033[0m", 4, 1, fp
);
1627 FL
struct str
const *
1628 colour_get(enum colourspec cs
)
1630 struct str
const *rv
= NULL
;
1633 if (colour_table
!= NULL
)
1634 if ((rv
= colour_table
->ct_csinfo
+ cs
)->s
== NULL
)
1639 #endif /* HAVE_COLOUR */
1642 time_current_update(struct time_current
*tc
, bool_t full_update
)
1645 tc
->tc_time
= time(NULL
);
1647 memcpy(&tc
->tc_gm
, gmtime(&tc
->tc_time
), sizeof tc
->tc_gm
);
1648 memcpy(&tc
->tc_local
, localtime(&tc
->tc_time
), sizeof tc
->tc_local
);
1649 sstpcpy(tc
->tc_ctime
, ctime(&tc
->tc_time
));
1655 _out_of_memory(void)
1662 smalloc(size_t s SMALLOC_DEBUG_ARGS
)
1669 if ((rv
= malloc(s
)) == NULL
)
1676 srealloc(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
1685 else if ((rv
= realloc(v
, s
)) == NULL
)
1692 scalloc(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
1699 if ((rv
= calloc(nmemb
, size
)) == NULL
)
1705 #else /* !HAVE_DEBUG */
1706 CTA(sizeof(char) == sizeof(ui8_t
));
1708 # define _HOPE_SIZE (2 * 8 * sizeof(char))
1709 # define _HOPE_SET(C) \
1711 union mem_ptr __xl, __xu;\
1712 struct mem_chunk *__xc;\
1713 __xl.p_p = (C).p_p;\
1714 __xc = __xl.p_c - 1;\
1717 __xl.p_ui8p[0]=0xDE; __xl.p_ui8p[1]=0xAA;\
1718 __xl.p_ui8p[2]=0x55; __xl.p_ui8p[3]=0xAD;\
1719 __xl.p_ui8p[4]=0xBE; __xl.p_ui8p[5]=0x55;\
1720 __xl.p_ui8p[6]=0xAA; __xl.p_ui8p[7]=0xEF;\
1721 __xu.p_ui8p += __xc->mc_size - 8;\
1722 __xu.p_ui8p[0]=0xDE; __xu.p_ui8p[1]=0xAA;\
1723 __xu.p_ui8p[2]=0x55; __xu.p_ui8p[3]=0xAD;\
1724 __xu.p_ui8p[4]=0xBE; __xu.p_ui8p[5]=0x55;\
1725 __xu.p_ui8p[6]=0xAA; __xu.p_ui8p[7]=0xEF;\
1727 # define _HOPE_GET_TRACE(C,BAD) \
1733 # define _HOPE_GET(C,BAD) \
1735 union mem_ptr __xl, __xu;\
1736 struct mem_chunk *__xc;\
1738 __xl.p_p = (C).p_p;\
1740 (C).p_cp = __xl.p_cp;\
1741 __xc = __xl.p_c - 1;\
1744 if (__xl.p_ui8p[0] != 0xDE) __i |= 1<<0;\
1745 if (__xl.p_ui8p[1] != 0xAA) __i |= 1<<1;\
1746 if (__xl.p_ui8p[2] != 0x55) __i |= 1<<2;\
1747 if (__xl.p_ui8p[3] != 0xAD) __i |= 1<<3;\
1748 if (__xl.p_ui8p[4] != 0xBE) __i |= 1<<4;\
1749 if (__xl.p_ui8p[5] != 0x55) __i |= 1<<5;\
1750 if (__xl.p_ui8p[6] != 0xAA) __i |= 1<<6;\
1751 if (__xl.p_ui8p[7] != 0xEF) __i |= 1<<7;\
1754 alert("%p: corrupt lower canary: 0x%02X: %s, line %u",\
1755 __xl.p_p, __i, mdbg_file, mdbg_line);\
1758 __xu.p_ui8p += __xc->mc_size - 8;\
1760 if (__xu.p_ui8p[0] != 0xDE) __i |= 1<<0;\
1761 if (__xu.p_ui8p[1] != 0xAA) __i |= 1<<1;\
1762 if (__xu.p_ui8p[2] != 0x55) __i |= 1<<2;\
1763 if (__xu.p_ui8p[3] != 0xAD) __i |= 1<<3;\
1764 if (__xu.p_ui8p[4] != 0xBE) __i |= 1<<4;\
1765 if (__xu.p_ui8p[5] != 0x55) __i |= 1<<5;\
1766 if (__xu.p_ui8p[6] != 0xAA) __i |= 1<<6;\
1767 if (__xu.p_ui8p[7] != 0xEF) __i |= 1<<7;\
1770 alert("%p: corrupt upper canary: 0x%02X: %s, line %u",\
1771 __xl.p_p, __i, mdbg_file, mdbg_line);\
1774 alert(" ..canary last seen: %s, line %u",\
1775 __xc->mc_file, __xc->mc_line);\
1779 (smalloc
)(size_t s SMALLOC_DEBUG_ARGS
)
1786 s
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
1788 if ((p
.p_p
= (malloc
)(s
)) == NULL
)
1790 p
.p_c
->mc_prev
= NULL
;
1791 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
1792 _mem_list
->mc_prev
= p
.p_c
;
1793 p
.p_c
->mc_file
= mdbg_file
;
1794 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
1795 p
.p_c
->mc_isfree
= FAL0
;
1796 p
.p_c
->mc_size
= (ui32_t
)s
;
1797 _mem_list
= p
.p_c
++;
1802 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
1805 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
1811 (srealloc
)(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
1817 if ((p
.p_p
= v
) == NULL
) {
1818 p
.p_p
= (smalloc
)(s
, mdbg_file
, mdbg_line
);
1822 _HOPE_GET(p
, isbad
);
1824 if (p
.p_c
->mc_isfree
) {
1825 fprintf(stderr
, "srealloc(): region freed! At %s, line %d\n"
1826 "\tLast seen: %s, line %d\n",
1827 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1831 if (p
.p_c
== _mem_list
)
1832 _mem_list
= p
.p_c
->mc_next
;
1834 p
.p_c
->mc_prev
->mc_next
= p
.p_c
->mc_next
;
1835 if (p
.p_c
->mc_next
!= NULL
)
1836 p
.p_c
->mc_next
->mc_prev
= p
.p_c
->mc_prev
;
1839 _mem_mcur
-= p
.p_c
->mc_size
;
1843 s
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
1845 if ((p
.p_p
= (realloc
)(p
.p_c
, s
)) == NULL
)
1847 p
.p_c
->mc_prev
= NULL
;
1848 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
1849 _mem_list
->mc_prev
= p
.p_c
;
1850 p
.p_c
->mc_file
= mdbg_file
;
1851 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
1852 p
.p_c
->mc_isfree
= FAL0
;
1853 p
.p_c
->mc_size
= (ui32_t
)s
;
1854 _mem_list
= p
.p_c
++;
1859 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
1862 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
1869 (scalloc
)(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
1879 size
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
1881 if ((p
.p_p
= (malloc
)(size
)) == NULL
)
1883 memset(p
.p_p
, 0, size
);
1884 p
.p_c
->mc_prev
= NULL
;
1885 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
1886 _mem_list
->mc_prev
= p
.p_c
;
1887 p
.p_c
->mc_file
= mdbg_file
;
1888 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
1889 p
.p_c
->mc_isfree
= FAL0
;
1890 p
.p_c
->mc_size
= (ui32_t
)size
;
1891 _mem_list
= p
.p_c
++;
1896 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
1899 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
1905 (sfree
)(void *v SMALLOC_DEBUG_ARGS
)
1911 if ((p
.p_p
= v
) == NULL
) {
1912 fprintf(stderr
, "sfree(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
1916 _HOPE_GET(p
, isbad
);
1918 if (p
.p_c
->mc_isfree
) {
1919 fprintf(stderr
, "sfree(): double-free avoided at %s, line %d\n"
1920 "\tLast seen: %s, line %d\n",
1921 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
1925 if (p
.p_c
== _mem_list
)
1926 _mem_list
= p
.p_c
->mc_next
;
1928 p
.p_c
->mc_prev
->mc_next
= p
.p_c
->mc_next
;
1929 if (p
.p_c
->mc_next
!= NULL
)
1930 p
.p_c
->mc_next
->mc_prev
= p
.p_c
->mc_prev
;
1931 p
.p_c
->mc_isfree
= TRU1
;
1934 _mem_mcur
-= p
.p_c
->mc_size
;
1936 if (options
& OPT_DEBUG
) {
1937 p
.p_c
->mc_next
= _mem_free
;
1949 size_t c
= 0, s
= 0;
1952 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
;) {
1955 s
+= p
.p_c
->mc_size
;
1956 p
.p_c
= p
.p_c
->mc_next
;
1961 if (options
& OPT_DEBUG
)
1962 fprintf(stderr
, "smemreset(): freed %" ZFMT
" chunks/%" ZFMT
" bytes\n",
1968 c_smemtrace(void *v
)
1970 /* For _HOPE_GET() */
1971 char const * const mdbg_file
= "smemtrace()";
1972 int const mdbg_line
= -1;
1974 union mem_ptr p
, xp
;
1980 if ((fp
= Ftmp(NULL
, "memtr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1986 fprintf(fp
, "Memory statistics:\n"
1987 " Count cur/peek/all: %7" ZFMT
"/%7" ZFMT
"/%10" ZFMT
"\n"
1988 " Bytes cur/peek/all: %7" ZFMT
"/%7" ZFMT
"/%10" ZFMT
"\n\n",
1989 _mem_acur
, _mem_amax
, _mem_aall
, _mem_mcur
, _mem_mmax
, _mem_mall
);
1991 fprintf(fp
, "Currently allocated memory chunks:\n");
1992 for (lines
= 0, p
.p_c
= _mem_list
; p
.p_c
!= NULL
;
1993 ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
1996 _HOPE_GET_TRACE(xp
, isbad
);
1997 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
1998 (isbad
? "! CANARY ERROR: " : ""), xp
.p_p
,
1999 (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)), p
.p_c
->mc_file
,
2003 if (options
& OPT_DEBUG
) {
2004 fprintf(fp
, "sfree()d memory chunks awaiting free():\n");
2005 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
; ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2008 _HOPE_GET_TRACE(xp
, isbad
);
2009 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
2010 (isbad
? "! CANARY ERROR: " : ""), xp
.p_p
,
2011 (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2012 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2016 page_or_print(fp
, lines
);
2024 # ifdef _HAVE_MEMCHECK
2026 _smemcheck(char const *mdbg_file
, int mdbg_line
)
2028 union mem_ptr p
, xp
;
2029 bool_t anybad
= FAL0
, isbad
;
2033 for (lines
= 0, p
.p_c
= _mem_list
; p
.p_c
!= NULL
;
2034 ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2037 _HOPE_GET_TRACE(xp
, isbad
);
2041 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
2042 xp
.p_p
, (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2043 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2047 if (options
& OPT_DEBUG
) {
2048 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
; ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2051 _HOPE_GET_TRACE(xp
, isbad
);
2055 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
2056 xp
.p_p
, (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2057 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2064 # endif /* _HAVE_MEMCHECK */
2068 # undef _HOPE_GET_TRACE
2070 #endif /* HAVE_DEBUG */
2072 /* vim:set fenc=utf-8:s-it-mode */