My slstatus configuration
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

76 righe
1.6 KiB

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