summaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 1a186d9f7f865d2de36153284af0d777b6f7c58e (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * Copyright (C) 2003-2011 Jason Woodward <woodwardj at jaos dot org>
 *
 * This program 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
 * any later version.
 *
 * This program 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main.h"


FILE *slapt_open_file(const char *file_name,const char *mode)
{
  FILE *fh = NULL;
  if ( (fh = fopen(file_name,mode)) == NULL ) {
    fprintf(stderr,gettext("Failed to open %s\n"),file_name);

    if (errno)
      perror(file_name);

    return NULL;
  }
  return fh;
}

/* initialize regex structure and compile the regular expression */
slapt_regex_t *slapt_init_regex(const char *regex_string)
{
  slapt_regex_t *r;

  if (regex_string == NULL)
    return NULL;

  r = slapt_malloc(sizeof *r);

  r->nmatch = SLAPT_MAX_REGEX_PARTS;
  r->reg_return = -1;

  /* compile our regex */
  r->reg_return = regcomp(&r->regex, regex_string,
                                REG_EXTENDED|REG_NEWLINE|REG_ICASE);
  if ( r->reg_return != 0 ) {
    size_t regerror_size;
    char errbuf[1024];
    size_t errbuf_size = 1024;
    fprintf(stderr, gettext("Failed to compile regex\n"));

    if ( (regerror_size = regerror(r->reg_return,
                                   &r->regex,errbuf,errbuf_size)) != 0 ) {
      printf(gettext("Regex Error: %s\n"),errbuf);
    }
    free(r);
    return NULL;
  }

  return r;
}

/*
  execute the regular expression and set the return code
  in the passed in structure
 */
void slapt_execute_regex(slapt_regex_t *r,const char *string)
{
  r->reg_return = regexec(&r->regex, string,
                                r->nmatch,r->pmatch,0);
}

char *slapt_regex_extract_match(const slapt_regex_t *r, const char *src, const int i)
{
  regmatch_t m  = r->pmatch[i];
  char *str = NULL;

  if (m.rm_so != -1)
  {
    unsigned int len = m.rm_eo - m.rm_so + 1;
    str = malloc(sizeof *str * len);

    str = strncpy(str, src + m.rm_so, len);
    if (len > 0)
      str[len - 1] = '\0';
  }

  return str;
}


void slapt_free_regex(slapt_regex_t *r)
{
  regfree(&r->regex);
  free(r);
}

void slapt_gen_md5_sum_of_file(FILE *f,char *result_sum)
{
  EVP_MD_CTX mdctx;
  const EVP_MD *md;
  unsigned char md_value[EVP_MAX_MD_SIZE];
  unsigned int md_len = 0, i;
  ssize_t getline_read;
  size_t getline_size;
  char *result_sum_tmp = NULL;
  char *getline_buffer = NULL;

  md = EVP_md5();

  EVP_MD_CTX_init(&mdctx);
  EVP_DigestInit_ex(&mdctx, md, NULL);

  rewind(f);

  while ( (getline_read = getline(&getline_buffer, &getline_size, f)) != EOF )
    EVP_DigestUpdate(&mdctx, getline_buffer, getline_read);

  free(getline_buffer);

  EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
  EVP_MD_CTX_cleanup(&mdctx);

  result_sum[0] = '\0';

  for (i = 0; i < md_len; ++i) {
    char *p = slapt_malloc( sizeof *p * 3 );

    if ( snprintf(p,3,"%02x",md_value[i]) > 0 ) {

      if ( (result_sum_tmp = strncat(result_sum,p,3)) != NULL )
        result_sum = result_sum_tmp;

    }

    free(p);
  }

}

/* recursively create dirs */
void slapt_create_dir_structure(const char *dir_name)
{
  char *cwd = NULL;
  int position = 0,len = 0;

  cwd = getcwd(NULL,0);
  if ( cwd == NULL ) {
    fprintf(stderr,gettext("Failed to get cwd\n"));
    return;
  }

  len = strlen(dir_name);
  while ( position < len ) {

    char *pointer = NULL;
    char *dir_name_buffer = NULL;

    /* if no more directory delim, then this must be last dir */
    if ( strstr(dir_name + position,"/" ) == NULL ) {

      dir_name_buffer = strndup(
        dir_name + position,
        strlen(dir_name + position)
      );

      if ( strcmp(dir_name_buffer,".") != 0 ) {
        if ( (mkdir(dir_name_buffer,0755)) == -1) {
          if (errno != EEXIST) {
            fprintf(stderr,gettext("Failed to mkdir: %s\n"),dir_name_buffer);
            exit(EXIT_FAILURE);
          }
        }
        if ( (chdir(dir_name_buffer)) == -1 ) {
          fprintf(stderr,gettext("Failed to chdir to %s\n"),dir_name_buffer);
          exit(EXIT_FAILURE);
        }
      }/* don't create . */

      free(dir_name_buffer);
      break;
    } else {
      if ( dir_name[position] == '/' ) {
        /* move on ahead */
        ++position;
      } else {

        /* figure our dir name and mk it */
        pointer = strchr(dir_name + position,'/');
        dir_name_buffer = strndup(
          dir_name + position,
          strlen(dir_name + position) - strlen(pointer)
        );

        if ( strcmp(dir_name_buffer,".") != 0 ) {
          if ( (mkdir(dir_name_buffer,0755)) == -1 ) {
            if (errno != EEXIST) {
              fprintf(stderr,gettext("Failed to mkdir: %s\n"),dir_name_buffer);
              exit(EXIT_FAILURE);
            }
          }
          if ( (chdir(dir_name_buffer)) == -1 ) {
            fprintf(stderr,gettext("Failed to chdir to %s\n"),dir_name_buffer);
            exit(EXIT_FAILURE);
          }
        } /* don't create . */

        free(dir_name_buffer);
        position += (strlen(dir_name + position) - strlen(pointer));
      }
    }
  }/* end while */

  if ( (chdir(cwd)) == -1 ) {
    fprintf(stderr,gettext("Failed to chdir to %s\n"),cwd);
    return;
  }

  free(cwd);
}

int slapt_ask_yes_no(const char *format, ...)
{
  va_list arg_list;
  int answer, parsed_answer = 0;

  va_start(arg_list, format);
  vprintf(format, arg_list);
  va_end(arg_list);

  while ((answer = fgetc(stdin)) != EOF) {

    if (answer == '\n')
      break;

    if ( ((tolower(answer) == 'y') ||
          (tolower(answer) == 'n')) && parsed_answer == 0)
      parsed_answer = tolower(answer);

  }

  if (parsed_answer == 'y')
    return 1;

  if (parsed_answer == 'n')
    return 0;

  return -1;
}

char *slapt_str_replace_chr(const char *string,const char find,
                            const char replace)
{
  unsigned int i,len = 0;
  char *clean = slapt_calloc( strlen(string) + 1, sizeof *clean);;

  len = strlen(string);
  for (i = 0;i < len; ++i) {
    if (string[i] == find ) {
      clean[i] = replace;
    } else {
      clean[i] = string[i];
    }
  }
  clean[ strlen(string) ] = '\0';

  return clean;
}

__inline void *slapt_malloc(size_t s)
{
  void *p;
  if ( ! (p = malloc(s)) ) {
    fprintf(stderr,gettext("Failed to malloc\n"));

    if ( errno )
      perror("malloc");

    exit(EXIT_FAILURE);
  }
  return p;
}

__inline void *slapt_calloc(size_t n,size_t s)
{
  void *p;
  if ( ! (p = calloc(n,s)) ) {
    fprintf(stderr,gettext("Failed to calloc\n"));

    if ( errno )
      perror("calloc");

    exit(EXIT_FAILURE);
  }
  return p;
}

const char *slapt_strerror(slapt_code_t code)
{
  switch (code) {
    case SLAPT_OK:
      return "No error";
    case SLAPT_MD5_CHECKSUM_MISMATCH:
      return gettext("MD5 checksum mismatch, override with --no-md5");
    case SLAPT_MD5_CHECKSUM_MISSING:
      return gettext("Missing MD5 checksum, override with --no-md5");
    case SLAPT_DOWNLOAD_INCOMPLETE:
      return gettext("Incomplete download");
    #ifdef SLAPT_HAS_GPGME
    case SLAPT_GPG_KEY_IMPORTED:
      return gettext("GPG key successfully imported");
    case SLAPT_GPG_KEY_NOT_IMPORTED:
      return gettext("GPG key could not be imported");
    case SLAPT_GPG_KEY_UNCHANGED:
      return gettext("GPG key already present");
    case SLAPT_CHECKSUMS_VERIFIED:
      return gettext("Checksums signature successfully verified");
    case SLAPT_CHECKSUMS_NOT_VERIFIED:
      return gettext("Checksums signature could not be verified");
    case SLAPT_CHECKSUMS_MISSING_KEY:
      return gettext("No key for verification");
    #endif
    default:
  return gettext("Unknown error");
  };
}

const char *slapt_priority_to_str(SLAPT_PRIORITY_T priority)
{

  switch(priority) {
    case SLAPT_PRIORITY_DEFAULT:
      return gettext("Default");
    case SLAPT_PRIORITY_DEFAULT_PATCH:
      return gettext("Default Patch");
    case SLAPT_PRIORITY_PREFERRED:
      return gettext("Preferred");
    case SLAPT_PRIORITY_PREFERRED_PATCH:
      return gettext("Preferred Patch");
    case SLAPT_PRIORITY_OFFICIAL:
      return gettext("Official");
    case SLAPT_PRIORITY_OFFICIAL_PATCH:
      return gettext("Official Patch");
    case SLAPT_PRIORITY_CUSTOM:
      return gettext("Custom");
    case SLAPT_PRIORITY_CUSTOM_PATCH:
      return gettext("Custom Patch");
    default:
      return NULL;
  };

}

SLAPT_BOOL_T slapt_disk_space_check (const char *path,double space_needed)
{
  struct statvfs statvfs_buf;

  if (space_needed < 0)
    return SLAPT_TRUE;

  space_needed *= 1024;

  if (statvfs(path, &statvfs_buf) != 0) {

    if (errno)
      perror("statvfs");

    return SLAPT_FALSE;
  } else {
    if (statvfs_buf.f_bavail < (space_needed / statvfs_buf.f_bsize))
      return SLAPT_FALSE;
  }

  return SLAPT_TRUE;
}

slapt_list_t *slapt_init_list(void)
{
  slapt_list_t *list = slapt_malloc(sizeof *list);
  list->items = slapt_malloc(sizeof *list->items);
  list->count = 0;

  return list;
}

void slapt_add_list_item(slapt_list_t *list,const char *item)
{
  char **realloc_tmp;

  realloc_tmp =
    realloc( list->items, sizeof *list->items * (list->count + 1) );

  if ( realloc_tmp == NULL )
    return;

  list->items = realloc_tmp;
  list->items[ list->count ] = strndup(item, strlen(item));
  list->items[ list->count ][strlen(item)] = '\0';
  ++list->count;

}

void slapt_remove_list_item(slapt_list_t *list, const char *item)
{
  unsigned int i = 0;
  char *tmp = NULL;

  while (i < list->count) {
    if ( strcmp(item,list->items[i]) == 0 && tmp == NULL ) {
      tmp = list->items[i];
    }
    if ( tmp != NULL && (i+1 < list->count) ) {
      list->items[i] = list->items[i + 1];
    }
    ++i;
  }
  if ( tmp != NULL ) {
    char **realloc_tmp;
    int count = list->count - 1;
    if ( count < 1 )
      count = 1;

    free(tmp);

    realloc_tmp = realloc(list->items, sizeof *list->items * count);
    if ( realloc_tmp != NULL ) {
      list->items = realloc_tmp;
      if (list->count > 0)
        --list->count;
    }

  }
}

const char *slapt_search_list(slapt_list_t *list, const char *needle)
{
  unsigned int i;

  for (i = 0; i < list->count; i++) {
    if (strcmp (list->items[i], needle) == 0)
      return list->items[i];
  }

  return NULL;
}

void slapt_free_list(slapt_list_t *list)
{
  unsigned int i;
  
  for (i = 0; i < list->count; ++i) {
    free(list->items[i]);
  }
  free(list->items);
  free(list);
}

slapt_list_t *slapt_parse_delimited_list(char *line, char delim)
{
  slapt_list_t *list = slapt_init_list();
  int count = 0, position = 0, len = strlen(line);

  while (isspace(line[position]) != 0)
      ++position;

  while (position < len) {
    char *start = line + position;
    char *end   = NULL, *ptr = NULL;
    int start_len = strlen(start), end_len = 0;

    if (strchr(start,delim) != NULL) {
      if (line[position] == delim || isspace(line[position]) != 0) {
        ++position;
        continue;
      } 
      end = strchr(start,delim);
    }

    if (end != NULL)
      end_len = strlen(end);

    ptr = strndup(start, start_len - end_len);

    slapt_add_list_item(list, ptr);
    free(ptr);
    ++count;

    position += start_len - end_len;
  }

  return list;
}