home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / goo / vms_directory.c < prev    next >
C/C++ Source or Header  |  1998-11-27  |  5KB  |  215 lines

  1. /*
  2.  *    DIRECTORY.C - VMS emulation routines for UNIX Directory
  3.  *                  callable routines
  4.  *
  5.  *    Author:        Patrick L. Mahan
  6.  *    Location:    TGV, Inc
  7.  *    Date:        19-November-1991
  8.  *
  9.  *    Purpose:    Provides emulation of the BSD directory routines
  10.  *            which are used by some of the X11 R4 release
  11.  *            software.
  12.  *
  13.  *    Side effects:    This is only a partial emulation.  Not all of
  14.  *            the required information is passed to the user.
  15.  *
  16.  *    Modification History
  17.  *
  18.  *    Date        | Who    | Version    | History
  19.  *    ------------+-----------+---------------+----------------------------
  20.  *    19-Nov-1991 | PLM    | 1.0        | First Write
  21.  *    20-Apr-1992 | PLM    | 1.1        | Added validation check for
  22.  *                |        |        | for the directory
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <rmsdef.h>
  29. #include <descrip.h>
  30. #include <lib$routines.h>
  31. #include "vms_dirent.h"
  32.  
  33. #define    NOWILD        0x00000001
  34. #define MULTIPLE    0x00000002
  35.  
  36. static unsigned long context = 0;
  37.  
  38. static struct dsc$descriptor_s *create_descriptor ( name )
  39. char *name;
  40. {
  41.    struct dsc$descriptor_s *retdescrip;
  42.  
  43.    retdescrip = (struct dsc$descriptor_s *)calloc(1, sizeof(struct dsc$descriptor_s));
  44.  
  45.    if (retdescrip == NULL) return ((struct dsc$descriptor_s *)NULL);
  46.  
  47.    retdescrip->dsc$b_dtype    = DSC$K_DTYPE_T;
  48.    retdescrip->dsc$b_class    = DSC$K_CLASS_S;
  49.    retdescrip->dsc$w_length    = strlen(name);
  50.    retdescrip->dsc$a_pointer    = name;
  51.  
  52.    return (retdescrip);
  53. }
  54.  
  55. static int Check_Directory( dirname )
  56. char *dirname;
  57. {
  58.     static char *tmpdir, *cp;
  59.     FILE *tfp;
  60.     int status;
  61.  
  62.     status = 1;
  63.  
  64.     tmpdir = calloc(strlen(dirname)+15,sizeof(char));
  65.  
  66.     strcpy(tmpdir, dirname);
  67.  
  68.     cp = strrchr(tmpdir, '.');
  69.  
  70.     if (cp != NULL) {
  71.         *cp = ']';
  72.         cp = strrchr(tmpdir, ']');
  73.         *cp = '.';
  74.         strcat(tmpdir, "dir");
  75.     }
  76.     else {
  77.         char *tmp1;
  78.         tmp1 = calloc(strlen(dirname)+1,sizeof(char));
  79.         cp = strchr(tmpdir, '[');
  80.         cp++;
  81.         strcpy(tmp1, cp);
  82.         cp = strrchr(tmp1, ']');
  83.         *cp = '\0';
  84.         cp = strchr(tmpdir, '[');
  85.         cp++;
  86.         *cp = '\0';
  87.         strcat(tmpdir, "000000]");
  88.         strcat(tmpdir, tmp1);
  89.         strcat(tmpdir, ".dir");
  90.     }
  91.  
  92.     tfp = fopen(tmpdir, "r");
  93.  
  94.     if (tfp == NULL) status = 0;
  95.  
  96.     fclose(tfp);
  97.  
  98.     return (status);
  99. }
  100.  
  101. DIR *opendir( dirname )
  102. char *dirname;
  103. {
  104.    DIR *retdir;
  105.    struct dsc$descriptor_s filedescriptor;
  106.    char *filepathname;
  107.  
  108.    retdir = (DIR *) calloc(1, sizeof(DIR));
  109.  
  110.    if (retdir == NULL) return ((DIR *)NULL);
  111.  
  112.    if (!Check_Directory(dirname)) return ((DIR *)NULL);
  113.  
  114.    filepathname = (char *)calloc(256, sizeof(char));
  115.  
  116.    strcpy(filepathname, dirname);
  117.    strcat(filepathname, "*.*.*");
  118.  
  119.    retdir->dd_fd = (unsigned long) create_descriptor(filepathname);
  120.    retdir->dd_loc = 0;
  121.    retdir->dd_size = strlen(filepathname);
  122.    retdir->dd_bsize = 0;
  123.    retdir->dd_off = 0;
  124.    retdir->dd_buf = filepathname;
  125.  
  126.    return (retdir);
  127. }
  128.  
  129. struct dirent *readdir( dirp )
  130. DIR *dirp;
  131. {
  132.    static struct dirent *retdirent;
  133.    struct dsc$descriptor_s retfilenamedesc;
  134.    struct dsc$descriptor_s searchpathdesc = *((struct dsc$descriptor_s *)dirp->dd_fd);
  135.    char retfilename[256];
  136.    char *sp;
  137.    unsigned long istatus;
  138.    unsigned long rms_status;
  139.    unsigned long flags;
  140.  
  141.    retdirent = (struct dirent *)NULL;
  142.  
  143.    flags = MULTIPLE;
  144.  
  145.    retfilenamedesc.dsc$b_dtype    = DSC$K_DTYPE_T;
  146.    retfilenamedesc.dsc$b_class    = DSC$K_CLASS_S;
  147.    retfilenamedesc.dsc$w_length    = 255;
  148.    retfilenamedesc.dsc$a_pointer= retfilename;
  149.  
  150.    istatus = lib$find_file (&searchpathdesc,
  151.                             &retfilenamedesc,
  152.                             &dirp->dd_loc,
  153.                             0, 0,
  154.                             &rms_status,
  155.                             &flags);
  156.  
  157.    if (!(istatus & 1) && (istatus != RMS$_NMF) && (istatus != RMS$_FNF))
  158.    {
  159.       lib$signal (istatus);
  160.       return (retdirent);
  161.    }
  162.    else if ((istatus == RMS$_NMF) || (istatus == RMS$_FNF))
  163.       return (retdirent);
  164.  
  165.    retfilename[retfilenamedesc.dsc$w_length] = '\0';
  166.  
  167.    sp = strchr(retfilename, ' ');
  168.    if (sp != NULL) *sp = '\0';
  169.  
  170.    sp = strrchr(retfilename, ']');
  171.    if (sp != NULL)
  172.       sp++;
  173.    else
  174.       sp = retfilename;
  175.  
  176.    retdirent = (struct dirent *)calloc(1, sizeof(struct dirent));
  177.  
  178.    strcpy(retdirent->d_name, sp);
  179.    retdirent->d_namlen = strlen(sp);
  180.    retdirent->d_fileno = 0;
  181.    retdirent->d_off = 0;
  182.    retdirent->d_reclen = DIRSIZ(retdirent);
  183.  
  184.    return (retdirent);
  185. }
  186.  
  187. long telldir( dirp )
  188. DIR *dirp;
  189. {
  190.    return(0);
  191. }
  192.  
  193. void seekdir( dirp, loc )
  194. DIR *dirp;
  195. int loc;
  196. {
  197.    return;
  198. }
  199.  
  200. void rewinddir( dirp )
  201. DIR *dirp;
  202. {
  203.    lib$find_file_end (&dirp->dd_loc);
  204. }
  205.  
  206. void closedir( dirp )
  207. DIR *dirp;
  208. {
  209.    lib$find_file_end (&dirp->dd_loc);
  210.  
  211.    cfree ((void *) dirp->dd_fd);
  212.    cfree (dirp->dd_buf);
  213.    cfree (dirp);
  214. }
  215.