1*0d9d0fd8Schristos.\" $NetBSD: openpam_readword.3,v 1.8 2023/06/30 21:46:20 christos Exp $ 223e44a77Schristos.\" 38fa0fefeSchristos.\" Generated from openpam_readword.c by gendoc.pl 4*0d9d0fd8Schristos.Dd June 27, 2023 523e44a77Schristos.Dt OPENPAM_READWORD 3 623e44a77Schristos.Os 723e44a77Schristos.Sh NAME 823e44a77Schristos.Nm openpam_readword 923e44a77Schristos.Nd read a word from a file, respecting shell quoting rules 1023e44a77Schristos.Sh SYNOPSIS 1123e44a77Schristos.In sys/types.h 1223e44a77Schristos.In stdio.h 1323e44a77Schristos.In security/pam_appl.h 1423e44a77Schristos.In security/openpam.h 1523e44a77Schristos.Ft "char *" 1623e44a77Schristos.Fn openpam_readword "FILE *f" "int *lineno" "size_t *lenp" 1723e44a77Schristos.Sh DESCRIPTION 1823e44a77SchristosThe 1923e44a77Schristos.Fn openpam_readword 2023e44a77Schristosfunction reads the next word from a file, and 2123e44a77Schristosreturns it in a NUL-terminated buffer allocated with 2223e44a77Schristos.Xr malloc 3 . 2323e44a77Schristos.Pp 2423e44a77SchristosA word is a sequence of non-whitespace characters. 2523e44a77SchristosHowever, whitespace characters can be included in a word if quoted or 2623e44a77Schristosescaped according to the following rules: 2723e44a77Schristos.Bl -bullet 2823e44a77Schristos.It 2923e44a77SchristosAn unescaped single or double quote introduces a quoted string, 3023e44a77Schristoswhich ends when the same quote character is encountered a second 3123e44a77Schristostime. 3223e44a77SchristosThe quotes themselves are stripped. 3323e44a77Schristos.It 3423e44a77SchristosWithin a single- or double-quoted string, all whitespace characters, 3523e44a77Schristosincluding the newline character, are preserved as-is. 3623e44a77Schristos.It 3723e44a77SchristosOutside a quoted string, a backslash escapes the next character, 3823e44a77Schristoswhich is preserved as-is, unless that character is a newline, in 3923e44a77Schristoswhich case it is discarded and reading continues at the beginning of 4023e44a77Schristosthe next line as if the backslash and newline had not been there. 4123e44a77SchristosIn all cases, the backslash itself is discarded. 4223e44a77Schristos.It 4323e44a77SchristosWithin a single-quoted string, double quotes and backslashes are 4423e44a77Schristospreserved as-is. 4523e44a77Schristos.It 4623e44a77SchristosWithin a double-quoted string, a single quote is preserved as-is, 4723e44a77Schristosand a backslash is preserved as-is unless used to escape a double 4823e44a77Schristosquote. 4923e44a77Schristos.El 5023e44a77Schristos.Pp 5123e44a77SchristosIn addition, if the first non-whitespace character on the line is a 5223e44a77Schristoshash character (#), the rest of the line is discarded. 5323e44a77SchristosIf a hash character occurs within a word, however, it is preserved 5423e44a77Schristosas-is. 5523e44a77SchristosA backslash at the end of a comment does cause line continuation. 5623e44a77Schristos.Pp 5723e44a77SchristosIf 5823e44a77Schristos.Fa lineno 5923e44a77Schristosis not 6023e44a77Schristos.Dv NULL , 6123e44a77Schristosthe integer variable it points to is 6223e44a77Schristosincremented every time a quoted or escaped newline character is read. 6323e44a77Schristos.Pp 6423e44a77SchristosIf 6523e44a77Schristos.Fa lenp 6623e44a77Schristosis not 6723e44a77Schristos.Dv NULL , 6823e44a77Schristosthe length of the word (after quotes and 6923e44a77Schristosbackslashes have been removed) is stored in the variable it points to. 7023e44a77Schristos.Sh RETURN VALUES 7123e44a77SchristosIf successful, the 7223e44a77Schristos.Fn openpam_readword 7323e44a77Schristosfunction returns a pointer to a 7423e44a77Schristosdynamically allocated NUL-terminated string containing the first word 7523e44a77Schristosencountered on the line. 7623e44a77Schristos.Pp 7723e44a77SchristosThe caller is responsible for releasing the returned buffer by passing 7823e44a77Schristosit to 7923e44a77Schristos.Xr free 3 . 8023e44a77Schristos.Pp 8123e44a77SchristosIf 8223e44a77Schristos.Fn openpam_readword 8323e44a77Schristosreaches the end of the line or file before any 8423e44a77Schristoscharacters are copied to the word, it returns 8523e44a77Schristos.Dv NULL . 8623e44a77SchristosIn the former 8723e44a77Schristoscase, the newline is pushed back to the file. 8823e44a77Schristos.Pp 8923e44a77SchristosIf 9023e44a77Schristos.Fn openpam_readword 9123e44a77Schristosreaches the end of the file while a quote or 9223e44a77Schristosbackslash escape is in effect, it sets 9323e44a77Schristos.Va errno 9423e44a77Schristosto 9523e44a77Schristos.Dv EINVAL 9623e44a77Schristosand returns 9723e44a77Schristos.Dv NULL . 9823e44a77Schristos.Sh IMPLEMENTATION NOTES 9923e44a77SchristosThe parsing rules are intended to be equivalent to the normal POSIX 10023e44a77Schristosshell quoting rules. 10123e44a77SchristosAny discrepancy is a bug and should be reported to the author along 10223e44a77Schristoswith sample input that can be used to reproduce the error. 10323e44a77Schristos.Pp 10423e44a77Schristos.Sh SEE ALSO 10523e44a77Schristos.Xr openpam_readline 3 , 10623e44a77Schristos.Xr openpam_readlinev 3 , 10723e44a77Schristos.Xr pam 3 10823e44a77Schristos.Sh STANDARDS 10923e44a77SchristosThe 11023e44a77Schristos.Fn openpam_readword 11123e44a77Schristosfunction is an OpenPAM extension. 11223e44a77Schristos.Sh AUTHORS 11323e44a77SchristosThe 11423e44a77Schristos.Fn openpam_readword 11523e44a77Schristosfunction and this manual page were 11623e44a77Schristosdeveloped by 117a5684d07Swiz.An Dag-Erling Sm\(/orgrav Aq Mt des@des.no . 118