/* 
TRUENAME.C 
revised from version in first and second printings of UNDOCUMENTED DOS, pp.
190-191.

To use in DOS 4 and higher (which have an undocumented TRUENAME internal
command built into COMMAND.COM!), use .\TRUENAME rather than TRUENAME
*/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <dos.h>

#ifdef __TURBOC__
#define _asm asm
#endif

void ret(char *s, int retval) { puts(s); exit(retval); }

char far *truename(char far *s, char far *d)
{
    char far *s2;
    
    /* INT 21h AH=60h doesn't like leading or trailing blanks */
    while (isspace(*s))
        s++;
    s2 = s;
    while (*s2) s2++;
    s2--;
    while (isspace(*s2))
        *s2-- = 0;
    
    _asm push di
    _asm push si
    _asm les di, d
    _asm lds si, s
    _asm mov ah, 60h
    _asm int 21h
    _asm pop si
    _asm pop di
    _asm jc error
    return d;
error:
    return (char far *) 0;
}

main(int argc, char *argv[])
{
    char buf[128];
    if (argc < 2)
        ret("usage: truename <filename>", 1);
    if (_osmajor < 3)
        ret("requires DOS 3.0 or greater", 1);
    
    if (truename(argv[1], buf))
        ret(buf, 0);
    else
        ret("invalid filename", 1);
}
