Added basic implementation of destroying a GPT table: just delete the
[AROS.git] / workbench / c / Filenote.c
blob44968a7c0c053b85c09024ad9b50b3a9b2588081
1 /*
2 Copyright © 1995-2011 The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Filenote CLI command
6 Lang: english
7 */
9 /*****************************************************************************
11 NAME
13 Filenote
15 SYNOPSIS
17 FILE/A,COMMENT,ALL/S,QUIET/S
19 LOCATION
23 FUNCTION
25 Add a comment to a file or directory.
27 Filenote allows a recursive scan of all directories adding comments
28 to each file/directory it finds that matches the file pattern
29 specified.
31 INPUTS
33 FILE - Always has to be specified. Can be either a filename with
34 a full path or a file pattern that is to be matched.
36 COMMENT - The string that is to be added as a comment to the
37 file(s)/dir(s) specified. If no comment is specified, any
38 existing comment is deleted.
40 To provide a comment that has embedded quotation marks,
41 precede each quote with an asterisk.
43 i.e. Filenote FILE=RAM:test.txt COMMENT=*"hello*"
45 ALL - Boolean switch. If specified, Filenote scans the directories
46 that match the pattern specified, recursively.
48 QUIET - Boolean switch. If specified, no diagnostic text will be
49 displayed to stdout.
51 RESULT
53 Standard DOS return codes.
55 NOTES
57 Output from AROS' Filenote is more neat and structured than the
58 standard Filenote command.
60 Does not yet support multi-assigns.
62 EXAMPLE
64 Filenote ram: hello all
66 Recurses through each directory in RAM: adding "hello" as a
67 filenote to each file/directory.
69 BUGS
71 SEE ALSO
73 dos.library/SetComment()
75 INTERNALS
77 ******************************************************************************/
79 #include <proto/arossupport.h>
80 #include <proto/dos.h>
81 #include <proto/exec.h>
83 #include <dos/dos.h>
84 #include <dos/dosasl.h>
85 #include <dos/dosextens.h>
86 #include <dos/rdargs.h>
87 #include <exec/memory.h>
88 #include <utility/utility.h>
90 #include <ctype.h>
92 #include <aros/rt.h>
94 #define ERROR_HEADER "Filenote"
96 #define ARG_TEMPLATE "FILE/A,COMMENT,ALL/S,QUIET/S"
97 #define ARG_FILE 0
98 #define ARG_COMMENT 1
99 #define ARG_ALL 2
100 #define ARG_QUIET 3
101 #define TOTAL_ARGS 4
103 /* To define whether a command line switch was set or not.
105 #define NOT_SET 0
107 #define MAX_PATH_LEN 512
109 const TEXT version[] = "$VER: Filenote 41.2 (30.12.2011)\n";
111 int Do_Filenote(struct AnchorPath *, STRPTR, STRPTR, LONG, LONG);
113 int __nocommandline;
115 int main(void)
117 struct RDArgs * rda;
118 struct AnchorPath * apath;
119 IPTR args[TOTAL_ARGS] = { 0, (IPTR)"", 0, 0};
120 int Return_Value;
122 RT_Init();
124 Return_Value = RETURN_OK;
126 apath = AllocVec(sizeof(struct AnchorPath) + MAX_PATH_LEN,
127 MEMF_ANY | MEMF_CLEAR);
128 if (apath)
130 /* Make sure DOS knows the buffer size.
132 apath->ap_Flags = APF_DOWILD | APF_FollowHLinks;
133 apath->ap_Strlen = MAX_PATH_LEN;
134 apath->ap_BreakBits = 0;
135 apath->ap_FoundBreak = 0;
137 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
138 if (rda)
140 Return_Value = Do_Filenote(apath,
141 (STRPTR)args[ARG_FILE],
142 (STRPTR)args[ARG_COMMENT],
143 (LONG)args[ARG_ALL],
144 (LONG)args[ARG_QUIET]
146 FreeArgs(rda);
148 else
150 PrintFault(IoErr(), ERROR_HEADER);
152 Return_Value = RETURN_ERROR;
155 else
157 Return_Value = RETURN_FAIL;
160 FreeVec(apath);
162 RT_Exit();
164 return (Return_Value);
166 } /* main */
169 /* Defines whether MatchFirst(), etc has matched a file.
171 #define MATCHED_FILE 0
173 void PrintFileName(struct AnchorPath *, LONG);
174 int SafeSetFileComment(struct AnchorPath *, char *);
176 int Do_Filenote(struct AnchorPath *a,
177 STRPTR File,
178 STRPTR Comment,
179 LONG All,
180 LONG Quiet)
182 LONG Result,
183 TabValue;
184 int Return_Value;
186 Return_Value = RETURN_OK;
187 TabValue = 0L;
189 /* Looks to see if the user has given a volume name as a pattern
190 * without the all switch set. If so, we fail.
195 IsDosEntryA((char *)File, LDF_VOLUMES | LDF_DEVICES) == TRUE
197 All == NOT_SET
200 PrintFault(ERROR_OBJECT_WRONG_TYPE, ERROR_HEADER);
201 Return_Value = RETURN_FAIL;
203 else
205 if (Return_Value != RETURN_FAIL)
207 Result = MatchFirst(File, a);
209 if (Result == MATCHED_FILE)
213 if (All == NOT_SET)
215 Return_Value = SafeSetFileComment(a, Comment);
216 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
218 PrintFileName(a, TabValue);
221 else
223 /* Allow a recursive scan.
226 if (a->ap_Info.fib_DirEntryType > 0)
228 /* Enter directory.
230 if (!(a->ap_Flags & APF_DIDDIR))
232 a->ap_Flags |= APF_DODIR;
234 Return_Value = SafeSetFileComment(a, Comment);
236 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
238 PrintFileName(a, TabValue);
240 TabValue++;
242 else
244 /* Leave directory.
246 a->ap_Flags &= ~APF_DIDDIR;
247 TabValue--;
250 else
252 Return_Value = SafeSetFileComment(a, Comment);
254 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
256 PrintFileName(a, TabValue);
261 while
263 ((Result = MatchNext(a)) == MATCHED_FILE)
265 Return_Value != RETURN_FAIL
268 else
270 PrintFault(IoErr(), ERROR_HEADER);
271 Return_Value = RETURN_FAIL;
274 MatchEnd(a);
278 return (Return_Value);
280 } /* Do_Filenote */
283 void PrintFileName(struct AnchorPath *a, LONG t)
285 int i;
286 IPTR args[2];
288 args[0] = (IPTR)FilePart(&a->ap_Buf[0]);
289 args[1] = (IPTR)NULL;
291 VPrintf(" ", NULL);
293 for (i = 0; i != t; i++)
295 VPrintf(" ", NULL);
298 VPrintf("%s", args);
300 if (a->ap_Info.fib_DirEntryType > 0)
302 VPrintf(" (dir)", NULL);
305 VPrintf("...Done\n", NULL);
307 } /* PrintFileName */
310 int SafeSetFileComment(struct AnchorPath *a, char *c)
312 int Return_Value;
314 Return_Value = RETURN_OK;
316 if (!SetComment(a->ap_Buf, c))
318 PrintFault(IoErr(), ERROR_HEADER);
319 Return_Value = RETURN_WARN;
322 return(Return_Value);
324 } /* SafeSetFileComment */