Dillo v3.1.1-119-g140d9ebd
Loading...
Searching...
No Matches
misc_new.c
Go to the documentation of this file.
1/*
2 * File: misc_new.c
3 *
4 * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <errno.h> /* errno, err-codes */
13#include <unistd.h>
14#include <time.h>
15#include <sys/stat.h> /* stat */
16#include <stdlib.h> /* rand, srand */
17
18#include "../dlib/dlib.h"
19#include "dpid_common.h"
20#include "misc_new.h" /* for function prototypes */
21
28Dstr *a_Misc_rdtag(int socket)
29{
30 char c = '\0';
31 ssize_t rdlen;
32 Dstr *tag;
33
34 tag = dStr_sized_new(64);
35
36 errno = 0;
37
38 do {
39 rdlen = read(socket, &c, 1);
40 if (rdlen == -1 && errno != EINTR)
41 break;
42 dStr_append_c(tag, c);
43 } while (c != '>');
44
45 if (rdlen == -1) {
46 perror("a_Misc_rdtag");
47 dStr_free(tag, TRUE);
48 return (NULL);
49 }
50 return (tag);
51}
52
58char *a_Misc_readtag(int sock)
59{
60 char *tag, c;
61 size_t i;
62 size_t tagmem = 10;
63 ssize_t rdln = 1;
64
65 tag = NULL;
66 // new start
67 tag = (char *) dMalloc(tagmem + 1);
68 for (i = 0; (rdln = read(sock, &c, 1)) != 0; i++) {
69 if (i == tagmem) {
70 tagmem += tagmem;
71 tag = (char *) dRealloc(tag, tagmem + 1);
72 }
73 tag[i] = c;
74 if (c == '>') {
75 tag[i + 1] = '\0';
76 break;
77 }
78 }
79 // new end
80 if (rdln == -1) {
81 ERRMSG("a_Misc_readtag", "read", errno);
82 }
83
84 return (tag);
85}
86
95/* Is this useful?
96int a_Misc_nohang_rdtag(int socket, int timeout, Dstr **tag)
97{
98 int n_fd;
99 fd_set sock_set, select_set;
100 struct timeval tout;
101
102 FD_ZERO(&sock_set);
103 FD_SET(socket, &sock_set);
104
105 errno = 0;
106 do {
107 select_set = sock_set;
108 tout.tv_sec = 0;
109 tout.tv_usec = timeout;
110 n_fd = select(socket + 1, &select_set, NULL, NULL, &tout);
111 } while (n_fd == -1 && errno == EINTR);
112
113 if (n_fd == -1) {
114 MSG_ERR("%s:%d: a_Misc_nohang_rdtag: %s\n",
115 __FILE__, __LINE__, dStrerror(errno));
116 return(-1);
117 }
118 if (n_fd == 0) {
119 return(0);
120 } else {
121 *tag = a_Misc_rdtag(socket);
122 return(1);
123 }
124}
125*/
126
131char *a_Misc_mkdtemp(char *template)
132{
133 for (;;) {
134 if (a_Misc_mkfname(template) && mkdir(template, 0700) == 0)
135 break;
136 if (errno == EEXIST)
137 continue;
138 return 0;
139 }
140 return template;
141}
142
147char *a_Misc_mkfname(char *template)
148{
149 char *tmp = template + strlen(template) - 6;
150 int i;
151 uint_t random;
152 struct stat stat_buf;
153
154 if (tmp < template)
155 goto error;
156 for (i = 0; i < 6; ++i)
157 if (tmp[i] != 'X') {
158 error:
159 errno = EINVAL;
160 return 0;
161 }
162 srand((uint_t)(time(0) ^ getpid()));
163
164 for (;;) {
165 random = (unsigned) rand();
166 for (i = 0; i < 6; ++i) {
167 int hexdigit = (random >> (i * 5)) & 0x1f;
168
169 tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
170 }
171 if (stat(template, &stat_buf) == -1 && errno == ENOENT)
172 return template;
173
174 MSG_ERR("a_Misc_mkfname: another round for %s \n", template);
175 }
176}
177
181char *a_Misc_mksecret(int nchar)
182{
183 int i;
184 uint_t random;
185 char *secret = dNew(char, nchar + 1);
186
187 srand((uint_t)(time(0) ^ getpid()));
188 random = (unsigned) rand();
189 for (i = 0; i < nchar; ++i) {
190 int hexdigit = (random >> (i * 5)) & 0x0f;
191
192 secret[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
193 }
194 secret[i] = 0;
195 MSG("a_Misc_mksecret: %s\n", secret);
196
197 return secret;
198}
199
#define MSG(...)
Definition bookmarks.c:46
unsigned int uint_t
Definition d_size.h:20
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
Definition dlib.c:254
void * dMalloc(size_t size)
Definition dlib.c:45
void dStr_free(Dstr *ds, int all)
Free a dillo string.
Definition dlib.c:337
void dStr_append_c(Dstr *ds, int c)
Append one character.
Definition dlib.c:349
void * dRealloc(void *mem, size_t size)
Definition dlib.c:53
#define TRUE
Definition dlib.h:23
#define dNew(type, count)
Definition dlib.h:49
#define MSG_ERR(...)
Definition dpid_common.h:23
#define ERRMSG(CALLER, CALLED, ERR)
Definition dpid_common.h:29
static void error(char *msg)
Definition dpidc.c:39
char * a_Misc_mkdtemp(char *template)
Alternative to mkdtemp().
Definition misc_new.c:131
char * a_Misc_readtag(int sock)
Definition misc_new.c:58
char * a_Misc_mksecret(int nchar)
Return a new, random hexadecimal string of 'nchar' characters.
Definition misc_new.c:181
Dstr * a_Misc_rdtag(int socket)
Definition misc_new.c:28
char * a_Misc_mkfname(char *template)
Return a new, nonexistent file name from a template.
Definition misc_new.c:147
Definition dlib.h:102