Annotation of os2sdk/demos/apps/filelist/filelist.c, revision 1.1.1.2

1.1       root        1: /*
1.1.1.2 ! root        2:  * This example illustrates the use of DosFindFirst, DosFindNext, and
        !             3:  * DosFindClose.  The program lists the contents of a directory.
1.1       root        4:  * the parameter format is: FILELIST [-1al] names...
1.1.1.2 ! root        5:  *
        !             6:  * Created by Microsoft Corp. 1987
1.1       root        7:  */
                      8: 
1.1.1.2 ! root        9: #define INCL_ERRORS
        !            10: #define INCL_DOSPROCESS
        !            11: #define INCL_DOSFILEMGR
        !            12: 
1.1       root       13: #include <malloc.h>
                     14: #include <stdio.h>
                     15: #include <time.h>
1.1.1.2 ! root       16: #include <os2def.h>
        !            17: #include <bse.h>
1.1       root       18: 
                     19: 
                     20: /* file attribute constants */
                     21: 
                     22: #define        ATTR_READONLY   0x001   /* read only file */
                     23: #define        ATTR_HIDDEN     0x002   /* hidden file */
                     24: #define        ATTR_SYSTEM     0x004   /* system file */
                     25: #define        ATTR_DIRECTORY  0x010   /* subdirectory entry */
                     26: #define        ATTR_ARCHIVE    0x020   /* archive bit */
                     27: 
                     28: /* type of list */
                     29: 
                     30: #define        NAME_MULTICOLM  0       /* list name only in multipule columns */
                     31: #define        NAME_SINGCOLM   1       /* list name only in a single column */
                     32: #define        LONG_LIST       2       /* list all data on file */
                     33: #define        TOTALCOLMS      4       /* number of colms when listing name only */
                     34: 
1.1.1.2 ! root       35: #define RESULTBUFLEN   sizeof(FILEFINDBUF)      /* size of ResultBuf */
1.1       root       36: 
                     37: #define        SEARCHALL       "*.*"   /* default - search for everything */
                     38: #define        MAXPATHSIZE     128
                     39: #define        FILEPATHSIZE    MAXPATHSIZE + sizeof(SEARCHALL) + 1
                     40: 
                     41: main(argc, argv)
                     42:        int     argc;
1.1.1.2 ! root       43:        NPSZ    argv[];
1.1       root       44: {
1.1.1.2 ! root       45:        HDIR            DirHandle = -1; /* use any available directory handle */
        !            46:        USHORT          SearchCount;    /* number of files to search for */
        !            47:        USHORT          Attribute = ATTR_DIRECTORY;     /* default attribute */
        !            48:        USHORT          rc;                             /* return code */
        !            49:        USHORT          listType = NAME_MULTICOLM;      /* default output */
        !            50:        CHAR            FilePath[FILEPATHSIZE];
        !            51:        NPSZ            s;
        !            52:        PFILEFINDBUF    ResultBuf;     /* pointer to returned data */
1.1       root       53: 
                     54:        /* parse command line for switches */
                     55: 
                     56:        while ((--argc > 0) && (**++argv == '-')) {
                     57:            for (s = argv[0]+1; *s != '\0'; s++)
                     58: 
                     59:                switch(*s) {
                     60: 
                     61:                case 'a': Attribute |= (ATTR_HIDDEN | ATTR_SYSTEM);
                     62:                          break;
                     63: 
                     64:                case 'l': listType = LONG_LIST;
                     65:                          Attribute |= (ATTR_HIDDEN | ATTR_SYSTEM);
                     66:                          break;
                     67:        
                     68:                case '1': listType = NAME_SINGCOLM;
                     69:                          break;
                     70: 
                     71:                default: printf("usage: filelist [ -1al ] name...\n");
1.1.1.2 ! root       72:                         DosExit(EXIT_PROCESS, 0);
1.1       root       73:                }
                     74:        }
                     75: 
                     76:        /* allocate buffer for file data returned from find calls */
                     77: 
                     78:        if ((ResultBuf =
1.1.1.2 ! root       79:                (PFILEFINDBUF) malloc(RESULTBUFLEN)) == NULL) {
1.1       root       80: 
                     81:            printf("error, not enough memory\n");
1.1.1.2 ! root       82:            DosExit(EXIT_PROCESS, 0);
1.1       root       83:        }
                     84: 
                     85:        do {
                     86: 
                     87:            if (argc > 0) {
                     88:                if (strlen(*argv) > MAXPATHSIZE) {
                     89:                        printf("error, path too large\n");
1.1.1.2 ! root       90:                        DosExit(EXIT_PROCESS, 1);
1.1       root       91:                }
                     92:                else {
                     93:                        /* if path ends with a \, append "*.*" */
                     94: 
                     95:                        strcpy(FilePath, *argv);
                     96:                        if (FilePath[strlen(*argv) - 1] == '\\')
                     97:                                strcat(FilePath, SEARCHALL);
                     98:                }
                     99:            }
                    100:            else
                    101:                strcpy(FilePath, SEARCHALL);    /* search using "*.*" */
                    102: 
                    103:            SearchCount = 1;            /* search for one at a time */
                    104: 
                    105:            /* search for first occurance of file search pattern */
                    106: 
1.1.1.2 ! root      107:            if (rc = DosFindFirst( (PSZ) FilePath,
        !           108:                                   &DirHandle,
1.1       root      109:                                   Attribute,
                    110:                                   ResultBuf,   /* ptr to returned data */
                    111:                                   RESULTBUFLEN,
1.1.1.2 ! root      112:                                   &SearchCount,
1.1       root      113:                                   0L )) {
                    114: 
                    115:                if (rc != ERROR_NO_MORE_FILES) {
                    116:                    printf("\nFindFirst failed, error %d\n", rc);
1.1.1.2 ! root      117:                    DosExit(EXIT_PROCESS, 1);
1.1       root      118:                }
                    119: 
                    120:            } else {
                    121: 
                    122:                printDirEntry( ResultBuf, listType );   /* print file data */
                    123: 
                    124:                do {
                    125: 
                    126:                    /* search for next occurance of search pattern */
                    127: 
1.1.1.2 ! root      128:                    if (rc = DosFindNext( DirHandle,
1.1       root      129:                                          ResultBuf,
                    130:                                          RESULTBUFLEN,
1.1.1.2 ! root      131:                                          &SearchCount )) {
1.1       root      132: 
                    133:                        if (rc != ERROR_NO_MORE_FILES)  {
                    134:                            printf("DosFindNext failed, error: %d\n", rc);
1.1.1.2 ! root      135:                            DosExit(EXIT_PROCESS, 1);
1.1       root      136:                        }
                    137:                    }
                    138:                    else
                    139:                        /* print file data */
                    140: 
                    141:                        printDirEntry( ResultBuf, listType );
                    142:        
                    143:                } while (SearchCount > 0);      /* when 0, no more files */
                    144:            }
                    145: 
1.1.1.2 ! root      146:            DosFindClose( DirHandle );          /* free directory handle */
1.1       root      147: 
                    148:            if (argc > 1)       /* if any more arguments */
                    149:                argv++;         /* point to next one */
                    150: 
                    151:        } while (--argc > 0);                   /* for all filenames */
                    152: }
                    153:        
                    154: 
                    155: /*
                    156:  * This routine prints the data on the file found.
                    157:  */
                    158: 
                    159: printDirEntry(dirEntry, ltype)
1.1.1.2 ! root      160:        PFILEFINDBUF dirEntry;
        !           161:        USHORT       ltype;
1.1       root      162: {
                    163:        static int      colm = 0;
                    164:        char            attStr[6];
                    165: 
                    166: 
                    167:        if (ltype == NAME_SINGCOLM)
1.1.1.2 ! root      168:            printf("%s\n", dirEntry->achName);
1.1       root      169: 
                    170:        else if (ltype == NAME_MULTICOLM) {
                    171: 
1.1.1.2 ! root      172:            printf("%-15s", dirEntry->achName);
1.1       root      173: 
                    174:            if (++colm == TOTALCOLMS) {
                    175:                printf("\n");
                    176:                colm = 0;
                    177:            }
                    178:        }
                    179:        else {
                    180: 
                    181:           /* build attribute string */
                    182: 
                    183:           strcpy(attStr, "-----");
1.1.1.2 ! root      184:           if (dirEntry->attrFile & ATTR_DIRECTORY)
1.1       root      185:                attStr[0] = 'd';
1.1.1.2 ! root      186:           if (dirEntry->attrFile * ATTR_ARCHIVE)
1.1       root      187:                attStr[1] = 'a';
1.1.1.2 ! root      188:           if (dirEntry->attrFile & ATTR_SYSTEM)
1.1       root      189:                attStr[2] = 's';
1.1.1.2 ! root      190:           if (dirEntry->attrFile & ATTR_HIDDEN)
1.1       root      191:                attStr[3] = 'h';
1.1.1.2 ! root      192:           if (dirEntry->attrFile & ATTR_READONLY)
1.1       root      193:                attStr[4] = 'r';
                    194: 
                    195:           printf("%-7s", attStr);                      /* print attribute */
1.1.1.2 ! root      196:           printf("%7lu", dirEntry->cbFile);            /* print file size */
        !           197:           printf("  %s\n", dirEntry->achName);/* print filename */
1.1       root      198:        }
                    199: }

unix.superglobalmegacorp.com

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