]>
git.proxmox.com Git - mirror_frr.git/blob - lib/sbuf.c
4 * Copyright (C) 2017 Christian Franke
6 * This file is part of FRR.
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with FRR; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 void sbuf_init(struct sbuf
*dest
, char *buf
, size_t size
)
31 dest
->fixed
= (size
> 0);
36 dest
->buf
= XMALLOC(MTYPE_TMP
, 4096);
44 void sbuf_reset(struct sbuf
*dest
)
50 const char *sbuf_buf(struct sbuf
*buf
)
55 void sbuf_free(struct sbuf
*buf
)
58 XFREE(MTYPE_TMP
, buf
->buf
);
61 void sbuf_push(struct sbuf
*buf
, int indent
, const char *format
, ...)
67 int written1
, written2
;
71 va_start(args
, format
);
72 written2
= vsnprintfrr(NULL
, 0, format
, args
);
76 if (written1
>= 0 && written2
>= 0) {
77 while (buf
->pos
+ written1
+ written2
>= new_size
)
79 if (new_size
> buf
->size
) {
81 XREALLOC(MTYPE_TMP
, buf
->buf
, new_size
);
87 written
= snprintf(buf
->buf
+ buf
->pos
, buf
->size
- buf
->pos
, "%*s",
92 if (buf
->pos
> buf
->size
)
95 va_start(args
, format
);
96 written
= vsnprintfrr(buf
->buf
+ buf
->pos
, buf
->size
- buf
->pos
,
102 if (buf
->pos
> buf
->size
)
103 buf
->pos
= buf
->size
;
105 if (buf
->pos
== buf
->size
)
106 assert(!"Buffer filled up!");