Retrieve application filename

C and PASCAL (or any other high-level languages) in here please

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
Arne
Captain Atari
Captain Atari
Posts: 437
Joined: Thu Nov 01, 2007 10:01 am

Retrieve application filename

Postby Arne » Mon May 14, 2018 1:34 pm

Got a problem retrieving the filename of my own application at runtime. As argv[0] is empty under TOS I tried to get the filename via DTA:

Code: Select all

DTA      myDTA;

memcpy(&myDTA, Fgetdta(), sizeof(DTA));
printf(">%s<\n\r", myDTA.d_fname);

I'm using Pure C 1.1 and if I enter the debugger via Alt-D I can see the filename in d_fname. But if I start the program via Alt-R (Run) then d_fname is filled with 0-Bytes and no filename is displayed. Same if I call the application from desktop.
Something wrong with checking DTA or is there a more suitable way of getting the filename?

thanx, Arne
Image

joska
Hardware Guru
Hardware Guru
Posts: 3991
Joined: Tue Oct 30, 2007 2:55 pm
Location: Florø, Norway
Contact:

Re: Retrieve application filename

Postby joska » Mon May 14, 2018 1:51 pm

I guess argv[0] is unused under TOS simply because there is no way to get this information. The default DTA points to the command line string in the basepage, and there is no program name in the command line string under plain TOS.
Jo Even

Firebee - Falcon060 - Milan060 - Falcon040 - MIST - Mega ST - STM - STE - Amiga 600 - Sharp MZ700 - MSX - Amstrad CPC - C64

ThorstenOtto
Captain Atari
Captain Atari
Posts: 295
Joined: Sun Aug 03, 2014 5:54 pm

Re: Retrieve application filename

Postby ThorstenOtto » Mon May 14, 2018 2:50 pm

Arne wrote:tried to get the filename via DTA:


What makes you think that the current DTA contains the filename of your app? If at all, it is the desktop that will use Fsfirst(), but it might at well use Dopendir(). And even if it is using Fsfirst(), you won't be able to retrieve the DTA of the desktop; the one you get in your program is different.

is there a more suitable way of getting the filename?


If your program file has been renamed, then there is no reliable way. It only works when the desktop (and your startup code) support the ARGV method.

BTW when using PD, you might be fooled, because the desktop might have used the ARGV environment, but neither PC nor PD use it when starting your program, so you may end up with reading the environment that was set up by the desktop to start PC, not your program.

gstoll
Atari freak
Atari freak
Posts: 65
Joined: Sun Feb 17, 2008 4:29 pm

Re: Retrieve application filename

Postby gstoll » Mon May 14, 2018 4:41 pm

Arne wrote:Something wrong with checking DTA or is there a more suitable way of getting the filename?

Only with Fcntl ( PLOADINFO )

In the text from Julian F. Reschke "Atarium 82, November 1994, Identitätsfragen" (ST-Computer 11/1994) are some info.

Code: Select all

/*
    @(#)atarium/pinfo.c
   
    Julian F. Reschke, 31. September 1994
*/

#include <stddef.h>
#include <stdio.h>
#include <tos.h>

#ifndef PLOADINFO
#define PLOADINFO   (('P'<< 8) | 12)
#endif

struct ploadinfo {
    /* passed */
    short fnamelen;
    /* returned */
    char *cmdlin, *fname;
};

int
main (void)
{
    struct ploadinfo pl;
    char fname[PATH_MAX];
    char cmdlin[PATH_MAX];
    long handle;
    long ret;

    pl.fnamelen = sizeof (fname);
    pl.cmdlin = cmdlin;
    pl.fname = fname;
   
    handle = Fopen ("u:\\proc\\x.-1", 0);
   
    if (handle < 0) return 1;
   
    ret = Fcntl ((int) handle, (long) &pl, PLOADINFO);
   
    Fclose ((int) handle);
   
    if (ret == 0)
        printf ("Dateiname: `%s'\n", pl.fname);
   
    return 0;       
}

joska
Hardware Guru
Hardware Guru
Posts: 3991
Joined: Tue Oct 30, 2007 2:55 pm
Location: Florø, Norway
Contact:

Re: Retrieve application filename

Postby joska » Mon May 14, 2018 5:42 pm

This only works with MiNT and MagiC. In that case you can find the program name on the commandline too.
Jo Even

Firebee - Falcon060 - Milan060 - Falcon040 - MIST - Mega ST - STM - STE - Amiga 600 - Sharp MZ700 - MSX - Amstrad CPC - C64

User avatar
Arne
Captain Atari
Captain Atari
Posts: 437
Joined: Thu Nov 01, 2007 10:01 am

Re: Retrieve application filename

Postby Arne » Tue May 15, 2018 10:20 am

Thanks to all of you. My application shall be run from the AUTO folder, so the advised methods won't work for me.
I think I have found a solution for my problem (not getting the filename, but the underlying problem).
Anyway: I do add a cookie but the cookie's value (uint32_t) is too small to take all the infos. So I put my stuff into a malloc'ed area (overlaying struct) and take the value field of the cookie as a pointer to that malloc'ed space. I do only one malloc in my program. If my program exits TOS frees all malloc'ed space. Of course I want my space to survive. Is it advised to terminate my program with

Code: Select all

Ptermres(0, 0)
?

thanks, Arne
Image

ThorstenOtto
Captain Atari
Captain Atari
Posts: 295
Joined: Sun Aug 03, 2014 5:54 pm

Re: Retrieve application filename

Postby ThorstenOtto » Tue May 15, 2018 11:16 am

Yes, you should terminate your program with Ptermres() then, but of course not with a keepcnt (the first parameter) of 0.

I do only one malloc in my program.


When adding a cookie, you should also be prepared to enlarge the cookie jar, which involves mallocing a new area, and copying the old contents. Or at least check that the cookie fits in the jar, and abort otherwise (there are already quite some programs around that will enlarge the jar).


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 2 guests