-\r
-Main test.exe : test.c ;\r
-\r
+
+Main test.exe : test.c ;
+
-/*\r
- * A test which demonstrates the use of opendir and related functions\r
- * declared in dirent.h.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <errno.h>\r
-\r
-#include <dirent.h>\r
-\r
-int\r
-main (int argc, char* argv[])\r
-{\r
- int i;\r
- struct dirent* de;\r
- DIR* dir;\r
- long lPos;\r
-\r
- if (argc == 2)\r
- {\r
- printf ("Opening directory \"%s\"\n", argv[1]);\r
- dir = opendir(argv[1]);\r
- }\r
- else\r
- {\r
- printf ("Opening \".\"\n");\r
- dir = opendir(".");\r
- }\r
-\r
- if (!dir)\r
- {\r
- printf ("Directory open failed!\n");\r
- if (errno)\r
- {\r
- printf ("Error : %s\n", strerror(errno));\r
- }\r
- return 1;\r
- }\r
-\r
- i = 0;\r
- lPos = -1;\r
-\r
- while (de = readdir (dir))\r
- {\r
- i++;\r
- printf ("%d : \"%s\" (tell %ld)\n", i, de->d_name,\r
- telldir(dir));\r
-\r
- if (i == 3)\r
- {\r
- printf ("We will seek here later.\n");\r
- lPos = telldir (dir);\r
- }\r
- }\r
-\r
- printf ("Rewind directory.\n");\r
- rewinddir (dir);\r
-\r
- if (de = readdir (dir))\r
- {\r
- printf ("First entry : \"%s\"\n", de->d_name);\r
- }\r
- else\r
- {\r
- printf ("Empty directory.\n");\r
- }\r
-\r
- if (lPos != -1)\r
- {\r
- printf ("Seeking to fourth entry.\n");\r
- seekdir (dir, lPos);\r
-\r
- if (de = readdir (dir))\r
- {\r
- printf ("Fourth entry : \"%s\"\n", de->d_name);\r
- }\r
- else\r
- {\r
- printf ("No fourth entry.\n");\r
- }\r
- }\r
- else\r
- {\r
- printf ("Seek position is past end of directory.\n");\r
- }\r
-\r
- printf ("Closing directory.\n");\r
- closedir (dir);\r
-}\r
-\r
+/*
+ * A test which demonstrates the use of opendir and related functions
+ * declared in dirent.h.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <dirent.h>
+
+int
+main (int argc, char* argv[])
+{
+ int i;
+ struct dirent* de;
+ DIR* dir;
+ long lPos;
+
+ if (argc == 2)
+ {
+ printf ("Opening directory \"%s\"\n", argv[1]);
+ dir = opendir(argv[1]);
+ }
+ else
+ {
+ printf ("Opening \".\"\n");
+ dir = opendir(".");
+ }
+
+ if (!dir)
+ {
+ printf ("Directory open failed!\n");
+ if (errno)
+ {
+ printf ("Error : %s\n", strerror(errno));
+ }
+ return 1;
+ }
+
+ i = 0;
+ lPos = -1;
+
+ while (de = readdir (dir))
+ {
+ i++;
+ printf ("%d : \"%s\" (tell %ld)\n", i, de->d_name,
+ telldir(dir));
+
+ if (i == 3)
+ {
+ printf ("We will seek here later.\n");
+ lPos = telldir (dir);
+ }
+ }
+
+ printf ("Rewind directory.\n");
+ rewinddir (dir);
+
+ if (de = readdir (dir))
+ {
+ printf ("First entry : \"%s\"\n", de->d_name);
+ }
+ else
+ {
+ printf ("Empty directory.\n");
+ }
+
+ if (lPos != -1)
+ {
+ printf ("Seeking to fourth entry.\n");
+ seekdir (dir, lPos);
+
+ if (de = readdir (dir))
+ {
+ printf ("Fourth entry : \"%s\"\n", de->d_name);
+ }
+ else
+ {
+ printf ("No fourth entry.\n");
+ }
+ }
+ else
+ {
+ printf ("Seek position is past end of directory.\n");
+ }
+
+ printf ("Closing directory.\n");
+ closedir (dir);
+}
+
-/*\r
- * Source code of the functions inside our test DLL. Note that DllMain is\r
- * not required (it will be provided by the stub in libmingw32.a).\r
- */\r
-\r
-#if 0\r
-#include <windows.h>\r
-#endif\r
-\r
-int Add (int x, int y)\r
-{\r
- printf ("In add!\nx = %d\ny = %d\n", x, y);\r
- return (x + y);\r
-}\r
-\r
-\r
-double __attribute__((stdcall)) Sub (double x, double y) \r
-{\r
- printf ("In sub!\nx = %f\ny = %f\n", x, y);\r
- return (x - y);\r
-}\r
-\r
+/*
+ * Source code of the functions inside our test DLL. Note that DllMain is
+ * not required (it will be provided by the stub in libmingw32.a).
+ */
+
+#if 0
+#include <windows.h>
+#endif
+
+int Add (int x, int y)
+{
+ printf ("In add!\nx = %d\ny = %d\n", x, y);
+ return (x + y);
+}
+
+
+double __attribute__((stdcall)) Sub (double x, double y)
+{
+ printf ("In sub!\nx = %f\ny = %f\n", x, y);
+ return (x - y);
+}
+
-EXPORTS\r
-Add\r
-Sub@16\r
+EXPORTS
+Add
+Sub@16
-\r
-int Add (int x, int y);\r
-double __attribute__((stdcall)) Sub (double x, double y);\r
-\r
+
+int Add (int x, int y);
+double __attribute__((stdcall)) Sub (double x, double y);
+
-#include <stdio.h>\r
-\r
-#include "dll.h"\r
-\r
-int main()\r
-{\r
- int i, j, k;\r
- double dk;\r
-\r
- i = 10;\r
- j = 13;\r
-\r
- k = Add(i, j);\r
-\r
- printf ("%d + %d = %d\n", i, j, k);\r
- \r
- dk = Sub(i, j);\r
-\r
- printf ("%d - %d = %f\n", i, j, dk);\r
-\r
- return 0;\r
-}\r
-\r
+#include <stdio.h>
+
+#include "dll.h"
+
+int main()
+{
+ int i, j, k;
+ double dk;
+
+ i = 10;
+ j = 13;
+
+ k = Add(i, j);
+
+ printf ("%d + %d = %d\n", i, j, k);
+
+ dk = Sub(i, j);
+
+ printf ("%d - %d = %f\n", i, j, dk);
+
+ return 0;
+}
+
-In add!\r
-x = 10\r
-y = 13\r
-10 + 13 = 23\r
-In sub!\r
-x = 10\r
-y = 13\r
-10 - 13 = -3\r
+In add!
+x = 10
+y = 13
+10 + 13 = 23
+In sub!
+x = 10
+y = 13
+10 - 13 = -3
-\r
-#include <stdio.h>\r
-\r
-int\r
-ExportedFromExe ()\r
-{\r
- printf ("This output produced by ExportedFromExe.\n");\r
- return 0;\r
-}\r
-\r
-int main()\r
-{\r
- printf ("Hello, world\n");\r
-\r
- return 0;\r
-}\r
-\r
+
+#include <stdio.h>
+
+int
+ExportedFromExe ()
+{
+ printf ("This output produced by ExportedFromExe.\n");
+ return 0;
+}
+
+int main()
+{
+ printf ("Hello, world\n");
+
+ return 0;
+}
+
-EXPORTS\r
-ExportedFromExe\r
+EXPORTS
+ExportedFromExe
-/*\r
- * This version attempts to load dll.dll dynamically, get the address of the\r
- * Add function, and then call it.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <windows.h>\r
-\r
-int (*Add)(int x, int y);\r
-\r
-int main()\r
-{\r
- HINSTANCE hDll;\r
- int i, j, k;\r
-\r
- hDll = LoadLibrary ("dll.dll");\r
- if (!hDll)\r
- {\r
- printf ("Error %d loading dll.\n", GetLastError());\r
- exit (-1);\r
- }\r
-\r
- if (!(Add = GetProcAddress (hDll, "Add")))\r
- {\r
- printf ("Error %d getting Add function.\n", GetLastError());\r
- exit (-1);\r
- }\r
-\r
- i = 10;\r
- j = 13;\r
-\r
- k = Add(i, j);\r
-\r
- printf ("i %d, j %d, k %d\n", i, j, k);\r
-\r
- FreeLibrary (hDll);\r
- \r
- return 0;\r
-}\r
-\r
+/*
+ * This version attempts to load dll.dll dynamically, get the address of the
+ * Add function, and then call it.
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+int (*Add)(int x, int y);
+
+int main()
+{
+ HINSTANCE hDll;
+ int i, j, k;
+
+ hDll = LoadLibrary ("dll.dll");
+ if (!hDll)
+ {
+ printf ("Error %d loading dll.\n", GetLastError());
+ exit (-1);
+ }
+
+ if (!(Add = GetProcAddress (hDll, "Add")))
+ {
+ printf ("Error %d getting Add function.\n", GetLastError());
+ exit (-1);
+ }
+
+ i = 10;
+ j = 13;
+
+ k = Add(i, j);
+
+ printf ("i %d, j %d, k %d\n", i, j, k);
+
+ FreeLibrary (hDll);
+
+ return 0;
+}
+
-/*\r
- * This program attempts to load expexe.exe dynamically, get the address of the\r
- * ExportedFromExe function, and then call it.\r
- *\r
- * This example DOES NOT WORK! I don't know exactly what can be done, but\r
- * it simply seems that LoadLibrary refuses to load executables.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <windows.h>\r
-\r
-int (*ExportedFromExe)();\r
-\r
-int main()\r
-{\r
- HINSTANCE hDll;\r
- int i, j, k;\r
-\r
- hDll = LoadLibrary ("expexe.exe");\r
- if (!hDll)\r
- {\r
- printf ("Error %d loading exe.\n", GetLastError());\r
- exit (-1);\r
- }\r
-\r
- if (!(ExportedFromExe = GetProcAddress (hDll, "ExportedFromExe")))\r
- {\r
- printf ("Error %d getting ExportedFromExe function.\n",\r
- GetLastError());\r
- exit (-1);\r
- }\r
- else\r
- {\r
- ExportedFromExe ();\r
- }\r
-\r
- /* NOTE: Unlike a DLL the exe doesn't have an entry point which\r
- * initializes global objects and adds __do_global_dtors to\r
- * the atexit list. Thus it should be safe(?) to free the\r
- * library. Of course, this also makes it unsafe to use\r
- * executables at all in this manner.\r
- */\r
- FreeLibrary (hDll);\r
-\r
- return 0;\r
-}\r
-\r
+/*
+ * This program attempts to load expexe.exe dynamically, get the address of the
+ * ExportedFromExe function, and then call it.
+ *
+ * This example DOES NOT WORK! I don't know exactly what can be done, but
+ * it simply seems that LoadLibrary refuses to load executables.
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+int (*ExportedFromExe)();
+
+int main()
+{
+ HINSTANCE hDll;
+ int i, j, k;
+
+ hDll = LoadLibrary ("expexe.exe");
+ if (!hDll)
+ {
+ printf ("Error %d loading exe.\n", GetLastError());
+ exit (-1);
+ }
+
+ if (!(ExportedFromExe = GetProcAddress (hDll, "ExportedFromExe")))
+ {
+ printf ("Error %d getting ExportedFromExe function.\n",
+ GetLastError());
+ exit (-1);
+ }
+ else
+ {
+ ExportedFromExe ();
+ }
+
+ /* NOTE: Unlike a DLL the exe doesn't have an entry point which
+ * initializes global objects and adds __do_global_dtors to
+ * the atexit list. Thus it should be safe(?) to free the
+ * library. Of course, this also makes it unsafe to use
+ * executables at all in this manner.
+ */
+ FreeLibrary (hDll);
+
+ return 0;
+}
+
-This directory contains two examples of building DLLs. The exe.c and dll.c\r
-files are used to build a very simple example DLL with a function that\r
-adds two numbers together (and prints some text at the same time). The\r
-exe.c program links to the DLL and prints the results of the function\r
-call.\r
-\r
-The C++ example "silly" is more interesting because it involves a DLL which\r
-contains the code for a C++ class. The CSilly class has all of its code in\r
-the sillydll.cpp source file, which is used to build the silly.dll. The\r
-silly.cpp source code builds the main silly.exe executable which makes a\r
-dynamic instance of the object and calls its member functions.\r
-\r
-The C++ silly.def file was generated by doing a nm of sillydll.o after it\r
-was generated and then getting the symbol names from that. Removing the\r
-leading underscore produces the appropriate name to include in the EXPORTS\r
-section. Notice there are a few weird functions.\r
-\r
-Since there are now several different versions of the GNU compiler capable\r
-of doing this, and they each seem to have different requirements for exports\r
-for classes, it has gotten kind of messy. The silly.def file here is for\r
-use with the native Mingw32 build of the EGCS version of GCC. The silly.def.old\r
-file was the def file I used when I was using Jan-Jaap's Mingw32 native port\r
-of GCC. The Cygnus version is different again, if I recall correctly, but I\r
-don't have it hanging around anymore.\r
-\r
-The jamfile builds all the components from the raw sources.\r
-\r
-The expected output of exe.exe and silly.exe are in the files exe.exp\r
-and silly.exp.\r
-\r
-\r
-The source code in this directory is in the PUBLIC DOMAIN and can be\r
-used or abused as you see fit. There is NO WARRANTY for this code,\r
-including (but not limited to) implied warranties of MERCHANTABILITY\r
-or FITNESS FOR A PARTICULAR PURPOSE.\r
-\r
-\r
-Colin Peters <colin@bird.fu.is.saga-u.ac.jp>\r
-\r
+This directory contains two examples of building DLLs. The exe.c and dll.c
+files are used to build a very simple example DLL with a function that
+adds two numbers together (and prints some text at the same time). The
+exe.c program links to the DLL and prints the results of the function
+call.
+
+The C++ example "silly" is more interesting because it involves a DLL which
+contains the code for a C++ class. The CSilly class has all of its code in
+the sillydll.cpp source file, which is used to build the silly.dll. The
+silly.cpp source code builds the main silly.exe executable which makes a
+dynamic instance of the object and calls its member functions.
+
+The C++ silly.def file was generated by doing a nm of sillydll.o after it
+was generated and then getting the symbol names from that. Removing the
+leading underscore produces the appropriate name to include in the EXPORTS
+section. Notice there are a few weird functions.
+
+Since there are now several different versions of the GNU compiler capable
+of doing this, and they each seem to have different requirements for exports
+for classes, it has gotten kind of messy. The silly.def file here is for
+use with the native Mingw32 build of the EGCS version of GCC. The silly.def.old
+file was the def file I used when I was using Jan-Jaap's Mingw32 native port
+of GCC. The Cygnus version is different again, if I recall correctly, but I
+don't have it hanging around anymore.
+
+The jamfile builds all the components from the raw sources.
+
+The expected output of exe.exe and silly.exe are in the files exe.exp
+and silly.exp.
+
+
+The source code in this directory is in the PUBLIC DOMAIN and can be
+used or abused as you see fit. There is NO WARRANTY for this code,
+including (but not limited to) implied warranties of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE.
+
+
+Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+
-//\r
-// C++ test of a dll which contains a C++ class.\r
-//\r
-\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-\r
-// Interface of class.\r
-#include "silly.h"\r
-\r
-#ifdef DERIVED_TEST\r
-// Here is a derived class too.\r
-class CMoreSilly : public CSilly\r
-{\r
- public:\r
- CMoreSilly (char* szNewName) : CSilly (szNewName) {};\r
- ~CMoreSilly ();\r
-\r
- WhatsYourName();\r
-};\r
-\r
-CMoreSilly::\r
-~CMoreSilly ()\r
-{\r
- printf ("In CMoreSilly \"%s\" destructor!\n", szName);\r
-}\r
-\r
-CMoreSilly::\r
-WhatsYourName ()\r
-{\r
- printf ("I'm more silly and my name is \"%s\"\n", szName);\r
-}\r
-#endif\r
-\r
-int\r
-main ()\r
-{\r
- CSilly* psilly = new CSilly("silly");\r
-\r
- psilly->WhatsYourName();\r
- psilly->Poke(); // Poke him, he should say "Ouch!"\r
- psilly->Stab(4); // Stab him four times he should say "Ugh!!!!"\r
-\r
- delete psilly;\r
-\r
-#ifdef DERIVED_TEST\r
- psilly = new CMoreSilly("more silly");\r
- psilly->WhatsYourName();\r
- psilly->Stab(5);\r
- delete psilly;\r
-#endif\r
-\r
- return 0;\r
-}\r
-\r
+//
+// C++ test of a dll which contains a C++ class.
+//
+
+#include <stdlib.h>
+#include <stdio.h>
+
+// Interface of class.
+#include "silly.h"
+
+#ifdef DERIVED_TEST
+// Here is a derived class too.
+class CMoreSilly : public CSilly
+{
+ public:
+ CMoreSilly (char* szNewName) : CSilly (szNewName) {};
+ ~CMoreSilly ();
+
+ WhatsYourName();
+};
+
+CMoreSilly::
+~CMoreSilly ()
+{
+ printf ("In CMoreSilly \"%s\" destructor!\n", szName);
+}
+
+CMoreSilly::
+WhatsYourName ()
+{
+ printf ("I'm more silly and my name is \"%s\"\n", szName);
+}
+#endif
+
+int
+main ()
+{
+ CSilly* psilly = new CSilly("silly");
+
+ psilly->WhatsYourName();
+ psilly->Poke(); // Poke him, he should say "Ouch!"
+ psilly->Stab(4); // Stab him four times he should say "Ugh!!!!"
+
+ delete psilly;
+
+#ifdef DERIVED_TEST
+ psilly = new CMoreSilly("more silly");
+ psilly->WhatsYourName();
+ psilly->Stab(5);
+ delete psilly;
+#endif
+
+ return 0;
+}
+
-EXPORTS\r
-DllMain@12\r
-Poke__6CSilly\r
-Stab__6CSillyi\r
-WhatsYourName__6CSilly\r
-_$_6CSilly\r
-__6CSilly\r
-__6CSillyPc\r
-__tf6CSilly\r
-__ti6CSilly\r
-_vt$6CSilly\r
+EXPORTS
+DllMain@12
+Poke__6CSilly
+Stab__6CSillyi
+WhatsYourName__6CSilly
+_$_6CSilly
+__6CSilly
+__6CSillyPc
+__tf6CSilly
+__ti6CSilly
+_vt$6CSilly
-I'm silly.\r
-Ouch!\r
-Ugh!!!!\r
-In CSilly destructor.\r
-I'm more silly and my name is "more silly"\r
-Ugh!!!!!\r
-In CMoreSilly "more silly" destructor!\r
-In CSilly destructor.\r
+I'm silly.
+Ouch!
+Ugh!!!!
+In CSilly destructor.
+I'm more silly and my name is "more silly"
+Ugh!!!!!
+In CMoreSilly "more silly" destructor!
+In CSilly destructor.
-\r
-#define DERIVED_TEST 1\r
-\r
-class CSilly\r
-{\r
- protected:\r
- char* szName;\r
-\r
- public:\r
- CSilly();\r
- CSilly(char* szName);\r
-#ifdef DERIVED_TEST\r
- virtual ~CSilly();\r
-#else\r
- ~CSilly();\r
-#endif\r
-\r
- Poke ();\r
- Stab (int nTimes);\r
-#ifdef DERIVED_TEST\r
- virtual WhatsYourName ();\r
-#else\r
- WhatsYourName ();\r
-#endif\r
-\r
-};\r
-\r
+
+#define DERIVED_TEST 1
+
+class CSilly
+{
+ protected:
+ char* szName;
+
+ public:
+ CSilly();
+ CSilly(char* szName);
+#ifdef DERIVED_TEST
+ virtual ~CSilly();
+#else
+ ~CSilly();
+#endif
+
+ Poke ();
+ Stab (int nTimes);
+#ifdef DERIVED_TEST
+ virtual WhatsYourName ();
+#else
+ WhatsYourName ();
+#endif
+
+};
+
-\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <windows.h>\r
-\r
-\r
-#if 0\r
-#define STREAMS_VERSION\r
-#endif\r
-\r
-#if defined(STREAMS_VERSION)\r
-#include <iostream.h>\r
-#endif\r
-\r
-#include "silly.h"\r
-\r
-extern "C"\r
-BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)\r
-{\r
- return TRUE;\r
-}\r
-\r
-CSilly::\r
-CSilly()\r
-{\r
- szName = NULL;\r
-}\r
-\r
-CSilly::\r
-CSilly(char* new_szName)\r
-{\r
- szName = new char[strlen(new_szName)+1];\r
-\r
- if (szName)\r
- {\r
- strcpy (szName, new_szName);\r
- }\r
-}\r
-\r
-CSilly::\r
-~CSilly()\r
-{\r
- printf ("In CSilly destructor.\n");\r
- if (szName)\r
- {\r
- delete szName;\r
- }\r
-}\r
-\r
-CSilly::\r
-Poke ()\r
-{\r
-#ifndef STREAMS_VERSION\r
- printf ("Ouch!\n");\r
-#else\r
- cout << "Ouch!" << endl;\r
-#endif\r
-}\r
-\r
-CSilly::\r
-Stab (int nTimes)\r
-{\r
-#ifndef STREAMS_VERSION\r
- printf ("Ugh");\r
-#else\r
- cout << "Ugh";\r
-#endif\r
-\r
- int i;\r
- for (i = 0; i < nTimes; i++)\r
- {\r
-#ifndef STREAMS_VERSION\r
- putchar('!');\r
-#else\r
- cout << '!' ;\r
-#endif\r
- }\r
-\r
-#ifndef STREAMS_VERSION\r
- putchar('\n');\r
-#else\r
- cout << endl;\r
-#endif\r
-}\r
-\r
-CSilly::\r
-WhatsYourName ()\r
-{\r
- if (szName)\r
- {\r
-#ifndef STREAMS_VERSION\r
- printf ("I'm %s.\n", szName);\r
-#else\r
- cout << "I'm " << szName << "." << endl;\r
-#endif\r
- }\r
- else\r
- {\r
-#ifndef STREAMS_VERSION\r
- printf ("I have no name.\n");\r
-#else\r
- cout << "I have no name." << endl;\r
-#endif\r
- }\r
-}\r
-\r
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <windows.h>
+
+
+#if 0
+#define STREAMS_VERSION
+#endif
+
+#if defined(STREAMS_VERSION)
+#include <iostream.h>
+#endif
+
+#include "silly.h"
+
+extern "C"
+BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
+{
+ return TRUE;
+}
+
+CSilly::
+CSilly()
+{
+ szName = NULL;
+}
+
+CSilly::
+CSilly(char* new_szName)
+{
+ szName = new char[strlen(new_szName)+1];
+
+ if (szName)
+ {
+ strcpy (szName, new_szName);
+ }
+}
+
+CSilly::
+~CSilly()
+{
+ printf ("In CSilly destructor.\n");
+ if (szName)
+ {
+ delete szName;
+ }
+}
+
+CSilly::
+Poke ()
+{
+#ifndef STREAMS_VERSION
+ printf ("Ouch!\n");
+#else
+ cout << "Ouch!" << endl;
+#endif
+}
+
+CSilly::
+Stab (int nTimes)
+{
+#ifndef STREAMS_VERSION
+ printf ("Ugh");
+#else
+ cout << "Ugh";
+#endif
+
+ int i;
+ for (i = 0; i < nTimes; i++)
+ {
+#ifndef STREAMS_VERSION
+ putchar('!');
+#else
+ cout << '!' ;
+#endif
+ }
+
+#ifndef STREAMS_VERSION
+ putchar('\n');
+#else
+ cout << endl;
+#endif
+}
+
+CSilly::
+WhatsYourName ()
+{
+ if (szName)
+ {
+#ifndef STREAMS_VERSION
+ printf ("I'm %s.\n", szName);
+#else
+ cout << "I'm " << szName << "." << endl;
+#endif
+ }
+ else
+ {
+#ifndef STREAMS_VERSION
+ printf ("I have no name.\n");
+#else
+ cout << "I have no name." << endl;
+#endif
+ }
+}
+
-/*\r
- * An example showing how you can obtain the UNIX-ish file number from a\r
- * FILE* and in turn how you can get the Win32 HANDLE of the file from\r
- * the file number.\r
- *\r
- * This code is in the PUBLIC DOMAIN and has NO WARRANTY.\r
- *\r
- * Colin Peters <colin@fu.is.saga-u.ac.jp>\r
- */\r
-\r
-#include <stdio.h>\r
-#include <io.h>\r
-#include <windows.h>\r
-\r
-int\r
-main (int argc, char* argv[])\r
-{\r
- char* szFileName;\r
- FILE* fileIn;\r
- int fnIn;\r
- HANDLE hFileIn;\r
- char caBuf[81];\r
- int nRead;\r
-\r
- if (argc >= 2)\r
- {\r
- szFileName = argv[1];\r
- }\r
- else\r
- {\r
- szFileName = "junk.txt";\r
- }\r
-\r
- fileIn = fopen (szFileName, "r");\r
-\r
- if (!fileIn)\r
- {\r
- printf ("Could not open %s for reading\n", szFileName);\r
- exit(1);\r
- }\r
-\r
- fnIn = fileno (fileIn);\r
- hFileIn = (HANDLE) _get_osfhandle (fnIn);\r
-\r
- printf ("OS file handle %d\n", (int) hFileIn);\r
-\r
- ReadFile (hFileIn, caBuf, 80, &nRead, NULL);\r
-\r
- printf ("Read %d bytes using ReadFile.\n", nRead);\r
-\r
- caBuf[nRead] = '\0';\r
-\r
- printf ("\"%s\"\n", caBuf);\r
-\r
- fclose (fileIn);\r
-}\r
-\r
+/*
+ * An example showing how you can obtain the UNIX-ish file number from a
+ * FILE* and in turn how you can get the Win32 HANDLE of the file from
+ * the file number.
+ *
+ * This code is in the PUBLIC DOMAIN and has NO WARRANTY.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ */
+
+#include <stdio.h>
+#include <io.h>
+#include <windows.h>
+
+int
+main (int argc, char* argv[])
+{
+ char* szFileName;
+ FILE* fileIn;
+ int fnIn;
+ HANDLE hFileIn;
+ char caBuf[81];
+ int nRead;
+
+ if (argc >= 2)
+ {
+ szFileName = argv[1];
+ }
+ else
+ {
+ szFileName = "junk.txt";
+ }
+
+ fileIn = fopen (szFileName, "r");
+
+ if (!fileIn)
+ {
+ printf ("Could not open %s for reading\n", szFileName);
+ exit(1);
+ }
+
+ fnIn = fileno (fileIn);
+ hFileIn = (HANDLE) _get_osfhandle (fnIn);
+
+ printf ("OS file handle %d\n", (int) hFileIn);
+
+ ReadFile (hFileIn, caBuf, 80, &nRead, NULL);
+
+ printf ("Read %d bytes using ReadFile.\n", nRead);
+
+ caBuf[nRead] = '\0';
+
+ printf ("\"%s\"\n", caBuf);
+
+ fclose (fileIn);
+}
+
-\r
-Main filehand.exe : filehand.c ;\r
-\r
+
+Main filehand.exe : filehand.c ;
+
-This is a test file.\r
+This is a test file.
-/*\r
- * fixargv.c\r
- *\r
- * A special function which "fixes" an argv array by replacing arguments\r
- * that need quoting with quoted versions.\r
- *\r
- * NOTE: In order to be reasonably consistent there is some misuse of the\r
- * const keyword here-- which leads to compilation warnings. These\r
- * should be ok to ignore.\r
- *\r
- * This is a sample distributed as part of the Mingw32 package.\r
- *\r
- * Contributors:\r
- * Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>\r
- *\r
- * THIS SOFTWARE IS NOT COPYRIGHTED\r
- *\r
- * This source code is offered for use in the public domain. You may\r
- * use, modify or distribute it freely.\r
- *\r
- * This code is distributed in the hope that it will be useful but\r
- * WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY\r
- * DISCLAMED. This includes but is not limited to warrenties of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
- *\r
- * $Revision$\r
- * $Author$\r
- * $Date$\r
- *\r
- */\r
-\r
-#include <string.h>\r
-#include "fixargv.h"\r
-\r
-/*\r
- * This takes a single string and fixes it, enclosing it in quotes if it\r
- * contains any spaces and/or escaping the quotes it contains.\r
- */\r
-char*\r
-fix_arg (const char* szArg)\r
-{\r
- int nQuoteAll; /* Does the whole arg need quoting? */\r
- int nBkSlRun; /* How may backslashes in a row? */\r
- char* sz;\r
- char* szNew;\r
- size_t sizeLen;\r
-\r
- nQuoteAll = 0;\r
- nBkSlRun = 0;\r
- sz = szArg;\r
- sizeLen = 1;\r
-\r
- /* First we figure out how much bigger the new string has to be\r
- * than the old one. */\r
- while (*sz != '\0')\r
- {\r
- /*\r
- * Arguments containing whitespace of wildcards will be\r
- * quoted to preserve tokenization and/or those special\r
- * characters (i.e. wildcarding will NOT be done at the\r
- * other end-- they will get the * and ? characters as is).\r
- * TODO: Is this the best way? Do we want to enable wildcards?\r
- * If so, when?\r
- */\r
- if (!nQuoteAll &&\r
- (*sz == ' ' || *sz == '\t' || *sz == '*' || *sz == '?'))\r
- {\r
- nQuoteAll = 1;\r
- }\r
- else if (*sz == '\\')\r
- {\r
- nBkSlRun++;\r
- }\r
- else\r
- {\r
- if (*sz == '\"')\r
- {\r
- sizeLen += nBkSlRun + 1;\r
- }\r
- nBkSlRun = 0;\r
- }\r
-\r
- sizeLen++;\r
- sz++;\r
- }\r
-\r
- if (nQuoteAll)\r
- {\r
- sizeLen += 2;\r
- }\r
-\r
- /*\r
- * Make a new string big enough.\r
- */\r
- szNew = (char*) malloc (sizeLen);\r
- if (!szNew)\r
- {\r
- return NULL;\r
- }\r
- sz = szNew;\r
-\r
- /* First enclosing quote for fully quoted args. */\r
- if (nQuoteAll)\r
- {\r
- *sz = '\"';\r
- sz++;\r
- }\r
-\r
- /*\r
- * Go through the string putting backslashes in front of quotes,\r
- * and doubling all backslashes immediately in front of quotes.\r
- */\r
- nBkSlRun = 0;\r
- while (*szArg != '\0')\r
- {\r
- if (*szArg == '\\')\r
- {\r
- nBkSlRun++;\r
- }\r
- else\r
- {\r
- if (*szArg == '\"')\r
- {\r
- while (nBkSlRun > 0)\r
- {\r
- *sz = '\\';\r
- sz++;\r
- nBkSlRun--;\r
- }\r
- *sz = '\\';\r
- sz++;\r
- }\r
- nBkSlRun = 0;\r
- }\r
-\r
- *sz = *szArg;\r
- sz++;\r
- szArg++;\r
- }\r
-\r
- /* Closing quote for fully quoted args. */\r
- if (nQuoteAll)\r
- {\r
- *sz = '\"';\r
- sz++;\r
- }\r
-\r
- *sz = '\0';\r
- return szNew;\r
-}\r
-\r
-/*\r
- * Takes argc and argv and returns a new argv with escaped members. Pass\r
- * this fixed argv (along with the old one) to free_fixed_argv after\r
- * you finish with it. Pass in an argc of -1 and make sure the argv vector\r
- * ends with a null pointer to have fix_argv count the arguments for you.\r
- */\r
-char* const*\r
-fix_argv (int argc, char* const* szaArgv)\r
-{\r
- char** szaNew;\r
- char* sz;\r
- int i;\r
-\r
- if (!szaArgv)\r
- {\r
- return NULL;\r
- }\r
-\r
- /*\r
- * Count the arguments if asked.\r
- */\r
- if (argc == -1)\r
- {\r
- for (i = 0; szaArgv[i]; i++)\r
- ;\r
-\r
- argc = i;\r
- }\r
-\r
- /*\r
- * If there are no args or only one arg then do no escaping.\r
- */\r
- if (argc < 2)\r
- {\r
- return szaArgv;\r
- }\r
-\r
- for (i = 1, szaNew = NULL; i < argc; i++)\r
- {\r
- sz = szaArgv[i];\r
-\r
- /*\r
- * If an argument needs fixing, then fix it.\r
- */\r
- if (strpbrk (sz, "\" \t*?"))\r
- {\r
- /*\r
- * If we haven't created a new argv list already\r
- * then make one.\r
- */\r
- if (!szaNew)\r
- {\r
- szaNew = (char**) malloc ((argc + 1) *\r
- sizeof (char*));\r
- if (!szaNew)\r
- {\r
- return NULL;\r
- }\r
-\r
- /*\r
- * Copy previous args from old to new.\r
- */\r
- memcpy (szaNew, szaArgv, sizeof(char*) * i);\r
- }\r
-\r
- /*\r
- * Now do the fixing.\r
- */\r
- szaNew[i] = fix_arg (sz);\r
- if (!szaNew[i])\r
- {\r
- /* Fixing failed, free up and return error. */\r
- free_fixed_argv (szaNew, szaArgv);\r
- return NULL;\r
- }\r
- }\r
- else if (szaNew)\r
- {\r
- szaNew[i] = sz;\r
- }\r
- }\r
-\r
- if (szaNew)\r
- {\r
- /* If we have created a new argv list then we might as well\r
- * terminate it nicely. (And we depend on it in\r
- * free_fixed_argv.) */\r
- szaNew[argc] = NULL;\r
- }\r
- else\r
- {\r
- /* If we didn't create a new argv list then return the\r
- * original. */\r
- return szaArgv;\r
- }\r
-\r
- return szaNew;\r
-}\r
-\r
-void\r
-free_fixed_argv (char* const* szaFixed, char* const* szaOld)\r
-{\r
- char* const* sza;\r
-\r
- /*\r
- * Check for error conditions. Also note that if no corrections\r
- * were required the fixed argv will actually be the same as\r
- * the old one, and we don't need to do anything.\r
- */\r
- if (!szaFixed || !szaOld || szaFixed == szaOld)\r
- {\r
- return;\r
- }\r
-\r
- /*\r
- * Go through all members of the argv list. If any of the\r
- * members in the fixed list are different from the old\r
- * list we free those members.\r
- * NOTE: The first member is never modified, so we don't need to\r
- * check.\r
- */\r
- sza = szaFixed + 1;\r
- szaOld++;\r
- while (*sza)\r
- {\r
- if (*sza != *szaOld)\r
- {\r
- free (*sza);\r
- }\r
- sza++;\r
- szaOld++;\r
- }\r
-\r
- /*\r
- * Now we can free the array of char pointers itself.\r
- */\r
- free (szaFixed);\r
-}\r
-\r
+/*
+ * fixargv.c
+ *
+ * A special function which "fixes" an argv array by replacing arguments
+ * that need quoting with quoted versions.
+ *
+ * NOTE: In order to be reasonably consistent there is some misuse of the
+ * const keyword here-- which leads to compilation warnings. These
+ * should be ok to ignore.
+ *
+ * This is a sample distributed as part of the Mingw32 package.
+ *
+ * Contributors:
+ * Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+ *
+ * THIS SOFTWARE IS NOT COPYRIGHTED
+ *
+ * This source code is offered for use in the public domain. You may
+ * use, modify or distribute it freely.
+ *
+ * This code is distributed in the hope that it will be useful but
+ * WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
+ * DISCLAMED. This includes but is not limited to warrenties of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * $Revision$
+ * $Author$
+ * $Date$
+ *
+ */
+
+#include <string.h>
+#include "fixargv.h"
+
+/*
+ * This takes a single string and fixes it, enclosing it in quotes if it
+ * contains any spaces and/or escaping the quotes it contains.
+ */
+char*
+fix_arg (const char* szArg)
+{
+ int nQuoteAll; /* Does the whole arg need quoting? */
+ int nBkSlRun; /* How may backslashes in a row? */
+ char* sz;
+ char* szNew;
+ size_t sizeLen;
+
+ nQuoteAll = 0;
+ nBkSlRun = 0;
+ sz = szArg;
+ sizeLen = 1;
+
+ /* First we figure out how much bigger the new string has to be
+ * than the old one. */
+ while (*sz != '\0')
+ {
+ /*
+ * Arguments containing whitespace of wildcards will be
+ * quoted to preserve tokenization and/or those special
+ * characters (i.e. wildcarding will NOT be done at the
+ * other end-- they will get the * and ? characters as is).
+ * TODO: Is this the best way? Do we want to enable wildcards?
+ * If so, when?
+ */
+ if (!nQuoteAll &&
+ (*sz == ' ' || *sz == '\t' || *sz == '*' || *sz == '?'))
+ {
+ nQuoteAll = 1;
+ }
+ else if (*sz == '\\')
+ {
+ nBkSlRun++;
+ }
+ else
+ {
+ if (*sz == '\"')
+ {
+ sizeLen += nBkSlRun + 1;
+ }
+ nBkSlRun = 0;
+ }
+
+ sizeLen++;
+ sz++;
+ }
+
+ if (nQuoteAll)
+ {
+ sizeLen += 2;
+ }
+
+ /*
+ * Make a new string big enough.
+ */
+ szNew = (char*) malloc (sizeLen);
+ if (!szNew)
+ {
+ return NULL;
+ }
+ sz = szNew;
+
+ /* First enclosing quote for fully quoted args. */
+ if (nQuoteAll)
+ {
+ *sz = '\"';
+ sz++;
+ }
+
+ /*
+ * Go through the string putting backslashes in front of quotes,
+ * and doubling all backslashes immediately in front of quotes.
+ */
+ nBkSlRun = 0;
+ while (*szArg != '\0')
+ {
+ if (*szArg == '\\')
+ {
+ nBkSlRun++;
+ }
+ else
+ {
+ if (*szArg == '\"')
+ {
+ while (nBkSlRun > 0)
+ {
+ *sz = '\\';
+ sz++;
+ nBkSlRun--;
+ }
+ *sz = '\\';
+ sz++;
+ }
+ nBkSlRun = 0;
+ }
+
+ *sz = *szArg;
+ sz++;
+ szArg++;
+ }
+
+ /* Closing quote for fully quoted args. */
+ if (nQuoteAll)
+ {
+ *sz = '\"';
+ sz++;
+ }
+
+ *sz = '\0';
+ return szNew;
+}
+
+/*
+ * Takes argc and argv and returns a new argv with escaped members. Pass
+ * this fixed argv (along with the old one) to free_fixed_argv after
+ * you finish with it. Pass in an argc of -1 and make sure the argv vector
+ * ends with a null pointer to have fix_argv count the arguments for you.
+ */
+char* const*
+fix_argv (int argc, char* const* szaArgv)
+{
+ char** szaNew;
+ char* sz;
+ int i;
+
+ if (!szaArgv)
+ {
+ return NULL;
+ }
+
+ /*
+ * Count the arguments if asked.
+ */
+ if (argc == -1)
+ {
+ for (i = 0; szaArgv[i]; i++)
+ ;
+
+ argc = i;
+ }
+
+ /*
+ * If there are no args or only one arg then do no escaping.
+ */
+ if (argc < 2)
+ {
+ return szaArgv;
+ }
+
+ for (i = 1, szaNew = NULL; i < argc; i++)
+ {
+ sz = szaArgv[i];
+
+ /*
+ * If an argument needs fixing, then fix it.
+ */
+ if (strpbrk (sz, "\" \t*?"))
+ {
+ /*
+ * If we haven't created a new argv list already
+ * then make one.
+ */
+ if (!szaNew)
+ {
+ szaNew = (char**) malloc ((argc + 1) *
+ sizeof (char*));
+ if (!szaNew)
+ {
+ return NULL;
+ }
+
+ /*
+ * Copy previous args from old to new.
+ */
+ memcpy (szaNew, szaArgv, sizeof(char*) * i);
+ }
+
+ /*
+ * Now do the fixing.
+ */
+ szaNew[i] = fix_arg (sz);
+ if (!szaNew[i])
+ {
+ /* Fixing failed, free up and return error. */
+ free_fixed_argv (szaNew, szaArgv);
+ return NULL;
+ }
+ }
+ else if (szaNew)
+ {
+ szaNew[i] = sz;
+ }
+ }
+
+ if (szaNew)
+ {
+ /* If we have created a new argv list then we might as well
+ * terminate it nicely. (And we depend on it in
+ * free_fixed_argv.) */
+ szaNew[argc] = NULL;
+ }
+ else
+ {
+ /* If we didn't create a new argv list then return the
+ * original. */
+ return szaArgv;
+ }
+
+ return szaNew;
+}
+
+void
+free_fixed_argv (char* const* szaFixed, char* const* szaOld)
+{
+ char* const* sza;
+
+ /*
+ * Check for error conditions. Also note that if no corrections
+ * were required the fixed argv will actually be the same as
+ * the old one, and we don't need to do anything.
+ */
+ if (!szaFixed || !szaOld || szaFixed == szaOld)
+ {
+ return;
+ }
+
+ /*
+ * Go through all members of the argv list. If any of the
+ * members in the fixed list are different from the old
+ * list we free those members.
+ * NOTE: The first member is never modified, so we don't need to
+ * check.
+ */
+ sza = szaFixed + 1;
+ szaOld++;
+ while (*sza)
+ {
+ if (*sza != *szaOld)
+ {
+ free (*sza);
+ }
+ sza++;
+ szaOld++;
+ }
+
+ /*
+ * Now we can free the array of char pointers itself.
+ */
+ free (szaFixed);
+}
+
-/*\r
- * fixargv.h\r
- *\r
- * Prototypes of utility functions for 'properly' escaping argv vectors.\r
- *\r
- * THIS SOFTWARE IS NOT COPYRIGHTED\r
- *\r
- * This source code is offered for use in the public domain. You may\r
- * use, modify or distribute it freely.\r
- *\r
- * $Revision$\r
- * $Author$\r
- * $Date$\r
- *\r
- */\r
-\r
-#ifndef _FIXARGV_H_\r
-#define _FIXARGV_H_\r
-\r
-char* fix_arg (const char* szArg);\r
-char* const* fix_argv (int argc, char* const* szaArgv);\r
-void free_fixed_argv (char* const* szaFixed, char* const* szaOld);\r
-\r
-#endif\r
+/*
+ * fixargv.h
+ *
+ * Prototypes of utility functions for 'properly' escaping argv vectors.
+ *
+ * THIS SOFTWARE IS NOT COPYRIGHTED
+ *
+ * This source code is offered for use in the public domain. You may
+ * use, modify or distribute it freely.
+ *
+ * $Revision$
+ * $Author$
+ * $Date$
+ *
+ */
+
+#ifndef _FIXARGV_H_
+#define _FIXARGV_H_
+
+char* fix_arg (const char* szArg);
+char* const* fix_argv (int argc, char* const* szaArgv);
+void free_fixed_argv (char* const* szaFixed, char* const* szaOld);
+
+#endif
-\r
-This code is a utility function I was considering adding to Mingw32. The\r
-Microsoft versions of argc, argv construction use quotes and backslashes\r
-to allow the user to pass arguments containing spaces (or quotes) to \r
-programs they invoke. The rules are\r
-\r
- - Arguments containing spaces must be enclosed in quotes.\r
- - A quote can be passed by preceeding it with a backslash.\r
- - Backslashes immediately preceeding a quote must be doubled to avoid\r
- escaping the quote.\r
-\r
-Thus an argument like:\r
-\r
- -D="Foo Bar\\"\r
-\r
-needs to be mangled as:\r
-\r
- "-D\"Foo Bar\\\\\""\r
-\r
-in order to get to the program as what was intended above.\r
-\r
-The fix_argv set of functions is meant to be used with spawnv and the\r
-like to allow a program to set up an argv array for the spawned program\r
-and have that array duplicated *exactly* in the spawned program, no\r
-matter what it contains (it also quotes 'globbing' characters like *\r
-and ?, so it does not matter if the destination has globbing turned on\r
-or not; it might be a reasonable extension to allow a flag to allow\r
-globbing characters to pass through unmolested, but they would still\r
-be quoted if the string contained whitespace).\r
-\r
-The reason for writing this came up because of problems with arguments\r
-like -DBLAH="Foo Bar" to GCC (define BLAH as a preprocessor constant\r
-being the string "Foo Bar", including the quotes). Because GCC simply\r
-passes the argument directly to CPP (the preprocessor) it had to be\r
-escaped *twice*:\r
-\r
- "-DBLAH=\"\\\"Foo Bar\\\"\""\r
-\r
-This would reach GCC as\r
-\r
- -DBLAH="\"Foo Bar\""\r
-\r
-And that would reach CPP as the desired\r
-\r
- -DBLAH="Foo Bar"\r
-\r
-One level of quoting and escaping is to be expected (although MS's\r
-standard is, arguably, not very good), but forcing the user to know\r
-how many different programs the argument is going to pass through,\r
-and perform double quoting and escaping, seems unreasonable. If\r
-GCC and friends all used fix_argv (they use their own version of\r
-it now) then the original argument could be\r
-\r
- "-DBLAH=\"Foo Bar\""\r
-\r
-And that would work fine, no matter how many different tools it\r
-passed through.\r
-\r
-The only basic limitation with this code is that it assumes that all\r
-the spawned programs use Microsoft-type escaping when interpreting\r
-their command line. Most programs on Win32 machines do (anything\r
-compiled with Mingw32 will).\r
-\r
-For now, this code has been relegated to 'sample' status. If you want\r
-to use it, feel free (it is public domain after all).\r
-\r
-Colin.\r
-\r
-P.S. Just out of interest you might try writing your own little program\r
- to look at the interaction of wildcards and quotes. Use the glob.exe\r
- program in ../globbing and see what it does with\r
-\r
- glob "foo*.txt"\r
-\r
- even if there are files foo.txt and foobar.txt in the same directory.\r
-\r
- Note that\r
-\r
- del "My *.txt"\r
-\r
- works (i.e. it deletes all files starting with My<space>). This could\r
- not be done unless del does globbing *after* processing escapes and\r
- quotes, which is not the way it seems to work normally (again see\r
- the glob example).\r
-\r
+
+This code is a utility function I was considering adding to Mingw32. The
+Microsoft versions of argc, argv construction use quotes and backslashes
+to allow the user to pass arguments containing spaces (or quotes) to
+programs they invoke. The rules are
+
+ - Arguments containing spaces must be enclosed in quotes.
+ - A quote can be passed by preceeding it with a backslash.
+ - Backslashes immediately preceeding a quote must be doubled to avoid
+ escaping the quote.
+
+Thus an argument like:
+
+ -D="Foo Bar\\"
+
+needs to be mangled as:
+
+ "-D\"Foo Bar\\\\\""
+
+in order to get to the program as what was intended above.
+
+The fix_argv set of functions is meant to be used with spawnv and the
+like to allow a program to set up an argv array for the spawned program
+and have that array duplicated *exactly* in the spawned program, no
+matter what it contains (it also quotes 'globbing' characters like *
+and ?, so it does not matter if the destination has globbing turned on
+or not; it might be a reasonable extension to allow a flag to allow
+globbing characters to pass through unmolested, but they would still
+be quoted if the string contained whitespace).
+
+The reason for writing this came up because of problems with arguments
+like -DBLAH="Foo Bar" to GCC (define BLAH as a preprocessor constant
+being the string "Foo Bar", including the quotes). Because GCC simply
+passes the argument directly to CPP (the preprocessor) it had to be
+escaped *twice*:
+
+ "-DBLAH=\"\\\"Foo Bar\\\"\""
+
+This would reach GCC as
+
+ -DBLAH="\"Foo Bar\""
+
+And that would reach CPP as the desired
+
+ -DBLAH="Foo Bar"
+
+One level of quoting and escaping is to be expected (although MS's
+standard is, arguably, not very good), but forcing the user to know
+how many different programs the argument is going to pass through,
+and perform double quoting and escaping, seems unreasonable. If
+GCC and friends all used fix_argv (they use their own version of
+it now) then the original argument could be
+
+ "-DBLAH=\"Foo Bar\""
+
+And that would work fine, no matter how many different tools it
+passed through.
+
+The only basic limitation with this code is that it assumes that all
+the spawned programs use Microsoft-type escaping when interpreting
+their command line. Most programs on Win32 machines do (anything
+compiled with Mingw32 will).
+
+For now, this code has been relegated to 'sample' status. If you want
+to use it, feel free (it is public domain after all).
+
+Colin.
+
+P.S. Just out of interest you might try writing your own little program
+ to look at the interaction of wildcards and quotes. Use the glob.exe
+ program in ../globbing and see what it does with
+
+ glob "foo*.txt"
+
+ even if there are files foo.txt and foobar.txt in the same directory.
+
+ Note that
+
+ del "My *.txt"
+
+ works (i.e. it deletes all files starting with My<space>). This could
+ not be done unless del does globbing *after* processing escapes and
+ quotes, which is not the way it seems to work normally (again see
+ the glob example).
+
-/*\r
- * A sample program demonstrating how to use _CRT_fmode to change the default\r
- * file opening mode to binary AND change stdin, stdout and stderr. Redirect\r
- * stdout to a file from the command line to see the difference.\r
- *\r
- * Also try directing a file into stdin. If you type into stdin you will get\r
- * \r\n at the end of every line... unlike UNIX. But at least if you\r
- * redirect a file in you will get exactly the characters in the file as input.\r
- *\r
- * THIS CODE IS IN THE PUBLIC DOMAIN.\r
- *\r
- * Colin Peters <colin@fu.is.saga-u.ac.jp>\r
- */\r
-\r
-#include <stdio.h>\r
-#include <fcntl.h>\r
-\r
-unsigned int _CRT_fmode = _O_BINARY;\r
-\r
-main ()\r
-{\r
- char* sz = "This is line one.\nThis is line two.\n";\r
- FILE* fp;\r
- int c;\r
-\r
- printf (sz);\r
-\r
- /* Note how this fopen does NOT indicate "wb" to open the file in\r
- * binary mode. */\r
- fp = fopen ("all.out", "w");\r
-\r
- fprintf (fp, sz);\r
-\r
- fclose (fp);\r
-\r
- if (_isatty (_fileno(stdin)))\r
- {\r
- fprintf (stderr, "Waiting for input, press Ctrl-Z to finish.\n");\r
- }\r
-\r
- while ((c = fgetc(stdin)) != EOF)\r
- {\r
- printf ("\'%c\' %02X\n", (char) c, c);\r
- }\r
-}\r
-\r
+/*
+ * A sample program demonstrating how to use _CRT_fmode to change the default
+ * file opening mode to binary AND change stdin, stdout and stderr. Redirect
+ * stdout to a file from the command line to see the difference.
+ *
+ * Also try directing a file into stdin. If you type into stdin you will get
+ * \r\n at the end of every line... unlike UNIX. But at least if you
+ * redirect a file in you will get exactly the characters in the file as input.
+ *
+ * THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+
+unsigned int _CRT_fmode = _O_BINARY;
+
+main ()
+{
+ char* sz = "This is line one.\nThis is line two.\n";
+ FILE* fp;
+ int c;
+
+ printf (sz);
+
+ /* Note how this fopen does NOT indicate "wb" to open the file in
+ * binary mode. */
+ fp = fopen ("all.out", "w");
+
+ fprintf (fp, sz);
+
+ fclose (fp);
+
+ if (_isatty (_fileno(stdin)))
+ {
+ fprintf (stderr, "Waiting for input, press Ctrl-Z to finish.\n");
+ }
+
+ while ((c = fgetc(stdin)) != EOF)
+ {
+ printf ("\'%c\' %02X\n", (char) c, c);
+ }
+}
+
-\r
-Main test.exe : test.c ;\r
-\r
-Main all.exe : all.c ;\r
-\r
+
+Main test.exe : test.c ;
+
+Main all.exe : all.c ;
+
-\r
-These two demos show how _fmode and _CRT_fmode can be used to modify the\r
-default file opening mode (text vs. binary) and/or the mode of the standard\r
-file handles.\r
-\r
+
+These two demos show how _fmode and _CRT_fmode can be used to modify the
+default file opening mode (text vs. binary) and/or the mode of the standard
+file handles.
+
-/*\r
- * A sample program demonstrating how to use fmode to change the default\r
- * file opening mode to binary. NOTE: Does not change stdin, stdout or\r
- * stderr.\r
- *\r
- * THIS CODE IS IN THE PUBLIC DOMAIN.\r
- *\r
- * Colin Peters <colin@fu.is.saga-u.ac.jp>\r
- */\r
-\r
-#include <stdio.h>\r
-#include <fcntl.h> /* Required to get _fmode and _O_BINARY */\r
-\r
-main ()\r
-{\r
- char* sz = "This is line one.\nThis is line two.\n";\r
- FILE* fp;\r
-\r
- _fmode = _O_BINARY;\r
-\r
- printf (sz);\r
-\r
- /* Note how this fopen does NOT indicate "wb" to open the file in\r
- * binary mode. */\r
- fp = fopen ("test.out", "w");\r
-\r
- fprintf (fp, sz);\r
-\r
- fclose (fp);\r
-}\r
-\r
+/*
+ * A sample program demonstrating how to use fmode to change the default
+ * file opening mode to binary. NOTE: Does not change stdin, stdout or
+ * stderr.
+ *
+ * THIS CODE IS IN THE PUBLIC DOMAIN.
+ *
+ * Colin Peters <colin@fu.is.saga-u.ac.jp>
+ */
+
+#include <stdio.h>
+#include <fcntl.h> /* Required to get _fmode and _O_BINARY */
+
+main ()
+{
+ char* sz = "This is line one.\nThis is line two.\n";
+ FILE* fp;
+
+ _fmode = _O_BINARY;
+
+ printf (sz);
+
+ /* Note how this fopen does NOT indicate "wb" to open the file in
+ * binary mode. */
+ fp = fopen ("test.out", "w");
+
+ fprintf (fp, sz);
+
+ fclose (fp);
+}
+
-#\r
-# A simple example which prints a message on a selected printer. This won't\r
-# work right unless you make PRINTDLG a packed structure!\r
-#\r
-\r
-Main prntest.exe : prntest.c ;\r
-\r
-Gui prntest.exe ;\r
-\r
+#
+# A simple example which prints a message on a selected printer. This won't
+# work right unless you make PRINTDLG a packed structure!
+#
+
+Main prntest.exe : prntest.c ;
+
+Gui prntest.exe ;
+
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <windows.h>\r
-\r
-main ()\r
-{\r
- PRINTDLG pd;\r
- DOCINFO di;\r
- char* szMessage;\r
-\r
- memset (&pd, 0, sizeof(PRINTDLG));\r
- memset (&di, 0, sizeof(DOCINFO));\r
-\r
- di.cbSize = sizeof(DOCINFO);\r
- di.lpszDocName = "Test";\r
-\r
- pd.lStructSize = sizeof(PRINTDLG);\r
- pd.Flags = PD_PAGENUMS | PD_RETURNDC;\r
- pd.nFromPage = 1;\r
- pd.nToPage = 1;\r
- pd.nMinPage = 1;\r
- pd.nMaxPage = 1;\r
-\r
- szMessage = 0;\r
-\r
- if (PrintDlg (&pd))\r
- {\r
- if (pd.hDC)\r
- {\r
- if (StartDoc (pd.hDC, &di) != SP_ERROR)\r
- {\r
- StartPage (pd.hDC);\r
-\r
- TextOut (pd.hDC, 0, 0, "Hello, printer!", 15);\r
-\r
- EndPage (pd.hDC);\r
-\r
- EndDoc (pd.hDC);\r
-\r
- szMessage = "Printed.";\r
- }\r
- else\r
- {\r
- szMessage = "Could not start document.";\r
- }\r
- }\r
- else\r
- {\r
- szMessage = "Could not create device context.";\r
- }\r
- }\r
- else\r
- {\r
- szMessage = "Canceled or printer could not be setup.";\r
- }\r
-\r
- if (szMessage)\r
- {\r
- MessageBox (NULL, szMessage, "Print Test", MB_OK);\r
- }\r
-\r
- return 0;\r
-}\r
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <windows.h>
+
+main ()
+{
+ PRINTDLG pd;
+ DOCINFO di;
+ char* szMessage;
+
+ memset (&pd, 0, sizeof(PRINTDLG));
+ memset (&di, 0, sizeof(DOCINFO));
+
+ di.cbSize = sizeof(DOCINFO);
+ di.lpszDocName = "Test";
+
+ pd.lStructSize = sizeof(PRINTDLG);
+ pd.Flags = PD_PAGENUMS | PD_RETURNDC;
+ pd.nFromPage = 1;
+ pd.nToPage = 1;
+ pd.nMinPage = 1;
+ pd.nMaxPage = 1;
+
+ szMessage = 0;
+
+ if (PrintDlg (&pd))
+ {
+ if (pd.hDC)
+ {
+ if (StartDoc (pd.hDC, &di) != SP_ERROR)
+ {
+ StartPage (pd.hDC);
+
+ TextOut (pd.hDC, 0, 0, "Hello, printer!", 15);
+
+ EndPage (pd.hDC);
+
+ EndDoc (pd.hDC);
+
+ szMessage = "Printed.";
+ }
+ else
+ {
+ szMessage = "Could not start document.";
+ }
+ }
+ else
+ {
+ szMessage = "Could not create device context.";
+ }
+ }
+ else
+ {
+ szMessage = "Canceled or printer could not be setup.";
+ }
+
+ if (szMessage)
+ {
+ MessageBox (NULL, szMessage, "Print Test", MB_OK);
+ }
+
+ return 0;
+}
-int\r
-__except_handler3(\r
- struct _EXCEPTION_RECORD* pExceptionRecord,\r
- struct EXCEPTION_REGISTRATION* pRegistrationFrame,\r
- struct _CONTEXT* pContextRecord,\r
- void* pDispatcherContext\r
- )\r
-{\r
- LONG filterFuncRet;\r
- LONG trylevel;\r
- EXCEPTION_POINTERS exceptPtrs;\r
- PSCOPETABLE pScopeTable;\r
-\r
-\r
- CLD // Clear the direction flag (make no assumptions!)\r
-\r
- // if neither the EXCEPTION_UNWINDING nor EXCEPTION_EXIT_UNWIND bit\r
- // is set... This is true the first time through the handler (the\r
- // non-unwinding case)\r
-\r
- if ( ! (pExceptionRecord->ExceptionFlags\r
- & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND)\r
- ) )\r
- {\r
- // Build the EXCEPTION_POINTERS structure on the stack\r
- exceptPtrs.ExceptionRecord = pExceptionRecord;\r
- exceptPtrs.ContextRecord = pContextRecord;\r
-\r
- // Put the pointer to the EXCEPTION_POINTERS 4 bytes below the\r
- // establisher frame. See ASM code for GetExceptionInformation\r
- *(PDWORD)((PBYTE)pRegistrationFrame - 4) = &exceptPtrs;\r
-\r
- // Get initial "trylevel" value\r
- trylevel = pRegistrationFrame->trylevel \r
-\r
- // Get a pointer to the scopetable array\r
- scopeTable = pRegistrationFrame->scopetable;\r
-\r
-search_for_handler: \r
- if ( pRegistrationFrame->trylevel != TRYLEVEL_NONE )\r
- {\r
- if ( pRegistrationFrame->scopetable[trylevel].lpfnFilter )\r
- {\r
-\r
- PUSH EBP // Save this frame EBP\r
-\r
- // !!!Very Important!!! Switch to original EBP. This is\r
- // what allows all locals in the frame to have the same\r
- // value as before the exception occurred.\r
-\r
- EBP = &pRegistrationFrame->_ebp\r
-\r
- // Call the filter function\r
- filterFuncRet = scopetable[trylevel].lpfnFilter();\r
-\r
- POP EBP // Restore handler frame EBP\r
-\r
- if ( filterFuncRet != EXCEPTION_CONTINUE_SEARCH )\r
- {\r
- if ( filterFuncRet < 0 ) // EXCEPTION_CONTINUE_EXECUTION\r
- return ExceptionContinueExecution;\r
-\r
- // If we get here, EXCEPTION_EXECUTE_HANDLER was specified\r
- scopetable == pRegistrationFrame->scopetable\r
-\r
- // Does the actual OS cleanup of registration frames\r
- // Causes this function to recurse\r
- __global_unwind2( pRegistrationFrame );\r
-\r
-\r
- // Once we get here, everything is all cleaned up, except\r
- // for the last frame, where we'll continue execution\r
- EBP = &pRegistrationFrame->_ebp\r
-\r
- __local_unwind2( pRegistrationFrame, trylevel );\r
-\r
- // NLG == "non-local-goto" (setjmp/longjmp stuff)\r
- __NLG_Notify( 1 ); // EAX == scopetable->lpfnHandler\r
-\r
- // Set the current trylevel to whatever SCOPETABLE entry\r
- // was being used when a handler was found\r
- pRegistrationFrame->trylevel = scopetable->previousTryLevel;\r
-\r
- // Call the _except {} block. Never returns.\r
- pRegistrationFrame->scopetable[trylevel].lpfnHandler();\r
- }\r
- }\r
-\r
- scopeTable = pRegistrationFrame->scopetable;\r
- trylevel = scopeTable->previousTryLevel\r
-\r
- goto search_for_handler;\r
- }\r
- else // trylevel == TRYLEVEL_NONE\r
- {\r
- retvalue == DISPOSITION_CONTINUE_SEARCH;\r
- }\r
- }\r
- else // EXCEPTION_UNWINDING or EXCEPTION_EXIT_UNWIND flags are set\r
- {\r
- PUSH EBP // Save EBP\r
-\r
- EBP = pRegistrationFrame->_ebp // Set EBP for __local_unwind2\r
-\r
- __local_unwind2( pRegistrationFrame, TRYLEVEL_NONE )\r
-\r
- POP EBP // Restore EBP\r
-\r
- retvalue == DISPOSITION_CONTINUE_SEARCH;\r
- }\r
-}\r
-\r
+int
+__except_handler3(
+ struct _EXCEPTION_RECORD* pExceptionRecord,
+ struct EXCEPTION_REGISTRATION* pRegistrationFrame,
+ struct _CONTEXT* pContextRecord,
+ void* pDispatcherContext
+ )
+{
+ LONG filterFuncRet;
+ LONG trylevel;
+ EXCEPTION_POINTERS exceptPtrs;
+ PSCOPETABLE pScopeTable;
+
+
+ CLD // Clear the direction flag (make no assumptions!)
+
+ // if neither the EXCEPTION_UNWINDING nor EXCEPTION_EXIT_UNWIND bit
+ // is set... This is true the first time through the handler (the
+ // non-unwinding case)
+
+ if ( ! (pExceptionRecord->ExceptionFlags
+ & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND)
+ ) )
+ {
+ // Build the EXCEPTION_POINTERS structure on the stack
+ exceptPtrs.ExceptionRecord = pExceptionRecord;
+ exceptPtrs.ContextRecord = pContextRecord;
+
+ // Put the pointer to the EXCEPTION_POINTERS 4 bytes below the
+ // establisher frame. See ASM code for GetExceptionInformation
+ *(PDWORD)((PBYTE)pRegistrationFrame - 4) = &exceptPtrs;
+
+ // Get initial "trylevel" value
+ trylevel = pRegistrationFrame->trylevel
+
+ // Get a pointer to the scopetable array
+ scopeTable = pRegistrationFrame->scopetable;
+
+search_for_handler:
+ if ( pRegistrationFrame->trylevel != TRYLEVEL_NONE )
+ {
+ if ( pRegistrationFrame->scopetable[trylevel].lpfnFilter )
+ {
+
+ PUSH EBP // Save this frame EBP
+
+ // !!!Very Important!!! Switch to original EBP. This is
+ // what allows all locals in the frame to have the same
+ // value as before the exception occurred.
+
+ EBP = &pRegistrationFrame->_ebp
+
+ // Call the filter function
+ filterFuncRet = scopetable[trylevel].lpfnFilter();
+
+ POP EBP // Restore handler frame EBP
+
+ if ( filterFuncRet != EXCEPTION_CONTINUE_SEARCH )
+ {
+ if ( filterFuncRet < 0 ) // EXCEPTION_CONTINUE_EXECUTION
+ return ExceptionContinueExecution;
+
+ // If we get here, EXCEPTION_EXECUTE_HANDLER was specified
+ scopetable == pRegistrationFrame->scopetable
+
+ // Does the actual OS cleanup of registration frames
+ // Causes this function to recurse
+ __global_unwind2( pRegistrationFrame );
+
+
+ // Once we get here, everything is all cleaned up, except
+ // for the last frame, where we'll continue execution
+ EBP = &pRegistrationFrame->_ebp
+
+ __local_unwind2( pRegistrationFrame, trylevel );
+
+ // NLG == "non-local-goto" (setjmp/longjmp stuff)
+ __NLG_Notify( 1 ); // EAX == scopetable->lpfnHandler
+
+ // Set the current trylevel to whatever SCOPETABLE entry
+ // was being used when a handler was found
+ pRegistrationFrame->trylevel = scopetable->previousTryLevel;
+
+ // Call the _except {} block. Never returns.
+ pRegistrationFrame->scopetable[trylevel].lpfnHandler();
+ }
+ }
+
+ scopeTable = pRegistrationFrame->scopetable;
+ trylevel = scopeTable->previousTryLevel
+
+ goto search_for_handler;
+ }
+ else // trylevel == TRYLEVEL_NONE
+ {
+ retvalue == DISPOSITION_CONTINUE_SEARCH;
+ }
+ }
+ else // EXCEPTION_UNWINDING or EXCEPTION_EXIT_UNWIND flags are set
+ {
+ PUSH EBP // Save EBP
+
+ EBP = pRegistrationFrame->_ebp // Set EBP for __local_unwind2
+
+ __local_unwind2( pRegistrationFrame, TRYLEVEL_NONE )
+
+ POP EBP // Restore EBP
+
+ retvalue == DISPOSITION_CONTINUE_SEARCH;
+ }
+}
+
-\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <excpt.h>\r
-#include <windows.h>\r
-\r
-#include "exutil.h"\r
-\r
-void\r
-WalkExceptionHandlers ()\r
-{\r
- PEXCEPTION_REGISTRATION_RECORD p;\r
- int i;\r
-\r
- __asm__("movl %%fs:0,%%eax;movl %%eax,%0" : "=g" (p) : : "%eax");\r
-\r
- i = 0;\r
- while (p != (PEXCEPTION_REGISTRATION_RECORD) -1 && p)\r
- {\r
- printf ("Registration %d at %08x : ", i, p);\r
- printf ("Handler = %08x ", p->handler);\r
- printf ("Next Registration = %08x\n", p->prev);\r
- p = p->prev;\r
- i++;\r
- }\r
- printf ("End of exception handler list.\n");\r
- fflush (stdout);\r
-}\r
-\r
-void\r
-DumpExceptionRecord (struct _EXCEPTION_RECORD* pExRec)\r
-{\r
- printf ("Exception: Code = %08x Flags %08x", pExRec->ExceptionCode,\r
- pExRec->ExceptionFlags);\r
-\r
- if (pExRec->ExceptionFlags)\r
- {\r
- printf (" ( ");\r
- if (pExRec->ExceptionFlags & EH_NONCONTINUABLE)\r
- {\r
- printf ("EH_NONCONTINUABLE ");\r
- }\r
- if (pExRec->ExceptionFlags & EH_UNWINDING)\r
- {\r
- printf ("EH_UNWINDING ");\r
- }\r
- if (pExRec->ExceptionFlags & EH_EXIT_UNWIND)\r
- {\r
- printf ("EH_EXIT_UNWIND ");\r
- }\r
- if (pExRec->ExceptionFlags & EH_STACK_INVALID)\r
- {\r
- printf ("EH_STACK_INVALID ");\r
- }\r
- if (pExRec->ExceptionFlags & EH_NESTED_CALL)\r
- {\r
- printf ("EH_NESTED_CALL ");\r
- }\r
- printf (")\n");\r
- }\r
- else\r
- {\r
- printf ("\n");\r
- }\r
-\r
- fflush(stdout);\r
-}\r
-\r
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <excpt.h>
+#include <windows.h>
+
+#include "exutil.h"
+
+void
+WalkExceptionHandlers ()
+{
+ PEXCEPTION_REGISTRATION_RECORD p;
+ int i;
+
+ __asm__("movl %%fs:0,%%eax;movl %%eax,%0" : "=g" (p) : : "%eax");
+
+ i = 0;
+ while (p != (PEXCEPTION_REGISTRATION_RECORD) -1 && p)
+ {
+ printf ("Registration %d at %08x : ", i, p);
+ printf ("Handler = %08x ", p->handler);
+ printf ("Next Registration = %08x\n", p->prev);
+ p = p->prev;
+ i++;
+ }
+ printf ("End of exception handler list.\n");
+ fflush (stdout);
+}
+
+void
+DumpExceptionRecord (struct _EXCEPTION_RECORD* pExRec)
+{
+ printf ("Exception: Code = %08x Flags %08x", pExRec->ExceptionCode,
+ pExRec->ExceptionFlags);
+
+ if (pExRec->ExceptionFlags)
+ {
+ printf (" ( ");
+ if (pExRec->ExceptionFlags & EH_NONCONTINUABLE)
+ {
+ printf ("EH_NONCONTINUABLE ");
+ }
+ if (pExRec->ExceptionFlags & EH_UNWINDING)
+ {
+ printf ("EH_UNWINDING ");
+ }
+ if (pExRec->ExceptionFlags & EH_EXIT_UNWIND)
+ {
+ printf ("EH_EXIT_UNWIND ");
+ }
+ if (pExRec->ExceptionFlags & EH_STACK_INVALID)
+ {
+ printf ("EH_STACK_INVALID ");
+ }
+ if (pExRec->ExceptionFlags & EH_NESTED_CALL)
+ {
+ printf ("EH_NESTED_CALL ");
+ }
+ printf (")\n");
+ }
+ else
+ {
+ printf ("\n");
+ }
+
+ fflush(stdout);
+}
+
-EXPORTS\r
- WalkExceptionHandlers\r
- DumpExceptionRecord\r
+EXPORTS
+ WalkExceptionHandlers
+ DumpExceptionRecord
-/*\r
- * Definitions of some internal stuff for exception handling, including\r
- * a version of the all-important EXCEPTION_REGISTRATION_RECORD.\r
- */\r
-\r
-#ifndef _EXUTIL_H_\r
-#define _EXUTIL_H_\r
-\r
-#include <windows.h>\r
-#include <excpt.h>\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-void WalkExceptionHandlers ();\r
-void DumpExceptionRecord (struct _EXCEPTION_RECORD* pExRec);\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-#endif\r
+/*
+ * Definitions of some internal stuff for exception handling, including
+ * a version of the all-important EXCEPTION_REGISTRATION_RECORD.
+ */
+
+#ifndef _EXUTIL_H_
+#define _EXUTIL_H_
+
+#include <windows.h>
+#include <excpt.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void WalkExceptionHandlers ();
+void DumpExceptionRecord (struct _EXCEPTION_RECORD* pExRec);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
-\r
-Dll exutil.dll : exutil.c ;\r
-\r
-ImportLib libexutil.a : exutil.def ;\r
-\r
-Main sehtest.exe : sehtest.c ;\r
-\r
-Main sehfix.exe : sehfix.c ;\r
-\r
-Main sehsub.exe : sehsub.c ;\r
-\r
-LinkLibraries sehtest.exe sehfix.exe sehsub.exe : libexutil.a ;\r
-\r
+
+Dll exutil.dll : exutil.c ;
+
+ImportLib libexutil.a : exutil.def ;
+
+Main sehtest.exe : sehtest.c ;
+
+Main sehfix.exe : sehfix.c ;
+
+Main sehsub.exe : sehsub.c ;
+
+LinkLibraries sehtest.exe sehfix.exe sehsub.exe : libexutil.a ;
+
-/*\r
- * sehfix.c\r
- *\r
- * A test program involving an exception handler that fixes the exception\r
- * causing condition.\r
- *\r
- * In this code we install an exception handler my_handler and then a piece\r
- * of inline assembly attempts to write at the address marked in eax, after\r
- * setting eax to 10. This should produce an exception. The handler then\r
- * changes the eax register of the exception context to be the address of\r
- * a static variable and restarts the code. This should allow everything\r
- * to continue.\r
- */\r
-\r
-#include <windows.h>\r
-#include <excpt.h>\r
-\r
-#include "exutil.h"\r
-\r
-int x;\r
-\r
-EXCEPTION_DISPOSITION\r
-my_handler (\r
- struct _EXCEPTION_RECORD* pExceptionRec,\r
- void* pEstablisherFrame,\r
- struct _CONTEXT* pContextRecord,\r
- void* pDispatcherContext\r
- )\r
-{\r
- printf ("In my exception handler!\n");\r
- DumpExceptionRecord (pExceptionRec);\r
- pContextRecord->Eax = (DWORD) &x;\r
- return ExceptionContinueExecution;\r
-}\r
-\r
-main ()\r
-{\r
- x = 2;\r
-\r
- printf ("x = %d\n", x);\r
-\r
- WalkExceptionHandlers();\r
-\r
- __try1(my_handler)\r
-\r
- WalkExceptionHandlers();\r
-\r
- /* This assembly code should produce an exception. */\r
- __asm__("movl $10,%%eax;movl $1,(%%eax);" : : : "%eax");\r
-\r
- __except1\r
-\r
- WalkExceptionHandlers();\r
-\r
- printf ("x = %d\n", x);\r
-\r
- printf ("Finished!\n");\r
-}\r
-\r
-\r
+/*
+ * sehfix.c
+ *
+ * A test program involving an exception handler that fixes the exception
+ * causing condition.
+ *
+ * In this code we install an exception handler my_handler and then a piece
+ * of inline assembly attempts to write at the address marked in eax, after
+ * setting eax to 10. This should produce an exception. The handler then
+ * changes the eax register of the exception context to be the address of
+ * a static variable and restarts the code. This should allow everything
+ * to continue.
+ */
+
+#include <windows.h>
+#include <excpt.h>
+
+#include "exutil.h"
+
+int x;
+
+EXCEPTION_DISPOSITION
+my_handler (
+ struct _EXCEPTION_RECORD* pExceptionRec,
+ void* pEstablisherFrame,
+ struct _CONTEXT* pContextRecord,
+ void* pDispatcherContext
+ )
+{
+ printf ("In my exception handler!\n");
+ DumpExceptionRecord (pExceptionRec);
+ pContextRecord->Eax = (DWORD) &x;
+ return ExceptionContinueExecution;
+}
+
+main ()
+{
+ x = 2;
+
+ printf ("x = %d\n", x);
+
+ WalkExceptionHandlers();
+
+ __try1(my_handler)
+
+ WalkExceptionHandlers();
+
+ /* This assembly code should produce an exception. */
+ __asm__("movl $10,%%eax;movl $1,(%%eax);" : : : "%eax");
+
+ __except1
+
+ WalkExceptionHandlers();
+
+ printf ("x = %d\n", x);
+
+ printf ("Finished!\n");
+}
+
+
-/*\r
- * sehsub.c\r
- *\r
- * In an attempt to see what might be going on inside CRTDLL, this program\r
- * walks the exception list after creating a new thread with _beginthread.\r
- *\r
- * It turns out that _beginthread DOES install an exception handler, as\r
- * expected, but this handler is NOT exported by CRTDLL (it is certainly\r
- * not _except_handler2 or _XcptFilter)... an odd and unpleasant turn of\r
- * events.\r
- */\r
-\r
-#include <windows.h>\r
-#include <excpt.h>\r
-#include <process.h>\r
-\r
-#include "exutil.h"\r
-\r
-extern void* __imp__except_handler3;\r
-\r
-unsigned\r
-my_thread (void * p)\r
-{\r
- printf ("In my thread.\n");\r
- WalkExceptionHandlers();\r
- return 0;\r
-}\r
-\r
-main ()\r
-{\r
- unsigned long h;\r
- unsigned id;\r
- printf ("In main.\n");\r
- WalkExceptionHandlers();\r
-\r
- printf ("Except_handler3 %08x\n", __imp__except_handler3);\r
- h = _beginthreadex (NULL, 0, my_thread, NULL, 0, &id);\r
-\r
- WaitForSingleObject ((HANDLE) h, INFINITE);\r
- CloseHandle ((HANDLE) h);\r
- return;\r
-}\r
-\r
+/*
+ * sehsub.c
+ *
+ * In an attempt to see what might be going on inside CRTDLL, this program
+ * walks the exception list after creating a new thread with _beginthread.
+ *
+ * It turns out that _beginthread DOES install an exception handler, as
+ * expected, but this handler is NOT exported by CRTDLL (it is certainly
+ * not _except_handler2 or _XcptFilter)... an odd and unpleasant turn of
+ * events.
+ */
+
+#include <windows.h>
+#include <excpt.h>
+#include <process.h>
+
+#include "exutil.h"
+
+extern void* __imp__except_handler3;
+
+unsigned
+my_thread (void * p)
+{
+ printf ("In my thread.\n");
+ WalkExceptionHandlers();
+ return 0;
+}
+
+main ()
+{
+ unsigned long h;
+ unsigned id;
+ printf ("In main.\n");
+ WalkExceptionHandlers();
+
+ printf ("Except_handler3 %08x\n", __imp__except_handler3);
+ h = _beginthreadex (NULL, 0, my_thread, NULL, 0, &id);
+
+ WaitForSingleObject ((HANDLE) h, INFINITE);
+ CloseHandle ((HANDLE) h);
+ return;
+}
+
-/*\r
- * This file tests some of the basics of structured exception handling as\r
- * implemented in excpt.h and the Windows API header files.\r
- *\r
- * The program installs two exception handlers, then attempts to write to\r
- * a pointer to an invalid address. This causes an exception which passes\r
- * through the exception handlers and on to the default system exception\r
- * handler. That handler brings up the dialog box all Windows users know\r
- * and love, and then the program is terminated.\r
- *\r
- * You might note that after the initial run up through our exception frames\r
- * we get a second run up through them with the exception code\r
- * STATUS_INVALID_DISPOSITION and the code EH_UNWINDING. This seems normal\r
- * except that the code got changed from the previous STATUS_ACCESS_VIOLATION.\r
- * I don't understand that bit particularly.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <excpt.h>\r
-\r
-#include "exutil.h"\r
-\r
-EXCEPTION_DISPOSITION\r
-my_handler (\r
- struct _EXCEPTION_RECORD* pExceptionRec,\r
- void* pEstablisherFrame,\r
- struct _CONTEXT* pContextRecord,\r
- void* pDispatcherContext\r
- )\r
-{\r
- printf ("In my exception handler!\n");\r
- DumpExceptionRecord (pExceptionRec);\r
- return ExceptionContinueSearch;\r
-}\r
-\r
-EXCEPTION_DISPOSITION\r
-my_handler2 (\r
- struct _EXCEPTION_RECORD* pExceptionRec,\r
- void* pEstablisherFrame,\r
- struct _CONTEXT* pContextRecord,\r
- void* pDispatcherContext\r
- )\r
-{\r
- printf ("In top exception handler!\n");\r
- DumpExceptionRecord (pExceptionRec);\r
- return ExceptionContinueSearch;\r
-}\r
-\r
-main ()\r
-{\r
- char* x;\r
-\r
- printf ("my_handler2 = %08x\n", my_handler2);\r
- printf ("my_handler = %08x\n", my_handler);\r
-\r
- WalkExceptionHandlers();\r
-\r
- __try1(my_handler2)\r
- x = (char*) 10;\r
-\r
- WalkExceptionHandlers();\r
-\r
- __try1(my_handler)\r
-\r
- WalkExceptionHandlers();\r
-\r
- *x = 1;\r
- __except1\r
- __except1\r
- printf ("Finished!\n");\r
-}\r
-\r
+/*
+ * This file tests some of the basics of structured exception handling as
+ * implemented in excpt.h and the Windows API header files.
+ *
+ * The program installs two exception handlers, then attempts to write to
+ * a pointer to an invalid address. This causes an exception which passes
+ * through the exception handlers and on to the default system exception
+ * handler. That handler brings up the dialog box all Windows users know
+ * and love, and then the program is terminated.
+ *
+ * You might note that after the initial run up through our exception frames
+ * we get a second run up through them with the exception code
+ * STATUS_INVALID_DISPOSITION and the code EH_UNWINDING. This seems normal
+ * except that the code got changed from the previous STATUS_ACCESS_VIOLATION.
+ * I don't understand that bit particularly.
+ */
+
+#include <stdio.h>
+#include <excpt.h>
+
+#include "exutil.h"
+
+EXCEPTION_DISPOSITION
+my_handler (
+ struct _EXCEPTION_RECORD* pExceptionRec,
+ void* pEstablisherFrame,
+ struct _CONTEXT* pContextRecord,
+ void* pDispatcherContext
+ )
+{
+ printf ("In my exception handler!\n");
+ DumpExceptionRecord (pExceptionRec);
+ return ExceptionContinueSearch;
+}
+
+EXCEPTION_DISPOSITION
+my_handler2 (
+ struct _EXCEPTION_RECORD* pExceptionRec,
+ void* pEstablisherFrame,
+ struct _CONTEXT* pContextRecord,
+ void* pDispatcherContext
+ )
+{
+ printf ("In top exception handler!\n");
+ DumpExceptionRecord (pExceptionRec);
+ return ExceptionContinueSearch;
+}
+
+main ()
+{
+ char* x;
+
+ printf ("my_handler2 = %08x\n", my_handler2);
+ printf ("my_handler = %08x\n", my_handler);
+
+ WalkExceptionHandlers();
+
+ __try1(my_handler2)
+ x = (char*) 10;
+
+ WalkExceptionHandlers();
+
+ __try1(my_handler)
+
+ WalkExceptionHandlers();
+
+ *x = 1;
+ __except1
+ __except1
+ printf ("Finished!\n");
+}
+
-\r
-#include <windows.h>\r
-\r
-BOOL WINAPI\r
-DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)\r
-{\r
- switch (dwReason)\r
- {\r
- case DLL_PROCESS_ATTACH:\r
- printf ("DLL Attached.\n");\r
- break;\r
-\r
- case DLL_PROCESS_DETACH:\r
- printf ("DLL Detached.\n");\r
- break;\r
-\r
- case DLL_THREAD_ATTACH:\r
- printf ("DLL Thread Attached.\n");\r
- break;\r
-\r
- case DLL_THREAD_DETACH:\r
- printf ("DLL Thread Detached.\n");\r
- break;\r
- }\r
- return TRUE;\r
-}\r
-\r
-void\r
-Test ()\r
-{\r
- printf ("Test Function called!\n");\r
-}\r
-\r
+
+#include <windows.h>
+
+BOOL WINAPI
+DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
+{
+ switch (dwReason)
+ {
+ case DLL_PROCESS_ATTACH:
+ printf ("DLL Attached.\n");
+ break;
+
+ case DLL_PROCESS_DETACH:
+ printf ("DLL Detached.\n");
+ break;
+
+ case DLL_THREAD_ATTACH:
+ printf ("DLL Thread Attached.\n");
+ break;
+
+ case DLL_THREAD_DETACH:
+ printf ("DLL Thread Detached.\n");
+ break;
+ }
+ return TRUE;
+}
+
+void
+Test ()
+{
+ printf ("Test Function called!\n");
+}
+
-//\r
-// This is a C++ version of the code in dll.c. NOTE that you need to put\r
-// extern "C" { ... } around DllMain or it will not be called when your\r
-// Dll starts up! (It will get name mangled as a C++ function and the C\r
-// default version in libmingw32.a will get called instead.)\r
-//\r
-\r
-#include <windows.h>\r
-\r
-#include <iostream>\r
-\r
-extern "C" {\r
-\r
-BOOL WINAPI\r
-DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)\r
-{\r
- switch (dwReason)\r
- {\r
- case DLL_PROCESS_ATTACH:\r
- cout << "Dll Attached" << endl ;\r
- break;\r
-\r
- case DLL_PROCESS_DETACH:\r
- cout << "Dll Detached" << endl ;\r
- break;\r
-\r
- case DLL_THREAD_ATTACH:\r
- printf ("DLL Thread Attached.\n");\r
- break;\r
-\r
- case DLL_THREAD_DETACH:\r
- printf ("DLL Thread Detached.\n");\r
- break;\r
- }\r
- return TRUE;\r
-}\r
-\r
-void\r
-Test ()\r
-{\r
- printf ("Test Function called!\n");\r
-}\r
-\r
-};\r
+//
+// This is a C++ version of the code in dll.c. NOTE that you need to put
+// extern "C" { ... } around DllMain or it will not be called when your
+// Dll starts up! (It will get name mangled as a C++ function and the C
+// default version in libmingw32.a will get called instead.)
+//
+
+#include <windows.h>
+
+#include <iostream>
+
+extern "C" {
+
+BOOL WINAPI
+DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
+{
+ switch (dwReason)
+ {
+ case DLL_PROCESS_ATTACH:
+ cout << "Dll Attached" << endl ;
+ break;
+
+ case DLL_PROCESS_DETACH:
+ cout << "Dll Detached" << endl ;
+ break;
+
+ case DLL_THREAD_ATTACH:
+ printf ("DLL Thread Attached.\n");
+ break;
+
+ case DLL_THREAD_DETACH:
+ printf ("DLL Thread Detached.\n");
+ break;
+ }
+ return TRUE;
+}
+
+void
+Test ()
+{
+ printf ("Test Function called!\n");
+}
+
+};
-EXPORTS\r
-Test\r
+EXPORTS
+Test
-#include <stdio.h>\r
-\r
-extern void Test();\r
-\r
-int main()\r
-{\r
- printf ("Program started.\n");\r
- Test ();\r
- printf ("Program ends.\n");\r
-\r
- return 0;\r
-}\r
-\r
+#include <stdio.h>
+
+extern void Test();
+
+int main()
+{
+ printf ("Program started.\n");
+ Test ();
+ printf ("Program ends.\n");
+
+ return 0;
+}
+
-\r
-Dll dll.dll : dll.c ;\r
-\r
-ImportLib libdll.a : dll.def ;\r
-\r
-\r
-Main exe.exe : exe.c ;\r
-\r
-LinkLibraries exe.exe : libdll.a ;\r
-\r
-DEPENDS exe.exe : dll.dll ;\r
-\r
-LINKFLAGS on exe.exe = $(LINKFLAGS) -L. ;\r
-\r
+
+Dll dll.dll : dll.c ;
+
+ImportLib libdll.a : dll.def ;
+
+
+Main exe.exe : exe.c ;
+
+LinkLibraries exe.exe : libdll.a ;
+
+DEPENDS exe.exe : dll.dll ;
+
+LINKFLAGS on exe.exe = $(LINKFLAGS) -L. ;
+
-#\r
-# A very simple example with which you can test your compiler/jam setup.\r
-# Also an interesting example of a hybrid console/GUI application.\r
-#\r
-\r
-Main test.exe : test.c ;\r
-\r
-GuiLibs test.exe ;\r
-\r
+#
+# A very simple example with which you can test your compiler/jam setup.
+# Also an interesting example of a hybrid console/GUI application.
+#
+
+Main test.exe : test.c ;
+
+GuiLibs test.exe ;
+
-/*\r
- * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.\r
- *\r
- * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>, April 15, 1997.\r
- */\r
-\r
-#include <windows.h>\r
-#include <stdio.h>\r
-\r
-int STDCALL\r
-WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)\r
-{\r
- char text[80];\r
-\r
- printf ("Enter message box text:");\r
- fgets(text, 80, stdin);\r
- MessageBox (NULL, text, "Test", MB_OK);\r
- printf ("\nHello after message box.\n");\r
- return 0;\r
-}\r
+/*
+ * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
+ *
+ * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>, April 15, 1997.
+ */
+
+#include <windows.h>
+#include <stdio.h>
+
+int STDCALL
+WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
+{
+ char text[80];
+
+ printf ("Enter message box text:");
+ fgets(text, 80, stdin);
+ MessageBox (NULL, text, "Test", MB_OK);
+ printf ("\nHello after message box.\n");
+ return 0;
+}
-#\r
-# A example of Win32 GUI programming in C.\r
-#\r
-\r
-Main test.exe : test.c ;\r
-\r
-Gui test.exe ;\r
-\r
+#
+# A example of Win32 GUI programming in C.
+#
+
+Main test.exe : test.c ;
+
+Gui test.exe ;
+
-/*\r
- * A basic example of Win32 programming in C.\r
- *\r
- * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.\r
- *\r
- * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>\r
- */\r
-\r
-#include <windows.h>\r
-#include <string.h>\r
-\r
-/*\r
- * This is the window function for the main window. Whenever a message is\r
- * dispatched using DispatchMessage (or sent with SendMessage) this function\r
- * gets called with the contents of the message.\r
- */\r
-LRESULT CALLBACK\r
-MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)\r
-{\r
- /* The window handle for the "Click Me" button. */\r
- static HWND hwndButton = 0;\r
- static int cx, cy; /* Height and width of our button. */\r
-\r
- HDC hdc; /* A device context used for drawing */\r
- PAINTSTRUCT ps; /* Also used during window drawing */\r
- RECT rc; /* A rectangle used during drawing */\r
-\r
- /*\r
- * Perform processing based on what kind of message we got.\r
- */\r
- switch (nMsg)\r
- {\r
- case WM_CREATE:\r
- {\r
- /* The window is being created. Create our button\r
- * window now. */\r
- TEXTMETRIC tm;\r
-\r
- /* First we use the system fixed font size to choose\r
- * a nice button size. */\r
- hdc = GetDC (hwnd);\r
- SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));\r
- GetTextMetrics (hdc, &tm);\r
- cx = tm.tmAveCharWidth * 30;\r
- cy = (tm.tmHeight + tm.tmExternalLeading) * 2;\r
- ReleaseDC (hwnd, hdc);\r
-\r
- /* Now create the button */\r
- hwndButton = CreateWindow (\r
- "button", /* Builtin button class */\r
- "Click Here",\r
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,\r
- 0, 0, cx, cy,\r
- hwnd, /* Parent is this window. */\r
- (HMENU) 1, /* Control ID: 1 */\r
- ((LPCREATESTRUCT) lParam)->hInstance,\r
- NULL\r
- );\r
-\r
- return 0;\r
- break;\r
- }\r
-\r
- case WM_DESTROY:\r
- /* The window is being destroyed, close the application\r
- * (the child button gets destroyed automatically). */\r
- PostQuitMessage (0);\r
- return 0;\r
- break;\r
-\r
- case WM_PAINT:\r
- /* The window needs to be painted (redrawn). */\r
- hdc = BeginPaint (hwnd, &ps);\r
- GetClientRect (hwnd, &rc);\r
-\r
- /* Draw "Hello, World" in the middle of the upper\r
- * half of the window. */\r
- rc.bottom = rc.bottom / 2;\r
- DrawText (hdc, "Hello, World", -1, &rc,\r
- DT_SINGLELINE | DT_CENTER | DT_VCENTER);\r
-\r
- EndPaint (hwnd, &ps);\r
- return 0;\r
- break;\r
-\r
- case WM_SIZE:\r
- /* The window size is changing. If the button exists\r
- * then place it in the center of the bottom half of\r
- * the window. */\r
- if (hwndButton &&\r
- (wParam == SIZEFULLSCREEN ||\r
- wParam == SIZENORMAL)\r
- )\r
- {\r
- rc.left = (LOWORD(lParam) - cx) / 2;\r
- rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;\r
- MoveWindow (\r
- hwndButton,\r
- rc.left, rc.top, cx, cy, TRUE);\r
- }\r
- break;\r
-\r
- case WM_COMMAND:\r
- /* Check the control ID, notification code and\r
- * control handle to see if this is a button click\r
- * message from our child button. */\r
- if (LOWORD(wParam) == 1 &&\r
- HIWORD(wParam) == BN_CLICKED &&\r
- (HWND) lParam == hwndButton)\r
- {\r
- /* Our button was clicked. Close the window. */\r
- DestroyWindow (hwnd);\r
- }\r
- return 0;\r
- break;\r
- }\r
-\r
- /* If we don't handle a message completely we hand it to the system\r
- * provided default window function. */\r
- return DefWindowProc (hwnd, nMsg, wParam, lParam);\r
-}\r
-\r
-\r
-int STDCALL\r
-WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)\r
-{\r
- HWND hwndMain; /* Handle for the main window. */\r
- MSG msg; /* A Win32 message structure. */\r
- WNDCLASSEX wndclass; /* A window class structure. */\r
- char* szMainWndClass = "WinTestWin";\r
- /* The name of the main window class */\r
-\r
- /*\r
- * First we create a window class for our main window.\r
- */\r
-\r
- /* Initialize the entire structure to zero. */\r
- memset (&wndclass, 0, sizeof(WNDCLASSEX));\r
-\r
- /* This class is called WinTestWin */\r
- wndclass.lpszClassName = szMainWndClass;\r
-\r
- /* cbSize gives the size of the structure for extensibility. */\r
- wndclass.cbSize = sizeof(WNDCLASSEX);\r
-\r
- /* All windows of this class redraw when resized. */\r
- wndclass.style = CS_HREDRAW | CS_VREDRAW;\r
-\r
- /* All windows of this class use the MainWndProc window function. */\r
- wndclass.lpfnWndProc = MainWndProc;\r
-\r
- /* This class is used with the current program instance. */\r
- wndclass.hInstance = hInst;\r
-\r
- /* Use standard application icon and arrow cursor provided by the OS */\r
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);\r
- wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);\r
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);\r
-\r
- /* Color the background white */\r
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);\r
-\r
- /*\r
- * Now register the window class for use.\r
- */\r
- RegisterClassEx (&wndclass);\r
-\r
- /*\r
- * Create our main window using that window class.\r
- */\r
- hwndMain = CreateWindow (\r
- szMainWndClass, /* Class name */\r
- "Hello", /* Caption */\r
- WS_OVERLAPPEDWINDOW, /* Style */\r
- CW_USEDEFAULT, /* Initial x (use default) */\r
- CW_USEDEFAULT, /* Initial y (use default) */\r
- CW_USEDEFAULT, /* Initial x size (use default) */\r
- CW_USEDEFAULT, /* Initial y size (use default) */\r
- NULL, /* No parent window */\r
- NULL, /* No menu */\r
- hInst, /* This program instance */\r
- NULL /* Creation parameters */\r
- );\r
- \r
- /*\r
- * Display the window which we just created (using the nShow\r
- * passed by the OS, which allows for start minimized and that\r
- * sort of thing).\r
- */\r
- ShowWindow (hwndMain, nShow);\r
- UpdateWindow (hwndMain);\r
-\r
- /*\r
- * The main message loop. All messages being sent to the windows\r
- * of the application (or at least the primary thread) are retrieved\r
- * by the GetMessage call, then translated (mainly for keyboard\r
- * messages) and dispatched to the appropriate window procedure.\r
- * This is the simplest kind of message loop. More complex loops\r
- * are required for idle processing or handling modeless dialog\r
- * boxes. When one of the windows calls PostQuitMessage GetMessage\r
- * will return zero and the wParam of the message will be filled\r
- * with the argument to PostQuitMessage. The loop will end and\r
- * the application will close.\r
- */\r
- while (GetMessage (&msg, NULL, 0, 0))\r
- {\r
- TranslateMessage (&msg);\r
- DispatchMessage (&msg);\r
- }\r
- return msg.wParam;\r
-}\r
-\r
+/*
+ * A basic example of Win32 programming in C.
+ *
+ * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
+ *
+ * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+ */
+
+#include <windows.h>
+#include <string.h>
+
+/*
+ * This is the window function for the main window. Whenever a message is
+ * dispatched using DispatchMessage (or sent with SendMessage) this function
+ * gets called with the contents of the message.
+ */
+LRESULT CALLBACK
+MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
+{
+ /* The window handle for the "Click Me" button. */
+ static HWND hwndButton = 0;
+ static int cx, cy; /* Height and width of our button. */
+
+ HDC hdc; /* A device context used for drawing */
+ PAINTSTRUCT ps; /* Also used during window drawing */
+ RECT rc; /* A rectangle used during drawing */
+
+ /*
+ * Perform processing based on what kind of message we got.
+ */
+ switch (nMsg)
+ {
+ case WM_CREATE:
+ {
+ /* The window is being created. Create our button
+ * window now. */
+ TEXTMETRIC tm;
+
+ /* First we use the system fixed font size to choose
+ * a nice button size. */
+ hdc = GetDC (hwnd);
+ SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
+ GetTextMetrics (hdc, &tm);
+ cx = tm.tmAveCharWidth * 30;
+ cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
+ ReleaseDC (hwnd, hdc);
+
+ /* Now create the button */
+ hwndButton = CreateWindow (
+ "button", /* Builtin button class */
+ "Click Here",
+ WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
+ 0, 0, cx, cy,
+ hwnd, /* Parent is this window. */
+ (HMENU) 1, /* Control ID: 1 */
+ ((LPCREATESTRUCT) lParam)->hInstance,
+ NULL
+ );
+
+ return 0;
+ break;
+ }
+
+ case WM_DESTROY:
+ /* The window is being destroyed, close the application
+ * (the child button gets destroyed automatically). */
+ PostQuitMessage (0);
+ return 0;
+ break;
+
+ case WM_PAINT:
+ /* The window needs to be painted (redrawn). */
+ hdc = BeginPaint (hwnd, &ps);
+ GetClientRect (hwnd, &rc);
+
+ /* Draw "Hello, World" in the middle of the upper
+ * half of the window. */
+ rc.bottom = rc.bottom / 2;
+ DrawText (hdc, "Hello, World", -1, &rc,
+ DT_SINGLELINE | DT_CENTER | DT_VCENTER);
+
+ EndPaint (hwnd, &ps);
+ return 0;
+ break;
+
+ case WM_SIZE:
+ /* The window size is changing. If the button exists
+ * then place it in the center of the bottom half of
+ * the window. */
+ if (hwndButton &&
+ (wParam == SIZEFULLSCREEN ||
+ wParam == SIZENORMAL)
+ )
+ {
+ rc.left = (LOWORD(lParam) - cx) / 2;
+ rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
+ MoveWindow (
+ hwndButton,
+ rc.left, rc.top, cx, cy, TRUE);
+ }
+ break;
+
+ case WM_COMMAND:
+ /* Check the control ID, notification code and
+ * control handle to see if this is a button click
+ * message from our child button. */
+ if (LOWORD(wParam) == 1 &&
+ HIWORD(wParam) == BN_CLICKED &&
+ (HWND) lParam == hwndButton)
+ {
+ /* Our button was clicked. Close the window. */
+ DestroyWindow (hwnd);
+ }
+ return 0;
+ break;
+ }
+
+ /* If we don't handle a message completely we hand it to the system
+ * provided default window function. */
+ return DefWindowProc (hwnd, nMsg, wParam, lParam);
+}
+
+
+int STDCALL
+WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
+{
+ HWND hwndMain; /* Handle for the main window. */
+ MSG msg; /* A Win32 message structure. */
+ WNDCLASSEX wndclass; /* A window class structure. */
+ char* szMainWndClass = "WinTestWin";
+ /* The name of the main window class */
+
+ /*
+ * First we create a window class for our main window.
+ */
+
+ /* Initialize the entire structure to zero. */
+ memset (&wndclass, 0, sizeof(WNDCLASSEX));
+
+ /* This class is called WinTestWin */
+ wndclass.lpszClassName = szMainWndClass;
+
+ /* cbSize gives the size of the structure for extensibility. */
+ wndclass.cbSize = sizeof(WNDCLASSEX);
+
+ /* All windows of this class redraw when resized. */
+ wndclass.style = CS_HREDRAW | CS_VREDRAW;
+
+ /* All windows of this class use the MainWndProc window function. */
+ wndclass.lpfnWndProc = MainWndProc;
+
+ /* This class is used with the current program instance. */
+ wndclass.hInstance = hInst;
+
+ /* Use standard application icon and arrow cursor provided by the OS */
+ wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
+ wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
+ wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
+
+ /* Color the background white */
+ wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
+
+ /*
+ * Now register the window class for use.
+ */
+ RegisterClassEx (&wndclass);
+
+ /*
+ * Create our main window using that window class.
+ */
+ hwndMain = CreateWindow (
+ szMainWndClass, /* Class name */
+ "Hello", /* Caption */
+ WS_OVERLAPPEDWINDOW, /* Style */
+ CW_USEDEFAULT, /* Initial x (use default) */
+ CW_USEDEFAULT, /* Initial y (use default) */
+ CW_USEDEFAULT, /* Initial x size (use default) */
+ CW_USEDEFAULT, /* Initial y size (use default) */
+ NULL, /* No parent window */
+ NULL, /* No menu */
+ hInst, /* This program instance */
+ NULL /* Creation parameters */
+ );
+
+ /*
+ * Display the window which we just created (using the nShow
+ * passed by the OS, which allows for start minimized and that
+ * sort of thing).
+ */
+ ShowWindow (hwndMain, nShow);
+ UpdateWindow (hwndMain);
+
+ /*
+ * The main message loop. All messages being sent to the windows
+ * of the application (or at least the primary thread) are retrieved
+ * by the GetMessage call, then translated (mainly for keyboard
+ * messages) and dispatched to the appropriate window procedure.
+ * This is the simplest kind of message loop. More complex loops
+ * are required for idle processing or handling modeless dialog
+ * boxes. When one of the windows calls PostQuitMessage GetMessage
+ * will return zero and the wParam of the message will be filled
+ * with the argument to PostQuitMessage. The loop will end and
+ * the application will close.
+ */
+ while (GetMessage (&msg, NULL, 0, 0))
+ {
+ TranslateMessage (&msg);
+ DispatchMessage (&msg);
+ }
+ return msg.wParam;
+}
+