chris@umcp-cs.uucp (12/09/84)
Nice, but.... (There's always something to complain about! :-) ) The kernel printf already uses %b for its own purposes (which as it happens might be nice to have in printf itself (but that's another matter)). I don't know what else one might use for binary output -- maybe %@2 for base 2, then you can extend to any base up to 9 with %@3, %@4,.... If you use hex you can get to base 15. Anyone else have any favorite additions to printf? -- (This line accidently left nonblank.) In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (301) 454-7690 UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@maryland
aeb@turing.UUCP (12/10/84)
A long time ago I changed printf in V6 to accept %b for binary output. Not only electrical engineers but also people doing design theory or coding theory have a frequent need for (zero padded) binary output. -- Andries Brouwer -- CWI, Amsterdam -- {philabs,decvax}!mcvax!aeb
lepreau@utah-cs.UUCP (Jay Lepreau) (12/27/84)
Chris Torek (foolishly?) asks: Anyone else have any favorite additions to printf? I like this one best, as it's the most general: a superset of all the rest. It's less than a dozen lines added to the 4.2 BSD doprnt.s-- they are appended below. From utah-gr!thomas (Spencer W. Thomas) Thu Mar 10 16:34:39 1983 Subject: New printf format (feep) Newsgroups: vax.general From: Spencer W. Thomas <thomas> A new format effector has been added to printf. The %p format can be used to format an argument using an arbitrary procedure (thus p). The %p format takes TWO arguments - the first is the address of the procedure to call, the second is the data item. The procedure is called as shown: proc( fdesc, arg, flags, width, ndigits ) FILE *fdesc; char *arg; int flags, width, ndigits; fdesc is the FILE which is being written to. arg is a pointer to the item in the printf argument list. flags is a bit mask describing the presence of various options between the `%' and the `p'. The bits are: 0x01 width was specified. The number is passed in width. 0x02 ndigits was specified. The number is passed in ndigits. 0x04 width was specified with a leading 0. 0x08 a minus sign was included. 0x10 a plus sign was included. 0x20 a # was included. 0x40 a capital `P' was used rather than a lower case `p'. 0x80 a space was included. The printf specification may include numbers between the % and the p, separated by a `.'. The first is passed in the variable width, the second in ndigits. These arguments should be ignored if the appropriate flag bit is not set. The formatting function must return the number of bytes used from the printf argument list (i.e. when formatting an int, return sizeof(int), when formatting a double or float, return sizeof(double), when formatting a pointer, return sizeof(pointer). The formatting function may call printf recursively. A sample function which formats a point is given below, along with a sample call. struct point { float xyz[3]; } p; /* Format a point as [x y z] using `g' format. Ignore flags, etc. */ prpoint( f, pt, flags, wid, nd ) FILE *f; point *pt; { fprintf(f, "[%g %g %g]", pt->xyz[0], pt->xyz[1], pt->xyz[2]); return sizeof(point); } /* * A sample call. Note that the actual point is put in the * arguments, NOT a pointer. printf("p value = %p\n", prpoint, p); 198c198 < .word fmtbad-L5 # P --- > .word capital-L5 # P 230c230 < .word fmtbad-L5 # p --- > .word procfmt-L5 # p 246a247,258 > > > procfmt: # use a procedure to format value > movl (ap)+,r0 > pushl ndigit > pushl width > pushl flags > pushl ap # address of data item > pushl fdesc > calls $5,(r0) # proc( file, arg, flags, width, prec ) > addl2 r0,ap # proc returns # bytes "consumed" > jbr loop
henry@utzoo.UUCP (Henry Spencer) (12/30/84)
Those of you who are concerned about compatibility should know something before you install Spencer's %p format: there is a %p format in the printf in the ANSI C draft, and it's not the same thing at all. An unfortunate choice of letter... -- Henry Spencer @ U of Toronto Zoology {allegra,ihnp4,linus,decvax}!utzoo!henry