2 libpsh/util.c - libpsh utilities for everyone
3 Copyright 2018-2020 Zhang Maiyun
5 This file is part of Psh, P shell.
7 Psh is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Psh is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
27 #ifdef HAVE_READLINE_READLINE_H
28 #include <readline/readline.h>
29 #elif defined(HAVE_READLINE)
30 /* Have libreadline but no header */
31 char *readline(char *);
34 #include "libpsh/util.h"
35 #include "libpsh/xmalloc.h"
37 /* Get a line from file FP with prompt PROMPT.
38 * The memory is allocated automatically.
39 * Returns the content when an EOF or \n is read,
40 * not including the trailing EOF or \n. */
41 char *psh_fgets(const char *prompt
, FILE *fp
)
45 return readline(prompt
);
55 size_t charcount
= 0, nowhave
= 256;
57 char *result
= xmalloc(P_CS
* nowhave
);
66 if (ptr
== result
) /* nothing read */
76 if ((++charcount
) == nowhave
)
77 result
= xrealloc(result
, P_CS
* (nowhave
<<= 1));
79 *ptr
= 0; /* Replace EOF or \n with NUL */
80 result
= xrealloc(result
, P_CS
* (strlen(result
) +
81 1)); /* Resize the array to minimum */
86 char *psh_gets(const char *prompt
) { return psh_fgets(prompt
, stdin
); }
88 /* Copy string of length SIZE from SRC to DST */
89 size_t psh_strncpy(char *dst
, const char *src
, size_t size
)
91 strncpy(dst
, src
, size
);
96 /* Compare the string str1 to a string as were produced by strcat(str2_1,
98 int strdblcmp(const char *str1
, const char *str2_1
, const char *str2_2
)
101 /* End loop if str2_1 is empty */
104 printf("%d %d\n", *str1
, *str2_1
);
105 diff
= *(str1
++) - *(str2_1
++);
111 printf("%d %d\n", *str1
, *str2_2
);
112 diff
= *str1
- *str2_2
;
115 } while (*(str2_2
++) && *(str1
++));
119 char *psh_strdup(const char *str
)
121 size_t length
= strlen(str
) + 1;
122 char *dest
= xmalloc(P_CS
* length
);
123 memmove(dest
, str
, length
);
127 /* Get a string from FUNC, where the first argument is a string pointer,
128 * while the second one is the length of the buffer.
129 * Original function's return value is copied into *RESULT.
130 * If that doesn't matter, use NULL, and RESULT will be left untouched.
131 * returned pointer needs to be free()d
133 char *psh_getstring(void *(*func
)(char *, size_t), void **result
)
136 char *oldtry
= NULL
, *newtry
= NULL
;
139 newtry
= xmalloc(P_CS
* len
);
141 *result
= (*func
)(newtry
, len
);
143 (*func
)(newtry
, len
);
144 if (oldtry
&& strcmp(oldtry
, newtry
) == 0) /* Identical */
151 newtry
= xrealloc(newtry
, strlen(newtry
) + 1);