| 1 |
|
| /* |
| 2 |
|
| * $Id: Netgroup.xs,v 1.3 2010/08/12 14:10:24 bastian Exp $ |
| 3 |
|
| * |
| 4 |
|
| * Copyright (C) 2009, 2010 Collax GmbH |
| 5 |
|
| * (Bastian Friedrich <bastian.friedrich@collax.com>) |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include <unistd.h> |
| 9 |
|
| |
| 10 |
|
| #include "EXTERN.h" |
| 11 |
|
| #include "perl.h" |
| 12 |
|
| #include "XSUB.h" |
| 13 |
|
| |
| 14 |
|
| MODULE = Net::NIS::Netgroup PACKAGE = Net::NIS::Netgroup |
| 15 |
|
| |
| 16 |
|
| SV * |
| 17 |
|
| getdomainname() |
| 18 |
|
| PREINIT: |
| 19 |
1 |
| int len = 512; |
| 20 |
|
| char buf[512]; |
| 21 |
|
| PPCODE: |
| 22 |
1 |
| int ret = getdomainname(buf, len); |
| 23 |
1 |
▶ | if (ret == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -1) { |
0 |
1 |
1 |
|
| 24 |
0 |
▶ | XPUSHs(&PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(&PL_sv_undef); |
0 |
0 |
0 |
|
| 25 |
|
| } else { |
| 26 |
1 |
▶ | XPUSHs(sv_2mortal(newSVpv(buf, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(buf, 0))); |
0 |
1 |
1 |
|
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
|
| IV * |
| 30 |
|
| setdomainname(name) |
| 31 |
|
| const char *name |
| 32 |
|
| PREINIT: |
| 33 |
0 |
| int len = strlen(name) + 1; |
| 34 |
|
| PPCODE: |
| 35 |
0 |
| int ret = setdomainname(name, len); |
| 36 |
0 |
▶ | if (ret == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == -1) { |
0 |
0 |
0 |
|
| 37 |
0 |
▶ | XPUSHs(&PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(&PL_sv_undef); |
0 |
0 |
0 |
|
| 38 |
|
| } else { |
| 39 |
0 |
▶ | XPUSHs(sv_2mortal(newSViv(1))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(1))); |
0 |
0 |
0 |
|
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| IV * |
| 43 |
|
| innetgr(sv_netgroup, sv_host, sv_user, sv_domain) |
| 44 |
|
| SV *sv_netgroup |
| 45 |
|
| SV *sv_host |
| 46 |
|
| SV *sv_user |
| 47 |
|
| SV *sv_domain |
| 48 |
|
| PREINIT: |
| 49 |
|
| char *netgroup; |
| 50 |
|
| char *host; |
| 51 |
|
| char *user; |
| 52 |
|
| char *domain; |
| 53 |
|
| |
| 54 |
|
| int ret; |
| 55 |
|
| |
| 56 |
|
| PPCODE: |
| 57 |
1 |
| ret = 0; |
| 58 |
|
| |
| 59 |
2 |
▶ | if (!SvOK(sv_netgroup)) { netgroup = NULL; } else { netgroup = SvPV_nolen(sv_netgroup); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv_netgroup)) { netgroup = NULL; } else { netgroup = SvPV_nolen(sv_netgroup); } |
0 |
1 |
1 |
|
| 60 |
2 |
▶ | if (!SvOK(sv_host)) { host = NULL; } else { host = SvPV_nolen(sv_host); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv_host)) { host = NULL; } else { host = SvPV_nolen(sv_host); } |
0 |
1 |
1 |
|
| 61 |
2 |
▶ | if (!SvOK(sv_user)) { user = NULL; } else { user = SvPV_nolen(sv_user); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv_user)) { user = NULL; } else { user = SvPV_nolen(sv_user); } |
0 |
1 |
1 |
|
| 62 |
2 |
▶ | if (!SvOK(sv_domain)) { domain = NULL; } else { domain = SvPV_nolen(sv_domain); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv_domain)) { domain = NULL; } else { domain = SvPV_nolen(sv_domain); } |
0 |
1 |
1 |
|
| 63 |
|
| |
| 64 |
|
| /* |
| 65 |
|
| printf("xs code: netgroup == %s\n", netgroup); |
| 66 |
|
| printf("xs code: host == '%s'\n", host); |
| 67 |
|
| printf("xs code: user == '%s'\n", user); |
| 68 |
|
| printf("xs code: domain == '%s'\n", domain); |
| 69 |
|
| */ |
| 70 |
1 |
▶ | if (netgroup) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (netgroup) { |
1 |
0 |
1 |
|
| 71 |
1 |
| ret = innetgr(netgroup, host, user, domain); |
| 72 |
|
| } |
| 73 |
1 |
▶ | if (ret == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 1) { |
0 |
1 |
1 |
|
| 74 |
0 |
▶ | XPUSHs(sv_2mortal(newSViv(1))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(1))); |
0 |
0 |
0 |
|
| 75 |
|
| } else { |
| 76 |
1 |
▶ | XPUSHs(&PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(&PL_sv_undef); |
0 |
1 |
1 |
|
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| |
| 80 |
|
| AV * |
| 81 |
|
| listnetgr(netgroup, stringify = 0) |
| 82 |
|
| char *netgroup |
| 83 |
|
| bool stringify |
| 84 |
|
| |
| 85 |
|
| PREINIT: |
| 86 |
|
| char *host; |
| 87 |
|
| char *user; |
| 88 |
|
| char *domain; |
| 89 |
|
| |
| 90 |
|
| HV *el; |
| 91 |
|
| char *buf; |
| 92 |
1 |
▶ | int bufsize = 256; |
Branch: 1 branch
| Branch | True | False | Total |
| int bufsize = 256; |
0 |
1 |
1 |
|
| 93 |
|
| int len; |
| 94 |
|
| PPCODE: |
| 95 |
|
| |
| 96 |
1 |
▶ | if (stringify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stringify) { |
1 |
0 |
1 |
|
| 97 |
1 |
| buf = malloc(bufsize); |
| 98 |
1 |
▶ | if (!buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { |
0 |
1 |
1 |
|
| 99 |
0 |
| Perl_croak(aTHX_ "Could not allocate memory"); |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
1 |
| setnetgrent(netgroup); |
| 104 |
|
| |
| 105 |
1 |
▶ | while (getnetgrent (&host, &user, &domain)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (getnetgrent (&host, &user, &domain)) { |
0 |
1 |
1 |
|
| 106 |
0 |
▶ | if (stringify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stringify) { |
0 |
0 |
0 |
|
| 107 |
0 |
▶ | if (!host) host = ""; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!host) host = ""; |
0 |
0 |
0 |
|
| 108 |
0 |
▶ | if (!user) user = ""; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!user) user = ""; |
0 |
0 |
0 |
|
| 109 |
0 |
▶ | if (!domain) domain = ""; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!domain) domain = ""; |
0 |
0 |
0 |
|
| 110 |
0 |
| len = strlen("(,,)") + strlen(host) + strlen(user) + strlen(domain); |
| 111 |
0 |
▶ | if ((len+1) > bufsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((len+1) > bufsize) { |
0 |
0 |
0 |
|
| 112 |
0 |
| bufsize = len + 1; |
| 113 |
0 |
| buf = realloc(buf, bufsize); |
| 114 |
0 |
▶ | if (!buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { |
0 |
0 |
0 |
|
| 115 |
0 |
| Perl_croak(aTHX_ "Could not allocate memory"); |
| 116 |
|
| } |
| 117 |
|
| } |
| 118 |
0 |
| sprintf(buf, "(%s,%s,%s)", host, user, domain); |
| 119 |
0 |
▶ | XPUSHs(sv_2mortal(newSVpv(buf, len))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(buf, len))); |
0 |
0 |
0 |
|
| 120 |
|
| } else { |
| 121 |
0 |
| el = newHV(); |
| 122 |
|
| |
| 123 |
0 |
▶ | hv_store(el, "host", strlen("host"), host ? newSVpv(strdup(host), 0) : newSV(0), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_store(el, "host", strlen("host"), host ? newSVpv(strdup(host), 0) : newSV(0), 0); |
0 |
0 |
0 |
|
| 124 |
0 |
▶ | hv_store(el, "user", strlen("user"), user ? newSVpv(strdup(user), 0) : newSV(0), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_store(el, "user", strlen("user"), user ? newSVpv(strdup(user), 0) : newSV(0), 0); |
0 |
0 |
0 |
|
| 125 |
0 |
▶ | hv_store(el, "domain", strlen("domain"), domain ? newSVpv(strdup(domain), 0) : newSV(0), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_store(el, "domain", strlen("domain"), domain ? newSVpv(strdup(domain), 0) : newSV(0), 0); |
0 |
0 |
0 |
|
| 126 |
|
| |
| 127 |
0 |
▶ | XPUSHs(sv_2mortal(newRV_noinc((SV*)el))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newRV_noinc((SV*)el))); |
0 |
0 |
0 |
|
| 128 |
|
| } |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
1 |
▶ | if (stringify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stringify) { |
1 |
0 |
1 |
|
| 132 |
1 |
| free(buf); |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
1 |
| endnetgrent(); |
| 136 |
|
| |