Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!ucbcad!ucbvax!uvabick.UUCP!uucp From: uucp@uvabick.UUCP.UUCP Newsgroups: comp.os.vms Subject: Submission for mod-computers-vax Message-ID: <8704290528.AA05411@uvabick.UUCP> Date: Wed, 29-Apr-87 01:28:34 EDT Article-I.D.: uvabick.8704290528.AA05411 Posted: Wed Apr 29 01:28:34 1987 Date-Received: Fri, 1-May-87 06:47:40 EDT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: world Organization: The ARPA Internet Lines: 122 Path: uvabick!mcvax!seismo!rutgers!mit-eddie!genrad!decvax!minow From: minow@decvax.UUCP (Martin Minow) Newsgroups: mod.computers.vax,comp.sys.dec Subject: Re: redirection of stdio, et al. Message-ID: <49@decvax.UUCP> Date: 26 Apr 87 23:10:57 GMT References: <224@unc.cs.unc.edu> Reply-To: minow@decvax.UUCP (Martin Minow) Organization: Digital Eq. Corp. - Merrimack NH. Lines: 110 Posted: Sun Apr 26 19:10:57 1987 In article <224@unc.cs.unc.edu> mcguffey@unc.cs.unc.edu (Michael McGuffey) requests a method to allow command line redirection under VMS. What I use follows the signature. Martin Minow decvax!minow main(argc, argv) int argc; char *argv[]; { #ifdef vms argc = getredirection(argc, argv); #endif ... } -------- /* * getredirection() is intended to aid in porting C programs * to VMS (Vax-11 C) which does not support '>' and '<' * I/O redirection. With suitable modification, it may * useful for other portability problems as well. */ static int getredirection(argc, argv) int argc; char **argv; /* * Process vms redirection arg's. Exit if any error is seen. * If getredirection() processes an argument, it is erased * from the vector. getredirection() returns a new argc value. * * Warning: do not try to simplify the code for vms. The code * presupposes that getredirection() is called before any data is * read from stdin or written to stdout. * * Normal usage is as follows: * * main(argc, argv) * int argc; * char *argv[]; * { * argc = getredirection(argc, argv); * } */ { #ifdef vms register char *ap; /* Argument pointer */ int i; /* argv[] index */ int j; /* Output index */ int file; /* File_descriptor */ extern int errno; /* Last vms i/o error */ for (j = i = 1; i < argc; i++) { /* Do all arguments */ switch (*(ap = argv[i])) { case '<': /* ': /* >file or >>file */ if (*++ap == '>') { /* >>file */ /* * If the file exists, and is writable by us, * call freopen to append to the file (using the * file's current attributes). Otherwise, create * a new file with "vanilla" attributes as if * the argument was given as ">filename". * access(name, 2) is TRUE if we can write on * the specified file. */ if (access(++ap, 2) == 0) { if (freopen(ap, "a", stdout) != NULL) break; /* Exit case statement */ perror(ap); /* Error, can't append */ exit(errno); /* After access test */ } /* If file accessable */ } /* * On vms, we want to create the file using "standard" * record attributes. create(...) creates the file * using the caller's default protection mask and * "variable length, implied carriage return" * attributes. dup2() associates the file with stdout. */ if ((file = creat(ap, 0, "rat=cr", "rfm=var")) == -1 || dup2(file, fileno(stdout)) == -1) { perror(ap); /* Can't create file */ exit(errno); /* is a fatal error */ } /* If '>' creation */ break; /* Exit case test */ default: argv[j++] = ap; /* Not a redirector */ break; /* Exit case test */ } } /* For all arguments */ return (j); #else /* * Note: argv[] is referenced to fool the Decus C * syntax analyser, supressing an unneeded warning * message. */ return (argv[0], argc); /* Just return as seen */ #endif }