Changeset 533 in openpam


Ignore:
Timestamp:
Mar 31, 2012, 2:25:43 PM (3 years ago)
Author:
des
Message:

Clean up and document

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/openpam_straddch.c

    r530 r533  
    3535 
    3636#include <security/pam_appl.h> 
     37 
    3738#include "openpam_impl.h" 
     39 
     40#define MIN_STR_SIZE    32 
    3841 
    3942/* 
     
    4649openpam_straddch(char **str, size_t *size, size_t *len, char ch) 
    4750{ 
    48         char *tmp; 
     51        size_t tmpsize; 
     52        char *tmpstr; 
    4953 
    5054        if (*str == NULL) { 
    5155                /* initial allocation */ 
    52                 if ((*str = malloc(*size = 32)) == NULL) { 
     56                tmpsize = MIN_STR_SIZE 
     57                if ((tmpstr = malloc(tmpsize)) == NULL) { 
    5358                        openpam_log(PAM_LOG_ERROR, "malloc(): %m"); 
    5459                        return (-1); 
    5560                } 
     61                *str = tmpstr; 
     62                *size = tmpsize; 
    5663                *len = 0; 
    5764        } else if (*len >= *size - 1) { 
    5865                /* additional space required */ 
    59                 if ((tmp = realloc(*str, *size *= 2)) == NULL) { 
     66                tmpsize = *size * 2; 
     67                if ((tmpstr = realloc(*str, tmpsize)) == NULL) { 
    6068                        openpam_log(PAM_LOG_ERROR, "realloc(): %m"); 
    61                         free(*str); 
    62                         *str = NULL; 
    6369                        return (-1); 
    6470                } 
    65                 *str = tmp; 
     71                size = tmpsize; 
     72                *str = tmpstr; 
    6673        } 
    6774        (*str)[*len] = ch; 
     
    7178} 
    7279 
    73 /* 
    74  * NODOC 
     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. 
    75103 */ 
Note: See TracChangeset for help on using the changeset viewer.