1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Codendi Command-line Interface |
4
|
|
|
* |
5
|
|
|
* Portion of this file is inspired from the GForge Command-line Interface |
6
|
|
|
* contained in GForge. |
7
|
|
|
* Copyright 2005 GForge, LLC |
8
|
|
|
* http://gforge.org/ |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* common.php - Common functions |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* exit_error - Exits the program displaying an error and returning an error code |
18
|
|
|
*/ |
19
|
|
|
function exit_error($msg, $errcode=1) { |
20
|
|
|
if (is_string($msg)) { |
21
|
|
|
echo "Fatal error: ".$msg."\n"; |
22
|
|
|
} elseif (is_object($msg)) { |
23
|
|
|
echo "Fatal error: [".$msg->faultcode."] ".$msg->faultstring."\n"; |
24
|
|
|
} |
25
|
|
|
exit (intval($errcode)); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Return human readable sizes |
30
|
|
|
* |
31
|
|
|
* @author Aidan Lister <[email protected]> |
32
|
|
|
* @version 1.3.0 |
33
|
|
|
* @link http://aidanlister.com/repos/v/function.size_readable.php |
34
|
|
|
* @param int $size size in bytes |
35
|
|
|
* @param string $max maximum unit |
36
|
|
|
* @param string $system 'si' for SI, 'bi' for binary prefixes |
37
|
|
|
* @param string $retstring return string format |
38
|
|
|
*/ |
39
|
|
|
function size_readable($size, $max = null, $system = 'bi', $retstring = '%d %s') |
40
|
|
|
{ |
41
|
|
|
// Pick units |
42
|
|
|
$systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB'); |
43
|
|
|
$systems['si']['size'] = 1000; |
44
|
|
|
$systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); |
45
|
|
|
$systems['bi']['size'] = 1024; |
46
|
|
|
$sys = isset($systems[$system]) ? $systems[$system] : $systems['si']; |
47
|
|
|
|
48
|
|
|
// Max unit to display |
49
|
|
|
$depth = count($sys['prefix']) - 1; |
50
|
|
|
if ($max && false !== $d = array_search($max, $sys['prefix'])) { |
|
|
|
|
51
|
|
|
$depth = $d; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Loop |
55
|
|
|
$i = 0; |
56
|
|
|
while (abs($size) >= $sys['size'] && $i < $depth) { |
57
|
|
|
$size /= $sys['size']; |
58
|
|
|
$i++; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return sprintf($retstring, $size, $sys['prefix'][$i]); |
62
|
|
|
} |
63
|
|
|
?> |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.