home *** CD-ROM | disk | FTP | other *** search
- /* bdmg.c 1989 june 4 [gh]
- +-----------------------------------------------------------------------------
- | Abstract:
- | Functions that compensate some of the braindamage in various DUCOS.
- |
- | Authorship:
- | Copyright (c) 1988, 1989 Gisle Hannemyr.
- | Permission is granted to hack, make and distribute copies of this module
- | as long as this notice and the copyright notices are not removed.
- | If you intend to distribute changed versions of this module, please make
- | an entry in the "history" log (below) and mark the hacked lines with your
- | initials. I maintain the module, and shall appreiciate copies of bug
- | fixes and new versions.
- | Flames, bug reports, comments and improvements to:
- | snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway
- | email: X400: gisle@nr.uninett
- | RFC: gisle@ifi.uio.no
- | (and several BBS mailboxes in the Oslo area).
- |
- | Access programs:
- | int stricmp() -- case insenitive compare
- | int rename() -- rename file
- | struct DIRLIST *expwildcard() -- ersatz shell wildcard expansion
- | void dispwildcard() -- dispose list created by expwildcard
- |
- | History:
- | 11 dec 89 [gh] Latest update.
- |
- | See main module for more comments.
- +---------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include "pep.h"
- #include "bdmg.h"
- #include <string.h>
- #if __CPM86__ || __MSDOS__
- #include <dos.h>
- #endif
-
- /*---( types )--------------------------------------------------------------*/
-
- #ifdef __CPM86__
- struct DTA {
- unsigned char f_driv;
- unsigned char f_name[8];
- unsigned char f_type[3];
- unsigned char f_dumm[20];
- }; /* DTA */
- #endif
-
-
- #ifdef __MSDOS__
- struct DTA {
- char bogus[21];
- char attri;
- int ftime;
- int fdate;
- int fsize[2];
- char fname[64];
- }; /* DTA */
- #endif
-
-
- /*---( braindamage compensation )-------------------------------------------*/
-
- #ifdef STRICMP
- int stricmp(ss,tt)
- char *ss,*tt;
- {
- while (*ss && (tolower(*ss) == tolower(*tt))) { ss++; tt++; }
- return(tolower(*ss) - tolower(*tt));
- } /* stricmp */
- #endif
-
-
- #ifdef SYSV2
- int rename(from,to)
- char *from,*to;
- {
- (void)unlink(to);
- if (link(from,to)) return(-1);
- return(unlink(from));
- } /* rename */
- #endif
-
-
- #ifdef VMSV1
- int rename(from, to)
- char *from, *to;
- {
- struct dsc$descriptor_s From={strlen(from),
- DSC$K_DTYPE_T,DSC$K_CLASS_S,from};
- struct dsc$descriptor_s To={strlen(to),
- DSC$K_DTYPE_T,DSC$K_CLASS_S,to};
-
- if (LIB$RENAME_FILE(&From, &To) == SS$_NORMAL) return(0);
- return(-1);
- } /* rename */
- #endif
-
-
- #if __CPM86__ || __MSDOS__
- #ifdef __CPM86__
- /*
- | Abs: Pack user number and fcb structure into string.
- | Ret: String with filename that was in fcb.
- | Imp: System dependent: CP/M-86, C-DOS 3.20
- */
- static char *fcb2str(buf,lfcb,usr)
- char *buf;
- struct fcb *lfcb;
- int usr;
- {
- char *cp;
- int i;
-
- cp=buf;
- if (usr!=255) {
- if (usr>9) *buf++=((usr/10)%10)+'0';
- *buf++=(usr%10)+'0';
- *buf++='/';
- }
- if (lfcb->f_driv) {
- *buf++=lfcb->f_driv+'a'-1;
- *buf++=':';
- }
- for (i=0; i<8 && lfcb->f_name[i]!=' '; ++i)
- *buf++=tolower(lfcb->f_name[i]&0x7f); *buf++='.';
- for (i=0; i<3 && lfcb->f_type[i]!=' '; ++i)
- *buf++=tolower(lfcb->f_type[i]&0x7f); *buf=0;
- return(cp);
- } /* fcb2str */
-
-
- /*
- | Abs: Return list of malloc'ed filenames matching ambigious input list.
- | Ret: Pointer to alloced list, or 0 if none found.
- | Imp: System dependent: CP/M-86, C-DOS 3.20
- */
- struct DIRLIST *expwildcard(ambig)
- char **ambig;
- {
- struct DIRLIST *first, *last, *prev;
- int pos;
- char *n;
- int user;
- struct DTA dma[4]; /* 4 CP/M directory entries */
- struct DTA fcb; /* Working FCB */
-
- setmem(&fcb,sizeof(struct DTA),0); /* Initialize FCB */
- bdos(SETDTA,dma);
-
- first = NULL;
- while (*ambig) {
- if ((user=fcbinit(*ambig,&fcb)) == -1) continue; /* Bogus file name */
- setusr(user);
- for (pos = bdos(FNDFRST,&fcb); pos != 0xff; pos = bdos(FNDNEXT,&fcb)) {
- if ((last = (struct DIRLIST *)malloc(sizeof(struct DIRLIST)))
- && (last->fnam=malloc(18))) {
- dma[pos].f_driv=fcb.f_driv; /* set drive */
- if (!first) first = last; else prev->next = last;
- fcb2str(last->fnam,&dma[pos],user);
- last->next = NULL;
- prev = last;
- } else mess(5); /* No more room */
- } /* for */
- ambig++;
- } /* while */
- rstusr();
- return(first);
- } /* expwildcard */
- #endif
-
-
- #ifdef __MSDOS__
- /*
- | Abs: Return list of malloc'ed filenames matching ambigious input list.
- | Ret: Pointer to alloced list, or 0 if none found.
- | Imp: System dependent: MS-DOS 2.0, 3.2.
- */
- struct DIRLIST *expwildcard(ambig)
- char **ambig;
- {
- union REGS ireg;
- union REGS oreg;
- struct SREGS sreg;
- struct DTA dta;
-
- char name[64];
- char *endpath;
- int lenpath;
- struct DIRLIST *first, *last, *prev;
-
- if (!*ambig) return(NULL); /* Doing stdin */
-
- ireg.h.ah = SETDTA;
- ireg.x.dx = (int)&dta;
- intdos(&ireg,&oreg);
- first = NULL;
-
- while (*ambig) {
- endpath = strrchr(*ambig,DIRCHAR);
- if (endpath) {
- endpath++; /* Behind "\" */
- lenpath = (unsigned int)endpath - (unsigned int)*ambig;
- } else lenpath = 0;
- ireg.h.ah = GETFRST;
- ireg.x.cx = _A_NORMAL; /* Look up all normal files. */
- ireg.x.dx = (int)*ambig;
- for (;;) {
- intdos(&ireg,&oreg);
- /* printf("AX = 0x%x CF = 0x%x\n",oreg.x.ax,oreg.x.cflag); */
- if (oreg.x.cflag) break;
- if ((last = (struct DIRLIST *)malloc(sizeof(struct DIRLIST)))
- && (last->fnam = (char *)malloc(strlen(dta.fname)+lenpath+1))) {
- if (!first) first = last; else prev->next = last;
- strncpy(last->fnam,*ambig,lenpath);
- last->fnam[lenpath] = '\0';
- strcat(last->fnam,dta.fname);
- last->next = NULL;
- prev = last;
- ireg.h.ah = GETNEXT;
- } else mess(5); /* No more room */
- } /* for */
- ambig++;
- } /* while */
- return(first);
- } /* expwildcard */
- #endif
-
-
- /*
- | Abs: Dispose list of malloc'ed filenames.
- */
- void dispwildcard(first)
- struct DIRLIST *first;
- {
- struct DIRLIST *last, *prev;
-
- last = first;
- while (last)
- {
- prev = last;
- free(last->fnam);
- last = last->next;
- free(prev);
- }
- } /* dispwildcard */
- #endif
-
- /* EOF */
-