spaces.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Allocate memory region filled with spaces.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /*
  17. @deftypefn Extension char* spaces (int @var{count})
  18. Returns a pointer to a memory region filled with the specified
  19. number of spaces and null terminated. The returned pointer is
  20. valid until at least the next call.
  21. @end deftypefn
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "ansidecl.h"
  27. #include "libiberty.h"
  28. #if VMS
  29. #include <stdlib.h>
  30. #include <unixlib.h>
  31. #else
  32. /* For systems with larger pointers than ints, these must be declared. */
  33. extern PTR malloc (size_t);
  34. extern void free (PTR);
  35. #endif
  36. const char *
  37. spaces (int count)
  38. {
  39. register char *t;
  40. static char *buf;
  41. static int maxsize;
  42. if (count > maxsize)
  43. {
  44. free (buf);
  45. buf = (char *) malloc (count + 1);
  46. if (buf == (char *) 0)
  47. return 0;
  48. for (t = buf + count ; t != buf ; )
  49. {
  50. *--t = ' ';
  51. }
  52. maxsize = count;
  53. buf[count] = '\0';
  54. }
  55. return (const char *) (buf + maxsize - count);
  56. }