print-copyr.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* copysym.c -- Return a copyright symbol suitable for the current locale.
  2. Copyright (C) 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Paul Eggert. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stddef.h>
  19. #if HAVE_ICONV
  20. # include <iconv.h>
  21. # if ! USE_INCLUDED_LIBINTL && HAVE_LANGINFO_CODESET
  22. # include <langinfo.h>
  23. # endif
  24. # if HAVE_STDLIB_H
  25. # include <stdlib.h>
  26. # endif
  27. #endif
  28. #include "copysym.h"
  29. /* Store into BUF (of size BUFSIZE) a representation of the copyright
  30. symbol (C-in-a-circle) that is a valid text string for the current
  31. locale. Return BUF if successful, and a pointer to some other
  32. string otherwise. */
  33. char const *
  34. copyright_symbol (char *buf, size_t bufsize)
  35. {
  36. #if HAVE_ICONV
  37. char const *outcharset = getenv ("OUTPUT_CHARSET");
  38. if (! (outcharset && *outcharset))
  39. {
  40. #if USE_INCLUDED_LIBINTL
  41. extern char const *locale_charset (void);
  42. outcharset = locale_charset ();
  43. #else
  44. # if HAVE_LANGINFO_CODESET
  45. outcharset = nl_langinfo (CODESET);
  46. # endif
  47. #endif
  48. }
  49. if (*outcharset)
  50. {
  51. iconv_t conv = iconv_open (outcharset, "UTF-8");
  52. if (conv != (iconv_t) -1)
  53. {
  54. static char const copyright_utf_8[] = "\302\251";
  55. char ICONV_CONST *inptr = (char ICONV_CONST *) &copyright_utf_8;
  56. size_t inleft = sizeof copyright_utf_8;
  57. char *outptr = buf;
  58. size_t chars = iconv (conv, &inptr, &inleft, &outptr, &bufsize);
  59. iconv_close (conv);
  60. if (chars != (size_t) -1)
  61. return buf;
  62. }
  63. }
  64. #endif
  65. /* "(C)" is the best we can do in ASCII. */
  66. return "(C)";
  67. }