| 1 |
/* @(#)osl.c 19.1 (ESO-DMD) 02/25/03 13:56:13 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.IDENTIFICATION osl |
| 32 |
.AUTHOR Michele Peron |
| 33 |
.LANGUAGE C |
| 34 |
.KEYWORDS Directory searching |
| 35 |
.ENVIRONMENT POSIX complaint. |
| 36 |
.VERSION |
| 37 |
|
| 38 |
010514 last modif |
| 39 |
|
| 40 |
---------------------------------------------------------------------*/ |
| 41 |
/* |
| 42 |
* Define _POSIX_SOURCE to indicate |
| 43 |
* that this is a POSIX program |
| 44 |
*/ |
| 45 |
|
| 46 |
|
| 47 |
/* #define _POSIX_SOURCE 1 */ |
| 48 |
|
| 49 |
#include <dirent.h> |
| 50 |
#include <stdio.h> |
| 51 |
|
| 52 |
static DIR *dp; |
| 53 |
static char patmatch[80]; |
| 54 |
|
| 55 |
|
| 56 |
int oslopen(dirname,pattern) |
| 57 |
/*++++++++++++++++ |
| 58 |
.PURPOSE opens one directory |
| 59 |
.RETURNS number of entries in the directory matching the given pattern |
| 60 |
---------*/ |
| 61 |
char *dirname; |
| 62 |
char *pattern; |
| 63 |
|
| 64 |
{ |
| 65 |
int noentries,res; |
| 66 |
struct dirent *dirp; |
| 67 |
|
| 68 |
|
| 69 |
(void) strcpy(patmatch,pattern); |
| 70 |
if ( (dp = opendir(dirname)) == NULL) { |
| 71 |
noentries = -1; |
| 72 |
return(noentries); |
| 73 |
} |
| 74 |
noentries = 0; |
| 75 |
while ( (dirp = readdir(dp)) != NULL) { |
| 76 |
res = amatch(dirp->d_name,pattern); |
| 77 |
if (res == 0) noentries++; |
| 78 |
} |
| 79 |
rewinddir(dp); |
| 80 |
return(noentries); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
struct dirent *oslread() |
| 85 |
/*++++++++++++++++ |
| 86 |
.PURPOSE look for the next entry in the directory matching a given pattern |
| 87 |
.RETURNS returns NULL if not found |
| 88 |
---------*/ |
| 89 |
|
| 90 |
{ |
| 91 |
struct dirent *dirp; |
| 92 |
int res; |
| 93 |
|
| 94 |
|
| 95 |
while ( (dirp = readdir(dp)) != NULL) |
| 96 |
if ( (res = amatch(dirp->d_name,patmatch) ) == 0 ) return (dirp); |
| 97 |
return(NULL); |
| 98 |
} |
| 99 |
|
| 100 |
int oslclose() |
| 101 |
/*++++++++++++++++ |
| 102 |
.PURPOSE closes one directory |
| 103 |
.RETURNS 0 (normal) or -1 (failure) |
| 104 |
---------*/ |
| 105 |
|
| 106 |
{ |
| 107 |
int status; |
| 108 |
|
| 109 |
|
| 110 |
status = closedir(dp); |
| 111 |
return(status); |
| 112 |
} |
| 113 |
|
| 114 |
int amatch(lin,arg) |
| 115 |
/*++++++++++++++++ |
| 116 |
.PURPOSE checks whether lin matches the given pattern arg |
| 117 |
.RETURNS 0 (yes) or 1 (no) |
| 118 |
---------*/ |
| 119 |
char *lin; |
| 120 |
char *arg; |
| 121 |
|
| 122 |
{ |
| 123 |
int i,j; |
| 124 |
|
| 125 |
|
| 126 |
while (*arg) |
| 127 |
{ |
| 128 |
if (!*lin && (*arg != '*' || (*arg == '*' && *(arg+1)))) return(1); |
| 129 |
else if (*arg == '[') |
| 130 |
{ |
| 131 |
arg++; |
| 132 |
while (*arg != ']') { |
| 133 |
if (*lin == *arg) { |
| 134 |
lin++; |
| 135 |
arg+=2; |
| 136 |
return(0); |
| 137 |
} |
| 138 |
else arg++; |
| 139 |
} |
| 140 |
return(1); |
| 141 |
} |
| 142 |
else if (*arg == '?') lin++; |
| 143 |
else if (*arg == '*') |
| 144 |
{ |
| 145 |
arg++; |
| 146 |
while (*lin) |
| 147 |
if (!*arg) return(0); |
| 148 |
else if ((i = amatch(lin,arg)) == 0) return(0); |
| 149 |
else lin++; |
| 150 |
if (i == 1) return(i); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
if (*arg != *lin) return (1); |
| 155 |
else lin++; |
| 156 |
} |
| 157 |
arg++; |
| 158 |
} |
| 159 |
if (*lin) return(1); else return(0); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
int omatch(ch,s,j) |
| 166 |
char *ch,*s; |
| 167 |
int j; |
| 168 |
{ |
| 169 |
while (j--) if (*s++==*ch) return 1; |
| 170 |
return 0; |
| 171 |
} |