]> diplodocus.org Git - nmh/blob - uip/imaptest.c
utils.c: Improve comment describing add(). Suggest mh_xstrdup().
[nmh] / uip / imaptest.c
1 /* imaptest.c -- A program to send commands to an IMAP server
2 *
3 * This code is Copyright (c) 2017, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include <h/netsec.h>
11 #include <stdarg.h>
12 #include <sys/time.h>
13 #include "h/done.h"
14 #include "sbr/base64.h"
15
16 #define IMAPTEST_SWITCHES \
17 X("host hostname", 0, HOSTSW) \
18 X("user username", 0, USERSW) \
19 X("port name/number", 0, PORTSW) \
20 X("snoop", 0, SNOOPSW) \
21 X("nosnoop", 0, NOSNOOPSW) \
22 X("sasl", 0, SASLSW) \
23 X("nosasl", 0, NOSASLSW) \
24 X("saslmech", 0, SASLMECHSW) \
25 X("authservice", 0, AUTHSERVICESW) \
26 X("tls", 0, TLSSW) \
27 X("notls", 0, NOTLSSW) \
28 X("initialtls", 0, INITIALTLSSW) \
29 X("append filename", 0, APPENDSW) \
30 X("timestamp", 0, TIMESTAMPSW) \
31 X("notimestamp", 0, NOTIMESTAMPSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(IMAPTEST);
37 #undef X
38
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(IMAPTEST, switches);
41 #undef X
42
43 struct imap_msg;
44
45 struct imap_msg {
46 char *command; /* Command to send */
47 int queue; /* If true, queue for later delivery */
48 struct imap_msg *next; /* Next pointer */
49 };
50
51 struct imap_msg *msgqueue_head = NULL;
52 struct imap_msg *msgqueue_tail = NULL;
53
54 struct imap_cmd;
55
56 struct imap_cmd {
57 char tag[16]; /* Command tag */
58 struct timeval start; /* Time command was sent */
59 struct imap_cmd *next; /* Next pointer */
60 };
61
62 static struct imap_cmd *cmdqueue = NULL;
63
64 static char *saslmechs = NULL;
65 svector_t imap_capabilities = NULL;
66
67 static int imap_sasl_callback(enum sasl_message_type, unsigned const char *,
68 unsigned int, unsigned char **, unsigned int *,
69 void *, char **);
70
71 static void parse_capability(const char *, unsigned int len);
72 static int capability_set(const char *);
73 static void clear_capability(void);
74 static int have_capability(void);
75 static int send_imap_command(netsec_context *, int noflush, char **errstr,
76 const char *fmt, ...) CHECK_PRINTF(4, 5);
77 static int get_imap_response(netsec_context *, const char *token,
78 char **tokenresp, char **status, int failerr,
79 char **errstr);
80
81 static void ts_report(const char *str, struct timeval *tv);
82
83 static void add_msg(int queue, const char *fmt, ...) CHECK_PRINTF(2, 3);
84
85 static bool timestamp = false;
86
87 int
88 main (int argc, char **argv)
89 {
90 bool sasl = false, tls = false, initialtls = false;
91 bool snoop = false;
92 int fd;
93 char *saslmech = NULL, *host = NULL, *port = "143", *user = NULL;
94 char *cp, **argp, buf[BUFSIZ], *oauth_svc = NULL, *errstr, **arguments, *p;
95 netsec_context *nsc = NULL;
96 struct imap_msg *imsg;
97 size_t len;
98 struct timeval tv_start, tv_connect, tv_auth;
99
100 if (nmh_init(argv[0], 1)) { return 1; }
101
102 arguments = getarguments (invo_name, argc, argv, 1);
103 argp = arguments;
104
105 while ((cp = *argp++)) {
106 if (*cp == '-') {
107 switch (smatch (++cp, switches)) {
108 case AMBIGSW:
109 ambigsw (cp, switches);
110 done (1);
111 case UNKWNSW:
112 adios (NULL, "-%s unknown", cp);
113
114 case HELPSW:
115 snprintf (buf, sizeof(buf), "%s [switches] +folder command "
116 "[command ...]", invo_name);
117 print_help (buf, switches, 1);
118 done (0);
119 case VERSIONSW:
120 print_version(invo_name);
121 done (0);
122
123 case HOSTSW:
124 if (!(host = *argp++) || *host == '-')
125 adios(NULL, "missing argument to %s", argp[-2]);
126 continue;
127 case PORTSW:
128 if (!(port = *argp++) || *port == '-')
129 adios(NULL, "missing argument to %s", argp[-2]);
130 continue;
131 case USERSW:
132 if (!(user = *argp++) || *user == '-')
133 adios(NULL, "missing argument to %s", argp[-2]);
134 continue;
135
136 case SNOOPSW:
137 snoop = true;
138 continue;
139 case NOSNOOPSW:
140 snoop = false;
141 continue;
142 case SASLSW:
143 sasl = true;
144 continue;
145 case NOSASLSW:
146 sasl = false;
147 continue;
148 case SASLMECHSW:
149 if (!(saslmech = *argp++) || *saslmech == '-')
150 adios(NULL, "missing argument to %s", argp[-2]);
151 continue;
152 case AUTHSERVICESW:
153 if (!(oauth_svc = *argp++) || *oauth_svc == '-')
154 adios(NULL, "missing argument to %s", argp[-2]);
155 continue;
156 case TLSSW:
157 tls = true;
158 initialtls = false;
159 continue;
160 case INITIALTLSSW:
161 tls = false;
162 initialtls = true;
163 continue;
164 case NOTLSSW:
165 tls = false;
166 initialtls = false;
167 continue;
168 case TIMESTAMPSW:
169 timestamp = true;
170 continue;
171 case NOTIMESTAMPSW:
172 timestamp = false;
173 continue;
174 }
175 } else if (*cp == '+') {
176 if (*(cp + 1) == '\0')
177 adios(NULL, "Invalid null folder name");
178 add_msg(0, "SELECT \"%s\"", cp + 1);
179 } else {
180 add_msg(0, "%s", cp);
181 }
182 }
183
184 if (! host)
185 adios(NULL, "A hostname must be given with -host");
186
187 nsc = netsec_init();
188
189 if (user)
190 netsec_set_userid(nsc, user);
191
192 netsec_set_hostname(nsc, host);
193
194 if (snoop)
195 netsec_set_snoop(nsc, 1);
196
197 if (oauth_svc) {
198 if (netsec_set_oauth_service(nsc, oauth_svc) != OK) {
199 adios(NULL, "OAuth2 not supported");
200 }
201 }
202
203 if (timestamp)
204 gettimeofday(&tv_start, NULL);
205
206 if ((fd = client(host, port, buf, sizeof(buf), snoop)) == NOTOK)
207 adios(NULL, "Connect failed: %s", buf);
208
209 if (timestamp) {
210 ts_report("Connect time", &tv_start);
211 gettimeofday(&tv_connect, NULL);
212 }
213
214 netsec_set_fd(nsc, fd, fd);
215 netsec_set_snoop(nsc, snoop);
216
217 if (initialtls || tls) {
218 if (netsec_set_tls(nsc, 1, 0, &errstr) != OK)
219 adios(NULL, "%s", errstr);
220
221 if (initialtls && netsec_negotiate_tls(nsc, &errstr) != OK)
222 adios(NULL, "%s", errstr);
223 }
224
225 if (sasl) {
226 if (netsec_set_sasl_params(nsc, "imap", saslmech, imap_sasl_callback,
227 nsc, &errstr) != OK)
228 adios(NULL, "%s", errstr);
229 }
230
231 if ((cp = netsec_readline(nsc, &len, &errstr)) == NULL) {
232 adios(NULL, "%s", errstr);
233 }
234
235 if (has_prefix(cp, "* BYE")) {
236 fprintf(stderr, "Connection rejected: %s\n", cp + 5);
237 goto finish;
238 }
239
240 if (!has_prefix(cp, "* OK") && !has_prefix(cp, "* PREAUTH")) {
241 fprintf(stderr, "Invalid server response: %s\n", cp);
242 goto finish;
243 }
244
245 if ((p = strchr(cp + 2, ' ')) && *(p + 1) != '\0' &&
246 has_prefix(p + 1, "[CAPABILITY ")) {
247 /*
248 * Parse the capability list out to the end
249 */
250 char *q;
251 p += 13; /* 1 + [CAPABILITY + space */
252
253 if (!(q = strchr(p, ']'))) {
254 fprintf(stderr, "Cannot find end of CAPABILITY announcement\n");
255 goto finish;
256 }
257
258 parse_capability(p, q - p);
259 } else {
260 char *capstring;
261
262 if (send_imap_command(nsc, 0, &errstr, "CAPABILITY") != OK) {
263 fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr);
264 goto finish;
265 }
266
267 if (get_imap_response(nsc, "CAPABILITY", &capstring, NULL, 1,
268 &errstr) != OK) {
269 fprintf(stderr, "Cannot get CAPABILITY response: %s\n", errstr);
270 goto finish;
271 }
272
273 if (! capstring) {
274 fprintf(stderr, "No CAPABILITY response seen\n");
275 goto finish;
276 }
277
278 parse_capability(capstring, strlen(capstring));
279 free(capstring);
280 }
281
282 if (tls) {
283 if (!capability_set("STARTTLS")) {
284 fprintf(stderr, "Requested STARTTLS with -tls, but IMAP server "
285 "has no support for STARTTLS\n");
286 goto finish;
287 }
288 if (send_imap_command(nsc, 0, &errstr, "STARTTLS") != OK) {
289 fprintf(stderr, "Unable to issue STARTTLS: %s\n", errstr);
290 goto finish;
291 }
292
293 if (get_imap_response(nsc, NULL, NULL, NULL, 1, &errstr) != OK) {
294 fprintf(stderr, "STARTTLS failed: %s\n", errstr);
295 goto finish;
296 }
297 if (netsec_negotiate_tls(nsc, &errstr) != OK) {
298 adios(NULL, "%s", errstr);
299 }
300 }
301
302 if (sasl) {
303 if (netsec_negotiate_sasl(nsc, saslmechs, &errstr) != OK) {
304 fprintf(stderr, "SASL negotiation failed: %s\n", errstr);
305 goto finish;
306 }
307 /*
308 * Sigh. If we negotiated a SSF AND we got a capability response
309 * as part of the AUTHENTICATE OK message, we can't actually trust
310 * it because it's not protected at that point. So discard the
311 * capability list and we will generate a fresh CAPABILITY command
312 * later.
313 */
314 if (netsec_get_sasl_ssf(nsc) > 0) {
315 clear_capability();
316 }
317 } else {
318 if (capability_set("LOGINDISABLED")) {
319 fprintf(stderr, "User did not request SASL, but LOGIN "
320 "is disabled\n");
321 goto finish;
322 }
323 }
324
325 if (!have_capability()) {
326 char *capstring;
327
328 if (send_imap_command(nsc, 0, &errstr, "CAPABILITY") != OK) {
329 fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr);
330 goto finish;
331 }
332
333 if (get_imap_response(nsc, "CAPABILITY", &capstring, NULL, 1,
334 &errstr) != OK) {
335 fprintf(stderr, "Cannot get CAPABILITY response: %s\n", errstr);
336 goto finish;
337 }
338
339 if (! capstring) {
340 fprintf(stderr, "No CAPABILITY response seen\n");
341 goto finish;
342 }
343
344 parse_capability(capstring, strlen(capstring));
345 free(capstring);
346 }
347
348 if (timestamp) {
349 ts_report("Authentication time", &tv_connect);
350 gettimeofday(&tv_auth, NULL);
351 }
352
353 while (msgqueue_head != NULL) {
354 imsg = msgqueue_head;
355
356 if (send_imap_command(nsc, imsg->queue, &errstr, "%s",
357 imsg->command) != OK) {
358 fprintf(stderr, "Cannot send command \"%s\": %s\n", imsg->command,
359 errstr);
360 free(errstr);
361 goto finish;
362 }
363
364 if (! imsg->queue) {
365 if (get_imap_response(nsc, NULL, NULL, NULL, 0, &errstr) != OK) {
366 fprintf(stderr, "Unable to get response for command "
367 "\"%s\": %s\n", imsg->command, errstr);
368 goto finish;
369 }
370 }
371
372 msgqueue_head = imsg->next;
373
374 free(imsg->command);
375 free(imsg);
376 }
377
378 ts_report("Total command execution time", &tv_auth);
379
380 send_imap_command(nsc, 0, NULL, "LOGOUT");
381 get_imap_response(nsc, NULL, NULL, NULL, 0, NULL);
382
383 finish:
384 netsec_shutdown(nsc);
385
386 ts_report("Total elapsed time", &tv_start);
387
388 exit(0);
389 }
390
391 /*
392 * Parse a capability string
393 *
394 * Put these into an svector so we can check for stuff later. But special
395 * things, like AUTH, we parse now
396 */
397
398 static void
399 parse_capability(const char *cap, unsigned int len)
400 {
401 char *str = mh_xmalloc(len + 1);
402 char **caplist;
403 int i;
404
405 trunccpy(str, cap, len + 1);
406 caplist = brkstring(str, " ", NULL);
407
408 if (imap_capabilities) {
409 svector_free(imap_capabilities);
410 }
411
412 imap_capabilities = svector_create(32);
413
414 if (saslmechs) {
415 free(saslmechs);
416 saslmechs = NULL;
417 }
418
419 for (i = 0; caplist[i] != NULL; i++) {
420 if (has_prefix(caplist[i], "AUTH=") && *(caplist[i] + 5) != '\0') {
421 if (saslmechs) {
422 saslmechs = add(" ", saslmechs);
423 }
424 saslmechs = add(caplist[i] + 5, saslmechs);
425 } else {
426 svector_push_back(imap_capabilities, getcpy(caplist[i]));
427 }
428 }
429
430 free(str);
431 }
432
433 /*
434 * Return 1 if a particular capability is set
435 */
436
437 static int
438 capability_set(const char *capability)
439 {
440 if (!imap_capabilities)
441 return 0;
442
443 return svector_find(imap_capabilities, capability) != NULL;
444 }
445
446 /*
447 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
448 */
449
450 static void
451 clear_capability(void)
452 {
453 svector_free(imap_capabilities);
454 imap_capabilities = NULL;
455 }
456
457 static int
458 have_capability(void)
459 {
460 return imap_capabilities != NULL;
461 }
462
463 /*
464 * Our SASL callback, which handles the SASL authentication dialog
465 */
466
467 static int
468 imap_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
469 unsigned int indatalen, unsigned char **outdata,
470 unsigned int *outdatalen, void *context, char **errstr)
471 {
472 int rc, snoopoffset;
473 char *mech, *line;
474 size_t len;
475 netsec_context *nsc = (netsec_context *) context;
476
477 switch (mtype) {
478 case NETSEC_SASL_START:
479 /*
480 * Generate our AUTHENTICATE message.
481 *
482 * If SASL-IR capability is set, we can include any initial response
483 * in the AUTHENTICATE command. Otherwise we have to wait for
484 * the first server response (which should be blank).
485 */
486
487 mech = netsec_get_sasl_mechanism(nsc);
488
489 if (indatalen) {
490 char *b64data;
491 b64data = mh_xmalloc(BASE64SIZE(indatalen));
492 writeBase64raw(indata, indatalen, (unsigned char *) b64data);
493 if (capability_set("SASL-IR")) {
494 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder,
495 &snoopoffset);
496 /*
497 * Sigh. We're assuming a short tag length here. Really,
498 * I guess we should figure out a way to get the length
499 * of the next tag.
500 */
501 snoopoffset = 17 + strlen(mech);
502 rc = send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s %s",
503 mech, b64data);
504 free(b64data);
505 netsec_set_snoop_callback(nsc, NULL, NULL);
506 if (rc)
507 return NOTOK;
508 } else {
509 rc = send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s", mech);
510 line = netsec_readline(nsc, &len, errstr);
511 if (! line)
512 return NOTOK;
513 /*
514 * We should get a "+ ", nothing else.
515 */
516 if (len != 2 || strcmp(line, "+ ") != 0) {
517 netsec_err(errstr, "Did not get expected blank response "
518 "for initial challenge response");
519 return NOTOK;
520 }
521 rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
522 free(b64data);
523 if (rc != OK)
524 return NOTOK;
525 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
526 rc = netsec_flush(nsc, errstr);
527 netsec_set_snoop_callback(nsc, NULL, NULL);
528 if (rc != OK)
529 return NOTOK;
530 }
531 } else {
532 if (send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s",
533 mech) != OK)
534 return NOTOK;
535 }
536
537 break;
538
539 /*
540 * Get a response, decode it and process it.
541 */
542
543 case NETSEC_SASL_READ:
544 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, &snoopoffset);
545 snoopoffset = 2;
546 line = netsec_readline(nsc, &len, errstr);
547 netsec_set_snoop_callback(nsc, NULL, NULL);
548
549 if (line == NULL)
550 return NOTOK;
551
552 if (len < 2 || (len == 2 && strcmp(line, "+ ") != 0)) {
553 netsec_err(errstr, "Invalid format for SASL response");
554 return NOTOK;
555 }
556
557 if (len == 2) {
558 *outdata = NULL;
559 *outdatalen = 0;
560 } else {
561 rc = decodeBase64(line + 2, outdata, &len, 0, NULL);
562 *outdatalen = len;
563 if (rc != OK) {
564 netsec_err(errstr, "Unable to decode base64 response");
565 return NOTOK;
566 }
567 }
568 break;
569
570 /*
571 * Simple request encoding
572 */
573
574 case NETSEC_SASL_WRITE:
575 if (indatalen > 0) {
576 unsigned char *b64data;
577 b64data = mh_xmalloc(BASE64SIZE(indatalen));
578 writeBase64raw(indata, indatalen, b64data);
579 rc = netsec_printf(nsc, errstr, "%s", b64data);
580 free(b64data);
581 if (rc != OK)
582 return NOTOK;
583 }
584
585 if (netsec_printf(nsc, errstr, "\r\n") != OK)
586 return NOTOK;
587
588 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
589 rc = netsec_flush(nsc, errstr);
590 netsec_set_snoop_callback(nsc, NULL, NULL);
591 if (rc != OK)
592 return NOTOK;
593 break;
594
595 /*
596 * Finish protocol
597 */
598
599 case NETSEC_SASL_FINISH:
600 line = NULL;
601 if (get_imap_response(nsc, NULL, NULL, &line, 1, errstr) != OK)
602 return NOTOK;
603 /*
604 * We MIGHT get a capability response here. If so, be sure we
605 * parse it.
606 */
607
608 if (line && has_prefix(line, "OK [CAPABILITY ")) {
609 char *p = line + 15, *q;
610 q = strchr(p, ']');
611
612 if (q)
613 parse_capability(p, q - p);
614 }
615
616 break;
617
618 /*
619 * Cancel an authentication dialog
620 */
621
622 case NETSEC_SASL_CANCEL:
623 rc = netsec_printf(nsc, errstr, "*\r\n");
624 if (rc == OK)
625 rc = netsec_flush(nsc, errstr);
626 if (rc != OK)
627 return NOTOK;
628 break;
629 }
630 return OK;
631 }
632
633 /*
634 * Send a single command to the IMAP server.
635 */
636
637 static int
638 send_imap_command(netsec_context *nsc, int noflush, char **errstr,
639 const char *fmt, ...)
640 {
641 static unsigned int seq = 0; /* Tag sequence number */
642 va_list ap;
643 int rc;
644 struct imap_cmd *cmd;
645
646 cmd = mh_xmalloc(sizeof(*cmd));
647
648 snprintf(cmd->tag, sizeof(cmd->tag), "A%u ", seq++);
649
650 if (timestamp)
651 gettimeofday(&cmd->start, NULL);
652
653 if (netsec_write(nsc, cmd->tag, strlen(cmd->tag), errstr) != OK) {
654 free(cmd);
655 return NOTOK;
656 }
657
658 va_start(ap, fmt);
659 rc = netsec_vprintf(nsc, errstr, fmt, ap);
660 va_end(ap);
661
662 if (rc == OK)
663 rc = netsec_write(nsc, "\r\n", 2, errstr);
664
665 if (rc != OK) {
666 free(cmd);
667 return NOTOK;
668 }
669
670 if (!noflush && netsec_flush(nsc, errstr) != OK) {
671 free(cmd);
672 return NOTOK;
673 }
674
675 cmd->next = cmdqueue;
676 cmdqueue = cmd;
677
678 return OK;
679 }
680
681 /*
682 * Get all outstanding responses. If we were passed in a token string
683 * to look for, return it.
684 */
685
686 static int
687 get_imap_response(netsec_context *nsc, const char *token, char **tokenresponse,
688 char **status, int failerr, char **errstr)
689 {
690 char *line;
691 struct imap_cmd *cmd;
692 int numerrs = 0;
693
694 if (tokenresponse)
695 *tokenresponse = NULL;
696
697 getline:
698 while (cmdqueue) {
699 if (!(line = netsec_readline(nsc, NULL, errstr)))
700 return NOTOK;
701 if (has_prefix(line, "* ") && *(line + 2) != '\0') {
702 if (token && tokenresponse && has_prefix(line + 2, token)) {
703 if (*tokenresponse)
704 free(*tokenresponse);
705 *tokenresponse = getcpy(line + 2);
706 }
707 } else {
708
709 if (has_prefix(line, cmdqueue->tag)) {
710 cmd = cmdqueue;
711 if (timestamp)
712 ts_report("Command execution time", &cmd->start);
713 cmdqueue = cmd->next;
714 free(cmd);
715 } else {
716 for (cmd = cmdqueue; cmd->next != NULL; cmd = cmd->next) {
717 if (has_prefix(line, cmd->next->tag)) {
718 struct imap_cmd *cmd2 = cmd->next;
719 cmd->next = cmd->next->next;
720 if (failerr && strncmp(line + strlen(cmd2->tag),
721 "OK ", 3) != 0) {
722 numerrs++;
723 netsec_err(errstr, "%s", line + strlen(cmd2->tag));
724 }
725 free(cmd2);
726 if (status)
727 *status = getcpy(line);
728 goto getline;
729 }
730 netsec_err(errstr, "Non-matching response line: %s\n",
731 line);
732 return NOTOK;
733 }
734 }
735 }
736 }
737
738 return numerrs == 0 ? OK : NOTOK;
739 }
740
741 /*
742 * Add an IMAP command to the msg queue
743 */
744
745 static void
746 add_msg(int queue, const char *fmt, ...)
747 {
748 struct imap_msg *imsg;
749 va_list ap;
750 size_t msgbufsize;
751 char *msg = NULL;
752 int rc = 63;
753
754 do {
755 msgbufsize = rc + 1;
756 msg = mh_xrealloc(msg, msgbufsize);
757 va_start(ap, fmt);
758 rc = vsnprintf(msg, msgbufsize, fmt, ap);
759 va_end(ap);
760 } while (rc >= (int) msgbufsize);
761
762 imsg = mh_xmalloc(sizeof(*imsg));
763
764 imsg->command = msg;
765 imsg->queue = queue;
766 imsg->next = NULL;
767
768 if (msgqueue_head == NULL) {
769 msgqueue_head = imsg;
770 msgqueue_tail = imsg;
771 } else {
772 msgqueue_tail->next = imsg;
773 msgqueue_tail = imsg;
774 }
775 }
776
777 /*
778 * Give a timestamp report.
779 */
780
781 static void
782 ts_report(const char *str, struct timeval *tv)
783 {
784 struct timeval now;
785 double delta;
786
787 gettimeofday(&now, NULL);
788
789 delta = ((double) now.tv_sec) - ((double) tv->tv_sec) +
790 (now.tv_usec / 1E6 - tv->tv_usec / 1E6);
791
792 printf("%s: %f sec\n", str, delta);
793 }