use m4_normalize on the output strings to remove trailing spaces/tabs and consolidate...
[AROS.git] / workbench / c / shellcommands / PathPart.c
blob288535adfb7bd08b53c0c24f52844089181229ea
1 /*
2 Copyright © 2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /******************************************************************************
11 NAME
13 PathPart
15 TEMPLATE
17 DIR/K,FILE/K,ADD/K/M
19 LOCATION
23 FUNCTION
25 Extracts directory or file name from a path, or assembles a path
27 FORMAT
29 PATHPART [DIR <path name>] [FILE <path name>] [ADD {<device name | directory name | file name>}]
31 RESULT
33 Standard DOS return codes.
36 ******************************************************************************/
38 #include <proto/exec.h>
39 #include <proto/dos.h>
41 //#define DEBUG 1
42 #include <aros/debug.h>
44 #include <aros/shcommands.h>
46 #define MAX_PATH_LEN 2048 /* Same as in C:Delete */
48 AROS_SH3H(PathPart,50.1, "extract directory or file name from a path, or assemble a path\n",
49 AROS_SHAH(STRPTR ,,DIR ,/K ,NULL,"Path from which to extract the directory name"),
50 AROS_SHAH(STRPTR ,,FILE,/K ,NULL,"Path from which to extract the file name"),
51 AROS_SHAH(STRPTR *,,ADD ,/K/M,NULL,"Device and/or directory(ies) and/or file names from\n"
52 "\twhich to build a path (order matters!)\n") )
54 AROS_SHCOMMAND_INIT
56 int rc = RETURN_FAIL;
57 STRPTR outstr = NULL, buf = NULL;
58 UBYTE buf2[MAX_PATH_LEN];
59 LONG size;
61 if ( (SHArg(ADD) && SHArg(DIR))
62 || (SHArg(ADD) && SHArg(FILE))
63 || (SHArg(DIR) && SHArg(FILE))
66 SetIoErr(ERROR_BAD_TEMPLATE);
68 else if (SHArg(DIR))
70 size = strlen((char *)SHArg(DIR));
71 if ((buf = AllocVec(size, MEMF_ANY)))
73 CopyMem (SHArg(DIR), buf, size);
74 buf[PathPart(buf) - buf] = '\0';
75 outstr = buf;
76 rc = RETURN_OK;
79 else if (SHArg(FILE))
81 outstr = FilePart(SHArg(FILE));
82 rc = RETURN_OK;
84 else if (SHArg(ADD))
86 buf2[0]='\0';
87 while (*SHArg(ADD))
89 if (!AddPart(buf2 ,*SHArg(ADD)++ ,MAX_PATH_LEN))
91 /* rc == RETURN_FAIL until this point... */
92 rc = RETURN_ERROR;
93 break;
96 if (rc != RETURN_ERROR)
98 outstr = buf2;
99 rc = RETURN_OK;
102 else
103 SetIoErr(ERROR_REQUIRED_ARG_MISSING);
105 if (rc == RETURN_OK)
107 if (outstr)
109 PutStr(outstr);
110 FPutC(Output(), '\n');
113 else
114 PrintFault(IoErr(), (CONST_STRPTR)"PathPart");
116 if (buf)
117 FreeVec(buf);
119 return rc;
121 AROS_SHCOMMAND_EXIT