added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / c / Filenote.c
blob584d83882950d156b887f3c0edd06c4fe19760cb
1 /*
2 Copyright © 1995-2007, 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
21 Sys:C
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 HISTORY
79 27-Jul-1997 laguest Initial inclusion into the AROS tree
81 ******************************************************************************/
83 #include <proto/arossupport.h>
84 #include <proto/dos.h>
85 #include <proto/exec.h>
87 #include <dos/dos.h>
88 #include <dos/dosasl.h>
89 #include <dos/dosextens.h>
90 #include <dos/rdargs.h>
91 #include <exec/memory.h>
92 #include <utility/utility.h>
94 #include <ctype.h>
96 #include <aros/rt.h>
98 #define ERROR_HEADER "Filenote"
100 #define ARG_TEMPLATE "FILE/A,COMMENT,ALL/S,QUIET/S"
101 #define ARG_FILE 0
102 #define ARG_COMMENT 1
103 #define ARG_ALL 2
104 #define ARG_QUIET 3
105 #define TOTAL_ARGS 4
107 /* To define whether a command line switch was set or not.
109 #define NOT_SET 0
111 #define MAX_PATH_LEN 512
113 const TEXT version[] = "$VER: Filenote 41.1 (29.8.1998)\n";
115 int Do_Filenote(struct AnchorPath *, STRPTR, STRPTR, LONG, LONG);
117 int __nocommandline;
119 int main(void)
121 struct RDArgs * rda;
122 struct AnchorPath * apath;
123 IPTR args[TOTAL_ARGS] = { NULL, NULL, NULL, NULL};
124 int Return_Value;
126 RT_Init();
128 Return_Value = RETURN_OK;
130 apath = AllocVec(sizeof(struct AnchorPath) + MAX_PATH_LEN,
131 MEMF_ANY | MEMF_CLEAR);
132 if (apath)
134 /* Make sure DOS knows the buffer size.
136 apath->ap_Flags = APF_DOWILD | APF_FollowHLinks;
137 apath->ap_Strlen = MAX_PATH_LEN;
138 apath->ap_BreakBits = 0;
139 apath->ap_FoundBreak = 0;
141 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
142 if (rda)
144 Return_Value = Do_Filenote(apath,
145 (STRPTR)args[ARG_FILE],
146 (STRPTR)args[ARG_COMMENT],
147 (LONG)args[ARG_ALL],
148 (LONG)args[ARG_QUIET]
150 FreeArgs(rda);
152 else
154 PrintFault(IoErr(), ERROR_HEADER);
156 Return_Value = RETURN_ERROR;
159 else
161 Return_Value = RETURN_FAIL;
164 FreeVec(apath);
166 RT_Exit();
168 return (Return_Value);
170 } /* main */
173 /* Defines whether MatchFirst(), etc has matched a file.
175 #define MATCHED_FILE 0
177 void PrintFileName(struct AnchorPath *, LONG);
178 int SafeSetFileComment(struct AnchorPath *, char *);
180 int Do_Filenote(struct AnchorPath *a,
181 STRPTR File,
182 STRPTR Comment,
183 LONG All,
184 LONG Quiet)
186 LONG Result,
187 TabValue;
188 int Return_Value;
190 Return_Value = RETURN_OK;
191 TabValue = 0L;
193 /* Looks to see if the user has given a volume name as a pattern
194 * without the all switch set. If so, we fail.
199 IsDosEntryA((char *)File, LDF_VOLUMES | LDF_DEVICES) == TRUE
201 All == NOT_SET
204 PrintFault(ERROR_OBJECT_WRONG_TYPE, ERROR_HEADER);
205 Return_Value = RETURN_FAIL;
207 else
209 if (Return_Value != RETURN_FAIL)
211 Result = MatchFirst(File, a);
213 if (Result == MATCHED_FILE)
217 if (All == NOT_SET)
219 Return_Value = SafeSetFileComment(a, Comment);
221 if (Quiet == NOT_SET)
223 PrintFileName(a, TabValue);
226 else
228 /* Allow a recursive scan.
231 if (a->ap_Info.fib_DirEntryType > 0)
233 /* Enter directory.
235 if (!(a->ap_Flags & APF_DIDDIR))
237 a->ap_Flags |= APF_DODIR;
239 Return_Value = SafeSetFileComment(a, Comment);
241 if (Quiet == NOT_SET)
243 PrintFileName(a, TabValue);
245 TabValue++;
247 else
249 /* Leave directory.
251 a->ap_Flags &= ~APF_DIDDIR;
252 TabValue--;
255 else
257 Return_Value = SafeSetFileComment(a, Comment);
259 if (Quiet == NOT_SET)
261 PrintFileName(a, TabValue);
266 while
268 ((Result = MatchNext(a)) == MATCHED_FILE)
270 Return_Value != RETURN_FAIL
273 else
275 PrintFault(IoErr(), ERROR_HEADER);
276 Return_Value = RETURN_FAIL;
279 MatchEnd(a);
283 return (Return_Value);
285 } /* Do_Filenote */
288 void PrintFileName(struct AnchorPath *a, LONG t)
290 int i;
291 IPTR args[2];
293 args[0] = (IPTR)FilePart(&a->ap_Buf[0]);
294 args[1] = (IPTR)NULL;
296 VPrintf(" ", NULL);
298 for (i = 0; i != t; i++)
300 VPrintf(" ", NULL);
303 VPrintf("%s", args);
305 if (a->ap_Info.fib_DirEntryType > 0)
307 VPrintf(" (dir)", NULL);
310 VPrintf("...Done\n", NULL);
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 Return_Value = RETURN_WARN;
326 return(Return_Value);
328 } /* SafeSetFileComment */