home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / tools / pep / bdmg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-29  |  6.6 KB  |  252 lines

  1. /* bdmg.c  1989 june 4 [gh]
  2. +-----------------------------------------------------------------------------
  3. | Abstract:
  4. |    Functions that compensate some of the braindamage in various DUCOS.
  5. |
  6. | Authorship:
  7. |    Copyright (c) 1988, 1989 Gisle Hannemyr.
  8. |    Permission is granted to hack, make and distribute copies of this module
  9. |    as long as this notice and the copyright notices are not removed.
  10. |    If you intend to distribute changed versions of this module, please make
  11. |    an entry in the "history" log (below) and mark the hacked lines with your
  12. |    initials. I maintain the module, and shall appreiciate copies of bug
  13. |    fixes and new versions.
  14. |    Flames, bug reports, comments and improvements to:
  15. |       snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway
  16. |       email: X400: gisle@nr.uninett
  17. |              RFC:  gisle@ifi.uio.no
  18. |              (and several BBS mailboxes in the Oslo area).
  19. |
  20. | Access programs:
  21. |    int stricmp()           -- case insenitive compare
  22. |    int rename()           -- rename file
  23. |    struct DIRLIST *expwildcard() -- ersatz shell wildcard expansion
  24. |    void dispwildcard()       -- dispose list created by expwildcard
  25. |
  26. | History:
  27. |    11 dec 89 [gh] Latest update.
  28. |
  29. | See main module for more comments.
  30. +---------------------------------------------------------------------------*/
  31.  
  32. #include <stdio.h>
  33. #include "pep.h"
  34. #include "bdmg.h"
  35. #include <string.h>
  36. #if __CPM86__ || __MSDOS__
  37. #include <dos.h>
  38. #endif
  39.  
  40. /*---( types )--------------------------------------------------------------*/
  41.  
  42. #ifdef __CPM86__
  43. struct DTA {
  44.    unsigned char f_driv;
  45.    unsigned char f_name[8];
  46.    unsigned char f_type[3];
  47.    unsigned char f_dumm[20];
  48. }; /* DTA */
  49. #endif
  50.  
  51.  
  52. #ifdef __MSDOS__
  53. struct DTA {
  54.    char bogus[21];
  55.    char attri;
  56.    int  ftime;
  57.    int  fdate;
  58.    int  fsize[2];
  59.    char fname[64];
  60. }; /* DTA */
  61. #endif
  62.  
  63.  
  64. /*---( braindamage compensation )-------------------------------------------*/
  65.  
  66. #ifdef STRICMP
  67. int stricmp(ss,tt)
  68. char *ss,*tt;
  69. {
  70.    while (*ss && (tolower(*ss) == tolower(*tt))) { ss++; tt++; }
  71.    return(tolower(*ss) - tolower(*tt));
  72. } /* stricmp */
  73. #endif
  74.  
  75.  
  76. #ifdef SYSV2
  77. int rename(from,to)
  78. char *from,*to;
  79. {
  80.    (void)unlink(to);
  81.    if (link(from,to)) return(-1);
  82.    return(unlink(from));
  83. } /* rename */
  84. #endif
  85.  
  86.  
  87. #ifdef VMSV1
  88. int rename(from, to)
  89. char *from, *to;
  90. {
  91.   struct dsc$descriptor_s From={strlen(from),
  92.                   DSC$K_DTYPE_T,DSC$K_CLASS_S,from};
  93.   struct dsc$descriptor_s To={strlen(to),
  94.                   DSC$K_DTYPE_T,DSC$K_CLASS_S,to};
  95.  
  96.   if (LIB$RENAME_FILE(&From, &To) == SS$_NORMAL) return(0);
  97.   return(-1);
  98. } /* rename */
  99. #endif
  100.  
  101.  
  102. #if __CPM86__ || __MSDOS__
  103. #ifdef __CPM86__
  104. /*
  105. | Abs: Pack user number and fcb structure into string.
  106. | Ret: String with filename that was in fcb.
  107. | Imp: System dependent: CP/M-86, C-DOS 3.20
  108. */
  109. static char *fcb2str(buf,lfcb,usr)
  110. char *buf;
  111. struct fcb *lfcb;
  112. int usr;
  113. {
  114.     char *cp;
  115.     int i;
  116.  
  117.     cp=buf;
  118.     if (usr!=255) {
  119.     if (usr>9) *buf++=((usr/10)%10)+'0';
  120.     *buf++=(usr%10)+'0';
  121.     *buf++='/';
  122.     }
  123.     if (lfcb->f_driv) {
  124.     *buf++=lfcb->f_driv+'a'-1;
  125.     *buf++=':';
  126.     }
  127.     for (i=0; i<8 && lfcb->f_name[i]!=' '; ++i)
  128.     *buf++=tolower(lfcb->f_name[i]&0x7f); *buf++='.';
  129.     for (i=0; i<3 && lfcb->f_type[i]!=' '; ++i)
  130.     *buf++=tolower(lfcb->f_type[i]&0x7f); *buf=0;
  131.     return(cp);
  132. } /* fcb2str */
  133.  
  134.  
  135. /*
  136. | Abs: Return list of malloc'ed filenames matching ambigious input list.
  137. | Ret: Pointer to alloced list, or 0 if none found.
  138. | Imp: System dependent: CP/M-86, C-DOS 3.20
  139. */
  140. struct DIRLIST *expwildcard(ambig)
  141. char **ambig;
  142. {
  143.    struct DIRLIST *first, *last, *prev;
  144.    int pos;
  145.    char *n;
  146.    int user;
  147.    struct DTA dma[4];                 /* 4 CP/M directory entries */
  148.    struct DTA fcb;            /* Working FCB              */
  149.  
  150.    setmem(&fcb,sizeof(struct DTA),0);                    /* Initialize FCB  */
  151.    bdos(SETDTA,dma);
  152.  
  153.    first = NULL;
  154.    while (*ambig) {
  155.       if ((user=fcbinit(*ambig,&fcb)) == -1) continue;     /* Bogus file name */
  156.       setusr(user);
  157.       for (pos = bdos(FNDFRST,&fcb); pos != 0xff; pos = bdos(FNDNEXT,&fcb)) {
  158.          if ((last = (struct DIRLIST *)malloc(sizeof(struct DIRLIST)))
  159.          && (last->fnam=malloc(18))) {
  160.             dma[pos].f_driv=fcb.f_driv;        /* set drive */
  161.             if (!first) first = last; else prev->next = last;
  162.             fcb2str(last->fnam,&dma[pos],user);
  163.             last->next = NULL;
  164.             prev       = last;
  165.          } else mess(5); /* No more room */
  166.       } /* for */
  167.       ambig++;
  168.    } /* while */
  169.    rstusr();
  170.    return(first);
  171. }  /* expwildcard */
  172. #endif
  173.  
  174.  
  175. #ifdef __MSDOS__
  176. /*
  177. | Abs: Return list of malloc'ed filenames matching ambigious input list.
  178. | Ret: Pointer to alloced list, or 0 if none found.
  179. | Imp: System dependent: MS-DOS 2.0, 3.2.
  180. */
  181. struct DIRLIST *expwildcard(ambig)
  182. char **ambig;
  183. {
  184.    union  REGS  ireg;
  185.    union  REGS  oreg;
  186.    struct SREGS sreg;
  187.    struct DTA   dta;
  188.  
  189.    char name[64];
  190.    char *endpath;
  191.    int   lenpath;
  192.    struct DIRLIST *first, *last, *prev;
  193.  
  194.    if (!*ambig) return(NULL);      /* Doing stdin */
  195.  
  196.    ireg.h.ah = SETDTA;
  197.    ireg.x.dx = (int)&dta;
  198.    intdos(&ireg,&oreg);
  199.    first = NULL;
  200.  
  201.    while (*ambig) {
  202.       endpath = strrchr(*ambig,DIRCHAR);
  203.       if (endpath) {
  204.          endpath++;              /* Behind "\"                */
  205.          lenpath = (unsigned int)endpath - (unsigned int)*ambig;
  206.       } else lenpath = 0;
  207.       ireg.h.ah = GETFRST;
  208.       ireg.x.cx = _A_NORMAL;     /* Look up all normal files. */
  209.       ireg.x.dx = (int)*ambig;
  210.       for (;;) {
  211.          intdos(&ireg,&oreg);
  212.          /* printf("AX = 0x%x   CF = 0x%x\n",oreg.x.ax,oreg.x.cflag); */
  213.          if (oreg.x.cflag) break;
  214.          if ((last = (struct DIRLIST *)malloc(sizeof(struct DIRLIST)))
  215.            && (last->fnam = (char *)malloc(strlen(dta.fname)+lenpath+1))) {
  216.             if (!first) first = last; else prev->next = last;
  217.             strncpy(last->fnam,*ambig,lenpath);
  218.             last->fnam[lenpath] = '\0';
  219.             strcat(last->fnam,dta.fname);
  220.             last->next = NULL;
  221.             prev = last;
  222.             ireg.h.ah = GETNEXT;
  223.          } else mess(5); /* No more room */
  224.       } /* for */
  225.       ambig++;
  226.    } /* while */
  227.    return(first);
  228. }  /* expwildcard */
  229. #endif
  230.  
  231.  
  232. /*
  233. | Abs: Dispose list of malloc'ed filenames.
  234. */
  235. void dispwildcard(first)
  236. struct DIRLIST *first;
  237. {
  238.    struct DIRLIST *last, *prev;
  239.  
  240.    last = first;
  241.    while (last)
  242.    {
  243.       prev = last;
  244.       free(last->fnam);
  245.       last = last->next;
  246.       free(prev);
  247.    }
  248. } /* dispwildcard */
  249. #endif
  250.  
  251. /* EOF */
  252.