Subject: usr.bin/file: use types in inttypes.h
To: None <christos@netbsd.org>
From: Jun-ichiro itojun Hagino <itojun@iijlab.net>
List: tech-userlevel
Date: 06/05/2002 11:41:38
	is this okay to commit this change?

itojun


Index: apprentice.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/file/apprentice.c,v
retrieving revision 1.29
diff -u -r1.29 apprentice.c
--- apprentice.c	2002/05/21 12:51:27	1.29
+++ apprentice.c	2002/06/05 02:41:03
@@ -85,19 +85,19 @@
 static int getvalue	__P((struct magic *, char **));
 static int hextoint	__P((int));
 static char *getstr	__P((char *, char *, int, int *));
-static int parse	__P((struct magic **, uint32 *, char *, int));
+static int parse	__P((struct magic **, uint32_t *, char *, int));
 static void eatsize	__P((char **));
 static int apprentice_1	__P((const char *, int));
-static int apprentice_file	__P((struct magic **, uint32 *,
+static int apprentice_file	__P((struct magic **, uint32_t *,
     const char *, int));
-static void byteswap	__P((struct magic *, uint32));
+static void byteswap	__P((struct magic *, uint32_t));
 static void bs1		__P((struct magic *));
-static uint16 swap2	__P((uint16));
-static uint32 swap4	__P((uint32));
+static uint16_t swap2	__P((uint16_t));
+static uint32_t swap4	__P((uint32_t));
 static char *mkdbname	__P((const char *));
-static int apprentice_map	__P((struct magic **, uint32 *,
+static int apprentice_map	__P((struct magic **, uint32_t *,
     const char *, int));
-static int apprentice_compile	__P((struct magic **, uint32 *,
+static int apprentice_compile	__P((struct magic **, uint32_t *,
     const char *, int));
 
 static int maxmagic = 0;
@@ -143,7 +143,7 @@
 	int action;
 {
 	struct magic *magic = NULL;
-	uint32 nmagic = 0;
+	uint32_t nmagic = 0;
 	struct mlist *ml;
 	int rv = -1;
 
@@ -233,7 +233,7 @@
 static int
 apprentice_file(magicp, nmagicp, fn, action)
 	struct magic **magicp;
-	uint32 *nmagicp;
+	uint32_t *nmagicp;
 	const char *fn;			/* name of magic file */
 	int action;
 {
@@ -287,10 +287,10 @@
 /*
  * extend the sign bit if the comparison is to be signed
  */
-uint32
+uint32_t
 signextend(m, v)
 	struct magic *m;
-	uint32 v;
+	uint32_t v;
 {
 	if (!(m->flag & UNSIGNED))
 		switch(m->type) {
@@ -316,7 +316,7 @@
 		case LONG:
 		case BELONG:
 		case LELONG:
-			v = (int32) v;
+			v = (int32_t) v;
 			break;
 		case STRING:
 		case PSTRING:
@@ -337,7 +337,7 @@
 static int
 parse(magicp, nmagicp, l, action)
 	struct magic **magicp;
-	uint32 *nmagicp;
+	uint32_t *nmagicp;
 	char *l;
 	int action;
 {
@@ -942,14 +942,14 @@
 static int
 apprentice_map(magicp, nmagicp, fn, action)
 	struct magic **magicp;
-	uint32 *nmagicp;
+	uint32_t *nmagicp;
 	const char *fn;
 	int action;
 {
 	int fd;
 	struct stat st;
-	uint32 *ptr;
-	uint32 version;
+	uint32_t *ptr;
+	uint32_t version;
 	int needsbyteswap;
 	char *dbname = mkdbname(fn);
 
@@ -986,7 +986,7 @@
 #endif
 	(void)close(fd);
 	fd = -1;
-	ptr = (uint32 *) *magicp;
+	ptr = (uint32_t *) *magicp;
 	if (*ptr != MAGICNO) {
 		if (swap4(*ptr) != MAGICNO) {
 			(void)fprintf(stderr, "%s: Bad magic in `%s'\n",
@@ -1034,13 +1034,13 @@
 static int
 apprentice_compile(magicp, nmagicp, fn, action)
 	struct magic **magicp;
-	uint32 *nmagicp;
+	uint32_t *nmagicp;
 	const char *fn;
 	int action;
 {
 	int fd;
 	char *dbname = mkdbname(fn);
-	static const uint32 ar[] = {
+	static const uint32_t ar[] = {
 	    MAGICNO, VERSIONNO
 	};
 
@@ -1106,9 +1106,9 @@
 static void
 byteswap(magic, nmagic)
 	struct magic *magic;
-	uint32 nmagic;
+	uint32_t nmagic;
 {
-	uint32 i;
+	uint32_t i;
 	for (i = 0; i < nmagic; i++)
 		bs1(&magic[i]);
 }
@@ -1116,13 +1116,13 @@
 /*
  * swap a short
  */
-static uint16
+static uint16_t
 swap2(sv) 
-	uint16 sv;
+	uint16_t sv;
 {
-	uint16 rv;
-	uint8 *s = (uint8 *) &sv; 
-	uint8 *d = (uint8 *) &rv; 
+	uint16_t rv;
+	uint8_t *s = (uint8_t *) &sv; 
+	uint8_t *d = (uint8_t *) &rv; 
 	d[0] = s[1];
 	d[1] = s[0];
 	return rv;
@@ -1131,13 +1131,13 @@
 /*
  * swap an int
  */
-static uint32
+static uint32_t
 swap4(sv) 
-	uint32 sv;
+	uint32_t sv;
 {
-	uint32 rv;
-	uint8 *s = (uint8 *) &sv; 
-	uint8 *d = (uint8 *) &rv; 
+	uint32_t rv;
+	uint8_t *s = (uint8_t *) &sv; 
+	uint8_t *d = (uint8_t *) &rv; 
 	d[0] = s[3];
 	d[1] = s[2];
 	d[2] = s[1];
Index: file.h
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/file/file.h,v
retrieving revision 1.25
diff -u -r1.25 file.h
--- file.h	2002/05/18 07:00:45	1.25
+++ file.h	2002/06/05 02:41:03
@@ -37,13 +37,6 @@
 #include <config.h>
 #endif
 
-typedef int int32;
-typedef unsigned int uint32;
-typedef short int16;
-typedef unsigned short uint16;
-typedef char int8;
-typedef unsigned char uint8;
-
 #ifndef HOWMANY
 # define HOWMANY 16384		/* how much of the file to look at */
 #endif
@@ -58,16 +51,16 @@
 #define COMPILE	2
 
 struct magic {
-	uint16 cont_level;/* level of ">" */
-	uint8 nospflag;	/* supress space character */
-	uint8 flag;
+	uint16_t cont_level;/* level of ">" */
+	uint8_t nospflag;	/* supress space character */
+	uint8_t flag;
 #define INDIR	1		/* if '>(...)' appears,  */
 #define	UNSIGNED 2		/* comparison is unsigned */
 #define OFFADD	4		/* if '>&' appears,  */
-	uint8 reln;		/* relation (0=eq, '>'=gt, etc) */
-	uint8 vallen;		/* length of string value, if any */
-	uint8 type;		/* int, short, long or string. */
-	uint8 in_type;		/* type of indirrection */
+	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
+	uint8_t vallen;		/* length of string value, if any */
+	uint8_t type;		/* int, short, long or string. */
+	uint8_t in_type;	/* type of indirrection */
 #define 			BYTE	1
 #define				SHORT	2
 #define				LONG	4
@@ -84,8 +77,8 @@
 #define				BELDATE	15
 #define				LELDATE	16
 #define				REGEX	17
-	uint8 in_op;		/* operator for indirection */
-	uint8 mask_op;		/* operator for mask */
+	uint8_t in_op;		/* operator for indirection */
+	uint8_t mask_op;	/* operator for mask */
 #define				OPAND	1
 #define				OPOR	2
 #define				OPXOR	3
@@ -95,18 +88,18 @@
 #define				OPDIVIDE	7
 #define				OPMODULO	8
 #define				OPINVERSE	0x80
-	int32 offset;		/* offset to magic number */
-	int32 in_offset;	/* offset from indirection */
+	int32_t offset;		/* offset to magic number */
+	int32_t in_offset;	/* offset from indirection */
 	union VALUETYPE {
-		unsigned char b;
-		unsigned short h;
-		uint32 l;
+		uint8_t b;
+		uint16_t h;
+		uint32_t l;
 		char s[MAXstring];
 		char *buf;
-		unsigned char hs[2];	/* 2 bytes of a fixed-endian "short" */
-		unsigned char hl[4];	/* 4 bytes of a fixed-endian "long" */
+		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
+		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
 	} value;		/* either number or string */
-	uint32 mask;	/* mask before comparison with value */
+	uint32_t mask;	/* mask before comparison with value */
 	char desc[MAXDESC];	/* description */
 };
 
@@ -122,7 +115,7 @@
 /* list of magic entries */
 struct mlist {
 	struct magic *magic;		/* array of magic entries */
-	uint32 nmagic;			/* number of entries in array */
+	uint32_t nmagic;		/* number of entries in array */
 	struct mlist *next, *prev;
 };
 
@@ -155,7 +148,7 @@
 extern int   tryit		__P((const char *, unsigned char *, int, int));
 extern int   zmagic		__P((const char *, unsigned char *, int));
 extern void  ckfprintf		__P((FILE *, const char *, ...));
-extern uint32 signextend	__P((struct magic *, unsigned int32));
+extern uint32_t signextend	__P((struct magic *, unsigned int32));
 extern void tryelf		__P((int, unsigned char *, int));
 extern int pipe2file		__P((int, void *, size_t));
 
Index: readelf.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/file/readelf.c,v
retrieving revision 1.13
diff -u -r1.13 readelf.c
--- readelf.c	2002/05/18 07:00:46	1.13
+++ readelf.c	2002/06/05 02:41:11
@@ -567,8 +567,8 @@
 	int nbytes;
 {
 	union {
-		int32 l;
-		char c[sizeof (int32)];
+		int32_t l;
+		char c[sizeof (int32_t)];
 	} u;
 	int class;
 	int swap;
@@ -601,7 +601,7 @@
 
 		u.l = 1;
 		(void) memcpy(&elfhdr, buf, sizeof elfhdr);
-		swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
+		swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
 
 		if (getu16(swap, elfhdr.e_type) == ET_CORE) 
 #ifdef ELFCORE
@@ -638,7 +638,7 @@
 
 		u.l = 1;
 		(void) memcpy(&elfhdr, buf, sizeof elfhdr);
-		swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
+		swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
 
 		if (getu16(swap, elfhdr.e_type) == ET_CORE) 
 #ifdef ELFCORE
Index: softmagic.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/file/softmagic.c,v
retrieving revision 1.26
diff -u -r1.26 softmagic.c
--- softmagic.c	2002/05/18 07:00:47	1.26
+++ softmagic.c	2002/06/05 02:41:15
@@ -34,6 +34,7 @@
 #include <time.h>
 #include <sys/types.h>
 #include <regex.h>
+#include <inttypes.h>
 
 #include "file.h"
 
@@ -46,12 +47,12 @@
 #endif
 #endif	/* lint */
 
-static int match	__P((struct magic *, uint32, unsigned char *, int));
+static int match	__P((struct magic *, uint32_t, unsigned char *, int));
 static int mget		__P((union VALUETYPE *,
 			     unsigned char *, struct magic *, int));
 static int mcheck	__P((union VALUETYPE *, struct magic *));
-static int32 mprint	__P((union VALUETYPE *, struct magic *));
-static void mdebug	__P((int32, char *, int));
+static int32_t mprint	__P((union VALUETYPE *, struct magic *));
+static void mdebug	__P((int32_t, char *, int));
 static int mconvert	__P((union VALUETYPE *, struct magic *));
 
 extern int kflag;
@@ -106,7 +107,7 @@
 static int
 match(magic, nmagic, s, nbytes)
 	struct magic *magic;
-	uint32 nmagic;
+	uint32_t nmagic;
 	unsigned char	*s;
 	int nbytes;
 {
@@ -114,14 +115,14 @@
 	int cont_level = 0;
 	int need_separator = 0;
 	union VALUETYPE p;
-	static int32 *tmpoff = NULL;
+	static int32_t *tmpoff = NULL;
 	static size_t tmplen = 0;
-	int32 oldoff = 0;
+	int32_t oldoff = 0;
 	int returnval = 0; /* if a match is found it is set to 1*/
 	int firstline = 1; /* a flag to print X\n  X\n- X */
 
 	if (tmpoff == NULL)
-		if ((tmpoff = (int32 *) malloc(tmplen = 20)) == NULL)
+		if ((tmpoff = (int32_t *) malloc(tmplen = 20)) == NULL)
 			error("out of memory\n");
 
 	for (magindex = 0; magindex < nmagic; magindex++) {
@@ -152,7 +153,7 @@
 			need_separator = 1;
 		/* and any continuations that match */
 		if (++cont_level >= tmplen)
-			if ((tmpoff = (int32 *) realloc(tmpoff,
+			if ((tmpoff = (int32_t *) realloc(tmpoff,
 						       tmplen += 20)) == NULL)
 				error("out of memory\n");
 		while (magic[magindex+1].cont_level != 0 && 
@@ -199,7 +200,7 @@
 					 */
 					if (++cont_level >= tmplen)
 						if ((tmpoff = 
-						    (int32 *) realloc(tmpoff,
+						    (int32_t *) realloc(tmpoff,
 						    tmplen += 20)) == NULL)
 							error("out of memory\n");
 				}
@@ -217,13 +218,13 @@
 	return returnval;  /* This is hit if -k is set or there is no match */
 }
 
-static int32
+static int32_t
 mprint(p, m)
 	union VALUETYPE *p;
 	struct magic *m;
 {
-	uint32 v;
-	int32 t=0 ;
+	uint32_t v;
+	int32_t t=0 ;
 
 
   	switch (m->type) {
@@ -245,8 +246,8 @@
   	case BELONG:
   	case LELONG:
 		v = signextend(m, p->l);
-		(void) printf(m->desc, (uint32) v);
-		t = m->offset + sizeof(int32);
+		(void) printf(m->desc, (uint32_t) v);
+		t = m->offset + sizeof(int32_t);
   		break;
 
   	case STRING:
@@ -457,7 +458,7 @@
 	case BELONG:
 	case BEDATE:
 	case BELDATE:
-		p->l = (int32)
+		p->l = (int32_t)
 		    ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
 		if (m->mask)
 			switch (m->mask_op&0x7F) {
@@ -524,7 +525,7 @@
 	case LELONG:
 	case LEDATE:
 	case LELDATE:
-		p->l = (int32)
+		p->l = (int32_t)
 		    ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
 		if (m->mask)
 			switch (m->mask_op&0x7F) {
@@ -567,7 +568,7 @@
 
 static void
 mdebug(offset, str, len)
-	int32 offset;
+	int32_t offset;
 	char *str;
 	int len;
 {
@@ -584,7 +585,7 @@
 	struct magic *m;
 	int nbytes;
 {
-	int32 offset = m->offset;
+	int32_t offset = m->offset;
 
 	if (m->type == REGEX) {
 	      /*
@@ -604,7 +605,7 @@
 		 * the usefulness of padding with zeroes eludes me, it
 		 * might even cause problems
 		 */
-		int32 have = nbytes - offset;
+		int32_t have = nbytes - offset;
 		memset(p, 0, sizeof(union VALUETYPE));
 		if (have > 0)
 			memcpy(p, s + offset, have);
@@ -777,56 +778,56 @@
 			if (m->in_offset)
 				switch (m->in_op&0x7F) {
 				case OPAND:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) &
 						 m->in_offset;
 					break;
 				case OPOR:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) |
 						 m->in_offset;
 					break;
 				case OPXOR:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) ^
 						 m->in_offset;
 					break;
 				case OPADD:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) +
 						 m->in_offset;
 					break;
 				case OPMINUS:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) -
 						 m->in_offset;
 					break;
 				case OPMULTIPLY:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) *
 						 m->in_offset;
 					break;
 				case OPDIVIDE:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) /
 						 m->in_offset;
 					break;
 				case OPMODULO:
-					offset = (int32)((p->hl[0]<<24)|
+					offset = (int32_t)((p->hl[0]<<24)|
 							 (p->hl[1]<<16)|
 							 (p->hl[2]<<8)|
 							 (p->hl[3])) %
@@ -840,56 +841,56 @@
 			if (m->in_offset)
 				switch (m->in_op&0x7F) {
 				case OPAND:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) &
 						 m->in_offset;
 					break;
 				case OPOR:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) |
 						 m->in_offset;
 					break;
 				case OPXOR:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) ^
 						 m->in_offset;
 					break;
 				case OPADD:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) +
 						 m->in_offset;
 					break;
 				case OPMINUS:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) -
 						 m->in_offset;
 					break;
 				case OPMULTIPLY:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) *
 						 m->in_offset;
 					break;
 				case OPDIVIDE:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) /
 						 m->in_offset;
 					break;
 				case OPMODULO:
-					offset = (int32)((p->hl[3]<<24)|
+					offset = (int32_t)((p->hl[3]<<24)|
 							 (p->hl[2]<<16)|
 							 (p->hl[1]<<8)|
 							 (p->hl[0])) %
@@ -959,8 +960,8 @@
 	union VALUETYPE* p;
 	struct magic *m;
 {
-	uint32 l = m->value.l;
-	uint32 v;
+	uint32_t l = m->value.l;
+	uint32_t v;
 	int matched;
 
 	if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
@@ -1091,7 +1092,7 @@
 					       v, l, matched);
 		}
 		else {
-			matched = (int32) v > (int32) l;
+			matched = (int32_t) v > (int32_t) l;
 			if (debug)
 				(void) fprintf(stderr, "%d > %d = %d\n",
 					       v, l, matched);
@@ -1106,7 +1107,7 @@
 					       v, l, matched);
 		}
 		else {
-			matched = (int32) v < (int32) l;
+			matched = (int32_t) v < (int32_t) l;
 			if (debug)
 				(void) fprintf(stderr, "%d < %d = %d\n",
 					       v, l, matched);