[3ae31e9] | 1 | #include "dos.h"
|
---|
| 2 | #include "errno.h"
|
---|
| 3 | #include "dir.h"
|
---|
| 4 |
|
---|
| 5 | #define NULL (char *)0
|
---|
| 6 |
|
---|
| 7 | static struct direct buffer;
|
---|
| 8 | extern int errno;
|
---|
| 9 |
|
---|
| 10 | extern char *calloc(), *malloc(), *realloc();
|
---|
| 11 |
|
---|
| 12 | scandir(dirname, namelist, select, compar)
|
---|
| 13 | char *dirname;
|
---|
| 14 | struct direct *(*namelist[]);
|
---|
| 15 | int (*select)();
|
---|
| 16 | int (*compar)();
|
---|
| 17 | {
|
---|
| 18 | register struct direct **names;
|
---|
| 19 | register int dirno;
|
---|
| 20 | union REGS sregs, oregs;
|
---|
| 21 | struct SREGS segregs;
|
---|
| 22 | char *ptr, *paths;
|
---|
| 23 |
|
---|
| 24 | names = (struct direct **)calloc(1, sizeof(struct direct *));
|
---|
| 25 |
|
---|
| 26 | paths = calloc(128, 1);
|
---|
| 27 |
|
---|
| 28 | if(names == (struct direct **)0 || paths == NULL) {
|
---|
| 29 |
|
---|
| 30 | errno = ENOMEM;
|
---|
| 31 | return(-1);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | strcpy(paths, dirname);
|
---|
| 35 |
|
---|
| 36 | ptr = &paths[strlen(paths) - 1];
|
---|
| 37 |
|
---|
| 38 | if(*ptr == '/' || *ptr == '\\')
|
---|
| 39 | *ptr = '\0';
|
---|
| 40 |
|
---|
| 41 | strcat(paths, "/*.*");
|
---|
| 42 |
|
---|
| 43 | /* |
---|
| 44 |
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | segread(&segregs); /* set up segment registers */
|
---|
| 48 |
|
---|
| 49 | ptr = (char *)&buffer;
|
---|
| 50 |
|
---|
| 51 | sregs.h.ah = 0x1a; /* set DTA to buffer */
|
---|
| 52 | sregs.x.dx = FP_OFF(ptr); /* offset */
|
---|
| 53 |
|
---|
| 54 | #ifdef M_I86LM
|
---|
| 55 | segregs.ds = FP_SEG(ptr); /* pointer in large model */
|
---|
| 56 | #endif M_I86LM
|
---|
| 57 |
|
---|
| 58 | intdosx(&sregs, &oregs, &segregs);
|
---|
| 59 |
|
---|
| 60 | sregs.x.ax = 0x4e00; /* search for first */
|
---|
| 61 | sregs.x.cx = 0x1f; /* include all attributes */
|
---|
| 62 | sregs.x.dx = FP_OFF(paths); /* offset to path */
|
---|
| 63 |
|
---|
| 64 | #ifdef M_I86LM
|
---|
| 65 | segregs.ds = FP_SEG(paths); /* segment for large model */
|
---|
| 66 | #endif M_I86LM
|
---|
| 67 |
|
---|
| 68 | intdosx(&sregs, &oregs, &segregs);
|
---|
| 69 |
|
---|
| 70 | if(oregs.x.cflag) {
|
---|
| 71 |
|
---|
| 72 | errno = ENOTDIR;
|
---|
| 73 | return(-1);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | sregs.x.ax = 0x4f00; /* search for next */
|
---|
| 77 |
|
---|
| 78 | /* |
---|
| 79 |
|
---|
| 80 | */
|
---|
| 81 | for(dirno = 0; oregs.x.cflag == 0; intdosx(&sregs, &oregs, &segregs)) {
|
---|
| 82 |
|
---|
| 83 | for(ptr = buffer.d_name; *ptr; ptr++)
|
---|
| 84 | *ptr = tolower(*ptr);
|
---|
| 85 |
|
---|
| 86 | if(select == (int (*)())0 || (*select)(&buffer)) {
|
---|
| 87 |
|
---|
| 88 | names = (struct direct **)realloc((char *)names,
|
---|
| 89 | (dirno + 2)*sizeof(struct direct *));
|
---|
| 90 |
|
---|
| 91 | if(names == (struct direct **)0) {
|
---|
| 92 |
|
---|
| 93 | errno = ENOMEM;
|
---|
| 94 | return(-1);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | names[dirno] = (struct direct *)calloc(1,
|
---|
| 98 | sizeof(struct direct));
|
---|
| 99 |
|
---|
| 100 | if(names[dirno] == (struct direct *)0) {
|
---|
| 101 |
|
---|
| 102 | errno = ENOMEM;
|
---|
| 103 | return(-1);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | *names[dirno] = buffer;
|
---|
| 107 | names[++dirno] = (struct direct *)0;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if(compar != (int (*)())0)
|
---|
| 112 | qsort((char *)names, dirno, sizeof(char *), compar);
|
---|
| 113 |
|
---|
| 114 | *namelist = names;
|
---|
| 115 | free(paths); /* free temp space */
|
---|
| 116 | return(dirno);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /* |
---|
| 120 |
|
---|
| 121 | */
|
---|
| 122 |
|
---|
| 123 | freedir(dirs)
|
---|
| 124 | register struct direct **dirs;
|
---|
| 125 | {
|
---|
| 126 | register int ii;
|
---|
| 127 |
|
---|
| 128 | if(dirs == (struct direct **)0)
|
---|
| 129 | return(-1);
|
---|
| 130 |
|
---|
| 131 | for(ii = 0; dirs[ii] != (struct direct *)0; ii++)
|
---|
| 132 | free(dirs[ii]);
|
---|
| 133 |
|
---|
| 134 | free(dirs);
|
---|
| 135 | return(0);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | int
|
---|
| 139 | alphasort(dirptr1, dirptr2)
|
---|
| 140 | struct direct **dirptr1, **dirptr2;
|
---|
| 141 | {
|
---|
| 142 | return(strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
|
---|
| 143 | }
|
---|