Uwe Ohse

available software

uostr: string construction library


uostr is a collection of library functions to create and manipulate strings. The goal is to not have to worry anymore about the possibility of a buffer overflow. All functions which may fail due to out of memory have variants with an x in the name which cause the program to exit in that case (uostr_add_char -> uostr_xadd_char). All functions and variables have an uostr_ prefix.

Example

Error checking has been omitted.
  #include "uostr.h"
  /* delete everything under /var/log, except subdirectories and their
   * contents.
   */
  uostr_t path={0};
  DIR *d;
  struct dirent *de;
  uostr_xdup_cstr(&path,"/var/log");
  /* opendir want's a \0 terminated string, so do that now */
  uostr_x0(&path);
  d=opendir(path.data);
#ifdef POSSIBILITY1
  uostr_cut(&path,-1); /* throw away that \0 */
  uostr_xadd_char(&path,'/');
#else
  /* also possible: */
  path.data[path.len-1]='\0';
#endif
  while ((de=readdir(d))!=NULL) {
    size_t pos=path.len;
    uostr_xadd_cstr(path,de->d_name);
    uostr_x0(&path); /* terminate that \0 */
    unlink(path.data); XXX do not run me as root!
    /* cut the string to the old length */
    uostr_cut(&path,pos);
  }
  closedir(d);
  /* free the memory */
  uostr_freedata(&path);
As you can see there's no such thing as an automatical garbage collection (when i want to use that i'll use another library instead, uostr is meant to be as small as possible), and strings are not 0 terminated unless you explicitly do so.

Downloading uostr

See here.

Recent changes


Send comments to uwe@ohse.de.