system() --- the C function from hell?

Jonathan I. Kamens jik at athena.mit.edu
Sun Apr 30 21:23:05 AEST 1989


In article <1827 at uop.edu> jeff at 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 at 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?



More information about the Comp.unix.questions mailing list