An Introduction To Embedded Tk (page 19 of 32)

[Previous Page][Next Page][Table of Contents]

9.2 Tcl/Tk Variables Created By ET

Besides the 3 global C variables, ET also provides two Tcl/Tk variables that are of frequent use: cmd_name and cmd_dir. The cmd_name variable contains the name of the file holding the executable for the application, and cmd_dir is the name of the directory containing that file.

The cmd_name and cmd_dir variables are useful to programs that need to read or write auxiliary data files. In order to open an auxiliary file, the program needs to know the files pathname, but it is not a good idea to hard-code a complete pathname into the program. Otherwise, the auxiliary file can't be moved without recompiling the program. By careful use of cmd_name and/or cmd_dir, we can arrange to have auxiliary files located in a directory relative to the executable, rather that at some fixed location. That way, a system adminstrator is free to move the auxiliary file to a different directory as long as the executable moves with it.

For example, suppose you are writing a program named acctrec that needs to access a data file named acctrec.db. Furthermore, suppose the data file is located in a directory ../data relative to the executable. Then to open the data file for reading, a program could write:

  char *fullName = ET_STR( return $cmd_dir/../data/$cmd_name.db );
  FILE *fp = fopen(fullName,"r");
Using this scheme, both the executable and the datafile can be placed anywhere in the filesystem, as long as they are in the same position relative to one another. They can also be renamed, so long as they retain the same base name. This flexibility is a boon to system adminstraters, and also make the program less sensitive to installation errors.

[Next Page][Table of Contents]