home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / xv221src / unsupt / vms / vms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  8.2 KB  |  277 lines

  1. /* Some unix emulation procedures to make XV happy */
  2. /* 1-NOV-1990 GJC@MITECH.COM */
  3.  
  4. #include <stdio.h>
  5.  
  6. #include <string.h>
  7. #include <descrip.h>
  8. #include <rmsdef.h>
  9. #include <ssdef.h>
  10. #include <stat.h>
  11.  
  12.  
  13. int xv_bcmp ( s1, s2, size )
  14.     char *s1, *s2;
  15.     int size;
  16. {
  17.     int i;
  18.     for ( i = 0; i < size; i++ ) if ( *s1++ != *s2++ ) {
  19.     if ( *(--s1) > *(--s2) ) return 1; else return -1;
  20.     }
  21.     return 0;
  22. }
  23.  
  24. bcopy(x,y,n)
  25.      char *x,*y;
  26.      long n;
  27. {memmove(y,x,n);}    /* reverse order of arguments */
  28.  
  29. static char *getwd_tmp = NULL;
  30.  
  31. char *getwd(p)
  32.      char *p;
  33. {int c;
  34.  char *root_dir,*l2;
  35.  getcwd(p,512,0);    /* get current working directory in unix format*/
  36.  
  37.  root_dir = strstr ( p, "/000000" );
  38.  if ( root_dir != NULL ) {
  39.     /* trim root directory out of specification */
  40.     if ( (strlen(root_dir) == 7) && 
  41.      (strpbrk(p+1,"/") == root_dir) ) *root_dir = '\0';
  42.  }
  43.  /* special kludge for "/" directory */
  44.  if ( strcmp ( p, "/DEVICE_LIST_ROOT" ) == 0 ) strcpy  ( p, "/" );
  45.  return(p);
  46. }
  47.  
  48. unlink(p)
  49.      char *p;
  50. {printf("unlink: '%s'\n",p); delete(p);}
  51.  
  52. rindex(p,c)
  53.      char *p;
  54.      int c;
  55. {return(strrchr(p,c));}
  56.  
  57. int lstat(f,st)        /* fake a stat operation to return file type */
  58.    char *f;
  59.    stat_t *st;
  60. {
  61.     char *dirext, *name;
  62.     int extlen;
  63.  
  64.     st->st_mode = S_IFREG;    /* default to normal file */
  65.     name = strrchr ( f, '/' );    /* locate rightmost slash */
  66.     if ( name == NULL ) name = f; else name++;
  67.  
  68.     dirext = strstr ( name, ".DIR" );
  69.     if ( dirext != NULL ) {
  70.     /* make it an exact match */
  71.     extlen = strcspn(&dirext[1],".;");
  72.         if ( (extlen == 0) || (extlen == 3) ) {
  73.         st->st_mode = S_IFDIR;
  74.         if ( strncmp ( name, "000000.", 7 ) == 0 ) return 0;
  75.         else return (stat ( f, st ));
  76.     }
  77.     }
  78.     return 0;
  79. }
  80.  
  81. do_vms_wildcard(pargc,pargv)
  82.      int *pargc;
  83.      char ***pargv;
  84. {int j,vsize;
  85.  int argc; char **argv;
  86.  argc = *pargc;
  87.  argv = *pargv;
  88.  *pargc = 0;
  89.  vsize = 3;
  90.  *pargv = (char **) malloc(sizeof (char *) * vsize);
  91.  for(j=0;j<argc;++j)
  92.    vms_wild_putargs(argv[j],pargc,pargv,&vsize);}
  93.  
  94. vms_wild_putargs(s,pargc,pargv,pvsize)
  95.      char *s; int *pargc; char ***pargv; int *pvsize;
  96. {if ( (!strchr(s,'*')) && (!strchr(s,'%')) )
  97.    vms_wild_put_one(s,pargc,pargv,pvsize);
  98.  else
  99.    vms_wild_put_wild(s,pargc,pargv,pvsize);}
  100.  
  101.  
  102. vms_wild_put_one(s,pargc,pargv,pvsize)
  103.      char *s; int *pargc; char ***pargv; int *pvsize;
  104. {int nvsize,i;
  105.  char ** nargv, *uname, *SHELL$TRANSLATE_VMS();
  106.  if (*pargc == *pvsize)
  107.    {nvsize = 2 * *pvsize;
  108.     nargv = (char **) malloc(sizeof (char *) * nvsize);
  109.     for(i=0;i < *pargc; ++i) nargv[i] = (*pargv)[i];
  110.     free(*pargv);
  111.     *pargv = nargv;
  112.     *pvsize = nvsize;}
  113.  if ( uname = SHELL$TRANSLATE_VMS ( s ) ) {
  114.     /* printf("vms: '%s' -> unix: '%s'\n", s, uname ); */
  115.     if ( strlen(s) >= strlen(uname) ) { strcpy(s,uname); free(uname); }
  116.     else s = uname;  /* will lose s's old allocation */
  117.  } 
  118.  (*pargv)[(*pargc)++] = s;}
  119.  
  120.  
  121. set_dsc(x,buff,len)
  122.  struct dsc$descriptor *x;
  123.  char *buff;
  124.  int len;
  125. {(*x).dsc$w_length = len;
  126.  (*x).dsc$a_pointer = buff;
  127.  (*x).dsc$b_class = DSC$K_CLASS_S;
  128.  (*x).dsc$b_dtype = DSC$K_DTYPE_T;
  129.  return(x);}
  130.  
  131.  struct dsc$descriptor *
  132. set_dsc_cst(x,buff)
  133.  struct dsc$descriptor *x;
  134.  char *buff;
  135. {return(set_dsc(x,buff,strlen(buff)));}
  136.  
  137.  
  138. vms_wild_put_wild(s,pargc,pargv,pvsize)
  139.      char *s; int *pargc; char ***pargv; int *pvsize;
  140. {struct dsc$descriptor fnamed,foutd,rfnamed;
  141.  char *ns,*p;
  142.  int rval;
  143.  long context;
  144.  set_dsc_cst(&rfnamed,";");
  145.  set_dsc_cst(&fnamed,s);
  146.  set_dsc(&foutd,0,0);
  147.  foutd.dsc$b_class = DSC$K_CLASS_D;
  148.  context = 0;
  149.  while(1)
  150.   {rval = lib$find_file(&fnamed,&foutd,&context,0,&rfnamed,0,0);
  151.    if (rval == RMS$_NMF) break;
  152.    if (rval == RMS$_FNF) break;
  153.    if (rval != RMS$_NORMAL) exit(rval);
  154.    ns = (char *) malloc(foutd.dsc$w_length + 1);
  155.    ns[foutd.dsc$w_length] = 0;
  156.    memcpy(ns,foutd.dsc$a_pointer,foutd.dsc$w_length);
  157.    /*if (p = strchr(ns,']')) ns = p+1;*/
  158.    /* if (p = strchr(ns,';')) *p = 0; */
  159.    vms_wild_put_one(ns,pargc,pargv,pvsize);}
  160.  if (foutd.dsc$a_pointer) lib$sfree1_dd(&foutd);
  161.  if (context)
  162.    {rval = lib$find_file_end(&context);
  163.     if (rval != SS$_NORMAL) exit(rval);}}
  164.  
  165. /*
  166.  * Define substitue qsort for one that dec broke.  Only handle case where
  167.  * element size is 4 (same as integer).
  168.  */
  169. #ifdef qsort
  170. #undefine qsort
  171. #endif
  172. void xv_qsort ( array, size, unit, compare )
  173.    int array[1000];    /* array to be sorted */
  174.    int size;        /* size of array to sort, should be at least 100 */
  175.    int unit;        /* Size of array element */
  176.    int compare();    /* comaparison function */
  177. {
  178.    int stack[68], *top;    /* work array, depth of stack is bounded */
  179.    int start, finish, lbound, hbound, pivot, temp, i, j;
  180.  
  181.    if ( unit != sizeof(int) ) {    /* punt */
  182.     qsort ( array, size, unit, compare );
  183.     return;
  184.     }
  185.    if ( size <= 1 ) return;
  186.    /* set up initial partition on top of stack */
  187.    top = &stack[68];
  188.    *--top = 0;        /* push lbound */
  189.    *--top = size-1;    /* push initial hbound */
  190.  
  191.    /* loop until stack is emtpy */
  192.  
  193.    while ( top < &stack[68] ) {
  194.  
  195.       /* pop next range from stack and see if it has at least 3 elements */
  196.  
  197.       finish = *top++; start = *top++;
  198.       pivot = array[start];
  199.       if ( finish > start + 1 ) {
  200.          /*
  201.           *  more than 2 elements, split range into 2 sections according to
  202.           *  the relation to the pivot value.
  203.           */
  204.      array[start] = array[(start+finish)/2];    /* avoid sequence */
  205.      array[(start+finish)/2] = pivot; pivot = array[start];
  206.          lbound = start; hbound = finish;
  207.          while ( lbound < hbound ) {
  208.             if ( compare(&pivot, &array[lbound+1]) > 0 ) {
  209.                lbound++;
  210.             } else {
  211.                temp = array[hbound];
  212.                array[hbound] = array[lbound+1];
  213.                hbound--;
  214.                array[lbound+1] = temp;
  215.             }
  216.          }
  217.          /* determine which parition is bigger and push onto stack */
  218.          if ( lbound + lbound < (start+finish) ) {
  219.             /* push high partition first. */
  220.             *--top = lbound + 1;
  221.             *--top = finish;
  222.             finish = start;        /* skip add step below */
  223.              /* either push low partition or sort by inspection */
  224.             if ( lbound - start > 1 ) {
  225.                *--top = start;
  226.                *--top = lbound;
  227.             } else {
  228.                /* 2 element parition (start+1=lbound), sort by looking */
  229.                if ( pivot > array[lbound] ) {
  230.                   array[start] = array[lbound];
  231.                   array[lbound] = pivot;
  232.                }
  233.             }
  234.          } else if ( lbound != finish ) {
  235.             /* either push low partition or sort by inspection */
  236.             if ( lbound - start > 1 ) {
  237.                *--top = start;
  238.                *--top = lbound;
  239.             } else if ( lbound  > start ) {
  240.                /* 2 element parition (start+1=lbound), sort by looking */
  241.                if ( compare(&pivot, &array[lbound]) > 0 ) {
  242.                   array[start] = array[lbound];
  243.                   array[lbound] = pivot;
  244.                }
  245.             }
  246.             /* push high partition or sort by inspection */
  247.             if ( lbound < finish - 2 ) {
  248.                *--top = lbound + 1;
  249.                *--top = finish;
  250.             } else if ( lbound < finish - 1 ) {  /* 2 in partition */
  251.                if ( compare(&array[lbound+1], &array[finish]) > 0 ) {
  252.                   temp = array[lbound+1];
  253.                   array[lbound+1] = array[finish];
  254.                   array[finish] = temp;
  255.                }
  256.             }
  257.          } else {
  258.             /*
  259.              * Special case: high partition is empty, indicating that pivot
  260.              * value is at maximum.  Move to end and re-push remainder.
  261.              */
  262.             array[start] = array[finish];
  263.             array[finish] = pivot;
  264.             *--top = start;
  265.             *--top = finish - 1;
  266.          }
  267.  
  268.       } else {
  269.          /* only 2 elements in partition, sort inline */
  270.          if ( compare(&pivot,&array[finish]) > 0 ) {
  271.             array[start] = array[finish];
  272.             array[finish] = pivot;
  273.          }
  274.       }
  275.    }
  276.