libstand - Make ether_sprintf() public.
[dragonfly.git] / lib / libstand / libstand.3
1 .\" Copyright (c) Michael Smith
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
12 .\"
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
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD: src/lib/libstand/libstand.3,v 1.5.2.11 2002/06/26 19:14:43 schweikh Exp $
26 .\"
27 .Dd December 29, 2012
28 .Dt LIBSTAND 3
29 .Os
30 .Sh NAME
31 .Nm libstand
32 .Nd support library for standalone executables
33 .Sh LIBRARY
34 .Lb libstand
35 .Sh SYNOPSIS
36 .In stand.h
37 .Sh DESCRIPTION
38 .Nm
39 provides a set of supporting functions for standalone
40 applications, mimicking where possible the standard
41 .Bx
42 programming
43 environment.
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.
47 .Sh STRING FUNCTIONS
48 String functions are available as documented in
49 .Xr string 3
50 and
51 .Xr bstring 3 .
52 .Sh MEMORY ALLOCATION
53 .Bl -hang -width 10n
54 .It Xo
55 .Ft "void *"
56 .Fn malloc "size_t size"
57 .Xc
58 .Pp
59 Allocate
60 .Fa size
61 bytes of memory from the heap using a best-fit algorithm.
62 .It Xo
63 .Ft void
64 .Fn free "void *ptr"
65 .Xc
66 .Pp
67 Free the allocated object at
68 .Fa ptr .
69 .It Xo
70 .Ft void
71 .Fn setheap "void *start" "void *limit"
72 .Xc
73 .Pp
74 Initialise the heap.
75 This function must be called before calling
76 .Fn alloc
77 for the first time.
78 The region between
79 .Fa start
80 and
81 .Fa limit
82 will be used for the heap; attempting to allocate beyond this will result
83 in a panic.
84 .It Xo
85 .Ft "char *"
86 .Fn sbrk "int junk"
87 .Xc
88 .Pp
89 Provides the behaviour of
90 .Fn sbrk 0 ,
91 i.e.\& returns the highest point that the heap has reached.
92 This value can
93 be used during testing to determine the actual heap usage.
94 The
95 .Fa junk
96 argument is ignored.
97 .El
98 .Sh ENVIRONMENT
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.
103 .Bl -hang -width 10n
104 .It Xo
105 .Ft "char *"
106 .Fn getenv "const char *name"
107 .Xc
108 .It Xo
109 .Ft int
110 .Fn setenv "const char *name" "char *value" "int overwrite"
111 .Xc
112 .It Xo
113 .Ft int
114 .Fn putenv "const char *string"
115 .Xc
116 .It Xo
117 .Ft int
118 .Fn unsetenv "const char *name"
119 .Xc
120 .Pp
121 These functions behave similarly to their standard library counterparts.
122 .It Xo
123 .Ft "struct env_var *"
124 .Fn env_getenv "const char *name"
125 .Xc
126 .Pp
127 Looks up a variable in the environment and returns its entire
128 data structure.
129 .It Xo
130 .Ft int
131 .Fn env_setenv "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
132 .Xc
133 .Pp
134 Creates a new or sets an existing environment variable called
135 .Fa name .
136 If creating a new variable, the
137 .Fa sethook
138 and
139 .Fa unsethook
140 arguments may be specified.
141 .Pp
142 The set hook is invoked whenever an attempt
143 is made to set the variable, unless the
144 .Dv EV_NOHOOK
145 flag is set.
146 Typically
147 a set hook will validate the
148 .Fa value
149 argument, and then call
150 .Fn env_setenv
151 again with
152 .Dv EV_NOHOOK
153 set to actually save the value.
154 The predefined function
155 .Fn env_noset
156 may be specified to refuse all attempts to set a variable.
157 .Pp
158 The unset hook is invoked when an attempt is made to unset a variable.
159 If it
160 returns zero, the  variable will be unset.
161 The predefined function
162 .Fn env_nounset
163 may be used to prevent a variable being unset.
164 .El
165 .Sh STANDARD LIBRARY SUPPORT
166 .Bl -hang -width 10n
167 .It Xo
168 .Ft int
169 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
170 .Xc
171 .It Xo
172 .Ft long
173 .Fn strtol "const char *nptr" "char **endptr" "int base"
174 .Xc
175 .It Xo
176 .Ft void
177 .Fn srandom "unsigned long seed"
178 .Xc
179 .It Xo
180 .Ft "unsigned long"
181 .Fn random void
182 .Xc
183 .It Xo
184 .Ft "char *"
185 .Fn strerror "int error"
186 .Xc
187 .Pp
188 Returns error messages for the subset of
189 .Va errno
190 values supported by
191 .Nm .
192 .It Fn assert expression
193 .Pp
194 Requires
195 .In assert.h .
196 .It Xo
197 .Ft int
198 .Fn setjmp "jmp_buf env"
199 .Xc
200 .It Xo
201 .Ft void
202 .Fn longjmp "jmp_buf env" "int val"
203 .Xc
204 .Pp
205 Defined as
206 .Fn _setjmp
207 and
208 .Fn _longjmp
209 respectively as there is no signal state to manipulate.
210 Requires
211 .In setjmp.h .
212 .El
213 .Sh CHARACTER I/O
214 .Bl -hang -width 10n
215 .It Xo
216 .Ft void
217 .Fn gets "char *buf"
218 .Xc
219 .Pp
220 Read characters from the console into
221 .Fa buf .
222 All of the standard cautions apply to this function.
223 .It Xo
224 .Ft void
225 .Fn ngets "char *buf" "size_t size"
226 .Xc
227 .Pp
228 Read at most
229 .Fa size
230 - 1 characters from the console into
231 .Fa buf .
232 If
233 .Fa size
234 is less than 1, the function's behaviour is as for
235 .Fn gets .
236 .It Xo
237 .Ft char *
238 .Fn fgets "char *buf" "int size" "int fd"
239 .Xc
240 .Pp
241 Read a line of at most
242 .Fa size Ns -1
243 characters into
244 .Fa buf .
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.
250 If an error occurs,
251 NULL is returned and the buffer contents are indeterminate.
252 .It Xo
253 .Ft int
254 .Fn fgetstr "char *buf" "int size" "int fd"
255 .Xc
256 .Pp
257 Read a line of at most
258 .Fa size
259 characters into
260 .Fa buf .
261 Line terminating characters are stripped, and the buffer is always
262 nul-terminated.
263 Returns the number of characters in
264 .Fa buf
265 if successful, or -1 if a read error occurs.
266 .It Xo
267 .Ft int
268 .Fn printf "const char *fmt" "..."
269 .Xc
270 .It Xo
271 .Ft void
272 .Fn vprintf "const char *fmt" "va_list ap"
273 .Xc
274 .It Xo
275 .Ft int
276 .Fn sprintf "char *buf" "const char *fmt" "..."
277 .Xc
278 .It Xo
279 .Ft void
280 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
281 .Xc
282 .Pp
283 The *printf functions implement a subset of the standard
284 .Fn printf
285 family functionality and some extensions.
286 The following standard conversions
287 are supported:
288 .Cm c , d , n , o , p , s , u , x .
289 The following modifiers are supported:
290 .Cm + , - , # , * , 0 , field width,  precision,  l .
291 .Pp
292 The
293 .Cm b
294 conversion is provided to decode error registers.
295 Its usage is:
296 .Bd -ragged -offset indent
297 printf(
298 .Qq reg=%b\en ,
299 regval,
300 .Qq <base><arg>*
301 );
302 .Ed
303 .Pp
304 where <base> is the output expressed as a control character, e.g.\& \e10 gives
305 octal, \e20 gives hex.
306 Each <arg> is a sequence of characters, the first of
307 which gives the bit number to be inspected (origin 1) and the next characters
308 (up to a character less than 32) give the text to be displayed if the bit is set.
309 Thus
310 .Bd -ragged -offset indent
311 printf(
312 .Qq reg=%b\en ,
313 3,
314 .Qq \e10\e2BITTWO\e1BITONE\en
315 );
316 .Ed
317 .Pp
318 would give the output
319 .Bd -ragged -offset indent
320 reg=3<BITTWO,BITONE>
321 .Ed
322 .Pp
323 The
324 .Cm D
325 conversion provides a hexdump facility, eg.
326 .Bd -ragged -offset indent
327 printf(
328 .Qq %6D ,
329 ptr,
330 .Qq \&:
331 );  gives
332 .Qq XX:XX:XX:XX:XX:XX
333 .Ed
334 .Bd -ragged -offset indent
335 printf(
336 .Qq %*D ,
337 len,
338 ptr,
339 .Qq "\ "
340 );  gives
341 .Qq XX XX XX ...
342 .Ed
343 .El
344 .Sh CHARACTER TESTS AND CONVERSIONS
345 .Bl -hang -width 10n
346 .It Xo
347 .Ft int
348 .Fn isupper "int c"
349 .Xc
350 .It Xo
351 .Ft int
352 .Fn islower "int c"
353 .Xc
354 .It Xo
355 .Ft int
356 .Fn isspace "int c"
357 .Xc
358 .It Xo
359 .Ft int
360 .Fn isdigit "int c"
361 .Xc
362 .It Xo
363 .Ft int
364 .Fn isxdigit "int c"
365 .Xc
366 .It Xo
367 .Ft int
368 .Fn isascii "int c"
369 .Xc
370 .It Xo
371 .Ft int
372 .Fn isalpha "int c"
373 .Xc
374 .It Xo
375 .Ft int
376 .Fn toupper "int c"
377 .Xc
378 .It Xo
379 .Ft int
380 .Fn tolower "int c"
381 .Xc
382 .El
383 .Sh FILE I/O
384 .Bl -hang -width 10n
385 .It Xo
386 .Ft int
387 .Fn open "const char *path" "int flags"
388 .Xc
389 .Pp
390 Similar to the behaviour as specified in
391 .Xr open 2 ,
392 except that file creation is not supported, so the mode parameter is not
393 required.
394 The
395 .Fa flags
396 argument may be one of
397 .Dv O_RDONLY ,
398 .Dv O_WRONLY
399 and
400 .Dv O_RDWR
401 (although no filesystems currently support writing).
402 .It Xo
403 .Ft int
404 .Fn close "int fd"
405 .Xc
406 .It Xo
407 .Ft void
408 .Fn closeall void
409 .Xc
410 .Pp
411 Close all open files.
412 .It Xo
413 .Ft ssize_t
414 .Fn read "int fd" "void *buf" "size_t len"
415 .Xc
416 .It Xo
417 .Ft ssize_t
418 .Fn write "int fd" "void *buf" "size_t len"
419 .Xc
420 .Pp
421 (No filesystems currently support writing.)
422 .It Xo
423 .Ft off_t
424 .Fn lseek "int fd" "off_t offset" "int whence"
425 .Xc
426 .Pp
427 Files being automatically uncompressed during reading cannot seek backwards
428 from the current point.
429 .It Xo
430 .Ft int
431 .Fn stat "const char *path" "struct stat *sb"
432 .Xc
433 .It Xo
434 .Ft int
435 .Fn fstat "int fd" "struct stat *sb"
436 .Xc
437 .Pp
438 The
439 .Fn stat
440 and
441 .Fn fstat
442 functions only fill out the following fields in the
443 .Fa sb
444 structure:
445 .Fa st_mode , st_nlink , st_uid , st_gid , st_size .
446 The
447 .Nm tftp
448 filesystem cannot provide meaningful values for this call, and the
449 .Nm cd9660
450 filesystem always reports files having uid/gid of zero.
451 .El
452 .Sh PAGER
453 .Nm
454 supplies a simple internal pager to ease reading the output of large commands.
455 .Bl -hang -width 10n
456 .It Xo
457 .Ft void
458 .Fn pager_open
459 .Xc
460 .Pp
461 Initialises the pager and tells it that the next line output will be the
462 top of the display.
463 The environment variable LINES is consulted to determine the number of
464 lines to be displayed before pausing.
465 .It Xo
466 .Ft void
467 .Fn pager_close void
468 .Xc
469 .Pp
470 Closes the pager.
471 .It Xo
472 .Ft int
473 .Fn pager_output "char *lines"
474 .Xc
475 .Pp
476 Sends the lines in the nul-terminated buffer at
477 .Fa lines
478 to the pager.
479 Newline characters are counted in order to determine the number
480 of lines being output (wrapped lines are not accounted for).
481 .Fn pager_output
482 will return zero when all of the lines have been output, or nonzero if the
483 display was paused and the user elected to quit.
484 .It Xo
485 .Ft int
486 .Fn pager_file "char *fname"
487 .Xc
488 .Pp
489 Attempts to open and display the file
490 .Fa fname .
491 Returns -1 on error, 0 at
492 .Dv EOF ,
493 or 1 if the user elects to quit while reading.
494 .El
495 .Sh NETWORK
496 .Bl -hang -width 10n
497 .It Xo
498 .Ft "char *"
499 .Fn ether_sprintf "u_char *ap"
500 .Xc
501 .Pp
502 Convert an ethernet address to its human readable notation as specified in IEEE 802.
503 .El
504 .Sh MISC
505 .Bl -hang -width 10n
506 .It Xo
507 .Ft void
508 .Fn twiddle void
509 .Xc
510 .Pp
511 Successive calls emit the characters in the sequence |, /, -, \\ followed by a
512 backspace in order to provide reassurance to the user.
513 .El
514 .Sh REQUIRED LOW-LEVEL SUPPORT
515 The following resources are consumed by
516 .Nm
517 - stack, heap, console and devices.
518 .Pp
519 The stack must be established before
520 .Nm
521 functions can be invoked.
522 Stack requirements vary depending on the functions
523 and filesystems used by the consumer and the support layer functions detailed
524 below.
525 .Pp
526 The heap must be established before calling
527 .Fn alloc
528 or
529 .Fn open
530 by calling
531 .Fn setheap .
532 Heap usage will vary depending on the number of simultaneously open files,
533 as well as client behaviour.
534 Automatic decompression will allocate more
535 than 64K of data per open file.
536 .Pp
537 Console access is performed via the
538 .Fn getchar ,
539 .Fn putchar
540 and
541 .Fn ischar
542 functions detailed below.
543 .Pp
544 Device access is initiated via
545 .Fn devopen
546 and is performed through the
547 .Fn dv_strategy ,
548 .Fn dv_ioctl
549 and
550 .Fn dv_close
551 functions in the device switch structure that
552 .Fn devopen
553 returns.
554 .Pp
555 The consumer must provide the following support functions:
556 .Bl -hang -width 10n
557 .It Xo
558 .Ft int
559 .Fn getchar void
560 .Xc
561 .Pp
562 Return a character from the console, used by
563 .Fn gets ,
564 .Fn ngets
565 and pager functions.
566 .It Xo
567 .Ft int
568 .Fn ischar void
569 .Xc
570 .Pp
571 Returns nonzero if a character is waiting from the console.
572 .It Xo
573 .Ft void
574 .Fn putchar int
575 .Xc
576 .Pp
577 Write a character to the console, used by
578 .Fn gets ,
579 .Fn ngets ,
580 .Fn *printf ,
581 .Fn panic
582 and
583 .Fn twiddle
584 and thus by many other functions for debugging and informational output.
585 .It Xo
586 .Ft int
587 .Fn devopen "struct open_file *of" "const char *name" "char **file"
588 .Xc
589 .Pp
590 Open the appropriate device for the file named in
591 .Fa name ,
592 returning in
593 .Fa file
594 a pointer to the remaining body of
595 .Fa name
596 which does not refer to the device.
597 The
598 .Va f_dev
599 field in
600 .Fa of
601 will be set to point to the
602 .Vt devsw
603 structure for the opened device if successful.
604 Device identifiers must
605 always precede the path component, but may otherwise be arbitrarily formatted.
606 Used by
607 .Fn open
608 and thus for all device-related I/O.
609 .It Xo
610 .Ft int
611 .Fn devclose "struct open_file *of"
612 .Xc
613 .Pp
614 Close the device allocated for
615 .Fa of .
616 The device driver itself will already have been called for the close;
617 this call should clean up any allocation made by
618 .Fn devopen
619 only.
620 .It Xo
621 .Ft void
622 .Fn panic "const char *msg" "..."
623 .Xc
624 .Pp
625 Signal a fatal and unrecoverable error condition.
626 The
627 .Fa msg ...
628 arguments are as for
629 .Fn printf .
630 .El
631 .Sh INTERNAL FILESYSTEMS
632 Internal filesystems are enabled by the consumer exporting the array
633 .Vt struct fs_ops *file_system[] ,
634 which should be initialised with pointers
635 to
636 .Vt struct fs_ops
637 structures.
638 The following filesystem handlers are supplied by
639 .Nm ,
640 the consumer may supply other filesystems of their own:
641 .Bl -hang -width ".Va cd9660_fsops"
642 .It Va ufs_fsops
643 The
644 .Bx
645 .Xr UFS 5 .
646 .It Va hammer_fsops
647 .Xr HAMMER 5
648 filesystem.
649 .It Va ext2fs_fsops
650 Linux ext2fs filesystem.
651 .It Va msdos_fsops
652 MS-DOS filesystem.
653 .It Va tftp_fsops
654 File access via TFTP.
655 .It Va nfs_fsops
656 File access via NFS.
657 .It Va cd9660_fsops
658 ISO 9660 (CD-ROM) filesystem.
659 .It Va zipfs_fsops
660 Stacked filesystem supporting gzipped files.
661 When trying the zipfs filesystem,
662 .Nm
663 appends
664 .Li .gz
665 to the end of the filename, and then tries to locate the file using the other
666 filesystems.
667 Placement of this filesystem in the
668 .Va file_system[]
669 array determines whether gzipped files will be opened in preference to non-gzipped
670 files.
671 It is only possible to seek a gzipped file forwards, and
672 .Fn stat
673 and
674 .Fn fstat
675 on gzipped files will report an invalid length.
676 .It Va bzipfs_fsops
677 The same as
678 .Va zipfs_fsops ,
679 but for
680 .Xr bzip2 1 Ns -compressed
681 files.
682 .El
683 .Pp
684 The array of
685 .Vt struct fs_ops
686 pointers should be terminated with a NULL.
687 .Sh DEVICES
688 Devices are exported by the supporting code via the array
689 .Vt struct devsw *devsw[]
690 which is a NULL terminated array of pointers to device switch structures.
691 .Sh HISTORY
692 .Nm
693 contains contributions from many sources, including:
694 .Bl -bullet -compact
695 .It
696 .Nm libsa
697 from
698 .Nx
699 .It
700 .Nm libc
701 and
702 .Nm libkern
703 from
704 .Fx 3.0 .
705 .It
706 .Nm zalloc
707 from
708 .An Matthew Dillon Aq dillon@backplane.com
709 .El
710 .Pp
711 The reorganisation and port to
712 .Fx 3.0 ,
713 the environment functions and this manpage were written by
714 .An Mike Smith Aq msmith@FreeBSD.org .
715 .Sh BUGS
716 The lack of detailed memory usage data is unhelpful.