1 /* *****************************************************************************
2    Beg of hello_world.h header file which will be used in the following examples
3    ****************************************************************************/
4 #ifndef HELLO_WORLD_H
5 #define HELLO_WORLD_H
6 
7 /* ************************************************************************** */
8 /* headers: Vstr (and all supporting system headers), plus extra ones we need */
9 /* ************************************************************************** */
10 
11 #define VSTR_COMPILE_INCLUDE 1 /* make Vstr include it's system headers */
12 #include <vstr.h>
13 #include <errno.h>
14 #include <err.h> /* BSD/Linux header see: man errx */
15 #include <unistd.h> /* for STDOUT_FILENO */
16 
17 /* ********************************* */
18 /* generic POSIX IO helper functions */
19 /* ********************************* */
20 
21 #define IO_OK    0
22 /* the missing values will be explained later in the tutorial... */
23 #define IO_NONE  3
24 
io_put(Vstr_base * io_w,int fd)25 static int io_put(Vstr_base *io_w, int fd)
26 { /* assumes POSIX */
27   if (!io_w->len)
28     return (IO_NONE);
29 
30   if (!vstr_sc_write_fd(io_w, 1, io_w->len, fd, NULL))
31   {
32     if (errno != EINTR)
33       err(EXIT_FAILURE, "write");
34   }
35 
36   return (IO_OK);
37 }
38 
39 /* ************************ */
40 /* generic helper functions */
41 /* ************************ */
42 
43 /* hello world init function, init library and create a string */
hw_init(void)44 static Vstr_base *hw_init(void)
45 {
46   Vstr_base *s1 = NULL;
47 
48   if (!vstr_init())
49     errno = ENOMEM, err(EXIT_FAILURE, "init");
50 
51   if (!(s1 = vstr_make_base(NULL))) /* create an empty string */
52     errno = ENOMEM, err(EXIT_FAILURE, "Create string");
53 
54   return (s1);
55 }
56 
57 /* hello world exit function, cleanup what was allocated in hw_init() */
hw_exit(Vstr_base * s1)58 static int hw_exit(Vstr_base *s1)
59 {
60   vstr_free_base(s1);
61 
62   vstr_exit();
63 
64   return (EXIT_SUCCESS);
65 }
66 
67 #endif
68 /* *****************************************************************************
69    End of hello_world.h header file which will be used in the following examples
70    ****************************************************************************/
71