Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc. |
| 3 | * |
Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 4 | * util_is_printable_string contributed by |
| 5 | * Pantelis Antoniou <pantelis.antoniou AT gmail.com> |
| 6 | * |
Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 7 | * 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 Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 23 | #include <ctype.h> |
David Gibson | 8765874 | 2010-03-03 16:38:01 +1100 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "util.h" |
Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 30 | |
| 31 | char *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 Gibson | d68cb36 | 2009-12-08 14:24:42 +1100 | [diff] [blame] | 40 | |
| 41 | char *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 Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 64 | |
| 65 | int 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 | } |