          <<< NOTED::DISK$NOTES7:[NOTES$LIBRARY_7OF4]HACKERS.NOTE;1 >>>
                               -< ** Hackers ** >-
================================================================================
Note 1765.7          show device/files from within an image               7 of 8
SSDEVO::BLAYLOCK "If at first you doubt,doubt agai" 127 lines  25-APR-1995 11:45
               -< Compile /stand=relaxed and link with /SYSEXE >-
--------------------------------------------------------------------------------


/*

        Jacket to demo/test device_files routine.

*/

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

#include <descrip.h>
#include <fscndef.h>
#include <starlet.h>
#include <ssdef.h>
#include <jpidef.h>
#include <lib$routines.h>
#include <str$routines.h>
#include <fiddef.h>

#define NUM_ELEMENTS (1000)

typedef struct dsc$descriptor DESCRIP ;
typedef struct fiddef FIDDEF ;

typedef struct __file_list
    {
    int pid ;
    FIDDEF fid ;
    } FILE_LIST ;

typedef struct __item_list_2
    {
    short lenght ;
    short type ;
    char * addr ;
    } item_list_2_t ;

static $DESCRIPTOR( sysdisk, "SYS$DISK:") ;
static $DESCRIPTOR( blank, "               " ) ;

int device_file_list( short channel, FILE_LIST *file_list, 
                int file_list_elements, int *elements_written );


main(int argc, char *argv[] ) 
{

    int status, actual_elements ;
    short channel ;
    FILE_LIST *file_list ;
    DESCRIP *disk_ptr, disk = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 },
        process_name = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0 },
        file_name = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0 } ;

    if (argc <= 1)
        disk_ptr = (DESCRIP *) &sysdisk ;
    else
        {
        disk_ptr = (DESCRIP *) &disk ;
        disk.dsc$w_length = strlen( argv[1] ) ;
        disk.dsc$a_pointer = argv[1] ;
        }


    file_list = malloc( NUM_ELEMENTS * sizeof(FILE_LIST)) ;

    if (file_list == NULL)
    {
        perror("Unable to allocate file list\n") ;
    }


    status = sys$assign( disk_ptr, &channel, 0, 0) ;

    if (!(status&1))
    {
        printf("Unable to assign channel") ;
        exit(status) ;
    }

    status = device_file_list( channel, file_list, NUM_ELEMENTS,
                    &actual_elements); 
    sys$dassgn( channel ) ;
    if (status&1)
    {
        int i, item_code = JPI$_PRCNAM, length, status2 ;
        FILE_LIST *flp = file_list ;
        item_list_2_t itmlst[] = {
                { 0, FSCN$_DIRECTORY, 0 },
                { 0, 0, 0 } } ;

        for( i = 0; i < actual_elements; i++ )
        {
            status2 = SS$_NORMAL ;
            if (flp->pid != 0)
                status2 = lib$getjpi( &item_code, &flp->pid, 0, 0, 
                                &process_name) ;
            else
                str$copy_dx( &process_name, &blank ) ;

            if (!(status2&1)) 
                str$copy_dx( &process_name, &blank ) ;

            lib$fid_to_name( disk_ptr, &flp->fid, &file_name) ;
            sys$filescan( &file_name, itmlst, 0, 0, 0) ;
            length = itmlst[0].addr - file_name.dsc$a_pointer ;
            length = file_name.dsc$w_length - length ;
            printf("%-15.*s  %08X %.*s\n",
                    process_name.dsc$w_length,
                    process_name.dsc$a_pointer,
                    flp->pid,
                    length,
                    itmlst[0].addr ) ;
            flp ++ ;
        }
    }
    else
    {
        printf("Error in getting elements from device\n") ;
    }
    free(file_list) ;
    exit(status) ;

}
