Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | http://code.google.com/p/inferno-os/source/browse/libbio/bprint.c |
| 3 | |
| 4 | Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. |
| 5 | Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved. |
Russ Cox | 9d5da46 | 2010-08-10 17:38:28 -0700 | [diff] [blame] | 6 | Revisions Copyright © 2010 Google Inc. All rights reserved. |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 7 | |
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | of this software and associated documentation files (the "Software"), to deal |
| 10 | in the Software without restriction, including without limitation the rights |
| 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the Software is |
| 13 | furnished to do so, subject to the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be included in |
| 16 | all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <u.h> |
| 28 | #include <libc.h> |
| 29 | #include <bio.h> |
| 30 | |
| 31 | int |
| 32 | Bprint(Biobuf *bp, char *fmt, ...) |
| 33 | { |
Russ Cox | 9d5da46 | 2010-08-10 17:38:28 -0700 | [diff] [blame] | 34 | int n; |
| 35 | va_list arg; |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 36 | |
Russ Cox | 9d5da46 | 2010-08-10 17:38:28 -0700 | [diff] [blame] | 37 | va_start(arg, fmt); |
| 38 | n = Bvprint(bp, fmt, arg); |
| 39 | va_end(arg); |
| 40 | return n; |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | bflush(Fmt *f) |
| 45 | { |
| 46 | Biobuf *bp; |
| 47 | |
| 48 | if(f->stop == nil) |
| 49 | return 0; |
| 50 | |
| 51 | bp = f->farg; |
| 52 | bp->ocount = (char*)f->to - (char*)f->stop; |
| 53 | if(Bflush(bp) < 0) { |
| 54 | f->stop = nil; |
| 55 | f->to = nil; |
| 56 | return 0; |
| 57 | } |
| 58 | f->to = (char*)f->stop + bp->ocount; |
| 59 | |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | Bvprint(Biobuf *bp, char *fmt, va_list arg) |
| 65 | { |
| 66 | int n; |
| 67 | Fmt f; |
| 68 | |
| 69 | memset(&f, 0, sizeof f); |
| 70 | fmtlocaleinit(&f, nil, nil, nil); |
| 71 | f.stop = bp->ebuf; |
| 72 | f.to = (char*)f.stop + bp->ocount; |
| 73 | f.flush = bflush; |
| 74 | f.farg = bp; |
| 75 | |
| 76 | n = fmtvprint(&f, fmt, arg); |
| 77 | |
| 78 | if(f.stop != nil) |
| 79 | bp->ocount = (char*)f.to - (char*)f.stop; |
| 80 | |
| 81 | return n; |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 82 | } |