HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index


read

Reads bytes from a file and places them in a buffer.

Format

#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); (COMPATABILITY)


Arguments

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.

Description

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.

Return Values

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.

Example


#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); 
}                                                           


readdir, readdir_r

Finds entries in a directory.

Format

#include <dirent.h>

struct dirent *readdir (DIR *dir_pointer);

int readdir_r (DIR *dir_pointer, struct dirent *entry, struct dirent **result);


Arguments

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.

Description

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:

If an error occurred, an error value is returned that indicates the cause of the error.

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.


Example

See the description of closedir for an example.


Return Values

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.

readlink (ALPHA, I64)

Reads the contents of the specified symbolic link and places them into a user-supplied buffer.

Format

#include <unistd.h>

ssize_t readlink (const char *restrict link_name, char *restrict user_buffer, size_t buffer_size);


Arguments

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.

Description

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 .


Return Values

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 .

readv (ALPHA, I64)

Reads from a file.

Format

#include <sys/uio.h>

ssize_t readv (int file_desc, const struct iovec *iov, int iovcnt);

ssize_t _readv64 (int file_desc, struct __iovec64 *iov, int iovcnt);

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.10 for more information on using pointer-size-specific functions.

Arguments

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.

Description

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.


Return Values

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.

realloc

Changes the size of the area pointed to by the first argument to the number of bytes given by the second argument. These functions are AST-reentrant.

Format

#include <stdlib.h>

void *realloc (void *ptr, size_t size);

Function Variants The realloc function has variants named _realloc32 and _realloc64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

ptr

Points to an allocated area, or can be NULL.

size

The new size of the allocated area.

Description

If ptr is the NULL pointer, the behavior of the realloc function is identical to the malloc function.

The contents of the area are unchanged up to the lesser of the old and new sizes. The ANSI C Standard states that, "If the new size is larger than the old size, the value of the newly allocated portion of memory is indeterminate." For compatibility with old implementations, HP C initializes the newly allocated memory to 0.

For efficiency, the previous actual allocation could have been larger than the requested size. If it was allocated with malloc , the value of the portion of memory between the previous requested allocation and the actual allocation is indeterminate. If it was allocated with calloc , that same memory was initialized to 0. If your application relies on realloc initializing memory to 0, then use calloc instead of malloc to perform the initial allocation.

See also free , cfree , calloc , and malloc .


Return Values

x The address of the area, quadword-aligned (ALPHA ONLY) or octaword-aligned (I64 ONLY) . The address is returned because the area may have to be moved to a new address to reallocate enough space. If the area was moved, the space previously occupied is freed.
NULL Indicates that space cannot be reallocated (for example, if there is not enough room).

realpath

Returns an absolute pathname from the POSIX root.

Format

#include <stdlib.h>

char realpath (const char *restrict file_name, char *restrict resolved_name);


Arguments

file_name

Pointer to the text string representing the name of the file for which you want the absolute path.

resolved name

Pointer to the generated absolute path stored as a null-terminated string.

Description

The realpath function returns an absolute pathname from the POSIX root. The generated pathname is stored as a null-terminated string, up to a maximum of PATH_MAX bytes, in the buffer pointed to by resolved_name.

The realpath function is supported only in POSIX-compliant modes (that is, with DECC$POSIX_COMPLIANT_PATHNAMES defined to one of the allowed values).

See also symlink , unlink , readlink , lchown , and lstat .


Return Values

x Upon successful completion, a pointer to the resolved_name.
NULL Indicates an error. A null pointer is returned, the contents of the buffer pointed to by resolved_name are undefined, and errno is set to indicate the error:
  • ENAMETOOLONG -- The length of the file_name argument exceeds PATH_MAX, or a pathname component is longer than NAME_MAX.
  • ENOENT -- A component of file_name does not name an existing file, or file_name points to an empty string.
  • Any errno value from chdir or stat .

[w]refresh

Repaint the specified window on the terminal screen. The refresh function acts on the stdscr window.

Format

#include <curses.h>

int refresh();

int wrefresh (WINDOW *win);


Argument

win

A pointer to the window.

Description

The result of this process is that the portion of the window not occluded by subwindows or other windows appears on the terminal screen. To see the entire occluded window on the terminal screen, call the touchwin function instead of the refresh or wrefresh function.

See also touchwin .


Return Values

OK Indicates success.
ERR Indicates an error.

remainder (ALPHA, I64)

Returns the floating-point remainder r = x - n*y) when y is nonzero.

Format

#include <math.h>

double remainder (double x, double y);

float remainderf (float x, float y);

long double remainderl (long double x, long double y);


Argument

x

A real number.

y

A real number.

Description

These functions return the floating-point remainder r = x - n*y) when y is nonzero. The value n is the integral value nearest the exact value x/y. That is, n = rint (x/y).

When |n - x/y| = 1/2 , the value n is chosen to be even.

The behavior of the remainder function is independent of the rounding mode.

The remainder functions are functionally equivalent to the remquo functions.


Return Values

r Upon successful completion, these functions return the floating-point remainder r = x - ny when y is nonzero.
Nan If x or y is Nan.

remquo (ALPHA, I64)

Returns the floating-point remainder r = x - n*y) when y is nonzero.

Format

#include <math.h>

double remquo (double x, double y, int * quo);

float remquof (float x, float y, int * quo);

long double remquol (long double x, long double y, int * quo);


Argument

x

A real number.

y

A real number.

quo


Description

The remquo(), remquof(), and remquol() functions compute the same remainder as the remainder(), remainderf(), and remainderl() functions, respectively. In the object pointed to by quo, they store a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2n to the magnitude of the integral quotient of x/y, where n is an implementation-defined integer greater than or equal to 3.

The remquo functions are functionally equivalent to the remainder functions.


Return Values

r Upon successful completion, these functions return the floating-point remainder r = x - ny when y is nonzero.
Nan If x or y is Nan.

remove

Deletes a file.

Format

#include <stdio.h>

int remove (const char *file_spec);


Argument

file_spec

A pointer to the string that is an OpenVMS or a UNIX style file specification. The file specification can include a wildcard in its version number. So, for example, files of the form filename.txt;* can be deleted.

Description

If you specify a directory in the file name and it is a search list that contains an error, HP C for OpenVMS Systems interprets it as a file error.

Note

The DECC$ALLOW_REMOVE_OPEN_FILES feature logical controls the behavior of the remove function on open files. Ordinarily, the operation fails. However, POSIX conformance dictates that the operation succeed.

With DECC$ALLOW_REMOVE_OPEN_FILES enabled, this POSIX conformant behavior is achieved.

When remove is used to delete a symbolic link, the link itself is deleted, not the file to which it refers.

The remove and delete functions are functionally equivalent in the HP C RTL.

See also delete .


Return Values

0 Indicates success.
nonzero value Indicates failure.


Previous Next Contents Index