util.h 2.1 KB

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