-
Notifications
You must be signed in to change notification settings - Fork 0
/
Std.pm
241 lines (236 loc) · 4.7 KB
/
Std.pm
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
package Std;
# Title: Std.pm
# package: paquetage Std
use strict;
no warnings 'uninitialized';
use base 'Exporter';
our @EXPORT = qw(
println
printarray
mexec
writefile
readfile
write_hash_2_xml_file
read_hash_2_xml_file
genere_password
);
sub println
{
# Sub: println()
# write with a carriage return
#
# Usage:
# println($b)
#
# Parameter:
# *$b* string to display
#
# Return:
# NONE
#
# Action:
# display a string (in fact a perl var, could be an int a float, ...) followed by a carriage return
# If $::Response is defined the we are in web apache-asp mode and use $response->write()
# If $::Response and $::WEB_BUFFERING are defined then we store in $::WEB_BUFFER for late displaying
#
my $cmd = shift;
if ($::Response)
{
if ($::WEB_BUFFERING)
{
$::WEB_BUFFER.=$cmd."<BR>";
}
else
{
$::Response->Write($cmd."<BR>");
}
}
else
{
print $cmd."\n" if ($cmd);
}
}
sub printarray(@)
{
# Sub: printarray()
# print an array
#
# Usage:
# printarray(@b)
#
# Parameter:
# *@b* the array to print
#
# Return:
# NONE
#
# Action:
# print each item of the array followed by a carriage return
#
my(@array)=@_;
my($t);
foreach $t (@array)
{
println($t);
}
}
sub mexec
{
# Sub: mexec()
# execute a command
#
# Usage:
# $a = mexec($cmd)
#
# Parameter:
# *$cmd* command line to execute
#
# Return:
# *$a* what command produce on STDOUT (not STDERR)
#
# Action:
# print the command (if global var $::QUIET is not defined) execute and print the results
#
my $cmd = shift;
println("\t".$cmd) if (!$::QUIET);
my $res = `$cmd 2>&1`;
return $res;
}
sub writefile($@)
{
# Sub: writefile()
# print in a file
#
# Usage:
# writefile($filename,@text)
#
# Parameter:
# *$filename* filename
#
# *@text* text to write (multiple line in an array)
#
# Return:
# NONE
#
# Action:
# Write @text in $filename file
#
my $file=shift;
my @message = @_;
open(FILEIN,'>'.$file) || die ("---openning $file is impossible! $!");
print FILEIN @message;
close(FILEIN);
}
sub readfile($)
{
# Sub: readfile()
# read from a file
#
# Usage:
# @text = readfile($filename)
#
# Parameter:
# *$filename* filename to read
#
# Return:
# *@text* the text in the file, each line is an item of the array returned
#
# Action:
# Read a file $filename and return the text @text
#
my $file=shift;
my @message = shift;
open(FILEIN,"<$file") || die ("---openning $file is impossible! $!");
@message = <FILEIN>;
close(FILEIN);
return @message;
}
sub write_hash_2_xml_file
{
# sub: write_hash_2_xml_file()
# write a hash in an xml file (SimpleXML is used)
#
# Usage:
#
# write_hash_2_xml_file($filename,%H)
#
# Parameter:
# *$filename* filename where to store
# *%H* the hash to store
#
# Return:
# *none*
#
# Action:
# write a hash in an xml file (SimpleXML)
#
# Note:
#
#
my $fname = shift;
my %H = @_;
use XML::Simple;
open(FOUT,">$fname");
print FOUT XML::Simple::XMLout(\%H);
close(FOUT);
}
sub read_hash_2_xml_file
{
# sub: read_hash_2_xml_file()
# read a hash from an xml file (SimpleXML)
#
# Usage:
#
# my %H = read_hash_2_xml_file($filename)
#
# Parameter:
# *$filename* filename where is stored the hash
#
# Return:
# *%H* the hash to load
#
# Action:
# read a hash in an xml file (SimpleXML)
#
# Note:
#
#
my $fname = shift;
use XML::Simple;
my $h_ptr = XML::Simple::XMLin($fname);
return %{$h_ptr};
}
sub genere_password
{
# Sub: genere_password($len)
# generate a password with length $len
#
# Usage:
# print genere_password(15)
#
# Parameter:
# *$len* the length
#
# Return:
# NONE
#
# Action:
# genere un password de longeur $len
#
my $len=shift();
my @consones = ('z','r','t','p','q','s','d','f','g','h','j','k','l','m','w','x','c','v','b','n','Z','R','T','P','Q','S','D','F','G','H','J','K','L','M','W','X','C','V','B','N');
my @voyelles = ('a','e','y','u','i','o','A','E','Y','U','I','O');
my @chiffre = ('0','1','2','3','4','5','6','7','8','9');
my @specialcars = ('&','#','{','[','(','-','|','_',')',']','}','=','+','/','*','$','%',';',':','*');
my $pass='';
for (my $i=0;$i<=$len;$i++)
{
my $typec = int(rand(4));
$pass.=$consones[int(rand($#consones+1))] if($typec eq 0);
$pass.=$voyelles[int(rand($#voyelles+1))] if($typec eq 1);
$pass.=$specialcars[int(rand($#specialcars+1))] if($typec eq 2);
$pass.=$chiffre[int(rand($#chiffre+1))] if($typec eq 3);
}
return $pass;
}
1;