Home | History | Annotate | Download | only in diskomizer
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #pragma ident	"@(#)time.c	1.8	09/05/26 SMI"
     28 
     29 #include "time.h"
     30 /*
     31  *  This check finds bugs of the sort 4126459 where usec is greater than
     32  *  1, 000, 000 which makes no sense.
     33  */
     34 int
     35 my_gettimeofday(struct timeval *tp, void *x)
     36 {
     37 	int stat;
     38 	int i;
     39 
     40 	for (i = 0; i <= opts.max_gettimeofday_retries; i++) {
     41 		stat = gettimeofday(tp, x);
     42 		if (stat != -1 && tp->tv_usec > 1000000) {
     43 			pfprintf(stderr, "gettimeofday: bogus usec %ld\n",
     44 			    tp->tv_usec);
     45 			continue;
     46 		}
     47 		return (stat);
     48 	}
     49 	exit(1);
     50 	/*NOTREACHED*/
     51 }
     52 char *
     53 alloc_time_str_fmt(time_t tv, char *fmt)
     54 {
     55 	struct tm *tm;
     56 	char *str = NULL;
     57 	size_t size = 1024;
     58 
     59 	tm = localtime(&tv);
     60 
     61 	while ((str = realloc(str, size)) != NULL) {
     62 		if (tm == NULL) {
     63 			snprintf(str, size, "bad time");
     64 			break;
     65 		}
     66 		if (strftime(str, size, fmt, tm) != 0)
     67 			break;
     68 		size += 1024;
     69 	}
     70 	return (str);
     71 }
     72 char *
     73 alloc_time_str(time_t tv)
     74 {
     75 	return (alloc_time_str_fmt(tv, NULL));
     76 }
     77 char *
     78 alloc_time_now_fmt(char *fmt)
     79 {
     80 	struct timeval tv;
     81 	while (my_gettimeofday(&tv, NULL) == -1)
     82 		pperror("gettimeofday");
     83 
     84 	return (alloc_time_str_fmt(tv.tv_sec, fmt));
     85 }
     86 char *
     87 alloc_time_now(void)
     88 {
     89 	return (alloc_time_now_fmt(NULL));
     90 }
     91