Combine generator subdirectories into one.
[wrappi.git] / examples / remote.c
1 /* Example using wrappi to access a remote server and read a file. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <inttypes.h>
6
7 #include <wrappi.h>
8
9 int
10 main (int argc, char *argv[])
11 {
12   wrap_h *w;
13   const char *hostname;
14
15   /* We're going to use ssh to access the remote server.  This is
16    * automatic but we need a hostname on the command line.
17    */
18   if (argc < 2) {
19     fprintf (stderr, "missing parameter: give the remote hostname\n");
20     exit (EXIT_FAILURE);
21   }
22   hostname = argv[1];
23
24   w = wrap_create ();
25   if (!w) {
26     fprintf (stderr, "could not allocate wrappi handle\n");
27     exit (EXIT_FAILURE);
28   }
29
30   /* Set the connection method to ssh to the remote host.  If you
31    * simply comment out the next two lines, then wrappi will run the
32    * commands on the local machine.
33    */
34   wrap_set_scheme (w, WRAP_SCHEME_SSH);
35   wrap_set_hostname (w, hostname);
36 #if 1
37   wrap_set_wrappid_path (w, "/home/rjones/d/wrappi/daemon/wrappid");
38 #endif
39
40   /* Connect the handle.  Because we didn't set any error handler,
41    * errors will be printed on stderr, so we can just exit if we get
42    * an error.
43    */
44   wrap_connect (w);
45   if (wrap_error (w))
46     exit (EXIT_FAILURE);
47
48 #if 0
49   /* Read a file from the remote machine.  Most Un*x-like machines
50    * should have /etc/issue.
51    */
52   printf ("--- contents of /etc/issue from %s ---\n", hostname);
53   wrap_download (w, "/etc/issue", "/dev/stdout");
54   if (wrap_error (w))
55     exit (EXIT_FAILURE);
56 #else
57   int64_t size = wrap_filesize (w, "/etc/issue");
58   if (wrap_error (w))
59     exit (EXIT_FAILURE);
60   printf ("size of /etc/issue = %" PRIi64 " bytes\n", size);
61 #endif
62
63   wrap_close (w);
64
65   exit (EXIT_SUCCESS);
66 }