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 $
32 .Nd support library for standalone executables
39 provides a set of supporting functions for standalone
40 applications, mimicking where possible the standard
44 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"
75 This function must be called before calling
82 will be used for the heap; attempting to allocate beyond this will result
86 .Fn sbrk "intptr_t incr"
89 Provides the behaviour of
91 i.e.\& returns the highest point that the heap has reached.
93 be used during testing to determine the actual heap usage.
99 A set of functions are provided for manipulating a flat variable space similar
100 to the traditional shell-supported environment.
101 Major enhancements are support
102 for set/unset hook functions.
106 .Fn getenv "const char *name"
110 .Fn setenv "const char *name" "char *value" "int overwrite"
114 .Fn putenv "const char *string"
118 .Fn unsetenv "const char *name"
121 These functions behave similarly to their standard library counterparts.
123 .Ft "struct env_var *"
124 .Fn env_getenv "const char *name"
127 Looks up a variable in the environment and returns its entire
131 .Fn env_setenv "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
134 Creates a new or sets an existing environment variable called
136 If creating a new variable, the
140 arguments may be specified.
142 The set hook is invoked whenever an attempt
143 is made to set the variable, unless the
147 a set hook will validate the
149 argument, and then call
153 set to actually save the value.
154 The predefined function
156 may be specified to refuse all attempts to set a variable.
158 The unset hook is invoked when an attempt is made to unset a variable.
160 returns zero, the variable will be unset.
161 The predefined function
163 may be used to prevent a variable being unset.
165 .Sh STANDARD LIBRARY SUPPORT
169 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
173 .Fn strtol "const char *nptr" "char **endptr" "int base"
177 .Fn srandom "unsigned long seed"
185 .Fn strerror "int error"
188 Returns error messages for the subset of
192 .It Fn assert expression
198 .Fn setjmp "jmp_buf env"
202 .Fn longjmp "jmp_buf env" "int val"
209 respectively as there is no signal state to manipulate.
220 Read characters from the console into
222 All of the standard cautions apply to this function.
225 .Fn ngets "char *buf" "size_t size"
230 - 1 characters from the console into
234 is less than 1, the function's behaviour is as for
238 .Fn fgets "char *buf" "int size" "int fd"
241 Read a line of at most
245 Line terminating characters are not stripped,
246 and the buffer is always nul-terminated.
247 Upon successful completion a pointer to the string is returned.
248 If end-of-file occurs before any characters are read,
249 NULL is returned and the buffer contents remain unchanged.
251 NULL is returned and the buffer contents are indeterminate.
254 .Fn fgetstr "char *buf" "int size" "int fd"
257 Read a line of at most
261 Line terminating characters are stripped, and the buffer is always
263 Returns the number of characters in
265 if successful, or -1 if a read error occurs.
268 .Fn printf "const char *fmt" "..."
272 .Fn vprintf "const char *fmt" "va_list ap"
276 .Fn sprintf "char *buf" "const char *fmt" "..."
280 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
283 The *printf functions implement a subset of the standard
285 family functionality and some extensions.
286 The following standard conversions
288 .Cm c , d , n , o , p , s , u , x .
289 The following modifiers are supported:
290 .Cm + , - , # , * , 0 , field width , precision , l .
292 .Sh CHARACTER TESTS AND CONVERSIONS
335 .Fn open "const char *path" "int flags"
338 Similar to the behaviour as specified in
340 except that file creation is not supported, so the mode parameter is not
344 argument may be one of
349 (although no filesystems currently support writing).
359 Close all open files.
362 .Fn read "int fd" "void *buf" "size_t len"
366 .Fn write "int fd" "void *buf" "size_t len"
369 (No filesystems currently support writing.)
372 .Fn lseek "int fd" "off_t offset" "int whence"
375 Files being automatically uncompressed during reading cannot seek backwards
376 from the current point.
379 .Fn stat "const char *path" "struct stat *sb"
383 .Fn fstat "int fd" "struct stat *sb"
390 functions only fill out the following fields in the
393 .Fa st_mode , st_nlink , st_uid , st_gid , st_size .
396 filesystem cannot provide meaningful values for this call, and the
398 filesystem always reports files having uid/gid of zero.
402 supplies a simple internal pager to ease reading the output of large commands.
409 Initialises the pager and tells it that the next line output will be the
411 The environment variable LINES is consulted to determine the number of
412 lines to be displayed before pausing.
421 .Fn pager_output "char *lines"
424 Sends the lines in the nul-terminated buffer at
427 Newline characters are counted in order to determine the number
428 of lines being output (wrapped lines are not accounted for).
430 will return zero when all of the lines have been output, or nonzero if the
431 display was paused and the user elected to quit.
434 .Fn pager_file "char *fname"
437 Attempts to open and display the file
439 Returns -1 on error, 0 at
441 or 1 if the user elects to quit while reading.
447 .Fn ether_sprintf "u_char *ap"
450 Convert an ethernet address to its human readable notation as specified in IEEE 802.
459 Successive calls emit the characters in the sequence |, /, -, \\ followed by a
460 backspace in order to provide reassurance to the user.
462 .Sh REQUIRED LOW-LEVEL SUPPORT
463 The following resources are consumed by
465 - stack, heap, console and devices.
467 The stack must be established before
469 functions can be invoked.
470 Stack requirements vary depending on the functions
471 and filesystems used by the consumer and the support layer functions detailed
474 The heap must be established before calling
480 Heap usage will vary depending on the number of simultaneously open files,
481 as well as client behaviour.
482 Automatic decompression will allocate more
483 than 64K of data per open file.
485 Console access is performed via the
490 functions detailed below.
492 Device access is initiated via
494 and is performed through the
499 functions in the device switch structure that
503 The consumer must provide the following support functions:
510 Return a character from the console, used by
519 Returns nonzero if a character is waiting from the console.
525 Write a character to the console, used by
532 and thus by many other functions for debugging and informational output.
535 .Fn devopen "struct open_file *of" "const char *name" "char **file"
538 Open the appropriate device for the file named in
542 a pointer to the remaining body of
544 which does not refer to the device.
549 will be set to point to the
551 structure for the opened device if successful.
552 Device identifiers must
553 always precede the path component, but may otherwise be arbitrarily formatted.
556 and thus for all device-related I/O.
559 .Fn devclose "struct open_file *of"
562 Close the device allocated for
564 The device driver itself will already have been called for the close;
565 this call should clean up any allocation made by
570 .Fn panic "const char *msg" "..."
573 Signal a fatal and unrecoverable error condition.
579 .Sh INTERNAL FILESYSTEMS
580 Internal filesystems are enabled by the consumer exporting the array
581 .Vt struct fs_ops *file_system[] ,
582 which should be initialised with pointers
586 The following filesystem handlers are supplied by
588 the consumer may supply other filesystems of their own:
589 .Bl -hang -width ".Va cd9660_fsops"
598 Linux ext2fs filesystem.
602 File access via TFTP.
606 ISO 9660 (CD-ROM) filesystem.
608 Stacked filesystem supporting gzipped files.
609 When trying the zipfs filesystem,
613 to the end of the filename, and then tries to locate the file using the other
615 Placement of this filesystem in the
617 array determines whether gzipped files will be opened in preference to non-gzipped
619 It is only possible to seek a gzipped file forwards, and
623 on gzipped files will report an invalid length.
628 .Xr bzip2 1 Ns -compressed
634 pointers should be terminated with a NULL.
636 Devices are exported by the supporting code via the array
637 .Vt struct devsw *devsw[]
638 which is a NULL terminated array of pointers to device switch structures.
641 contains contributions from many sources, including:
656 .An Matthew Dillon Aq Mt dillon@backplane.com
659 The reorganisation and port to
661 the environment functions and this manpage were written by
662 .An Mike Smith Aq Mt msmith@FreeBSD.org .
664 The lack of detailed memory usage data is unhelpful.