use m4_normalize on the output strings to remove trailing spaces/tabs and consolidate...
[AROS.git] / workbench / c / shellcommands / CD.c
blobe8f5fec96b4d7d4a0e58ac927e77a4b85aa07b7b
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: CD CLI command
6 Lang: English
7 */
8 /*****************************************************************************
10 NAME
14 SYNOPSIS
16 DIR
18 LOCATION
22 FUNCTION
24 Without argument it shows the name of the current directory.
25 With argument it changes the current directory.
27 INPUTS
29 DIR -- path to change to current directory
31 RESULT
33 EXAMPLE
35 BUGS
37 SEE ALSO
39 INTERNALS
41 HISTORY
43 ******************************************************************************/
45 #include <exec/execbase.h>
46 #include <exec/memory.h>
47 #include <proto/exec.h>
48 #include <dos/dos.h>
49 #include <proto/dos.h>
51 #include <aros/shcommands.h>
53 static STRPTR GetLockName(BPTR lock, struct ExecBase *SysBase,
54 struct DosLibrary *DOSBase);
56 AROS_SH1(CD, 41.2,
57 AROS_SHA(STRPTR, , DIR, , NULL))
59 AROS_SHCOMMAND_INIT
61 BPTR dir,newdir;
62 STRPTR buf;
63 struct FileInfoBlock *fib;
64 LONG return_code = 0, error = 0;
66 if (SHArg(DIR))
68 dir = Lock(SHArg(DIR), SHARED_LOCK);
70 if (dir)
72 fib = AllocDosObject(DOS_FIB, NULL);
74 if (fib != NULL)
76 if (Examine(dir, fib))
78 if (fib->fib_DirEntryType > 0)
80 newdir = dir;
81 dir = CurrentDir(newdir);
83 buf = GetLockName(newdir, SysBase, DOSBase);
84 if (buf != NULL)
86 SetCurrentDirName(buf);
87 FreeVec(buf);
90 else
91 error = ERROR_OBJECT_WRONG_TYPE;
93 else
94 error = IoErr();
96 FreeDosObject(DOS_FIB, fib);
98 else
99 error = IoErr();
101 UnLock(dir);
103 else
104 error = IoErr();
106 else
108 dir = CurrentDir(BNULL);
110 buf = GetLockName(dir, SysBase, DOSBase);
111 if (buf != NULL)
113 if (FPuts(Output(), buf) < 0 || FPuts(Output(), "\n") < 0)
114 error = IoErr();
116 else
117 error = IoErr();
118 FreeVec(buf);
119 CurrentDir(dir);
122 if (error != 0)
124 PrintFault(error, "CD");
125 return_code = RETURN_ERROR;
128 return return_code;
130 AROS_SHCOMMAND_EXIT
134 static STRPTR GetLockName(BPTR lock, struct ExecBase *SysBase, struct DosLibrary *DOSBase)
136 LONG error = 0;
137 STRPTR buf = NULL;
138 ULONG i;
140 for(i = 256; buf == NULL && error == 0; i += 256)
142 buf = AllocVec(i, MEMF_PUBLIC);
143 if (buf != NULL)
145 if (!NameFromLock(lock, buf, i))
147 error = IoErr();
148 if (error == ERROR_LINE_TOO_LONG)
150 error = 0;
151 FreeVec(buf);
152 buf = NULL;
156 else
157 error = IoErr();
160 if (error != 0)
161 SetIoErr(error);
162 return buf;