/* pathcode/fget_rmtree.c. Generated from rmtree.c.in by configure. */ /* ** Copyright 1998-2002 University of Illinois Board of Trustees ** Copyright 1998-2002 Mark D. Roth ** All rights reserved. ** ** fget_rmtree.c - recursive directory removal ** ** Mark D. Roth ** Campus Information Technologies and Educational Services ** University of Illinois at Urbana-Champaign */ #include #include #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef STDC_HEADERS # include #endif /* ** fget_rmtree() - delete all files in a given tree ** returns: ** 0 success ** -1 (and sets errno) error */ int fget_rmtree(char *path, fget_rmtree_printfunc_t *printfunc, void *state) { char buf[MAXPATHLEN]; DIR *dirp; struct dirent *dep; struct stat s; if (printfunc != NULL) (*printfunc)(state, path); dirp = opendir(path); if (dirp == NULL) return -1; while ((dep = readdir(dirp)) != NULL) { if (strcmp(dep->d_name, ".") == 0 || strcmp(dep->d_name, "..") == 0) continue; snprintf(buf, sizeof(buf), "%s/%s", path, dep->d_name); if (stat(buf, &s) == -1) goto rmtree_error; if (S_ISDIR(s.st_mode) && fget_rmtree(buf, printfunc, state) == -1) goto rmtree_error; if (unlink(buf) == -1) goto rmtree_error; } closedir(dirp); return rmdir(path); rmtree_error: closedir(dirp); return -1; } #ifdef TEST_RMTREE static void rmtree_print(void *state, char *dir) { printf("removing directory: %s\n", dir); } int main(int argc, char *argv[]) { if (fget_rmtree(argv[1], rmtree_print, NULL) == -1) perror("fget_rmtree()"); } #endif /* TEST_RMTREE */