- next test
[oweals/gnunet.git] / src / gns / gnocksy / gns_glue.c
1 #include <stdio.h>
2 #include <string.h>
3
4 /*
5  * Glue function to return the authoritative part
6  * of a name. i.e. the site of origin
7  *
8  * @param name the name to process
9  * @param auth pointer where the result is stored
10  * @return 0 on success < 0 on failure
11  */
12 int
13 gns_glue_get_auth ( char* name, char* auth )
14 {
15   char cmd[1024];
16   char line[1024];
17   FILE *p;
18
19   sprintf (cmd, "%s %s", "gnunet-gns -a", name);
20
21   p = popen(cmd, "r");
22
23   if (p != NULL)
24   {
25     while (fgets (line, sizeof(line), p) != NULL)
26     {
27       if (line[strlen(line)-1] == '\n')
28       {
29         line[strlen(line)-1] = '\0';
30         strcpy (auth, line);
31         return 0;
32       }
33     }
34
35   }
36
37   fclose (p);
38
39   return -1;
40 }
41
42 /*
43  * Glue function to return the short version of
44  * a given name
45  *
46  * @param name the name to shorten
47  * @param shortened pointer where the result will be stored
48  * @return 0 on success < 0 on failure
49  */
50 int
51 gns_glue_shorten ( char* name, char* shortened )
52 {
53   char cmd[1024];
54   char line[1024];
55   FILE *p;
56
57   sprintf (cmd, "%s %s", "gnunet-gns -r -s", name);
58
59   p = popen(cmd, "r");
60
61   if (p != NULL)
62   {
63     while (fgets (line, sizeof(line), p) != NULL)
64     {
65       if (line[strlen(line)-1] == '\n')
66       {
67         line[strlen(line)-1] = '\0';
68         strcpy (shortened, line);
69         return 0;
70       }
71     }
72
73   }
74
75   fclose (p);
76
77   return -1;
78 }
79
80
81 /*
82  * Glue function to expand .+ urls and shorted the
83  * resulting name
84  *
85  * @param to_expand the .+ name to expand
86  * @param host the site of origin
87  * @param shortened the expanded and shortened result pointer
88  */
89 int
90 gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened )
91 {
92   char cmd[1024];
93   char line[1024];
94   FILE *p;
95   char sorig[256];
96   char expanded[256];
97
98   sprintf (shortened, "%s%s", to_expand, host); //TODO this is a mockup
99   return 0;
100
101   sprintf (cmd, "%s %s", "gnunet-gns -a", host);
102
103   p = popen(cmd, "r");
104
105   if (p != NULL)
106   {
107     while (fgets (line, sizeof(line), p) != NULL)
108     {
109       if (line[strlen(line)-1] == '\n')
110       {
111         line[strlen(line)-1] = '\0';
112         strcpy (sorig, line);
113         return 0;
114       }
115     }
116
117   }
118
119   fclose (p);
120
121   sprintf (expanded, "%s.%s", to_expand, sorig);
122   
123   sprintf (cmd, "%s %s", "gnunet-gns -r -s", expanded);
124  
125   p = popen(cmd, "r");
126
127   if (p != NULL)
128   {
129     while (fgets (line, sizeof(line), p) != NULL)
130     {
131       if (line[strlen(line)-1] == '\n')
132       {
133         line[strlen(line)-1] = '\0';
134         strcpy (shortened, line);
135         return 0;
136       }
137     }
138
139   }
140
141   fclose (p);
142
143   return -1;
144 }