SLSH Library Reference (version 2.3.0): Filename Globbing and Related Functions

Next Previous Contents

## 4. Filename Globbing and Related Functions

The `glob` function is defined `glob.sl`. The `fswalk_new` may be used for more generic filesystem processing. It is defined in `fswalk.sl`.

## 4.1 glob

Synopsis

  Find files using wildcards

Usage

  `files = glob (pattern1, ..., patternN)`;

Description

  This function returns a list of files whose names match the specified globbing patterns. A globbing pattern is one in which '?' matches a single character, and '*' matches 0 or more characters.

Example

  > `

  >        files = glob ("*.c", "*.h");

  > `

See Also

  `glob_to_regexp, fswalk_new`

## 4.2 fswalk_new

Synopsis

  Create an object to walk the filesystem tree

Usage

  `obj = fswalk_new (Ref_Type dirfunc, Ref_Type filefunc; qualifiers)`

Description

  The `fswalk_new` function creates an object that is useful for exploring a filesystem tree. It requires two arguments that are references to functions to be called when a directory or file is encountered. Each of these functions is passed at least two arguments: the name of the file or directory (including leading path elements relative to the directory where processing started), and the stat structure of the of the file or directory. Qualifiers may be used to specify additional arguments.

  The object's `walk` method is the one that actually walks the filesystem.

  The directory callback function must return an integer value that indicates how it should be processed. If the function returns 0, then the directory will be skipped (pruned). A positive value indicates that the directory will processed. If the function returns a negative value, then no further processing by the walk function will take place and control will pass to the user.

  The file callback function must also return an integer that indicates how processing should continue. If it returns a positive value, then additional files in the corresponding directory will be processed. If it returns 0, then no further files or subdirectories of the directory will be processed, and processing will continue to take place in the parent directory. Otherwise, the return value is negative, which indicates that processing should be stopped and control will pass back to the caller.

Qualifiers

  The following qualifiers are supported:

  > `

  >        dargs={args...}

  > `

  `dargs` is a list of additional arguments that will be added when calling the directory callback function.

  > `

  >        fargs={args...}

  > `

  `fargs` is a list of additional arguments that will be added when calling the file callback function.

  > `

  >        followlinks[=val]

  > `

  The `followlinks` qualifier may be used to indicate whether or not directories that are symbolic links are to be followed. By default, they are not. If `followlinks` is present with no value, or has a non-zero value, then symbolic links will be followed. Otherwise, if `followlinks` is not present, or is set to 0, then directories that are symbolic links will be skipped.
Methods

  ``

  > `

  >        .walk (String_Type top_dir)

  > `

  The `.walk` function walks the filesystem starting at the specified top-level directory calling the directory and file callback functions as it goes.
Example

  Print a list of all files containing a `.png` extension under the current directory:

  > `

  >          private define file_callback (name, st)
  >          {
  >             if (".png" == path_extname (name))
  >               message (name);
  >             return 1;
  >          }
  >          variable w = fswalk_new (NULL, &file_callback);
  >          w.walk (".");

  > `

  Get a list of all directories that are symbolic links under /usr.

  > `

  >          private define dir_callback (name, st, list)
  >          {
  >             st = lstat_file (name);
  >             if (stat_is ("lnk", st.st_mode))
  >               {
  >                 list_append (list, name);
  >                 return 0;
  >               }
  >             return 1;
  >          }

  >          define get_symdir_list (top)
  >          {
  >             variable list = {};
  >             variable w = fswalk_new (&dir_callback, NULL
  >                                      ;dargs={list}, followlinks);
  >             w.walk (top);
  >             return list;
  >          }
  >          symdirlist = get_symdir_list ("/usr");

  > `

  Note that in this example, the dir_callback function returns 0 if the directory corresponds to a symbolic link. This causes the link to not be followed.

  Get a list of dangling symbolic links:

  > `

  >          private define file_callback (name, st, list)
  >          {
  >             if (stat_is ("lnk", st.st_mode))
  >               {
  >                  if ((NULL == stat_file (name))
  >                      && (errno == ENOENT))
  >                    list_append (list, name);
  >               }
  >             return 1;
  >          }

  >          define get_badlinks (top)
  >          {
  >             variable list = {};
  >             variable w = fswalk_new (NULL, &file_callback ;fargs={list});
  >             w.walk (top);
  >             return list;
  >          }

  > `

See Also

  `glob, stat_file, lstat_file, listdir`;

Next Previous Contents