home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / vmsisode / vmsisode80_tar.Z / vmsisode80_tar / sockit / source / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-06  |  4.1 KB  |  169 lines

  1. #include <stdio.h>
  2. #include <ssdef.h>
  3. #include <dirent.h>
  4.  
  5. static lowercopy();
  6. char *unix2vms();
  7. char *malloc();
  8. static int debug = 0;
  9. int all_versions = 0;
  10.  
  11. DIR *opendir(filename)
  12. char *filename;
  13. {
  14.    int status;
  15.    DIR *dirp;
  16.    char *p, q[256];
  17. if (debug) fprintf(stderr,"opendir called %s\n",filename);
  18.    dirp = (DIR *)malloc(sizeof(DIR));
  19.    dirp->fab = (struct FAB *)malloc(sizeof(struct FAB));
  20.    dirp->nam = (struct NAM *)malloc(sizeof(struct NAM));
  21.    dirp->buffer = malloc(NAM$C_MAXRSS);
  22.    dirp->output = malloc(NAM$C_MAXRSS);
  23.    dirp->exp_output = malloc(NAM$C_MAXRSS);
  24.    dirp->ddirent = (struct dirent *)malloc(sizeof(struct dirent));
  25.    bzero(dirp->fab,sizeof(struct FAB));
  26.    bzero(dirp->nam,sizeof(struct NAM));
  27.    bzero(dirp->ddirent,sizeof(struct dirent));
  28.    strcpy(q, filename);
  29.    if (q[strlen(q)-1] != '/') strcat(q,"/");
  30.    strcpy(dirp->buffer,unix2vms(q));
  31.    if (all_versions) strcat(dirp->buffer,"*.*;*");
  32.    else strcat(dirp->buffer,"*.*");
  33. if (debug) fprintf(stderr,"dir = %s\n",dirp->buffer);
  34.    dirp->fab->fab$b_bid = FAB$C_BID;
  35.    dirp->fab->fab$b_bln = FAB$C_BLN;
  36.    dirp->fab->fab$l_fna = dirp->buffer;
  37.    dirp->fab->fab$b_fns = strlen(dirp->buffer);
  38.    dirp->fab->fab$l_nam = dirp->nam;
  39.    dirp->nam->nam$b_bid = NAM$C_BID;
  40.    dirp->nam->nam$b_bln = NAM$C_BLN;
  41.    dirp->nam->nam$l_rsa = dirp->output;
  42.    dirp->nam->nam$b_rss = NAM$C_MAXRSS;
  43.    dirp->nam->nam$l_esa = dirp->exp_output;
  44.    dirp->nam->nam$b_ess = NAM$C_MAXRSS;
  45.    status = sys$parse(dirp->fab,0,0);
  46.    if (status != RMS$_NORMAL) {
  47. if (debug) fprintf(stderr," failed\n");
  48.     return(NULL);
  49.    }
  50. if (debug) fprintf(stderr,"succeeded\n");
  51.    return(dirp);
  52. }
  53.  
  54. struct dirent *readdir(dirp)
  55. DIR *dirp;
  56. {
  57.    int status;
  58. if (debug) fprintf(stderr,"readdir called\n");
  59.    status = sys$search(dirp->fab,0,0);
  60.    if (status != RMS$_NORMAL) {
  61. if (debug) fprintf(stderr," failed\n");
  62.       return(NULL);
  63.    }
  64.    dirp->nam->nam$l_rsa[dirp->nam->nam$b_rsl] = '\0';
  65.    dirp->ddirent->d_namlen = dirp->nam->nam$l_ver - dirp->nam->nam$l_name;
  66.    bzero(dirp->ddirent->d_name,257);
  67.    lowercopy(dirp->nam->nam$l_name,dirp->ddirent->d_name,dirp->ddirent->d_namlen);
  68.    dirp->dd_loc++;
  69.    dirp->ddirent->d_ino = 1234; /*shouldn't be zero!*/
  70. if (debug) fprintf(stderr," returns %s\n",dirp->ddirent->d_name);
  71.    return(dirp->ddirent);
  72. }
  73.  
  74. void seekdir(dirp, loc)
  75. DIR *dirp;
  76. long loc;
  77. {
  78.    int i;
  79. if (debug) fprintf(stderr,"seekdir called\n");
  80.    sys$close(dirp->fab);
  81.    sys$parse(dirp->fab,0,0);
  82.    for (i=0; i<loc; i++) sys$search(dirp->fab);
  83.    dirp->dd_loc = loc;
  84. }
  85.  
  86. long telldir(dirp)
  87. DIR *dirp;
  88. {
  89. if (debug) fprintf(stderr,"telldir called\n");
  90.    return(dirp->dd_loc);
  91. }
  92.  
  93. closedir(dirp)
  94. DIR *dirp;
  95. {
  96. if (debug) fprintf(stderr,"closedir called\n");
  97.    sys$close(dirp->fab);
  98.    free(dirp->fab);
  99.    free(dirp->nam);
  100.    free(dirp->buffer);
  101.    free(dirp->output);
  102.    free(dirp->exp_output);
  103.    free(dirp->ddirent);
  104.    free(dirp);
  105. }
  106.  
  107. /*chdir works but doesn't change "PATH" variable! so we have to catch it*/
  108. static char *current_path = NULL;
  109.  
  110. int si_chdir(path)
  111. char *path;
  112. {
  113.    char *malloc();
  114.    int i;
  115.    if (current_path == NULL) current_path = malloc(128);
  116.    i = chdir(path);
  117.    if (i == 0) {
  118.        strcpy(current_path,path);
  119.        if (path[strlen(path)-1] != '/') strcat(current_path,"/");
  120.    }
  121.    return(i);
  122. }
  123.  
  124. char *si_getenv(name)
  125. char *name;
  126. {
  127.    static char buffer[128];
  128.    char tmp[128], *p, *q, *r, *strchr(), *getenv();
  129.    if (strcmp(name,"PATH") == 0) {
  130.       if (current_path == NULL) return(getenv("PATH"));
  131.       return(unix2vms(current_path));
  132.    }
  133.    if (strcmp(name,"HOME") != 0) return(getenv(name));
  134.    strcpy(tmp,getenv(name));
  135.    p = tmp;
  136.    q = buffer;
  137.    r = strchr(tmp,':');
  138.    if (r == NULL) {
  139.       strcpy(buffer,tmp);
  140.       return(buffer);
  141.    }
  142.    *r = '\0';
  143.    *buffer = '/';
  144.    strcpy(buffer+1,p);
  145.    strcat(buffer,"/");
  146.    r++;
  147.    r++;
  148.    p = r;
  149.    q = r;
  150.    while ( (r = strchr(p,'.')) != NULL) {
  151.       *r = '/';
  152.       p = r+1;
  153.    }
  154.    r = strchr(p,']');
  155.    *r = '\0';
  156.    strcat(buffer,q);
  157.    return(buffer);
  158. }
  159.  
  160. static lowercopy(src, dst, length)
  161. char *src, *dst;
  162. int length;
  163. {
  164.   int i;
  165.   for (i=0;i<length;i++) *dst++ = tolower(*src++);
  166. }
  167.  
  168.  
  169.