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 "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
168 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
172 .Fn strtol "const char *nptr" "char **endptr" "int base"
176 .Fn strtoll "const char *nptr" "char **endptr" "int base"
180 .Fn strtoul "const char *nptr" "char **endptr" "int base"
184 .Fn strtoull "const char *nptr" "char **endptr" "int base"
188 .Fn srandom "unsigned int seed"
196 .Fn strerror "int error"
199 Returns error messages for the subset of errno values supported by
201 .It Fn assert expression
207 .Fn setjmp "jmp_buf env"
211 .Fn longjmp "jmp_buf env" "int val"
218 respectively as there is no signal state to manipulate.
229 Read characters from the console into
231 All of the standard cautions apply to this function.
234 .Fn ngets "char *buf" "int size"
239 - 1 characters from the console into
243 is less than 1, the function's behaviour is as for
247 .Fn fgetstr "char *buf" "int size" "int fd"
250 Read a line of at most
254 Line terminating characters are stripped, and the buffer is always
257 Returns the number of characters in
259 if successful, or -1 if a read error occurs.
262 .Fn printf "const char *fmt" "..."
266 .Fn vprintf "const char *fmt" "va_list ap"
270 .Fn sprintf "char *buf" "const char *fmt" "..."
274 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
277 The *printf functions implement a subset of the standard
279 family functionality and some extensions.
280 The following standard conversions
281 are supported: c,d,n,o,p,s,u,x.
282 The following modifiers are supported:
283 +,-,#,*,0,field width,precision,l.
287 conversion is provided to decode error registers.
289 .Bd -ragged -offset indent
297 where <base> is the output expressed as a control character, e.g.\& \e10 gives
298 octal, \e20 gives hex.
299 Each <arg> is a sequence of characters, the first of
300 which gives the bit number to be inspected (origin 1) and the next characters
301 (up to a character less than 32) give the text to be displayed if the bit is set.
303 .Bd -ragged -offset indent
307 .Qq \e10\e2BITTWO\e1BITONE
311 would give the output
312 .Bd -ragged -offset indent
318 conversion provides a hexdump facility, e.g.
319 .Bd -ragged -offset indent
325 .Qq XX:XX:XX:XX:XX:XX
327 .Bd -ragged -offset indent
337 .Sh CHARACTER TESTS AND CONVERSIONS
396 .Fn open "const char *path" "int flags"
399 Similar to the behaviour as specified in
401 except that file creation is not supported, so the mode parameter is not
405 argument may be one of O_RDONLY, O_WRONLY and O_RDWR.
406 Only UFS currently supports writing.
416 Close all open files.
419 .Fn read "int fd" "void *buf" "size_t len"
423 .Fn write "int fd" "void *buf" "size_t len"
426 (No file systems currently support writing.)
429 .Fn lseek "int fd" "off_t offset" "int whence"
432 Files being automatically uncompressed during reading cannot seek backwards
433 from the current point.
436 .Fn stat "const char *path" "struct stat *sb"
440 .Fn fstat "int fd" "struct stat *sb"
447 functions only fill out the following fields in the
449 structure: st_mode,st_nlink,st_uid,st_gid,st_size.
452 file system cannot provide meaningful values for this call, and the
454 file system always reports files having uid/gid of zero.
459 library supplies a simple internal pager to ease reading the output of large
467 Initialises the pager and tells it that the next line output will be the top of the
469 The environment variable LINES is consulted to determine the number of
470 lines to be displayed before pausing.
479 .Fn pager_output "const char *lines"
482 Sends the lines in the
484 -terminated buffer at
487 Newline characters are counted in order to determine the number
488 of lines being output (wrapped lines are not accounted for).
491 function will return zero when all of the lines have been output, or nonzero
492 if the display was paused and the user elected to quit.
495 .Fn pager_file "const char *fname"
498 Attempts to open and display the file
500 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
506 .Fn devformat "struct devdesc *"
509 Format the specified device as a string.
512 .Fn devparse "struct devdesc **dev" "const char *devdesc" "const char **path"
518 .Sq device:[/path/to/file] .
521 table is used to match the start of the
527 is non-NULL, then it will be called to parse the rest of the string and allocate
531 If NULL, then a default routine will be called that will allocate a simple
533 parse a unit number and ensure there's no trailing characters.
536 is non-NULL, then a pointer to the remainder of the
538 string after the device specification is written.
546 array, returning the number of routines that returned an error.
552 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
553 backspace in order to provide reassurance to the user.
555 .Sh REQUIRED LOW-LEVEL SUPPORT
556 The following resources are consumed by
558 - stack, heap, console and devices.
560 The stack must be established before
562 functions can be invoked.
563 Stack requirements vary depending on the functions
564 and file systems used by the consumer and the support layer functions detailed
567 The heap must be established before calling
573 Heap usage will vary depending on the number of simultaneously open files,
574 as well as client behaviour.
575 Automatic decompression will allocate more
576 than 64K of data per open file.
578 Console access is performed via the
583 functions detailed below.
585 Device access is initiated via
587 and is performed through the
592 functions in the device switch structure that
596 The consumer must provide the following support functions:
603 Return a character from the console, used by
612 Returns nonzero if a character is waiting from the console.
618 Write a character to the console, used by
625 and thus by many other functions for debugging and informational output.
628 .Fn devopen "struct open_file *of" "const char *name" "const char **file"
631 Open the appropriate device for the file named in
635 a pointer to the remaining body of
637 which does not refer to the device.
642 will be set to point to the
644 structure for the opened device if successful.
645 Device identifiers must
646 always precede the path component, but may otherwise be arbitrarily formatted.
649 and thus for all device-related I/O.
652 .Fn devclose "struct open_file *of"
655 Close the device allocated for
657 The device driver itself will already have been called for the close; this call
658 should clean up any allocation made by devopen only.
669 .Fn panic "const char *msg" "..."
672 Signal a fatal and unrecoverable error condition.
678 .Sh INTERNAL FILE SYSTEMS
679 Internal file systems are enabled by the consumer exporting the array
680 .Vt struct fs_ops *file_system[] ,
681 which should be initialised with pointers
685 The following file system handlers are supplied by
687 the consumer may supply other file systems of their own:
688 .Bl -hang -width ".Va cd9660_fsops"
694 Linux ext2fs file system.
696 File access via TFTP.
700 ISO 9660 (CD-ROM) file system.
702 Stacked file system supporting gzipped files.
703 When trying the gzipfs file system,
707 to the end of the filename, and then tries to locate the file using the other
709 Placement of this file system in the
711 array determines whether gzipped files will be opened in preference to non-gzipped
713 It is only possible to seek a gzipped file forwards, and
717 on gzipped files will report an invalid length.
722 .Xr bzip2 1 Ns -compressed
728 pointers should be terminated with a NULL.
730 Devices are exported by the supporting code via the array
731 .Vt struct devsw *devsw[]
732 which is a NULL terminated array of pointers to device switch structures.
734 The driver needs to provide a common set of entry points that are
737 to interface with the device.
740 const char dv_name[DEV_NAMLEN];
742 int (*dv_init)(void);
743 int (*dv_strategy)(void *devdata, int rw, daddr_t blk,
744 size_t size, char *buf, size_t *rsize);
745 int (*dv_open)(struct open_file *f, ...);
746 int (*dv_close)(struct open_file *f);
747 int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
748 int (*dv_print)(int verbose);
749 void (*dv_cleanup)(void);
750 char * (*dv_fmtdev)(struct devdesc *);
751 int (*dv_parsedev)(struct devdesc **dev, const char *devpart,
753 bool (*dv_match)(struct devsw *dv, const char *devspec);
756 .Bl -tag -width ".Fn dv_strategy"
761 The supported types are:
762 .Bl -tag -width "DEVT_NONE"
770 Each type may have its own associated (struct type_devdesc),
771 which has the generic (struct devdesc) as its first member.
773 Driver initialization routine.
774 This routine should probe for available units.
775 Drivers are responsible for maintaining lists of units for later enumeration.
776 No other driver routines may be called before
780 The driver open routine.
782 The driver close routine.
784 The driver ioctl routine.
786 Prints information about the available devices.
787 Information should be presented with
790 Cleans up any memory used by the device before the next stage is run.
792 Converts the specified devdesc to the canonical string representation
795 Parses the device portion of a file path.
800 of device name, possibly followed by a colon and a path within the device.
803 is, by convention, the part of the device specification that follows the
808 is parsing the string
814 The parsing routine is expected to allocate a new
816 or subclass and return it in
819 This routine should set
821 to point to the portion of the string after device specification, or
823 in the earlier example.
824 Generally, code needing to parse a path will use
826 instead of calling this routine directly.
829 to specify that all device paths starting with
832 Otherwise, this function returns 0 for a match and a non-zero
834 to indicate why it didn't match.
835 This is helpful when you claim the device path after using it to query
836 properties on systems that have uniform naming for different types of
842 library contains contributions from many sources, including:
857 .An Matthew Dillon Aq Mt dillon@backplane.com
860 The reorganisation and port to
862 the environment functions and this manpage were written by
863 .An Mike Smith Aq Mt msmith@FreeBSD.org .
865 The lack of detailed memory usage data is unhelpful.