http://lists.gnu.org/archive/html/bug-cssc/2014-09/msg00003.html
http://git.savannah.gnu.org/cgit/cssc.git/commit/?id=9737f41b5d2a49e61061e9f896b5a9ae19e3a3ac
diff --git a/auxfiles/Makefile.am b/auxfiles/Makefile.am
index b08555d..51751a6 100644
--- a/auxfiles/Makefile.am
+++ b/auxfiles/Makefile.am
@@ -1,4 +1,4 @@
-EXTRA_DIST = check-testfiles.sh
+EXTRA_DIST = check-testfiles.sh firstinclude.sed
 
 dist-hook: CSSC.spec
 	cp CSSC.spec $(distdir)/CSSC.spec
diff --git a/auxfiles/firstinclude.sed b/auxfiles/firstinclude.sed
new file mode 100644
index 0000000..df1714a
--- a/dev/null
+++ b/auxfiles/firstinclude.sed
@@ -0,0 +1,19 @@
+/^ *# *include ["<].*[">]/ {
+s/^ *# *include ["<]\(.*\)[">]/\1/ 
+t YESREALLY
+i\
+Internal error - regexes are inconsistent
+q
+: YESREALLY
+#s/^\(config\|cssc\).h$//
+s/^config\.h$//
+t OK
+=
+i\
+config.h should be #included first
+: OK
+# q with an exit code is a GNU extension, don't use it.
+q
+}
+
+   
\ No newline at end of file
diff --git a/src/Makefile.am b/src/Makefile.am
index 7a9bd3a..7eb9de3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -112,6 +112,20 @@ sccs_LDADD = libcssc.a $(LIBOBJS) -lgnulib
 sccsdiff: sccsdiff.sh
 	sed -e 's:__csscutildir__:$(csscutildir):g' < sccsdiff.sh > $@ && chmod a+x $@ || { rm -f $@; false; }
 
+check-include-order:
+	rv=0; \
+	for f in $(srcdir)/*.cc; \
+	do \
+		msg="`sed -n -f $(top_srcdir)/auxfiles/firstinclude.sed < $$f`"; \
+		if test -n "$${msg}"; \
+		then \
+			set -- $${msg} ; line=$${1} ; shift; errmsg="$${*}"; \
+			echo "$${f}:$${line}:$${errmsg}" >&2; \
+			rv=1; \
+		fi \
+	done; \
+	exit $${rv}
+
 testsplit$(EXEEXT): fnsplit.cc mystring.$(OBJEXT) quit.$(OBJEXT)
 	$(CXXCOMPILE) -DTEST_FNSPLIT -o testsplit$(EXEEXT) \
 		$(srcdir)/fnsplit.cc mystring.$(OBJEXT) quit.$(OBJEXT)
diff --git a/src/admin.cc b/src/admin.cc
index 88e527e..e46ec1c 100644
--- a/src/admin.cc
+++ b/src/admin.cc
@@ -23,6 +23,8 @@
  * Administer and create SCCS files.
  *
  */
+#include <config.h>
+
 #include <errno.h>
 
 #include "cssc.h"
diff --git a/src/bodyio.cc b/src/bodyio.cc
index 3d3794d..05a98f6 100644
--- a/src/bodyio.cc
+++ b/src/bodyio.cc
@@ -23,6 +23,12 @@
  *
  */
 
+#include <config.h>
+
+#include <cstdio>
+#include <cstring>
+
+#include "cssc.h"
 #include "bodyio.h"
 #include "quit.h"
 #include "cssc-assert.h"
@@ -32,9 +38,6 @@
 #include "ioerr.h"
 #include "file.h"
 
-#include <cstdio>
-#include <cstring>
-
 /* Check if we have exceeded the maximum line length.
  */
 static bool check_line_len(const char *iname,
diff --git a/src/cap.cc b/src/cap.cc
index 226c463..d6d0810 100644
--- a/src/cap.cc
+++ b/src/cap.cc
@@ -23,6 +23,8 @@
  * Utilities for limiting output to the constraints of the file format.
  */
 
+#include <config.h>
+
 #include "cssc.h"
 
 
diff --git a/src/cdc.cc b/src/cdc.cc
index c258f8c..df8f9c4 100644
--- a/src/cdc.cc
+++ b/src/cdc.cc
@@ -23,6 +23,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "my-getopt.h"
 #include "fileiter.h"
diff --git a/src/delta-iterator.cc b/src/delta-iterator.cc
index c6daf0b..808864c 100644
--- a/src/delta-iterator.cc
+++ b/src/delta-iterator.cc
@@ -25,6 +25,9 @@
  *
  */
 
+#include <config.h>
+
+#include "cssc.h"
 #include "delta-iterator.h"
 #include "delta-table.h"
 #include "cssc-assert.h"
diff --git a/src/delta-table.cc b/src/delta-table.cc
index 196f7fd..b7c758a 100644
--- a/src/delta-table.cc
+++ b/src/delta-table.cc
@@ -22,6 +22,9 @@
  *
  */
 
+#include <config.h>
+
+#include "cssc.h"
 #include "delta-table.h"
 #include "delta-iterator.h"
 
diff --git a/src/delta.cc b/src/delta.cc
index 48eb367..8dfe3bd 100644
--- a/src/delta.cc
+++ b/src/delta.cc
@@ -24,6 +24,10 @@
  *
  */
 
+
+#include <config.h>
+
+#include "cssc.h"
 #include "delta.h"
 #include "cleanup.h"
 #include "my-getopt.h"
diff --git a/src/dtbl-prepend.cc b/src/dtbl-prepend.cc
index c85e6be..f6ecfc7 100644
--- a/src/dtbl-prepend.cc
+++ b/src/dtbl-prepend.cc
@@ -24,6 +24,9 @@
  *
  */
 
+#include <config.h>
+
+#include "cssc.h"
 #include "delta-table.h"
 
 
diff --git a/src/encoding.cc b/src/encoding.cc
index f62782c..b37724e 100644
--- a/src/encoding.cc
+++ b/src/encoding.cc
@@ -25,6 +25,9 @@
  * Compile on its own, defining TEST_CODE, to compile a test program.
  */
 
+#include <config.h>
+
+#include "cssc.h"
 #include "bodyio.h"
 #include "cssc-assert.h"
 
diff --git a/src/environment.cc b/src/environment.cc
index 425766a..6f0c5d3 100644
--- a/src/environment.cc
+++ b/src/environment.cc
@@ -22,15 +22,15 @@
  * sections "Environment" and "Interoperability" in the CSSC manual.
  *
  */
+#include "config.h"
+
+#include <cerrno>
+#include <cstdio>
 #include <cstdlib>
 #include <cstring>
-#include <cstdio>
-#include <cerrno>
-
-#include "cssc.h"
-
 #include <limits.h>
 
+#include "cssc.h"
 
 bool binary_file_creation_allowed (void)
 {
diff --git a/src/fatalsig.cc b/src/fatalsig.cc
index 04835b1..b0333f1 100644
--- a/src/fatalsig.cc
+++ b/src/fatalsig.cc
@@ -22,14 +22,15 @@
  * Handle fatal signals...
  *
  */
+#include "config.h"
+
 #include <cstdlib>
+#include <signal.h>             /* TODO: consider using sigaction(). */
 
+#include "cssc-assert.h"
 #include "cssc.h"
-#include "version.h"
 #include "quit.h"
-
-#include <signal.h>             /* TODO: consider using sigaction(). */
-#include "cssc-assert.h"
+#include "version.h"
 
 
 /* The expansion of RETSIGTYPE is automatically decided by the configure
diff --git a/src/fdclosed.cc b/src/fdclosed.cc
index f653d01..85687d5 100644
--- a/src/fdclosed.cc
+++ b/src/fdclosed.cc
@@ -31,13 +31,14 @@
  * the file pointer on our file too).
  */
 
-#include "cssc.h"
-#include "sysdep.h"
-#include "defaults.h"		// CONFIG_NULL_FILENAME
+#include <config.h>
 
 #include <unistd.h>
 #include <stdio.h>		/* perror() */
 
+#include "cssc.h"
+#include "sysdep.h"
+#include "defaults.h"		// CONFIG_NULL_FILENAME
 
 class SafeFdCheck
 {
diff --git a/src/file.cc b/src/file.cc
index c2c4512..8821de0 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -23,19 +23,22 @@
  * System dependent routines for accessing files.
  *
  */
+#include "config.h"
+
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
 
-#include "cssc.h"
+#include "cssc.h"		/* for CONFIG_CAN_HARD_LINK_AN_OPEN_FILE */
 #include "cssc-assert.h"
 #include "sysdep.h"
 #include "file.h"
 #include "quit.h"
 #include "ioerr.h"
 #include "defaults.h"
-
-#include <unistd.h>
-#include <sys/stat.h>
-#include <stdio.h>
 #include "dirent-safer.h"
 
 /* Redirects stdout to a "null" file (eg. /dev/null). */
@@ -152,14 +155,14 @@
 
 
 
+static int unprivileged = 0;
+
 #ifdef CONFIG_UIDS
 
 /* A flag to indicate whether or not the programme is an privileged
    (effective UID != real UID) or unprivileged (effective UID == real
    UID). */
 
-static int unprivileged = 0;
-
 #ifdef SAVED_IDS_OK
 
 static uid_t old_euid;
diff --git a/src/file.h b/src/file.h
index 34292fc..293d2f4 100644
--- a/src/file.h
+++ b/src/file.h
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 
 #include "filelock.h"
+#include "mystring.h"
 
 enum create_mode {
 	CREATE_EXCLUSIVE     =  001,
diff --git a/src/filelock.h b/src/filelock.h
index bbd37df..3119a35 100644
--- a/src/filelock.h
+++ b/src/filelock.h
@@ -28,6 +28,7 @@
 #define CSSC__FILELOCK_H__
 
 #include "cleanup.h"
+#include "mystring.h"
 
 class file_lock : private cleanup {
 	int locked;
diff --git a/src/fnsplit.cc b/src/fnsplit.cc
index deed952..26da763 100644
--- a/src/fnsplit.cc
+++ b/src/fnsplit.cc
@@ -20,6 +20,8 @@
  * Functions for canonifying filenames.
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "mystring.h"
 #include "file.h"		// declaration of split_filename.
diff --git a/src/get.cc b/src/get.cc
index dffafba..de644f1 100644
--- a/src/get.cc
+++ b/src/get.cc
@@ -23,6 +23,8 @@
  * Extract a requested delta from a SCCS file.
  *
  */
+#include "config.h"
+
 #include <errno.h>
 
 #include "cssc.h"
diff --git a/src/l-split.cc b/src/l-split.cc
index 00712de..1aa2dc4 100644
--- a/src/l-split.cc
+++ b/src/l-split.cc
@@ -24,6 +24,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "mylist.h"
 #include "sccsfile.h"		// declares these functions.
diff --git a/src/linebuf.cc b/src/linebuf.cc
index 822d97d..af6f04b 100644
--- a/src/linebuf.cc
+++ b/src/linebuf.cc
@@ -23,6 +23,8 @@
  * Members of the class cssc_linebuf.
  *
  */
+#include "config.h"
+
 #include <cstdio>
 #include <cstring>
 #include <climits>
diff --git a/src/pf-add.cc b/src/pf-add.cc
index fa441b3..293ff03 100644
--- a/src/pf-add.cc
+++ b/src/pf-add.cc
@@ -23,6 +23,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "pfile.h"
 #include "except.h"
diff --git a/src/pf-del.cc b/src/pf-del.cc
index 8d13da8..9e9c078 100644
--- a/src/pf-del.cc
+++ b/src/pf-del.cc
@@ -24,6 +24,8 @@
  * a p-file.
  *
  */
+#include "config.h"
+
 #include <utility>
 
 #include "cssc.h"
diff --git a/src/pfile.cc b/src/pfile.cc
index 7bb5989..8c10d70 100644
--- a/src/pfile.cc
+++ b/src/pfile.cc
@@ -23,6 +23,8 @@
  * Common members of the class sccs_pfile.
  *
  */
+#include "config.h"
+
 #include <errno.h>
 
 #include "cssc.h"
diff --git a/src/prompt.cc b/src/prompt.cc
index 69eb744..92fcce6 100644
--- a/src/prompt.cc
+++ b/src/prompt.cc
@@ -24,6 +24,8 @@
  *
  */
 
+#include "config.h"
+
 #include <cstdio>
 #include <cstring>
 #include <cstdio>
diff --git a/src/prs.cc b/src/prs.cc
index b4c4bdb..5f1edbb 100644
--- a/src/prs.cc
+++ b/src/prs.cc
@@ -24,6 +24,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "fileiter.h"
 #include "sccsfile.h"
diff --git a/src/prt.cc b/src/prt.cc
index 9ed915d..3856734 100644
--- a/src/prt.cc
+++ b/src/prt.cc
@@ -26,6 +26,7 @@
  */
 
 
+#include <config.h>
 #include "cssc.h"
 #include "fileiter.h"
 #include "sccsfile.h"
diff --git a/src/quit.cc b/src/quit.cc
index ac64c1c..0f602a9 100644
--- a/src/quit.cc
+++ b/src/quit.cc
@@ -23,6 +23,8 @@
  * Functions for cleaning up and quitting.
  *
  */
+#include "config.h"
+
 #include <cstdio>
 #include <cstring>
 #include <cerrno>
diff --git a/src/rel_list.cc b/src/rel_list.cc
index a9f837b..d367668 100644
--- a/src/rel_list.cc
+++ b/src/rel_list.cc
@@ -20,6 +20,7 @@
  * placed in the Public Domain.
  *
  */
+#include "config.h"
 #include <cstdlib>
 
 #include "cssc.h"
diff --git a/src/rl-merge.cc b/src/rl-merge.cc
index ea78e51..e129327 100644
--- a/src/rl-merge.cc
+++ b/src/rl-merge.cc
@@ -22,6 +22,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "rel_list.h"
 
diff --git a/src/rmdel.cc b/src/rmdel.cc
index ecc4e07..c839446 100644
--- a/src/rmdel.cc
+++ b/src/rmdel.cc
@@ -25,6 +25,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "fileiter.h"
 #include "sccsfile.h"
diff --git a/src/run.cc b/src/run.cc
index d298846..0932687 100644
--- a/src/run.cc
+++ b/src/run.cc
@@ -24,6 +24,8 @@
  * Routines for running programmes.
  *
  */
+#include "config.h"
+
 #include <cstdio>
 #include <errno.h>
 
diff --git a/src/sact.cc b/src/sact.cc
index 9ba7cbf..90ebe08 100644
--- a/src/sact.cc
+++ b/src/sact.cc
@@ -25,6 +25,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "fileiter.h"
 #include "pfile.h"
diff --git a/src/sccs-delta.cc b/src/sccs-delta.cc
index c4d6222..098ee1d 100644
--- a/src/sccs-delta.cc
+++ b/src/sccs-delta.cc
@@ -25,6 +25,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sccsdate.cc b/src/sccsdate.cc
index 9b79b89..433a2fe 100644
--- a/src/sccsdate.cc
+++ b/src/sccsdate.cc
@@ -24,6 +24,7 @@
  * Members of the class sccs_date.
  *
  */
+#include <config.h>
 #include <cstring>
 
 #include "cssc.h"
diff --git a/src/sccsfile.cc b/src/sccsfile.cc
index d698509..6cb211d 100644
--- a/src/sccsfile.cc
+++ b/src/sccsfile.cc
@@ -25,6 +25,7 @@
  * the members in this file are used to read from the SCCS file.
  *
  */
+#include <config.h>
 #include <errno.h>
 
 #include "cssc.h"
diff --git a/src/sccsname.cc b/src/sccsname.cc
index ef61b41..81edc54 100644
--- a/src/sccsname.cc
+++ b/src/sccsname.cc
@@ -25,6 +25,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "sccsname.h"
 #include "cssc-assert.h"
diff --git a/src/seqstate.cc b/src/seqstate.cc
index 61ae27a..ccdafb0 100644
--- a/src/seqstate.cc
+++ b/src/seqstate.cc
@@ -21,6 +21,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "seqstate.h"
 
diff --git a/src/sf-add.cc b/src/sf-add.cc
index cbda552..113d484 100644
--- a/src/sf-add.cc
+++ b/src/sf-add.cc
@@ -26,6 +26,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sf-admin.cc b/src/sf-admin.cc
index de64a68..1bc94cd 100644
--- a/src/sf-admin.cc
+++ b/src/sf-admin.cc
@@ -26,6 +26,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "sccsfile.h"
 #include "sl-merge.h"
diff --git a/src/sf-cdc.cc b/src/sf-cdc.cc
index 88e2f15..8a3c804 100644
--- a/src/sf-cdc.cc
+++ b/src/sf-cdc.cc
@@ -26,6 +26,7 @@
  *
  */
 
+#include <config.h>
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sf-chkid.cc b/src/sf-chkid.cc
index bd649b1..e6e9f02 100644
--- a/src/sf-chkid.cc
+++ b/src/sf-chkid.cc
@@ -23,13 +23,15 @@
  *
  */
 
+#include <config.h>
+
+#include <string.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "linebuf.h"
 #include "bodyio.h"
 
-#include <string.h>
-
 bool
 is_id_keyword_letter(char ch)
 {
diff --git a/src/sf-delta.cc b/src/sf-delta.cc
index 286b44f..f2317af 100644
--- a/src/sf-delta.cc
+++ b/src/sf-delta.cc
@@ -25,7 +25,10 @@
  * Members of sccs_file for adding a delta to the SCCS file.
  *
  */
+#include <config.h>
+
 #include <errno.h>
+#include <unistd.h>
 
 #include "cssc.h"
 #include "sccsfile.h"
@@ -43,9 +46,6 @@
 
 #undef JAY_DEBUG
 
-#include <unistd.h>
-
-
 
 class diff_state
 {
diff --git a/src/sf-get.cc b/src/sf-get.cc
index 2f0c104..6001ba5 100644
--- a/src/sf-get.cc
+++ b/src/sf-get.cc
@@ -26,6 +26,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "pfile.h"
diff --git a/src/sf-get2.cc b/src/sf-get2.cc
index 24831f9..7f970b4 100644
--- a/src/sf-get2.cc
+++ b/src/sf-get2.cc
@@ -25,6 +25,10 @@
  *
  */
 
+#include <config.h>
+
+#include <ctype.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "pfile.h"
@@ -33,8 +37,6 @@
 #include "delta-table.h"
 
 
-#include <ctype.h>
-
 bool sccs_file::sid_matches(const sid& requested,
 			    const sid& found,
 			    bool get_top_delta) const
diff --git a/src/sf-get3.cc b/src/sf-get3.cc
index e066675..9c7ef88 100644
--- a/src/sf-get3.cc
+++ b/src/sf-get3.cc
@@ -25,6 +25,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "seqstate.h"
diff --git a/src/sf-kw.cc b/src/sf-kw.cc
index 97e5942..33ad58f 100644
--- a/src/sf-kw.cc
+++ b/src/sf-kw.cc
@@ -21,6 +21,8 @@
  * sccs_file::no_id_keywords()
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "except.h"
diff --git a/src/sf-prs.cc b/src/sf-prs.cc
index 9b451b5..43745f3 100644
--- a/src/sf-prs.cc
+++ b/src/sf-prs.cc
@@ -27,6 +27,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "seqstate.h"
diff --git a/src/sf-prt.cc b/src/sf-prt.cc
index 41b7b92..1d6c4e7 100644
--- a/src/sf-prt.cc
+++ b/src/sf-prt.cc
@@ -24,6 +24,8 @@
  */
 
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "seqstate.h"
diff --git a/src/sf-rmdel.cc b/src/sf-rmdel.cc
index 9b1992f..a99b4d2 100644
--- a/src/sf-rmdel.cc
+++ b/src/sf-rmdel.cc
@@ -26,6 +26,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sf-val.cc b/src/sf-val.cc
index 2e34efd..5b3474b 100644
--- a/src/sf-val.cc
+++ b/src/sf-val.cc
@@ -22,7 +22,10 @@
  * Members of class sccs_file used by "val".
  *
  */
+#include <config.h>
+
 #include <vector>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sf-write.cc b/src/sf-write.cc
index 74c2d31..6433f26 100644
--- a/src/sf-write.cc
+++ b/src/sf-write.cc
@@ -26,6 +26,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
diff --git a/src/sid.cc b/src/sid.cc
index 400cda1..99a31ce 100644
--- a/src/sid.cc
+++ b/src/sid.cc
@@ -25,12 +25,14 @@
  *
  */
 
+#include <config.h>
+
+#include <ctype.h>
+
 #include "cssc.h"
 #include "sid.h"
 #include "ioerr.h"
 
-#include <ctype.h>
-
 mystring sid::as_string() const
 {
   char buf[32];
diff --git a/src/unget.cc b/src/unget.cc
index c6a21d5..38bb012 100644
--- a/src/unget.cc
+++ b/src/unget.cc
@@ -25,6 +25,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "fileiter.h"
 #include "pfile.h"
diff --git a/src/val.cc b/src/val.cc
index ccf6a2b..87ab911 100644
--- a/src/val.cc
+++ b/src/val.cc
@@ -21,6 +21,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "fileiter.h"
 #include "sccsfile.h"
diff --git a/src/writesubst.cc b/src/writesubst.cc
index c4cdde6..38b512a 100644
--- a/src/writesubst.cc
+++ b/src/writesubst.cc
@@ -22,6 +22,8 @@
  *
  */
 
+#include <config.h>
+
 #include "cssc.h"
 #include "sccsfile.h"
 #include "delta.h"
--
cgit v0.9.0.2
