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

Annotation of src/usr.bin/window/string.c, Revision 1.3

1.3     ! downsj      1: /*     $OpenBSD$       */
1.1       deraadt     2: /*     $NetBSD: string.c,v 1.5 1995/09/29 00:44:06 cgd Exp $   */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Edward Wang at The University of California, Berkeley.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)string.c   8.1 (Berkeley) 6/6/93";
                     43: #else
1.3     ! downsj     44: static char rcsid[] = "$OpenBSD$";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: #include "string.h"
                     49: #include <stdlib.h>
                     50: #include <string.h>
                     51:
                     52: char *
                     53: str_cpy(s)
                     54: register char *s;
                     55: {
                     56:        char *str;
                     57:        register char *p;
                     58:
                     59:        str = p = str_alloc(strlen(s) + 1);
                     60:        if (p == 0)
                     61:                return 0;
                     62:        while (*p++ = *s++)
                     63:                ;
                     64:        return str;
                     65: }
                     66:
                     67: char *
                     68: str_ncpy(s, n)
                     69: register char *s;
                     70: register n;
                     71: {
                     72:        int l = strlen(s);
                     73:        char *str;
                     74:        register char *p;
                     75:
                     76:        if (n > l)
                     77:                n = l;
                     78:        str = p = str_alloc(n + 1);
                     79:        if (p == 0)
                     80:                return 0;
                     81:        while (--n >= 0)
                     82:                *p++ = *s++;
                     83:        *p = 0;
                     84:        return str;
                     85: }
                     86:
                     87: char *
                     88: str_itoa(i)
                     89: int i;
                     90: {
                     91:        char buf[30];
                     92:
                     93:        (void) sprintf(buf, "%d", i);
                     94:        return str_cpy(buf);
                     95: }
                     96:
                     97: char *
                     98: str_cat(s1, s2)
                     99: char *s1, *s2;
                    100: {
                    101:        char *str;
                    102:        register char *p, *q;
                    103:
                    104:        str = p = str_alloc(strlen(s1) + strlen(s2) + 1);
                    105:        if (p == 0)
                    106:                return 0;
                    107:        for (q = s1; *p++ = *q++;)
                    108:                ;
                    109:        for (q = s2, p--; *p++ = *q++;)
                    110:                ;
                    111:        return str;
                    112: }
                    113:
                    114: /*
                    115:  * match s against p.
                    116:  * s can be a prefix of p with at least min characters.
                    117:  */
                    118: str_match(s, p, min)
                    119: register char *s, *p;
                    120: register min;
                    121: {
                    122:        for (; *s && *p && *s == *p; s++, p++, min--)
                    123:                ;
                    124:        return *s == *p || *s == 0 && min <= 0;
                    125: }
                    126:
                    127: #ifdef STR_DEBUG
                    128: char *
                    129: str_alloc(l)
                    130: int l;
                    131: {
                    132:        register struct string *s;
                    133:
                    134:        s = (struct string *) malloc((unsigned)l + str_offset);
                    135:        if (s == 0)
                    136:                return 0;
                    137:        if (str_head.s_forw == 0)
                    138:                str_head.s_forw = str_head.s_back = &str_head;
                    139:        s->s_forw = str_head.s_forw;
                    140:        s->s_back = &str_head;
                    141:        str_head.s_forw = s;
                    142:        s->s_forw->s_back = s;
                    143:        return s->s_data;
                    144: }
                    145:
                    146: str_free(str)
                    147: char *str;
                    148: {
                    149:        register struct string *s;
                    150:
                    151:        for (s = str_head.s_forw; s != &str_head && s->s_data != str;
                    152:             s = s->s_forw)
                    153:                ;
                    154:        if (s == &str_head)
                    155:                abort();
                    156:        s->s_back->s_forw = s->s_forw;
                    157:        s->s_forw->s_back = s->s_back;
                    158:        free((char *)s);
                    159: }
                    160: #endif