Using C on the UNIX System
Using C on the UNIX System
by David A. Curry
ISBN 10: 0-937175-23-4 / ISBN 13: 9780937175231
O'Reilly & Associates
January 1989
The following are made available for your personal, non-commercial use only. You may cite this document as a bibliographic reference in any works that you are writing. Any commercial use of this document, including printing and distribution to groups of people (such as a classroom) is prohibited without my prior written permission.
Complete text of the book:
Example source code:
From the back cover...
There are C programmers and there are UNIX programmers, and the difference between them is the knowledge of the system calls and special library routines available on the UNIX system.
If you are an intermediate to experienced C programmer, and you'd like to take the next step to become a UNIX system programmer, this book is for you. Students who wish to work for a university computer center, researchers wishing to write their own tools, systems programmers unfamiliar with UNIX who find they must write programs for their UNIX-based PC or workstation, bulletin board operators using a UNIX system to support their operation—all would be candidates for this book.
Topics covered include:
- I/O without using stdio
- Manipulating files and directories
- Device I/O control
- Getting information about users
- Telling time and timing things
- Processing signals
- Creating processes and executing programs
- Job control
- Interprocess communication
- Networking (Internet clients and servers)
- Pseudo terminals
- Reading kernel data structures
Table of Contents
Preface
Documentation Conventions
Acknowledgements
Chapter 1 Introduction
System Calls vs. Library Routines
Versions of UNIX
Error Handling
Chapter 2 The Standard I/O Library
File Pointers
Opening and Creating Files
Closing Files
Reading and Writing Files
The getc and putc Routines
The fgets and fputs Routines
The fread and fwrite Routines
The fscanf and fprintf Routines
The sscanf and sprintf Routines
Moving Around in Files
Chapter 3 Low-Level I/O
File Descriptors
Opening and Creating Files
Opening and Creating Files on Older UNIX Systems
Closing Files
Reading and Writing Files
Moving Around in Files
Duplicating File Descriptors
Converting File Descriptors to File Pointers
Chapter 4 Files and Directories
File System Concepts
Ordinary Files
Directories
Special Files
Removable File Systems
Device Numbers
I-Numbers, the I-List, and I-Nodes
Hard Links
Symbolic Links
Determining the Accessibility of a File
Getting Information From an I-Node
Reading Directories
Modifying File Attributes
Miscellaneous File System Routines
Changing Directories
Deleting and Truncating Files
Making Directories
Linking and Renaming Files
Symbolic Links
The umask Value
Chapter 5 Device I/O Control
The Version 7 and Berkeley UNIX ioctl
Line Disciplines
The sgttyb Structure
The tchars Structure
The ltchars Structure
The Local Mode Word
The winsize Structure
Miscellaneous Operations
The System V ioctl
c_iflag
c_oflag
c_cflag
c_lflag
c_cc
The fcntl System Call
Non-Blocking I/O
The select System Call
Chapter 6 Information About Users
The Login Name
The User Id
The Group Id
The Berkeley UNIX Group Mechanism
Reading the Password File
Reading the Group File
Reading the utmp File
Chapter 7 Telling Time and Timing Things
Telling Time
Obtaining the Time
Timezones
Converting the Time to ASCII
Time Differences
Sleeping and Alarm Clocks
Sleeping
The Alarm Clock
Interval Timers
Process Timing
Changing File Times
Chapter 8 Processing Signals
Overview of Signal Handling
Resetting Signals
Restarting System Calls
The Signals
Sending Signals
Catching and Ignoring Signals
Ignoring Signals
Catching Signals
Using Signals for Timeouts
The setjmp and longjmp Routines
The New Berkeley UNIX Signal Mechanism
Handler Calling Conventions
The Signal Mask
The Signal Stack
Chapter 9 Executing Programs
The system Library Routine
Executing Programs Directly
Creating Processes
Executing Programs
Waiting for Processes to Terminate
Redirecting Input and Output
Setting Up Pipelines
The popen Library Routine
Creating Pipes Directly
Chapter 10 Job Control
Preliminary Concepts
The Controlling Terminal
Process Groups
System Calls
ioctl
setpgrp
killpg
wait3
The JOB and PROC Data Types
Job Control in the Shell
Setting Up for Job Control
Executing a Program
Stopping a Job
Backgrounding a Job
Foregrounding a Job
The jobs Command
Waiting for Jobs
Asynchronous Process Notification
Job Control Outside the Shell
Important Points
Chapter 11 Interprocess Communication
Berkeley UNIX IPC
The socket System Call
The bind System Call
The send and recv System Calls
The listen System Call
The shutdown System Call
Connection-Based Sockets
The accept System Call
The connect System Call
Connectionless Sockets
The sendto System Call
The recvfrom System Call
Connecting Datagram Sockets
A Small Client Program
A Small Server Program
System V IPC
Message Queues
The msgget System Call
The msgctl System Call
The msgsnd and msgrcv System Calls
Semaphores
The semget System Call
The semctl System Call
The semop System Call
Shared Memory
The shmget System Call
The shmctl System Call
The shmat System Call
The shmdt System Call
Chapter 12 Networking
Addresses
Translating Hostnames Into Network Numbers
Obtaining Port Numbers
Network Byte Order
Networking System Calls
Chapter 13 The File System
Disk Terminology
The "Standard" UNIX File System
The Berkeley Fast File System
Reading Data Blocks From the File System
Chapter 14 Miscellaneous Routines
Resource Limits
The getrlimit System Call
The setrlimit System Call
Obtaining Resource Usage Information
Manipulating Byte Strings
The bcmp and memcmp Library Routines
The bcopy and memcpy Library Routines
The bzero and memset Library Routines
Environment Variables
The Current Working Directory
Searching for Characters in Strings
Determining Whether a File is a Terminal
The isatty Library Routine
The ttyname Library Routine
The /dev/tty Device
Printing Error Messages
The perror Library Routine
The psignal Library Routine
Sorting Arrays in Memory
Appendix A Calling FORTRAN From C
Data Representation
Procedure Naming
Naming C Routines to be Called From FORTRAN
Naming FORTRAN Routines to be Called From C
Returning Values from Functions
Integer, Logical, Real, Double Precision
Complex and Double Complex
Character Strings
Passing Arguments
Integers, Floats (Reals), and Doubles
Characters and Logicals
Character Strings
Functions
Overall Argument Sequence
Input and Output
From C Programs
From FORTRAN Programs
Libraries
Further Information
Appendix B Using Berkeley UNIX Pseudo Terminals
Appendix C Reading Kernel Data Structures
Appendix D Berkeley UNIX Directory Compatibility Routines
Appendix E Interval Timer Version of nap()
Bibliography
Index
Colophon