My slstatus configuration
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <err.h>
  2. #include <ifaddrs.h>
  3. #include <netdb.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "util.h"
  7. const char *
  8. ipv4(const char *iface)
  9. {
  10. struct ifaddrs *ifaddr, *ifa;
  11. int s;
  12. char host[NI_MAXHOST];
  13. if (getifaddrs(&ifaddr) == -1) {
  14. warn("Failed to get IPv4 address for interface %s", iface);
  15. return NULL;
  16. }
  17. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  18. if (ifa->ifa_addr == NULL) {
  19. continue;
  20. }
  21. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  22. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
  23. if (s != 0) {
  24. warnx("Failed to get IPv4 address for interface %s", iface);
  25. return NULL;
  26. }
  27. return bprintf("%s", host);
  28. }
  29. }
  30. freeifaddrs(ifaddr);
  31. return NULL;
  32. }
  33. const char *
  34. ipv6(const char *iface)
  35. {
  36. struct ifaddrs *ifaddr, *ifa;
  37. int s;
  38. char host[NI_MAXHOST];
  39. if (getifaddrs(&ifaddr) == -1) {
  40. warn("Failed to get IPv6 address for interface %s", iface);
  41. return NULL;
  42. }
  43. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  44. if (ifa->ifa_addr == NULL) {
  45. continue;
  46. }
  47. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  48. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) {
  49. if (s != 0) {
  50. warnx("Failed to get IPv6 address for interface %s", iface);
  51. return NULL;
  52. }
  53. return bprintf("%s", host);
  54. }
  55. }
  56. freeifaddrs(ifaddr);
  57. return NULL;
  58. }