GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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__ls filtering. More...
 
void G_set_ls_exclude_filter (ls_filter_func *func, void *closure)
 
char ** G__ls (const char *dir, int *num_files)
 Stores a sorted directory listing in an array. More...
 
void G_ls (const char *dir, FILE *stream)
 Prints a directory listing to a stream, in prettified column format. More...
 
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. More...
 

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

typedef int ls_filter_func(const char *, void *)

Definition at line 31 of file ls.c.

Function Documentation

char** G__ls ( 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
dirDirectory to list
num_filesPointer 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 88 of file ls.c.

References G_fatal_error(), G_store(), n, NULL, opendir(), and readdir().

Referenced by G_ls(), and list_mapsets().

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
dirDirectory to list
streamStream to print listing to

Definition at line 129 of file ls.c.

References G__ls(), G_free(), G_ls_format(), and n.

Referenced by list_locations().

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
listArray of strings containing items to be printed
num_itemsNumber of items in the array
perlineNumber of items to print per line, 0 for autodetect
streamStream to print listing to

Definition at line 159 of file ls.c.

References max.

Referenced by G_ls().

void G_set_ls_exclude_filter ( ls_filter_func func,
void *  closure 
)

Definition at line 66 of file ls.c.

void G_set_ls_filter ( ls_filter_func func,
void *  closure 
)

Sets a function and its complementary data for G__ls filtering.

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

Parameters
funcFilter 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.
closureData used to determine if a file name matches the rule.

Definition at line 60 of file ls.c.