#include <NSLSemaphore.h>
#include "zonelist.h"
static NSLSemaphore *sNSLSemaphore = NULL;
int ZIPGetZoneList(int lookupType, char *buffer, int bufferSize, long *actualCount)
{
static u_char zonebuf[ATP_DATA_SIZE+1]; u_char *start, *end, save;
int i, j, n=-1;
UInt16 len;
long entryCount = 0;
Str32 *currStr = (Str32 *) buffer;
long maxCount;
int result = 0;
at_nvestr_t aZone;
if ( actualCount == NULL )
return -2;
if ( sNSLSemaphore == NULL ) {
sNSLSemaphore = new NSLSemaphore(1);
if ( sNSLSemaphore == NULL ) {
fprintf(stderr, "sNSLSemaphore is NULL\n");
return -1;
}
}
maxCount = bufferSize / sizeof(Str32); *actualCount = 0;
i = ZIP_FIRST_ZONE;
do
{
sNSLSemaphore->Wait();
switch( lookupType )
{
case LOOKUP_ALL:
n = zip_getzonelist( ZIP_DEF_INTERFACE, &i, zonebuf, sizeof(zonebuf) );
break;
case LOOKUP_LOCAL:
n = zip_getlocalzones( ZIP_DEF_INTERFACE, &i, zonebuf, sizeof(zonebuf) );
break;
case LOOKUP_CURRENT:
n = zip_getmyzone( ZIP_DEF_INTERFACE, &aZone );
DBGLOG( "zip_getmyzone = %d\n",n );
break;
}
sNSLSemaphore->Signal();
if (n == -1)
{
DBGLOG( "zip_getzonelist failed\n" );
return (-1); }
if ( lookupType == LOOKUP_CURRENT )
{
*actualCount = 1;
strncpy( buffer, (char *)aZone.str, aZone.len );
buffer[aZone.len] = '\0';
DBGLOG( "myzone=%s\n", buffer );
return 0;
}
if ((entryCount + n) > maxCount) {
n = maxCount - entryCount; if (n <= 0) { result = 1; break;
}
}
start = zonebuf;
for (j = 0; j < n; j++)
{
len = *start;
end = start + 1 + len;
save = *end;
*end = 0x00;
if (sprintf((char*) currStr,"%s", (char*)start+1) <= 0 )
{
return (-1);
}
*end = save;
currStr ++;
start = end;
}
entryCount += n;
} while (n != -1 && i != ZIP_NO_MORE_ZONES);
*actualCount = entryCount;
return(result);
}