xref: /freebsd/bin/pax/gen_subs.c (revision 1c324569)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
34b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
44b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
54b88c807SRodney W. Grimes  *
64b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
74b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
35c9a8d1f4SPhilippe Charnier #if 0
36c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
37c9a8d1f4SPhilippe Charnier #endif
384b88c807SRodney W. Grimes #endif /* not lint */
392749b141SDavid E. O'Brien #include <sys/cdefs.h>
402749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes #include <sys/time.h>
444b88c807SRodney W. Grimes #include <sys/stat.h>
455f94e68dSAndrey A. Chernov #include <langinfo.h>
465b94264cSTim J. Robbins #include <stdint.h>
474b88c807SRodney W. Grimes #include <stdio.h>
484b88c807SRodney W. Grimes #include <string.h>
494b88c807SRodney W. Grimes #include "pax.h"
504b88c807SRodney W. Grimes #include "extern.h"
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes  * a collection of general purpose subroutines used by pax
544b88c807SRodney W. Grimes  */
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes /*
574b88c807SRodney W. Grimes  * constants used by ls_list() when printing out archive members
584b88c807SRodney W. Grimes  */
594b88c807SRodney W. Grimes #define MODELEN 20
604b88c807SRodney W. Grimes #define DATELEN 64
61656dcd43SGarrett Wollman #define SIXMONTHS	 ((365 / 2) * 86400)
625f94e68dSAndrey A. Chernov #define CURFRMTM	"%b %e %H:%M"
635f94e68dSAndrey A. Chernov #define OLDFRMTM	"%b %e  %Y"
645f94e68dSAndrey A. Chernov #define CURFRMTD	"%e %b %H:%M"
655f94e68dSAndrey A. Chernov #define OLDFRMTD	"%e %b  %Y"
664b88c807SRodney W. Grimes 
675f94e68dSAndrey A. Chernov static int d_first = -1;
685f94e68dSAndrey A. Chernov 
694b88c807SRodney W. Grimes /*
704b88c807SRodney W. Grimes  * ls_list()
714b88c807SRodney W. Grimes  *	list the members of an archive in ls format
724b88c807SRodney W. Grimes  */
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes void
75f789b261SWarner Losh ls_list(ARCHD *arcn, time_t now, FILE *fp)
764b88c807SRodney W. Grimes {
77f789b261SWarner Losh 	struct stat *sbp;
784b88c807SRodney W. Grimes 	char f_mode[MODELEN];
794b88c807SRodney W. Grimes 	char f_date[DATELEN];
8040feca3aSMark Murray 	const char *timefrmt;
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes 	/*
834b88c807SRodney W. Grimes 	 * if not verbose, just print the file name
844b88c807SRodney W. Grimes 	 */
854b88c807SRodney W. Grimes 	if (!vflag) {
86b1787decSKris Kennaway 		(void)fprintf(fp, "%s\n", arcn->name);
87b1787decSKris Kennaway 		(void)fflush(fp);
884b88c807SRodney W. Grimes 		return;
894b88c807SRodney W. Grimes 	}
904b88c807SRodney W. Grimes 
915f94e68dSAndrey A. Chernov 	if (d_first < 0)
925f94e68dSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
934b88c807SRodney W. Grimes 	/*
944b88c807SRodney W. Grimes 	 * user wants long mode
954b88c807SRodney W. Grimes 	 */
964b88c807SRodney W. Grimes 	sbp = &(arcn->sb);
974b88c807SRodney W. Grimes 	strmode(sbp->st_mode, f_mode);
984b88c807SRodney W. Grimes 
994b88c807SRodney W. Grimes 	/*
1007a6be913SRuslan Ermilov 	 * time format based on age compared to the time pax was started.
1014b88c807SRodney W. Grimes 	 */
1024b88c807SRodney W. Grimes 	if ((sbp->st_mtime + SIXMONTHS) <= now)
1035f94e68dSAndrey A. Chernov 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
1044b88c807SRodney W. Grimes 	else
1055f94e68dSAndrey A. Chernov 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
1064b88c807SRodney W. Grimes 
1074b88c807SRodney W. Grimes 	/*
1084b88c807SRodney W. Grimes 	 * print file mode, link count, uid, gid and time
1094b88c807SRodney W. Grimes 	 */
1104b88c807SRodney W. Grimes 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
1114b88c807SRodney W. Grimes 		f_date[0] = '\0';
1121c324569SKonstantin Belousov 	(void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,
1131c324569SKonstantin Belousov 		(uintmax_t)sbp->st_nlink,
114759dba8cSEd Schouten 		name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
1154b88c807SRodney W. Grimes 
1164b88c807SRodney W. Grimes 	/*
1174b88c807SRodney W. Grimes 	 * print device id's for devices, or sizes for other nodes
1184b88c807SRodney W. Grimes 	 */
1194b88c807SRodney W. Grimes 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
1204b88c807SRodney W. Grimes #		ifdef NET2_STAT
121b1787decSKris Kennaway 		(void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
12278b09ffeSSteve Price 		    MINOR(sbp->st_rdev));
1234b88c807SRodney W. Grimes #		else
124b1787decSKris Kennaway 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
12578b09ffeSSteve Price 		    (unsigned long)MINOR(sbp->st_rdev));
1264b88c807SRodney W. Grimes #		endif
1274b88c807SRodney W. Grimes 	else {
1284b88c807SRodney W. Grimes #		ifdef NET2_STAT
129b1787decSKris Kennaway 		(void)fprintf(fp, "%9lu ", sbp->st_size);
1304b88c807SRodney W. Grimes #		else
1315b94264cSTim J. Robbins 		(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
1324b88c807SRodney W. Grimes #		endif
1334b88c807SRodney W. Grimes 	}
1344b88c807SRodney W. Grimes 
1354b88c807SRodney W. Grimes 	/*
1364b88c807SRodney W. Grimes 	 * print name and link info for hard and soft links
1374b88c807SRodney W. Grimes 	 */
138b1787decSKris Kennaway 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
1394b88c807SRodney W. Grimes 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
140b1787decSKris Kennaway 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
1414b88c807SRodney W. Grimes 	else if (arcn->type == PAX_SLK)
142b1787decSKris Kennaway 		(void)fprintf(fp, " => %s\n", arcn->ln_name);
1434b88c807SRodney W. Grimes 	else
144b1787decSKris Kennaway 		(void)putc('\n', fp);
145b1787decSKris Kennaway 	(void)fflush(fp);
1464b88c807SRodney W. Grimes 	return;
1474b88c807SRodney W. Grimes }
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes /*
1504b88c807SRodney W. Grimes  * tty_ls()
1514b88c807SRodney W. Grimes  * 	print a short summary of file to tty.
1524b88c807SRodney W. Grimes  */
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes void
155f789b261SWarner Losh ls_tty(ARCHD *arcn)
1564b88c807SRodney W. Grimes {
1574b88c807SRodney W. Grimes 	char f_date[DATELEN];
1584b88c807SRodney W. Grimes 	char f_mode[MODELEN];
15940feca3aSMark Murray 	const char *timefrmt;
1604b88c807SRodney W. Grimes 
1615f94e68dSAndrey A. Chernov 	if (d_first < 0)
1625f94e68dSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1635f94e68dSAndrey A. Chernov 
164778766feSKris Kennaway 	if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
1655f94e68dSAndrey A. Chernov 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
1664b88c807SRodney W. Grimes 	else
1675f94e68dSAndrey A. Chernov 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
1684b88c807SRodney W. Grimes 
1694b88c807SRodney W. Grimes 	/*
1704b88c807SRodney W. Grimes 	 * convert time to string, and print
1714b88c807SRodney W. Grimes 	 */
1724b88c807SRodney W. Grimes 	if (strftime(f_date, DATELEN, timefrmt,
1734b88c807SRodney W. Grimes 	    localtime(&(arcn->sb.st_mtime))) == 0)
1744b88c807SRodney W. Grimes 		f_date[0] = '\0';
1754b88c807SRodney W. Grimes 	strmode(arcn->sb.st_mode, f_mode);
1764b88c807SRodney W. Grimes 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
1774b88c807SRodney W. Grimes 	return;
1784b88c807SRodney W. Grimes }
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes /*
1814b88c807SRodney W. Grimes  * l_strncpy()
182b1787decSKris Kennaway  *	copy src to dest up to len chars (stopping at first '\0').
183b1787decSKris Kennaway  *	when src is shorter than len, pads to len with '\0'.
1844b88c807SRodney W. Grimes  * Return:
1854b88c807SRodney W. Grimes  *	number of chars copied. (Note this is a real performance win over
186b1787decSKris Kennaway  *	doing a strncpy(), a strlen(), and then a possible memset())
1874b88c807SRodney W. Grimes  */
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes int
19040feca3aSMark Murray l_strncpy(char *dest, const char *src, int len)
1914b88c807SRodney W. Grimes {
192f789b261SWarner Losh 	char *stop;
193f789b261SWarner Losh 	char *start;
1944b88c807SRodney W. Grimes 
1954b88c807SRodney W. Grimes 	stop = dest + len;
1964b88c807SRodney W. Grimes 	start = dest;
1974b88c807SRodney W. Grimes 	while ((dest < stop) && (*src != '\0'))
1984b88c807SRodney W. Grimes 		*dest++ = *src++;
199b1787decSKris Kennaway 	len = dest - start;
200b1787decSKris Kennaway 	while (dest < stop)
201b1787decSKris Kennaway 		*dest++ = '\0';
202b1787decSKris Kennaway 	return(len);
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes /*
2064b88c807SRodney W. Grimes  * asc_ul()
2074b88c807SRodney W. Grimes  *	convert hex/octal character string into a u_long. We do not have to
2084b88c807SRodney W. Grimes  *	check for overflow! (the headers in all supported formats are not large
2094b88c807SRodney W. Grimes  *	enough to create an overflow).
2104b88c807SRodney W. Grimes  *	NOTE: strings passed to us are NOT TERMINATED.
2114b88c807SRodney W. Grimes  * Return:
2124b88c807SRodney W. Grimes  *	unsigned long value
2134b88c807SRodney W. Grimes  */
2144b88c807SRodney W. Grimes 
2154b88c807SRodney W. Grimes u_long
216f789b261SWarner Losh asc_ul(char *str, int len, int base)
2174b88c807SRodney W. Grimes {
218f789b261SWarner Losh 	char *stop;
2194b88c807SRodney W. Grimes 	u_long tval = 0;
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes 	stop = str + len;
2224b88c807SRodney W. Grimes 
2234b88c807SRodney W. Grimes 	/*
2244b88c807SRodney W. Grimes 	 * skip over leading blanks and zeros
2254b88c807SRodney W. Grimes 	 */
2264b88c807SRodney W. Grimes 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
2274b88c807SRodney W. Grimes 		++str;
2284b88c807SRodney W. Grimes 
2294b88c807SRodney W. Grimes 	/*
2304b88c807SRodney W. Grimes 	 * for each valid digit, shift running value (tval) over to next digit
2314b88c807SRodney W. Grimes 	 * and add next digit
2324b88c807SRodney W. Grimes 	 */
2334b88c807SRodney W. Grimes 	if (base == HEX) {
2344b88c807SRodney W. Grimes 		while (str < stop) {
2354b88c807SRodney W. Grimes 			if ((*str >= '0') && (*str <= '9'))
2364b88c807SRodney W. Grimes 				tval = (tval << 4) + (*str++ - '0');
2374b88c807SRodney W. Grimes 			else if ((*str >= 'A') && (*str <= 'F'))
2384b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'A');
2394b88c807SRodney W. Grimes 			else if ((*str >= 'a') && (*str <= 'f'))
2404b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'a');
2414b88c807SRodney W. Grimes 			else
2424b88c807SRodney W. Grimes 				break;
2434b88c807SRodney W. Grimes 		}
2444b88c807SRodney W. Grimes 	} else {
2454b88c807SRodney W. Grimes  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
2464b88c807SRodney W. Grimes 			tval = (tval << 3) + (*str++ - '0');
2474b88c807SRodney W. Grimes 	}
2484b88c807SRodney W. Grimes 	return(tval);
2494b88c807SRodney W. Grimes }
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes /*
2524b88c807SRodney W. Grimes  * ul_asc()
2534b88c807SRodney W. Grimes  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
2544b88c807SRodney W. Grimes  *	ascii 0's to fill string completely
2554b88c807SRodney W. Grimes  *	NOTE: the string created is NOT TERMINATED.
2564b88c807SRodney W. Grimes  */
2574b88c807SRodney W. Grimes 
2584b88c807SRodney W. Grimes int
259f789b261SWarner Losh ul_asc(u_long val, char *str, int len, int base)
2604b88c807SRodney W. Grimes {
261f789b261SWarner Losh 	char *pt;
2624b88c807SRodney W. Grimes 	u_long digit;
2634b88c807SRodney W. Grimes 
2644b88c807SRodney W. Grimes 	/*
2654b88c807SRodney W. Grimes 	 * WARNING str is not '\0' terminated by this routine
2664b88c807SRodney W. Grimes 	 */
2674b88c807SRodney W. Grimes 	pt = str + len - 1;
2684b88c807SRodney W. Grimes 
2694b88c807SRodney W. Grimes 	/*
2704b88c807SRodney W. Grimes 	 * do a tailwise conversion (start at right most end of string to place
2714b88c807SRodney W. Grimes 	 * least significant digit). Keep shifting until conversion value goes
2724b88c807SRodney W. Grimes 	 * to zero (all digits were converted)
2734b88c807SRodney W. Grimes 	 */
2744b88c807SRodney W. Grimes 	if (base == HEX) {
2754b88c807SRodney W. Grimes 		while (pt >= str) {
2764b88c807SRodney W. Grimes 			if ((digit = (val & 0xf)) < 10)
2774b88c807SRodney W. Grimes 				*pt-- = '0' + (char)digit;
2784b88c807SRodney W. Grimes 			else
2794b88c807SRodney W. Grimes 				*pt-- = 'a' + (char)(digit - 10);
2804b88c807SRodney W. Grimes 			if ((val = (val >> 4)) == (u_long)0)
2814b88c807SRodney W. Grimes 				break;
2824b88c807SRodney W. Grimes 		}
2834b88c807SRodney W. Grimes 	} else {
2844b88c807SRodney W. Grimes 		while (pt >= str) {
2854b88c807SRodney W. Grimes 			*pt-- = '0' + (char)(val & 0x7);
2864b88c807SRodney W. Grimes 			if ((val = (val >> 3)) == (u_long)0)
2874b88c807SRodney W. Grimes 				break;
2884b88c807SRodney W. Grimes 		}
2894b88c807SRodney W. Grimes 	}
2904b88c807SRodney W. Grimes 
2914b88c807SRodney W. Grimes 	/*
2924b88c807SRodney W. Grimes 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
2934b88c807SRodney W. Grimes 	 */
2944b88c807SRodney W. Grimes 	while (pt >= str)
2954b88c807SRodney W. Grimes 		*pt-- = '0';
2964b88c807SRodney W. Grimes 	if (val != (u_long)0)
2974b88c807SRodney W. Grimes 		return(-1);
2984b88c807SRodney W. Grimes 	return(0);
2994b88c807SRodney W. Grimes }
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes #ifndef NET2_STAT
3024b88c807SRodney W. Grimes /*
3034b88c807SRodney W. Grimes  * asc_uqd()
3044b88c807SRodney W. Grimes  *	convert hex/octal character string into a u_quad_t. We do not have to
3054b88c807SRodney W. Grimes  *	check for overflow! (the headers in all supported formats are not large
3064b88c807SRodney W. Grimes  *	enough to create an overflow).
3074b88c807SRodney W. Grimes  *	NOTE: strings passed to us are NOT TERMINATED.
3084b88c807SRodney W. Grimes  * Return:
3094b88c807SRodney W. Grimes  *	u_quad_t value
3104b88c807SRodney W. Grimes  */
3114b88c807SRodney W. Grimes 
3124b88c807SRodney W. Grimes u_quad_t
313f789b261SWarner Losh asc_uqd(char *str, int len, int base)
3144b88c807SRodney W. Grimes {
315f789b261SWarner Losh 	char *stop;
3164b88c807SRodney W. Grimes 	u_quad_t tval = 0;
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes 	stop = str + len;
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes 	/*
3214b88c807SRodney W. Grimes 	 * skip over leading blanks and zeros
3224b88c807SRodney W. Grimes 	 */
3234b88c807SRodney W. Grimes 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
3244b88c807SRodney W. Grimes 		++str;
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes 	/*
3274b88c807SRodney W. Grimes 	 * for each valid digit, shift running value (tval) over to next digit
3284b88c807SRodney W. Grimes 	 * and add next digit
3294b88c807SRodney W. Grimes 	 */
3304b88c807SRodney W. Grimes 	if (base == HEX) {
3314b88c807SRodney W. Grimes 		while (str < stop) {
3324b88c807SRodney W. Grimes 			if ((*str >= '0') && (*str <= '9'))
3334b88c807SRodney W. Grimes 				tval = (tval << 4) + (*str++ - '0');
3344b88c807SRodney W. Grimes 			else if ((*str >= 'A') && (*str <= 'F'))
3354b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'A');
3364b88c807SRodney W. Grimes 			else if ((*str >= 'a') && (*str <= 'f'))
3374b88c807SRodney W. Grimes 				tval = (tval << 4) + 10 + (*str++ - 'a');
3384b88c807SRodney W. Grimes 			else
3394b88c807SRodney W. Grimes 				break;
3404b88c807SRodney W. Grimes 		}
3414b88c807SRodney W. Grimes 	} else {
3424b88c807SRodney W. Grimes  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
3434b88c807SRodney W. Grimes 			tval = (tval << 3) + (*str++ - '0');
3444b88c807SRodney W. Grimes 	}
3454b88c807SRodney W. Grimes 	return(tval);
3464b88c807SRodney W. Grimes }
3474b88c807SRodney W. Grimes 
3484b88c807SRodney W. Grimes /*
3494b88c807SRodney W. Grimes  * uqd_asc()
3504b88c807SRodney W. Grimes  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
3514b88c807SRodney W. Grimes  *	ascii 0's to fill string completely
3524b88c807SRodney W. Grimes  *	NOTE: the string created is NOT TERMINATED.
3534b88c807SRodney W. Grimes  */
3544b88c807SRodney W. Grimes 
3554b88c807SRodney W. Grimes int
356f789b261SWarner Losh uqd_asc(u_quad_t val, char *str, int len, int base)
3574b88c807SRodney W. Grimes {
358f789b261SWarner Losh 	char *pt;
3594b88c807SRodney W. Grimes 	u_quad_t digit;
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes 	/*
3624b88c807SRodney W. Grimes 	 * WARNING str is not '\0' terminated by this routine
3634b88c807SRodney W. Grimes 	 */
3644b88c807SRodney W. Grimes 	pt = str + len - 1;
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes 	/*
3674b88c807SRodney W. Grimes 	 * do a tailwise conversion (start at right most end of string to place
3684b88c807SRodney W. Grimes 	 * least significant digit). Keep shifting until conversion value goes
3694b88c807SRodney W. Grimes 	 * to zero (all digits were converted)
3704b88c807SRodney W. Grimes 	 */
3714b88c807SRodney W. Grimes 	if (base == HEX) {
3724b88c807SRodney W. Grimes 		while (pt >= str) {
3734b88c807SRodney W. Grimes 			if ((digit = (val & 0xf)) < 10)
3744b88c807SRodney W. Grimes 				*pt-- = '0' + (char)digit;
3754b88c807SRodney W. Grimes 			else
3764b88c807SRodney W. Grimes 				*pt-- = 'a' + (char)(digit - 10);
3774b88c807SRodney W. Grimes 			if ((val = (val >> 4)) == (u_quad_t)0)
3784b88c807SRodney W. Grimes 				break;
3794b88c807SRodney W. Grimes 		}
3804b88c807SRodney W. Grimes 	} else {
3814b88c807SRodney W. Grimes 		while (pt >= str) {
3824b88c807SRodney W. Grimes 			*pt-- = '0' + (char)(val & 0x7);
3834b88c807SRodney W. Grimes 			if ((val = (val >> 3)) == (u_quad_t)0)
3844b88c807SRodney W. Grimes 				break;
3854b88c807SRodney W. Grimes 		}
3864b88c807SRodney W. Grimes 	}
3874b88c807SRodney W. Grimes 
3884b88c807SRodney W. Grimes 	/*
3894b88c807SRodney W. Grimes 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
3904b88c807SRodney W. Grimes 	 */
3914b88c807SRodney W. Grimes 	while (pt >= str)
3924b88c807SRodney W. Grimes 		*pt-- = '0';
3934b88c807SRodney W. Grimes 	if (val != (u_quad_t)0)
3944b88c807SRodney W. Grimes 		return(-1);
3954b88c807SRodney W. Grimes 	return(0);
3964b88c807SRodney W. Grimes }
3974b88c807SRodney W. Grimes #endif
398