GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 <termios.h>
#include <sys/ioctl.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. More...
 
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. 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

◆ ls_filter_func

typedef int ls_filter_func(const char *, void *)

Definition at line 35 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
dirDirectory to list
streamStream 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
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 95 of file ls.c.

References _, G_fatal_error(), G_realloc, G_store(), NULL, opendir(), and readdir().

Referenced by G_ls(), and list_subgroups().

◆ 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
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 165 of file ls.c.

References 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 73 of file ls.c.

◆ 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
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 67 of file ls.c.