Annotation of q_a/samples/drives/drives.c, revision 1.1.1.2

1.1.1.2 ! root        1: 
        !             2: /******************************************************************************\
        !             3: *       This is a part of the Microsoft Source Code Samples. 
        !             4: *       Copyright (C) 1993 Microsoft Corporation.
        !             5: *       All rights reserved. 
        !             6: *       This source code is only intended as a supplement to 
        !             7: *       Microsoft Development Tools and/or WinHelp documentation.
        !             8: *       See these sources for detailed information regarding the 
        !             9: *       Microsoft samples programs.
        !            10: \******************************************************************************/
        !            11: 
1.1       root       12: /****************************************************************************
                     13: *
                     14: *    PROGRAM: DRIVES.C
                     15: *
                     16: *    PURPOSE: Determines all drives in the system, both local and remote,
                     17: *             and their file system type
                     18: *
                     19: *    FUNCTIONS:
                     20: *
                     21: *    CheckRM() - verifies that a removeable media drive contains a disk
                     22: *    GVI() - gets the volume information for a drive and prints out the
                     23: *            file system type
                     24: *    UpdateBuffer() - updates string pointer to the next string in an array
                     25: *            of strings
                     26: *
                     27: *    COMMENTS:
                     28: *
                     29: ****************************************************************************/
                     30: 
                     31: #include <windows.h>
                     32: #include <stdio.h>
                     33: #include <string.h>
                     34: #include "drives.h"
                     35: 
                     36: #define BUFSIZE 2048
                     37: 
                     38: CHAR chBuffer[BUFSIZE];
                     39: 
                     40: /****************************************************************************
                     41: *
                     42: *    FUNCTION: CheckRM(LPTSTR)
                     43: *
                     44: *    PURPOSE: Verifies that a removeable media drive contains a disk
                     45: *
                     46: *    COMMENTS:
                     47: *
                     48: *        This function is called each time a drive type is determined to be
                     49: *        removeable (DRIVE_REMOVEABLE).  An attempt is made to open a
                     50: *        file in the root directory of the drive.  If the attempt succeeds,
                     51: *        then media is present in the drive and subsequent calls to the
                     52: *        drive can be safely made.  If the call fails, then there is no media
                     53: *        present in the drive and no attempts should be made to access this
                     54: *        drive.  The Error Mode is temporarily set to 1 to allow failures
                     55: *        to immediately return to the calling program.  This eliminates
                     56: *        unwanted dialog boxes that prompt for disks to be placed in the
                     57: *        drive.
                     58: *
                     59: *    INPUT: lpszDriveName - removeable media drive name (ex - "a:")
                     60: *
                     61: *    OUTPUT: Returns TRUE if media present
                     62: *                    FALSE if media is not present
                     63: *
                     64: ****************************************************************************/
                     65: 
                     66: BOOL CheckRM(LPTSTR lpszDriveName)
                     67: {
                     68:   int      iRC;
                     69:   LPTSTR   lpszFileName="         ";
                     70:   OFSTRUCT ofstruct;
                     71:   WORD     wStyle=OF_EXIST;
                     72: 
                     73:   SetErrorMode(1);
                     74:   strcpy(lpszFileName,lpszDriveName);
                     75:   strcat(lpszFileName,".");
                     76:   iRC=OpenFile(lpszFileName,&ofstruct,wStyle);
                     77:   SetErrorMode(0);
                     78:   if (iRC==-1)
                     79:     return (FALSE);
                     80:   _lclose(iRC);
                     81:   return (TRUE);
                     82: }
                     83: 
                     84: /****************************************************************************
                     85: *
                     86: *    FUNCTION: GVI(LPTSTR, LPTSTR, DWORD)
                     87: *
                     88: *    PURPOSE: gets the Volume Information for the specified drive and prints
                     89: *             the file system type
                     90: *
                     91: *    COMMENTS:
                     92: *        This function makes a call to GetVolumeInformation.  The file system
                     93: *        type is returned in the szSystemName buffer.  The file system type
                     94: *        is printed to the screen.
                     95: *
                     96: *    INPUT: lpszDriveName - pointer to the drive name (ex - "a:")
                     97: *           lpszSystemName - pointer to the system name buffer.  This will
                     98: *                            be filled in by the GetVolumeInformation call
                     99: *                            (e.g. FAT, HPFS)
                    100: *           nSystemNameSize - size of the lpszSystemName buffer
                    101: *
                    102: *    OUTPUT: None
                    103: *
                    104: ****************************************************************************/
                    105: 
                    106: VOID GVI(LPTSTR lpszDriveName,LPTSTR lpszSystemName, DWORD nSystemNameSize)
                    107: {
                    108:   BOOL     bRC;
                    109: 
1.1.1.2 ! root      110:   lstrcpy( lpszSystemName, "Unknown Media Type" );
1.1       root      111:   bRC=GetVolumeInformation(lpszDriveName,
                    112:                            NULL,
1.1.1.2 ! root      113:                            0,
1.1       root      114:                            NULL,
                    115:                            NULL,
                    116:                            NULL,
                    117:                            lpszSystemName,
                    118:                            nSystemNameSize);
                    119:   printf("File system = %s",lpszSystemName);
                    120: }
                    121: 
                    122: /****************************************************************************
                    123: *
                    124: *    FUNCTION: UpdateBuffer(LPTSTR)
                    125: *
                    126: *    PURPOSE: updates string pointer to the next string in an array
                    127: *             of strings
                    128: *
                    129: *    COMMENTS:
                    130: *
                    131: *    INPUT: lpszString - pointer to an array of zero terminated strings.
                    132: *                        The final string is terminated with a double null.
                    133: *
                    134: *    OUTPUT: modified pointer that points to the next string in the array
                    135: *
                    136: ****************************************************************************/
                    137: 
                    138: LPTSTR UpdateBuffer(LPTSTR lpszString)
                    139: {
                    140:   return (lpszString+strlen(lpszString)+1);
                    141: }
                    142: 
                    143: /****************************************************************************
                    144: *
                    145: *    FUNCTION: main()
                    146: *
                    147: *    PURPOSE: the main C routine
                    148: *
                    149: *    COMMENTS:
                    150: *        The number of available drives is first determined by a call to
                    151: *        GetLogicalDrives.  This provides a bit mask of all logical drives.
                    152: *        Next, a call is made to GetLogicalDriveStrings to get the valid
                    153: *        drive strings for the system.
                    154: *
                    155: *        For each logical drive, a call is made to GetDriveType to determine
                    156: *        the drive type.  If the logical drive is not in the bit mask that
                    157: *        was created with the GetLogicalDrives call, then this drive is
                    158: *        bypassed.
                    159: *
                    160: *        GetVolumeInformation is used to determine the file system
                    161: *        for the logical drive.  This information is returned in the
                    162: *        lpszFileSystemNameBuffer variable.  If the drive type is
                    163: *        remote, a check must be made to see if the drive contains a
                    164: *        disk. This is accomplished by opening '.' in the root
                    165: *        directory of the remote drive.  The error level is temporarily
                    166: *        changed from 0 to 1, to allow any OpenFileErrors to immediately
                    167: *        return to the calling function, thus eliminating any unwanted
                    168: *        dialog boxes.  If the remote drive contains a disk, then it is
                    169: *        safe to proceed with the GetVolumeInformation call.
                    170: *
                    171: *    INPUT: None
                    172: *
                    173: *    OUTPUT: None
                    174: *
                    175: ****************************************************************************/
                    176: 
                    177: VOID main(VOID)
                    178: {
                    179:   DWORD     dwDriveMask;
                    180:   DWORD     dwRC;
                    181:   DWORD     dwTemp;
                    182:   DWORD     cbDriveStringBufferSize=BUFSIZE;
                    183:   LPTSTR    lpszDriveStringBuffer=chBuffer;
                    184:   LPTSTR    lpszRootPathName="?:\\";
                    185:   LPTSTR    lpszFileSystemNameBuffer="                              ";
                    186:   DWORD     nFileSystemNameSize=sizeof("                              ");
                    187: 
                    188:   dwDriveMask=GetLogicalDrives();
                    189:   dwRC=GetLogicalDriveStrings(cbDriveStringBufferSize,lpszDriveStringBuffer);
                    190:   for (*lpszRootPathName='a';*lpszRootPathName<='z';(*lpszRootPathName)++)
                    191:     {
                    192:     dwTemp=dwDriveMask & 1;
                    193:     dwDriveMask >>= 1;
                    194:     if (dwTemp)
                    195:       {
                    196:       dwRC=GetDriveType(lpszRootPathName);
                    197:       printf("The drive type for %s is ",lpszDriveStringBuffer);
                    198:       lpszDriveStringBuffer=UpdateBuffer(lpszDriveStringBuffer);
                    199:       switch (dwRC)
                    200:         {
                    201:         case DRIVE_REMOVEABLE :
                    202:             printf("removeable. ");
                    203:             if (CheckRM(lpszRootPathName))
                    204:               GVI(lpszRootPathName,lpszFileSystemNameBuffer,nFileSystemNameSize);
                    205:             else
                    206:               printf("File system = INDETERMINATE");
                    207:             break;
                    208:         case DRIVE_FIXED      :
                    209:             printf("fixed.      ");
                    210:             GVI(lpszRootPathName,lpszFileSystemNameBuffer,nFileSystemNameSize);
                    211:             break;
                    212:         case DRIVE_REMOTE     :
                    213:             printf("remote.     ");
                    214:             GVI(lpszRootPathName,lpszFileSystemNameBuffer,nFileSystemNameSize);
                    215:             break;
                    216:         case DRIVE_CDROM      :
                    217:             printf("CD-ROM.     ");
                    218:             GVI(lpszRootPathName,lpszFileSystemNameBuffer,nFileSystemNameSize);
                    219:             break;
                    220:         case DRIVE_RAMDISK    :
                    221:             printf("RAM disk.   ");
                    222:             GVI(lpszRootPathName,lpszFileSystemNameBuffer,nFileSystemNameSize);
                    223:             break;
                    224:         default               :
                    225:             break;
                    226:         }
                    227:       printf("\n");
                    228:       }
                    229:     }
                    230: }

unix.superglobalmegacorp.com

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