#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.
Send comments to uwe@ohse.de.