~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to imap/src/osdep/unix/opendir.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ========================================================================
 
2
 * Copyright 1988-2006 University of Washington
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * 
 
11
 * ========================================================================
 
12
 */
 
13
 
 
14
/*
 
15
 * Program:     Read directories
 
16
 *
 
17
 * Author:      Mark Crispin
 
18
 *              Networks and Distributed Computing
 
19
 *              Computing & Communications
 
20
 *              University of Washington
 
21
 *              Administration Building, AG-44
 
22
 *              Seattle, WA  98195
 
23
 *              Internet: MRC@CAC.Washington.EDU
 
24
 *
 
25
 * Date:        16 December 1993
 
26
 * Last Edited: 30 August 2006
 
27
 */
 
28
 
 
29
/* Emulator for BSD opendir() call
 
30
 * Accepts: directory name
 
31
 * Returns: directory structure pointer
 
32
 */
 
33
 
 
34
DIR *opendir (char *name)
 
35
{
 
36
  DIR *d = NIL;
 
37
  struct stat sbuf;
 
38
  int fd = open (name,O_RDONLY,NIL);
 
39
  errno = ENOTDIR;              /* default error is bogus directory */
 
40
  if ((fd >= 0) && !(fstat (fd,&sbuf)) && ((sbuf.st_mode&S_IFMT) == S_IFDIR)) {
 
41
    d = (DIR *) fs_get (sizeof (DIR));
 
42
                                /* initialize structure */
 
43
    d->dd_loc = 0;
 
44
    read (fd,d->dd_buf = (char *) fs_get (sbuf.st_size),
 
45
          d->dd_size = sbuf.st_size);
 
46
  }
 
47
  else if (d) fs_give ((void **) &d);
 
48
  if (fd >= 0) close (fd);
 
49
  return d;
 
50
}
 
51
 
 
52
 
 
53
/* Emulator for BSD closedir() call
 
54
 * Accepts: directory structure pointer
 
55
 */
 
56
 
 
57
int closedir (DIR *d)
 
58
{
 
59
                                /* free storage */
 
60
  fs_give ((void **) &(d->dd_buf));
 
61
  fs_give ((void **) &d);
 
62
  return NIL;                   /* return */
 
63
}
 
64
 
 
65
 
 
66
/* Emulator for BSD readdir() call
 
67
 * Accepts: directory structure pointer
 
68
 */
 
69
 
 
70
struct direct *readdir (DIR *d)
 
71
{
 
72
                                /* loop through directory */
 
73
  while (d->dd_loc < d->dd_size) {
 
74
    struct direct *dp = (struct direct *) (d->dd_buf + d->dd_loc);
 
75
    d->dd_loc += sizeof (struct direct);
 
76
    if (dp->d_ino) return dp;   /* if have a good entry return it */
 
77
  }
 
78
  return NIL;                   /* all done */
 
79
}