1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #define _GNU_SOURCE
30
31 #include <endian.h>
32
33 #if defined(__APPLE__)
34
35 #include <endian-darwin.h>
36 #define HAVE_DECL_STRNDUPA
37 #define strndupa(_s,_l) strdup(_s)
38 char* basename(const char*);
39 #define init_module darwin_init_module
40 #define delete_module darwin_delete_module
41 #define program_invocation_short_name "depmod"
42
43 #endif
44
45 #if defined(__ANDROID__) || defined(__APPLE__)
46 #include <stdlib.h>
47 #include <unistd.h>
get_current_dir_name(void)48 static inline char *get_current_dir_name(void) {
49 return getcwd(malloc(PATH_MAX), PATH_MAX);
50 }
51 #endif
52
53 #if defined(ANDROID_HOST_MUSL)
54
55 // musl string.h doesn't have basename. libgen.h's basename is
56 // different from GNU basename. Define our own basename to avoid confusion.
57
58 extern char* strrchr(const char*, int);
basename(const char * path)59 static inline char* basename(const char* path) {
60 const char* last_slash = strrchr(path, '/');
61 return (char*)((last_slash != 0) ? last_slash + 1 : path);
62 }
63
64 #endif // defined(ANDROID_HOST_MUSL)
65