2004-07-10 13:16:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004, Richard Levitte <richard@levitte.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2004-09-23 22:11:39 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2004-07-10 13:16:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <descrip.h>
|
|
|
|
#include <namdef.h>
|
|
|
|
#include <rmsdef.h>
|
|
|
|
#include <libfildef.h>
|
|
|
|
#include <lib$routines.h>
|
|
|
|
#include <strdef.h>
|
|
|
|
#include <str$routines.h>
|
|
|
|
#include <stsdef.h>
|
|
|
|
#ifndef LPDIR_H
|
2015-01-22 03:40:55 +00:00
|
|
|
# include "LPdir.h"
|
2004-07-10 13:16:02 +00:00
|
|
|
#endif
|
2011-03-19 10:58:14 +00:00
|
|
|
#include "vms_rms.h"
|
2004-07-10 13:16:02 +00:00
|
|
|
|
2011-03-19 10:58:14 +00:00
|
|
|
/* Some compiler options hide EVMSERR. */
|
2004-07-11 20:21:19 +00:00
|
|
|
#ifndef EVMSERR
|
2015-01-22 03:40:55 +00:00
|
|
|
# define EVMSERR 65535 /* error for non-translatable VMS errors */
|
2004-07-11 20:21:19 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
struct LP_dir_context_st {
|
|
|
|
unsigned long VMS_context;
|
|
|
|
char filespec[NAMX_MAXRSS + 1];
|
|
|
|
char result[NAMX_MAXRSS + 1];
|
|
|
|
struct dsc$descriptor_d filespec_dsc;
|
|
|
|
struct dsc$descriptor_d result_dsc;
|
2004-07-10 13:16:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
int status;
|
|
|
|
char *p, *r;
|
|
|
|
size_t l;
|
|
|
|
unsigned long flags = 0;
|
2011-03-19 10:58:14 +00:00
|
|
|
|
|
|
|
/* Arrange 32-bit pointer to (copied) string storage, if needed. */
|
|
|
|
#if __INITIAL_POINTER_SIZE == 64
|
|
|
|
# pragma pointer_size save
|
|
|
|
# pragma pointer_size 32
|
2015-01-22 03:40:55 +00:00
|
|
|
char *ctx_filespec_32p;
|
2011-03-19 10:58:14 +00:00
|
|
|
# pragma pointer_size restore
|
2015-01-22 03:40:55 +00:00
|
|
|
char ctx_filespec_32[NAMX_MAXRSS + 1];
|
|
|
|
#endif /* __INITIAL_POINTER_SIZE == 64 */
|
2011-03-19 10:58:14 +00:00
|
|
|
|
2004-07-10 13:16:02 +00:00
|
|
|
#ifdef NAML$C_MAXRSS
|
2015-01-22 03:40:55 +00:00
|
|
|
flags |= LIB$M_FIL_LONG_NAMES;
|
2004-07-10 13:16:02 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ctx == NULL || directory == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
errno = 0;
|
|
|
|
if (*ctx == NULL) {
|
|
|
|
size_t filespeclen = strlen(directory);
|
|
|
|
char *filespec = NULL;
|
|
|
|
|
|
|
|
if (filespeclen == 0) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MUST be a VMS directory specification! Let's estimate if it is. */
|
|
|
|
if (directory[filespeclen - 1] != ']'
|
|
|
|
&& directory[filespeclen - 1] != '>'
|
|
|
|
&& directory[filespeclen - 1] != ':') {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
filespeclen += 4; /* "*.*;" */
|
|
|
|
|
|
|
|
if (filespeclen > NAMX_MAXRSS) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-02 03:10:31 +00:00
|
|
|
*ctx = malloc(sizeof(**ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
if (*ctx == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-02 03:10:31 +00:00
|
|
|
memset(*ctx, '\0', sizeof(**ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
strcpy((*ctx)->filespec, directory);
|
|
|
|
strcat((*ctx)->filespec, "*.*;");
|
2011-03-19 10:58:14 +00:00
|
|
|
|
|
|
|
/* Arrange 32-bit pointer to (copied) string storage, if needed. */
|
|
|
|
#if __INITIAL_POINTER_SIZE == 64
|
|
|
|
# define CTX_FILESPEC ctx_filespec_32p
|
|
|
|
/* Copy the file name to storage with a 32-bit pointer. */
|
|
|
|
ctx_filespec_32p = ctx_filespec_32;
|
2015-01-22 03:40:55 +00:00
|
|
|
strcpy(ctx_filespec_32p, (*ctx)->filespec);
|
|
|
|
#else /* __INITIAL_POINTER_SIZE == 64 */
|
2011-03-19 10:58:14 +00:00
|
|
|
# define CTX_FILESPEC (*ctx)->filespec
|
2015-01-22 03:40:55 +00:00
|
|
|
#endif /* __INITIAL_POINTER_SIZE == 64 [else] */
|
2011-03-19 10:58:14 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
(*ctx)->filespec_dsc.dsc$w_length = filespeclen;
|
|
|
|
(*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
|
|
|
(*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
|
|
|
|
(*ctx)->filespec_dsc.dsc$a_pointer = CTX_FILESPEC;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
(*ctx)->result_dsc.dsc$w_length = 0;
|
|
|
|
(*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
|
|
|
|
(*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
|
|
|
|
(*ctx)->result_dsc.dsc$a_pointer = 0;
|
2004-07-10 13:16:02 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
|
|
|
|
&(*ctx)->VMS_context, 0, 0, 0, &flags);
|
2004-07-10 13:16:02 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (status == RMS$_NMF) {
|
|
|
|
errno = 0;
|
|
|
|
vaxc$errno = status;
|
|
|
|
return NULL;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!$VMS_STATUS_SUCCESS(status)) {
|
|
|
|
errno = EVMSERR;
|
|
|
|
vaxc$errno = status;
|
|
|
|
return NULL;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Quick, cheap and dirty way to discard any device and directory, since
|
|
|
|
* we only want file names
|
|
|
|
*/
|
|
|
|
l = (*ctx)->result_dsc.dsc$w_length;
|
|
|
|
p = (*ctx)->result_dsc.dsc$a_pointer;
|
|
|
|
r = p;
|
|
|
|
for (; *p; p++) {
|
|
|
|
if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */
|
|
|
|
p++;
|
|
|
|
} else if (*p == ':' || *p == '>' || *p == ']') {
|
|
|
|
l -= p + 1 - r;
|
|
|
|
r = p + 1;
|
|
|
|
} else if (*p == ';') {
|
|
|
|
l = p - r;
|
|
|
|
break;
|
|
|
|
}
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
strncpy((*ctx)->result, r, l);
|
|
|
|
(*ctx)->result[l] = '\0';
|
|
|
|
str$free1_dx(&(*ctx)->result_dsc);
|
2004-07-10 13:16:02 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return (*ctx)->result;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LP_find_file_end(LP_DIR_CTX **ctx)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ctx != NULL && *ctx != NULL) {
|
|
|
|
int status = lib$find_file_end(&(*ctx)->VMS_context);
|
|
|
|
|
|
|
|
free(*ctx);
|
|
|
|
|
|
|
|
if (!$VMS_STATUS_SUCCESS(status)) {
|
|
|
|
errno = EVMSERR;
|
|
|
|
vaxc$errno = status;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
2004-07-10 13:16:02 +00:00
|
|
|
}
|