Back to index

glibc  2.9
getent.c
Go to the documentation of this file.
00001 /* Copyright (c) 1998-2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /* getent: get entries from administrative database.  */
00021 
00022 #include <aliases.h>
00023 #include <argp.h>
00024 #include <ctype.h>
00025 #include <error.h>
00026 #include <grp.h>
00027 #include <libintl.h>
00028 #include <locale.h>
00029 #include <mcheck.h>
00030 #include <netdb.h>
00031 #include <pwd.h>
00032 #include <shadow.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <arpa/inet.h>
00037 #include <arpa/nameser.h>
00038 #include <netinet/ether.h>
00039 #include <netinet/in.h>
00040 #include <sys/socket.h>
00041 
00042 /* Get libc version number.  */
00043 #include <version.h>
00044 
00045 #define PACKAGE _libc_intl_domainname
00046 
00047 /* Name and version of program.  */
00048 static void print_version (FILE *stream, struct argp_state *state);
00049 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
00050 
00051 /* Short description of parameters.  */
00052 static const char args_doc[] = N_("database [key ...]");
00053 
00054 /* Supported options. */
00055 static const struct argp_option args_options[] =
00056   {
00057     { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
00058     { NULL, 0, NULL, 0, NULL },
00059   };
00060 
00061 /* Short description of program.  */
00062 static const char doc[] = N_("Get entries from administrative database.\v\
00063 For bug reporting instructions, please see:\n\
00064 <http://www.gnu.org/software/libc/bugs.html>.\n");
00065 
00066 /* Prototype for option handler.  */
00067 static error_t parse_option (int key, char *arg, struct argp_state *state);
00068 
00069 /* Function to print some extra text in the help message.  */
00070 static char *more_help (int key, const char *text, void *input);
00071 
00072 /* Data structure to communicate with argp functions.  */
00073 static struct argp argp =
00074   {
00075     args_options, parse_option, args_doc, doc, NULL, more_help
00076   };
00077 
00078 /* Print the version information.  */
00079 static void
00080 print_version (FILE *stream, struct argp_state *state)
00081 {
00082   fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
00083   fprintf (stream, gettext ("\
00084 Copyright (C) %s Free Software Foundation, Inc.\n\
00085 This is free software; see the source for copying conditions.  There is NO\n\
00086 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
00087 "), "2008");
00088   fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
00089 }
00090 
00091 /* This is for aliases */
00092 static inline void
00093 print_aliases (struct aliasent *alias)
00094 {
00095   unsigned int i = 0;
00096 
00097   printf ("%s: ", alias->alias_name);
00098   for  (i = strlen (alias->alias_name); i < 14; ++i)
00099     fputs_unlocked (" ", stdout);
00100 
00101   for (i = 0; i < alias->alias_members_len; ++i)
00102     printf ("%s%s",
00103            alias->alias_members [i],
00104            i + 1 == alias->alias_members_len ? "\n" : ", ");
00105 }
00106 
00107 static int
00108 aliases_keys (int number, char *key[])
00109 {
00110   int result = 0;
00111   int i;
00112   struct aliasent *alias;
00113 
00114   if (number == 0)
00115     {
00116       setaliasent ();
00117       while ((alias = getaliasent ()) != NULL)
00118        print_aliases (alias);
00119       endaliasent ();
00120       return result;
00121     }
00122 
00123   for (i = 0; i < number; ++i)
00124     {
00125       alias = getaliasbyname (key[i]);
00126 
00127       if (alias == NULL)
00128        result = 2;
00129       else
00130        print_aliases (alias);
00131     }
00132 
00133   return result;
00134 }
00135 
00136 /* This is for ethers */
00137 static int
00138 ethers_keys (int number, char *key[])
00139 {
00140   int result = 0;
00141   int i;
00142 
00143   if (number == 0)
00144     {
00145       fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
00146       return 3;
00147     }
00148 
00149   for (i = 0; i < number; ++i)
00150     {
00151       struct ether_addr *ethp, eth;
00152       char buffer [1024], *p;
00153 
00154       ethp = ether_aton (key[i]);
00155       if (ethp != NULL)
00156        {
00157          if (ether_ntohost (buffer, ethp))
00158            {
00159              result = 2;
00160              continue;
00161            }
00162          p = buffer;
00163        }
00164       else
00165        {
00166          if (ether_hostton (key[i], &eth))
00167            {
00168              result = 2;
00169              continue;
00170            }
00171          p = key[i];
00172          ethp = &eth;
00173        }
00174       printf ("%s %s\n", ether_ntoa (ethp), p);
00175     }
00176 
00177   return result;
00178 }
00179 
00180 /* This is for group */
00181 static inline void
00182 print_group (struct group *grp)
00183 {
00184   unsigned int i = 0;
00185 
00186   printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
00187          grp->gr_passwd ? grp->gr_passwd : "",
00188          (unsigned long int) grp->gr_gid);
00189 
00190   while (grp->gr_mem[i] != NULL)
00191     {
00192       fputs_unlocked (grp->gr_mem[i], stdout);
00193       ++i;
00194       if (grp->gr_mem[i] != NULL)
00195        putchar_unlocked (',');
00196     }
00197   putchar_unlocked ('\n');
00198 }
00199 
00200 static int
00201 group_keys (int number, char *key[])
00202 {
00203   int result = 0;
00204   int i;
00205   struct group *grp;
00206 
00207   if (number == 0)
00208     {
00209       setgrent ();
00210       while ((grp = getgrent ()) != NULL)
00211        print_group (grp);
00212       endgrent ();
00213       return result;
00214     }
00215 
00216   for (i = 0; i < number; ++i)
00217     {
00218       errno = 0;
00219       char *ep;
00220       gid_t arg_gid = strtoul(key[i], &ep, 10);
00221 
00222       if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
00223        /* Valid numeric gid.  */
00224        grp = getgrgid (arg_gid);
00225       else
00226        grp = getgrnam (key[i]);
00227 
00228       if (grp == NULL)
00229        result = 2;
00230       else
00231        print_group (grp);
00232     }
00233 
00234   return result;
00235 }
00236 
00237 /* This is for hosts */
00238 static void
00239 print_hosts (struct hostent *host)
00240 {
00241   unsigned int cnt;
00242 
00243   for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
00244     {
00245       char buf[INET6_ADDRSTRLEN];
00246       const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
00247                               buf, sizeof (buf));
00248 
00249       printf ("%-15s %s", ip, host->h_name);
00250 
00251       unsigned int i;
00252       for (i = 0; host->h_aliases[i] != NULL; ++i)
00253        {
00254          putchar_unlocked (' ');
00255          fputs_unlocked (host->h_aliases[i], stdout);
00256        }
00257       putchar_unlocked ('\n');
00258     }
00259 }
00260 
00261 static int
00262 hosts_keys (int number, char *key[])
00263 {
00264   int result = 0;
00265   int i;
00266   struct hostent *host;
00267 
00268   if (number == 0)
00269     {
00270       sethostent (0);
00271       while ((host = gethostent ()) != NULL)
00272        print_hosts (host);
00273       endhostent ();
00274       return result;
00275     }
00276 
00277   for (i = 0; i < number; ++i)
00278     {
00279       struct hostent *host = NULL;
00280       char addr[IN6ADDRSZ];
00281 
00282       if (inet_pton (AF_INET6, key[i], &addr) > 0)
00283        host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
00284       else if (inet_pton (AF_INET, key[i], &addr) > 0)
00285        host = gethostbyaddr (addr, INADDRSZ, AF_INET);
00286       else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
00287        host = gethostbyname2 (key[i], AF_INET);
00288 
00289       if (host == NULL)
00290        result = 2;
00291       else
00292        print_hosts (host);
00293     }
00294 
00295   return result;
00296 }
00297 
00298 /* This is for hosts, but using getaddrinfo */
00299 static int
00300 ahosts_keys_int (int af, int xflags, int number, char *key[])
00301 {
00302   int result = 0;
00303   int i;
00304   struct hostent *host;
00305 
00306   if (number == 0)
00307     {
00308       sethostent (0);
00309       while ((host = gethostent ()) != NULL)
00310        print_hosts (host);
00311       endhostent ();
00312       return result;
00313     }
00314 
00315   struct addrinfo hint;
00316   memset (&hint, '\0', sizeof (hint));
00317   hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
00318   hint.ai_family = af;
00319 
00320   for (i = 0; i < number; ++i)
00321     {
00322       struct addrinfo *res;
00323 
00324       if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
00325        result = 2;
00326       else
00327        {
00328          struct addrinfo *runp = res;
00329 
00330          while (runp != NULL)
00331            {
00332              char sockbuf[20];
00333              const char *sockstr;
00334              if (runp->ai_socktype == SOCK_STREAM)
00335               sockstr = "STREAM";
00336              else if (runp->ai_socktype == SOCK_DGRAM)
00337               sockstr = "DGRAM";
00338              else if (runp->ai_socktype == SOCK_RAW)
00339               sockstr = "RAW";
00340 #ifdef SOCK_SEQPACKET
00341              else if (runp->ai_socktype == SOCK_SEQPACKET)
00342               sockstr = "SEQPACKET";
00343 #endif
00344 #ifdef SOCK_RDM
00345              else if (runp->ai_socktype == SOCK_RDM)
00346               sockstr = "RDM";
00347 #endif
00348 #ifdef SOCK_DCCP
00349              else if (runp->ai_socktype == SOCK_DCCP)
00350               sockstr = "DCCP";
00351 #endif
00352 #ifdef SOCK_PACKET
00353              else if (runp->ai_socktype == SOCK_PACKET)
00354               sockstr = "PACKET";
00355 #endif
00356              else
00357               {
00358                 snprintf (sockbuf, sizeof (sockbuf), "%d",
00359                          runp->ai_socktype);
00360                 sockstr = sockbuf;
00361               }
00362 
00363              char buf[INET6_ADDRSTRLEN];
00364              printf ("%-15s %-6s %s\n",
00365                     inet_ntop (runp->ai_family,
00366                              runp->ai_family == AF_INET
00367                              ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
00368                              : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
00369                              buf, sizeof (buf)),
00370                     sockstr,
00371                     runp->ai_canonname ?: "");
00372 
00373              runp = runp->ai_next;
00374            }
00375 
00376          freeaddrinfo (res);
00377        }
00378     }
00379 
00380   return result;
00381 }
00382 
00383 static int
00384 ahosts_keys (int number, char *key[])
00385 {
00386   return ahosts_keys_int (AF_UNSPEC, 0, number, key);
00387 }
00388 
00389 static int
00390 ahostsv4_keys (int number, char *key[])
00391 {
00392   return ahosts_keys_int (AF_INET, 0, number, key);
00393 }
00394 
00395 static int
00396 ahostsv6_keys (int number, char *key[])
00397 {
00398   return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
00399 }
00400 
00401 /* This is for netgroup */
00402 static int
00403 netgroup_keys (int number, char *key[])
00404 {
00405   int result = 0;
00406   int i;
00407 
00408   if (number == 0)
00409     {
00410       fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
00411       return 3;
00412     }
00413 
00414   for (i = 0; i < number; ++i)
00415     {
00416       if (!setnetgrent (key[i]))
00417        result = 2;
00418       else
00419        {
00420          char *p[3];
00421 
00422          printf ("%-21s", key[i]);
00423 
00424          while (getnetgrent (p, p + 1, p + 2))
00425            printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
00426          putchar_unlocked ('\n');
00427        }
00428     }
00429 
00430   endnetgrent ();
00431 
00432   return result;
00433 }
00434 
00435 /* This is for networks */
00436 static void
00437 print_networks (struct netent *net)
00438 {
00439   unsigned int i;
00440   struct in_addr ip;
00441   ip.s_addr = htonl (net->n_net);
00442 
00443   printf ("%-21s %s", net->n_name, inet_ntoa (ip));
00444 
00445   i = 0;
00446   while (net->n_aliases[i] != NULL)
00447     {
00448       putchar_unlocked (' ');
00449       fputs_unlocked (net->n_aliases[i], stdout);
00450       ++i;
00451       if (net->n_aliases[i] != NULL)
00452        putchar_unlocked (',');
00453     }
00454   putchar_unlocked ('\n');
00455 }
00456 
00457 static int
00458 networks_keys (int number, char *key[])
00459 {
00460   int result = 0;
00461   int i;
00462   struct netent *net;
00463 
00464   if (number == 0)
00465     {
00466       setnetent (0);
00467       while ((net = getnetent ()) != NULL)
00468        print_networks (net);
00469       endnetent ();
00470       return result;
00471     }
00472 
00473   for (i = 0; i < number; ++i)
00474     {
00475       if (isdigit (key[i][0]))
00476        net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
00477       else
00478        net = getnetbyname (key[i]);
00479 
00480       if (net == NULL)
00481        result = 2;
00482       else
00483        print_networks (net);
00484     }
00485 
00486   return result;
00487 }
00488 
00489 /* Now is all for passwd */
00490 static inline void
00491 print_passwd (struct passwd *pwd)
00492 {
00493   printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
00494          pwd->pw_name ? pwd->pw_name : "",
00495          pwd->pw_passwd ? pwd->pw_passwd : "",
00496          (unsigned long int) pwd->pw_uid,
00497          (unsigned long int) pwd->pw_gid,
00498          pwd->pw_gecos ? pwd->pw_gecos : "",
00499          pwd->pw_dir ? pwd->pw_dir : "",
00500          pwd->pw_shell ? pwd->pw_shell : "");
00501 }
00502 
00503 static int
00504 passwd_keys (int number, char *key[])
00505 {
00506   int result = 0;
00507   int i;
00508   struct passwd *pwd;
00509 
00510   if (number == 0)
00511     {
00512       setpwent ();
00513       while ((pwd = getpwent ()) != NULL)
00514        print_passwd (pwd);
00515       endpwent ();
00516       return result;
00517     }
00518 
00519   for (i = 0; i < number; ++i)
00520     {
00521       errno = 0;
00522       char *ep;
00523       uid_t arg_uid = strtoul(key[i], &ep, 10);
00524 
00525       if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
00526        /* Valid numeric uid.  */
00527        pwd = getpwuid (arg_uid);
00528       else
00529        pwd = getpwnam (key[i]);
00530 
00531       if (pwd == NULL)
00532        result = 2;
00533       else
00534        print_passwd (pwd);
00535     }
00536 
00537   return result;
00538 }
00539 
00540 /* This is for protocols */
00541 static inline void
00542 print_protocols (struct protoent *proto)
00543 {
00544   unsigned int i;
00545 
00546   printf ("%-21s %d", proto->p_name, proto->p_proto);
00547 
00548   i = 0;
00549   while (proto->p_aliases[i] != NULL)
00550     {
00551       putchar_unlocked (' ');
00552       fputs_unlocked (proto->p_aliases[i], stdout);
00553       ++i;
00554     }
00555   putchar_unlocked ('\n');
00556 }
00557 
00558 static int
00559 protocols_keys (int number, char *key[])
00560 {
00561   int result = 0;
00562   int i;
00563   struct protoent *proto;
00564 
00565   if (number == 0)
00566     {
00567       setprotoent (0);
00568       while ((proto = getprotoent ()) != NULL)
00569        print_protocols (proto);
00570       endprotoent ();
00571       return result;
00572     }
00573 
00574   for (i = 0; i < number; ++i)
00575     {
00576       if (isdigit (key[i][0]))
00577        proto = getprotobynumber (atol (key[i]));
00578       else
00579        proto = getprotobyname (key[i]);
00580 
00581       if (proto == NULL)
00582        result = 2;
00583       else
00584        print_protocols (proto);
00585     }
00586 
00587   return result;
00588 }
00589 
00590 /* Now is all for rpc */
00591 static inline void
00592 print_rpc (struct rpcent *rpc)
00593 {
00594   int i;
00595 
00596   printf ("%-15s %d%s",
00597          rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
00598 
00599   for (i = 0; rpc->r_aliases[i]; ++i)
00600     printf (" %s", rpc->r_aliases[i]);
00601   putchar_unlocked ('\n');
00602 }
00603 
00604 static int
00605 rpc_keys (int number, char *key[])
00606 {
00607   int result = 0;
00608   int i;
00609   struct rpcent *rpc;
00610 
00611   if (number == 0)
00612     {
00613       setrpcent (0);
00614       while ((rpc = getrpcent ()) != NULL)
00615        print_rpc (rpc);
00616       endrpcent ();
00617       return result;
00618     }
00619 
00620   for (i = 0; i < number; ++i)
00621     {
00622       if (isdigit (key[i][0]))
00623        rpc = getrpcbynumber (atol (key[i]));
00624       else
00625        rpc = getrpcbyname (key[i]);
00626 
00627       if (rpc == NULL)
00628        result = 2;
00629       else
00630        print_rpc (rpc);
00631     }
00632 
00633   return result;
00634 }
00635 
00636 /* for services */
00637 static void
00638 print_services (struct servent *serv)
00639 {
00640   unsigned int i;
00641 
00642   printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
00643 
00644   i = 0;
00645   while (serv->s_aliases[i] != NULL)
00646     {
00647       putchar_unlocked (' ');
00648       fputs_unlocked (serv->s_aliases[i], stdout);
00649       ++i;
00650     }
00651   putchar_unlocked ('\n');
00652 }
00653 
00654 static int
00655 services_keys (int number, char *key[])
00656 {
00657   int result = 0;
00658   int i;
00659   struct servent *serv;
00660 
00661   if (!number)
00662     {
00663       setservent (0);
00664       while ((serv = getservent ()) != NULL)
00665        print_services (serv);
00666       endservent ();
00667       return result;
00668     }
00669 
00670   for (i = 0; i < number; ++i)
00671     {
00672       struct servent *serv;
00673       char *proto = strchr (key[i], '/');
00674 
00675       if (proto != NULL)
00676        *proto++ = '\0';
00677 
00678       if (isdigit (key[i][0]))
00679        serv = getservbyport (htons (atol (key[i])), proto);
00680       else
00681        serv = getservbyname (key[i], proto);
00682 
00683       if (serv == NULL)
00684        result = 2;
00685       else
00686        print_services (serv);
00687     }
00688 
00689   return result;
00690 }
00691 
00692 /* This is for shadow */
00693 static void
00694 print_shadow (struct spwd *sp)
00695 {
00696   printf ("%s:%s:",
00697          sp->sp_namp ? sp->sp_namp : "",
00698          sp->sp_pwdp ? sp->sp_pwdp : "");
00699 
00700 #define SHADOW_FIELD(n) \
00701   if (sp->n == -1)                                                   \
00702     putchar_unlocked (':');                                          \
00703   else                                                               \
00704     printf ("%ld:", sp->n)
00705 
00706   SHADOW_FIELD (sp_lstchg);
00707   SHADOW_FIELD (sp_min);
00708   SHADOW_FIELD (sp_max);
00709   SHADOW_FIELD (sp_warn);
00710   SHADOW_FIELD (sp_inact);
00711   SHADOW_FIELD (sp_expire);
00712   if (sp->sp_flag == ~0ul)
00713     putchar_unlocked ('\n');
00714   else
00715     printf ("%lu\n", sp->sp_flag);
00716 }
00717 
00718 static int
00719 shadow_keys (int number, char *key[])
00720 {
00721   int result = 0;
00722   int i;
00723 
00724   if (number == 0)
00725     {
00726       struct spwd *sp;
00727 
00728       setspent ();
00729       while ((sp = getspent ()) != NULL)
00730        print_shadow (sp);
00731       endpwent ();
00732       return result;
00733     }
00734 
00735   for (i = 0; i < number; ++i)
00736     {
00737       struct spwd *sp;
00738 
00739       sp = getspnam (key[i]);
00740 
00741       if (sp == NULL)
00742        result = 2;
00743       else
00744        print_shadow (sp);
00745     }
00746 
00747   return result;
00748 }
00749 
00750 struct
00751   {
00752     const char *name;
00753     int (*func) (int number, char *key[]);
00754   } databases[] =
00755   {
00756 #define D(name) { #name, name ## _keys },
00757 D(ahosts)
00758 D(ahostsv4)
00759 D(ahostsv6)
00760 D(aliases)
00761 D(ethers)
00762 D(group)
00763 D(hosts)
00764 D(netgroup)
00765 D(networks)
00766 D(passwd)
00767 D(protocols)
00768 D(rpc)
00769 D(services)
00770 D(shadow)
00771 #undef D
00772     { NULL, NULL }
00773   };
00774 
00775 /* Handle arguments found by argp. */
00776 static error_t
00777 parse_option (int key, char *arg, struct argp_state *state)
00778 {
00779   char *endp;
00780   switch (key)
00781     {
00782     case 's':
00783       endp = strchr (arg, ':');
00784       if (endp == NULL)
00785        /* No specific database, change them all.  */
00786        for (int i = 0; databases[i].name != NULL; ++i)
00787          __nss_configure_lookup (databases[i].name, arg);
00788       else
00789        {
00790          int i;
00791          for (i = 0; databases[i].name != NULL; ++i)
00792            if (strncmp (databases[i].name, arg, endp - arg) == 0)
00793              {
00794               __nss_configure_lookup (databases[i].name, endp + 1);
00795               break;
00796              }
00797          if (databases[i].name == NULL)
00798            error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
00799        }
00800       break;
00801 
00802     default:
00803       return ARGP_ERR_UNKNOWN;
00804     }
00805 
00806   return 0;
00807 }
00808 
00809 
00810 static char *
00811 more_help (int key, const char *text, void *input)
00812 {
00813   switch (key)
00814     {
00815       size_t len;
00816       char *doc;
00817       FILE *fp;
00818 
00819     case ARGP_KEY_HELP_EXTRA:
00820       /* We print some extra information.  */
00821       fp = open_memstream (&doc, &len);
00822       if (fp != NULL)
00823        {
00824          fputs_unlocked (_("Supported databases:\n"), fp);
00825 
00826          for (int i = 0, col = 0; databases[i].name != NULL; ++i)
00827            {
00828              len = strlen (databases[i].name);
00829              if (i != 0)
00830               {
00831                 if (col + len > 72)
00832                   {
00833                     col = 0;
00834                     fputc_unlocked ('\n', fp);
00835                   }
00836                 else
00837                   fputc_unlocked (' ', fp);
00838               }
00839 
00840              fputs_unlocked (databases[i].name, fp);
00841              col += len + 1;
00842            }
00843 
00844          if (fclose (fp) == 0)
00845            return doc;
00846        }
00847       break;
00848 
00849     default:
00850       break;
00851     }
00852   return (char *) text;
00853 }
00854 
00855 
00856 /* the main function */
00857 int
00858 main (int argc, char *argv[])
00859 {
00860   /* Debugging support.  */
00861   mtrace ();
00862 
00863   /* Set locale via LC_ALL.  */
00864   setlocale (LC_ALL, "");
00865   /* Set the text message domain.  */
00866   textdomain (PACKAGE);
00867 
00868   /* Parse and process arguments.  */
00869   int remaining;
00870   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
00871 
00872   if ((argc - remaining) < 1)
00873     {
00874       error (0, 0, gettext ("wrong number of arguments"));
00875       argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
00876       return 1;
00877     }
00878 
00879   for (int i = 0; databases[i].name; ++i)
00880     if (argv[remaining][0] == databases[i].name[0]
00881        && !strcmp (argv[remaining], databases[i].name))
00882       return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
00883 
00884   fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
00885   argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
00886   return 1;
00887 }