Simple C Program to set DS Variable

Hi All,

I have the written the following simple C program to set a DS variable:

#include <stdlib.h>
#include <stdio.h>
#include <ds.h>
#include <string.h>
#include <sys/wait.h>

int main( void )
{
ds_t ds_descriptor;
char test[5];
char flag = 0;
int length = 5;
char test_val[5];
int count = 0;

ds_descriptor = ds_register();
if(ds_descriptor==-1)
{
perror(“ds_register”);
exit(1);
}

if(ds_create(ds_descriptor, test, flag, 0)==-1)
{
perror(“ds_create”);
exit(1);
}

while(count!=5)
{
count++;

 sprintf(test_val,"%d",count);
 
 if (ds_set(ds_descriptor,test,test_val,length)==-1)
 {
       perror("ds_set");
       exit(1);
 }
 printf("Test:%s*\n",test);
} 

printf(“End of loop\n”);
ds_clear(ds_descriptor,test);
ds_deregister(ds_descriptor);

return 0;
}

However, when I run the program,it does not output the value of the ds variable test. Am I missing something?
Please let me know…
Thanks in advance!

QNXSJ,

Two things here:

  1. When you run the program, what are you seeing as output? Are you getting any errors? You didn’t happen to name your test.c program ‘test’ did you? If you did, it won’t run becuase there is a built in shell command called test that would be executed instead. This bites a lot of people if that’s the case, rename your executable.

  2. In the ds_create() call you pass in the string test as the 2nd argument. However it doesn’t have any value set in your program. If you mean to set it to “test” you need a sprintf(test, “test”); line before ds_create().

Tim

Thanks for the prompt response…

  1. I have named my program dsscript.c and i am compiling it using qcc dsscript.c -lds which creates an output file a.out I then just type a.out and it executes the program and I see the output as junk characters . The test_val variable is printed correctly. So i know that the sprintf variable is working fine… but i think the problem is with ds_set cos the value in test_val is not getting populated correctly in the test variable.
    1
    Test:è)3°ÿÿÿÿè)3°è)3°*
    2
    Test:è)3°ÿÿÿÿè)3°è)3°*
    3
    Test:è)3°ÿÿÿÿè)3°è)3°*
    4
    Test:è)3°ÿÿÿÿè)3°è)3°*
    5
    Test:è)3°ÿÿÿÿè)3°è)3°*
    End of loop

  2. In ds_create() i am just trying to create a ds variable named test to which i set the value equal to test_val in the ds_set command.
    Is it necessary to assign a value to a variable before ds_create?

As Tim mentioned the problem is not with test_val it’s with test, which should be the name of the variable. You are not initializing it, hence it’s garbage…

I initialized the test variable
char test[5]=“a”;
and now I get the initialized value :frowning:
which means that the ds_set is not working??

/# /cmu/web/a.out
1
Test:a*
2
Test:a*
3
Test:a*
4
Test:a*
5
Test:a*
End of loop

try this

#define OVEN_ID "oven1"
#define DATA_LENGTH 5

void InitDataServer (void)
{
      ds_t ds_descriptor;
      char oven_temp[DATA_LENGTH]; 
      char flag =0;

      ds_descriptor = ds_register();
      if (ds_descriptor == -1)
     {
           perror ("ds_register");
           exit(-1);
      }

      if (ds_create(ds_descriptor, OVEN_ID , flag , 0) == -1)
      {
           perror("ds_create");
           exit(-1);
       }

       strcpy(oven_temp,"66666");
       ds_set(ds_descriptor, OVEN_ID, oven_temp, DATA_LENGTH);
}

#define OVEN_ID "oven1" 
#define DATA_LENGTH 5 
ds_t ds_descriptor; 

Call funtion with a timer every sec
void TestLoop (void)
{
       static int i=0;
       char oven_temp[DATA_LENGTH]; 
     
        i++;
       if (i>99999) i=0;
       printf("%d\n",i);
       sprintf(oven_temp,"%5d",i);
       ds_set(ds_descriptor, OVEN_ID, oven_temp, DATA_LENGTH);
       system ("touch /usr/web/index.shtml");
}

void InitDataServer (void) 
{ 
      char flag =0; 

      ds_descriptor = ds_register(); 
      if (ds_descriptor == -1) 
     { 
           perror ("ds_register"); 
           exit(-1); 
      } 

      if (ds_create(ds_descriptor, OVEN_ID , flag , 0) == -1) 
      { 
           perror("ds_create"); 
           exit(-1); 
       } 
} 

void main (int argc , char *argv[])
{
       InitDataServer();
}

Script for slinger 
#!/bin/sh

/bin/slay -f slinger

#Start a Web Server (Slinger)
export CMD_INT=/bin
export HTTPD_ROOT_DIR=/usr/web
export HTTPD_ROOT_DOC=/usr/web/index.shtml
export HTTPD_SCRIPTALIAS=/usr/cgi

slinger -e -s &

[Also you should have an index.shtml with


<META HTTP-EQUIV="Refresh" CONTENT="2;URL=index.shtml">

<html>
<head>
<title>OVEN</title>
</head>

<body>

<tr><td valign="top"></td>
    <td valign="top"><h2><i><B>OVEN Status Screen</B></i></h2></td></tr>


<!-- Start magic -->

<P>
<!-- Show the Curent oven count -->
<!--#qnxvar format="<P>Oven count = (%s)." -->
<!--#qnxvar read="oven1 5" -->
<P>

<!-- End magic -->

</body>

</html>

johnny,

#define DATA_LENGTH 5 should be #define DATA_LENGTH 6

since your using all 5 of those characters to create numbers up to 99999 in size in your data set string.

Tim

QNXJS,

Maybe you should read the documentation on ds_set and ds_get. From what I can tell you misunderstood what they are use for.

No… the ds_set function is correct and the output that i get is also correct. I just needed to check the value of the variable from an html page. When I did that, i got the incremented values :slight_smile:

Let me see, you are using ds_set properly but it`s not working, then the problem must be the QNX program ds. In that case contact QNX to get official support, maybe you can get the source to ds and fix it.

The only explanation I can see is that ds must take the variable string convert it to an integer, add one to it then convert it back to a string…