Everhart, Glenn From: Richard B. Gilbert [76702.1567@compuserve.com] Sent: Monday, January 11, 1999 8:10 AM To: Info-VAX@Mvb.Saic.Com Subject: is there a vms command like unix command strings? Message text written by INTERNET:tdai@cn.oracle.com >is there a vms command like unix command strings? < No, but there is a "strings" program available. You need a C compiler to build it. /*---------------------------------------------------------------------- * * Strings.C -- finds strings in a file (' ' <= c <= '~') * * Usage: strings [-hnrt] [-wn] file ... * -h no filename headers * -n newline instead of space separating strings * -r newlines are valid printing characters * -t tabs are valid printing characters * -wn set mininum word length to n (default 3) * file(s) list of filenames (one required) * * Date : May 2, 1988 * * Author: Michael N. Kimura (w/ many thanks to Chris Graves) * * Internet: mnk%draco@hac2arpa.hac.com * BITnet: mnk%draco.hac.com * * Hughes Aircraft Company (RSG) * MS: R2/A159 * P.O. Box 92426 * Los Angeles, CA 90009 * * (213) 615-9775 * *---------------------------------------------------------------------- */ #include errno #include descrip #include file #include rmsdef #include ssdef #include stdio #include stsdef #include unixio #define BUFSIZ 2048 /* Buffer size */ #define DEFWRD 3 /* Default minimum word length */ #define FNSIZ 256 /* Maximum filename size */ #define USAGE "usage: strings [-hnrt] [-wn] file ...\n" main(argc,argv) int argc; char *argv[]; { char header = TRUE; /* Display file headers? */ int minword = DEFWRD; /* Size of minimum word */ char separator = ' '; /* Word separation character */ char tab = ' '; /* TAB flag, default off */ char nl = ' '; /* Newline flag, default off */ int arg; /* Argument index */ int fcnt = 0; /* Filespec counter */ char fname[FNSIZ]; /* Filename */ int status = SS$_NORMAL; /* Final status of STRINGS */ int succ; /* VMS Success Code */ /*--------------------------------------------------------------*/ /* Get switches from the command line and count the number of */ /* filespecs */ get_switches(argc,argv,&fcnt,&header,&minword,&separator,&tab,&nl); /*--------------------------------------------------------------*/ /* One filespec is REQUIRED */ if (fcnt == 0) { printf(USAGE); /* Display usage string */ exit(SS$_BADPARAM | STS$M_INHIB_MSG); } /*--------------------------------------------------------------*/ /* Process each filespec */ for (arg = 1; arg <= argc; arg++) { /*----------------------------------------------------------*/ /* Skip args already processed */ if (argv[arg] == 0) continue; /*----------------------------------------------------------*/ /* Process each file specification */ while ((succ = find_file(argv[arg],fname)) == RMS$_NORMAL) { succ = strings(fname,header,minword,separator,tab,nl); if ((status == SS$_NORMAL) && (succ & STS$M_SUCCESS) == 0) status = succ; } /*----------------------------------------------------------*/ /* Something went wrong in find_file. Display an error */ /* message and record the status if this is 1st error */ if (succ != RMS$_NMF) { vms_error("SEARCHFAIL, error searching for ",fname,"",succ); if ((status == SS$_NORMAL) && (succ & STS$M_SUCCESS) == 0) status = succ; } } /*--------------------------------------------------------------*/ /* Exit with status back to DCL */ exit(status | STS$M_INHIB_MSG); } /*----------------------------------------------------------------------*/ /* Strings - Subroutine to find ASCII strings in a file */ /*----------------------------------------------------------------------*/ strings(fname,header,minword,separator,tab,nl) char *fname; /* File Name */ char *header; /* Display file headers? */ int *minword; /* Size of minimum word */ char *separator; /* Word separation character */ char *tab; /* TAB flag, default off */ char *nl; /* Newline flag, default off */ { int fd; /* File Descriptor */ char word[BUFSIZ]; /* String */ register char c; /* Character */ register int cc; /* Number of chars in word */ register int cnt; /* Number of chars read */ register int succ; /* VMS Success Code */ /*--------------------------------------------------------------*/ /* Open the file readonly */ if ( (fd = open(fname,O_RDONLY,0)) == -1) { succ = vaxc$errno; /* Record open status */ vms_error("OPENIN, error opening ",fname," as input",succ); } else succ = RMS$_NORMAL; /*--------------------------------------------------------------*/ /* If the open was successful then proceed */ if (succ & STS$M_SUCCESS) { /*----------------------------------------------------------*/ /* Print header if desired */ if (header) { printf("\n%s\n\n",fname); } /*----------------------------------------------------------*/ /* Find strings and print them */ cc = 0; while ((cnt = read(fd,&c,1)) && (succ & STS$M_SUCCESS)) { /*------------------------------------------------------*/ /* Check for read errors */ if (cnt == -1) { succ = vaxc$errno; vms_error("READERR, error reading ",fname,"",succ); continue; } /*------------------------------------------------------*/ /* If character is printable then add it to buffer and */ /* if the buffer is full then dump it */ if ( (' ' <= c && c <= '~') || c == tab || c == nl) { word[cc++] = c; if (cc == BUFSIZ) { printf("%.*s%c",cc,word,separator); cc = 0; } } /*------------------------------------------------------*/ /* Character is unprintable, dump the buffer if the */ /* buffer is over "minword" characters long */ else { if (cc >= minword) printf("%.*s%c",cc,word,separator); cc = 0; } } /*----------------------------------------------------------*/ /* If last buffer is larger then "minword" then dump it */ if (cc >= minword) printf("%.*s%c",cc,word,separator); /*----------------------------------------------------------*/ /* Close the file */ if (close(fd) == -1) { succ = vaxc$errno; vms_error("CLOSERR, error closing ",fname,"",succ); } /*----------------------------------------------------------*/ /* If the separation character is SP then output \n */ if (separator == ' ') printf("\n"); } return succ; } /*----------------------------------------------------------------------*/ /* Get_Switches - Subroutine to get switches from the command line */ /*----------------------------------------------------------------------*/ get_switches(argc,argv,fcnt,header,minword,separator,tab,nl) int argc; /* Argument Count */ char *argv[]; /* Argument Vector */ int *fcnt; /* Filespec counter */ char *header; /* Display file headers? */ int *minword; /* Size of minimum word */ char *separator; /* Word separation character */ char *tab; /* TAB flag, default off */ char *nl; /* Newline flag, default off */ { register int arg; /* Argument Index */ register char *ptr; /* Character Pointer */ /*--------------------------------------------------------------*/ /* Process switches and count remaining arguments (files) */ for (--argc, arg = 1; arg <= argc; arg++) { /*----------------------------------------------------------*/ /* Check for leading "-" (indicates a switch) */ if ( *(ptr = argv[arg]) == '-') { /*------------------------------------------------------*/ /* Convert to lowercase and process each switch */ ptr++; *ptr = tolower(*ptr); for (; *ptr != NULL; ptr++) switch (*ptr) { case 'h': /* Disable headers */ *header = 0; break; case 'n': /* Separator is \n instead of ' ' */ *separator = '\n'; break; case 'r': /* Newlines are valid printing chars */ *nl = '\n'; break; case 't': /* Tabs are valid printing chars */ *tab = '\t'; break; case 'w': /* Minimum word length */ sscanf(++ptr,"%d",minword); *ptr-- = NULL; break; default: /* Invalid switch */ printf(USAGE); exit(SS$_BADPARAM | 0x10000000); } argv[arg] = 0; /* Mark arg as processed */ } else (*fcnt)++; /* Update filespec counter */ } } /*----------------------------------------------------------------------*/ /* Find_File - Subroutine to call LIB$FIND_FILE */ /*----------------------------------------------------------------------*/ #define MULTIPLE 2 find_file(fspec,fname) char *fspec; char *fname; { static int context=0; register int succ; register char *ptr; struct dsc$descriptor_s fspec_d = {strlen(fspec), DSC$K_DTYPE_T, DSC$K_CLASS_S, fspec}; struct dsc$descriptor_s fname_d = {FNSIZ, DSC$K_DTYPE_T, DSC$K_CLASS_S, fname}; succ = LIB$FIND_FILE(&fspec_d,&fname_d,&context,0,0,0,&MULTIPLE); for(ptr=fname; *ptr != ' '; ptr++) ; *ptr = NULL; return succ; } /*----------------------------------------------------------------------*/ /* VMS_Error - Prints text plus VMS error message */ /*----------------------------------------------------------------------*/ vms_error(prefix,fname,suffix,msg_id) char *prefix; char *fname; char *suffix; int msg_id; { char msgbuf[FNSIZ]; $DESCRIPTOR(msgdsc,msgbuf); int succ; short msglen; succ = LIB$SYS_GETMSG(&msg_id,&msglen,&msgdsc); if (!(succ & STS$M_SUCCESS)) exit(succ); printf("%%STRINGS-W-%s%s%s\n%.*s\n",prefix,fname,suffix,msglen,msgbuf); }