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