xref: /freebsd/bin/pax/gen_subs.c (revision e043f372)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/time.h>
384b88c807SRodney W. Grimes #include <sys/stat.h>
395f94e68dSAndrey A. Chernov #include <langinfo.h>
405b94264cSTim J. Robbins #include <stdint.h>
414b88c807SRodney W. Grimes #include <stdio.h>
424b88c807SRodney W. Grimes #include <string.h>
434b88c807SRodney W. Grimes #include "pax.h"
444b88c807SRodney W. Grimes #include "extern.h"
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes /*
474b88c807SRodney W. Grimes  * a collection of general purpose subroutines used by pax
484b88c807SRodney W. Grimes  */
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes /*
514b88c807SRodney W. Grimes  * constants used by ls_list() when printing out archive members
524b88c807SRodney W. Grimes  */
534b88c807SRodney W. Grimes #define MODELEN 20
544b88c807SRodney W. Grimes #define DATELEN 64
55656dcd43SGarrett Wollman #define SIXMONTHS	 ((365 / 2) * 86400)
565f94e68dSAndrey A. Chernov #define CURFRMTM	"%b %e %H:%M"
575f94e68dSAndrey A. Chernov #define OLDFRMTM	"%b %e  %Y"
585f94e68dSAndrey A. Chernov #define CURFRMTD	"%e %b %H:%M"
595f94e68dSAndrey A. Chernov #define OLDFRMTD	"%e %b  %Y"
604b88c807SRodney W. Grimes 
615f94e68dSAndrey A. Chernov static int d_first = -1;
625f94e68dSAndrey A. Chernov 
634b88c807SRodney W. Grimes /*
644b88c807SRodney W. Grimes  * ls_list()
654b88c807SRodney W. Grimes  *	list the members of an archive in ls format
664b88c807SRodney W. Grimes  */
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes void
ls_list(ARCHD * arcn,time_t now,FILE * fp)69f789b261SWarner Losh ls_list(ARCHD *arcn, time_t now, FILE *fp)
704b88c807SRodney W. Grimes {
71f789b261SWarner Losh 	struct stat *sbp;
724b88c807SRodney W. Grimes 	char f_mode[MODELEN];
734b88c807SRodney W. Grimes 	char f_date[DATELEN];
7440feca3aSMark Murray 	const char *timefrmt;
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 	/*
774b88c807SRodney W. Grimes 	 * if not verbose, just print the file name
784b88c807SRodney W. Grimes 	 */
794b88c807SRodney W. Grimes 	if (!vflag) {
80b1787decSKris Kennaway 		(void)fprintf(fp, "%s\n", arcn->name);
81b1787decSKris Kennaway 		(void)fflush(fp);
824b88c807SRodney W. Grimes 		return;
834b88c807SRodney W. Grimes 	}
844b88c807SRodney W. Grimes 
855f94e68dSAndrey A. Chernov 	if (d_first < 0)
865f94e68dSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
874b88c807SRodney W. Grimes 	/*
884b88c807SRodney W. Grimes 	 * user wants long mode
894b88c807SRodney W. Grimes 	 */
904b88c807SRodney W. Grimes 	sbp = &(arcn->sb);
914b88c807SRodney W. Grimes 	strmode(sbp->st_mode, f_mode);
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes 	/*
947a6be913SRuslan Ermilov 	 * time format based on age compared to the time pax was started.
954b88c807SRodney W. Grimes 	 */
964b88c807SRodney W. Grimes 	if ((sbp->st_mtime + SIXMONTHS) <= now)
975f94e68dSAndrey A. Chernov 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
984b88c807SRodney W. Grimes 	else
995f94e68dSAndrey A. Chernov 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes 	/*
1024b88c807SRodney W. Grimes 	 * print file mode, link count, uid, gid and time
1034b88c807SRodney W. Grimes 	 */
1044b88c807SRodney W. Grimes 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
1054b88c807SRodney W. Grimes 		f_date[0] = '\0';
1061c324569SKonstantin Belousov 	(void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,
1071c324569SKonstantin Belousov 		(uintmax_t)sbp->st_nlink,
108759dba8cSEd Schouten 		name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 	/*
1114b88c807SRodney W. Grimes 	 * print device id's for devices, or sizes for other nodes
1124b88c807SRodney W. Grimes 	 */
1134b88c807SRodney W. Grimes 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
114b1787decSKris Kennaway 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
11578b09ffeSSteve Price 		    (unsigned long)MINOR(sbp->st_rdev));
1164b88c807SRodney W. Grimes 	else {
1175b94264cSTim J. Robbins 		(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
1184b88c807SRodney W. Grimes 	}
1194b88c807SRodney W. Grimes 
1204b88c807SRodney W. Grimes 	/*
1214b88c807SRodney W. Grimes 	 * print name and link info for hard and soft links
1224b88c807SRodney W. Grimes 	 */
123b1787decSKris Kennaway 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
1244b88c807SRodney W. Grimes 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
125b1787decSKris Kennaway 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
1264b88c807SRodney W. Grimes 	else if (arcn->type == PAX_SLK)
127b1787decSKris Kennaway 		(void)fprintf(fp, " => %s\n", arcn->ln_name);
1284b88c807SRodney W. Grimes 	else
129b1787decSKris Kennaway 		(void)putc('\n', fp);
130b1787decSKris Kennaway 	(void)fflush(fp);
1314b88c807SRodney W. Grimes 	return;
1324b88c807SRodney W. Grimes }
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes /*
1354b88c807SRodney W. Grimes  * tty_ls()
1364b88c807SRodney W. Grimes  * 	print a short summary of file to tty.
1374b88c807SRodney W. Grimes  */
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes void
ls_tty(ARCHD * arcn)140f789b261SWarner Losh ls_tty(ARCHD *arcn)
1414b88c807SRodney W. Grimes {
1424b88c807SRodney W. Grimes 	char f_date[DATELEN];
1434b88c807SRodney W. Grimes 	char f_mode[MODELEN];
14440feca3aSMark Murray 	const char *timefrmt;
1454b88c807SRodney W. Grimes 
1465f94e68dSAndrey A. Chernov 	if (d_first < 0)
1475f94e68dSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1485f94e68dSAndrey A. Chernov 
149778766feSKris Kennaway 	if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
1505f94e68dSAndrey A. Chernov 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
1514b88c807SRodney W. Grimes 	else
1525f94e68dSAndrey A. Chernov 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes 	/*
1554b88c807SRodney W. Grimes 	 * convert time to string, and print
1564b88c807SRodney W. Grimes 	 */
1574b88c807SRodney W. Grimes 	if (strftime(f_date, DATELEN, timefrmt,
1584b88c807SRodney W. Grimes 	    localtime(&(arcn->sb.st_mtime))) == 0)
1594b88c807SRodney W. Grimes 		f_date[0] = '\0';
1604b88c807SRodney W. Grimes 	strmode(arcn->sb.st_mode, f_mode);
1614b88c807SRodney W. Grimes 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
1624b88c807SRodney W. Grimes 	return;
1634b88c807SRodney W. Grimes }
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes /*
1664b88c807SRodney W. Grimes  * l_strncpy()
167b1787decSKris Kennaway  *	copy src to dest up to len chars (stopping at first '\0').
168b1787decSKris Kennaway  *	when src is shorter than len, pads to len with '\0'.
1694b88c807SRodney W. Grimes  * Return:
1704b88c807SRodney W. Grimes  *	number of chars copied. (Note this is a real performance win over
171b1787decSKris Kennaway  *	doing a strncpy(), a strlen(), and then a possible memset())
1724b88c807SRodney W. Grimes  */
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes int
l_strncpy(char * dest,const char * src,int len)17540feca3aSMark Murray l_strncpy(char *dest, const char *src, int len)
1764b88c807SRodney W. Grimes {
177f789b261SWarner Losh 	char *stop;
178f789b261SWarner Losh 	char *start;
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes 	stop = dest + len;
1814b88c807SRodney W. Grimes 	start = dest;
1824b88c807SRodney W. Grimes 	while ((dest < stop) && (*src != '\0'))
1834b88c807SRodney W. Grimes 		*dest++ = *src++;
184b1787decSKris Kennaway 	len = dest - start;
185b1787decSKris Kennaway 	while (dest < stop)
186b1787decSKris Kennaway 		*dest++ = '\0';
187b1787decSKris Kennaway 	return(len);
1884b88c807SRodney W. Grimes }
1894b88c807SRodney W. Grimes 
1904b88c807SRodney W. Grimes /*
1914b88c807SRodney W. Grimes  * asc_ul()
1924b88c807SRodney W. Grimes  *	convert hex/octal character string into a u_long. We do not have to
1934b88c807SRodney W. Grimes  *	check for overflow! (the headers in all supported formats are not large
1944b88c807SRodney W. Grimes  *	enough to create an overflow).
1954b88c807SRodney W. Grimes  *	NOTE: strings passed to us are NOT TERMINATED.
1964b88c807SRodney W. Grimes  * Return:
1974b88c807SRodney W. Grimes  *	unsigned long value
1984b88c807SRodney W. Grimes  */
1994b88c807SRodney W. Grimes 
2004b88c807SRodney W. Grimes u_long
asc_ul(char * str,int len,int base)201f789b261SWarner Losh asc_ul(char *str, int len, int base)
2024b88c807SRodney W. Grimes {
203f789b261SWarner Losh 	char *stop;
2044b88c807SRodney W. Grimes 	u_long tval = 0;
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes 	stop = str + len;
2074b88c807SRodney W. Grimes 
2084b88c807SRodney W. Grimes 	/*
2094b88c807SRodney W. Grimes 	 * skip over leading blanks and zeros
2104b88c807SRodney W. Grimes 	 */
2114b88c807SRodney W. Grimes 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
2124b88c807SRodney W. Grimes 		++str;
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes 	/*
2154b88c807SRodney W. Grimes 	 * for each valid digit, shift running value (tval) over to next digit
2164b88c807SRodney W. Grimes 	 * and add next digit
2174b88c807SRodney W. Grimes 	 */
2184b88c807SRodney W. Grimes 	if (base == HEX) {
2194b88c807SRodney W. Grimes 		while (str < stop) {
2204b88c807SRodney W. Grimes 			if ((*str >= '0') && (*str <= '9'))
2214b88c807SRodney W. Grimes 				tval = (tval << 4) + (*str++ - '0');
2224b88c807SRodney W. Grimes 			else if ((*str >= 'A') && (*str <= 'F'))
2234b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'A');
2244b88c807SRodney W. Grimes 			else if ((*str >= 'a') && (*str <= 'f'))
2254b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'a');
2264b88c807SRodney W. Grimes 			else
2274b88c807SRodney W. Grimes 				break;
2284b88c807SRodney W. Grimes 		}
2294b88c807SRodney W. Grimes 	} else {
2304b88c807SRodney W. Grimes  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
2314b88c807SRodney W. Grimes 			tval = (tval << 3) + (*str++ - '0');
2324b88c807SRodney W. Grimes 	}
2334b88c807SRodney W. Grimes 	return(tval);
2344b88c807SRodney W. Grimes }
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes /*
2374b88c807SRodney W. Grimes  * ul_asc()
2384b88c807SRodney W. Grimes  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
2394b88c807SRodney W. Grimes  *	ascii 0's to fill string completely
2404b88c807SRodney W. Grimes  *	NOTE: the string created is NOT TERMINATED.
2414b88c807SRodney W. Grimes  */
2424b88c807SRodney W. Grimes 
2434b88c807SRodney W. Grimes int
ul_asc(u_long val,char * str,int len,int base)244f789b261SWarner Losh ul_asc(u_long val, char *str, int len, int base)
2454b88c807SRodney W. Grimes {
246f789b261SWarner Losh 	char *pt;
2474b88c807SRodney W. Grimes 	u_long digit;
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes 	/*
2504b88c807SRodney W. Grimes 	 * WARNING str is not '\0' terminated by this routine
2514b88c807SRodney W. Grimes 	 */
2524b88c807SRodney W. Grimes 	pt = str + len - 1;
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 	/*
2554b88c807SRodney W. Grimes 	 * do a tailwise conversion (start at right most end of string to place
2564b88c807SRodney W. Grimes 	 * least significant digit). Keep shifting until conversion value goes
2574b88c807SRodney W. Grimes 	 * to zero (all digits were converted)
2584b88c807SRodney W. Grimes 	 */
2594b88c807SRodney W. Grimes 	if (base == HEX) {
2604b88c807SRodney W. Grimes 		while (pt >= str) {
2614b88c807SRodney W. Grimes 			if ((digit = (val & 0xf)) < 10)
2624b88c807SRodney W. Grimes 				*pt-- = '0' + (char)digit;
2634b88c807SRodney W. Grimes 			else
2644b88c807SRodney W. Grimes 				*pt-- = 'a' + (char)(digit - 10);
2654b88c807SRodney W. Grimes 			if ((val = (val >> 4)) == (u_long)0)
2664b88c807SRodney W. Grimes 				break;
2674b88c807SRodney W. Grimes 		}
2684b88c807SRodney W. Grimes 	} else {
2694b88c807SRodney W. Grimes 		while (pt >= str) {
2704b88c807SRodney W. Grimes 			*pt-- = '0' + (char)(val & 0x7);
2714b88c807SRodney W. Grimes 			if ((val = (val >> 3)) == (u_long)0)
2724b88c807SRodney W. Grimes 				break;
2734b88c807SRodney W. Grimes 		}
2744b88c807SRodney W. Grimes 	}
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes 	/*
2774b88c807SRodney W. Grimes 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
2784b88c807SRodney W. Grimes 	 */
2794b88c807SRodney W. Grimes 	while (pt >= str)
2804b88c807SRodney W. Grimes 		*pt-- = '0';
2814b88c807SRodney W. Grimes 	if (val != (u_long)0)
2824b88c807SRodney W. Grimes 		return(-1);
2834b88c807SRodney W. Grimes 	return(0);
2844b88c807SRodney W. Grimes }
2854b88c807SRodney W. Grimes 
2864b88c807SRodney W. Grimes /*
2874b88c807SRodney W. Grimes  * asc_uqd()
2884b88c807SRodney W. Grimes  *	convert hex/octal character string into a u_quad_t. We do not have to
2894b88c807SRodney W. Grimes  *	check for overflow! (the headers in all supported formats are not large
2904b88c807SRodney W. Grimes  *	enough to create an overflow).
2914b88c807SRodney W. Grimes  *	NOTE: strings passed to us are NOT TERMINATED.
2924b88c807SRodney W. Grimes  * Return:
2934b88c807SRodney W. Grimes  *	u_quad_t value
2944b88c807SRodney W. Grimes  */
2954b88c807SRodney W. Grimes 
2964b88c807SRodney W. Grimes u_quad_t
asc_uqd(char * str,int len,int base)297f789b261SWarner Losh asc_uqd(char *str, int len, int base)
2984b88c807SRodney W. Grimes {
299f789b261SWarner Losh 	char *stop;
3004b88c807SRodney W. Grimes 	u_quad_t tval = 0;
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 	stop = str + len;
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes 	/*
3054b88c807SRodney W. Grimes 	 * skip over leading blanks and zeros
3064b88c807SRodney W. Grimes 	 */
3074b88c807SRodney W. Grimes 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
3084b88c807SRodney W. Grimes 		++str;
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes 	/*
3114b88c807SRodney W. Grimes 	 * for each valid digit, shift running value (tval) over to next digit
3124b88c807SRodney W. Grimes 	 * and add next digit
3134b88c807SRodney W. Grimes 	 */
3144b88c807SRodney W. Grimes 	if (base == HEX) {
3154b88c807SRodney W. Grimes 		while (str < stop) {
3164b88c807SRodney W. Grimes 			if ((*str >= '0') && (*str <= '9'))
3174b88c807SRodney W. Grimes 				tval = (tval << 4) + (*str++ - '0');
3184b88c807SRodney W. Grimes 			else if ((*str >= 'A') && (*str <= 'F'))
3194b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'A');
3204b88c807SRodney W. Grimes 			else if ((*str >= 'a') && (*str <= 'f'))
3214b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'a');
3224b88c807SRodney W. Grimes 			else
3234b88c807SRodney W. Grimes 				break;
3244b88c807SRodney W. Grimes 		}
3254b88c807SRodney W. Grimes 	} else {
3264b88c807SRodney W. Grimes  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
3274b88c807SRodney W. Grimes 			tval = (tval << 3) + (*str++ - '0');
3284b88c807SRodney W. Grimes 	}
3294b88c807SRodney W. Grimes 	return(tval);
3304b88c807SRodney W. Grimes }
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes /*
3334b88c807SRodney W. Grimes  * uqd_asc()
3344b88c807SRodney W. Grimes  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
3354b88c807SRodney W. Grimes  *	ascii 0's to fill string completely
3364b88c807SRodney W. Grimes  *	NOTE: the string created is NOT TERMINATED.
3374b88c807SRodney W. Grimes  */
3384b88c807SRodney W. Grimes 
3394b88c807SRodney W. Grimes int
uqd_asc(u_quad_t val,char * str,int len,int base)340f789b261SWarner Losh uqd_asc(u_quad_t val, char *str, int len, int base)
3414b88c807SRodney W. Grimes {
342f789b261SWarner Losh 	char *pt;
3434b88c807SRodney W. Grimes 	u_quad_t digit;
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 	/*
3464b88c807SRodney W. Grimes 	 * WARNING str is not '\0' terminated by this routine
3474b88c807SRodney W. Grimes 	 */
3484b88c807SRodney W. Grimes 	pt = str + len - 1;
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes 	/*
3514b88c807SRodney W. Grimes 	 * do a tailwise conversion (start at right most end of string to place
3524b88c807SRodney W. Grimes 	 * least significant digit). Keep shifting until conversion value goes
3534b88c807SRodney W. Grimes 	 * to zero (all digits were converted)
3544b88c807SRodney W. Grimes 	 */
3554b88c807SRodney W. Grimes 	if (base == HEX) {
3564b88c807SRodney W. Grimes 		while (pt >= str) {
3574b88c807SRodney W. Grimes 			if ((digit = (val & 0xf)) < 10)
3584b88c807SRodney W. Grimes 				*pt-- = '0' + (char)digit;
3594b88c807SRodney W. Grimes 			else
3604b88c807SRodney W. Grimes 				*pt-- = 'a' + (char)(digit - 10);
3614b88c807SRodney W. Grimes 			if ((val = (val >> 4)) == (u_quad_t)0)
3624b88c807SRodney W. Grimes 				break;
3634b88c807SRodney W. Grimes 		}
3644b88c807SRodney W. Grimes 	} else {
3654b88c807SRodney W. Grimes 		while (pt >= str) {
3664b88c807SRodney W. Grimes 			*pt-- = '0' + (char)(val & 0x7);
3674b88c807SRodney W. Grimes 			if ((val = (val >> 3)) == (u_quad_t)0)
3684b88c807SRodney W. Grimes 				break;
3694b88c807SRodney W. Grimes 		}
3704b88c807SRodney W. Grimes 	}
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes 	/*
3734b88c807SRodney W. Grimes 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
3744b88c807SRodney W. Grimes 	 */
3754b88c807SRodney W. Grimes 	while (pt >= str)
3764b88c807SRodney W. Grimes 		*pt-- = '0';
3774b88c807SRodney W. Grimes 	if (val != (u_quad_t)0)
3784b88c807SRodney W. Grimes 		return(-1);
3794b88c807SRodney W. Grimes 	return(0);
3804b88c807SRodney W. Grimes }
381