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 Ohttp://wafu.netgate.net/tama/unix/indexe.htmlTHERWISE) 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
34 .Fd #include <stand.h>
37 provides a set of supporting functions for standalone
38 applications, mimicking where possible the standard BSD programming
39 environment. The following sections group these functions by kind.
40 Unless specifically described here, see the corresponding section 3
41 manpages for the given functions.
43 String functions are available as documented in
49 .It Fn "void *malloc" "size_t size"
53 bytes of memory from the heap using a best-fit algorithm.
54 .It Fn "void free" "void *ptr"
56 Free the allocated object at
58 .It Fn "void setheap" "void *start" "void *limit"
60 Initialise the heap. This function must be called before calling
62 for the first time. The region between
66 will be used for the heap; attempting to allocate beyond this will result
68 .It Fn "char *sbrk" "int junk"
70 Provides the behaviour of
72 ie. returns the highest point that the heap has reached. This value can
73 be used during testing to determine the actual heap usage. The
78 A set of functions are provided for manipulating a flat variable space similar
79 to the traditional shell-supported evironment. Major enhancements are support
80 for set/unset hook functions.
82 .It Fn "char *getenv" "const char *name"
83 .It Fn "int setenv" "const char *name" "char *value" "int overwrite"
84 .It Fn "int putenv" "const char *string"
85 .It Fn "int unsetenv" "const char *name"
87 These functions behave similarly to their standard library counterparts.
88 .It Fn "struct env_var *env_getenv" "const char *name"
90 Looks up a variable in the environment and returns its entire
92 .It Fn "int env_setenv" "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
94 Creates a new or sets an existing environment variable called
96 If creating a new variable, the
100 arguments may be specified.
102 The set hook is invoked whenever an attempt
103 is made to set the variable, unless the EV_NOHOOK flag is set. Typically
104 a set hook will validate the
106 argument, and then call
108 again with EV_NOHOOK set to actually save the value. The predefined function
110 may be specified to refuse all attempts to set a variable.
112 The unset hook is invoked when an attempt is made to unset a variable. If it
113 returns zero, the variable will be unset. The predefined function
115 may be used to prevent a variable being unset.
117 .Sh STANDARD LIBRARY SUPPORT
119 .It Fn "int getopt" "int argc" "char * const *argv" "cont char *optstring"
120 .It Fn "long strtol" "const char *nptr" "char **endptr" "int base"
121 .It Fn "void srandom" "unsigned long seed"
122 .It Fn "unsigned long random" "void"
123 .It Fn "char *strerror" "int error"
125 Returns error messages for the subset of errno values supported by
127 .It Fn "assert" "expression"
130 .Fd #include <assert.h>
131 .It Fn "int setjmp" "jmp_buf env"
132 .It Fn "void longjmp" "jmp_buf env" "int val"
138 respectively as there is no signal state to manipulate. Requires
139 .Fd #include <setjmp.h>
143 .It Fn "void gets" "char *buf"
145 Read characters from the console into
147 All of the standard cautions apply to this function.
148 .It Fn "void ngets" "char *buf" "size_t size"
152 - 1 characters from the console into
156 is less than 1, the function's behaviour is as for
158 .It Fn "int fgetstr" "char *buf" "int size" "int fd"
160 Read a line of at most
164 Line terminating characters are stripped, and the buffer is always nul
165 terminated. Returns the number of characters in
167 if successful, or -1 if a read error occurs.
168 .It Fn "int printf" "const char *fmt" "..."
169 .It Fn "void vprintf" "const char *fmt" "va_list ap"
170 .It Fn "int sprintf" "char *buf" "const char *fmt" "..."
171 .It Fn "void vsprintf" "char *buf" "const char *fmt" "va_list ap"
173 The *printf functions implement a subset of the standard
175 family functionality and some extensions. The following standard conversions
176 are supported: c,d,n,o,p,s,u,x. The following modifiers are supported:
177 +,-,#,*,0,field width,precision,l.
181 conversion is provided to decode error registers. Its usage is:
191 where <base> is the output expressed as a control character, eg. \e10 gives
192 octal, \e20 gives hex. Each <arg> is a sequence of characters, the first of
193 which gives the bit number to be inspected (origin 1) and the next characters
194 (up to a character less than 32) give the text to be displayed if the bit is set.
201 .Qq \e10\e2BITTWO\e1BITONE\en
205 would give the output
213 conversion provides a hexdump facility, eg.
215 .Bd -offset indent -literal
221 .Qq XX:XX:XX:XX:XX:XX
223 .Bd -offset indent -literal
233 .Sh CHARACTER TESTS AND CONVERSIONS
235 .It Fn "int isupper" "int c"
236 .It Fn "int islower" "int c"
237 .It Fn "int isspace" "int c"
238 .It Fn "int isdigit" "int c"
239 .It Fn "int isxdigit" "int c"
240 .It Fn "int isascii" "int c"
241 .It Fn "int isalpha" "int c"
242 .It Fn "int toupper" "int c"
243 .It Fn "int tolower" "int c"
247 .It Fn "int open" "const char *path" "int flags"
249 Similar to the behaviour as specified in
251 except that file creation is not supported, so the mode parameter is not
254 argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no filesystems
255 currently support writing).
256 .It Fn "int close" "int fd"
257 .It Fn "void closeall" "void"
259 Close all open files.
260 .It Fn "ssize_t read" "int fd" "void *buf" "size_t len"
261 .It Fn "ssize_t write" "int fd" "void *buf" "size_t len"
263 (No filesystems currently support writing.)
264 .It Fn "off_t lseek" "int fd" "off_t offset" "int whence"
266 Files being automatically uncompressed during reading cannot seek backwards
267 from the current point.
268 .It Fn "int stat" "const char *path" "struct stat *sb"
269 .It Fn "int fstat" "int fd" "struct stat *sb"
275 functions only fill out the following fields in the
277 structure: st_mode,st_nlink,st_uid,st_gid,st_size. The
279 filesystem cannot provide meaningful values for this call, and the
281 filesystem always reports files having uid/gid of zero.
285 supplies a simple internal pager to ease reading the output of large commands.
287 .It Fn "void pager_open"
289 Initialises the pager and tells it that the next line output will be the top of the
290 display. The environment variable LINES is consulted to determine the number of
291 lines to be displayed before pausing.
292 .It Fn "void pager_close" "void"
295 .It Fn "void pager_output" "char *lines"
297 Sends the lines in the nul-terminated buffer at
299 to the pager. Newline characters are counted in order to determine the number
300 of lines being output (wrapped lines are not accounted for).
302 will return zero when all of the lines have been output, or nonzero if the
303 display was paused and the user elected to quit.
304 .It Fn "int pager_file" "char *fname"
306 Attempts to open and display the file
308 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
312 .It Fn "void twiddle" "void"
314 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
315 backspace in order to provide reassurance to the user.
317 .Sh REQUIRED LOW-LEVEL SUPPORT
318 The following resources are consumed by
320 - stack, heap, console and devices.
322 The stack must be established before
324 functions can be invoked. Stack requirements vary depending on the functions
325 and filesystems used by the consumer and the support layer functions detailed
328 The heap must be established before calling
334 Heap usage will vary depending on the number of simultaneously open files,
335 as well as client behaviour. Automatic decompression will allocate more
336 than 64K of data per open file.
338 Console access is performed via the
343 functions detailed below.
345 Device access is initiated via
347 and is performed through the
352 functions in the device switch structure that
356 The consumer must provide the following support functions:
358 .It Fn "int getchar" "void"
360 Return a character from the console, used by
364 .It Fn "int ischar" "void"
366 Returns nonzero if a character is waiting from the console.
367 .It Fn "void putchar" "int"
369 Write a character to the console, used by
376 and thus by many other functions for debugging and informational output.
377 .It Fn "int devopen" "struct open_file *of" "const char *name" "char **file"
379 Open the appropriate device for the file named in
383 a pointer to the remaining body of
385 which does not refer to the device. The
389 will be set to point to the
391 structure for the opened device if successful. Device identifiers must
392 always precede the path component, but may otherwise be arbitrarily formatted.
395 and thus for all device-related I/O.
396 .It Fn "int devclose" "struct open_file *of"
397 Close the device allocated for
399 The device driver itself will already have been called for the close; this call
400 should clean up any allocation made by devopen only.
401 .It Fn "void panic" "const char *msg" "..."
403 Signal a fatal and unrecoverable error condition. The
408 .Sh INTERNAL FILESYSTEMS
409 Internal filesystems are enabled by the consumer exporting the array
410 .Dv struct fs_ops *file_system[], which should be initialised with pointers
413 structures. The following filesystem handlers are supplied by
415 the consumer may supply other filesystems of their own:
416 .Bl -hang -width "cd9660_fsops "
420 File access via TFTP.
424 ISO 9660 (CD-ROM) filesystem.
426 Stacked filesystem supporting gzipped files. When trying the zipfs filesystem,
430 to the end of the filename, and then tries to locate the file using the other
431 filesystems. Placement of this filesystem in the
433 array determines whether gzipped files will be opened in preference to non-gzipped
434 files. It is only possible to seek a gzipped file forwards, and
438 on gzipped files will report an invalid length.
443 pointers should be terminated with a NULL.
445 Devices are exported by the supporting code via the array
446 .Dv struct devsw *devsw[]
447 which is a NULL terminated array of pointers to device switch structures.
450 The lack of detailed memory usage data is unhelpful.
453 contains contributions from many sources, including:
468 .An Matthew Dillon Aq dillon@backplane.com
471 The reorganisation and port to
473 the environment functions and this manpage were written by
474 .An Mike Smith Aq msmith@freebsd.org .