| 1 |
/* @(#)osl.c 19.1 (ES0-DMD) 02/25/03 13:55:50 */ |
| 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 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding 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 |
---------------------------------------------------------------------*/ |
| 37 |
/* |
| 38 |
* Define _POSIX_SOURCE to indicate |
| 39 |
* that this is a POSIX program |
| 40 |
* With _POSIX_SOURCE defined fails in ULTRIX V4.3 |
| 41 |
#define _POSIX_SOURCE 1 |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <sys/types.h> /* Needed for Convex V10.0.2 */ |
| 45 |
#include <dirent.h> |
| 46 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 47 |
#include <stdio.h> |
| 48 |
static DIR *dp; |
| 49 |
static char patmatch[80]; |
| 50 |
|
| 51 |
int oslopen(dirname,pattern) |
| 52 |
/*++++++++++++++++ |
| 53 |
.PURPOSE opens one directory |
| 54 |
.RETURNS number of entries in the directory matching the given pattern |
| 55 |
---------*/ |
| 56 |
char *dirname; |
| 57 |
char *pattern; |
| 58 |
{ |
| 59 |
int noentries,res; |
| 60 |
struct dirent *dirp; |
| 61 |
strcpy(patmatch,pattern); |
| 62 |
if ( (dp = opendir(dirname)) == NULL) { |
| 63 |
noentries = -1; |
| 64 |
return(noentries); |
| 65 |
} |
| 66 |
noentries = 0; |
| 67 |
while ( (dirp = readdir(dp)) != NULL) { |
| 68 |
res = amatch(dirp->d_name,pattern); |
| 69 |
if (res == 0) noentries++; |
| 70 |
} |
| 71 |
rewinddir(dp); |
| 72 |
return(noentries); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
struct dirent *oslread() |
| 77 |
/*++++++++++++++++ |
| 78 |
.PURPOSE look for the next entry in the directory matching a given pattern |
| 79 |
.RETURNS returns NULL if not found |
| 80 |
---------*/ |
| 81 |
{ |
| 82 |
struct dirent *dirp; |
| 83 |
|
| 84 |
while ( (dirp = readdir(dp)) != NULL) |
| 85 |
if ( amatch(dirp->d_name,patmatch) == 0 ) return(dirp); |
| 86 |
return(NULL); |
| 87 |
} |
| 88 |
|
| 89 |
int oslclose() |
| 90 |
/*++++++++++++++++ |
| 91 |
.PURPOSE closes one directory |
| 92 |
.RETURNS 0 (normal) or -1 (failure) |
| 93 |
---------*/ |
| 94 |
{ |
| 95 |
int status; |
| 96 |
status = closedir(dp); |
| 97 |
return(status); |
| 98 |
} |
| 99 |
|
| 100 |
int amatch(lin,arg) |
| 101 |
/*++++++++++++++++ |
| 102 |
.PURPOSE checks whether lin matches the given pattern arg |
| 103 |
.RETURNS 0 (yes) or 1 (no) |
| 104 |
---------*/ |
| 105 |
char *lin; |
| 106 |
char *arg; |
| 107 |
{ |
| 108 |
int i; |
| 109 |
while (*arg) { |
| 110 |
if (!*lin && (*arg != '*' || (*arg == '*' && *(arg+1)))) return(1); |
| 111 |
else if (!*lin && *arg == '*') return(0); |
| 112 |
else if (*arg == '[') { |
| 113 |
arg++; |
| 114 |
while (*arg != ']') { |
| 115 |
if (*lin == *arg) { |
| 116 |
lin++; |
| 117 |
arg+=2; |
| 118 |
return(0); |
| 119 |
} |
| 120 |
else arg++; |
| 121 |
} |
| 122 |
return(1); |
| 123 |
} |
| 124 |
else if (*arg == '?') lin++; |
| 125 |
else if (*arg == '*') { |
| 126 |
arg++; |
| 127 |
while (*lin) |
| 128 |
if (!*arg) return(0); |
| 129 |
else if ((i = amatch(lin,arg)) == 0) return(0); |
| 130 |
else lin++; |
| 131 |
if (i == 1) return(i); |
| 132 |
} |
| 133 |
else { |
| 134 |
if (*arg != *lin) return (1); |
| 135 |
else lin++; |
| 136 |
} |
| 137 |
arg++; |
| 138 |
} |
| 139 |
if (*lin) return(1); else return(0); |
| 140 |
} |
| 141 |
|
| 142 |
int omatch(ch,s,j) |
| 143 |
char *ch,*s; |
| 144 |
int j; |
| 145 |
{ |
| 146 |
while (j--) if (*s++==*ch) return(1); |
| 147 |
return(0); |
| 148 |
} |