strchg.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.6 */
/*
* Streams Command strchg: change the configuration of the
* stream associated with stdin.
*
* USAGE: strchg -h module1[,module2,module3 ...]
* or: strchg -p
* or: strchg -p -a
* or: strchg -p -u module
* or: strchg -f file
*
* -h pusHes the named module(s) onto the stdin stream
* -p poPs the topmost module from the stdin stream
* -p -a poPs All modules
* -p -u module poPs all modules Up to, but not including, the named module
* -f file reads a list of modules from the named File, pops all modules,
* then pushes the list of modules
*
* RETURNS:
* 0 SUCCESS it worked
* 1 ERR_USAGE bad invocation
* 2 ERR_MODULE bad module name(s)
* 3 ERR_STDIN an ioctl or stat on the stdin stream failed
* 4 ERR_MEM couldn't allocate memory
* 5 ERR_OPEN couldn't open file in -f opt
* 6 ERR_PERM not owner or superuser
*
*/
#include <stdio.h>
#define FALSE 0
#define TRUE 1
#define SUCCESS 0
#define FAILURE 1
/* (can push more if you like) */
#define OPTLIST "af:h:pu:"
#define USAGE "Usage:\t%s -h module1[,module2 ... ]\n\t%s -f file\n\t%s -p [-a | -u module ]\n"
#define STDIN 0
#define SAME 0 /* return from str[n]cmp if match */
static char *Cmd_namep; /* how was it invoked? */
extern char *strncpy();
extern char *strtok();
extern int getopt();
extern unsigned short geteuid();
extern int ioctl();
extern int strncmp();
extern void perror();
static int pop_modules(); /* pop 'n' modules */
static int push_module(); /* push a module */
static int more_modules(); /* increase size of mod lists */
static void restore(); /* restore state of stdin */
int argc;
char *argv[];
{
char *file_namep; /* file from -f opt */
char *modnamep; /* mods from -h or -u opt */
char *modp; /* for walking thru modnamep */
register int i; /* loop index and junk var */
register int j; /* loop index and junk var */
int euid; /* effective uid */
short error; /* TRUE if usage error */
short fromfile; /* TRUE if -f file */
short is_a_tty; /* TRUE if TCGETA succeeds */
short pop; /* TRUE if -p */
short popall; /* TRUE if -p -a */
short popupto; /* TRUE if -p -u module */
short push; /* TRUE if -h mod1[,mod2 ...] */
struct str_mlist
extern char *optarg; /* for getopt() */
extern int optind; /* for getopt() */
/*
* init
*/
/*
* only owner and root can change stream configuration
*/
perror("fstat");
return(ERR_STDIN);
}
"%s: not owner of stdin\n", Cmd_namep);
return(ERR_PERM);
}
}
/*
* parse args
*/
if ( argc == 1 ) {
return(ERR_USAGE);
}
switch (i) {
case 'a': /* pop All */
else
break;
case 'f': /* read from File*/
else {
file_namep = optarg;
}
break;
case 'h': /* pusH */
else {
}
break;
case 'p': /* poP */
else
break;
case 'u': /* pop Upto */
else {
}
break;
default:
return(ERR_USAGE);
/*NOTREACHED*/
}
}
return(ERR_USAGE);
}
"%s: -p option must be used with -a or -u to pop modules\n",
return(ERR_USAGE);
}
/*
* Save state so can restore if something goes wrong
* (If are only going to push modules, don't need to
* save original module list for restore.)
*/
/*
* get number of modules on stream
* allocate more room if needed
*/
< 0 ) {
perror("I_LIST");
"%s: I_LIST ioctl failed\n", Cmd_namep);
return(ERR_STDIN);
}
return(ERR_MEM);
/*
* get list of modules on stream
*/
perror("I_LIST");
"%s: I_LIST ioctl failed\n", Cmd_namep);
return(ERR_STDIN);
}
/*
* The following attempts to avoid leaving a
* terminal line that does not respond to anything
* if the strchg -h or -f options failed due to
* specifying invalid module names for pushing
*/
}
/*
* push modules on stream
*/
if ( push ) {
/*
* pull mod names out of comma-separated list
*/
/* pop the 'i' modules we just added */
restore(i, 0);
return(ERR_STDIN);
}
}
return(SUCCESS);
}
/*
* read configuration from a file
*/
if ( fromfile ) {
perror("fopen");
"%s: could not open file '%s'\n",
return(ERR_OPEN);
}
/*
* read file and construct a new strlist
*/
i = 0;
if ( buf[0] == '#' )
continue; /* skip comments */
/*
* skip trailing newline, trailing and leading
* whitespace
*/
continue; /* blank line */
++i;
/*
* bad format
* should only be one name per line
*/
"%s: error on line %d in file %s: multiple module names??\n",
Cmd_namep, i, file_namep);
return(ERR_MODULE);
}
return(ERR_MEM);
}
/*
* If an empty file, exit silently
*/
if ( i == 0 )
return(SUCCESS);
/*
* Pop all modules currently on the stream.
*/
/* put back whatever we've popped */
restore(0, i);
return(ERR_STDIN);
}
/*
* Push new modules
*/
== FAILURE ) {
/*
* pop whatever new modules we've pushed
* then push old module list back on
*/
/*
* If the stream is a tty line, at least try
* to set the state to what it was before.
*/
if ( is_a_tty ) {
perror("TCSETA");
"%s: WARNING: Could not restore the states of the terminal line discipline\n",
}
}
return(ERR_STDIN);
}
}
return(SUCCESS);
} /* end if-fromfile */
/*
* pop all modules (except driver)
*/
if ( popall ) {
restore(0, i);
return(ERR_STDIN);
}
}
return(SUCCESS);
}
/*
* pop up to (but not including) a module
*/
if ( popupto ) {
/*
* check that the module is in fact on the stream
*/
break;
/* no match found */
return(ERR_MODULE);
}
if ( (j = pop_modules(i)) != i ) {
/* put back whatever we've popped */
restore(0, j);
return(ERR_STDIN);
}
return(SUCCESS);
}
/*
* pop the topmost module
*/
if ( pop ) {
/* no need to restore */
return(ERR_STDIN);
return(SUCCESS);
}
/*NOTREACHED*/
}
/*
* pop_module(n) pop 'n' modules from stream
*
* returns # of modules popped
*/
static int
int num_modules;
{
register short i; /* the ubiquitous loop variable */
for ( i = 0; i < num_modules; i++ ) {
perror("I_POP");
"%s: I_POP ioctl failed\n", Cmd_namep);
return(i);
}
}
return(i);
}
/*
* push_module(modnamep) pushes 'modnamep' module on stream
*
* returns SUCCESS or FAILURE
*/
static int
char *modnamep;
{
perror("I_PUSH");
return(FAILURE);
}
return (SUCCESS);
}
/*
* restore(npop, npush) restore original state of stream
*
* pops 'npop' modules, then pushes the topmost 'npush' modules from
* Oldlist
*
*/
static void
int npop;
int npush;
{
register int i;
"%s: WARNING: could not restore state of stream\n", Cmd_namep);
return;
}
"%s: internal logic error in restore\n", Cmd_namep);
"%s: WARNING: could not restore state of stream\n", Cmd_namep);
return;
}
for ( i = npush - 1; i >= 0; --i ) {
"%s: WARNING: could not restore state of stream\n",
return;
}
}
return;
}
/*
* more_modules(listp, n) allocate space for 'n' modules in 'listp'
*
* returns: SUCCESS or FAILURE
*/
static int
more_modules(listp, n)
int n; /* # of modules */
{
register int i;
extern char *calloc();
if ( n > MAXMODULES ) {
"%s: too many modules (%d) -- max is %d\n",
Cmd_namep, n, MAXMODULES);
return(FAILURE);
}
perror("calloc");
"%s: failed to allocate space for module list\n",
return(FAILURE);
}
FMNAMESZ);
return(SUCCESS);
}