Annotation of src/usr.bin/ftp/file.c, Revision 1.2
1.2 ! jasper 1: /* $OpenBSD$ */
! 2:
1.1 kmos 3: /*
4: * Copyright (c) 2015 Sunil Nimmagadda <sunil@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/stat.h>
20:
21: #include <err.h>
22: #include <fcntl.h>
23: #include <stdio.h>
24:
25: #include "ftp.h"
26:
27: struct imsgbuf;
28:
29: static FILE *src_fp;
30:
31: struct url *
32: file_request(struct imsgbuf *ibuf, struct url *url, off_t *offset, off_t *sz)
33: {
34: struct stat sb;
35: int src_fd;
36:
37: if ((src_fd = fd_request(url->path, O_RDONLY, NULL)) == -1)
38: err(1, "Can't open file %s", url->path);
39:
40: if (fstat(src_fd, &sb) == 0)
41: *sz = sb.st_size;
42:
43: if ((src_fp = fdopen(src_fd, "r")) == NULL)
44: err(1, "%s: fdopen", __func__);
45:
46: if (*offset && fseeko(src_fp, *offset, SEEK_SET) == -1)
47: err(1, "%s: fseeko", __func__);
48:
49: return url;
50: }
51:
52: void
53: file_save(struct url *url, FILE *dst_fp, off_t *offset)
54: {
55: copy_file(dst_fp, src_fp, offset);
56: fclose(src_fp);
57: }