Previous | Contents | Index |
Sorts an array of objects in place. It implements the quick-sort algorithm.
#include <stdlib.h>Function Variants The qsort function has variants named _qsort32 and _qsort64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.9 for more information on using pointer-size-specific functions.void qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));
base
A pointer to the first member of the array. The pointer should be of type pointer-to-element and cast to type pointer-to-character.nmemb
The number of objects in the array.size
The size of an object, in bytes.compar
A pointer to the comparison function.
Two arguments are passed to the comparison function pointed to by compar. The two arguments point to the objects being compared. Depending on whether the first argument is less than, equal to, or greater than the second argument, the comparison function returns an integer less then, equal to, or greater than 0.The comparison function compar need not compare every byte, so arbitrary data might be contained in the objects in addition to the values being compared.
The order in the output of two objects that compare as equal is unpredictable.
Generates a specified software signal. Generating a signal causes the action routine established by the signal , ssignal , or sigvec function to be invoked.
#include <signal.h>int raise (int sig); (ANSI C)
int raise (int sig[, int sigcode]); (HP C EXTENSION)
sig
The signal to be generated.sigcode
An optional signal code, available only when not compiling in strict ANSI C mode. For example, signal SIGFPE---the arithmetic trap signal---has 10 different codes, each representing a different type of arithmetic trap.The signal codes can be represented by mnemonics or numbers. The arithmetic trap codes are represented by the numbers 1 to 10; the SIGILL codes are represented by the numbers 0 to 2. The code values are defined in the <signal.h> header file. See Table 4-4 for a list of signal mnemonics, codes, and corresponding OpenVMS exceptions.
Calling the raise function has one of the following results:
- If raise specifies a sig argument that is outside the range defined in the <signal.h> header file, then the raise function returns 0, and the errno variable is set to EINVAL.
- If signal , ssignal , or sigvec establishes SIG_DFL (default action) for the signal, then the functions do not return. The image is exited with the OpenVMS error code corresponding to the signal.
- If signal , ssignal , or sigvec establishes SIG_IGN (ignore signal) as the action for the signal, then raise returns its argument, sig.
- signal , ssignal , or sigvec must establish an action function for the signal. That function is called and its return value is returned by raise .
See Chapter 4 for more information on signal processing.
See also gsignal , signal , ssignal , and sigvec .
0 If successful. nonzero If unsuccessful.
Returns pseudorandom numbers in the range 0 to 231 - 1.
#include <stdlib.h>int rand (void);
int rand_r (unsigned int seed); (INTEGRITY SERVERS, ALPHA)
seed
An initial seed value.
The rand function computes a sequence of pseudorandom integers in the range 0 to {RAND_MAX} with a period of at least 232.The rand_r function computes a sequence of pseudorandom integers in the range 0 to {RAND_MAX}. The value of the {RAND_MAX} macro will be at least 32767.
If rand_r is called with the same initial value for the object pointed to by seed and that object is not modified between successive returns and calls to rand_r , the same sequence is generated.
See also srand .
For other random-number algorithms, see random and all the * 48 functions.
n A pseudorandom number.
Generates pseudorandom numbers in a more random sequence.
#include <stdlib.h>long int random (void);
The random function is a random-number generator that has virtually the same calling sequence and initialization properties as the rand function, but produces sequences that are more random. The low 12 bits generated by rand go through a cyclic pattern. All bits generated by random are usable. For example, random () &1 produces a random binary value.The random function uses a nonlinear, additive-feedback, random-number generator employing a default state-array size of 31 integers to return successive pseudorandom numbers in the range from 0 to 231-1 . The period of this random-number generator is approximately 16*( 231-1 ). The size of the state array determines the period of the random-number generator. Increasing the state array size increases the period.
With a full 256 bytes of state information, the period of the random-number generator is greater than 269 , and is sufficient for most purposes.
Like the rand function, the random function produces by default a sequence of numbers that you can duplicate by calling the srandom function with a value of 1 as the seed. The srandom function, unlike the srand function, does not return the old seed because the amount of state information used is more than a single word.
See also rand , srand , srandom , setstate , and initstate .
n A random number.
Raw mode only works with the Curses input routines [w]getch and [w]getstr . Raw mode is not supported with the HP C RTL emulation of UNIX I/O, Terminal I/O, or Standard I/O.
#include <curses.h>raw()
noraw()
Raw mode reads are satisfied on one of two conditions: after a minimum number (5) of characters are input at the terminal or after waiting a fixed time (10 seconds) from receipt of any characters from the terminal.
/* Example of standard and raw input in Curses package. */ #include <curses.h> main() { WINDOW *win1; char vert = '.', hor = '.', str[80]; /* Initialize standard screen, turn echo off. */ initscr(); noecho(); /* Define a user window. */ win1 = newwin(22, 78, 1, 1); leaveok(win1, TRUE); leaveok(stdscr, TRUE); box(stdscr, vert, hor); /* Reset the video, refresh(redraw) both windows. */ mvwaddstr(win1, 2, 2, "Test line terminated input"); wrefresh(win1); /* Do some input and output it. */ nocrmode(); wgetstr(win1, str); mvwaddstr(win1, 5, 5, str); mvwaddstr(win1, 7, 7, "Type something to clear screen"); wrefresh(win1); /* Get another character then delete the window. */ wgetch(win1); wclear(win1); mvwaddstr(win1, 2, 2, "Test raw input"); wrefresh(win1); /* Do some raw input 5 chars or timeout - and output it. */ raw(); getstr(str); noraw(); mvwaddstr(win1, 5, 5, str); mvwaddstr(win1, 7, 7, "Raw input completed"); wrefresh(win1); endwin(); }
Reads bytes from a file and places them in a buffer.
#include <unistd.h>ssize_t read (int file_desc, void *buffer, size_t nbytes); (ISO POSIX-1)
int read (int file_desc, void *buffer, int nbytes); (COMPATIBILITY)
file_desc
A file descriptor. The specified file descriptor must refer to a file currently opened for reading.buffer
The address of contiguous storage in which the input data is placed.nbytes
The maximum number of bytes involved in the read operation.
The read function returns the number of bytes read. The return value does not necessarily equal nbytes. For example, if the input is from a terminal, at most one line of characters is read.
Note
The read function does not span record boundaries in a record file and, therefore, reads at most one record. A separate read must be done for each record.
n The number of bytes read. - 1 Indicates a read error, including physical input errors, illegal buffer addresses, protection violations, undefined file descriptors, and so forth.
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> main() { int fd, i; char buf[10]; FILE *fp ; /* Temporary STDIO file */ /* Create a dummy data file */ if ((fp = fopen("test.txt", "w+")) == NULL) { perror("open"); exit(1); } fputs("XYZ\n",fp) ; fclose(fp) ; /* And now practice "read" */ if ((fd = open("test.txt", O_RDWR, 0, "shr=upd")) <= 0) { perror("open"); exit(0); } /* Read 2 characters into buf. */ if ((i = read(fd, buf, 2)) < 0) { perror("read"); exit(0); } /* Print out what was read. */ if (i > 0) printf("buf='%c%c'\n", buf[0], buf[1]); close(fd); }
Finds entries in a directory.
#include <dirent.h>struct dirent *readdir (DIR *dir_pointer);
int readdir_r (DIR *dir_pointer, struct dirent *entry, struct dirent **result);
dir_pointer
A pointer to the dir structure of an open directory.entry
A pointer to a dirent structure that will be initialized with the directory entry at the current position of the specified stream.result
Upon successful completion, the location where a pointer to entry is stored.
The readdir function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by dir_pointer, and positions the directory stream at the next entry. It returns a NULL pointer upon reaching the end of the directory stream. The dirent structure defined in the <dirent.h> header file describes a directory entry.The type DIR defined in the <dirent.h> header file represents a directory stream. A directory stream is an ordered sequence of all the directory entries in a particular directory. Directory entries represent files. You can remove files from or add files to a directory asynchronously to the operation of the readdir function.
The pointer returned by the readdir function points to data that you can overwrite by another call to readdir on the same directory stream. This data is not overwritten by another call to readdir on a different directory stream.
If a file is removed from or added to the directory after the most recent call to the opendir or rewinddir function, a subsequent call to the readdir function might not return an entry for that file.
When it reaches the end of the directory, or when it detects an invalid seekdir operation, the readdir function returns the null value.
An attempt to seek to an invalid location causes the readdir function to return the null value the next time it is called. A previous telldir function call returns the position.
The readdir_r function is a reentrant version of readdir . In addition to dir_pointer, you must specify a pointer to a dirent structure in which the current directory entry of the specified stream is returned.
If the operation is successful, readdir_r returns 0 and stores one of the following two pointers in result:
- Pointer to entry if the entry was found
- NULL pointer if the end of the directory stream was reached
The storage pointed to by entry must be large enough for a dirent with an array of char d_name member containing at least NAME_MAX + 1 elements.
If an error occurred, an error value is returned that indicates the cause of the error.
Applications wishing to check for error situations should set errno to 0 before calling readdir . If errno is set to nonzero on return, then an error occurred.
See the description of closedir for an example.
x On successful completion of readdir , a pointer to an object of type struct dirent . 0 Successful completion of readdir_r . x On error, an error value ( readdir_r only). NULL An error occurred or end of the directory stream ( readdir_r only). If an error occurred, errno is set to a value indicating the cause.
Reads the contents of the specified symbolic link and places them into a user-supplied buffer.
#include <unistd.h>ssize_t readlink (const char *restrict link_name, char *restrict user_buffer, size_t buffer_size);
link_name
Pointer to the text string representing the name of the symbolic link file.user_buffer
Pointer to the user buffer.buffer_size
Size of the user buffer.
The readlink function reads the contents of the specified symbolic link (link_name) and places them into a user-supplied buffer (user_buffer) of size (buffer_size).See also symlink , unlink , realpath , lchown , and lstat .
n Upon successful completion, the count of bytes placed in the user_buffer - 1 Indicates an error. The buffer is unchanged, and errno is set to indicate the error:
- EACCES -- Read permission is denied in the directory where the symbolic link is being read, or search permission is denied for a component of the path prefix of link_name.
- ENAMETOOLONG -- The length of the link_name argument exceeds PATH_MAX, or a pathname component is longer than NAME_MAX.
- Any errno value from close , open , or read .
Reads from a file.
#include <sys/uio.h>Function Variants The readv function has variants named _readv32 and _readv64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.9 for more information on using pointer-size-specific functions.ssize_t readv (int file_desc, const struct iovec *iov, int iovcnt);
ssize_t _readv64 (int file_desc, struct __iovec64 *iov, int iovcnt);
file_desc
A file descriptor. A file descriptor that must refer to a file currently opened for reading.iov
Array of iovec structures into which the input data is placed.iovcnt
The number of buffers specified by the members of the iov array.
The readv function is equivalent to read , but places the input data into the iovcnt buffers specified by the members of the iov array: iov[0], iov[1], ..., iov[iovcnt-1]. The iovcnt argument is valid if it is greater than 0 and less than or equal to IOV_MAX.Each iovec entry specifies the base address and length of an area in memory where data should be placed. The readv function always fills an area completely before proceeding to the next.
Upon successful completion, readv marks for update the st_atime field of the file.
If the Synchronized Input and Output option is supported:
If the O_DSYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor will complete as defined by synchronized I/O data integrity completion.
If the O_SYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor will complete as defined by synchronized I/O file integrity completion.
If the Shared Memory Objects option is supported:
If file_desc refers to a shared memory object, the result of the read function is unspecified.
For regular files, no data transfer occurs past the offset maximum established in the open file description associated with file_desc.
n The number of bytes read. - 1 Indicates a read error. The function sets errno to one of the following values:
- EAGAIN -- The O_NONBLOCK flag is set for the file descriptor, and the process would be delayed.
- EBADF -- The file_desc argument is not a valid file descriptor open for reading.
- EBADMSG -- The file is a STREAM file that is set to control-normal mode, and the message waiting to be read includes a control part.
- EINTER -- The read operation was terminated because of the receipt of a signal, and no data was transferred.
- EINVAL -- The STREAM or multiplexer referenced by file_desc is linked (directly or indirectly) downstream from a multiplexer.
OR
The sum of the iov_len values in the iov array overflowed an ssize_t .
- EIO -- A physical I/O error has occurred.
OR
The process is a member of a background process attempting to read from its controlling terminal, the process is ignoring or blocking the SIGTTIN signal, or the process group is orphaned.
- EISDIR -- The file_desc argument refers to a directory, and the implementation does not allow the directory to be read using read , pread or readv . Use the readdir function instead.
- EOVERFLOW -- The file is a regular file, nbyte is greater than 0, and the starting position is before the end-of-file and is greater than or equal to the offset maximum established in the open file description associated with file_desc.
The readv function may fail if:
- EINVAL -- The iovcnt argument was less than or equal to 0, or greater than IOV_MAX.
Previous | Next | Contents | Index |