Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.

21.1 Any AmigaDOS

This first example works with any AmigaDOS. The first thing that is done is the assignment of wbmessage to a correctly typed pointer. At the same time we can check to see if it is NIL (i.e., whether the program was started from Workbench or not). If it was not started from Workbench the arguments in arg are printed. Otherwise we need to use the fact that wbmessage is really a pointer to a wbstartup object (defined in module `workbench/startup'), so we can get at the argument list. Then for each argument in the list we need to check the lock supplied with the argument. If it's a proper lock it will be a lock on the directory containing the argument file. The name in the argument is just a filename, not a complete path, so to read the file we need to change the current directory to the lock directory. Once we've got a valid lock and we've changed directory to there, we can find the length of the file (using FileLength) and print it. If there was no lock or the file did not exist, the name of the file and an appropriate error message is printed.

MODULE 'workbench/startup'

PROC main()
  DEF startup:PTR TO wbstartup, args:PTR TO wbarg, i, oldlock, len
  IF (startup:=wbmessage)=NIL
    WriteF('Started from Shell/CLI\n   Arguments: "\s"\n', arg)
    WriteF('Started from Workbench\n')
    FOR i:=1 TO startup.numargs  /* Loop through the arguments */
      IF args[].lock=NIL
        WriteF('  Argument \d: "\s" (no lock)\n', i, args[].name)
        len:=FileLength(args[].name)  /* Do something with file */
        IF len=-1
          WriteF('  Argument \d: "\s" (file does not exist)\n',
                 i, args[].name)
          WriteF('  Argument \d: "\s", file length is \d bytes\n',
                 i, args[].name, len)
        CurrentDir(oldlock) /* Important: restore current dir */

When you run this program you'll notice a slight difference between arg and the Workbench message: arg does not contain the program name, just the arguments, whereas the first argument in the Workbench argument list is the program. You can simply ignore the first Workbench argument in the list if you want.

Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.