]>
Commit | Line | Data |
---|---|---|
fd67aa11 | 1 | /* Copyright (C) 2021-2024 Free Software Foundation, Inc. |
bb368aad VM |
2 | Contributed by Oracle. |
3 | ||
4 | This file is part of GNU Binutils. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3, or (at your option) | |
9 | any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, 51 Franklin Street - Fifth Floor, Boston, | |
19 | MA 02110-1301, USA. */ | |
20 | ||
21 | #ifndef _PERFAN_UTIL_H | |
22 | #define _PERFAN_UTIL_H | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/stat.h> | |
28 | #include <stdint.h> | |
29 | ||
30 | #include "gp-defs.h" | |
31 | #include "gp-time.h" | |
32 | #include "i18n.h" | |
33 | #include "debug.h" | |
34 | ||
35 | #define SWAP_ENDIAN(x) swapByteOrder((void *) (&(x)), sizeof(x)) | |
36 | #define AppendString(len, arr, ...) len += snprintf(arr + len, sizeof(arr) - len, __VA_ARGS__) | |
37 | #define ARR_SIZE(x) (sizeof (x) / sizeof (*(x))) | |
38 | ||
39 | // Utility routines. | |
40 | ||
41 | // | |
42 | // Inline functions | |
43 | // | |
44 | // max(a, b) - Return the maximum of two values | |
45 | inline int | |
46 | max (int a, int b) | |
47 | { | |
48 | return (a >= b) ? a : b; | |
49 | } | |
50 | ||
51 | // min(a, b) - Return the minimum of two values | |
52 | inline int | |
53 | min (int a, int b) | |
54 | { | |
55 | return (a <= b) ? a : b; | |
56 | } | |
57 | ||
58 | // streq(s1, s2) - Returns 1 if strings are the same, 0 otherwise | |
59 | inline int | |
60 | streq (const char *s1, const char *s2) | |
61 | { | |
62 | return strcmp (s1, s2) == 0; | |
63 | } | |
64 | ||
65 | // StrChr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or | |
66 | // a pointer to the next symbol after the first occurrence of 'ch' in 'str' | |
67 | inline char * | |
68 | StrChr (char *str, char ch) | |
69 | { | |
70 | char *s = strchr (str, ch); | |
71 | return s ? (s + 1) : str; | |
72 | } | |
73 | ||
74 | // StrRchr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or | |
75 | // a pointer to the next symbol after the last occurrence of 'ch' in 'str' | |
76 | inline char * | |
77 | StrRchr (char *str, char ch) | |
78 | { | |
79 | char *s = strrchr (str, ch); | |
80 | return s ? (s + 1) : str; | |
81 | } | |
82 | ||
83 | inline char* | |
84 | STR (const char *s) | |
85 | { | |
86 | return s ? (char*) s : (char*) NTXT ("NULL"); | |
87 | } | |
88 | ||
89 | inline char* | |
90 | get_str (const char *s, const char *s1) | |
91 | { | |
92 | return s ? (char*) s : (char*) s1; | |
93 | } | |
94 | ||
95 | inline char * | |
96 | get_basename (const char* name) | |
97 | { | |
98 | return StrRchr ((char*) name, '/'); | |
99 | } | |
100 | ||
101 | inline char * | |
102 | dbe_strdup (const char *str) | |
103 | { | |
104 | return str ? strdup (str) : NULL; | |
105 | } | |
106 | ||
107 | inline long | |
108 | dbe_sstrlen (const char *str) | |
109 | { | |
110 | return str ? (long) strlen (str) : 0; | |
111 | } | |
112 | ||
113 | inline int | |
114 | dbe_strcmp (const char *s1, const char *s2) | |
115 | { | |
116 | return s1 ? (s2 ? strcmp (s1, s2) : 1) : (s2 ? -1 : 0); | |
117 | } | |
118 | ||
119 | // tstodouble(t) - Return timestruc_t in (double) seconds | |
120 | inline double | |
121 | tstodouble (timestruc_t t) | |
122 | { | |
123 | return (double) t.tv_sec + (double) (t.tv_nsec / 1000000000.0); | |
124 | } | |
125 | ||
126 | inline void | |
127 | hr2timestruc (timestruc_t *d, hrtime_t s) | |
128 | { | |
129 | d->tv_sec = (long) (s / NANOSEC); | |
130 | d->tv_nsec = (long) (s % NANOSEC); | |
131 | } | |
132 | ||
133 | inline hrtime_t | |
134 | timestruc2hr (timestruc_t *s) | |
135 | { | |
136 | return (hrtime_t) s->tv_sec * NANOSEC + (hrtime_t) s->tv_nsec; | |
137 | } | |
138 | ||
576d2c97 VM |
139 | #if defined(__MUSL_LIBC) |
140 | typedef struct stat dbe_stat_t; | |
141 | #define fstat64 fstat | |
142 | #define open64 open | |
143 | #else | |
144 | typedef struct stat64 dbe_stat_t; | |
145 | #endif | |
bb368aad VM |
146 | |
147 | #if defined(__cplusplus) | |
148 | extern "C" | |
149 | { | |
150 | #endif | |
151 | // | |
152 | // Declaration of utility functions | |
153 | // | |
154 | void tsadd (timestruc_t *result, timestruc_t *time); | |
155 | void tssub (timestruc_t *result, timestruc_t *time1, timestruc_t *time2); | |
156 | int tscmp (timestruc_t *time1, timestruc_t *time2); | |
157 | void int_max (int *maximum, int count); | |
158 | char *strstr_r (char *s1, const char *s2); | |
159 | char *strrpbrk (const char *string, const char *brkset); | |
160 | char *read_line (FILE *); | |
161 | char *parse_qstring (char *in_str, char **endptr); | |
162 | char *parse_fname (char *in_str, char **fcontext); | |
163 | int get_paren (const char *name); | |
164 | ||
165 | uint64_t crc64 (const char *str, size_t len); | |
166 | char *canonical_path (char *path); | |
167 | char *get_relative_path (char *name); | |
168 | char *get_relative_link (const char *path_to, const char *path_from); | |
169 | char *get_prog_name (int basename); | |
170 | char *dbe_strndup (const char *str, size_t len); | |
576d2c97 VM |
171 | int dbe_stat (const char *path, dbe_stat_t *sbuf); |
172 | int dbe_stat_file (const char *path, dbe_stat_t *sbuf); | |
bb368aad VM |
173 | char *dbe_read_dir (const char *path, const char *format); |
174 | char *dbe_get_processes (const char *format); | |
175 | char *dbe_create_directories (const char *pathname); | |
176 | char *dbe_delete_file (const char *pathname); | |
177 | char *dbe_xml2str (const char *s); | |
178 | void swapByteOrder (void *p, size_t sz); | |
179 | char *dbe_sprintf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); | |
180 | ssize_t dbe_write (int f, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); | |
181 | char *dbe_create_symlink_to_path (const char *path, const char *dir); | |
182 | int64_t read_from_file (int fd, void *buffer, int64_t nbyte); | |
183 | uint32_t get_cksum (const char * pathname, char ** errmsg); | |
184 | ||
185 | #ifdef __cplusplus | |
186 | } | |
187 | int catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[]); | |
188 | #endif | |
189 | ||
190 | ||
191 | #endif /* _UTIL_H */ |