atou

Convert ASCII hex numbers to UTF-8 character
git clone git://jacobedwards.org/atou
Log | Files | Refs | README | LICENSE

main.c (1470B)


      1 /* Copyright 2021 Jacob R. Edwards
      2  *
      3  * atou: Translate hexadecimal ASCII numbers to UTF-8
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17  */
     18 
     19 #include <errno.h>
     20 #include <limits.h>
     21 #include <locale.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include <unistd.h>
     26 
     27 void
     28 die(int error, char const *s)
     29 {
     30 	fprintf(stderr, "%s: %s: %s\n", getprogname(), s, strerror(error));
     31 	exit(EXIT_FAILURE);
     32 }
     33 
     34 int
     35 main(int argc, char *argv[])
     36 {
     37 	char *ep;
     38 	int i;
     39 	unsigned long n;
     40 
     41 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL)
     42 		die(errno, "setlocale");
     43 
     44 #ifdef __OpenBSD__
     45 	if (pledge("stdio", NULL) == -1)
     46 		die(errno, "pledge");
     47 #endif
     48 
     49 	for (i = 1; i < argc; ++i) {
     50 		n = strtoul(argv[i], &ep, 16);
     51 		if (*ep != '\0' || n > INT_MAX || errno == ERANGE)
     52 			die(EINVAL, "strtoul");
     53 		if (printf("%lc\n", (int)n) < 0)
     54 			die(errno, "printf");
     55 	}
     56 	return 0;
     57 }