Rev 8185 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
8185 | bpr | 17 | #include "wimslogd.h" |
10 | reyssat | 18 | |
19 | /* File manipulation */ |
||
20 | |||
21 | struct stat ftst; |
||
22 | |||
8155 | bpr | 23 | /* A simple front-end of stat(). */ |
10 | reyssat | 24 | int ftest(char *fname) |
25 | { |
||
26 | if(strstr(fname,"..")!=NULL) return -1; /* parent directory not allowed */ |
||
27 | if(stat(fname,&ftst)) return -1; |
||
28 | if(S_ISREG(ftst.st_mode)) { |
||
8849 | bpr | 29 | if((ftst.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))!=0) return is_exec; |
30 | else return is_file; |
||
10 | reyssat | 31 | } |
32 | if(S_ISDIR(ftst.st_mode)) return is_dir; |
||
33 | if(S_ISFIFO(ftst.st_mode)) return is_fifo; |
||
34 | if(S_ISSOCK(ftst.st_mode)) return is_socket; |
||
35 | return is_unknown; |
||
36 | } |
||
37 | |||
8155 | bpr | 38 | /* read the content of a file */ |
10 | reyssat | 39 | void readfile(char *fname, char buf[], long int buflen) |
40 | { |
||
6790 | bpr | 41 | int fd, st; |
10 | reyssat | 42 | long int l, lc; |
6790 | bpr | 43 | buf[0]=0; |
10 | reyssat | 44 | st=ftest(fname); if(st!=is_file) return; |
45 | l=ftst.st_size; if(l<=0) return; |
||
8849 | bpr | 46 | if(l>=buflen) l=buflen-1; /* silent trancation */ |
10 | reyssat | 47 | fd=open(fname,O_RDONLY); if(fd==-1) return; |
48 | lc=read(fd,buf,l); close(fd); |
||
49 | if(lc!=l) {buf[0]=0; return;} |
||
50 | buf[lc]=0; _tolinux(buf); return; |
||
51 | } |
||
52 | |||
8155 | bpr | 53 | /* datafile structure: number of records. |
54 | * tag=1 if direct access |
||
55 | */ |
||
10 | reyssat | 56 | unsigned int datafile_recordnum(char *p) |
57 | { |
||
58 | char *pp, buf[MAX_FILELEN+1]; |
||
59 | int i; |
||
60 | |||
61 | readfile(p,buf,sizeof(buf)); |
||
62 | if(buf[0]!=tag_string[1]) i=0; else i=1; |
||
8155 | bpr | 63 | for(pp=strstr(buf,tag_string); pp!=NULL; i++, pp=strstr(pp+1,tag_string)); |
10 | reyssat | 64 | return i; |
65 | } |
||
66 | |||
8155 | bpr | 67 | /* datafile structure: find record n, starting from 1 */ |
10 | reyssat | 68 | char *datafile_fnd_record(char *p, int n, char bf[]) |
69 | { |
||
70 | char *pp, *p2, buf[MAX_FILELEN+1]; |
||
71 | int i; |
||
72 | |||
73 | bf[0]=0; |
||
74 | if(n<0) return bf; |
||
75 | readfile(p,buf,sizeof(buf)); |
||
76 | if(buf[0]!=tag_string[1]) i=0; else i=1; |
||
77 | if(i<n) { |
||
8849 | bpr | 78 | for(i++, pp=strstr(buf,tag_string); |
79 | i<n && pp!=NULL; |
||
80 | i++, pp=strstr(pp+1,tag_string)); |
||
10 | reyssat | 81 | } |
82 | else { |
||
8849 | bpr | 83 | if(i>n) goto end; |
84 | pp=buf-1; |
||
10 | reyssat | 85 | } |
86 | if(pp==NULL) { /* n too big */ |
||
8849 | bpr | 87 | goto end; |
10 | reyssat | 88 | } |
89 | if(n>0) pp+=strlen(tag_string); else pp=buf; |
||
90 | p2=strstr(pp,tag_string); if(p2) *p2=0; |
||
91 | mystrncpy(bf,pp,MAX_LINELEN); |
||
92 | end: return bf; |
||
93 | } |