Hint added.
[AROS.git] / workbench / c / Filenote.c
blob020601125c944a344b3e1b1f909c3aa19f662208
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 #ifdef __AROS__
80 #include <proto/arossupport.h>
81 #endif
82 #include <proto/dos.h>
83 #include <proto/exec.h>
85 #include <dos/dos.h>
86 #include <dos/dosasl.h>
87 #include <dos/dosextens.h>
88 #include <dos/rdargs.h>
89 #include <exec/memory.h>
90 #include <utility/utility.h>
92 #include <ctype.h>
93 #include <string.h>
95 #ifdef __AROS__
96 #include <aros/rt.h>
97 #else
98 #define RT_Init()
99 #define RT_Exit()
100 #define IsDosEntryA(file, flags) 0
101 #endif
103 #define ERROR_HEADER "Filenote"
105 #define ARG_TEMPLATE "FILE/A,COMMENT,ALL/S,QUIET/S"
106 #define ARG_FILE 0
107 #define ARG_COMMENT 1
108 #define ARG_ALL 2
109 #define ARG_QUIET 3
110 #define TOTAL_ARGS 4
112 /* To define whether a command line switch was set or not.
114 #define NOT_SET 0
116 #define MAX_PATH_LEN 512
118 const TEXT version[] = "$VER: Filenote 41.3 (7.5.2015)\n";
120 int Do_Filenote(struct AnchorPath *, STRPTR, STRPTR, LONG, LONG);
122 int __nocommandline;
124 int main(void)
126 struct RDArgs * rda;
127 struct AnchorPath * apath;
128 IPTR args[TOTAL_ARGS] = { 0, (IPTR)"", 0, 0};
129 int Return_Value;
131 RT_Init();
133 Return_Value = RETURN_OK;
135 apath = AllocVec(sizeof(struct AnchorPath) + MAX_PATH_LEN,
136 MEMF_ANY | MEMF_CLEAR);
137 if (apath)
139 /* Make sure DOS knows the buffer size.
141 apath->ap_Flags = APF_DOWILD | APF_FollowHLinks;
142 apath->ap_Strlen = MAX_PATH_LEN;
143 apath->ap_BreakBits = 0;
144 apath->ap_FoundBreak = 0;
146 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
147 if (rda)
149 Return_Value = Do_Filenote(apath,
150 (STRPTR)args[ARG_FILE],
151 (STRPTR)args[ARG_COMMENT],
152 (LONG)args[ARG_ALL],
153 (LONG)args[ARG_QUIET]
155 FreeArgs(rda);
157 else
159 PrintFault(IoErr(), ERROR_HEADER);
161 Return_Value = RETURN_ERROR;
164 else
166 Return_Value = RETURN_FAIL;
169 FreeVec(apath);
171 RT_Exit();
173 return Return_Value;
175 } /* main */
178 /* Defines whether MatchFirst(), etc has matched a file.
180 #define MATCHED_FILE 0
182 void PrintFileName(struct AnchorPath *, LONG);
183 int SafeSetFileComment(struct AnchorPath *, char *);
185 int Do_Filenote(struct AnchorPath *a,
186 STRPTR File,
187 STRPTR Comment,
188 LONG All,
189 LONG Quiet)
191 LONG Result,
192 TabValue;
193 int Return_Value;
195 Return_Value = RETURN_OK;
196 TabValue = 0L;
198 /* Looks to see if the user has given a volume name as a pattern
199 * without the all switch set. If so, we fail.
204 IsDosEntryA((char *)File, LDF_VOLUMES | LDF_DEVICES) == TRUE
206 All == NOT_SET
209 PrintFault(ERROR_OBJECT_WRONG_TYPE, ERROR_HEADER);
210 Return_Value = RETURN_FAIL;
212 else
214 if (Return_Value != RETURN_FAIL)
216 Result = MatchFirst(File, a);
218 if (Result == MATCHED_FILE)
222 if (All == NOT_SET)
224 Return_Value = SafeSetFileComment(a, Comment);
225 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
227 PrintFileName(a, TabValue);
230 else
232 /* Allow a recursive scan.
235 if (a->ap_Info.fib_DirEntryType > 0)
237 /* Enter directory.
239 if (!(a->ap_Flags & APF_DIDDIR))
241 a->ap_Flags |= APF_DODIR;
243 Return_Value = SafeSetFileComment(a, Comment);
245 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
247 PrintFileName(a, TabValue);
249 TabValue++;
251 else
253 /* Leave directory.
255 a->ap_Flags &= ~APF_DIDDIR;
256 TabValue--;
259 else
261 Return_Value = SafeSetFileComment(a, Comment);
263 if ((Quiet == NOT_SET) && (Return_Value == RETURN_OK))
265 PrintFileName(a, TabValue);
270 while
272 ((Result = MatchNext(a)) == MATCHED_FILE)
274 Return_Value != RETURN_FAIL
277 else
279 PrintFault(IoErr(), ERROR_HEADER);
280 Return_Value = RETURN_FAIL;
283 MatchEnd(a);
287 return Return_Value;
289 } /* Do_Filenote */
292 void PrintFileName(struct AnchorPath *a, LONG t)
294 int i;
296 Printf(" ");
298 for (i = 0; i != t; i++)
300 Printf(" ");
303 Printf("%s", FilePart(&a->ap_Buf[0]));
305 if (a->ap_Info.fib_DirEntryType > 0)
307 Printf(" (dir)");
310 Printf("...Done\n");
312 } /* PrintFileName */
315 int SafeSetFileComment(struct AnchorPath *a, char *c)
317 int Return_Value;
319 Return_Value = RETURN_OK;
321 if (!SetComment(a->ap_Buf, c))
323 PrintFault(IoErr(), ERROR_HEADER);
324 Return_Value = RETURN_ERROR;
326 else
327 if (strlen(c) > 79)
329 VPrintf("Note truncated to 79 characters\n", NULL);
330 Return_Value = RETURN_WARN;
333 return Return_Value;
335 } /* SafeSetFileComment */