FIGURE 1 FID_TEST.C #include stdio /* standard I/O */ #include rms /* loads RMS control block definitions */ #include fiddef /* loads FID structure definition */ #include dvidef /* loads $GETDVI definitions */ #include descrip /* loads VMS descriptor definitions */ #define DEV_NAME "FILE_DEV" /* logical for disk device */ #define FILE_NAME "TEST.DAT" /* name for file in header record */ #define TEST_REC "This is a test" /* text of test record */ /* some convenient VMS type definitions */ typedef unsigned char BYTE; typedef unsigned short WORD; /* define RMS structures as types */ typedef struct FAB RMS_FAB; typedef struct RAB RMS_RAB; typedef struct NAM RMS_NAM; typedef struct fiddef VMS_FID; /* define item list type */ typedef struct vms_itemlist_3 { WORD buf_len; WORD code; unsigned buffer; WORD *ret_len; } VMS_ITEMLIST_3; /* define IOSB type */ typedef struct vms_iosb { WORD code; WORD length; unsigned info; } VMS_IOSB; /****** variable declarations ******/ /* the RMS control blocks */ static RMS_FAB fab; static RMS_NAM nam; static RMS_RAB rab; /* buffer for reading back the test record */ static char read_buf[256]; /* saved FID and device info */ static VMS_FID sav_fid; static char sav_dvi[NAM$C_DVI]; /* $GETDVI data declarations */ static VMS_ITEMLIST_3 dvi_itm[2]; /* item list */ static VMS_IOSB dvi_iosb; /* IOSB */ static WORD dvi_len; /* return length */ static char dvi_str[64]; /* returned physical device name */ static $DESCRIPTOR(dev_d, DEV_NAME); /* device logical name */ /* descriptor and buffer for returned file name */ static char fname_buf[256]; static $DESCRIPTOR(fname_d, fname_buf); static WORD fname_len; main() { int sys_stat; /****** CREATE A PERMANENT TEMPORARY FILE *******/ /* set up item list for SYS$GETDVI and get physical name of device */ dvi_itm[0].code = DVI$_DEVNAM; dvi_itm[0].buf_len = sizeof(dvi_str); dvi_itm[0].buffer = &dvi_str; dvi_itm[0].ret_len = &dvi_len; dvi_itm[1].code = dvi_itm[1].buf_len = 0; sys_stat = sys$getdviw ( 0, /* efn */ 0, /* channel */ &dev_d, /* device name */ &dvi_itm, /* item list */ &dvi_iosb, /* IOSB */ 0, /* AST address */ 0, /* AST param */ 0); /* null argument */ if (!(sys_stat & 1)) {sys$exit(sys_stat);} if (!(dvi_iosb.code & 1)) {sys$exit(dvi_iosb.code);} /* cleanup and sanity check of device name length */ /* the trailing colon MUST be removed */ if ((dvi_len > 15) || (dvi_len == 0)) sys$exit(0); if (dvi_str[dvi_len - 1] = ':') dvi_len -= 1; /* set up FAB block */ fab = cc$rms_fab; /* initialize the FAB */ fab.fab$l_nam = &nam; /* link to NAM block */ fab.fab$l_fop = FAB$M_TMP /* specify temporary file */ | FAB$M_NAM; /* and NAM block input */ fab.fab$b_fac = FAB$M_PUT; /* write access */ fab.fab$l_fna = FILE_NAME; /* specify file name */ fab.fab$b_fns = strlen(FILE_NAME); /* set up NAM block */ nam = cc$rms_nam; /* initialize NAM block */ /* the nam$t_dvi field is a counted string: the first byte is the */ /* length of the device name string, which starts with the second byte */ memcpy(&nam.nam$t_dvi[1], dvi_str, /* specify dev name */ dvi_len); nam.nam$t_dvi[0] = dvi_len; /* and length */ /* set up RAB block */ rab = cc$rms_rab; /* initialize RAB block */ rab.rab$l_fab = &fab; /* link to FAB */ rab.rab$l_rbf = TEST_REC; /* point to test record */ rab.rab$w_rsz = strlen(TEST_REC); /* test record size */ /* create the file */ sys_stat = SYS$CREATE(&fab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /* display the file information */ printf("The file was created on %15s with FID %d,%d,%d\n", &nam.nam$t_dvi[1], nam.nam$w_fid[0], nam.nam$w_fid[1], nam.nam$w_fid[2]); /* save the FID and DVI information */ memcpy(&sav_fid, &nam.nam$w_fid, sizeof(sav_fid)); memcpy(&sav_dvi, &nam.nam$t_dvi, sizeof(sav_dvi)); /* connect the record stream */ sys_stat = SYS$CONNECT(&rab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /* write the test record */ sys_stat = SYS$PUT(&rab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /* close the file */ sys_stat = SYS$CLOSE(&fab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /********* OPEN THE FILE FOR READ ACCESS *******/ /* get and display the file name */ dev_d.dsc$a_pointer = &sav_dvi[1]; dev_d.dsc$w_length = sav_dvi[0]; sys_stat = LIB$FID_TO_NAME(&dev_d, &sav_fid, &fname_d, &fname_len); if (!(sys_stat & 1)) {sys$exit(sys_stat);} fname_buf[fname_len] = '\0'; printf("The returned file name is %s\n", fname_buf); /* set up FAB block */ fab = cc$rms_fab; /* initialize */ fab.fab$l_fop = FAB$M_NAM; /* specify NAM block input */ fab.fab$l_nam = &nam; /* link in NAM block */ fab.fab$b_fac = FAB$M_GET; /* read access */ /* set up NAM block */ nam = cc$rms_nam; /* initialize */ /* set up saved device and FID information */ memcpy(&nam.nam$w_fid, &sav_fid, sizeof(nam.nam$w_fid)); memcpy(&nam.nam$t_dvi, &sav_dvi, sizeof(nam.nam$t_dvi)); /* set up the RAB block */ rab = cc$rms_rab; /* initialize the RAB */ rab.rab$l_fab = &fab; /* link to FAB */ rab.rab$l_ubf = read_buf; /* point to read buffer */ rab.rab$w_usz = sizeof(read_buf) - 1; /* allow for terminating '\0' */ /* open the file */ sys_stat = SYS$OPEN(&fab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /* connect the record stream */ sys_stat = SYS$CONNECT(&rab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /* read a record */ sys_stat = SYS$GET(&rab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} read_buf[rab.rab$w_rsz] = '\0'; /* terminate the string */ printf("The record returned is: %s\n", read_buf); /* close the file */ sys_stat = SYS$CLOSE(&fab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} /******* DELETE THE FILE WITH THE $ERASE FUNCTION ******/ /* set up FAB block */ fab = cc$rms_fab; /* initialize */ fab.fab$l_fop = FAB$M_NAM; /* specify NAM block input */ fab.fab$l_nam = &nam; /* link in NAM block */ /* set up NAM block */ nam = cc$rms_nam; /* initialize */ /* set up saved device and FID information */ memcpy(&nam.nam$w_fid, &sav_fid, sizeof(nam.nam$w_fid)); memcpy(&nam.nam$t_dvi, &sav_dvi, sizeof(nam.nam$t_dvi)); /* delete the file */ sys_stat = SYS$ERASE(&fab, NULL, NULL); if (!(sys_stat & 1)) {sys$exit(sys_stat);} }