summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--misc.c71
-rw-r--r--misc.h3
2 files changed, 53 insertions, 21 deletions
diff --git a/misc.c b/misc.c
index 554ceb0b1..3e661ac77 100644
--- a/misc.c
+++ b/misc.c
@@ -1,29 +1,23 @@
1/* $OpenBSD: misc.c,v 1.147 2020/04/25 06:59:36 dtucker Exp $ */ 1/* $OpenBSD: misc.c,v 1.148 2020/05/26 01:06:52 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005-2020 Damien Miller. All rights reserved.
5 * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
5 * 6 *
6 * Redistribution and use in source and binary forms, with or without 7 * Permission to use, copy, modify, and distribute this software for any
7 * modification, are permitted provided that the following conditions 8 * purpose with or without fee is hereby granted, provided that the above
8 * are met: 9 * copyright notice and this permission notice appear in all copies.
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 10 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 18 */
26 19
20
27#include "includes.h" 21#include "includes.h"
28 22
29#include <sys/types.h> 23#include <sys/types.h>
@@ -551,6 +545,43 @@ convtime(const char *s)
551 return total; 545 return total;
552} 546}
553 547
548#define TF_BUFS 8
549#define TF_LEN 9
550
551const char *
552fmt_timeframe(time_t t)
553{
554 char *buf;
555 static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */
556 static int idx = 0;
557 unsigned int sec, min, hrs, day;
558 unsigned long long week;
559
560 buf = tfbuf[idx++];
561 if (idx == TF_BUFS)
562 idx = 0;
563
564 week = t;
565
566 sec = week % 60;
567 week /= 60;
568 min = week % 60;
569 week /= 60;
570 hrs = week % 24;
571 week /= 24;
572 day = week % 7;
573 week /= 7;
574
575 if (week > 0)
576 snprintf(buf, TF_LEN, "%02lluw%01ud%02uh", week, day, hrs);
577 else if (day > 0)
578 snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
579 else
580 snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec);
581
582 return (buf);
583}
584
554/* 585/*
555 * Returns a standardized host+port identifier string. 586 * Returns a standardized host+port identifier string.
556 * Caller must free returned string. 587 * Caller must free returned string.
diff --git a/misc.h b/misc.h
index 4a05db2da..8e4234b65 100644
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.h,v 1.84 2020/01/24 23:54:40 djm Exp $ */ 1/* $OpenBSD: misc.h,v 1.85 2020/05/26 01:06:52 djm Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -66,6 +66,7 @@ int parse_user_host_path(const char *, char **, char **, char **);
66int parse_user_host_port(const char *, char **, char **, int *); 66int parse_user_host_port(const char *, char **, char **, int *);
67int parse_uri(const char *, const char *, char **, char **, int *, char **); 67int parse_uri(const char *, const char *, char **, char **, int *, char **);
68long convtime(const char *); 68long convtime(const char *);
69const char *fmt_timeframe(time_t t);
69char *tilde_expand_filename(const char *, uid_t); 70char *tilde_expand_filename(const char *, uid_t);
70char *percent_expand(const char *, ...) __attribute__((__sentinel__)); 71char *percent_expand(const char *, ...) __attribute__((__sentinel__));
71char *tohex(const void *, size_t); 72char *tohex(const void *, size_t);