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

1.1       root        1: /*
                      2:  * This example illustrates the use of DOSFINDFIRST, DOSFINDNEXT, and
                      3:  * DOSFINDCLOSE.  The program lists the contents of a directory.
                      4:  * the parameter format is: FILELIST [-1al] names...
                      5:  */
                      6: 
                      7: #include <malloc.h>
                      8: #include <stdio.h>
                      9: #include <time.h>
                     10: #include <doscalls.h>
                     11: #include <subcalls.h>
                     12: 
                     13: #define        TRUE    0
                     14: #define        FALSE   1
                     15: 
                     16: /* error codes */
                     17: 
                     18: #define ERROR_NO_MORE_FILES    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: 
                     35: #define        RESULTBUFLEN    sizeof(struct FileFindBuf)      /* size of ResultBuf */
                     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;
                     43:        char    *argv[];
                     44: {
                     45:        unsigned        DirHandle = -1; /* use any available directory handle */
                     46:        unsigned        SearchCount;    /* number of files to search for */
                     47:        unsigned        Attribute = ATTR_DIRECTORY;     /* default attribute */
                     48:        unsigned        rc;                             /* return code */
                     49:        unsigned        listType = NAME_MULTICOLM;      /* default output */
                     50:        char            FilePath[FILEPATHSIZE];
                     51:        char            *s;
                     52:        struct FileFindBuf *ResultBuf;     /* pointer to returned data */
                     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");
                     72:                         DOSEXIT(1, 0);
                     73:                }
                     74:        }
                     75: 
                     76:        /* allocate buffer for file data returned from find calls */
                     77: 
                     78:        if ((ResultBuf =
                     79:                (struct FileFindBuf *) malloc(RESULTBUFLEN)) == NULL) {
                     80: 
                     81:            printf("error, not enough memory\n");
                     82:            DOSEXIT(1, 0);
                     83:        }
                     84: 
                     85:        do {
                     86: 
                     87:            if (argc > 0) {
                     88:                if (strlen(*argv) > MAXPATHSIZE) {
                     89:                        printf("error, path too large\n");
                     90:                        DOSEXIT(1, 1);
                     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: 
                    107:            if (rc = DOSFINDFIRST( (char far *) FilePath,
                    108:                                   (unsigned far *) &DirHandle,
                    109:                                   Attribute,
                    110:                                   ResultBuf,   /* ptr to returned data */
                    111:                                   RESULTBUFLEN,
                    112:                                   (unsigned far *) &SearchCount,
                    113:                                   0L )) {
                    114: 
                    115:                if (rc != ERROR_NO_MORE_FILES) {
                    116:                    printf("\nFindFirst failed, error %d\n", rc);
                    117:                    DOSEXIT(1, 1);
                    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: 
                    128:                    if (rc = DOSFINDNEXT( DirHandle,
                    129:                                          ResultBuf,
                    130:                                          RESULTBUFLEN,
                    131:                                          (unsigned far *) &SearchCount )) {
                    132: 
                    133:                        if (rc != ERROR_NO_MORE_FILES)  {
                    134:                            printf("DosFindNext failed, error: %d\n", rc);
                    135:                            DOSEXIT(1, 1);
                    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: 
                    146:            DOSFINDCLOSE( DirHandle );          /* free directory handle */
                    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)
                    160:        struct FileFindBuf *dirEntry;
                    161:        unsigned                ltype;
                    162: {
                    163:        static int      colm = 0;
                    164:        char            attStr[6];
                    165:        long            dateTime;
                    166: 
                    167: 
                    168:        if (ltype == NAME_SINGCOLM)
                    169:            printf("%s\n", dirEntry->file_name);
                    170: 
                    171:        else if (ltype == NAME_MULTICOLM) {
                    172: 
                    173:            printf("%-15s", dirEntry->file_name);
                    174: 
                    175:            if (++colm == TOTALCOLMS) {
                    176:                printf("\n");
                    177:                colm = 0;
                    178:            }
                    179:        }
                    180:        else {
                    181: 
                    182:           /* build attribute string */
                    183: 
                    184:           strcpy(attStr, "-----");
                    185:           if (dirEntry->attributes & ATTR_DIRECTORY)
                    186:                attStr[0] = 'd';
                    187:           if (dirEntry->attributes * ATTR_ARCHIVE)
                    188:                attStr[1] = 'a';
                    189:           if (dirEntry->attributes & ATTR_SYSTEM)
                    190:                attStr[2] = 's';
                    191:           if (dirEntry->attributes & ATTR_HIDDEN)
                    192:                attStr[3] = 'h';
                    193:           if (dirEntry->attributes & ATTR_READONLY)
                    194:                attStr[4] = 'r';
                    195: 
                    196:           printf("%-7s", attStr);                      /* print attribute */
                    197:           printf("%7lu", dirEntry->file_size);         /* print file size */
                    198:           printf("  %s\n", dirEntry->file_name);/* print filename */
                    199:        }
                    200: }

unix.superglobalmegacorp.com

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