@@ -183,10 +183,12 @@ char* ctime(const time_t *tim_p)
183
183
}
184
184
RTM_EXPORT (ctime );
185
185
186
- static void get_timeval (struct timeval * tv )
186
+ /*-1 failure; 1 success*/
187
+ static int get_timeval (struct timeval * tv )
187
188
{
188
189
if (tv == RT_NULL )
189
- return ;
190
+ return -1 ;
191
+
190
192
/* default is not available */
191
193
tv -> tv_sec = -1 ;
192
194
/* default is 0 */
@@ -217,8 +219,11 @@ static void get_timeval(struct timeval *tv)
217
219
{
218
220
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
219
221
rt_kprintf ("Cannot find a RTC device to provide time!\r\n" );
220
- errno = ENOSYS ;
222
+ tv -> tv_sec = 0 ;
223
+ return -1 ;
221
224
}
225
+
226
+ return 1 ;
222
227
}
223
228
224
229
/**
@@ -234,13 +239,19 @@ RT_WEAK time_t time(time_t *t)
234
239
{
235
240
struct timeval now ;
236
241
237
- get_timeval (& now );
238
-
239
- if (t )
242
+ if (get_timeval (& now )> 0 )
240
243
{
241
- * t = now .tv_sec ;
244
+ if (t )
245
+ {
246
+ * t = now .tv_sec ;
247
+ }
248
+ return now .tv_sec ;
249
+ }
250
+ else
251
+ {
252
+ errno = EFAULT ;
253
+ return -1 ;
242
254
}
243
- return now .tv_sec ;
244
255
}
245
256
RTM_EXPORT (time );
246
257
@@ -265,13 +276,13 @@ int stime(const time_t *t)
265
276
else
266
277
{
267
278
LOG_W ("Cannot find a RTC device to provide time!" );
268
- errno = ENOSYS ;
279
+ errno = EFAULT ;
269
280
return -1 ;
270
281
}
271
282
return 0 ;
272
283
#else
273
284
LOG_W ("Cannot find a RTC device to provide time!" );
274
- errno = ENOSYS ;
285
+ errno = EFAULT ;
275
286
return -1 ;
276
287
#endif /* RT_USING_RTC */
277
288
}
@@ -355,15 +366,13 @@ RTM_EXPORT(timegm);
355
366
/* TODO: timezone */
356
367
int gettimeofday (struct timeval * tv , struct timezone * tz )
357
368
{
358
- get_timeval (tv );
359
-
360
- if (tv != RT_NULL && tv -> tv_sec != (time_t ) - 1 )
369
+ if (tv != RT_NULL && get_timeval (tv )> 0 )
361
370
{
362
371
return 0 ;
363
372
}
364
373
else
365
374
{
366
- errno = ENOSYS ;
375
+ errno = EFAULT ;
367
376
return -1 ;
368
377
}
369
378
}
@@ -374,11 +383,19 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz)
374
383
{
375
384
if (tv != RT_NULL )
376
385
{
377
- return stime ((const time_t * )& tv -> tv_sec );
386
+ if (tv -> tv_sec >= 0 && tv -> tv_usec >= 0 )
387
+ {
388
+ return stime ((const time_t * )& tv -> tv_sec );
389
+ }
390
+ else
391
+ {
392
+ errno = EINVAL ;
393
+ return -1 ;
394
+ }
378
395
}
379
396
else
380
397
{
381
- errno = ENOSYS ;
398
+ errno = EFAULT ;
382
399
return -1 ;
383
400
}
384
401
}
0 commit comments