xref: /freebsd/contrib/openpam/doc/man/openpam_readword.3 (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1.\" Generated from openpam_readword.c by gendoc.pl
2.Dd June 27, 2023
3.Dt OPENPAM_READWORD 3
4.Os
5.Sh NAME
6.Nm openpam_readword
7.Nd read a word from a file, respecting shell quoting rules
8.Sh SYNOPSIS
9.In sys/types.h
10.In stdio.h
11.In security/pam_appl.h
12.In security/openpam.h
13.Ft "char *"
14.Fn openpam_readword "FILE *f" "int *lineno" "size_t *lenp"
15.Sh DESCRIPTION
16The
17.Fn openpam_readword
18function reads the next word from a file, and
19returns it in a NUL-terminated buffer allocated with
20.Xr malloc 3 .
21.Pp
22A word is a sequence of non-whitespace characters.
23However, whitespace characters can be included in a word if quoted or
24escaped according to the following rules:
25.Bl -bullet
26.It
27An unescaped single or double quote introduces a quoted string,
28which ends when the same quote character is encountered a second
29time.
30The quotes themselves are stripped.
31.It
32Within a single- or double-quoted string, all whitespace characters,
33including the newline character, are preserved as-is.
34.It
35Outside a quoted string, a backslash escapes the next character,
36which is preserved as-is, unless that character is a newline, in
37which case it is discarded and reading continues at the beginning of
38the next line as if the backslash and newline had not been there.
39In all cases, the backslash itself is discarded.
40.It
41Within a single-quoted string, double quotes and backslashes are
42preserved as-is.
43.It
44Within a double-quoted string, a single quote is preserved as-is,
45and a backslash is preserved as-is unless used to escape a double
46quote.
47.El
48.Pp
49In addition, if the first non-whitespace character on the line is a
50hash character (#), the rest of the line is discarded.
51If a hash character occurs within a word, however, it is preserved
52as-is.
53A backslash at the end of a comment does cause line continuation.
54.Pp
55If
56.Fa lineno
57is not
58.Dv NULL ,
59the integer variable it points to is
60incremented every time a quoted or escaped newline character is read.
61.Pp
62If
63.Fa lenp
64is not
65.Dv NULL ,
66the length of the word (after quotes and
67backslashes have been removed) is stored in the variable it points to.
68.Sh RETURN VALUES
69If successful, the
70.Fn openpam_readword
71function returns a pointer to a
72dynamically allocated NUL-terminated string containing the first word
73encountered on the line.
74.Pp
75The caller is responsible for releasing the returned buffer by passing
76it to
77.Xr free 3 .
78.Pp
79If
80.Fn openpam_readword
81reaches the end of the line or file before any
82characters are copied to the word, it returns
83.Dv NULL .
84In the former
85case, the newline is pushed back to the file.
86.Pp
87If
88.Fn openpam_readword
89reaches the end of the file while a quote or
90backslash escape is in effect, it sets
91.Va errno
92to
93.Dv EINVAL
94and returns
95.Dv NULL .
96.Sh IMPLEMENTATION NOTES
97The parsing rules are intended to be equivalent to the normal POSIX
98shell quoting rules.
99Any discrepancy is a bug and should be reported to the author along
100with sample input that can be used to reproduce the error.
101.Pp
102.Sh SEE ALSO
103.Xr openpam_readline 3 ,
104.Xr openpam_readlinev 3 ,
105.Xr pam 3
106.Sh STANDARDS
107The
108.Fn openpam_readword
109function is an OpenPAM extension.
110.Sh AUTHORS
111The
112.Fn openpam_readword
113function and this manual page were
114developed by
115.An Dag-Erling Sm\(/orgrav Aq Mt des@des.no .
116