imapext-2007
diff src/osdep/unix/scandir.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/unix/scandir.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,81 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: Scan directories 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 1 August 1988 1.29 + * Last Edited: 15 September 2006 1.30 + */ 1.31 + 1.32 +/* Emulator for BSD scandir() call 1.33 + * Accepts: directory name 1.34 + * destination pointer of names array 1.35 + * selection function 1.36 + * comparison function 1.37 + * Returns: number of elements in the array or -1 if error 1.38 + */ 1.39 + 1.40 +int scandir (char *dirname,struct direct ***namelist,select_t select, 1.41 + compar_t compar) 1.42 +{ 1.43 + struct direct *p,*d,**names; 1.44 + int nitems; 1.45 + struct stat stb; 1.46 + long nlmax; 1.47 + DIR *dirp = opendir (dirname);/* open directory and get status poop */ 1.48 + if ((!dirp) || (fstat (dirp->dd_fd,&stb) < 0)) return -1; 1.49 + nlmax = stb.st_size / 24; /* guesstimate at number of files */ 1.50 + names = (struct direct **) fs_get (nlmax * sizeof (struct direct *)); 1.51 + nitems = 0; /* initially none found */ 1.52 + while (d = readdir (dirp)) { /* read directory item */ 1.53 + /* matches select criterion? */ 1.54 + if (select && !(*select) (d)) continue; 1.55 + /* get size of direct record for this file */ 1.56 + p = (struct direct *) fs_get (DIR_SIZE (d)); 1.57 + p->d_ino = d->d_ino; /* copy the poop */ 1.58 + strcpy (p->d_name,d->d_name); 1.59 + if (++nitems >= nlmax) { /* if out of space, try bigger guesstimate */ 1.60 + void *s = (void *) names; /* stupid language */ 1.61 + nlmax *= 2; /* double it */ 1.62 + fs_resize ((void **) &s,nlmax * sizeof (struct direct *)); 1.63 + names = (struct direct **) s; 1.64 + } 1.65 + names[nitems - 1] = p; /* store this file there */ 1.66 + } 1.67 + closedir (dirp); /* done with directory */ 1.68 + /* sort if necessary */ 1.69 + if (nitems && compar) qsort (names,nitems,sizeof (struct direct *),compar); 1.70 + *namelist = names; /* return directory */ 1.71 + return nitems; /* and size */ 1.72 +} 1.73 + 1.74 +/* Alphabetic file name comparision 1.75 + * Accepts: first candidate directory entry 1.76 + * second candidate directory entry 1.77 + * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2 1.78 + */ 1.79 + 1.80 +int alphasort (void *d1,void *d2) 1.81 +{ 1.82 + return strcmp ((*(struct direct **) d1)->d_name, 1.83 + (*(struct direct **) d2)->d_name); 1.84 +}