6e63df1fc23642b821cf1fa29ce6a2ec58cd13a6
[openssl.git] / apps / openssl.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/lhash.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
19 #include <openssl/ssl.h>
20 #ifndef OPENSSL_NO_ENGINE
21 # include <openssl/engine.h>
22 #endif
23 #include <openssl/err.h>
24 #ifdef OPENSSL_FIPS
25 # include <openssl/fips.h>
26 #endif
27 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
28 #include "s_apps.h"
29 /* Needed to get the other O_xxx flags. */
30 #ifdef OPENSSL_SYS_VMS
31 # include <unixio.h>
32 #endif
33 #define INCLUDE_FUNCTION_TABLE
34 #include "apps.h"
35
36
37 #ifdef OPENSSL_NO_CAMELLIA
38 # define FORMAT "%-15s"
39 # define COLUMNS 5
40 #else
41 # define FORMAT "%-18s"
42 # define COLUMNS 4
43 #endif
44
45 /* Special sentinel to exit the program. */
46 #define EXIT_THE_PROGRAM (-1)
47
48 /*
49  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
50  * the base prototypes (we cast each variable inside the function to the
51  * required type of "FUNCTION*"). This removes the necessity for
52  * macro-generated wrapper functions.
53  */
54 static LHASH_OF(FUNCTION) *prog_init(void);
55 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
56 static void list_pkey(void);
57 static void list_type(FUNC_TYPE ft);
58 static void list_disabled(void);
59 char *default_config_file = NULL;
60
61 BIO *bio_in = NULL;
62 BIO *bio_out = NULL;
63 BIO *bio_err = NULL;
64
65 static int apps_startup()
66 {
67 #ifdef SIGPIPE
68     signal(SIGPIPE, SIG_IGN);
69 #endif
70
71     /* Set non-default library initialisation settings */
72     if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
73                              | OPENSSL_INIT_LOAD_CONFIG, NULL))
74         return 0;
75
76 #ifndef OPENSSL_NO_UI
77     setup_ui_method();
78 #endif
79
80     return 1;
81 }
82
83 static void apps_shutdown()
84 {
85 #ifndef OPENSSL_NO_UI
86     destroy_ui_method();
87 #endif
88 }
89
90 static char *make_config_name()
91 {
92     const char *t;
93     size_t len;
94     char *p;
95
96     if ((t = getenv("OPENSSL_CONF")) != NULL)
97         return OPENSSL_strdup(t);
98
99     t = X509_get_default_cert_area();
100     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
101     p = app_malloc(len, "config filename buffer");
102     strcpy(p, t);
103 #ifndef OPENSSL_SYS_VMS
104     strcat(p, "/");
105 #endif
106     strcat(p, OPENSSL_CONF);
107
108     return p;
109 }
110
111 int main(int argc, char *argv[])
112 {
113     FUNCTION f, *fp;
114     LHASH_OF(FUNCTION) *prog = NULL;
115     char **copied_argv = NULL;
116     char *p, *pname;
117     char buf[1024];
118     const char *prompt;
119     ARGS arg;
120     int first, n, i, ret = 0;
121
122     arg.argv = NULL;
123     arg.size = 0;
124
125     /* Set up some of the environment. */
126     default_config_file = make_config_name();
127     bio_in = dup_bio_in(FORMAT_TEXT);
128     bio_out = dup_bio_out(FORMAT_TEXT);
129     bio_err = dup_bio_err(FORMAT_TEXT);
130
131 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
132     copied_argv = argv = copy_argv(&argc, argv);
133 #elif defined(_WIN32)
134     /*
135      * Replace argv[] with UTF-8 encoded strings.
136      */
137     win32_utf8argv(&argc, &argv);
138 #endif
139
140     p = getenv("OPENSSL_DEBUG_MEMORY");
141     if (p != NULL && strcmp(p, "on") == 0)
142         CRYPTO_set_mem_debug(1);
143     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
144
145     if (getenv("OPENSSL_FIPS")) {
146 #ifdef OPENSSL_FIPS
147         if (!FIPS_mode_set(1)) {
148             ERR_print_errors(bio_err);
149             return 1;
150         }
151 #else
152         BIO_printf(bio_err, "FIPS mode not supported.\n");
153         return 1;
154 #endif
155     }
156
157     if (!apps_startup())
158         goto end;
159
160     prog = prog_init();
161     pname = opt_progname(argv[0]);
162
163     /* first check the program name */
164     f.name = pname;
165     fp = lh_FUNCTION_retrieve(prog, &f);
166     if (fp != NULL) {
167         argv[0] = pname;
168         ret = fp->func(argc, argv);
169         goto end;
170     }
171
172     /* If there is stuff on the command line, run with that. */
173     if (argc != 1) {
174         argc--;
175         argv++;
176         ret = do_cmd(prog, argc, argv);
177         if (ret < 0)
178             ret = 0;
179         goto end;
180     }
181
182     /* ok, lets enter interactive mode */
183     for (;;) {
184         ret = 0;
185         /* Read a line, continue reading if line ends with \ */
186         for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
187             prompt = first ? "OpenSSL> " : "> ";
188             p[0] = '\0';
189 #ifndef READLINE
190             fputs(prompt, stdout);
191             fflush(stdout);
192             if (!fgets(p, n, stdin))
193                 goto end;
194             if (p[0] == '\0')
195                 goto end;
196             i = strlen(p);
197             if (i <= 1)
198                 break;
199             if (p[i - 2] != '\\')
200                 break;
201             i -= 2;
202             p += i;
203             n -= i;
204 #else
205             {
206                 extern char *readline(const char *);
207                 extern void add_history(const char *cp);
208                 char *text;
209
210                 text = readline(prompt);
211                 if (text == NULL)
212                     goto end;
213                 i = strlen(text);
214                 if (i == 0 || i > n)
215                     break;
216                 if (text[i - 1] != '\\') {
217                     p += strlen(strcpy(p, text));
218                     free(text);
219                     add_history(buf);
220                     break;
221                 }
222
223                 text[i - 1] = '\0';
224                 p += strlen(strcpy(p, text));
225                 free(text);
226                 n -= i;
227             }
228 #endif
229         }
230
231         if (!chopup_args(&arg, buf)) {
232             BIO_printf(bio_err, "Can't parse (no memory?)\n");
233             break;
234         }
235
236         ret = do_cmd(prog, arg.argc, arg.argv);
237         if (ret == EXIT_THE_PROGRAM) {
238             ret = 0;
239             goto end;
240         }
241         if (ret != 0)
242             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
243         (void)BIO_flush(bio_out);
244         (void)BIO_flush(bio_err);
245     }
246     ret = 1;
247  end:
248     OPENSSL_free(copied_argv);
249     OPENSSL_free(default_config_file);
250     lh_FUNCTION_free(prog);
251     OPENSSL_free(arg.argv);
252
253     BIO_free(bio_in);
254     BIO_free_all(bio_out);
255     apps_shutdown();
256 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
257     if (CRYPTO_mem_leaks(bio_err) <= 0)
258         ret = 1;
259 #endif
260     BIO_free(bio_err);
261     EXIT(ret);
262 }
263
264 const OPTIONS exit_options[] = {
265     {NULL}
266 };
267
268 static void list_cipher_fn(const EVP_CIPHER *c,
269                            const char *from, const char *to, void *arg)
270 {
271     if (c)
272         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
273     else {
274         if (!from)
275             from = "<undefined>";
276         if (!to)
277             to = "<undefined>";
278         BIO_printf(arg, "%s => %s\n", from, to);
279     }
280 }
281
282 static void list_md_fn(const EVP_MD *m,
283                        const char *from, const char *to, void *arg)
284 {
285     if (m)
286         BIO_printf(arg, "%s\n", EVP_MD_name(m));
287     else {
288         if (!from)
289             from = "<undefined>";
290         if (!to)
291             to = "<undefined>";
292         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
293     }
294 }
295
296 static void list_missing_help(void)
297 {
298     const FUNCTION *fp;
299     const OPTIONS *o;
300
301     for (fp = functions; fp->name != NULL; fp++) {
302         if ((o = fp->help) == NULL) {
303             BIO_printf(bio_out, "%s *\n", fp->name);
304             continue;
305         }
306         for ( ; o->name != NULL; o++) {
307             if (o->helpstr == NULL)
308                 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
309         }
310     }
311 }
312
313
314 /* Unified enum for help and list commands. */
315 typedef enum HELPLIST_CHOICE {
316     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
317     OPT_COMMANDS, OPT_DIGEST_COMMANDS,
318     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
319     OPT_PK_ALGORITHMS, OPT_DISABLED, OPT_MISSING_HELP
320 } HELPLIST_CHOICE;
321
322 const OPTIONS list_options[] = {
323     {"help", OPT_HELP, '-', "Display this summary"},
324     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
325     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
326      "List of message digest commands"},
327     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
328      "List of message digest algorithms"},
329     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
330     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
331      "List of cipher algorithms"},
332     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
333      "List of public key algorithms"},
334     {"disabled", OPT_DISABLED, '-',
335      "List of disabled features"},
336     {"missing-help", OPT_MISSING_HELP, '-',
337      "List missing detailed help strings"},
338     {NULL}
339 };
340
341 int list_main(int argc, char **argv)
342 {
343     char *prog;
344     HELPLIST_CHOICE o;
345     int done = 0;
346
347     prog = opt_init(argc, argv, list_options);
348     while ((o = opt_next()) != OPT_EOF) {
349         switch (o) {
350         case OPT_EOF:  /* Never hit, but suppresses warning */
351         case OPT_ERR:
352             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
353             return 1;
354         case OPT_HELP:
355             opt_help(list_options);
356             break;
357         case OPT_COMMANDS:
358             list_type(FT_general);
359             break;
360         case OPT_DIGEST_COMMANDS:
361             list_type(FT_md);
362             break;
363         case OPT_DIGEST_ALGORITHMS:
364             EVP_MD_do_all_sorted(list_md_fn, bio_out);
365             break;
366         case OPT_CIPHER_COMMANDS:
367             list_type(FT_cipher);
368             break;
369         case OPT_CIPHER_ALGORITHMS:
370             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
371             break;
372         case OPT_PK_ALGORITHMS:
373             list_pkey();
374             break;
375         case OPT_DISABLED:
376             list_disabled();
377             break;
378         case OPT_MISSING_HELP:
379             list_missing_help();
380             break;
381         }
382         done = 1;
383     }
384
385     if (!done) {
386         BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
387         return 1;
388     }
389
390     return 0;
391 }
392
393 typedef enum HELP_CHOICE {
394     OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
395 } HELP_CHOICE;
396
397 const OPTIONS help_options[] = {
398     {"help", OPT_hHELP, '-', "Display this summary"},
399     {NULL}
400 };
401
402
403 int help_main(int argc, char **argv)
404 {
405     FUNCTION *fp;
406     int i, nl;
407     FUNC_TYPE tp;
408     char *prog;
409     HELP_CHOICE o;
410
411     prog = opt_init(argc, argv, help_options);
412     while ((o = opt_next()) != OPT_hEOF) {
413         switch (o) {
414         case OPT_hERR:
415         case OPT_hEOF:
416             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
417             return 1;
418         case OPT_hHELP:
419             opt_help(help_options);
420             return 0;
421         }
422     }
423
424     if (opt_num_rest() != 0) {
425         BIO_printf(bio_err, "Usage: %s\n", prog);
426         return 1;
427     }
428
429     BIO_printf(bio_err, "\nStandard commands");
430     i = 0;
431     tp = FT_none;
432     for (fp = functions; fp->name != NULL; fp++) {
433         nl = 0;
434         if (((i++) % COLUMNS) == 0) {
435             BIO_printf(bio_err, "\n");
436             nl = 1;
437         }
438         if (fp->type != tp) {
439             tp = fp->type;
440             if (!nl)
441                 BIO_printf(bio_err, "\n");
442             if (tp == FT_md) {
443                 i = 1;
444                 BIO_printf(bio_err,
445                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
446             } else if (tp == FT_cipher) {
447                 i = 1;
448                 BIO_printf(bio_err,
449                            "\nCipher commands (see the `enc' command for more details)\n");
450             }
451         }
452         BIO_printf(bio_err, FORMAT, fp->name);
453     }
454     BIO_printf(bio_err, "\n\n");
455     return 0;
456 }
457
458 int exit_main(int argc, char **argv)
459 {
460     return EXIT_THE_PROGRAM;
461 }
462
463 static void list_type(FUNC_TYPE ft)
464 {
465     FUNCTION *fp;
466     int i = 0;
467
468     for (fp = functions; fp->name != NULL; fp++)
469         if (fp->type == ft) {
470             if ((i++ % COLUMNS) == 0)
471                 BIO_printf(bio_out, "\n");
472             BIO_printf(bio_out, FORMAT, fp->name);
473         }
474     BIO_printf(bio_out, "\n");
475 }
476
477 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
478 {
479     FUNCTION f, *fp;
480
481     if (argc <= 0 || argv[0] == NULL)
482         return (0);
483     f.name = argv[0];
484     fp = lh_FUNCTION_retrieve(prog, &f);
485     if (fp == NULL) {
486         if (EVP_get_digestbyname(argv[0])) {
487             f.type = FT_md;
488             f.func = dgst_main;
489             fp = &f;
490         } else if (EVP_get_cipherbyname(argv[0])) {
491             f.type = FT_cipher;
492             f.func = enc_main;
493             fp = &f;
494         }
495     }
496     if (fp != NULL) {
497         return (fp->func(argc, argv));
498     }
499     if ((strncmp(argv[0], "no-", 3)) == 0) {
500         /*
501          * User is asking if foo is unsupported, by trying to "run" the
502          * no-foo command.  Strange.
503          */
504         f.name = argv[0] + 3;
505         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
506             BIO_printf(bio_out, "%s\n", argv[0]);
507             return (0);
508         }
509         BIO_printf(bio_out, "%s\n", argv[0] + 3);
510         return 1;
511     }
512     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
513         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
514         /* Special value to mean "exit the program. */
515         return EXIT_THE_PROGRAM;
516
517     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
518                argv[0]);
519     return (1);
520 }
521
522 static void list_pkey(void)
523 {
524     int i;
525
526     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
527         const EVP_PKEY_ASN1_METHOD *ameth;
528         int pkey_id, pkey_base_id, pkey_flags;
529         const char *pinfo, *pem_str;
530         ameth = EVP_PKEY_asn1_get0(i);
531         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
532                                 &pinfo, &pem_str, ameth);
533         if (pkey_flags & ASN1_PKEY_ALIAS) {
534             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
535             BIO_printf(bio_out, "\tAlias for: %s\n",
536                        OBJ_nid2ln(pkey_base_id));
537         } else {
538             BIO_printf(bio_out, "Name: %s\n", pinfo);
539             BIO_printf(bio_out, "\tType: %s Algorithm\n",
540                        pkey_flags & ASN1_PKEY_DYNAMIC ?
541                        "External" : "Builtin");
542             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
543             if (pem_str == NULL)
544                 pem_str = "(none)";
545             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
546         }
547
548     }
549 }
550
551 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
552 {
553     return strncmp(a->name, b->name, 8);
554 }
555
556 static unsigned long function_hash(const FUNCTION * a)
557 {
558     return OPENSSL_LH_strhash(a->name);
559 }
560
561 static int SortFnByName(const void *_f1, const void *_f2)
562 {
563     const FUNCTION *f1 = _f1;
564     const FUNCTION *f2 = _f2;
565
566     if (f1->type != f2->type)
567         return f1->type - f2->type;
568     return strcmp(f1->name, f2->name);
569 }
570
571 static void list_disabled(void)
572 {
573     BIO_puts(bio_out, "Disabled algorithms:\n");
574 #ifdef OPENSSL_NO_BF
575     BIO_puts(bio_out, "BF\n");
576 #endif
577 #ifdef OPENSSL_NO_BLAKE2
578     BIO_puts(bio_out, "BLAKE2\n");
579 #endif
580 #ifdef OPENSSL_NO_CAMELLIA
581     BIO_puts(bio_out, "CAMELLIA\n");
582 #endif
583 #ifdef OPENSSL_NO_CAST
584     BIO_puts(bio_out, "CAST\n");
585 #endif
586 #ifdef OPENSSL_NO_CMAC
587     BIO_puts(bio_out, "CMAC\n");
588 #endif
589 #ifdef OPENSSL_NO_CMS
590     BIO_puts(bio_out, "CMS\n");
591 #endif
592 #ifdef OPENSSL_NO_COMP
593     BIO_puts(bio_out, "COMP\n");
594 #endif
595 #ifdef OPENSSL_NO_DES
596     BIO_puts(bio_out, "DES\n");
597 #endif
598 #ifdef OPENSSL_NO_DGRAM
599     BIO_puts(bio_out, "DGRAM\n");
600 #endif
601 #ifdef OPENSSL_NO_DH
602     BIO_puts(bio_out, "DH\n");
603 #endif
604 #ifdef OPENSSL_NO_DSA
605     BIO_puts(bio_out, "DSA\n");
606 #endif
607 #if defined(OPENSSL_NO_DTLS)
608     BIO_puts(bio_out, "DTLS\n");
609 #endif
610 #if defined(OPENSSL_NO_DTLS1)
611     BIO_puts(bio_out, "DTLS1\n");
612 #endif
613 #if defined(OPENSSL_NO_DTLS1_2)
614     BIO_puts(bio_out, "DTLS1_2\n");
615 #endif
616 #ifdef OPENSSL_NO_EC
617     BIO_puts(bio_out, "EC\n");
618 #endif
619 #ifdef OPENSSL_NO_EC2M
620     BIO_puts(bio_out, "EC2M\n");
621 #endif
622 #ifdef OPENSSL_NO_ENGINE
623     BIO_puts(bio_out, "ENGINE\n");
624 #endif
625 #ifdef OPENSSL_NO_GOST
626     BIO_puts(bio_out, "GOST\n");
627 #endif
628 #ifdef OPENSSL_NO_HEARTBEATS
629     BIO_puts(bio_out, "HEARTBEATS\n");
630 #endif
631 #ifdef OPENSSL_NO_IDEA
632     BIO_puts(bio_out, "IDEA\n");
633 #endif
634 #ifdef OPENSSL_NO_MD2
635     BIO_puts(bio_out, "MD2\n");
636 #endif
637 #ifdef OPENSSL_NO_MD4
638     BIO_puts(bio_out, "MD4\n");
639 #endif
640 #ifdef OPENSSL_NO_MD5
641     BIO_puts(bio_out, "MD5\n");
642 #endif
643 #ifdef OPENSSL_NO_MDC2
644     BIO_puts(bio_out, "MDC2\n");
645 #endif
646 #ifdef OPENSSL_NO_OCB
647     BIO_puts(bio_out, "OCB\n");
648 #endif
649 #ifdef OPENSSL_NO_OCSP
650     BIO_puts(bio_out, "OCSP\n");
651 #endif
652 #ifdef OPENSSL_NO_PSK
653     BIO_puts(bio_out, "PSK\n");
654 #endif
655 #ifdef OPENSSL_NO_RC2
656     BIO_puts(bio_out, "RC2\n");
657 #endif
658 #ifdef OPENSSL_NO_RC4
659     BIO_puts(bio_out, "RC4\n");
660 #endif
661 #ifdef OPENSSL_NO_RC5
662     BIO_puts(bio_out, "RC5\n");
663 #endif
664 #ifdef OPENSSL_NO_RMD160
665     BIO_puts(bio_out, "RMD160\n");
666 #endif
667 #ifdef OPENSSL_NO_RSA
668     BIO_puts(bio_out, "RSA\n");
669 #endif
670 #ifdef OPENSSL_NO_SCRYPT
671     BIO_puts(bio_out, "SCRYPT\n");
672 #endif
673 #ifdef OPENSSL_NO_SCTP
674     BIO_puts(bio_out, "SCTP\n");
675 #endif
676 #ifdef OPENSSL_NO_SEED
677     BIO_puts(bio_out, "SEED\n");
678 #endif
679 #ifdef OPENSSL_NO_SOCK
680     BIO_puts(bio_out, "SOCK\n");
681 #endif
682 #ifdef OPENSSL_NO_SRP
683     BIO_puts(bio_out, "SRP\n");
684 #endif
685 #ifdef OPENSSL_NO_SRTP
686     BIO_puts(bio_out, "SRTP\n");
687 #endif
688 #ifdef OPENSSL_NO_SSL3
689     BIO_puts(bio_out, "SSL3\n");
690 #endif
691 #ifdef OPENSSL_NO_TLS1
692     BIO_puts(bio_out, "TLS1\n");
693 #endif
694 #ifdef OPENSSL_NO_TLS1_1
695     BIO_puts(bio_out, "TLS1_1\n");
696 #endif
697 #ifdef OPENSSL_NO_TLS1_2
698     BIO_puts(bio_out, "TLS1_2\n");
699 #endif
700 #ifdef OPENSSL_NO_WHIRLPOOL
701     BIO_puts(bio_out, "WHIRLPOOL\n");
702 #endif
703 #ifndef ZLIB
704     BIO_puts(bio_out, "ZLIB\n");
705 #endif
706 }
707
708 static LHASH_OF(FUNCTION) *prog_init(void)
709 {
710     LHASH_OF(FUNCTION) *ret;
711     FUNCTION *f;
712     size_t i;
713
714     /* Sort alphabetically within category. For nicer help displays. */
715     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
716     qsort(functions, i, sizeof(*functions), SortFnByName);
717
718     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
719         return (NULL);
720
721     for (f = functions; f->name != NULL; f++)
722         (void)lh_FUNCTION_insert(ret, f);
723     return (ret);
724 }