ut.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. *$Id$
  3. *
  4. * various general purpose functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  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.
  11. *
  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.
  19. *
  20. */
  21. /** various general purpose/utility functions.
  22. * @file ut.c
  23. * @ingroup core
  24. * Module: core
  25. */
  26. #include <sys/types.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31. #include <sys/utsname.h> /* uname() */
  32. #include <libgen.h>
  33. #include "ut.h"
  34. #include "mem/mem.h"
  35. #include "globals.h"
  36. /* global buffer for ut.h int2str() */
  37. char ut_buf_int2str[INT2STR_MAX_LEN];
  38. /* converts a username into uid:gid,
  39. * returns -1 on error & 0 on success */
  40. int user2uid(int* uid, int* gid, char* user)
  41. {
  42. char* tmp;
  43. struct passwd *pw_entry;
  44. if (user){
  45. *uid=strtol(user, &tmp, 10);
  46. if ((tmp==0) ||(*tmp)){
  47. /* maybe it's a string */
  48. pw_entry=getpwnam(user);
  49. if (pw_entry==0){
  50. goto error;
  51. }
  52. *uid=pw_entry->pw_uid;
  53. if (gid) *gid=pw_entry->pw_gid;
  54. }
  55. return 0;
  56. }
  57. error:
  58. return -1;
  59. }
  60. /* converts a group name into a gid
  61. * returns -1 on error, 0 on success */
  62. int group2gid(int* gid, char* group)
  63. {
  64. char* tmp;
  65. struct group *gr_entry;
  66. if (group){
  67. *gid=strtol(group, &tmp, 10);
  68. if ((tmp==0) ||(*tmp)){
  69. /* maybe it's a string */
  70. gr_entry=getgrnam(group);
  71. if (gr_entry==0){
  72. goto error;
  73. }
  74. *gid=gr_entry->gr_gid;
  75. }
  76. return 0;
  77. }
  78. error:
  79. return -1;
  80. }
  81. /*
  82. * Replacement of timegm (does not exists on all platforms
  83. * Taken from
  84. * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
  85. */
  86. time_t _timegm(struct tm* t)
  87. {
  88. time_t tl, tb;
  89. struct tm* tg;
  90. t->tm_isdst = 0;
  91. tl = mktime(t);
  92. if (tl == -1) {
  93. t->tm_hour--;
  94. tl = mktime (t);
  95. if (tl == -1) {
  96. return -1; /* can't deal with output from strptime */
  97. }
  98. tl += 3600;
  99. }
  100. tg = gmtime(&tl);
  101. tg->tm_isdst = 0;
  102. tb = mktime(tg);
  103. if (tb == -1) {
  104. tg->tm_hour--;
  105. tb = mktime (tg);
  106. if (tb == -1) {
  107. return -1; /* can't deal with output from gmtime */
  108. }
  109. tb += 3600;
  110. }
  111. return (tl - (tb - tl));
  112. }
  113. /* Convert time_t value that is relative to local timezone to UTC */
  114. time_t local2utc(time_t in)
  115. {
  116. struct tm* tt;
  117. tt = gmtime(&in);
  118. tt->tm_isdst = -1;
  119. return mktime(tt);
  120. }
  121. /* Convert time_t value in UTC to to value relative to local time zone */
  122. time_t utc2local(time_t in)
  123. {
  124. struct tm* tt;
  125. tt = localtime(&in);
  126. #ifdef HAVE_TIMEGM
  127. return timegm(tt);
  128. #else
  129. return _timegm(tt);
  130. #endif
  131. }
  132. /*
  133. * Return str as zero terminated string allocated
  134. * using pkg_malloc
  135. */
  136. char* as_asciiz(str* s)
  137. {
  138. char* r;
  139. r = (char*)pkg_malloc(s->len + 1);
  140. if (!r) {
  141. ERR("Out of memory\n");
  142. return 0;
  143. }
  144. memcpy(r, s->s, s->len);
  145. r[s->len] = '\0';
  146. return r;
  147. }
  148. /* return system version (major.minor.minor2) as
  149. * (major<<16)|(minor)<<8|(minor2)
  150. * (if some of them are missing, they are set to 0)
  151. * if the parameters are not null they are set to the coresp. part
  152. */
  153. unsigned int get_sys_version(int* major, int* minor, int* minor2)
  154. {
  155. struct utsname un;
  156. int m1;
  157. int m2;
  158. int m3;
  159. char* p;
  160. memset (&un, 0, sizeof(un));
  161. m1=m2=m3=0;
  162. /* get sys version */
  163. uname(&un);
  164. m1=strtol(un.release, &p, 10);
  165. if (*p=='.'){
  166. p++;
  167. m2=strtol(p, &p, 10);
  168. if (*p=='.'){
  169. p++;
  170. m3=strtol(p, &p, 10);
  171. }
  172. }
  173. if (major) *major=m1;
  174. if (minor) *minor=m2;
  175. if (minor2) *minor2=m3;
  176. return ((m1<<16)|(m2<<8)|(m3));
  177. }
  178. /** transform a relative pathname into an absolute one.
  179. * @param base - base file, used to extract the absolute path prefix.
  180. * Might be NULL, in which case the path of the ser.cfg is
  181. * used.
  182. * @param file - file path to be transformed. If it's already absolute
  183. * (starts with '/') is left alone. If not the result will
  184. * be `dirname base`/file.
  185. * @return pkg allocated asciiz string or 0 on error.
  186. */
  187. char* get_abs_pathname(str* base, str* file)
  188. {
  189. str ser_cfg;
  190. char* buf, *dir, *res;
  191. int len;
  192. if (base == NULL) {
  193. ser_cfg.s = cfg_file;
  194. ser_cfg.len = strlen(cfg_file);
  195. base = &ser_cfg;
  196. }
  197. if (!base->s || base->len <= 0 || base->s[0] != '/') {
  198. BUG("get_abs_pathname: Base file must be absolute pathname: "
  199. "'%.*s'\n", STR_FMT(base));
  200. return NULL;
  201. }
  202. if (!file || !file->s || file->len <= 0) {
  203. BUG("get_abs_pathname: Invalid 'file' parameter\n");
  204. return NULL;
  205. }
  206. if (file->s[0] == '/') {
  207. /* This is an absolute pathname, make a zero terminated
  208. * copy and use it as it is */
  209. if ((res = pkg_malloc(file->len+1)) == NULL) {
  210. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  211. return NULL;
  212. }
  213. memcpy(res, file->s, file->len);
  214. res[file->len]=0;
  215. } else {
  216. /* This is not an absolute pathname, make it relative
  217. * to the location of the base file
  218. */
  219. /* Make a copy, function dirname may modify the string */
  220. if ((buf = pkg_malloc(base->len+1)) == NULL) {
  221. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  222. return NULL;
  223. }
  224. memcpy(buf, base->s, base->len);
  225. buf[base->len]=0;
  226. dir = dirname(buf);
  227. len = strlen(dir);
  228. if ((res = pkg_malloc(len + 1 + file->len + 1)) == NULL) {
  229. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  230. pkg_free(buf);
  231. return NULL;
  232. }
  233. memcpy(res, dir, len);
  234. res[len] = '/';
  235. memcpy(res + len + 1, file->s, file->len);
  236. res[len + 1 + file->len] = '\0';
  237. pkg_free(buf);
  238. }
  239. return res;
  240. }
  241. /**
  242. * @brief search for occurence of needle in text
  243. * @return pointer to start of needle in text or NULL if the needle
  244. * is not found
  245. */
  246. char *str_search(str *text, str *needle)
  247. {
  248. char *p;
  249. if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
  250. || text->len<needle->len)
  251. return NULL;
  252. for (p = text->s; p <= text->s + text->len - needle->len; p++) {
  253. if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
  254. return p;
  255. }
  256. }
  257. return NULL;
  258. }
  259. /*
  260. * ser_memmem() returns the location of the first occurrence of data
  261. * pattern b2 of size len2 in memory block b1 of size len1 or
  262. * NULL if none is found. Obtained from NetBSD.
  263. */
  264. void * ser_memmem(const void *b1, const void *b2, size_t len1, size_t len2)
  265. {
  266. /* Initialize search pointer */
  267. char *sp = (char *) b1;
  268. /* Initialize pattern pointer */
  269. char *pp = (char *) b2;
  270. /* Initialize end of search address space pointer */
  271. char *eos = sp + len1 - len2;
  272. /* Sanity check */
  273. if(!(b1 && b2 && len1 && len2))
  274. return NULL;
  275. while (sp <= eos) {
  276. if (*sp == *pp)
  277. if (memcmp(sp, pp, len2) == 0)
  278. return sp;
  279. sp++;
  280. }
  281. return NULL;
  282. }
  283. /*
  284. * ser_memrmem() returns the location of the last occurrence of data
  285. * pattern b2 of size len2 in memory block b1 of size len1 or
  286. * NULL if none is found.
  287. */
  288. void * ser_memrmem(const void *b1, const void *b2, size_t len1, size_t len2)
  289. {
  290. /* Initialize search pointer */
  291. char *sp = (char *) b1 + len1 - len2;
  292. /* Initialize pattern pointer */
  293. char *pp = (char *) b2;
  294. /* Initialize end of search address space pointer */
  295. char *eos = (char *) b1;
  296. /* Sanity check */
  297. if(!(b1 && b2 && len1 && len2))
  298. return NULL;
  299. while (sp >= eos) {
  300. if (*sp == *pp)
  301. if (memcmp(sp, pp, len2) == 0)
  302. return sp;
  303. sp--;
  304. }
  305. return NULL;
  306. }