~ubuntu-branches/debian/squeeze/alpine/squeeze

« back to all changes in this revision

Viewing changes to imap/src/osdep/unix/scandir.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:     Scan 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:        1 August 1988
 
26
 * Last Edited: 15 September 2006
 
27
 */
 
28
 
 
29
/* Emulator for BSD scandir() call
 
30
 * Accepts: directory name
 
31
 *          destination pointer of names array
 
32
 *          selection function
 
33
 *          comparison function
 
34
 * Returns: number of elements in the array or -1 if error
 
35
 */
 
36
 
 
37
int scandir (char *dirname,struct direct ***namelist,select_t select,
 
38
             compar_t compar)
 
39
{
 
40
  struct direct *p,*d,**names;
 
41
  int nitems;
 
42
  struct stat stb;
 
43
  long nlmax;
 
44
  DIR *dirp = opendir (dirname);/* open directory and get status poop */
 
45
  if ((!dirp) || (fstat (dirp->dd_fd,&stb) < 0)) return -1;
 
46
  nlmax = stb.st_size / 24;     /* guesstimate at number of files */
 
47
  names = (struct direct **) fs_get (nlmax * sizeof (struct direct *));
 
48
  nitems = 0;                   /* initially none found */
 
49
  while (d = readdir (dirp)) {  /* read directory item */
 
50
                                /* matches select criterion? */
 
51
    if (select && !(*select) (d)) continue;
 
52
                                /* get size of direct record for this file */
 
53
    p = (struct direct *) fs_get (DIR_SIZE (d));
 
54
    p->d_ino = d->d_ino;        /* copy the poop */
 
55
    strcpy (p->d_name,d->d_name);
 
56
    if (++nitems >= nlmax) {    /* if out of space, try bigger guesstimate */
 
57
      void *s = (void *) names; /* stupid language */
 
58
      nlmax *= 2;               /* double it */
 
59
      fs_resize ((void **) &s,nlmax * sizeof (struct direct *));
 
60
      names = (struct direct **) s;
 
61
    }
 
62
    names[nitems - 1] = p;      /* store this file there */
 
63
  }
 
64
  closedir (dirp);              /* done with directory */
 
65
                                /* sort if necessary */
 
66
  if (nitems && compar) qsort (names,nitems,sizeof (struct direct *),compar);
 
67
  *namelist = names;            /* return directory */
 
68
  return nitems;                /* and size */
 
69
}
 
70
 
 
71
/* Alphabetic file name comparision
 
72
 * Accepts: first candidate directory entry
 
73
 *          second candidate directory entry
 
74
 * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
 
75
 */
 
76
 
 
77
int alphasort (void *d1,void *d2)
 
78
{
 
79
  return strcmp ((*(struct direct **) d1)->d_name,
 
80
                 (*(struct direct **) d2)->d_name);
 
81
}