My slstatus configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

71 行
1.5 KiB

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