openenvutils/commandshell/shell/src/string.c
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2  * string.c - string manipulation
       
     3  *
       
     4  * This file is part of zsh, the Z shell.
       
     5  *
       
     6  * Copyright (c) 2000 Peter Stephenson
       
     7  * All rights reserved.
       
     8  *
       
     9  * Permission is hereby granted, without written agreement and without
       
    10  * license or royalty fees, to use, copy, modify, and distribute this
       
    11  * software and to distribute modified versions of this software for any
       
    12  * purpose, provided that the above copyright notice and the following
       
    13  * two paragraphs appear in all copies of this software.
       
    14  *
       
    15  * In no event shall Peter Stephenson or the Zsh Development Group be liable
       
    16  * to any party for direct, indirect, special, incidental, or consequential
       
    17  * damages arising out of the use of this software and its documentation,
       
    18  * even if Peter Stephenson and the Zsh Development Group have been advised of
       
    19  * the possibility of such damage.
       
    20  *
       
    21  * Peter Stephenson and the Zsh Development Group specifically disclaim any
       
    22  * warranties, including, but not limited to, the implied warranties of
       
    23  * merchantability and fitness for a particular purpose.  The software
       
    24  * provided hereunder is on an "as is" basis, and Peter Stephenson and the
       
    25  * Zsh Development Group have no obligation to provide maintenance,
       
    26  * support, updates, enhancements, or modifications.
       
    27  */
       
    28 
       
    29 #include "zsh.mdh"
       
    30 
       
    31 /**/
       
    32 mod_export char *
       
    33 dupstring(const char *s)
       
    34 {
       
    35     char *t;
       
    36 
       
    37     if (!s)
       
    38 	return NULL;
       
    39     
       
    40     #ifndef __SYMBIAN32__
       
    41     	t = (char *) zhalloc(strlen((char *)s) + 1);
       
    42     #else
       
    43     	t = (char *) zhalloc(strlen((char *)s) + 3);
       
    44     #endif
       
    45     strcpy(t, s);
       
    46     return t;
       
    47 }
       
    48 
       
    49 /**/
       
    50 mod_export char *
       
    51 ztrdup(const char *s)
       
    52 {
       
    53     char *t;
       
    54 
       
    55     if (!s)
       
    56 	return NULL;
       
    57     t = (char *)zalloc(strlen((char *)s) + 1);
       
    58     strcpy(t, s);
       
    59     return t;
       
    60 }
       
    61 
       
    62 /* concatenate s1, s2, and s3 in dynamically allocated buffer */
       
    63 
       
    64 /**/
       
    65 mod_export char *
       
    66 tricat(char const *s1, char const *s2, char const *s3)
       
    67 {
       
    68     /* This version always uses permanently-allocated space. */
       
    69     char *ptr;
       
    70     size_t l1 = strlen(s1);
       
    71     size_t l2 = strlen(s2);
       
    72 
       
    73     ptr = (char *)zalloc(l1 + l2 + strlen(s3) + 1);
       
    74     strcpy(ptr, s1);
       
    75     strcpy(ptr + l1, s2);
       
    76     strcpy(ptr + l1 + l2, s3);
       
    77     return ptr;
       
    78 }
       
    79 
       
    80 /**/
       
    81 mod_export char *
       
    82 zhtricat(char const *s1, char const *s2, char const *s3)
       
    83 {
       
    84     char *ptr;
       
    85     size_t l1 = strlen(s1);
       
    86     size_t l2 = strlen(s2);
       
    87 
       
    88     ptr = (char *)zhalloc(l1 + l2 + strlen(s3) + 1);
       
    89     strcpy(ptr, s1);
       
    90     strcpy(ptr + l1, s2);
       
    91     strcpy(ptr + l1 + l2, s3);
       
    92     return ptr;
       
    93 }
       
    94 
       
    95 /* concatenate s1 and s2 in dynamically allocated buffer */
       
    96 
       
    97 /**/
       
    98 mod_export char *
       
    99 dyncat(char *s1, char *s2)
       
   100 {
       
   101     /* This version always uses space from the current heap. */
       
   102     char *ptr;
       
   103     size_t l1 = strlen(s1);
       
   104 
       
   105     ptr = (char *)zhalloc(l1 + strlen(s2) + 1);
       
   106     strcpy(ptr, s1);
       
   107     strcpy(ptr + l1, s2);
       
   108     return ptr;
       
   109 }
       
   110 
       
   111 /**/
       
   112 mod_export char *
       
   113 bicat(const char *s1, const char *s2)
       
   114 {
       
   115     /* This version always uses permanently-allocated space. */
       
   116     char *ptr;
       
   117     size_t l1 = strlen(s1);
       
   118 
       
   119     ptr = (char *)zalloc(l1 + strlen(s2) + 1);
       
   120     strcpy(ptr, s1);
       
   121     strcpy(ptr + l1, s2);
       
   122     return ptr;
       
   123 }
       
   124 
       
   125 /* like strdup(), but with a specified length */
       
   126 
       
   127 /**/
       
   128 mod_export char *
       
   129 dupstrpfx(const char *s, int len)
       
   130 {
       
   131     char *r = zhalloc(len + 1);
       
   132 
       
   133     memcpy(r, s, len);
       
   134     r[len] = '\0';
       
   135     return r;
       
   136 }
       
   137 
       
   138 /**/
       
   139 mod_export char *
       
   140 ztrduppfx(const char *s, int len)
       
   141 {
       
   142     /* This version always uses permanently-allocated space. */
       
   143     char *r = zalloc(len + 1);
       
   144 
       
   145     memcpy(r, s, len);
       
   146     r[len] = '\0';
       
   147     return r;
       
   148 }
       
   149 
       
   150 /* Append a string to an allocated string, reallocating to make room. */
       
   151 
       
   152 /**/
       
   153 mod_export char *
       
   154 appstr(char *base, char const *append)
       
   155 {
       
   156     return strcat(realloc(base, strlen(base) + strlen(append) + 1), append);
       
   157 }
       
   158 
       
   159 /* Return a pointer to the last character of a string,
       
   160    unless the string is empty. */
       
   161 
       
   162 /**/
       
   163 mod_export char *
       
   164 strend(char *str)
       
   165 {
       
   166     if (*str == '\0')
       
   167 	return str;
       
   168     return str + strlen (str) - 1;
       
   169 }