~ubuntu-branches/ubuntu/hoary/curl/hoary-security

« back to all changes in this revision

Viewing changes to tests/libtest/lib505.c

  • Committer: Bazaar Package Importer
  • Author(s): Domenico Andreoli
  • Date: 2004-06-04 19:09:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040604190925-wy048bp31320r2z6
Tags: 7.12.0.is.7.11.2-1
* Reverted to version 7.11.2 (closes: #252348).
* Disabled support for libidn (closes: #252367). This is to leave
  curl in unstable as much similar as possible to the one in testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *                                  _   _ ____  _     
 
3
 *  Project                     ___| | | |  _ \| |    
 
4
 *                             / __| | | | |_) | |    
 
5
 *                            | (__| |_| |  _ <| |___ 
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * $Id: lib505.c,v 1.4 2004/02/05 12:34:17 bagder Exp $
 
9
 */
 
10
 
 
11
#include "test.h"
 
12
 
 
13
#ifdef HAVE_SYS_SOCKET_H
 
14
#include <sys/socket.h>
 
15
#endif
 
16
#ifdef HAVE_SYS_TYPES_
 
17
#include <sys/types.h>
 
18
#endif
 
19
#ifdef HAVE_SYS_STAT_H
 
20
#include <sys/stat.h>
 
21
#endif
 
22
#ifdef HAVE_FCNTL_H
 
23
#include <fcntl.h>
 
24
#endif
 
25
 
 
26
#ifdef HAVE_UNISTD_H
 
27
#include <unistd.h>
 
28
#endif
 
29
 
 
30
/*
 
31
 * This example shows an FTP upload, with a rename of the file just after
 
32
 * a successful upload.
 
33
 *
 
34
 * Example based on source code provided by Erick Nuwendam. Thanks!
 
35
 */
 
36
 
 
37
int test(char *URL)
 
38
{
 
39
  CURL *curl;
 
40
  CURLcode res;
 
41
  FILE *hd_src ;
 
42
  int hd ;
 
43
  struct stat file_info;
 
44
 
 
45
  struct curl_slist *headerlist=NULL;
 
46
  const char *buf_1 = "RNFR 505";
 
47
  const char *buf_2 = "RNTO 505-forreal";
 
48
 
 
49
  /* get the file size of the local file */
 
50
  hd = stat(arg2, &file_info);
 
51
  if(hd == -1) {
 
52
    /* can't open file, bail out */
 
53
    return -1;
 
54
  }
 
55
 
 
56
  if(! file_info.st_size) {
 
57
    fprintf(stderr, "WARNING: file %s has no size!\n", arg2);
 
58
    return -4;
 
59
  }
 
60
 
 
61
  /* get a FILE * of the same file, could also be made with
 
62
     fdopen() from the previous descriptor, but hey this is just 
 
63
     an example! */
 
64
  hd_src = fopen(arg2, "rb");
 
65
  if(NULL == hd_src) {
 
66
    return -2; /* if this happens things are major weird */
 
67
  }
 
68
 
 
69
  /* In windows, this will init the winsock stuff */
 
70
  curl_global_init(CURL_GLOBAL_ALL);
 
71
 
 
72
  /* get a curl handle */
 
73
  curl = curl_easy_init();
 
74
  if(curl) {
 
75
    /* build a list of commands to pass to libcurl */
 
76
    headerlist = curl_slist_append(headerlist, buf_1);
 
77
    headerlist = curl_slist_append(headerlist, buf_2);
 
78
 
 
79
    /* enable uploading */
 
80
    curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
 
81
 
 
82
    /* enable verbose */
 
83
    curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE) ;
 
84
 
 
85
    /* specify target */
 
86
    curl_easy_setopt(curl,CURLOPT_URL, URL);
 
87
 
 
88
    /* pass in that last of FTP commands to run after the transfer */
 
89
    curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
 
90
 
 
91
    /* now specify which file to upload */
 
92
    curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
 
93
 
 
94
    /* and give the size of the upload (optional) */
 
95
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
 
96
                     file_info.st_size);
 
97
 
 
98
    /* Now run off and do what you've been told! */
 
99
    res = curl_easy_perform(curl);
 
100
 
 
101
    /* clean up the FTP commands list */
 
102
    curl_slist_free_all (headerlist);
 
103
 
 
104
    /* always cleanup */
 
105
    curl_easy_cleanup(curl);
 
106
  }
 
107
  fclose(hd_src); /* close the local file */
 
108
 
 
109
  curl_global_cleanup();
 
110
  return res;
 
111
}