Fix a few 'gcc -fanalyzer' warnings.
[pwmd.git] / src / util-misc.h
bloba0abb8602401daebcd3c8180d5410a9bdae2f425
1 /*
2 Copyright (C) 2006-2023 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef UTIL_MISC_H
19 #define UTIL_MISC_H
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <pthread.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <wchar.h>
29 #include <pwd.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #include "rcfile.h"
35 #ifdef HAVE_BACKTRACE
36 #include <execinfo.h>
37 #define BACKTRACE_COUNT 64
38 #define BACKTRACE(fn) do { \
39 char tmp[PATH_MAX]; \
40 int nptrs; \
41 void *buffer[BACKTRACE_COUNT]; \
42 nptrs = backtrace(buffer, BACKTRACE_COUNT); \
43 snprintf(tmp, sizeof(tmp), "%s/crash-%u.log", homedir, getpid());\
44 int fd = open(tmp,O_WRONLY|O_CREAT|O_TRUNC, 0600); \
45 if (fd != -1) { \
46 backtrace_symbols_fd(buffer, nptrs, fd); \
47 close(fd); \
48 } \
49 } while (0)
50 #endif
52 #define log_write0 log_write
54 #define log_write1(...) do { \
55 if (log_level >= 1) \
56 log_write(__VA_ARGS__); \
57 } while (0)
59 #define log_write2(...) do { \
60 if (log_level >= 2) \
61 log_write(__VA_ARGS__); \
62 } while (0)
64 typedef enum
66 OPTION_TYPE_NOARG,
67 OPTION_TYPE_ARG,
68 OPTION_TYPE_OPTARG,
69 } option_type_t;
71 struct argv_s
73 const char *opt;
74 option_type_t type;
75 gpg_error_t (*func) (void *data, void *value);
78 typedef struct
80 size_t len;
81 void *buf;
82 } membuf_t;
84 extern char *home_directory;
85 extern int log_level;
87 gpg_error_t parse_options (char **line, struct argv_s *args[], void *data,
88 int more);
89 int valid_filename (const char *filename);
90 char *expand_homedir (char *str);
91 char *bin2hex (const unsigned char *data, size_t len);
92 char *gnupg_escape (const char *str);
93 char *strip_texi_and_wrap (const char *str, int html);
94 void free_key (void *data);
95 gpg_error_t create_thread (void *(*cb) (void *), void *data,
96 pthread_t * tid, int detached);
97 void release_mutex_cb (void *arg);
98 void close_fd_cb (void *arg);
99 gpg_error_t get_checksum (const char *filename, unsigned char **r_crc,
100 size_t * r_crclen);
101 gpg_error_t get_checksum_memory (void *data, size_t size,
102 unsigned char **r_crc, size_t *r_crclen);
103 char *get_username (uid_t);
104 char *get_home_dir ();
105 struct passwd *get_pwd_struct (const char *user, uid_t uid, struct passwd *,
106 char **buf, gpg_error_t *);
107 wchar_t *str_to_wchar (const char *str);
108 void *get_in_addr (struct sockaddr *sa);
109 gpg_error_t open_check_file (const char *, int *, struct stat *, int);
111 #endif