macos-use-platform-copy-of-afssettings-20060802
[openafs.git] / src / WINNT / afsd / dosutils95.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* functions for DJGPP to write to DOS memory or duplicate Win32 functions. */
11
12 #include <stdio.h>
13 #include <sys/farptr.h>
14 #include <go32.h>
15 #include <sys/time.h>
16 #include "dosdefs95.h"
17
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19
20 dos_memset(dos_ptr offset, int val, int size)
21 {
22   int i;
23   
24   for (i = 0; i < size; i++)
25   {
26     _farpokeb(_dos_ds, offset++, val);
27   }
28 }
29     
30 char *dos_strcpy_get(char *str, unsigned int offset)
31 {
32   register char a;
33   
34   while ((a = _farpeekb(_dos_ds, offset++)) != 0)
35     *str++ = a;
36   *str = 0;
37
38   return str;
39 }
40
41 char *dos_strncpy_get(char *str, unsigned int offset, int len)
42 {
43   register char a;
44   register int n=0;
45   
46   while ((a = _farpeekb(_dos_ds, offset++)) != 0 && n++ < len)
47     *str++ = a;
48   *str = 0;
49
50   return str;
51 }
52
53 dos_ptr dos_strcpy_put(dos_ptr offset, char *str)
54 {
55   char a;
56   
57   while ((a = *str++) != 0)
58     _farpokeb(_dos_ds, offset++, a);
59   _farpokeb(_dos_ds, offset, 0);
60
61   return offset;
62 }
63
64 dos_ptr dos_strncpy_put(dos_ptr offset, char *str, int len)
65 {
66   register char a;
67   register int n=0;
68   
69   while ((a = *str++) != 0 && n++ < len)
70     _farpokeb(_dos_ds, offset++, a);
71   _farpokeb(_dos_ds, offset, 0);
72
73   return offset;
74 }
75
76 dos_ptr dos_strrchr(dos_ptr offset, char c)
77 {
78   dos_ptr temp = 0;
79   char a;
80   
81   while ((a = _farpeekb(_dos_ds, offset++)) != 0)
82   {
83     if (a == c) temp = offset-1;
84   }
85
86   return temp;
87 }
88
89 int dos_strcmp(unsigned char *str, dos_ptr offset)
90 {
91   register unsigned char a, b;
92   
93   while (((a = *str++) == (b = _farpeekb(_dos_ds, offset++))) && a && b);
94   return a-b;
95 }
96
97 int dos_strncmp(unsigned char *str, dos_ptr offset, int len)
98 {
99   register unsigned char a, b;
100   register int i=0;
101   
102   while (i++ < len && ((a = *str++) == (b = _farpeekb(_dos_ds, offset++))) && a && b);
103   return a-b;
104 }
105
106 int dos_strlen(dos_ptr offset)
107 {
108   int len=0;
109   
110   while (_farpeekb(_dos_ds, offset++))
111     len++;
112
113   return len;
114 }
115
116
117 int sub_time(struct timeval a, struct timeval b)
118 {
119   int n = a.tv_sec - b.tv_sec;
120   n *= 1000000;
121   n += a.tv_usec - b.tv_usec;
122   return n / 1000;
123 }
124
125 int tm_to_ms(struct timeval t)
126 {
127   int n = t.tv_sec * 1000;
128   n += t.tv_usec / 1000;
129   return n;
130 }
131
132 int gettime_ms()
133 {
134   struct timeval t;
135   int n = t.tv_sec * 1000;
136
137   gettimeofday(&t, NULL);
138   n += t.tv_usec / 1000;
139   return n;
140 }
141   
142 int gettime_us()
143 {
144   struct timeval t;
145   int n;
146
147   gettimeofday(&t, NULL);
148   n = t.tv_sec * 1000000;
149   n += t.tv_usec;
150   return n;
151 }
152
153 int GetPrivateProfileString(char *sect, char *key, char *def,
154                             char *buf, int len, char *file)
155 {
156   char s[256];
157   char skey[128];
158   int nchars=0;
159   int amt;
160   int offset;
161   char sectstr[256];
162   char *p;
163   FILE *f = fopen(file, "r");
164   if (!f) return 0;
165
166   sprintf(sectstr, "[%s]", sect);
167   while (1)
168   {
169     fgets(s, 256, f);
170     if (feof(f)) break;
171
172     /* look for section names */
173     if (s[0] != '[')
174       continue;
175     
176     /* if sect is NULL, copy all section names */
177     if (!sect)
178     {
179       amt = MIN(strlen(s)+1, len-1);
180       strncpy(buf, s, amt-1);
181       buf[amt] = 0;
182       len -= amt;
183       buf += amt;
184       nchars += amt;
185       continue;
186     }
187
188     /* continue if non-matching section name */
189     if (sect && strnicmp(s+1, sect, strlen(sect)) != 0)
190       continue;
191
192     /* else we have the correct section */
193
194     while (len > 0)
195     {
196       fgets(s, 256, f);
197       if (feof(f)) break;
198       
199       /* get the key part */
200       strcpy(skey, s);
201       p = strrchr(skey, '=');
202       if (!p) { fclose(f); return 0; }
203       *p = 0;
204       
205       /* continue if key doesn't match */
206       if (key && stricmp(skey, key) != 0)
207         continue;
208
209       /* if NULL key, copy key names */
210       if (!key)
211       {
212         amt = MIN(strlen(skey)+1, len-2);
213         strncpy(buf, skey, amt);
214         buf[amt] = 0;
215         buf[amt+1] = 0;   /* final trailing NULL */
216         len -= amt;
217         buf += amt;
218         nchars += amt;
219         continue;
220       }
221         
222       /* discard key= and newline */
223       offset = strlen(key) + 1;
224       amt = MIN(strlen(s+offset)-1, len-1);
225       strncpy(buf, s+offset, amt);
226       buf[amt] = 0;
227       len -= amt;
228       buf += amt;
229       nchars += amt;
230     }
231   }
232   
233   if (nchars == 0)
234   {
235     if (def)
236     {
237       strcpy(buf, def);
238       nchars = strlen(def);
239     }
240   }
241
242   fclose(f);
243   return nchars;
244 }
245
246 int WritePrivateProfileString(char *sect, char *key, char *str, char *file)
247 {
248   char tmpfile[256], s[256], sectstr[256];
249   int found = 0;
250   char *p;
251   FILE *fr = fopen(file, "r");
252   FILE *fw = fopen(tmpfile, "w");
253
254   strcpy(tmpfile, file);
255   p = strrchr(tmpfile, '.');
256   *p = 0;
257   strcat(tmpfile, ".tmp");   /* change extension to .tmp */
258   
259   sprintf(sectstr, "[%s]", sect);
260   while (1)
261   {
262     fgets(s, 256, fr);
263     if (feof(fr)) break;
264
265     fputs(s, fw);
266     
267     /* look for section names */
268     if (found || s[0] != '[')
269     {
270       continue;
271     }
272   
273     if (stricmp(s, sectstr) == 0)
274     {
275       /* found section, print new item */
276       found = 1;
277       strcpy(s, key);
278       strcat(s, "=");
279       strcat(s, str);
280       strcat(s, "\n");
281       fputs(s, fw);
282     }
283   }
284   fclose(fw);
285   fclose(fr);
286
287   /* delete old file */
288   remove(file);
289   
290   /* rename .tmp */
291   rename(tmpfile, file);
292   
293   return found;
294 }