How do I check if a filename is valid?

Here's some code that should help. I found that you have to look at each return code to see if it's really an error. This routine does do syntax checking, it's just a little more complicated than before :^)

#define INCL_DOSFILEMGR
#define INCL_DOSERRORS
#include <os2.h>
#include <stdio.h>

int main( int argc, char **argv )
{
        int         rc;
        FILESTATUS3 piBuffer;

        if ( argc !=2 )
        {
                printf( "Must pass filename on command line!\n");
                return( -1 );
        }
        else
        {
                printf( "Checking on %s, ", argv[ 1 ] );
                rc = DosQueryPathInfo( (PSZ)argv[1], FIL_STANDARD, &piBuffer,
                                       sizeof(FILESTATUS3));

                if ( rc == 0 )
                        printf( "syntax valid and file exists\n" );
                else
                if ( rc == ERROR_FILE_NOT_FOUND )
                        printf( "syntax valid and file doesn't exist.\n");
                else
                if ( rc == ERROR_PATH_NOT_FOUND )
                        printf( "syntax valid, somthing in path was not found\n");
                else
                {
                        printf( "bad, rc=%d, ",rc );
                        switch( rc )
                        {
                                case ERROR_INVALID_DRIVE:
                                        printf( "drive name does not exist\n");
                                        break;

                                case ERROR_INVALID_NAME:
                                        printf( "invalid syntax for drive name\n");
                                        break;

                                case ERROR_FILENAME_EXCED_RANGE:
                                        printf( "dir name and/or filename too long\n");
                                        break;

                                case ERROR_SHARING_VIOLATION:
                                        printf( "sharing violation\n");
                                        break;

                                case ERROR_BUFFER_OVERFLOW:
                                        printf( "buffer overflow\n");
                                        break;

                                case ERROR_INVALID_LEVEL:
                                        printf( "invalid level requested\n");
                                        break;

                                case ERROR_INVALID_EA_NAME:
                                        printf( "invalid EA name\n");
                                        break;

                                case ERROR_EA_LIST_INCONSISTENT:
                                        printf( "EA list inconsistent\n");
                                        break;

                                default:
                                        printf("Undocumented return value.\n");
                        }
                        return( -1 );
            }
            return( 0 );
        }
}

Credit: Mike Brown


[Back: How do I start another session?]
[Next: Why should I use _beginthread instead of DosCreateThread?]