source: openpam/trunk/lib/openpam_straddch.c @ 533

Last change on this file since 533 was 533, checked in by des, 3 years ago

Clean up and document

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/*-
2 * Copyright (c) 2012 Dag-Erling Smørgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $Id$
28 */
29
30#ifdef HAVE_CONFIG_H
31# include "config.h"
32#endif
33
34#include <stdlib.h>
35
36#include <security/pam_appl.h>
37
38#include "openpam_impl.h"
39
40#define MIN_STR_SIZE    32
41
42/*
43 * OpenPAM extension
44 *
45 * Add a character to a string, expanding the buffer if needed.
46 */
47
48int
49openpam_straddch(char **str, size_t *size, size_t *len, char ch)
50{
51        size_t tmpsize;
52        char *tmpstr;
53
54        if (*str == NULL) {
55                /* initial allocation */
56                tmpsize = MIN_STR_SIZE
57                if ((tmpstr = malloc(tmpsize)) == NULL) {
58                        openpam_log(PAM_LOG_ERROR, "malloc(): %m");
59                        return (-1);
60                }
61                *str = tmpstr;
62                *size = tmpsize;
63                *len = 0;
64        } else if (*len >= *size - 1) {
65                /* additional space required */
66                tmpsize = *size * 2;
67                if ((tmpstr = realloc(*str, tmpsize)) == NULL) {
68                        openpam_log(PAM_LOG_ERROR, "realloc(): %m");
69                        return (-1);
70                }
71                size = tmpsize;
72                *str = tmpstr;
73        }
74        (*str)[*len] = ch;
75        ++*len;
76        (*str)[*len] = '\0';
77        return (0);
78}
79
80/**
81 * The =openpam_straddch function appends a character to a dynamically
82 * allocated NUL-terminated buffer, reallocating the buffer as needed.
83 *
84 * The =str argument points to a variable containing either a pointer to
85 * an existing buffer or =NULL.
86 * If the value of the variable pointed to by =str is =NULL, a new buffer
87 * is allocated.
88 *
89 * The =size and =len argument point to variables used to hold the size
90 * of the buffer and the length of the string it contains, respectively.
91 *
92 * If a new buffer is allocated or an existing buffer is reallocated to
93 * make room for the additional character, =str and =size are updated
94 * accordingly.
95 *
96 * The =openpam_straddch function ensures that the buffer is always
97 * NUL-terminated.
98 *
99 * If the =openpam_straddch function is successful, it increments the
100 * integer variable pointed to by =len and returns 0.
101 * Otherwise, it leaves the variables pointed to by =str, =size and =len
102 * unmodified and returns -1.
103 */
Note: See TracBrowser for help on using the repository browser.