📄 how-api.texinfo
字号:
@section Cygwin API Questions@subsection How does everything work?There's a C library which provides a Unix-style API. Theapplications are linked with it and voila - they run on Windows.The aim is to add all the goop necessary to make your apps run onWindows into the C library. Then your apps should run on Unix andWindows with no changes at the source level.The C library is in a DLL, which makes basic applications quite small.And it allows relatively easy upgrades to the Win32/Unix translationlayer, providing that dll changes stay backward-compatible.For a good overview of Cygwin, you may want to read the paper on Cygwinpublished by the Usenix Association in conjunction with the 2d Usenix NTSymposium in August 1998. It is available in html format on the projectWWW site.@subsection Are development snapshots for the Cygwin library available?Yes. They're made whenever anything interesting happens inside theCygwin library (usually roughly on a nightly basis, depending on how muchis going on). They are only intended for those people who wish tocontribute code to the project. If you aren't going to be happydebugging problems in a buggy snapshot, avoid these and wait for a realrelease. The snapshots are available fromhttp://cygwin.com/snapshots/@subsection How is the DOS/Unix CR/LF thing handled?Let's start with some background.In UNIX, a file is a file and what the file contains is whatever theprogram/programmer/user told it to put into it. In Windows, a file isalso a file and what the file contains depends not only on theprogram/programmer/user but also the file processing mode.When processing in text mode, certain values of data are treatedspecially. A \n (new line) written to the file will prepend a \r(carriage return) so that if you `printf("Hello\n") you in fact get"Hello\r\n". Upon reading this combination, the \r is removed and thenumber of bytes returned by the read is 1 less than was actually read.This tends to confuse programs dependant on ftell() and fseek(). ACtrl-Z encountered while reading a file sets the End Of File flags eventhough it truly isn't the end of file.One of Cygwin's goals is to make it possible to easily mix Cygwin-portedUnix programs with generic Windows programs. As a result, Cygwin opensfiles in text mode as is normal under Windows. In the accompanyingtools, tools that deal with binaries (e.g. objdump) operate in unixbinary mode and tools that deal with text files (e.g. bash) operate intext mode.Some people push the notion of globally setting the default processingmode to binary via mount point options or by setting the CYGWINenvironment variable. But that creates a different problem. Inbinary mode, the program receives all of the data in the file, includinga \r. Since the programs will no longer deal with these properly foryou, you would have to remove the \r from the relevant text files,especially scripts and startup resource files. This is a porter "copout", forcing the user to deal with the \r for the porter.It is rather easy for the porter to fix the source code by supplying theappropriate file processing mode switches to the open/fopen functions.Treat all text files as text and treat all binary files as binary.To be specific, you can select binary mode by adding @code{O_BINARY} tothe second argument of an @code{open} call, or @code{"b"} to secondargument of an @code{fopen} call. You can also call @code{setmode (fd,O_BINARY)}.Note that because the open/fopen switches are defined by ANSI, theyexist under most flavors of Unix; open/fopen will just ignore the switchsince they have no meaning to UNIX.Explanation adapted from mailing list email by Earnie Boyd<earnie_boyd@@yahoo.com>.@subsection Is the Cygwin library multi-thread-safe?Yes.There is also extensive support for 'POSIX threads', see the file@code{cygwin.din} for the list of POSIX thread functions provided.@subsection Why is some functionality only supported in Windows NT?Windows 9x: n.32 bit extensions and a graphical shell for a 16 bit patch to an8 bit operating system originally coded for a 4 bit microprocessor,written by a 2 bit company that can't stand 1 bit of competition.But seriously, Windows 9x lacks most of the security-related calls andhas several other deficiencies with respect to its version of the Win32API. See the calls.texinfo document for more information as to whatis not supported in Win 9x.@subsection How is fork() implemented?Cygwin fork() essentially works like a non-copy on write versionof fork() (like old Unix versions used to do). Because of this itcan be a little slow. In most cases, you are better off using thespawn family of calls if possible.Here's how it works:Parent initializes a space in the Cygwin process table for child.Parent creates child suspended using Win32 CreateProcess call, givingthe same path it was invoked with itself. Parent calls setjmp to saveits own context and then sets a pointer to this in the Cygwin sharedmemory area (shared among all Cygwin tasks). Parent fills in the childs.data and .bss subsections by copying from its own address space intothe suspended child's address space. Parent then starts the child.Parent waits on mutex for child to get to safe point. Child starts anddiscovers if has been forked and then longjumps using the saved jumpbuffer. Child sets mutex parent is waiting on and then blocks onanother mutex waiting for parent to fill in its stack and heap. Parentnotices child is in safe area, copies stack and heap from itself intochild, releases the mutex the child is waiting on and returns from thefork call. Child wakes from blocking on mutex, recreates any mmappedareas passed to it via shared area and then returns from fork itself.@subsection How does wildcarding (globbing) work?If the DLL thinks it was invoked from a DOS style prompt, it runs a`globber' over the arguments provided on the command line. This meansthat if you type @code{LS *.EXE} from DOS, it will do what you mightexpect.Beware: globbing uses @code{malloc}. If your application defines@code{malloc}, that will get used. This may do horrible things to you.@subsection How do symbolic links work?Cygwin knows of two ways to create symlinks.The old method is the only valid one up to but not including version 1.3.0.If it's enabled (from 1.3.0 on by setting `nowinsymlinks' in the environmentvariable CYGWIN) Cygwin generates link files with a magic header. When youopen a file or directory that is a link to somewhere else, it opens the fileor directory listed in the magic header. Because we don't want to have toopen every referenced file to check symlink status, Cygwin marks symlinkswith the system attribute. Files without the system attribute are notchecked. Because remote samba filesystems do not enable the systemattribute by default, symlinks do not work on network drives unless youexplicitly enable this attribute.The new method which is introduced with Cygwin version 1.3.0 is enabledby default or if `winsymlinks' is set in the environment variable CYGWIN.Using this method, Cygwin generates symlinks by creating Windows shortcuts.Cygwin created shortcuts have a special header (which is in that way nevercreated by Explorer) and the R/O attribute set. A DOS path is stored inthe shortcut as usual and the description entry is used to store the POSIXpath. While the POSIX path is stored as is, the DOS path has perhaps to berearranged to result in a valid path. This may result in a divergencebetween the DOS and the POSIX path when symlinks are moved crossing mountpoints. When a user changes the shortcut, this will be detected by Cygwinand it will only use the DOS path then. While Cygwin shortcuts are shownwithout the ".lnk" suffix in `ls' output, non-Cygwin shortcuts are shownwith the suffix. However, both are treated as symlinks.Both, the old and the new symlinks can live peacefully together since Cygwintreats both as symlinks regardless of the setting of `(no)winsymlinks' inthe environment variable CYGWIN.@subsection Why do some files, which are not executables have the 'x' type.When working out the unix-style attribute bits on a file, the libraryhas to fill out some information not provided by the WIN32 API. It guesses that files ending in .exe and .bat are executable, as areones which have a "#!" as their first characters.@subsection How secure is Cygwin in a multi-user environment?Cygwin is not secure in a multi-user environment. Forexample if you have a long running daemon such as "inetd"running as admin while ordinary users are logged in, or ifyou have a user logged in remotely while another user is loggedinto the console, one cygwin client can trick another intorunning code for it. In this way one user may gain thepriveledge of another cygwin program running on the machine.This is because cygwin has shared state that is accessible by all processes.(Thanks to Tim Newsham (newsham@@lava.net) for this explanation).@subsection How do the net-related functions work?@strong{(Please note: This section has not yet been updated for the latestnet release.)}The network support in Cygwin is supposed to provide the Unix API, notthe Winsock API.There are differences between the semantics of functions with the samename under the API.E.g., the select system call on Unix can wait on a standard file handlesand handles to sockets. The select call in winsock can only wait onsockets. Because of this, cygwin.dll does a lot of nasty stuff behindthe scenes, trying to persuade various winsock/win32 functions to do whata Unix select would do.If you are porting an application which already uses Winsock, thenusing the net support in Cygwin is wrong.But you can still use native Winsock, and use Cygwin. The functionswhich cygwin.dll exports are called 'cygwin_<name>'. Thereare a load of defines which map the standard Unix names to the namesexported by the dll -- check out include/netdb.h:@example..etc..void cygwin_setprotoent (int);void cygwin_setservent (int);void cygwin_setrpcent (int);..etc..#ifndef __INSIDE_CYGWIN_NET__#define endprotoent cygwin_endprotoent #define endservent cygwin_endservent #define endrpcent cygwin_endrpcent ..etc..@end exampleThe idea is that you'll get the Unix->Cygwin mapping if you includethe standard Unix header files. If you use this, you won't need tolink with libwinsock.a - all the net stuff is inside the dll.The mywinsock.h file is a standard winsock.h which has been hacked toremove the bits which conflict with the standard Unix API, or aredefined in other headers. E.g., in mywinsock.h, the definition ofstruct hostent is removed. This is because on a Unix box, it lives innetdb. It isn't a good idea to use it in your applications.As of the b19 release, this information may be slightly out of date.@subsection I don't want Unix sockets, how do I use normal Win32 winsock?@strong{(Please note: This section has not yet been updated for the latestnet release.)}To use the vanilla Win32 winsock, you just need to #define Win32_Winsockand #include "windows.h" at the top of your source file(s). You'll alsowant to add -lwsock32 to the compiler's command line so you link againstlibwsock32.a.@subsection What version numbers are associated with Cygwin?@strong{(Please note: This section has not yet been updated for the latestnet release.)}There is a cygwin.dll major version number that gets incrementedevery time we make a new Cygwin release available. Thiscorresponds to the name of the release (e.g. beta 19's majornumber is "19"). There is also a cygwin.dll minor version number. Ifwe release an update of the library for an existing release, the minornumber would be incremented.There are also Cygwin API major and minor numbers. The major numbertracks important non-backward-compatible interface changes to the API.An executable linked with an earlier major number will not be compatiblewith the latest DLL. The minor number tracks significant API additionsor changes that will not break older executables but may be required bynewly compiled ones.Then there is a shared memory region compatibity version number. It isincremented when incompatible changes are made to the shared memoryregion or to any named shared mutexes, semaphores, etc.Finally there is a mount point registry version number which keeps trackof non-backwards-compatible changes to the registry mount table layout.This has been "B15.0" since the beta 15 release.@subsection Why isn't _timezone set correctly?@strong{(Please note: This section has not yet been updated for the latestnet release.)}Did you explicitly call tzset() before checking the value of _timezone?If not, you must do so.@subsection Is there a mouse interface?There is no way to capture mouse events from Cygwin. There arecurrently no plans to add support for this.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -