File: util.c

package info (click to toggle)
libsmi 0.1.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 10,364 kB
  • ctags: 2,173
  • sloc: ansic: 10,484; sh: 7,338; yacc: 6,150; lex: 1,524; makefile: 162
file content (167 lines) | stat: -rw-r--r-- 3,069 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * util.c --
 *
 *      Misc utility functions.
 *
 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
 *
 * See the file "COPYING" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * @(#) $Id: util.c,v 1.11 1999/06/09 19:43:32 strauss Exp $
 */

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "smi.h"
#include "error.h"
#include "parser-smi.h"



void *util_malloc(size_t size)
{
    return calloc(size, 1);
}



void *util_realloc(void *ptr, size_t size)
{
    return realloc(ptr, size);
}



char *util_strdup(const char *s1)
{
    if (s1) {
	return strdup(s1);
    } else {
	return NULL;
    }
}



char *util_strndup(const char *s1, size_t n)
{
    char *p;
    
    p = util_malloc(n+1);
    strncpy(p, s1, n);
    p[n] = 0;
    return p;
}



int util_strcmp(const char *s1, const char *s2)
{
    if ((!s1) || (!s2))
	return -1;
    
    return strcmp(s1, s2);
}



char *util_strcat(char **s1, char *s2)
{
    char *s;
    
    if (!s2)
	return *s1;

    if ((!s1) || (!*s1))
	return s2;

    s = util_malloc(strlen(*s1) + strlen(s2) + sizeof(char));
    sprintf(s, "%s%s", *s1, s2);

    free(*s1);

    *s1 = s;
    
    return s;
}



void util_free(void *ptr)
{
    if (ptr) {
	free(ptr);
    }
}



int util_ispath(char *s)
{
    return (strchr(s, '.') || strchr(s, '/'));
}



time_t smiMkTime(const char *s)
{
    struct tm  tm;
    char       tmp[3];
    time_t     t;
    
    tm.tm_isdst = 0;
    tm.tm_wday = 0;
    tm.tm_yday = 0;
    tm.tm_sec = 0;

    if (strlen(s) == 11) {
	/* seems to be SMIv2 11-char format yymmddhhmmZ */
	tmp[2] = 0;
	tm.tm_year = atoi(strncpy(tmp, &s[0], 2));
	tm.tm_mon = atoi(strncpy(tmp, &s[2], 2)) - 1;
	tm.tm_mday = atoi(strncpy(tmp, &s[4], 2));
	tm.tm_hour = atoi(strncpy(tmp, &s[6], 2));
	tm.tm_min = atoi(strncpy(tmp, &s[8], 2));
    } else if (strlen(s) == 13) {
	/* seems to be SMIv2 13-char format yyyymmddhhmmZ */
	tmp[4] = 0;
	tm.tm_year = atoi(strncpy(tmp, &s[0], 4)) - 1900;
	tmp[2] = 0;
	tm.tm_mon = atoi(strncpy(tmp, &s[4], 2)) - 1;
	tm.tm_mday = atoi(strncpy(tmp, &s[6], 2));
	tm.tm_hour = atoi(strncpy(tmp, &s[8], 2));
	tm.tm_min = atoi(strncpy(tmp, &s[10], 2));
    } else if (strlen(s) == 10) {
	/* seems to be SMIng 10-char format yyyy-mm-dd */
	tmp[4] = 0;
	tm.tm_year = atoi(strncpy(tmp, &s[0], 4)) - 1900;
	tmp[2] = 0;
	tm.tm_mon = atoi(strncpy(tmp, &s[5], 2)) - 1;
	tm.tm_mday = atoi(strncpy(tmp, &s[8], 2));
	tm.tm_hour = 0;
	tm.tm_min = 0;
    } else if (strlen(s) == 16) {
	/* seems to be SMIng 16-char format yyyy-mm-dd hh:mm */
	tmp[4] = 0;
	tm.tm_year = atoi(strncpy(tmp, &s[0], 4)) - 1900;
	tmp[2] = 0;
	tm.tm_mon = atoi(strncpy(tmp, &s[5], 2)) - 1;
	tm.tm_mday = atoi(strncpy(tmp, &s[8], 2));
	tm.tm_hour = atoi(strncpy(tmp, &s[11], 2));
	tm.tm_min = atoi(strncpy(tmp, &s[14], 2));
    } else {
	return 0;
    }

    putenv("TZ=UTC"); tzset();
    /* TODO: a better way to make mktime() use UTC !? */

    t = mktime(&tm);
   
    return t;
}