~ubuntu-branches/ubuntu/trusty/curl/trusty-proposed

« back to all changes in this revision

Viewing changes to docs/examples/pop3-multi.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2014-01-31 08:42:28 UTC
  • mfrom: (3.4.45 sid)
  • Revision ID: package-import@ubuntu.com-20140131084228-rnste9wj6fqiy9zl
Tags: 7.35.0-1ubuntu1
* Resynchronize on Debian, remaining changes:
  - Drop dependencies not in main:
    + Build-Depends: Drop stunnel4 and libssh2-1-dev.
    + Drop libssh2-1-dev from binary package Depends.
  - Add new libcurl3-udeb package.
  - Add new curl-udeb package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * This software is licensed as described in the file COPYING, which
 
11
 * you should have received as part of this distribution. The terms
 
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
13
 *
 
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
15
 * copies of the Software, and permit persons to whom the Software is
 
16
 * furnished to do so, under the terms of the COPYING file.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 ***************************************************************************/
 
22
#include <stdio.h>
 
23
#include <curl/curl.h>
 
24
 
 
25
/* This is a simple example showing how to retrieve mail using libcurl's POP3
 
26
 * capabilities. It builds on the pop3-retr.c example to demonstrate how to use
 
27
 * libcurl's multi interface.
 
28
 *
 
29
 * Note that this example requires libcurl 7.20.0 or above.
 
30
 */
 
31
 
 
32
#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
 
33
 
 
34
static struct timeval tvnow(void)
 
35
{
 
36
  struct timeval now;
 
37
 
 
38
  /* time() returns the value of time in seconds since the epoch */
 
39
  now.tv_sec = (long)time(NULL);
 
40
  now.tv_usec = 0;
 
41
 
 
42
  return now;
 
43
}
 
44
 
 
45
static long tvdiff(struct timeval newer, struct timeval older)
 
46
{
 
47
  return (newer.tv_sec - older.tv_sec) * 1000 +
 
48
    (newer.tv_usec - older.tv_usec) / 1000;
 
49
}
 
50
 
 
51
int main(void)
 
52
{
 
53
  CURL *curl;
 
54
  CURLM *mcurl;
 
55
  int still_running = 1;
 
56
  struct timeval mp_start;
 
57
 
 
58
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
59
 
 
60
  curl = curl_easy_init();
 
61
  if(!curl)
 
62
    return 1;
 
63
 
 
64
  mcurl = curl_multi_init();
 
65
  if(!mcurl)
 
66
    return 2;
 
67
 
 
68
  /* Set username and password */
 
69
  curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
 
70
  curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
 
71
 
 
72
  /* This will retreive message 1 from the user's mailbox */
 
73
  curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
 
74
 
 
75
  /* Tell the multi stack about our easy handle */
 
76
  curl_multi_add_handle(mcurl, curl);
 
77
 
 
78
  /* Record the start time which we can use later */
 
79
  mp_start = tvnow();
 
80
 
 
81
  /* We start some action by calling perform right away */
 
82
  curl_multi_perform(mcurl, &still_running);
 
83
 
 
84
  while(still_running) {
 
85
    struct timeval timeout;
 
86
    fd_set fdread;
 
87
    fd_set fdwrite;
 
88
    fd_set fdexcep;
 
89
    int maxfd = -1;
 
90
    int rc;
 
91
 
 
92
    long curl_timeo = -1;
 
93
 
 
94
    /* Initialise the file descriptors */
 
95
    FD_ZERO(&fdread);
 
96
    FD_ZERO(&fdwrite);
 
97
    FD_ZERO(&fdexcep);
 
98
 
 
99
    /* Set a suitable timeout to play around with */
 
100
    timeout.tv_sec = 1;
 
101
    timeout.tv_usec = 0;
 
102
 
 
103
    curl_multi_timeout(mcurl, &curl_timeo);
 
104
    if(curl_timeo >= 0) {
 
105
      timeout.tv_sec = curl_timeo / 1000;
 
106
      if(timeout.tv_sec > 1)
 
107
        timeout.tv_sec = 1;
 
108
      else
 
109
        timeout.tv_usec = (curl_timeo % 1000) * 1000;
 
110
    }
 
111
 
 
112
    /* Get file descriptors from the transfers */
 
113
    curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
 
114
 
 
115
    /* In a real-world program you OF COURSE check the return code of the
 
116
       function calls. On success, the value of maxfd is guaranteed to be
 
117
       greater or equal than -1. We call select(maxfd + 1, ...), specially in
 
118
       case of (maxfd == -1), we call select(0, ...), which is basically equal
 
119
       to sleep. */
 
120
    rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
 
121
 
 
122
    if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
 
123
      fprintf(stderr,
 
124
              "ABORTING: Since it seems that we would have run forever.\n");
 
125
      break;
 
126
    }
 
127
 
 
128
    switch(rc) {
 
129
    case -1:  /* select error */
 
130
      break;
 
131
    case 0:   /* timeout */
 
132
    default:  /* action */
 
133
      curl_multi_perform(mcurl, &still_running);
 
134
      break;
 
135
    }
 
136
  }
 
137
 
 
138
  /* Always cleanup */
 
139
  curl_multi_remove_handle(mcurl, curl);
 
140
  curl_multi_cleanup(mcurl);
 
141
  curl_easy_cleanup(curl);
 
142
  curl_global_cleanup();
 
143
 
 
144
  return 0;
 
145
}