# New Ticket Created by  webmas...@cosmicperl.com 
# Please include the string:  [perl #64854]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=64854 >


Hi,
  Please find the patch attached. Let me know if you have any questions.

I've tested this code against the chdir and cwd spec tests.


Lyle
>From 17585fed1a5b868be0d8ec0ed9ca84159217d617 Mon Sep 17 00:00:00 2001
From: CosmicNet <webmas...@cosmicperl.com>
Date: Sun, 19 Apr 2009 16:54:14 +0100
Subject: [PATCH] added chdir and CWD

---
 src/builtins/globals.pir |    8 +++++++
 src/builtins/io.pir      |   48 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/src/builtins/globals.pir b/src/builtins/globals.pir
index 4d51c94..2749127 100644
--- a/src/builtins/globals.pir
+++ b/src/builtins/globals.pir
@@ -53,6 +53,14 @@ src/builtins/globals.pir - initialize miscellaneous global 
variables
     $P0 = info
     set_hll_global '$EXECUTABLE_NAME', $P0
 
+    ##  create basic $*CWD probably wants changing in future
+    .local pmc os
+    os = new ['OS']
+    ## Wouldn't let me use basic string
+    $P0 = new 'Str'
+       $P0 = os."cwd"()
+       set_hll_global '$CWD', $P0
+
     ##  create $*IN, $*OUT, $*ERR filehandles
     .local pmc pio, perl6io, perl6ioclass
     perl6ioclass = get_hll_global "IO"
diff --git a/src/builtins/io.pir b/src/builtins/io.pir
index 89bf441..57a98e5 100644
--- a/src/builtins/io.pir
+++ b/src/builtins/io.pir
@@ -167,6 +167,54 @@ Shows the supplied message and then waits for input from 
$*IN.
     .return ($S0)
 .end
 
+
+=item chdir STRING
+
+Changes the current working directory.
+
+    chdir '/new/dir';
+
+On Success the value of the new directory is put in $*CWD.
+
+=cut
+
+.sub 'chdir'
+    .param string newdir
+    .local pmc os
+    .local int success
+
+    # Error with no arguments.
+    $I0 = length newdir
+    if $I0 goto ok
+    'die'("Cannot call chdir without an argument")
+  ok:
+
+    os = new 'OS'
+    success = 0
+
+       # Get CWD
+    $S0 = os.'cwd'()
+    
+    # skip if already there
+    if $S0 == newdir goto noneed
+
+       # Try to set CWD
+    os.'chdir'(newdir)
+    
+    # See if CWD has changed
+    $S1 = os.'cwd'()
+    if $S0 == $S1 goto finish
+
+    $P0 = new 'Str'
+    $P0 = $S1
+       set_hll_global '$CWD', $P0
+  noneed:
+    inc success
+  finish:
+
+  .return (success)
+.end
+
 =back
 
 =cut
-- 
1.6.2.2.1669.g7eaf8

Reply via email to