| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 1 | /* | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 2 |  * Copyright 2011 The Chromium Authors, All Rights Reserved. | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 3 |  * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc. | 
 | 4 |  * | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 5 |  * util_is_printable_string contributed by | 
 | 6 |  *	Pantelis Antoniou <pantelis.antoniou AT gmail.com> | 
 | 7 |  * | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 8 |  * This program is free software; you can redistribute it and/or | 
 | 9 |  * modify it under the terms of the GNU General Public License as | 
 | 10 |  * published by the Free Software Foundation; either version 2 of the | 
 | 11 |  * License, or (at your option) any later version. | 
 | 12 |  * | 
 | 13 |  *  This program 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 |  *  General Public License for more details. | 
 | 17 |  * | 
 | 18 |  *  You should have received a copy of the GNU General Public License | 
 | 19 |  *  along with this program; if not, write to the Free Software | 
 | 20 |  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 | 
 | 21 |  *                                                                   USA | 
 | 22 |  */ | 
 | 23 |  | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 24 | #include <ctype.h> | 
| David Gibson | 8765874 | 2010-03-03 16:38:01 +1100 | [diff] [blame] | 25 | #include <stdio.h> | 
 | 26 | #include <stdlib.h> | 
 | 27 | #include <stdarg.h> | 
 | 28 | #include <string.h> | 
| Anton Staaf | b43335a | 2011-09-09 12:16:29 -0700 | [diff] [blame] | 29 | #include <assert.h> | 
| David Gibson | 8765874 | 2010-03-03 16:38:01 +1100 | [diff] [blame] | 30 |  | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 31 | #include <errno.h> | 
 | 32 | #include <fcntl.h> | 
 | 33 | #include <unistd.h> | 
 | 34 |  | 
 | 35 | #include "libfdt.h" | 
| David Gibson | 8765874 | 2010-03-03 16:38:01 +1100 | [diff] [blame] | 36 | #include "util.h" | 
| Mike Frysinger | 31be4ce | 2013-04-10 14:29:09 -0400 | [diff] [blame] | 37 | #include "version_gen.h" | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 38 |  | 
 | 39 | char *xstrdup(const char *s) | 
 | 40 | { | 
 | 41 | 	int len = strlen(s) + 1; | 
| Florian Fainelli | 24cb3d0 | 2014-02-01 16:41:59 +1100 | [diff] [blame] | 42 | 	char *d = xmalloc(len); | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 43 |  | 
| Florian Fainelli | 24cb3d0 | 2014-02-01 16:41:59 +1100 | [diff] [blame] | 44 | 	memcpy(d, s, len); | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 45 |  | 
| Florian Fainelli | 24cb3d0 | 2014-02-01 16:41:59 +1100 | [diff] [blame] | 46 | 	return d; | 
| Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 47 | } | 
| David Gibson | d68cb36 | 2009-12-08 14:24:42 +1100 | [diff] [blame] | 48 |  | 
 | 49 | char *join_path(const char *path, const char *name) | 
 | 50 | { | 
 | 51 | 	int lenp = strlen(path); | 
 | 52 | 	int lenn = strlen(name); | 
 | 53 | 	int len; | 
 | 54 | 	int needslash = 1; | 
 | 55 | 	char *str; | 
 | 56 |  | 
 | 57 | 	len = lenp + lenn + 2; | 
 | 58 | 	if ((lenp > 0) && (path[lenp-1] == '/')) { | 
 | 59 | 		needslash = 0; | 
 | 60 | 		len--; | 
 | 61 | 	} | 
 | 62 |  | 
 | 63 | 	str = xmalloc(len); | 
 | 64 | 	memcpy(str, path, lenp); | 
 | 65 | 	if (needslash) { | 
 | 66 | 		str[lenp] = '/'; | 
 | 67 | 		lenp++; | 
 | 68 | 	} | 
 | 69 | 	memcpy(str+lenp, name, lenn+1); | 
 | 70 | 	return str; | 
 | 71 | } | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 72 |  | 
| David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame] | 73 | bool util_is_printable_string(const void *data, int len) | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 74 | { | 
 | 75 | 	const char *s = data; | 
| Pantelis Antoniou | 1c1efd6 | 2013-01-04 21:12:58 +0200 | [diff] [blame] | 76 | 	const char *ss, *se; | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 77 |  | 
 | 78 | 	/* zero length is not */ | 
 | 79 | 	if (len == 0) | 
 | 80 | 		return 0; | 
 | 81 |  | 
 | 82 | 	/* must terminate with zero */ | 
 | 83 | 	if (s[len - 1] != '\0') | 
 | 84 | 		return 0; | 
 | 85 |  | 
| Pantelis Antoniou | 1c1efd6 | 2013-01-04 21:12:58 +0200 | [diff] [blame] | 86 | 	se = s + len; | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 87 |  | 
| Pantelis Antoniou | 1c1efd6 | 2013-01-04 21:12:58 +0200 | [diff] [blame] | 88 | 	while (s < se) { | 
 | 89 | 		ss = s; | 
| Serge Lamikhov-Center | 17119ab | 2013-12-25 15:26:03 +1100 | [diff] [blame] | 90 | 		while (s < se && *s && isprint((unsigned char)*s)) | 
| Pantelis Antoniou | 1c1efd6 | 2013-01-04 21:12:58 +0200 | [diff] [blame] | 91 | 			s++; | 
 | 92 |  | 
 | 93 | 		/* not zero, or not done yet */ | 
 | 94 | 		if (*s != '\0' || s == ss) | 
 | 95 | 			return 0; | 
 | 96 |  | 
 | 97 | 		s++; | 
 | 98 | 	} | 
| Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 99 |  | 
 | 100 | 	return 1; | 
 | 101 | } | 
| Anton Staaf | b43335a | 2011-09-09 12:16:29 -0700 | [diff] [blame] | 102 |  | 
 | 103 | /* | 
 | 104 |  * Parse a octal encoded character starting at index i in string s.  The | 
 | 105 |  * resulting character will be returned and the index i will be updated to | 
 | 106 |  * point at the character directly after the end of the encoding, this may be | 
 | 107 |  * the '\0' terminator of the string. | 
 | 108 |  */ | 
 | 109 | static char get_oct_char(const char *s, int *i) | 
 | 110 | { | 
 | 111 | 	char x[4]; | 
 | 112 | 	char *endx; | 
 | 113 | 	long val; | 
 | 114 |  | 
 | 115 | 	x[3] = '\0'; | 
 | 116 | 	strncpy(x, s + *i, 3); | 
 | 117 |  | 
 | 118 | 	val = strtol(x, &endx, 8); | 
 | 119 |  | 
 | 120 | 	assert(endx > x); | 
 | 121 |  | 
 | 122 | 	(*i) += endx - x; | 
 | 123 | 	return val; | 
 | 124 | } | 
 | 125 |  | 
 | 126 | /* | 
 | 127 |  * Parse a hexadecimal encoded character starting at index i in string s.  The | 
 | 128 |  * resulting character will be returned and the index i will be updated to | 
 | 129 |  * point at the character directly after the end of the encoding, this may be | 
 | 130 |  * the '\0' terminator of the string. | 
 | 131 |  */ | 
 | 132 | static char get_hex_char(const char *s, int *i) | 
 | 133 | { | 
 | 134 | 	char x[3]; | 
 | 135 | 	char *endx; | 
 | 136 | 	long val; | 
 | 137 |  | 
 | 138 | 	x[2] = '\0'; | 
 | 139 | 	strncpy(x, s + *i, 2); | 
 | 140 |  | 
 | 141 | 	val = strtol(x, &endx, 16); | 
 | 142 | 	if (!(endx  > x)) | 
 | 143 | 		die("\\x used with no following hex digits\n"); | 
 | 144 |  | 
 | 145 | 	(*i) += endx - x; | 
 | 146 | 	return val; | 
 | 147 | } | 
 | 148 |  | 
 | 149 | char get_escape_char(const char *s, int *i) | 
 | 150 | { | 
 | 151 | 	char	c = s[*i]; | 
 | 152 | 	int	j = *i + 1; | 
 | 153 | 	char	val; | 
 | 154 |  | 
| Anton Staaf | b43335a | 2011-09-09 12:16:29 -0700 | [diff] [blame] | 155 | 	switch (c) { | 
 | 156 | 	case 'a': | 
 | 157 | 		val = '\a'; | 
 | 158 | 		break; | 
 | 159 | 	case 'b': | 
 | 160 | 		val = '\b'; | 
 | 161 | 		break; | 
 | 162 | 	case 't': | 
 | 163 | 		val = '\t'; | 
 | 164 | 		break; | 
 | 165 | 	case 'n': | 
 | 166 | 		val = '\n'; | 
 | 167 | 		break; | 
 | 168 | 	case 'v': | 
 | 169 | 		val = '\v'; | 
 | 170 | 		break; | 
 | 171 | 	case 'f': | 
 | 172 | 		val = '\f'; | 
 | 173 | 		break; | 
 | 174 | 	case 'r': | 
 | 175 | 		val = '\r'; | 
 | 176 | 		break; | 
 | 177 | 	case '0': | 
 | 178 | 	case '1': | 
 | 179 | 	case '2': | 
 | 180 | 	case '3': | 
 | 181 | 	case '4': | 
 | 182 | 	case '5': | 
 | 183 | 	case '6': | 
 | 184 | 	case '7': | 
 | 185 | 		j--; /* need to re-read the first digit as | 
 | 186 | 		      * part of the octal value */ | 
 | 187 | 		val = get_oct_char(s, &j); | 
 | 188 | 		break; | 
 | 189 | 	case 'x': | 
 | 190 | 		val = get_hex_char(s, &j); | 
 | 191 | 		break; | 
 | 192 | 	default: | 
 | 193 | 		val = c; | 
 | 194 | 	} | 
 | 195 |  | 
 | 196 | 	(*i) = j; | 
 | 197 | 	return val; | 
 | 198 | } | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 199 |  | 
| Mike Frysinger | a6d55e0 | 2013-04-08 00:56:54 -0400 | [diff] [blame] | 200 | int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len) | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 201 | { | 
 | 202 | 	int fd = 0;	/* assume stdin */ | 
 | 203 | 	char *buf = NULL; | 
 | 204 | 	off_t bufsize = 1024, offset = 0; | 
 | 205 | 	int ret = 0; | 
 | 206 |  | 
 | 207 | 	*buffp = NULL; | 
 | 208 | 	if (strcmp(filename, "-") != 0) { | 
 | 209 | 		fd = open(filename, O_RDONLY); | 
 | 210 | 		if (fd < 0) | 
 | 211 | 			return errno; | 
 | 212 | 	} | 
 | 213 |  | 
 | 214 | 	/* Loop until we have read everything */ | 
| Mike Frysinger | f8cb5dd | 2013-04-10 14:29:06 -0400 | [diff] [blame] | 215 | 	buf = xmalloc(bufsize); | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 216 | 	do { | 
 | 217 | 		/* Expand the buffer to hold the next chunk */ | 
 | 218 | 		if (offset == bufsize) { | 
 | 219 | 			bufsize *= 2; | 
| Mike Frysinger | f8cb5dd | 2013-04-10 14:29:06 -0400 | [diff] [blame] | 220 | 			buf = xrealloc(buf, bufsize); | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 221 | 		} | 
 | 222 |  | 
 | 223 | 		ret = read(fd, &buf[offset], bufsize - offset); | 
 | 224 | 		if (ret < 0) { | 
 | 225 | 			ret = errno; | 
 | 226 | 			break; | 
 | 227 | 		} | 
 | 228 | 		offset += ret; | 
 | 229 | 	} while (ret != 0); | 
 | 230 |  | 
 | 231 | 	/* Clean up, including closing stdin; return errno on error */ | 
 | 232 | 	close(fd); | 
 | 233 | 	if (ret) | 
 | 234 | 		free(buf); | 
 | 235 | 	else | 
 | 236 | 		*buffp = buf; | 
| Mike Frysinger | a6d55e0 | 2013-04-08 00:56:54 -0400 | [diff] [blame] | 237 | 	*len = bufsize; | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 238 | 	return ret; | 
 | 239 | } | 
 | 240 |  | 
| Mike Frysinger | a6d55e0 | 2013-04-08 00:56:54 -0400 | [diff] [blame] | 241 | int utilfdt_read_err(const char *filename, char **buffp) | 
 | 242 | { | 
 | 243 | 	off_t len; | 
 | 244 | 	return utilfdt_read_err_len(filename, buffp, &len); | 
 | 245 | } | 
 | 246 |  | 
 | 247 | char *utilfdt_read_len(const char *filename, off_t *len) | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 248 | { | 
 | 249 | 	char *buff; | 
| Mike Frysinger | a6d55e0 | 2013-04-08 00:56:54 -0400 | [diff] [blame] | 250 | 	int ret = utilfdt_read_err_len(filename, &buff, len); | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 251 |  | 
 | 252 | 	if (ret) { | 
 | 253 | 		fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename, | 
 | 254 | 			strerror(ret)); | 
 | 255 | 		return NULL; | 
 | 256 | 	} | 
 | 257 | 	/* Successful read */ | 
 | 258 | 	return buff; | 
 | 259 | } | 
 | 260 |  | 
| Mike Frysinger | a6d55e0 | 2013-04-08 00:56:54 -0400 | [diff] [blame] | 261 | char *utilfdt_read(const char *filename) | 
 | 262 | { | 
 | 263 | 	off_t len; | 
 | 264 | 	return utilfdt_read_len(filename, &len); | 
 | 265 | } | 
 | 266 |  | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 267 | int utilfdt_write_err(const char *filename, const void *blob) | 
 | 268 | { | 
 | 269 | 	int fd = 1;	/* assume stdout */ | 
 | 270 | 	int totalsize; | 
 | 271 | 	int offset; | 
 | 272 | 	int ret = 0; | 
 | 273 | 	const char *ptr = blob; | 
 | 274 |  | 
 | 275 | 	if (strcmp(filename, "-") != 0) { | 
 | 276 | 		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | 
 | 277 | 		if (fd < 0) | 
 | 278 | 			return errno; | 
 | 279 | 	} | 
 | 280 |  | 
 | 281 | 	totalsize = fdt_totalsize(blob); | 
 | 282 | 	offset = 0; | 
 | 283 |  | 
 | 284 | 	while (offset < totalsize) { | 
 | 285 | 		ret = write(fd, ptr + offset, totalsize - offset); | 
 | 286 | 		if (ret < 0) { | 
 | 287 | 			ret = -errno; | 
 | 288 | 			break; | 
 | 289 | 		} | 
 | 290 | 		offset += ret; | 
 | 291 | 	} | 
 | 292 | 	/* Close the file/stdin; return errno on error */ | 
 | 293 | 	if (fd != 1) | 
 | 294 | 		close(fd); | 
 | 295 | 	return ret < 0 ? -ret : 0; | 
 | 296 | } | 
 | 297 |  | 
 | 298 |  | 
 | 299 | int utilfdt_write(const char *filename, const void *blob) | 
 | 300 | { | 
 | 301 | 	int ret = utilfdt_write_err(filename, blob); | 
 | 302 |  | 
 | 303 | 	if (ret) { | 
 | 304 | 		fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename, | 
 | 305 | 			strerror(ret)); | 
 | 306 | 	} | 
 | 307 | 	return ret ? -1 : 0; | 
 | 308 | } | 
 | 309 |  | 
 | 310 | int utilfdt_decode_type(const char *fmt, int *type, int *size) | 
 | 311 | { | 
 | 312 | 	int qualifier = 0; | 
 | 313 |  | 
| David Gibson | e280442 | 2012-02-03 17:06:12 +1100 | [diff] [blame] | 314 | 	if (!*fmt) | 
 | 315 | 		return -1; | 
 | 316 |  | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 317 | 	/* get the conversion qualifier */ | 
 | 318 | 	*size = -1; | 
 | 319 | 	if (strchr("hlLb", *fmt)) { | 
 | 320 | 		qualifier = *fmt++; | 
 | 321 | 		if (qualifier == *fmt) { | 
 | 322 | 			switch (*fmt++) { | 
 | 323 | /* TODO:		case 'l': qualifier = 'L'; break;*/ | 
 | 324 | 			case 'h': | 
 | 325 | 				qualifier = 'b'; | 
 | 326 | 				break; | 
 | 327 | 			} | 
 | 328 | 		} | 
 | 329 | 	} | 
 | 330 |  | 
 | 331 | 	/* we should now have a type */ | 
| David Gibson | e280442 | 2012-02-03 17:06:12 +1100 | [diff] [blame] | 332 | 	if ((*fmt == '\0') || !strchr("iuxs", *fmt)) | 
| Simon Glass | 36204fd | 2011-09-22 10:11:02 -0700 | [diff] [blame] | 333 | 		return -1; | 
 | 334 |  | 
 | 335 | 	/* convert qualifier (bhL) to byte size */ | 
 | 336 | 	if (*fmt != 's') | 
 | 337 | 		*size = qualifier == 'b' ? 1 : | 
 | 338 | 				qualifier == 'h' ? 2 : | 
 | 339 | 				qualifier == 'l' ? 4 : -1; | 
 | 340 | 	*type = *fmt++; | 
 | 341 |  | 
 | 342 | 	/* that should be it! */ | 
 | 343 | 	if (*fmt) | 
 | 344 | 		return -1; | 
 | 345 | 	return 0; | 
 | 346 | } | 
| Simon Glass | d20391d | 2013-01-21 12:59:16 -0800 | [diff] [blame] | 347 |  | 
 | 348 | void utilfdt_print_data(const char *data, int len) | 
 | 349 | { | 
 | 350 | 	int i; | 
| Simon Glass | d20391d | 2013-01-21 12:59:16 -0800 | [diff] [blame] | 351 | 	const char *s; | 
 | 352 |  | 
 | 353 | 	/* no data, don't print */ | 
 | 354 | 	if (len == 0) | 
 | 355 | 		return; | 
 | 356 |  | 
 | 357 | 	if (util_is_printable_string(data, len)) { | 
 | 358 | 		printf(" = "); | 
 | 359 |  | 
 | 360 | 		s = data; | 
 | 361 | 		do { | 
 | 362 | 			printf("\"%s\"", s); | 
 | 363 | 			s += strlen(s) + 1; | 
 | 364 | 			if (s < data + len) | 
 | 365 | 				printf(", "); | 
 | 366 | 		} while (s < data + len); | 
 | 367 |  | 
 | 368 | 	} else if ((len % 4) == 0) { | 
 | 369 | 		const uint32_t *cell = (const uint32_t *)data; | 
 | 370 |  | 
 | 371 | 		printf(" = <"); | 
| Simon Glass | c78ca72 | 2014-06-18 01:00:23 -0600 | [diff] [blame] | 372 | 		for (i = 0, len /= 4; i < len; i++) | 
 | 373 | 			printf("0x%08x%s", fdt32_to_cpu(cell[i]), | 
 | 374 | 			       i < (len - 1) ? " " : ""); | 
| Simon Glass | d20391d | 2013-01-21 12:59:16 -0800 | [diff] [blame] | 375 | 		printf(">"); | 
 | 376 | 	} else { | 
| David Gibson | e5e6df7 | 2015-07-09 13:47:19 +1000 | [diff] [blame] | 377 | 		const unsigned char *p = (const unsigned char *)data; | 
| Simon Glass | d20391d | 2013-01-21 12:59:16 -0800 | [diff] [blame] | 378 | 		printf(" = ["); | 
 | 379 | 		for (i = 0; i < len; i++) | 
 | 380 | 			printf("%02x%s", *p++, i < len - 1 ? " " : ""); | 
 | 381 | 		printf("]"); | 
 | 382 | 	} | 
 | 383 | } | 
| Mike Frysinger | 31be4ce | 2013-04-10 14:29:09 -0400 | [diff] [blame] | 384 |  | 
 | 385 | void util_version(void) | 
 | 386 | { | 
 | 387 | 	printf("Version: %s\n", DTC_VERSION); | 
 | 388 | 	exit(0); | 
 | 389 | } | 
| Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 390 |  | 
| Mike Frysinger | b9e8065 | 2013-05-24 18:04:43 +1000 | [diff] [blame] | 391 | void util_usage(const char *errmsg, const char *synopsis, | 
 | 392 | 		const char *short_opts, struct option const long_opts[], | 
 | 393 | 		const char * const opts_help[]) | 
| Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 394 | { | 
 | 395 | 	FILE *fp = errmsg ? stderr : stdout; | 
 | 396 | 	const char a_arg[] = "<arg>"; | 
 | 397 | 	size_t a_arg_len = strlen(a_arg) + 1; | 
 | 398 | 	size_t i; | 
 | 399 | 	int optlen; | 
 | 400 |  | 
 | 401 | 	fprintf(fp, | 
 | 402 | 		"Usage: %s\n" | 
 | 403 | 		"\n" | 
 | 404 | 		"Options: -[%s]\n", synopsis, short_opts); | 
 | 405 |  | 
 | 406 | 	/* prescan the --long opt length to auto-align */ | 
 | 407 | 	optlen = 0; | 
 | 408 | 	for (i = 0; long_opts[i].name; ++i) { | 
 | 409 | 		/* +1 is for space between --opt and help text */ | 
 | 410 | 		int l = strlen(long_opts[i].name) + 1; | 
 | 411 | 		if (long_opts[i].has_arg == a_argument) | 
 | 412 | 			l += a_arg_len; | 
 | 413 | 		if (optlen < l) | 
 | 414 | 			optlen = l; | 
 | 415 | 	} | 
 | 416 |  | 
 | 417 | 	for (i = 0; long_opts[i].name; ++i) { | 
 | 418 | 		/* helps when adding new applets or options */ | 
 | 419 | 		assert(opts_help[i] != NULL); | 
 | 420 |  | 
 | 421 | 		/* first output the short flag if it has one */ | 
 | 422 | 		if (long_opts[i].val > '~') | 
 | 423 | 			fprintf(fp, "      "); | 
 | 424 | 		else | 
 | 425 | 			fprintf(fp, "  -%c, ", long_opts[i].val); | 
 | 426 |  | 
 | 427 | 		/* then the long flag */ | 
 | 428 | 		if (long_opts[i].has_arg == no_argument) | 
 | 429 | 			fprintf(fp, "--%-*s", optlen, long_opts[i].name); | 
 | 430 | 		else | 
 | 431 | 			fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg, | 
 | 432 | 				(int)(optlen - strlen(long_opts[i].name) - a_arg_len), ""); | 
 | 433 |  | 
 | 434 | 		/* finally the help text */ | 
 | 435 | 		fprintf(fp, "%s\n", opts_help[i]); | 
 | 436 | 	} | 
 | 437 |  | 
 | 438 | 	if (errmsg) { | 
 | 439 | 		fprintf(fp, "\nError: %s\n", errmsg); | 
 | 440 | 		exit(EXIT_FAILURE); | 
 | 441 | 	} else | 
 | 442 | 		exit(EXIT_SUCCESS); | 
 | 443 | } |