File: utils.c

package info (click to toggle)
lebiniou 3.18-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,396 kB
  • sloc: ansic: 20,369; sh: 4,169; makefile: 834; awk: 194
file content (233 lines) | stat: -rw-r--r-- 3,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  Copyright 1994-2012 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "globals.h"


void
xerror(const char *fmt, ...)
{
  va_list ap;

  fprintf(stderr, "O_o ");
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

#ifdef DEBUG
  assert(0); /* to get a backtrace */
#else
  exit(1);
#endif
}


void
xperror(const char *why)
{
  fprintf(stderr, "[!] System error: ");
  perror(why);
  exit(1);
}


void
okdone(const char *what)
{
  if (libbiniou_verbose)
    printf("[+] %s\n", what);
}


void *
xmalloc(const size_t size)
{
  void *pouet = malloc(size);

  if (NULL == pouet)
    xerror("xmalloc: malloc failed\n");

  return pouet;
}


void *
xcalloc(const size_t nmemb, const size_t size)
{
  void *pouet = calloc(nmemb, size);

  if (NULL == pouet)
    xerror("xcalloc: calloc failed\n");

  return pouet;
}


void *
xrealloc(void *ptr, size_t size)
{
  void *pouet = realloc(ptr, size);

  if (NULL == pouet)
    xerror("xrealloc: realloc failed\n");

  return pouet;
}


long
xatol(const char *value)
{
  long l;

  errno = 0;
  l = strtol(value, NULL, 10);
  if (errno != 0)
    xperror("strtol");

  return l;
}

static void
rmkdir2(const char *dir)
{
  const mode_t omode = S_IRWXU|S_IRWXG|S_IRWXO;
#ifdef XDEBUG
  printf("mkdir(%s)... ", dir);
#endif
  if (access(dir, R_OK) == -1) { 
    if (mkdir(dir, omode) == -1)
      xperror("mkdir");
  }
#ifdef XDEBUG
  printf("done\n");
#endif
}


void
rmkdir(const char *p)
{
  /* recursive mkdir */
  char *slash = NULL;
  char *path;

  assert(p != NULL);
  path = strdup(p);

  if (path[0] == '/')
    slash = strchr(path+sizeof(char), '/');
  else
    slash = strchr(path, '/');

  while (slash != NULL) {
    *slash = '\0';
    rmkdir2(path);
    *slash = '/';
    slash = strchr(slash+sizeof(char), '/');
  }
  rmkdir2(path);

  xfree(path);
}


uint32_t
FNV_hash(const char* str)
{
  const unsigned int fnv_prime = 0x811C9DC5;
  unsigned int hash = 0;
  int c;
  
  while ((c = *str++)) {
    hash *= fnv_prime;
    hash ^= c;
  }
   
  return (uint32_t)hash;
}


void
ms_sleep(const u_long msec)
{
#if 0 /* nanosleep seems NOK */
  struct timespec ts;

  ts.tv_sec = (msec / 1000);
  ts.tv_nsec = (msec % 1000) * 1000000;

  nanosleep(&ts, NULL);
#else
  struct timeval tv;

  tv.tv_sec = (msec / 1000);
  tv.tv_usec = (msec % 1000) * 1000;

  select(0, 0, 0, 0, &tv);
#endif
}


/*!
 * Parses a string containing 2 shorts
 * eg "640x480" => 640, 480 (for resolution)
 * "15,30" => 15, 30 (for timers)
 */
int
parse_two_shorts(const char *str, const int delim, short *a, short *b)
{
  int rc = 0;
  char *dup = NULL;
  char *found = NULL;
  long a0, b0;

  if ((NULL == a) && (NULL == b))
    xerror("%s: no variable to set !\n", __FUNCTION__, a, b);

  dup = strdup(str);
  if (NULL == dup)
    xerror("strdup\n");

  found = strchr(str, delim);
  if (NULL == found)
    xerror("%s: did not find delimiter '%c' in \"%s\"\n", __FUNCTION__, delim, str);
  *found = '\0';
  found++;

  if (NULL != a) {
    a0 = xatol(dup);
    if ((a0 >= SHRT_MIN) && (a0 <= SHRT_MAX))
      *a = a0;
    else
      rc = -1;
  }

  if (NULL != b) {
    b0 = xatol(found);
    if ((b0 >= SHRT_MIN) && (b0 <= SHRT_MAX))
      *b = b0;
    else
      rc = -1;
  }

  xfree(dup);

  return rc;
}