Better error handling.
[AROS.git] / workbench / c / SetDate.c
blob78d25abd2e884568caddf7d606d068885ce573b1
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: SetDate CLI command
6 Lang: English
7 */
9 /*************************************************************************
11 NAME
13 SetDate
15 FORMAT
17 SetDate (file | pattern) [(weekday)] [(date)] [(time)] [ALL]
19 SYNOPSIS
21 FILE/A,WEEKDAY,DATE,TIME,ALL/S
23 LOCATION
27 FUNCTION
29 Changes the date and time of the creation or last change of a file or
30 directory. With option ALL, it also changes the date and time of all
31 files and subdirectories within directories matching the specified
32 pattern. If either the date or time is unspecified, the current date
33 or time is used.
35 INPUTS
37 FILE -- File (or pattern) to change the date of.
39 WEEKDAY -- Specification of the day of the date. This is locale
40 sensitive, and you may use standard keywords such as
41 'Tomorrow' and 'Yesterday' (in the language used, of
42 course).
44 DATE -- A date in the format DD-MMM-YY.
45 MMM is either the number or the first 3 letters of the
46 month in English.
48 TIME -- Time string in the format HH:MM:SS or HH:MM.
50 ALL -- Recurse through subdirectories.
52 RESULT
54 Standard DOS return codes
56 NOTES
58 EXAMPLE
60 SetDate #? ALL
62 Sets the date for all files and directories in the current directory
63 and its subdirectories to the current date.
65 BUGS
69 SEE ALSO
71 Date
73 INTERNALS
75 *************************************************************************/
78 #include <proto/exec.h>
79 #include <dos/datetime.h>
80 #include <proto/dos.h>
81 #include <dos/rdargs.h>
82 #include <dos/dosasl.h>
84 #define MAX_PATH_LEN 512
86 const TEXT version[] = "$VER: SetDate 1.1 (3.4.2014)\n";
88 enum { ARG_FILE = 0, ARG_WEEKDAY, ARG_DATE, ARG_TIME, ARG_ALL };
90 int __nocommandline;
92 int main(void)
94 struct AnchorPath *aPath;
95 struct RDArgs *rda;
96 IPTR args[5] = { (IPTR)NULL, (IPTR)NULL, (IPTR)NULL, (IPTR)NULL, (IPTR)FALSE };
97 struct DateTime dt;
98 LONG error = 0;
99 BPTR oldCurDir;
100 LONG retval = RETURN_OK;
101 BOOL timeError = FALSE; /* Error in time/date specification? */
103 rda = ReadArgs("FILE/A,WEEKDAY,DATE,TIME,ALL/S", args, NULL);
105 if(rda == NULL)
107 PrintFault(IoErr(), "SetDate");
108 return RETURN_FAIL;
111 /* Use the current time as default (if no DATE, TIME or WEEKDAY is
112 defined) */
113 DateStamp(&dt.dat_Stamp);
115 dt.dat_Flags = DTF_FUTURE;
116 dt.dat_Format = FORMAT_DOS;
117 dt.dat_StrDate = (TEXT *)args[ARG_DATE];
118 dt.dat_StrTime = (TEXT *)args[ARG_TIME];
120 /* Change the defaults according to the user's specifications */
121 if(StrToDate(&dt))
123 dt.dat_StrDate = (TEXT *)args[ARG_WEEKDAY];
125 if(!StrToDate(&dt))
127 timeError = TRUE;
130 else
132 timeError = TRUE;
135 if(timeError)
137 PutStr("SetDate: Illegal DATE or TIME string\n");
138 return RETURN_FAIL;
141 aPath = AllocVec(sizeof(struct AnchorPath) + MAX_PATH_LEN,
142 MEMF_ANY | MEMF_CLEAR);
144 if (aPath != NULL)
146 aPath->ap_Flags = (BOOL)args[ARG_ALL] ? (APF_DODIR | APF_DOWILD) : APF_DOWILD;
147 aPath->ap_BreakBits = SIGBREAKF_CTRL_C;
148 aPath->ap_Strlen = MAX_PATH_LEN;
150 /* Save the current dir */
151 oldCurDir = CurrentDir(BNULL);
152 CurrentDir(oldCurDir);
154 error = MatchFirst((STRPTR)args[ARG_FILE], aPath);
156 while(error == 0)
158 CurrentDir(aPath->ap_Current->an_Lock);
160 //VPrintf("%s\n", (IPTR *)&aPath->ap_Info.fib_FileName);
161 if ( ((&aPath->ap_Info)->fib_DirEntryType >= 0)
162 && !(aPath->ap_Flags & APF_DIDDIR)
163 && ((BOOL)args[ARG_ALL]))
165 aPath->ap_Flags |= APF_DODIR;
167 SetFileDate(aPath->ap_Info.fib_FileName, &dt.dat_Stamp);
169 error = MatchNext(aPath);
172 MatchEnd(aPath);
174 /* Restore the current dir */
175 CurrentDir(oldCurDir);
177 FreeArgs(rda);
178 FreeVec(aPath);
180 else
182 retval = RETURN_FAIL;
185 if(error != ERROR_NO_MORE_ENTRIES)
187 if(error == ERROR_BREAK)
189 retval = RETURN_WARN;
191 else
193 retval = RETURN_FAIL;
196 PrintFault(error, "SetDate");
199 return retval;