www.gibmonks.com

  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 9.  Working with Files and Programs


File Attributes

There are several file operations that return specific file attributes: atime, executable, exists, isdirectory, isfile, mtime, owned, readable, readlink, size and type. Refer to Table 9-2 on page 102 for their function. The following command uses file mtime to compare the modify times of two files. If you have ever resorted to piping the results of ls -l into awk in order to derive this information in other shell scripts, you will appreciate this example:

Example 9-2 Comparing file modify times.
proc newer { file1 file2 } {
   if ![file exists $file2] {
      return 1
   } else {
      # Assume file1 exists
      expr [file mtime $file1] > [file mtime $file2]
   }
}

The stat and lstat operations return a collection of file attributes. They take a third argument that is the name of an array variable, and they initialize that array with elements that contain the file attributes. If the file is a symbolic link, then the lstat operation returns information about the link itself and the stat operation returns information about the target of the link. The array elements are listed in Table 9-3. All the element values are decimal strings, except for type, which can have the values returned by the type option. The element names are based on the UNIX stat system call. Use the file attributes command described later to get other platform-specific attributes:

Table 9-3. Array elements defined by file stat.
atimeThe last access time, in seconds.
ctimeThe last change time (not the create time), in seconds.
devThe device identifier, an integer.
gidThe group owner, an integer.
inoThe file number (i.e., inode number), an integer.
modeThe permission bits.
mtimeThe last modify time, in seconds.
nlinkThe number of links, or directory references, to the file.
sizeThe number of bytes in the file.
typefile, directory, characterSpecial, blockSpecial, fifo, link, or socket.
uidThe owner's user ID, an integer.

Example 9-3 uses the device (dev) and inode (ino) attributes of a file to determine whether two pathnames reference the same file. The attributes are UNIX specific; they are not well defined on Windows and Macintosh.

Example 9-3 Determining whether pathnames reference the same file.
proc fileeq { path1 path2 } {
   file stat $path1 stat1
   file stat $path2 stat2
   expr $stat1(ino) == $stat2(ino) && \
          $stat1(dev) == $stat2(dev)
}

The file attributes operation was added in Tcl 8.0 to provide access to platform-specific attributes. The attributes operation lets you set and query attributes. The interface uses option-value pairs. With no options, all the current values are returned.

file attributes book.doc
=> -creator FRAM -hidden 0 -readonly 0 -type MAKR

These Macintosh attributes are explained in Table 9-4. The four-character type codes used on Macintosh are illustrated on page 516. With a single option, only that value is returned:

file attributes book.doc -readonly
=> 0

The attributes are modified by specifying one or more option–value pairs. Setting attributes can raise an error if you do not have the right permissions:

file attributes book.doc -readonly 1 -hidden 0

Table 9-4. Platform-specific file attributes.
-permissions modeFile permission bits. mode is a number with bits defined by the chmod system call. (UNIX)
-group IDThe group owner of the file. (UNIX)
-owner IDThe owner of the file. (UNIX)
-archive boolThe archive bit, which is set by backup programs. (Windows)
-hidden boolIf set, then the file does not appear in listings. (Windows, Macintosh)
-readonly boolIf set, then you cannot write the file. (Windows, Macintosh)
-system boolIf set, then you cannot remove the file. (Windows)
-creator typetype is 4-character code of creating application. (Macintosh)
-type typetype is 4-character type code. (Macintosh)


      Previous section   Next section
    Top