open-isns: Fix warnings reported by gcc-4.5.2
[open-iscsi.git] / usr / strings.c
blobee6a51cd6d8e8afe59ed0a0399861b6b526585c7
1 /*
2 * Copyright (C) 2002 Cisco Systems, Inc.
3 * maintained by linux-iscsi-devel@lists.sourceforge.net
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * See the file COPYING included with this distribution for more details.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/param.h>
26 #include "strings.h"
27 #include "log.h"
29 int str_init_buffer(struct str_buffer *s, size_t initial_allocation)
31 if (s) {
32 memset(s, 0, sizeof (*s));
33 s->buffer = NULL;
34 if (initial_allocation) {
35 s->buffer = malloc(initial_allocation);
36 if (s->buffer) {
37 s->allocated_length = initial_allocation;
38 memset(s->buffer, 0, initial_allocation);
41 s->data_length = 0;
42 return 1;
45 return 0;
48 struct str_buffer *str_alloc_buffer(size_t initial_allocation)
50 struct str_buffer *s = calloc(1, sizeof (*s));
52 if (s)
53 str_init_buffer(s, initial_allocation);
55 return s;
58 void str_free_buffer(struct str_buffer *s)
60 if (s) {
61 if (s->buffer) {
62 free(s->buffer);
63 s->buffer = NULL;
65 s->allocated_length = 0;
66 s->data_length = 0;
70 int str_enlarge_data(struct str_buffer *s, int length)
72 void *new_buf;
74 if (s) {
75 s->data_length += length;
76 if (s->data_length > s->allocated_length) {
77 log_debug(7, "enlarge buffer from %lu to %lu\n",
78 s->allocated_length, s->data_length);
79 new_buf = realloc(s->buffer, s->data_length);
80 if (!new_buf) {
81 /* too big */
82 log_error("enlarged buffer %p to %d data "
83 "bytes, with only %d bytes of buffer "
84 "space", s, (int)s->data_length,
85 (int)s->allocated_length);
86 return ENOMEM;
88 s->buffer = new_buf;
89 memset(s->buffer + s->allocated_length, 0,
90 s->data_length - s->allocated_length);
91 s->allocated_length = s->data_length;
95 return 0;
98 void str_remove_initial(struct str_buffer *s, int length)
100 char *remaining = s->buffer + length;
101 int amount = s->data_length - length;
103 if (s && length) {
104 memmove(s->buffer, remaining, amount);
105 s->data_length = amount;
106 s->buffer[amount] = '\0';
110 /* truncate the data length down */
111 void str_truncate_buffer(struct str_buffer *s, size_t length)
113 if (s) {
114 if (!s->data_length)
115 return;
116 if (length <= s->data_length) {
117 s->data_length = length;
118 s->buffer[s->data_length] = '\0';
119 } else if (length <= s->allocated_length) {
120 /* clear the data, and declare the
121 * data length to be larger
123 memset(s->buffer + s->data_length, 0,
124 length - s->data_length);
125 s->data_length = length;
126 } else {
127 log_error(
128 "couldn't truncate data buffer to length %d, "
129 "only allocated %d",
130 (int)length, (int)s->allocated_length);
135 char *str_buffer_data(struct str_buffer *s)
137 if (s)
138 return s->buffer;
139 else
140 return NULL;
143 size_t str_data_length(struct str_buffer * s)
145 if (s)
146 return s->data_length;
147 else
148 return 0;
151 size_t str_unused_length(struct str_buffer * s)
153 if (s)
154 return s->allocated_length - s->data_length;
155 else
156 return 0;