Remove unneeded #include statements completely

Unistd.h was only used to get a definition of NULL in all files. Add our
own NULL #define and remove unistd.h from flash.h
stdio.h has no place in flash.h, it should be included only in files
which really need it.
Add #include statements in individual .c files where needed.

Replace a few printf with msg_* to eliminate the need for stdio.h.

Corresponding to flashrom svn r1021.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Uwe Hermann <uwe@hermann-uwe.de>
diff --git a/82802ab.c b/82802ab.c
index c9d1a75..3935a7e 100644
--- a/82802ab.c
+++ b/82802ab.c
@@ -26,8 +26,6 @@
  *  - Order number: 290658-004
  */
 
-#include <string.h>
-#include <stdlib.h>
 #include "flash.h"
 #include "chipdrivers.h"
 
diff --git a/board_enable.c b/board_enable.c
index b5eb63f..618eb0d 100644
--- a/board_enable.c
+++ b/board_enable.c
@@ -25,7 +25,6 @@
  */
 
 #include <string.h>
-#include <fcntl.h>
 #include "flash.h"
 
 #if defined(__i386__) || defined(__x86_64__)
@@ -107,8 +106,8 @@
 	OUTB(0x55, port);	/* enter conf mode */
 	id = sio_read(port, 0x20);
 	if (id != 0x44) {
-		fprintf(stderr, "\nERROR: %s: FDC37B787: Wrong ID 0x%02X.\n",
-		        name, id);
+		msg_perr("\nERROR: %s: FDC37B787: Wrong ID 0x%02X.\n",
+			 name, id);
 		OUTB(0xAA, port); /* leave conf mode */
 		return -1;
 	}
@@ -118,8 +117,8 @@
 	val = sio_read(port, 0xC8);	/* GP50 */
 	if ((val & 0x1B) != 0x10)	/* output, no invert, GPIO */
 	{
-		fprintf(stderr, "\nERROR: %s: GPIO50 mode 0x%02X unexpected.\n",
-		        name, val);
+		msg_perr("\nERROR: %s: GPIO50 mode 0x%02X unexpected.\n",
+			 name, val);
 		OUTB(0xAA, port);
 		return -1;
 	}
diff --git a/cbtable.c b/cbtable.c
index 3e97ed7..6a6f7a9 100644
--- a/cbtable.c
+++ b/cbtable.c
@@ -22,6 +22,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <unistd.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <string.h>
diff --git a/chipset_enable.c b/chipset_enable.c
index 04b1a1c..9356b1e 100644
--- a/chipset_enable.c
+++ b/chipset_enable.c
@@ -27,6 +27,7 @@
 
 #define _LARGEFILE64_SOURCE
 
+#include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/cli_classic.c b/cli_classic.c
index baca900..f541084 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <stdio.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/dediprog.c b/dediprog.c
index 2d0d060..206d860 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -18,9 +18,6 @@
  */
 
 #include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <sys/types.h>
 #include <usb.h>
 #include "flash.h"
 #include "chipdrivers.h"
diff --git a/drkaiser.c b/drkaiser.c
index 572fee3..f1078cb 100644
--- a/drkaiser.c
+++ b/drkaiser.c
@@ -19,8 +19,6 @@
  */
 
 #include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
 #include "flash.h"
 
 #define PCI_VENDOR_ID_DRKAISER		0x1803
diff --git a/flash.h b/flash.h
index ec7dd8e..8e8939b 100644
--- a/flash.h
+++ b/flash.h
@@ -24,9 +24,7 @@
 #ifndef __FLASH_H__
 #define __FLASH_H__ 1
 
-#include <unistd.h>
 #include <stdint.h>
-#include <stdio.h>
 #include "hwaccess.h"
 #ifdef _WIN32
 #include <windows.h>
@@ -34,6 +32,10 @@
 #undef max
 #endif
 
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
 typedef unsigned long chipaddr;
 
 enum programmer {
@@ -556,7 +558,6 @@
 extern int verbose;
 extern const char *flashrom_version;
 extern char *chip_to_probe;
-#define printf_debug(x...) { if (verbose) printf(x); }
 void map_flash_registers(struct flashchip *flash);
 int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len);
 int erase_flash(struct flashchip *flash);
diff --git a/flashrom.c b/flashrom.c
index 3cf55e6..071c52e 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <stdio.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/hwaccess.c b/hwaccess.c
index 44d7871..f754166 100644
--- a/hwaccess.c
+++ b/hwaccess.c
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <errno.h>
diff --git a/layout.c b/layout.c
index df77c2f..363e4f3 100644
--- a/layout.c
+++ b/layout.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -73,7 +74,7 @@
 	mb_vendor_offset = *(walk - 2);
 	if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
 	    mb_part_offset > size || mb_vendor_offset > size) {
-		printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
+		msg_pinfo("Flash image seems to be a legacy BIOS. Disabling checks.\n");
 		return 0;
 	}
 
@@ -81,25 +82,25 @@
 	mb_vendor = (char *)(bios + size - mb_vendor_offset);
 	if (!isprint((unsigned char)*mb_part) ||
 	    !isprint((unsigned char)*mb_vendor)) {
-		printf("Flash image seems to have garbage in the ID location."
+		msg_pinfo("Flash image seems to have garbage in the ID location."
 		       " Disabling checks.\n");
 		return 0;
 	}
 
-	printf_debug("coreboot last image size "
+	msg_pdbg("coreboot last image size "
 		     "(not ROM size) is %d bytes.\n", *walk);
 
 	mainboard_part = strdup(mb_part);
 	mainboard_vendor = strdup(mb_vendor);
-	printf_debug("Manufacturer: %s\n", mainboard_vendor);
-	printf_debug("Mainboard ID: %s\n", mainboard_part);
+	msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
+	msg_pdbg("Mainboard ID: %s\n", mainboard_part);
 
 	/*
 	 * If lb_vendor is not set, the coreboot table was
 	 * not found. Nor was -m VENDOR:PART specified.
 	 */
 	if (!lb_vendor || !lb_part) {
-		printf("Note: If the following flash access fails, "
+		msg_pinfo("Note: If the following flash access fails, "
 		       "try -m <vendor>:<mainboard>.\n");
 		return 0;
 	}
@@ -109,13 +110,13 @@
 	 */
 	if (!strcasecmp(mainboard_vendor, lb_vendor) &&
 	    !strcasecmp(mainboard_part, lb_part)) {
-		printf_debug("This firmware image matches this mainboard.\n");
+		msg_pdbg("This firmware image matches this mainboard.\n");
 	} else {
 		if (force_boardmismatch) {
-			printf("WARNING: This firmware image does not "
+			msg_pinfo("WARNING: This firmware image does not "
 			       "seem to fit to this machine - forcing it.\n");
 		} else {
-			printf("ERROR: Your firmware image (%s:%s) does not "
+			msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
 			       "appear to\n       be correct for the detected "
 			       "mainboard (%s:%s)\n\nOverride with -p internal:"
 			       "boardmismatch=force if you are absolutely sure "
@@ -141,7 +142,7 @@
 	romlayout = fopen(name, "r");
 
 	if (!romlayout) {
-		fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
+		msg_gerr("ERROR: Could not open ROM layout (%s).\n",
 			name);
 		return -1;
 	}
@@ -159,7 +160,7 @@
 		tstr1 = strtok(tempstr, ":");
 		tstr2 = strtok(NULL, ":");
 		if (!tstr1 || !tstr2) {
-			fprintf(stderr, "Error parsing layout file.\n");
+			msg_gerr("Error parsing layout file.\n");
 			fclose(romlayout);
 			return 1;
 		}
@@ -170,7 +171,7 @@
 	}
 
 	for (i = 0; i < romimages; i++) {
-		printf_debug("romlayout %08x - %08x named %s\n",
+		msg_gdbg("romlayout %08x - %08x named %s\n",
 			     rom_entries[i].start,
 			     rom_entries[i].end, rom_entries[i].name);
 	}
@@ -187,16 +188,16 @@
 	if (!romimages)
 		return -1;
 
-	printf("Looking for \"%s\"... ", name);
+	msg_ginfo("Looking for \"%s\"... ", name);
 
 	for (i = 0; i < romimages; i++) {
 		if (!strcmp(rom_entries[i].name, name)) {
 			rom_entries[i].included = 1;
-			printf("found.\n");
+			msg_ginfo("found.\n");
 			return i;
 		}
 	}
-	printf("not found.\n");	// Not found. Error.
+	msg_ginfo("not found.\n");	// Not found. Error.
 
 	return -1;
 }
diff --git a/nic3com.c b/nic3com.c
index 8064b57..29f83c3 100644
--- a/nic3com.c
+++ b/nic3com.c
@@ -21,8 +21,6 @@
 #if defined(__i386__) || defined(__x86_64__)
 
 #include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
 #include "flash.h"
 
 #define BIOS_ROM_ADDR		0x04
diff --git a/nicrealtek.c b/nicrealtek.c
index d145886..ad39eb2 100644
--- a/nicrealtek.c
+++ b/nicrealtek.c
@@ -21,8 +21,6 @@
 #if defined(__i386__) || defined(__x86_64__)
 
 #include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
 #include "flash.h"
 
 #define PCI_VENDOR_ID_REALTEK	0x10ec
diff --git a/physmap.c b/physmap.c
index 1f47ce0..7642938 100644
--- a/physmap.c
+++ b/physmap.c
@@ -20,6 +20,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <unistd.h>
+#include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -310,8 +312,8 @@
 int setup_cpu_msr(int cpu)
 {
 	char msrfilename[64];
-	memset(msrfilename, 0, 64);
-	sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
+	memset(msrfilename, 0, sizeof(msrfilename));
+	snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
 
 	if (fd_msr != -1) {
 		msg_pinfo("MSR was already initialized\n");
@@ -393,8 +395,8 @@
 int setup_cpu_msr(int cpu)
 {
 	char msrfilename[64];
-	memset(msrfilename, 0, 64);
-	sprintf(msrfilename, "/dev/cpu%d", cpu);
+	memset(msrfilename, 0, sizeof(msrfilename));
+	snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
 
 	if (fd_msr != -1) {
 		msg_pinfo("MSR was already initialized\n");
diff --git a/print.c b/print.c
index a8da65f..a2f4e5d 100644
--- a/print.c
+++ b/print.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include "flash.h"
diff --git a/programmer.c b/programmer.c
index ca641e1..0160867 100644
--- a/programmer.c
+++ b/programmer.c
@@ -18,7 +18,6 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <stdlib.h>
 #include "flash.h"
 
 /* No-op shutdown() for programmers which don't need special handling */
diff --git a/satasii.c b/satasii.c
index 90d38bd..5ac27c9 100644
--- a/satasii.c
+++ b/satasii.c
@@ -21,7 +21,6 @@
 /* Datasheets can be found on http://www.siliconimage.com. Great thanks! */
 
 #include <stdlib.h>
-#include <string.h>
 #include "flash.h"
 
 #define PCI_VENDOR_ID_SII	0x1095
diff --git a/sb600spi.c b/sb600spi.c
index 973d1ad..b44a585 100644
--- a/sb600spi.c
+++ b/sb600spi.c
@@ -23,7 +23,6 @@
 
 #if defined(__i386__) || defined(__x86_64__)
 
-#include <string.h>
 #include "flash.h"
 #include "chipdrivers.h"
 #include "spi.h"
diff --git a/sharplhf00l04.c b/sharplhf00l04.c
index 70b8135..a891e2c 100644
--- a/sharplhf00l04.c
+++ b/sharplhf00l04.c
@@ -18,7 +18,6 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <stdlib.h>
 #include "flash.h"
 #include "chipdrivers.h"
 
diff --git a/spi.c b/spi.c
index 0d501c0..67682b5 100644
--- a/spi.c
+++ b/spi.c
@@ -22,7 +22,6 @@
  * Contains the generic SPI framework
  */
 
-#include <string.h>
 #include "flash.h"
 #include "flashchips.h"
 #include "chipdrivers.h"
diff --git a/sst49lfxxxc.c b/sst49lfxxxc.c
index 1331cda..c15b56b 100644
--- a/sst49lfxxxc.c
+++ b/sst49lfxxxc.c
@@ -20,7 +20,6 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <stdlib.h>
 #include "flash.h"
 #include "chipdrivers.h"
 
diff --git a/sst_fwhub.c b/sst_fwhub.c
index 2b867c8..017ab56 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -22,8 +22,6 @@
 
 /* Adapted from the Intel FW hub stuff for 82802ax parts. */
 
-#include <stdlib.h>
-#include <string.h>
 #include "flash.h"
 #include "chipdrivers.h"
 
diff --git a/stm50flw0x0x.c b/stm50flw0x0x.c
index 3539395..81265c9 100644
--- a/stm50flw0x0x.c
+++ b/stm50flw0x0x.c
@@ -27,8 +27,6 @@
  * ST M50FLW080B (not yet tested)
  */
 
-#include <string.h>
-#include <stdlib.h>
 #include "flash.h"
 #include "flashchips.h"
 #include "chipdrivers.h"
diff --git a/udelay.c b/udelay.c
index bae6467..9c7a673 100644
--- a/udelay.c
+++ b/udelay.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <unistd.h>
 #include <sys/time.h>
 #include <stdlib.h>
 #include <limits.h>
@@ -63,7 +64,7 @@
 	unsigned long timeusec;
 	int i, tries = 0;
 
-	printf("Calibrating delay loop... ");
+	msg_pinfo("Calibrating delay loop... ");
 
 recalibrate:
 	count = 1000;
@@ -113,7 +114,7 @@
 	timeusec = measure_delay(10000);
 	msg_pdbg("10000 myus = %ld us, ", timeusec);
 
-	printf("OK.\n");
+	msg_pinfo("OK.\n");
 }
 
 void internal_delay(int usecs)
diff --git a/wbsio_spi.c b/wbsio_spi.c
index d733247..53a382e 100644
--- a/wbsio_spi.c
+++ b/wbsio_spi.c
@@ -20,7 +20,6 @@
 
 #if defined(__i386__) || defined(__x86_64__)
 
-#include <string.h>
 #include "flash.h"
 #include "chipdrivers.h"
 #include "spi.h"