Assigning variables in C programming

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

Assigning variables in C programming

#1 Post by Karl Godt »

There are some sources flying around here on the forum ,
one of them is freememapplet[_tray] source .

Today i discovered on a Puppy 5 installation a folder i had nearly forgotten about where i had several source tarballs of freememapplet .

The problem is that since Lupu 5.1.1
playdays somehow shifted the Puppy df wrapper,
that substitutes "/dev/root" with `rdev' output for full installs,

this source had been altered to

Code: Select all

else if (pupmode==2) fp = (FILE *)popen("df -m |  grep '/dev/root' | tr -s ' '| cut -f 2,4 -d ' '","r");
which does not work on Puppy full installations that have the df script and the df-FULL binary .

It is easy to alter the code to

Code: Select all

else if (pupmode==2) fp = (FILE *)popen("df -m |  grep -m1 -w '/' | tr -s ' '| cut -f 2,4 -d ' '","r");
To make it work .

But i wanted to create a fall back line if fp still stays NULL somehow .

I tried this

Code: Select all

 if (!fp) fp=-1;
which gave Warning but compiles
// freememapplet_tray.c: In function ‘Update’:
// freememapplet_tray.c:71: Warnung: assignment makes pointer from integer without a cast
and is incorrect, since fd should contain 2 values
and freememapplet still segfaults

and tried

Code: Select all

if (!fp) (char *) fp = "0 0";
which gave error
// freememapplet_tray.c: In function ‘Update’:
// freememapplet_tray.c:71: Fehler: lvalue required as left operand of assignment
I am guessing i need a C command to parse a string of two values to the fd variable .
:?:

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

snprintf example
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

Thanks, technosaurus !

Googled a little for "snprintf example" and found this almost complete :

http://www.codecogs.com/reference/compu ... s=snprintf

for all the other *printf 's .

will take me a while to figure things, cos

Code: Select all

if (!fp) sprintf( (char *)fp, "%d %d\n", 0, 0) ;
or

Code: Select all

if (fp == NULL) sprintf( (char *)fp, "%d %d", 100, 10) ;
seems not to work, it seems that fp is not empty , probably still as a linefeed \n or \0 char somewhere .

the source i am nagging on can be found here :
http://www.murga-linux.com/puppy/viewto ... 192#450192

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

crap I answered the question asked, not the real question
- fp is a pointer to an int (that is what FILE does - it is equivalent to int) so (FILE *) is a file descriptor pointer the equivalent of (int *) ... does not cast well to a char *
fyi: the standard file descriptors are 0 (stdin), 1 (stdout), 2 (stderr)

anyhow after that last one you can just try your alternate if !fp do:

Code: Select all

else if (pupmode==2) {
    fp = (FILE *)popen("df -m |  grep '/dev/root' | tr -s ' '| cut -f 2,4 -d ' '","r");
    if (!fp) fp = (FILE *)popen("df -m |  grep -m1 -w '/' | tr -s ' '| cut -f 2,4 -d ' '","r");
}
later you may use that file descriptor to read() the output (which is when you will store it in a char *)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#5 Post by Karl Godt »

:oops: the 'fd' was a typo of mine, meant 'fp' :oops:

Thanks, techno, this way i altered xosview to look for different paths for the temperature line . Will post source if i find them. But in xosview it seems to be clumpsy, should look everywhere in /sys for temp_input[0-9] files .

Post Reply