自動拡張する文字列用バッファ

GString

#include <glib.h>
typedef struct {
  gchar  *str;
  gsize len;
  gsize allocated_len;
} GString;
GString* g_string_new(const gchar *init);
GString* g_string_append(GString *string, const gchar *val);
GString* g_string_prepend(GString *string, const gchar *val);
GString* g_string_insert(GString *string, gssize pos, const gchar *val);
GString* g_string_erase(GString *string, gssize pos, gssize len);
gchar* g_string_free(GString *string, gboolean free_segment);

GStringは、GLibに含まれている文字列用バッファです。文字列の追加、挿入、削除が容易に行えます。

gchar* g_string_free(GString *string, gboolean free_segment);

stringを解放します。free_segmentをTRUEにすると、string->strも解放されます。
なお、GString->strは文字列を追加した際にアドレスが変わる可能性があるので注意しましょう。

使い方
#include <glib.h>
#include <stdio.h>

int main() {
  GString *gstring;

  gstring=g_string_new("def");
  g_string_append(gstring,"ghi");
  g_string_prepend(gstring,"abc");
  g_string_insert(gstring,3,"__");
  printf("%s\n",gstring->str);
  g_string_free(gstring,TRUE);
  return 0;
}
コンパイル

プログラムが書かれたファイル名をtest.cとします。

gcc test.c `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0`
実行結果
abc__defghi

stralloc

#include <djbdns/stralloc.h>
typedef struct stralloc {
  char *s;
  unsigned int len;
  unsigned int a;
} stralloc;
int stralloc_copy(stralloc *a,const stralloc *b);
int stralloc_copys(stralloc *a,const char *b);
int stralloc_cat(stralloc *a,const stralloc *b);
int stralloc_cats(stralloc *a,const char *b);
int stralloc_copyb(stralloc *a,const char *b,unsigned int n);
int stralloc_catb(stralloc *a,const char *b,unsigned int n);
int stralloc_append(stralloc *a,const char *b);

#include <djbdns/alloc.h>
void alloc_free(char *x);

strallocはdjbdnsに含まれている構造体です。Debianの場合、libdjbdns1-devというパッケージになっています。

int stralloc_copy(stralloc *a,const stralloc *b);
int stralloc_copys(stralloc *a,const char *b);

bの文字列(ヌル文字は含みません)でaを上書きします。aの長さはbと同じになります。aの文字列にはヌル文字が追加されないことに注意してください。
成功すると1を返し、失敗すると0を返します。

int stralloc_cat(stralloc *a,const stralloc *b);
int stralloc_cats(stralloc *a,const char *b);

bの文字列(ヌル文字は含みません)をaに追加します。この関数は単純に追加するだけなので、aがヌル文字で終わっている場合はヌル文字の後ろからbが追加されます。また、追加後、aの文字列にはヌル文字が追加されないことに注意してください。
成功すると1を返し、失敗すると0を返します。

int stralloc_copyb(stralloc *a,const char *b,unsigned int n);

n文字分のbの文字列でaを上書きします。nがbの長さを超えないように気をつけましょう。
aの長さはnになります。aの文字列にはヌル文字が追加されないことに注意してください。
なお、nにヌル文字も含めた長さを渡すと、aにヌル文字が付加されます。
成功すると1を返し、失敗すると0を返します。

int stralloc_catb(stralloc *a,const char *b,unsigned int n);

n文字分のbの文字列をaに追加します。nがbの長さを超えないように気をつけましょう。
この関数は単純に追加するだけなので、aがヌル文字で終わっている場合はヌル文字の後ろからbが追加されます。また、追加後、aの文字列にはヌル文字が追加されないことに注意してください。
なお、nにヌル文字も含めた長さを渡すと、aにヌル文字が付加されます。
成功すると1を返し、失敗すると0を返します。

int stralloc_append(stralloc *a,const char *b);

bの先頭の一文字をaに追加します。aがヌル文字で終わっている場合はヌル文字の後ろからbが追加されます。また、追加後、aの文字列にはヌル文字が追加されないことに注意してください。
成功すると1を返し、失敗すると0を返します。

void alloc_free(char *x);

strallocは、djbdnsに含まれているalloc()でメモリを確保します。alloc()は静的に確保された領域を持っており、最初はその領域を返します。そして、静的に確保された領域を使い切るとmalloc()を使って動的に領域を確保します。
alloc_free()は、渡されたポインタが静的に確保された領域なのか、動的に確保された領域なのかを判別し、動的に確保された領域の場合はfree()を呼んで解放します。

使い方
#include <djbdns/alloc.h>
#include <djbdns/stralloc.h>
#include <stdio.h>

int main() {
  stralloc a={0};

  stralloc_copys(&a,"abcdef");
  stralloc_append(&a,""); // ヌル文字を追加
  printf("%s\n",a.s);
  stralloc_copys(&a,"ghi");
  stralloc_catb(&a,"jklmn",6); // ヌル文字も含めて追加
  printf("%s\n",a.s);
  alloc_free(a.s);
  return 0;
}
コンパイル

プログラムが書かれたファイル名をtest.cとします。

gcc -ldjbdns test.c
実行結果
abcdef
ghijklmn