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
32 .Nd support library for standalone executables
38 library provides a set of supporting functions for standalone
39 applications, mimicking where possible the standard
43 The following sections group these functions by kind.
44 Unless specifically described here, see the corresponding section 3
45 manpages for the given functions.
47 String functions are available as documented in
55 .Fn malloc "size_t size"
60 bytes of memory from the heap using a best-fit algorithm.
66 Free the allocated object at
70 .Fn setheap "void *start" "void *limit"
74 This function must be called before calling
81 will be used for the heap; attempting to allocate beyond this will result
88 Provides the behaviour of
90 i.e., returns the highest point that the heap has reached.
92 be used during testing to determine the actual heap usage.
98 A set of functions are provided for manipulating a flat variable space similar
99 to the traditional shell-supported environment.
100 Major enhancements are support
101 for set/unset hook functions.
105 .Fn getenv "const char *name"
109 .Fn setenv "const char *name" "const char *value" "int overwrite"
113 .Fn putenv "const char *string"
117 .Fn unsetenv "const char *name"
120 These functions behave similarly to their standard library counterparts.
122 .Ft "struct env_var *"
123 .Fn env_getenv "const char *name"
126 Looks up a variable in the environment and returns its entire
130 .Fn env_setenv "const char *name" "int flags" "const void *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
133 Creates a new or sets an existing environment variable called
135 If creating a new variable, the
139 arguments may be specified.
141 The set hook is invoked whenever an attempt
142 is made to set the variable, unless the EV_NOHOOK flag is set.
144 a set hook will validate the
146 argument, and then call
148 again with EV_NOHOOK set to actually save the value.
149 The predefined function
151 may be specified to refuse all attempts to set a variable.
153 The unset hook is invoked when an attempt is made to unset a variable.
155 returns zero, the variable will be unset.
156 The predefined function
158 may be used to prevent a variable being unset.
160 .Sh STANDARD LIBRARY SUPPORT
164 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
168 .Fn strtol "const char *nptr" "char **endptr" "int base"
172 .Fn srandom "unsigned long seed"
180 .Fn strerror "int error"
183 Returns error messages for the subset of errno values supported by
185 .It Fn assert expression
191 .Fn setjmp "jmp_buf env"
195 .Fn longjmp "jmp_buf env" "int val"
202 respectively as there is no signal state to manipulate.
213 Read characters from the console into
215 All of the standard cautions apply to this function.
218 .Fn ngets "char *buf" "int size"
223 - 1 characters from the console into
227 is less than 1, the function's behaviour is as for
231 .Fn fgetstr "char *buf" "int size" "int fd"
234 Read a line of at most
238 Line terminating characters are stripped, and the buffer is always
241 Returns the number of characters in
243 if successful, or -1 if a read error occurs.
246 .Fn printf "const char *fmt" "..."
250 .Fn vprintf "const char *fmt" "va_list ap"
254 .Fn sprintf "char *buf" "const char *fmt" "..."
258 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
261 The *printf functions implement a subset of the standard
263 family functionality and some extensions.
264 The following standard conversions
265 are supported: c,d,n,o,p,s,u,x.
266 The following modifiers are supported:
267 +,-,#,*,0,field width,precision,l.
271 conversion is provided to decode error registers.
273 .Bd -ragged -offset indent
281 where <base> is the output expressed as a control character, e.g.\& \e10 gives
282 octal, \e20 gives hex.
283 Each <arg> is a sequence of characters, the first of
284 which gives the bit number to be inspected (origin 1) and the next characters
285 (up to a character less than 32) give the text to be displayed if the bit is set.
287 .Bd -ragged -offset indent
291 .Qq \e10\e2BITTWO\e1BITONE
295 would give the output
296 .Bd -ragged -offset indent
302 conversion provides a hexdump facility, e.g.
303 .Bd -ragged -offset indent
309 .Qq XX:XX:XX:XX:XX:XX
311 .Bd -ragged -offset indent
321 .Sh CHARACTER TESTS AND CONVERSIONS
364 .Fn open "const char *path" "int flags"
367 Similar to the behaviour as specified in
369 except that file creation is not supported, so the mode parameter is not
373 argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no file systems
374 currently support writing).
384 Close all open files.
387 .Fn read "int fd" "void *buf" "size_t len"
391 .Fn write "int fd" "void *buf" "size_t len"
394 (No file systems currently support writing.)
397 .Fn lseek "int fd" "off_t offset" "int whence"
400 Files being automatically uncompressed during reading cannot seek backwards
401 from the current point.
404 .Fn stat "const char *path" "struct stat *sb"
408 .Fn fstat "int fd" "struct stat *sb"
415 functions only fill out the following fields in the
417 structure: st_mode,st_nlink,st_uid,st_gid,st_size.
420 file system cannot provide meaningful values for this call, and the
422 file system always reports files having uid/gid of zero.
427 library supplies a simple internal pager to ease reading the output of large
435 Initialises the pager and tells it that the next line output will be the top of the
437 The environment variable LINES is consulted to determine the number of
438 lines to be displayed before pausing.
447 .Fn pager_output "const char *lines"
450 Sends the lines in the
452 -terminated buffer at
455 Newline characters are counted in order to determine the number
456 of lines being output (wrapped lines are not accounted for).
459 function will return zero when all of the lines have been output, or nonzero
460 if the display was paused and the user elected to quit.
463 .Fn pager_file "const char *fname"
466 Attempts to open and display the file
468 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
477 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
478 backspace in order to provide reassurance to the user.
480 .Sh REQUIRED LOW-LEVEL SUPPORT
481 The following resources are consumed by
483 - stack, heap, console and devices.
485 The stack must be established before
487 functions can be invoked.
488 Stack requirements vary depending on the functions
489 and file systems used by the consumer and the support layer functions detailed
492 The heap must be established before calling
498 Heap usage will vary depending on the number of simultaneously open files,
499 as well as client behaviour.
500 Automatic decompression will allocate more
501 than 64K of data per open file.
503 Console access is performed via the
508 functions detailed below.
510 Device access is initiated via
512 and is performed through the
517 functions in the device switch structure that
521 The consumer must provide the following support functions:
528 Return a character from the console, used by
537 Returns nonzero if a character is waiting from the console.
543 Write a character to the console, used by
550 and thus by many other functions for debugging and informational output.
553 .Fn devopen "struct open_file *of" "const char *name" "const char **file"
556 Open the appropriate device for the file named in
560 a pointer to the remaining body of
562 which does not refer to the device.
567 will be set to point to the
569 structure for the opened device if successful.
570 Device identifiers must
571 always precede the path component, but may otherwise be arbitrarily formatted.
574 and thus for all device-related I/O.
577 .Fn devclose "struct open_file *of"
580 Close the device allocated for
582 The device driver itself will already have been called for the close; this call
583 should clean up any allocation made by devopen only.
586 .Fn panic "const char *msg" "..."
589 Signal a fatal and unrecoverable error condition.
595 .Sh INTERNAL FILE SYSTEMS
596 Internal file systems are enabled by the consumer exporting the array
597 .Vt struct fs_ops *file_system[] ,
598 which should be initialised with pointers
602 The following file system handlers are supplied by
604 the consumer may supply other file systems of their own:
605 .Bl -hang -width ".Va cd9660_fsops"
611 Linux ext2fs file system.
613 File access via TFTP.
617 ISO 9660 (CD-ROM) file system.
619 Stacked file system supporting gzipped files.
620 When trying the gzipfs file system,
624 to the end of the filename, and then tries to locate the file using the other
626 Placement of this file system in the
628 array determines whether gzipped files will be opened in preference to non-gzipped
630 It is only possible to seek a gzipped file forwards, and
634 on gzipped files will report an invalid length.
639 .Xr bzip2 1 Ns -compressed
645 pointers should be terminated with a NULL.
647 Devices are exported by the supporting code via the array
648 .Vt struct devsw *devsw[]
649 which is a NULL terminated array of pointers to device switch structures.
653 library contains contributions from many sources, including:
668 .An Matthew Dillon Aq Mt dillon@backplane.com
671 The reorganisation and port to
673 the environment functions and this manpage were written by
674 .An Mike Smith Aq Mt msmith@FreeBSD.org .
676 The lack of detailed memory usage data is unhelpful.