Article 126498 of comp.os.vms:
Path: nntpd.lkg.dec.com!pa.dec.com!decwrl!sunsite.doc.ic.ac.uk!plug.news.pipex.net!pipex!tank.news.pipex.net!pipex!oleane!jussieu.fr!univ-lyon1.fr!swidir.switch.ch!scsing.switch.ch!news.belwue.de!news.dfn.de!news.dkrz.de!newsserver.rrzn.uni-hannover.de!tubsibr!news
From: meyer@ifn.ing.tu-bs.de
Newsgroups: comp.os.vms
Subject: RE: readdir on VMS
Date: Sun,  6 Aug 95 02:05:01 GMT
Organization: Inst.f.Nachrichtentechnik, TU Braunschweig, Germany
Lines: 177
Distribution: world
Message-ID: <400tbm$mlq@ra.ibr.cs.tu-bs.de>
References: ,<3vtivl$bob@holmes.ee.ubc.ca>
NNTP-Posting-Host: ifn.ing.tu-bs.de

In Article <3vtivl$bob@holmes.ee.ubc.ca>
beng@canucks.ee.ubc.ca (Brian Eng (N.B.S)) writes:
>
>Hi, 
>I'm looking for the dir.[c,h] library for VMS which is
>similar to the UNIX version.  The function I need
>is the readdir function.  Does anyone know where
>I can find this??

	Since this module is really small I will (exceptionally) append
	the code here. I used it for a long time (just added the comments
	today :-). Works with VAXC and DECC.

--------------------------- begin DIR.C ------------------------
/*	dir.c
 *				5-Aug-1995	IfN/Mey
 *
 *	Derived from a module from P Kay's UNIXSHR.
 *
 *	Slightly modified by Eckart Meyer, meyer@ifn.ing.tu-bs.de
 *+
 * opendir, readdir, seekdir and closedir.
 *-
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dir.h"
#include <lib$routines.h>
#include <ssdef.h>
#include <rmsdef.h>
#include <errno.h>

#ifndef __UNISTD_LOADED
char *getcwd (char *__buffer, unsigned int __size);
int chdir   (char *__dir_spec);
#endif

/*
 * readdir() should return files relative to the path specified by
 * opendir(). For this reason we have to chdir() to that directory
 * before calling SYS$FIND_FILE. When called often and with large
 * directories this decreases performance significantly.
 *
 * If you are sure that chdir() is not called from other places between
 * calls to opendir() and closedir() and that your application does not
 * care about a default directory change, you may define FAST_READDIR.
 * This way, chdir() is only called once in opendir() and again in closedir()
 * to change the default directory back to the saved path.
 */

/* #define FAST_READDIR	/**/


static DIR directory;
static struct direct dp;
static char *wild = "*.*;0";
static char path[256];
static char resfile[256];
static char save_path[256];

int DEBUGLEVEL = 0;
#define DEBUG(level,body) ((DEBUGLEVEL>=(level))?(printf body):0)


DIR *opendir(filename)
char *filename;
{
  DEBUG (4,("opendir: \"%s\"\n",filename));
  if (strlen(filename) > 250) {
    errno = ENAMETOOLONG;
    return(NULL);
  }
  strcpy(path,filename);
  directory.dd_loc = 0;
#ifdef FAST_READDIR
  getcwd(save_path,256);
  chdir(path);
#endif
  return(&directory);
}

struct direct *readdir(dirp)
DIR *dirp;
{
  int st;
  char *p, *q;
  struct {
     int length;
     char *ptr;
  } filedesc, res_filedesc;
  filedesc.length = strlen(wild);
  filedesc.ptr = wild;
  res_filedesc.length = 256;
  res_filedesc.ptr = resfile;
#ifndef FAST_READDIR
  getcwd(save_path,256);
  chdir(path);
#endif
  DEBUG (8,("readdir: saved path = \"%s\", path = \"%s\"\n",save_path,path)); /**/
  st = lib$find_file(&filedesc,&res_filedesc,&directory.dd_loc,0,0,0,0);
#ifndef FAST_READDIR
  chdir(save_path);
#endif
  if (st == SS$_NORMAL || st == RMS$_NORMAL) {
     for (p = res_filedesc.ptr;*p != ']';p++);
     for (q = dp.d_name, ++p; *p != ';'; q++ ,p++){
       *q = *p;
       if ( *q >= 'A' && *q <= 'Z') *q = *q -'A'+'a';
     }
     *q = '\0';
     dp.d_namlen = strlen(dp.d_name);
     DEBUG (3,("readdir: returns \"%s\"\n",dp.d_name));
     return(&dp);
  }
  if (st == RMS$_NMF) return(NULL);	/* don't touch errno if no more files */
  errno = ENOENT;
  if (st == SS$_NOPRIV) errno = EACCES;
  return(NULL);
}

closedir(dirp)
DIR *dirp;
{
  lib$find_file_end(&directory.dd_loc);
#ifdef FAST_READDIR
  chdir(save_path);
#endif
}

rewinddir(dirp)
DIR *dirp;
{
/* sorry no time to do this properly */
}
--------------------------- begin DIR.H ------------------------
/* <sys/dir.h> */
#define MAXNAMLEN 255

typedef unsigned short u_short;
typedef unsigned long u_long;

struct direct {
 u_long d_fileno;
 u_short d_reclen;
 u_short d_namlen;
 char d_name[MAXNAMLEN + 1];
};
#define d_ino d_fileno
typedef struct _dirdesc {
 int dd_fd;
 long dd_loc;
 long dd_size;
 long dd_bbase;
 long dd_entno;
 long dd_bsize;
 char *dd_buf;
} DIR;
extern DIR *opendir();
extern struct direct *readdir();
extern long telldir();
extern seekdir();
#define reinddir(dirp) seekdir((dirp), (long)0)
extern closedir();

------------------------------- end ----------------------------

-----------------------------------------------------------------------------
Eckart Meyer                                     Address:  Schleinitzstr.  23
Institute for Telecommunication                            38092 Braunschweig
Technical University of Braunschweig                                  Germany
                                                 Phone:      +49 531 391 2454
E-Mail:   meyer@ifn.ing.tu-bs.de                 FAX:        +49 531 391 5192
VMSmail:  PSI%26245050351130::MEYER (DATEX-P)
-----------------------------------------------------------------------------



