1 .\" Copyright (c) Michael Smith
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" $FreeBSD: src/lib/libstand/libstand.3,v 1.5.2.11 2002/06/26 19:14:43 schweikh Exp $
26 .\" $DragonFly: src/lib/libstand/libstand.3,v 1.8 2008/05/02 02:05:04 swildner Exp $
28 .Dd September 13, 2004
33 .Nd support library for standalone executables
40 provides a set of supporting functions for standalone
41 applications, mimicking where possible the standard
44 environment. The following sections group these functions by kind.
45 Unless specifically described here, see the corresponding section 3
46 manpages for the given functions.
48 String functions are available as documented in
56 .Fn malloc "size_t size"
61 bytes of memory from the heap using a best-fit algorithm.
67 Free the allocated object at
71 .Fn setheap "void *start" "void *limit"
74 Initialise the heap. This function must be called before calling
76 for the first time. The region between
80 will be used for the heap; attempting to allocate beyond this will result
87 Provides the behaviour of
89 ie. returns the highest point that the heap has reached. This value can
90 be used during testing to determine the actual heap usage. The
95 A set of functions are provided for manipulating a flat variable space similar
96 to the traditional shell-supported environment. Major enhancements are support
97 for set/unset hook functions.
101 .Fn getenv "const char *name"
105 .Fn setenv "const char *name" "char *value" "int overwrite"
109 .Fn putenv "const char *string"
113 .Fn unsetenv "const char *name"
116 These functions behave similarly to their standard library counterparts.
118 .Ft "struct env_var *"
119 .Fn env_getenv "const char *name"
122 Looks up a variable in the environment and returns its entire
126 .Fn env_setenv "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
129 Creates a new or sets an existing environment variable called
131 If creating a new variable, the
135 arguments may be specified.
137 The set hook is invoked whenever an attempt
138 is made to set the variable, unless the EV_NOHOOK flag is set. Typically
139 a set hook will validate the
141 argument, and then call
143 again with EV_NOHOOK set to actually save the value. The predefined function
145 may be specified to refuse all attempts to set a variable.
147 The unset hook is invoked when an attempt is made to unset a variable.
149 returns zero, the variable will be unset. The predefined function
151 may be used to prevent a variable being unset.
153 .Sh STANDARD LIBRARY SUPPORT
157 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
161 .Fn strtol "const char *nptr" "char **endptr" "int base"
165 .Fn srandom "unsigned long seed"
173 .Fn strerror "int error"
176 Returns error messages for the subset of
180 .It Fn assert expression
186 .Fn setjmp "jmp_buf env"
190 .Fn longjmp "jmp_buf env" "int val"
197 respectively as there is no signal state to manipulate. Requires
207 Read characters from the console into
209 All of the standard cautions apply to this function.
212 .Fn ngets "char *buf" "size_t size"
217 - 1 characters from the console into
221 is less than 1, the function's behaviour is as for
225 .Fn fgetstr "char *buf" "int size" "int fd"
228 Read a line of at most
232 Line terminating characters are stripped, and the buffer is always nul
233 terminated. Returns the number of characters in
235 if successful, or -1 if a read error occurs.
238 .Fn printf "const char *fmt" "..."
242 .Fn vprintf "const char *fmt" "va_list ap"
246 .Fn sprintf "char *buf" "const char *fmt" "..."
250 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
253 The *printf functions implement a subset of the standard
255 family functionality and some extensions. The following standard conversions
256 are supported: c,d,n,o,p,s,u,x. The following modifiers are supported:
257 +,-,#,*,0,field width,precision,l.
261 conversion is provided to decode error registers. Its usage is:
262 .Bd -ragged -offset indent
270 where <base> is the output expressed as a control character, eg. \e10 gives
271 octal, \e20 gives hex. Each <arg> is a sequence of characters, the first of
272 which gives the bit number to be inspected (origin 1) and the next characters
273 (up to a character less than 32) give the text to be displayed if the bit is set.
275 .Bd -ragged -offset indent
279 .Qq \e10\e2BITTWO\e1BITONE\en
283 would give the output
284 .Bd -ragged -offset indent
290 conversion provides a hexdump facility, eg.
291 .Bd -ragged -offset indent
297 .Qq XX:XX:XX:XX:XX:XX
299 .Bd -ragged -offset indent
309 .Sh CHARACTER TESTS AND CONVERSIONS
352 .Fn open "const char *path" "int flags"
355 Similar to the behaviour as specified in
357 except that file creation is not supported, so the mode parameter is not
360 argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no filesystems
361 currently support writing).
371 Close all open files.
374 .Fn read "int fd" "void *buf" "size_t len"
378 .Fn write "int fd" "void *buf" "size_t len"
381 (No filesystems currently support writing.)
384 .Fn lseek "int fd" "off_t offset" "int whence"
387 Files being automatically uncompressed during reading cannot seek backwards
388 from the current point.
391 .Fn stat "const char *path" "struct stat *sb"
395 .Fn fstat "int fd" "struct stat *sb"
402 functions only fill out the following fields in the
404 structure: st_mode,st_nlink,st_uid,st_gid,st_size. The
406 filesystem cannot provide meaningful values for this call, and the
408 filesystem always reports files having uid/gid of zero.
412 supplies a simple internal pager to ease reading the output of large commands.
419 Initialises the pager and tells it that the next line output will be the top of the
420 display. The environment variable LINES is consulted to determine the number of
421 lines to be displayed before pausing.
430 .Fn pager_output "char *lines"
433 Sends the lines in the nul-terminated buffer at
435 to the pager. Newline characters are counted in order to determine the number
436 of lines being output (wrapped lines are not accounted for).
438 will return zero when all of the lines have been output, or nonzero if the
439 display was paused and the user elected to quit.
442 .Fn pager_file "char *fname"
445 Attempts to open and display the file
447 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
456 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
457 backspace in order to provide reassurance to the user.
459 .Sh REQUIRED LOW-LEVEL SUPPORT
460 The following resources are consumed by
462 - stack, heap, console and devices.
464 The stack must be established before
466 functions can be invoked. Stack requirements vary depending on the functions
467 and filesystems used by the consumer and the support layer functions detailed
470 The heap must be established before calling
476 Heap usage will vary depending on the number of simultaneously open files,
477 as well as client behaviour. Automatic decompression will allocate more
478 than 64K of data per open file.
480 Console access is performed via the
485 functions detailed below.
487 Device access is initiated via
489 and is performed through the
494 functions in the device switch structure that
498 The consumer must provide the following support functions:
505 Return a character from the console, used by
514 Returns nonzero if a character is waiting from the console.
520 Write a character to the console, used by
527 and thus by many other functions for debugging and informational output.
530 .Fn devopen "struct open_file *of" "const char *name" "char **file"
533 Open the appropriate device for the file named in
537 a pointer to the remaining body of
539 which does not refer to the device. The
543 will be set to point to the
545 structure for the opened device if successful. Device identifiers must
546 always precede the path component, but may otherwise be arbitrarily formatted.
549 and thus for all device-related I/O.
552 .Fn devclose "struct open_file *of"
554 Close the device allocated for
556 The device driver itself will already have been called for the close; this call
557 should clean up any allocation made by devopen only.
560 .Fn panic "const char *msg" "..."
563 Signal a fatal and unrecoverable error condition. The
568 .Sh INTERNAL FILESYSTEMS
569 Internal filesystems are enabled by the consumer exporting the array
570 .Vt struct fs_ops *file_system[] ,
571 which should be initialised with pointers
574 structures. The following filesystem handlers are supplied by
576 the consumer may supply other filesystems of their own:
577 .Bl -hang -width ".Va cd9660_fsops"
583 Linux ext2fs filesystem.
585 File access via TFTP.
589 ISO 9660 (CD-ROM) filesystem.
591 Stacked filesystem supporting gzipped files.
592 When trying the zipfs filesystem,
596 to the end of the filename, and then tries to locate the file using the other
597 filesystems. Placement of this filesystem in the
599 array determines whether gzipped files will be opened in preference to non-gzipped
600 files. It is only possible to seek a gzipped file forwards, and
604 on gzipped files will report an invalid length.
609 .Xr bzip2 1 Ns -compressed
615 pointers should be terminated with a NULL.
617 Devices are exported by the supporting code via the array
618 .Vt struct devsw *devsw[]
619 which is a NULL terminated array of pointers to device switch structures.
622 contains contributions from many sources, including:
637 .An Matthew Dillon Aq dillon@backplane.com
640 The reorganisation and port to
642 the environment functions and this manpage were written by
643 .An Mike Smith Aq msmith@FreeBSD.org .
645 The lack of detailed memory usage data is unhelpful.