[comp.unix.questions] System

guy@gorodish.Sun.COM (Guy Harris) (01/30/88)

(This long ago ceased to be a C-related discussion; it is now a UNIX-related
discussion.  It is now being redirected to comp.unix.questions.)

> Actually, there is not much reason for worrying about whether or not
> there is a library function for mkdir on most (all?) System V boxes,
> since the mkdir system call is reserved for the super user alone.

WRONG.  It is not a privileged system call in S5R3.

> In the standard System V world, you will find that even the 'mkdir' command
> is setuid to root.

Not in S5R3.1, at least, and probably not in S5R3.  On the 3B2/400s we have
here, running S5R3.1, "/bin/mkdir" is not set-UID, and it works just fine.

In S5 systems *prior* to S5R3, it is set-UID "root" because it has to use the
privileged "mknod" system call.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

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!

barmar@think.com (Barry Margolin) (11/16/90)

In article <1990Nov15.183359.963@ssd.kodak.com> weimer@ssd.kodak.com (Gary Weimer) writes:
>In article <1990Nov15.132952.11932@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>>You don't have to use system(3), you can use fork/execl(2) (or one of it's 
>>family of functions) as follows:
>That's just about exactly what system(3) does. (i.e. you gain nothing for
>all the added code)

There are some performance and behavioral differences.  First of all,
system() has to first exec /bin/sh after forking.  Second, the shell has to
parse the command line back into individual arguments.  Why exec() two
programs when you can exec() one, and parsing arguments that were
originally separate variables is also wasteful.  System() is best used when
you are taking the command line from the user.

There are also behavioral differences, and some caveats apply when using
system().  System() does globbing, variable expansion, etc., while these
aren't done by exec().  Whitespace and other special characters can affect
the shell's parse of the command line, so you have to be careful about
quoting when you build the command line for system(), e.g. 

	char file1[] = "foo bar"
	char file2[] = "baz"
	char command[COMMAND_LEN];

	sprintf(command, "mv %s %s", file1, file2);
	system(command);

is not equivalent to a fork followed by

	execlp("mv", "mv", file1, file2, (char*)0);

because the first will pass three filename arguments to mv, while the
second will pass only two.  You can get around this by putting quotes into
the sprintf() format string, but you'll still have problems if a filename
contains quotes.

--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

gumbyltd@spam.berkeley.edu (Dmitry Gokhman) (04/11/91)

In a DOS implementation of AWK there is a system
function, which I've grown to rely on chiefly
to import contents of files into the final output.
For example:
   $1 == "#include" {system(type $2)}
Now I'd like to something like this in UNIX
(with 'cat' instead of 'type'), but AWK 
doesn't seem to recognize system().  Any clues?
/////////////////////////////////////////////////////////////////////
- Mr. Gumby    * \oo7    Dmitry Gokhman -> gumbyltd@math.berkeley.edu
   says:        `/v/-*   University of Cauliflower
MY BRAIN HURTS  J  L     Broccoli CA 94720
/////////////////////////////////////////////////////////////////////

jik@athena.mit.edu (Jonathan I. Kamens) (04/11/91)

In article <1991Apr11.043525.25646@agate.berkeley.edu>, gumbyltd@spam.berkeley.edu (Dmitry Gokhman) writes:
|>    $1 == "#include" {system(type $2)}
|> Now I'd like to something like this in UNIX
|> (with 'cat' instead of 'type'), but AWK 
|> doesn't seem to recognize system().  Any clues?

  Get a newer version of Unix awk that understands system().  For example, the
GNU version of awk, available for anonymous ftp on prep.ai.mit.edu in /pub/gnu
or at another friendly neighborhood GNU archive site near you.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

quang@CSUFRES.CSUFRESNO.EDU (Quang Ngo) (05/15/91)

I have a question about nawk.  Why does the command 'system' know id but
not f[i]?  It says

nawk: f[i]: No such file .....

However, if I change it to 

   if (system("grep id 1fl_87.txt") == 1)

then it works okay.

Thank you,

-Quang (quang@csufres.CSUFresno.EDU)

--------------------------
BEGIN {
	f[0] = "1fl_87.txt"
	f[1] = "2sp_88.txt"
	f[2] = "3fl_88.txt"
	f[3] = "4sp_89.txt"
	f[4] = "5fl_89.txt"
	f[5] = "6-SP90"
}

{
	id = substr($0,34,9)

	for (i=0; i<6; i++)
	if (system("grep id f[i]") == 1)    
		printf("%s %d\n",id,i)
}
--------------------------