man.bsd.lv manual page server

Manual Page Search Parameters

LIBSTAND(3) Library Functions Manual LIBSTAND(3)

libstandsupport library for standalone executables

library “libstand”

#include <stand.h>

libstand provides a set of supporting functions for standalone applications, mimicking where possible the standard BSD programming environment. The following sections group these functions by kind. Unless specifically described here, see the corresponding section 3 manpages for the given functions.

String functions are available as documented in string(3) and bstring(3).

void * (size_t size)

Allocate size bytes of memory from the heap using a best-fit algorithm.

void (void *ptr)

Free the allocated object at ptr.

void (void *start, void *limit)

Initialise the heap. This function must be called before calling () for the first time. The region between start and limit will be used for the heap; attempting to allocate beyond this will result in a panic.

char * sbrk(intptr_t incr)

Provides the behaviour of (0), i.e. returns the highest point that the heap has reached. This value can be used during testing to determine the actual heap usage. The incr argument is ignored.

A set of functions are provided for manipulating a flat variable space similar to the traditional shell-supported environment. Major enhancements are support for set/unset hook functions.

char * getenv(const char *name)
int setenv(const char *name, char *value, int overwrite)
int putenv(const char *string)
int unsetenv(const char *name)

These functions behave similarly to their standard library counterparts.

struct env_var * env_getenv(const char *name)

Looks up a variable in the environment and returns its entire data structure.

int env_setenv(const char *name, int flags, char *value, ev_sethook_t sethook, ev_unsethook_t unsethook)

Creates a new or sets an existing environment variable called name. If creating a new variable, the sethook and unsethook arguments may be specified.

The set hook is invoked whenever an attempt is made to set the variable, unless the EV_NOHOOK flag is set. Typically a set hook will validate the value argument, and then call env_setenv() again with EV_NOHOOK set to actually save the value. The predefined function env_noset() may be specified to refuse all attempts to set a variable.

The unset hook is invoked when an attempt is made to unset a variable. If it returns zero, the variable will be unset. The predefined function env_nounset() may be used to prevent a variable being unset.

int (int argc, char * const *argv, const char *optstring)
long (const char *nptr, char **endptr, int base)
void (unsigned long seed)
unsigned long (void)
char * (int error)

Returns error messages for the subset of errno values supported by libstand.

(expression)

Requires <assert.h>.

int (jmp_buf env)
void (jmp_buf env, int val)

Defined as () and () respectively as there is no signal state to manipulate. Requires <setjmp.h>.

void gets(char *buf)

Read characters from the console into buf. All of the standard cautions apply to this function.

void (char *buf, size_t size)

Read at most size - 1 characters from the console into buf. If size is less than 1, the function's behaviour is as for ().

char * (char *buf, int size, int fd)

Read a line of at most size-1 characters into buf. Line terminating characters are not stripped, and the buffer is always nul-terminated. Upon successful completion a pointer to the string is returned. If end-of-file occurs before any characters are read, NULL is returned and the buffer contents remain unchanged. If an error occurs, NULL is returned and the buffer contents are indeterminate.

int (char *buf, int size, int fd)

Read a line of at most size characters into buf. Line terminating characters are stripped, and the buffer is always nul-terminated. Returns the number of characters in buf if successful, or -1 if a read error occurs.

int (const char *fmt, ...)
void (const char *fmt, va_list ap)
int (char *buf, const char *fmt, ...)
void (char *buf, const char *fmt, va_list ap)

The *printf functions implement a subset of the standard () family functionality and some extensions. The following standard conversions are supported: c, d, n, o, p, s, u, x. The following modifiers are supported: +, -, #, *, 0, field width, precision, l.

int (int c)
int (int c)
int (int c)
int (int c)
int (int c)
int (int c)
int (int c)
int (int c)
int (int c)

int open(const char *path, int flags)

Similar to the behaviour as specified in open(2), except that file creation is not supported, so the mode parameter is not required. The flags argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no filesystems currently support writing).

int (int fd)
void (void)

Close all open files.

ssize_t (int fd, void *buf, size_t len)
ssize_t (int fd, void *buf, size_t len)

(No filesystems currently support writing.)

off_t (int fd, off_t offset, int whence)

Files being automatically uncompressed during reading cannot seek backwards from the current point.

int (const char *path, struct stat *sb)
int (int fd, struct stat *sb)

The () and () functions only fill out the following fields in the sb structure: st_mode, st_nlink, st_uid, st_gid, st_size. The tftp filesystem cannot provide meaningful values for this call, and the cd9660 filesystem always reports files having uid/gid of zero.

libstand supplies a simple internal pager to ease reading the output of large commands.

void ()

Initialises the pager and tells it that the next line output will be the top of the display. The environment variable LINES is consulted to determine the number of lines to be displayed before pausing.

void (void)

Closes the pager.

int (char *lines)

Sends the lines in the nul-terminated buffer at lines to the pager. Newline characters are counted in order to determine the number of lines being output (wrapped lines are not accounted for). () will return zero when all of the lines have been output, or nonzero if the display was paused and the user elected to quit.

int (char *fname)

Attempts to open and display the file fname. Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.

char * (u_char *ap)

Convert an ethernet address to its human readable notation as specified in IEEE 802.

void (void)

Successive calls emit the characters in the sequence |, /, -, \ followed by a backspace in order to provide reassurance to the user.

The following resources are consumed by libstand - stack, heap, console and devices.

The stack must be established before libstand functions can be invoked. Stack requirements vary depending on the functions and filesystems used by the consumer and the support layer functions detailed below.

The heap must be established before calling () or open() by calling setheap(). Heap usage will vary depending on the number of simultaneously open files, as well as client behaviour. Automatic decompression will allocate more than 64K of data per open file.

Console access is performed via the (), putchar() and () functions detailed below.

Device access is initiated via () and is performed through the (), () and () functions in the device switch structure that devopen() returns.

The consumer must provide the following support functions:

int (void)

Return a character from the console, used by (), ngets() and pager functions.

int (void)

Returns nonzero if a character is waiting from the console.

void (int)

Write a character to the console, used by (), ngets(), (), panic() and twiddle() and thus by many other functions for debugging and informational output.

int devopen(struct open_file *of, const char *name, char **file)

Open the appropriate device for the file named in name, returning in file a pointer to the remaining body of name which does not refer to the device. The f_dev field in of will be set to point to the devsw structure for the opened device if successful. Device identifiers must always precede the path component, but may otherwise be arbitrarily formatted. Used by () and thus for all device-related I/O.

int (struct open_file *of)

Close the device allocated for of. The device driver itself will already have been called for the close; this call should clean up any allocation made by () only.

void (const char *msg, ...)

Signal a fatal and unrecoverable error condition. The msg ... arguments are as for ().

Internal filesystems are enabled by the consumer exporting the array struct fs_ops *file_system[], which should be initialised with pointers to struct fs_ops structures. The following filesystem handlers are supplied by libstand, the consumer may supply other filesystems of their own:

ufs_fsops
The BSD UFS(5).
hammer_fsops
HAMMER(5) filesystem.
ext2fs_fsops
Linux ext2fs filesystem.
msdos_fsops
MS-DOS filesystem.
tftp_fsops
File access via TFTP.
nfs_fsops
File access via NFS.
cd9660_fsops
ISO 9660 (CD-ROM) filesystem.
gzipfs_fsops
Stacked filesystem supporting gzipped files. When trying the gzipfs filesystem, libstand appends .gz to the end of the filename, and then tries to locate the file using the other filesystems. Placement of this filesystem in the file_system[] array determines whether gzipped files will be opened in preference to non-gzipped files. It is only possible to seek a gzipped file forwards, and stat() and fstat() on gzipped files will report an invalid length.
bzipfs_fsops
The same as gzipfs_fsops, but for bzip2(1)-compressed files.

The array of struct fs_ops pointers should be terminated with a NULL.

Devices are exported by the supporting code via the array struct devsw *devsw[] which is a NULL terminated array of pointers to device switch structures.

libstand contains contributions from many sources, including:

The reorganisation and port to FreeBSD 3.0, the environment functions and this manpage were written by Mike Smith <msmith@FreeBSD.org>.

The lack of detailed memory usage data is unhelpful.

April 8, 2019 DragonFly-5.6.1