[comp.unix.questions] printf

norm@oglvee.UUCP (Norman Joseph) (05/18/89)

Regarding the recent discussions over echo & printf...

Would anyone be willing to e-mail me a copy of their man
page for printf(1)?  We don't have it here and although
I can take a good guess, I'd like to see exactly what its
supposed to do.

Thanks in advance (as they say).
-- 
Norm Joseph - Oglevee Computer System, Inc.
  UUCP: ...!{pitt,cgh}!amanue!oglvee!norm
    "A point in all directions is no point at all."

paulr@sequent.UUCP (Paul Reger) (10/12/89)

I was wondering if there exists such a beast as:

void vec_printf(char *fmt,void *args[]);

(as opposed to what we're all familiar with: void printf(char *fmt, ...);)

Such a thing would be useful for a tool that can be used with any
shell - call it shell_printf.  This would have the synopsis:

shell_printf fmt [arg1 [arg2 [arg3 ... [argn]]]]

and its purpose would be to format and print out its arguments to
stdout in the exact same manner as printf() does.  Such a tool would
be useful for output in shell scripts. For example:

shell_printf "There are %9d dogs, weighing %9g pounds, and my son's name is '%s'.\n\n\n" 100 12.23 Eric

which would print out to stdout:

``
There are       100 dogs, weighing     12.23 punds, and my son's name is 'Eric'.


''

Without the vec_printf() routine, such a tool would be hard to do.


			paulr    (Paul Reger)
	      Sequent Computer Systems, Beaverton Oregon
	     {uunet,ucbvax!rutgers!ogccse}!sequent!paulr
-- 
			paulr    (Paul Reger)
	      Sequent Computer Systems, Beaverton Oregon
	     {uunet,ucbvax!rutgers!ogccse}!sequent!paulr

ok@cs.mu.oz.au (Richard O'Keefe) (10/12/89)

In article <23078@sequent.UUCP>, paulr@sequent.UUCP (Paul Reger) writes:
> I was wondering if there exists such a beast as:
> void vec_printf(char *fmt,void *args[]);
> (as opposed to what we're all familiar with: void printf(char *fmt, ...);)

Check your manuals to see if you have vprintf().  It's about as close as
you'll get.  If you haven't got vprintf(), you may have _doprnt().

> Such a thing would be useful for a tool that can be used with any
> shell - call it shell_printf.  This would have the synopsis:
> shell_printf fmt [arg1 [arg2 [arg3 ... [argn]]]]

Why call it shell_printf?  I call mine printf.

> Without the vec_printf() routine, such a tool would be hard to do.

Why?  Mine wasn't.  You have to scan the format yourself anyway to ensure
that the number and type of the actual parameters corresponds to the
number and type of arguments expected by the format.  If you don't do
that, (a) you miss the chance to do some pleasant conversions, e.g.
	printf %d 0xABCD
does hex to decimal conversion and
	printf %d "'x'"
prints the ASCII code for x, and (b) your program will dump core from
time to time, which is not a courteous thing for a utility to do.

All you really need is printf.  For example, in handling
	printf "Hello%c %.*s" "','" 5 "World series"
you scan along looking for a %, printing "Hello".  Then you parse the
format code, copying it into a buffer, note that it requires an integer,
fetch an integer from the remaining arguments, and
	printf(buffer, intcvt(*++argv));
Eventually, you have written "Hello, World", having called printf()
twice.

Ok, so this is slower than doing a call to vec_printf(), but the cost
can be ignored in comparison with the cost of starting up the process
in the first place, and it gives you the opportunity to check for mistakes.

khera@juliet.cs.duke.edu (Vick Khera) (10/13/89)

In article <23078@sequent.UUCP> paulr@crg3.UUCP (Paul Reger) writes:
>I was wondering if there exists such a beast as:
>void vec_printf(char *fmt,void *args[]);
> ...
>Such a thing would be useful for a tool that can be used with any
>shell - call it shell_printf.  This would have the synopsis:
> ...
>Without the vec_printf() routine, such a tool would be hard to do.
>
>			paulr    (Paul Reger)
>	     {uunet,ucbvax!rutgers!ogccse}!sequent!paulr

I picked up a utility called printf (written by Chris Torek) which does
just what you describe, and does it quite well. It even does roman numeral
conversions. I believe it came across one of the comp.sources groups. Here
is an excerpt from the man page:

NAME
     printf - formatted output at shell command level

SYNOPSIS
     printf format-string [ arg1 ] [ arg2 ] ...

DESCRIPTION
     Printf duplicates  (as  far  as  possible)  the  standard  C
     library  routine  of  the  same  name,  at the shell command
     level.  It is similar to echo, except that  it  formats  its
     arguments  according  to  conversion specifications given in
     the format-string before writing them to the  standard  out-
     put.   For  a thorough explanation of format specifications,
     see printf(3s).

     For the sake of perversity,  printf  implements  one  format
     conversion  not supported by the standard printf subroutine:
     the %r and %R conversions, which  print  integers  as  Roman
     numerals.   The  first  format  produces  lowercase, and the
     second uppercase.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ARPA:	khera@cs.duke.edu		Department of Computer Science
CSNET:	khera@duke			Duke University
UUCP:	{mcnc,decvax}!duke!khera	Durham, NC 27706

merlyn@iwarp.intel.com (Randal Schwartz) (10/17/89)

In article <23078@sequent.UUCP>, paulr@sequent (Paul Reger) writes:
| 
| I was wondering if there exists such a beast as:
| 
| void vec_printf(char *fmt,void *args[]);
| 
| (as opposed to what we're all familiar with: void printf(char *fmt, ...);)
| 
| Such a thing would be useful for a tool that can be used with any
| shell - call it shell_printf.  This would have the synopsis:
| 
| shell_printf fmt [arg1 [arg2 [arg3 ... [argn]]]]
| 
| and its purpose would be to format and print out its arguments to
| stdout in the exact same manner as printf() does.  Such a tool would
| be useful for output in shell scripts. For example:
| 
| shell_printf "There are %9d dogs, weighing %9g pounds, and my son's name is '%s'.\n\n\n" 100 12.23 Eric
| 
| which would print out to stdout:
| 
| ``
| There are       100 dogs, weighing     12.23 punds, and my son's name is 'Eric'.
| 
| 
| ''
| 
| Without the vec_printf() routine, such a tool would be hard to do.

In Perl:

perl -e 'printf "There are %9d dogs, weighing %9g pounds, and my son'\''s name is '\''%s'\''.\n\n\n",@ARGV; exit 0;' 100 12.23 Eric

generates:
There are       100 dogs, weighing     12.23 pounds, and my son's name is 'Eric'.


Close enough?

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/