|
| 1 | +/* |
| 2 | + * $Header: /cvsroot/pgsql/src/port/Attic/opendir.c,v 1.1 2003/05/09 01:16:29 momjian Exp $ |
| 3 | + * |
| 4 | + * Copyright (c) 2003 SRA, Inc. |
| 5 | + * Copyright (c) 2003 SKC, Inc. |
| 6 | + * |
| 7 | + * Permission to use, copy, modify, and distribute this software and |
| 8 | + * its documentation for any purpose, without fee, and without a |
| 9 | + * written agreement is hereby granted, provided that the above |
| 10 | + * copyright notice and this paragraph and the following two |
| 11 | + * paragraphs appear in all copies. |
| 12 | + * |
| 13 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, |
| 14 | + * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING |
| 15 | + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS |
| 16 | + * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED |
| 17 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | + * |
| 19 | + * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS |
| 22 | + * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, |
| 23 | + * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | + */ |
| 25 | + |
| 26 | +#include"postgres.h" |
| 27 | + |
| 28 | +#include<stddef.h> |
| 29 | +#include<sys/types.h> |
| 30 | +#include<windows.h> |
| 31 | + |
| 32 | +#include"dirent.h" |
| 33 | + |
| 34 | +DIR* |
| 35 | +opendir(constchar*dir) |
| 36 | +{ |
| 37 | +DIR*dp; |
| 38 | +char*findspec; |
| 39 | +HANDLEhandle; |
| 40 | +size_tdirlen; |
| 41 | + |
| 42 | +dirlen=strlen(dir); |
| 43 | +findspec=palloc(dirlen+2+1); |
| 44 | +if (findspec==NULL) |
| 45 | +returnNULL; |
| 46 | + |
| 47 | +if (dirlen==0) |
| 48 | +strcpy(findspec,"*"); |
| 49 | +elseif (isalpha(dir[0])&&dir[1]==':'&&dir[2]=='\0') |
| 50 | +sprintf(findspec,"%s*",dir); |
| 51 | +elseif (dir[dirlen-1]=='/'||dir[dirlen-1]=='\\') |
| 52 | +sprintf(findspec,"%s*",dir); |
| 53 | +else |
| 54 | +sprintf(findspec,"%s\\*",dir); |
| 55 | + |
| 56 | +dp= (DIR*)palloc(sizeof(DIR)); |
| 57 | +if (dp==NULL) |
| 58 | +{ |
| 59 | +pfree(findspec); |
| 60 | +errno=ENOMEM; |
| 61 | +returnNULL; |
| 62 | +} |
| 63 | + |
| 64 | +dp->offset=0; |
| 65 | +dp->finished=0; |
| 66 | +dp->dir=pstrdup(dir); |
| 67 | +if (dp->dir==NULL) |
| 68 | +{ |
| 69 | +pfree(dp); |
| 70 | +pfree(findspec); |
| 71 | +errno=ENOMEM; |
| 72 | +returnNULL; |
| 73 | +} |
| 74 | + |
| 75 | +handle=FindFirstFile(findspec,&(dp->finddata)); |
| 76 | +if (handle==INVALID_HANDLE_VALUE) |
| 77 | +{ |
| 78 | +pfree(dp->dir); |
| 79 | +pfree(dp); |
| 80 | +pfree(findspec); |
| 81 | +errno=ENOENT; |
| 82 | +returnNULL; |
| 83 | +} |
| 84 | +dp->handle=handle; |
| 85 | + |
| 86 | +pfree(findspec); |
| 87 | +returndp; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +structdirent* |
| 92 | +readdir(DIR*dp) |
| 93 | +{ |
| 94 | +if (dp==NULL||dp->finished) |
| 95 | +returnNULL; |
| 96 | + |
| 97 | +if (dp->offset!=0) |
| 98 | +{ |
| 99 | +if (FindNextFile(dp->handle,&(dp->finddata))==0) |
| 100 | +{ |
| 101 | +dp->finished=1; |
| 102 | +returnNULL; |
| 103 | +} |
| 104 | +} |
| 105 | +dp->offset++; |
| 106 | + |
| 107 | +strncpy(dp->ent.d_name,dp->finddata.cFileName,MAX_PATH); |
| 108 | +dp->ent.d_ino=1; |
| 109 | + |
| 110 | +return&(dp->ent); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +int |
| 115 | +closedir(DIR*dp) |
| 116 | +{ |
| 117 | +FindClose(dp->handle); |
| 118 | +pfree(dp->dir); |
| 119 | +pfree(dp); |
| 120 | + |
| 121 | +return0; |
| 122 | +} |