@@ -183,10 +183,12 @@ char* ctime(const time_t *tim_p)
183183}
184184RTM_EXPORT (ctime );
185185
186- static void get_timeval (struct timeval * tv )
186+ /*-1 failure; 1 success*/
187+ static int get_timeval (struct timeval * tv )
187188{
188189 if (tv == RT_NULL )
189- return ;
190+ return -1 ;
191+
190192 /* default is not available */
191193 tv -> tv_sec = -1 ;
192194 /* default is 0 */
@@ -217,8 +219,11 @@ static void get_timeval(struct timeval *tv)
217219 {
218220 /* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
219221 rt_kprintf ("Cannot find a RTC device to provide time!\r\n" );
220- errno = ENOSYS ;
222+ tv -> tv_sec = 0 ;
223+ return -1 ;
221224 }
225+
226+ return 1 ;
222227}
223228
224229/**
@@ -234,13 +239,19 @@ RT_WEAK time_t time(time_t *t)
234239{
235240 struct timeval now ;
236241
237- get_timeval (& now );
238-
239- if (t )
242+ if (get_timeval (& now )> 0 )
240243 {
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 ;
242254 }
243- return now .tv_sec ;
244255}
245256RTM_EXPORT (time );
246257
@@ -265,13 +276,13 @@ int stime(const time_t *t)
265276 else
266277 {
267278 LOG_W ("Cannot find a RTC device to provide time!" );
268- errno = ENOSYS ;
279+ errno = EFAULT ;
269280 return -1 ;
270281 }
271282 return 0 ;
272283#else
273284 LOG_W ("Cannot find a RTC device to provide time!" );
274- errno = ENOSYS ;
285+ errno = EFAULT ;
275286 return -1 ;
276287#endif /* RT_USING_RTC */
277288}
@@ -355,15 +366,13 @@ RTM_EXPORT(timegm);
355366/* TODO: timezone */
356367int gettimeofday (struct timeval * tv , struct timezone * tz )
357368{
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 )
361370 {
362371 return 0 ;
363372 }
364373 else
365374 {
366- errno = ENOSYS ;
375+ errno = EFAULT ;
367376 return -1 ;
368377 }
369378}
@@ -374,11 +383,19 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz)
374383{
375384 if (tv != RT_NULL )
376385 {
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+ }
378395 }
379396 else
380397 {
381- errno = ENOSYS ;
398+ errno = EFAULT ;
382399 return -1 ;
383400 }
384401}
0 commit comments