├── 05UnixunitStdIo ├── 01MyEffectIo ├── 01MyEffectIo.c ├── 02MyeffectioRdWr ├── 02MyeffectioRdWr.c ├── 03MyIoStdio ├── 03MyIoStdio.c ├── 04Mytempfile ├── 04Mytempfile.c ├── 05Mymsktemp ├── 05Mymsktemp.c ├── 065-14Memorystream ├── 065-14Memorystream.c ├── std.err └── std.out ├── 06UnixunitsystemdateInfo ├── 016-2Mygetpwnam.c ├── 02Mygettime ├── 02Mygettime.c ├── 03Myuname ├── 03Myuname.c ├── 06.2Myshadow └── 06.2Myshadow.c ├── 07UnixunitForkEnv ├── 01Myhelloworld ├── 01Myhelloworld.c ├── 02Myexit ├── 02Myexit.c ├── 03Myecho ├── 03Myecho.c ├── 047-10Myjmp ├── 047-10Myjmp.c ├── 057-11externMyjmp ├── 057-11externMyjmp.c ├── 067-13Mytestjmp ├── 067-13Mytestjmp.c └── a.out ├── 08ForkContral ├── 01.out ├── 018-1MyforkGetid ├── 018-1MyforkGetid.c ├── 028-3Myvfork ├── 028-3Myvfork.c ├── 03-8_6Mypr_exitstatus ├── 03-8_6Mypr_exitstatus.c ├── 04-8_8MysolveZombieFork ├── 04-8_8MysolveZombieFork.c ├── 05-8-12MyIpc ├── 05-8-12MyIpc.c ├── 06-8_13MyextendIPC ├── 06-8_13MyextendIPC.c ├── 08-8_16Myexec ├── 08-8_16Myexec.c ├── 09-8-17Myechoall ├── 09-8-17Myechoall.c ├── 11-8_22MySystem.c ├── 12-8_23MytestSystem ├── 12-8_23MytestSystem.c ├── 13-8-24MycommandSystem ├── 13-8-24MycommandSystem.c ├── 14-08_28MyProcessAccount ├── 14-08_28MyProcessAccount.c ├── 15-08_29MyTestProcessAccount ├── 15-08_29MyTestProcessAccount.c ├── 16-08_30Mynice ├── 16-08_30Mynice.c ├── 17-08_31MyTmsShell ├── 17-08_31MyTmsShell.c ├── 18-MyZombieofork └── 18-MyZombieofork.c ├── 09Forkresulation ├── 01-09_12MyOrphanProcess ├── 01-09_12MyOrphanProcess.c ├── 9.1practice.md ├── a.out ├── main.c ├── makefile └── temp.foo ├── 10Signal ├── 01MyexternSignal ├── 01MyexternSignal.c ├── 02-1MyTrafficcontral ├── 02-1MyTrafficcontral.c ├── 02MyTrafficContral.c ├── 03-10_2Mysignal ├── 03-10_2Mysignal.c ├── 10-10_23MySigsuspendWait ├── 10-10_23MySigsuspendWait.c ├── 11-10_24MyTELLSignal.c ├── 12-10_25Myabort.c ├── 13-10_26MysystemEd ├── 13-10_26MysystemEd.c ├── 14-10_28MySystem.c ├── 15-10_31MySignalContral ├── 15-10_31MySignalContral.c ├── 2-10_5MySIGALARMsignal ├── 2-10_5MySIGALARMsignal.c ├── 3-10MySIGCHLD ├── 3-10MySIGCHLD.c ├── 3-10_6MySIGCLD ├── 3-10_6MySIGCLD.c ├── 4-10.7.8.9MySIGALRM ├── 5-10_10Myalarm ├── 5-10_10Myalarm.c ├── 6-10MyalarmLongjmp ├── 6-10MyalarmLongjmp.c ├── 7-10.11MySignalSet ├── 7-10.11MySignalSet.c ├── 8-10_20.19.18MySetjmp ├── 8-10_20.19.18MySetjmp.c ├── 9-10-22MySigsuspend ├── 9-10-22MySigsuspend.c ├── changeSystem.md ├── log ├── temp.foo └── trafficContral.txt ├── 11UnixUnitThread ├── 01-11_2MyThreadCreate ├── 01-11_2MyThreadCreate.c ├── 10-11_15MyValueMUtex.c ├── 11-11_16MyTasksPthreadSort ├── 11-11_16MyTasksPthreadSort.c ├── 2-11_3MyThreadExit ├── 2-11_3MyThreadExit.c ├── 3-11_4MyPthread_exitwrong ├── 3-11_4MyPthread_exitwrong.c ├── 4-11_5MyPthreadCleanProgram ├── 4-11_5MyPthreadCleanProgram.c ├── 5-11_10MyPthreadMutex_t.c ├── 6-11_11MyTwoMutex.c ├── 7-11_12MyUpdateTwoMutex.c ├── 8-11_13_Mypthread_mutex_timedlock ├── 8-11_13_Mypthread_mutex_timedlock.c └── 9-11_14MyReadWriteLock.c ├── 12UnixUnitThreadAttr ├── .02-12_4MySpecStatusCreatePthread.c.swo ├── .02-12_4MySpecStatusCreatePthread.c.swp ├── 01MyThreadSide ├── 01MyThreadSide.c ├── 02-12_4MySpecStatusCreatePthread ├── 02-12_4MySpecStatusCreatePthread.c ├── 06-2_16MymutexSignalPthread ├── 06-2_16MymutexSignalPthread.c ├── 07-12_17MyPthread_atfork ├── 07-12_17MyPthread_atfork.c └── 7-12_17.txt ├── 13UnixUnitDaemonFork ├── 01-13_1MyinitDaemonFork ├── 01-13_1MyinitDaemonFork.c ├── 02-13_6MySigleDaemon.c ├── 03-13_7MyDaemonSecond ├── 03-13_7MyDaemonSecond.c ├── 04-13_8MyPthreadGetSignalConf ├── 04-13_8MyPthreadGetSignalConf.c ├── 05-13_9Mysetckoexec.c └── readme.md ├── 14UnixUnitAdvancedIO ├── 01-14_1MyNoBlockIo ├── 01-14_1MyNoBlockIo.c ├── 01stderr.file ├── 01temp.file ├── 0214_5MyAccReleaseLock.c ├── 04-14_7MyDiedLock ├── 04-14_7MyDiedLock.c ├── 05-14_9Myloocfile.c ├── 06-.lock ├── 06-14_12MyConfirmLockSystem ├── 06-14_12MyConfirmLockSystem.c ├── 07-14_20MyTransFile ├── 07-14_20MyTransFile.c ├── 07_testone.md ├── 07_testtwo.md ├── 08-14_21MyAsynchronousIO ├── 08-14_21MyAsynchronousIO.c ├── 08-final.md ├── 08-test.md ├── 09-14_24MyReadnWriten.c ├── 10-14_27MyMemory-mappedIO ├── 10-14_27MyMemory-mappedIO.c ├── 10-Test.txt ├── 10-TestFinal.txt ├── 14_Homework01 ├── 14_Homework01.c ├── _testone.md ├── lockfile ├── readme.md └── templock ├── 15UnixUnitIPC ├── 01-15_5MyParent2ChildForkIPC ├── 01-15_5MyParent2ChildForkIPC.c ├── 02-15_6MyPipeDeparPage ├── 02-15_6MyPipeDeparPage.c ├── 03-15_7MyPipeTellWait.c ├── 04-15_6MyPopenOveriedCp ├── 04-15_6MyPopenOveriedCp.c ├── 05-15_12MyFunctionPopen-Pclose.c ├── 06-15_14MySimpleCp ├── 06-15_14MySimpleCp.c ├── 07-15_15MyExternPopenGetFileToExec ├── 07-15_15MyExternPopenGetFileToExec.c ├── 08-15_17MyCoprocess ├── 08-15_17MyCoprocess.c ├── 09-15_18MyCoprocessApplication ├── 09-15_18MyCoprocessApplication.c ├── 10-15_19MyStdIoCoprocessadd.c ├── 11-15_31MyPrintMemAddr ├── 11-15_31MyPrintMemAddr.c ├── 12-15_33MyIPCSharedMemUse_dev_zero ├── 12-15_33MyIPCSharedMemUse_dev_zero.c ├── 13-15_35MySemaphore.c ├── readme.md └── slock.h ├── 16UnixUnitInterIPCsocket ├── 01-16_9MyGetaddrinfoHowToUse ├── 01-16_9MyGetaddrinfoHowToUse.c ├── 02@03-16_10@11MyConnect_retry.c ├── 04-16_12MyInitSocketFunction.c ├── 05-16_16MyGetSeverTime ├── 05-16_16MyGetSeverTime.c ├── 06-_17MyPostClientTime ├── 06-_17MyPostClientTime.c ├── 07-16_MyExternSever.c └── readme.md ├── GetEnv ├── GetEnv.cpp ├── Myselectexample ├── Myselectexample.c ├── OperatorEnv ├── OperatorEnv.cpp ├── README.md ├── SerityTest.txt ├── UnixUnit01 ├── Dirls.c ├── Dirls.o ├── MyCopy.c ├── MyCopy.o ├── MyPrintuserid.c ├── MyPrintuserid.o ├── MyShell.c ├── MyShell.o ├── MyShell01singal.c ├── MyShell01singal.o ├── Myerror.c ├── Myerror.o ├── Myfock.c ├── Myfork.o ├── Myio.c ├── Myio.data ├── Myio.o └── Test*++.c ├── UnixUnitIO03 ├── MyCreatemptyio ├── MyCreatemptyio.c ├── MyCreatio.c ├── MyEffectio ├── MyEffectio.c ├── MyFileioAnnoce ├── MyFileioAnnoce.c ├── MyStdio ├── MyStdio.c ├── Myoffsetio ├── Myoffsetio.c ├── file └── myfile.txt ├── UnixUnitIO04 ├── 10Mydev ├── 10Mydev.c ├── MyFileDirTraverse ├── MyFileDirTraverse.c ├── MyJudgementIo ├── MyJudgementIo.c ├── Myaccessio ├── Myaccessio.c ├── Mychdir08 ├── Mychdir08.c ├── Mychmodio ├── Mychmodio.c ├── Myfutimes ├── Myfutimes.c ├── Mygetcwd09 ├── Mygetcwd09.c ├── Mylink ├── Mylink.c ├── Myumask ├── Myumask.c ├── bar ├── foo └── testln │ ├── a │ └── testdir ├── apue.3e ├── DISCLAIMER ├── Make.defines.freebsd ├── Make.defines.linux ├── Make.defines.macos ├── Make.defines.solaris ├── Make.libapue.inc ├── Makefile ├── README ├── advio │ ├── Makefile │ ├── deadlock.c │ ├── fixup.awk │ ├── lockfile.c │ ├── mandatory.c │ ├── mcopy2.c │ ├── nonblockw.c │ ├── readn.c │ ├── rot13a.c │ ├── rot13c2.c.in │ └── writen.c ├── daemons │ ├── Makefile │ ├── init.c │ ├── reread.c │ ├── reread2.c │ └── single.c ├── datafiles │ ├── Makefile │ ├── getpwnam.c │ └── strftime.c ├── db │ ├── Makefile │ ├── apue_db.h │ ├── db.c │ └── t4.c ├── environ │ ├── Makefile │ ├── cmd1.c │ ├── cmd2.c │ ├── doatexit.c │ ├── echoarg.c │ ├── getrlimit.c │ ├── hello1.c │ ├── opendata.c │ ├── scope.c │ └── testjmp.c ├── exercises │ ├── Makefile │ ├── asyncsocket.c │ ├── bo.c │ ├── fifo1.c │ ├── fmemopen.c │ ├── getlogin.c │ ├── getpw44bsd.c │ ├── getpwsvr4.c │ ├── goodexit.c │ ├── longpath.c │ ├── openmax.c │ ├── pendlock.c │ ├── pollmsg2.c │ ├── prtime.c │ ├── sizepipe.c │ ├── sleep.c │ ├── sleepus_poll.c │ ├── sleepus_select.c │ ├── vfork3.c │ └── zombie.c ├── figlinks │ ├── fig1.10 │ ├── fig1.3 │ ├── fig1.4 │ ├── fig1.5 │ ├── fig1.6 │ ├── fig1.7 │ ├── fig1.8 │ ├── fig1.9 │ ├── fig10.10 │ ├── fig10.11 │ ├── fig10.12 │ ├── fig10.14 │ ├── fig10.15 │ ├── fig10.18 │ ├── fig10.19 │ ├── fig10.2 │ ├── fig10.20 │ ├── fig10.22 │ ├── fig10.23 │ ├── fig10.24 │ ├── fig10.25 │ ├── fig10.26 │ ├── fig10.28 │ ├── fig10.29 │ ├── fig10.31 │ ├── fig10.5 │ ├── fig10.6 │ ├── fig10.7 │ ├── fig10.8 │ ├── fig10.9 │ ├── fig11.10 │ ├── fig11.11 │ ├── fig11.12 │ ├── fig11.14 │ ├── fig11.15 │ ├── fig11.16 │ ├── fig11.2 │ ├── fig11.3 │ ├── fig11.4 │ ├── fig11.5 │ ├── fig12.11 │ ├── fig12.12 │ ├── fig12.13 │ ├── fig12.16 │ ├── fig12.17 │ ├── fig12.4 │ ├── fig12.8 │ ├── fig13.1 │ ├── fig13.6 │ ├── fig13.7 │ ├── fig13.8 │ ├── fig13.9 │ ├── fig14.1 │ ├── fig14.12 │ ├── fig14.20 │ ├── fig14.21 │ ├── fig14.24 │ ├── fig14.27 │ ├── fig14.5 │ ├── fig14.6 │ ├── fig14.7 │ ├── fig14.9 │ ├── fig15.11 │ ├── fig15.12 │ ├── fig15.14 │ ├── fig15.15 │ ├── fig15.17 │ ├── fig15.18 │ ├── fig15.19 │ ├── fig15.31 │ ├── fig15.33 │ ├── fig15.35 │ ├── fig15.5 │ ├── fig15.6 │ ├── fig15.7 │ ├── fig16.10 │ ├── fig16.11 │ ├── fig16.12 │ ├── fig16.16 │ ├── fig16.17 │ ├── fig16.18 │ ├── fig16.19 │ ├── fig16.20 │ ├── fig16.22 │ ├── fig16.9 │ ├── fig17.10 │ ├── fig17.12 │ ├── fig17.13 │ ├── fig17.14 │ ├── fig17.15 │ ├── fig17.16 │ ├── fig17.17 │ ├── fig17.18 │ ├── fig17.19 │ ├── fig17.2 │ ├── fig17.20 │ ├── fig17.21 │ ├── fig17.22 │ ├── fig17.23 │ ├── fig17.24 │ ├── fig17.25 │ ├── fig17.26 │ ├── fig17.27 │ ├── fig17.28 │ ├── fig17.29 │ ├── fig17.3 │ ├── fig17.30 │ ├── fig17.31 │ ├── fig17.4 │ ├── fig17.5 │ ├── fig17.8 │ ├── fig17.9 │ ├── fig18.10 │ ├── fig18.11 │ ├── fig18.12 │ ├── fig18.13 │ ├── fig18.14 │ ├── fig18.15 │ ├── fig18.16 │ ├── fig18.17 │ ├── fig18.18 │ ├── fig18.20 │ ├── fig18.21 │ ├── fig18.22 │ ├── fig19.10 │ ├── fig19.11 │ ├── fig19.12 │ ├── fig19.16 │ ├── fig19.9 │ ├── fig2.13 │ ├── fig2.14 │ ├── fig2.16 │ ├── fig2.17 │ ├── fig20.3 │ ├── fig3.1 │ ├── fig3.11 │ ├── fig3.12 │ ├── fig3.2 │ ├── fig3.5 │ ├── fig4.12 │ ├── fig4.16 │ ├── fig4.21 │ ├── fig4.22 │ ├── fig4.23 │ ├── fig4.24 │ ├── fig4.25 │ ├── fig4.3 │ ├── fig4.8 │ ├── fig4.9 │ ├── fig5.11 │ ├── fig5.12 │ ├── fig5.13 │ ├── fig5.15 │ ├── fig5.4 │ ├── fig5.5 │ ├── fig6.11 │ ├── fig6.2 │ ├── fig7.1 │ ├── fig7.11 │ ├── fig7.13 │ ├── fig7.14 │ ├── fig7.16 │ ├── fig7.3 │ ├── fig7.4 │ ├── fig7.9 │ ├── fig8.1 │ ├── fig8.12 │ ├── fig8.13 │ ├── fig8.16 │ ├── fig8.17 │ ├── fig8.20 │ ├── fig8.21 │ ├── fig8.22 │ ├── fig8.23 │ ├── fig8.24 │ ├── fig8.25 │ ├── fig8.28 │ ├── fig8.29 │ ├── fig8.3 │ ├── fig8.30 │ ├── fig8.31 │ ├── fig8.5 │ ├── fig8.6 │ ├── fig8.8 │ ├── fig9.12 │ ├── figB.1 │ ├── figB.3 │ ├── figB.4 │ ├── figC.1 │ ├── figC.10 │ ├── figC.12 │ ├── figC.13 │ ├── figC.14 │ ├── figC.15 │ ├── figC.16 │ ├── figC.17 │ ├── figC.18 │ ├── figC.20 │ ├── figC.22 │ ├── figC.23 │ ├── figC.24 │ ├── figC.3 │ ├── figC.4 │ ├── figC.5 │ ├── figC.6 │ ├── figC.7 │ └── figC.8 ├── filedir │ ├── Makefile │ ├── access.c │ ├── cdpwd.c │ ├── changemod.c │ ├── devrdev.c │ ├── filetype.c │ ├── ftw8.c │ ├── mycd.c │ ├── umask.c │ ├── unlink.c │ └── zap.c ├── fileio │ ├── Makefile │ ├── fileflags.c │ ├── hole.c │ ├── mycat.c │ ├── seek.c │ └── setfl.c ├── intro │ ├── Makefile │ ├── getcputc.c │ ├── hello.c │ ├── ls1.c │ ├── mycat.c │ ├── shell1.c │ ├── shell2.c │ ├── testerror.c │ └── uidgid.c ├── ipc1 │ ├── Makefile │ ├── add2.c │ ├── add2stdio.c │ ├── devzero.c │ ├── myuclc.c │ ├── pipe1.c │ ├── pipe2.c │ ├── pipe4.c │ ├── popen.c │ ├── popen1.c │ ├── popen2.c │ ├── slock.c │ ├── slock.h │ ├── tellwait.c │ └── tshm.c ├── ipc2 │ ├── Makefile │ ├── bindunix.c │ ├── open.fe │ │ ├── Makefile │ │ ├── main.c │ │ ├── open.c │ │ └── open.h │ ├── open │ │ ├── Makefile │ │ ├── main.c │ │ ├── open.c │ │ └── open.h │ ├── opend.fe │ │ ├── Makefile │ │ ├── cliargs.c │ │ ├── main.c │ │ ├── opend.h │ │ └── request.c │ ├── opend │ │ ├── Makefile │ │ ├── cliargs.c │ │ ├── client.c │ │ ├── loop.poll.c │ │ ├── loop.select.c │ │ ├── main.c │ │ ├── opend.h │ │ └── request.c │ ├── pollmsg.c │ ├── recvfd2.c │ ├── sendfd2.c │ └── sendmsg.c ├── lib │ ├── Makefile │ ├── Orecvfd.c │ ├── bufargs.c │ ├── cliconn.c │ ├── clrfl.c │ ├── daemonize.c │ ├── errorlog.c │ ├── lockreg.c │ ├── locktest.c │ ├── nspipe.c │ ├── openmax.c │ ├── pathalloc.c │ ├── popen.c │ ├── prexit.c │ ├── prmask.c │ ├── ptyfork.c │ ├── ptyopen.c │ ├── readn.c │ ├── recvfd.c │ ├── semaph.c │ ├── senderr.c │ ├── sendfd.c │ ├── servaccept.c │ ├── servlisten.c │ ├── setfd.c │ ├── setfl.c │ ├── signal.c │ ├── signalintr.c │ ├── sleep.c │ ├── sleepus.c │ ├── spipe.c │ ├── strerror.c │ ├── tellwait.c │ ├── ttymodes.c │ └── writen.c ├── printer │ ├── Makefile │ ├── ipp.h │ ├── print.c │ ├── print.h │ ├── printd.c │ └── util.c ├── proc │ ├── Makefile │ ├── awkexample │ ├── echoall.c │ ├── exec1.c │ ├── exec2.c │ ├── fork1.c │ ├── fork2.c │ ├── nice.c │ ├── pracct.c │ ├── pruids.c │ ├── system.c │ ├── systest1.c │ ├── systest3.c │ ├── tellwait1.c │ ├── tellwait2.c │ ├── test1.c │ ├── times1.c │ ├── vfork1.c │ └── wait1.c ├── pty │ ├── Makefile │ ├── driver.c │ ├── loop.c │ └── main.c ├── relation │ ├── Makefile │ └── orphan3.c ├── signals │ ├── Makefile │ ├── abort.c │ ├── child.c │ ├── critical.c │ ├── mask.c │ ├── read1.c │ ├── read2.c │ ├── reenter.c │ ├── setops.c │ ├── sigtstp.c │ ├── sigusr.c │ ├── sleep1.c │ ├── sleep2.c │ ├── suspend1.c │ ├── suspend2.c │ ├── system.c │ ├── systest2.c │ └── tsleep2.c ├── sockets │ ├── clconn.c │ ├── clconn2.c │ ├── findsvc.c │ ├── initsrv1.c │ ├── initsrv2.c │ ├── makefile │ ├── ruptime-dg.c │ ├── ruptime.c │ ├── ruptimed-dg.c │ ├── ruptimed-fd.c │ └── ruptimed.c ├── standards │ ├── Makefile │ ├── conf.c.modified │ ├── makeconf.awk │ ├── makeopt.awk │ ├── pathconf-lim.sym │ ├── pathconf-opt.sym │ ├── sysconf-lim.sym │ └── sysconf-opt.sym ├── stdio │ ├── Makefile │ ├── buf.c │ ├── fgetsfputs.c │ ├── getcharbug.c │ ├── getcputc.c │ ├── memstr.c │ ├── mkstemp.c │ └── tempfiles.c ├── systype.sh ├── termios │ ├── Makefile │ ├── csize.c │ ├── ctermid.c │ ├── getpass.c │ ├── isatty.c │ ├── settty.c │ ├── t_getpass.c │ ├── t_isatty.c │ ├── t_raw.c │ ├── t_ttyname.c │ ├── ttyname.c │ └── winch.c ├── threadctl │ ├── Makefile │ ├── atfork.c │ ├── detach.c │ ├── getenv1.c │ ├── getenv2.c │ ├── getenv3.c │ ├── suspend.c │ └── timeout.c └── threads │ ├── Makefile │ ├── badexit2.c │ ├── barrier.c │ ├── cleanup.c │ ├── condvar.c │ ├── exitstatus.c │ ├── maketimeout.c │ ├── mutex1.c │ ├── mutex2.c │ ├── mutex3.c │ ├── rwlock.c │ ├── threadid.c │ └── timedlock.c ├── apue.h ├── epolldir ├── Client ├── Client1 ├── Sever ├── client.cpp ├── epollClient.cpp ├── epollHeader.h └── epollSever.cpp ├── error.c └── fork.c /05UnixunitStdIo/01MyEffectIo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/01MyEffectIo -------------------------------------------------------------------------------- /05UnixunitStdIo/01MyEffectIo.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(void) 3 | { 4 | int c; 5 | while((c=getc(stdin))!=EOF) 6 | { 7 | if(putc(c,stdout)==EOF) 8 | err_sys("output error"); 9 | } 10 | if(ferror(stdin)) 11 | err_sys("input error"); 12 | exit(0); 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /05UnixunitStdIo/02MyeffectioRdWr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/02MyeffectioRdWr -------------------------------------------------------------------------------- /05UnixunitStdIo/02MyeffectioRdWr.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(void) 3 | { 4 | char buf[MAXLINE]; 5 | while(fgets(buf,MAXLINE,stdin)!=NULL) 6 | if(fputs(buf,stdout)==EOF) 7 | err_sys("output error"); 8 | if(ferror(stdin)) 9 | err_sys("input error"); 10 | exit(0); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /05UnixunitStdIo/03MyIoStdio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/03MyIoStdio -------------------------------------------------------------------------------- /05UnixunitStdIo/04Mytempfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/04Mytempfile -------------------------------------------------------------------------------- /05UnixunitStdIo/04Mytempfile.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(void) 3 | { 4 | char name[L_tmpnam],line[MAXLINE]; 5 | FILE*fp; 6 | printf("%s \n",tmpnam(NULL)); 7 | tmpnam(name); 8 | printf("%s\n",name); 9 | if((fp=tmpfile())==NULL) 10 | err_sys("tmpfile error"); 11 | fputs("one line of output \n",fp); 12 | rewind(fp); 13 | if(fgets(line,sizeof(line),fp)==NULL) 14 | err_sys("fgets error"); 15 | fputs(line,stdout);// print the line we wrote 16 | exit(0); 17 | } 18 | //每次产生不同的临时文件,但是临时文件对我们来说是不可兼得的,所以说是不安全的,对于ISO 从规范来说 ,建议使用它更安全的mkstemp函数。 19 | -------------------------------------------------------------------------------- /05UnixunitStdIo/05Mymsktemp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/05Mymsktemp -------------------------------------------------------------------------------- /05UnixunitStdIo/05Mymsktemp.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | void make_temp(char *); 4 | int main(void) 5 | { 6 | char good_template[]="/tmp/dirXXXXXX"; 7 | char *bad_template="/tmp/dirXXXXXX"; 8 | printf("trying to create first temp file...\n"); 9 | make_temp(good_template); 10 | printf("trying to create second temp file...\n"); 11 | make_temp(bad_template); 12 | exit(0); 13 | } 14 | 15 | void make_temp(char * template) 16 | { 17 | int fd; 18 | struct stat sbuf; 19 | if((fd=mkstemp(template))<0) 20 | err_sys("can't create tempfile"); 21 | printf("temp name =%s\n",template); 22 | close(fd); 23 | if((stat(template,&sbuf))<0) 24 | { 25 | if(errno==ENOENT) 26 | printf("file doesn't exist !\n"); 27 | else 28 | err_sys("stat failed"); 29 | 30 | } 31 | else 32 | { 33 | printf("file exist \n"); 34 | unlink(template); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /05UnixunitStdIo/065-14Memorystream: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/05UnixunitStdIo/065-14Memorystream -------------------------------------------------------------------------------- /05UnixunitStdIo/std.err: -------------------------------------------------------------------------------- 1 | one line to standard error 2 | -------------------------------------------------------------------------------- /05UnixunitStdIo/std.out: -------------------------------------------------------------------------------- 1 | enter any character : 2 | stream =stdin ,fully buffered, buffered size =4096 3 | stream =stdout ,fully buffered, buffered size =4096 4 | stream =stderr ,unbuffered, buffered size =1 5 | stream =/etc/passwd ,fully buffered, buffered size =4096 6 | -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/016-2Mygetpwnam.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct passwd*getpwnam(const char * name) 5 | { 6 | struct passwd *ptr; 7 | setpwent(); 8 | while((ptr=getpwent())!=NULL) 9 | if(strcmp(name,ptr->pw_name)==0) 10 | break; 11 | endpwent(); 12 | return (0); 13 | 14 | 15 | 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/02Mygettime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/06UnixunitsystemdateInfo/02Mygettime -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/02Mygettime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(void) 5 | { 6 | time_t t; 7 | struct tm* mytime; 8 | char buf[16]; 9 | char buff[64]; 10 | time(&t); 11 | mytime=localtime(&t); 12 | if(strftime(buf,16,"time and date : %r ,%a %b %d, %Y",mytime)==0) 13 | printf("bufer length is too small\n"); 14 | else 15 | printf("%s\n", buf); 16 | if(strftime(buff,64,"time and date : %r ,%a %b %d, %Y",mytime)==0) 17 | printf("bufer length is too small\n"); 18 | else 19 | printf("%s\n",buff); 20 | exit(0); 21 | } 22 | -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/03Myuname: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/06UnixunitsystemdateInfo/03Myuname -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/03Myuname.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | #include 4 | int main(void) 5 | { 6 | int i; 7 | struct utsname myuname; 8 | if((i=uname(&myuname))<0) 9 | err_sys("uname error"); 10 | else 11 | printf("%s\t%s\t%s\t%s\t%s",myuname.sysname,myuname.nodename,myuname.version,myuname.release,myuname.machine); 12 | 13 | 14 | 15 | // char buf[64]; 16 | // if((gethostname(&buf,strlen(buf))<0) 17 | // err_sys("gethostname error"); 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | exit(0); 30 | } 31 | -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/06.2Myshadow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/06UnixunitsystemdateInfo/06.2Myshadow -------------------------------------------------------------------------------- /06UnixunitsystemdateInfo/06.2Myshadow.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(void) 4 | { 5 | struct spwd *ptr; 6 | if((ptr=getspnam("sar"))==NULL) 7 | err_sys("getspnam error"); 8 | printf("sp_ pwdp=%s\n",ptr->sp_pwdp==NULL||ptr->sp_pwdp[0]==0? "(null)" : ptr->sp_pwdp); 9 | 10 | 11 | 12 | exit(0); 13 | } 14 | -------------------------------------------------------------------------------- /07UnixunitForkEnv/01Myhelloworld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/01Myhelloworld -------------------------------------------------------------------------------- /07UnixunitForkEnv/01Myhelloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | int f1(int val) 3 | { 4 | int num=0; 5 | int *ptr=# 6 | if(val==0) 7 | { 8 | int val=5; 9 | ptr=&val; 10 | } 11 | return (*ptr+1); 12 | } 13 | int main(void) 14 | { 15 | printf("hello world !\n"); 16 | printf("%d",f1(1)); 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /07UnixunitForkEnv/02Myexit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/02Myexit -------------------------------------------------------------------------------- /07UnixunitForkEnv/02Myexit.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | static void my_exit1(void); 3 | static void my_exit2(void); 4 | 5 | 6 | int main (void) 7 | { 8 | if(atexit(my_exit2)!=0) 9 | err_sys("can't register my_exit2)"); 10 | if(atexit(my_exit1)!=0) 11 | err_sys("can't register my_exit1"); 12 | if(atexit(my_exit1)!=0) 13 | err_sys("can't register my_exit1"); 14 | printf("main is done \n"); 15 | return 0; 16 | 17 | } 18 | 19 | static void my_exit1(void) 20 | { 21 | printf("first exit hander\n"); 22 | 23 | } 24 | static void my_exit2(void) 25 | { 26 | printf("second exit hander\n"); 27 | 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /07UnixunitForkEnv/03Myecho: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/03Myecho -------------------------------------------------------------------------------- /07UnixunitForkEnv/03Myecho.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(int argc,char **argv,char ** envp) 3 | { 4 | int i; 5 | for(i=0;i0) 24 | { 25 | switch(cmd) 26 | { 27 | case TOK_ADD: 28 | cmd_add(); 29 | break; 30 | } 31 | 32 | } 33 | 34 | 35 | } 36 | 37 | 38 | void cmd_add(void) 39 | { 40 | int token; 41 | token=get_token(); 42 | //reset the processing for this command 43 | 44 | } 45 | int get_token(void) 46 | { 47 | //fetch next token from line pointed to by tok_ptr 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /07UnixunitForkEnv/057-11externMyjmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/057-11externMyjmp -------------------------------------------------------------------------------- /07UnixunitForkEnv/067-13Mytestjmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/067-13Mytestjmp -------------------------------------------------------------------------------- /07UnixunitForkEnv/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/07UnixunitForkEnv/a.out -------------------------------------------------------------------------------- /08ForkContral/01.out: -------------------------------------------------------------------------------- 1 | a write t ostdout 2 | before fork 3 | pid=20833 glob=7 val =89 4 | before fork 5 | pid=20834 glob=7 val =89 6 | -------------------------------------------------------------------------------- /08ForkContral/018-1MyforkGetid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/018-1MyforkGetid -------------------------------------------------------------------------------- /08ForkContral/018-1MyforkGetid.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int globval=6; 3 | char buf[]="a write t ostdout\n"; 4 | int main(int argc,char **argv) 5 | { 6 | int var; 7 | pid_t pid; 8 | var =88; 9 | if(write(STDOUT_FILENO,buf,sizeof(buf)-1)!=sizeof(buf)-1) 10 | err_sys("write error"); 11 | printf("before fork\n"); 12 | if(pid=fork()<0) 13 | { 14 | err_sys("fork error"); 15 | } 16 | else if(pid==0) 17 | { 18 | globval++; 19 | var++; 20 | } 21 | else 22 | { 23 | sleep(2); 24 | } 25 | 26 | printf("pid=%ld \t glob=%d \t val =%d\n ",(long)getpid(),globval,var); 27 | 28 | 29 | exit(0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /08ForkContral/028-3Myvfork: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/028-3Myvfork -------------------------------------------------------------------------------- /08ForkContral/028-3Myvfork.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int globval =6; 3 | 4 | int main(int argc,char ** argv) 5 | { 6 | int val; 7 | pid_t pid ; 8 | val=88; 9 | printf("before vfork\n"); 10 | if((pid=vfork())<0) 11 | { 12 | err_sys("vfork error"); 13 | } 14 | else if(pid==0) 15 | { 16 | globval++; 17 | val++; 18 | _exit(0); 19 | } 20 | printf("pid=%ld,glob=%d,val=%d\n",(long)getpid(),globval,val); 21 | exit(0); 22 | 23 | 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /08ForkContral/03-8_6Mypr_exitstatus: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/03-8_6Mypr_exitstatus -------------------------------------------------------------------------------- /08ForkContral/03-8_6Mypr_exitstatus.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(void) 4 | { 5 | pid_t pid; 6 | int status; 7 | if((pid=fork())<0) 8 | err_sys("fork error"); 9 | else if(pid==0)//chile 10 | exit(7); 11 | 12 | 13 | if(wait(&status)!=pid) 14 | err_sys("wait error"); 15 | pr_exit(status); 16 | 17 | if((pid=fork())<0) 18 | err_sys("fork error"); 19 | else if(pid==0) 20 | abort(); 21 | 22 | if(wait(&status)!=pid) 23 | err_sys("wait error"); 24 | pr_exit(status); 25 | 26 | if((pid=fork())<0) 27 | err_sys("fork error"); 28 | else if(pid==0) 29 | status /=0; 30 | if(wait(&status)!=pid) 31 | err_sys("wait error"); 32 | pr_exit(status); 33 | 34 | 35 | exit(0); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /08ForkContral/04-8_8MysolveZombieFork: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/04-8_8MysolveZombieFork -------------------------------------------------------------------------------- /08ForkContral/04-8_8MysolveZombieFork.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | //通过调用fork()函数两次来解决僵尸进程的出现 3 | int main(void) 4 | { 5 | pid_t pid; 6 | if((pid=fork())<0) 7 | err_sys("fork error"); 8 | else if(pid==0) 9 | { 10 | if((pid=fork())<0) 11 | err_sys("fork error"); 12 | else if(pid>0) 13 | exit(0); 14 | sleep(2); 15 | printf("second child ,parent pid =%ld\n, self pid=%ld\n",(long )getppid(),(long)getpid()); 16 | exit(0); 17 | } 18 | if(waitpid(pid ,NULL,0)!=pid) 19 | err_sys("waitpid error"); 20 | //we're the parent we continue executing ,knowing that we are not the parent of the second child 21 | 22 | 23 | 24 | exit(0); 25 | } 26 | -------------------------------------------------------------------------------- /08ForkContral/05-8-12MyIpc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/05-8-12MyIpc -------------------------------------------------------------------------------- /08ForkContral/05-8-12MyIpc.c: -------------------------------------------------------------------------------- 1 | //To solve the race condition 解决竞争关系和轮询的情况 2 | //在unix中有专门的IPC通信或者管道符操作。 3 | #include"apue.h" 4 | static void charactatime(char *); 5 | int main(void) 6 | { 7 | pid_t pid ; 8 | if((pid= fork())<0) 9 | err_sys("fork error"); 10 | else if(pid==0) 11 | charactatime("output from child\n"); 12 | else 13 | charactatime("output from parent\n"); 14 | 15 | 16 | exit(0); 17 | } 18 | static void charactatime(char*str) 19 | { 20 | char *ptr; 21 | int c; 22 | setbuf(stdout,NULL); 23 | for(ptr=str;(c=*ptr++)!=0;) 24 | putc(c,stdout); 25 | } 26 | //竞争策略对于完善的linux系统来说已经完善的很好了,对于一些宏,或者函数也不再需要 27 | -------------------------------------------------------------------------------- /08ForkContral/06-8_13MyextendIPC: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/06-8_13MyextendIPC -------------------------------------------------------------------------------- /08ForkContral/06-8_13MyextendIPC.c: -------------------------------------------------------------------------------- 1 | //To solve the race condition 解决竞争关系和轮询的情况 2 | //在unix中有专门的IPC通信或者管道符操作。 3 | #include"apue.h" 4 | static void charactatime(char *); 5 | int main(void) 6 | { 7 | pid_t pid ; 8 | TELL_WAIT(); 9 | if((pid= fork())<0) 10 | err_sys("fork error"); 11 | else if(pid==0) 12 | { 13 | charactatime("output from child\n"); 14 | WAIT_PARENT(); 15 | } 16 | else 17 | { 18 | charactatime("output from parent\n"); 19 | TELL_CHILD(pid); 20 | } 21 | 22 | exit(0); 23 | } 24 | static void charactatime(char*str) 25 | { 26 | char *ptr; 27 | int c; 28 | setbuf(stdout,NULL); 29 | for(ptr=str;(c=*ptr++)!=0;) 30 | putc(c,stdout); 31 | } 32 | -------------------------------------------------------------------------------- /08ForkContral/08-8_16Myexec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/08-8_16Myexec -------------------------------------------------------------------------------- /08ForkContral/08-8_16Myexec.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | char * env_init[]={"USER=unknown","PATH=/tmp",NULL}; 4 | int main(int argc,char **argv) 5 | { 6 | pid_t pid; 7 | if((pid=fork())<0) 8 | { 9 | err_sys("fork error"); 10 | } 11 | else if(pid==0) 12 | { 13 | if(execle("~/linuxshell/ceshi.sh",(char *)0,env_init)<0) 14 | err_sys("execle error"); 15 | 16 | } 17 | if(waitpid(pid,NULL,0)<0) 18 | err_sys("wait error"); 19 | if((pid=fork())<0) 20 | { 21 | err_sys("fork error"); 22 | } 23 | else if (pid ==0 ) 24 | { 25 | if(execlp("~/linuxshell/ceshi.sh","only 1 arg",(char*)0)<0) 26 | err_sys("execlp error"); 27 | } 28 | exit(0); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /08ForkContral/09-8-17Myechoall: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/09-8-17Myechoall -------------------------------------------------------------------------------- /08ForkContral/09-8-17Myechoall.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(int argc,char **argv,char ** env) 3 | { 4 | int i; 5 | char **ptr; 6 | for(i=0;i 2 | #include 3 | #include 4 | int mysystem(const char *cmdstring) 5 | { 6 | pid_t pid; 7 | int status; 8 | 9 | if((pid=fork())<0) 10 | { 11 | status =-1; 12 | 13 | } 14 | else if(pid==0) 15 | { 16 | execl("/bin/sh","sh","-c",cmdstring,(char *)0); 17 | _exit(127); 18 | } 19 | 20 | else 21 | { 22 | while(waitpid(pid,&status,0)<0) 23 | { 24 | status=-1; 25 | break; 26 | } 27 | } 28 | 29 | return (status); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /08ForkContral/12-8_23MytestSystem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/12-8_23MytestSystem -------------------------------------------------------------------------------- /08ForkContral/12-8_23MytestSystem.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(int argc,char ** argv) 4 | { 5 | int status; 6 | if((status=system("date"))<0) 7 | err_sys("system() error"); 8 | pr_exit(status); 9 | if((status=system("nosuchcommand"))<0) 10 | err_sys("system() error"); 11 | pr_exit(status); 12 | if((status=system("who ;exit 44"))<0) 13 | err_sys("system() error"); 14 | pr_exit(status); 15 | exit(0); 16 | 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /08ForkContral/13-8-24MycommandSystem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/13-8-24MycommandSystem -------------------------------------------------------------------------------- /08ForkContral/13-8-24MycommandSystem.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(int argc,char **argv) 3 | { 4 | int status; 5 | if(argc<2) 6 | err_quit("command-line argument required"); 7 | if((status=system(argv[1]))<0) 8 | err_sys("system() error"); 9 | pr_exit(status); 10 | exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /08ForkContral/14-08_28MyProcessAccount: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/14-08_28MyProcessAccount -------------------------------------------------------------------------------- /08ForkContral/15-08_29MyTestProcessAccount: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/15-08_29MyTestProcessAccount -------------------------------------------------------------------------------- /08ForkContral/16-08_30Mynice: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/16-08_30Mynice -------------------------------------------------------------------------------- /08ForkContral/17-08_31MyTmsShell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/17-08_31MyTmsShell -------------------------------------------------------------------------------- /08ForkContral/18-MyZombieofork: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/08ForkContral/18-MyZombieofork -------------------------------------------------------------------------------- /08ForkContral/18-MyZombieofork.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #define PSCMD "ps -o pid,ppid,state,tty,command" 3 | int main(void) 4 | { 5 | pid_t pid; 6 | if((pid=fork())<0) 7 | err_sys("fork error"); 8 | else if(pid==0) 9 | exit(0); 10 | //父进程 11 | sleep(4); 12 | system(PSCMD); 13 | exit(0); 14 | } 15 | -------------------------------------------------------------------------------- /09Forkresulation/01-09_12MyOrphanProcess: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/09Forkresulation/01-09_12MyOrphanProcess -------------------------------------------------------------------------------- /09Forkresulation/01-09_12MyOrphanProcess.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | static void sig_ids(int signo) 4 | { 5 | printf("SIGHUP received,pid=%ld\n",(long)getpid()); 6 | } 7 | static void pr_ids(char *name) 8 | { 9 | //分别获取进程组id父进程id,进程组id前台进程组id 10 | printf("%s :pid =%ld,ppid=%ld,pgid=%ld,tpgrp=%ld\n",name,(long)getpid(),(long)getppid(),(long)getpgrp(),(long)tcgetpgrp(STDIN_FILENO)); 11 | fflush(stdout);//刷新缓冲区 12 | } 13 | 14 | int main (void) 15 | { 16 | char c; 17 | pid_t pid; 18 | pr_ids("parent"); 19 | if((pid=fork())<0) 20 | err_sys("fork error"); 21 | else if(pid>0) 22 | { 23 | sleep(5); 24 | } 25 | else 26 | { 27 | pr_ids("child"); 28 | signal(SIGHUP,sig_ids); 29 | kill(getpid(),SIGTSTP); 30 | pr_ids("child"); 31 | if(read(STDIN_FILENO,&c,1)!=1) 32 | printf("read error %d on contralling TTY\n",errno); 33 | } 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /09Forkresulation/9.1practice.md: -------------------------------------------------------------------------------- 1 | ###?考虑6.8节中说明的utmp和wtmp文件,为什么logout记录是由init进程写入的?对于网络登录的原理是否相同? 2 | 3 | ``` 4 | 这个是这样的,因为init是登录shell的父进程,当登录shell终止时他收到SIGCHLD信号量,所以init进程知道什么时候 5 | 终端用户注销。 6 | 网络登录没有包含init在utmp文件和wtmp文件的登录项和相应的注销项是由一个处理登录并检测注销的进程写的。一般是 initd、telnetd等守护进程 7 | ``` 8 | -------------------------------------------------------------------------------- /09Forkresulation/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/09Forkresulation/a.out -------------------------------------------------------------------------------- /09Forkresulation/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/09Forkresulation/main.c -------------------------------------------------------------------------------- /09Forkresulation/makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/09Forkresulation/makefile -------------------------------------------------------------------------------- /09Forkresulation/temp.foo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/09Forkresulation/temp.foo -------------------------------------------------------------------------------- /10Signal/01MyexternSignal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/01MyexternSignal -------------------------------------------------------------------------------- /10Signal/02-1MyTrafficcontral: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/02-1MyTrafficcontral -------------------------------------------------------------------------------- /10Signal/03-10_2Mysignal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/03-10_2Mysignal -------------------------------------------------------------------------------- /10Signal/10-10_23MySigsuspendWait: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/10-10_23MySigsuspendWait -------------------------------------------------------------------------------- /10Signal/13-10_26MysystemEd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/13-10_26MysystemEd -------------------------------------------------------------------------------- /10Signal/13-10_26MysystemEd.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13-10_26MysystemEd.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Fri 22 Jan 2016 05:05:13 PM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | static void sig_int(int signo ) 10 | { 11 | printf("caugth SIGINT \n"); 12 | } 13 | static void sig_chld(int signo ) 14 | { 15 | printf("caugth SIGCHLD\n"); 16 | } 17 | int main(void) 18 | { 19 | if(signal(SIGINT,sig_int)==SIG_ERR) 20 | err_sys("signal sigint error"); 21 | if(signal(SIGCHLD,sig_chld)==SIG_ERR) 22 | err_sys("signal sigchld error"); 23 | 24 | if(system("/bin/ed")<0) 25 | err_sys("system() error"); 26 | exit (0); 27 | } 28 | -------------------------------------------------------------------------------- /10Signal/14-10_28MySystem.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13-10_26MysystemEd.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Fri 22 Jan 2016 05:05:13 PM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | static void sig_int(int signo ) 10 | { 11 | printf("caugth SIGINT \n"); 12 | } 13 | static void sig_chld(int signo ) 14 | { 15 | printf("caugth SIGCHLD\n"); 16 | } 17 | int main(void) 18 | { 19 | if(signal(SIGINT,sig_int)==SIG_ERR) 20 | err_sys("signal sigint error"); 21 | if(signal(SIGCHLD,sig_chld)==SIG_ERR) 22 | err_sys("signal sigchld error"); 23 | 24 | if(system("/bin/ed")<0) 25 | err_sys("system() error"); 26 | exit (0); 27 | } 28 | -------------------------------------------------------------------------------- /10Signal/15-10_31MySignalContral: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/15-10_31MySignalContral -------------------------------------------------------------------------------- /10Signal/2-10_5MySIGALARMsignal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/2-10_5MySIGALARMsignal -------------------------------------------------------------------------------- /10Signal/2-10_5MySIGALARMsignal.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | 4 | static void my_alarm(int signo) 5 | { 6 | struct passwd * rootptr; 7 | printf("int the signal function \n"); 8 | if((rootptr=getpwnam("root"))==NULL) 9 | err_sys("getpwnam(root) error\n"); 10 | alarm(1); 11 | } 12 | 13 | 14 | int main(void) 15 | { 16 | struct passwd *ptr; 17 | signal(SIGALRM,my_alarm); 18 | alarm(1); 19 | for(;;) 20 | { 21 | if((ptr=getpwnam("victor"))==NULL) 22 | err_sys("getpwnam(victor) error\n"); 23 | if(strcmp(ptr->pw_name,"victor")!=0) 24 | printf("return value corrupted!,pw_name=%s\n",ptr->pw_name); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /10Signal/3-10MySIGCHLD: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/3-10MySIGCHLD -------------------------------------------------------------------------------- /10Signal/3-10_6MySIGCLD: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/3-10_6MySIGCLD -------------------------------------------------------------------------------- /10Signal/4-10.7.8.9MySIGALRM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/4-10.7.8.9MySIGALRM -------------------------------------------------------------------------------- /10Signal/5-10_10Myalarm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/5-10_10Myalarm -------------------------------------------------------------------------------- /10Signal/5-10_10Myalarm.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5-10_10Myalarm.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Thu 21 Jan 2016 04:45:07 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include"apue.h" 10 | static void sig_alrm(int signo) 11 | { 12 | // longjmp(env_alrm,1); 13 | } 14 | int main(void) 15 | { 16 | int n; 17 | char line[MAXLINE]; 18 | if(signal(SIGALRM,sig_alrm)==SIG_ERR) 19 | err_sys("signal error"); 20 | alarm(10); 21 | if((n=read(STDIN_FILENO,line,MAXLINE))<0) 22 | err_sys("read error"); 23 | alarm(0); 24 | write(STDOUT_FILENO,line,n); 25 | exit(0); 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /10Signal/6-10MyalarmLongjmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/6-10MyalarmLongjmp -------------------------------------------------------------------------------- /10Signal/7-10.11MySignalSet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/7-10.11MySignalSet -------------------------------------------------------------------------------- /10Signal/8-10_20.19.18MySetjmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/8-10_20.19.18MySetjmp -------------------------------------------------------------------------------- /10Signal/9-10-22MySigsuspend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/9-10-22MySigsuspend -------------------------------------------------------------------------------- /10Signal/changeSystem.md: -------------------------------------------------------------------------------- 1 | ##告诉自己从这里开始更改了系统盘为centos,不在使用unbutu 2 | - 原因是以内unbutu内核有点奇怪,在做信号这章的实验时出现的问题,让人很莫名其妙,在centos,redhat fedroa debin系列的linux系统下运行良好,所以才去centos系统 3 | -------------------------------------------------------------------------------- /10Signal/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/10Signal/log -------------------------------------------------------------------------------- /10Signal/temp.foo: -------------------------------------------------------------------------------- 1 | hello,world,right 2 | -------------------------------------------------------------------------------- /11UnixUnitThread/01-11_2MyThreadCreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/01-11_2MyThreadCreate -------------------------------------------------------------------------------- /11UnixUnitThread/11-11_16MyTasksPthreadSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/11-11_16MyTasksPthreadSort -------------------------------------------------------------------------------- /11UnixUnitThread/2-11_3MyThreadExit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/2-11_3MyThreadExit -------------------------------------------------------------------------------- /11UnixUnitThread/3-11_4MyPthread_exitwrong: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/3-11_4MyPthread_exitwrong -------------------------------------------------------------------------------- /11UnixUnitThread/4-11_5MyPthreadCleanProgram: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/4-11_5MyPthreadCleanProgram -------------------------------------------------------------------------------- /11UnixUnitThread/8-11_13_Mypthread_mutex_timedlock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/11UnixUnitThread/8-11_13_Mypthread_mutex_timedlock -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/.02-12_4MySpecStatusCreatePthread.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/.02-12_4MySpecStatusCreatePthread.c.swo -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/.02-12_4MySpecStatusCreatePthread.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/.02-12_4MySpecStatusCreatePthread.c.swp -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/01MyThreadSide: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/01MyThreadSide -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/02-12_4MySpecStatusCreatePthread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/02-12_4MySpecStatusCreatePthread -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/06-2_16MymutexSignalPthread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/06-2_16MymutexSignalPthread -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/07-12_17MyPthread_atfork: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/12UnixUnitThreadAttr/07-12_17MyPthread_atfork -------------------------------------------------------------------------------- /12UnixUnitThreadAttr/7-12_17.txt: -------------------------------------------------------------------------------- 1 | thread started.... 2 | parent about t ofork ... 3 | preparing locks .. 4 | parent unlocking locks .. 5 | parent return from fork 6 | thread started.... 7 | parent about t ofork ... 8 | preparing locks .. 9 | child unlocking locks .. 10 | child return from fork 11 | -------------------------------------------------------------------------------- /13UnixUnitDaemonFork/01-13_1MyinitDaemonFork: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/13UnixUnitDaemonFork/01-13_1MyinitDaemonFork -------------------------------------------------------------------------------- /13UnixUnitDaemonFork/03-13_7MyDaemonSecond: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/13UnixUnitDaemonFork/03-13_7MyDaemonSecond -------------------------------------------------------------------------------- /13UnixUnitDaemonFork/04-13_8MyPthreadGetSignalConf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/13UnixUnitDaemonFork/04-13_8MyPthreadGetSignalConf -------------------------------------------------------------------------------- /13UnixUnitDaemonFork/05-13_9Mysetckoexec.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 05-13_9Mysetckoexec.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Mon 22 Feb 2016 01:53:37 AM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | #include 10 | int set_cloexec(int fd) 11 | { 12 | int val; 13 | if((val=fcntl(fd,F_GETFD,0))<0) 14 | return -1; 15 | val |=FD_CLOEXEC; 16 | return(fcntl(fd,F_SETFD,val)); 17 | } 18 | -------------------------------------------------------------------------------- /13UnixUnitDaemonFork/readme.md: -------------------------------------------------------------------------------- 1 | ##对于守护进程 2 | - 其作用就是为系统服务,做守护,单方面讲就是让其成为系统进程,只要系统运行,服务无就会一直开启 3 | 因为守护进程是一直在目录底下运行的 , 4 | - 其要成为守护进程,必须成为孤儿进程,但是呢,又不会被杀死,工作目录切换到/ 下,去掉终端显示。成为孤傲的守护进程 5 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/01-14_1MyNoBlockIo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/01-14_1MyNoBlockIo -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/01stderr.file: -------------------------------------------------------------------------------- 1 | read 50000 bytes 2 | nwrite=50000,errno=0 3 | fcntl F_SETFL error: Invalid argument 4 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/04-14_7MyDiedLock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/04-14_7MyDiedLock -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/05-14_9Myloocfile.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 05-14_9Myloocfile.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Sat 30 Jan 2016 04:04:22 PM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | //守护进程可以通过该函书在文件上加锁 10 | //一种简便的方法定义lookfile就是通过write_lock函数定义 11 | //#define lockfile(fd) write_lock ((fd),0,SEEK_SET,0) 12 | 13 | 14 | int lookfile(int fd) 15 | { 16 | struct flock fl; 17 | fl.l_type=F_WRLCK; 18 | fl.l_start=0; 19 | fl.l_whence=SEEK_SET; 20 | fl.l_len=0; 21 | return (fctnl(fd,F_SETCK,&fl)); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/06-.lock: -------------------------------------------------------------------------------- 1 | abcdef -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/06-14_12MyConfirmLockSystem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/06-14_12MyConfirmLockSystem -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/07-14_20MyTransFile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/07-14_20MyTransFile -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/07_testone.md: -------------------------------------------------------------------------------- 1 | . 2 | .. 3 | 01-14_1MyNoBlockIo 4 | 01-14_1MyNoBlockIo.c 5 | 01stderr.file 6 | 01temp.file 7 | 0214_5MyAccReleaseLock.c 8 | 04-14_7MyDiedLock 9 | 04-14_7MyDiedLock.c 10 | 05-14_9Myloocfile.c 11 | 06-14_12MyConfirmLockSystem 12 | 06-14_12MyConfirmLockSystem.c 13 | 06-.lock 14 | 07-14_20MyTransFile 15 | 07-14_20MyTransFile.c 16 | 07_testone.md 17 | 07_testtwo.md 18 | templock 19 | _testone.md 20 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/07_testtwo.md: -------------------------------------------------------------------------------- 1 | . 2 | .. 3 | 01-14_1ZlAbOybpxVb 4 | 01-14_1ZlAbOybpxVb.p 5 | 01fgqree.svyr 6 | 01grzc.svyr 7 | 0214_5ZlNppEryrnfrYbpx.p 8 | 04-14_7ZlQvrqYbpx 9 | 04-14_7ZlQvrqYbpx.p 10 | 05-14_9Zlybbpsvyr.p 11 | 06-14_12ZlPbasvezYbpxFlfgrz 12 | 06-14_12ZlPbasvezYbpxFlfgrz.p 13 | 06-.ybpx 14 | 07-14_20ZlGenafSvyr 15 | 07-14_20ZlGenafSvyr.p 16 | 07_grfgbar.zq 17 | 07_grfggjb.zq 18 | grzcybpx 19 | _grfgbar.zq 20 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/08-14_21MyAsynchronousIO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/08-14_21MyAsynchronousIO -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/10-14_27MyMemory-mappedIO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/10-14_27MyMemory-mappedIO -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/14_Homework01: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/14_Homework01 -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/_testone.md: -------------------------------------------------------------------------------- 1 | . 2 | .. 3 | 01-14_1MyNoBlockIo 4 | 01-14_1MyNoBlockIo.c 5 | 01stderr.file 6 | 01temp.file 7 | 0214_5MyAccReleaseLock.c 8 | 04-14_7MyDiedLock 9 | 04-14_7MyDiedLock.c 10 | 05-14_9Myloocfile.c 11 | 06-14_12MyConfirmLockSystem 12 | 06-14_12MyConfirmLockSystem.c 13 | 06-.lock 14 | 07-14_20MyTransFile 15 | 07-14_20MyTransFile.c 16 | 07_testtwo.md 17 | templock 18 | _testone.md 19 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/lockfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/14UnixUnitAdvancedIO/lockfile -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/readme.md: -------------------------------------------------------------------------------- 1 | ##高级IO 2 | - 本章含有众多概念和函数,我们把它们都统统归类为高级IO。 3 | - 其中包括非阻塞IO、记录锁、IO多路转换就是我们常说的IO多路复用技术的基础 4 | - select和poll函数、异步IO、readv和writev函数以及存储映射IO(mmap). 5 | - 在后续15章和第17章的进程间通信以及以后各章节的实例都要使用本章所描述的概念和函数 6 | 7 | 8 | # 出自APUE第十四章高级IO部分 9 | `let's coding now!!` 10 | -------------------------------------------------------------------------------- /14UnixUnitAdvancedIO/templock: -------------------------------------------------------------------------------- 1 | ab -------------------------------------------------------------------------------- /15UnixUnitIPC/01-15_5MyParent2ChildForkIPC: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/01-15_5MyParent2ChildForkIPC -------------------------------------------------------------------------------- /15UnixUnitIPC/01-15_5MyParent2ChildForkIPC.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 01-15_5MyParent2ChildForkIPC.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Wed 17 Feb 2016 11:15:26 AM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | int main() 10 | { 11 | int n; 12 | int fd[2]; 13 | pid_t pid; 14 | char line[MAXLINE]; 15 | 16 | if(pipe(fd)<0) 17 | err_sys("pipe error"); 18 | if((pid=fork())<0) 19 | err_sys("fork error"); 20 | else if(pid==0) 21 | { 22 | close(fd[1]); 23 | n=read(fd[0],line,MAXLINE); 24 | write(STDOUT_FILENO,line,n); 25 | } 26 | else 27 | { 28 | close(fd[0]); 29 | write(fd[1],"hello world \n",12); 30 | } 31 | exit(0); 32 | } 33 | -------------------------------------------------------------------------------- /15UnixUnitIPC/02-15_6MyPipeDeparPage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/02-15_6MyPipeDeparPage -------------------------------------------------------------------------------- /15UnixUnitIPC/04-15_6MyPopenOveriedCp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/04-15_6MyPopenOveriedCp -------------------------------------------------------------------------------- /15UnixUnitIPC/06-15_14MySimpleCp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/06-15_14MySimpleCp -------------------------------------------------------------------------------- /15UnixUnitIPC/06-15_14MySimpleCp.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 06-15_14MySimpleCp.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Thu 18 Feb 2016 11:48:30 PM CST 6 | ************************************************************************/ 7 | 8 | #include "apue.h" 9 | #include 10 | int main(void) 11 | { 12 | int c; 13 | while((c=getchar())!=EOF) 14 | { 15 | if(isupper(c)) 16 | c=tolower(c); 17 | if(putchar(c)==EOF) 18 | err_sys("output error"); 19 | if(c=='\n') 20 | fflush(stdout);//对于标准输出来讲通常是行缓冲的,而提示并不包含换行符,需要调用fflush 21 | } 22 | exit(0); 23 | } 24 | -------------------------------------------------------------------------------- /15UnixUnitIPC/07-15_15MyExternPopenGetFileToExec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/07-15_15MyExternPopenGetFileToExec -------------------------------------------------------------------------------- /15UnixUnitIPC/08-15_17MyCoprocess: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/08-15_17MyCoprocess -------------------------------------------------------------------------------- /15UnixUnitIPC/09-15_18MyCoprocessApplication: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/09-15_18MyCoprocessApplication -------------------------------------------------------------------------------- /15UnixUnitIPC/11-15_31MyPrintMemAddr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/11-15_31MyPrintMemAddr -------------------------------------------------------------------------------- /15UnixUnitIPC/12-15_33MyIPCSharedMemUse_dev_zero: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/15UnixUnitIPC/12-15_33MyIPCSharedMemUse_dev_zero -------------------------------------------------------------------------------- /15UnixUnitIPC/readme.md: -------------------------------------------------------------------------------- 1 | ##历经波折到了IPC通信 2 | - 1、对于IPC通信,相信大家已经耳熟能详了,因为在日常linux的使用中,我们用了大量的通信机制 3 | - 2、管道:经常见吧 ‘| ’就是这个符号,但是管道也分为半双工管道。 全双工管道(目测大多数由 4 | 域套接字来支持,有的用管道和域套接字共同支持) 5 | - 3、对于命名全双工管道,在SUS中被废弃,其他体系的系统支持 6 | - 4、XSI消息队列 XSI信号量 XSI共享存储 7 | - 5、消息队列(实时),信号量 共享存储(实时) 8 | - 6、套接字都支持 但是对于STREAMS 所有标准的类unix系统 均已废除这一机制 9 | 10 | ####不多说了,有点兴奋,开始吧 11 | -------------------------------------------------------------------------------- /15UnixUnitIPC/slock.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: slock.h 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Sun 21 Feb 2016 04:12:26 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef _SLOCK_H 9 | #define _SLOCK_H 10 | #include 11 | 12 | 13 | struct slock 14 | { 15 | sem_t *semp; 16 | char name[_POSIX_NAME_MAX]; 17 | }; 18 | #endif 19 | -------------------------------------------------------------------------------- /16UnixUnitInterIPCsocket/01-16_9MyGetaddrinfoHowToUse: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/16UnixUnitInterIPCsocket/01-16_9MyGetaddrinfoHowToUse -------------------------------------------------------------------------------- /16UnixUnitInterIPCsocket/05-16_16MyGetSeverTime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/16UnixUnitInterIPCsocket/05-16_16MyGetSeverTime -------------------------------------------------------------------------------- /16UnixUnitInterIPCsocket/06-_17MyPostClientTime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/16UnixUnitInterIPCsocket/06-_17MyPostClientTime -------------------------------------------------------------------------------- /16UnixUnitInterIPCsocket/readme.md: -------------------------------------------------------------------------------- 1 | #网络IPC:套接字 2 | 3 | 4 | - 对于上一张我们学了经典的多进程通信机制IPC:管道 、FIFO有名管道、消息队列、信号量、以及共享存储 5 | - 这些队列允许在同一台计算机上运行的进程可以互相通信。 6 | - 但是在本章 7 | ##Scoket 8 | - 通过网络连接来使得不同计算机上的进程相互通信。网络进程间通信(Network IPC).一个拉风的名字 9 | - 在本章中,我们将描述桃姐在网络进程件通信接口,进程用该接口能够和其他进程通信,无论他们是在同一计算机内,还是在不同的计算机上。 10 | - 实际上这就是网络套接字的设计目标之一,同样的接口既可以用于计算机间通信。也可以用于计算机内通信。 11 | - 尽管套接字接口可以采用不同的网络协议进行通信,但本章的讨论限制在因特网事实上的通信标准。TCP/IP协议栈。 12 | 13 | -------------------------------------------------------------------------------- /GetEnv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/GetEnv -------------------------------------------------------------------------------- /GetEnv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc,char **argv,char **env) 4 | { 5 | while(env&&*env) 6 | { 7 | cout<<*env< 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | char *env="USER_HOME"; 8 | putenv("USER_HOME=/user/root"); 9 | char *a=getenv(env); 10 | if(a!=NULL) 11 | cout<<"env :"< 2 | #include"apue.h" 3 | //#include 4 | //#include 5 | //#include 6 | #include 7 | //#include//这个文件不可用 8 | int main(int argc,char **argv) 9 | { 10 | DIR *dp; 11 | struct dirent *dirp; 12 | if(argc!=2) 13 | err_quit("usage:ls directory_name"); 14 | // printf("error"); 15 | if((dp=opendir(argv[1]))==NULL) 16 | err_sys("can't open %s",argv[1]); 17 | // printf("can't open %s",argv[1]); 18 | while((dirp=readdir(dp))!=NULL) 19 | printf("%s\n",dirp->d_name); 20 | closedir(dp); 21 | exit(0); 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /UnixUnit01/Dirls.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/Dirls.o -------------------------------------------------------------------------------- /UnixUnit01/MyCopy.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(int argc,char **argv) 3 | { 4 | int c; 5 | while((c=getc(stdin))!=EOF) 6 | { 7 | if(putc(c,stdout)==EOF) 8 | err_sys("output error"); 9 | } 10 | if(ferror(stdin)) 11 | err_sys("input error"); 12 | 13 | 14 | 15 | exit(0); 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /UnixUnit01/MyCopy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/MyCopy.o -------------------------------------------------------------------------------- /UnixUnit01/MyPrintuserid.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | int main(void) 3 | { 4 | printf("uid=%d gid=%d \n",getuid(),getgid()); 5 | exit(0); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /UnixUnit01/MyPrintuserid.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/MyPrintuserid.o -------------------------------------------------------------------------------- /UnixUnit01/MyShell.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(int argc,char **argv) 4 | { 5 | char buf[MAXLINE];// from apue.h referrnce 6 | pid_t pid; 7 | int status; 8 | printf("%%"); 9 | while(fgets(buf,MAXLINE,stdin)!=NULL) 10 | { 11 | if((buf[strlen(buf)-1])=='\n') 12 | buf[strlen(buf)-1]=0;//relplace newline with null; 13 | if ( (pid = fork() ) < 0) 14 | { 15 | err_sys("fork error"); 16 | } 17 | else if(pid==0) 18 | { 19 | execlp(buf,buf, (char*)0); 20 | err_ret("couldn't execute :%s ",buf); 21 | exit(127); 22 | } 23 | 24 | 25 | if((pid=waitpid(pid,&status,0))<0) 26 | err_sys("waitpaid error"); 27 | printf("%%"); 28 | } 29 | exit(0); 30 | } 31 | -------------------------------------------------------------------------------- /UnixUnit01/MyShell.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/MyShell.o -------------------------------------------------------------------------------- /UnixUnit01/MyShell01singal.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | static void sig_int(int signo); 4 | int main(int argc,char **argv) 5 | { 6 | char buf[MAXLINE];// from apue.h referrnce 7 | pid_t pid; 8 | int status; 9 | if(signal(SIGINT,sig_int)==SIG_ERR) 10 | err_sys("singal error"); 11 | printf("%%"); 12 | while(fgets(buf,MAXLINE,stdin)!=NULL) 13 | { 14 | if((buf[strlen(buf)-1])=='\n') 15 | buf[strlen(buf)-1]=0;//relplace newline with null; 16 | if ( (pid = fork() ) < 0) 17 | { 18 | err_sys("fork error"); 19 | } 20 | else if(pid==0) 21 | { 22 | execlp(buf,buf, (char*)0); 23 | err_ret("couldn't execute :%s ",buf); 24 | exit(127); 25 | } 26 | 27 | 28 | if((pid=waitpid(pid,&status,0))<0) 29 | err_sys("waitpaid error"); 30 | printf("%%"); 31 | } 32 | exit(0); 33 | } 34 | 35 | 36 | void sig_int(int signo) 37 | { 38 | printf("interrupt\n%%"); 39 | } 40 | -------------------------------------------------------------------------------- /UnixUnit01/MyShell01singal.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/MyShell01singal.o -------------------------------------------------------------------------------- /UnixUnit01/Myerror.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main (int argc,char ** argv) 4 | { 5 | fprintf(stderr,"EACCESS :%s\n",strerror(EACCES)); 6 | errno=ENOENT; 7 | perror(argv[0]); 8 | exit(0); 9 | } 10 | -------------------------------------------------------------------------------- /UnixUnit01/Myerror.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/Myerror.o -------------------------------------------------------------------------------- /UnixUnit01/Myfock.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | int main(int argc,char ** argv) 3 | { 4 | printf("hello world from process Id %ld\n",(long)getpid()); 5 | exit(0); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /UnixUnit01/Myfork.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/Myfork.o -------------------------------------------------------------------------------- /UnixUnit01/Myio.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #define BUFFSIZE 4096 3 | int main(void) 4 | { 5 | int n; 6 | char buf[BUFFSIZE]; 7 | while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0) 8 | { 9 | if(write(STDOUT_FILENO,buf,n)!=n) 10 | err_sys("write error"); 11 | } 12 | if(n<0) 13 | err_sys("read error"); 14 | exit(0); 15 | } 16 | -------------------------------------------------------------------------------- /UnixUnit01/Myio.data: -------------------------------------------------------------------------------- 1 | sad 2 | asda 3 | ds 4 | ad 5 | a 6 | d 7 | -------------------------------------------------------------------------------- /UnixUnit01/Myio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnit01/Myio.o -------------------------------------------------------------------------------- /UnixUnit01/Test*++.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: Test*++.c 3 | > Author:Victor Qi 4 | > Mail:Victor_QiGetFuture@163.com 5 | > Created Time: Wed 02 Mar 2016 01:00:43 AM CST 6 | ************************************************************************/ 7 | #include 8 | int main(void) 9 | { 10 | char *ptr="Linux"; 11 | printf("%c",*ptr++); 12 | printf("%c",*ptr); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /UnixUnitIO03/MyCreatemptyio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/MyCreatemptyio -------------------------------------------------------------------------------- /UnixUnitIO03/MyCreatemptyio.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | char buf1[]="qwertyuiop"; 4 | char buf2[]="QWERTYUIOP"; 5 | int main(void) 6 | { 7 | int fd; 8 | if((fd=creat("myfile.txt",FILE_MODE))<0) 9 | err_sys("creat error"); 10 | if(write(fd,buf1,10)!=10) 11 | err_sys("write error"); 12 | if(lseek(fd,16384,SEEK_SET)==-1) 13 | err_sys("lseek error"); 14 | if(write (fd ,buf2,10)!=10) 15 | err_sys("write error"); 16 | 17 | 18 | 19 | 20 | //创建一个空洞文件,空洞文件是多线程的支持点之一,因为空洞文件的存在才能支持多线程断点续传 21 | 22 | 23 | exit(0); 24 | 25 | 26 | 27 | 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /UnixUnitIO03/MyCreatio.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include"fcntl.h" 3 | int main(int argc,char ** argv) 4 | { 5 | FILE *fp="./Mycreat.txt" 6 | creat(fp,"w+"); 7 | 8 | 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /UnixUnitIO03/MyEffectio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/MyEffectio -------------------------------------------------------------------------------- /UnixUnitIO03/MyEffectio.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #define BUFFSIZE 4096 3 | int main(void) 4 | { 5 | int n; 6 | char buff[BUFFSIZE]; 7 | while((n=read(STDIN_FILENO,buff,BUFFSIZE))>0) 8 | { 9 | if(write(STDOUT_FILENO,buff,n)!=n) 10 | err_sys("write error"); 11 | } 12 | if(n<0) 13 | err_sys("read error"); 14 | exit(0); 15 | } 16 | -------------------------------------------------------------------------------- /UnixUnitIO03/MyFileioAnnoce: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/MyFileioAnnoce -------------------------------------------------------------------------------- /UnixUnitIO03/MyStdio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/MyStdio -------------------------------------------------------------------------------- /UnixUnitIO03/MyStdio.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main (void) 4 | { 5 | putchar('a'); 6 | write(1, "b", 1); 7 | putchar('a'); 8 | write(1, "b", 1); 9 | putchar('a'); 10 | write(1, "b", 1); 11 | printf("\n"); 12 | return 0; 13 | } 14 | //window 下生成ababab 15 | //linux下是bbbaaa 16 | -------------------------------------------------------------------------------- /UnixUnitIO03/Myoffsetio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/Myoffsetio -------------------------------------------------------------------------------- /UnixUnitIO03/Myoffsetio.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(void) 4 | { 5 | if(lseek(STDIN_FILENO,0,SEEK_CUR)==-1) 6 | printf("cannot seek\n"); 7 | else 8 | printf("seek ok\n"); 9 | exit(0); 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /UnixUnitIO03/file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO03/file -------------------------------------------------------------------------------- /UnixUnitIO04/10Mydev: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/10Mydev -------------------------------------------------------------------------------- /UnixUnitIO04/10Mydev.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #ifdef SOLARIS 3 | #include 4 | #endif 5 | #include 6 | int main(int argc,char **argv) 7 | { 8 | int i; 9 | struct stat buf; 10 | for(i =1;i 3 | int main(int argc, char ** argv) 4 | { 5 | if(argc!=2) 6 | err_quit("\tusage:%s ",argv[0]); 7 | if(access(argv[1],R_OK)<0) 8 | err_ret("\taccess error for %s",argv[1]); 9 | else 10 | printf(" \tread access OK\n"); 11 | if(open(argv[1],O_RDONLY)<0) 12 | err_ret("\topen error for %s",argv[1]); 13 | else 14 | printf("\topen for reading OK \n"); 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /UnixUnitIO04/Mychdir08: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/Mychdir08 -------------------------------------------------------------------------------- /UnixUnitIO04/Mychdir08.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(void) 4 | { 5 | if(chdir("/tmp")<0) 6 | err_sys("chdir error"); 7 | printf("chdir to /tmp successed\n"); 8 | exit(0); 9 | 10 | 11 | 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /UnixUnitIO04/Mychmodio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/Mychmodio -------------------------------------------------------------------------------- /UnixUnitIO04/Mychmodio.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | 3 | 4 | int main(int argc , char** argv) 5 | { 6 | struct stat buf; 7 | if(stat("foo",&buf)<0) 8 | err_sys("stat error for foo"); 9 | if(chmod("foo",(buf.st_mode & ~S_IXGRP)|S_ISGID)<0) 10 | err_sys("chmod error for foo"); 11 | 12 | 13 | if(chmod("bar",S_IRUSR|S_IWUSR|S_IRWXG)<0) 14 | err_sys("chmod error for bar"); 15 | 16 | exit(0); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /UnixUnitIO04/Myfutimes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/Myfutimes -------------------------------------------------------------------------------- /UnixUnitIO04/Myfutimes.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | int main(int argc,char **argv) 4 | { 5 | int i,fd; 6 | struct stat buf; 7 | struct timespec times[2]; 8 | for(i=1;i 3 | int main(int argc ,char **argv) 4 | { 5 | if(open("tempfile",O_RDWR)<0) 6 | err_sys("open tempfile file faied"); 7 | if(unlink("tempfile")<0) 8 | err_sys("unlink error"); 9 | printf("file unlinked \n"); 10 | sleep(10); 11 | printf("done\n"); 12 | exit(0); 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | } 21 | 22 | -------------------------------------------------------------------------------- /UnixUnitIO04/Myumask: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/Myumask -------------------------------------------------------------------------------- /UnixUnitIO04/Myumask.c: -------------------------------------------------------------------------------- 1 | #include"apue.h" 2 | #include 3 | #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 4 | int main(int argc , char ** argv) 5 | { 6 | umask(0); 7 | if(creat("foo",RWRWRW)<0) 8 | err_sys("creat error for foo"); 9 | umask(S_IRUSR|S_IWUSR|S_IXUSR|S_IROTH|S_IWOTH); 10 | if(creat("bar",RWRWRW)<0) 11 | err_sys("create error for bar"); 12 | exit(0); 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /UnixUnitIO04/bar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/bar -------------------------------------------------------------------------------- /UnixUnitIO04/foo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/foo -------------------------------------------------------------------------------- /UnixUnitIO04/testln/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/UnixUnitIO04/testln/a -------------------------------------------------------------------------------- /UnixUnitIO04/testln/testdir: -------------------------------------------------------------------------------- 1 | testln/ -------------------------------------------------------------------------------- /apue.3e/DISCLAIMER: -------------------------------------------------------------------------------- 1 | LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY 2 | 3 | The author and publisher of the book "Advanced Programming in the 4 | Unix Environment" have used their best efforts in preparing this 5 | software. These efforts include the development, research, and 6 | testing of the theories and programs to determine their effectiveness. 7 | The author and publisher make no warranty of any kind, express or 8 | implied, with regard to these programs or the documentation contained 9 | in the book. The author and publisher shall not be liable in any event 10 | for incidental or consequential damages in connection with, or arising 11 | out of, the furnishing, performance, or use of these programs. 12 | -------------------------------------------------------------------------------- /apue.3e/Make.defines.freebsd: -------------------------------------------------------------------------------- 1 | # Common make definitions, customized for each platform 2 | 3 | # Definitions required in all program directories to compile and link 4 | # C programs using gcc. 5 | 6 | CC=gcc 7 | COMPILE.c=$(CC) $(CFLAGS) $(CPPFLAGS) -c 8 | LINK.c=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 9 | LDFLAGS= 10 | LDDIR=-L$(ROOT)/lib 11 | LDLIBS=$(LDDIR) -lapue $(EXTRALIBS) 12 | CFLAGS=-ansi -I$(ROOT)/include -Wall -DBSD -D__BSD_VISIBLE $(EXTRA) 13 | RANLIB=ranlib 14 | AR=ar 15 | AWK=awk 16 | LIBAPUE=$(ROOT)/lib/libapue.a 17 | 18 | # Common temp files to delete from each directory. 19 | TEMPFILES=core core.* *.o temp.* *.out 20 | -------------------------------------------------------------------------------- /apue.3e/Make.defines.linux: -------------------------------------------------------------------------------- 1 | # Common make definitions, customized for each platform 2 | 3 | # Definitions required in all program directories to compile and link 4 | # C programs using gcc. 5 | 6 | CC=gcc 7 | COMPILE.c=$(CC) $(CFLAGS) $(CPPFLAGS) -c 8 | LINK.c=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 9 | LDFLAGS= 10 | LDDIR=-L$(ROOT)/lib 11 | LDLIBS=$(LDDIR) -lapue $(EXTRALIBS) 12 | CFLAGS=-ansi -I$(ROOT)/include -Wall -DLINUX -D_GNU_SOURCE $(EXTRA) 13 | RANLIB=echo 14 | AR=ar 15 | AWK=awk 16 | LIBAPUE=$(ROOT)/lib/libapue.a 17 | 18 | # Common temp files to delete from each directory. 19 | TEMPFILES=core core.* *.o temp.* *.out 20 | -------------------------------------------------------------------------------- /apue.3e/Make.defines.macos: -------------------------------------------------------------------------------- 1 | # Common make definitions, customized for each platform 2 | 3 | # Definitions required in all program directories to compile and link 4 | # C programs using gcc. 5 | 6 | CC=gcc 7 | COMPILE.c=$(CC) $(CFLAGS) $(CPPFLAGS) -c 8 | LINK.c=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 9 | LDFLAGS= 10 | LDDIR=-L$(ROOT)/lib 11 | LDLIBS=$(LDDIR) -lapue $(EXTRALIBS) 12 | CFLAGS=-ansi -I$(ROOT)/include -Wall -DMACOS -D_DARWIN_C_SOURCE $(EXTRA) 13 | RANLIB=ranlib 14 | AR=ar 15 | AWK=awk 16 | LIBAPUE=$(ROOT)/lib/libapue.a 17 | 18 | # Common temp files to delete from each directory. 19 | TEMPFILES=core core.* *.o temp.* *.out 20 | -------------------------------------------------------------------------------- /apue.3e/Make.defines.solaris: -------------------------------------------------------------------------------- 1 | # Common make definitions, customized for each platform 2 | 3 | # Definitions required in all program directories to compile and link 4 | # C programs using gcc. 5 | 6 | CC=gcc 7 | COMPILE.c=$(CC) $(CFLAGS) $(CPPFLAGS) -c 8 | LINK.c=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 9 | LDFLAGS= 10 | LDDIR=-L$(ROOT)/lib 11 | LDLIBS=$(LDDIR) -lapue $(EXTRALIBS) 12 | CFLAGS=-std=c99 -m64 -I$(ROOT)/include -Wall -DSOLARIS -D__EXTENSIONS__ $(EXTRA) 13 | RANLIB=echo 14 | AR=ar 15 | AWK=nawk 16 | LIBAPUE=$(ROOT)/lib/libapue.a 17 | NAMEMAX=-DNAME_MAX=_XOPEN_NAME_MAX 18 | 19 | # Common temp files to delete from each directory. 20 | TEMPFILES=core core.* *.o temp.* *.out 21 | -------------------------------------------------------------------------------- /apue.3e/Make.libapue.inc: -------------------------------------------------------------------------------- 1 | $(LIBAPUE): 2 | (cd $(ROOT)/lib && $(MAKE)) 3 | -------------------------------------------------------------------------------- /apue.3e/Makefile: -------------------------------------------------------------------------------- 1 | DIRS = lib intro sockets advio daemons datafiles db environ \ 2 | fileio filedir ipc1 ipc2 proc pty relation signals standards \ 3 | stdio termios threadctl threads printer exercises 4 | 5 | all: 6 | for i in $(DIRS); do \ 7 | (cd $$i && echo "making $$i" && $(MAKE) ) || exit 1; \ 8 | done 9 | 10 | clean: 11 | for i in $(DIRS); do \ 12 | (cd $$i && echo "cleaning $$i" && $(MAKE) clean) || exit 1; \ 13 | done 14 | -------------------------------------------------------------------------------- /apue.3e/README: -------------------------------------------------------------------------------- 1 | Read the file called DISCLAIMER. 2 | 3 | On Freebsd, type "gmake". 4 | On other platforms, type "make" (as long as this is gnu make). 5 | 6 | For FAQs, updated source code, and the lost chapter, see http://www.apuebook.com. 7 | Please direct questions, suggestions, and bug reports to sar@apuebook.com. 8 | 9 | Steve Rago 10 | January 2013 11 | -------------------------------------------------------------------------------- /apue.3e/advio/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "linux" 6 | EXTRALIBS=-lrt 7 | endif 8 | ifeq "$(PLATFORM)" "solaris" 9 | EXTRALIBS=-lrt 10 | endif 11 | 12 | PROGS = deadlock mandatory mcopy2 nonblockw rot13a 13 | MOREPROGS = rot13c2 14 | 15 | all: $(PROGS) $(MOREPROGS) lockfile.o 16 | 17 | rot13c2.c: rot13c2.c.in $(LIBAPUE) 18 | ./fixup.awk rot13a.c >xlate 19 | sed '/same/q' rot13c2.c.in >rot13c2.c 20 | cat xlate >>rot13c2.c 21 | sed '1,/same/d' rot13c2.c.in >>rot13c2.c 22 | 23 | %: %.c $(LIBAPUE) 24 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 25 | 26 | clean: 27 | rm -f $(PROGS) $(MOREPROGS) $(TEMPFILES) *.o xlate rot13c2.c 28 | 29 | include $(ROOT)/Make.libapue.inc 30 | -------------------------------------------------------------------------------- /apue.3e/advio/fixup.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | BEGIN { 3 | ready = 0 4 | } 5 | /^translate/ { 6 | ready = 1 7 | next 8 | } 9 | /^{/ { 10 | next 11 | } 12 | /^}/ { 13 | ready = 0 14 | next 15 | } 16 | { 17 | if (ready) 18 | print $0 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/advio/lockfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int 5 | lockfile(int fd) 6 | { 7 | struct flock fl; 8 | 9 | fl.l_type = F_WRLCK; 10 | fl.l_start = 0; 11 | fl.l_whence = SEEK_SET; 12 | fl.l_len = 0; 13 | return(fcntl(fd, F_SETLK, &fl)); 14 | } 15 | -------------------------------------------------------------------------------- /apue.3e/advio/nonblockw.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | char buf[500000]; 6 | 7 | int 8 | main(void) 9 | { 10 | int ntowrite, nwrite; 11 | char *ptr; 12 | 13 | ntowrite = read(STDIN_FILENO, buf, sizeof(buf)); 14 | fprintf(stderr, "read %d bytes\n", ntowrite); 15 | 16 | set_fl(STDOUT_FILENO, O_NONBLOCK); /* set nonblocking */ 17 | 18 | ptr = buf; 19 | while (ntowrite > 0) { 20 | errno = 0; 21 | nwrite = write(STDOUT_FILENO, ptr, ntowrite); 22 | fprintf(stderr, "nwrite = %d, errno = %d\n", nwrite, errno); 23 | 24 | if (nwrite > 0) { 25 | ptr += nwrite; 26 | ntowrite -= nwrite; 27 | } 28 | } 29 | 30 | clr_fl(STDOUT_FILENO, O_NONBLOCK); /* clear nonblocking */ 31 | 32 | exit(0); 33 | } 34 | -------------------------------------------------------------------------------- /apue.3e/advio/readn.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | ssize_t /* Read "n" bytes from a descriptor */ 4 | readn(int fd, void *ptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nread; 8 | 9 | nleft = n; 10 | while (nleft > 0) { 11 | if ((nread = read(fd, ptr, nleft)) < 0) { 12 | if (nleft == n) 13 | return(-1); /* error, return -1 */ 14 | else 15 | break; /* error, return amount read so far */ 16 | } else if (nread == 0) { 17 | break; /* EOF */ 18 | } 19 | nleft -= nread; 20 | ptr += nread; 21 | } 22 | return(n - nleft); /* return >= 0 */ 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/advio/writen.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | ssize_t /* Write "n" bytes to a descriptor */ 4 | writen(int fd, const void *ptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nwritten; 8 | 9 | nleft = n; 10 | while (nleft > 0) { 11 | if ((nwritten = write(fd, ptr, nleft)) < 0) { 12 | if (nleft == n) 13 | return(-1); /* error, return -1 */ 14 | else 15 | break; /* error, return amount written so far */ 16 | } else if (nwritten == 0) { 17 | break; 18 | } 19 | nleft -= nwritten; 20 | ptr += nwritten; 21 | } 22 | return(n - nleft); /* return >= 0 */ 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/daemons/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | all: init.o reread.o reread2.o single.o 6 | 7 | clean: 8 | rm -f $(TEMPFILES) *.o 9 | -------------------------------------------------------------------------------- /apue.3e/datafiles/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = strftime 6 | 7 | all: $(PROGS) getpwnam.o 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/datafiles/getpwnam.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct passwd * 6 | getpwnam(const char *name) 7 | { 8 | struct passwd *ptr; 9 | 10 | setpwent(); 11 | while ((ptr = getpwent()) != NULL) 12 | if (strcmp(name, ptr->pw_name) == 0) 13 | break; /* found a match */ 14 | endpwent(); 15 | return(ptr); /* ptr is NULL if no match found */ 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/datafiles/strftime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int 6 | main(void) 7 | { 8 | time_t t; 9 | struct tm *tmp; 10 | char buf1[16]; 11 | char buf2[64]; 12 | 13 | time(&t); 14 | tmp = localtime(&t); 15 | if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y", tmp) == 0) 16 | printf("buffer length 16 is too small\n"); 17 | else 18 | printf("%s\n", buf1); 19 | if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y", tmp) == 0) 20 | printf("buffer length 64 is too small\n"); 21 | else 22 | printf("%s\n", buf2); 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/db/t4.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include "apue_db.h" 3 | #include 4 | 5 | int 6 | main(void) 7 | { 8 | DBHANDLE db; 9 | 10 | if ((db = db_open("db4", O_RDWR | O_CREAT | O_TRUNC, 11 | FILE_MODE)) == NULL) 12 | err_sys("db_open error"); 13 | 14 | if (db_store(db, "Alpha", "data1", DB_INSERT) != 0) 15 | err_quit("db_store error for alpha"); 16 | if (db_store(db, "beta", "Data for beta", DB_INSERT) != 0) 17 | err_quit("db_store error for beta"); 18 | if (db_store(db, "gamma", "record3", DB_INSERT) != 0) 19 | err_quit("db_store error for gamma"); 20 | 21 | db_close(db); 22 | exit(0); 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/environ/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = doatexit echoarg getrlimit hello1 testjmp 6 | 7 | all: $(PROGS) opendata.o scope.o 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/environ/cmd1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define TOK_ADD 5 4 | 5 | void do_line(char *); 6 | void cmd_add(void); 7 | int get_token(void); 8 | 9 | int 10 | main(void) 11 | { 12 | char line[MAXLINE]; 13 | 14 | while (fgets(line, MAXLINE, stdin) != NULL) 15 | do_line(line); 16 | exit(0); 17 | } 18 | 19 | char *tok_ptr; /* global pointer for get_token() */ 20 | 21 | void 22 | do_line(char *ptr) /* process one line of input */ 23 | { 24 | int cmd; 25 | 26 | tok_ptr = ptr; 27 | while ((cmd = get_token()) > 0) { 28 | switch (cmd) { /* one case for each command */ 29 | case TOK_ADD: 30 | cmd_add(); 31 | break; 32 | } 33 | } 34 | } 35 | 36 | void 37 | cmd_add(void) 38 | { 39 | int token; 40 | 41 | token = get_token(); 42 | /* rest of processing for this command */ 43 | } 44 | 45 | int 46 | get_token(void) 47 | { 48 | /* fetch next token from line pointed to by tok_ptr */ 49 | } 50 | -------------------------------------------------------------------------------- /apue.3e/environ/cmd2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define TOK_ADD 5 5 | 6 | jmp_buf jmpbuffer; 7 | 8 | int 9 | main(void) 10 | { 11 | char line[MAXLINE]; 12 | 13 | if (setjmp(jmpbuffer) != 0) 14 | printf("error"); 15 | while (fgets(line, MAXLINE, stdin) != NULL) 16 | do_line(line); 17 | exit(0); 18 | } 19 | 20 | . . . 21 | 22 | void 23 | cmd_add(void) 24 | { 25 | int token; 26 | 27 | token = get_token(); 28 | if (token < 0) /* an error has occurred */ 29 | longjmp(jmpbuffer, 1); 30 | /* rest of processing for this command */ 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/environ/doatexit.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void my_exit1(void); 4 | static void my_exit2(void); 5 | 6 | int 7 | main(void) 8 | { 9 | if (atexit(my_exit2) != 0) 10 | err_sys("can't register my_exit2"); 11 | 12 | if (atexit(my_exit1) != 0) 13 | err_sys("can't register my_exit1"); 14 | if (atexit(my_exit1) != 0) 15 | err_sys("can't register my_exit1"); 16 | 17 | printf("main is done\n"); 18 | return(0); 19 | } 20 | 21 | static void 22 | my_exit1(void) 23 | { 24 | printf("first exit handler\n"); 25 | } 26 | 27 | static void 28 | my_exit2(void) 29 | { 30 | printf("second exit handler\n"); 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/environ/echoarg.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int i; 7 | 8 | for (i = 0; i < argc; i++) /* echo all command-line args */ 9 | printf("argv[%d]: %s\n", i, argv[i]); 10 | exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /apue.3e/environ/hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("hello, world\n"); 6 | } 7 | -------------------------------------------------------------------------------- /apue.3e/environ/opendata.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | FILE * 4 | open_data(void) 5 | { 6 | FILE *fp; 7 | char databuf[BUFSIZ]; /* setvbuf makes this the stdio buffer */ 8 | 9 | if ((fp = fopen("datafile", "r")) == NULL) 10 | return(NULL); 11 | if (setvbuf(fp, databuf, _IOLBF, BUFSIZ) != 0) 12 | return(NULL); 13 | return(fp); /* error */ 14 | } 15 | -------------------------------------------------------------------------------- /apue.3e/environ/scope.c: -------------------------------------------------------------------------------- 1 | int 2 | f1(int val) 3 | { 4 | int num = 0; 5 | int *ptr = # 6 | 7 | if (val == 0) { 8 | int val; 9 | 10 | val = 5; 11 | ptr = &val; 12 | } 13 | return(*ptr + 1); 14 | } 15 | -------------------------------------------------------------------------------- /apue.3e/exercises/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "freebsd" 6 | GETPW = getpw44bsd 7 | FMEM = fmemopen.o 8 | EXTRALIBS=-pthread 9 | endif 10 | ifeq "$(PLATFORM)" "linux" 11 | GETPW = getpwsvr4 12 | FMEM = 13 | EXTRALIBS=-pthread 14 | endif 15 | ifeq "$(PLATFORM)" "macos" 16 | GETPW = getpw44bsd 17 | FMEM = fmemopen.o 18 | endif 19 | ifeq "$(PLATFORM)" "solaris" 20 | GETPW = getpwsvr4 21 | FMEM = 22 | endif 23 | 24 | PROGS = bo fifo1 getlogin goodexit longpath pendlock prtime sizepipe vfork3 zombie 25 | 26 | all: $(PROGS) asyncsocket.o $(FMEM) openmax.o sleep.o sleepus_poll.o sleepus_select.o $(GETPW) 27 | 28 | %: %.c $(LIBAPUE) 29 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 30 | 31 | clean: 32 | rm -f $(PROGS) $(TEMPFILES) *.o $(GETPW) 33 | 34 | include $(ROOT)/Make.libapue.inc 35 | -------------------------------------------------------------------------------- /apue.3e/exercises/asyncsocket.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #if defined(BSD) || defined(MACOS) || defined(SOLARIS) 7 | #include 8 | #endif 9 | 10 | int 11 | setasync(int sockfd) 12 | { 13 | int n; 14 | 15 | if (fcntl(sockfd, F_SETOWN, getpid()) < 0) 16 | return(-1); 17 | n = 1; 18 | if (ioctl(sockfd, FIOASYNC, &n) < 0) 19 | return(-1); 20 | return(0); 21 | } 22 | 23 | int 24 | clrasync(int sockfd) 25 | { 26 | int n; 27 | 28 | n = 0; 29 | if (ioctl(sockfd, FIOASYNC, &n) < 0) 30 | return(-1); 31 | return(0); 32 | } 33 | -------------------------------------------------------------------------------- /apue.3e/exercises/bo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int 6 | main(void) 7 | { 8 | uint32_t i = 0x04030201; 9 | unsigned char *cp = (unsigned char *)&i; 10 | 11 | if (*cp == 1) 12 | printf("little-endian\n"); 13 | else if (*cp == 4) 14 | printf("big-endian\n"); 15 | else 16 | printf("who knows?\n"); 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/exercises/fifo1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define FIFO "temp.fifo" 5 | 6 | int 7 | main(void) 8 | { 9 | int fdread, fdwrite; 10 | 11 | unlink(FIFO); 12 | if (mkfifo(FIFO, FILE_MODE) < 0) 13 | err_sys("mkfifo error"); 14 | if ((fdread = open(FIFO, O_RDONLY | O_NONBLOCK)) < 0) 15 | err_sys("open error for reading"); 16 | if ((fdwrite = open(FIFO, O_WRONLY)) < 0) 17 | err_sys("open error for writing"); 18 | clr_fl(fdread, O_NONBLOCK); 19 | exit(0); 20 | } 21 | -------------------------------------------------------------------------------- /apue.3e/exercises/getlogin.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | FILE *fp; 7 | char *p; 8 | 9 | daemonize("getlog"); 10 | p = getlogin(); 11 | fp = fopen("/tmp/getlog.out", "w"); 12 | if (fp != NULL) { 13 | if (p == NULL) 14 | fprintf(fp, "no login name\n"); 15 | else 16 | fprintf(fp, "login name: %s\n", p); 17 | } 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/exercises/getpw44bsd.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) /* FreeBSD/Mac OS X version */ 6 | { 7 | struct passwd *ptr; 8 | 9 | if ((ptr = getpwnam("sar")) == NULL) 10 | err_sys("getpwnam error"); 11 | printf("pw_passwd = %s\n", ptr->pw_passwd == NULL || 12 | ptr->pw_passwd[0] == 0 ? "(null)" : ptr->pw_passwd); 13 | exit(0); 14 | } 15 | -------------------------------------------------------------------------------- /apue.3e/exercises/getpwsvr4.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) /* Linux/Solaris version */ 6 | { 7 | struct spwd *ptr; 8 | 9 | if ((ptr = getspnam("sar")) == NULL) 10 | err_sys("getspnam error"); 11 | printf("sp_pwdp = %s\n", ptr->sp_pwdp == NULL || 12 | ptr->sp_pwdp[0] == 0 ? "(null)" : ptr->sp_pwdp); 13 | exit(0); 14 | } 15 | -------------------------------------------------------------------------------- /apue.3e/exercises/openmax.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #define OPEN_MAX_GUESS 256 6 | 7 | long 8 | open_max(void) 9 | { 10 | long openmax; 11 | struct rlimit rl; 12 | 13 | if ((openmax = sysconf(_SC_OPEN_MAX)) < 0 || 14 | openmax == LONG_MAX) { 15 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) 16 | err_sys("can't get file limit"); 17 | if (rl.rlim_max == RLIM_INFINITY) 18 | openmax = OPEN_MAX_GUESS; 19 | else 20 | openmax = rl.rlim_max; 21 | } 22 | return(openmax); 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/exercises/prtime.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | time_t caltime; 8 | struct tm *tm; 9 | char line[MAXLINE]; 10 | 11 | if ((caltime = time(NULL)) == -1) 12 | err_sys("time error"); 13 | if ((tm = localtime(&caltime)) == NULL) 14 | err_sys("localtime error"); 15 | if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", tm) == 0) 16 | err_sys("strftime error"); 17 | fputs(line, stdout); 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/exercises/sizepipe.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | int i, n; 8 | int fd[2]; 9 | 10 | if (pipe(fd) < 0) 11 | err_sys("pipe error"); 12 | set_fl(fd[1], O_NONBLOCK); 13 | 14 | /* write 1 byte at a time until pipe is full */ 15 | for (n = 0; ; n++) { 16 | if ((i = write(fd[1], "a", 1)) != 1) { 17 | printf("write ret %d, ", i); 18 | break; 19 | } 20 | } 21 | printf("pipe capacity = %d\n", n); 22 | exit(0); 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/exercises/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | unsigned 6 | sleep(unsigned seconds) 7 | { 8 | int n; 9 | unsigned slept; 10 | time_t start, end; 11 | struct timeval tv; 12 | 13 | tv.tv_sec = seconds; 14 | tv.tv_usec = 0; 15 | time(&start); 16 | n = select(0, NULL, NULL, NULL, &tv); 17 | if (n == 0) 18 | return(0); 19 | time(&end); 20 | slept = end - start; 21 | if (slept >= seconds) 22 | return(0); 23 | return(seconds - slept); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/exercises/sleepus_poll.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void 4 | sleep_us(unsigned int nusecs) 5 | { 6 | struct pollfd dummy; 7 | int timeout; 8 | 9 | if ((timeout = nusecs / 1000) <= 0) 10 | timeout = 1; 11 | poll(&dummy, 0, timeout); 12 | } 13 | -------------------------------------------------------------------------------- /apue.3e/exercises/sleepus_select.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | sleep_us(unsigned int nusecs) 6 | { 7 | struct timeval tval; 8 | 9 | tval.tv_sec = nusecs / 1000000; 10 | tval.tv_usec = nusecs % 1000000; 11 | select(0, NULL, NULL, NULL, &tval); 12 | } 13 | -------------------------------------------------------------------------------- /apue.3e/exercises/vfork3.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void f1(void), f2(void); 4 | 5 | int 6 | main(void) 7 | { 8 | f1(); 9 | f2(); 10 | _exit(0); 11 | } 12 | 13 | static void 14 | f1(void) 15 | { 16 | pid_t pid; 17 | 18 | if ((pid = vfork()) < 0) 19 | err_sys("vfork error"); 20 | /* child and parent both return */ 21 | } 22 | 23 | static void 24 | f2(void) 25 | { 26 | char buf[1000]; /* automatic variables */ 27 | int i; 28 | 29 | for (i = 0; i < sizeof(buf); i++) 30 | buf[i] = 0; 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/exercises/zombie.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #ifdef SOLARIS 4 | #define PSCMD "ps -a -o pid,ppid,s,tty,comm" 5 | #else 6 | #define PSCMD "ps -o pid,ppid,state,tty,command" 7 | #endif 8 | 9 | int 10 | main(void) 11 | { 12 | pid_t pid; 13 | 14 | if ((pid = fork()) < 0) 15 | err_sys("fork error"); 16 | else if (pid == 0) /* child */ 17 | exit(0); 18 | 19 | /* parent */ 20 | sleep(4); 21 | system(PSCMD); 22 | 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.10: -------------------------------------------------------------------------------- 1 | ../intro/shell2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.3: -------------------------------------------------------------------------------- 1 | ../intro/ls1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.4: -------------------------------------------------------------------------------- 1 | ../intro/mycat.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.5: -------------------------------------------------------------------------------- 1 | ../intro/getcputc.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.6: -------------------------------------------------------------------------------- 1 | ../intro/hello.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.7: -------------------------------------------------------------------------------- 1 | ../intro/shell1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.8: -------------------------------------------------------------------------------- 1 | ../intro/testerror.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig1.9: -------------------------------------------------------------------------------- 1 | ../intro/uidgid.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.10: -------------------------------------------------------------------------------- 1 | ../signals/read1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.11: -------------------------------------------------------------------------------- 1 | ../signals/read2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.12: -------------------------------------------------------------------------------- 1 | ../signals/setops.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.14: -------------------------------------------------------------------------------- 1 | ../lib/prmask.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.15: -------------------------------------------------------------------------------- 1 | ../signals/critical.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.18: -------------------------------------------------------------------------------- 1 | ../lib/signal.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.19: -------------------------------------------------------------------------------- 1 | ../lib/signalintr.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.2: -------------------------------------------------------------------------------- 1 | ../signals/sigusr.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.20: -------------------------------------------------------------------------------- 1 | ../signals/mask.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.22: -------------------------------------------------------------------------------- 1 | ../signals/suspend1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.23: -------------------------------------------------------------------------------- 1 | ../signals/suspend2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.24: -------------------------------------------------------------------------------- 1 | ../lib/tellwait.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.25: -------------------------------------------------------------------------------- 1 | ../signals/abort.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.26: -------------------------------------------------------------------------------- 1 | ../signals/systest2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.28: -------------------------------------------------------------------------------- 1 | ../signals/system.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.29: -------------------------------------------------------------------------------- 1 | ../lib/sleep.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.31: -------------------------------------------------------------------------------- 1 | ../signals/sigtstp.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.5: -------------------------------------------------------------------------------- 1 | ../signals/reenter.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.6: -------------------------------------------------------------------------------- 1 | ../signals/child.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.7: -------------------------------------------------------------------------------- 1 | ../signals/sleep1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.8: -------------------------------------------------------------------------------- 1 | ../signals/sleep2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig10.9: -------------------------------------------------------------------------------- 1 | ../signals/tsleep2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.10: -------------------------------------------------------------------------------- 1 | ../threads/mutex1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.11: -------------------------------------------------------------------------------- 1 | ../threads/mutex2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.12: -------------------------------------------------------------------------------- 1 | ../threads/mutex3.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.14: -------------------------------------------------------------------------------- 1 | ../threads/rwlock.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.15: -------------------------------------------------------------------------------- 1 | ../threads/condvar.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.16: -------------------------------------------------------------------------------- 1 | ../threads/barrier.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.2: -------------------------------------------------------------------------------- 1 | ../threads/threadid.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.3: -------------------------------------------------------------------------------- 1 | ../threads/exitstatus.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.4: -------------------------------------------------------------------------------- 1 | ../threads/badexit2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig11.5: -------------------------------------------------------------------------------- 1 | ../threads/cleanup.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.11: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.12: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.13: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv3.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.16: -------------------------------------------------------------------------------- 1 | ../threadctl/suspend.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.17: -------------------------------------------------------------------------------- 1 | ../threadctl/atfork.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.4: -------------------------------------------------------------------------------- 1 | ../threadctl/detach.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig12.8: -------------------------------------------------------------------------------- 1 | ../threadctl/timeout.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig13.1: -------------------------------------------------------------------------------- 1 | ../daemons/init.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig13.6: -------------------------------------------------------------------------------- 1 | ../daemons/single.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig13.7: -------------------------------------------------------------------------------- 1 | ../daemons/reread.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig13.8: -------------------------------------------------------------------------------- 1 | ../daemons/reread2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig13.9: -------------------------------------------------------------------------------- 1 | ../lib/setfd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.1: -------------------------------------------------------------------------------- 1 | ../advio/nonblockw.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.12: -------------------------------------------------------------------------------- 1 | ../advio/mandatory.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.20: -------------------------------------------------------------------------------- 1 | ../advio/rot13a.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.21: -------------------------------------------------------------------------------- 1 | ../advio/rot13c2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.24: -------------------------------------------------------------------------------- 1 | ../advio/writen.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.27: -------------------------------------------------------------------------------- 1 | ../advio/mcopy2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.5: -------------------------------------------------------------------------------- 1 | ../lib/lockreg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.6: -------------------------------------------------------------------------------- 1 | ../lib/locktest.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.7: -------------------------------------------------------------------------------- 1 | ../advio/deadlock.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig14.9: -------------------------------------------------------------------------------- 1 | ../advio/lockfile.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.11: -------------------------------------------------------------------------------- 1 | ../ipc1/popen2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.12: -------------------------------------------------------------------------------- 1 | ../ipc1/popen.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.14: -------------------------------------------------------------------------------- 1 | ../ipc1/myuclc.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.15: -------------------------------------------------------------------------------- 1 | ../ipc1/popen1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.17: -------------------------------------------------------------------------------- 1 | ../ipc1/add2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.18: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe4.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.19: -------------------------------------------------------------------------------- 1 | ../ipc1/add2stdio.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.31: -------------------------------------------------------------------------------- 1 | ../ipc1/tshm.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.33: -------------------------------------------------------------------------------- 1 | ../ipc1/devzero.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.35: -------------------------------------------------------------------------------- 1 | ../ipc1/slock.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.5: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.6: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig15.7: -------------------------------------------------------------------------------- 1 | ../ipc1/tellwait.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.10: -------------------------------------------------------------------------------- 1 | ../sockets/clconn.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.11: -------------------------------------------------------------------------------- 1 | ../sockets/clconn2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.12: -------------------------------------------------------------------------------- 1 | ../sockets/initsrv1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.16: -------------------------------------------------------------------------------- 1 | ../sockets/ruptime.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.17: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.18: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed-fd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.19: -------------------------------------------------------------------------------- 1 | ../sockets/ruptime-dg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.20: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed-dg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.22: -------------------------------------------------------------------------------- 1 | ../sockets/initsrv2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig16.9: -------------------------------------------------------------------------------- 1 | ../sockets/findsvc.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.10: -------------------------------------------------------------------------------- 1 | ../lib/cliconn.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.12: -------------------------------------------------------------------------------- 1 | ../lib/senderr.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.13: -------------------------------------------------------------------------------- 1 | ../lib/sendfd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.14: -------------------------------------------------------------------------------- 1 | ../lib/recvfd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.15: -------------------------------------------------------------------------------- 1 | ../ipc2/sendfd2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.16: -------------------------------------------------------------------------------- 1 | ../ipc2/recvfd2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.17: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/open.h -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.18: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/main.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.19: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/open.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.2: -------------------------------------------------------------------------------- 1 | ../lib/spipe.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.20: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/opend.h -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.21: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/main.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.22: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/request.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.23: -------------------------------------------------------------------------------- 1 | ../lib/bufargs.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.24: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/cliargs.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.25: -------------------------------------------------------------------------------- 1 | ../ipc2/open/open.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.26: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/opend.h -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.27: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/client.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.28: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/main.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.29: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/loop.select.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.3: -------------------------------------------------------------------------------- 1 | ../ipc2/pollmsg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.30: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/loop.poll.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.31: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/request.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.4: -------------------------------------------------------------------------------- 1 | ../ipc2/sendmsg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.5: -------------------------------------------------------------------------------- 1 | ../ipc2/bindunix.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.8: -------------------------------------------------------------------------------- 1 | ../lib/servlisten.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig17.9: -------------------------------------------------------------------------------- 1 | ../lib/servaccept.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.10: -------------------------------------------------------------------------------- 1 | ../termios/settty.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.11: -------------------------------------------------------------------------------- 1 | ../termios/csize.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.12: -------------------------------------------------------------------------------- 1 | ../termios/ctermid.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.13: -------------------------------------------------------------------------------- 1 | ../termios/isatty.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.14: -------------------------------------------------------------------------------- 1 | ../termios/t_isatty.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.15: -------------------------------------------------------------------------------- 1 | ../termios/ttyname.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.16: -------------------------------------------------------------------------------- 1 | ../termios/t_ttyname.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.17: -------------------------------------------------------------------------------- 1 | ../termios/getpass.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.18: -------------------------------------------------------------------------------- 1 | ../termios/t_getpass.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.20: -------------------------------------------------------------------------------- 1 | ../lib/ttymodes.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.21: -------------------------------------------------------------------------------- 1 | ../termios/t_raw.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig18.22: -------------------------------------------------------------------------------- 1 | ../termios/winch.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig19.10: -------------------------------------------------------------------------------- 1 | ../lib/ptyfork.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig19.11: -------------------------------------------------------------------------------- 1 | ../pty/main.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig19.12: -------------------------------------------------------------------------------- 1 | ../pty/loop.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig19.16: -------------------------------------------------------------------------------- 1 | ../pty/driver.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig19.9: -------------------------------------------------------------------------------- 1 | ../lib/ptyopen.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig2.13: -------------------------------------------------------------------------------- 1 | ../standards/makeconf.awk -------------------------------------------------------------------------------- /apue.3e/figlinks/fig2.14: -------------------------------------------------------------------------------- 1 | ../standards/conf.c.modified -------------------------------------------------------------------------------- /apue.3e/figlinks/fig2.16: -------------------------------------------------------------------------------- 1 | ../lib/pathalloc.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig2.17: -------------------------------------------------------------------------------- 1 | ../lib/openmax.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig20.3: -------------------------------------------------------------------------------- 1 | ../db/t4.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig3.1: -------------------------------------------------------------------------------- 1 | ../fileio/seek.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig3.11: -------------------------------------------------------------------------------- 1 | ../fileio/fileflags.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig3.12: -------------------------------------------------------------------------------- 1 | ../fileio/setfl.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig3.2: -------------------------------------------------------------------------------- 1 | ../fileio/hole.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig3.5: -------------------------------------------------------------------------------- 1 | ../fileio/mycat.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.12: -------------------------------------------------------------------------------- 1 | ../filedir/changemod.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.16: -------------------------------------------------------------------------------- 1 | ../filedir/unlink.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.21: -------------------------------------------------------------------------------- 1 | ../filedir/zap.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.22: -------------------------------------------------------------------------------- 1 | ../filedir/ftw8.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.23: -------------------------------------------------------------------------------- 1 | ../filedir/mycd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.24: -------------------------------------------------------------------------------- 1 | ../filedir/cdpwd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.25: -------------------------------------------------------------------------------- 1 | ../filedir/devrdev.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.3: -------------------------------------------------------------------------------- 1 | ../filedir/filetype.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.8: -------------------------------------------------------------------------------- 1 | ../filedir/access.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig4.9: -------------------------------------------------------------------------------- 1 | ../filedir/umask.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.11: -------------------------------------------------------------------------------- 1 | ../stdio/buf.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.12: -------------------------------------------------------------------------------- 1 | ../stdio/tempfiles.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.13: -------------------------------------------------------------------------------- 1 | ../stdio/mkstemp.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.15: -------------------------------------------------------------------------------- 1 | ../stdio/memstr.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.4: -------------------------------------------------------------------------------- 1 | ../stdio/getcputc.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig5.5: -------------------------------------------------------------------------------- 1 | ../stdio/fgetsfputs.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig6.11: -------------------------------------------------------------------------------- 1 | ../datafiles/strftime.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig6.2: -------------------------------------------------------------------------------- 1 | ../datafiles/getpwnam.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.1: -------------------------------------------------------------------------------- 1 | ../environ/hello1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.11: -------------------------------------------------------------------------------- 1 | ../environ/cmd2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.13: -------------------------------------------------------------------------------- 1 | ../environ/testjmp.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.14: -------------------------------------------------------------------------------- 1 | ../environ/opendata.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.16: -------------------------------------------------------------------------------- 1 | ../environ/getrlimit.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.3: -------------------------------------------------------------------------------- 1 | ../environ/doatexit.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.4: -------------------------------------------------------------------------------- 1 | ../environ/echoarg.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig7.9: -------------------------------------------------------------------------------- 1 | ../environ/cmd1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.1: -------------------------------------------------------------------------------- 1 | ../proc/fork1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.12: -------------------------------------------------------------------------------- 1 | ../proc/tellwait1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.13: -------------------------------------------------------------------------------- 1 | ../proc/tellwait2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.16: -------------------------------------------------------------------------------- 1 | ../proc/exec1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.17: -------------------------------------------------------------------------------- 1 | ../proc/echoall.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.20: -------------------------------------------------------------------------------- 1 | ../proc/exec2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.21: -------------------------------------------------------------------------------- 1 | ../proc/awkexample -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.22: -------------------------------------------------------------------------------- 1 | ../proc/system.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.23: -------------------------------------------------------------------------------- 1 | ../proc/systest1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.24: -------------------------------------------------------------------------------- 1 | ../proc/systest3.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.25: -------------------------------------------------------------------------------- 1 | ../proc/pruids.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.28: -------------------------------------------------------------------------------- 1 | ../proc/test1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.29: -------------------------------------------------------------------------------- 1 | ../proc/pracct.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.3: -------------------------------------------------------------------------------- 1 | ../proc/vfork1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.30: -------------------------------------------------------------------------------- 1 | ../proc/nice.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.31: -------------------------------------------------------------------------------- 1 | ../proc/times1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.5: -------------------------------------------------------------------------------- 1 | ../lib/prexit.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.6: -------------------------------------------------------------------------------- 1 | ../proc/wait1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig8.8: -------------------------------------------------------------------------------- 1 | ../proc/fork2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/fig9.12: -------------------------------------------------------------------------------- 1 | ../relation/orphan3.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figB.1: -------------------------------------------------------------------------------- 1 | ../include/apue.h -------------------------------------------------------------------------------- /apue.3e/figlinks/figB.3: -------------------------------------------------------------------------------- 1 | ../lib/error.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figB.4: -------------------------------------------------------------------------------- 1 | ../lib/errorlog.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.1: -------------------------------------------------------------------------------- 1 | ../exercises/openmax.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.10: -------------------------------------------------------------------------------- 1 | ../exercises/zombie.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.12: -------------------------------------------------------------------------------- 1 | ../exercises/goodexit.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.13: -------------------------------------------------------------------------------- 1 | ../exercises/sleep.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.14: -------------------------------------------------------------------------------- 1 | ../exercises/getlogin.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.15: -------------------------------------------------------------------------------- 1 | ../exercises/pendlock.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.16: -------------------------------------------------------------------------------- 1 | ../exercises/sleepus_select.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.17: -------------------------------------------------------------------------------- 1 | ../exercises/sleepus_poll.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.18: -------------------------------------------------------------------------------- 1 | ../exercises/sizepipe.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.20: -------------------------------------------------------------------------------- 1 | ../exercises/fifo1.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.22: -------------------------------------------------------------------------------- 1 | ../exercises/bo.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.23: -------------------------------------------------------------------------------- 1 | ../exercises/asyncsocket.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.24: -------------------------------------------------------------------------------- 1 | ../exercises/pollmsg2.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.3: -------------------------------------------------------------------------------- 1 | ../exercises/longpath.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.4: -------------------------------------------------------------------------------- 1 | ../exercises/fmemopen.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.5: -------------------------------------------------------------------------------- 1 | ../exercises/getpwsvr4.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.6: -------------------------------------------------------------------------------- 1 | ../exercises/getpw44bsd.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.7: -------------------------------------------------------------------------------- 1 | ../exercises/prtime.c -------------------------------------------------------------------------------- /apue.3e/figlinks/figC.8: -------------------------------------------------------------------------------- 1 | ../exercises/vfork3.c -------------------------------------------------------------------------------- /apue.3e/filedir/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "linux" 6 | ZAP = zap 7 | else 8 | ZAP = 9 | endif 10 | 11 | 12 | PROGS = access cdpwd changemod devrdev filetype mycd umask unlink $(ZAP) 13 | MOREPROGS = ftw8 14 | 15 | all: $(PROGS) $(MOREPROGS) 16 | 17 | %: %.c $(LIBAPUE) 18 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 19 | 20 | ftw8: ftw8.c $(LIBAPUE) 21 | $(CC) $(CFLAGS) $(NAMEMAX) ftw8.c -o ftw8 $(LDFLAGS) $(LDLIBS) 22 | 23 | clean: 24 | rm -f $(PROGS) $(MOREPROGS) $(TEMPFILES) *.o $(ZAP) 25 | 26 | include $(ROOT)/Make.libapue.inc 27 | -------------------------------------------------------------------------------- /apue.3e/filedir/access.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | if (argc != 2) 8 | err_quit("usage: a.out "); 9 | if (access(argv[1], R_OK) < 0) 10 | err_ret("access error for %s", argv[1]); 11 | else 12 | printf("read access OK\n"); 13 | if (open(argv[1], O_RDONLY) < 0) 14 | err_ret("open error for %s", argv[1]); 15 | else 16 | printf("open for reading OK\n"); 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/filedir/cdpwd.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | char *ptr; 7 | size_t size; 8 | 9 | if (chdir("/usr/spool/uucppublic") < 0) 10 | err_sys("chdir failed"); 11 | 12 | ptr = path_alloc(&size); /* our own function */ 13 | if (getcwd(ptr, size) == NULL) 14 | err_sys("getcwd failed"); 15 | 16 | printf("cwd = %s\n", ptr); 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/filedir/changemod.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | struct stat statbuf; 7 | 8 | /* turn on set-group-ID and turn off group-execute */ 9 | 10 | if (stat("foo", &statbuf) < 0) 11 | err_sys("stat error for foo"); 12 | if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) 13 | err_sys("chmod error for foo"); 14 | 15 | /* set absolute mode to "rw-r--r--" */ 16 | 17 | if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) 18 | err_sys("chmod error for bar"); 19 | 20 | exit(0); 21 | } 22 | -------------------------------------------------------------------------------- /apue.3e/filedir/devrdev.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #ifdef SOLARIS 3 | #include 4 | #endif 5 | 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | int i; 10 | struct stat buf; 11 | 12 | for (i = 1; i < argc; i++) { 13 | printf("%s: ", argv[i]); 14 | if (stat(argv[i], &buf) < 0) { 15 | err_ret("stat error"); 16 | continue; 17 | } 18 | 19 | printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev)); 20 | 21 | if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) { 22 | printf(" (%s) rdev = %d/%d", 23 | (S_ISCHR(buf.st_mode)) ? "character" : "block", 24 | major(buf.st_rdev), minor(buf.st_rdev)); 25 | } 26 | printf("\n"); 27 | } 28 | 29 | exit(0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /apue.3e/filedir/filetype.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int i; 7 | struct stat buf; 8 | char *ptr; 9 | 10 | for (i = 1; i < argc; i++) { 11 | printf("%s: ", argv[i]); 12 | if (lstat(argv[i], &buf) < 0) { 13 | err_ret("lstat error"); 14 | continue; 15 | } 16 | if (S_ISREG(buf.st_mode)) 17 | ptr = "regular"; 18 | else if (S_ISDIR(buf.st_mode)) 19 | ptr = "directory"; 20 | else if (S_ISCHR(buf.st_mode)) 21 | ptr = "character special"; 22 | else if (S_ISBLK(buf.st_mode)) 23 | ptr = "block special"; 24 | else if (S_ISFIFO(buf.st_mode)) 25 | ptr = "fifo"; 26 | else if (S_ISLNK(buf.st_mode)) 27 | ptr = "symbolic link"; 28 | else if (S_ISSOCK(buf.st_mode)) 29 | ptr = "socket"; 30 | else 31 | ptr = "** unknown mode **"; 32 | printf("%s\n", ptr); 33 | } 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /apue.3e/filedir/mycd.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | if (chdir("/tmp") < 0) 7 | err_sys("chdir failed"); 8 | printf("chdir to /tmp succeeded\n"); 9 | exit(0); 10 | } 11 | -------------------------------------------------------------------------------- /apue.3e/filedir/umask.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 5 | 6 | int 7 | main(void) 8 | { 9 | umask(0); 10 | if (creat("foo", RWRWRW) < 0) 11 | err_sys("creat error for foo"); 12 | umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 13 | if (creat("bar", RWRWRW) < 0) 14 | err_sys("creat error for bar"); 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/filedir/unlink.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | if (open("tempfile", O_RDWR) < 0) 8 | err_sys("open error"); 9 | if (unlink("tempfile") < 0) 10 | err_sys("unlink error"); 11 | printf("file unlinked\n"); 12 | sleep(15); 13 | printf("done\n"); 14 | exit(0); 15 | } 16 | -------------------------------------------------------------------------------- /apue.3e/filedir/zap.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | int i, fd; 8 | struct stat statbuf; 9 | struct timespec times[2]; 10 | 11 | for (i = 1; i < argc; i++) { 12 | if (stat(argv[i], &statbuf) < 0) { /* fetch current times */ 13 | err_ret("%s: stat error", argv[i]); 14 | continue; 15 | } 16 | if ((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0) { /* truncate */ 17 | err_ret("%s: open error", argv[i]); 18 | continue; 19 | } 20 | times[0] = statbuf.st_atim; 21 | times[1] = statbuf.st_mtim; 22 | if (futimens(fd, times) < 0) /* reset times */ 23 | err_ret("%s: futimens error", argv[i]); 24 | close(fd); 25 | } 26 | exit(0); 27 | } 28 | -------------------------------------------------------------------------------- /apue.3e/fileio/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = fileflags hole mycat seek 6 | 7 | all: $(PROGS) setfl.o 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o file.hole 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/fileio/hole.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | char buf1[] = "abcdefghij"; 5 | char buf2[] = "ABCDEFGHIJ"; 6 | 7 | int 8 | main(void) 9 | { 10 | int fd; 11 | 12 | if ((fd = creat("file.hole", FILE_MODE)) < 0) 13 | err_sys("creat error"); 14 | 15 | if (write(fd, buf1, 10) != 10) 16 | err_sys("buf1 write error"); 17 | /* offset now = 10 */ 18 | 19 | if (lseek(fd, 16384, SEEK_SET) == -1) 20 | err_sys("lseek error"); 21 | /* offset now = 16384 */ 22 | 23 | if (write(fd, buf2, 10) != 10) 24 | err_sys("buf2 write error"); 25 | /* offset now = 16394 */ 26 | 27 | exit(0); 28 | } 29 | -------------------------------------------------------------------------------- /apue.3e/fileio/mycat.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define BUFFSIZE 4096 4 | 5 | int 6 | main(void) 7 | { 8 | int n; 9 | char buf[BUFFSIZE]; 10 | 11 | while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) 12 | if (write(STDOUT_FILENO, buf, n) != n) 13 | err_sys("write error"); 14 | 15 | if (n < 0) 16 | err_sys("read error"); 17 | 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/fileio/seek.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) 7 | printf("cannot seek\n"); 8 | else 9 | printf("seek OK\n"); 10 | exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /apue.3e/fileio/setfl.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | set_fl(int fd, int flags) /* flags are file status flags to turn on */ 6 | { 7 | int val; 8 | 9 | if ((val = fcntl(fd, F_GETFL, 0)) < 0) 10 | err_sys("fcntl F_GETFL error"); 11 | 12 | val |= flags; /* turn on flags */ 13 | 14 | if (fcntl(fd, F_SETFL, val) < 0) 15 | err_sys("fcntl F_SETFL error"); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/intro/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = getcputc hello ls1 mycat shell1 shell2 testerror uidgid 6 | 7 | all: $(PROGS) 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/intro/getcputc.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int c; 7 | 8 | while ((c = getc(stdin)) != EOF) 9 | if (putc(c, stdout) == EOF) 10 | err_sys("output error"); 11 | 12 | if (ferror(stdin)) 13 | err_sys("input error"); 14 | 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/intro/hello.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | printf("hello world from process ID %ld\n", (long)getpid()); 7 | exit(0); 8 | } 9 | -------------------------------------------------------------------------------- /apue.3e/intro/ls1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | DIR *dp; 8 | struct dirent *dirp; 9 | 10 | if (argc != 2) 11 | err_quit("usage: ls directory_name"); 12 | 13 | if ((dp = opendir(argv[1])) == NULL) 14 | err_sys("can't open %s", argv[1]); 15 | while ((dirp = readdir(dp)) != NULL) 16 | printf("%s\n", dirp->d_name); 17 | 18 | closedir(dp); 19 | exit(0); 20 | } 21 | -------------------------------------------------------------------------------- /apue.3e/intro/mycat.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define BUFFSIZE 4096 4 | 5 | int 6 | main(void) 7 | { 8 | int n; 9 | char buf[BUFFSIZE]; 10 | 11 | while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) 12 | if (write(STDOUT_FILENO, buf, n) != n) 13 | err_sys("write error"); 14 | 15 | if (n < 0) 16 | err_sys("read error"); 17 | 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/intro/shell1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | char buf[MAXLINE]; /* from apue.h */ 8 | pid_t pid; 9 | int status; 10 | 11 | printf("%% "); /* print prompt (printf requires %% to print %) */ 12 | while (fgets(buf, MAXLINE, stdin) != NULL) { 13 | if (buf[strlen(buf) - 1] == '\n') 14 | buf[strlen(buf) - 1] = 0; /* replace newline with null */ 15 | 16 | if ((pid = fork()) < 0) { 17 | err_sys("fork error"); 18 | } else if (pid == 0) { /* child */ 19 | execlp(buf, buf, (char *)0); 20 | err_ret("couldn't execute: %s", buf); 21 | exit(127); 22 | } 23 | 24 | /* parent */ 25 | if ((pid = waitpid(pid, &status, 0)) < 0) 26 | err_sys("waitpid error"); 27 | printf("%% "); 28 | } 29 | exit(0); 30 | } 31 | -------------------------------------------------------------------------------- /apue.3e/intro/testerror.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); 8 | errno = ENOENT; 9 | perror(argv[0]); 10 | exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /apue.3e/intro/uidgid.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | printf("uid = %d, gid = %d\n", getuid(), getgid()); 7 | exit(0); 8 | } 9 | -------------------------------------------------------------------------------- /apue.3e/ipc1/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = add2 add2stdio devzero myuclc pipe1 pipe2 pipe4 popen1 popen2 tshm 6 | 7 | all: $(PROGS) popen.o slock.o tellwait.o 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | slock.o: slock.c slock.h 13 | 14 | clean: 15 | rm -f $(PROGS) $(TEMPFILES) *.o 16 | 17 | include $(ROOT)/Make.libapue.inc 18 | -------------------------------------------------------------------------------- /apue.3e/ipc1/add2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int n, int1, int2; 7 | char line[MAXLINE]; 8 | 9 | while ((n = read(STDIN_FILENO, line, MAXLINE)) > 0) { 10 | line[n] = 0; /* null terminate */ 11 | if (sscanf(line, "%d%d", &int1, &int2) == 2) { 12 | sprintf(line, "%d\n", int1 + int2); 13 | n = strlen(line); 14 | if (write(STDOUT_FILENO, line, n) != n) 15 | err_sys("write error"); 16 | } else { 17 | if (write(STDOUT_FILENO, "invalid args\n", 13) != 13) 18 | err_sys("write error"); 19 | } 20 | } 21 | exit(0); 22 | } 23 | -------------------------------------------------------------------------------- /apue.3e/ipc1/add2stdio.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int int1, int2; 7 | char line[MAXLINE]; 8 | 9 | while (fgets(line, MAXLINE, stdin) != NULL) { 10 | if (sscanf(line, "%d%d", &int1, &int2) == 2) { 11 | if (printf("%d\n", int1 + int2) == EOF) 12 | err_sys("printf error"); 13 | } else { 14 | if (printf("invalid args\n") == EOF) 15 | err_sys("printf error"); 16 | } 17 | } 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/ipc1/myuclc.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | int c; 8 | 9 | while ((c = getchar()) != EOF) { 10 | if (isupper(c)) 11 | c = tolower(c); 12 | if (putchar(c) == EOF) 13 | err_sys("output error"); 14 | if (c == '\n') 15 | fflush(stdout); 16 | } 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/ipc1/pipe1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int n; 7 | int fd[2]; 8 | pid_t pid; 9 | char line[MAXLINE]; 10 | 11 | if (pipe(fd) < 0) 12 | err_sys("pipe error"); 13 | if ((pid = fork()) < 0) { 14 | err_sys("fork error"); 15 | } else if (pid > 0) { /* parent */ 16 | close(fd[0]); 17 | write(fd[1], "hello world\n", 12); 18 | } else { /* child */ 19 | close(fd[1]); 20 | n = read(fd[0], line, MAXLINE); 21 | write(STDOUT_FILENO, line, n); 22 | } 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/ipc1/popen1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | char line[MAXLINE]; 8 | FILE *fpin; 9 | 10 | if ((fpin = popen("myuclc", "r")) == NULL) 11 | err_sys("popen error"); 12 | for ( ; ; ) { 13 | fputs("prompt> ", stdout); 14 | fflush(stdout); 15 | if (fgets(line, MAXLINE, fpin) == NULL) /* read from pipe */ 16 | break; 17 | if (fputs(line, stdout) == EOF) 18 | err_sys("fputs error to pipe"); 19 | } 20 | if (pclose(fpin) == -1) 21 | err_sys("pclose error"); 22 | putchar('\n'); 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/ipc1/popen2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define PAGER "${PAGER:-more}" /* environment variable, or default */ 5 | 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | char line[MAXLINE]; 10 | FILE *fpin, *fpout; 11 | 12 | if (argc != 2) 13 | err_quit("usage: a.out "); 14 | if ((fpin = fopen(argv[1], "r")) == NULL) 15 | err_sys("can't open %s", argv[1]); 16 | 17 | if ((fpout = popen(PAGER, "w")) == NULL) 18 | err_sys("popen error"); 19 | 20 | /* copy argv[1] to pager */ 21 | while (fgets(line, MAXLINE, fpin) != NULL) { 22 | if (fputs(line, fpout) == EOF) 23 | err_sys("fputs error to pipe"); 24 | } 25 | if (ferror(fpin)) 26 | err_sys("fgets error"); 27 | if (pclose(fpout) == -1) 28 | err_sys("pclose error"); 29 | 30 | exit(0); 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/ipc1/slock.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct slock { 7 | sem_t *semp; 8 | char name[_POSIX_NAME_MAX]; 9 | }; 10 | 11 | struct slock * s_alloc(); 12 | void s_free(struct slock *); 13 | int s_lock(struct slock *); 14 | int s_trylock(struct slock *); 15 | int s_unlock(struct slock *); 16 | -------------------------------------------------------------------------------- /apue.3e/ipc1/tellwait.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static int pfd1[2], pfd2[2]; 4 | 5 | void 6 | TELL_WAIT(void) 7 | { 8 | if (pipe(pfd1) < 0 || pipe(pfd2) < 0) 9 | err_sys("pipe error"); 10 | } 11 | 12 | void 13 | TELL_PARENT(pid_t pid) 14 | { 15 | if (write(pfd2[1], "c", 1) != 1) 16 | err_sys("write error"); 17 | } 18 | 19 | void 20 | WAIT_PARENT(void) 21 | { 22 | char c; 23 | 24 | if (read(pfd1[0], &c, 1) != 1) 25 | err_sys("read error"); 26 | 27 | if (c != 'p') 28 | err_quit("WAIT_PARENT: incorrect data"); 29 | } 30 | 31 | void 32 | TELL_CHILD(pid_t pid) 33 | { 34 | if (write(pfd1[1], "p", 1) != 1) 35 | err_sys("write error"); 36 | } 37 | 38 | void 39 | WAIT_CHILD(void) 40 | { 41 | char c; 42 | 43 | if (read(pfd2[0], &c, 1) != 1) 44 | err_sys("read error"); 45 | 46 | if (c != 'c') 47 | err_quit("WAIT_CHILD: incorrect data"); 48 | } 49 | -------------------------------------------------------------------------------- /apue.3e/ipc2/bindunix.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | int 6 | main(void) 7 | { 8 | int fd, size; 9 | struct sockaddr_un un; 10 | 11 | un.sun_family = AF_UNIX; 12 | strcpy(un.sun_path, "foo.socket"); 13 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 14 | err_sys("socket failed"); 15 | size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 16 | if (bind(fd, (struct sockaddr *)&un, size) < 0) 17 | err_sys("bind failed"); 18 | printf("UNIX domain socket bound\n"); 19 | exit(0); 20 | } 21 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open.fe/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=../.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "solaris" 6 | EXTRALIBS=-lsocket -lnsl 7 | endif 8 | 9 | PROGS = openclient 10 | 11 | all: $(PROGS) 12 | 13 | openclient: main.o open.o $(LIBAPUE) 14 | $(CC) $(CFLAGS) -o openclient main.o open.o $(LDFLAGS) $(LDLIBS) 15 | 16 | clean: 17 | rm -f $(PROGS) $(TEMPFILES) *.o 18 | 19 | include $(ROOT)/Make.libapue.inc 20 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open.fe/main.c: -------------------------------------------------------------------------------- 1 | #include "open.h" 2 | #include 3 | 4 | #define BUFFSIZE 8192 5 | 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | int n, fd; 10 | char buf[BUFFSIZE]; 11 | char line[MAXLINE]; 12 | 13 | /* read filename to cat from stdin */ 14 | while (fgets(line, MAXLINE, stdin) != NULL) { 15 | if (line[strlen(line) - 1] == '\n') 16 | line[strlen(line) - 1] = 0; /* replace newline with null */ 17 | 18 | /* open the file */ 19 | if ((fd = csopen(line, O_RDONLY)) < 0) 20 | continue; /* csopen() prints error from server */ 21 | 22 | /* and cat to stdout */ 23 | while ((n = read(fd, buf, BUFFSIZE)) > 0) 24 | if (write(STDOUT_FILENO, buf, n) != n) 25 | err_sys("write error"); 26 | if (n < 0) 27 | err_sys("read error"); 28 | close(fd); 29 | } 30 | 31 | exit(0); 32 | } 33 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open.fe/open.h: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define CL_OPEN "open" /* client's request for server */ 5 | 6 | int csopen(char *, int); 7 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=../.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "solaris" 6 | EXTRALIBS=-lsocket -lnsl 7 | endif 8 | 9 | PROGS = openclient 10 | 11 | all: $(PROGS) 12 | 13 | openclient: main.o open.o $(LIBAPUE) 14 | $(CC) $(CFLAGS) -o openclient main.o open.o $(LDFLAGS) $(LDLIBS) 15 | 16 | clean: 17 | rm -f $(PROGS) $(TEMPFILES) *.o 18 | 19 | include $(ROOT)/Make.libapue.inc 20 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open/main.c: -------------------------------------------------------------------------------- 1 | #include "open.h" 2 | #include 3 | 4 | #define BUFFSIZE 8192 5 | 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | int n, fd; 10 | char buf[BUFFSIZE], line[MAXLINE]; 11 | 12 | /* read filename to cat from stdin */ 13 | while (fgets(line, MAXLINE, stdin) != NULL) { 14 | if (line[strlen(line) - 1] == '\n') 15 | line[strlen(line) - 1] = 0; /* replace newline with null */ 16 | 17 | /* open the file */ 18 | if ((fd = csopen(line, O_RDONLY)) < 0) 19 | continue; /* csopen() prints error from server */ 20 | 21 | /* and cat to stdout */ 22 | while ((n = read(fd, buf, BUFFSIZE)) > 0) 23 | if (write(STDOUT_FILENO, buf, n) != n) 24 | err_sys("write error"); 25 | if (n < 0) 26 | err_sys("read error"); 27 | close(fd); 28 | } 29 | 30 | exit(0); 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/ipc2/open/open.h: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define CL_OPEN "open" /* client's request for server */ 5 | #define CS_OPEN "/tmp/opend.socket" /* server's well-known name */ 6 | 7 | int csopen(char *, int); 8 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend.fe/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=../.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "solaris" 6 | EXTRALIBS=-lsocket -lnsl 7 | endif 8 | 9 | PROGS = opend 10 | 11 | all: $(PROGS) 12 | 13 | opend: main.o request.o cliargs.o $(LIBAPUE) 14 | $(CC) $(CFLAGS) -o opend main.o cliargs.o request.o $(LDFLAGS) $(LDLIBS) 15 | 16 | clean: 17 | rm -f $(PROGS) $(TEMPFILES) *.o 18 | 19 | include $(ROOT)/Make.libapue.inc 20 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend.fe/cliargs.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | 3 | /* 4 | * This function is called by buf_args(), which is called by 5 | * handle_request(). buf_args() has broken up the client's 6 | * buffer into an argv[]-style array, which we now process. 7 | */ 8 | int 9 | cli_args(int argc, char **argv) 10 | { 11 | if (argc != 3 || strcmp(argv[0], CL_OPEN) != 0) { 12 | strcpy(errmsg, "usage: \n"); 13 | return(-1); 14 | } 15 | pathname = argv[1]; /* save ptr to pathname to open */ 16 | oflag = atoi(argv[2]); 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend.fe/main.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | 3 | char errmsg[MAXLINE]; 4 | int oflag; 5 | char *pathname; 6 | 7 | int 8 | main(void) 9 | { 10 | int nread; 11 | char buf[MAXLINE]; 12 | 13 | for ( ; ; ) { /* read arg buffer from client, process request */ 14 | if ((nread = read(STDIN_FILENO, buf, MAXLINE)) < 0) 15 | err_sys("read error on stream pipe"); 16 | else if (nread == 0) 17 | break; /* client has closed the stream pipe */ 18 | handle_request(buf, nread, STDOUT_FILENO); 19 | } 20 | exit(0); 21 | } 22 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend.fe/opend.h: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define CL_OPEN "open" /* client's request for server */ 5 | 6 | extern char errmsg[]; /* error message string to return to client */ 7 | extern int oflag; /* open() flag: O_xxx ... */ 8 | extern char *pathname; /* of file to open() for client */ 9 | 10 | int cli_args(int, char **); 11 | void handle_request(char *, int, int); 12 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend.fe/request.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | #include 3 | 4 | void 5 | handle_request(char *buf, int nread, int fd) 6 | { 7 | int newfd; 8 | 9 | if (buf[nread-1] != 0) { 10 | snprintf(errmsg, MAXLINE-1, 11 | "request not null terminated: %*.*s\n", nread, nread, buf); 12 | send_err(fd, -1, errmsg); 13 | return; 14 | } 15 | if (buf_args(buf, cli_args) < 0) { /* parse args & set options */ 16 | send_err(fd, -1, errmsg); 17 | return; 18 | } 19 | if ((newfd = open(pathname, oflag)) < 0) { 20 | snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", pathname, 21 | strerror(errno)); 22 | send_err(fd, -1, errmsg); 23 | return; 24 | } 25 | if (send_fd(fd, newfd) < 0) /* send the descriptor */ 26 | err_sys("send_fd error"); 27 | close(newfd); /* we're done with descriptor */ 28 | } 29 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=../.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "solaris" 6 | EXTRALIBS=-lsocket -lnsl 7 | endif 8 | 9 | PROGS = opend.poll opend.select 10 | 11 | all: $(PROGS) 12 | 13 | opend.poll: main.o request.o cliargs.o client.o loop.poll.o $(LIBAPUE) 14 | $(CC) $(CFLAGS) -o opend.poll main.o cliargs.o client.o request.o loop.poll.o \ 15 | $(LDFLAGS) $(LDLIBS) 16 | 17 | opend.select: main.o request.o cliargs.o client.o loop.select.o $(LIBAPUE) 18 | $(CC) $(CFLAGS) -o opend.select main.o cliargs.o client.o request.o loop.select.o \ 19 | $(LDFLAGS) $(LDLIBS) 20 | 21 | clean: 22 | rm -f $(PROGS) $(TEMPFILES) *.o 23 | 24 | include $(ROOT)/Make.libapue.inc 25 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend/cliargs.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | 3 | /* 4 | * This function is called by buf_args(), which is called by 5 | * handle_request(). buf_args() has broken up the client's 6 | * buffer into an argv[] style array, which we now process. 7 | */ 8 | int 9 | cli_args(int argc, char **argv) 10 | { 11 | if (argc != 3 || strcmp(argv[0], CL_OPEN) != 0) { 12 | strcpy(errmsg, "usage: \n"); 13 | return(-1); 14 | } 15 | pathname = argv[1]; /* save ptr to pathname to open */ 16 | oflag = atoi(argv[2]); 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend/main.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | #include 3 | 4 | int debug, oflag, client_size, log_to_stderr; 5 | char errmsg[MAXLINE]; 6 | char *pathname; 7 | Client *client = NULL; 8 | 9 | int 10 | main(int argc, char *argv[]) 11 | { 12 | int c; 13 | 14 | log_open("open.serv", LOG_PID, LOG_USER); 15 | 16 | opterr = 0; /* don't want getopt() writing to stderr */ 17 | while ((c = getopt(argc, argv, "d")) != EOF) { 18 | switch (c) { 19 | case 'd': /* debug */ 20 | debug = log_to_stderr = 1; 21 | break; 22 | 23 | case '?': 24 | err_quit("unrecognized option: -%c", optopt); 25 | } 26 | } 27 | 28 | if (debug == 0) 29 | daemonize("opend"); 30 | 31 | loop(); /* never returns */ 32 | } 33 | -------------------------------------------------------------------------------- /apue.3e/ipc2/opend/opend.h: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define CS_OPEN "/tmp/opend.socket" /* well-known name */ 5 | #define CL_OPEN "open" /* client's request for server */ 6 | 7 | extern int debug; /* nonzero if interactive (not daemon) */ 8 | extern char errmsg[]; /* error message string to return to client */ 9 | extern int oflag; /* open flag: O_xxx ... */ 10 | extern char *pathname; /* of file to open for client */ 11 | 12 | typedef struct { /* one Client struct per connected client */ 13 | int fd; /* fd, or -1 if available */ 14 | uid_t uid; 15 | } Client; 16 | 17 | extern Client *client; /* ptr to malloc'ed array */ 18 | extern int client_size; /* # entries in client[] array */ 19 | 20 | int cli_args(int, char **); 21 | int client_add(int, uid_t); 22 | void client_del(int); 23 | void loop(void); 24 | void handle_request(char *, int, int, uid_t); 25 | -------------------------------------------------------------------------------- /apue.3e/ipc2/sendmsg.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define MAXMSZ 512 5 | 6 | struct mymesg { 7 | long mtype; 8 | char mtext[MAXMSZ]; 9 | }; 10 | 11 | int 12 | main(int argc, char *argv[]) 13 | { 14 | key_t key; 15 | long qid; 16 | size_t nbytes; 17 | struct mymesg m; 18 | 19 | if (argc != 3) { 20 | fprintf(stderr, "usage: sendmsg KEY message\n"); 21 | exit(1); 22 | } 23 | key = strtol(argv[1], NULL, 0); 24 | if ((qid = msgget(key, 0)) < 0) 25 | err_sys("can't open queue key %s", argv[1]); 26 | memset(&m, 0, sizeof(m)); 27 | strncpy(m.mtext, argv[2], MAXMSZ-1); 28 | nbytes = strlen(m.mtext); 29 | m.mtype = 1; 30 | if (msgsnd(qid, &m, nbytes, 0) < 0) 31 | err_sys("can't send message"); 32 | exit(0); 33 | } 34 | -------------------------------------------------------------------------------- /apue.3e/lib/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile for misc library. 3 | # 4 | ROOT=.. 5 | PLATFORM=$(shell $(ROOT)/systype.sh) 6 | include $(ROOT)/Make.defines.$(PLATFORM) 7 | 8 | LIBMISC = libapue.a 9 | OBJS = bufargs.o cliconn.o clrfl.o \ 10 | daemonize.o error.o errorlog.o lockreg.o locktest.o \ 11 | openmax.o pathalloc.o popen.o prexit.o prmask.o \ 12 | ptyfork.o ptyopen.o readn.o recvfd.o senderr.o sendfd.o \ 13 | servaccept.o servlisten.o setfd.o setfl.o signal.o signalintr.o \ 14 | sleepus.o spipe.o tellwait.o ttymodes.o writen.o 15 | 16 | all: $(LIBMISC) sleep.o 17 | 18 | $(LIBMISC): $(OBJS) 19 | $(AR) rv $(LIBMISC) $? 20 | $(RANLIB) $(LIBMISC) 21 | 22 | 23 | clean: 24 | rm -f *.o a.out core temp.* $(LIBMISC) 25 | 26 | include $(ROOT)/Make.libapue.inc 27 | -------------------------------------------------------------------------------- /apue.3e/lib/clrfl.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | clr_fl(int fd, int flags) 6 | /* flags are the file status flags to turn off */ 7 | { 8 | int val; 9 | 10 | if ((val = fcntl(fd, F_GETFL, 0)) < 0) 11 | err_sys("fcntl F_GETFL error"); 12 | 13 | val &= ~flags; /* turn flags off */ 14 | 15 | if (fcntl(fd, F_SETFL, val) < 0) 16 | err_sys("fcntl F_SETFL error"); 17 | } 18 | -------------------------------------------------------------------------------- /apue.3e/lib/lockreg.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len) 6 | { 7 | struct flock lock; 8 | 9 | lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */ 10 | lock.l_start = offset; /* byte offset, relative to l_whence */ 11 | lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ 12 | lock.l_len = len; /* #bytes (0 means to EOF) */ 13 | 14 | return(fcntl(fd, cmd, &lock)); 15 | } 16 | -------------------------------------------------------------------------------- /apue.3e/lib/locktest.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | pid_t 5 | lock_test(int fd, int type, off_t offset, int whence, off_t len) 6 | { 7 | struct flock lock; 8 | 9 | lock.l_type = type; /* F_RDLCK or F_WRLCK */ 10 | lock.l_start = offset; /* byte offset, relative to l_whence */ 11 | lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ 12 | lock.l_len = len; /* #bytes (0 means to EOF) */ 13 | 14 | if (fcntl(fd, F_GETLK, &lock) < 0) 15 | err_sys("fcntl error"); 16 | 17 | if (lock.l_type == F_UNLCK) 18 | return(0); /* false, region isn't locked by another proc */ 19 | return(lock.l_pid); /* true, return pid of lock owner */ 20 | } 21 | -------------------------------------------------------------------------------- /apue.3e/lib/nspipe.c: -------------------------------------------------------------------------------- 1 | /* Create a named stream pipe. Called by server on initialization. */ 2 | 3 | #include "apue.h" 4 | #include 5 | #include 6 | 7 | int /* returns 0 if all OK, -1 if error (with errno set) */ 8 | ns_pipe(const char *name, int fd[2]) 9 | { 10 | int len; 11 | struct sockaddr_un unix_addr; 12 | 13 | if (fd_pipe(fd) < 0) /* create unnamed stream pipe */ 14 | return(-1); 15 | 16 | unlink(name); /* remove the name, if it already exists */ 17 | 18 | memset(&unix_addr, 0, sizeof(unix_addr)); 19 | unix_addr.sun_family = AF_UNIX; 20 | strcpy(unix_addr.sun_path, name); 21 | len = strlen(unix_addr.sun_path) + sizeof(unix_addr.sun_family); 22 | 23 | return(bind(fd[0], (struct sockaddr *) &unix_addr, len)); 24 | /* fd[0] has the name bound to it */ 25 | } 26 | -------------------------------------------------------------------------------- /apue.3e/lib/openmax.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #ifdef OPEN_MAX 6 | static long openmax = OPEN_MAX; 7 | #else 8 | static long openmax = 0; 9 | #endif 10 | 11 | /* 12 | * If OPEN_MAX is indeterminate, this might be inadequate. 13 | */ 14 | #define OPEN_MAX_GUESS 256 15 | 16 | long 17 | open_max(void) 18 | { 19 | if (openmax == 0) { /* first time through */ 20 | errno = 0; 21 | if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) { 22 | if (errno == 0) 23 | openmax = OPEN_MAX_GUESS; /* it's indeterminate */ 24 | else 25 | err_sys("sysconf error for _SC_OPEN_MAX"); 26 | } 27 | } 28 | return(openmax); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/lib/prexit.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | pr_exit(int status) 6 | { 7 | if (WIFEXITED(status)) 8 | printf("normal termination, exit status = %d\n", 9 | WEXITSTATUS(status)); 10 | else if (WIFSIGNALED(status)) 11 | printf("abnormal termination, signal number = %d%s\n", 12 | WTERMSIG(status), 13 | #ifdef WCOREDUMP 14 | WCOREDUMP(status) ? " (core file generated)" : ""); 15 | #else 16 | ""); 17 | #endif 18 | else if (WIFSTOPPED(status)) 19 | printf("child stopped, signal number = %d\n", 20 | WSTOPSIG(status)); 21 | } 22 | -------------------------------------------------------------------------------- /apue.3e/lib/prmask.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | pr_mask(const char *str) 6 | { 7 | sigset_t sigset; 8 | int errno_save; 9 | 10 | errno_save = errno; /* we can be called by signal handlers */ 11 | if (sigprocmask(0, NULL, &sigset) < 0) { 12 | err_ret("sigprocmask error"); 13 | } else { 14 | printf("%s", str); 15 | if (sigismember(&sigset, SIGINT)) 16 | printf(" SIGINT"); 17 | if (sigismember(&sigset, SIGQUIT)) 18 | printf(" SIGQUIT"); 19 | if (sigismember(&sigset, SIGUSR1)) 20 | printf(" SIGUSR1"); 21 | if (sigismember(&sigset, SIGALRM)) 22 | printf(" SIGALRM"); 23 | 24 | /* remaining signals can go here */ 25 | 26 | printf("\n"); 27 | } 28 | 29 | errno = errno_save; /* restore errno */ 30 | } 31 | -------------------------------------------------------------------------------- /apue.3e/lib/readn.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | ssize_t /* Read "n" bytes from a descriptor */ 4 | readn(int fd, void *ptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nread; 8 | 9 | nleft = n; 10 | while (nleft > 0) { 11 | if ((nread = read(fd, ptr, nleft)) < 0) { 12 | if (nleft == n) 13 | return(-1); /* error, return -1 */ 14 | else 15 | break; /* error, return amount read so far */ 16 | } else if (nread == 0) { 17 | break; /* EOF */ 18 | } 19 | nleft -= nread; 20 | ptr += nread; 21 | } 22 | return(n - nleft); /* return >= 0 */ 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/lib/senderr.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | /* 4 | * Used when we had planned to send an fd using send_fd(), 5 | * but encountered an error instead. We send the error back 6 | * using the send_fd()/recv_fd() protocol. 7 | */ 8 | int 9 | send_err(int fd, int errcode, const char *msg) 10 | { 11 | int n; 12 | 13 | if ((n = strlen(msg)) > 0) 14 | if (writen(fd, msg, n) != n) /* send the error message */ 15 | return(-1); 16 | 17 | if (errcode >= 0) 18 | errcode = -1; /* must be negative */ 19 | 20 | if (send_fd(fd, errcode) < 0) 21 | return(-1); 22 | 23 | return(0); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/lib/setfd.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | set_cloexec(int fd) 6 | { 7 | int val; 8 | 9 | if ((val = fcntl(fd, F_GETFD, 0)) < 0) 10 | return(-1); 11 | 12 | val |= FD_CLOEXEC; /* enable close-on-exec */ 13 | 14 | return(fcntl(fd, F_SETFD, val)); 15 | } 16 | -------------------------------------------------------------------------------- /apue.3e/lib/setfl.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | set_fl(int fd, int flags) /* flags are file status flags to turn on */ 6 | { 7 | int val; 8 | 9 | if ((val = fcntl(fd, F_GETFL, 0)) < 0) 10 | err_sys("fcntl F_GETFL error"); 11 | 12 | val |= flags; /* turn on flags */ 13 | 14 | if (fcntl(fd, F_SETFL, val) < 0) 15 | err_sys("fcntl F_SETFL error"); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/lib/signal.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | /* Reliable version of signal(), using POSIX sigaction(). */ 4 | Sigfunc * 5 | signal(int signo, Sigfunc *func) 6 | { 7 | struct sigaction act, oact; 8 | 9 | act.sa_handler = func; 10 | sigemptyset(&act.sa_mask); 11 | act.sa_flags = 0; 12 | if (signo == SIGALRM) { 13 | #ifdef SA_INTERRUPT 14 | act.sa_flags |= SA_INTERRUPT; 15 | #endif 16 | } else { 17 | act.sa_flags |= SA_RESTART; 18 | } 19 | if (sigaction(signo, &act, &oact) < 0) 20 | return(SIG_ERR); 21 | return(oact.sa_handler); 22 | } 23 | -------------------------------------------------------------------------------- /apue.3e/lib/signalintr.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | Sigfunc * 4 | signal_intr(int signo, Sigfunc *func) 5 | { 6 | struct sigaction act, oact; 7 | 8 | act.sa_handler = func; 9 | sigemptyset(&act.sa_mask); 10 | act.sa_flags = 0; 11 | #ifdef SA_INTERRUPT 12 | act.sa_flags |= SA_INTERRUPT; 13 | #endif 14 | if (sigaction(signo, &act, &oact) < 0) 15 | return(SIG_ERR); 16 | return(oact.sa_handler); 17 | } 18 | -------------------------------------------------------------------------------- /apue.3e/lib/sleepus.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | sleep_us(unsigned int nusecs) 6 | { 7 | struct timeval tval; 8 | 9 | tval.tv_sec = nusecs / 1000000; 10 | tval.tv_usec = nusecs % 1000000; 11 | select(0, NULL, NULL, NULL, &tval); 12 | } 13 | -------------------------------------------------------------------------------- /apue.3e/lib/spipe.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | /* 5 | * Returns a full-duplex pipe (a UNIX domain socket) with 6 | * the two file descriptors returned in fd[0] and fd[1]. 7 | */ 8 | int 9 | fd_pipe(int fd[2]) 10 | { 11 | return(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)); 12 | } 13 | -------------------------------------------------------------------------------- /apue.3e/lib/strerror.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef LINUX 4 | extern char *sys_errlist[]; 5 | extern int sys_nerr; 6 | #endif 7 | 8 | char * 9 | strerror(int error) 10 | { 11 | static char mesg[30]; 12 | 13 | if (error >= 0 && error <= sys_nerr) 14 | return((char *)sys_errlist[error]); 15 | 16 | sprintf(mesg, "Unknown error (%d)", error); 17 | return(mesg); 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/lib/writen.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | ssize_t /* Write "n" bytes to a descriptor */ 4 | writen(int fd, const void *ptr, size_t n) 5 | { 6 | size_t nleft; 7 | ssize_t nwritten; 8 | 9 | nleft = n; 10 | while (nleft > 0) { 11 | if ((nwritten = write(fd, ptr, nleft)) < 0) { 12 | if (nleft == n) 13 | return(-1); /* error, return -1 */ 14 | else 15 | break; /* error, return amount written so far */ 16 | } else if (nwritten == 0) { 17 | break; 18 | } 19 | nleft -= nwritten; 20 | ptr += nwritten; 21 | } 22 | return(n - nleft); /* return >= 0 */ 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/proc/awkexample: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | # Note: on Solaris, use nawk instead 3 | BEGIN { 4 | for (i = 0; i < ARGC; i++) 5 | printf "ARGV[%d] = %s\n", i, ARGV[i] 6 | exit 7 | } 8 | -------------------------------------------------------------------------------- /apue.3e/proc/echoall.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int i; 7 | char **ptr; 8 | extern char **environ; 9 | 10 | for (i = 0; i < argc; i++) /* echo all command-line args */ 11 | printf("argv[%d]: %s\n", i, argv[i]); 12 | 13 | for (ptr = environ; *ptr != 0; ptr++) /* and all env strings */ 14 | printf("%s\n", *ptr); 15 | 16 | exit(0); 17 | } 18 | -------------------------------------------------------------------------------- /apue.3e/proc/exec1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL }; 5 | 6 | int 7 | main(void) 8 | { 9 | pid_t pid; 10 | 11 | if ((pid = fork()) < 0) { 12 | err_sys("fork error"); 13 | } else if (pid == 0) { /* specify pathname, specify environment */ 14 | if (execle("/home/sar/bin/echoall", "echoall", "myarg1", 15 | "MY ARG2", (char *)0, env_init) < 0) 16 | err_sys("execle error"); 17 | } 18 | 19 | if (waitpid(pid, NULL, 0) < 0) 20 | err_sys("wait error"); 21 | 22 | if ((pid = fork()) < 0) { 23 | err_sys("fork error"); 24 | } else if (pid == 0) { /* specify filename, inherit environment */ 25 | if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0) 26 | err_sys("execlp error"); 27 | } 28 | 29 | exit(0); 30 | } 31 | -------------------------------------------------------------------------------- /apue.3e/proc/exec2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | pid_t pid; 8 | 9 | if ((pid = fork()) < 0) { 10 | err_sys("fork error"); 11 | } else if (pid == 0) { /* child */ 12 | if (execl("/home/sar/bin/testinterp", 13 | "testinterp", "myarg1", "MY ARG2", (char *)0) < 0) 14 | err_sys("execl error"); 15 | } 16 | if (waitpid(pid, NULL, 0) < 0) /* parent */ 17 | err_sys("waitpid error"); 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/proc/fork1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int globvar = 6; /* external variable in initialized data */ 4 | char buf[] = "a write to stdout\n"; 5 | 6 | int 7 | main(void) 8 | { 9 | int var; /* automatic variable on the stack */ 10 | pid_t pid; 11 | 12 | var = 88; 13 | if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1) 14 | err_sys("write error"); 15 | printf("before fork\n"); /* we don't flush stdout */ 16 | 17 | if ((pid = fork()) < 0) { 18 | err_sys("fork error"); 19 | } else if (pid == 0) { /* child */ 20 | globvar++; /* modify variables */ 21 | var++; 22 | } else { 23 | sleep(2); /* parent */ 24 | } 25 | 26 | printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar, 27 | var); 28 | exit(0); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/proc/pruids.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | printf("real uid = %d, effective uid = %d\n", getuid(), geteuid()); 7 | exit(0); 8 | } 9 | -------------------------------------------------------------------------------- /apue.3e/proc/system.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int 6 | system(const char *cmdstring) /* version without signal handling */ 7 | { 8 | pid_t pid; 9 | int status; 10 | 11 | if (cmdstring == NULL) 12 | return(1); /* always a command processor with UNIX */ 13 | 14 | if ((pid = fork()) < 0) { 15 | status = -1; /* probably out of processes */ 16 | } else if (pid == 0) { /* child */ 17 | execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); 18 | _exit(127); /* execl error */ 19 | } else { /* parent */ 20 | while (waitpid(pid, &status, 0) < 0) { 21 | if (errno != EINTR) { 22 | status = -1; /* error other than EINTR from waitpid() */ 23 | break; 24 | } 25 | } 26 | } 27 | 28 | return(status); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/proc/systest1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | int status; 8 | 9 | if ((status = system("date")) < 0) 10 | err_sys("system() error"); 11 | 12 | pr_exit(status); 13 | 14 | if ((status = system("nosuchcommand")) < 0) 15 | err_sys("system() error"); 16 | 17 | pr_exit(status); 18 | 19 | if ((status = system("who; exit 44")) < 0) 20 | err_sys("system() error"); 21 | 22 | pr_exit(status); 23 | 24 | exit(0); 25 | } 26 | -------------------------------------------------------------------------------- /apue.3e/proc/systest3.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int status; 7 | 8 | if (argc < 2) 9 | err_quit("command-line argument required"); 10 | 11 | if ((status = system(argv[1])) < 0) 12 | err_sys("system() error"); 13 | 14 | pr_exit(status); 15 | 16 | exit(0); 17 | } 18 | -------------------------------------------------------------------------------- /apue.3e/proc/tellwait1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void charatatime(char *); 4 | 5 | int 6 | main(void) 7 | { 8 | pid_t pid; 9 | 10 | if ((pid = fork()) < 0) { 11 | err_sys("fork error"); 12 | } else if (pid == 0) { 13 | charatatime("output from child\n"); 14 | } else { 15 | charatatime("output from parent\n"); 16 | } 17 | exit(0); 18 | } 19 | 20 | static void 21 | charatatime(char *str) 22 | { 23 | char *ptr; 24 | int c; 25 | 26 | setbuf(stdout, NULL); /* set unbuffered */ 27 | for (ptr = str; (c = *ptr++) != 0; ) 28 | putc(c, stdout); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/proc/tellwait2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void charatatime(char *); 4 | 5 | int 6 | main(void) 7 | { 8 | pid_t pid; 9 | 10 | TELL_WAIT(); 11 | 12 | if ((pid = fork()) < 0) { 13 | err_sys("fork error"); 14 | } else if (pid == 0) { 15 | WAIT_PARENT(); /* parent goes first */ 16 | charatatime("output from child\n"); 17 | } else { 18 | charatatime("output from parent\n"); 19 | TELL_CHILD(pid); 20 | } 21 | exit(0); 22 | } 23 | 24 | static void 25 | charatatime(char *str) 26 | { 27 | char *ptr; 28 | int c; 29 | 30 | setbuf(stdout, NULL); /* set unbuffered */ 31 | for (ptr = str; (c = *ptr++) != 0; ) 32 | putc(c, stdout); 33 | } 34 | -------------------------------------------------------------------------------- /apue.3e/proc/vfork1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int globvar = 6; /* external variable in initialized data */ 4 | 5 | int 6 | main(void) 7 | { 8 | int var; /* automatic variable on the stack */ 9 | pid_t pid; 10 | 11 | var = 88; 12 | printf("before vfork\n"); /* we don't flush stdio */ 13 | if ((pid = vfork()) < 0) { 14 | err_sys("vfork error"); 15 | } else if (pid == 0) { /* child */ 16 | globvar++; /* modify parent's variables */ 17 | var++; 18 | _exit(0); /* child terminates */ 19 | } 20 | 21 | /* parent continues here */ 22 | printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar, 23 | var); 24 | exit(0); 25 | } 26 | -------------------------------------------------------------------------------- /apue.3e/pty/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "solaris" 6 | EXTRALIBS=-lsocket -lnsl 7 | endif 8 | 9 | PROGS = pty 10 | 11 | all: $(PROGS) 12 | 13 | pty: main.o loop.o driver.o $(LIBAPUE) 14 | $(CC) $(CFLAGS) -o pty main.o loop.o driver.o $(LDFLAGS) $(LDLIBS) 15 | 16 | clean: 17 | rm -f $(PROGS) $(TEMPFILES) *.o 18 | 19 | include $(ROOT)/Make.libapue.inc 20 | -------------------------------------------------------------------------------- /apue.3e/relation/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = orphan3 6 | 7 | all: $(PROGS) 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/signals/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | CLD = 6 | 7 | ifeq "$(PLATFORM)" "linux" 8 | CLD = child 9 | endif 10 | ifeq "$(PLATFORM)" "solaris" 11 | CLD = child 12 | endif 13 | 14 | PROGS = critical mask read1 read2 reenter sigtstp sigusr suspend1 suspend2 15 | MOREPROGS = systest2 tsleep2 16 | 17 | all: $(PROGS) $(MOREPROGS) abort.o sleep1.o sleep2.o system.o $(CLD) 18 | 19 | %: %.c $(LIBAPUE) 20 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 21 | 22 | tsleep2: tsleep2.o sleep2.o $(LIBAPUE) 23 | $(CC) $(CFLAGS) -o tsleep2 tsleep2.o sleep2.o $(LDFLAGS) $(LDLIBS) 24 | 25 | systest2: systest2.o system.o $(LIBAPUE) 26 | $(CC) $(CFLAGS) -o systest2 systest2.o system.o $(LDFLAGS) $(LDLIBS) 27 | 28 | clean: 29 | rm -f $(PROGS) $(MOREPROGS) $(TEMPFILES) *.o file.hole $(CLD) 30 | 31 | include $(ROOT)/Make.libapue.inc 32 | -------------------------------------------------------------------------------- /apue.3e/signals/child.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void sig_cld(int); 5 | 6 | int 7 | main() 8 | { 9 | pid_t pid; 10 | 11 | if (signal(SIGCLD, sig_cld) == SIG_ERR) 12 | perror("signal error"); 13 | if ((pid = fork()) < 0) { 14 | perror("fork error"); 15 | } else if (pid == 0) { /* child */ 16 | sleep(2); 17 | _exit(0); 18 | } 19 | 20 | pause(); /* parent */ 21 | exit(0); 22 | } 23 | 24 | static void 25 | sig_cld(int signo) /* interrupts pause() */ 26 | { 27 | pid_t pid; 28 | int status; 29 | 30 | printf("SIGCLD received\n"); 31 | 32 | if (signal(SIGCLD, sig_cld) == SIG_ERR) /* reestablish handler */ 33 | perror("signal error"); 34 | 35 | if ((pid = wait(&status)) < 0) /* fetch child status */ 36 | perror("wait error"); 37 | 38 | printf("pid = %d\n", pid); 39 | } 40 | -------------------------------------------------------------------------------- /apue.3e/signals/read1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void sig_alrm(int); 4 | 5 | int 6 | main(void) 7 | { 8 | int n; 9 | char line[MAXLINE]; 10 | 11 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 12 | err_sys("signal(SIGALRM) error"); 13 | 14 | alarm(10); 15 | if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0) 16 | err_sys("read error"); 17 | alarm(0); 18 | 19 | write(STDOUT_FILENO, line, n); 20 | exit(0); 21 | } 22 | 23 | static void 24 | sig_alrm(int signo) 25 | { 26 | /* nothing to do, just return to interrupt the read */ 27 | } 28 | -------------------------------------------------------------------------------- /apue.3e/signals/read2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void sig_alrm(int); 5 | static jmp_buf env_alrm; 6 | 7 | int 8 | main(void) 9 | { 10 | int n; 11 | char line[MAXLINE]; 12 | 13 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 14 | err_sys("signal(SIGALRM) error"); 15 | if (setjmp(env_alrm) != 0) 16 | err_quit("read timeout"); 17 | 18 | alarm(10); 19 | if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0) 20 | err_sys("read error"); 21 | alarm(0); 22 | 23 | write(STDOUT_FILENO, line, n); 24 | exit(0); 25 | } 26 | 27 | static void 28 | sig_alrm(int signo) 29 | { 30 | longjmp(env_alrm, 1); 31 | } 32 | -------------------------------------------------------------------------------- /apue.3e/signals/reenter.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void 5 | my_alarm(int signo) 6 | { 7 | struct passwd *rootptr; 8 | 9 | printf("in signal handler\n"); 10 | if ((rootptr = getpwnam("root")) == NULL) 11 | err_sys("getpwnam(root) error"); 12 | alarm(1); 13 | } 14 | 15 | int 16 | main(void) 17 | { 18 | struct passwd *ptr; 19 | 20 | signal(SIGALRM, my_alarm); 21 | alarm(1); 22 | for ( ; ; ) { 23 | if ((ptr = getpwnam("sar")) == NULL) 24 | err_sys("getpwnam error"); 25 | if (strcmp(ptr->pw_name, "sar") != 0) 26 | printf("return value corrupted!, pw_name = %s\n", 27 | ptr->pw_name); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/signals/setops.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | * usually defines NSIG to include signal number 0. 6 | */ 7 | #define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG) 8 | 9 | int 10 | sigaddset(sigset_t *set, int signo) 11 | { 12 | if (SIGBAD(signo)) { 13 | errno = EINVAL; 14 | return(-1); 15 | } 16 | *set |= 1 << (signo - 1); /* turn bit on */ 17 | return(0); 18 | } 19 | 20 | int 21 | sigdelset(sigset_t *set, int signo) 22 | { 23 | if (SIGBAD(signo)) { 24 | errno = EINVAL; 25 | return(-1); 26 | } 27 | *set &= ~(1 << (signo - 1)); /* turn bit off */ 28 | return(0); 29 | } 30 | 31 | int 32 | sigismember(const sigset_t *set, int signo) 33 | { 34 | if (SIGBAD(signo)) { 35 | errno = EINVAL; 36 | return(-1); 37 | } 38 | return((*set & (1 << (signo - 1))) != 0); 39 | } 40 | -------------------------------------------------------------------------------- /apue.3e/signals/sigusr.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void sig_usr(int); /* one handler for both signals */ 4 | 5 | int 6 | main(void) 7 | { 8 | if (signal(SIGUSR1, sig_usr) == SIG_ERR) 9 | err_sys("can't catch SIGUSR1"); 10 | if (signal(SIGUSR2, sig_usr) == SIG_ERR) 11 | err_sys("can't catch SIGUSR2"); 12 | for ( ; ; ) 13 | pause(); 14 | } 15 | 16 | static void 17 | sig_usr(int signo) /* argument is signal number */ 18 | { 19 | if (signo == SIGUSR1) 20 | printf("received SIGUSR1\n"); 21 | else if (signo == SIGUSR2) 22 | printf("received SIGUSR2\n"); 23 | else 24 | err_dump("received signal %d\n", signo); 25 | } 26 | -------------------------------------------------------------------------------- /apue.3e/signals/sleep1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void 5 | sig_alrm(int signo) 6 | { 7 | /* nothing to do, just return to wake up the pause */ 8 | } 9 | 10 | unsigned int 11 | sleep1(unsigned int seconds) 12 | { 13 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 14 | return(seconds); 15 | alarm(seconds); /* start the timer */ 16 | pause(); /* next caught signal wakes us up */ 17 | return(alarm(0)); /* turn off timer, return unslept time */ 18 | } 19 | -------------------------------------------------------------------------------- /apue.3e/signals/sleep2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static jmp_buf env_alrm; 6 | 7 | static void 8 | sig_alrm(int signo) 9 | { 10 | longjmp(env_alrm, 1); 11 | } 12 | 13 | unsigned int 14 | sleep2(unsigned int seconds) 15 | { 16 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 17 | return(seconds); 18 | if (setjmp(env_alrm) == 0) { 19 | alarm(seconds); /* start the timer */ 20 | pause(); /* next caught signal wakes us up */ 21 | } 22 | return(alarm(0)); /* turn off timer, return unslept time */ 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/signals/systest2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void 4 | sig_int(int signo) 5 | { 6 | printf("caught SIGINT\n"); 7 | } 8 | 9 | static void 10 | sig_chld(int signo) 11 | { 12 | printf("caught SIGCHLD\n"); 13 | } 14 | 15 | int 16 | main(void) 17 | { 18 | if (signal(SIGINT, sig_int) == SIG_ERR) 19 | err_sys("signal(SIGINT) error"); 20 | if (signal(SIGCHLD, sig_chld) == SIG_ERR) 21 | err_sys("signal(SIGCHLD) error"); 22 | if (system("/bin/ed") < 0) 23 | err_sys("system() error"); 24 | exit(0); 25 | } 26 | -------------------------------------------------------------------------------- /apue.3e/signals/tsleep2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | unsigned int sleep2(unsigned int); 4 | static void sig_int(int); 5 | 6 | int 7 | main(void) 8 | { 9 | unsigned int unslept; 10 | 11 | if (signal(SIGINT, sig_int) == SIG_ERR) 12 | err_sys("signal(SIGINT) error"); 13 | unslept = sleep2(5); 14 | printf("sleep2 returned: %u\n", unslept); 15 | exit(0); 16 | } 17 | 18 | static void 19 | sig_int(int signo) 20 | { 21 | int i, j; 22 | volatile int k; 23 | 24 | /* 25 | * Tune these loops to run for more than 5 seconds 26 | * on whatever system this test program is run. 27 | */ 28 | printf("\nsig_int starting\n"); 29 | for (i = 0; i < 300000; i++) 30 | for (j = 0; j < 4000; j++) 31 | k += i * j; 32 | printf("sig_int finished\n"); 33 | } 34 | -------------------------------------------------------------------------------- /apue.3e/sockets/clconn.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define MAXSLEEP 128 5 | 6 | int 7 | connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen) 8 | { 9 | int numsec; 10 | 11 | /* 12 | * Try to connect with exponential backoff. 13 | */ 14 | for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) { 15 | if (connect(sockfd, addr, alen) == 0) { 16 | /* 17 | * Connection accepted. 18 | */ 19 | return(0); 20 | } 21 | 22 | /* 23 | * Delay before trying again. 24 | */ 25 | if (numsec <= MAXSLEEP/2) 26 | sleep(numsec); 27 | } 28 | return(-1); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/sockets/clconn2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define MAXSLEEP 128 5 | 6 | int 7 | connect_retry(int domain, int type, int protocol, 8 | const struct sockaddr *addr, socklen_t alen) 9 | { 10 | int numsec, fd; 11 | 12 | /* 13 | * Try to connect with exponential backoff. 14 | */ 15 | for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) { 16 | if ((fd = socket(domain, type, protocol)) < 0) 17 | return(-1); 18 | if (connect(fd, addr, alen) == 0) { 19 | /* 20 | * Connection accepted. 21 | */ 22 | return(fd); 23 | } 24 | close(fd); 25 | 26 | /* 27 | * Delay before trying again. 28 | */ 29 | if (numsec <= MAXSLEEP/2) 30 | sleep(numsec); 31 | } 32 | return(-1); 33 | } 34 | -------------------------------------------------------------------------------- /apue.3e/sockets/initsrv1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | int 6 | initserver(int type, const struct sockaddr *addr, socklen_t alen, 7 | int qlen) 8 | { 9 | int fd; 10 | int err = 0; 11 | 12 | if ((fd = socket(addr->sa_family, type, 0)) < 0) 13 | return(-1); 14 | if (bind(fd, addr, alen) < 0) 15 | goto errout; 16 | if (type == SOCK_STREAM || type == SOCK_SEQPACKET) { 17 | if (listen(fd, qlen) < 0) 18 | goto errout; 19 | } 20 | return(fd); 21 | 22 | errout: 23 | err = errno; 24 | close(fd); 25 | errno = err; 26 | return(-1); 27 | } 28 | -------------------------------------------------------------------------------- /apue.3e/sockets/initsrv2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | int 6 | initserver(int type, const struct sockaddr *addr, socklen_t alen, 7 | int qlen) 8 | { 9 | int fd, err; 10 | int reuse = 1; 11 | 12 | if ((fd = socket(addr->sa_family, type, 0)) < 0) 13 | return(-1); 14 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, 15 | sizeof(int)) < 0) 16 | goto errout; 17 | if (bind(fd, addr, alen) < 0) 18 | goto errout; 19 | if (type == SOCK_STREAM || type == SOCK_SEQPACKET) 20 | if (listen(fd, qlen) < 0) 21 | goto errout; 22 | return(fd); 23 | 24 | errout: 25 | err = errno; 26 | close(fd); 27 | errno = err; 28 | return(-1); 29 | } 30 | -------------------------------------------------------------------------------- /apue.3e/standards/pathconf-lim.sym: -------------------------------------------------------------------------------- 1 | FILESIZEBITS _PC_FILESIZEBITS 2 | LINK_MAX _PC_LINK_MAX 3 | MAX_CANON _PC_MAX_CANON 4 | MAX_INPUT _PC_MAX_INPUT 5 | NAME_MAX _PC_NAME_MAX 6 | PATH_MAX _PC_PATH_MAX 7 | PIPE_BUF _PC_PIPE_BUF 8 | SYMLINK_MAX _PC_SYMLINK_MAX 9 | _POSIX_TIMESTAMP_RESOLUTION _PC_TIMESTAMP_RESOLUTION 10 | -------------------------------------------------------------------------------- /apue.3e/standards/pathconf-opt.sym: -------------------------------------------------------------------------------- 1 | _POSIX_CHOWN_RESTRICTED _PC_CHOWN_RESTRICTED 2 | _POSIX_NO_TRUNC _PC_NO_TRUNC 3 | _POSIX_VDISABLE _PC_VDISABLE 4 | _POSIX_ASYNC_IO _PC_ASYNC_IO 5 | _POSIX_PRIO_IO _PC_PRIO_IO 6 | _POSIX_SYNC_IO _PC_SYNC_IO 7 | _POSIX2_SYMLINKS _PC_2_SYMLINKS 8 | -------------------------------------------------------------------------------- /apue.3e/stdio/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "linux" 6 | MEMSTR = memstr 7 | else 8 | MEMSTR = 9 | endif 10 | 11 | PROGS = buf fgetsfputs getcharbug getcputc mkstemp tempfiles 12 | 13 | all: $(PROGS) $(MEMSTR) 14 | 15 | %: %.c $(LIBAPUE) 16 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 17 | 18 | clean: 19 | rm -f $(PROGS) $(TEMPFILES) *.o $(MEMSTR) 20 | 21 | include $(ROOT)/Make.libapue.inc 22 | -------------------------------------------------------------------------------- /apue.3e/stdio/fgetsfputs.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | char buf[MAXLINE]; 7 | 8 | while (fgets(buf, MAXLINE, stdin) != NULL) 9 | if (fputs(buf, stdout) == EOF) 10 | err_sys("output error"); 11 | 12 | if (ferror(stdin)) 13 | err_sys("input error"); 14 | 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/stdio/getcharbug.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | main(void) 5 | { 6 | char c; 7 | 8 | while ((c = getchar()) != EOF) 9 | putchar(c); 10 | } 11 | -------------------------------------------------------------------------------- /apue.3e/stdio/getcputc.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int c; 7 | 8 | while ((c = getc(stdin)) != EOF) 9 | if (putc(c, stdout) == EOF) 10 | err_sys("output error"); 11 | 12 | if (ferror(stdin)) 13 | err_sys("input error"); 14 | 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /apue.3e/stdio/tempfiles.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | char name[L_tmpnam], line[MAXLINE]; 7 | FILE *fp; 8 | 9 | printf("%s\n", tmpnam(NULL)); /* first temp name */ 10 | 11 | tmpnam(name); /* second temp name */ 12 | printf("%s\n", name); 13 | 14 | if ((fp = tmpfile()) == NULL) /* create temp file */ 15 | err_sys("tmpfile error"); 16 | fputs("one line of output\n", fp); /* write to temp file */ 17 | rewind(fp); /* then read it back */ 18 | if (fgets(line, sizeof(line), fp) == NULL) 19 | err_sys("fgets error"); 20 | fputs(line, stdout); /* print the line we wrote */ 21 | 22 | exit(0); 23 | } 24 | -------------------------------------------------------------------------------- /apue.3e/systype.sh: -------------------------------------------------------------------------------- 1 | # (leading space required for Xenix /bin/sh) 2 | 3 | # 4 | # Determine the type of *ix operating system that we're 5 | # running on, and echo an appropriate value. 6 | # This script is intended to be used in Makefiles. 7 | # (This is a kludge. Gotta be a better way.) 8 | # 9 | 10 | case `uname -s` in 11 | "FreeBSD") 12 | PLATFORM="freebsd" 13 | ;; 14 | "Linux") 15 | PLATFORM="linux" 16 | ;; 17 | "Darwin") 18 | PLATFORM="macos" 19 | ;; 20 | "SunOS") 21 | PLATFORM="solaris" 22 | ;; 23 | *) 24 | echo "Unknown platform" >&2 25 | exit 1 26 | esac 27 | echo $PLATFORM 28 | exit 0 29 | -------------------------------------------------------------------------------- /apue.3e/termios/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = csize settty t_getpass t_isatty t_raw t_ttyname winch 6 | 7 | all: $(PROGS) ctermid.o getpass.o isatty.o ttyname.o 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | clean: 13 | rm -f $(PROGS) $(TEMPFILES) *.o 14 | 15 | include $(ROOT)/Make.libapue.inc 16 | -------------------------------------------------------------------------------- /apue.3e/termios/csize.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | struct termios term; 8 | 9 | if (tcgetattr(STDIN_FILENO, &term) < 0) 10 | err_sys("tcgetattr error"); 11 | 12 | switch (term.c_cflag & CSIZE) { 13 | case CS5: 14 | printf("5 bits/byte\n"); 15 | break; 16 | case CS6: 17 | printf("6 bits/byte\n"); 18 | break; 19 | case CS7: 20 | printf("7 bits/byte\n"); 21 | break; 22 | case CS8: 23 | printf("8 bits/byte\n"); 24 | break; 25 | default: 26 | printf("unknown bits/byte\n"); 27 | } 28 | 29 | term.c_cflag &= ~CSIZE; /* zero out the bits */ 30 | term.c_cflag |= CS8; /* set 8 bits/byte */ 31 | if (tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) 32 | err_sys("tcsetattr error"); 33 | 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /apue.3e/termios/ctermid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static char ctermid_name[L_ctermid]; 5 | 6 | char * 7 | ctermid(char *str) 8 | { 9 | if (str == NULL) 10 | str = ctermid_name; 11 | return(strcpy(str, "/dev/tty")); /* strcpy() returns str */ 12 | } 13 | -------------------------------------------------------------------------------- /apue.3e/termios/isatty.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int 4 | isatty(int fd) 5 | { 6 | struct termios ts; 7 | 8 | return(tcgetattr(fd, &ts) != -1); /* true if no error (is a tty) */ 9 | } 10 | -------------------------------------------------------------------------------- /apue.3e/termios/settty.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | struct termios term; 8 | long vdisable; 9 | 10 | if (isatty(STDIN_FILENO) == 0) 11 | err_quit("standard input is not a terminal device"); 12 | 13 | if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) < 0) 14 | err_quit("fpathconf error or _POSIX_VDISABLE not in effect"); 15 | 16 | if (tcgetattr(STDIN_FILENO, &term) < 0) /* fetch tty state */ 17 | err_sys("tcgetattr error"); 18 | 19 | term.c_cc[VINTR] = vdisable; /* disable INTR character */ 20 | term.c_cc[VEOF] = 2; /* EOF is Control-B */ 21 | 22 | if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) < 0) 23 | err_sys("tcsetattr error"); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /apue.3e/termios/t_getpass.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | char *getpass(const char *); 4 | 5 | int 6 | main(void) 7 | { 8 | char *ptr; 9 | 10 | if ((ptr = getpass("Enter password:")) == NULL) 11 | err_sys("getpass error"); 12 | printf("password: %s\n", ptr); 13 | 14 | /* now use password (probably encrypt it) ... */ 15 | 16 | while (*ptr != 0) 17 | *ptr++ = 0; /* zero it out when we're done with it */ 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/termios/t_isatty.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | printf("fd 0: %s\n", isatty(0) ? "tty" : "not a tty"); 7 | printf("fd 1: %s\n", isatty(1) ? "tty" : "not a tty"); 8 | printf("fd 2: %s\n", isatty(2) ? "tty" : "not a tty"); 9 | exit(0); 10 | } 11 | -------------------------------------------------------------------------------- /apue.3e/termios/t_ttyname.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | char *name; 7 | if (isatty(0)) { 8 | name = ttyname(0); 9 | if (name == NULL) 10 | name = "undefined"; 11 | } else { 12 | name = "not a tty"; 13 | } 14 | printf("fd 0: %s\n", name); 15 | 16 | if (isatty(1)) { 17 | name = ttyname(1); 18 | if (name == NULL) 19 | name = "undefined"; 20 | } else { 21 | name = "not a tty"; 22 | } 23 | printf("fd 1: %s\n", name); 24 | 25 | if (isatty(2)) { 26 | name = ttyname(2); 27 | if (name == NULL) 28 | name = "undefined"; 29 | } else { 30 | name = "not a tty"; 31 | } 32 | printf("fd 2: %s\n", name); 33 | 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /apue.3e/termios/winch.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #ifndef TIOCGWINSZ 4 | #include 5 | #endif 6 | 7 | static void 8 | pr_winsize(int fd) 9 | { 10 | struct winsize size; 11 | 12 | if (ioctl(fd, TIOCGWINSZ, (char *) &size) < 0) 13 | err_sys("TIOCGWINSZ error"); 14 | printf("%d rows, %d columns\n", size.ws_row, size.ws_col); 15 | } 16 | 17 | static void 18 | sig_winch(int signo) 19 | { 20 | printf("SIGWINCH received\n"); 21 | pr_winsize(STDIN_FILENO); 22 | } 23 | 24 | int 25 | main(void) 26 | { 27 | if (isatty(STDIN_FILENO) == 0) 28 | exit(1); 29 | if (signal(SIGWINCH, sig_winch) == SIG_ERR) 30 | err_sys("signal error"); 31 | pr_winsize(STDIN_FILENO); /* print initial size */ 32 | for ( ; ; ) /* and sleep forever */ 33 | pause(); 34 | } 35 | -------------------------------------------------------------------------------- /apue.3e/threadctl/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | TOUT = 6 | ifeq "$(PLATFORM)" "freebsd" 7 | EXTRALIBS = -pthread 8 | endif 9 | ifeq "$(PLATFORM)" "linux" 10 | EXTRALIBS = -pthread 11 | TOUT = timeout.o 12 | endif 13 | ifeq "$(PLATFORM)" "solaris" 14 | EXTRALIBS = -lpthread 15 | TOUT = timeout.o 16 | endif 17 | 18 | PROGS = atfork suspend 19 | 20 | all: $(PROGS) detach.o getenv1.o getenv2.o getenv3.o $(TOUT) 21 | 22 | %: %.c $(LIBAPUE) 23 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 24 | 25 | clean: 26 | rm -f $(PROGS) $(TEMPFILES) *.o 27 | 28 | include $(ROOT)/Make.libapue.inc 29 | -------------------------------------------------------------------------------- /apue.3e/threadctl/detach.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | makethread(void *(*fn)(void *), void *arg) 6 | { 7 | int err; 8 | pthread_t tid; 9 | pthread_attr_t attr; 10 | 11 | err = pthread_attr_init(&attr); 12 | if (err != 0) 13 | return(err); 14 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 15 | if (err == 0) 16 | err = pthread_create(&tid, &attr, fn, arg); 17 | pthread_attr_destroy(&attr); 18 | return(err); 19 | } 20 | -------------------------------------------------------------------------------- /apue.3e/threadctl/getenv1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAXSTRINGSZ 4096 5 | 6 | static char envbuf[MAXSTRINGSZ]; 7 | 8 | extern char **environ; 9 | 10 | char * 11 | getenv(const char *name) 12 | { 13 | int i, len; 14 | 15 | len = strlen(name); 16 | for (i = 0; environ[i] != NULL; i++) { 17 | if ((strncmp(name, environ[i], len) == 0) && 18 | (environ[i][len] == '=')) { 19 | strncpy(envbuf, &environ[i][len+1], MAXSTRINGSZ-1); 20 | return(envbuf); 21 | } 22 | } 23 | return(NULL); 24 | } 25 | -------------------------------------------------------------------------------- /apue.3e/threads/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | EXTRALIBS=-pthread 3 | PLATFORM=$(shell $(ROOT)/systype.sh) 4 | include $(ROOT)/Make.defines.$(PLATFORM) 5 | 6 | BAR = 7 | ifeq "$(PLATFORM)" "macos" 8 | TLOCK = 9 | EXTRALIBS=-pthread 10 | else 11 | TLOCK = timedlock 12 | endif 13 | ifeq "$(PLATFORM)" "linux" 14 | BAR = barrier 15 | EXTRALIBS=-pthread -lrt -lbsd 16 | endif 17 | ifeq "$(PLATFORM)" "freebsd" 18 | BAR = barrier 19 | EXTRALIBS=-pthread 20 | endif 21 | ifeq "$(PLATFORM)" "solaris" 22 | BAR = barrier 23 | EXTRALIBS=-lpthread -lrt 24 | endif 25 | 26 | PROGS = badexit2 cleanup exitstatus threadid 27 | 28 | all: $(PROGS) condvar.o maketimeout.o mutex1.o mutex2.o mutex3.o rwlock.o $(TLOCK) $(BAR) 29 | 30 | %: %.c $(LIBAPUE) 31 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 32 | 33 | clean: 34 | rm -f $(PROGS) $(TEMPFILES) *.o $(TLOCK) $(BAR) 35 | 36 | include $(ROOT)/Make.libapue.inc 37 | -------------------------------------------------------------------------------- /apue.3e/threads/condvar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct msg { 4 | struct msg *m_next; 5 | /* ... more stuff here ... */ 6 | }; 7 | 8 | struct msg *workq; 9 | 10 | pthread_cond_t qready = PTHREAD_COND_INITIALIZER; 11 | 12 | pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; 13 | 14 | void 15 | process_msg(void) 16 | { 17 | struct msg *mp; 18 | 19 | for (;;) { 20 | pthread_mutex_lock(&qlock); 21 | while (workq == NULL) 22 | pthread_cond_wait(&qready, &qlock); 23 | mp = workq; 24 | workq = mp->m_next; 25 | pthread_mutex_unlock(&qlock); 26 | /* now process the message mp */ 27 | } 28 | } 29 | 30 | void 31 | enqueue_msg(struct msg *mp) 32 | { 33 | pthread_mutex_lock(&qlock); 34 | mp->m_next = workq; 35 | workq = mp; 36 | pthread_mutex_unlock(&qlock); 37 | pthread_cond_signal(&qready); 38 | } 39 | -------------------------------------------------------------------------------- /apue.3e/threads/maketimeout.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void 5 | maketimeout(struct timespec *tsp, long minutes) 6 | { 7 | struct timeval now; 8 | 9 | /* get the current time */ 10 | gettimeofday(&now, NULL); 11 | tsp->tv_sec = now.tv_sec; 12 | tsp->tv_nsec = now.tv_usec * 1000; /* usec to nsec */ 13 | /* add the offset to get timeout value */ 14 | tsp->tv_sec += minutes * 60; 15 | } 16 | -------------------------------------------------------------------------------- /apue.3e/threads/threadid.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | pthread_t ntid; 5 | 6 | void 7 | printids(const char *s) 8 | { 9 | pid_t pid; 10 | pthread_t tid; 11 | 12 | pid = getpid(); 13 | tid = pthread_self(); 14 | printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, 15 | (unsigned long)tid, (unsigned long)tid); 16 | } 17 | 18 | void * 19 | thr_fn(void *arg) 20 | { 21 | printids("new thread: "); 22 | return((void *)0); 23 | } 24 | 25 | int 26 | main(void) 27 | { 28 | int err; 29 | 30 | err = pthread_create(&ntid, NULL, thr_fn, NULL); 31 | if (err != 0) 32 | err_exit(err, "can't create thread"); 33 | printids("main thread:"); 34 | sleep(1); 35 | exit(0); 36 | } 37 | -------------------------------------------------------------------------------- /epolldir/Client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/epolldir/Client -------------------------------------------------------------------------------- /epolldir/Client1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/epolldir/Client1 -------------------------------------------------------------------------------- /epolldir/Sever: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VVictorQi/APUE/969987db50572feb948b2dba5113c605cd30b452/epolldir/Sever -------------------------------------------------------------------------------- /fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(int argc,char** argv) 6 | { 7 | int fd; 8 | pid_t pid; 9 | pid=fork(); 10 | if(pid<0) 11 | { 12 | perror("fork"); 13 | } 14 | else if(pid>0) 15 | { 16 | wait(NULL); 17 | exit(EXIT_FAILURE); 18 | // 19 | } 20 | else 21 | { 22 | if(fd=open("/dev/tty1",O_RDWR)==-1) 23 | perror("open"); 24 | 25 | printf("pid=%d,ppid=%d\n",getpid(),getppid()); 26 | printf("sid=%d,tcgetsid=%d\n",getsid(getpid()),tcgetsid(fd)); 27 | printf("pigd=%d\n",getpgid(getpid())); 28 | 29 | } 30 | } 31 | --------------------------------------------------------------------------------