OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / ez-ipupdate / cache_file.c
1 /* ============================================================================
2  * Copyright (C) 1999-2000 Angus Mackay. All rights reserved; 
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
10  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
11  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
13  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
15  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
16  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
17  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18  * POSSIBILITY OF SUCH DAMAGE.
19  * ============================================================================
20  */
21
22 /*
23  * cache_file.c
24  *
25  */
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #if HAVE_SYS_STAT_H
37 #  include <sys/stat.h>
38 #endif
39 #if HAVE_ERRNO_H
40 #  include <errno.h>
41 #endif
42 #ifdef EMBED
43 #include <fcntl.h>
44 #include <signal.h>
45 #include <config/autoconf.h>
46 #endif
47
48 #include <cache_file.h>
49 #include <dprintf.h>
50
51 #if HAVE_STRERROR
52 extern int errno;
53 #  define error_string strerror(errno)
54 #elif HAVE_SYS_ERRLIST
55 extern const char *const sys_errlist[];
56 extern int errno;
57 #  define error_string (sys_errlist[errno])
58 #else
59 #  define error_string "error message not found"
60 #endif
61
62 static void sync_file(void)
63 {
64 #ifdef CONFIG_USER_FLATFSD_FLATFSD
65   system("exec flatfsd -s");
66 #endif
67 }
68
69 int read_cache_file(char *file, time_t *date, char **ipaddr)
70 {
71   FILE *fp = NULL;
72   char buf[BUFSIZ+1];
73   char *p;
74   char *datestr;
75   char *ipstr;
76 #if HAVE_STAT
77   struct stat st;
78 #endif
79
80   // safety first
81   buf[BUFSIZ] = '\0';
82
83   // indicate failure
84   *date = 0;
85   *ipaddr = NULL;
86
87 #if HAVE_STAT
88   if(stat(file, &st) != 0)
89   {
90     if(errno == ENOENT)
91     {
92       return(0);
93     }
94     return(-1);
95   }
96 #endif
97
98   if((fp=fopen(file, "r")) == NULL)
99   {
100     return(-1);
101   }
102
103   if(fgets(buf, BUFSIZ, fp) != NULL)
104   {
105
106     /* chomp new line */
107     p = buf;
108     while(*p != '\0' && *p != '\r' && *p != '\n') { p++; }
109     *p = '\0';
110
111     /* find the first comma */
112     p = buf;
113     while(*p != '\0' && *p != ',') { p++; }
114     if(*p == '\0')
115     {
116       goto ERR;
117     }
118
119     // slap in a null
120     *p++ = '\0';
121
122     datestr = buf;
123     ipstr = p;
124
125     *date = strtoul(datestr, NULL, 10);
126     *ipaddr = strdup(ipstr);
127   }
128   else
129   {
130     *date = 0;
131     *ipaddr = NULL;
132   }
133
134   fclose(fp);
135
136   return 0;
137
138 ERR:
139
140   if(fp) { fclose(fp); }
141   return(-1);
142 }
143
144 int write_cache_file(char *file, time_t date, char *ipaddr)
145 {
146   FILE *fp = NULL;
147
148   if((fp=fopen(file, "w")) == NULL)
149   {
150     return(-1);
151   }
152
153   fprintf(fp, "%ld,%s\n", date, ipaddr);
154
155   fclose(fp);
156
157   sync_file();
158
159   return 0;
160 }
161
162 int write_block_file(char *file)
163 {
164   FILE *fp = NULL;
165
166   if((fp=fopen(file, "w")) == NULL)
167   {
168     return(-1);
169   }
170
171   fclose(fp);
172
173   sync_file();
174
175   return 0;
176 }