Mercurial > dillo_port1.3
annotate dpid/misc_new.c @ 2104:3e7e5395f0bc
nonASCII keybindings
Alexander Voigt has kindly done some testing, and it seems that this
makes bindings to most keys on a German keyboard possible  except
those that need AltGr don't work yet.
author  corvid <corvid@lavabit.com> 

date  Thu, 23 Jun 2011 19:24:11 +0000 
parents  16e52ecf20a8 
children 
rev  line source 

779
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

1 /* 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

2 * File: misc_new.c 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

3 * 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

4 * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org> 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

5 * 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

6 * This program is free software; you can redistribute it and/or modify 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

7 * it under the terms of the GNU General Public License as published by 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

8 * the Free Software Foundation; either version 3 of the License, or 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

9 * (at your option) any later version. 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

10 */ 
49735e578039
[mq]: adddpidcopyright
Jeremy Henty <onepoint@starurchin.org>
parents:
153
diff
changeset

11 
1223
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

12 #include <errno.h> /* errno, errcodes */ 
0  13 #include <unistd.h> 
1223
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

14 #include <time.h> 
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

15 #include <sys/stat.h> /* stat */ 
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

16 #include <stdlib.h> /* rand, srand */ 
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

17 
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

18 #include "../dlib/dlib.h" 
0  19 #include "dpid_common.h" 
1223
a09dbf132be6
Remove system includes for dpid
Jorge Arellano Cid <jcid@dillo.org>
parents:
1059
diff
changeset

20 #include "misc_new.h" /* for function prototypes */ 
0  21 
22  
23 /*  
24 * Close a FD handling EINTR.  
25 */  
26 int a_Misc_close_fd(int fd)  
27 {  
28 int st;  
29  
30 do {  
31 st = close(fd);  
32 } while (st < 0 && errno == EINTR);  
33 return st;  
34 }  
35  
36 /*! Reads a dpi tag from a socket  
37 * \li Continues after a signal interrupt  
38 * \Return  
39 * Dstr pointer to tag on success, NULL on failure  
40 * \important Caller is responsible for freeing the returned Dstr *  
41 */  
42 Dstr *a_Misc_rdtag(int socket)  
43 {  
44 char c = '\0';  
45 ssize_t rdlen;  
46 Dstr *tag;  
47  
48 tag = dStr_sized_new(64);  
49  
50 errno = 0;  
51  
52 do {  
53 rdlen = read(socket, &c, 1);  
54 if (rdlen == 1 && errno != EINTR)  
55 break;  
56 dStr_append_c(tag, c);  
57 } while (c != '>');  
58  
59 if (rdlen == 1) {  
60 perror("a_Misc_rdtag");  
61 dStr_free(tag, TRUE);  
62 return (NULL);  
63 }  
64 return (tag);  
65 }  
66  
67 /*!  
68 * Read a dpi tag from sock  
69 * \return  
70 * pointer to dynamically allocated request tag  
71 */  
72 char *a_Misc_readtag(int sock)  
73 {  
2103
16e52ecf20a8
Eliminated a pack of 22 compiler warnings (gcc4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents:
1384
diff
changeset

74 char *tag, c; 
16e52ecf20a8
Eliminated a pack of 22 compiler warnings (gcc4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents:
1384
diff
changeset

75 size_t i; 
0  76 size_t taglen = 0, tagmem = 10; 
77 ssize_t rdln = 1;  
78  
79 tag = NULL;  
80 // new start  
81 tag = (char *) dMalloc(tagmem + 1);  
82 for (i = 0; (rdln = read(sock, &c, 1)) != 0; i++) {  
83 if (i == tagmem) {  
84 tagmem += tagmem;  
85 tag = (char *) dRealloc(tag, tagmem + 1);  
86 }  
87 tag[i] = c;  
88 taglen += rdln;  
89 if (c == '>') {  
90 tag[i + 1] = '\0';  
91 break;  
92 }  
93 }  
94 // new end  
95 if (rdln == 1) {  
96 ERRMSG("a_Misc_readtag", "read", errno);  
97 }  
98  
99 return (tag);  
100 }  
101  
102 /*! Reads a dpi tag from a socket without hanging on read.  
103 * \li Continues after a signal interrupt  
104 * \Return  
105 * \li 1 on success  
106 * \li 0 if input is not available within timeout microseconds.  
107 * \li 1 on failure  
108 * \important Caller is responsible for freeing the returned Dstr *  
109 */  
110 /* Is this useful?  
111 int a_Misc_nohang_rdtag(int socket, int timeout, Dstr **tag)  
112 {  
113 int n_fd;  
114 fd_set sock_set, select_set;  
115 struct timeval tout;  
116  
117 FD_ZERO(&sock_set);  
118 FD_SET(socket, &sock_set);  
119  
120 errno = 0;  
121 do {  
122 select_set = sock_set;  
123 tout.tv_sec = 0;  
124 tout.tv_usec = timeout;  
125 n_fd = select(socket + 1, &select_set, NULL, NULL, &tout);  
126 } while (n_fd == 1 && errno == EINTR);  
127  
128 if (n_fd == 1) {  
129 MSG_ERR("%s:%d: a_Misc_nohang_rdtag: %s\n",  
130 __FILE__, __LINE__, dStrerror(errno));  
131 return(1);  
132 }  
133 if (n_fd == 0) {  
134 return(0);  
135 } else {  
136 *tag = a_Misc_rdtag(socket);  
137 return(1);  
138 }  
139 }  
140 */  
141  
142 /*  
143 * Alternative to mkdtemp().  
144 * Not as strong as mkdtemp, but enough for creating a directory.  
145 */  
146 char *a_Misc_mkdtemp(char *template)  
147 {  
88
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

148 for (;;) { 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

149 if (a_Misc_mkfname(template) && mkdir(template, 0700) == 0) 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

150 break; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

151 if (errno == EEXIST) 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

152 continue; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

153 return 0; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

154 } 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

155 return template; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

156 } 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

157 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

158 /* 
153  159 * Return a new, nonexistent file name from a template 
88
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

160 * (adapted from dietlibc; alternative to mkdtemp()) 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

161 */ 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

162 char *a_Misc_mkfname(char *template) 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

163 { 
0  164 char *tmp = template + strlen(template)  6; 
165 int i;  
1059
7bec6cfe6b6b
Switched a few remaining "unsigned int" to "uint_t"
Jorge Arellano Cid <jcid@dillo.org>
parents:
779
diff
changeset

166 uint_t random; 
88
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

167 struct stat stat_buf; 
0  168 
169 if (tmp < template)  
170 goto error;  
171 for (i = 0; i < 6; ++i)  
172 if (tmp[i] != 'X') {  
173 error:  
174 errno = EINVAL;  
175 return 0;  
176 }  
177 srand((uint_t)(time(0) ^ getpid()));  
88
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

178 
0  179 for (;;) { 
180 random = (unsigned) rand();  
181 for (i = 0; i < 6; ++i) {  
182 int hexdigit = (random >> (i * 5)) & 0x1f;  
183  
184 tmp[i] = hexdigit > 9 ? hexdigit + 'a'  10 : hexdigit + '0';  
185 }  
88
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

186 if (stat(template, &stat_buf) == 1 && errno == ENOENT) 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

187 return template; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

188 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

189 MSG_ERR("a_Misc_mkfname: another round for %s \n", template); 
0  190 } 
191 }  
1384
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

192 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

193 /* 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

194 * Return a new, random hexadecimal string of 'nchar' characters. 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

195 */ 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

196 char *a_Misc_mksecret(int nchar) 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

197 { 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

198 int i; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

199 uint_t random; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

200 char *secret = dNew(char, nchar + 1); 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

201 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

202 srand((uint_t)(time(0) ^ getpid())); 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

203 random = (unsigned) rand(); 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

204 for (i = 0; i < nchar; ++i) { 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

205 int hexdigit = (random >> (i * 5)) & 0x0f; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

206 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

207 secret[i] = hexdigit > 9 ? hexdigit + 'a'  10 : hexdigit + '0'; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

208 } 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

209 secret[i] = 0; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

210 MSG("a_Misc_mksecret: %s\n", secret); 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

211 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

212 return secret; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

213 } 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

214 