util.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. int prefixcmp(const char *str, const char *prefix);
  28. static inline void *zalloc(size_t size)
  29. {
  30. return calloc(1, size);
  31. }
  32. #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  33. struct dirent;
  34. struct strlist;
  35. int mkdir_p(char *path, mode_t mode);
  36. int rm_rf(const char *path);
  37. struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
  38. bool lsdir_no_dot_filter(const char *name, struct dirent *d);
  39. int copyfile(const char *from, const char *to);
  40. int copyfile_mode(const char *from, const char *to, mode_t mode);
  41. int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
  42. ssize_t readn(int fd, void *buf, size_t n);
  43. ssize_t writen(int fd, void *buf, size_t n);
  44. size_t hex_width(u64 v);
  45. int hex2u64(const char *ptr, u64 *val);
  46. extern unsigned int page_size;
  47. extern int cacheline_size;
  48. bool find_process(const char *name);
  49. int fetch_kernel_version(unsigned int *puint,
  50. char *str, size_t str_sz);
  51. #define KVER_VERSION(x) (((x) >> 16) & 0xff)
  52. #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
  53. #define KVER_SUBLEVEL(x) ((x) & 0xff)
  54. #define KVER_FMT "%d.%d.%d"
  55. #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
  56. const char *perf_tip(const char *dirpath);
  57. #ifndef HAVE_SCHED_GETCPU_SUPPORT
  58. int sched_getcpu(void);
  59. #endif
  60. #endif /* GIT_COMPAT_UTIL_H */