Mercurial > dillo_port1.3
annotate dpid/misc_new.c @ 2048:5060d415a85a
clickable menu items (even those introducing submenus) MUST have callbacks
I clicked on the "Panel size" item itself instead of any of the
options in its submenu, and: Segfault!
author  corvid <corvid@lavabit.com> 

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

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

151 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

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

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

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

155 return 0; 
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 return template; 
f1eca1aba94a
 Removed a warning in dpi by adding a mkfname function.
jcid
parents:
0
diff
changeset

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

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

160 /* 
153  161 * 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

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

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

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

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

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

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

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

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

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

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

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

194 
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 * Return a new, random hexadecimal string of 'nchar' characters. 
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 char *a_Misc_mksecret(int nchar) 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

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

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

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

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

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

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

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

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

207 int hexdigit = (random >> (i * 5)) & 0x0f; 
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] = hexdigit > 9 ? hexdigit + 'a'  10 : hexdigit + '0'; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

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

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

212 MSG("a_Misc_mksecret: %s\n", 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 return secret; 
6c8346ed1073
Introduce basic sharedsecretbased authentication
Jorge Arellano Cid <jcid@dillo.org>
parents:
1223
diff
changeset

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

216 