xref: /freebsd/stand/libsa/libsa.3 (revision 3a7d8294f04a747c594a9c9e50dc68be64873ba5)
1*3a7d8294SWarner Losh.\" Copyright (c) Michael Smith
2*3a7d8294SWarner Losh.\" All rights reserved.
3*3a7d8294SWarner Losh.\"
4*3a7d8294SWarner Losh.\" Redistribution and use in source and binary forms, with or without
5*3a7d8294SWarner Losh.\" modification, are permitted provided that the following conditions
6*3a7d8294SWarner Losh.\" are met:
7*3a7d8294SWarner Losh.\" 1. Redistributions of source code must retain the above copyright
8*3a7d8294SWarner Losh.\"    notice, this list of conditions and the following disclaimer.
9*3a7d8294SWarner Losh.\" 2. Redistributions in binary form must reproduce the above copyright
10*3a7d8294SWarner Losh.\"    notice, this list of conditions and the following disclaimer in the
11*3a7d8294SWarner Losh.\"    documentation and/or other materials provided with the distribution.
12*3a7d8294SWarner Losh.\"
13*3a7d8294SWarner Losh.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*3a7d8294SWarner Losh.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*3a7d8294SWarner Losh.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*3a7d8294SWarner Losh.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*3a7d8294SWarner Losh.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*3a7d8294SWarner Losh.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*3a7d8294SWarner Losh.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*3a7d8294SWarner Losh.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*3a7d8294SWarner Losh.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*3a7d8294SWarner Losh.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*3a7d8294SWarner Losh.\" SUCH DAMAGE.
24*3a7d8294SWarner Losh.\"
25*3a7d8294SWarner Losh.\" $FreeBSD$
26*3a7d8294SWarner Losh.\"
27*3a7d8294SWarner Losh.Dd February 1, 2018
28*3a7d8294SWarner Losh.Dt LIBSTAND 3
29*3a7d8294SWarner Losh.Os
30*3a7d8294SWarner Losh.Sh NAME
31*3a7d8294SWarner Losh.Nm libstand
32*3a7d8294SWarner Losh.Nd support library for standalone executables
33*3a7d8294SWarner Losh.Sh SYNOPSIS
34*3a7d8294SWarner Losh.In stand.h
35*3a7d8294SWarner Losh.Sh DESCRIPTION
36*3a7d8294SWarner LoshThe
37*3a7d8294SWarner Losh.Nm
38*3a7d8294SWarner Loshlibrary provides a set of supporting functions for standalone
39*3a7d8294SWarner Loshapplications, mimicking where possible the standard
40*3a7d8294SWarner Losh.Bx
41*3a7d8294SWarner Loshprogramming
42*3a7d8294SWarner Loshenvironment.
43*3a7d8294SWarner LoshThe following sections group these functions by kind.
44*3a7d8294SWarner LoshUnless specifically described here, see the corresponding section 3
45*3a7d8294SWarner Loshmanpages for the given functions.
46*3a7d8294SWarner Losh.Sh STRING FUNCTIONS
47*3a7d8294SWarner LoshString functions are available as documented in
48*3a7d8294SWarner Losh.Xr string 3
49*3a7d8294SWarner Loshand
50*3a7d8294SWarner Losh.Xr bstring 3 .
51*3a7d8294SWarner Losh.Sh MEMORY ALLOCATION
52*3a7d8294SWarner Losh.Bl -hang -width 10n
53*3a7d8294SWarner Losh.It Xo
54*3a7d8294SWarner Losh.Ft "void *"
55*3a7d8294SWarner Losh.Fn malloc "size_t size"
56*3a7d8294SWarner Losh.Xc
57*3a7d8294SWarner Losh.Pp
58*3a7d8294SWarner LoshAllocate
59*3a7d8294SWarner Losh.Fa size
60*3a7d8294SWarner Loshbytes of memory from the heap using a best-fit algorithm.
61*3a7d8294SWarner Losh.It Xo
62*3a7d8294SWarner Losh.Ft void
63*3a7d8294SWarner Losh.Fn free "void *ptr"
64*3a7d8294SWarner Losh.Xc
65*3a7d8294SWarner Losh.Pp
66*3a7d8294SWarner LoshFree the allocated object at
67*3a7d8294SWarner Losh.Fa ptr .
68*3a7d8294SWarner Losh.It Xo
69*3a7d8294SWarner Losh.Ft void
70*3a7d8294SWarner Losh.Fn setheap "void *start" "void *limit"
71*3a7d8294SWarner Losh.Xc
72*3a7d8294SWarner Losh.Pp
73*3a7d8294SWarner LoshInitialise the heap.
74*3a7d8294SWarner LoshThis function must be called before calling
75*3a7d8294SWarner Losh.Fn alloc
76*3a7d8294SWarner Loshfor the first time.
77*3a7d8294SWarner LoshThe region between
78*3a7d8294SWarner Losh.Fa start
79*3a7d8294SWarner Loshand
80*3a7d8294SWarner Losh.Fa limit
81*3a7d8294SWarner Loshwill be used for the heap; attempting to allocate beyond this will result
82*3a7d8294SWarner Loshin a panic.
83*3a7d8294SWarner Losh.It Xo
84*3a7d8294SWarner Losh.Ft "char *"
85*3a7d8294SWarner Losh.Fn sbrk "int junk"
86*3a7d8294SWarner Losh.Xc
87*3a7d8294SWarner Losh.Pp
88*3a7d8294SWarner LoshProvides the behaviour of
89*3a7d8294SWarner Losh.Fn sbrk 0 ,
90*3a7d8294SWarner Loshi.e., returns the highest point that the heap has reached.
91*3a7d8294SWarner LoshThis value can
92*3a7d8294SWarner Loshbe used during testing to determine the actual heap usage.
93*3a7d8294SWarner LoshThe
94*3a7d8294SWarner Losh.Fa junk
95*3a7d8294SWarner Loshargument is ignored.
96*3a7d8294SWarner Losh.El
97*3a7d8294SWarner Losh.Sh ENVIRONMENT
98*3a7d8294SWarner LoshA set of functions are provided for manipulating a flat variable space similar
99*3a7d8294SWarner Loshto the traditional shell-supported environment.
100*3a7d8294SWarner LoshMajor enhancements are support
101*3a7d8294SWarner Loshfor set/unset hook functions.
102*3a7d8294SWarner Losh.Bl -hang -width 10n
103*3a7d8294SWarner Losh.It Xo
104*3a7d8294SWarner Losh.Ft "char *"
105*3a7d8294SWarner Losh.Fn getenv "const char *name"
106*3a7d8294SWarner Losh.Xc
107*3a7d8294SWarner Losh.It Xo
108*3a7d8294SWarner Losh.Ft int
109*3a7d8294SWarner Losh.Fn setenv "const char *name" "const char *value" "int overwrite"
110*3a7d8294SWarner Losh.Xc
111*3a7d8294SWarner Losh.It Xo
112*3a7d8294SWarner Losh.Ft int
113*3a7d8294SWarner Losh.Fn putenv "char *string"
114*3a7d8294SWarner Losh.Xc
115*3a7d8294SWarner Losh.It Xo
116*3a7d8294SWarner Losh.Ft int
117*3a7d8294SWarner Losh.Fn unsetenv "const char *name"
118*3a7d8294SWarner Losh.Xc
119*3a7d8294SWarner Losh.Pp
120*3a7d8294SWarner LoshThese functions behave similarly to their standard library counterparts.
121*3a7d8294SWarner Losh.It Xo
122*3a7d8294SWarner Losh.Ft "struct env_var *"
123*3a7d8294SWarner Losh.Fn env_getenv "const char *name"
124*3a7d8294SWarner Losh.Xc
125*3a7d8294SWarner Losh.Pp
126*3a7d8294SWarner LoshLooks up a variable in the environment and returns its entire
127*3a7d8294SWarner Loshdata structure.
128*3a7d8294SWarner Losh.It Xo
129*3a7d8294SWarner Losh.Ft int
130*3a7d8294SWarner Losh.Fn env_setenv "const char *name" "int flags" "const void *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
131*3a7d8294SWarner Losh.Xc
132*3a7d8294SWarner Losh.Pp
133*3a7d8294SWarner LoshCreates a new or sets an existing environment variable called
134*3a7d8294SWarner Losh.Fa name .
135*3a7d8294SWarner LoshIf creating a new variable, the
136*3a7d8294SWarner Losh.Fa sethook
137*3a7d8294SWarner Loshand
138*3a7d8294SWarner Losh.Fa unsethook
139*3a7d8294SWarner Losharguments may be specified.
140*3a7d8294SWarner Losh.Pp
141*3a7d8294SWarner LoshThe set hook is invoked whenever an attempt
142*3a7d8294SWarner Loshis made to set the variable, unless the EV_NOHOOK flag is set.
143*3a7d8294SWarner LoshTypically
144*3a7d8294SWarner Losha set hook will validate the
145*3a7d8294SWarner Losh.Fa value
146*3a7d8294SWarner Loshargument, and then call
147*3a7d8294SWarner Losh.Fn env_setenv
148*3a7d8294SWarner Loshagain with EV_NOHOOK set to actually save the value.
149*3a7d8294SWarner LoshThe predefined function
150*3a7d8294SWarner Losh.Fn env_noset
151*3a7d8294SWarner Loshmay be specified to refuse all attempts to set a variable.
152*3a7d8294SWarner Losh.Pp
153*3a7d8294SWarner LoshThe unset hook is invoked when an attempt is made to unset a variable.
154*3a7d8294SWarner LoshIf it
155*3a7d8294SWarner Loshreturns zero, the variable will be unset.
156*3a7d8294SWarner LoshThe predefined function
157*3a7d8294SWarner Losh.Fa env_nounset
158*3a7d8294SWarner Loshmay be used to prevent a variable being unset.
159*3a7d8294SWarner Losh.El
160*3a7d8294SWarner Losh.Sh STANDARD LIBRARY SUPPORT
161*3a7d8294SWarner Losh.Bl -hang -width 10n
162*3a7d8294SWarner Losh.It Xo
163*3a7d8294SWarner Losh.Ft int
164*3a7d8294SWarner Losh.Fn abs "int i"
165*3a7d8294SWarner Losh.Xc
166*3a7d8294SWarner Losh.It Xo
167*3a7d8294SWarner Losh.Ft int
168*3a7d8294SWarner Losh.Fn getopt "int argc" "char * const *argv" "const char *optstring"
169*3a7d8294SWarner Losh.Xc
170*3a7d8294SWarner Losh.It Xo
171*3a7d8294SWarner Losh.Ft long
172*3a7d8294SWarner Losh.Fn strtol "const char *nptr" "char **endptr" "int base"
173*3a7d8294SWarner Losh.Xc
174*3a7d8294SWarner Losh.It Xo
175*3a7d8294SWarner Losh.Ft long long
176*3a7d8294SWarner Losh.Fn strtoll "const char *nptr" "char **endptr" "int base"
177*3a7d8294SWarner Losh.Xc
178*3a7d8294SWarner Losh.It Xo
179*3a7d8294SWarner Losh.Ft long
180*3a7d8294SWarner Losh.Fn strtoul "const char *nptr" "char **endptr" "int base"
181*3a7d8294SWarner Losh.Xc
182*3a7d8294SWarner Losh.It Xo
183*3a7d8294SWarner Losh.Ft long long
184*3a7d8294SWarner Losh.Fn strtoull "const char *nptr" "char **endptr" "int base"
185*3a7d8294SWarner Losh.Xc
186*3a7d8294SWarner Losh.It Xo
187*3a7d8294SWarner Losh.Ft void
188*3a7d8294SWarner Losh.Fn srandom "unsigned int seed"
189*3a7d8294SWarner Losh.Xc
190*3a7d8294SWarner Losh.It Xo
191*3a7d8294SWarner Losh.Ft "long"
192*3a7d8294SWarner Losh.Fn random void
193*3a7d8294SWarner Losh.Xc
194*3a7d8294SWarner Losh.It Xo
195*3a7d8294SWarner Losh.Ft "char *"
196*3a7d8294SWarner Losh.Fn strerror "int error"
197*3a7d8294SWarner Losh.Xc
198*3a7d8294SWarner Losh.Pp
199*3a7d8294SWarner LoshReturns error messages for the subset of errno values supported by
200*3a7d8294SWarner Losh.Nm .
201*3a7d8294SWarner Losh.It Fn assert expression
202*3a7d8294SWarner Losh.Pp
203*3a7d8294SWarner LoshRequires
204*3a7d8294SWarner Losh.In assert.h .
205*3a7d8294SWarner Losh.It Xo
206*3a7d8294SWarner Losh.Ft int
207*3a7d8294SWarner Losh.Fn setjmp "jmp_buf env"
208*3a7d8294SWarner Losh.Xc
209*3a7d8294SWarner Losh.It Xo
210*3a7d8294SWarner Losh.Ft void
211*3a7d8294SWarner Losh.Fn longjmp "jmp_buf env" "int val"
212*3a7d8294SWarner Losh.Xc
213*3a7d8294SWarner Losh.Pp
214*3a7d8294SWarner LoshDefined as
215*3a7d8294SWarner Losh.Fn _setjmp
216*3a7d8294SWarner Loshand
217*3a7d8294SWarner Losh.Fn _longjmp
218*3a7d8294SWarner Loshrespectively as there is no signal state to manipulate.
219*3a7d8294SWarner LoshRequires
220*3a7d8294SWarner Losh.In setjmp.h .
221*3a7d8294SWarner Losh.El
222*3a7d8294SWarner Losh.Sh CHARACTER I/O
223*3a7d8294SWarner Losh.Bl -hang -width 10n
224*3a7d8294SWarner Losh.It Xo
225*3a7d8294SWarner Losh.Ft void
226*3a7d8294SWarner Losh.Fn gets "char *buf"
227*3a7d8294SWarner Losh.Xc
228*3a7d8294SWarner Losh.Pp
229*3a7d8294SWarner LoshRead characters from the console into
230*3a7d8294SWarner Losh.Fa buf .
231*3a7d8294SWarner LoshAll of the standard cautions apply to this function.
232*3a7d8294SWarner Losh.It Xo
233*3a7d8294SWarner Losh.Ft void
234*3a7d8294SWarner Losh.Fn ngets "char *buf" "int size"
235*3a7d8294SWarner Losh.Xc
236*3a7d8294SWarner Losh.Pp
237*3a7d8294SWarner LoshRead at most
238*3a7d8294SWarner Losh.Fa size
239*3a7d8294SWarner Losh- 1 characters from the console into
240*3a7d8294SWarner Losh.Fa buf .
241*3a7d8294SWarner LoshIf
242*3a7d8294SWarner Losh.Fa size
243*3a7d8294SWarner Loshis less than 1, the function's behaviour is as for
244*3a7d8294SWarner Losh.Fn gets .
245*3a7d8294SWarner Losh.It Xo
246*3a7d8294SWarner Losh.Ft int
247*3a7d8294SWarner Losh.Fn fgetstr "char *buf" "int size" "int fd"
248*3a7d8294SWarner Losh.Xc
249*3a7d8294SWarner Losh.Pp
250*3a7d8294SWarner LoshRead a line of at most
251*3a7d8294SWarner Losh.Fa size
252*3a7d8294SWarner Loshcharacters into
253*3a7d8294SWarner Losh.Fa buf .
254*3a7d8294SWarner LoshLine terminating characters are stripped, and the buffer is always
255*3a7d8294SWarner Losh.Dv NUL
256*3a7d8294SWarner Loshterminated.
257*3a7d8294SWarner LoshReturns the number of characters in
258*3a7d8294SWarner Losh.Fa buf
259*3a7d8294SWarner Loshif successful, or -1 if a read error occurs.
260*3a7d8294SWarner Losh.It Xo
261*3a7d8294SWarner Losh.Ft int
262*3a7d8294SWarner Losh.Fn printf "const char *fmt" "..."
263*3a7d8294SWarner Losh.Xc
264*3a7d8294SWarner Losh.It Xo
265*3a7d8294SWarner Losh.Ft void
266*3a7d8294SWarner Losh.Fn vprintf "const char *fmt" "va_list ap"
267*3a7d8294SWarner Losh.Xc
268*3a7d8294SWarner Losh.It Xo
269*3a7d8294SWarner Losh.Ft int
270*3a7d8294SWarner Losh.Fn sprintf "char *buf" "const char *fmt" "..."
271*3a7d8294SWarner Losh.Xc
272*3a7d8294SWarner Losh.It Xo
273*3a7d8294SWarner Losh.Ft void
274*3a7d8294SWarner Losh.Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
275*3a7d8294SWarner Losh.Xc
276*3a7d8294SWarner Losh.Pp
277*3a7d8294SWarner LoshThe *printf functions implement a subset of the standard
278*3a7d8294SWarner Losh.Fn printf
279*3a7d8294SWarner Loshfamily functionality and some extensions.
280*3a7d8294SWarner LoshThe following standard conversions
281*3a7d8294SWarner Loshare supported: c,d,n,o,p,s,u,x.
282*3a7d8294SWarner LoshThe following modifiers are supported:
283*3a7d8294SWarner Losh+,-,#,*,0,field width,precision,l.
284*3a7d8294SWarner Losh.Pp
285*3a7d8294SWarner LoshThe
286*3a7d8294SWarner Losh.Li b
287*3a7d8294SWarner Loshconversion is provided to decode error registers.
288*3a7d8294SWarner LoshIts usage is:
289*3a7d8294SWarner Losh.Bd -ragged -offset indent
290*3a7d8294SWarner Loshprintf(
291*3a7d8294SWarner Losh.Qq reg=%b\en ,
292*3a7d8294SWarner Loshregval,
293*3a7d8294SWarner Losh.Qq <base><arg>*
294*3a7d8294SWarner Losh);
295*3a7d8294SWarner Losh.Ed
296*3a7d8294SWarner Losh.Pp
297*3a7d8294SWarner Loshwhere <base> is the output expressed as a control character, e.g.\& \e10 gives
298*3a7d8294SWarner Loshoctal, \e20 gives hex.
299*3a7d8294SWarner LoshEach <arg> is a sequence of characters, the first of
300*3a7d8294SWarner Loshwhich gives the bit number to be inspected (origin 1) and the next characters
301*3a7d8294SWarner Losh(up to a character less than 32) give the text to be displayed if the bit is set.
302*3a7d8294SWarner LoshThus
303*3a7d8294SWarner Losh.Bd -ragged -offset indent
304*3a7d8294SWarner Loshprintf(
305*3a7d8294SWarner Losh.Qq reg=%b\en ,
306*3a7d8294SWarner Losh3,
307*3a7d8294SWarner Losh.Qq \e10\e2BITTWO\e1BITONE
308*3a7d8294SWarner Losh);
309*3a7d8294SWarner Losh.Ed
310*3a7d8294SWarner Losh.Pp
311*3a7d8294SWarner Loshwould give the output
312*3a7d8294SWarner Losh.Bd -ragged -offset indent
313*3a7d8294SWarner Loshreg=3<BITTWO,BITONE>
314*3a7d8294SWarner Losh.Ed
315*3a7d8294SWarner Losh.Pp
316*3a7d8294SWarner LoshThe
317*3a7d8294SWarner Losh.Li D
318*3a7d8294SWarner Loshconversion provides a hexdump facility, e.g.
319*3a7d8294SWarner Losh.Bd -ragged -offset indent
320*3a7d8294SWarner Loshprintf(
321*3a7d8294SWarner Losh.Qq %6D ,
322*3a7d8294SWarner Loshptr,
323*3a7d8294SWarner Losh.Qq \&:
324*3a7d8294SWarner Losh); gives
325*3a7d8294SWarner Losh.Qq XX:XX:XX:XX:XX:XX
326*3a7d8294SWarner Losh.Ed
327*3a7d8294SWarner Losh.Bd -ragged -offset indent
328*3a7d8294SWarner Loshprintf(
329*3a7d8294SWarner Losh.Qq %*D ,
330*3a7d8294SWarner Loshlen,
331*3a7d8294SWarner Loshptr,
332*3a7d8294SWarner Losh.Qq "\ "
333*3a7d8294SWarner Losh); gives
334*3a7d8294SWarner Losh.Qq XX XX XX ...
335*3a7d8294SWarner Losh.Ed
336*3a7d8294SWarner Losh.El
337*3a7d8294SWarner Losh.Sh CHARACTER TESTS AND CONVERSIONS
338*3a7d8294SWarner Losh.Bl -hang -width 10n
339*3a7d8294SWarner Losh.It Xo
340*3a7d8294SWarner Losh.Ft int
341*3a7d8294SWarner Losh.Fn isupper "int c"
342*3a7d8294SWarner Losh.Xc
343*3a7d8294SWarner Losh.It Xo
344*3a7d8294SWarner Losh.Ft int
345*3a7d8294SWarner Losh.Fn islower "int c"
346*3a7d8294SWarner Losh.Xc
347*3a7d8294SWarner Losh.It Xo
348*3a7d8294SWarner Losh.Ft int
349*3a7d8294SWarner Losh.Fn isspace "int c"
350*3a7d8294SWarner Losh.Xc
351*3a7d8294SWarner Losh.It Xo
352*3a7d8294SWarner Losh.Ft int
353*3a7d8294SWarner Losh.Fn isdigit "int c"
354*3a7d8294SWarner Losh.Xc
355*3a7d8294SWarner Losh.It Xo
356*3a7d8294SWarner Losh.Ft int
357*3a7d8294SWarner Losh.Fn isxdigit "int c"
358*3a7d8294SWarner Losh.Xc
359*3a7d8294SWarner Losh.It Xo
360*3a7d8294SWarner Losh.Ft int
361*3a7d8294SWarner Losh.Fn isascii "int c"
362*3a7d8294SWarner Losh.Xc
363*3a7d8294SWarner Losh.It Xo
364*3a7d8294SWarner Losh.Ft int
365*3a7d8294SWarner Losh.Fn isalpha "int c"
366*3a7d8294SWarner Losh.Xc
367*3a7d8294SWarner Losh.It Xo
368*3a7d8294SWarner Losh.Ft int
369*3a7d8294SWarner Losh.Fn isalnum "int c"
370*3a7d8294SWarner Losh.Xc
371*3a7d8294SWarner Losh.It Xo
372*3a7d8294SWarner Losh.Ft int
373*3a7d8294SWarner Losh.Fn iscntrl "int c"
374*3a7d8294SWarner Losh.Xc
375*3a7d8294SWarner Losh.It Xo
376*3a7d8294SWarner Losh.Ft int
377*3a7d8294SWarner Losh.Fn isgraph "int c"
378*3a7d8294SWarner Losh.Xc
379*3a7d8294SWarner Losh.It Xo
380*3a7d8294SWarner Losh.Ft int
381*3a7d8294SWarner Losh.Fn ispunct "int c"
382*3a7d8294SWarner Losh.Xc
383*3a7d8294SWarner Losh.It Xo
384*3a7d8294SWarner Losh.Ft int
385*3a7d8294SWarner Losh.Fn toupper "int c"
386*3a7d8294SWarner Losh.Xc
387*3a7d8294SWarner Losh.It Xo
388*3a7d8294SWarner Losh.Ft int
389*3a7d8294SWarner Losh.Fn tolower "int c"
390*3a7d8294SWarner Losh.Xc
391*3a7d8294SWarner Losh.El
392*3a7d8294SWarner Losh.Sh FILE I/O
393*3a7d8294SWarner Losh.Bl -hang -width 10n
394*3a7d8294SWarner Losh.It Xo
395*3a7d8294SWarner Losh.Ft int
396*3a7d8294SWarner Losh.Fn open "const char *path" "int flags"
397*3a7d8294SWarner Losh.Xc
398*3a7d8294SWarner Losh.Pp
399*3a7d8294SWarner LoshSimilar to the behaviour as specified in
400*3a7d8294SWarner Losh.Xr open 2 ,
401*3a7d8294SWarner Loshexcept that file creation is not supported, so the mode parameter is not
402*3a7d8294SWarner Loshrequired.
403*3a7d8294SWarner LoshThe
404*3a7d8294SWarner Losh.Fa flags
405*3a7d8294SWarner Loshargument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no file systems
406*3a7d8294SWarner Loshcurrently support writing).
407*3a7d8294SWarner Losh.It Xo
408*3a7d8294SWarner Losh.Ft int
409*3a7d8294SWarner Losh.Fn close "int fd"
410*3a7d8294SWarner Losh.Xc
411*3a7d8294SWarner Losh.It Xo
412*3a7d8294SWarner Losh.Ft void
413*3a7d8294SWarner Losh.Fn closeall void
414*3a7d8294SWarner Losh.Xc
415*3a7d8294SWarner Losh.Pp
416*3a7d8294SWarner LoshClose all open files.
417*3a7d8294SWarner Losh.It Xo
418*3a7d8294SWarner Losh.Ft ssize_t
419*3a7d8294SWarner Losh.Fn read "int fd" "void *buf" "size_t len"
420*3a7d8294SWarner Losh.Xc
421*3a7d8294SWarner Losh.It Xo
422*3a7d8294SWarner Losh.Ft ssize_t
423*3a7d8294SWarner Losh.Fn write "int fd" "void *buf" "size_t len"
424*3a7d8294SWarner Losh.Xc
425*3a7d8294SWarner Losh.Pp
426*3a7d8294SWarner Losh(No file systems currently support writing.)
427*3a7d8294SWarner Losh.It Xo
428*3a7d8294SWarner Losh.Ft off_t
429*3a7d8294SWarner Losh.Fn lseek "int fd" "off_t offset" "int whence"
430*3a7d8294SWarner Losh.Xc
431*3a7d8294SWarner Losh.Pp
432*3a7d8294SWarner LoshFiles being automatically uncompressed during reading cannot seek backwards
433*3a7d8294SWarner Loshfrom the current point.
434*3a7d8294SWarner Losh.It Xo
435*3a7d8294SWarner Losh.Ft int
436*3a7d8294SWarner Losh.Fn stat "const char *path" "struct stat *sb"
437*3a7d8294SWarner Losh.Xc
438*3a7d8294SWarner Losh.It Xo
439*3a7d8294SWarner Losh.Ft int
440*3a7d8294SWarner Losh.Fn fstat "int fd" "struct stat *sb"
441*3a7d8294SWarner Losh.Xc
442*3a7d8294SWarner Losh.Pp
443*3a7d8294SWarner LoshThe
444*3a7d8294SWarner Losh.Fn stat
445*3a7d8294SWarner Loshand
446*3a7d8294SWarner Losh.Fn fstat
447*3a7d8294SWarner Loshfunctions only fill out the following fields in the
448*3a7d8294SWarner Losh.Fa sb
449*3a7d8294SWarner Loshstructure: st_mode,st_nlink,st_uid,st_gid,st_size.
450*3a7d8294SWarner LoshThe
451*3a7d8294SWarner Losh.Nm tftp
452*3a7d8294SWarner Loshfile system cannot provide meaningful values for this call, and the
453*3a7d8294SWarner Losh.Nm cd9660
454*3a7d8294SWarner Loshfile system always reports files having uid/gid of zero.
455*3a7d8294SWarner Losh.El
456*3a7d8294SWarner Losh.Sh PAGER
457*3a7d8294SWarner LoshThe
458*3a7d8294SWarner Losh.Nm
459*3a7d8294SWarner Loshlibrary supplies a simple internal pager to ease reading the output of large
460*3a7d8294SWarner Loshcommands.
461*3a7d8294SWarner Losh.Bl -hang -width 10n
462*3a7d8294SWarner Losh.It Xo
463*3a7d8294SWarner Losh.Ft void
464*3a7d8294SWarner Losh.Fn pager_open
465*3a7d8294SWarner Losh.Xc
466*3a7d8294SWarner Losh.Pp
467*3a7d8294SWarner LoshInitialises the pager and tells it that the next line output will be the top of the
468*3a7d8294SWarner Loshdisplay.
469*3a7d8294SWarner LoshThe environment variable LINES is consulted to determine the number of
470*3a7d8294SWarner Loshlines to be displayed before pausing.
471*3a7d8294SWarner Losh.It Xo
472*3a7d8294SWarner Losh.Ft void
473*3a7d8294SWarner Losh.Fn pager_close void
474*3a7d8294SWarner Losh.Xc
475*3a7d8294SWarner Losh.Pp
476*3a7d8294SWarner LoshCloses the pager.
477*3a7d8294SWarner Losh.It Xo
478*3a7d8294SWarner Losh.Ft int
479*3a7d8294SWarner Losh.Fn pager_output "const char *lines"
480*3a7d8294SWarner Losh.Xc
481*3a7d8294SWarner Losh.Pp
482*3a7d8294SWarner LoshSends the lines in the
483*3a7d8294SWarner Losh.Dv NUL Ns
484*3a7d8294SWarner Losh-terminated buffer at
485*3a7d8294SWarner Losh.Fa lines
486*3a7d8294SWarner Loshto the pager.
487*3a7d8294SWarner LoshNewline characters are counted in order to determine the number
488*3a7d8294SWarner Loshof lines being output (wrapped lines are not accounted for).
489*3a7d8294SWarner LoshThe
490*3a7d8294SWarner Losh.Fn pager_output
491*3a7d8294SWarner Loshfunction will return zero when all of the lines have been output, or nonzero
492*3a7d8294SWarner Loshif the display was paused and the user elected to quit.
493*3a7d8294SWarner Losh.It Xo
494*3a7d8294SWarner Losh.Ft int
495*3a7d8294SWarner Losh.Fn pager_file "const char *fname"
496*3a7d8294SWarner Losh.Xc
497*3a7d8294SWarner Losh.Pp
498*3a7d8294SWarner LoshAttempts to open and display the file
499*3a7d8294SWarner Losh.Fa fname .
500*3a7d8294SWarner LoshReturns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
501*3a7d8294SWarner Losh.El
502*3a7d8294SWarner Losh.Sh MISC
503*3a7d8294SWarner Losh.Bl -hang -width 10n
504*3a7d8294SWarner Losh.It Xo
505*3a7d8294SWarner Losh.Ft void
506*3a7d8294SWarner Losh.Fn twiddle void
507*3a7d8294SWarner Losh.Xc
508*3a7d8294SWarner Losh.Pp
509*3a7d8294SWarner LoshSuccessive calls emit the characters in the sequence |,/,-,\\ followed by a
510*3a7d8294SWarner Loshbackspace in order to provide reassurance to the user.
511*3a7d8294SWarner Losh.El
512*3a7d8294SWarner Losh.Sh REQUIRED LOW-LEVEL SUPPORT
513*3a7d8294SWarner LoshThe following resources are consumed by
514*3a7d8294SWarner Losh.Nm
515*3a7d8294SWarner Losh- stack, heap, console and devices.
516*3a7d8294SWarner Losh.Pp
517*3a7d8294SWarner LoshThe stack must be established before
518*3a7d8294SWarner Losh.Nm
519*3a7d8294SWarner Loshfunctions can be invoked.
520*3a7d8294SWarner LoshStack requirements vary depending on the functions
521*3a7d8294SWarner Loshand file systems used by the consumer and the support layer functions detailed
522*3a7d8294SWarner Loshbelow.
523*3a7d8294SWarner Losh.Pp
524*3a7d8294SWarner LoshThe heap must be established before calling
525*3a7d8294SWarner Losh.Fn alloc
526*3a7d8294SWarner Loshor
527*3a7d8294SWarner Losh.Fn open
528*3a7d8294SWarner Loshby calling
529*3a7d8294SWarner Losh.Fn setheap .
530*3a7d8294SWarner LoshHeap usage will vary depending on the number of simultaneously open files,
531*3a7d8294SWarner Loshas well as client behaviour.
532*3a7d8294SWarner LoshAutomatic decompression will allocate more
533*3a7d8294SWarner Loshthan 64K of data per open file.
534*3a7d8294SWarner Losh.Pp
535*3a7d8294SWarner LoshConsole access is performed via the
536*3a7d8294SWarner Losh.Fn getchar ,
537*3a7d8294SWarner Losh.Fn putchar
538*3a7d8294SWarner Loshand
539*3a7d8294SWarner Losh.Fn ischar
540*3a7d8294SWarner Loshfunctions detailed below.
541*3a7d8294SWarner Losh.Pp
542*3a7d8294SWarner LoshDevice access is initiated via
543*3a7d8294SWarner Losh.Fn devopen
544*3a7d8294SWarner Loshand is performed through the
545*3a7d8294SWarner Losh.Fn dv_strategy ,
546*3a7d8294SWarner Losh.Fn dv_ioctl
547*3a7d8294SWarner Loshand
548*3a7d8294SWarner Losh.Fn dv_close
549*3a7d8294SWarner Loshfunctions in the device switch structure that
550*3a7d8294SWarner Losh.Fn devopen
551*3a7d8294SWarner Loshreturns.
552*3a7d8294SWarner Losh.Pp
553*3a7d8294SWarner LoshThe consumer must provide the following support functions:
554*3a7d8294SWarner Losh.Bl -hang -width 10n
555*3a7d8294SWarner Losh.It Xo
556*3a7d8294SWarner Losh.Ft int
557*3a7d8294SWarner Losh.Fn getchar void
558*3a7d8294SWarner Losh.Xc
559*3a7d8294SWarner Losh.Pp
560*3a7d8294SWarner LoshReturn a character from the console, used by
561*3a7d8294SWarner Losh.Fn gets ,
562*3a7d8294SWarner Losh.Fn ngets
563*3a7d8294SWarner Loshand pager functions.
564*3a7d8294SWarner Losh.It Xo
565*3a7d8294SWarner Losh.Ft int
566*3a7d8294SWarner Losh.Fn ischar void
567*3a7d8294SWarner Losh.Xc
568*3a7d8294SWarner Losh.Pp
569*3a7d8294SWarner LoshReturns nonzero if a character is waiting from the console.
570*3a7d8294SWarner Losh.It Xo
571*3a7d8294SWarner Losh.Ft void
572*3a7d8294SWarner Losh.Fn putchar int
573*3a7d8294SWarner Losh.Xc
574*3a7d8294SWarner Losh.Pp
575*3a7d8294SWarner LoshWrite a character to the console, used by
576*3a7d8294SWarner Losh.Fn gets ,
577*3a7d8294SWarner Losh.Fn ngets ,
578*3a7d8294SWarner Losh.Fn *printf ,
579*3a7d8294SWarner Losh.Fn panic
580*3a7d8294SWarner Loshand
581*3a7d8294SWarner Losh.Fn twiddle
582*3a7d8294SWarner Loshand thus by many other functions for debugging and informational output.
583*3a7d8294SWarner Losh.It Xo
584*3a7d8294SWarner Losh.Ft int
585*3a7d8294SWarner Losh.Fn devopen "struct open_file *of" "const char *name" "const char **file"
586*3a7d8294SWarner Losh.Xc
587*3a7d8294SWarner Losh.Pp
588*3a7d8294SWarner LoshOpen the appropriate device for the file named in
589*3a7d8294SWarner Losh.Fa name ,
590*3a7d8294SWarner Loshreturning in
591*3a7d8294SWarner Losh.Fa file
592*3a7d8294SWarner Losha pointer to the remaining body of
593*3a7d8294SWarner Losh.Fa name
594*3a7d8294SWarner Loshwhich does not refer to the device.
595*3a7d8294SWarner LoshThe
596*3a7d8294SWarner Losh.Va f_dev
597*3a7d8294SWarner Loshfield in
598*3a7d8294SWarner Losh.Fa of
599*3a7d8294SWarner Loshwill be set to point to the
600*3a7d8294SWarner Losh.Vt devsw
601*3a7d8294SWarner Loshstructure for the opened device if successful.
602*3a7d8294SWarner LoshDevice identifiers must
603*3a7d8294SWarner Loshalways precede the path component, but may otherwise be arbitrarily formatted.
604*3a7d8294SWarner LoshUsed by
605*3a7d8294SWarner Losh.Fn open
606*3a7d8294SWarner Loshand thus for all device-related I/O.
607*3a7d8294SWarner Losh.It Xo
608*3a7d8294SWarner Losh.Ft int
609*3a7d8294SWarner Losh.Fn devclose "struct open_file *of"
610*3a7d8294SWarner Losh.Xc
611*3a7d8294SWarner Losh.Pp
612*3a7d8294SWarner LoshClose the device allocated for
613*3a7d8294SWarner Losh.Fa of .
614*3a7d8294SWarner LoshThe device driver itself will already have been called for the close; this call
615*3a7d8294SWarner Loshshould clean up any allocation made by devopen only.
616*3a7d8294SWarner Losh.It Xo
617*3a7d8294SWarner Losh.Ft void
618*3a7d8294SWarner Losh.Fn abort
619*3a7d8294SWarner Losh.Xc
620*3a7d8294SWarner Losh.Pp
621*3a7d8294SWarner LoshCalls
622*3a7d8294SWarner Losh.Fn panic
623*3a7d8294SWarner Loshwith a fixed string.
624*3a7d8294SWarner Losh.It Xo
625*3a7d8294SWarner Losh.Ft void
626*3a7d8294SWarner Losh.Fn panic "const char *msg" "..."
627*3a7d8294SWarner Losh.Xc
628*3a7d8294SWarner Losh.Pp
629*3a7d8294SWarner LoshSignal a fatal and unrecoverable error condition.
630*3a7d8294SWarner LoshThe
631*3a7d8294SWarner Losh.Fa msg ...
632*3a7d8294SWarner Losharguments are as for
633*3a7d8294SWarner Losh.Fn printf .
634*3a7d8294SWarner Losh.El
635*3a7d8294SWarner Losh.Sh INTERNAL FILE SYSTEMS
636*3a7d8294SWarner LoshInternal file systems are enabled by the consumer exporting the array
637*3a7d8294SWarner Losh.Vt struct fs_ops *file_system[] ,
638*3a7d8294SWarner Loshwhich should be initialised with pointers
639*3a7d8294SWarner Loshto
640*3a7d8294SWarner Losh.Vt struct fs_ops
641*3a7d8294SWarner Loshstructures.
642*3a7d8294SWarner LoshThe following file system handlers are supplied by
643*3a7d8294SWarner Losh.Nm ,
644*3a7d8294SWarner Loshthe consumer may supply other file systems of their own:
645*3a7d8294SWarner Losh.Bl -hang -width ".Va cd9660_fsops"
646*3a7d8294SWarner Losh.It Va ufs_fsops
647*3a7d8294SWarner LoshThe
648*3a7d8294SWarner Losh.Bx
649*3a7d8294SWarner LoshUFS.
650*3a7d8294SWarner Losh.It Va ext2fs_fsops
651*3a7d8294SWarner LoshLinux ext2fs file system.
652*3a7d8294SWarner Losh.It Va tftp_fsops
653*3a7d8294SWarner LoshFile access via TFTP.
654*3a7d8294SWarner Losh.It Va nfs_fsops
655*3a7d8294SWarner LoshFile access via NFS.
656*3a7d8294SWarner Losh.It Va cd9660_fsops
657*3a7d8294SWarner LoshISO 9660 (CD-ROM) file system.
658*3a7d8294SWarner Losh.It Va gzipfs_fsops
659*3a7d8294SWarner LoshStacked file system supporting gzipped files.
660*3a7d8294SWarner LoshWhen trying the gzipfs file system,
661*3a7d8294SWarner Losh.Nm
662*3a7d8294SWarner Loshappends
663*3a7d8294SWarner Losh.Li .gz
664*3a7d8294SWarner Loshto the end of the filename, and then tries to locate the file using the other
665*3a7d8294SWarner Loshfile systems.
666*3a7d8294SWarner LoshPlacement of this file system in the
667*3a7d8294SWarner Losh.Va file_system[]
668*3a7d8294SWarner Losharray determines whether gzipped files will be opened in preference to non-gzipped
669*3a7d8294SWarner Loshfiles.
670*3a7d8294SWarner LoshIt is only possible to seek a gzipped file forwards, and
671*3a7d8294SWarner Losh.Fn stat
672*3a7d8294SWarner Loshand
673*3a7d8294SWarner Losh.Fn fstat
674*3a7d8294SWarner Loshon gzipped files will report an invalid length.
675*3a7d8294SWarner Losh.It Va bzipfs_fsops
676*3a7d8294SWarner LoshThe same as
677*3a7d8294SWarner Losh.Va gzipfs_fsops ,
678*3a7d8294SWarner Loshbut for
679*3a7d8294SWarner Losh.Xr bzip2 1 Ns -compressed
680*3a7d8294SWarner Loshfiles.
681*3a7d8294SWarner Losh.El
682*3a7d8294SWarner Losh.Pp
683*3a7d8294SWarner LoshThe array of
684*3a7d8294SWarner Losh.Vt struct fs_ops
685*3a7d8294SWarner Loshpointers should be terminated with a NULL.
686*3a7d8294SWarner Losh.Sh DEVICES
687*3a7d8294SWarner LoshDevices are exported by the supporting code via the array
688*3a7d8294SWarner Losh.Vt struct devsw *devsw[]
689*3a7d8294SWarner Loshwhich is a NULL terminated array of pointers to device switch structures.
690*3a7d8294SWarner Losh.Sh HISTORY
691*3a7d8294SWarner LoshThe
692*3a7d8294SWarner Losh.Nm
693*3a7d8294SWarner Loshlibrary contains contributions from many sources, including:
694*3a7d8294SWarner Losh.Bl -bullet -compact
695*3a7d8294SWarner Losh.It
696*3a7d8294SWarner Losh.Nm libsa
697*3a7d8294SWarner Loshfrom
698*3a7d8294SWarner Losh.Nx
699*3a7d8294SWarner Losh.It
700*3a7d8294SWarner Losh.Nm libc
701*3a7d8294SWarner Loshand
702*3a7d8294SWarner Losh.Nm libkern
703*3a7d8294SWarner Loshfrom
704*3a7d8294SWarner Losh.Fx 3.0 .
705*3a7d8294SWarner Losh.It
706*3a7d8294SWarner Losh.Nm zalloc
707*3a7d8294SWarner Loshfrom
708*3a7d8294SWarner Losh.An Matthew Dillon Aq Mt dillon@backplane.com
709*3a7d8294SWarner Losh.El
710*3a7d8294SWarner Losh.Pp
711*3a7d8294SWarner LoshThe reorganisation and port to
712*3a7d8294SWarner Losh.Fx 3.0 ,
713*3a7d8294SWarner Loshthe environment functions and this manpage were written by
714*3a7d8294SWarner Losh.An Mike Smith Aq Mt msmith@FreeBSD.org .
715*3a7d8294SWarner Losh.Sh BUGS
716*3a7d8294SWarner LoshThe lack of detailed memory usage data is unhelpful.
717