4 * various general purpose functions
6 * Copyright (C) 2001-2003 FhG Fokus
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 /** various general purpose/helper functions.
29 #include <sys/types.h>
34 #include <sys/utsname.h> /* uname() */
42 /* global buffer for ut.h int2str() */
43 char ut_buf_int2str[INT2STR_MAX_LEN];
46 /* converts a username into uid:gid,
47 * returns -1 on error & 0 on success */
48 int user2uid(int* uid, int* gid, char* user)
51 struct passwd *pw_entry;
54 *uid=strtol(user, &tmp, 10);
55 if ((tmp==0) ||(*tmp)){
56 /* maybe it's a string */
57 pw_entry=getpwnam(user);
61 *uid=pw_entry->pw_uid;
62 if (gid) *gid=pw_entry->pw_gid;
72 /* converts a group name into a gid
73 * returns -1 on error, 0 on success */
74 int group2gid(int* gid, char* group)
77 struct group *gr_entry;
80 *gid=strtol(group, &tmp, 10);
81 if ((tmp==0) ||(*tmp)){
82 /* maybe it's a string */
83 gr_entry=getgrnam(group);
87 *gid=gr_entry->gr_gid;
97 * Replacement of timegm (does not exists on all platforms
99 * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
101 time_t _timegm(struct tm* t)
112 return -1; /* can't deal with output from strptime */
124 return -1; /* can't deal with output from gmtime */
128 return (tl - (tb - tl));
132 /* Convert time_t value that is relative to local timezone to UTC */
133 time_t local2utc(time_t in)
142 /* Convert time_t value in UTC to to value relative to local time zone */
143 time_t utc2local(time_t in)
156 * Return str as zero terminated string allocated
159 char* as_asciiz(str* s)
163 r = (char*)pkg_malloc(s->len + 1);
165 ERR("Out of memory\n");
168 memcpy(r, s->s, s->len);
175 /* return system version (major.minor.minor2) as
176 * (major<<16)|(minor)<<8|(minor2)
177 * (if some of them are missing, they are set to 0)
178 * if the parameters are not null they are set to the coresp. part
180 unsigned int get_sys_version(int* major, int* minor, int* minor2)
188 memset (&un, 0, sizeof(un));
190 /* get sys version */
192 m1=strtol(un.release, &p, 10);
195 m2=strtol(p, &p, 10);
198 m3=strtol(p, &p, 10);
201 if (major) *major=m1;
202 if (minor) *minor=m2;
203 if (minor2) *minor2=m3;
204 return ((m1<<16)|(m2<<8)|(m3));
208 char* get_abs_pathname(str* base, str* file)
211 char* buf, *dir, *res;
215 ser_cfg.s = cfg_file;
216 ser_cfg.len = strlen(cfg_file);
220 if (!base->s || base->len <= 0 || base->s[0] != '/') {
221 BUG("get_abs_pathname: Base file must be absolute pathname: "
222 "'%.*s'\n", STR_FMT(base));
226 if (!file || !file->s || file->len <= 0) {
227 BUG("get_abs_pathname: Invalid 'file' parameter\n");
231 if (file->s[0] == '/') {
232 /* This is an absolute pathname, make a zero terminated
233 * copy and use it as it is */
234 if ((res = malloc(file->len+1)) == NULL) {
235 ERR("get_abs_pathname: No memory left (malloc failed)\n");
237 memcpy(res, file->s, file->len);
240 /* This is not an absolute pathname, make it relative
241 * to the location of the base file
243 /* Make a copy, function dirname may modify the string */
244 if ((buf = malloc(base->len+1)) == NULL) {
245 ERR("get_abs_pathname: No memory left (malloc failed)\n");
248 memcpy(buf, base->s, base->len);
253 if ((res = malloc(len + 1 + file->len + 1)) == NULL) {
254 ERR("get_abs_pathname: No memory left (malloc failed)\n");
258 memcpy(res, dir, len);
260 memcpy(res + len + 1, file->s, file->len);
261 res[len + 1 + file->len] = '\0';