Path: blob/master/src/java.base/share/native/libjli/jli_util.c
67707 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <string.h>27#include <stdarg.h>28#include "jni.h"29#include "jli_util.h"3031/*32* Returns a pointer to a block of at least 'size' bytes of memory.33* Prints error message and exits if the memory could not be allocated.34*/35JNIEXPORT void * JNICALL36JLI_MemAlloc(size_t size)37{38void *p = malloc(size);39if (p == 0) {40perror("malloc");41exit(1);42}43return p;44}4546/*47* Equivalent to realloc(size).48* Prints error message and exits if the memory could not be reallocated.49*/50void *51JLI_MemRealloc(void *ptr, size_t size)52{53void *p = realloc(ptr, size);54if (p == 0) {55perror("realloc");56exit(1);57}58return p;59}6061/*62* Wrapper over strdup(3C) which prints an error message and exits if memory63* could not be allocated.64*/65JNIEXPORT char * JNICALL66JLI_StringDup(const char *s1)67{68char *s = strdup(s1);69if (s == NULL) {70perror("strdup");71exit(1);72}73return s;74}7576/*77* Very equivalent to free(ptr).78* Here to maintain pairing with the above routines.79*/80JNIEXPORT void JNICALL81JLI_MemFree(void *ptr)82{83free(ptr);84}8586jboolean87JLI_HasSuffix(const char *s1, const char *s2)88{89char *p = JLI_StrRChr(s1, '.');90if (p == NULL || *p == '\0') {91return JNI_FALSE;92}93return (JLI_StrCaseCmp(p, s2) == 0);94}9596/*97* debug helpers we use98*/99static jboolean _launcher_debug = JNI_FALSE;100101void102JLI_TraceLauncher(const char* fmt, ...)103{104va_list vl;105if (_launcher_debug != JNI_TRUE) return;106va_start(vl, fmt);107vprintf(fmt,vl);108va_end(vl);109fflush(stdout);110}111112JNIEXPORT void JNICALL113JLI_SetTraceLauncher()114{115if (getenv(JLDEBUG_ENV_ENTRY) != 0) {116_launcher_debug = JNI_TRUE;117JLI_TraceLauncher("----%s----\n", JLDEBUG_ENV_ENTRY);118}119}120121jboolean122JLI_IsTraceLauncher()123{124return _launcher_debug;125}126127int128JLI_StrCCmp(const char *s1, const char* s2)129{130return JLI_StrNCmp(s1, s2, JLI_StrLen(s2));131}132133JNIEXPORT JLI_List JNICALL134JLI_List_new(size_t capacity)135{136JLI_List l = (JLI_List) JLI_MemAlloc(sizeof(struct JLI_List_));137l->capacity = capacity;138l->elements = (char **) JLI_MemAlloc(capacity * sizeof(l->elements[0]));139l->size = 0;140return l;141}142143void144JLI_List_free(JLI_List sl)145{146if (sl) {147if (sl->elements) {148size_t i;149for (i = 0; i < sl->size; i++)150JLI_MemFree(sl->elements[i]);151JLI_MemFree(sl->elements);152}153JLI_MemFree(sl);154}155}156157void158JLI_List_ensureCapacity(JLI_List sl, size_t capacity)159{160if (sl->capacity < capacity) {161while (sl->capacity < capacity)162sl->capacity *= 2;163sl->elements = JLI_MemRealloc(sl->elements,164sl->capacity * sizeof(sl->elements[0]));165}166}167168JNIEXPORT void JNICALL169JLI_List_add(JLI_List sl, char *str)170{171JLI_List_ensureCapacity(sl, sl->size+1);172sl->elements[sl->size++] = str;173}174175void176JLI_List_addSubstring(JLI_List sl, const char *beg, size_t len)177{178char *str = (char *) JLI_MemAlloc(len+1);179memcpy(str, beg, len);180str[len] = '\0';181JLI_List_ensureCapacity(sl, sl->size+1);182sl->elements[sl->size++] = str;183}184185char *186JLI_List_combine(JLI_List sl)187{188size_t i;189size_t size;190char *str;191char *p;192for (i = 0, size = 1; i < sl->size; i++)193size += JLI_StrLen(sl->elements[i]);194195str = JLI_MemAlloc(size);196197for (i = 0, p = str; i < sl->size; i++) {198size_t len = JLI_StrLen(sl->elements[i]);199memcpy(p, sl->elements[i], len);200p += len;201}202*p = '\0';203204return str;205}206207char *208JLI_List_join(JLI_List sl, char sep)209{210size_t i;211size_t size;212char *str;213char *p;214for (i = 0, size = 1; i < sl->size; i++)215size += JLI_StrLen(sl->elements[i]) + 1;216217str = JLI_MemAlloc(size);218219for (i = 0, p = str; i < sl->size; i++) {220size_t len = JLI_StrLen(sl->elements[i]);221if (i > 0) *p++ = sep;222memcpy(p, sl->elements[i], len);223p += len;224}225*p = '\0';226227return str;228}229230JLI_List231JLI_List_split(const char *str, char sep)232{233const char *p, *q;234size_t len = JLI_StrLen(str);235int count;236JLI_List sl;237for (count = 1, p = str; p < str + len; p++)238count += (*p == sep);239sl = JLI_List_new(count);240for (p = str;;) {241for (q = p; q <= str + len; q++) {242if (*q == sep || *q == '\0') {243JLI_List_addSubstring(sl, p, q - p);244if (*q == '\0')245return sl;246p = q + 1;247}248}249}250}251252253