3/09/2009

Windows, Unix and ANSI C API Comparison

  The "Windows System Programming"(3E) has a great appendix about Windows, Unix, C Library API comparison. It's obvious that this appendix lacks of many APIs in Memory, DLL and Security related areas, but it is still very helpful for referencing when doing system programming on windows platform.
  Original content can be found here: http://my.safaribooksonline.com/0321256190/app02

1. I/O

1. 1 File & Directory


SubjectWindowsUNIXC LibraryComments
Console I/OAllocConsoleterminal I/ON/A
Console I/OFreeConsoleterminal I/ON/A
Console I/OReadConsolereadgetc, scanf, gets
Console I/OSetConsoleModeioctlN/A
Console I/OWriteConsolewriteputc, printf, puts
Directory MgtCreateDirectorymkdir*N/AMake a new directory
Directory MgtFindCloseclosedir*N/AClose a directory search handle
Directory MgtFindFirstFileopendir*, readdir*N/AFind first file matching a pattern
Directory MgtFindNextFilereaddir*N/AFind subsequent files
Directory MgtGetCurrentDirectorygetcwd*N/A
Directory MgtGetFullPathNameN/AN/A
Directory MgtGetSystemDirectoryWell-known pathnamesN/A
Directory MgtRemoveDirectoryrmdir, unlink*remove
Directory MgtSearchPathUse opendir, readdirN/ASearch for a file on a specified path
Directory MgtSetCurrentDirectorychdir*, fchdirN/AChange the working directory
Error HandlingFormatMessagestrerrorperror
Error HandlingGetLastErrorerrnoerrnoGlobal variable
Error HandlingSetLastErrorerrnoerrnoGlobal variable
File LockingLockFilefcntl (cmd=F_GETLK, ..)N/A
File LockingLockFileExfcntl (cmd=F_GETLK, ..)N/A
File LockingUnlockFilefcntl (cmd=F_GETLK, ..)N/A
File LockingUnlockFileExfcntl (cmd=F_GETLK, ..)N/A
File SystemCloseHandle (file handle)close*fcloseCloseHandle is not limited to files
File SystemCopyFileopen; read; write; closefopen; fread; fwrite; fcloseDuplicate a file
File SystemCreateFileopen*, creat*fopenOpen/create a file
File SystemDeleteFileunlink*removeDelete a file
File SystemFlushFileBuffersfsynchfflushWrite file buffers
File SystemGetFileAttributesstat*, fstat*, lstatN/A
File SystemGetFileInformationByHandlestat*, fstat*, lstatN/AFill structure with file info
File SystemGetFileSizestat*, fstat*, lstatftell, fseekGet length of file in bytes
File SystemGetFileTimestat*, fstat*, lstatN/A
File SystemGetFileTypestat*, fstat*, lstatN/ACheck for character stream device or file
File SystemGetStdHandleUse file desc 0, 1, or 2Use stdin, stdout, stderr
File SystemGetTempFileNameUse C librarytmpnamCreate a unique file name
File SystemGetTempFileName, CreateFileUse C librarytmpfileCreate a temporary file
File SystemGetTempPath/temp pathN/ADirectory for temp files
File SystemMoveFile, MoveFileExUse C libraryrenameRename a file or directory
File SystemCreateHardLinklink, unlink*N/AWindows does not support links
File SystemN/AsymlinkN/ACreate a symbolic link
File SystemN/AreadlinkN/ARead name in a symbolic link
File SystemN/A, ReadFile returns 0 bytesN/A, read returns 0 bytesfeofRest for end of file
File SystemN/A, use multiple ReadFilesreadvN/A, use multiple freadsScatter read
File SystemN/A, use multiple WriteFileswritevN/A, use multiple fwritesGather write
File SystemReadFilereadfreadRead data from a file
File SystemSetEndOfFile chsize*N/A
File SystemSetFileAttributesfcntlN/A
File SystemSetFilePointerlseekfseekSet file pointer
FileSystemSetFilePointer (to 0)lseek (0)rewind
File SystemSetFileTimeutime*N/A
File SystemSetStdHandleclose, dup*, dup2*, or fcntlfreopendup2 or fcntl
File SystemWriteFilewritefwriteWrite data to a file

1.2 Async I/O

SubjectWindowsUNIXC LibraryComments
Asynch I/OGetOverlappedResultN/AN/A
Asynch I/OReadFileExN/AN/AExtended I/O with completion routine
Asynch I/OSleepExN/AN/AAlertable wait
Asynch I/OWaitForMultipleObjects (file handles)poll, selectN/A
Asynch I/OWaitForMultipleObjectsExN/AN/AAlertable wait
Asynch I/OWriteFileExN/AN/AExtended I/O with completion routine
Asynch I/OWaitForSingleObjectExwaitpidN/AAlertable wait

2. Memory & DLL

SubjectWindowsUNIXC Library
Mapped FilesCreateFileMappingshmgetN/A
Mapped FilesMapViewOfFilemmap, shmatN/A
Mapped FilesMapViewOfFileExmmap, shmatN/A
Mapped FilesOpenFileMappingshmgetN/A
Mapped FilesUnmapViewOfFilemunmap, shmdt, shmctlN/A
Memory MgtGetProcessHeapN/AN/A
Memory MgtGetSystemInfoN/AN/A
Memory MgtHeapAllocsbrk, brk, or C librarymalloc, calloc
Memory MgtHeapCreateN/AN/A
Memory MgtHeapDestroyN/AN/A
Memory MgtHeapFreeUse C libraryfree
Memory MgtHeapReAllocUse C libraryrealloc
Memory MgtHeapSizeN/AN/A
Shared MemoryCloseHandle (map handle)shmctlN/A
Shared MemoryCreateFileMapping, OpenFileMappingshmgetN/A
Shared MemoryMapViewOfFileshmatN/A
Shared MemoryUnmapViewOfFileshmdtN/A
DLLsLoadLibrarydlopenN/A
DLLsFreeLibrarydlcloseN/A
DLLsGetProcAddressdlsynN/A
DLLsDllMainpthread_onceN/A

3. Process & Thread

3.1 Process


SubjectWindowsUNIXC LibraryComments
Process MgtCreateProcessfork (); execl ()*, system()N/AThere are 6 execxx functions
Process MgtExitProcess_exitexit
Process MgtGetCommandLineargv []argv []
Process MgtGetCurrentProcessgetpid*N/A
Process MgtGetCurrentProcessIdgetpid*N/A
Process MgtGetEnvironmentStringsN/Agetenv
Process MgtGetEnvironmentVariableN/Agetenv
Process MgtGetExitCodeProcesswait, waitpidN/A
Process MgtGetProcessTimestimes, wait3, wait4N/A
Process MgtGetProcessWorkingSetSizewait3, wait4N/A
Process MgtN/Aexecl*, execv*, execle*, execve*, execlp*, execvp*N/AWindows does not have a direct equivalent
Process MgtN/Afork, vforkN/AWindows does not have a direct equivalent
Process MgtN/AgetppidN/ANo parent/child relationships in Windows
Process MgtN/Agetgid, getegidN/ANo process groups in Windows
Process MgtN/AgetpgrpN/A
Process MgtN/AsetpgidN/A
Process MgtN/AsetsidN/A
Process MgtN/AtcgetpgrpN/A
Process MgtN/AtcsetpgrpN/A
Process MgtOpenProcessN/AN/A
Process MgtSetEnvironmentVariableputenvN/Aputenv is not part of the Standard C library
Process MgtTerminateProcesskillN/A
Synch: ProcessWaitForMultipleObjects (process handles)waitpidN/A
Synch: ProcessWaitForSingleObject (process handle)wait, waitpidN/A
TimersKillTimeralarm (0)N/A
TimersSetTimeralarmN/A
TimersSleepsleepN/A
TimersSleeppoll or select, no file descriptorN/A

3.2 Thread

SubjectWindowsUNIX/PthreadsComments
Thread MgtCreateRemoteThreadN/A
TLSTlsAllocpthread_key_alloc
TLSTlsFreepthread_key_delete
TLSTlsGetValuepthread_getspecific
TLSTlsSetValuepthread_setspecific
Thread MgtCreateThread, _beginthreadexpthread_create
Thread MgtExitThread, _endthreadexpthread_exit
Thread MgtGetCurrentThreadpthread_self
Thread MgtGetCurrentThreadIdN/A
Thread MgtGetExitCodeThreadpthread_yield
Thread MgtResumeThreadN/A
Thread MgtSuspendThreadN/A
Thread MgtTerminateThreadpthread_cancelpthread_cancel is safer
Thread MgtWaitForSingleObject(thread handle)pthread_join
Thread PriorityGetPriorityClasspthread_attr_getschedpolicy, getpriority
Thread PriorityGetThreadPrioritypthread_attr_getschedparam
Thread PrioritySetPriorityClasspthread_attr_setschedpolicy, setpriority, nice
Thread PrioritySetThreadPrioritypthread_attr_setschedparam
Note: Pthreads, while a part of all modern UNIX offerings, are available on non-UNIX systems as well.

3.3 Synchronization

SubjectWindowsUNIX/PthreadsComments
Synch: CritSecDeleteCriticalSectionUse mutexes to emulate critical sections. Some systems provide proprietary equivalents.C library is not applicable
Synch: CritSecEnterCriticalSectionC library is not applicable
Synch: CritSecInitializeCriticalSection
Synch: CritSecLeaveCriticalSection
Synch: EventCloseHandle (event handle)pthread_cond_destroy
Synch: EventCreateEventpthread_cond_init
Synch: EventPulseEventpthread_cond_signalManual-reset event
Synch: EventResetEventN/A
Synch: EventSetEventpthread_cond_broadcastAuto-reset event
Synch: EventWaitForSingleObject (event handle)pthread_cond_wait
Synch: EventWaitForSingleObject (event handle)pthread_timed_wait
Synch: MutexCloseHandle (mutex handle)pthread_mutex_destroy
Synch: MutexCreateMutexpthread_mutex_init
Synch: MutexReleaseMutexpthread_mutex_unlock
Synch: MutexWaitForSingleObject (mutex handle)pthread_mutex_lock
Synch: SemCreateSemaphoresemget
Synch: SemN/AsemctlWindows does not directly support all these options
Synch: SemOpenSemaphoresemget
Synch: SemReleaseSemaphoresemop (+)
Synch: SemWaitForSingleObject (semaphore handle)semop (-)Windows can wait for only one count

3.4 IPC

SubjectWindowsUNIXC LibraryComments
IPCCallNamedPipeN/AN/ACreateFile, WriteFile, ReadFile, CloseHandle
IPCCloseHandle (pipe handle)close, msgctlpcloseNot part of the Standard C library—see Stevens
IPCConnectNamedPipeN/AN/A
IPCCreateMailslotN/AN/A
IPCCreateNamedPipemkfifo, msggetN/A
IPCCreatePipepipepopenNot part of the Standard C library—see Stevens
IPCDuplicateHandledup, dup2, or fcntlN/AOr use file names CONIN$, CONOUT$
IPCGetNamedPipeHandleStatestat, fstat, lstat64N/A
IPCGetNamedPipeInfostat, fstat, lstatN/A
IPCImpersonateNamedPipeClientN/AN/A
IPCPeekNamedPipeN/AN/A
IPCReadFile (named pipe handle)read (fifo), msgsndN/A
IPCRevertToSelfN/AN/A
IPCSetNamedPipeHandleStateN/AN/A
IPCTransactNamedPipeN/AN/AWriteFile; ReadFile
IPCWriteFile (named pipe handle)write (fifo), msgrcvN/A
Misc.GetComputerNameunameN/A
Misc.SetComputerNameN/AN/A
SecuritySetNamedPipeIdentityUse directory sticky bitN/A

4. Misc

4.1 Security

SubjectWindowsUNIXComments
SecurityAddAccessAllowedAcechmod, fchmodC library does not support security

SecurityAddAccessDeniedAcechmod, fchmod
SecurityAddAuditAceN/A
SecurityCreatePrivateObjectSecurityN/A
SecurityDeleteAcechmod, fchmod
SecurityDestroyPrivateObjectSecurityN/A
SecurityGetAcestat*, fstat*, lstat
SecurityGetAclInformationstat*, fstat*, lstat
SecurityGetFileSecuritystat*, fstat*, lstat
SecurityGetPrivateObjectSecurityN/A
SecurityGetSecurityDescriptorDaclstat*, fstat*, lstat
SecurityGetUserNamegetlogin
SecurityInitializeAclN/A
SecurityInitializeSecurityDescriptorUmask
SecurityLookupAccountNamegetpwnam, getgrnam
SecurityLookupAccountSidgetpwuid, getuid, geteuid
SecurityN/Agetpwend, setpwent, endpwent
SecurityN/Agetgrent, setgrent, endgrent
SecurityN/ASetuid, seteuid, setreuid
SecurityN/ASetgid, setegid, setregid
SecurityOpenProcessTokengetgroups, setgroups, initgroups
SecuritySetFileSecuritychmod*, fchmod
SecuritySetPrivateObjectSecurityN/A
SecuritySetSecurityDescriptorDaclUmask
SecuritySetSecurityDescriptorGroupchown, fchown, lchown
SecuritySetSecurityDescriptorOwnerchown, fchown, lchown
SecuritySetSecurityDescriptorSaclN/A

4.2 Exception Handling

SubjectWindowsUNIXC Library
SEH_try – _exceptUse C library signalsUse C library signals
SEH_try – _finallyUse C library signalsUse C library signals
SEHAbnormalTerminationUse C library signalsUse C library signals
SEHGetExceptionCodeUse C library signalsUse C library signals
SEHRaiseExceptionUse C library signalssignal, raise
SignalsUse _finally blockUse C libraryatexit
SignalsUse C library or terminate processkillraise
SignalsUse C libraryUse C librarysignal
SignalsUse SEH, VEHsigemptysetN/A
SignalsUse SEH, VEHsigfillsetN/A
SignalsUse SEH, VEHsigaddsetN/A
SignalsUse SEH, VEHsigdelsetN/A
SignalsUse SEH, VEHsigismemberN/A
SignalsUse SEH, VEHsigprocmaskN/A
SignalsUse SEH, VEHsigpendingN/A
SignalsUse SEH, VEHsigactionN/A
SignalsUse SEH, VEHsigsetjmpN/A
SignalsUse SEH, VEHsiglongjmpN/A
SignalsUse SEH, VEHsigsuspendfN/A
SignalsUse SEH, VEHpsignalN/A
SignalsUse SEH, VEH, or C libraryUse C libraryabort
Note: Many UNIX vendors provide proprietary exception handling capabilities.

4.3 System Information & Time

SubjectWindowsUNIXC LibraryComments
System InfoGetDiskFreeSpaceN/AN/A
System InfoGetSystemInfogetrusageN/A
System InfoGetVersionunameN/A
System InfoGetVolumeInformationN/AN/A
System InfoGlobalMemoryStatusgetrlimitN/A
System InfoVarious defined constantssysconf, pathconf, fpathconfN/A
TimeGetSystemTimeUse C librarytime, gmtime
TimeSee ls program,
Use C libraryasctime
TimeCompareFileTimeUse C librarydifftimeCompare "calendar" times
TimeFileTimeToLocalFileTime, FileTimeToSystemTimeUse C librarylocaltime
TimeFileTimeToSystemTimeUse C librarygmtime
TimeGetLocalTimeUse C librarytime, localtime
TimeSee touch program,
Use C librarystrftime
TimeSetLocalTimeN/AN/A
TimeSetSystemTimeN/AN/A
TimeSubtract file timesUse C librarydifftime
TimeSystemTimeToFileTimeUse C librarymktime

1 comment:

goooooood girl said...

your blog is feel good......