Back to index

cell-binutils  2.17cvs20070401
spaces.c
Go to the documentation of this file.
00001 /* Allocate memory region filled with spaces.
00002    Copyright (C) 1991 Free Software Foundation, Inc.
00003 
00004 This file is part of the libiberty library.
00005 Libiberty is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 Libiberty 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 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with libiberty; see the file COPYING.LIB.  If
00017 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00018 Boston, MA 02110-1301, USA.  */
00019 
00020 /*
00021 
00022 @deftypefn Extension char* spaces (int @var{count})
00023 
00024 Returns a pointer to a memory region filled with the specified
00025 number of spaces and null terminated.  The returned pointer is
00026 valid until at least the next call.
00027 
00028 @end deftypefn
00029 
00030 */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 #include "ansidecl.h"
00036 #include "libiberty.h"
00037 
00038 #if VMS
00039 #include <stdlib.h>
00040 #include <unixlib.h>
00041 #else
00042 /* For systems with larger pointers than ints, these must be declared.  */
00043 extern PTR malloc (size_t);
00044 extern void free (PTR);
00045 #endif
00046 
00047 const char *
00048 spaces (int count)
00049 {
00050   register char *t;
00051   static char *buf;
00052   static int maxsize;
00053 
00054   if (count > maxsize)
00055     {
00056       if (buf)
00057        {
00058          free (buf);
00059        }
00060       buf = (char *) malloc (count + 1);
00061       if (buf == (char *) 0)
00062        return 0;
00063       for (t = buf + count ; t != buf ; )
00064        {
00065          *--t = ' ';
00066        }
00067       maxsize = count;
00068       buf[count] = '\0';
00069     }
00070   return (const char *) (buf + maxsize - count);
00071 }
00072