line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
** Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
3
|
|
|
|
|
|
|
** |
4
|
|
|
|
|
|
|
** This program is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
** modify it under the same terms as Perl itself. |
6
|
|
|
|
|
|
|
** |
7
|
|
|
|
|
|
|
** See file and |
8
|
|
|
|
|
|
|
*/ |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "EXTERN.h" |
11
|
|
|
|
|
|
|
#include "perl.h" |
12
|
|
|
|
|
|
|
#include "XSUB.h" |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#include "CTK.h" |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#define PROGRAM_NAME "shred" |
17
|
|
|
|
|
|
|
#define PROGRAM_VERSION "1.00" |
18
|
|
|
|
|
|
|
#define AUTHORS "Serz Minus" |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#define PASSES 3 |
21
|
|
|
|
|
|
|
#define BUFFSIZE 512 |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
static char * |
24
|
6
|
|
|
|
|
|
rnds(int n) { |
25
|
6
|
|
|
|
|
|
int pid = getpid(); |
26
|
|
|
|
|
|
|
int i; |
27
|
|
|
|
|
|
|
static char rndch[BUFFSIZE]; |
28
|
|
|
|
|
|
|
|
29
|
6
|
50
|
|
|
|
|
if (pid < 0) pid = pid * -1; |
30
|
6
|
|
|
|
|
|
pid += n; |
31
|
6
|
|
|
|
|
|
srand(pid % 255); |
32
|
|
|
|
|
|
|
|
33
|
3078
|
100
|
|
|
|
|
for (i = 0; i < BUFFSIZE; i++) { |
34
|
3072
|
|
|
|
|
|
rndch[i] = (rand() % 255); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
6
|
|
|
|
|
|
return rndch; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
static int |
41
|
6
|
|
|
|
|
|
pass( int fd, off_t *sizep, int n) { |
42
|
6
|
|
|
|
|
|
off_t size = *sizep; |
43
|
6
|
|
|
|
|
|
off_t blksize = (off_t) BUFFSIZE; |
44
|
6
|
|
|
|
|
|
off_t blks = (off_t) 0; |
45
|
6
|
|
|
|
|
|
off_t blkn = (off_t) 0; |
46
|
6
|
|
|
|
|
|
off_t blka = size % blksize; |
47
|
|
|
|
|
|
|
ssize_t ssize; /* Return value from write */ |
48
|
|
|
|
|
|
|
char *buf; |
49
|
6
|
50
|
|
|
|
|
if (blka == 0) { |
50
|
0
|
|
|
|
|
|
blks = size / blksize; |
51
|
|
|
|
|
|
|
} else { |
52
|
6
|
|
|
|
|
|
blks = (size - blka) / blksize; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
6
|
50
|
|
|
|
|
if (lseek (fd, (off_t) 0, SEEK_SET) == -1) { |
56
|
0
|
|
|
|
|
|
fprintf(stderr, "Error. Can't rewind\n"); |
57
|
0
|
|
|
|
|
|
fflush (stderr); |
58
|
0
|
|
|
|
|
|
return 0; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
/* Loop to retry partial writes. */ |
62
|
6
|
|
|
|
|
|
buf = rnds(n); |
63
|
6
|
50
|
|
|
|
|
for (blkn = 0; blkn < blks; blkn ++) { |
64
|
0
|
|
|
|
|
|
ssize = write (fd, buf, blksize); |
65
|
|
|
|
|
|
|
} |
66
|
6
|
50
|
|
|
|
|
if (blka > 0) { |
67
|
6
|
|
|
|
|
|
ssize = write (fd, buf, blka); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
6
|
|
|
|
|
|
return 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
static int |
74
|
2
|
|
|
|
|
|
wipe(char *fn, size_t sz) { |
75
|
|
|
|
|
|
|
int fd; |
76
|
|
|
|
|
|
|
off_t size; |
77
|
|
|
|
|
|
|
size_t i; |
78
|
|
|
|
|
|
|
unsigned int n; |
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
|
|
|
fd = open (fn, O_WRONLY | O_NOCTTY); |
81
|
2
|
50
|
|
|
|
|
if (fd < 0) { |
82
|
0
|
|
|
|
|
|
fprintf(stderr, "Can't open file \"%s\": %s\n", fn, rerr); |
83
|
0
|
|
|
|
|
|
fflush (stderr); |
84
|
0
|
|
|
|
|
|
return 0; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
|
size = sz; |
88
|
|
|
|
|
|
|
/* printf("SIZE: %d\n",size); */ |
89
|
|
|
|
|
|
|
|
90
|
2
|
50
|
|
|
|
|
if (size < 0) { |
91
|
0
|
|
|
|
|
|
fprintf(stderr, "File \"%s\" has negative size\n", fn); |
92
|
0
|
|
|
|
|
|
fflush (stderr); |
93
|
0
|
|
|
|
|
|
return 0; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
/* Do the work */ |
97
|
2
|
|
|
|
|
|
n = PASSES; |
98
|
|
|
|
|
|
|
|
99
|
8
|
100
|
|
|
|
|
for (i = 0; i < n; i++) { |
100
|
6
|
|
|
|
|
|
if (pass(fd, &size, i)) { |
101
|
|
|
|
|
|
|
/*printf("Pass %d on %d\n", i+1, n);*/ |
102
|
|
|
|
|
|
|
} else { |
103
|
|
|
|
|
|
|
/*printf("Fault %d on %d\n", i+1, n);*/ |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
2
|
50
|
|
|
|
|
if (close (fd) != 0) { |
108
|
0
|
|
|
|
|
|
fprintf(stderr, "Can't close \"%s\": %s\n", fn, rerr); |
109
|
0
|
|
|
|
|
|
fflush (stderr); |
110
|
0
|
|
|
|
|
|
return 0; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
MODULE = CTK PACKAGE = CTK::UtilXS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
SV * |
120
|
|
|
|
|
|
|
xstest() |
121
|
|
|
|
|
|
|
ALIAS: |
122
|
|
|
|
|
|
|
xsver = 1 |
123
|
|
|
|
|
|
|
CODE: |
124
|
|
|
|
|
|
|
{ |
125
|
1
|
|
|
|
|
|
RETVAL = newSVpv(PROGRAM_VERSION,0); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
OUTPUT: |
128
|
|
|
|
|
|
|
RETVAL |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
int |
131
|
|
|
|
|
|
|
wipef(str,sz) |
132
|
|
|
|
|
|
|
SV * str |
133
|
|
|
|
|
|
|
size_t sz |
134
|
|
|
|
|
|
|
PROTOTYPE: $ |
135
|
|
|
|
|
|
|
PREINIT: |
136
|
|
|
|
|
|
|
char *rstr; |
137
|
|
|
|
|
|
|
STRLEN rlen; |
138
|
|
|
|
|
|
|
CODE: |
139
|
|
|
|
|
|
|
{ |
140
|
|
|
|
|
|
|
int wres; |
141
|
2
|
50
|
|
|
|
|
rstr = SvPV(str, rlen); /* SvPV(sv, len) gives warning for signed len */ |
142
|
|
|
|
|
|
|
/* printf("String: %s\n",rstr); */ |
143
|
2
|
|
|
|
|
|
wres = wipe(rstr,sz); |
144
|
2
|
|
|
|
|
|
RETVAL = wres; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
OUTPUT: |
147
|
|
|
|
|
|
|
RETVAL |