Back to index

wims  3.65+svn20090927
files.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* File manipulation */
00019 
00020 enum{is_file, is_dir, is_exec, is_fifo, is_socket, is_unknown};
00021 struct stat ftst;
00022 
00023        /* A simple front-end of stat(). */
00024 int ftest(char *fname)
00025 {
00026     if(strstr(fname,"..")!=NULL) return -1; /* parent directory not allowed */
00027     if(stat(fname,&ftst)) return -1;
00028     if(S_ISREG(ftst.st_mode)) {
00029        if((ftst.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))!=0) return is_exec;
00030        else return is_file;
00031     }
00032     if(S_ISDIR(ftst.st_mode)) return is_dir;
00033     if(S_ISFIFO(ftst.st_mode)) return is_fifo;
00034     if(S_ISSOCK(ftst.st_mode)) return is_socket;
00035     return is_unknown;
00036 }
00037 
00038        /* read the content of a file */
00039 void readfile(char *fname, char buf[], long int buflen)
00040 {
00041     int fd, t, st;
00042     long int l, lc;
00043     t=0; buf[0]=0;
00044     st=ftest(fname); if(st!=is_file) return;
00045     l=ftst.st_size; if(l<=0) return;
00046     if(l>=buflen) l=buflen-1;      /* silent trancation */
00047     fd=open(fname,O_RDONLY); if(fd==-1) return;
00048     lc=read(fd,buf,l); close(fd);
00049     if(lc!=l) {buf[0]=0; return;}
00050     buf[lc]=0; _tolinux(buf); return;
00051 }
00052 
00053        /* datafile structure: number of records.
00054         * tag=1 if direct access */
00055 unsigned int datafile_recordnum(char *p)
00056 {
00057     char *pp, buf[MAX_FILELEN+1];
00058     int i;
00059 
00060     readfile(p,buf,sizeof(buf));
00061     if(buf[0]!=tag_string[1]) i=0; else i=1;
00062     for(pp=strstr(buf,tag_string); pp!=NULL; i++, pp=strstr(pp+1,tag_string)); 
00063     return i;
00064 }
00065 
00066        /* datafile structure: find record n, starting from 1 */
00067 char *datafile_fnd_record(char *p, int n, char bf[])
00068 {
00069     char *pp, *p2, buf[MAX_FILELEN+1];
00070     int i;
00071 
00072     bf[0]=0;
00073     if(n<0) return bf;
00074     readfile(p,buf,sizeof(buf));
00075     if(buf[0]!=tag_string[1]) i=0; else i=1;
00076     if(i<n) {
00077        for(i++, pp=strstr(buf,tag_string);
00078            i<n && pp!=NULL;
00079            i++, pp=strstr(pp+1,tag_string));
00080     }
00081     else {
00082        if(i>n) goto end;
00083        pp=buf-1;
00084     }
00085     if(pp==NULL) { /* n too big */
00086        goto end;
00087     }
00088     if(n>0) pp+=strlen(tag_string); else pp=buf;
00089     p2=strstr(pp,tag_string); if(p2) *p2=0;
00090     mystrncpy(bf,pp,MAX_LINELEN);
00091     end: return bf;
00092 }
00093