Welcome, Guest. Please login or register.

Author Topic: Using Execute() in C.  (Read 3084 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Using Execute() in C.
« on: March 23, 2004, 07:44:15 AM »
Fixed the bugs pointed out by CodeSmith and nueron. Also fixed a bug that would occur with filename with spaces.
Code: [Select]

#include <exec/types.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include &quot;assembler.h&quot;

int assemble(char *file_name)
{
    struct Library *DOSBase = NULL;

    if (!(DOSBase = OpenLibrary(DOSNAME, 33)))
    {
        printf(&quot;Unable to open dos.library\n&quot;);
        return FALSE;
    }
    else
    {
        char *command = NULL;
        char *assembler_name = &quot;PhxAss&quot;;
        char *output_file_name = &quot;file.err&quot;;
        BPTR output_file = NULL;

        if (!(command = (char *)malloc(strlen(assembler_name) + strlen(file_name) + 4)))
        {
            printf(&quot;Out of memory\n&quot;);
            return FALSE;
        }

        strcpy(command, assembler_name);
        strcat(command, &quot; \&quot;&quot;);
        strcat(command, file_name);
        strcat(command, &quot;\&quot;&quot;);

        output_file = Open(output_file_name, MODE_NEWFILE);

        printf(&quot;command <%s>\n&quot;, command);

        Execute(command, NULL, output_file);    /* Call the assembler. */

        if (output_file)
        {
            Close(output_file);
        }

        CloseLibrary(DOSBase);
    }

    return TRUE;
}
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Using Execute() in C.
« Reply #1 on: March 24, 2004, 10:16:05 AM »
@Noster
Quote
You might enlarge your command-buffer "command" by one and add a "strcat(command, "\n")" to your code.

No, the command need not be newline terminated for Execute().

@Sidewinder

If you enable "AmigaDOS Functions/Lock" in SnoopDos you will see PhxAss Lock()s the source file before it Open() it. So if the Lock()ing fails, no Open() will ever happen.

Execute() doesn't interherit the current directory of the parent calling it, so you need to pass absolute path to sourcefile.

There are several ways to do this: one is to Lock() the sourcefile, NameFromLock() the full path the file. Another is to add a temporary assign to the sourcedir, and use this assign + filename with phxass.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Using Execute() in C.
« Reply #2 on: March 25, 2004, 10:24:31 AM »
@Noster
Quote
Of cause, I know that Execute() doesn't requires a newline terminated command string, but I've thought: "Who knows how PhxAss parses its arguments ?"
Bad written programs may not use the C-style argument parsing or the AmigaDOS ReadArgs() command for parsing their arguments, instead looking for a terminating linefeed to find the end of the command line.

Ok.

Execute() adds the linefeed however. :-)