More questions about how to issue a C-SHELL command within a C program

Randal Schwartz merlyn at iwarp.intel.com
Thu Aug 16 08:15:04 AEST 1990


In article <25285.26c9113d at kuhub.cc.ukans.edu>, jian at kuhub writes:
| Yesterday I asked for help about how to issue a C-SHELL command within a C 
| program. I was lucky: serveral people came up with immediate answers. I very
| much appreciate those answers.
| 
| According to the answers and what I read from the menu, system() and execlp()
| should work without any doubts. However, I can't make it. What I want to do
| is to issue a csh command to change terminal type within a C program. I did 
| try:
|         system("csh -cf \"setenv TERM adm3a"); 
|    and
|         system("/bin/csh -c 'setenv TERM adm3a");
| 
|    and
|         execlp("/bin/csh", "csh", "-c", "setenv TERM adm3a", (char *) 0);
| 
| There was not any runtime errors if I embeded one of above statements into my
| C program. But none of them can change the terminal type. I don't know why.
| Would someone point me another way to change terminal type within a C program
| or give me some hints what I did wrong. I would appreciate any helps.

Aha.  As I said in my private mail, *why* would you want to execute a
csh command?  Now I can see.

The short answer:

You *cannot* change an environment variable (such as TERM) from a
child process.  The shell provides a *copy* of its environment to the
children processes (such as your C program), so changes to that copy
(or in your case, using a further csh which has its *own* copy), will
not affect the parent shell.  This is by design, so don't ask "Well,
are they going to fix this some day?"

The longer answer:

If you invoke a program from the shell in such a way that the shell is
going to expect an answer back and take action on that, you can get
away with it.  For example, if your program "pickterm" is invoked
from the csh as:

	setenv TERM `pickterm`

then all "pickterm" has to do is output the desired term-type on
stdout.  If you need interaction, you may need to arrange for the csh
to get it from a file.  For example,

	pickterm /tmp/$$ && setenv TERM `cat /tmp/$$`
	rm /tmp/$$

in which case pickterm would open /tmp/$$ for output, put the desired
termtype into it after interacting with the user, and then exit
successfully.

Either of these solutions could be wrapped up inside an alias to
prevent the nitty gritties from being exposed to the poor user.

Just another Unix hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/



More information about the Comp.unix.questions mailing list