concat_buf functions are moved into a new separate file concat_buf.c It is common for tgtd and tgtadm, so usr/Makefile is updated to reflect this. To be applied upon the last concat_buf patch. Signed-off-by: Alexander Nezhinsky <alexandern@xxxxxxxxxxxx> --- usr/Makefile | 4 +- usr/concat_buf.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ usr/util.h | 70 +++------------------------------------ 3 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 usr/concat_buf.c diff --git a/usr/Makefile b/usr/Makefile index 14d5b06..64cb58c 100644 --- a/usr/Makefile +++ b/usr/Makefile @@ -40,7 +40,7 @@ LIBS += -lpthread PROGRAMS += tgtd tgtadm tgtimg TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \ - parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o \ + concat_buf.o parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o \ ssc.o bs_ssc.o libssc.o \ bs_null.o bs_sg.o bs.o libcrc32c.o @@ -54,7 +54,7 @@ tgtd: $(TGTD_OBJS) -include $(TGTD_DEP) -TGTADM_OBJS = tgtadm.o +TGTADM_OBJS = tgtadm.o concat_buf.o TGTADM_DEP = $(TGTADM_OBJS:.o=.d) tgtadm: $(TGTADM_OBJS) diff --git a/usr/concat_buf.c b/usr/concat_buf.c new file mode 100644 index 0000000..9395f4b --- /dev/null +++ b/usr/concat_buf.c @@ -0,0 +1,94 @@ +/* + * concat_buf functions + * appending formatted output to dynamically grown strings + * + * Copyright (C) 2011 Alexander Nezhinsky <alexandernf@xxxxxxxxxxxx> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <inttypes.h> +#include <unistd.h> +#include <stdio.h> + +#include "log.h" +#include "util.h" + +void concat_buf_init(struct concat_buf *b) +{ + b->streamf = open_memstream(&b->buf, (size_t *)&b->size); + b->err = b->streamf ? 0 : errno; + b->used = 0; +} + +int concat_printf(struct concat_buf *b, const char *format, ...) +{ + va_list args; + int nprinted; + + if (!b->err) { + va_start(args, format); + nprinted = vfprintf(b->streamf, format, args); + if (nprinted >= 0) + b->used += nprinted; + else { + b->err = nprinted; + fclose(b->streamf); + b->streamf = NULL; + } + va_end(args); + } + return b->err; +} + +const char *concat_delim(struct concat_buf *b, const char *delim) +{ + return !b->used ? "" : delim; +} + +int concat_buf_finish(struct concat_buf *b) +{ + if (b->streamf) { + fclose(b->streamf); + b->streamf = NULL; + if (b->size) + b->size++; /* account for trailing NULL char */ + } + return b->err; +} + +int concat_write(struct concat_buf *b, int fd, int offset) +{ + concat_buf_finish(b); + + if (b->err) + return b->err; + + if (b->size - offset > 0) + return write(fd, b->buf + offset, b->size - offset); + else + return 0; +} + +void concat_buf_release(struct concat_buf *b) +{ + concat_buf_finish(b); + if (b->buf) { + free(b->buf); + memset(b, 0, sizeof(*b)); + } +} + diff --git a/usr/util.h b/usr/util.h index 45a96ec..e75b23a 100644 --- a/usr/util.h +++ b/usr/util.h @@ -152,69 +152,11 @@ struct concat_buf { int size; }; -static inline void concat_buf_init(struct concat_buf *b) -{ - b->streamf = open_memstream(&b->buf, (size_t *)&b->size); - b->err = b->streamf ? 0 : errno; - b->used = 0; -} - -static inline int concat_printf(struct concat_buf *b, const char *format, ...) -{ - va_list args; - int nprinted; - - if (!b->err) { - va_start(args, format); - nprinted = vfprintf(b->streamf, format, args); - if (nprinted >= 0) - b->used += nprinted; - else { - b->err = nprinted; - fclose(b->streamf); - b->streamf = NULL; - } - va_end(args); - } - return b->err; -} - -static inline const char *concat_delim(struct concat_buf *b, const char *delim) -{ - return !b->used ? "" : delim; -} - -static inline int concat_buf_finish(struct concat_buf *b) -{ - if (b->streamf) { - fclose(b->streamf); - b->streamf = NULL; - if (b->size) - b->size ++; /* account for trailing NULL char */ - } - return b->err; -} - -static inline int concat_write(struct concat_buf *b, int fd, int offset) -{ - concat_buf_finish(b); - - if (b->err) - return b->err; - - if (b->size - offset > 0) - return write(fd, b->buf + offset, b->size - offset); - else - return 0; -} - -static inline void concat_buf_release(struct concat_buf *b) -{ - concat_buf_finish(b); - if (b->buf) { - free(b->buf); - memset(b, 0, sizeof(*b)); - } -} +void concat_buf_init(struct concat_buf *b); +int concat_printf(struct concat_buf *b, const char *format, ...); +const char *concat_delim(struct concat_buf *b, const char *delim); +int concat_buf_finish(struct concat_buf *b); +int concat_write(struct concat_buf *b, int fd, int offset); +void concat_buf_release(struct concat_buf *b); #endif -- 1.7.3.2 -- To unsubscribe from this list: send the line "unsubscribe stgt" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html