1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1999.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
27 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
31 * $Source: /cvsroot/curl/curl/lib/file.c,v $
32 * $Revision: 1.1.1.1 $
33 * $Date: 1999-12-29 14:21:21 $
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 /* -- WIN32 approved -- */
47 #include <sys/types.h>
54 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
60 #ifdef HAVE_SYS_SOCKET_H
61 #include <sys/socket.h>
63 #include <netinet/in.h>
65 #include <sys/resource.h>
70 #ifdef HAVE_ARPA_INET_H
71 #include <arpa/inet.h>
76 #include <sys/ioctl.h>
79 #ifdef HAVE_SYS_PARAM_H
80 #include <sys/param.h>
83 #ifdef HAVE_SYS_STAT_H
94 #include <curl/curl.h>
99 #define _MPRINTF_REPLACE /* use our functions only */
100 #include <curl/mprintf.h>
103 UrgError
file(struct UrlData
*data
, char *path
, long *bytecountp
)
105 /* This implementation ignores the host name in conformance with
106 RFC 1738. Only local files (reachable via the standard file system)
107 are supported. This means that files on remotely mounted directories
108 (via NFS, Samba, NT sharing) can be accessed through a file:// URL
112 size_t expected_size
=-1;
114 char *buf
= data
->buffer
;
116 struct timeval start
= tvnow();
117 struct timeval now
= start
;
119 char *actual_path
= curl_unescape(path
);
124 /* change path separators from '/' to '\\' for Windows */
125 for (i
=0; actual_path
[i
] != '\0'; ++i
)
126 if (actual_path
[i
] == '/')
127 actual_path
[i
] = '\\';
129 fd
= open(actual_path
, O_RDONLY
| O_BINARY
); /* no CR/LF translation! */
131 fd
= open(actual_path
, O_RDONLY
);
136 failf(data
, "Couldn't open file %s", path
);
137 return URG_FILE_COULDNT_READ_FILE
;
139 if( -1 != fstat(fd
, &statbuf
)) {
140 /* we could stat it, then read out the size */
141 expected_size
= statbuf
.st_size
;
144 /* The following is a shortcut implementation of file reading
145 this is both more efficient than the former call to download() and
146 it avoids problems with select() and recv() on file descriptors
148 ProgressInit (data
, expected_size
);
150 nread
= read(fd
, buf
, BUFSIZE
-1);
158 /* NOTE: The following call to fwrite does CR/LF translation on
159 Windows systems if the target is stdout. Use -O or -o parameters
160 to prevent CR/LF translation (this then goes to a binary mode
162 if(nread
!= data
->fwrite (buf
, 1, nread
, data
->out
)) {
163 failf (data
, "Failed writing output");
164 return URG_WRITE_ERROR
;
167 ProgressShow (data
, bytecount
, start
, now
, FALSE
);
170 ProgressShow (data
, bytecount
, start
, now
, TRUE
);