+void glob_expand(char *base1, char **argv, int *argc, int maxargs, int sanitize_paths)
+{
+ char *s = argv[*argc];
+ char *p, *q;
+ char *base = base1;
+
+ if (!s || !*s) return;
+
+ if (strncmp(s, base, strlen(base)) == 0) {
+ s += strlen(base);
+ }
+
+ s = strdup(s);
+ if (!s) out_of_memory("glob_expand");
+
+ base = (char *)malloc(strlen(base1)+3);
+ if (!base) out_of_memory("glob_expand");
+
+ sprintf(base," %s/", base1);
+
+ q = s;
+ while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
+ /* split it at this point */
+ *p = 0;
+ glob_expand_one(q, argv, argc, maxargs, sanitize_paths);
+ q = p+strlen(base);
+ }
+
+ if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs, sanitize_paths);
+
+ free(s);
+ free(base);
+}
+
+/*******************************************************************
+ convert a string to lower case
+********************************************************************/
+void strlower(char *s)
+{
+ while (*s) {
+ if (isupper(*s)) *s = tolower(*s);
+ s++;
+ }
+}
+
+/* this is like vsnprintf but it always null terminates, so you
+ can fit at most n-1 chars in */
+int vslprintf(char *str, int n, const char *format, va_list ap)
+{
+ int ret = vsnprintf(str, n, format, ap);
+ if (ret >= n || ret < 0) {
+ str[n-1] = 0;
+ return -1;
+ }
+ str[ret] = 0;
+ return ret;
+}
+
+
+/* like snprintf but always null terminates */
+int slprintf(char *str, int n, char *format, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, format);
+ ret = vslprintf(str,n,format,ap);
+ va_end(ap);
+ return ret;
+}
+
+
+void *Realloc(void *p, int size)
+{
+ if (!p) return (void *)malloc(size);
+ return (void *)realloc(p, size);
+}
+
+
+void clean_fname(char *name)
+{
+ char *p;
+ int l;
+ int modified = 1;
+
+ if (!name) return;
+
+ while (modified) {
+ modified = 0;
+
+ if ((p=strstr(name,"/./"))) {
+ modified = 1;
+ while (*p) {
+ p[0] = p[2];
+ p++;
+ }
+ }
+
+ if ((p=strstr(name,"//"))) {
+ modified = 1;
+ while (*p) {
+ p[0] = p[1];
+ p++;
+ }
+ }
+
+ if (strncmp(p=name,"./",2) == 0) {
+ modified = 1;
+ do {
+ p[0] = p[2];
+ } while (*p++);
+ }
+
+ l = strlen(p=name);
+ if (l > 1 && p[l-1] == '/') {
+ modified = 1;
+ p[l-1] = 0;
+ }
+ }
+}
+
+/*
+ * Make path appear as if a chroot had occurred:
+ * 1. remove leading "/" (or replace with "." if at end)
+ * 2. remove leading ".." components
+ * 3. delete any other "<dir>/.." (recursively)
+ * While we're at it, remove double slashes and "." components like
+ * clean_fname does(), but DON'T remove a trailing slash because that
+ * is sometimes significant on command line arguments.
+ * Return a malloc'ed copy.
+ * Contributed by Dave Dykstra <dwd@bell-labs.com>
+ */
+
+char *sanitize_path(char *p)
+{
+ char *copy, *copyp;
+
+ copy = (char *) malloc(strlen(p)+1);
+ copyp = copy;
+ while (*p == '/') {
+ /* remove leading slashes */
+ p++;
+ }
+ while (*p != '\0') {
+ /* this loop iterates once per filename component in p.
+ * both p (and copyp if the original had a slash) should
+ * always be left pointing after a slash
+ */
+ if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
+ /* skip "." component */
+ while (*++p == '/') {
+ /* skip following slashes */
+ ;
+ }
+ } else if ((*p == '.') && (*(p+1) == '.') &&
+ ((*(p+2) == '/') || (*(p+2) == '\0'))) {
+ /* skip ".." component followed by slash or end */
+ p += 2;
+ if (*p == '/')
+ p++;
+ if (copyp != copy) {
+ /* back up the copy one level */
+ --copyp; /* now pointing at slash */
+ while ((copyp > copy) && (*(copyp - 1) != '/')) {
+ /* skip back up to slash */
+ copyp--;
+ }
+ }
+ } else {
+ while (1) {
+ /* copy one component through next slash */
+ *copyp++ = *p++;
+ if ((*p == '\0') || (*(p-1) == '/')) {
+ while (*p == '/') {
+ /* skip multiple slashes */
+ p++;
+ }
+ break;
+ }
+ }
+ }
+ }
+ if (copyp == copy) {
+ /* ended up with nothing, so put in "." component */
+ *copyp++ = '.';
+ }
+ *copyp = '\0';
+ return(copy);