├── DISCLAIMER ├── Make.defines.freebsd ├── Make.defines.linux ├── Make.defines.macos ├── Make.defines.solaris ├── Make.libapue.inc ├── Makefile ├── README ├── README.md ├── 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 ├── include └── apue.h ├── 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 ├── error.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 /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Make.libapue.inc: -------------------------------------------------------------------------------- 1 | $(LIBAPUE): 2 | (cd $(ROOT)/lib && $(MAKE)) 3 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | APUE-code 2 | ========= 3 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /advio/deadlock.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void 5 | lockabyte(const char *name, int fd, off_t offset) 6 | { 7 | if (writew_lock(fd, offset, SEEK_SET, 1) < 0) 8 | err_sys("%s: writew_lock error", name); 9 | printf("%s: got the lock, byte %lld\n", name, (long long)offset); 10 | } 11 | 12 | int 13 | main(void) 14 | { 15 | int fd; 16 | pid_t pid; 17 | 18 | /* 19 | * Create a file and write two bytes to it. 20 | */ 21 | if ((fd = creat("templock", FILE_MODE)) < 0) 22 | err_sys("creat error"); 23 | if (write(fd, "ab", 2) != 2) 24 | err_sys("write error"); 25 | 26 | TELL_WAIT(); 27 | if ((pid = fork()) < 0) { 28 | err_sys("fork error"); 29 | } else if (pid == 0) { /* child */ 30 | lockabyte("child", fd, 0); 31 | TELL_PARENT(getppid()); 32 | WAIT_PARENT(); 33 | lockabyte("child", fd, 1); 34 | } else { /* parent */ 35 | lockabyte("parent", fd, 1); 36 | TELL_CHILD(pid); 37 | WAIT_CHILD(); 38 | lockabyte("parent", fd, 0); 39 | } 40 | exit(0); 41 | } 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /advio/mcopy2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #define COPYINCR (1024*1024*1024) /* 1 GB */ 6 | 7 | int 8 | main(int argc, char *argv[]) 9 | { 10 | int fdin, fdout; 11 | void *src, *dst; 12 | size_t copysz; 13 | struct stat sbuf; 14 | off_t fsz = 0; 15 | 16 | if (argc != 3) 17 | err_quit("usage: %s ", argv[0]); 18 | 19 | if ((fdin = open(argv[1], O_RDONLY)) < 0) 20 | err_sys("can't open %s for reading", argv[1]); 21 | 22 | if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 23 | FILE_MODE)) < 0) 24 | err_sys("can't creat %s for writing", argv[2]); 25 | 26 | if (fstat(fdin, &sbuf) < 0) /* need size of input file */ 27 | err_sys("fstat error"); 28 | 29 | if (ftruncate(fdout, sbuf.st_size) < 0) /* set output file size */ 30 | err_sys("ftruncate error"); 31 | 32 | while (fsz < sbuf.st_size) { 33 | if ((sbuf.st_size - fsz) > COPYINCR) 34 | copysz = COPYINCR; 35 | else 36 | copysz = sbuf.st_size - fsz; 37 | 38 | if ((src = mmap(0, copysz, PROT_READ, MAP_SHARED, 39 | fdin, fsz)) == MAP_FAILED) 40 | err_sys("mmap error for input"); 41 | if ((dst = mmap(0, copysz, PROT_READ | PROT_WRITE, 42 | MAP_SHARED, fdout, fsz)) == MAP_FAILED) 43 | err_sys("mmap error for output"); 44 | 45 | memcpy(dst, src, copysz); /* does the file copy */ 46 | munmap(src, copysz); 47 | munmap(dst, copysz); 48 | fsz += copysz; 49 | } 50 | exit(0); 51 | } 52 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /advio/rot13a.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #define BSZ 4096 6 | 7 | unsigned char buf[BSZ]; 8 | 9 | unsigned char 10 | translate(unsigned char c) 11 | { 12 | if (isalpha(c)) { 13 | if (c >= 'n') 14 | c -= 13; 15 | else if (c >= 'a') 16 | c += 13; 17 | else if (c >= 'N') 18 | c -= 13; 19 | else 20 | c += 13; 21 | } 22 | return(c); 23 | } 24 | 25 | int 26 | main(int argc, char* argv[]) 27 | { 28 | int ifd, ofd, i, n, nw; 29 | 30 | if (argc != 3) 31 | err_quit("usage: rot13 infile outfile"); 32 | if ((ifd = open(argv[1], O_RDONLY)) < 0) 33 | err_sys("can't open %s", argv[1]); 34 | if ((ofd = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, FILE_MODE)) < 0) 35 | err_sys("can't create %s", argv[2]); 36 | 37 | while ((n = read(ifd, buf, BSZ)) > 0) { 38 | for (i = 0; i < n; i++) 39 | buf[i] = translate(buf[i]); 40 | if ((nw = write(ofd, buf, n)) != n) { 41 | if (nw < 0) 42 | err_sys("write failed"); 43 | else 44 | err_quit("short write (%d/%d)", nw, n); 45 | } 46 | } 47 | 48 | fsync(ofd); 49 | exit(0); 50 | } 51 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /daemons/reread2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | extern int lockfile(int); 6 | extern int already_running(void); 7 | 8 | void 9 | reread(void) 10 | { 11 | /* ... */ 12 | } 13 | 14 | void 15 | sigterm(int signo) 16 | { 17 | syslog(LOG_INFO, "got SIGTERM; exiting"); 18 | exit(0); 19 | } 20 | 21 | void 22 | sighup(int signo) 23 | { 24 | syslog(LOG_INFO, "Re-reading configuration file"); 25 | reread(); 26 | } 27 | 28 | int 29 | main(int argc, char *argv[]) 30 | { 31 | char *cmd; 32 | struct sigaction sa; 33 | 34 | if ((cmd = strrchr(argv[0], '/')) == NULL) 35 | cmd = argv[0]; 36 | else 37 | cmd++; 38 | 39 | /* 40 | * Become a daemon. 41 | */ 42 | daemonize(cmd); 43 | 44 | /* 45 | * Make sure only one copy of the daemon is running. 46 | */ 47 | if (already_running()) { 48 | syslog(LOG_ERR, "daemon already running"); 49 | exit(1); 50 | } 51 | 52 | /* 53 | * Handle signals of interest. 54 | */ 55 | sa.sa_handler = sigterm; 56 | sigemptyset(&sa.sa_mask); 57 | sigaddset(&sa.sa_mask, SIGHUP); 58 | sa.sa_flags = 0; 59 | if (sigaction(SIGTERM, &sa, NULL) < 0) { 60 | syslog(LOG_ERR, "can't catch SIGTERM: %s", strerror(errno)); 61 | exit(1); 62 | } 63 | sa.sa_handler = sighup; 64 | sigemptyset(&sa.sa_mask); 65 | sigaddset(&sa.sa_mask, SIGTERM); 66 | sa.sa_flags = 0; 67 | if (sigaction(SIGHUP, &sa, NULL) < 0) { 68 | syslog(LOG_ERR, "can't catch SIGHUP: %s", strerror(errno)); 69 | exit(1); 70 | } 71 | 72 | /* 73 | * Proceed with the rest of the daemon. 74 | */ 75 | /* ... */ 76 | exit(0); 77 | } 78 | -------------------------------------------------------------------------------- /daemons/single.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define LOCKFILE "/var/run/daemon.pid" 11 | #define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 12 | 13 | extern int lockfile(int); 14 | 15 | int 16 | already_running(void) 17 | { 18 | int fd; 19 | char buf[16]; 20 | 21 | fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE); 22 | if (fd < 0) { 23 | syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno)); 24 | exit(1); 25 | } 26 | if (lockfile(fd) < 0) { 27 | if (errno == EACCES || errno == EAGAIN) { 28 | close(fd); 29 | return(1); 30 | } 31 | syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno)); 32 | exit(1); 33 | } 34 | ftruncate(fd, 0); 35 | sprintf(buf, "%ld", (long)getpid()); 36 | write(fd, buf, strlen(buf)+1); 37 | return(0); 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /db/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | LIBMISC = libapue_db.a 6 | COMM_OBJ = db.o 7 | 8 | ifeq "$(PLATFORM)" "solaris" 9 | LDCMD=$(LD) -64 -G -Bdynamic -R/lib/64:/usr/ucblib/sparcv9 -o libapue_db.so.1 -L/lib/64 -L/usr/ucblib/sparcv9 -L$(ROOT)/lib -lapue db.o 10 | EXTRALD=-m64 -R. 11 | else 12 | LDCMD=$(CC) -shared -Wl,-dylib -o libapue_db.so.1 -L$(ROOT)/lib -lapue -lc db.o 13 | endif 14 | ifeq "$(PLATFORM)" "linux" 15 | EXTRALD=-Wl,-rpath=. 16 | endif 17 | ifeq "$(PLATFORM)" "freebsd" 18 | EXTRALD=-R. 19 | endif 20 | ifeq "$(PLATFORM)" "macos" 21 | EXTRALD=-R. 22 | endif 23 | 24 | all: libapue_db.so.1 t4 $(LIBMISC) 25 | 26 | libapue_db.a: $(COMM_OBJ) $(LIBAPUE) 27 | $(AR) rsv $(LIBMISC) $(COMM_OBJ) 28 | $(RANLIB) $(LIBMISC) 29 | 30 | libapue_db.so.1: db.c $(LIBAPUE) 31 | $(CC) -fPIC $(CFLAGS) -c db.c 32 | $(LDCMD) 33 | ln -s libapue_db.so.1 libapue_db.so 34 | 35 | t4: $(LIBAPUE) 36 | $(CC) $(CFLAGS) -c -I. t4.c 37 | $(CC) $(EXTRALD) -o t4 t4.o -L$(ROOT)/lib -L. -lapue_db -lapue 38 | 39 | clean: 40 | rm -f *.o a.out core temp.* $(LIBMISC) t4 libapue_db.so.* *.dat *.idx libapue_db.so 41 | 42 | include $(ROOT)/Make.libapue.inc 43 | -------------------------------------------------------------------------------- /db/apue_db.h: -------------------------------------------------------------------------------- 1 | #ifndef _APUE_DB_H 2 | #define _APUE_DB_H 3 | 4 | typedef void * DBHANDLE; 5 | 6 | DBHANDLE db_open(const char *, int, ...); 7 | void db_close(DBHANDLE); 8 | char *db_fetch(DBHANDLE, const char *); 9 | int db_store(DBHANDLE, const char *, const char *, int); 10 | int db_delete(DBHANDLE, const char *); 11 | void db_rewind(DBHANDLE); 12 | char *db_nextrec(DBHANDLE, char *); 13 | 14 | /* 15 | * Flags for db_store(). 16 | */ 17 | #define DB_INSERT 1 /* insert new record only */ 18 | #define DB_REPLACE 2 /* replace existing record */ 19 | #define DB_STORE 3 /* replace or insert */ 20 | 21 | /* 22 | * Implementation limits. 23 | */ 24 | #define IDXLEN_MIN 6 /* key, sep, start, sep, length, \n */ 25 | #define IDXLEN_MAX 1024 /* arbitrary */ 26 | #define DATLEN_MIN 2 /* data byte, newline */ 27 | #define DATLEN_MAX 1024 /* arbitrary */ 28 | 29 | #endif /* _APUE_DB_H */ 30 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /environ/getrlimit.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define doit(name) pr_limits(#name, name) 5 | 6 | static void pr_limits(char *, int); 7 | 8 | int 9 | main(void) 10 | { 11 | #ifdef RLIMIT_AS 12 | doit(RLIMIT_AS); 13 | #endif 14 | 15 | doit(RLIMIT_CORE); 16 | doit(RLIMIT_CPU); 17 | doit(RLIMIT_DATA); 18 | doit(RLIMIT_FSIZE); 19 | 20 | #ifdef RLIMIT_MEMLOCK 21 | doit(RLIMIT_MEMLOCK); 22 | #endif 23 | 24 | #ifdef RLIMIT_MSGQUEUE 25 | doit(RLIMIT_MSGQUEUE); 26 | #endif 27 | 28 | #ifdef RLIMIT_NICE 29 | doit(RLIMIT_NICE); 30 | #endif 31 | 32 | doit(RLIMIT_NOFILE); 33 | 34 | #ifdef RLIMIT_NPROC 35 | doit(RLIMIT_NPROC); 36 | #endif 37 | 38 | #ifdef RLIMIT_NPTS 39 | doit(RLIMIT_NPTS); 40 | #endif 41 | 42 | #ifdef RLIMIT_RSS 43 | doit(RLIMIT_RSS); 44 | #endif 45 | 46 | #ifdef RLIMIT_SBSIZE 47 | doit(RLIMIT_SBSIZE); 48 | #endif 49 | 50 | #ifdef RLIMIT_SIGPENDING 51 | doit(RLIMIT_SIGPENDING); 52 | #endif 53 | 54 | doit(RLIMIT_STACK); 55 | 56 | #ifdef RLIMIT_SWAP 57 | doit(RLIMIT_SWAP); 58 | #endif 59 | 60 | #ifdef RLIMIT_VMEM 61 | doit(RLIMIT_VMEM); 62 | #endif 63 | 64 | exit(0); 65 | } 66 | 67 | static void 68 | pr_limits(char *name, int resource) 69 | { 70 | struct rlimit limit; 71 | unsigned long long lim; 72 | 73 | if (getrlimit(resource, &limit) < 0) 74 | err_sys("getrlimit error for %s", name); 75 | printf("%-14s ", name); 76 | if (limit.rlim_cur == RLIM_INFINITY) { 77 | printf("(infinite) "); 78 | } else { 79 | lim = limit.rlim_cur; 80 | printf("%10lld ", lim); 81 | } 82 | if (limit.rlim_max == RLIM_INFINITY) { 83 | printf("(infinite)"); 84 | } else { 85 | lim = limit.rlim_max; 86 | printf("%10lld", lim); 87 | } 88 | putchar((int)'\n'); 89 | } 90 | -------------------------------------------------------------------------------- /environ/hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("hello, world\n"); 6 | } 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /environ/testjmp.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void f1(int, int, int, int); 5 | static void f2(void); 6 | 7 | static jmp_buf jmpbuffer; 8 | static int globval; 9 | 10 | int 11 | main(void) 12 | { 13 | int autoval; 14 | register int regival; 15 | volatile int volaval; 16 | static int statval; 17 | 18 | globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5; 19 | 20 | if (setjmp(jmpbuffer) != 0) { 21 | printf("after longjmp:\n"); 22 | printf("globval = %d, autoval = %d, regival = %d," 23 | " volaval = %d, statval = %d\n", 24 | globval, autoval, regival, volaval, statval); 25 | exit(0); 26 | } 27 | 28 | /* 29 | * Change variables after setjmp, but before longjmp. 30 | */ 31 | globval = 95; autoval = 96; regival = 97; volaval = 98; 32 | statval = 99; 33 | 34 | f1(autoval, regival, volaval, statval); /* never returns */ 35 | exit(0); 36 | } 37 | 38 | static void 39 | f1(int i, int j, int k, int l) 40 | { 41 | printf("in f1():\n"); 42 | printf("globval = %d, autoval = %d, regival = %d," 43 | " volaval = %d, statval = %d\n", globval, i, j, k, l); 44 | f2(); 45 | } 46 | 47 | static void 48 | f2(void) 49 | { 50 | longjmp(jmpbuffer, 1); 51 | } 52 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /exercises/goodexit.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | struct foo { 5 | int a, b, c, d; 6 | }; 7 | 8 | void 9 | printfoo(const char *s, const struct foo *fp) 10 | { 11 | fputs(s, stdout); 12 | printf(" structure at 0x%lx\n", (unsigned long)fp); 13 | printf(" foo.a = %d\n", fp->a); 14 | printf(" foo.b = %d\n", fp->b); 15 | printf(" foo.c = %d\n", fp->c); 16 | printf(" foo.d = %d\n", fp->d); 17 | } 18 | 19 | void * 20 | thr_fn1(void *arg) 21 | { 22 | struct foo *fp; 23 | 24 | if ((fp = malloc(sizeof(struct foo))) == NULL) 25 | err_sys("can't allocate memory"); 26 | fp->a = 1; 27 | fp->b = 2; 28 | fp->c = 3; 29 | fp->d = 4; 30 | printfoo("thread:\n", fp); 31 | return((void *)fp); 32 | } 33 | 34 | int 35 | main(void) 36 | { 37 | int err; 38 | pthread_t tid1; 39 | struct foo *fp; 40 | 41 | err = pthread_create(&tid1, NULL, thr_fn1, NULL); 42 | if (err != 0) 43 | err_exit(err, "can't create thread 1"); 44 | err = pthread_join(tid1, (void *)&fp); 45 | if (err != 0) 46 | err_exit(err, "can't join with thread 1"); 47 | printfoo("parent:\n", fp); 48 | exit(0); 49 | } 50 | -------------------------------------------------------------------------------- /exercises/longpath.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define DEPTH 1000 /* directory depth */ 5 | #define STARTDIR "/tmp" 6 | #define NAME "alonglonglonglonglonglonglonglonglonglongname" 7 | #define MAXSZ (10*8192) 8 | 9 | int 10 | main(void) 11 | { 12 | int i; 13 | size_t size; 14 | char *path; 15 | 16 | if (chdir(STARTDIR) < 0) 17 | err_sys("chdir error"); 18 | 19 | for (i = 0; i < DEPTH; i++) { 20 | if (mkdir(NAME, DIR_MODE) < 0) 21 | err_sys("mkdir failed, i = %d", i); 22 | if (chdir(NAME) < 0) 23 | err_sys("chdir failed, i = %d", i); 24 | } 25 | 26 | if (creat("afile", FILE_MODE) < 0) 27 | err_sys("creat error"); 28 | 29 | /* 30 | * The deep directory is created, with a file at the leaf. 31 | * Now let's try to obtain its pathname. 32 | */ 33 | path = path_alloc(&size); 34 | for ( ; ; ) { 35 | if (getcwd(path, size) != NULL) { 36 | break; 37 | } else { 38 | err_ret("getcwd failed, size = %ld", (long)size); 39 | size += 100; 40 | if (size > MAXSZ) 41 | err_quit("giving up"); 42 | if ((path = realloc(path, size)) == NULL) 43 | err_sys("realloc error"); 44 | } 45 | } 46 | printf("length = %ld\n%s\n", (long)strlen(path), path); 47 | 48 | exit(0); 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /figlinks/fig1.10: -------------------------------------------------------------------------------- 1 | ../intro/shell2.c -------------------------------------------------------------------------------- /figlinks/fig1.3: -------------------------------------------------------------------------------- 1 | ../intro/ls1.c -------------------------------------------------------------------------------- /figlinks/fig1.4: -------------------------------------------------------------------------------- 1 | ../intro/mycat.c -------------------------------------------------------------------------------- /figlinks/fig1.5: -------------------------------------------------------------------------------- 1 | ../intro/getcputc.c -------------------------------------------------------------------------------- /figlinks/fig1.6: -------------------------------------------------------------------------------- 1 | ../intro/hello.c -------------------------------------------------------------------------------- /figlinks/fig1.7: -------------------------------------------------------------------------------- 1 | ../intro/shell1.c -------------------------------------------------------------------------------- /figlinks/fig1.8: -------------------------------------------------------------------------------- 1 | ../intro/testerror.c -------------------------------------------------------------------------------- /figlinks/fig1.9: -------------------------------------------------------------------------------- 1 | ../intro/uidgid.c -------------------------------------------------------------------------------- /figlinks/fig10.10: -------------------------------------------------------------------------------- 1 | ../signals/read1.c -------------------------------------------------------------------------------- /figlinks/fig10.11: -------------------------------------------------------------------------------- 1 | ../signals/read2.c -------------------------------------------------------------------------------- /figlinks/fig10.12: -------------------------------------------------------------------------------- 1 | ../signals/setops.c -------------------------------------------------------------------------------- /figlinks/fig10.14: -------------------------------------------------------------------------------- 1 | ../lib/prmask.c -------------------------------------------------------------------------------- /figlinks/fig10.15: -------------------------------------------------------------------------------- 1 | ../signals/critical.c -------------------------------------------------------------------------------- /figlinks/fig10.18: -------------------------------------------------------------------------------- 1 | ../lib/signal.c -------------------------------------------------------------------------------- /figlinks/fig10.19: -------------------------------------------------------------------------------- 1 | ../lib/signalintr.c -------------------------------------------------------------------------------- /figlinks/fig10.2: -------------------------------------------------------------------------------- 1 | ../signals/sigusr.c -------------------------------------------------------------------------------- /figlinks/fig10.20: -------------------------------------------------------------------------------- 1 | ../signals/mask.c -------------------------------------------------------------------------------- /figlinks/fig10.22: -------------------------------------------------------------------------------- 1 | ../signals/suspend1.c -------------------------------------------------------------------------------- /figlinks/fig10.23: -------------------------------------------------------------------------------- 1 | ../signals/suspend2.c -------------------------------------------------------------------------------- /figlinks/fig10.24: -------------------------------------------------------------------------------- 1 | ../lib/tellwait.c -------------------------------------------------------------------------------- /figlinks/fig10.25: -------------------------------------------------------------------------------- 1 | ../signals/abort.c -------------------------------------------------------------------------------- /figlinks/fig10.26: -------------------------------------------------------------------------------- 1 | ../signals/systest2.c -------------------------------------------------------------------------------- /figlinks/fig10.28: -------------------------------------------------------------------------------- 1 | ../signals/system.c -------------------------------------------------------------------------------- /figlinks/fig10.29: -------------------------------------------------------------------------------- 1 | ../lib/sleep.c -------------------------------------------------------------------------------- /figlinks/fig10.31: -------------------------------------------------------------------------------- 1 | ../signals/sigtstp.c -------------------------------------------------------------------------------- /figlinks/fig10.5: -------------------------------------------------------------------------------- 1 | ../signals/reenter.c -------------------------------------------------------------------------------- /figlinks/fig10.6: -------------------------------------------------------------------------------- 1 | ../signals/child.c -------------------------------------------------------------------------------- /figlinks/fig10.7: -------------------------------------------------------------------------------- 1 | ../signals/sleep1.c -------------------------------------------------------------------------------- /figlinks/fig10.8: -------------------------------------------------------------------------------- 1 | ../signals/sleep2.c -------------------------------------------------------------------------------- /figlinks/fig10.9: -------------------------------------------------------------------------------- 1 | ../signals/tsleep2.c -------------------------------------------------------------------------------- /figlinks/fig11.10: -------------------------------------------------------------------------------- 1 | ../threads/mutex1.c -------------------------------------------------------------------------------- /figlinks/fig11.11: -------------------------------------------------------------------------------- 1 | ../threads/mutex2.c -------------------------------------------------------------------------------- /figlinks/fig11.12: -------------------------------------------------------------------------------- 1 | ../threads/mutex3.c -------------------------------------------------------------------------------- /figlinks/fig11.14: -------------------------------------------------------------------------------- 1 | ../threads/rwlock.c -------------------------------------------------------------------------------- /figlinks/fig11.15: -------------------------------------------------------------------------------- 1 | ../threads/condvar.c -------------------------------------------------------------------------------- /figlinks/fig11.16: -------------------------------------------------------------------------------- 1 | ../threads/barrier.c -------------------------------------------------------------------------------- /figlinks/fig11.2: -------------------------------------------------------------------------------- 1 | ../threads/threadid.c -------------------------------------------------------------------------------- /figlinks/fig11.3: -------------------------------------------------------------------------------- 1 | ../threads/exitstatus.c -------------------------------------------------------------------------------- /figlinks/fig11.4: -------------------------------------------------------------------------------- 1 | ../threads/badexit2.c -------------------------------------------------------------------------------- /figlinks/fig11.5: -------------------------------------------------------------------------------- 1 | ../threads/cleanup.c -------------------------------------------------------------------------------- /figlinks/fig12.11: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv1.c -------------------------------------------------------------------------------- /figlinks/fig12.12: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv2.c -------------------------------------------------------------------------------- /figlinks/fig12.13: -------------------------------------------------------------------------------- 1 | ../threadctl/getenv3.c -------------------------------------------------------------------------------- /figlinks/fig12.16: -------------------------------------------------------------------------------- 1 | ../threadctl/suspend.c -------------------------------------------------------------------------------- /figlinks/fig12.17: -------------------------------------------------------------------------------- 1 | ../threadctl/atfork.c -------------------------------------------------------------------------------- /figlinks/fig12.4: -------------------------------------------------------------------------------- 1 | ../threadctl/detach.c -------------------------------------------------------------------------------- /figlinks/fig12.8: -------------------------------------------------------------------------------- 1 | ../threadctl/timeout.c -------------------------------------------------------------------------------- /figlinks/fig13.1: -------------------------------------------------------------------------------- 1 | ../daemons/init.c -------------------------------------------------------------------------------- /figlinks/fig13.6: -------------------------------------------------------------------------------- 1 | ../daemons/single.c -------------------------------------------------------------------------------- /figlinks/fig13.7: -------------------------------------------------------------------------------- 1 | ../daemons/reread.c -------------------------------------------------------------------------------- /figlinks/fig13.8: -------------------------------------------------------------------------------- 1 | ../daemons/reread2.c -------------------------------------------------------------------------------- /figlinks/fig13.9: -------------------------------------------------------------------------------- 1 | ../lib/setfd.c -------------------------------------------------------------------------------- /figlinks/fig14.1: -------------------------------------------------------------------------------- 1 | ../advio/nonblockw.c -------------------------------------------------------------------------------- /figlinks/fig14.12: -------------------------------------------------------------------------------- 1 | ../advio/mandatory.c -------------------------------------------------------------------------------- /figlinks/fig14.20: -------------------------------------------------------------------------------- 1 | ../advio/rot13a.c -------------------------------------------------------------------------------- /figlinks/fig14.21: -------------------------------------------------------------------------------- 1 | ../advio/rot13c2.c -------------------------------------------------------------------------------- /figlinks/fig14.24: -------------------------------------------------------------------------------- 1 | ../advio/writen.c -------------------------------------------------------------------------------- /figlinks/fig14.27: -------------------------------------------------------------------------------- 1 | ../advio/mcopy2.c -------------------------------------------------------------------------------- /figlinks/fig14.5: -------------------------------------------------------------------------------- 1 | ../lib/lockreg.c -------------------------------------------------------------------------------- /figlinks/fig14.6: -------------------------------------------------------------------------------- 1 | ../lib/locktest.c -------------------------------------------------------------------------------- /figlinks/fig14.7: -------------------------------------------------------------------------------- 1 | ../advio/deadlock.c -------------------------------------------------------------------------------- /figlinks/fig14.9: -------------------------------------------------------------------------------- 1 | ../advio/lockfile.c -------------------------------------------------------------------------------- /figlinks/fig15.11: -------------------------------------------------------------------------------- 1 | ../ipc1/popen2.c -------------------------------------------------------------------------------- /figlinks/fig15.12: -------------------------------------------------------------------------------- 1 | ../ipc1/popen.c -------------------------------------------------------------------------------- /figlinks/fig15.14: -------------------------------------------------------------------------------- 1 | ../ipc1/myuclc.c -------------------------------------------------------------------------------- /figlinks/fig15.15: -------------------------------------------------------------------------------- 1 | ../ipc1/popen1.c -------------------------------------------------------------------------------- /figlinks/fig15.17: -------------------------------------------------------------------------------- 1 | ../ipc1/add2.c -------------------------------------------------------------------------------- /figlinks/fig15.18: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe4.c -------------------------------------------------------------------------------- /figlinks/fig15.19: -------------------------------------------------------------------------------- 1 | ../ipc1/add2stdio.c -------------------------------------------------------------------------------- /figlinks/fig15.31: -------------------------------------------------------------------------------- 1 | ../ipc1/tshm.c -------------------------------------------------------------------------------- /figlinks/fig15.33: -------------------------------------------------------------------------------- 1 | ../ipc1/devzero.c -------------------------------------------------------------------------------- /figlinks/fig15.35: -------------------------------------------------------------------------------- 1 | ../ipc1/slock.c -------------------------------------------------------------------------------- /figlinks/fig15.5: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe1.c -------------------------------------------------------------------------------- /figlinks/fig15.6: -------------------------------------------------------------------------------- 1 | ../ipc1/pipe2.c -------------------------------------------------------------------------------- /figlinks/fig15.7: -------------------------------------------------------------------------------- 1 | ../ipc1/tellwait.c -------------------------------------------------------------------------------- /figlinks/fig16.10: -------------------------------------------------------------------------------- 1 | ../sockets/clconn.c -------------------------------------------------------------------------------- /figlinks/fig16.11: -------------------------------------------------------------------------------- 1 | ../sockets/clconn2.c -------------------------------------------------------------------------------- /figlinks/fig16.12: -------------------------------------------------------------------------------- 1 | ../sockets/initsrv1.c -------------------------------------------------------------------------------- /figlinks/fig16.16: -------------------------------------------------------------------------------- 1 | ../sockets/ruptime.c -------------------------------------------------------------------------------- /figlinks/fig16.17: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed.c -------------------------------------------------------------------------------- /figlinks/fig16.18: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed-fd.c -------------------------------------------------------------------------------- /figlinks/fig16.19: -------------------------------------------------------------------------------- 1 | ../sockets/ruptime-dg.c -------------------------------------------------------------------------------- /figlinks/fig16.20: -------------------------------------------------------------------------------- 1 | ../sockets/ruptimed-dg.c -------------------------------------------------------------------------------- /figlinks/fig16.22: -------------------------------------------------------------------------------- 1 | ../sockets/initsrv2.c -------------------------------------------------------------------------------- /figlinks/fig16.9: -------------------------------------------------------------------------------- 1 | ../sockets/findsvc.c -------------------------------------------------------------------------------- /figlinks/fig17.10: -------------------------------------------------------------------------------- 1 | ../lib/cliconn.c -------------------------------------------------------------------------------- /figlinks/fig17.12: -------------------------------------------------------------------------------- 1 | ../lib/senderr.c -------------------------------------------------------------------------------- /figlinks/fig17.13: -------------------------------------------------------------------------------- 1 | ../lib/sendfd.c -------------------------------------------------------------------------------- /figlinks/fig17.14: -------------------------------------------------------------------------------- 1 | ../lib/recvfd.c -------------------------------------------------------------------------------- /figlinks/fig17.15: -------------------------------------------------------------------------------- 1 | ../ipc2/sendfd2.c -------------------------------------------------------------------------------- /figlinks/fig17.16: -------------------------------------------------------------------------------- 1 | ../ipc2/recvfd2.c -------------------------------------------------------------------------------- /figlinks/fig17.17: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/open.h -------------------------------------------------------------------------------- /figlinks/fig17.18: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/main.c -------------------------------------------------------------------------------- /figlinks/fig17.19: -------------------------------------------------------------------------------- 1 | ../ipc2/open.fe/open.c -------------------------------------------------------------------------------- /figlinks/fig17.2: -------------------------------------------------------------------------------- 1 | ../lib/spipe.c -------------------------------------------------------------------------------- /figlinks/fig17.20: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/opend.h -------------------------------------------------------------------------------- /figlinks/fig17.21: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/main.c -------------------------------------------------------------------------------- /figlinks/fig17.22: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/request.c -------------------------------------------------------------------------------- /figlinks/fig17.23: -------------------------------------------------------------------------------- 1 | ../lib/bufargs.c -------------------------------------------------------------------------------- /figlinks/fig17.24: -------------------------------------------------------------------------------- 1 | ../ipc2/opend.fe/cliargs.c -------------------------------------------------------------------------------- /figlinks/fig17.25: -------------------------------------------------------------------------------- 1 | ../ipc2/open/open.c -------------------------------------------------------------------------------- /figlinks/fig17.26: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/opend.h -------------------------------------------------------------------------------- /figlinks/fig17.27: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/client.c -------------------------------------------------------------------------------- /figlinks/fig17.28: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/main.c -------------------------------------------------------------------------------- /figlinks/fig17.29: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/loop.select.c -------------------------------------------------------------------------------- /figlinks/fig17.3: -------------------------------------------------------------------------------- 1 | ../ipc2/pollmsg.c -------------------------------------------------------------------------------- /figlinks/fig17.30: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/loop.poll.c -------------------------------------------------------------------------------- /figlinks/fig17.31: -------------------------------------------------------------------------------- 1 | ../ipc2/opend/request.c -------------------------------------------------------------------------------- /figlinks/fig17.4: -------------------------------------------------------------------------------- 1 | ../ipc2/sendmsg.c -------------------------------------------------------------------------------- /figlinks/fig17.5: -------------------------------------------------------------------------------- 1 | ../ipc2/bindunix.c -------------------------------------------------------------------------------- /figlinks/fig17.8: -------------------------------------------------------------------------------- 1 | ../lib/servlisten.c -------------------------------------------------------------------------------- /figlinks/fig17.9: -------------------------------------------------------------------------------- 1 | ../lib/servaccept.c -------------------------------------------------------------------------------- /figlinks/fig18.10: -------------------------------------------------------------------------------- 1 | ../termios/settty.c -------------------------------------------------------------------------------- /figlinks/fig18.11: -------------------------------------------------------------------------------- 1 | ../termios/csize.c -------------------------------------------------------------------------------- /figlinks/fig18.12: -------------------------------------------------------------------------------- 1 | ../termios/ctermid.c -------------------------------------------------------------------------------- /figlinks/fig18.13: -------------------------------------------------------------------------------- 1 | ../termios/isatty.c -------------------------------------------------------------------------------- /figlinks/fig18.14: -------------------------------------------------------------------------------- 1 | ../termios/t_isatty.c -------------------------------------------------------------------------------- /figlinks/fig18.15: -------------------------------------------------------------------------------- 1 | ../termios/ttyname.c -------------------------------------------------------------------------------- /figlinks/fig18.16: -------------------------------------------------------------------------------- 1 | ../termios/t_ttyname.c -------------------------------------------------------------------------------- /figlinks/fig18.17: -------------------------------------------------------------------------------- 1 | ../termios/getpass.c -------------------------------------------------------------------------------- /figlinks/fig18.18: -------------------------------------------------------------------------------- 1 | ../termios/t_getpass.c -------------------------------------------------------------------------------- /figlinks/fig18.20: -------------------------------------------------------------------------------- 1 | ../lib/ttymodes.c -------------------------------------------------------------------------------- /figlinks/fig18.21: -------------------------------------------------------------------------------- 1 | ../termios/t_raw.c -------------------------------------------------------------------------------- /figlinks/fig18.22: -------------------------------------------------------------------------------- 1 | ../termios/winch.c -------------------------------------------------------------------------------- /figlinks/fig19.10: -------------------------------------------------------------------------------- 1 | ../lib/ptyfork.c -------------------------------------------------------------------------------- /figlinks/fig19.11: -------------------------------------------------------------------------------- 1 | ../pty/main.c -------------------------------------------------------------------------------- /figlinks/fig19.12: -------------------------------------------------------------------------------- 1 | ../pty/loop.c -------------------------------------------------------------------------------- /figlinks/fig19.16: -------------------------------------------------------------------------------- 1 | ../pty/driver.c -------------------------------------------------------------------------------- /figlinks/fig19.9: -------------------------------------------------------------------------------- 1 | ../lib/ptyopen.c -------------------------------------------------------------------------------- /figlinks/fig2.13: -------------------------------------------------------------------------------- 1 | ../standards/makeconf.awk -------------------------------------------------------------------------------- /figlinks/fig2.14: -------------------------------------------------------------------------------- 1 | ../standards/conf.c.modified -------------------------------------------------------------------------------- /figlinks/fig2.16: -------------------------------------------------------------------------------- 1 | ../lib/pathalloc.c -------------------------------------------------------------------------------- /figlinks/fig2.17: -------------------------------------------------------------------------------- 1 | ../lib/openmax.c -------------------------------------------------------------------------------- /figlinks/fig20.3: -------------------------------------------------------------------------------- 1 | ../db/t4.c -------------------------------------------------------------------------------- /figlinks/fig3.1: -------------------------------------------------------------------------------- 1 | ../fileio/seek.c -------------------------------------------------------------------------------- /figlinks/fig3.11: -------------------------------------------------------------------------------- 1 | ../fileio/fileflags.c -------------------------------------------------------------------------------- /figlinks/fig3.12: -------------------------------------------------------------------------------- 1 | ../fileio/setfl.c -------------------------------------------------------------------------------- /figlinks/fig3.2: -------------------------------------------------------------------------------- 1 | ../fileio/hole.c -------------------------------------------------------------------------------- /figlinks/fig3.5: -------------------------------------------------------------------------------- 1 | ../fileio/mycat.c -------------------------------------------------------------------------------- /figlinks/fig4.12: -------------------------------------------------------------------------------- 1 | ../filedir/changemod.c -------------------------------------------------------------------------------- /figlinks/fig4.16: -------------------------------------------------------------------------------- 1 | ../filedir/unlink.c -------------------------------------------------------------------------------- /figlinks/fig4.21: -------------------------------------------------------------------------------- 1 | ../filedir/zap.c -------------------------------------------------------------------------------- /figlinks/fig4.22: -------------------------------------------------------------------------------- 1 | ../filedir/ftw8.c -------------------------------------------------------------------------------- /figlinks/fig4.23: -------------------------------------------------------------------------------- 1 | ../filedir/mycd.c -------------------------------------------------------------------------------- /figlinks/fig4.24: -------------------------------------------------------------------------------- 1 | ../filedir/cdpwd.c -------------------------------------------------------------------------------- /figlinks/fig4.25: -------------------------------------------------------------------------------- 1 | ../filedir/devrdev.c -------------------------------------------------------------------------------- /figlinks/fig4.3: -------------------------------------------------------------------------------- 1 | ../filedir/filetype.c -------------------------------------------------------------------------------- /figlinks/fig4.8: -------------------------------------------------------------------------------- 1 | ../filedir/access.c -------------------------------------------------------------------------------- /figlinks/fig4.9: -------------------------------------------------------------------------------- 1 | ../filedir/umask.c -------------------------------------------------------------------------------- /figlinks/fig5.11: -------------------------------------------------------------------------------- 1 | ../stdio/buf.c -------------------------------------------------------------------------------- /figlinks/fig5.12: -------------------------------------------------------------------------------- 1 | ../stdio/tempfiles.c -------------------------------------------------------------------------------- /figlinks/fig5.13: -------------------------------------------------------------------------------- 1 | ../stdio/mkstemp.c -------------------------------------------------------------------------------- /figlinks/fig5.15: -------------------------------------------------------------------------------- 1 | ../stdio/memstr.c -------------------------------------------------------------------------------- /figlinks/fig5.4: -------------------------------------------------------------------------------- 1 | ../stdio/getcputc.c -------------------------------------------------------------------------------- /figlinks/fig5.5: -------------------------------------------------------------------------------- 1 | ../stdio/fgetsfputs.c -------------------------------------------------------------------------------- /figlinks/fig6.11: -------------------------------------------------------------------------------- 1 | ../datafiles/strftime.c -------------------------------------------------------------------------------- /figlinks/fig6.2: -------------------------------------------------------------------------------- 1 | ../datafiles/getpwnam.c -------------------------------------------------------------------------------- /figlinks/fig7.1: -------------------------------------------------------------------------------- 1 | ../environ/hello1.c -------------------------------------------------------------------------------- /figlinks/fig7.11: -------------------------------------------------------------------------------- 1 | ../environ/cmd2.c -------------------------------------------------------------------------------- /figlinks/fig7.13: -------------------------------------------------------------------------------- 1 | ../environ/testjmp.c -------------------------------------------------------------------------------- /figlinks/fig7.14: -------------------------------------------------------------------------------- 1 | ../environ/opendata.c -------------------------------------------------------------------------------- /figlinks/fig7.16: -------------------------------------------------------------------------------- 1 | ../environ/getrlimit.c -------------------------------------------------------------------------------- /figlinks/fig7.3: -------------------------------------------------------------------------------- 1 | ../environ/doatexit.c -------------------------------------------------------------------------------- /figlinks/fig7.4: -------------------------------------------------------------------------------- 1 | ../environ/echoarg.c -------------------------------------------------------------------------------- /figlinks/fig7.9: -------------------------------------------------------------------------------- 1 | ../environ/cmd1.c -------------------------------------------------------------------------------- /figlinks/fig8.1: -------------------------------------------------------------------------------- 1 | ../proc/fork1.c -------------------------------------------------------------------------------- /figlinks/fig8.12: -------------------------------------------------------------------------------- 1 | ../proc/tellwait1.c -------------------------------------------------------------------------------- /figlinks/fig8.13: -------------------------------------------------------------------------------- 1 | ../proc/tellwait2.c -------------------------------------------------------------------------------- /figlinks/fig8.16: -------------------------------------------------------------------------------- 1 | ../proc/exec1.c -------------------------------------------------------------------------------- /figlinks/fig8.17: -------------------------------------------------------------------------------- 1 | ../proc/echoall.c -------------------------------------------------------------------------------- /figlinks/fig8.20: -------------------------------------------------------------------------------- 1 | ../proc/exec2.c -------------------------------------------------------------------------------- /figlinks/fig8.21: -------------------------------------------------------------------------------- 1 | ../proc/awkexample -------------------------------------------------------------------------------- /figlinks/fig8.22: -------------------------------------------------------------------------------- 1 | ../proc/system.c -------------------------------------------------------------------------------- /figlinks/fig8.23: -------------------------------------------------------------------------------- 1 | ../proc/systest1.c -------------------------------------------------------------------------------- /figlinks/fig8.24: -------------------------------------------------------------------------------- 1 | ../proc/systest3.c -------------------------------------------------------------------------------- /figlinks/fig8.25: -------------------------------------------------------------------------------- 1 | ../proc/pruids.c -------------------------------------------------------------------------------- /figlinks/fig8.28: -------------------------------------------------------------------------------- 1 | ../proc/test1.c -------------------------------------------------------------------------------- /figlinks/fig8.29: -------------------------------------------------------------------------------- 1 | ../proc/pracct.c -------------------------------------------------------------------------------- /figlinks/fig8.3: -------------------------------------------------------------------------------- 1 | ../proc/vfork1.c -------------------------------------------------------------------------------- /figlinks/fig8.30: -------------------------------------------------------------------------------- 1 | ../proc/nice.c -------------------------------------------------------------------------------- /figlinks/fig8.31: -------------------------------------------------------------------------------- 1 | ../proc/times1.c -------------------------------------------------------------------------------- /figlinks/fig8.5: -------------------------------------------------------------------------------- 1 | ../lib/prexit.c -------------------------------------------------------------------------------- /figlinks/fig8.6: -------------------------------------------------------------------------------- 1 | ../proc/wait1.c -------------------------------------------------------------------------------- /figlinks/fig8.8: -------------------------------------------------------------------------------- 1 | ../proc/fork2.c -------------------------------------------------------------------------------- /figlinks/fig9.12: -------------------------------------------------------------------------------- 1 | ../relation/orphan3.c -------------------------------------------------------------------------------- /figlinks/figB.1: -------------------------------------------------------------------------------- 1 | ../include/apue.h -------------------------------------------------------------------------------- /figlinks/figB.3: -------------------------------------------------------------------------------- 1 | ../lib/error.c -------------------------------------------------------------------------------- /figlinks/figB.4: -------------------------------------------------------------------------------- 1 | ../lib/errorlog.c -------------------------------------------------------------------------------- /figlinks/figC.1: -------------------------------------------------------------------------------- 1 | ../exercises/openmax.c -------------------------------------------------------------------------------- /figlinks/figC.10: -------------------------------------------------------------------------------- 1 | ../exercises/zombie.c -------------------------------------------------------------------------------- /figlinks/figC.12: -------------------------------------------------------------------------------- 1 | ../exercises/goodexit.c -------------------------------------------------------------------------------- /figlinks/figC.13: -------------------------------------------------------------------------------- 1 | ../exercises/sleep.c -------------------------------------------------------------------------------- /figlinks/figC.14: -------------------------------------------------------------------------------- 1 | ../exercises/getlogin.c -------------------------------------------------------------------------------- /figlinks/figC.15: -------------------------------------------------------------------------------- 1 | ../exercises/pendlock.c -------------------------------------------------------------------------------- /figlinks/figC.16: -------------------------------------------------------------------------------- 1 | ../exercises/sleepus_select.c -------------------------------------------------------------------------------- /figlinks/figC.17: -------------------------------------------------------------------------------- 1 | ../exercises/sleepus_poll.c -------------------------------------------------------------------------------- /figlinks/figC.18: -------------------------------------------------------------------------------- 1 | ../exercises/sizepipe.c -------------------------------------------------------------------------------- /figlinks/figC.20: -------------------------------------------------------------------------------- 1 | ../exercises/fifo1.c -------------------------------------------------------------------------------- /figlinks/figC.22: -------------------------------------------------------------------------------- 1 | ../exercises/bo.c -------------------------------------------------------------------------------- /figlinks/figC.23: -------------------------------------------------------------------------------- 1 | ../exercises/asyncsocket.c -------------------------------------------------------------------------------- /figlinks/figC.24: -------------------------------------------------------------------------------- 1 | ../exercises/pollmsg2.c -------------------------------------------------------------------------------- /figlinks/figC.3: -------------------------------------------------------------------------------- 1 | ../exercises/longpath.c -------------------------------------------------------------------------------- /figlinks/figC.4: -------------------------------------------------------------------------------- 1 | ../exercises/fmemopen.c -------------------------------------------------------------------------------- /figlinks/figC.5: -------------------------------------------------------------------------------- 1 | ../exercises/getpwsvr4.c -------------------------------------------------------------------------------- /figlinks/figC.6: -------------------------------------------------------------------------------- 1 | ../exercises/getpw44bsd.c -------------------------------------------------------------------------------- /figlinks/figC.7: -------------------------------------------------------------------------------- 1 | ../exercises/prtime.c -------------------------------------------------------------------------------- /figlinks/figC.8: -------------------------------------------------------------------------------- 1 | ../exercises/vfork3.c -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /fileio/fileflags.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | int val; 8 | 9 | if (argc != 2) 10 | err_quit("usage: a.out "); 11 | 12 | if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) 13 | err_sys("fcntl error for fd %d", atoi(argv[1])); 14 | 15 | switch (val & O_ACCMODE) { 16 | case O_RDONLY: 17 | printf("read only"); 18 | break; 19 | 20 | case O_WRONLY: 21 | printf("write only"); 22 | break; 23 | 24 | case O_RDWR: 25 | printf("read write"); 26 | break; 27 | 28 | default: 29 | err_dump("unknown access mode"); 30 | } 31 | 32 | if (val & O_APPEND) 33 | printf(", append"); 34 | if (val & O_NONBLOCK) 35 | printf(", nonblocking"); 36 | if (val & O_SYNC) 37 | printf(", synchronous writes"); 38 | 39 | #if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC) && (O_FSYNC != O_SYNC) 40 | if (val & O_FSYNC) 41 | printf(", synchronous writes"); 42 | #endif 43 | 44 | putchar('\n'); 45 | exit(0); 46 | } 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /intro/shell2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void sig_int(int); /* our signal-catching function */ 5 | 6 | int 7 | main(void) 8 | { 9 | char buf[MAXLINE]; /* from apue.h */ 10 | pid_t pid; 11 | int status; 12 | 13 | if (signal(SIGINT, sig_int) == SIG_ERR) 14 | err_sys("signal error"); 15 | 16 | printf("%% "); /* print prompt (printf requires %% to print %) */ 17 | while (fgets(buf, MAXLINE, stdin) != NULL) { 18 | if (buf[strlen(buf) - 1] == '\n') 19 | buf[strlen(buf) - 1] = 0; /* replace newline with null */ 20 | 21 | if ((pid = fork()) < 0) { 22 | err_sys("fork error"); 23 | } else if (pid == 0) { /* child */ 24 | execlp(buf, buf, (char *)0); 25 | err_ret("couldn't execute: %s", buf); 26 | exit(127); 27 | } 28 | 29 | /* parent */ 30 | if ((pid = waitpid(pid, &status, 0)) < 0) 31 | err_sys("waitpid error"); 32 | printf("%% "); 33 | } 34 | exit(0); 35 | } 36 | 37 | void 38 | sig_int(int signo) 39 | { 40 | printf("interrupt\n%% "); 41 | } 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc1/devzero.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #define NLOOPS 1000 6 | #define SIZE sizeof(long) /* size of shared memory area */ 7 | 8 | static int 9 | update(long *ptr) 10 | { 11 | return((*ptr)++); /* return value before increment */ 12 | } 13 | 14 | int 15 | main(void) 16 | { 17 | int fd, i, counter; 18 | pid_t pid; 19 | void *area; 20 | 21 | if ((fd = open("/dev/zero", O_RDWR)) < 0) 22 | err_sys("open error"); 23 | if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, 24 | fd, 0)) == MAP_FAILED) 25 | err_sys("mmap error"); 26 | close(fd); /* can close /dev/zero now that it's mapped */ 27 | 28 | TELL_WAIT(); 29 | 30 | if ((pid = fork()) < 0) { 31 | err_sys("fork error"); 32 | } else if (pid > 0) { /* parent */ 33 | for (i = 0; i < NLOOPS; i += 2) { 34 | if ((counter = update((long *)area)) != i) 35 | err_quit("parent: expected %d, got %d", i, counter); 36 | 37 | TELL_CHILD(pid); 38 | WAIT_CHILD(); 39 | } 40 | } else { /* child */ 41 | for (i = 1; i < NLOOPS + 1; i += 2) { 42 | WAIT_PARENT(); 43 | 44 | if ((counter = update((long *)area)) != i) 45 | err_quit("child: expected %d, got %d", i, counter); 46 | 47 | TELL_PARENT(getppid()); 48 | } 49 | } 50 | 51 | exit(0); 52 | } 53 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc1/pipe2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define DEF_PAGER "/bin/more" /* default pager program */ 5 | 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | int n; 10 | int fd[2]; 11 | pid_t pid; 12 | char *pager, *argv0; 13 | char line[MAXLINE]; 14 | FILE *fp; 15 | 16 | if (argc != 2) 17 | err_quit("usage: a.out "); 18 | 19 | if ((fp = fopen(argv[1], "r")) == NULL) 20 | err_sys("can't open %s", argv[1]); 21 | if (pipe(fd) < 0) 22 | err_sys("pipe error"); 23 | 24 | if ((pid = fork()) < 0) { 25 | err_sys("fork error"); 26 | } else if (pid > 0) { /* parent */ 27 | close(fd[0]); /* close read end */ 28 | 29 | /* parent copies argv[1] to pipe */ 30 | while (fgets(line, MAXLINE, fp) != NULL) { 31 | n = strlen(line); 32 | if (write(fd[1], line, n) != n) 33 | err_sys("write error to pipe"); 34 | } 35 | if (ferror(fp)) 36 | err_sys("fgets error"); 37 | 38 | close(fd[1]); /* close write end of pipe for reader */ 39 | 40 | if (waitpid(pid, NULL, 0) < 0) 41 | err_sys("waitpid error"); 42 | exit(0); 43 | } else { /* child */ 44 | close(fd[1]); /* close write end */ 45 | if (fd[0] != STDIN_FILENO) { 46 | if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) 47 | err_sys("dup2 error to stdin"); 48 | close(fd[0]); /* don't need this after dup2 */ 49 | } 50 | 51 | /* get arguments for execl() */ 52 | if ((pager = getenv("PAGER")) == NULL) 53 | pager = DEF_PAGER; 54 | if ((argv0 = strrchr(pager, '/')) != NULL) 55 | argv0++; /* step past rightmost slash */ 56 | else 57 | argv0 = pager; /* no slash in pager */ 58 | 59 | if (execl(pager, argv0, (char *)0) < 0) 60 | err_sys("execl error for %s", pager); 61 | } 62 | exit(0); 63 | } 64 | -------------------------------------------------------------------------------- /ipc1/pipe4.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void sig_pipe(int); /* our signal handler */ 4 | 5 | int 6 | main(void) 7 | { 8 | int n, fd1[2], fd2[2]; 9 | pid_t pid; 10 | char line[MAXLINE]; 11 | 12 | if (signal(SIGPIPE, sig_pipe) == SIG_ERR) 13 | err_sys("signal error"); 14 | 15 | if (pipe(fd1) < 0 || pipe(fd2) < 0) 16 | err_sys("pipe error"); 17 | 18 | if ((pid = fork()) < 0) { 19 | err_sys("fork error"); 20 | } else if (pid > 0) { /* parent */ 21 | close(fd1[0]); 22 | close(fd2[1]); 23 | 24 | while (fgets(line, MAXLINE, stdin) != NULL) { 25 | n = strlen(line); 26 | if (write(fd1[1], line, n) != n) 27 | err_sys("write error to pipe"); 28 | if ((n = read(fd2[0], line, MAXLINE)) < 0) 29 | err_sys("read error from pipe"); 30 | if (n == 0) { 31 | err_msg("child closed pipe"); 32 | break; 33 | } 34 | line[n] = 0; /* null terminate */ 35 | if (fputs(line, stdout) == EOF) 36 | err_sys("fputs error"); 37 | } 38 | 39 | if (ferror(stdin)) 40 | err_sys("fgets error on stdin"); 41 | exit(0); 42 | } else { /* child */ 43 | close(fd1[1]); 44 | close(fd2[0]); 45 | if (fd1[0] != STDIN_FILENO) { 46 | if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO) 47 | err_sys("dup2 error to stdin"); 48 | close(fd1[0]); 49 | } 50 | 51 | if (fd2[1] != STDOUT_FILENO) { 52 | if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO) 53 | err_sys("dup2 error to stdout"); 54 | close(fd2[1]); 55 | } 56 | if (execl("./add2", "add2", (char *)0) < 0) 57 | err_sys("execl error"); 58 | } 59 | exit(0); 60 | } 61 | 62 | static void 63 | sig_pipe(int signo) 64 | { 65 | printf("SIGPIPE caught\n"); 66 | exit(1); 67 | } 68 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc1/slock.c: -------------------------------------------------------------------------------- 1 | #include "slock.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct slock * 8 | s_alloc() 9 | { 10 | struct slock *sp; 11 | static int cnt; 12 | 13 | if ((sp = malloc(sizeof(struct slock))) == NULL) 14 | return(NULL); 15 | do { 16 | snprintf(sp->name, sizeof(sp->name), "/%ld.%d", (long)getpid(), 17 | cnt++); 18 | sp->semp = sem_open(sp->name, O_CREAT|O_EXCL, S_IRWXU, 1); 19 | } while ((sp->semp == SEM_FAILED) && (errno == EEXIST)); 20 | if (sp->semp == SEM_FAILED) { 21 | free(sp); 22 | return(NULL); 23 | } 24 | sem_unlink(sp->name); 25 | return(sp); 26 | } 27 | 28 | void 29 | s_free(struct slock *sp) 30 | { 31 | sem_close(sp->semp); 32 | free(sp); 33 | } 34 | 35 | int 36 | s_lock(struct slock *sp) 37 | { 38 | return(sem_wait(sp->semp)); 39 | } 40 | 41 | int 42 | s_trylock(struct slock *sp) 43 | { 44 | return(sem_trywait(sp->semp)); 45 | } 46 | 47 | int 48 | s_unlock(struct slock *sp) 49 | { 50 | return(sem_post(sp->semp)); 51 | } 52 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc1/tshm.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | #define ARRAY_SIZE 40000 5 | #define MALLOC_SIZE 100000 6 | #define SHM_SIZE 100000 7 | #define SHM_MODE 0600 /* user read/write */ 8 | 9 | char array[ARRAY_SIZE]; /* uninitialized data = bss */ 10 | 11 | int 12 | main(void) 13 | { 14 | int shmid; 15 | char *ptr, *shmptr; 16 | 17 | printf("array[] from %p to %p\n", (void *)&array[0], 18 | (void *)&array[ARRAY_SIZE]); 19 | printf("stack around %p\n", (void *)&shmid); 20 | 21 | if ((ptr = malloc(MALLOC_SIZE)) == NULL) 22 | err_sys("malloc error"); 23 | printf("malloced from %p to %p\n", (void *)ptr, 24 | (void *)ptr+MALLOC_SIZE); 25 | 26 | if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) < 0) 27 | err_sys("shmget error"); 28 | if ((shmptr = shmat(shmid, 0, 0)) == (void *)-1) 29 | err_sys("shmat error"); 30 | printf("shared memory attached from %p to %p\n", (void *)shmptr, 31 | (void *)shmptr+SHM_SIZE); 32 | 33 | if (shmctl(shmid, IPC_RMID, 0) < 0) 34 | err_sys("shmctl error"); 35 | 36 | exit(0); 37 | } 38 | -------------------------------------------------------------------------------- /ipc2/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ifeq "$(PLATFORM)" "freebsd" 6 | EXTRALIBS=-pthread 7 | endif 8 | ifeq "$(PLATFORM)" "linux" 9 | EXTRALIBS=-pthread 10 | endif 11 | ifeq "$(PLATFORM)" "solaris" 12 | EXTRALIBS=-lsocket -lnsl 13 | endif 14 | 15 | PROGS = bindunix pollmsg sendmsg 16 | FDPASS = 17 | 18 | ifeq "$(PLATFORM)" "linux" 19 | FDPASS = recvfd2.o sendfd2.o 20 | endif 21 | ifeq "$(PLATFORM)" "freebsd" 22 | FDPASS = recvfd2.o sendfd2.o 23 | endif 24 | 25 | all: $(PROGS) $(FDPASS) 26 | for i in open opend open.fe opend.fe; do \ 27 | (cd $$i && $(MAKE) ) || exit 1; \ 28 | done 29 | 30 | %: %.c $(LIBAPUE) 31 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 32 | 33 | clean: 34 | rm -f $(PROGS) $(TEMPFILES) *.o 35 | for i in open opend open.fe opend.fe; do \ 36 | (cd $$i && $(MAKE) clean) || exit 1; \ 37 | done 38 | 39 | include $(ROOT)/Make.libapue.inc 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc2/open.fe/open.c: -------------------------------------------------------------------------------- 1 | #include "open.h" 2 | #include /* struct iovec */ 3 | 4 | /* 5 | * Open the file by sending the "name" and "oflag" to the 6 | * connection server and reading a file descriptor back. 7 | */ 8 | int 9 | csopen(char *name, int oflag) 10 | { 11 | pid_t pid; 12 | int len; 13 | char buf[10]; 14 | struct iovec iov[3]; 15 | static int fd[2] = { -1, -1 }; 16 | 17 | if (fd[0] < 0) { /* fork/exec our open server first time */ 18 | if (fd_pipe(fd) < 0) { 19 | err_ret("fd_pipe error"); 20 | return(-1); 21 | } 22 | if ((pid = fork()) < 0) { 23 | err_ret("fork error"); 24 | return(-1); 25 | } else if (pid == 0) { /* child */ 26 | close(fd[0]); 27 | if (fd[1] != STDIN_FILENO && 28 | dup2(fd[1], STDIN_FILENO) != STDIN_FILENO) 29 | err_sys("dup2 error to stdin"); 30 | if (fd[1] != STDOUT_FILENO && 31 | dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) 32 | err_sys("dup2 error to stdout"); 33 | if (execl("./opend", "opend", (char *)0) < 0) 34 | err_sys("execl error"); 35 | } 36 | close(fd[1]); /* parent */ 37 | } 38 | sprintf(buf, " %d", oflag); /* oflag to ascii */ 39 | iov[0].iov_base = CL_OPEN " "; /* string concatenation */ 40 | iov[0].iov_len = strlen(CL_OPEN) + 1; 41 | iov[1].iov_base = name; 42 | iov[1].iov_len = strlen(name); 43 | iov[2].iov_base = buf; 44 | iov[2].iov_len = strlen(buf) + 1; /* +1 for null at end of buf */ 45 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; 46 | if (writev(fd[0], &iov[0], 3) != len) { 47 | err_ret("writev error"); 48 | return(-1); 49 | } 50 | 51 | /* read descriptor, returned errors handled by write() */ 52 | return(recv_fd(fd[0], write)); 53 | } 54 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc2/open/open.c: -------------------------------------------------------------------------------- 1 | #include "open.h" 2 | #include /* struct iovec */ 3 | 4 | /* 5 | * Open the file by sending the "name" and "oflag" to the 6 | * connection server and reading a file descriptor back. 7 | */ 8 | int 9 | csopen(char *name, int oflag) 10 | { 11 | int len; 12 | char buf[12]; 13 | struct iovec iov[3]; 14 | static int csfd = -1; 15 | 16 | if (csfd < 0) { /* open connection to conn server */ 17 | if ((csfd = cli_conn(CS_OPEN)) < 0) { 18 | err_ret("cli_conn error"); 19 | return(-1); 20 | } 21 | } 22 | 23 | sprintf(buf, " %d", oflag); /* oflag to ascii */ 24 | iov[0].iov_base = CL_OPEN " "; /* string concatenation */ 25 | iov[0].iov_len = strlen(CL_OPEN) + 1; 26 | iov[1].iov_base = name; 27 | iov[1].iov_len = strlen(name); 28 | iov[2].iov_base = buf; 29 | iov[2].iov_len = strlen(buf) + 1; /* null always sent */ 30 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; 31 | if (writev(csfd, &iov[0], 3) != len) { 32 | err_ret("writev error"); 33 | return(-1); 34 | } 35 | 36 | /* read back descriptor; returned errors handled by write() */ 37 | return(recv_fd(csfd, write)); 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc2/opend/client.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | 3 | #define NALLOC 10 /* # client structs to alloc/realloc for */ 4 | 5 | static void 6 | client_alloc(void) /* alloc more entries in the client[] array */ 7 | { 8 | int i; 9 | 10 | if (client == NULL) 11 | client = malloc(NALLOC * sizeof(Client)); 12 | else 13 | client = realloc(client, (client_size+NALLOC)*sizeof(Client)); 14 | if (client == NULL) 15 | err_sys("can't alloc for client array"); 16 | 17 | /* initialize the new entries */ 18 | for (i = client_size; i < client_size + NALLOC; i++) 19 | client[i].fd = -1; /* fd of -1 means entry available */ 20 | 21 | client_size += NALLOC; 22 | } 23 | 24 | /* 25 | * Called by loop() when connection request from a new client arrives. 26 | */ 27 | int 28 | client_add(int fd, uid_t uid) 29 | { 30 | int i; 31 | 32 | if (client == NULL) /* first time we're called */ 33 | client_alloc(); 34 | again: 35 | for (i = 0; i < client_size; i++) { 36 | if (client[i].fd == -1) { /* find an available entry */ 37 | client[i].fd = fd; 38 | client[i].uid = uid; 39 | return(i); /* return index in client[] array */ 40 | } 41 | } 42 | 43 | /* client array full, time to realloc for more */ 44 | client_alloc(); 45 | goto again; /* and search again (will work this time) */ 46 | } 47 | 48 | /* 49 | * Called by loop() when we're done with a client. 50 | */ 51 | void 52 | client_del(int fd) 53 | { 54 | int i; 55 | 56 | for (i = 0; i < client_size; i++) { 57 | if (client[i].fd == fd) { 58 | client[i].fd = -1; 59 | return; 60 | } 61 | } 62 | log_quit("can't find client entry for fd %d", fd); 63 | } 64 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ipc2/opend/request.c: -------------------------------------------------------------------------------- 1 | #include "opend.h" 2 | #include 3 | 4 | void 5 | handle_request(char *buf, int nread, int clifd, uid_t uid) 6 | { 7 | int newfd; 8 | 9 | if (buf[nread-1] != 0) { 10 | snprintf(errmsg, MAXLINE-1, 11 | "request from uid %d not null terminated: %*.*s\n", 12 | uid, nread, nread, buf); 13 | send_err(clifd, -1, errmsg); 14 | return; 15 | } 16 | log_msg("request: %s, from uid %d", buf, uid); 17 | 18 | /* parse the arguments, set options */ 19 | if (buf_args(buf, cli_args) < 0) { 20 | send_err(clifd, -1, errmsg); 21 | log_msg(errmsg); 22 | return; 23 | } 24 | 25 | if ((newfd = open(pathname, oflag)) < 0) { 26 | snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", 27 | pathname, strerror(errno)); 28 | send_err(clifd, -1, errmsg); 29 | log_msg(errmsg); 30 | return; 31 | } 32 | 33 | /* send the descriptor */ 34 | if (send_fd(clifd, newfd) < 0) 35 | log_sys("send_fd error"); 36 | log_msg("sent fd %d over fd %d for %s", newfd, clifd, pathname); 37 | close(newfd); /* we're done with descriptor */ 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/bufargs.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define MAXARGC 50 /* max number of arguments in buf */ 4 | #define WHITE " \t\n" /* white space for tokenizing arguments */ 5 | 6 | /* 7 | * buf[] contains white-space-separated arguments. We convert it to an 8 | * argv-style array of pointers, and call the user's function (optfunc) 9 | * to process the array. We return -1 if there's a problem parsing buf, 10 | * else we return whatever optfunc() returns. Note that user's buf[] 11 | * array is modified (nulls placed after each token). 12 | */ 13 | int 14 | buf_args(char *buf, int (*optfunc)(int, char **)) 15 | { 16 | char *ptr, *argv[MAXARGC]; 17 | int argc; 18 | 19 | if (strtok(buf, WHITE) == NULL) /* an argv[0] is required */ 20 | return(-1); 21 | argv[argc = 0] = buf; 22 | while ((ptr = strtok(NULL, WHITE)) != NULL) { 23 | if (++argc >= MAXARGC-1) /* -1 for room for NULL at end */ 24 | return(-1); 25 | argv[argc] = ptr; 26 | } 27 | argv[++argc] = NULL; 28 | 29 | /* 30 | * Since argv[] pointers point into the user's buf[], 31 | * user's function can just copy the pointers, even 32 | * though argv[] array will disappear on return. 33 | */ 34 | return((*optfunc)(argc, argv)); 35 | } 36 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/pathalloc.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #ifdef PATH_MAX 6 | static long pathmax = PATH_MAX; 7 | #else 8 | static long pathmax = 0; 9 | #endif 10 | 11 | static long posix_version = 0; 12 | static long xsi_version = 0; 13 | 14 | /* If PATH_MAX is indeterminate, no guarantee this is adequate */ 15 | #define PATH_MAX_GUESS 1024 16 | 17 | char * 18 | path_alloc(size_t *sizep) /* also return allocated size, if nonnull */ 19 | { 20 | char *ptr; 21 | size_t size; 22 | 23 | if (posix_version == 0) 24 | posix_version = sysconf(_SC_VERSION); 25 | 26 | if (xsi_version == 0) 27 | xsi_version = sysconf(_SC_XOPEN_VERSION); 28 | 29 | if (pathmax == 0) { /* first time through */ 30 | errno = 0; 31 | if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) { 32 | if (errno == 0) 33 | pathmax = PATH_MAX_GUESS; /* it's indeterminate */ 34 | else 35 | err_sys("pathconf error for _PC_PATH_MAX"); 36 | } else { 37 | pathmax++; /* add one since it's relative to root */ 38 | } 39 | } 40 | 41 | /* 42 | * Before POSIX.1-2001, we aren't guaranteed that PATH_MAX includes 43 | * the terminating null byte. Same goes for XPG3. 44 | */ 45 | if ((posix_version < 200112L) && (xsi_version < 4)) 46 | size = pathmax + 1; 47 | else 48 | size = pathmax; 49 | 50 | if ((ptr = malloc(size)) == NULL) 51 | err_sys("malloc error for pathname"); 52 | 53 | if (sizep != NULL) 54 | *sizep = size; 55 | return(ptr); 56 | } 57 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/ptyopen.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | #if defined(SOLARIS) 5 | #include 6 | #endif 7 | 8 | int 9 | ptym_open(char *pts_name, int pts_namesz) 10 | { 11 | char *ptr; 12 | int fdm, err; 13 | 14 | if ((fdm = posix_openpt(O_RDWR)) < 0) 15 | return(-1); 16 | if (grantpt(fdm) < 0) /* grant access to slave */ 17 | goto errout; 18 | if (unlockpt(fdm) < 0) /* clear slave's lock flag */ 19 | goto errout; 20 | if ((ptr = ptsname(fdm)) == NULL) /* get slave's name */ 21 | goto errout; 22 | 23 | /* 24 | * Return name of slave. Null terminate to handle 25 | * case where strlen(ptr) > pts_namesz. 26 | */ 27 | strncpy(pts_name, ptr, pts_namesz); 28 | pts_name[pts_namesz - 1] = '\0'; 29 | return(fdm); /* return fd of master */ 30 | errout: 31 | err = errno; 32 | close(fdm); 33 | errno = err; 34 | return(-1); 35 | } 36 | 37 | int 38 | ptys_open(char *pts_name) 39 | { 40 | int fds; 41 | #if defined(SOLARIS) 42 | int err, setup; 43 | #endif 44 | 45 | if ((fds = open(pts_name, O_RDWR)) < 0) 46 | return(-1); 47 | 48 | #if defined(SOLARIS) 49 | /* 50 | * Check if stream is already set up by autopush facility. 51 | */ 52 | if ((setup = ioctl(fds, I_FIND, "ldterm")) < 0) 53 | goto errout; 54 | 55 | if (setup == 0) { 56 | if (ioctl(fds, I_PUSH, "ptem") < 0) 57 | goto errout; 58 | if (ioctl(fds, I_PUSH, "ldterm") < 0) 59 | goto errout; 60 | if (ioctl(fds, I_PUSH, "ttcompat") < 0) { 61 | errout: 62 | err = errno; 63 | close(fds); 64 | errno = err; 65 | return(-1); 66 | } 67 | } 68 | #endif 69 | return(fds); 70 | } 71 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/sendfd.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | /* size of control buffer to send/recv one file descriptor */ 5 | #define CONTROLLEN CMSG_LEN(sizeof(int)) 6 | 7 | static struct cmsghdr *cmptr = NULL; /* malloc'ed first time */ 8 | 9 | /* 10 | * Pass a file descriptor to another process. 11 | * If fd<0, then -fd is sent back instead as the error status. 12 | */ 13 | int 14 | send_fd(int fd, int fd_to_send) 15 | { 16 | struct iovec iov[1]; 17 | struct msghdr msg; 18 | char buf[2]; /* send_fd()/recv_fd() 2-byte protocol */ 19 | 20 | iov[0].iov_base = buf; 21 | iov[0].iov_len = 2; 22 | msg.msg_iov = iov; 23 | msg.msg_iovlen = 1; 24 | msg.msg_name = NULL; 25 | msg.msg_namelen = 0; 26 | 27 | if (fd_to_send < 0) { 28 | msg.msg_control = NULL; 29 | msg.msg_controllen = 0; 30 | buf[1] = -fd_to_send; /* nonzero status means error */ 31 | if (buf[1] == 0) 32 | buf[1] = 1; /* -256, etc. would screw up protocol */ 33 | } else { 34 | if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL) 35 | return(-1); 36 | cmptr->cmsg_level = SOL_SOCKET; 37 | cmptr->cmsg_type = SCM_RIGHTS; 38 | cmptr->cmsg_len = CONTROLLEN; 39 | msg.msg_control = cmptr; 40 | msg.msg_controllen = CONTROLLEN; 41 | *(int *)CMSG_DATA(cmptr) = fd_to_send; /* the fd to pass */ 42 | buf[1] = 0; /* zero status means OK */ 43 | } 44 | 45 | buf[0] = 0; /* null byte flag to recv_fd() */ 46 | if (sendmsg(fd, &msg, 0) != 2) 47 | return(-1); 48 | return(0); 49 | } 50 | -------------------------------------------------------------------------------- /lib/servlisten.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #define QLEN 10 7 | 8 | /* 9 | * Create a server endpoint of a connection. 10 | * Returns fd if all OK, <0 on error. 11 | */ 12 | int 13 | serv_listen(const char *name) 14 | { 15 | int fd, len, err, rval; 16 | struct sockaddr_un un; 17 | 18 | if (strlen(name) >= sizeof(un.sun_path)) { 19 | errno = ENAMETOOLONG; 20 | return(-1); 21 | } 22 | 23 | /* create a UNIX domain stream socket */ 24 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 25 | return(-2); 26 | 27 | unlink(name); /* in case it already exists */ 28 | 29 | /* fill in socket address structure */ 30 | memset(&un, 0, sizeof(un)); 31 | un.sun_family = AF_UNIX; 32 | strcpy(un.sun_path, name); 33 | len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 34 | 35 | /* bind the name to the descriptor */ 36 | if (bind(fd, (struct sockaddr *)&un, len) < 0) { 37 | rval = -3; 38 | goto errout; 39 | } 40 | 41 | if (listen(fd, QLEN) < 0) { /* tell kernel we're a server */ 42 | rval = -4; 43 | goto errout; 44 | } 45 | return(fd); 46 | 47 | errout: 48 | err = errno; 49 | close(fd); 50 | errno = err; 51 | return(rval); 52 | } 53 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/sleep.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void 4 | sig_alrm(int signo) 5 | { 6 | /* nothing to do, just returning wakes up sigsuspend() */ 7 | } 8 | 9 | unsigned int 10 | sleep(unsigned int seconds) 11 | { 12 | struct sigaction newact, oldact; 13 | sigset_t newmask, oldmask, suspmask; 14 | unsigned int unslept; 15 | 16 | /* set our handler, save previous information */ 17 | newact.sa_handler = sig_alrm; 18 | sigemptyset(&newact.sa_mask); 19 | newact.sa_flags = 0; 20 | sigaction(SIGALRM, &newact, &oldact); 21 | 22 | /* block SIGALRM and save current signal mask */ 23 | sigemptyset(&newmask); 24 | sigaddset(&newmask, SIGALRM); 25 | sigprocmask(SIG_BLOCK, &newmask, &oldmask); 26 | 27 | alarm(seconds); 28 | suspmask = oldmask; 29 | 30 | /* make sure SIGALRM isn't blocked */ 31 | sigdelset(&suspmask, SIGALRM); 32 | 33 | /* wait for any signal to be caught */ 34 | sigsuspend(&suspmask); 35 | 36 | /* some signal has been caught, SIGALRM is now blocked */ 37 | 38 | unslept = alarm(0); 39 | 40 | /* reset previous action */ 41 | sigaction(SIGALRM, &oldact, NULL); 42 | 43 | /* reset signal mask, which unblocks SIGALRM */ 44 | sigprocmask(SIG_SETMASK, &oldmask, NULL); 45 | return(unslept); 46 | } 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/tellwait.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */ 4 | static sigset_t newmask, oldmask, zeromask; 5 | 6 | static void 7 | sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */ 8 | { 9 | sigflag = 1; 10 | } 11 | 12 | void 13 | TELL_WAIT(void) 14 | { 15 | if (signal(SIGUSR1, sig_usr) == SIG_ERR) 16 | err_sys("signal(SIGUSR1) error"); 17 | if (signal(SIGUSR2, sig_usr) == SIG_ERR) 18 | err_sys("signal(SIGUSR2) error"); 19 | sigemptyset(&zeromask); 20 | sigemptyset(&newmask); 21 | sigaddset(&newmask, SIGUSR1); 22 | sigaddset(&newmask, SIGUSR2); 23 | 24 | /* Block SIGUSR1 and SIGUSR2, and save current signal mask */ 25 | if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 26 | err_sys("SIG_BLOCK error"); 27 | } 28 | 29 | void 30 | TELL_PARENT(pid_t pid) 31 | { 32 | kill(pid, SIGUSR2); /* tell parent we're done */ 33 | } 34 | 35 | void 36 | WAIT_PARENT(void) 37 | { 38 | while (sigflag == 0) 39 | sigsuspend(&zeromask); /* and wait for parent */ 40 | sigflag = 0; 41 | 42 | /* Reset signal mask to original value */ 43 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 44 | err_sys("SIG_SETMASK error"); 45 | } 46 | 47 | void 48 | TELL_CHILD(pid_t pid) 49 | { 50 | kill(pid, SIGUSR1); /* tell child we're done */ 51 | } 52 | 53 | void 54 | WAIT_CHILD(void) 55 | { 56 | while (sigflag == 0) 57 | sigsuspend(&zeromask); /* and wait for child */ 58 | sigflag = 0; 59 | 60 | /* Reset signal mask to original value */ 61 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 62 | err_sys("SIG_SETMASK error"); 63 | } 64 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /printer/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | EXTRA= 5 | 6 | ifeq "$(PLATFORM)" "solaris" 7 | EXTRALIBS=-lsocket -lnsl -lrt -lpthread 8 | else 9 | EXTRALIBS=-pthread 10 | endif 11 | 12 | PROGS = print printd 13 | HDRS = print.h ipp.h 14 | 15 | all: $(PROGS) 16 | 17 | util.o: util.c $(HDRS) 18 | 19 | print.o: print.c $(HDRS) 20 | 21 | printd.o: printd.c $(HDRS) 22 | 23 | print: print.o util.o $(ROOT)/sockets/clconn2.o $(LIBAPUE) 24 | $(CC) $(CFLAGS) -o print print.o util.o $(ROOT)/sockets/clconn2.o $(LDFLAGS) $(LDDIR) $(LDLIBS) 25 | 26 | printd: printd.o util.o $(ROOT)/sockets/clconn2.o $(ROOT)/sockets/initsrv2.o $(LIBAPUE) 27 | $(CC) $(CFLAGS) -o printd printd.o util.o $(ROOT)/sockets/clconn2.o $(ROOT)/sockets/initsrv2.o \ 28 | $(LDFLAGS) $(LDDIR) $(LDLIBS) 29 | 30 | clean: 31 | rm -f $(PROGS) $(TEMPFILES) *.o 32 | 33 | include $(ROOT)/Make.libapue.inc 34 | -------------------------------------------------------------------------------- /proc/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | ACCT= 6 | ACCTFLAGS= 7 | ifeq "$(PLATFORM)" "linux" 8 | ACCT=pracct 9 | ACCTFLAGS = -DHAS_AXSIG -DHAS_ACORE 10 | endif 11 | ifeq "$(PLATFORM)" "macos" 12 | ACCT=pracct 13 | ACCTFLAGS = -DHAS_AXSIG -DHAS_ACORE 14 | endif 15 | ifeq "$(PLATFORM)" "solaris" 16 | ACCT=pracct 17 | ACCTFLAGS = -DHAS_AC_STAT 18 | endif 19 | 20 | PROGS = echoall exec1 exec2 fork1 fork2 nice pruids tellwait1 tellwait2 test1 times1 vfork1 wait1 21 | MOREPROGS = systest1 systest3 22 | 23 | all: $(PROGS) $(MOREPROGS) system.o $(ACCT) 24 | 25 | %: %.c $(LIBAPUE) 26 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 27 | 28 | systest1: system.o systest1.o $(LIBAPUE) 29 | $(CC) $(CFLAGS) -o systest1 systest1.o system.o $(LDFLAGS) $(LDLIBS) 30 | 31 | systest3: system.o systest3.o $(LIBAPUE) 32 | $(CC) $(CFLAGS) -o systest3 systest3.o system.o $(LDFLAGS) $(LDLIBS) 33 | 34 | pracct: pracct.c $(LIBAPUE) 35 | $(CC) $(CFLAGS) $(ACCTFLAGS) -o pracct pracct.c $(LDFLAGS) $(LDLIBS) 36 | 37 | clean: 38 | rm -f $(PROGS) $(MOREPROGS) $(TEMPFILES) *.o $(ACCT) 39 | 40 | include $(ROOT)/Make.libapue.inc 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /proc/fork2.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) { /* first child */ 12 | if ((pid = fork()) < 0) 13 | err_sys("fork error"); 14 | else if (pid > 0) 15 | exit(0); /* parent from second fork == first child */ 16 | 17 | /* 18 | * We're the second child; our parent becomes init as soon 19 | * as our real parent calls exit() in the statement above. 20 | * Here's where we'd continue executing, knowing that when 21 | * we're done, init will reap our status. 22 | */ 23 | sleep(2); 24 | printf("second child, parent pid = %ld\n", (long)getppid()); 25 | exit(0); 26 | } 27 | 28 | if (waitpid(pid, NULL, 0) != pid) /* wait for first child */ 29 | err_sys("waitpid error"); 30 | 31 | /* 32 | * We're the parent (the original process); we continue executing, 33 | * knowing that we're not the parent of the second child. 34 | */ 35 | exit(0); 36 | } 37 | -------------------------------------------------------------------------------- /proc/nice.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | #if defined(MACOS) 6 | #include 7 | #elif defined(SOLARIS) 8 | #include 9 | #elif defined(BSD) 10 | #include 11 | #endif 12 | 13 | unsigned long long count; 14 | struct timeval end; 15 | 16 | void 17 | checktime(char *str) 18 | { 19 | struct timeval tv; 20 | 21 | gettimeofday(&tv, NULL); 22 | if (tv.tv_sec >= end.tv_sec && tv.tv_usec >= end.tv_usec) { 23 | printf("%s count = %lld\n", str, count); 24 | exit(0); 25 | } 26 | } 27 | 28 | int 29 | main(int argc, char *argv[]) 30 | { 31 | pid_t pid; 32 | char *s; 33 | int nzero, ret; 34 | int adj = 0; 35 | 36 | setbuf(stdout, NULL); 37 | #if defined(NZERO) 38 | nzero = NZERO; 39 | #elif defined(_SC_NZERO) 40 | nzero = sysconf(_SC_NZERO); 41 | #else 42 | #error NZERO undefined 43 | #endif 44 | printf("NZERO = %d\n", nzero); 45 | if (argc == 2) 46 | adj = strtol(argv[1], NULL, 10); 47 | gettimeofday(&end, NULL); 48 | end.tv_sec += 10; /* run for 10 seconds */ 49 | 50 | if ((pid = fork()) < 0) { 51 | err_sys("fork failed"); 52 | } else if (pid == 0) { /* child */ 53 | s = "child"; 54 | printf("current nice value in child is %d, adjusting by %d\n", 55 | nice(0)+nzero, adj); 56 | errno = 0; 57 | if ((ret = nice(adj)) == -1 && errno != 0) 58 | err_sys("child set scheduling priority"); 59 | printf("now child nice value is %d\n", ret+nzero); 60 | } else { /* parent */ 61 | s = "parent"; 62 | printf("current nice value in parent is %d\n", nice(0)+nzero); 63 | } 64 | for(;;) { 65 | if (++count == 0) 66 | err_quit("%s counter wrap", s); 67 | checktime(s); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /proc/test1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | int 4 | main(void) 5 | { 6 | pid_t pid; 7 | 8 | if ((pid = fork()) < 0) 9 | err_sys("fork error"); 10 | else if (pid != 0) { /* parent */ 11 | sleep(2); 12 | exit(2); /* terminate with exit status 2 */ 13 | } 14 | 15 | if ((pid = fork()) < 0) 16 | err_sys("fork error"); 17 | else if (pid != 0) { /* first child */ 18 | sleep(4); 19 | abort(); /* terminate with core dump */ 20 | } 21 | 22 | if ((pid = fork()) < 0) 23 | err_sys("fork error"); 24 | else if (pid != 0) { /* second child */ 25 | execl("/bin/dd", "dd", "if=/etc/passwd", "of=/dev/null", NULL); 26 | exit(7); /* shouldn't get here */ 27 | } 28 | 29 | if ((pid = fork()) < 0) 30 | err_sys("fork error"); 31 | else if (pid != 0) { /* third child */ 32 | sleep(8); 33 | exit(0); /* normal exit */ 34 | } 35 | 36 | sleep(6); /* fourth child */ 37 | kill(getpid(), SIGKILL); /* terminate w/signal, no core dump */ 38 | exit(6); /* shouldn't get here */ 39 | } 40 | -------------------------------------------------------------------------------- /proc/times1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void pr_times(clock_t, struct tms *, struct tms *); 5 | static void do_cmd(char *); 6 | 7 | int 8 | main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | setbuf(stdout, NULL); 13 | for (i = 1; i < argc; i++) 14 | do_cmd(argv[i]); /* once for each command-line arg */ 15 | exit(0); 16 | } 17 | 18 | static void 19 | do_cmd(char *cmd) /* execute and time the "cmd" */ 20 | { 21 | struct tms tmsstart, tmsend; 22 | clock_t start, end; 23 | int status; 24 | 25 | printf("\ncommand: %s\n", cmd); 26 | 27 | if ((start = times(&tmsstart)) == -1) /* starting values */ 28 | err_sys("times error"); 29 | 30 | if ((status = system(cmd)) < 0) /* execute command */ 31 | err_sys("system() error"); 32 | 33 | if ((end = times(&tmsend)) == -1) /* ending values */ 34 | err_sys("times error"); 35 | 36 | pr_times(end-start, &tmsstart, &tmsend); 37 | pr_exit(status); 38 | } 39 | 40 | static void 41 | pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend) 42 | { 43 | static long clktck = 0; 44 | 45 | if (clktck == 0) /* fetch clock ticks per second first time */ 46 | if ((clktck = sysconf(_SC_CLK_TCK)) < 0) 47 | err_sys("sysconf error"); 48 | 49 | printf(" real: %7.2f\n", real / (double) clktck); 50 | printf(" user: %7.2f\n", 51 | (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck); 52 | printf(" sys: %7.2f\n", 53 | (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck); 54 | printf(" child user: %7.2f\n", 55 | (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck); 56 | printf(" child sys: %7.2f\n", 57 | (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck); 58 | } 59 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /proc/wait1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | pid_t pid; 8 | int status; 9 | 10 | if ((pid = fork()) < 0) 11 | err_sys("fork error"); 12 | else if (pid == 0) /* child */ 13 | exit(7); 14 | 15 | if (wait(&status) != pid) /* wait for child */ 16 | err_sys("wait error"); 17 | pr_exit(status); /* and print its status */ 18 | 19 | if ((pid = fork()) < 0) 20 | err_sys("fork error"); 21 | else if (pid == 0) /* child */ 22 | abort(); /* generates SIGABRT */ 23 | 24 | if (wait(&status) != pid) /* wait for child */ 25 | err_sys("wait error"); 26 | pr_exit(status); /* and print its status */ 27 | 28 | if ((pid = fork()) < 0) 29 | err_sys("fork error"); 30 | else if (pid == 0) /* child */ 31 | status /= 0; /* divide by 0 generates SIGFPE */ 32 | 33 | if (wait(&status) != pid) /* wait for child */ 34 | err_sys("wait error"); 35 | pr_exit(status); /* and print its status */ 36 | 37 | exit(0); 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /pty/driver.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | void 4 | do_driver(char *driver) 5 | { 6 | pid_t child; 7 | int pipe[2]; 8 | 9 | /* 10 | * Create a full-duplex pipe to communicate with the driver. 11 | */ 12 | if (fd_pipe(pipe) < 0) 13 | err_sys("can't create stream pipe"); 14 | 15 | if ((child = fork()) < 0) { 16 | err_sys("fork error"); 17 | } else if (child == 0) { /* child */ 18 | close(pipe[1]); 19 | 20 | /* stdin for driver */ 21 | if (dup2(pipe[0], STDIN_FILENO) != STDIN_FILENO) 22 | err_sys("dup2 error to stdin"); 23 | 24 | /* stdout for driver */ 25 | if (dup2(pipe[0], STDOUT_FILENO) != STDOUT_FILENO) 26 | err_sys("dup2 error to stdout"); 27 | if (pipe[0] != STDIN_FILENO && pipe[0] != STDOUT_FILENO) 28 | close(pipe[0]); 29 | 30 | /* leave stderr for driver alone */ 31 | execlp(driver, driver, (char *)0); 32 | err_sys("execlp error for: %s", driver); 33 | } 34 | 35 | close(pipe[0]); /* parent */ 36 | if (dup2(pipe[1], STDIN_FILENO) != STDIN_FILENO) 37 | err_sys("dup2 error to stdin"); 38 | if (dup2(pipe[1], STDOUT_FILENO) != STDOUT_FILENO) 39 | err_sys("dup2 error to stdout"); 40 | if (pipe[1] != STDIN_FILENO && pipe[1] != STDOUT_FILENO) 41 | close(pipe[1]); 42 | 43 | /* 44 | * Parent returns, but with stdin and stdout connected 45 | * to the driver. 46 | */ 47 | } 48 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /relation/orphan3.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | static void 5 | sig_hup(int signo) 6 | { 7 | printf("SIGHUP received, pid = %ld\n", (long)getpid()); 8 | } 9 | 10 | static void 11 | pr_ids(char *name) 12 | { 13 | printf("%s: pid = %ld, ppid = %ld, pgrp = %ld, tpgrp = %ld\n", 14 | name, (long)getpid(), (long)getppid(), (long)getpgrp(), 15 | (long)tcgetpgrp(STDIN_FILENO)); 16 | fflush(stdout); 17 | } 18 | 19 | int 20 | main(void) 21 | { 22 | char c; 23 | pid_t pid; 24 | 25 | pr_ids("parent"); 26 | if ((pid = fork()) < 0) { 27 | err_sys("fork error"); 28 | } else if (pid > 0) { /* parent */ 29 | sleep(5); /* sleep to let child stop itself */ 30 | } else { /* child */ 31 | pr_ids("child"); 32 | signal(SIGHUP, sig_hup); /* establish signal handler */ 33 | kill(getpid(), SIGTSTP); /* stop ourself */ 34 | pr_ids("child"); /* prints only if we're continued */ 35 | if (read(STDIN_FILENO, &c, 1) != 1) 36 | printf("read error %d on controlling TTY\n", errno); 37 | } 38 | exit(0); 39 | } 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /signals/abort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void 7 | abort(void) /* POSIX-style abort() function */ 8 | { 9 | sigset_t mask; 10 | struct sigaction action; 11 | 12 | /* Caller can't ignore SIGABRT, if so reset to default */ 13 | sigaction(SIGABRT, NULL, &action); 14 | if (action.sa_handler == SIG_IGN) { 15 | action.sa_handler = SIG_DFL; 16 | sigaction(SIGABRT, &action, NULL); 17 | } 18 | if (action.sa_handler == SIG_DFL) 19 | fflush(NULL); /* flush all open stdio streams */ 20 | 21 | /* Caller can't block SIGABRT; make sure it's unblocked */ 22 | sigfillset(&mask); 23 | sigdelset(&mask, SIGABRT); /* mask has only SIGABRT turned off */ 24 | sigprocmask(SIG_SETMASK, &mask, NULL); 25 | kill(getpid(), SIGABRT); /* send the signal */ 26 | 27 | /* If we're here, process caught SIGABRT and returned */ 28 | fflush(NULL); /* flush all open stdio streams */ 29 | action.sa_handler = SIG_DFL; 30 | sigaction(SIGABRT, &action, NULL); /* reset to default */ 31 | sigprocmask(SIG_SETMASK, &mask, NULL); /* just in case ... */ 32 | kill(getpid(), SIGABRT); /* and one more time */ 33 | exit(1); /* this should never be executed ... */ 34 | } 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /signals/critical.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void sig_quit(int); 4 | 5 | int 6 | main(void) 7 | { 8 | sigset_t newmask, oldmask, pendmask; 9 | 10 | if (signal(SIGQUIT, sig_quit) == SIG_ERR) 11 | err_sys("can't catch SIGQUIT"); 12 | 13 | /* 14 | * Block SIGQUIT and save current signal mask. 15 | */ 16 | sigemptyset(&newmask); 17 | sigaddset(&newmask, SIGQUIT); 18 | if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 19 | err_sys("SIG_BLOCK error"); 20 | 21 | sleep(5); /* SIGQUIT here will remain pending */ 22 | 23 | if (sigpending(&pendmask) < 0) 24 | err_sys("sigpending error"); 25 | if (sigismember(&pendmask, SIGQUIT)) 26 | printf("\nSIGQUIT pending\n"); 27 | 28 | /* 29 | * Restore signal mask which unblocks SIGQUIT. 30 | */ 31 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 32 | err_sys("SIG_SETMASK error"); 33 | printf("SIGQUIT unblocked\n"); 34 | 35 | sleep(5); /* SIGQUIT here will terminate with core file */ 36 | exit(0); 37 | } 38 | 39 | static void 40 | sig_quit(int signo) 41 | { 42 | printf("caught SIGQUIT\n"); 43 | if (signal(SIGQUIT, SIG_DFL) == SIG_ERR) 44 | err_sys("can't reset SIGQUIT"); 45 | } 46 | -------------------------------------------------------------------------------- /signals/mask.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | 5 | static void sig_usr1(int); 6 | static void sig_alrm(int); 7 | static sigjmp_buf jmpbuf; 8 | static volatile sig_atomic_t canjump; 9 | 10 | int 11 | main(void) 12 | { 13 | if (signal(SIGUSR1, sig_usr1) == SIG_ERR) 14 | err_sys("signal(SIGUSR1) error"); 15 | if (signal(SIGALRM, sig_alrm) == SIG_ERR) 16 | err_sys("signal(SIGALRM) error"); 17 | 18 | pr_mask("starting main: "); /* {Prog prmask} */ 19 | 20 | if (sigsetjmp(jmpbuf, 1)) { 21 | 22 | pr_mask("ending main: "); 23 | 24 | exit(0); 25 | } 26 | canjump = 1; /* now sigsetjmp() is OK */ 27 | 28 | for ( ; ; ) 29 | pause(); 30 | } 31 | 32 | static void 33 | sig_usr1(int signo) 34 | { 35 | time_t starttime; 36 | 37 | if (canjump == 0) 38 | return; /* unexpected signal, ignore */ 39 | 40 | pr_mask("starting sig_usr1: "); 41 | 42 | alarm(3); /* SIGALRM in 3 seconds */ 43 | starttime = time(NULL); 44 | for ( ; ; ) /* busy wait for 5 seconds */ 45 | if (time(NULL) > starttime + 5) 46 | break; 47 | 48 | pr_mask("finishing sig_usr1: "); 49 | 50 | canjump = 0; 51 | siglongjmp(jmpbuf, 1); /* jump back to main, don't return */ 52 | } 53 | 54 | static void 55 | sig_alrm(int signo) 56 | { 57 | pr_mask("in sig_alrm: "); 58 | } 59 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /signals/sigtstp.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define BUFFSIZE 1024 4 | 5 | static void 6 | sig_tstp(int signo) /* signal handler for SIGTSTP */ 7 | { 8 | sigset_t mask; 9 | 10 | /* ... move cursor to lower left corner, reset tty mode ... */ 11 | 12 | /* 13 | * Unblock SIGTSTP, since it's blocked while we're handling it. 14 | */ 15 | sigemptyset(&mask); 16 | sigaddset(&mask, SIGTSTP); 17 | sigprocmask(SIG_UNBLOCK, &mask, NULL); 18 | 19 | signal(SIGTSTP, SIG_DFL); /* reset disposition to default */ 20 | 21 | kill(getpid(), SIGTSTP); /* and send the signal to ourself */ 22 | 23 | /* we won't return from the kill until we're continued */ 24 | 25 | signal(SIGTSTP, sig_tstp); /* reestablish signal handler */ 26 | 27 | /* ... reset tty mode, redraw screen ... */ 28 | } 29 | 30 | int 31 | main(void) 32 | { 33 | int n; 34 | char buf[BUFFSIZE]; 35 | 36 | /* 37 | * Only catch SIGTSTP if we're running with a job-control shell. 38 | */ 39 | if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) 40 | signal(SIGTSTP, sig_tstp); 41 | 42 | while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) 43 | if (write(STDOUT_FILENO, buf, n) != n) 44 | err_sys("write error"); 45 | 46 | if (n < 0) 47 | err_sys("read error"); 48 | 49 | exit(0); 50 | } 51 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /signals/suspend1.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void sig_int(int); 4 | 5 | int 6 | main(void) 7 | { 8 | sigset_t newmask, oldmask, waitmask; 9 | 10 | pr_mask("program start: "); 11 | 12 | if (signal(SIGINT, sig_int) == SIG_ERR) 13 | err_sys("signal(SIGINT) error"); 14 | sigemptyset(&waitmask); 15 | sigaddset(&waitmask, SIGUSR1); 16 | sigemptyset(&newmask); 17 | sigaddset(&newmask, SIGINT); 18 | 19 | /* 20 | * Block SIGINT and save current signal mask. 21 | */ 22 | if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 23 | err_sys("SIG_BLOCK error"); 24 | 25 | /* 26 | * Critical region of code. 27 | */ 28 | pr_mask("in critical region: "); 29 | 30 | /* 31 | * Pause, allowing all signals except SIGUSR1. 32 | */ 33 | if (sigsuspend(&waitmask) != -1) 34 | err_sys("sigsuspend error"); 35 | 36 | pr_mask("after return from sigsuspend: "); 37 | 38 | /* 39 | * Reset signal mask which unblocks SIGINT. 40 | */ 41 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 42 | err_sys("SIG_SETMASK error"); 43 | 44 | /* 45 | * And continue processing ... 46 | */ 47 | pr_mask("program exit: "); 48 | 49 | exit(0); 50 | } 51 | 52 | static void 53 | sig_int(int signo) 54 | { 55 | pr_mask("\nin sig_int: "); 56 | } 57 | -------------------------------------------------------------------------------- /signals/suspend2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | volatile sig_atomic_t quitflag; /* set nonzero by signal handler */ 4 | 5 | static void 6 | sig_int(int signo) /* one signal handler for SIGINT and SIGQUIT */ 7 | { 8 | if (signo == SIGINT) 9 | printf("\ninterrupt\n"); 10 | else if (signo == SIGQUIT) 11 | quitflag = 1; /* set flag for main loop */ 12 | } 13 | 14 | int 15 | main(void) 16 | { 17 | sigset_t newmask, oldmask, zeromask; 18 | 19 | if (signal(SIGINT, sig_int) == SIG_ERR) 20 | err_sys("signal(SIGINT) error"); 21 | if (signal(SIGQUIT, sig_int) == SIG_ERR) 22 | err_sys("signal(SIGQUIT) error"); 23 | 24 | sigemptyset(&zeromask); 25 | sigemptyset(&newmask); 26 | sigaddset(&newmask, SIGQUIT); 27 | 28 | /* 29 | * Block SIGQUIT and save current signal mask. 30 | */ 31 | if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 32 | err_sys("SIG_BLOCK error"); 33 | 34 | while (quitflag == 0) 35 | sigsuspend(&zeromask); 36 | 37 | /* 38 | * SIGQUIT has been caught and is now blocked; do whatever. 39 | */ 40 | quitflag = 0; 41 | 42 | /* 43 | * Reset signal mask which unblocks SIGQUIT. 44 | */ 45 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 46 | err_sys("SIG_SETMASK error"); 47 | 48 | exit(0); 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /sockets/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 = ruptime ruptimed ruptimed-fd ruptimed-dg 10 | MOREPROGS = findsvc ruptime-dg 11 | 12 | all: $(PROGS) $(MOREPROGS) clconn.o clconn2.o initsrv1.o initsrv2.o 13 | 14 | %: %.c $(LIBAPUE) 15 | 16 | ruptime: ruptime.o clconn2.o $(LIBAPUE) 17 | $(CC) $(CFLAGS) -o ruptime ruptime.o clconn2.o $(LDFLAGS) $(LDLIBS) 18 | 19 | ruptimed: ruptimed.o initsrv2.o $(LIBAPUE) 20 | $(CC) $(CFLAGS) -o ruptimed ruptimed.o initsrv2.o $(LDFLAGS) $(LDLIBS) 21 | 22 | ruptimed-fd: ruptimed-fd.o initsrv2.o $(LIBAPUE) 23 | $(CC) $(CFLAGS) -o ruptimed-fd ruptimed-fd.o initsrv2.o $(LDFLAGS) $(LDLIBS) 24 | 25 | ruptimed-dg: ruptimed-dg.o initsrv2.o $(LIBAPUE) 26 | $(CC) $(CFLAGS) -o ruptimed-dg ruptimed-dg.o initsrv2.o $(LDFLAGS) $(LDLIBS) 27 | 28 | clean: 29 | rm -f $(PROGS) $(MOREPROGS) $(TEMPFILES) *.o 30 | 31 | include $(ROOT)/Make.libapue.inc 32 | -------------------------------------------------------------------------------- /sockets/ruptime-dg.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #define BUFLEN 128 7 | #define TIMEOUT 20 8 | 9 | void 10 | sigalrm(int signo) 11 | { 12 | } 13 | 14 | void 15 | print_uptime(int sockfd, struct addrinfo *aip) 16 | { 17 | int n; 18 | char buf[BUFLEN]; 19 | 20 | buf[0] = 0; 21 | if (sendto(sockfd, buf, 1, 0, aip->ai_addr, aip->ai_addrlen) < 0) 22 | err_sys("sendto error"); 23 | alarm(TIMEOUT); 24 | if ((n = recvfrom(sockfd, buf, BUFLEN, 0, NULL, NULL)) < 0) { 25 | if (errno != EINTR) 26 | alarm(0); 27 | err_sys("recv error"); 28 | } 29 | alarm(0); 30 | write(STDOUT_FILENO, buf, n); 31 | } 32 | 33 | int 34 | main(int argc, char *argv[]) 35 | { 36 | struct addrinfo *ailist, *aip; 37 | struct addrinfo hint; 38 | int sockfd, err; 39 | struct sigaction sa; 40 | 41 | if (argc != 2) 42 | err_quit("usage: ruptime hostname"); 43 | sa.sa_handler = sigalrm; 44 | sa.sa_flags = 0; 45 | sigemptyset(&sa.sa_mask); 46 | if (sigaction(SIGALRM, &sa, NULL) < 0) 47 | err_sys("sigaction error"); 48 | memset(&hint, 0, sizeof(hint)); 49 | hint.ai_socktype = SOCK_DGRAM; 50 | hint.ai_canonname = NULL; 51 | hint.ai_addr = NULL; 52 | hint.ai_next = NULL; 53 | if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0) 54 | err_quit("getaddrinfo error: %s", gai_strerror(err)); 55 | 56 | for (aip = ailist; aip != NULL; aip = aip->ai_next) { 57 | if ((sockfd = socket(aip->ai_family, SOCK_DGRAM, 0)) < 0) { 58 | err = errno; 59 | } else { 60 | print_uptime(sockfd, aip); 61 | exit(0); 62 | } 63 | } 64 | 65 | fprintf(stderr, "can't contact %s: %s\n", argv[1], strerror(err)); 66 | exit(1); 67 | } 68 | -------------------------------------------------------------------------------- /sockets/ruptime.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #define BUFLEN 128 7 | 8 | extern int connect_retry(int, int, int, const struct sockaddr *, 9 | socklen_t); 10 | 11 | void 12 | print_uptime(int sockfd) 13 | { 14 | int n; 15 | char buf[BUFLEN]; 16 | 17 | while ((n = recv(sockfd, buf, BUFLEN, 0)) > 0) 18 | write(STDOUT_FILENO, buf, n); 19 | if (n < 0) 20 | err_sys("recv error"); 21 | } 22 | 23 | int 24 | main(int argc, char *argv[]) 25 | { 26 | struct addrinfo *ailist, *aip; 27 | struct addrinfo hint; 28 | int sockfd, err; 29 | 30 | if (argc != 2) 31 | err_quit("usage: ruptime hostname"); 32 | memset(&hint, 0, sizeof(hint)); 33 | hint.ai_socktype = SOCK_STREAM; 34 | hint.ai_canonname = NULL; 35 | hint.ai_addr = NULL; 36 | hint.ai_next = NULL; 37 | if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0) 38 | err_quit("getaddrinfo error: %s", gai_strerror(err)); 39 | for (aip = ailist; aip != NULL; aip = aip->ai_next) { 40 | if ((sockfd = connect_retry(aip->ai_family, SOCK_STREAM, 0, 41 | aip->ai_addr, aip->ai_addrlen)) < 0) { 42 | err = errno; 43 | } else { 44 | print_uptime(sockfd); 45 | exit(0); 46 | } 47 | } 48 | err_exit(err, "can't connect to %s", argv[1]); 49 | } 50 | -------------------------------------------------------------------------------- /standards/Makefile: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | PLATFORM=$(shell $(ROOT)/systype.sh) 3 | include $(ROOT)/Make.defines.$(PLATFORM) 4 | 5 | PROGS = conf options 6 | 7 | all: $(PROGS) 8 | 9 | %: %.c $(LIBAPUE) 10 | $(CC) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LDLIBS) 11 | 12 | conf: conf.c 13 | 14 | conf.c: makeconf.awk sysconf.sym pathconf.sym 15 | $(AWK) -f makeconf.awk >conf.c 16 | 17 | sysconf.sym: sysconf-lim.sym 18 | grep -v "^#" sysconf-lim.sym >sysconf.sym 19 | 20 | pathconf.sym: pathconf-lim.sym 21 | grep -v "^#" pathconf-lim.sym >pathconf.sym 22 | 23 | options: options.c 24 | 25 | options.c: makeopt.awk sysopt.sym pathopt.sym 26 | $(AWK) -f makeopt.awk >options.c 27 | 28 | sysopt.sym: sysconf-opt.sym 29 | grep -v "^#" sysconf-opt.sym >sysopt.sym 30 | 31 | pathopt.sym: pathconf-opt.sym 32 | grep -v "^#" pathconf-opt.sym >pathopt.sym 33 | 34 | clean: 35 | rm -f $(PROGS) $(TEMPFILES) *.o conf.c options.c \ 36 | pathconf.sym pathopt.sym sysconf.sym sysopt.sym 37 | 38 | include $(ROOT)/Make.libapue.inc 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /standards/sysconf-lim.sym: -------------------------------------------------------------------------------- 1 | # AIO_LISTIO_MAX _SC_AIO_LISTIO_MAX ; defer to chapter 14 2 | # AIO_MAX _SC_AIO_MAX ; defer to chapter 14 3 | # AIO_PRIO_DELTA_MAX _SC_AIO_PRIO_DELTA_MAX ; defer to chapter 14 4 | ARG_MAX _SC_ARG_MAX 5 | ATEXIT_MAX _SC_ATEXIT_MAX 6 | CHARCLASS_NAME_MAX _SC_CHARCLASS_NAME_MAX 7 | CHILD_MAX _SC_CHILD_MAX 8 | CLOCKTICKSPERSECOND /*clock ticks/second*/ _SC_CLK_TCK 9 | COLL_WEIGHTS_MAX _SC_COLL_WEIGHTS_MAX 10 | DELAYTIMER_MAX _SC_DELAYTIMER_MAX 11 | HOST_NAME_MAX _SC_HOST_NAME_MAX 12 | IOV_MAX _SC_IOV_MAX 13 | LINE_MAX _SC_LINE_MAX 14 | LOGIN_NAME_MAX _SC_LOGIN_NAME_MAX 15 | NGROUPS_MAX _SC_NGROUPS_MAX 16 | # initial size of getgrgid_r() _SC_GETGR_R_SIZE_MAX 17 | # initial size of getpwuid_r() _SC_GETPW_R_SIZE_MAX 18 | OPEN_MAX _SC_OPEN_MAX 19 | PAGESIZE _SC_PAGESIZE 20 | PAGE_SIZE _SC_PAGE_SIZE 21 | # PTHREAD_DESTRUCTOR_ITERATIONS _SC_THREAD_DESTRUCTOR_ITERATIONS ; defer to chapter 12 22 | # PTHREAD_KEYS_MAX _SC_THREAD_KEYS_MAX ; defer to chapter 12 23 | # PTHREAD_STACK_MIN _SC_THREAD_STACK_MIN ; defer to chapter 12 24 | # PTHREAD_THREADS_MAX _SC_THREAD_THREADS_MAX ; defer to chapter 12 25 | RE_DUP_MAX _SC_RE_DUP_MAX 26 | RTSIG_MAX _SC_RTSIG_MAX 27 | SEM_NSEMS_MAX _SC_SEM_NSEMS_MAX 28 | SEM_VALUE_MAX _SC_SEM_VALUE_MAX 29 | SIGQUEUE_MAX _SC_SIGQUEUE_MAX 30 | STREAM_MAX _SC_STREAM_MAX 31 | SYMLOOP_MAX _SC_SYMLOOP_MAX 32 | TIMER_MAX _SC_TIMER_MAX 33 | TTY_NAME_MAX _SC_TTY_NAME_MAX 34 | TZNAME_MAX _SC_TZNAME_MAX 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /stdio/memstr.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | #define BSZ 48 4 | 5 | int 6 | main() 7 | { 8 | FILE *fp; 9 | char buf[BSZ]; 10 | 11 | memset(buf, 'a', BSZ-2); 12 | buf[BSZ-2] = '\0'; 13 | buf[BSZ-1] = 'X'; 14 | if ((fp = fmemopen(buf, BSZ, "w+")) == NULL) 15 | err_sys("fmemopen failed"); 16 | printf("initial buffer contents: %s\n", buf); 17 | fprintf(fp, "hello, world"); 18 | printf("before flush: %s\n", buf); 19 | fflush(fp); 20 | printf("after fflush: %s\n", buf); 21 | printf("len of string in buf = %ld\n", (long)strlen(buf)); 22 | 23 | memset(buf, 'b', BSZ-2); 24 | buf[BSZ-2] = '\0'; 25 | buf[BSZ-1] = 'X'; 26 | fprintf(fp, "hello, world"); 27 | fseek(fp, 0, SEEK_SET); 28 | printf("after fseek: %s\n", buf); 29 | printf("len of string in buf = %ld\n", (long)strlen(buf)); 30 | 31 | memset(buf, 'c', BSZ-2); 32 | buf[BSZ-2] = '\0'; 33 | buf[BSZ-1] = 'X'; 34 | fprintf(fp, "hello, world"); 35 | fclose(fp); 36 | printf("after fclose: %s\n", buf); 37 | printf("len of string in buf = %ld\n", (long)strlen(buf)); 38 | 39 | return(0); 40 | } 41 | -------------------------------------------------------------------------------- /stdio/mkstemp.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void make_temp(char *template); 5 | 6 | int 7 | main() 8 | { 9 | char good_template[] = "/tmp/dirXXXXXX"; /* right way */ 10 | char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/ 11 | 12 | printf("trying to create first temp file...\n"); 13 | make_temp(good_template); 14 | printf("trying to create second temp file...\n"); 15 | make_temp(bad_template); 16 | exit(0); 17 | } 18 | 19 | void 20 | make_temp(char *template) 21 | { 22 | int fd; 23 | struct stat sbuf; 24 | 25 | if ((fd = mkstemp(template)) < 0) 26 | err_sys("can't create temp file"); 27 | printf("temp name = %s\n", template); 28 | close(fd); 29 | if (stat(template, &sbuf) < 0) { 30 | if (errno == ENOENT) 31 | printf("file doesn't exist\n"); 32 | else 33 | err_sys("stat failed"); 34 | } else { 35 | printf("file exists\n"); 36 | unlink(template); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /termios/getpass.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_PASS_LEN 8 /* max #chars for user to enter */ 6 | 7 | char * 8 | getpass(const char *prompt) 9 | { 10 | static char buf[MAX_PASS_LEN + 1]; /* null byte at end */ 11 | char *ptr; 12 | sigset_t sig, osig; 13 | struct termios ts, ots; 14 | FILE *fp; 15 | int c; 16 | 17 | if ((fp = fopen(ctermid(NULL), "r+")) == NULL) 18 | return(NULL); 19 | setbuf(fp, NULL); 20 | 21 | sigemptyset(&sig); 22 | sigaddset(&sig, SIGINT); /* block SIGINT */ 23 | sigaddset(&sig, SIGTSTP); /* block SIGTSTP */ 24 | sigprocmask(SIG_BLOCK, &sig, &osig); /* and save mask */ 25 | 26 | tcgetattr(fileno(fp), &ts); /* save tty state */ 27 | ots = ts; /* structure copy */ 28 | ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 29 | tcsetattr(fileno(fp), TCSAFLUSH, &ts); 30 | fputs(prompt, fp); 31 | 32 | ptr = buf; 33 | while ((c = getc(fp)) != EOF && c != '\n') 34 | if (ptr < &buf[MAX_PASS_LEN]) 35 | *ptr++ = c; 36 | *ptr = 0; /* null terminate */ 37 | putc('\n', fp); /* we echo a newline */ 38 | 39 | tcsetattr(fileno(fp), TCSAFLUSH, &ots); /* restore TTY state */ 40 | sigprocmask(SIG_SETMASK, &osig, NULL); /* restore mask */ 41 | fclose(fp); /* done with /dev/tty */ 42 | return(buf); 43 | } 44 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /termios/t_raw.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | 3 | static void 4 | sig_catch(int signo) 5 | { 6 | printf("signal caught\n"); 7 | tty_reset(STDIN_FILENO); 8 | exit(0); 9 | } 10 | 11 | int 12 | main(void) 13 | { 14 | int i; 15 | char c; 16 | 17 | if (signal(SIGINT, sig_catch) == SIG_ERR) /* catch signals */ 18 | err_sys("signal(SIGINT) error"); 19 | if (signal(SIGQUIT, sig_catch) == SIG_ERR) 20 | err_sys("signal(SIGQUIT) error"); 21 | if (signal(SIGTERM, sig_catch) == SIG_ERR) 22 | err_sys("signal(SIGTERM) error"); 23 | 24 | if (tty_raw(STDIN_FILENO) < 0) 25 | err_sys("tty_raw error"); 26 | printf("Enter raw mode characters, terminate with DELETE\n"); 27 | while ((i = read(STDIN_FILENO, &c, 1)) == 1) { 28 | if ((c &= 255) == 0177) /* 0177 = ASCII DELETE */ 29 | break; 30 | printf("%o\n", c); 31 | } 32 | if (tty_reset(STDIN_FILENO) < 0) 33 | err_sys("tty_reset error"); 34 | if (i <= 0) 35 | err_sys("read error"); 36 | if (tty_cbreak(STDIN_FILENO) < 0) 37 | err_sys("tty_cbreak error"); 38 | printf("\nEnter cbreak mode characters, terminate with SIGINT\n"); 39 | while ((i = read(STDIN_FILENO, &c, 1)) == 1) { 40 | c &= 255; 41 | printf("%o\n", c); 42 | } 43 | if (tty_reset(STDIN_FILENO) < 0) 44 | err_sys("tty_reset error"); 45 | if (i <= 0) 46 | err_sys("read error"); 47 | 48 | exit(0); 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /threadctl/getenv2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | extern char **environ; 7 | 8 | pthread_mutex_t env_mutex; 9 | 10 | static pthread_once_t init_done = PTHREAD_ONCE_INIT; 11 | 12 | static void 13 | thread_init(void) 14 | { 15 | pthread_mutexattr_t attr; 16 | 17 | pthread_mutexattr_init(&attr); 18 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 19 | pthread_mutex_init(&env_mutex, &attr); 20 | pthread_mutexattr_destroy(&attr); 21 | } 22 | 23 | int 24 | getenv_r(const char *name, char *buf, int buflen) 25 | { 26 | int i, len, olen; 27 | 28 | pthread_once(&init_done, thread_init); 29 | len = strlen(name); 30 | pthread_mutex_lock(&env_mutex); 31 | for (i = 0; environ[i] != NULL; i++) { 32 | if ((strncmp(name, environ[i], len) == 0) && 33 | (environ[i][len] == '=')) { 34 | olen = strlen(&environ[i][len+1]); 35 | if (olen >= buflen) { 36 | pthread_mutex_unlock(&env_mutex); 37 | return(ENOSPC); 38 | } 39 | strcpy(buf, &environ[i][len+1]); 40 | pthread_mutex_unlock(&env_mutex); 41 | return(0); 42 | } 43 | } 44 | pthread_mutex_unlock(&env_mutex); 45 | return(ENOENT); 46 | } 47 | -------------------------------------------------------------------------------- /threadctl/getenv3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAXSTRINGSZ 4096 7 | 8 | static pthread_key_t key; 9 | static pthread_once_t init_done = PTHREAD_ONCE_INIT; 10 | pthread_mutex_t env_mutex = PTHREAD_MUTEX_INITIALIZER; 11 | 12 | extern char **environ; 13 | 14 | static void 15 | thread_init(void) 16 | { 17 | pthread_key_create(&key, free); 18 | } 19 | 20 | char * 21 | getenv(const char *name) 22 | { 23 | int i, len; 24 | char *envbuf; 25 | 26 | pthread_once(&init_done, thread_init); 27 | pthread_mutex_lock(&env_mutex); 28 | envbuf = (char *)pthread_getspecific(key); 29 | if (envbuf == NULL) { 30 | envbuf = malloc(MAXSTRINGSZ); 31 | if (envbuf == NULL) { 32 | pthread_mutex_unlock(&env_mutex); 33 | return(NULL); 34 | } 35 | pthread_setspecific(key, envbuf); 36 | } 37 | len = strlen(name); 38 | for (i = 0; environ[i] != NULL; i++) { 39 | if ((strncmp(name, environ[i], len) == 0) && 40 | (environ[i][len] == '=')) { 41 | strncpy(envbuf, &environ[i][len+1], MAXSTRINGSZ-1); 42 | pthread_mutex_unlock(&env_mutex); 43 | return(envbuf); 44 | } 45 | } 46 | pthread_mutex_unlock(&env_mutex); 47 | return(NULL); 48 | } 49 | -------------------------------------------------------------------------------- /threadctl/suspend.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int quitflag; /* set nonzero by thread */ 5 | sigset_t mask; 6 | 7 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 8 | pthread_cond_t waitloc = PTHREAD_COND_INITIALIZER; 9 | 10 | void * 11 | thr_fn(void *arg) 12 | { 13 | int err, signo; 14 | 15 | for (;;) { 16 | err = sigwait(&mask, &signo); 17 | if (err != 0) 18 | err_exit(err, "sigwait failed"); 19 | switch (signo) { 20 | case SIGINT: 21 | printf("\ninterrupt\n"); 22 | break; 23 | 24 | case SIGQUIT: 25 | pthread_mutex_lock(&lock); 26 | quitflag = 1; 27 | pthread_mutex_unlock(&lock); 28 | pthread_cond_signal(&waitloc); 29 | return(0); 30 | 31 | default: 32 | printf("unexpected signal %d\n", signo); 33 | exit(1); 34 | } 35 | } 36 | } 37 | 38 | int 39 | main(void) 40 | { 41 | int err; 42 | sigset_t oldmask; 43 | pthread_t tid; 44 | 45 | sigemptyset(&mask); 46 | sigaddset(&mask, SIGINT); 47 | sigaddset(&mask, SIGQUIT); 48 | if ((err = pthread_sigmask(SIG_BLOCK, &mask, &oldmask)) != 0) 49 | err_exit(err, "SIG_BLOCK error"); 50 | 51 | err = pthread_create(&tid, NULL, thr_fn, 0); 52 | if (err != 0) 53 | err_exit(err, "can't create thread"); 54 | 55 | pthread_mutex_lock(&lock); 56 | while (quitflag == 0) 57 | pthread_cond_wait(&waitloc, &lock); 58 | pthread_mutex_unlock(&lock); 59 | 60 | /* SIGQUIT has been caught and is now blocked; do whatever */ 61 | quitflag = 0; 62 | 63 | /* reset signal mask which unblocks SIGQUIT */ 64 | if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 65 | err_sys("SIG_SETMASK error"); 66 | exit(0); 67 | } 68 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /threads/badexit2.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | struct foo { 5 | int a, b, c, d; 6 | }; 7 | 8 | void 9 | printfoo(const char *s, const struct foo *fp) 10 | { 11 | printf("%s", s); 12 | printf(" structure at 0x%lx\n", (unsigned long)fp); 13 | printf(" foo.a = %d\n", fp->a); 14 | printf(" foo.b = %d\n", fp->b); 15 | printf(" foo.c = %d\n", fp->c); 16 | printf(" foo.d = %d\n", fp->d); 17 | } 18 | 19 | void * 20 | thr_fn1(void *arg) 21 | { 22 | struct foo foo = {1, 2, 3, 4}; 23 | 24 | printfoo("thread 1:\n", &foo); 25 | pthread_exit((void *)&foo); 26 | } 27 | 28 | void * 29 | thr_fn2(void *arg) 30 | { 31 | printf("thread 2: ID is %lu\n", (unsigned long)pthread_self()); 32 | pthread_exit((void *)0); 33 | } 34 | 35 | int 36 | main(void) 37 | { 38 | int err; 39 | pthread_t tid1, tid2; 40 | struct foo *fp; 41 | 42 | err = pthread_create(&tid1, NULL, thr_fn1, NULL); 43 | if (err != 0) 44 | err_exit(err, "can't create thread 1"); 45 | err = pthread_join(tid1, (void *)&fp); 46 | if (err != 0) 47 | err_exit(err, "can't join with thread 1"); 48 | sleep(1); 49 | printf("parent starting second thread\n"); 50 | err = pthread_create(&tid2, NULL, thr_fn2, NULL); 51 | if (err != 0) 52 | err_exit(err, "can't create thread 2"); 53 | sleep(1); 54 | printfoo("parent:\n", fp); 55 | exit(0); 56 | } 57 | -------------------------------------------------------------------------------- /threads/cleanup.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void 5 | cleanup(void *arg) 6 | { 7 | printf("cleanup: %s\n", (char *)arg); 8 | } 9 | 10 | void * 11 | thr_fn1(void *arg) 12 | { 13 | printf("thread 1 start\n"); 14 | pthread_cleanup_push(cleanup, "thread 1 first handler"); 15 | pthread_cleanup_push(cleanup, "thread 1 second handler"); 16 | printf("thread 1 push complete\n"); 17 | if (arg) 18 | return((void *)1); 19 | pthread_cleanup_pop(0); 20 | pthread_cleanup_pop(0); 21 | return((void *)1); 22 | } 23 | 24 | void * 25 | thr_fn2(void *arg) 26 | { 27 | printf("thread 2 start\n"); 28 | pthread_cleanup_push(cleanup, "thread 2 first handler"); 29 | pthread_cleanup_push(cleanup, "thread 2 second handler"); 30 | printf("thread 2 push complete\n"); 31 | if (arg) 32 | pthread_exit((void *)2); 33 | pthread_cleanup_pop(0); 34 | pthread_cleanup_pop(0); 35 | pthread_exit((void *)2); 36 | } 37 | 38 | int 39 | main(void) 40 | { 41 | int err; 42 | pthread_t tid1, tid2; 43 | void *tret; 44 | 45 | err = pthread_create(&tid1, NULL, thr_fn1, (void *)1); 46 | if (err != 0) 47 | err_exit(err, "can't create thread 1"); 48 | err = pthread_create(&tid2, NULL, thr_fn2, (void *)1); 49 | if (err != 0) 50 | err_exit(err, "can't create thread 2"); 51 | err = pthread_join(tid1, &tret); 52 | if (err != 0) 53 | err_exit(err, "can't join with thread 1"); 54 | printf("thread 1 exit code %ld\n", (long)tret); 55 | err = pthread_join(tid2, &tret); 56 | if (err != 0) 57 | err_exit(err, "can't join with thread 2"); 58 | printf("thread 2 exit code %ld\n", (long)tret); 59 | exit(0); 60 | } 61 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /threads/exitstatus.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | void * 5 | thr_fn1(void *arg) 6 | { 7 | printf("thread 1 returning\n"); 8 | return((void *)1); 9 | } 10 | 11 | void * 12 | thr_fn2(void *arg) 13 | { 14 | printf("thread 2 exiting\n"); 15 | pthread_exit((void *)2); 16 | } 17 | 18 | int 19 | main(void) 20 | { 21 | int err; 22 | pthread_t tid1, tid2; 23 | void *tret; 24 | 25 | err = pthread_create(&tid1, NULL, thr_fn1, NULL); 26 | if (err != 0) 27 | err_exit(err, "can't create thread 1"); 28 | err = pthread_create(&tid2, NULL, thr_fn2, NULL); 29 | if (err != 0) 30 | err_exit(err, "can't create thread 2"); 31 | err = pthread_join(tid1, &tret); 32 | if (err != 0) 33 | err_exit(err, "can't join with thread 1"); 34 | printf("thread 1 exit code %ld\n", (long)tret); 35 | err = pthread_join(tid2, &tret); 36 | if (err != 0) 37 | err_exit(err, "can't join with thread 2"); 38 | printf("thread 2 exit code %ld\n", (long)tret); 39 | exit(0); 40 | } 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /threads/mutex1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int f_count; 6 | pthread_mutex_t f_lock; 7 | int f_id; 8 | /* ... more stuff here ... */ 9 | }; 10 | 11 | struct foo * 12 | foo_alloc(int id) /* allocate the object */ 13 | { 14 | struct foo *fp; 15 | 16 | if ((fp = malloc(sizeof(struct foo))) != NULL) { 17 | fp->f_count = 1; 18 | fp->f_id = id; 19 | if (pthread_mutex_init(&fp->f_lock, NULL) != 0) { 20 | free(fp); 21 | return(NULL); 22 | } 23 | /* ... continue initialization ... */ 24 | } 25 | return(fp); 26 | } 27 | 28 | void 29 | foo_hold(struct foo *fp) /* add a reference to the object */ 30 | { 31 | pthread_mutex_lock(&fp->f_lock); 32 | fp->f_count++; 33 | pthread_mutex_unlock(&fp->f_lock); 34 | } 35 | 36 | void 37 | foo_rele(struct foo *fp) /* release a reference to the object */ 38 | { 39 | pthread_mutex_lock(&fp->f_lock); 40 | if (--fp->f_count == 0) { /* last reference */ 41 | pthread_mutex_unlock(&fp->f_lock); 42 | pthread_mutex_destroy(&fp->f_lock); 43 | free(fp); 44 | } else { 45 | pthread_mutex_unlock(&fp->f_lock); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /threads/timedlock.c: -------------------------------------------------------------------------------- 1 | #include "apue.h" 2 | #include 3 | 4 | int 5 | main(void) 6 | { 7 | int err; 8 | struct timespec tout; 9 | struct tm *tmp; 10 | char buf[64]; 11 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 12 | 13 | pthread_mutex_lock(&lock); 14 | printf("mutex is locked\n"); 15 | clock_gettime(CLOCK_REALTIME, &tout); 16 | tmp = localtime(&tout.tv_sec); 17 | strftime(buf, sizeof(buf), "%r", tmp); 18 | printf("current time is %s\n", buf); 19 | tout.tv_sec += 10; /* 10 seconds from now */ 20 | /* caution: this could lead to deadlock */ 21 | err = pthread_mutex_timedlock(&lock, &tout); 22 | clock_gettime(CLOCK_REALTIME, &tout); 23 | tmp = localtime(&tout.tv_sec); 24 | strftime(buf, sizeof(buf), "%r", tmp); 25 | printf("the time is now %s\n", buf); 26 | if (err == 0) 27 | printf("mutex locked again!\n"); 28 | else 29 | printf("can't lock mutex again: %s\n", strerror(err)); 30 | exit(0); 31 | } 32 | --------------------------------------------------------------------------------