[ Team LiB ] Previous Section Next Section

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 108 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]}
   }
}

You can use the optional time argument to mtime and atime to set the file's time attributes, like the Unix touch command. 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.

Table 9-3. Array elements defined by file stat

atime

The last access time, in seconds.

ctime

The last change time (not the create time), in seconds.

dev

The device identifier, an integer.

gid

The group owner, an integer.

ino

The file number (i.e., inode number), an integer.

mode

The permission bits.

mtime

The last modify time, in seconds.

nlink

The number of links, or directory references, to the file.

size

The number of bytes in the file.

type

file, directory, characterSpecial, blockSpecial, fifo, link, or socket.

uid

The owner's user ID, an integer.

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.

Example 9-3 uses the device (dev) and inode (ino) attributes of a file to determine whether two pathnames reference the same file. These 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 600. 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 mode

File permission bits. mode is an octal number or symbolic representation (e.g. a+x) with bits defined by the chmod system call, or a simplified ls-style string of the form rwxrwxrwx (must be 9 characters). (UNIX)

-group ID

The group owner of the file. (UNIX)

-owner ID

The owner of the file. (UNIX)

-archive bool

The archive bit, which is set by backup programs. (Windows)

-system bool

If set, then you cannot remove the file. (Windows)

-longname

The long (expanded) version of the pathname. Read-only. (Windows)

-shortname

The short (8.3) version of the pathname. Read-only. (Windows)

-hidden bool

If set, then the file does not appear in listings. (Windows, Macintosh)

-readonly bool

If set, then you cannot write the file. (Windows, Macintosh)

-creator type

type is 4-character code of creating application. (Macintosh)

-type type

type is 4-character type code. (Macintosh)

    [ Team LiB ] Previous Section Next Section