file.c revision 50476
137535Sdes/*- 237535Sdes * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 337535Sdes * All rights reserved. 437535Sdes * 537535Sdes * Redistribution and use in source and binary forms, with or without 637535Sdes * modification, are permitted provided that the following conditions 737535Sdes * are met: 837535Sdes * 1. Redistributions of source code must retain the above copyright 937535Sdes * notice, this list of conditions and the following disclaimer 1037535Sdes * in this position and unchanged. 1137535Sdes * 2. Redistributions in binary form must reproduce the above copyright 1237535Sdes * notice, this list of conditions and the following disclaimer in the 1337535Sdes * documentation and/or other materials provided with the distribution. 1437535Sdes * 3. The name of the author may not be used to endorse or promote products 1537535Sdes * derived from this software without specific prior written permission 1637535Sdes * 1737535Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1837535Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1937535Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2037535Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2137535Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2237535Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2337535Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2437535Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2537535Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2637535Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2737535Sdes * 2850476Speter * $FreeBSD: head/lib/libfetch/file.c 50476 1999-08-28 00:22:10Z peter $ 2937535Sdes */ 3037535Sdes 3141862Sdes#include <sys/param.h> 3240975Sdes#include <sys/stat.h> 3341989Sdes 3441989Sdes#include <dirent.h> 3537535Sdes#include <stdio.h> 3637535Sdes#include <string.h> 3737535Sdes 3837535Sdes#include "fetch.h" 3940975Sdes#include "common.h" 4037535Sdes 4137535SdesFILE * 4240975SdesfetchGetFile(struct url *u, char *flags) 4337535Sdes{ 4440975Sdes FILE *f; 4540975Sdes 4640975Sdes f = fopen(u->doc, "r"); 4740975Sdes 4840975Sdes if (f == NULL) 4940975Sdes _fetch_syserr(); 5040975Sdes return f; 5137535Sdes} 5237535Sdes 5337535SdesFILE * 5440975SdesfetchPutFile(struct url *u, char *flags) 5537535Sdes{ 5640975Sdes FILE *f; 5740975Sdes 5837535Sdes if (strchr(flags, 'a')) 5940975Sdes f = fopen(u->doc, "a"); 6040975Sdes else 6140975Sdes f = fopen(u->doc, "w"); 6240975Sdes 6340975Sdes if (f == NULL) 6440975Sdes _fetch_syserr(); 6540975Sdes return f; 6637535Sdes} 6740975Sdes 6841989Sdesstatic int 6941989Sdes_fetch_stat_file(char *fn, struct url_stat *us) 7040975Sdes{ 7140975Sdes struct stat sb; 7240975Sdes 7341989Sdes if (stat(fn, &sb) == -1) { 7440975Sdes _fetch_syserr(); 7540975Sdes return -1; 7640975Sdes } 7740975Sdes us->size = sb.st_size; 7841862Sdes us->atime = sb.st_atime; 7941862Sdes us->mtime = sb.st_mtime; 8040975Sdes return 0; 8140975Sdes} 8241989Sdes 8341989Sdesint 8441989SdesfetchStatFile(struct url *u, struct url_stat *us, char *flags) 8541989Sdes{ 8641989Sdes return _fetch_stat_file(u->doc, us); 8741989Sdes} 8841989Sdes 8941989Sdesstruct url_ent * 9041989SdesfetchListFile(struct url *u, char *flags) 9141989Sdes{ 9241989Sdes DIR *dir; 9341989Sdes struct dirent *de; 9441989Sdes struct url_stat us; 9541989Sdes struct url_ent *ue; 9641989Sdes int size, len; 9741989Sdes char fn[MAXPATHLEN], *p; 9841989Sdes int l; 9941989Sdes 10041989Sdes if ((dir = opendir(u->doc)) == NULL) { 10141989Sdes _fetch_syserr(); 10241989Sdes return NULL; 10341989Sdes } 10441989Sdes 10541989Sdes ue = NULL; 10641989Sdes strncpy(fn, u->doc, sizeof fn - 2); 10741989Sdes fn[sizeof fn - 2] = 0; 10841989Sdes strcat(fn, "/"); 10941989Sdes p = strchr(fn, 0); 11041989Sdes l = sizeof fn - strlen(fn) - 1; 11141989Sdes 11241989Sdes while ((de = readdir(dir)) != NULL) { 11341989Sdes strncpy(p, de->d_name, l - 1); 11441989Sdes p[l - 1] = 0; 11541989Sdes if (_fetch_stat_file(fn, &us) == -1) 11641989Sdes /* should I return a partial result, or abort? */ 11741989Sdes break; 11841989Sdes _fetch_add_entry(&ue, &size, &len, de->d_name, &us); 11941989Sdes } 12041989Sdes 12141989Sdes return ue; 12241989Sdes} 123