Loading...
Searching...
No Matches
ls.c File Reference

Functions to list the files in a directory. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <sys/types.h>`
`#include <dirent.h>`
`#include <unistd.h>`
`#include <grass/gis.h>`
`#include <grass/config.h>`
`#include <grass/glocale.h>`
Include dependency graph for ls.c:

Go to the source code of this file.

## Typedefs

typedef int | ls_filter_func(const char *, void *)

## Functions

void | G_set_ls_filter (ls_filter_func *func, void *closure)
| Sets a function and its complementary data for G_ls2 filtering.
void | G_set_ls_exclude_filter (ls_filter_func *func, void *closure)
char ** | G_ls2 (const char *dir, int *num_files)
| Stores a sorted directory listing in an array.
void | G_ls (const char *dir, FILE *stream)
| Prints a directory listing to a stream, in prettified column format.
void | G_ls_format (char **list, int num_items, int perline, FILE *stream)
| Prints a listing of items to a stream, in prettified column format.

## Detailed Description

Functions to list the files in a directory.

Author
  Paul Kelly

(C) 2007, 2008 by the GRASS Development Team

This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.

Definition in file ls.c.

## Typedef Documentation

## ls_filter_func

typedef int ls_filter_func(const char *, void *)

Definition at line 34 of file ls.c.

## Function Documentation

## G_ls()

void G_ls | ( | const char * | dir,
|  | FILE * | stream )

Prints a directory listing to a stream, in prettified column format.

A replacement for system("ls -C"). Lists the contents of the directory specified to the given stream, e.g. stderr. Tries to determine an appropriate column width to keep the number of lines used to a minimum and look pretty on the screen.

Parameters

  dir | Directory to list
  stream | Stream to print listing to

Definition at line 137 of file ls.c.

References G_free(), G_ls2(), and G_ls_format().

## G_ls2()

char ** G_ls2 | ( | const char * | dir,
|  | int * | num_files )

Stores a sorted directory listing in an array.

The filenames in the specified directory are stored in an array of strings, then sorted alphabetically. Each filename has space allocated using G_store(), which can be freed using G_free() if necessary. The same goes for the array itself.

Parameters

  dir | Directory to list
  num_files | Pointer to an integer in which the total number of files listed will be stored

Returns
  Pointer to array of strings containing the listing

Definition at line 94 of file ls.c.

References G_fatal_error(), G_store(), and NULL.

Referenced by G_ls().

## G_ls_format()

void G_ls_format | ( | char ** | list,
|  | int | num_items,
|  | int | perline,
|  | FILE * | stream )

Prints a listing of items to a stream, in prettified column format.

Lists the contents of the array passed to the given stream, e.g. stderr. Prints the number of items specified by "perline" to each line, unless perline is given as 0 in which case the function tries to determine an appropriate column width to keep the number of lines used to a minimum and look pretty on the screen.

Parameters

  list | Array of strings containing items to be printed
  num_items | Number of items in the array
  perline | Number of items to print per line, 0 for autodetect
  stream | Stream to print listing to

Definition at line 165 of file ls.c.

References list, max, and screen_width.

Referenced by G_ls().

## G_set_ls_exclude_filter()

void G_set_ls_exclude_filter | ( | ls_filter_func * | func,
|  | void * | closure )

Definition at line 72 of file ls.c.

Referenced by G_ls_regex_filter().

## G_set_ls_filter()

void G_set_ls_filter | ( | ls_filter_func * | func,
|  | void * | closure )

Sets a function and its complementary data for G_ls2 filtering.

Defines a filter function and its rule data that allow G_ls2 to filter out unwanted file names. Call this function before G_ls2.

Parameters

  func | Filter callback function to compare a file name and closure pattern (if NULL, no filter will be used). func(filename, closure) should return 1 on success, 0 on failure.
  closure | Data used to determine if a file name matches the rule.

Definition at line 66 of file ls.c.

Referenced by G_ls_regex_filter().