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