Fixes stat() #include
[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 <sys/types.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <stdio.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 *
28 netmkaddr(char *linear, char *defnet, char *defsrv)
29 {
30         /* TODO: this isn't threadsafe */
31         static char addr[256];
32         char *cp;
33
34         /*
35          *  dump network name
36          */
37         cp = strchr(linear, '!');
38         if(cp == 0){
39                 if(defnet==0){
40                         if(defsrv)
41                                 snprintf(addr, sizeof(addr), "net!%s!%s",
42                                         linear, defsrv);
43                         else
44                                 snprintf(addr, sizeof(addr), "net!%s", linear);
45                 }
46                 else {
47                         if(defsrv)
48                                 snprintf(addr, sizeof(addr), "%s!%s!%s", defnet,
49                                         linear, defsrv);
50                         else
51                                 snprintf(addr, sizeof(addr), "%s!%s", defnet,
52                                         linear);
53                 }
54                 return addr;
55         }
56
57         /*
58          *  if there is already a service, use it
59          */
60         cp = strchr(cp+1, '!');
61         if(cp)
62                 return linear;
63
64         /*
65          *  add default service
66          */
67         if(defsrv == 0)
68                 return linear;
69         snprintf(addr, sizeof(addr), "%s!%s", linear, defsrv);
70
71         return addr;
72 }