/ test_callsystem.c
test_callsystem.c
 1  /*
 2    TESTS
 3   */
 4  
 5  #include <stdio.h>
 6  #include <assert.h>
 7  #include "callsystem.c" /* run and compile this without additional link step */
 8  
 9  void usage_example()
10  {
11    int err;
12    int exitcode;
13    callsystem_pid_t child = CALLSYSTEM_ILG_PID;
14    char ** env = NULL;
15    char ** args = NULL;
16    unsigned counter=0;
17    callsystem_fd_t null[2];
18    char * foo;
19  
20    callsystem_null(null);
21  
22    callsystem_setenv (&env, "TEST", "here");
23  
24    foo = callsystem_env_subst (&env, "I am ${TEST}${TEST}$${TEST}${TEST}");
25  
26    printf("substituted: %s\n",foo);
27  
28    free (foo);
29  
30    callsystem_argv_pushback (&args, "TESTIT");
31  
32    /* most simple one */
33    err = callsystem ("echo", args, 0, 0, 0, 0, 0, 0, &child);
34    if (err == -1)
35      {
36        fprintf(stderr, "error callsystem failed %d\n",(int)GetLastError());
37        return;
38      }
39  
40    /*dont forget the finished call */
41    callsystem_finished(&child);
42  
43    /*alternatively we can poll the client, note that we can reuse &child IFF the former process has finished*/
44    err = callsystem ("echo", args, 0, 0, null, 0, 0, -1, &child);
45    if (err == -1)
46      {
47        fprintf(stderr, "error in sleep\n");
48        return;
49      }
50  
51    /* will barf, because &child is in use, don't do that*/
52    err = callsystem ("echo", args, 0, 0, 0, 0, 0, 0, &child);
53    if (err == -1)
54      {
55        printf("expected barf %d %s\n",err,strerror(errno));
56      }
57  
58    /* polling loop */
59    while (( exitcode=callsystem_running(&child)) > 255)
60      ++counter;
61    printf("\nPolling spinned %d times! %d\n",counter,exitcode);
62  
63    /* close the fd's */
64    callsystem_close(null);
65  
66    /* free argv and env resources */
67    callsystem_argv_clear(&args);
68    callsystem_env_clear(&env);
69  }
70  
71  
72  int main (void)
73  {
74    printf("Start testing ..\n");
75    usage_example();
76    printf("\n.. done.\n");
77    return 0;
78  }