Home | History | Annotate | Line # | Download | only in pax
gen_subs.c revision 1.11
      1  1.11   mycroft /*	$NetBSD: gen_subs.c,v 1.11 1998/07/26 19:43:14 mycroft Exp $	*/
      2   1.5       cgd 
      3   1.1       jtc /*-
      4   1.1       jtc  * Copyright (c) 1992 Keith Muller.
      5   1.1       jtc  * Copyright (c) 1992, 1993
      6   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7   1.1       jtc  *
      8   1.1       jtc  * This code is derived from software contributed to Berkeley by
      9   1.1       jtc  * Keith Muller of the University of California, San Diego.
     10   1.1       jtc  *
     11   1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12   1.1       jtc  * modification, are permitted provided that the following conditions
     13   1.1       jtc  * are met:
     14   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19   1.1       jtc  * 3. All advertising materials mentioning features or use of this software
     20   1.1       jtc  *    must display the following acknowledgement:
     21   1.1       jtc  *	This product includes software developed by the University of
     22   1.1       jtc  *	California, Berkeley and its contributors.
     23   1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       jtc  *    may be used to endorse or promote products derived from this software
     25   1.1       jtc  *    without specific prior written permission.
     26   1.1       jtc  *
     27   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       jtc  * SUCH DAMAGE.
     38   1.1       jtc  */
     39   1.1       jtc 
     40   1.7  christos #include <sys/cdefs.h>
     41   1.1       jtc #ifndef lint
     42   1.5       cgd #if 0
     43   1.5       cgd static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
     44   1.5       cgd #else
     45  1.11   mycroft __RCSID("$NetBSD: gen_subs.c,v 1.11 1998/07/26 19:43:14 mycroft Exp $");
     46   1.5       cgd #endif
     47   1.1       jtc #endif /* not lint */
     48   1.1       jtc 
     49   1.1       jtc #include <sys/types.h>
     50   1.1       jtc #include <sys/time.h>
     51   1.1       jtc #include <sys/stat.h>
     52   1.1       jtc #include <sys/param.h>
     53   1.1       jtc #include <stdio.h>
     54   1.1       jtc #include <ctype.h>
     55  1.10    kleink #include <time.h>
     56   1.1       jtc #include <tzfile.h>
     57   1.1       jtc #include <utmp.h>
     58   1.1       jtc #include <unistd.h>
     59   1.1       jtc #include <stdlib.h>
     60   1.1       jtc #include <string.h>
     61   1.1       jtc #include "pax.h"
     62   1.1       jtc #include "extern.h"
     63   1.1       jtc 
     64   1.1       jtc /*
     65   1.1       jtc  * a collection of general purpose subroutines used by pax
     66   1.1       jtc  */
     67   1.1       jtc 
     68   1.1       jtc /*
     69   1.1       jtc  * constants used by ls_list() when printing out archive members
     70   1.1       jtc  */
     71   1.1       jtc #define MODELEN 20
     72   1.1       jtc #define DATELEN 64
     73   1.1       jtc #define SIXMONTHS	 ((DAYSPERNYEAR / 2) * SECSPERDAY)
     74   1.1       jtc #define CURFRMT		"%b %e %H:%M"
     75   1.1       jtc #define OLDFRMT		"%b %e  %Y"
     76   1.1       jtc #ifndef UT_NAMESIZE
     77   1.1       jtc #define UT_NAMESIZE	8
     78   1.1       jtc #endif
     79   1.1       jtc #define UT_GRPSIZE	6
     80   1.1       jtc 
     81   1.1       jtc /*
     82   1.1       jtc  * ls_list()
     83   1.1       jtc  *	list the members of an archive in ls format
     84   1.1       jtc  */
     85   1.1       jtc 
     86   1.1       jtc #if __STDC__
     87   1.1       jtc void
     88   1.6       tls ls_list(ARCHD *arcn, time_t now)
     89   1.1       jtc #else
     90   1.1       jtc void
     91   1.1       jtc ls_list(arcn, now)
     92   1.6       tls 	ARCHD *arcn;
     93   1.1       jtc 	time_t now;
     94   1.1       jtc #endif
     95   1.1       jtc {
     96   1.6       tls 	struct stat *sbp;
     97   1.1       jtc 	char f_mode[MODELEN];
     98   1.1       jtc 	char f_date[DATELEN];
     99   1.1       jtc 	char *timefrmt;
    100   1.1       jtc 
    101   1.1       jtc 	/*
    102   1.1       jtc 	 * if not verbose, just print the file name
    103   1.1       jtc 	 */
    104   1.1       jtc 	if (!vflag) {
    105   1.1       jtc 		(void)printf("%s\n", arcn->name);
    106   1.1       jtc 		(void)fflush(stdout);
    107   1.1       jtc 		return;
    108   1.1       jtc 	}
    109   1.1       jtc 
    110   1.1       jtc 	/*
    111   1.1       jtc 	 * user wants long mode
    112   1.1       jtc 	 */
    113   1.1       jtc 	sbp = &(arcn->sb);
    114   1.1       jtc 	strmode(sbp->st_mode, f_mode);
    115   1.1       jtc 
    116   1.1       jtc 	if (ltmfrmt == NULL) {
    117   1.1       jtc 		/*
    118   1.1       jtc 		 * no locale specified format. time format based on age
    119   1.1       jtc 		 * compared to the time pax was started.
    120   1.1       jtc 		 */
    121   1.1       jtc 		if ((sbp->st_mtime + SIXMONTHS) <= now)
    122   1.1       jtc 			timefrmt = OLDFRMT;
    123   1.1       jtc 		else
    124   1.1       jtc 			timefrmt = CURFRMT;
    125   1.1       jtc 	} else
    126   1.1       jtc 		timefrmt = ltmfrmt;
    127   1.1       jtc 
    128   1.1       jtc 	/*
    129   1.1       jtc 	 * print file mode, link count, uid, gid and time
    130   1.1       jtc 	 */
    131   1.1       jtc 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
    132   1.1       jtc 		f_date[0] = '\0';
    133   1.1       jtc 	(void)printf("%s%2u %-*s %-*s ", f_mode, sbp->st_nlink, UT_NAMESIZE,
    134   1.1       jtc 		name_uid(sbp->st_uid, 1), UT_GRPSIZE,
    135   1.1       jtc 		name_gid(sbp->st_gid, 1));
    136   1.1       jtc 
    137   1.1       jtc 	/*
    138   1.1       jtc 	 * print device id's for devices, or sizes for other nodes
    139   1.1       jtc 	 */
    140   1.1       jtc 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
    141   1.1       jtc #		ifdef NET2_STAT
    142   1.1       jtc 		(void)printf("%4u,%4u ", MAJOR(sbp->st_rdev),
    143   1.7  christos 		    MINOR(sbp->st_rdev));
    144   1.1       jtc #		else
    145   1.7  christos 		(void)printf("%4lu,%4lu ", (long) MAJOR(sbp->st_rdev),
    146   1.7  christos 		    (long) MINOR(sbp->st_rdev));
    147   1.1       jtc #		endif
    148   1.1       jtc 	else {
    149   1.1       jtc #		ifdef NET2_STAT
    150   1.1       jtc 		(void)printf("%9lu ", sbp->st_size);
    151   1.1       jtc #		else
    152   1.8   thorpej 		(void)printf("%9qu ", (long long)sbp->st_size);
    153   1.1       jtc #		endif
    154   1.1       jtc 	}
    155   1.1       jtc 
    156   1.1       jtc 	/*
    157   1.1       jtc 	 * print name and link info for hard and soft links
    158   1.1       jtc 	 */
    159   1.1       jtc 	(void)printf("%s %s", f_date, arcn->name);
    160   1.1       jtc 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
    161   1.1       jtc 		(void)printf(" == %s\n", arcn->ln_name);
    162   1.1       jtc 	else if (arcn->type == PAX_SLK)
    163   1.1       jtc 		(void)printf(" => %s\n", arcn->ln_name);
    164   1.1       jtc 	else
    165   1.1       jtc 		(void)putchar('\n');
    166   1.1       jtc 	(void)fflush(stdout);
    167   1.1       jtc 	return;
    168   1.1       jtc }
    169   1.1       jtc 
    170   1.1       jtc /*
    171   1.1       jtc  * tty_ls()
    172   1.1       jtc  * 	print a short summary of file to tty.
    173   1.1       jtc  */
    174   1.1       jtc 
    175   1.1       jtc #if __STDC__
    176   1.1       jtc void
    177   1.6       tls ls_tty(ARCHD *arcn)
    178   1.1       jtc #else
    179   1.1       jtc void
    180   1.1       jtc ls_tty(arcn)
    181   1.6       tls 	ARCHD *arcn;
    182   1.1       jtc #endif
    183   1.1       jtc {
    184   1.1       jtc 	char f_date[DATELEN];
    185   1.1       jtc 	char f_mode[MODELEN];
    186   1.1       jtc 	char *timefrmt;
    187   1.1       jtc 
    188   1.1       jtc 	if (ltmfrmt == NULL) {
    189   1.1       jtc 		/*
    190   1.1       jtc 		 * no locale specified format
    191   1.1       jtc 		 */
    192   1.1       jtc 		if ((arcn->sb.st_mtime + SIXMONTHS) <= time((time_t *)NULL))
    193   1.1       jtc 			timefrmt = OLDFRMT;
    194   1.1       jtc 		else
    195   1.1       jtc 			timefrmt = CURFRMT;
    196   1.1       jtc 	} else
    197   1.1       jtc 		timefrmt = ltmfrmt;
    198   1.1       jtc 
    199   1.1       jtc 	/*
    200   1.1       jtc 	 * convert time to string, and print
    201   1.1       jtc 	 */
    202   1.1       jtc 	if (strftime(f_date, DATELEN, timefrmt,
    203   1.1       jtc 	    localtime(&(arcn->sb.st_mtime))) == 0)
    204   1.1       jtc 		f_date[0] = '\0';
    205   1.1       jtc 	strmode(arcn->sb.st_mode, f_mode);
    206   1.1       jtc 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
    207   1.1       jtc 	return;
    208   1.1       jtc }
    209   1.1       jtc 
    210   1.1       jtc /*
    211   1.1       jtc  * zf_strncpy()
    212   1.1       jtc  *	copy src to dest up to len chars (stopping at first '\0'), when src is
    213   1.1       jtc  *	shorter than len, pads to len with '\0'. big performance win (and
    214   1.1       jtc  *	a lot easier to code) over strncpy(), then a strlen() then a
    215   1.4   mycroft  *	memset(). (or doing the memset() first).
    216   1.1       jtc  */
    217   1.1       jtc 
    218   1.1       jtc #if __STDC__
    219   1.1       jtc void
    220  1.11   mycroft zf_strncpy(char *dest, const char *src, int len)
    221   1.1       jtc #else
    222   1.1       jtc void
    223   1.1       jtc zf_strncpy(dest, src, len)
    224   1.6       tls 	char *dest;
    225   1.6       tls 	char *src;
    226   1.1       jtc 	int len;
    227   1.1       jtc #endif
    228   1.1       jtc {
    229   1.6       tls 	char *stop;
    230   1.1       jtc 
    231   1.1       jtc 	stop = dest + len;
    232   1.1       jtc 	while ((dest < stop) && (*src != '\0'))
    233   1.1       jtc 		*dest++ = *src++;
    234   1.1       jtc 	while (dest < stop)
    235   1.1       jtc 		*dest++ = '\0';
    236   1.1       jtc 	return;
    237   1.1       jtc }
    238   1.1       jtc 
    239   1.1       jtc /*
    240   1.1       jtc  * l_strncpy()
    241   1.1       jtc  *	copy src to dest up to len chars (stopping at first '\0')
    242   1.1       jtc  * Return:
    243   1.1       jtc  *	number of chars copied. (Note this is a real performance win over
    244   1.1       jtc  *	doing a strncpy() then a strlen()
    245   1.1       jtc  */
    246   1.1       jtc 
    247   1.1       jtc #if __STDC__
    248   1.1       jtc int
    249  1.11   mycroft l_strncpy(char *dest, const char *src, int len)
    250   1.1       jtc #else
    251   1.1       jtc int
    252   1.1       jtc l_strncpy(dest, src, len)
    253   1.6       tls 	char *dest;
    254   1.6       tls 	char *src;
    255   1.1       jtc 	int len;
    256   1.1       jtc #endif
    257   1.1       jtc {
    258   1.6       tls 	char *stop;
    259   1.6       tls 	char *start;
    260   1.1       jtc 
    261   1.1       jtc 	stop = dest + len;
    262   1.1       jtc 	start = dest;
    263   1.1       jtc 	while ((dest < stop) && (*src != '\0'))
    264   1.1       jtc 		*dest++ = *src++;
    265   1.1       jtc 	if (dest < stop)
    266   1.1       jtc 		*dest = '\0';
    267   1.1       jtc 	return(dest - start);
    268   1.1       jtc }
    269   1.1       jtc 
    270   1.1       jtc /*
    271   1.1       jtc  * asc_ul()
    272   1.1       jtc  *	convert hex/octal character string into a u_long. We do not have to
    273   1.1       jtc  *	check for overflow! (the headers in all supported formats are not large
    274   1.1       jtc  *	enough to create an overflow).
    275   1.1       jtc  *	NOTE: strings passed to us are NOT TERMINATED.
    276   1.1       jtc  * Return:
    277   1.1       jtc  *	unsigned long value
    278   1.1       jtc  */
    279   1.1       jtc 
    280   1.1       jtc #if __STDC__
    281   1.1       jtc u_long
    282   1.6       tls asc_ul(char *str, int len, int base)
    283   1.1       jtc #else
    284   1.1       jtc u_long
    285   1.1       jtc asc_ul(str, len, base)
    286   1.6       tls 	char *str;
    287   1.1       jtc 	int len;
    288   1.6       tls 	int base;
    289   1.1       jtc #endif
    290   1.1       jtc {
    291   1.6       tls 	char *stop;
    292   1.1       jtc 	u_long tval = 0;
    293   1.1       jtc 
    294   1.1       jtc 	stop = str + len;
    295   1.1       jtc 
    296   1.1       jtc 	/*
    297   1.1       jtc 	 * skip over leading blanks and zeros
    298   1.1       jtc 	 */
    299   1.1       jtc 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
    300   1.1       jtc 		++str;
    301   1.1       jtc 
    302   1.1       jtc 	/*
    303   1.1       jtc 	 * for each valid digit, shift running value (tval) over to next digit
    304   1.1       jtc 	 * and add next digit
    305   1.1       jtc 	 */
    306   1.1       jtc 	if (base == HEX) {
    307   1.1       jtc 		while (str < stop) {
    308   1.1       jtc 			if ((*str >= '0') && (*str <= '9'))
    309   1.1       jtc 				tval = (tval << 4) + (*str++ - '0');
    310   1.1       jtc 			else if ((*str >= 'A') && (*str <= 'F'))
    311   1.1       jtc 				tval = (tval << 4) + 10 + (*str++ - 'A');
    312   1.1       jtc 			else if ((*str >= 'a') && (*str <= 'f'))
    313   1.1       jtc 				tval = (tval << 4) + 10 + (*str++ - 'a');
    314   1.1       jtc 			else
    315   1.1       jtc 				break;
    316   1.1       jtc 		}
    317   1.1       jtc 	} else {
    318   1.1       jtc  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
    319   1.1       jtc 			tval = (tval << 3) + (*str++ - '0');
    320   1.1       jtc 	}
    321   1.1       jtc 	return(tval);
    322   1.1       jtc }
    323   1.1       jtc 
    324   1.1       jtc /*
    325   1.1       jtc  * ul_asc()
    326   1.1       jtc  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
    327   1.1       jtc  *	ascii 0's to fill string completely
    328   1.1       jtc  *	NOTE: the string created is NOT TERMINATED.
    329   1.1       jtc  */
    330   1.1       jtc 
    331   1.1       jtc #if __STDC__
    332   1.1       jtc int
    333   1.6       tls ul_asc(u_long val, char *str, int len, int base)
    334   1.1       jtc #else
    335   1.1       jtc int
    336   1.1       jtc ul_asc(val, str, len, base)
    337   1.1       jtc 	u_long val;
    338   1.6       tls 	char *str;
    339   1.6       tls 	int len;
    340   1.6       tls 	int base;
    341   1.1       jtc #endif
    342   1.1       jtc {
    343   1.6       tls 	char *pt;
    344   1.1       jtc 	u_long digit;
    345   1.1       jtc 
    346   1.1       jtc 	/*
    347   1.1       jtc 	 * WARNING str is not '\0' terminated by this routine
    348   1.1       jtc 	 */
    349   1.1       jtc 	pt = str + len - 1;
    350   1.1       jtc 
    351   1.1       jtc 	/*
    352   1.1       jtc 	 * do a tailwise conversion (start at right most end of string to place
    353   1.1       jtc 	 * least significant digit). Keep shifting until conversion value goes
    354   1.1       jtc 	 * to zero (all digits were converted)
    355   1.1       jtc 	 */
    356   1.1       jtc 	if (base == HEX) {
    357   1.1       jtc 		while (pt >= str) {
    358   1.1       jtc 			if ((digit = (val & 0xf)) < 10)
    359   1.1       jtc 				*pt-- = '0' + (char)digit;
    360   1.1       jtc 			else
    361   1.1       jtc 				*pt-- = 'a' + (char)(digit - 10);
    362   1.1       jtc 			if ((val = (val >> 4)) == (u_long)0)
    363   1.1       jtc 				break;
    364   1.1       jtc 		}
    365   1.1       jtc 	} else {
    366   1.1       jtc 		while (pt >= str) {
    367   1.1       jtc 			*pt-- = '0' + (char)(val & 0x7);
    368   1.1       jtc 			if ((val = (val >> 3)) == (u_long)0)
    369   1.1       jtc 				break;
    370   1.1       jtc 		}
    371   1.1       jtc 	}
    372   1.1       jtc 
    373   1.1       jtc 	/*
    374   1.1       jtc 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
    375   1.1       jtc 	 */
    376   1.1       jtc 	while (pt >= str)
    377   1.1       jtc 		*pt-- = '0';
    378   1.1       jtc 	if (val != (u_long)0)
    379   1.1       jtc 		return(-1);
    380   1.1       jtc 	return(0);
    381   1.1       jtc }
    382   1.1       jtc 
    383   1.1       jtc #ifndef NET2_STAT
    384   1.1       jtc /*
    385   1.1       jtc  * asc_uqd()
    386   1.1       jtc  *	convert hex/octal character string into a u_quad_t. We do not have to
    387   1.1       jtc  *	check for overflow! (the headers in all supported formats are not large
    388   1.1       jtc  *	enough to create an overflow).
    389   1.1       jtc  *	NOTE: strings passed to us are NOT TERMINATED.
    390   1.1       jtc  * Return:
    391   1.1       jtc  *	u_quad_t value
    392   1.1       jtc  */
    393   1.1       jtc 
    394   1.1       jtc #if __STDC__
    395   1.1       jtc u_quad_t
    396   1.6       tls asc_uqd(char *str, int len, int base)
    397   1.1       jtc #else
    398   1.1       jtc u_quad_t
    399   1.1       jtc asc_uqd(str, len, base)
    400   1.6       tls 	char *str;
    401   1.1       jtc 	int len;
    402   1.6       tls 	int base;
    403   1.1       jtc #endif
    404   1.1       jtc {
    405   1.6       tls 	char *stop;
    406   1.1       jtc 	u_quad_t tval = 0;
    407   1.9   mycroft 
    408   1.9   mycroft #ifdef __GNUC__
    409   1.9   mycroft 	/* This outrageous construct just to shut up a GCC warning. */
    410   1.9   mycroft 	(void) &tval;
    411   1.9   mycroft #endif
    412   1.1       jtc 
    413   1.1       jtc 	stop = str + len;
    414   1.1       jtc 
    415   1.1       jtc 	/*
    416   1.1       jtc 	 * skip over leading blanks and zeros
    417   1.1       jtc 	 */
    418   1.1       jtc 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
    419   1.1       jtc 		++str;
    420   1.1       jtc 
    421   1.1       jtc 	/*
    422   1.1       jtc 	 * for each valid digit, shift running value (tval) over to next digit
    423   1.1       jtc 	 * and add next digit
    424   1.1       jtc 	 */
    425   1.1       jtc 	if (base == HEX) {
    426   1.1       jtc 		while (str < stop) {
    427   1.1       jtc 			if ((*str >= '0') && (*str <= '9'))
    428   1.1       jtc 				tval = (tval << 4) + (*str++ - '0');
    429   1.1       jtc 			else if ((*str >= 'A') && (*str <= 'F'))
    430   1.1       jtc 				tval = (tval << 4) + 10 + (*str++ - 'A');
    431   1.1       jtc 			else if ((*str >= 'a') && (*str <= 'f'))
    432   1.1       jtc 				tval = (tval << 4) + 10 + (*str++ - 'a');
    433   1.1       jtc 			else
    434   1.1       jtc 				break;
    435   1.1       jtc 		}
    436   1.1       jtc 	} else {
    437   1.1       jtc  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
    438   1.1       jtc 			tval = (tval << 3) + (*str++ - '0');
    439   1.1       jtc 	}
    440   1.1       jtc 	return(tval);
    441   1.1       jtc }
    442   1.1       jtc 
    443   1.1       jtc /*
    444   1.1       jtc  * uqd_asc()
    445   1.1       jtc  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
    446   1.1       jtc  *	ascii 0's to fill string completely
    447   1.1       jtc  *	NOTE: the string created is NOT TERMINATED.
    448   1.1       jtc  */
    449   1.1       jtc 
    450   1.1       jtc #if __STDC__
    451   1.1       jtc int
    452   1.6       tls uqd_asc(u_quad_t val, char *str, int len, int base)
    453   1.1       jtc #else
    454   1.1       jtc int
    455   1.1       jtc uqd_asc(val, str, len, base)
    456   1.1       jtc 	u_quad_t val;
    457   1.6       tls 	char *str;
    458   1.6       tls 	int len;
    459   1.6       tls 	int base;
    460   1.1       jtc #endif
    461   1.1       jtc {
    462   1.6       tls 	char *pt;
    463   1.1       jtc 	u_quad_t digit;
    464   1.1       jtc 
    465   1.1       jtc 	/*
    466   1.1       jtc 	 * WARNING str is not '\0' terminated by this routine
    467   1.1       jtc 	 */
    468   1.1       jtc 	pt = str + len - 1;
    469   1.1       jtc 
    470   1.1       jtc 	/*
    471   1.1       jtc 	 * do a tailwise conversion (start at right most end of string to place
    472   1.1       jtc 	 * least significant digit). Keep shifting until conversion value goes
    473   1.1       jtc 	 * to zero (all digits were converted)
    474   1.1       jtc 	 */
    475   1.1       jtc 	if (base == HEX) {
    476   1.1       jtc 		while (pt >= str) {
    477   1.1       jtc 			if ((digit = (val & 0xf)) < 10)
    478   1.1       jtc 				*pt-- = '0' + (char)digit;
    479   1.1       jtc 			else
    480   1.1       jtc 				*pt-- = 'a' + (char)(digit - 10);
    481   1.1       jtc 			if ((val = (val >> 4)) == (u_quad_t)0)
    482   1.1       jtc 				break;
    483   1.1       jtc 		}
    484   1.1       jtc 	} else {
    485   1.1       jtc 		while (pt >= str) {
    486   1.1       jtc 			*pt-- = '0' + (char)(val & 0x7);
    487   1.1       jtc 			if ((val = (val >> 3)) == (u_quad_t)0)
    488   1.1       jtc 				break;
    489   1.1       jtc 		}
    490   1.1       jtc 	}
    491   1.1       jtc 
    492   1.1       jtc 	/*
    493   1.1       jtc 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
    494   1.1       jtc 	 */
    495   1.1       jtc 	while (pt >= str)
    496   1.1       jtc 		*pt-- = '0';
    497   1.1       jtc 	if (val != (u_quad_t)0)
    498   1.1       jtc 		return(-1);
    499   1.1       jtc 	return(0);
    500   1.1       jtc }
    501   1.1       jtc #endif
    502