1 #ifndef GIT_COMPAT_UTIL_H 2 #define GIT_COMPAT_UTIL_H 3 4 #define _ALL_SOURCE 1 5 #define _BSD_SOURCE 1 6 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ 7 #define _DEFAULT_SOURCE 1 8 9 #include <fcntl.h> 10 #include <stdbool.h> 11 #include <stddef.h> 12 #include <stdlib.h> 13 #include <stdarg.h> 14 #include <linux/types.h> 15 16 #ifdef __GNUC__ 17 #define NORETURN __attribute__((__noreturn__)) 18 #else 19 #define NORETURN 20 #ifndef __attribute__ 21 #define __attribute__(x) 22 #endif 23 #endif 24 25 /* General helper functions */ 26 void usage(const char *err) NORETURN; 27 void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 28 int error(const char *err, ...) __attribute__((format (printf, 1, 2))); 29 void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 30 31 void set_warning_routine(void (*routine)(const char *err, va_list params)); 32 33 static inline void *zalloc(size_t size) 34 { 35 return calloc(1, size); 36 } 37 38 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) 39 40 struct dirent; 41 struct strlist; 42 43 int mkdir_p(char *path, mode_t mode); 44 int rm_rf(const char *path); 45 struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *)); 46 bool lsdir_no_dot_filter(const char *name, struct dirent *d); 47 int copyfile(const char *from, const char *to); 48 int copyfile_mode(const char *from, const char *to, mode_t mode); 49 int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size); 50 51 ssize_t readn(int fd, void *buf, size_t n); 52 ssize_t writen(int fd, void *buf, size_t n); 53 54 size_t hex_width(u64 v); 55 int hex2u64(const char *ptr, u64 *val); 56 57 extern unsigned int page_size; 58 extern int cacheline_size; 59 60 bool find_process(const char *name); 61 62 int fetch_kernel_version(unsigned int *puint, 63 char *str, size_t str_sz); 64 #define KVER_VERSION(x) (((x) >> 16) & 0xff) 65 #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff) 66 #define KVER_SUBLEVEL(x) ((x) & 0xff) 67 #define KVER_FMT "%d.%d.%d" 68 #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x) 69 70 const char *perf_tip(const char *dirpath); 71 72 #ifndef HAVE_SCHED_GETCPU_SUPPORT 73 int sched_getcpu(void); 74 #endif 75 76 #endif /* GIT_COMPAT_UTIL_H */ 77