You are not logged in.

#1 2010-05-25 18:12:23

virusso80
Member
From: Italy
Registered: 2007-03-09
Posts: 325

How to use shell commands inside a C program

Hi all, I hope to find someone that can help me.

I'm struggling in using the command base64, which encodes a file to another one

more in details, the following command works

echo VGhlIExpbnV4IEpvdXJuYWwK | base64 -d
and gives the proper result (The Linux Journal)

Is there a way to get the result into a C variable (i.e char*)?

PS i know that the method "system("type here the shell command") executes the command, but output is not recoverable...i mean, i don't know how.

Thanks

Last edited by virusso80 (2010-05-25 18:13:37)

Offline

#2 2010-05-25 18:27:55

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,334

Re: How to use shell commands inside a C program

man 3 exec
man fork
man pipe


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2010-05-25 18:28:20

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: How to use shell commands inside a C program

I think popen is what you are looking for.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#4 2010-05-25 18:41:49

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: How to use shell commands inside a C program

i second popen.

Last edited by n0dix (2010-05-25 18:56:37)

Offline

#5 2010-05-25 19:28:14

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: How to use shell commands inside a C program

But, you probably dont what to use it (popen), I think this can really slow down you program, I recommend that you implement a base64 dec/enc in C for example http://base64.sourceforge.net/b64.c

Offline

#6 2010-05-25 23:10:47

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: How to use shell commands inside a C program

Yes, don't use popen if you can avoid it.  For one thing it is slower, and for another it is less portable.  Write your own conversion algorithm (it's relatively short), or use someone else's.  Googling "base64 C library" brings up libb64 and a smattering of others.

Offline

#7 2010-05-25 23:33:15

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: How to use shell commands inside a C program

You should try to use a native library where possible

Offline

#8 2010-05-26 00:38:49

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: How to use shell commands inside a C program

And probably some library already provide base64, in my mind come openssl (its is a core package). For example google give me this http://www.ioncannon.net/programming/34 … d-openssl/ and http://www.mail-archive.com/openssl-use … 58722.html

Offline

Board footer

Powered by FluxBB