/*
   <p><i>itoa()</i> - convert an integer to a string.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include "environ.h"

/*
   Transform the integer <u>i</u> into an ASCII string
   and store the result in the buffer <u>a</u>, which must be long enough
   to hold the resulting string. The numeric string is encoded in the base
   given in <u>r</u>. The base must be in the range 2 to 36. If it is
   outside this range it is defaulted to base 10.
   This is a recursive function that occupies a stack level for every
   digit in the output string.
   
   The function returns a pointer to <u>a</u>.

   - written by Lew Pitcher 
*/
static char *_itoa(unsigned i, char *a, unsigned r)
{
        if (i/r > 0) a = _itoa(i/r,a,r);
        *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
        return a+1;
}


char *itoa(int i, char *a, int r)
{
        r = ((r < 2) || (r > 36)) ? 10 : r;
        if (i < 0)
        {
                *a = '-';
                *_itoa(-i,a+1,r) = 0;
        }
        else *_itoa(i,a,r) = 0;
        return a;
}

