Exemple #1
0
int dirlist(char *pathname)
{
	DIR *dirp;
	struct dirent *dt;
	char *currentpath;
	long max_pathname, namelen, orig_offset;
	int olderrno;

	dirp = opendir(pathname);
	if (dirp == NULL) {
		perror(":: opendir ");
		return RET_ERROR;
	}

	max_pathname = dirent_buf_size(dirp);

	namelen = strlen(pathname) + max_pathname + 1;

	currentpath = malloc(namelen);
	if (currentpath == NULL) {
		perror(":: malloc ");

		closedir(dirp);
		return RET_ERROR;
	}
	memset(currentpath, 0, namelen);

	orig_offset = sprintf(currentpath, "%s/", pathname);

	for (;;) {
		olderrno = errno;

		dt = readdir(dirp);
		if (dt == NULL) {
			/* error detected when errno changed with readdir return NULL. */
			if (errno != olderrno)
				perror(":: readdir ");
			/* else, end of stream reached */
			break;
		}

		/* hide . & .. */
		if (is_dot_or_dotdot(dt->d_name))
			continue;

		sprintf(currentpath + orig_offset, "%s", dt->d_name);

		printf("%s\n", currentpath);

#ifdef DIRLIST_RECURSIVE
		if (is_directory(currentpath))
			dirlist(currentpath);
#endif
	}

	free(currentpath);
	closedir(dirp);

	return RET_OKAY;
}
dbus_bool_t
_dbus_directory_get_next_file (DBusDirIter      *iter,
                               DBusString       *filename,
                               DBusError        *error)
{
  struct dirent *d, *ent;
  size_t buf_size;
  int err;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
  if (!dirent_buf_size (iter->d, &buf_size))
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Can't calculate buffer size when reading directory");
      return FALSE;
    }

  d = (struct dirent *)dbus_malloc (buf_size);
  if (!d)
    {
      dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
                      "No memory to read directory entry");
      return FALSE;
    }

 again:
  err = readdir_r (iter->d, d, &ent);
  if (err || !ent)
    {
      if (err != 0)
        dbus_set_error (error,
                        _dbus_error_from_errno (err),
                        "%s", _dbus_strerror (err));

      dbus_free (d);
      return FALSE;
    }
  else if (ent->d_name[0] == '.' &&
           (ent->d_name[1] == '\0' ||
            (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
    goto again;
  else
    {
      _dbus_string_set_length (filename, 0);
      if (!_dbus_string_append (filename, ent->d_name))
        {
          dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
                          "No memory to read directory entry");
          dbus_free (d);
          return FALSE;
        }
      else
        {
          dbus_free (d);
          return TRUE;
        }
    }
}
Exemple #3
0
/* scandir (optional, sort) files under directory "pathname" */
int _scandir(char *pathname)
{
	DIR *dirp;
	struct dirent **namelist;
	char *currentpath;
	long max_pathname, namelen, orig_offset;
	int count;

	dirp = opendir(pathname);
	if (dirp == NULL) {
		perror(":: opendir ");
		return RET_ERROR;
	}

	max_pathname = dirent_buf_size(dirp);

	namelen = strlen(pathname) + max_pathname + 1;

	currentpath = malloc(namelen);
	if (currentpath == NULL) {
		perror(":: malloc ");

		closedir(dirp);
		return RET_ERROR;
	}
	memset(currentpath, 0, namelen);

	orig_offset = sprintf(currentpath, "%s/", pathname);

	//	count = scandir(currentpath, &namelist, NULL, alphasort);
	count = scandir(currentpath, &namelist, NULL, NULL);
	if (count == -1)
		perror(":: scandir ");
	else {
		while (count--) {
			if (is_dot_or_dotdot(namelist[count]->d_name)) {
				continue;
				free(namelist[count]);
			}

			sprintf(currentpath + orig_offset, "%s", namelist[count]->d_name);
			printf("%s\n", currentpath);

			free(namelist[count]);

#ifdef DIRLIST_RECURSIVE
			if (is_directory(currentpath))
				_scandir(currentpath);
#endif
		}
		free(namelist);
	}

	closedir(dirp);
	free(currentpath);

	return RET_OKAY;
}
Exemple #4
0
/* reentrant version */
int dirlist_r(char *pathname)
{
	DIR *dirp;
	struct dirent *dt, *entry;
	long max_pathname, namelen, orig_offset;
	char *currentpath;

	dirp = opendir(pathname);
	if (dirp == NULL) {
		perror(":: opendir ");
		return RET_ERROR;
	}

	max_pathname = dirent_buf_size(dirp);

	entry = malloc(max_pathname);
	if (entry == NULL) {
		perror(":: malloc ");
		closedir(dirp);
		return RET_ERROR;
	}

	namelen = strlen(pathname) + max_pathname + 1;

	currentpath = malloc(namelen);
	if (currentpath == NULL) {
		perror(":: malloc ");

		free(entry);
		closedir(dirp);
		return RET_ERROR;
	}
	memset(currentpath, 0, namelen);

	orig_offset = sprintf(currentpath, "%s/", pathname);

	while ((readdir_r(dirp, entry, &dt) == 0) && (dt != NULL)) {
		/* hide . & .. */
		if (is_dot_or_dotdot(dt->d_name))
			continue;

		sprintf(currentpath + orig_offset, "%s", dt->d_name);

		printf("%s\n", currentpath);

#ifdef DIRLIST_RECURSIVE
		if (is_directory(currentpath))
			dirlist_r(currentpath);
#endif
	}

	free(entry);
	free(currentpath);
	closedir(dirp);

	return RET_OKAY;
}