blob: 994436f0ad023fcd1e4a0c16c6c49898ba14fb9f [file] [log] [blame]
Jon Loeliger879e4d22008-10-03 11:12:33 -05001/*
2 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
3 *
Simon Glass492f9d52011-07-05 12:02:49 -07004 * util_is_printable_string contributed by
5 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
6 *
Jon Loeliger879e4d22008-10-03 11:12:33 -05007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 */
22
Simon Glass492f9d52011-07-05 12:02:49 -070023#include <ctype.h>
David Gibson87658742010-03-03 16:38:01 +110024#include <stdio.h>
25#include <stdlib.h>
26#include <stdarg.h>
27#include <string.h>
28
29#include "util.h"
Jon Loeliger879e4d22008-10-03 11:12:33 -050030
31char *xstrdup(const char *s)
32{
33 int len = strlen(s) + 1;
34 char *dup = xmalloc(len);
35
36 memcpy(dup, s, len);
37
38 return dup;
39}
David Gibsond68cb362009-12-08 14:24:42 +110040
41char *join_path(const char *path, const char *name)
42{
43 int lenp = strlen(path);
44 int lenn = strlen(name);
45 int len;
46 int needslash = 1;
47 char *str;
48
49 len = lenp + lenn + 2;
50 if ((lenp > 0) && (path[lenp-1] == '/')) {
51 needslash = 0;
52 len--;
53 }
54
55 str = xmalloc(len);
56 memcpy(str, path, lenp);
57 if (needslash) {
58 str[lenp] = '/';
59 lenp++;
60 }
61 memcpy(str+lenp, name, lenn+1);
62 return str;
63}
Simon Glass492f9d52011-07-05 12:02:49 -070064
65int util_is_printable_string(const void *data, int len)
66{
67 const char *s = data;
68 const char *ss;
69
70 /* zero length is not */
71 if (len == 0)
72 return 0;
73
74 /* must terminate with zero */
75 if (s[len - 1] != '\0')
76 return 0;
77
78 ss = s;
79 while (*s && isprint(*s))
80 s++;
81
82 /* not zero, or not done yet */
83 if (*s != '\0' || (s + 1 - ss) < len)
84 return 0;
85
86 return 1;
87}