Annotation of embedaddon/sudo/include/sudo_util.h, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (c) 2013-2014 Todd C. Miller <Todd.Miller@courtesan.com>
3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
17: #ifndef _SUDO_UTIL_H
18: #define _SUDO_UTIL_H
19:
20: #ifdef HAVE_STDBOOL_H
21: # include <stdbool.h>
22: #else
23: # include "compat/stdbool.h"
24: #endif /* HAVE_STDBOOL_H */
25:
26: /*
27: * Macros for operating on struct timeval.
28: */
29: #define sudo_timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0)
30:
31: #define sudo_timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec)
32:
33: #define sudo_timevalcmp(tv1, tv2, op) \
34: (((tv1)->tv_sec == (tv2)->tv_sec) ? \
35: ((tv1)->tv_usec op (tv2)->tv_usec) : \
36: ((tv1)->tv_sec op (tv2)->tv_sec))
37:
38: #define sudo_timevaladd(tv1, tv2, tv3) \
39: do { \
40: (tv3)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
41: (tv3)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
42: if ((tv3)->tv_usec >= 1000000) { \
43: (tv3)->tv_sec++; \
44: (tv3)->tv_usec -= 1000000; \
45: } \
46: } while (0)
47:
48: #define sudo_timevalsub(tv1, tv2, tv3) \
49: do { \
50: (tv3)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
51: (tv3)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
52: if ((tv3)->tv_usec < 0) { \
53: (tv3)->tv_sec--; \
54: (tv3)->tv_usec += 1000000; \
55: } \
56: } while (0)
57:
58: #ifndef TIMEVAL_TO_TIMESPEC
59: # define TIMEVAL_TO_TIMESPEC(tv, ts) \
60: do { \
61: (ts)->tv_sec = (tv)->tv_sec; \
62: (ts)->tv_nsec = (tv)->tv_usec * 1000; \
63: } while (0)
64: #endif
65:
66: /*
67: * Macros for operating on struct timespec.
68: */
69: #define sudo_timespecclear(ts) ((ts)->tv_sec = (ts)->tv_nsec = 0)
70:
71: #define sudo_timespecisset(ts) ((ts)->tv_sec || (ts)->tv_nsec)
72:
73: #define sudo_timespeccmp(ts1, ts2, op) \
74: (((ts1)->tv_sec == (ts2)->tv_sec) ? \
75: ((ts1)->tv_nsec op (ts2)->tv_nsec) : \
76: ((ts1)->tv_sec op (ts2)->tv_sec))
77:
78: #define sudo_timespecadd(ts1, ts2, ts3) \
79: do { \
80: (ts3)->tv_sec = (ts1)->tv_sec + (ts2)->tv_sec; \
81: (ts3)->tv_nsec = (ts1)->tv_nsec + (ts2)->tv_nsec; \
82: while ((ts3)->tv_nsec >= 1000000000) { \
83: (ts3)->tv_sec++; \
84: (ts3)->tv_nsec -= 1000000000; \
85: } \
86: } while (0)
87:
88: #define sudo_timespecsub(ts1, ts2, ts3) \
89: do { \
90: (ts3)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \
91: (ts3)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
92: while ((ts3)->tv_nsec < 0) { \
93: (ts3)->tv_sec--; \
94: (ts3)->tv_nsec += 1000000000; \
95: } \
96: } while (0)
97:
98: #ifndef TIMESPEC_TO_TIMEVAL
99: # define TIMESPEC_TO_TIMEVAL(tv, ts) \
100: do { \
101: (tv)->tv_sec = (ts)->tv_sec; \
102: (tv)->tv_usec = (ts)->tv_nsec / 1000; \
103: } while (0)
104: #endif
105:
106: /*
107: * Macros to extract ctime and mtime as timevals.
108: */
109: #ifdef HAVE_ST_MTIM
110: # ifdef HAVE_ST__TIM
111: # define ctim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_ctim.st__tim)
112: # define mtim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_mtim.st__tim)
113: # else
114: # define ctim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_ctim)
115: # define mtim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_mtim)
116: # endif
117: #else
118: # ifdef HAVE_ST_MTIMESPEC
119: # define ctim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_ctimespec)
120: # define mtim_get(_x, _y) TIMESPEC_TO_TIMEVAL((_y), &(_x)->st_mtimespec)
121: # else
122: # define ctim_get(_x, _y) do { (_y)->tv_sec = (_x)->st_ctime; (_y)->tv_usec = 0; } while (0)
123: # define mtim_get(_x, _y) do { (_y)->tv_sec = (_x)->st_mtime; (_y)->tv_usec = 0; } while (0)
124: # endif /* HAVE_ST_MTIMESPEC */
125: #endif /* HAVE_ST_MTIM */
126:
127: /*
128: * Macros to quiet gcc's warn_unused_result attribute.
129: */
130: #ifdef __GNUC__
131: # define ignore_result(x) do { \
132: __typeof__(x) y = (x); \
133: (void)y; \
134: } while(0)
135: #else
136: # define ignore_result(x) (void)(x)
137: #endif
138:
139: /* aix.c */
140: void aix_prep_user(char *user, const char *tty);
141: void aix_restoreauthdb(void);
142: void aix_setauthdb(char *user);
143:
144: /* atobool.c */
145: int atobool(const char *str);
146:
147: /* atoid.c */
148: id_t atoid(const char *str, const char *sep, char **endp, const char **errstr);
149:
150: /* atomode.c */
151: int atomode(const char *cp, const char **errstr);
152:
153: /* fmt_string.h */
154: char *fmt_string(const char *var, const char *value);
155:
156: /* gidlist.c */
157: int parse_gid_list(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp);
158:
159: /* progname.c */
160: void initprogname(const char *);
161:
162: /* setgroups.c */
163: int sudo_setgroups(int ngids, const GETGROUPS_T *gids);
164:
165: /* term.c */
166: bool term_cbreak(int);
167: bool term_copy(int, int);
168: bool term_noecho(int);
169: bool term_raw(int, int);
170: bool term_restore(int, bool);
171:
172: /* ttysize.c */
173: void get_ttysize(int *rowp, int *colp);
174:
175: #endif /* _SUDO_UTIL_H */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>