Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

186 System Data Files and Information Chapter 6


The general principle is that every data file has at least three functions:


  1. Agetfunction that reads the next record, opening the file if necessary.These
    functions normally return a pointer to a structure. A null pointer is returned
    when the end of file is reached. Most of thegetfunctions return a pointer to a
    staticstructure, so we always have to copy the structureif we want to save it.

  2. Asetfunction that opens the file, if not already open, and rewinds the file. We
    use this function when we know we want to start again at the beginning of the
    file.

  3. Anendentry that closes the data file. As we mentioned earlier, we always have
    to call this function when we’redone, to close all the files.


Additionally, if the data file supports some form of keyed lookup, routines areprovided
to search for a recordwith a specific key.For example, two keyed lookup routines are
provided for the passwordfile:getpwnamlooks for a recordwith a specific user name,
andgetpwuidlooks for a recordwith a specific user ID.
Figure6.6 shows some of these routines, which arecommon to UNIX systems. In
this figure, we show the functions for the passwordfiles and group file, which we
discussed earlier in this chapter,and some of the networking functions. Thereareget,
set,andendfunctions for all the data files in this figure.

Description Data file Header StructureAdditional keyed lookup functions
passwords /etc/passwd <pwd.h> passwd getpwnam,getpwuid
groups /etc/group <grp.h> group getgrnam,getgrgid
shadow /etc/shadow <shadow.h> spwd getspnam
hosts /etc/hosts <netdb.h> hostent getnameinfo,getaddrinfo
networks /etc/networks <netdb.h> netent getnetbyname,getnetbyaddr
protocols /etc/protocols <netdb.h> protoent getprotobyname,getprotobynumber
services /etc/services <netdb.h> servent getservbyname,getservbyport

Figure 6.6Similar routines for accessing system data files

Under Solaris, the last four data files in Figure6.6 aresymbolic links to files of the same name
in the directory/etc/inet.Most UNIX System implementations have additional functions
that arelike these, but the additional functions tend to deal with system administration files
and arespecific to each implementation.

6.8 Login Accounting


Twodata files provided with most UNIX systems aretheutmpfile, which keeps track of
all the users currently logged in, and thewtmpfile, which keeps track of all logins and
logouts. With Version 7, one type of recordwas written to both files, a binary record
consisting of the following structure:
Free download pdf