iplib: Fix thread-unsafeness in myipaddr
[akaros.git] / user / iplib / netmkaddr.c
1 /*
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 #include <stdlib.h>
10
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 /* truncate functionality for now. No lib/ndb.
18 #include <libnet.h>
19  */
20
21 /*
22  *  make an address, add the defaults
23  *
24  *  if you pass in only one ! in linear, this assumes this ! was between the net
25  *  and the host.  if you pass in no !s, we'll build one from defnet/defsrv.
26  */
27 char *netmkaddr(char *linear, char *defnet, char *defsrv)
28 {
29         /* TODO: this isn't threadsafe */
30         static char addr[256];
31         char *cp;
32
33         /*
34          *  dump network name
35          */
36         cp = strchr(linear, '!');
37         if (cp == 0) {
38                 if (defnet == 0) {
39                         if (defsrv)
40                                 snprintf(addr, sizeof(addr), "net!%s!%s", linear, defsrv);
41                         else
42                                 snprintf(addr, sizeof(addr), "net!%s", linear);
43                 } else {
44                         if (defsrv)
45                                 snprintf(addr, sizeof(addr), "%s!%s!%s", defnet, linear,
46                                          defsrv);
47                         else
48                                 snprintf(addr, sizeof(addr), "%s!%s", defnet, linear);
49                 }
50                 return addr;
51         }
52
53         /*
54          *  if there is already a service, use it
55          */
56         cp = strchr(cp + 1, '!');
57         if (cp)
58                 return linear;
59
60         /*
61          *  add default service
62          */
63         if (defsrv == 0)
64                 return linear;
65         snprintf(addr, sizeof(addr), "%s!%s", linear, defsrv);
66
67         return addr;
68 }