File: misc.h

package info (click to toggle)
s390-tools 2.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,288 kB
  • sloc: ansic: 187,079; sh: 12,157; cpp: 5,049; makefile: 2,812; perl: 2,541; asm: 1,097; python: 697; xml: 29
file content (101 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * dump2tar - tool to dump files and command output into a tar archive
 *
 * Helper functions
 *
 * Copyright IBM Corp. 2016, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#ifndef MISC_H
#define MISC_H

#include <stdbool.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

#include "lib/util_libc.h"
#include "global.h"


#define MSG_LEN	256

#define DBG(...) \
		do { \
			if (global_debug) \
				debug(__FILE__, __LINE__, ##__VA_ARGS__); \
		} while (0)

#define mwarn(fmt, ...)		_mwarn(true, (fmt), ##__VA_ARGS__)
#define mwarnx(fmt, ...)	_mwarn(false, (fmt), ##__VA_ARGS__)

/* Helper macro for constructing messages in variables */
#define HANDLE_RC(rc, max, off, label)  \
	do { \
		if ((rc) > 0) \
			(off) += (rc); \
		if ((off) > (max)) \
			goto label; \
	} while (0)

/* Program exit codes */
#define EXIT_OK				0
#define EXIT_RUNTIME			1
#define EXIT_USAGE			2

/* Number of nanoseconds in a second */
#define NSEC_PER_SEC	1000000000L
#define NSEC_PER_MSEC	1000000L
#define NSEC_PER_USEC	1000L

extern struct timespec main_start_ts;
struct dref;

int misc_write_data(int fd, char *addr, size_t len);
ssize_t misc_read_data(int fd, char *addr, size_t len);
void inc_timespec(struct timespec *ts, time_t sec, long nsec);
void set_timespec(struct timespec *ts, time_t sec, long nsec);
bool ts_before(struct timespec *a, struct timespec *b);
int snprintf_duration(char *buff, size_t len, struct timespec *start,
		      struct timespec *end);
char *get_threadname(void);
void debug(const char *file, unsigned long line, const char *format, ...);
void _mwarn(bool print_errno, const char *format, ...);
void verb(const char *format, ...);
void info(const char *format, ...);
#define mmalloc(len)		util_zalloc(len)
#define mcalloc(n, len)		util_zalloc((n) * (len))
#define mrealloc(ptr, len)	util_realloc((ptr), (len))
#define mstrdup(str)		util_strdup(str)
#define masprintf(fmt, ...)	__masprintf(__func__, __FILE__, __LINE__, \
					    (fmt), ##__VA_ARGS__)
char *__masprintf(const char *func, const char *file, int line,
		  const char *fmt, ...);
#define set_threadname(fmt, ...) __set_threadname(__func__, __FILE__, \
						  __LINE__, (fmt), \
						  ##__VA_ARGS__)
void __set_threadname(const char *func, const char *file, int line,
                      const char *fmt, ...);

void clear_threadname(void);
void chomp(char *str, char *c);
void lchomp(char *str, char *c);
void remove_double_slashes(char *str);
int stat_file(bool dereference, const char *abs, const char *rel,
	      struct dref *dref, struct stat *st);
void set_dummy_stat(struct stat *st);
bool starts_with(const char *str, const char *prefix);
bool ends_with(const char *str, const char *suffix);

int cmd_child(int fd, char *cmd);
int cmd_open(char *cmd, pid_t *pid_ptr);
int cmd_close(int fd, pid_t pid, int *status_ptr);

void misc_init(void);
void misc_cleanup(void);
void set_stdout_data(void);

#endif /* MISC_H */