home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / docum / advdos.doc / s1c09 < prev    next >
Encoding:
Text File  |  1992-04-21  |  25.0 KB  |  492 lines

  1. ────────────────────────────────────────────────────────────────────────────
  2. Chapter 9  Volumes and Directories
  3.  
  4.   Each file in an MS-DOS system is uniquely identified by its name and its
  5.   location. The location, in turn, has two components: the logical drive
  6.   that contains the file and the directory on that drive where the filename
  7.   can be found.
  8.  
  9.   Logical drives are specified by a single letter followed by a colon (for
  10.   example, A:). The number of logical drives in a system is not necessarily
  11.   the same as the number of physical drives; for example, it is common for
  12.   large fixed-disk drives to be divided into two or more logical drives. The
  13.   key aspect of a logical drive is that it contains a self-sufficient file
  14.   system; that is, it contains one or more directories, zero or more
  15.   complete files, and all the information needed to locate the files and
  16.   directories and to determine which disk space is free and which is already
  17.   in use.
  18.  
  19.   Directories are simply lists or catalogs. Each entry in a directory
  20.   consists of the name, size, starting location, attributes, and last
  21.   modification date and time of a file or another directory that the disk
  22.   contains. The detailed information about the location of every block of
  23.   data assigned to a file or directory is in a separate control area on the
  24.   disk called the file allocation table (FAT). (See Chapter 10 for a
  25.   detailed discussion of the internal format of directories and the FAT.)
  26.  
  27.   Every disk potentially has two distinct kinds of directories: the root
  28.   directory and all other directories. The root directory is always present
  29.   and has a maximum number of entries, determined when the disk is
  30.   formatted; this number cannot be changed. The subdirectories of the root
  31.   directory, which may or may not be present on a given disk, can be nested
  32.   to any level and can grow to any size (Figure 9-1). This is the
  33.   hierarchical, or tree, directory structure referred to in earlier
  34.   chapters. Every directory has a name, except for the root directory, which
  35.   is designated by a single backslash (\) character.
  36.  
  37.   MS-DOS keeps track of a "current drive" for the system and uses this drive
  38.   when a file specification does not include an explicit drive code.
  39.   Similarly, MS-DOS maintains a "current directory" for each logical drive.
  40.   You can select any particular directory on a drive by naming in order──
  41.   either from the root directory or relative to the current directory──the
  42.   directories that lead to its location in the tree structure. Such a list
  43.   of directories, separated by backslash delimiters, is called a path. When
  44.   a complete path from the root directory is prefixed by a logical drive
  45.   code and followed by a filename and extension, the resulting string is a
  46.   fully qualified filename and unambiguously specifies a file.
  47.  
  48.                            ┌────────────┐
  49.                            │   Drive    │
  50.                            │ identifier │
  51.                            └─────┬──────┘
  52.                                  │
  53.                          ┌───────┴────────┐
  54.                          │ Root directory │
  55.                          │ (volume label) │
  56.                          └─┬──┬──┬───┬──┬─┘
  57.        ┌───────────────────┘  │  │   │  └───────────────────┐
  58.        │          ┌───────────┘  │   └───────────┐          │
  59.   ┌────┴───┐ ┌────┴──────┐   ┌───┴────┐   ┌──────┴────┐ ┌───┴────┐
  60.   │ File A │ │ Directory │   │ File B │   │ Directory │ │ File C │
  61.   └────────┘ └─┬───────┬─┘   └────────┘   └─┬─────────┘ └─┬──────┘
  62.                │       │                    │             │
  63.                │       │                    │             │
  64.          ┌─────┘       │                    │             │
  65.          │             │                    │             │
  66.     ┌────┴──────┐   ┌──┴─────┐        ┌─────┴──┐      ┌───┴────┐
  67.     │ Directory │   │ File D │        │ File E │      │ File F │
  68.     └───────────┘   └────────┘        └────────┘      └────────┘
  69.  
  70.   Figure 9-1.  An MS-DOS file-system structure.
  71.  
  72.  
  73. Drive and Directory Control
  74.  
  75.   You can examine, select, create, and delete disk directories interactively
  76.   with the DIR, CHDIR (CD), MKDIR (MD), and RMDIR (RD) commands. You can
  77.   select a new current drive by entering the letter of the desired drive,
  78.   followed by a colon. MS-DOS provides the following Int 21H functions to
  79.   give application programs similar control over drives and directories:
  80.  
  81.   Function                 Action
  82.   ──────────────────────────────────────────────────────────────────────────
  83.   0EH                     Select current drive.
  84.   19H                     Get current drive.
  85.   39H                     Create directory.
  86.   3AH                     Remove directory.
  87.   3BH                     Select current directory.
  88.   47H                     Get current directory.
  89.   ──────────────────────────────────────────────────────────────────────────
  90.  
  91.   The two functions that deal with disk drives accept or return a binary
  92.   drive code──0 represents drive A, 1 represents drive B, and so on. This
  93.   differs from most other MS-DOS functions, which use 0 to indicate the
  94.   current drive, 1 for drive A, and so on.
  95.  
  96.   The first three directory functions in the preceding list require an
  97.   ASCIIZ string that describes the path to the desired directory. As with
  98.   the handle-based file open and create functions, the address of the ASCIIZ
  99.   string is passed in the DS:DX registers. On return, the carry flag is
  100.   clear if the function succeeds or set if the function failed, with an
  101.   error code in the AX register. The directory functions can fail for a
  102.   variety of reasons, but the most common cause of an error is that some
  103.   element of the indicated path does not exist.
  104.  
  105.   The last function in the preceding list, Int 21H Function 47H, allows you
  106.   to obtain an ASCIIZ path for the current directory on the specified or
  107.   default drive. MS-DOS supplies the path string without the drive
  108.   identifier or a leading backslash. Int 21H Function 47H is most commonly
  109.   used with Int 21H Function 19H to build fully qualified filenames. Such
  110.   filenames are desirable because they remain valid if the user changes the
  111.   current drive or directory.
  112.  
  113.   Section 2 of this book, "MS-DOS Functions Reference," gives detailed
  114.   information on the drive and directory control functions.
  115.  
  116. Searching Directories
  117.  
  118.   When you request an open operation on a file, you are implicitly
  119.   performing a search of a directory. MS-DOS examines each entry of the
  120.   directory to find a match for the filename you have given as an argument;
  121.   if the file is found, MS-DOS copies certain information from the directory
  122.   into a data structure that it can use to control subsequent read or write
  123.   operations to the file. Thus, if you wish to test for the existence of a
  124.   specific file, you need only perform an open operation and observe whether
  125.   it is successful. (If it is, you should, of course, perform a subsequent
  126.   close operation to avoid needless expenditure of handles.)
  127.  
  128.   Sometimes you may need to perform more elaborate searches of a disk
  129.   directory. Perhaps you wish to find all the files with a certain
  130.   extension, a file with a particular attribute, or the names of the
  131.   subdirectories of a certain directory. Although the locations of a disk's
  132.   directories and the specifics of the entries that are found in them are of
  133.   necessity hardware dependent (for example, interpretation of the field
  134.   describing the starting location of a file depends upon the physical disk
  135.   format), MS-DOS does provide functions that will allow examination of a
  136.   disk directory in a hardware-independent fashion.
  137.  
  138.   In order to search a disk directory successfully, you must understand two
  139.   types of MS-DOS search services. The first type is the "search for first"
  140.   function, which accepts a file specification──possibly including wildcard
  141.   characters──and looks for the first matching file in the directory of
  142.   interest. If it finds a match, the function fills a buffer owned by the
  143.   requesting program with information about the file; if it does not find a
  144.   match, it returns an error flag.
  145.  
  146.   A program can call the second type of search service, called "search for
  147.   next," only after a successful "search for first." If the file
  148.   specification that was originally passed to "search for first" included
  149.   wildcard characters and at least one matching file was present, the
  150.   program can call "search for next" as many times as necessary to find all
  151.   additional matching files. Like "search for first," "search for next"
  152.   returns information about the matched files in a buffer designated by the
  153.   requesting program. When it can find no more matching files, "search for
  154.   next" returns an error flag.
  155.  
  156.   As with nearly every other operation, MS-DOS provides two parallel sets of
  157.   directory-searching services:
  158.  
  159.   Action             FCB function      Handle function
  160.   ──────────────────────────────────────────────────────────────────────────
  161.   Search for first   11H               4EH
  162.   Search for next    12H               4FH
  163.   ──────────────────────────────────────────────────────────────────────────
  164.  
  165.   The FCB directory functions allow searches to match a filename and
  166.   extension, both possibly containing wildcard characters, within the
  167.   current directory for the specified or current drive. The handle directory
  168.   functions, on the other hand, allow a program to perform searches within
  169.   any directory on any drive, regardless of the current directory.
  170.  
  171.   Searches that use normal FCBs find only normal files. Searches that use
  172.   extended FCBs, or the handle-type functions, can be qualified with file
  173.   attributes. The attribute bits relevant to searches are as follows:
  174.  
  175.   Bit                      Significance
  176.   ──────────────────────────────────────────────────────────────────────────
  177.   0                        Read-only file
  178.   1                        Hidden file
  179.   2                        System file
  180.   3                        Volume label
  181.   4                        Directory
  182.   5                        Archive needed (set when file modified)
  183.   ──────────────────────────────────────────────────────────────────────────
  184.  
  185.   The remaining bits of a search function's attribute parameter should be
  186.   zero. When any of the preceding attribute bits are set, the search
  187.   function returns all normal files plus any files with the specified
  188.   attributes, except in the case of the volume-label attribute bit, which
  189.   receives special treatment as described later in this chapter. Note that
  190.   by setting bit 4 you can include directories in a search, exactly as
  191.   though they were files.
  192.  
  193.   Both the FCB and handle directory-searching functions require that the
  194.   disk transfer area address be set (with Int 21H Function 1AH), before the
  195.   call to "search for first," to point to a working buffer for use by
  196.   MS-DOS. The DTA address should not be changed between calls to "search for
  197.   first" and "search for next." When it finds a matching file, MS-DOS places
  198.   the information about the file in the buffer and then inspects the buffer
  199.   on the next "search for next" call, to determine where to resume the
  200.   search. The format of the data returned in the buffer is different for the
  201.   FCB and handle functions, so read the detailed descriptions in Section 2
  202.   of this book, "MS-DOS Functions Reference," before attempting to interpret
  203.   the buffer contents.
  204.  
  205.   Figures 9-2 and 9-3 provide equivalent examples of searches for all
  206.   files in a given directory that have the .ASM extension, one example using
  207.   the FCB directory functions (Int 21H Functions 11H and 12H) and the
  208.   other using the handle functions (Int 21H Functions 4EH and 4FH). (Both
  209.   programs use the handle write function with the standard output handle to
  210.   display the matched filenames, to avoid introducing tangential differences
  211.   in the listings.)
  212.  
  213.   ──────────────────────────────────────────────────────────────────────────
  214.   start:                          ; set DTA address for buffer
  215.                                   ; used by search functions
  216.           mov     dx,seg buff     ; DS:DX = buffer address
  217.           mov     ds,dx
  218.           mov     dx,offset buff
  219.           mov     ah,1ah          ; function 1ah = search for first
  220.           int     21h             ; transfer to MS-DOS
  221.                                   ; search for first match...
  222.           mov     dx,offset fcb   ; DS:DX = FCB address
  223.           mov     ah,11h          ; function 11h = search for first
  224.           int     21h             ; transfer to MS-DOS
  225.           or      al,al           ; any matches at all?
  226.           jnz     exit            ; no, quit
  227.  
  228.   disp:                           ; go to a new line...
  229.           mov     dx,offset crlf  ; DS:DX = CR-LF string
  230.           mov     cx,2            ; CX = string length
  231.           mov     bx,1            ; BX = standard output handle
  232.           mov     ah,40h          ; function 40h = write
  233.           int     21h             ; transfer to MS-DOS
  234.  
  235.                                   ; display matching file
  236.           mov     dx,offset buff+1 ; DS:DX = filename
  237.           mov     cx,11           ; CX = length
  238.           mov     bx,1            ; BX = standard output handle
  239.           mov     ah,40h          ; function 40h = write
  240.           int     21h             ; transfer to MS-DOS
  241.  
  242.                                   ; search for next match...
  243.           mov     dx,offset fcb   ; DS:DX = FCB address
  244.           mov     ah,12h          ; function 12h = search for next
  245.           int     21h             ; transfer to MS-DOS
  246.           or      al,al           ; any more matches?
  247.           jz      disp            ; yes, go show filename
  248.  
  249.   exit:                           ; final exit point
  250.           mov     ax,4c00h        ; function 4ch = terminate,
  251.                                   ; return code = 0
  252.           int     21h             ; transfer to MS-DOS
  253.  
  254.           .
  255.           .
  256.           .
  257.  
  258.   crlf    db      0dh,0ah         ; ASCII carriage return-
  259.                                   ; linefeed string
  260.  
  261.   fcb     db      0               ; drive = current
  262.           db      8 dup ('?')     ; filename = wildcard
  263.           db      'ASM'           ; extension = ASM
  264.           db      25 dup (0)      ; remainder of FCB = zero
  265.  
  266.   buff    db      64 dup (0)      ; receives search results
  267.   ──────────────────────────────────────────────────────────────────────────
  268.  
  269.   Figure 9-2.  Example of an FCB-type directory search using Int 21H
  270.   Functions 11H and 12H. This routine displays the names of all files in
  271.   the current directory that have the .ASM extension.
  272.  
  273.   ──────────────────────────────────────────────────────────────────────────
  274.   start:                          ; set DTA address for buffer
  275.                                   ; used by search functions
  276.           mov     dx,seg buff     ; DS:DX = buffer address
  277.           mov     ds,dx
  278.           mov     dx,offset buff
  279.           mov     ah,1ah          ; function 1ah = search for first
  280.           int     21h             ; transfer to MS-DOS
  281.  
  282.                                   ; search for first match...
  283.           mov     dx,offset fname ; DS:DX = wildcard filename
  284.           mov     cx,0            ; CX = normal file attribute
  285.           mov     ah,4eh          ; function 4eh = search for first
  286.           int     21h             ; transfer to MS-DOS
  287.           jc      exit            ; quit if no matches at all
  288.  
  289.   disp:                           ; go to a new line...
  290.           mov     dx,offset crlf  ; DS:DX = CR-LF string
  291.           mov     cx,2            ; CX = string length
  292.           mov     bx,1            ; BX = standard output handle
  293.           mov     ah,40h          ; function 40h = write
  294.           int     21h             ; transfer to MS-DOS
  295.                                   ; find length of filename...
  296.           mov     cx,0            ; CX will be char count
  297.                                   ; DS:SI = start of name
  298.           mov     si,offset buff+30
  299.  
  300.   disp1:  lodsb                   ; get next character
  301.           or      al,al           ; is it null character?
  302.           jz      disp2           ; yes, found end of string
  303.           inc     cx              ; else count characters
  304.           jmp     disp1           ; and get another
  305.  
  306.   disp2:                          ; display matching file...
  307.                                   ; CX already contains length
  308.                                   ; DS:DX = filename
  309.           mov     dx,offset buff+30
  310.           mov     bx,1            ; BX = standard output handle
  311.           mov     ah,40h          ; function 40h = write
  312.           int     21h             ; transfer to MS-DOS
  313.                                   ; find next matching file...
  314.           mov     ah,4fh          ; function 4fh = search for next
  315.           int     21h             ; transfer to MS-DOS
  316.           jnc     disp            ; jump if another match found
  317.  
  318.   exit:                           ; final exit point
  319.           mov     ax,4c00h        ; function 4ch = terminate,
  320.                                   ; return code = 0
  321.           int     21h             ; transfer to MS-DOS
  322.  
  323.           .
  324.           .
  325.           .
  326.  
  327.   crlf    db      0dh,0ah         ; ASCII carriage return-
  328.                                   ; linefeed string
  329.  
  330.   fname   db      '*.ASM',0       ; ASCIIZ filename to
  331.                                   ; be matched
  332.  
  333.   buff    db      64 dup (0)      ; receives search results
  334.   ──────────────────────────────────────────────────────────────────────────
  335.  
  336.   Figure 9-3.  Example of a handle-type directory search using Int 21H
  337.   Functions 4EH and 4FH. This routine also displays the names of all files
  338.   in the current directory that have a .ASM extension.
  339.  
  340. Moving Files
  341.  
  342.   The rename file function that was added in MS-DOS version 2.0, Int 21H
  343.   Function 56H, has the little-advertised capability to move a file from
  344.   one directory to another. The function has two ASCIIZ parameters: the
  345.   "old" and "new" names for the file. If the old and new paths differ,
  346.   MS-DOS moves the file; if the filename or extension components differ,
  347.   MS-DOS renames the file. MS-DOS can carry out both of these actions in the
  348.   same function call.
  349.  
  350.   Of course, the old and new directories must be on the same drive, because
  351.   the file's actual data is not moved at all; only the information that
  352.   describes the file is removed from one directory and placed in another
  353.   directory. Function 56H fails if the two ASCIIZ strings include different
  354.   logical-drive codes, if the file is read-only, or if a file with the same
  355.   name and location as the "new" filename already exists.
  356.  
  357.   The FCB-based rename file service, Int 21H Function 17H, works only on
  358.   the current directory and cannot be used to move files.
  359.  
  360.  
  361. Volume Labels
  362.  
  363.   Support for volume labels was first added to MS-DOS in version 2.0. A
  364.   volume label is an optional name of from 1 to 11 characters that the user
  365.   assigns to a disk during a FORMAT operation. You can display a volume
  366.   label with the DIR, TREE, CHKDSK, or VOL command. Beginning with MS-DOS
  367.   version 3.0, you can use the LABEL command to add, display, or alter the
  368.   label after formatting. In MS-DOS version 4, the FORMAT program also
  369.   assigns a semi-random 32-bit binary ID to each disk it formats; you can
  370.   display this value, but you cannot change it.
  371.  
  372.   The distinction between volumes and drives is important. A volume label is
  373.   associated with a specific storage medium. A drive identifier (such as A)
  374.   is associated with a physical device that a storage medium can be mounted
  375.   on. In the case of fixed-disk drives, the medium associated with a drive
  376.   identifier does not change (hence the name). In the case of floppy disks
  377.   or other removable media, the disk accessed with a given drive identifier
  378.   might have any volume label or none at all.
  379.  
  380.   Hence, volume labels do not take the place of the logical-drive identifier
  381.   and cannot be used as part of a pathname to identify a file. In fact, in
  382.   MS-DOS version 2, the system does not use volume labels internally at all.
  383.   In MS-DOS versions 3.0 and later, a disk driver can use volume labels to
  384.   detect whether the user has replaced a disk while a file is open; this use
  385.   is optional, however, and is not implemented in all systems.
  386.  
  387.   MS-DOS volume labels are implemented as a special type of entry in a
  388.   disk's root directory. The entry contains a time-and-date stamp and has an
  389.   attribute value of 8 (i.e., bit 3 set). Except for the attribute, a volume
  390.   label is identical to the directory entry for a file that was created but
  391.   never had any data written into it, and you can manipulate volume labels
  392.   with Int 21H functions much as you manipulate files. However, a volume
  393.   label receives special handling at several levels:
  394.  
  395.   ■  When you create a volume label after a disk is formatted, MS-DOS always
  396.      places it in the root directory, regardless of the current directory.
  397.  
  398.   ■  A disk can contain only one volume label; attempts to create additional
  399.      volume labels (even with different names) will fail.
  400.  
  401.   ■  MS-DOS always carries out searches for volume labels in the root
  402.      directory, regardless of the current directory, and does not also
  403.      return all normal files.
  404.  
  405.   In MS-DOS version 2, support for volume labels is not completely
  406.   integrated into the handle file functions, and you must use extended FCBs
  407.   instead to manipulate volume labels. For example, the code in Figure 9-4
  408.   searches for the volume label in the root directory of the current drive.
  409.   You can also change volume labels with extended FCBs and the rename file
  410.   function (Int 21H Function 17H), but you should not attempt to remove an
  411.   existing volume label with Int 21H Function 13H under MS-DOS version 2,
  412.   because this operation can damage the disk's FAT in an unpredictable
  413.   manner.
  414.  
  415.   In MS-DOS versions 3.0 and later, you can create a volume label in the
  416.   expected manner, using Int 21H Function 3CH and an attribute of 8, and
  417.   you can use the handle-type "search for first" function (4EH) to obtain
  418.   an existing volume label for a logical drive (Figure 9-5). However, you
  419.   still must use extended FCBs to change a volume label.
  420.  
  421.   ──────────────────────────────────────────────────────────────────────────
  422.   buff    db      64 dup (?)   ; receives search results
  423.  
  424.   xfcb    db      0ffh         ; flag signifying extended FCB
  425.           db      5 dup (0)    ; reserved
  426.           db      8            ; volume attribute byte
  427.           db      0            ; drive code (0 = current)
  428.           db      11 dup ('?') ; wildcard filename and extension
  429.           db      25 dup (0)   ; remainder of FCB (not used)
  430.           .
  431.           .
  432.           .
  433.                                ; set DTA address for buffer
  434.                                ; used by search functions
  435.           mov     dx,seg buff  ; DS:DX = buffer address
  436.           mov     ds,dx
  437.           mov     dx,offset buff
  438.           mov     ah,1ah       ; function 1ah = set DTA
  439.           int     21h          ; transfer to MS-DOS
  440.  
  441.                                ; now search for label...
  442.                                ; DS:DX = extended FCB
  443.           mov     dx,offset xfcb
  444.           mov     ah,11h       ; function 11h = search for first
  445.           int     21h          ; transfer to MS-DOS
  446.           cmp     al,0ffh      ; search successful?
  447.           je      no_label     ; jump if no volume label
  448.           .
  449.           .
  450.           .
  451.   ──────────────────────────────────────────────────────────────────────────
  452.  
  453.   Figure 9-4.  A volume-label search under MS-DOS version 2, using an
  454.   extended file control block. If the search is successful, the volume label
  455.   is returned in buff, formatted in the filename and extension fields of an
  456.   extended FCB.
  457.  
  458.   ──────────────────────────────────────────────────────────────────────────
  459.   buff    db      64 dup (?)   ; receives search results
  460.  
  461.   wildcd  db      '*.*',0      ; wildcard ASCIIZ filename
  462.           .
  463.           .
  464.           .
  465.                                ; set DTA address for buffer
  466.                                ; used by search functions
  467.           mov     dx,seg buff  ; DS:DX = buffer address
  468.           mov     ds,dx
  469.           mov     dx,offset buff
  470.           mov     ah,1ah       ; function 1ah = set DTA
  471.           int     21h          ; transfer to MS-DOS
  472.  
  473.                                ; now search for label...
  474.                                ; DS:DX = ASCIIZ string
  475.           mov     dx,offset wildcd
  476.           mov     cx,8         ; CX = volume attribute
  477.           mov     ah,4eh       ; function 4eh = search for first
  478.           int     21h          ; transfer to MS-DOS
  479.           jc      no_label     ; jump if no volume label
  480.           .
  481.           .
  482.           .
  483.   ──────────────────────────────────────────────────────────────────────────
  484.  
  485.   Figure 9-5.  A volume-label search under MS-DOS version 3, using the
  486.   handle-type file functions. If the search is successful (carry flag
  487.   returned clear), the volume name is placed at location buff+1EH in the
  488.   form of an ASCIIZ string.
  489.  
  490.  
  491.  
  492.