[BACK]Return to stringlist.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ftp

Annotation of src/usr.bin/ftp/stringlist.c, Revision 1.14

1.13      kmos        1: /*     $OpenBSD: stringlist.c,v 1.12 2015/05/20 23:39:55 schwarze Exp $        */
1.1       millert     2: /*     $NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1994 Christos Zoulas
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     18:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     19:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     21:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
1.10      martynas   30: #ifndef SMALL
                     31:
1.1       millert    32: #include <stdio.h>
                     33: #include <string.h>
                     34: #include <err.h>
                     35: #include <stdlib.h>
                     36:
                     37: #include "stringlist.h"
                     38:
                     39: #define _SL_CHUNKSIZE  20
                     40:
                     41: /*
                     42:  * sl_init(): Initialize a string list
                     43:  */
                     44: StringList *
1.5       deraadt    45: sl_init(void)
1.1       millert    46: {
                     47:        StringList *sl = malloc(sizeof(StringList));
                     48:        if (sl == NULL)
1.3       deraadt    49:                err(1, "stringlist");
1.1       millert    50:
                     51:        sl->sl_cur = 0;
                     52:        sl->sl_max = _SL_CHUNKSIZE;
1.8       deraadt    53:        sl->sl_str = calloc(sl->sl_max, sizeof(char *));
1.1       millert    54:        if (sl->sl_str == NULL)
1.3       deraadt    55:                err(1, "stringlist");
1.1       millert    56:        return sl;
                     57: }
                     58:
                     59:
                     60: /*
                     61:  * sl_add(): Add an item to the string list
                     62:  */
                     63: void
1.5       deraadt    64: sl_add(StringList *sl, char *name)
1.1       millert    65: {
                     66:        if (sl->sl_cur == sl->sl_max - 1) {
                     67:                sl->sl_max += _SL_CHUNKSIZE;
1.11      doug       68:                sl->sl_str = reallocarray(sl->sl_str, sl->sl_max,
                     69:                    sizeof(char *));
1.1       millert    70:                if (sl->sl_str == NULL)
1.3       deraadt    71:                        err(1, "stringlist");
1.1       millert    72:        }
                     73:        sl->sl_str[sl->sl_cur++] = name;
                     74: }
                     75:
                     76:
                     77: /*
                     78:  * sl_free(): Free a stringlist
                     79:  */
                     80: void
1.5       deraadt    81: sl_free(StringList *sl, int all)
1.1       millert    82: {
                     83:        size_t i;
                     84:
                     85:        if (sl == NULL)
                     86:                return;
                     87:        if (sl->sl_str) {
                     88:                if (all)
                     89:                        for (i = 0; i < sl->sl_cur; i++)
                     90:                                free(sl->sl_str[i]);
                     91:                free(sl->sl_str);
                     92:        }
                     93:        free(sl);
                     94: }
1.10      martynas   95:
                     96: #endif /* !SMALL */
                     97: