xref: /onnv-gate/usr/src/lib/libast/common/string/fmtclock.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * return pointer to formatted clock() tics t
254887Schin  * return value length is at most 6
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin #include <tm.h>
304887Schin 
314887Schin char*
fmtclock(register Sfulong_t t)324887Schin fmtclock(register Sfulong_t t)
334887Schin {
344887Schin 	register int		u;
354887Schin 	char*			buf;
364887Schin 	int			z;
374887Schin 
384887Schin 	static unsigned int	clk_tck;
394887Schin 
408462SApril.Chin@Sun.COM 	if (!clk_tck)
418462SApril.Chin@Sun.COM 	{
428462SApril.Chin@Sun.COM #ifdef CLOCKS_PER_SEC
438462SApril.Chin@Sun.COM 		clk_tck = CLOCKS_PER_SEC;
448462SApril.Chin@Sun.COM #else
458462SApril.Chin@Sun.COM 		if (!(clk_tck = (unsigned int)strtoul(astconf("CLK_TCK", NiL, NiL), NiL, 10)))
468462SApril.Chin@Sun.COM 			clk_tck = 60;
478462SApril.Chin@Sun.COM #endif
488462SApril.Chin@Sun.COM 	}
494887Schin 	if (t == 0)
504887Schin 		return "0";
514887Schin 	if (t == ((Sfulong_t)~0))
524887Schin 		return "%";
534887Schin 	t = (t * 1000000) / clk_tck;
544887Schin 	if (t < 1000)
554887Schin 		u = 'u';
564887Schin 	else if ((t /= 1000) < 1000)
574887Schin 		u = 'm';
584887Schin 	else
594887Schin 		return fmtelapsed(t / 10, 100);
604887Schin 	buf = fmtbuf(z = 7);
614887Schin 	sfsprintf(buf, z, "%I*u%cs", sizeof(t), t, u);
624887Schin 	return buf;
634887Schin }
64