Hello ,

I need to pass a system (linux) result to a varaiable .

Example :

...
char KERNEL = system("uname");
printf(KERNEL);
...

this example dont work , can you help me ?

The only thing I can think of is to route the output to a file. system("uname > foo"); where you can use fgets to get the data on the file you want, and then delete the file when you're done with it.

Hope this helps.

Edited 6 Years Ago by IsharaComix: Shell variables aren't the same as environment variables.

The only thing I can think of is to route the output to a file. system("uname > foo"); where you can use fgets to get the data on the file you want, and then delete the file when you're done with it.

Hope this helps.

Thanks for reply ,

full example code is

#include <stdio.h>
#include <string.h>

int main ()
{


system("rm -f pass.txt");
system("makepasswd -p testpassword -e shmd5 | cut -d ' ' -f 2 >> pass.txt");


  FILE * filehandle;
  char myline[121];


  filehandle = fopen("pass.txt","r");


  while (fgets(myline,120,filehandle)) {
          system ("usermod -p",myline,"username");
  }

}


fclose( file );

In this case the problem is a concatenate function for string/variable in line :

system ("usermod -p",myline,"username");

How can I solve it ? what is teh correct syntax ?


thanks

Create a new string buffer for the command itself with the proper data inserted.

FILE * filehandle;
  char myline[121];
  char mycmd[200];

  filehandle = fopen("pass.txt","r");

  while (fgets(myline,120,filehandle)) {
          sprintf( mycmd, "usermod -p %s username", myline );
          system ( mycmd );
  }

Edited 6 Years Ago by IsharaComix: Getting used to the forum.

sprintf( mycmd, "usermod -p %s username", myline );
system ( mycmd );

It works, but add a NEWLINE after password :

Example Output :

usermod -p '$1$awsassookdosdkoakPyRa
' test

and not

usermod -p '$1$awsassookdosdkoakPyRa' test

any tips ?

Just run up through the string and replace the newline character with the null byte.

while (fgets(myline,120,filehandle)) {
    int i;
    for ( i = 0; myline[i] != '\0' && myline[i] != '\n' && myline[i] != '\r'; i++ ) {/*Do nothing*/};
    myline[i] = '\0';
    sprintf( mycmd, "usermod -p %s username", myline );
    system ( mycmd );
}

Alternatively, instead of using fgets to save the string, you could use fgetc and record characters up until you reach EOF or a newline character.

Try piping the the response back into the program like:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	int i = 0;
	char ans[256];
	char ch;
	int hpipe[2];
	pipe(hpipe);

	if (fork())
	{
		close(hpipe[WRITE]);
		dup2(hpipe[READ], 0);
		close(hpipe[READ]);
		while ((ch = fgetc(stdin)) != EOF)
		{
			ans[i++] = ch;
		}
		ans[i] = '\0';
		fprintf(stdout, "this is the respones stored in ans->%s\n",ans);
	}
	else
	{
		close(hpipe[READ]);
		dup2(hpipe[WRITE], 1);
		close(hpipe[WRITE]);
		execlp("uname", "uname", "-a", NULL);
	}
	exit(EXIT_SUCCESS);
}

Edited 6 Years Ago by gerard4143: n/a

If you're just going to call system() a bunch of times, there is little point in making it a C program.

It would be far simpler and quicker just to make a shell script.

It's not that it can't be done, it's just way too much effort to be worthwhile.

This article has been dead for over six months. Start a new discussion instead.