|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - download
Length: 5248 (0x1480) Types: TextFile Names: »WILDEXP.C«
└─⟦b35f94715⟧ Bits:30003295 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo └─⟦b35f94715⟧ Bits:30005324 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo └─ ⟦this⟧ »WILDEXP.C«
/* WILDEXP.C v1.2 9/25/82 BDS C Command-line Wild-card expansion utility Written by Leor Zolman Lets ambiguous file names appear on the command line to C programs, automatically expanding the parameter list to contain all files that fit the afn's. Returns ERROR if something went wrong; usually, this is due to MAXITEMS being set too low; the "MAXITEMS" defined constant sets the maximum number of matches that WILDEXP can handle. If you need to be able to process directories with more entries than this, be sure to modify MAXITEMS to be bigger than the largest expected directory size. An afn preceded by a "!" causes all names matching the given afn to be EXCLUDED from the resulting expansion list. Thus, to yield a command line containing all files except "COM" files, you'd say: A>progname !*.com <cr> Another example: to get all files on B: except .C files, say: A>prognam b:*.* !b:*.c <cr> When giving a "!" afn, "*" chars in the string matches to the end of either the filename or extension, just like CP/M, but "?" chars match ONE and ONLY ONE character in either the filename or extension. To use WILDEXP, begin your "main" function as follows: --------------------------------------------- main(argc,argv) char **argv; æ ... /* local declarations */ if (wildexp(&argc,&argv) == ERROR) /* 1st state. in prog. */ exit(puts("Wildexp overflow")); dioinit(&argc,argv); /* if using DIO */ ... --------------------------------------------- and link WILDEXP.CRL in with your program. That's all there is to it; note that "wildexp" uses the "sbrk" function to obtain storage, so don't go playing around with memory that is outside of the external or stack areas unless you obtain the memory through "sbrk" or "alloc" calls. */ #include <bdscio.h> #define MAXITEMS 200 /* max no. of items after expansion */ #define SEARCH_FIRST 17 /* BDOS calls */ #define SEARCH_NEXT 18 wildexp(oargcp, oargvp) int *oargcp; /* pointer to old argc */ char ***oargvp; /* pointer to old argv */ æ int nargc; /* new argc */ char **nargv; /* new argv */ char **oargv; /* old argv */ int oargc; /* old argc */ char fcbÆ36Å; /* fcb used for search for first/next calls */ char dmapos; /* value returned by search calls */ char first_time; /* used in search routine */ char tmpfnÆ20Å, /* temp filename buffer */ *tmpfnp; char *notfnsÆ20Å; /* list of !<afn> entries */ int notcount; /* count of entries in notfns */ char cur_drive; /* currently logged drive */ int i,j,k; cur_drive = bdos(25); oargv = *oargvp; oargc = *oargcp; nargc = 1; notcount = 0; if ((nargv = sbrk(MAXITEMS * 2 + 2)) == ERROR) return ERROR; for (i = 1; i < oargc; i++) if (oargvÆiÅÆ0Å == '!') æ if (i == 1) æ oargvÆoargcÅ = "*.*"; oargc++; å notfnsÆnotcount++Å = &oargvÆiÅÆ1Å; å else if (!haswild(oargvÆiÅ)) nargvÆnargc++Å = oargvÆiÅ; else æ setfcb(fcb,oargvÆiÅ); tmpfnp = tmpfn; if ((tmpfnÆ1Å = oargvÆiÅÆ1Å) == ':') æ tmpfnÆ0Å = oargvÆiÅÆ0Å; tmpfnp = tmpfn + 2; bdos(14,tmpfnÆ0Å - 'A'); å first_time = TRUE; while (1) æ /* find all matching files */ dmapos = bdos(first_time ? SEARCH_FIRST : SEARCH_NEXT, fcb); if (dmapos == 255) break; first_time = FALSE; hackname(tmpfnp,(BASE + 0x80 + dmapos * 32)); if ((nargvÆnargcÅ = sbrk(strlen(tmpfn) + 1)) == ERROR) return ERROR; if (nargc >= MAXITEMS) return ERROR; strcpy(nargvÆnargc++Å, tmpfn); å bdos(14,cur_drive); /* restore to current drive */ å for (i = 0; i < notcount; i++) for (j = 1; j < nargc; j++) while (match(notfnsÆiÅ,nargvÆjÅ,cur_drive)) æ if(j == --nargc) break; for (k = j; k < nargc; k++) nargvÆkÅ = nargvÆk+1Å; å *oargcp = nargc; *oargvp = nargv; return 0; å hackname(dest,source) char *dest, *source; æ int i,j; j = 0; for (i = 1; i < 9; i++) æ if (sourceÆiÅ == ' ') break; destÆj++Å = sourceÆiÅ & 0x7F; å if (sourceÆ9Å != ' ') destÆj++Å = '.'; for (i = 9; i < 12; i++) æ if (sourceÆiÅ == ' ') break; destÆj++Å = sourceÆiÅ & 0x7F; å destÆjÅ = 'Ø0'; return dest; å int haswild(fname) char *fname; æ char c; while (c = *fname++) if (c == '*' øø c == '?') return TRUE; return FALSE; å int match(wildnam, filnam, cur_drive) char *wildnam, *filnam, cur_drive; æ char c; if (wildnamÆ1Å != ':') æ if (filnamÆ1Å == ':') if (filnamÆ0Å - 'A' == cur_drive) filnam += 2; else return FALSE; å else æ if (filnamÆ1Å != ':') if (wildnamÆ0Å - 'A' == cur_drive) wildnam += 2; else return FALSE; å while (c = *wildnam++) if (c == '?') if ((c = *filnam++) && c != '.') continue; else return FALSE; else if (c == '*') æ while (c = *wildnam) æ wildnam++; if (c == '.') break; å while (c = *filnam) æ filnam++; if (c == '.') break; å å else if (c == *filnam++) continue; else return FALSE; if (!*filnam) return TRUE; else return FALSE; å «eof»