Add open_check_file() to replace open/[fl]stat.
[pwmd.git] / src / util-misc.h
blob6cc5ef148feca82044e529b391d8bdccaea4ba3b
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef UTIL_MISC_H
22 #define UTIL_MISC_H
24 #include <pthread.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <wchar.h>
28 #include <pwd.h>
29 #include <netinet/in.h>
30 #include "rcfile.h"
32 #ifdef HAVE_BACKTRACE
33 #include <execinfo.h>
34 #include <syslog.h>
35 #define BACKTRACE_COUNT 20
36 #define BACKTRACE(fn) do { \
37 int n, nptrs; \
38 char **strings; \
39 void *buffer[BACKTRACE_COUNT]; \
40 nptrs = backtrace(buffer, BACKTRACE_COUNT); \
41 strings = backtrace_symbols(buffer, nptrs); \
42 for (n = 0; n < nptrs; n++) \
43 log_write("BACKTRACE: (%s) %i: %s", fn, n, strings[n]); \
44 } while (0)
45 #endif
47 #define log_write0 log_write
49 #define log_write1(...) do { \
50 if (log_level >= 1) \
51 log_write(__VA_ARGS__); \
52 } while (0)
54 #define log_write2(...) do { \
55 if (log_level >= 2) \
56 log_write(__VA_ARGS__); \
57 } while (0)
59 typedef enum
61 OPTION_TYPE_NOARG,
62 OPTION_TYPE_ARG,
63 OPTION_TYPE_OPTARG,
64 } option_type_t;
66 struct argv_s
68 const char *opt;
69 option_type_t type;
70 gpg_error_t (*func) (void *data, void *value);
73 typedef struct
75 size_t len;
76 void *buf;
77 } membuf_t;
79 char *home_directory;
80 int log_level;
82 gpg_error_t parse_options (char **line, struct argv_s *args[], void *data,
83 int more);
84 int valid_filename (const char *filename);
85 char *expand_homedir (char *str);
86 char *bin2hex (const unsigned char *data, size_t len);
87 char *gnupg_escape (const char *str);
88 char *strip_texi_and_wrap (const char *str, int html);
89 void free_key (void *data);
90 gpg_error_t create_thread (void *(*cb) (void *), void *data,
91 pthread_t * tid, int detached);
92 void release_mutex_cb (void *arg);
93 void close_fd_cb (void *arg);
94 gpg_error_t get_checksum (const char *filename, unsigned char **r_crc,
95 size_t * r_crclen);
96 char *get_username (uid_t);
97 char *get_home_dir ();
98 struct passwd *get_pwd_struct (const char *user, uid_t uid, struct passwd *,
99 char **buf, gpg_error_t *);
100 wchar_t *str_to_wchar (const char *str);
101 void *get_in_addr (struct sockaddr *sa);
102 gpg_error_t open_check_file (const char *, int *, struct stat *, int);
104 #endif