Path: utzoo!attcan!utgpu!watmath!uunet!ethos!att!icus!lenny From: lenny@icus.islp.ny.us (Lenny Tropiano) Newsgroups: unix-pc.sources Subject: POSIX/SVID/X3J11 standard routines (libposix.a) (Part 2 of 5) Keywords: POSIX, SVID, X3J11, dirent, News 3.0, good stuff! Message-ID: <937@icus.islp.ny.us> Date: 3 Aug 89 01:51:25 GMT Distribution: unix-pc Organization: ICUS Software Systems, Islip, New York Lines: 1410 #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh dirent.4 <<'END_OF_dirent.4' X.TH DIRENT 4 "Standard Extension" X.SH NAME Xdirent \- file system independent directory entry X.SH SYNOPSIS X.B "#include " X.br X.B "#include " X.SH DESCRIPTION XDifferent file system types Xmay have different directory entries. XThe X.I dirent Xstructure defines a Xfile system independent directory entry, Xwhich contains information common to Xdirectory entries in different file system types. XA set of these structures is returned by the X.IR getdents (2) Xsystem call. X.P XThe X.I dirent Xstructure is defined below. X.br Xstruct dirent { X.br X long d_ino; X.br X off_t d_off; X.br X unsigned short d_reclen; X.br X char d_name[1]; X.br X }; X.P XThe field X.I d_ino Xis a number which is unique Xfor each file in the file system. XThe field X.I d_off\^ Xrepresents an offset of that directory entry Xin the actual file system directory. XThe field X.I d_name Xis the beginning of the character array Xgiving the name of the directory entry. XThis name is null terminated Xand may have at most X.SM NAME_MAX Xcharacters in addition to the null terminator. XThis results in file system independent directory entries Xbeing variable-length entities. XThe value of X.I d_reclen Xis the record length of this entry. XThis length is defined to be the number of bytes Xbetween the beginning of the current entry and the next one, Xadjusted so that the next entry Xwill start on a long boundary. X.SH FILES X/usr/include/sys/dirent.h X.SH "SEE ALSO" Xgetdents(2). X.SH WARNING XThe field X.I d_off\^ Xdoes not have a simple interpretation Xfor some file system types Xand should not be used directly by applications. END_OF_dirent.4 if test 1593 -ne `wc -c getdents.2 <<'END_OF_getdents.2' X.TH GETDENTS 2 "Standard Extension" X.SH NAME Xgetdents \- get directory entries in a file system independent format X.SH SYNOPSIS X.B "#include " X.br X.B "#include " X.P X.B "int getdents (fildes, buf, nbyte)" X.br X.B "int fildes;" X.br X.B "char \(**buf;" X.br X.B "unsigned nbyte;" X.SH DESCRIPTION X.I Fildes Xis a file descriptor obtained from an X.IR open (2) Xor X.IR dup (2) Xsystem call. X.P X.I Getdents Xattempts to read X.I nbyte Xbytes from the directory associated with X.I fildes Xand to format them as Xfile system independent entries Xin the buffer pointed to by X.IR buf . XSince the file system independent directory entries Xare of variable length, Xin most cases the actual number of bytes returned Xwill be less than X.IR nbyte . X.P XThe file system independent directory entry is specified by the X.I dirent Xstructure. XFor a description of this see X.IR dirent (4). X.P XOn devices capable of seeking, X.I getdents Xstarts at a position in the file given by Xthe file pointer associated with X.IR fildes . XUpon return from X.IR getdents , Xthe file pointer has been incremented Xto point to the next directory entry. X.P XThis system call was developed in order to implement the X.I readdir Xroutine X[for a description see X.IR directory (3C)] Xand should not be used for other purposes. X.SH "SEE ALSO" Xdirectory(3C), dirent(4). X.SH DIAGNOSTICS XUpon successful completion Xa non-negative integer is returned Xindicating the number of bytes of X.I buf\^ Xactually filled. X(This need not be the number actually used Xin the actual directory file.)\|\| XA value of zero Xindicates the end of the directory has been reached. XIf X.I getdents Xfails for any other reason, Xa value of \-1 is returned and Xthe external integer variable X.I errno Xis set to indicate the error. X.SH WARNINGS XEntries for "." and ".." Xmay not be reported for some file system types. X.P XThe exact set of X.I errno Xvalues and meanings may vary among implementations. END_OF_getdents.2 if test 1922 -ne `wc -c lsearch.3c <<'END_OF_lsearch.3c' X.TH LSEARCH 3C "Standard extension" X.SH NAME Xlsearch, lfind \- linear search and update X.SH SYNOPSIS X.B #include X.br X.B #include X.PP X.B "char \(**lsearch ((char \(**)key, (char \(**)base, nelp, sizeof(\(**key), compar)" X.br X.B unsigned \(**nelp; X.br X.B int (\(**compar)( ); X.PP X.B "char \(**lfind ((char \(**)key, (char \(**)base, nelp, sizeof(\(**key), compar)" X.br X.B unsigned \(**nelp; X.br X.B int (\(**compar)( ); X.SH DESCRIPTION X.I Lsearch Xis a linear search routine generalized from Knuth (6.1) Algorithm S. XIt returns a pointer into a table indicating where Xa datum may be found. XIf the datum does not occur, it is added Xat the end of the table. X.B Key Xpoints to the datum to be sought in the table. X.B Base Xpoints to the first element in the table. X.B Nelp Xpoints to an integer containing the current number of Xelements in the table. XThe integer is incremented if the datum is added to the table. X.B Compar Xis the name of the comparison function which the user must supply X.RI ( strcmp , Xfor example). XIt is called with two arguments that point Xto the elements being compared. XThe function must return zero if the elements Xare equal and non-zero otherwise. X.PP X.I Lfind Xis the same as X.I lsearch Xexcept that if the datum is not found, it is not added Xto the table. XInstead, a X.SM NULL Xpointer is returned. X.SH NOTES XThe pointers to the key and the element Xat the base of the table should be Xof type pointer-to-element, Xand cast to type pointer-to-character. X.br XThe comparison function need not compare every byte, Xso arbitrary data may be contained in the elements Xin addition to the values being compared. X.br XAlthough declared as type pointer-to-character, Xthe value returned should be cast into type pointer-to-element. X.SH EXAMPLE XThis fragment will read in \(<= \s-1TABSIZE\s+1 Xstrings of length \(<= \s-1ELSIZE\s+1 Xand store them in a table, eliminating duplicates. X.PP X.RS X.nf X.ss 18 X#include X#include X X#define \s-1TABSIZE\s+1 50 X#define \s-1ELSIZE\s+1 120 X X char line[\s-1ELSIZE\s+1], tab[\s-1TABSIZE\s+1][\s-1ELSIZE\s+1], \(**lsearch( ); X unsigned nel = 0; X int strcmp( ); X . . . X while (fgets(line, \s-1ELSIZE\s+1, stdin) != \s-1NULL\s+1 && X nel < \s-1TABSIZE\s+1) X (void) lsearch(line, (char \(**)tab, &nel, X \s-1ELSIZE\s+1, strcmp); X . . . X.fi X.SH SEE ALSO Xbsearch(3C), hsearch(3C), tsearch(3C). X.SH DIAGNOSTICS XIf the searched for datum is found, both X.I lsearch Xand X.I lfind Xreturn a pointer Xto it. XOtherwise, X.I lfind Xreturns NULL and X.I lsearch Xreturns a pointer to the newly Xadded element. X.SH BUGS XUndefined results can occur if there is not enough room in the table to Xadd a new item. END_OF_lsearch.3c if test 2671 -ne `wc -c memcpy.c <<'END_OF_memcpy.c' X/* X * Copy first n bytes of s to t, return t X * X * Written by reading the System V Interface Definition, not the code. X * X * Totally public domain. X * X */ X/*LINTLIBRARY*/ Xchar *memcpy(t, s, n) Xregister char *t, *s; Xregister int n; X{ X register char *os = t; X X while (--n >= 0) X *t++ = *s++; X return(os); X} X X/************************************************************************** X XIf you happen to be using a PDP-11, you can use this assembler version... X X/ MEMCPY(TO, FROM, N) CHAR *TO, *FROM; X/ X/ Copy "n" bytes from "from" to "to". X/ X.globl _memcpy X.globl csav, cret X X.text X_memcpy: jsr r0, csav / save registers X mov r5, r4 / get arguments... X cmp (r4)+, (r4)+ / X mov (r4)+, r1 / to X mov (r4)+, r0 / from X mov (r4), r2 / byte count X beq ret / if zero then return X bit $1, r1 / is "to" odd X beq 1f / yes... X movb (r0)+, (r1)+ / copy one byte to make it even X dec r2 / and adjust byte count X1: mov r2, r3 / save (low bit of) count X asr r2 / convert from bytes to words X beq 3f / if any words to copy... X bit $1, r0 / is "from" even now? X beq 2f / if not ... X1: movb (r0)+, (r1)+ / copy bytes... X movb (r0)+, (r1)+ / ... X sob r2, 1b / ... X br 3f / else ... X2: mov (r0)+, (r1)+ / copy words... X sob r2, 2b / ... X3: ror r3 / is byte count odd? X bcc ret / if so... X movb (r0)+, (r1)+ / copy odd byte. Xret: jmp cret / return X XIf you happen to be a VAX, you can use this: X X# memcpy(to, from, count) char *to, *from; int count; X# X# Copy "count" bytes from "from" to "to"; not guaranteed to X# work if "from" and "to" overlap. X X .align 2 X .globl _memcpy X_memcpy: X .word 0 X movl 4(ap), r3 # r3 = to X movl 8(ap), r1 # r1 = from XL1: X movzwl $65535, r0 # while more than 65535 bytes to move X cmpl 12(ap), r0 X jleq L2 # if <= 65535, break X subl2 r0, 12(ap) # count-=65535 (bytes moved this time) X movc3 r0, (r1), (r3) # r1, r3 magically point to next 65K X brb L1 XL2: X movc3 12(ap), (r1), (r3) # move up to 65535 bytes X ret X XIf you happen to be a 3b2, 3b5, 3b10, or 3b15 (but not a 3b1!) you can use this X X .file "memcpy.s" # silly assembler wants this line X .globl memcpy X .align 4 Xmemcpy: save &0 # set up stack frame X movw 0(%ap), %r1 # get source X movw 4(%ap), %r0 # and destination X movw 8(%ap), %r2 # get count X movblb # this instruction does it all X ret &0 # return X X**************************************************************************/ END_OF_memcpy.c if test 2473 -ne `wc -c memory.3c <<'END_OF_memory.3c' X.TH MEMORY 3C "Standard Extension" X.SH NAME Xmemccpy, memchr, memcmp, memcpy, memset \- memory operations X.SH SYNOPSIS X.nf X.B #include X.PP X.B char \(**memccpy (s1, s2, c, n) X.B char \(**s1, \(**s2; X.B int c, n; X.PP X.B char \(**memchr (s, c, n) X.B char \(**s; X.B int c, n; X.PP X.B int memcmp (s1, s2, n) X.B char \(**s1, \(**s2; X.B int n; X.PP X.B char \(**memcpy (s1, s2, n) X.B char \(**s1, \(**s2; X.B int n; X.PP X.B char \(**memset (s, c, n) X.B char \(**s; X.B int c, n; X.fi X.SH DESCRIPTION XThese functions operate as efficiently as possible on memory areas (arrays of Xcharacters bounded by a count, not necessarily terminated by a null character). XThey do not check for overflow of any receiving memory area. X.PP X.I Memccpy Xcopies characters from memory area X.B s2 Xinto X.BR s1 , Xstopping after the first occurrence of character X.B c Xhas been copied, or after X.B n Xcharacters have been copied, whichever comes first. XIt returns a pointer to the character after Xthe copy of X.B c Xin X.BR s1 , Xor a X.SM NULL Xpointer if X.B c Xwas not found in the first X.B n Xcharacters of X.BR s2 . X.PP X.PP X.I Memchr Xreturns a pointer to the first Xoccurrence of character X.B c Xin the first X.B n Xcharacters of memory area X.BR s, Xor a X.SM NULL Xpointer if X.B c Xdoes not occur. X.PP X.I Memcmp Xcompares its arguments, looking at the first X.B n Xcharacters only, and returns an integer Xless than, equal to, or greater than 0, Xaccording as X.B s1 Xis lexicographically less than, equal to, or Xgreater than X.BR s2 . X.PP X.I Memcpy Xcopies X.B n Xcharacters from memory area X.B s2 Xto X.BR s1 . XIt returns X.BR s1 . X.PP X.I Memset Xsets the first X.B n Xcharacters in memory area X.B s Xto the value of character X.BR c . XIt returns X.BR s . X.SH NOTE XAll these functions are declared in the optional X.I Xheader file. X.SH BUGS X.I Memcmp Xuses native character comparison, which may be signed or unsigned. XThus the sign of the value returned when one of the characters has its Xhigh-order bit set is implementation-dependent. X.PP XOverlapping moves may scramble the data in both source and target areas. END_OF_memory.3c if test 2063 -ne `wc -c mkdir.3 <<'END_OF_mkdir.3' X.TH MKDIR 3 "Standard Extension" X.SH NAME Xmkdir \- make a directory file X.SH SYNOPSIS X.nf X.ft B Xmkdir(path, mode) Xchar *path; Xint mode; X.fi X.ft R X.SH DESCRIPTION X.I Mkdir Xcreates a new directory file with name X.IR path . XThe mode of the new file Xis initialized from X.IR mode . X(The protection part of the mode Xis modified by the process's mode mask; see X.IR umask (2)). X.PP XThe directory's owner ID is set to the process's effective user ID. XThe directory's group ID is set to the process's effective group ID. X.PP XThe low-order 9 bits of mode are modified by the process's Xfile mode creation mask: all bits set in the process's file mode Xcreation mask are cleared. See X.IR umask (2). X.SH NOTES XThis function emulates the SVr3 behavior (group ID of the directory Xis the effective group ID of the calling process) rather than the BSD Xbehavior (group ID of the directory is the group ID of the parent directory). X.PP XOn USG it will succeed if the real or apparent uid of the calling process Xhas write privileges in the current directory (the latter case is implemented Xby a kluge that tries to set the parent directory's permissions to 0777, 'ware Xsecurity holes!). The new directory will be owned by the effective uid. X.PP XOn V7 (because it restricts the chown(2) call and mkdir(1) makes Xdirectories owned by the real ID of its caller) the call will only succeed Xif the real ID matches, and the new directory will be owned by the real Xuid. X.PP XThe overhead of this emulation is fairly high, requiring a fork/exec (on AT&T Xversions, two fork-execs). X.SH "RETURN VALUE XA 0 return value indicates success. A \-1 return value Xindicates an error, and an error code is stored in X.I errno. X.SH "ERRORS X.I Mkdir Xwill fail and no directory will be created if: X[ENOTDIR] XA component of the path prefix is not a directory. X.TP 15 X[ENOENT] XA component of the path prefix does not exist. X.TP 15 X[EEXIST] XThe named file exists. X.TP 15 X[EACCES] XA component of the path prefix denies search permission, or write permission Xis denied on the parent of the directory to be created. X.SH "SEE ALSO" Xchmod(2), stat(2), umask(2) END_OF_mkdir.3 if test 2107 -ne `wc -c opendir.c <<'END_OF_opendir.c' X/* X opendir -- open a directory stream X X last edit: 16-Jun-1987 D A Gwyn X*/ X X#include X#include X#include X#include X X#ifdef BSD_SYSV X#define open _open /* avoid emulation overhead */ X#endif X Xtypedef char *pointer; /* (void *) if you have it */ X Xextern void free(); Xextern pointer malloc(); Xextern int open(), close(), fstat(); X Xextern int errno; X X#ifndef NULL X#define NULL 0 X#endif X X#ifndef O_RDONLY X#define O_RDONLY 0 X#endif X X#ifndef S_ISDIR /* macro to test for directory file */ X#define S_ISDIR( mode ) (((mode) & S_IFMT) == S_IFDIR) X#endif X XDIR * Xopendir( dirname ) X char *dirname; /* name of directory */ X { X register DIR *dirp; /* -> malloc'ed storage */ X register int fd; /* file descriptor for read */ X struct stat sbuf; /* result of fstat() */ X X if ( (fd = open( dirname, O_RDONLY )) < 0 ) X return NULL; /* errno set by open() */ X X if ( fstat( fd, &sbuf ) != 0 || !S_ISDIR( sbuf.st_mode ) ) X { X (void)close( fd ); X errno = ENOTDIR; X return NULL; /* not a directory */ X } X X if ( (dirp = (DIR *)malloc( sizeof(DIR) )) == NULL X || (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL X ) { X register int serrno = errno; X /* errno set to ENOMEM by sbrk() */ X X if ( dirp != NULL ) X free( (pointer)dirp ); X X (void)close( fd ); X errno = serrno; X return NULL; /* not enough memory */ X } X X dirp->dd_fd = fd; X dirp->dd_loc = dirp->dd_size = 0; /* refill needed */ X X return dirp; X } END_OF_opendir.c if test 1473 -ne `wc -c rename.3 <<'END_OF_rename.3' X.TH RENAME 3 "Standard Extension" X.SH NAME Xrename \- change the name of a file X.SH SYNOPSIS X.ft B X.nf Xrename(from, to) Xchar *from, *to; X.fi X.ft R X.SH DESCRIPTION X.I Rename Xcauses the link named X.I from Xto be renamed as X.IR to . XIf X.I to Xexists, then it is first removed. XBoth X.I from Xand X.I to Xmust be of the same type (i.e., both directories or both Xnon-directories), and must reside on the same file system. X.PP X.I Rename Xguarantees that an instance of X.I to Xwill always exist, even if the system should crash in Xthe middle of the operation. X.SH CAVEAT XThe system can deadlock if a loop in the file system graph is present. XThis loop takes the form of an entry in directory \*(lqa\*(rq, Xsay \*(lqa/foo\*(rq, Xbeing a hard link to directory \*(lqb\*(rq, and an entry in Xdirectory \*(lqb\*(rq, say \*(lqb/bar\*(rq, being a hard link Xto directory \*(lqa\*(rq. XWhen such a loop exists and two separate processes attempt to Xperform \*(lqrename a/foo b/bar\*(rq and \*(lqrename b/bar a/foo\*(rq, Xrespectively, Xthe system may deadlock attempting to lock Xboth directories for modification. XOn systems with a symbolic link capability, hard links to directories should be Xreplaced by symbolic links by the system administrator. X.SH "RETURN VALUE" XA 0 value is returned if the operation succeeds, otherwise X.I rename Xreturns \-1 and the global variable X.I errno Xindicates the reason for the failure. X.SH "ERRORS X.I Rename Xwill fail and nothing will change if any of the following are true: X.TP 15 X[ENOTDIR] XA component of either path prefix is not a directory. X.TP 15 X[ENOENT] XA component of either path prefix does not exist. X.TP 15 X[EACCES] XA component of either path prefix denies search permission. X.TP 15 X[ENOENT] XThe file named by \fIfrom\fP does not exist. X.TP 15 X[EXDEV] XThe link named by \fIto\fP and the file named by \fIfrom\fP Xare on different logical devices (file systems). Note that this error Xcode will not be returned if the implementation permits cross-device Xlinks. X.TP 15 X[EACCES] XThe requested link requires writing in a directory with a mode Xthat denies write permission. X.TP 15 X[EROFS] XThe requested link requires writing in a directory on a read-only file Xsystem. X.TP 15 X[EFAULT] X.I Path Xpoints outside the process's allocated address space. X.SH "SEE ALSO" Xopen(2) END_OF_rename.3 if test 2283 -ne `wc -c rmdir.3 <<'END_OF_rmdir.3' X.TH RMDIR 3 "Standard Extension" X.SH NAME Xrmdir \- remove a directory file X.SH SYNOPSIS X.nf X.ft B Xrmdir(path) Xchar *path; X.fi X.ft R X.SH DESCRIPTION X.I Rmdir Xremoves a directory file Xwhose name is given by X.I path. XThe directory must not have any entries other Xthan \*(lq.\*(rq and \*(lq..\*(rq. X.SH "RETURN VALUE XA 0 is returned if the remove succeeds; otherwise a \-1 is Xreturned. X.SH ERRORS XThe named file is removed unless one or more of the Xfollowing are true: X.TP 15 X[ENOTEMPTY] XThe named directory contains files other than ``.'' and ``..'' in it. X.TP 15 X[EPERM] XThe pathname contained a non-ASCII character. X.TP 15 X[ENOENT] XThe pathname was too long. X.TP 15 X[ENOTDIR] XA component of the path prefix is not a directory. X.TP 15 X[ENOENT] XThe named file does not exist. X.TP 15 X[EACCES] XA component of the path prefix denies search permission. X.TP 15 X[EACCES] XWrite permission is denied on the directory containing the link Xto be removed. X.TP 15 X[EBUSY] XThe directory to be removed is the mount point Xfor a mounted file system. X.TP 15 X[EROFS] XThe directory entry to be removed resides on a read-only file system. X.TP 15 X[EFAULT] X.I Path Xpoints outside the process's allocated address space. X.SH WARNING XThis call is implemented via a spawn of /bin/rmdir. Its error return status Xis therefore unreliable, and the value of errno after the call will not Xtypically reflect the above error conditions properly. X.SH "SEE ALSO" Xmkdir(3), unlink(2) END_OF_rmdir.3 if test 1443 -ne `wc -c seekdir.c <<'END_OF_seekdir.c' X/* X seekdir -- reposition a directory stream X X last edit: 24-May-1987 D A Gwyn X X An unsuccessful seekdir() will in general alter the current X directory position; beware. X X NOTE: 4.nBSD directory compaction makes seekdir() & telldir() X practically impossible to do right. Avoid using them! X*/ X X#include X#include X#include X Xextern off_t lseek(); X Xextern int errno; X X#ifndef NULL X#define NULL 0 X#endif X X#ifndef SEEK_SET X#define SEEK_SET 0 X#endif X Xtypedef int bool; /* Boolean data type */ X#define false 0 X#define true 1 X Xvoid Xseekdir( dirp, loc ) X register DIR *dirp; /* stream from opendir() */ X register off_t loc; /* position from telldir() */ X { X register bool rewind; /* "start over when stymied" flag */ X X if ( dirp == NULL || dirp->dd_buf == NULL ) X { X errno = EFAULT; X return; /* invalid pointer */ X } X X /* A (struct dirent)'s d_off is an invented quantity on 4.nBSD X NFS-supporting systems, so it is not safe to lseek() to it. */ X X /* Monotonicity of d_off is heavily exploited in the following. */ X X /* This algorithm is tuned for modest directory sizes. For X huge directories, it might be more efficient to read blocks X until the first d_off is too large, then back up one block, X or even to use binary search on the directory blocks. I X doubt that the extra code for that would be worthwhile. */ X X if ( dirp->dd_loc >= dirp->dd_size /* invalid index */ X || ((struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off > loc X /* too far along in buffer */ X ) X dirp->dd_loc = 0; /* reset to beginning of buffer */ X /* else save time by starting at current dirp->dd_loc */ X X for ( rewind = true; ; ) X { X register struct dirent *dp; X X /* See whether the matching entry is in the current buffer. */ X X if ( (dirp->dd_loc < dirp->dd_size /* valid index */ X || readdir( dirp ) != NULL /* next buffer read */ X && (dirp->dd_loc = 0, true) /* beginning of buffer set */ X ) X && (dp = (struct dirent *)&dirp->dd_buf[dirp->dd_loc])->d_off X <= loc /* match possible in this buffer */ X ) { X for ( /* dp initialized above */ ; X (char *)dp < &dirp->dd_buf[dirp->dd_size]; X dp = (struct dirent *)((char *)dp + dp->d_reclen) X ) X if ( dp->d_off == loc ) X { /* found it! */ X dirp->dd_loc = X (char *)dp - dirp->dd_buf; X return; X } X X rewind = false; /* no point in backing up later */ X dirp->dd_loc = dirp->dd_size; /* set end of buffer */ X } X else /* whole buffer past matching entry */ X if ( !rewind ) X { /* no point in searching further */ X errno = EINVAL; X return; /* no entry at specified loc */ X } X else { /* rewind directory and start over */ X rewind = false; /* but only once! */ X X dirp->dd_loc = dirp->dd_size = 0; X X if ( lseek( dirp->dd_fd, (off_t)0, SEEK_SET ) X != 0 X ) X return; /* errno already set (EBADF) */ X X if ( loc == 0 ) X return; /* save time */ X } X } X } END_OF_seekdir.c if test 2965 -ne `wc -c ssignal.3c <<'END_OF_ssignal.3c' X.TH SSIGNAL 3C "Standard Extension" X.SH NAME Xssignal, gsignal \- software signals X.SH SYNOPSIS X.B #include X.PP X.B int (\(**ssignal (sig, action))( ) X.br X.B int sig, (\(**action)( ); X.PP X.B int gsignal (sig) X.br X.B int sig; X.SH DESCRIPTION X.I Ssignal Xand X.I gsignal Ximplement a software facility similar to X.IR signal (2). XOn some AT&T UNIX versions, an implementation of X.IR ssignal (3) Xis used by the Standard C Library to enable users to indicate the disposition Xof error conditions, and is also available to users for other purposes. This Xlibrary emulates that facility. X.PP XSoftware signals made available to users are associated Xwith integers in the inclusive range 1 through 15. A Xcall to X.I ssignal Xassociates a procedure, X.IR action , Xwith the software signal X.IR sig ; Xthe software signal, X.IR sig , Xis raised Xby a call to X.IR gsignal . XRaising a software signal causes the action established for that signal Xto be X.IR taken . X.PP XThe first argument to X.I ssignal Xis a number identifying the type of signal for which an action is to be Xestablished. The second argument defines the action; it is either the Xname of a (user-defined) X.I action function Xor one of the manifest constants X.SM X.B SIG_DFL X(default) or X.SM X.B SIG_IGN X(ignore). X.I Ssignal Xreturns the action previously established for that signal type; if no Xaction has been established or the signal number is illegal, X.I ssignal Xreturns X.SM X.BR SIG_DFL . X.PP X.I Gsignal Xraises the signal identified by its argument, X.IR sig : X.RS 5 X.PP XIf an action function has been established for X.IR sig , Xthen that action is reset to X.SM X.B SIG_DFL Xand the action function is entered with argument X.IR sig . X.I Gsignal Xreturns the value returned to it by the action function. X.PP XIf the action for X.I sig Xis X.SM X.BR SIG_IGN , X.I gsignal Xreturns the value 1 and takes no other action. X.PP XIf the action for X.I sig Xis X.SM X.BR SIG_DFL , X.I gsignal Xreturns the value 0 and takes no other action. X.PP XIf X.I sig Xhas an illegal value or no action was ever specified for X.IR sig , X.I gsignal Xreturns the value 0 and takes no other action. X.RE X.SH "SEE ALSO" Xsignal(2). END_OF_ssignal.3c if test 2129 -ne `wc -c strtol.3c <<'END_OF_strtol.3c' X.TH STRTOL 3C X.SH NAME Xstrtol, atol, atoi \- convert string to integer X.SH SYNOPSIS X.nf X.B long strtol (str, ptr, base) X.B char \(**str, \(**\(**ptr; X.B int base; X.SH DESCRIPTION X.I Strtol Xreturns as a long integer the value represented by the character string pointed Xto by X.IR str . XThe string is scanned up to the first character inconsistent with the base. XLeading ``white-space'' characters (as defined by X.I isspace Xin X.IR ctype (3C)) Xare ignored. X.PP XIf the value of X.I ptr Xis not (char \(**\(**)\s-1NULL\s+1, a pointer to the character terminating the Xscan is returned in the location pointed to by X.IR ptr . XIf no integer can be formed, that location is set to X.IR str , Xand zero is returned. X.PP XIf X.I base Xis positive (and not greater than 36), it is used as the base for conversion. XAfter an optional leading sign, leading zeros are ignored, and ``0x'' or ``0X'' Xis ignored if X.I base Xis 16. X.PP XIf X.I base Xis zero, the string itself determines the base thusly: After an optional leading Xsign a leading zero indicates octal conversion, and a leading ``0x'' or ``0X'' Xhexadecimal conversion. Otherwise, decimal conversion is used. X.PP XTruncation from long to int can, of course, take place upon assignment or by Xan explicit cast. X.PP X.I Atol(str) Xis equivalent to X.IR "strtol(str, (char \(**\(**)\s-1NULL\s+1, 10)" . X.PP X.I Atoi(str) Xis equivalent to X.IR "(int) strtol(str, (char \(**\(**)\s-1NULL\s+1, 10)" . X.SH SEE ALSO Xctype(3C), scanf(3S), strtod(3C), atoi(3S). X.SH BUGS XOverflow conditions are ignored. END_OF_strtol.3c if test 1522 -ne `wc -c strtol.c <<'END_OF_strtol.c' X/* X * strtol - convert string to long integer. X * X * Written by reading the System V Interface Definition, not the code. X * X * Totally public domain. X * X * Compile with -DTEST to get short interactive main() for testing. X */ X X#include X#include X Xlong Xstrtol(s, p, b) Xchar *s, **p; Xint b; X{ X int base = 10, n = 0, sign = 1, valid = 1; X X /* X * leading sign? X */ X if (*s=='-') X sign=(-1); X else X sign=1; X if (*s=='+' || *s=='-') X ++s; /* skip sign */ X X /* X * what base are we really using? X */ X if (b == 0) { X if (strncmp(s, "0x", 2) == 0 || X strncmp(s, "0X", 2) == 0) { X s += 2; X base = 16; X } else X if (*s == '0') X base = 8; X } X X /* X * convert the string to a number. X */ X while (isascii(*s) && valid) { X switch(*s) { X case '0': X case '1': X case '2': X case '3': X case '4': X case '5': X case '6': X case '7': X n = base*n + *s-'0'; X break; X case '8': X case '9': X if (base >8) X n = base*n + *s-'0'; X else X valid = 0; X break; X case 'a': X case 'b': X case 'c': X case 'd': X case 'e': X case 'f': X if (base == 16) X n = base*n + *s-'a'+10; X else X valid = 0; X break; X case 'A': X case 'B': X case 'C': X case 'D': X case 'E': X case 'F': X if (base == 16) X n = base*n + *s-'A'+10; X else X valid = 0; X break; X default: X valid = 0; X break; X } X ++s; X } X X /* X * if arg `p' is not NULL, a ptr to the character X * terminating the scan will be returned in `p' X */ X if (*p != (char *)NULL) X *p = s; X X return sign * n; X} X X#ifdef TEST Xmain(argc, argv) Xint argc; Xchar **argv; X{ X int i; X long j, strtol(); X X for (i=1; i %ld(%lx)\n", argv[i], j, j); X } X exit(0); X} X#endif /* TEST */ END_OF_strtol.c if test 1737 -ne `wc -c sys._dir.h <<'END_OF_sys._dir.h' X/* X -- definitions for 4.2,4.3BSD directories X X last edit: 25-Apr-1987 D A Gwyn X X A directory consists of some number of blocks of DIRBLKSIZ bytes each, X where DIRBLKSIZ is chosen such that it can be transferred to disk in a X single atomic operation (e.g., 512 bytes on most machines). X X Each DIRBLKSIZ-byte block contains some number of directory entry X structures, which are of variable length. Each directory entry has the X beginning of a (struct direct) at the front of it, containing its X filesystem-unique ident number, the length of the entry, and the length X of the name contained in the entry. These are followed by the NUL- X terminated name padded to a (long) boundary with 0 bytes. The maximum X length of a name in a directory is MAXNAMELEN. X X The macro DIRSIZ(dp) gives the amount of space required to represent a X directory entry. Free space in a directory is represented by entries X that have dp->d_reclen > DIRSIZ(dp). All DIRBLKSIZ bytes in a X directory block are claimed by the directory entries; this usually X results in the last entry in a directory having a large dp->d_reclen. X When entries are deleted from a directory, the space is returned to the X previous entry in the same directory block by increasing its X dp->d_reclen. If the first entry of a directory block is free, then X its dp->d_fileno is set to 0; entries other than the first in a X directory do not normally have dp->d_fileno set to 0. X X prerequisite: X*/ X X#if defined(accel) || defined(sun) || defined(vax) X#define DIRBLKSIZ 512 /* size of directory block */ X#else X#ifdef alliant X#define DIRBLKSIZ 4096 /* size of directory block */ X#else X#ifdef gould X#define DIRBLKSIZ 1024 /* size of directory block */ X#else X#ifdef ns32000 /* Dynix System V */ X#define DIRBLKSIZ 2600 /* size of directory block */ X#else /* be conservative; multiple blocks are okay but fractions are not */ X#define DIRBLKSIZ 4096 /* size of directory block */ X#endif X#endif X#endif X#endif X X#define MAXNAMELEN 255 /* maximum filename length */ X/* NOTE: not MAXNAMLEN, which has been preempted by SVR3 */ X Xstruct direct /* data from read()/_getdirentries() */ X { X unsigned long d_fileno; /* unique ident of entry */ X unsigned short d_reclen; /* length of this record */ X unsigned short d_namlen; /* length of string in d_name */ X char d_name[MAXNAMELEN+1]; /* NUL-terminated filename */ X /* typically shorter */ X }; X X/* X The DIRSIZ macro gives the minimum record length which will hold the X directory entry. This requires the amount of space in a (struct X direct) without the d_name field, plus enough space for the name with a X terminating NUL character, rounded up to a (long) boundary. X X (Note that Berkeley didn't properly compensate for struct padding, X but we nevertheless have to use the same size as the actual system.) X*/ X X#define DIRSIZ( dp ) ((sizeof(struct direct) - (MAXNAMELEN+1) \ X + sizeof(long) + (dp)->d_namlen) \ X / sizeof(long) * sizeof(long)) END_OF_sys._dir.h if test 2977 -ne `wc -c