Re: program directory

new topic     » goto parent     » topic index » view thread      » older message » newer message

>> A question:
>> I have been wondering for a while now, how does one find out the
>> directory a program is in when it was run?  Not the directory it was run
>> from, given by current_dir() or the location of ex.exe given by
>> examining the command_line() (the program is not bound), but the
>> location of the .ex file itself.
>
>include misc.e
>constant cl = command_line ()
>sequence s
>s = reverse(cl[2])
>if find ('\\', s) then
>    s = s[find('\\',s)..length(s)]
>end if
>puts (1, "The directory of the .ex file or the .exe file (when bound) is: "
& reverse (s) & '\n')
>
>-- An explenation:
>-- Command line always returns at least a 2-element-sequence.
>-- { path_name_of_interpreter , path_name_of_code , ...}
>
>With bound files, where the path and filename of the interpreter is the
same as that of the code, the first two arguments are
>the same. With unbound file they are not. Most likely it will be something
like ..
>
>{ "c:\\euphoria\\bin\\ex.exe" , "c:\\projects\\progs\\frog.ex", ...}

When you run your code from an editor this is (most likely) true, however
when you type for example at the dosprompt: ex frog.ex it will be like:
{ "c:\\euphoria\\bin\\ex.exe", "frog.ex", ... }
So your routine will not show current path. The next routine should handle
this:

include file.e
include misc.e
constant cl = command_line ()
sequence s

if compare(cl[1], cl[2]) = 0 then      -- Bound-file: First 2 arguments are
the same
    s = reverse(cl[2])
else
    if (cl[2][1] = '\\') or ((length(cl[2]) > 1) and (cl[2][2] = ':'))
     -- Complete path specified as argument
        s = reverse(cl[2])
    else
        s = reverse(current_dir() & '\\' & cl[2])
    end if
end if
if find ('\\', s) then
    s = s[find('\\',s)..length(s)]
end if
puts (1, "The directory of the .ex file or the .exe file (when bound) is: "
& reverse (s) & '\n')


The whole thing is has been broken down in 4 different possibilities:
1. The program is bound. Euphoria's docs state here that both the 1st as the
2nd argument specify the full-path
2. The program is called as in: ex \euphoria\progs\frog. The full path is
thus given by the user.
3. The program is called as in: ex d:\euphoria\progs\frog (note the
drive-letter). The full path is thus given by the user.
4. The program is called as in: ex frog. Now we have to figure out what
directory it is in. Since it is accessible from the current-directory, I
take the current directory as the 'base-directory' and add the second
argument to it.

Suppose I call the program now like: ex ..\frog\frog.ex, while I'm in the
directory c:\euphoria\test. The  directory shown would now be:
c:\euphoria\test\..\frog\

I _know_ this is not a nice looking directory, but it is legal.

OK I can now think of another exception of calling an Euphoria program
(while or at a directory at the c-drive): ex d:frog.ex

It isn't handled correctly... yet.

>This is what 'pifs' are really for. You can tell windows for an
dos-executable what the 'target'-directory must be.
>This is the default directory that applies when opening files without any
path.

In Win yes, but it applies to DOS as well. Since you don't have to be IN the
directory to execute a program.

>> I am reluctant to specify an absolute path to the data file, after all
>> programs that force install into a particular directory kind of suck,
>> huh.  I can't tell my friend to change his system configuration either,

I'm glad you did install Euphoria anyway. When I installed Euphoria I could
just choose the drive to install it on, not the directory I wanted it
in.

> c:\>ex c:\euphoria\progs\frog.ex
>> as I suspect happens sometimes, well maybe not, but windows does this

Well it DOES happen sometimes... often... always...

Bye,

Martin Schut

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu