diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-11-27 11:29:44 -0500 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-11-27 11:29:44 -0500 |
commit | de5cb6329b4e6a4c409d1418f16a3488a53ca953 (patch) | |
tree | 3a5af4ef0fd42e4f4904326d2cc94d53582e028e /shell/bin | |
parent | 76ead734626996f82caddaca57dc2f84243b0947 (diff) |
Diffstat (limited to 'shell/bin')
-rw-r--r-- | shell/bin/cat.php | 37 | ||||
-rw-r--r-- | shell/bin/cd.php | 18 | ||||
-rw-r--r-- | shell/bin/cp.php | 72 | ||||
-rw-r--r-- | shell/bin/editor.php | 50 | ||||
-rw-r--r-- | shell/bin/grep.php | 52 | ||||
-rw-r--r-- | shell/bin/mkdir.php | 13 | ||||
-rw-r--r-- | shell/bin/mv.php | 43 | ||||
-rw-r--r-- | shell/bin/php.php | 9 | ||||
-rw-r--r-- | shell/bin/rm.php | 46 |
9 files changed, 227 insertions, 113 deletions
diff --git a/shell/bin/cat.php b/shell/bin/cat.php index 8778f22..8376ca0 100644 --- a/shell/bin/cat.php +++ b/shell/bin/cat.php @@ -1,15 +1,22 @@ -<?php -class p_cat extends prog { - public static function main($args, $env) { - $me = array_shift($args); - if (count($args)==0) { $args = array('-'); } - foreach ($args as $file) { - if ( ($file=='-') || ($file=='/dev/stdin') ) { - echo $_POST['stdin']; - } else { - echo htmlentities(file_get_contents($file)); - } - } - } -} - +<?php
+class p_cat extends prog {
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ $raw = (isset($args[0])?($args[0]=='-r'):false);
+ if ($raw) {
+ array_shift($args);
+ }
+ if (count($args)==0) { $args = array('-'); }
+ foreach ($args as $file) {
+ if ( ($file=='-') || ($file=='/dev/stdin') ) {
+ echo $_POST['stdin'];
+ } else {
+ if ($raw) {
+ echo file_get_contents($file);
+ } else {
+ echo htmlentities(file_get_contents($file));
+ }
+ }
+ }
+ }
+}
diff --git a/shell/bin/cd.php b/shell/bin/cd.php index baf30f3..80e1da6 100644 --- a/shell/bin/cd.php +++ b/shell/bin/cd.php @@ -1,8 +1,10 @@ -<?php -class p_cd extends prog { - public static function main($args, $env) { - @$dir = $args[1]; - return lts_chdir($dir); - } -} - +<?php
+class p_cd extends prog {
+ public static function main($args, $env) {
+ @$dir = $args[1];
+ $ret = lts_chdir($dir);
+ echo getcwd()."\n";
+ return $ret;
+ }
+}
+
diff --git a/shell/bin/cp.php b/shell/bin/cp.php index 4a6cfae..8f47970 100644 --- a/shell/bin/cp.php +++ b/shell/bin/cp.php @@ -1,33 +1,39 @@ -<?php -class p_cp extends prog { - /* This method (recurse_copy) was written by gimmicklessgpt@gmail.com - * and posted to the PHP manual comments section on 20-May-2009 11:04 - */ - function recurse_copy($src,$dst) { - $dir = opendir($src); - @mkdir($dst); - while(false !== ( $file = readdir($dir)) ) { - if (( $file != '.' ) && ( $file != '..' )) { - if ( is_dir($src . '/' . $file) ) { - recurse_copy($src . '/' . $file,$dst . '/' . $file); - } - else { - copy($src . '/' . $file,$dst . '/' . $file); - } - } - } - closedir($dir); - } - - public static function main($args, $env) { - $me = array_shift($args); - $flags = ''; - while (strstr($args[0],0,1) == '-') { - $flags .= array_shift($args); - } - $flags = preg_replace('/[ -]/','',$flags); - if (strpos($flags,'r')===false) { copy($args[0],$args[1]); } - else { recurse_copy($args[0],$args[1]); } - } -} - +<?php
+class p_cp extends prog {
+ /* This method (recurse_copy) was written by gimmicklessgpt@gmail.com
+ * and posted to the PHP manual comments section on 20-May-2009 11:04
+ */
+ static function recurse_copy($src,$dst) {
+ //echo 'recurse_copy('.$src.', '.$dst.");\n";
+ $dir = opendir($src);
+ @mkdir($dst);
+ while(false !== ( $file = readdir($dir)) ) {
+ if (( $file != '.' ) && ( $file != '..' )) {
+ if ( is_dir($src . '/' . $file) ) {
+ self::recurse_copy($src . '/' . $file,$dst . '/' . $file);
+ }
+ else {
+ self::copy( $src.'/'.$file , $dst.'/'.$file );
+ }
+ }
+ }
+ closedir($dir);
+ }
+
+ static function copy($src,$dst) {
+ //echo 'copy('.$src.', '.$dst.");\n";
+ copy($src, $dst);
+ }
+
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ $flags = '';
+ while (substr($args[0],0,1) == '-') {
+ $flags .= array_shift($args);
+ }
+ $flags = preg_replace('/[ -]/','',$flags);
+ if (strpos($flags,'r')===false) { self::copy($args[0],$args[1]); }
+ else { self::recurse_copy($args[0],$args[1]); }
+ }
+}
+
diff --git a/shell/bin/editor.php b/shell/bin/editor.php index a136cd2..2d5d9c3 100644 --- a/shell/bin/editor.php +++ b/shell/bin/editor.php @@ -1,25 +1,25 @@ -<?php -class p_editor extends prog { - public static function main($args, $env) { - if (isset($_POST['stdin'])) { - if (false) {//if (isset($args[1])) { - echo $args[0].': saving to `'.$args[1]."'\n"; - file_put_contents($args[1],$_POST['stdin']); - } else { - echo $_POST['stdin']; - } - } else { - if (isset($args[1]) && file_exists($args[1])) { - $text = file_get_contents($args[1]); - } else { - $text = ''; - } - echo '<div class="editor">'; - echo '<input type="hidden" name="stdout_dest" value="'.$_POST['c'].'" />'; - echo '<textarea name="stdin">'.htmlentities($text).'</textarea>'."\n"; - echo '<input type="submit" value="save" />'; - echo '</div>'; - } - } -} - +<?php
+class p_editor extends prog {
+ public static function main($args, $env) {
+ if (isset($_POST['stdin'])) {
+ if (isset($args[1])) {
+ echo $args[0].': saving to `'.$args[1]."'\n";
+ file_put_contents($args[1],$_POST['stdin']);
+ } else {
+ echo htmlentities($_POST['stdin']);
+ }
+ } else {
+ if (isset($args[1]) && file_exists($args[1])) {
+ $text = file_get_contents($args[1]);
+ } else {
+ $text = '';
+ }
+ echo '<div class="editor">';
+ echo '<input type="hidden" name="stdout_dest" value="'.$_POST['c'].'" />';
+ echo '<textarea name="stdin">'.htmlentities($text).'</textarea>'."\n";
+ echo '<input type="submit" value="save" />';
+ echo '</div>';
+ }
+ }
+}
+
diff --git a/shell/bin/grep.php b/shell/bin/grep.php new file mode 100644 index 0000000..c73b911 --- /dev/null +++ b/shell/bin/grep.php @@ -0,0 +1,52 @@ +<?php
+class p_grep extends prog {
+ public static function flag($flags,$flag) {
+ return (strpos($flags,$flag)!==false);
+ }
+ public static function recurse_grep($pattern, $subject, $flags = '') {
+ // Do a breadth-first search
+ $dirs = array();
+ if ( is_dir($subject) ) {
+ $dirs[] = $subject;
+ } else {
+ self::grep($pattern, $subject, $flags);
+ }
+ foreach($dirs as $dir) {
+ $dh = opendir($dir);
+ while(false !== ( $file = readdir($dh)) ) {
+ if (( $file != '.' ) && ( $file != '..' )) {
+ self::recurse_grep($pattern, $subject.'/'.$file, $flags);
+ }
+ }
+ closedir($dh);
+ }
+ }
+
+ public static function grep($pattern, $file, $flags) {
+ $lines = preg_split("/\r\n/",file_get_contents($file));
+ foreach ($lines as $line_number => $line_contents) {
+ if (preg_match($pattern,$line_contents)>0) {
+ if (
+ (self::flag($flags,'r') && !self::flag($flags,'h')) // recursive
+ || (self::flag($flags,'H')) // or explicit
+ ) { echo htmlentities($file).': '; }
+ if (self::flag($flags,'n')) { echo $line_number.': '; }
+ echo htmlentities($line_contents)."\n";
+ }
+ }
+ }
+
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ $flags = '';
+ while (substr($args[0],0,1) == '-') {
+ $flags .= array_shift($args);
+ }
+ $flags = preg_replace('/[ -]/','',$flags);
+ $pattern = array_shift($args);
+ foreach ($args as $file) {
+ if (self::flag($flags,'r')) { self::recurse_grep($pattern, $file, $flags); }
+ else { self::grep($pattern, $file, $flags); }
+ }
+ }
+}
diff --git a/shell/bin/mkdir.php b/shell/bin/mkdir.php new file mode 100644 index 0000000..565f47a --- /dev/null +++ b/shell/bin/mkdir.php @@ -0,0 +1,13 @@ +<?php +class p_mkdir extends prog { + public static function main($args, $env) { + $me = array_shift($args); + @$recursive = ($args[0]=='-p'); + if ($recursive) { + array_shift($args); + } + foreach ($args as $dir) { + mkdir($dir, umask(), $recursive); + } + } +} diff --git a/shell/bin/mv.php b/shell/bin/mv.php index 8fc35cd..0a23048 100644 --- a/shell/bin/mv.php +++ b/shell/bin/mv.php @@ -1,22 +1,21 @@ -<?php -class p_mv extends prog { - public static main($args, $env) { - $me = array_shift($args); - if (count($args)>2) { - $dest = array_pop($args); - if (!is_dir($dest) { - echo $me.': dest must be a directory: `'.$dest."'\n"; - return 1; - } - foreach ($args as $src) { - rename($src,$dest.'/'.basename($src)); - } - } elseif (count($args)==2) { - rename($args[0],$args[1]); - } else { - echo 'Usage: '.$me." SOURCE [SOURCE2 [SOURCE3 ...]] DEST\n"; - return 1; - } - } -} - +<?php
+class p_mv extends prog {
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ if (count($args)>2) {
+ $dest = array_pop($args);
+ if (!is_dir($dest)) {
+ echo $me.': dest must be a directory: `'.$dest."'\n";
+ return 1;
+ }
+ foreach ($args as $src) {
+ rename($src,$dest.'/'.basename($src));
+ }
+ } elseif (count($args)==2) {
+ rename($args[0],$args[1]);
+ } else {
+ echo 'Usage: '.$me." SOURCE [SOURCE2 [SOURCE3 ...]] DEST\n";
+ return 1;
+ }
+ }
+}
diff --git a/shell/bin/php.php b/shell/bin/php.php new file mode 100644 index 0000000..0e3ba38 --- /dev/null +++ b/shell/bin/php.php @@ -0,0 +1,9 @@ +<?php
+class p_php extends prog {
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ foreach ($args as $file) {
+ include($file);
+ }
+ }
+}
diff --git a/shell/bin/rm.php b/shell/bin/rm.php index 5eadaff..c55de8c 100644 --- a/shell/bin/rm.php +++ b/shell/bin/rm.php @@ -1,10 +1,36 @@ -<?php -class p_rm extends prog { - public static function main($args, $env) { - $me = array_shift($args); - foreach ($args as $file) { - unlink($file); - } - } -} - +<?php
+class p_rm extends prog {
+ static function recurse_rm($src) {
+ if ( is_dir($src) ) {
+ $dir = opendir($src);
+ while(false !== ( $file = readdir($dir)) ) {
+ if (( $file != '.' ) && ( $file != '..' )) {
+ self::recurse_rm($src.'/'.$file);
+ }
+ }
+ closedir($dir);
+ rmdir($src);
+ } else {
+ self::rm($src);
+ }
+ }
+
+ public static function rm($file) {
+ chown($file,666);
+ unlink($file);
+ }
+
+ public static function main($args, $env) {
+ $me = array_shift($args);
+ $flags = '';
+ while (substr($args[0],0,1) == '-') {
+ $flags .= array_shift($args);
+ }
+ $flags = preg_replace('/[ -]/','',$flags);
+ foreach ($args as $file) {
+ if (strpos($flags,'r')===false) { self::rm($file); }
+ else { self::recurse_rm($file); }
+ }
+ }
+}
+
|