home *** CD-ROM | disk | FTP | other *** search
- /* GetLength.e
- * Gets the length of the specified file and prints it or put it in an
- * environment variable.
- *
- * Public Domain by Diego Caravana.
- *
- *
- * Thanks to Wouter van Oortmerssen for his VERY GOOD work, hoping that he
- * will continue to improve E language !
- *
- * Hey, consider to learn E !!!
- *
- */
-
- /* TAB=4 */
-
- OPT OSVERSION=37
-
- MODULE 'dos/var', 'utility/tagitem'
-
- CONST ARGS_NUM=4, BUFLEN=11
- ENUM ARG_FILE, ARG_VAR, ARG_GLOBAL, ARG_STDOUT
- ENUM ERR_NONE, ERR_WRONGARGS, ERR_BADKEYS, ERR_NOVAR
-
- /* global definitions are cleared on entry */
- DEF args[ARGS_NUM]:ARRAY OF LONG, buf[BUFLEN]:ARRAY OF CHAR
-
- RAISE ERR_WRONGARGS IF ReadArgs()=NIL,
- ERR_NOVAR IF SetVar()=NIL
-
- PROC main() HANDLE
- DEF rdargs=NIL, len, fmt, fmtarg
-
- /* version string, as C= says... */
- VOID '$VER: GetLength 1.0 (10.02.93) by Diego Caravana'
-
- /* get user arguments from command line */
- rdargs := ReadArgs('FILENAME/A,VAR=VARIABLE/K,GLOBAL/S,STDOUT/S', args, 0)
-
- /* do not allow the use of VAR/GLOBAL and STDOUT keywords
- * because in this way the user can print the result directly on stdout
- * without being bored by env variables
- */
- IF ((StrLen(args[ARG_VAR]) <> 0) OR (args[ARG_GLOBAL]=-1)) AND
- (args[ARG_STDOUT]=-1) THEN Raise(ERR_BADKEYS)
-
- len:=FileLength(args[ARG_FILE])
-
- IF args[ARG_STDOUT]=-1 /* choose the way */
- WriteF('\d', len)
- ELSE
- /* default name for environment variable */
- IF StrLen(args[ARG_VAR]) = 0 THEN args[ARG_VAR] := 'GetLengthResult'
-
- /* example of inline assembler; see the RawDoFmt Autodoc */
- fmt:='%ld'; fmtarg:=[len]
- MOVE.L fmt,A0
- MOVE.L fmtarg,A1
- LEA.L stuffChar(PC),A2
- MOVE.L buf,A3
- MOVE.L 4,A6
- JSR -$20A(A6) /* RawDoFmt */
-
- SetVar(args[ARG_VAR], buf, StrLen(buf),
- IF args[ARG_GLOBAL]=-1 THEN GVF_GLOBAL_ONLY ELSE GVF_LOCAL_ONLY)
- ENDIF
-
- /* let's exit cleanly */
- Raise(ERR_NONE)
-
- /* needed for RawDoFmt() (put here bucause is unreachable by program flux) */
- stuffChar:
- MOVE.B D0,(A3)+
- RTS
-
- EXCEPT
- IF rdargs THEN FreeArgs(rdargs)
-
- SELECT exception
- CASE ERR_NONE; CleanUp(0)
- CASE ERR_BADKEYS
- WriteF('*** no simultaneous VAR/GLOBAL and STDOUT keywords.\n')
- CleanUp(10)
- DEFAULT
- PrintFault(IoErr(), '*** DOS Error')
- CleanUp(10)
- ENDSELECT
- ENDPROC
-