home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / intl / bindtextdom.c next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  118 lines

  1. /* bindtextdom.c -- implementation of the bindtextdomain(3) function
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21.  
  22. #include <libgettext.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include "gettext.h"
  27. #include "gettextP.h"
  28.  
  29. /* @@ end of prolog @@ */
  30.  
  31. /* Contains the default location of the message catalogs.  */
  32. extern const char _nl_default_dirname[];
  33.  
  34. /* List with bindings of specific domains.  */
  35. extern struct binding *_nl_domain_bindings;
  36.  
  37. /* Prototypes for library functions.  */
  38. void *xmalloc ();
  39. char *xstrdup ();
  40.  
  41.  
  42. /* Specify that the DOMAINNAME message catalog will be found
  43.    in DIRNAME rather than in the system locale data base.  */
  44. char *
  45. bindtextdomain (domainname, dirname)
  46.      const char *domainname;
  47.      const char *dirname;
  48. {
  49.   struct binding *binding;
  50.  
  51.   /* Some sanity checks.  */
  52.   if (domainname == NULL || domainname[0] == '\0')
  53.     return NULL;
  54.  
  55.   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  56.     {
  57.       int compare = strcmp (domainname, binding->domainname);
  58.       if (compare == 0)
  59.     /* We found it!  */
  60.     break;
  61.       if (compare < 0)
  62.     {
  63.       /* It is not in the list.  */
  64.       binding = NULL;
  65.       break;
  66.     }
  67.     }
  68.  
  69.   if (dirname == NULL)
  70.     /* The current binding has be to returned.  */
  71.     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
  72.  
  73.   if (binding != NULL)
  74.     {
  75.       /* The domain is already bound.  Replace the old binding.  */
  76.       char *new_dirname = strcmp (dirname, _nl_default_dirname) == 0
  77.               ? (char *) _nl_default_dirname : xstrdup (dirname);
  78.  
  79.       if (strcmp (binding->dirname, _nl_default_dirname) != 0)
  80.         free (binding->dirname);
  81.  
  82.       binding->dirname = new_dirname;
  83.     }
  84.   else
  85.     {
  86.       /* We have to create a new binding.  */
  87.       struct binding *new_binding =
  88.     (struct binding *) xmalloc (sizeof (*new_binding));
  89.  
  90.  
  91.       new_binding->domainname = xstrdup (domainname);
  92.       new_binding->dirname = strcmp (dirname, _nl_default_dirname) == 0
  93.                  ? (char *) _nl_default_dirname : xstrdup (dirname);
  94.  
  95.       /* Now enqueue it.  */
  96.       if (_nl_domain_bindings == NULL
  97.       || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
  98.     {
  99.       new_binding->next = _nl_domain_bindings;
  100.       _nl_domain_bindings = new_binding;
  101.     }
  102.       else
  103.     {
  104.       binding = _nl_domain_bindings;
  105.       while (binding->next != NULL
  106.          && strcmp (domainname, binding->next->domainname) > 0)
  107.         binding = binding->next;
  108.  
  109.       new_binding->next = binding->next;
  110.       binding->next = new_binding;
  111.     }
  112.  
  113.       binding = new_binding;
  114.     }
  115.  
  116.   return binding->dirname;
  117. }
  118.