libgo: update to go1.9
[official-gcc.git] / libgo / go / net / port_unix.go
blob8dd1c32f95df803b119319d3e70c90a03fd9fa0e
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris nacl
7 // Read system port mappings from /etc/services
9 package net
11 import "sync"
13 var onceReadServices sync.Once
15 func readServices() {
16 file, err := open("/etc/services")
17 if err != nil {
18 return
20 defer file.close()
22 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
23 // "http 80/tcp www www-http # World Wide Web HTTP"
24 if i := byteIndex(line, '#'); i >= 0 {
25 line = line[:i]
27 f := getFields(line)
28 if len(f) < 2 {
29 continue
31 portnet := f[1] // "80/tcp"
32 port, j, ok := dtoi(portnet)
33 if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
34 continue
36 netw := portnet[j+1:] // "tcp"
37 m, ok1 := services[netw]
38 if !ok1 {
39 m = make(map[string]int)
40 services[netw] = m
42 for i := 0; i < len(f); i++ {
43 if i != 1 { // f[1] was port/net
44 m[f[i]] = port
50 // goLookupPort is the native Go implementation of LookupPort.
51 func goLookupPort(network, service string) (port int, err error) {
52 onceReadServices.Do(readServices)
53 return lookupPortMap(network, service)