Ejemplo n.º 1
0
DIR* opendir(const char *name)
{
	struct _finddata_t find_buf;
	DIR *dirp;
	struct _dircontents *dp;
	char name_buf[_MAX_PATH + 1];
	char *slash = "";
	intptr_t hFile;

	if (!name) {
		name="";
	} else if (*name) {
		const char *s;
		size_t l = strlen (name);

		s=name+l-1;
		if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
			slash = "/";	/* save to insert slash between path and "*.*" */
	}
	strcat(strcat(strcpy(name_buf, name), slash), "*.*");
	dirp=(DIR *)malloc(sizeof(DIR));
	if (dirp==(DIR *)0)
		return (DIR *)0;
	dirp->dd_loc=0;
	dirp->dd_contents=dirp->dd_cp=(struct _dircontents *)0;
	if ( (hFile = _findfirst (name_buf, &find_buf)) == -1 ) {
		free(dirp);
		return (DIR *)0;
	}
	do {
		dp=(struct _dircontents *) malloc (sizeof (struct _dircontents));
		if (dp == (struct _dircontents *)0) {
			free_dircontents (dirp->dd_contents);
			return (DIR *)0;
		}
		dp->_d_entry = malloc (strlen (find_buf.name) + 1);
		if (dp->_d_entry == (char *)0) {
			free (dp);
			free_dircontents (dirp->dd_contents);
			return (DIR *)0;
		}
		if (dirp->dd_contents)
			dirp->dd_cp = dirp->dd_cp->_d_next = dp;
		else
			dirp->dd_contents = dirp->dd_cp = dp;
		strcpy (dp->_d_entry, find_buf.name);
		dp->_d_next = (struct _dircontents *)0;
	} while (!_findnext (hFile, &find_buf));
	dirp->dd_cp = dirp->dd_contents;
	_findclose(hFile);
	return dirp;
}
Ejemplo n.º 2
0
void
closedir (DIR *dirp)
{
  free_dircontents (dirp->dd_contents);
  free ((char *) dirp);
}
Ejemplo n.º 3
0
DIR *opendirx(char *name, char *pattern)
{
    struct stat statb;
    DIR *dirp;
    char c;
    char *s;
    struct _dircontents *dp;
    int len;
    int unc;
    char path[OFS_MAXPATHNAME];
    register char *ip, *op;

    for (ip = name, op = path;; op++, ip++) {
        *op = *ip;
        if (*ip == '\0') {
            break;
        }
    }
    len = ip - name;
    if (len > 0) {
        unc = ((path[0] == '\\' || path[0] == '/') &&
               (path[1] == '\\' || path[1] == '/'));
        c = path[len - 1];
        if (unc) {
            if (c != '\\' && c != '/') {
                path[len] = '/';
                len++;
                path[len] = '\0';
            }
        } else {
            if ((c == '\\' || c == '/') && (len > 1)) {
                len--;
                path[len] = '\0';

                if (path[len - 1] == ':') {
                    path[len] = '/';
                    len++;
                    path[len] = '.';
                    len++;
                    path[len] = '\0';
                }
            } else if (c == ':') {
                path[len] = '.';
                len++;
                path[len] = '\0';
            }
        }
    } else {
        unc = 0;
        path[0] = '.';
        path[1] = '\0';
        len = 1;
    }

    if (stat(path, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR) {
        return NULL;
    }
    dirp = malloc(sizeof(DIR));
    if (dirp == NULL) {
        return dirp;
    }
    c = path[len - 1];
    if (c == '.') {
        if (len == 1) {
            len--;
        } else {
            c = path[len - 2];
            if (c == '\\' || c == ':') {
                len--;
            } else {
                path[len] = '/';
                len++;
            }
        }
    } else if (!unc && ((len != 1) || (c != '\\' && c != '/'))) {
        path[len] = '/';
        len++;
    }
    strcpy(path + len, pattern);

    dirp->dd_loc = 0;
    dirp->dd_contents = dirp->dd_cp = NULL;

    if ((s = getdirent(path)) == NULL) {
        return dirp;
    }
    do {
        if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
                ((dp->_d_entry = malloc(strlen(s) + 1)) == NULL)) {
            if (dp)
                free(dp);
            free_dircontents(dirp->dd_contents);

            return NULL;
        }
        if (dirp->dd_contents)
            dirp->dd_cp = dirp->dd_cp->_d_next = dp;
        else
            dirp->dd_contents = dirp->dd_cp = dp;

        strcpy(dp->_d_entry, s);
        dp->_d_next = NULL;

    }
    while ((s = getdirent(NULL)) != NULL);

    dirp->dd_cp = dirp->dd_contents;
    return dirp;
}
Ejemplo n.º 4
0
DIR *opendir(char *name)
{
  struct stat statb;
  DIR *dirp;
  char c;
  char *s;
  struct _dircontents *dp;
  char nbuf[MAXPATHLEN + 1];
  int len;

  strcpy(nbuf, name);
  len = strlen (nbuf);
  s = nbuf + len;

#if 1
  if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
       (strlen(nbuf) > 1) )
  {
    nbuf[strlen(nbuf) - 1] = 0;

    if ( nbuf[strlen(nbuf) - 1] == ':' )
      strcat(nbuf, "\\.");
  }
  else
    if ( nbuf[strlen(nbuf) - 1] == ':' )
      strcat(nbuf, ".");
#else
  if ( len && ((c = nbuf[len-1]) == '\\' || c == '/' || c == ':') )
  {
    nbuf[len++] = '.';      /* s now points to '.' */
    nbuf[len] = 0;
  }
#endif

  if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
    return NULL;

  if ( (dirp = malloc(sizeof(DIR))) == NULL )
    return NULL;

#if 1
  if ( nbuf[strlen(nbuf) - 1] == '.' )
    strcpy(nbuf + strlen(nbuf) - 1, "*.*");
  else
    if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
         (strlen(nbuf) == 1) )
      strcat(nbuf, "*.*");
    else
      strcat(nbuf, "\\*.*");
#else
  if ( *s == 0 )
    *s++ = '\\';

  strcpy (s, "*.*");
#endif

  dirp -> dd_loc = 0;
  dirp -> dd_contents = dirp -> dd_cp = NULL;

  if ((s = getdirent(nbuf)) == NULL)
    return dirp;

  do
  {
    if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
        ((dp -> _d_entry = malloc(strlen(s) + 1)) == NULL)      )
    {
      if (dp)
        free(dp);
      free_dircontents(dirp -> dd_contents);

      return NULL;
    }

    if (dirp -> dd_contents)
    {
      dirp -> dd_cp -> _d_next = dp;
      dirp -> dd_cp = dirp -> dd_cp -> _d_next;
    }
    else
      dirp -> dd_contents = dirp -> dd_cp = dp;

    strcpy(dp -> _d_entry, s);
    dp -> _d_next = NULL;

    dp -> _d_size = find.cbFile;
    dp -> _d_mode = find.attrFile;
    dp -> _d_time = *(unsigned *) &(find.ftimeLastWrite);
    dp -> _d_date = *(unsigned *) &(find.fdateLastWrite);
  }
  while ((s = getdirent(NULL)) != NULL);

  dirp -> dd_cp = dirp -> dd_contents;

  return dirp;
}