[comp.unix.wizards] system

jbn@glacier.STANFORD.EDU (John B. Nagle) (08/17/87)

      Does system(III) still invoke a shell for all requests, or does
it scan the string for shell characters and invoke the program directly
if none are found?  

      Make(I), incidentally, does make such a check, and no one has 
complained yet, so this is probably a valid optimization.

					John Nagle

jeff@uop.edu (Jeff Ferguson) (04/30/89)

	Hi kids,

	The system() call seems to wreak havoc with integer variables.
I have the following code:

/****** Code *******/

int lowp, hip;
...
scanf("%d", &hip);
scanf("%d", &lowp);
sprintf(str, "/bin/mkdir directory");
system(str);
...

/**** End Of Code ****/

I printed the values of LOWP and HIP before the system() call, and
everthing was pee-chee (like the folder).  I print them out after
the call and the values have been blown to high Heaven: before they
were both 3, while after system(str) they are in the tens of millions.

OK, people, for $200 and the chance at the Chevy Sprint Turbo --- my
question is this: what is happenning here an how can it be fixed?  I
am stymied.

Although I probably don't sound like it, I would be grateful (but not
dead) for any advice, suggestions, or visions from God.  Thanks, and
please drive safely.

------------------------------------------------------------------------------
Jeff Ferguson                | jeff@uop.edu
Computer Science Department  | uop!jeff@ucdavis.edu
University of the Pacific    | cogent!uop!jeff@pacbell.com
Stockton, California, USA    |
95211                 /^^^\  | "Get me out of this cage!" -- Rael
(209) 944-7105       { o_o } |     .--------------------------.
                      \ o / ----> ( "KJEF does it with class!" )
---------------------mm---mm-------'--------------------------'---------------
       "I met Andy Warhol at a really shiek party."
       "Blow it out your hair, too, 'cause you work at Hardee's".

jik@athena.mit.edu (Jonathan I. Kamens) (04/30/89)

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>	Hi kids,
>
>	The system() call seems to wreak havoc with integer variables.
>I have the following code:

Since you have failed to provide all of the relevant code, most
specifically the declaration of the variable str, I can't tell you for
sure what is wrong, but I can tell you that it has nothing to do with
system, and I can also tell you that what I suspect is wrong is that
you are not allocated enough space for str so that your sprintf is
overwriting your integer variables (You declared str to be "char *,"
perhaps?).  Here's your sample code in a simple program wrapper and a
sample run of it that works:

		      *************************

#include <stdio.h>

main()
{
     int hip, lowp;
     char str[50];

     fprintf(stdout, "Enter hip: ");
     fflush(stdout);
     scanf("%d", &hip);

     fprintf(stdout, "Enter lowp: ");
     fflush(stdout);
     scanf("%d", &lowp);

     sprintf(str, "/bin/mkdir directory");
     system(str);

     fprintf(stdout, "lowp = %d, hip = %d\n", lowp, hip);
}

		      *************************

oliver% a.out
Enter hip: 3
Enter lowp: 3
lowp = 3, hip = 3
oliver% ls -d directory
directory/
oliver%

		      *************************

Jonathan Kamens			              USnail:
MIT Project Athena				410 Memorial Drive, No. 223F
jik@Athena.MIT.EDU				Cambridge, MA 02139-4318
Office: 617-253-4261			      Home: 617-225-8218

P.S. I have directed follow-ups to comp.unix.questions, as you should
have done.  Did this really need to go to comp.unix.wizards in the
first place?

mcintyrd@cs.rpi.edu (David McIntyre) (04/30/89)

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>	The system() call seems to wreak havoc with integer variables.
>I have the following code:
>
>int lowp, hip;
>...
>scanf("%d", &hip);
>scanf("%d", &lowp);
>sprintf(str, "/bin/mkdir directory");
>system(str);
>...
>
>I printed the values of LOWP and HIP before the system() call, and
>everthing was pee-chee (like the folder).  I print them out after
>the call and the values have been blown to high Heaven: before they
>were both 3, while after system(str) they are in the tens of millions.

I would suggest that str is a (char *) variable that has not
had any space allocated for it, and it is overwriting the
two integers somehow.  You should either make str something like

char str[20];

or malloc it, something like this:

	str = (char *)malloc(20*sizeof(char));


Dave "mr question" McIntyre     |      "....say you're thinking about a plate
mcintyre@turing.cs.rpi.edu      |       of shrimp.....and someone says to 
office : 518-276-8633		|	you `plate,' or `shrimp'......"
home   : 518-271-6664		|

trebor@biar.UUCP (Robert J Woodhead) (05/01/89)

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>int lowp, hip;
>scanf("%d", &hip);
>scanf("%d", &lowp);
>sprintf(str, "/bin/mkdir directory");
>system(str);

I note that you haven't given us the definition of ``str''.  I would
like to see this definition,  for if str was not long enough to hold
"/bin/mkdir directory" plus the termination byte, and was defined so
the compiler put it in memory right before lowp and hip, it would be
possible for the sprintf to trash the integers.

-- 
Robert J Woodhead, Biar Games, Inc.  ...!uunet!biar!trebor | trebor@biar.UUCP
"The NY Times is read by the people who run the country.  The Washington Post
is read by the people who think they run the country.   The National Enquirer
is read by the people who think Elvis is alive and running the country..."

sa1z+@andrew.cmu.edu (Sudheer Apte) (05/01/89)

I suspect you've simply forgotten to allocate space for the string str.
(Maybe you've declared it a char * ?) I doubt if system() has anything
to do with it. So you need to define something like

	char str[40];

before you sprintf() into it.

Thanks,
	Sudheer.
-----------------
Sudheer Mukund Apte			sa1z@andrew.cmu.edu
Civil Engineering Department,
Carnegie-Mellon University,		...!harvard!andrew.cmu.edu!sa1z
Pittsburgh, PA 15213
------------------------------------------------------------------------

yuf@mentor.cc.purdue.edu (Kyle Grieser) (05/01/89)

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>
>OK, people, for $200 and the chance at the Chevy Sprint Turbo --- my
>question is this: what is happenning here an how can it be fixed?  I

	Well, from what you have posted it is hard to tell if it is your
fault or not.  What is "str" declared as.  If it is declared as a "char *"
before the integers, and you don't allocate space for it then the
sprintf might just be writing all over the integers.  But, like I said, without
a bit more information it is hard to tell if it is your fault or system()'s.

-----
Kyle Grieser, Purdue University Computing Center
mentor.cc.purdue.edu!yuf, yuf@mentor.cc.purdue.edu

jcbst3@cisunx.UUCP (James C. Benz) (05/02/89)

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>
>	Hi kids,
>
>	The system() call seems to wreak havoc with integer variables.
>I have the following code:
>
>/****** Code *******/
>
>int lowp, hip;
>...
>scanf("%d", &hip);
>scanf("%d", &lowp);
>sprintf(str, "/bin/mkdir directory");
>system(str);
>...
>
>/**** End Of Code ****/
>
>I printed the values of LOWP and HIP before the system() call, and
>everthing was pee-chee (like the folder).  I print them out after
>the call and the values have been blown to high Heaven: before they
>were both 3, while after system(str) they are in the tens of millions.
>
>OK, people, for $200 and the chance at the Chevy Sprint Turbo --- my
>question is this: what is happenning here an how can it be fixed?  I
>am stymied.

You don't mention how you declare the variable str, so I can't be sure at this,
but for a Sprint Turbo----

My experience with zapped int variables has nothing to do with the system call,
but rather with the length of string variables being declared too short to hold
what you try to stuff into them.  Are you sure the str size is long enough to
hold what you sprintf into it?  Is str declared directly before the two ints?
Sprintf and other string routines in (ATT Sys V) standard C don't know how long
the declared length of a string is, and will quite happily zap whatever is in
their way.  Also, I wouldn't trust scanf for anything.  So where can I pick up
the keys?
Newsgroups: comp.unix.questions,comp.unix.wizards
Subject: Re: system() --- the C function from hell?
Summary: 
Expires: 
References: <1827@uop.edu>
Sender: 
Reply-To: jcbst3@unix.cis.pittsburgh.edu (James C. Benz)
Followup-To: 
Distribution: 
Organization: Univ. of Pittsburgh, Comp & Info Sys
Keywords: system, unix, question, does anyone use this line?

In article <1827@uop.edu> jeff@uop.edu (Jeff Ferguson) writes:
>
>	Hi kids,
>
>	The system() call seems to wreak havoc with integer variables.
>I have the following code:
>
>/****** Code *******/
>
>int lowp, hip;
>...
>scanf("%d", &hip);
>scanf("%d", &lowp);
>sprintf(str, "/bin/mkdir directory");
>system(str);
>...
>
>/**** End Of Code ****/
>
>I printed the values of LOWP and HIP before the system() call, and
>everthing was pee-chee (like the folder).  I print them out after
>the call and the values have been blown to high Heaven: before they
>were both 3, while after system(str) they are in the tens of millions.
>
>OK, people, for $200 and the chance at the Chevy Sprint Turbo --- my
>question is this: what is happenning here an how can it be fixed?  I
>am stymied.

You don't mention how you declare the variable str, so I can't be sure at this,
but for a Sprint Turbo---- AND $200 (I'll settle for the bucks)

My experience with zapped int variables has nothing to do with the system call,
but rather with the length of string variables being declared too short to hold
what you try to stuff into them.  Are you sure the str size is long enough to
hold what you sprintf into it?  Is str declared directly before the two ints?
Sprintf and other string routines in (ATT Sys V) standard C don't know how long
the declared length of a string is, and will quite happily zap whatever is in
their way.  Also, I wouldn't trust scanf for anything.  So where can I pick up
the keys?











line fodder












and more line fodder
-- 
Jim Benz 		     jcbst3@unix.cis.pittsburgh.edu     If a modem 
University of Pittsburgh					 answers,
UCIR			     (412) 648-5930			 hang up!