New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / workbench / c / MakeDir.c
blob37c2c0b7e0795bc5a68d907da270d1d416d825ad
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
5 MakeDir CLI command.
6 */
8 #include <exec/memory.h>
9 #include <exec/execbase.h>
10 #include <proto/exec.h>
11 #include <dos/dos.h>
12 #include <proto/dos.h>
13 #include <utility/tagitem.h>
15 #include <stdio.h>
17 static const char version[] = "$VER: MakeDir 42.4 (17.10.2005)\n";
19 /******************************************************************************
21 NAME
23 MakeDir
25 SYNOPSIS
27 NAME/M,ALL/S
29 LOCATION
31 Workbench:C
33 FUNCTION
35 Create new empty directories with specified names.
37 INPUTS
39 NAME -- names of the directories that should be created
40 ALL -- creates intermediate directories
42 RESULT
44 NOTES
46 MakeDir does not create an icon for a new directory.
48 EXAMPLE
50 BUGS
52 SEE ALSO
54 INTERNALS
56 ******************************************************************************/
59 enum
61 ARG_NAME = 0,
62 ARG_ALL,
63 NOOFARGS
66 BPTR CreateDirAll(STRPTR name);
68 int __nocommandline;
70 int main(void)
72 IPTR args[NOOFARGS] = { (IPTR) NULL };
73 struct RDArgs *rda;
75 LONG error = RETURN_OK;
76 BPTR lock;
78 rda = ReadArgs("NAME/M,ALL/S", args, NULL);
80 if(rda != NULL)
82 int i = 0;
83 STRPTR *name = (STRPTR *)args[ARG_NAME];
85 if((name == NULL) || (*name == NULL))
87 PutStr("No name given\n");
88 error = RETURN_FAIL;
90 else
92 for(i = 0; name[i] != NULL; i++)
94 if (args[ARG_ALL])
95 lock = CreateDirAll(name[i]);
96 else
97 lock = CreateDir(name[i]);
99 /* The AmigaDOS semantics are quite strange here. When it is
100 impossible to create a certain directory, MakeDir goes on
101 to try to create the rest of the specified directories and
102 returns the LAST return value for the operation. */
103 if(lock != NULL)
105 UnLock(lock);
106 error = RETURN_OK;
108 else
110 PutStr("Cannot create directory ");
111 PutStr(name[i]);
112 PutStr("\n");
113 error = RETURN_ERROR;
118 FreeArgs(rda);
120 else
121 error = RETURN_FAIL;
123 if(error != RETURN_OK)
124 PrintFault(IoErr(), NULL);
126 return error;
129 /* CreateDirAll
131 * Walk path from left to right, Lock()ing each element. If locking fails,
132 * try CreateDir.
133 * This routine is smart enough to try optimize multiple '/'s.
136 BPTR CreateDirAll(STRPTR name)
138 STRPTR pt = name;
139 BOOL first = TRUE;
140 BPTR oldcurdir;
141 BPTR l, o;
142 UBYTE oc = 0;
143 int skip = 0;
144 UBYTE _fib[sizeof(struct FileInfoBlock) + 3];
145 struct FileInfoBlock *fib = (APTR) ((((IPTR) _fib) + 3) & ~3);
147 CurrentDir(oldcurdir = CurrentDir(0));
149 for (;;)
151 UBYTE c = *pt++;
153 if (c == ':' || c == '/' || c == '\0')
155 if (c == ':')
157 skip = 0;
159 if (!first)
161 SetIoErr(ERROR_DEVICE_NOT_MOUNTED);
162 break;
164 first = FALSE;
165 oc = pt[skip];
166 pt[skip] = '\0';
167 //Printf("Lock \"%s\"\n", (LONG) name);
168 l = Lock(name, ACCESS_READ);
170 else
172 skip = 0;
173 if (c == '/')
175 while (pt[skip] == '/')
177 skip++;
181 oc = pt[skip];
182 pt[skip] = '\0';
183 //Printf("Lock \"%s\"\n", (LONG) name);
184 l = Lock(name, ACCESS_READ);
185 if (!l)
187 pt[skip] = oc;
188 skip = *name != '/' && c == '/' ? -1 : 0;
189 oc = pt[skip];
190 pt[skip] = '\0';
192 //Printf("CreateDir \"%s\"\n", (LONG) name);
193 l = name[0] == '/' || name[0] == '\0' ? 0 : CreateDir(name);
194 if (l)
196 if (!ChangeMode(CHANGE_LOCK, l, ACCESS_READ))
198 UnLock(l);
199 l = Lock(name, ACCESS_READ);
203 else
205 LONG res;
207 /* Make sure it's a directory */
208 if (!(res = Examine(l, fib)) || fib->fib_DirEntryType < 0)
210 UnLock(l);
211 if (res)
213 SetIoErr(c == '\0' ? ERROR_OBJECT_EXISTS : ERROR_OBJECT_WRONG_TYPE);
215 break;
217 pt += skip;
218 skip = 0;
222 if (!l)
224 break;
227 o = CurrentDir(l);
228 if (o != oldcurdir)
230 UnLock(o);
233 pt[skip] = oc;
234 name = pt;
236 if (c == '\0')
238 //Printf("return success\n");
239 return CurrentDir(oldcurdir);
244 pt[skip] = oc;
246 o = CurrentDir(oldcurdir);
247 if (o != oldcurdir)
249 UnLock(o);
252 //Printf("return error\n");
253 return NULL;