Beispiel #1
0
internal_function attribute_compat_text_section
getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
{
  static size_t namelen;
  struct stat64 st;
  DIR *dirstream;
  struct dirent64 *d;
  size_t devlen = strlen (dev) + 1;

  dirstream = __opendir (dev);
  if (dirstream == NULL)
    {
      *dostat = -1;
      return NULL;
    }

  while ((d = __readdir64 (dirstream)) != NULL)
    if ((d->d_fileno == myino || *dostat)
	&& strcmp (d->d_name, "stdin")
	&& strcmp (d->d_name, "stdout")
	&& strcmp (d->d_name, "stderr"))
      {
	size_t dlen = _D_ALLOC_NAMLEN (d);
	if (devlen + dlen > namelen)
	  {
	    free (getttyname_name);
	    namelen = 2 * (devlen + dlen); /* Big enough.  */
	    getttyname_name = malloc (namelen);
	    if (! getttyname_name)
	      {
		*dostat = -1;
		/* Perhaps it helps to free the directory stream buffer.  */
		(void) __closedir (dirstream);
		return NULL;
	      }
	    *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
	  }
	memcpy (&getttyname_name[devlen], d->d_name, dlen);
	if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
#ifdef _STATBUF_ST_RDEV
	    && S_ISCHR (st.st_mode) && st.st_rdev == mydev
#else
	    && d->d_fileno == myino && st.st_dev == mydev
#endif
	   )
	  {
	    (void) __closedir (dirstream);
#if 0
	    __ttyname = getttyname_name;
#endif
	    __set_errno (save);
	    return getttyname_name;
	  }
      }

  (void) __closedir (dirstream);
  __set_errno (save);
  return NULL;
}
Beispiel #2
0
static char *
getttyname (int fd, dev_t mydev, ino_t myino, int save, int *dostat)
{
  static const char dev[] = "/dev";
  static size_t namelen;
  struct stat st;
  DIR *dirstream;
  struct dirent *d;

  dirstream = __opendir (dev);
  if (dirstream == NULL)
    {
      *dostat = -1;
      return NULL;
    }

  while ((d = __readdir (dirstream)) != NULL)
    if (((ino_t) d->d_fileno == myino || *dostat)
	&& strcmp (d->d_name, "stdin")
	&& strcmp (d->d_name, "stdout")
	&& strcmp (d->d_name, "stderr"))
      {
	size_t dlen = _D_ALLOC_NAMLEN (d);
	if (sizeof (dev) + dlen > namelen)
	  {
	    free (getttyname_name);
	    namelen = 2 * (sizeof (dev) + dlen); /* Big enough.  */
	    getttyname_name = malloc (namelen);
	    if (! getttyname_name)
	      {
		*dostat = -1;
		/* Perhaps it helps to free the directory stream buffer.  */
		(void) __closedir (dirstream);
		return NULL;
	      }
	    *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
	      = '/';
	  }
	(void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
	if (stat (getttyname_name, &st) == 0
#ifdef _STATBUF_ST_RDEV
	    && S_ISCHR (st.st_mode) && st.st_rdev == mydev
#else
	    && (ino_t) d->d_fileno == myino && st.st_dev == mydev
#endif
	   )
	  {
	    (void) __closedir (dirstream);
	    __ttyname = getttyname_name;
	    __set_errno (save);
	    return getttyname_name;
	  }
      }

  (void) __closedir (dirstream);
  __set_errno (save);
  return NULL;
}
Beispiel #3
0
internal_function
getttyname (const char *dev, dev_t mydev, ino64_t myino, int save)
{
  static size_t namelen;
  struct stat64 st;
  DIR *dirstream;
  struct dirent *d;
  size_t devlen = strlen (dev) + 1;

  dirstream = __opendir (dev);
  if (dirstream == NULL)
    {
      return NULL;
    }

  while ((d = __readdir (dirstream)) != NULL)
    if (d->d_fileno == myino)
      {
	size_t dlen = _D_ALLOC_NAMLEN (d);
	if (devlen + dlen > namelen)
	  {
	    free (getttyname_name);
	    namelen = 2 * (devlen + dlen); /* Big enough.  */
	    getttyname_name = malloc (namelen);
	    if (! getttyname_name)
	      {
		/* Perhaps it helps to free the directory stream buffer.  */
		(void) __closedir (dirstream);
		return NULL;
	      }
	  }
	/* Always recopy dev since it may be master or slave.  */
	*((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';

	memcpy (&getttyname_name[devlen], d->d_name, dlen);
	if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
#ifdef _STATBUF_ST_RDEV
	    && S_ISCHR (st.st_mode) && st.st_rdev == mydev
#else
	    && d->d_fileno == myino && st.st_dev == mydev
#endif
	   )
	  {
	    (void) __closedir (dirstream);
#if 0
	    __ttyname = getttyname_name;
#endif
	    __set_errno (save);
	    return getttyname_name;
	  }
      }

  (void) __closedir (dirstream);
  __set_errno (save);
  return NULL;
}
Beispiel #4
0
int security_get_boolean_names(char ***names, int *len)
{
	char path[PATH_MAX];
	int i, rc;
	struct dirent **namelist;
	char **n;

	if (!len || names == NULL) {
		errno = EINVAL;
		return -1;
	}
	if (!selinux_mnt) {
		errno = ENOENT;
		return -1;
	}

	snprintf(path, sizeof path, "%s%s", selinux_mnt, SELINUX_BOOL_DIR);
	*len = scandir(path, &namelist, &filename_select, alphasort);
	if (*len <= 0) {
		return -1;
	}

	n = (char **)malloc(sizeof(char *) * *len);
	if (!n) {
		rc = -1;
		goto bad;
	}

	for (i = 0; i < *len; i++) {
		n[i] = (char *)malloc(_D_ALLOC_NAMLEN(namelist[i]));
		if (!n[i]) {
			rc = -1;
			goto bad_freen;
		}
		strcpy(n[i], namelist[i]->d_name);
	}
	rc = 0;
	*names = n;
      out:
	for (i = 0; i < *len; i++) {
		free(namelist[i]);
	}
	free(namelist);
	return rc;
      bad_freen:
	for (--i; i >= 0; --i)
		free(n[i]);
	free(n);
      bad:
	goto out;
}
Beispiel #5
0
char *
__getcwd (char *buf, size_t size)
{
    /* Lengths of big file name components and entire file names, and a
       deep level of file name nesting.  These numbers are not upper
       bounds; they are merely large values suitable for initial
       allocations, designed to be large enough for most real-world
       uses.  */
    enum
    {
        BIG_FILE_NAME_COMPONENT_LENGTH = 255,
        BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
        DEEP_NESTING = 100
    };

#if HAVE_OPENAT_SUPPORT
    int fd = AT_FDCWD;
    bool fd_needs_closing = false;
#else
    char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
    char *dotlist = dots;
    size_t dotsize = sizeof dots;
    size_t dotlen = 0;
#endif
    DIR *dirstream = NULL;
    dev_t rootdev, thisdev;
    ino_t rootino, thisino;
    char *dir;
    register char *dirp;
    struct stat st;
    size_t allocated = size;
    size_t used;

#if HAVE_PARTLY_WORKING_GETCWD
    /* The system getcwd works, except it sometimes fails when it
       shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.  If
       AT_FDCWD is not defined, the algorithm below is O(N**2) and this
       is much slower than the system getcwd (at least on GNU/Linux).
       So trust the system getcwd's results unless they look
       suspicious.

       Use the system getcwd even if we have openat support, since the
       system getcwd works even when a parent is unreadable, while the
       openat-based approach does not.  */

# undef getcwd
    dir = getcwd (buf, size);
    if (dir || (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT))
        return dir;
#endif

    if (size == 0)
    {
        if (buf != NULL)
        {
            __set_errno (EINVAL);
            return NULL;
        }

        allocated = BIG_FILE_NAME_LENGTH + 1;
    }

    if (buf == NULL)
    {
        dir = malloc (allocated);
        if (dir == NULL)
            return NULL;
    }
    else
        dir = buf;

    dirp = dir + allocated;
    *--dirp = '\0';

    if (__lstat (".", &st) < 0)
        goto lose;
    thisdev = st.st_dev;
    thisino = st.st_ino;

    if (__lstat ("/", &st) < 0)
        goto lose;
    rootdev = st.st_dev;
    rootino = st.st_ino;

    while (!(thisdev == rootdev && thisino == rootino))
    {
        struct dirent *d;
        dev_t dotdev;
        ino_t dotino;
        bool mount_point;
        int parent_status;
        size_t dirroom;
        size_t namlen;
        bool use_d_ino = true;

        /* Look at the parent directory.  */
#if HAVE_OPENAT_SUPPORT
        fd = openat (fd, "..", O_RDONLY);
        if (fd < 0)
            goto lose;
        fd_needs_closing = true;
        parent_status = fstat (fd, &st);
#else
        dotlist[dotlen++] = '.';
        dotlist[dotlen++] = '.';
        dotlist[dotlen] = '\0';
        parent_status = __lstat (dotlist, &st);
#endif
        if (parent_status != 0)
            goto lose;

        if (dirstream && __closedir (dirstream) != 0)
        {
            dirstream = NULL;
            goto lose;
        }

        /* Figure out if this directory is a mount point.  */
        dotdev = st.st_dev;
        dotino = st.st_ino;
        mount_point = dotdev != thisdev;

        /* Search for the last directory.  */
#if HAVE_OPENAT_SUPPORT
        dirstream = fdopendir (fd);
        if (dirstream == NULL)
            goto lose;
        /* Reset fd.  It may have been closed by fdopendir.  */
        fd = dirfd (dirstream);
        fd_needs_closing = false;
#else
        dirstream = __opendir (dotlist);
        if (dirstream == NULL)
            goto lose;
        dotlist[dotlen++] = '/';
#endif
        for (;;)
        {
            /* Clear errno to distinguish EOF from error if readdir returns
               NULL.  */
            __set_errno (0);
            d = __readdir (dirstream);

            /* When we've iterated through all directory entries without finding
               one with a matching d_ino, rewind the stream and consider each
               name again, but this time, using lstat.  This is necessary in a
               chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
               .., ../.., ../../.., etc. all had the same device number, yet the
               d_ino values for entries in / did not match those obtained
               via lstat.  */
            if (d == NULL && errno == 0 && use_d_ino)
            {
                use_d_ino = false;
                rewinddir (dirstream);
                d = __readdir (dirstream);
            }

            if (d == NULL)
            {
                if (errno == 0)
                    /* EOF on dirstream, which can mean e.g., that the current
                       directory has been removed.  */
                    __set_errno (ENOENT);
                goto lose;
            }
            if (d->d_name[0] == '.' &&
                    (d->d_name[1] == '\0' ||
                     (d->d_name[1] == '.' && d->d_name[2] == '\0')))
                continue;

            if (use_d_ino)
            {
                bool match = (MATCHING_INO (d, thisino) || mount_point);
                if (! match)
                    continue;
            }

            {
                int entry_status;
#if HAVE_OPENAT_SUPPORT
                entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
#else
                /* Compute size needed for this file name, or for the file
                   name ".." in the same directory, whichever is larger.
                   Room for ".." might be needed the next time through
                   the outer loop.  */
                size_t name_alloc = _D_ALLOC_NAMLEN (d);
                size_t filesize = dotlen + MAX (sizeof "..", name_alloc);

                if (filesize < dotlen)
                    goto memory_exhausted;

                if (dotsize < filesize)
                {
                    /* My, what a deep directory tree you have, Grandma.  */
                    size_t newsize = MAX (filesize, dotsize * 2);
                    size_t i;
                    if (newsize < dotsize)
                        goto memory_exhausted;
                    if (dotlist != dots)
                        free (dotlist);
                    dotlist = malloc (newsize);
                    if (dotlist == NULL)
                        goto lose;
                    dotsize = newsize;

                    i = 0;
                    do
                    {
                        dotlist[i++] = '.';
                        dotlist[i++] = '.';
                        dotlist[i++] = '/';
                    }
                    while (i < dotlen);
                }

                memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
                entry_status = __lstat (dotlist, &st);
#endif
                /* We don't fail here if we cannot stat() a directory entry.
                   This can happen when (network) file systems fail.  If this
                   entry is in fact the one we are looking for we will find
                   out soon as we reach the end of the directory without
                   having found anything.  */
                if (entry_status == 0 && S_ISDIR (st.st_mode)
                        && st.st_dev == thisdev && st.st_ino == thisino)
                    break;
            }
        }

        dirroom = dirp - dir;
        namlen = _D_EXACT_NAMLEN (d);

        if (dirroom <= namlen)
        {
            if (size != 0)
            {
                __set_errno (ERANGE);
                goto lose;
            }
            else
            {
                char *tmp;
                size_t oldsize = allocated;

                allocated += MAX (allocated, namlen);
                if (allocated < oldsize
                        || ! (tmp = realloc (dir, allocated)))
                    goto memory_exhausted;

                /* Move current contents up to the end of the buffer.
                This is guaranteed to be non-overlapping.  */
                dirp = memcpy (tmp + allocated - (oldsize - dirroom),
                               tmp + dirroom,
                               oldsize - dirroom);
                dir = tmp;
            }
        }
        dirp -= namlen;
        memcpy (dirp, d->d_name, namlen);
        *--dirp = '/';

        thisdev = dotdev;
        thisino = dotino;
    }

    if (dirstream && __closedir (dirstream) != 0)
    {
        dirstream = NULL;
        goto lose;
    }

    if (dirp == &dir[allocated - 1])
        *--dirp = '/';

#if ! HAVE_OPENAT_SUPPORT
    if (dotlist != dots)
        free (dotlist);
#endif

    used = dir + allocated - dirp;
    memmove (dir, dirp, used);

    if (size == 0)
        /* Ensure that the buffer is only as large as necessary.  */
        buf = realloc (dir, used);

    if (buf == NULL)
        /* Either buf was NULL all along, or `realloc' failed but
           we still have the original string.  */
        buf = dir;

    return buf;

memory_exhausted:
    __set_errno (ENOMEM);
lose:
    {
        int save = errno;
        if (dirstream)
            __closedir (dirstream);
#if HAVE_OPENAT_SUPPORT
        if (fd_needs_closing)
            close (fd);
#else
        if (dotlist != dots)
            free (dotlist);
#endif
        if (buf == NULL)
            free (dir);
        __set_errno (save);
    }
    return NULL;
}
Beispiel #6
0
char *
__getcwd (char *buf, size_t size)
{
  /* Lengths of big file name components and entire file names, and a
     deep level of file name nesting.  These numbers are not upper
     bounds; they are merely large values suitable for initial
     allocations, designed to be large enough for most real-world
     uses.  */
  enum
    {
      BIG_FILE_NAME_COMPONENT_LENGTH = 255,
      BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
      DEEP_NESTING = 100
    };

#ifdef AT_FDCWD
  int fd = AT_FDCWD;
  bool fd_needs_closing = false;
#else
  char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
  char *dotlist = dots;
  size_t dotsize = sizeof dots;
  size_t dotlen = 0;
#endif
  DIR *dirstream = NULL;
  dev_t rootdev, thisdev;
  ino_t rootino, thisino;
  char *path;
  register char *pathp;
  struct stat st;
  size_t allocated = size;
  size_t used;

#if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD
  /* The system getcwd works, except it sometimes fails when it
     shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.  If
     AT_FDCWD is not defined, the algorithm below is O(N**2) and this
     is much slower than the system getcwd (at least on GNU/Linux).
     So trust the system getcwd's results unless they look
     suspicious.  */
# undef getcwd
  path = getcwd (buf, size);
  if (path || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
    return path;
#endif

  if (size == 0)
    {
      if (buf != NULL)
	{
	  __set_errno (EINVAL);
	  return NULL;
	}

      allocated = BIG_FILE_NAME_LENGTH + 1;
    }

  if (buf == NULL)
    {
      path = malloc (allocated);
      if (path == NULL)
	return NULL;
    }
  else
    path = buf;

  pathp = path + allocated;
  *--pathp = '\0';

  if (__lstat (".", &st) < 0)
    goto lose;
  thisdev = st.st_dev;
  thisino = st.st_ino;

  if (__lstat ("/", &st) < 0)
    goto lose;
  rootdev = st.st_dev;
  rootino = st.st_ino;

  while (!(thisdev == rootdev && thisino == rootino))
    {
      struct dirent *d;
      dev_t dotdev;
      ino_t dotino;
      bool mount_point;
      int parent_status;

      /* Look at the parent directory.  */
#ifdef AT_FDCWD
      fd = openat (fd, "..", O_RDONLY);
      if (fd < 0)
	goto lose;
      fd_needs_closing = true;
      parent_status = fstat (fd, &st);
#else
      dotlist[dotlen++] = '.';
      dotlist[dotlen++] = '.';
      dotlist[dotlen] = '\0';
      parent_status = __lstat (dotlist, &st);
#endif
      if (parent_status != 0)
	goto lose;

      if (dirstream && __closedir (dirstream) != 0)
	{
	  dirstream = NULL;
	  goto lose;
	}

      /* Figure out if this directory is a mount point.  */
      dotdev = st.st_dev;
      dotino = st.st_ino;
      mount_point = dotdev != thisdev;

      /* Search for the last directory.  */
#ifdef AT_FDCWD
      dirstream = fdopendir (fd);
      if (dirstream == NULL)
	goto lose;
      fd_needs_closing = false;
#else
      dirstream = __opendir (dotlist);
      if (dirstream == NULL)
	goto lose;
      dotlist[dotlen++] = '/';
#endif
      /* Clear errno to distinguish EOF from error if readdir returns
	 NULL.  */
      __set_errno (0);
      while ((d = __readdir (dirstream)) != NULL)
	{
	  if (d->d_name[0] == '.' &&
	      (d->d_name[1] == '\0' ||
	       (d->d_name[1] == '.' && d->d_name[2] == '\0')))
	    continue;
	  if (MATCHING_INO (d, thisino) || mount_point)
	    {
	      int entry_status;
#ifdef AT_FDCWD
	      entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
#else
	      /* Compute size needed for this file name, or for the file
		 name ".." in the same directory, whichever is larger.
	         Room for ".." might be needed the next time through
		 the outer loop.  */
	      size_t name_alloc = _D_ALLOC_NAMLEN (d);
	      size_t filesize = dotlen + MAX (sizeof "..", name_alloc);

	      if (filesize < dotlen)
		goto memory_exhausted;

	      if (dotsize < filesize)
		{
		  /* My, what a deep directory tree you have, Grandma.  */
		  size_t newsize = MAX (filesize, dotsize * 2);
		  size_t i;
		  if (newsize < dotsize)
		    goto memory_exhausted;
		  if (dotlist != dots)
		    free (dotlist);
		  dotlist = malloc (newsize);
		  if (dotlist == NULL)
		    goto lose;
		  dotsize = newsize;

		  i = 0;
		  do
		    {
		      dotlist[i++] = '.';
		      dotlist[i++] = '.';
		      dotlist[i++] = '/';
		    }
		  while (i < dotlen);
		}

	      strcpy (dotlist + dotlen, d->d_name);
	      entry_status = __lstat (dotlist, &st);
#endif
	      /* We don't fail here if we cannot stat() a directory entry.
		 This can happen when (network) file systems fail.  If this
		 entry is in fact the one we are looking for we will find
		 out soon as we reach the end of the directory without
		 having found anything.  */
	      if (entry_status == 0 && S_ISDIR (st.st_mode)
		  && st.st_dev == thisdev && st.st_ino == thisino)
		break;
	    }
	}
      if (d == NULL)
	{
	  if (errno == 0)
	    /* EOF on dirstream, which means that the current directory
	       has been removed.  */
	    __set_errno (ENOENT);
	  goto lose;
	}
      else
	{
	  size_t pathroom = pathp - path;
	  size_t namlen = _D_EXACT_NAMLEN (d);

	  if (pathroom <= namlen)
	    {
	      if (size != 0)
		{
		  __set_errno (ERANGE);
		  goto lose;
		}
	      else
		{
		  char *tmp;
		  size_t oldsize = allocated;

		  allocated += MAX (allocated, namlen);
		  if (allocated < oldsize
		      || ! (tmp = realloc (path, allocated)))
		    goto memory_exhausted;

		  /* Move current contents up to the end of the buffer.
		     This is guaranteed to be non-overlapping.  */
		  pathp = memcpy (tmp + allocated - (oldsize - pathroom),
				  tmp + pathroom,
				  oldsize - pathroom);
		  path = tmp;
		}
	    }
	  pathp -= namlen;
	  memcpy (pathp, d->d_name, namlen);
	  *--pathp = '/';
	}

      thisdev = dotdev;
      thisino = dotino;
    }

  if (dirstream && __closedir (dirstream) != 0)
    {
      dirstream = NULL;
      goto lose;
    }

  if (pathp == &path[allocated - 1])
    *--pathp = '/';

#ifndef AT_FDCWD
  if (dotlist != dots)
    free (dotlist);
#endif

  used = path + allocated - pathp;
  memmove (path, pathp, used);

  if (buf == NULL && size == 0)
    /* Ensure that the buffer is only as large as necessary.  */
    buf = realloc (path, used);

  if (buf == NULL)
    /* Either buf was NULL all along, or `realloc' failed but
       we still have the original string.  */
    buf = path;

  return buf;

 memory_exhausted:
  __set_errno (ENOMEM);
 lose:
  {
    int save = errno;
    if (dirstream)
      __closedir (dirstream);
#ifdef AT_FDCWD
    if (fd_needs_closing)
      close (fd);
#else
    if (dotlist != dots)
      free (dotlist);
#endif
    if (buf == NULL)
      free (path);
    __set_errno (save);
  }
  return NULL;
}
Beispiel #7
0
int
main(
    int argc, char *argv[]) 
{
    (void) argc;
    (void) argv;

    /* File type macros */
    {
        assert (DTTOIF(DT_REG) == S_IFREG);
        assert (DTTOIF(DT_DIR) == S_IFDIR);
        assert (DTTOIF(DT_FIFO) == S_IFIFO);
        assert (DTTOIF(DT_SOCK) == S_IFSOCK);
        assert (DTTOIF(DT_CHR) == S_IFCHR);
        assert (DTTOIF(DT_BLK) == S_IFBLK);

        assert (IFTODT(S_IFREG) == DT_REG);
        assert (IFTODT(S_IFDIR) == DT_DIR);
        assert (IFTODT(S_IFIFO) == DT_FIFO);
        assert (IFTODT(S_IFSOCK) == DT_SOCK);
        assert (IFTODT(S_IFCHR) == DT_CHR);
        assert (IFTODT(S_IFBLK) == DT_BLK);
    }

    /* Basic directory retrieval */
    {
        DIR *dir;
        struct dirent *ent;
        int found = 0;

        /* Open directory */
        dir = opendir ("tests/1");
        if (dir == NULL) {
            fprintf (stderr, "Directory tests not found\n");
            abort ();
        }

        /* Read entries */
        while ((ent = readdir (dir)) != NULL) {

            /* Check each file */
            if (strcmp (ent->d_name, ".") == 0) {
                /* Directory itself */
#ifdef _DIRENT_HAVE_D_TYPE
                assert (ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
                assert (ent->d_namlen == 1);
#endif
#ifdef _D_EXACT_NAMLEN
                assert (_D_EXACT_NAMLEN(ent) == 1);
#endif
#ifdef _D_ALLOC_NAMLEN
                assert (_D_ALLOC_NAMLEN(ent) > 1);
#endif
                found |= 1;

            } else if (strcmp (ent->d_name, "..") == 0) {
                /* Parent directory */
#ifdef _DIRENT_HAVE_D_TYPE
                assert (ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
                assert (ent->d_namlen == 2);
#endif
#ifdef _D_EXACT_NAMLEN
                assert (_D_EXACT_NAMLEN(ent) == 2);
#endif
#ifdef _D_ALLOC_NAMLEN
                assert (_D_ALLOC_NAMLEN(ent) > 2);
#endif
                found |= 2;

            } else if (strcmp (ent->d_name, "file") == 0) {
                /* Regular file */
#ifdef _DIRENT_HAVE_D_TYPE
                assert (ent->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
                assert (ent->d_namlen == 4);
#endif
#ifdef _D_EXACT_NAMLEN
                assert (_D_EXACT_NAMLEN(ent) == 4);
#endif
#ifdef _D_ALLOC_NAMLEN
                assert (_D_ALLOC_NAMLEN(ent) > 4);
#endif
                found |= 4;

            } else if (strcmp (ent->d_name, "dir") == 0) {
                /* Just a directory */
#ifdef _DIRENT_HAVE_D_TYPE
                assert (ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
                assert (ent->d_namlen == 3);
#endif
#ifdef _D_EXACT_NAMLEN
                assert (_D_EXACT_NAMLEN(ent) == 3);
#endif
#ifdef _D_ALLOC_NAMLEN
                assert (_D_ALLOC_NAMLEN(ent) > 3);
#endif
                found |= 8;

            } else {
                /* Other file */
                fprintf (stderr, "Unexpected file %s\n", ent->d_name);
                abort ();
            }

        }

        /* Make sure that all files were found */
        assert (found == 0xf);

        closedir (dir);
    }

    /* Rewind of directory stream */
    {
        DIR *dir;
        struct dirent *ent;
        int found = 0;

        /* Open directory */
        dir = opendir ("tests/1");
        assert (dir != NULL);

        /* Read entries */
        while ((ent = readdir (dir)) != NULL) {

            /* Check each file */
            if (strcmp (ent->d_name, ".") == 0) {
                /* Directory itself */
                found |= 1;

            } else if (strcmp (ent->d_name, "..") == 0) {
                /* Parent directory */
                found |= 2;

            } else if (strcmp (ent->d_name, "file") == 0) {
                /* Regular file */
                found |= 4;

            } else if (strcmp (ent->d_name, "dir") == 0) {
                /* Just a directory */
                found |= 8;

            } else {
                /* Other file */
                fprintf (stderr, "Unexpected file %s\n", ent->d_name);
                abort ();
            }

        }

        /* Make sure that all files were found */
        assert (found == 0xf);

        /* Rewind stream and read entries again */
        rewinddir (dir);
        found = 0;

        /* Read entries */
        while ((ent = readdir (dir)) != NULL) {

            /* Check each file */
            if (strcmp (ent->d_name, ".") == 0) {
                /* Directory itself */
                found |= 1;

            } else if (strcmp (ent->d_name, "..") == 0) {
                /* Parent directory */
                found |= 2;

            } else if (strcmp (ent->d_name, "file") == 0) {
                /* Regular file */
                found |= 4;

            } else if (strcmp (ent->d_name, "dir") == 0) {
                /* Just a directory */
                found |= 8;

            } else {
                /* Other file */
                fprintf (stderr, "Unexpected file %s\n", ent->d_name);
                abort ();
            }

        }

        /* Make sure that all files were found */
        assert (found == 0xf);

        closedir (dir);
    }

    printf ("OK\n");
    return EXIT_SUCCESS;
}
Beispiel #8
0
static hp_timing_t
__get_clockfreq_via_proc_openprom (void)
{
  hp_timing_t result;
  int obp_fd;

  result = 0;

  obp_fd = __open ("/proc/openprom", O_RDONLY);
  if (obp_fd != -1)
    {
      unsigned long int buf[4096 / sizeof (unsigned long int)];
      struct dirent *dirp = (struct dirent *) buf;
      ssize_t len;

      while ((len = __getdents (obp_fd, (char *) dirp, sizeof (buf))) > 0)
	{
	  struct dirent *this_dirp = dirp;

	  while (len > 0)
	    {
	      char node[strlen ("/proc/openprom/")
			+ _D_ALLOC_NAMLEN (this_dirp)
			+ strlen ("/clock-frequency")];
	      char *prop;
	      int fd;

	      /* Note that
		   strlen("/clock-frequency") > strlen("/device_type")
	      */
	      __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
					 this_dirp->d_name),
			"/device_type");
	      fd = __open (node, O_RDONLY);
	      if (fd != -1)
		{
		  char type_string[128];
		  int ret;

		  ret = __read (fd, type_string, sizeof (type_string));
		  if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
		    {
		      int clkfreq_fd;

		      __stpcpy (prop, "/clock-frequency");
		      clkfreq_fd = __open (node, O_RDONLY);
		      if (clkfreq_fd != -1)
			{
			  if (__read (clkfreq_fd, type_string,
				      sizeof (type_string)) > 0)
			    result = (hp_timing_t)
			      strtoumax (type_string, NULL, 16);
			  __close (clkfreq_fd);
			}
		    }
		  __close (fd);
		}

	      if (result != 0)
		break;

	      len -= this_dirp->d_reclen;
	      this_dirp = (struct dirent *)
		((char *) this_dirp + this_dirp->d_reclen);
	    }
	  if (result != 0)
	    break;
	}
      __close (obp_fd);
    }

  return result;
}
Beispiel #9
0
int
flu_scandir(const char *dir, struct dirent ***namelist,
	   int (*select)(struct dirent *),
	   int (*compar)(struct dirent **, struct dirent **))
{
  DIR *dp = opendir (dir);
  struct dirent **v = NULL;
  size_t vsize = 0, i;
  struct dirent *d;
  int save;

  if (dp == NULL)
    return -1;

  save = errno;
  errno = 0;

  i = 0;
  while ((d = readdir (dp)) != NULL)
    if (select == NULL || (*select) (d))
      {
      size_t dsize;

      if (i == vsize)
        {
          struct dirent **newv;
          if (vsize == 0)
            vsize = 10;
          else
            vsize *= 2;
          newv = (struct dirent **) realloc (v, vsize * sizeof (*v));
          if (newv == NULL)
            {
            lose:
              errno = ENOMEM;
              break;
            }
          v = newv;
        }

#    define _D_EXACT_NAMLEN(d) (strlen ((d)->d_name))
#    define _D_ALLOC_NAMLEN(d) (sizeof (d)->d_name > 1 ? sizeof (d)->d_name : \
                              _D_EXACT_NAMLEN (d) + 1)

      dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
      v[i] = (struct dirent *) malloc (dsize);
      if (v[i] == NULL)
        goto lose;

      memcpy (v[i++], d, dsize);
      }

  if (errno != 0)
    {
      save = errno;
      (void) closedir (dp);
      while (i > 0)
      free (v[--i]);
      free (v);
      errno = save;
      return -1;
    }

  (void) closedir (dp);
  errno = save;

  /* Sort the list if we have a comparison function to sort with.  */
  if (compar) qsort (v, i, sizeof (*v), (int (*)(const void *, const void *))compar);
  *namelist = v;
  return i;
}