Annotation of coherent/g/usr/bin/vi/wildcard.c, revision 1.1

1.1     ! root        1: /* wildcard.c */
        !             2: 
        !             3: /* Author:
        !             4:  *     Guntram Blohm
        !             5:  *     Buchenstrasse 19
        !             6:  *     7904 Erbach, West Germany
        !             7:  *     Tel. ++49-7305-6997
        !             8:  *     sorry - no regular network connection
        !             9:  */
        !            10: 
        !            11: /* this program implements wildcard expansion for elvis/dos. It works
        !            12:  * like UNIX echo, but uses the dos wildcard conventions
        !            13:  * (*.* matches all files, * matches files without extension only,
        !            14:  * filespecs may contain drive letters, wildcards not allowed in directory
        !            15:  * names).
        !            16:  *
        !            17:  * It is also #included into ctags.c, ref.c, ...; in this case,
        !            18:  * we don't want a main function here.
        !            19:  */
        !            20: 
        !            21: #include <stdio.h>
        !            22: #ifdef __STDC__
        !            23: # include <stdlib.h>
        !            24: #endif
        !            25: #ifndef        WILDCARD_NO_MAIN
        !            26: # include "config.h"
        !            27: #endif
        !            28: #include "ctype.h"
        !            29: #ifdef __TURBOC__
        !            30: #include <dir.h>
        !            31: #endif
        !            32: 
        !            33: #ifdef M_I86
        !            34: #define        findfirst(a,b,c)        _dos_findfirst(a,c,b)
        !            35: #define        findnext                _dos_findnext
        !            36: #define        ffblk                   find_t
        !            37: #define        ff_name                 name
        !            38: #include <dos.h>
        !            39: #endif
        !            40: 
        !            41: /* Atari TOS, MWC */
        !            42: #ifdef M68000
        !            43: #include <stat.h>
        !            44: #include <osbind.h>
        !            45: #define        findfirst(a,b,c)        (Fsetdta(b), (Fsfirst(a,c)))
        !            46: #define        findnext(x)             (Fsnext())
        !            47: #define        ff_name d_fname
        !            48: #endif
        !            49: 
        !            50: /* Atari TOS, GNU-C */
        !            51: #ifdef __m68k__
        !            52: #include <stat.h>
        !            53: #include <osbind.h>
        !            54: #define        findfirst(a,b,c)        (Fsetdta(b), (Fsfirst(a,c)))
        !            55: #define        findnext(x)             (Fsnext())
        !            56: #define        ff_name dta_name
        !            57: #define        DMABUFFER struct _dta
        !            58: #endif
        !            59: 
        !            60: #define        MAXFILES        1000
        !            61: 
        !            62: int pstrcmp();
        !            63: #ifndef __STDC__
        !            64: extern char *calloc();
        !            65: #endif
        !            66: 
        !            67: char *files[MAXFILES];
        !            68: int nfiles;
        !            69: 
        !            70: #ifndef        WILDCARD_NO_MAIN
        !            71: 
        !            72: main(argc, argv)
        !            73:        char **argv;
        !            74: {
        !            75:        int i;
        !            76: 
        !            77:        _ct_init("");
        !            78:        for (i=1; i<argc; i++)
        !            79:                expand(argv[i]);
        !            80:        if (nfiles)
        !            81:                printf("%s", files[0]);
        !            82:        for (i=1; i<nfiles; i++)
        !            83:        {
        !            84:                printf(" %s", files[i]);
        !            85:        }
        !            86:        putchar('\n');
        !            87:        return 0;
        !            88: }
        !            89: 
        !            90: #else
        !            91: char **wildexpand(argc, argv)
        !            92:        int *argc;
        !            93:        char **argv;
        !            94: {
        !            95:        int i;
        !            96:        
        !            97:        for (i=0; i<*argc; i++)
        !            98:                expand(argv[i]);
        !            99:        *argc=nfiles;
        !           100:        return files;
        !           101: }      
        !           102: #endif
        !           103: 
        !           104: expand(name)
        !           105:        char *name;
        !           106: {
        !           107:        char *filespec;
        !           108:        int wildcard=0;
        !           109: #if defined(M68000) || defined(__m68k__)
        !           110:        DMABUFFER findbuf;
        !           111: #else
        !           112:        struct ffblk findbuf;
        !           113: #endif
        !           114:        int err;
        !           115:        char buf[80];
        !           116:        int lastn;
        !           117: 
        !           118:        strcpy(buf, name);
        !           119:        for (filespec=buf; *filespec; filespec++)
        !           120:                ;
        !           121: 
        !           122:        while (--filespec>=buf)
        !           123:        {       if (*filespec=='?' || *filespec=='*')
        !           124:                        wildcard=1;
        !           125:                if (*filespec=='/' || *filespec=='\\' || *filespec==':')
        !           126:                        break;
        !           127:        }
        !           128:        if (!wildcard)
        !           129:                addfile(buf);
        !           130:        else
        !           131:        {
        !           132:                lastn=nfiles;
        !           133:                filespec++;
        !           134:                if ((err=findfirst(buf, &findbuf, 0))!=0)
        !           135:                        addfile(buf);
        !           136:                while (!err)
        !           137:                {
        !           138:                        strcpy(filespec, findbuf.ff_name);
        !           139:                        addfile(buf);
        !           140:                        err=findnext(&findbuf);
        !           141:                }
        !           142:                if (lastn!=nfiles)
        !           143:                        qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
        !           144:        }
        !           145: }
        !           146: 
        !           147: addfile(buf)
        !           148:        char *buf;
        !           149: {
        !           150:        char *p;
        !           151: 
        !           152:        for (p=buf; *p; p++)
        !           153:                *p=tolower(*p);
        !           154: 
        !           155:        if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, 1))!=0)
        !           156:                strcpy(files[nfiles++], buf);
        !           157: }
        !           158: 
        !           159: int pstrcmp(a, b)
        !           160:        char **a, **b;
        !           161: {
        !           162:        return strcmp(*a, *b);
        !           163: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.