aboutsummaryrefslogtreecommitdiff
path: root/src/extlib.c
blob: 13d7f50a9a4f464d03941703dbd4152c67b42efb (plain) (blame)
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
/* extlib.c
 *
 * Copyright 2024 axtlos <axtlos@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-3.0-only
 */


#define _XOPEN_SOURCE 500
#define USE_SECURE_MEM
#include <ftw.h>

#include "extlib.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#undef free
#undef malloc

void
free_secure(void **__ptr, size_t ptrlen)
{
  memset (*__ptr, 0, ptrlen);
  free (*__ptr);
  *__ptr = NULL;
  return;
}

void *
malloc_secure (size_t len)
{
  void *mem = malloc (len);
  memset (mem, 0, len);
  return mem;
}

int
memvcmp (void *str,
	 char val,
	 size_t n)
{
  char str2[n];
  memset (str2, val, n);
  return memcmp (str, str2, n);
}

void
fcopy(FILE *f1, FILE *f2)
{
  char buffer[BUFSIZ];
  size_t n;

  while ((n = fread (buffer, sizeof (char), sizeof (buffer), f1)) > 0)
  {
    if (fwrite (buffer, sizeof (char), n, f2) != n) {
      fprintf (stderr, "Failed to copy data");
      return;
    }
    fflush (f2);
  }
}

int
rmfile(const char *fpath,
       const struct stat *sb,
       int typeflag,
       struct FTW *ftwbuf)
{
  int err = remove (fpath);
  if (err < 0) return err;
  return 0;
}

int
rrmdir(char *pathname)
{
  int err = nftw (pathname, rmfile, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
  if (err < 0) return err;
  return 0;
}