tizen 2.3 release
[framework/connectivity/bluez.git] / src / shared / util.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012-2014  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <limits.h>
36
37 #include "src/shared/util.h"
38
39 void util_debug(util_debug_func_t function, void *user_data,
40                                                 const char *format, ...)
41 {
42         char str[78];
43         va_list ap;
44
45         if (!function || !format)
46                 return;
47
48         va_start(ap, format);
49         vsnprintf(str, sizeof(str), format, ap);
50         va_end(ap);
51
52         function(str, user_data);
53 }
54
55 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
56                                 util_debug_func_t function, void *user_data)
57 {
58         static const char hexdigits[] = "0123456789abcdef";
59         char str[68];
60         size_t i;
61
62         if (!function || !len)
63                 return;
64
65         str[0] = dir;
66
67         for (i = 0; i < len; i++) {
68                 str[((i % 16) * 3) + 1] = ' ';
69                 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
70                 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
71                 str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
72
73                 if ((i + 1) % 16 == 0) {
74                         str[49] = ' ';
75                         str[50] = ' ';
76                         str[67] = '\0';
77                         function(str, user_data);
78                         str[0] = ' ';
79                 }
80         }
81
82         if (i % 16 > 0) {
83                 size_t j;
84                 for (j = (i % 16); j < 16; j++) {
85                         str[(j * 3) + 1] = ' ';
86                         str[(j * 3) + 2] = ' ';
87                         str[(j * 3) + 3] = ' ';
88                         str[j + 51] = ' ';
89                 }
90                 str[49] = ' ';
91                 str[50] = ' ';
92                 str[67] = '\0';
93                 function(str, user_data);
94         }
95 }
96
97 /* Helper for getting the dirent type in case readdir returns DT_UNKNOWN */
98 unsigned char util_get_dt(const char *parent, const char *name)
99 {
100         char filename[PATH_MAX];
101         struct stat st;
102
103         snprintf(filename, sizeof(filename), "%s/%s", parent, name);
104         if (lstat(filename, &st) == 0 && S_ISDIR(st.st_mode))
105                 return DT_DIR;
106
107         return DT_UNKNOWN;
108 }