• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

ut.c

Go to the documentation of this file.
00001 /*
00002  *$Id$
00003  *
00004  * various general purpose functions
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * Permission to use, copy, modify, and distribute this software for any
00009  * purpose with or without fee is hereby granted, provided that the above
00010  * copyright notice and this permission notice appear in all copies.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00013  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00014  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00015  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00016  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00017  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00018  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00019  *
00020  */
00021 
00022 
00029 #include <sys/types.h>
00030 #include <pwd.h>
00031 #include <grp.h>
00032 #include <stdlib.h>
00033 #include <time.h>
00034 #include <sys/utsname.h> /* uname() */
00035 #include <libgen.h>
00036 
00037 
00038 #include "ut.h"
00039 #include "mem/mem.h"
00040 #include "globals.h"
00041 
00042 /* global buffer for ut.h int2str() */
00043 char ut_buf_int2str[INT2STR_MAX_LEN];
00044 
00045 
00046 /* converts a username into uid:gid,
00047  * returns -1 on error & 0 on success */
00048 int user2uid(int* uid, int* gid, char* user)
00049 {
00050         char* tmp;
00051         struct passwd *pw_entry;
00052         
00053         if (user){
00054                 *uid=strtol(user, &tmp, 10);
00055                 if ((tmp==0) ||(*tmp)){
00056                         /* maybe it's a string */
00057                         pw_entry=getpwnam(user);
00058                         if (pw_entry==0){
00059                                 goto error;
00060                         }
00061                         *uid=pw_entry->pw_uid;
00062                         if (gid) *gid=pw_entry->pw_gid;
00063                 }
00064                 return 0;
00065         }
00066 error:
00067         return -1;
00068 }
00069 
00070 
00071 
00072 /* converts a group name into a gid
00073  * returns -1 on error, 0 on success */
00074 int group2gid(int* gid, char* group)
00075 {
00076         char* tmp;
00077         struct group  *gr_entry;
00078         
00079         if (group){
00080                 *gid=strtol(group, &tmp, 10);
00081                 if ((tmp==0) ||(*tmp)){
00082                         /* maybe it's a string */
00083                         gr_entry=getgrnam(group);
00084                         if (gr_entry==0){
00085                                 goto error;
00086                         }
00087                         *gid=gr_entry->gr_gid;
00088                 }
00089                 return 0;
00090         }
00091  error:
00092         return -1;
00093 }
00094 
00095 
00096 /*
00097  * Replacement of timegm (does not exists on all platforms
00098  * Taken from 
00099  * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
00100  */
00101 time_t _timegm(struct tm* t)
00102 {
00103         time_t tl, tb;
00104         struct tm* tg;
00105 
00106         t->tm_isdst = 0;
00107         tl = mktime(t);
00108         if (tl == -1) {
00109                 t->tm_hour--;
00110                 tl = mktime (t);
00111                 if (tl == -1) {
00112                         return -1; /* can't deal with output from strptime */
00113                 }
00114                 tl += 3600;
00115         }
00116         
00117         tg = gmtime(&tl);
00118         tg->tm_isdst = 0;
00119         tb = mktime(tg);
00120         if (tb == -1) {
00121                 tg->tm_hour--;
00122                 tb = mktime (tg);
00123                 if (tb == -1) {
00124                         return -1; /* can't deal with output from gmtime */
00125                 }
00126                 tb += 3600;
00127         }
00128         return (tl - (tb - tl));
00129 }
00130 
00131 
00132 /* Convert time_t value that is relative to local timezone to UTC */
00133 time_t local2utc(time_t in)
00134 {
00135         struct tm* tt;
00136         tt = gmtime(&in);
00137         tt->tm_isdst = -1;
00138         return mktime(tt);
00139 }
00140 
00141 
00142 /* Convert time_t value in UTC to to value relative to local time zone */
00143 time_t utc2local(time_t in)
00144 {
00145         struct tm* tt;
00146         tt = localtime(&in);
00147 #ifdef HAVE_TIMEGM
00148         return timegm(tt);
00149 #else
00150         return _timegm(tt);
00151 #endif
00152 }
00153 
00154 
00155 /*
00156  * Return str as zero terminated string allocated
00157  * using pkg_malloc
00158  */
00159 char* as_asciiz(str* s)
00160 {
00161     char* r;
00162 
00163     r = (char*)pkg_malloc(s->len + 1);
00164     if (!r) {
00165                 ERR("Out of memory\n");
00166                 return 0;
00167     }
00168     memcpy(r, s->s, s->len);
00169     r[s->len] = '\0';
00170     return r;
00171 }
00172 
00173 
00174 
00175 /* return system version (major.minor.minor2) as
00176  *  (major<<16)|(minor)<<8|(minor2)
00177  * (if some of them are missing, they are set to 0)
00178  * if the parameters are not null they are set to the coresp. part 
00179  */
00180 unsigned int get_sys_version(int* major, int* minor, int* minor2)
00181 {
00182         struct utsname un;
00183         int m1;
00184         int m2;
00185         int m3;
00186         char* p;
00187         
00188         memset (&un, 0, sizeof(un));
00189         m1=m2=m3=0;
00190         /* get sys version */
00191         uname(&un);
00192         m1=strtol(un.release, &p, 10);
00193         if (*p=='.'){
00194                 p++;
00195                 m2=strtol(p, &p, 10);
00196                 if (*p=='.'){
00197                         p++;
00198                         m3=strtol(p, &p, 10);
00199                 }
00200         }
00201         if (major) *major=m1;
00202         if (minor) *minor=m2;
00203         if (minor2) *minor2=m3;
00204         return ((m1<<16)|(m2<<8)|(m3));
00205 }
00206 
00207 
00208 
00218 char* get_abs_pathname(str* base, str* file)
00219 {
00220         str ser_cfg;
00221         char* buf, *dir, *res;
00222         int len;
00223         
00224         if (base == NULL) {
00225                 ser_cfg.s = cfg_file;
00226                 ser_cfg.len = strlen(cfg_file);
00227                 base = &ser_cfg;
00228         }
00229         
00230         if (!base->s || base->len <= 0 || base->s[0] != '/') {
00231                 BUG("get_abs_pathname: Base file must be absolute pathname: "
00232                         "'%.*s'\n", STR_FMT(base));
00233                 return NULL;
00234         }
00235         
00236         if (!file || !file->s || file->len <= 0) {
00237                 BUG("get_abs_pathname: Invalid 'file' parameter\n");
00238                 return NULL;
00239         }
00240         
00241         if (file->s[0] == '/') {
00242                 /* This is an absolute pathname, make a zero terminated
00243                  * copy and use it as it is */
00244                 if ((res = pkg_malloc(file->len+1)) == NULL) {
00245                         ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
00246                         return NULL;
00247                 }
00248                 memcpy(res, file->s, file->len);
00249                 res[file->len]=0;
00250         } else {
00251                 /* This is not an absolute pathname, make it relative
00252                  * to the location of the base file
00253                  */
00254                 /* Make a copy, function dirname may modify the string */
00255                 if ((buf = pkg_malloc(base->len+1)) == NULL) {
00256                         ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
00257                         return NULL;
00258                 }
00259                 memcpy(buf, base->s, base->len);
00260                 buf[base->len]=0;
00261                 dir = dirname(buf);
00262                 
00263                 len = strlen(dir);
00264                 if ((res = pkg_malloc(len + 1 + file->len + 1)) == NULL) {
00265                         ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
00266                         pkg_free(buf);
00267                         return NULL;
00268                 }
00269                 memcpy(res, dir, len);
00270                 res[len] = '/';
00271                 memcpy(res + len + 1, file->s, file->len);
00272                 res[len + 1 + file->len] = '\0';
00273                 pkg_free(buf);
00274         }
00275         return res;
00276 }
00277 
00278 
00284 char *str_search(str *text, str *needle)
00285 {
00286     char *p;
00287 
00288     if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
00289                         || text->len<needle->len)
00290         return NULL;
00291 
00292     for (p = text->s; p <= text->s + text->len - needle->len; p++) {
00293         if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
00294             return p;
00295         }
00296     }
00297 
00298     return NULL;
00299 }

Generated on Tue May 22 2012 13:10:18 for SIP Router by  doxygen 1.7.1