Watcom C Library : qsort, raise, rand, read, readdir
Watcom C Reference/O - P - Q - R 2021. 10. 2. 18:18
Watcom C Library Reference : qsort, raise, rand, read, readdir
qsort
Synopsis : #include <stdlib.h>
void qsort( void *base, size_t num, size_t width,
int (*compar) ( const void *, const void *) );
Description : The qsort function sorts an array of num elements, which is pointed to by base, using Hoare's Quicksort algorithm. Each element in the array is width bytes in size. The comparison function pointed to by compar is called with two arguments that point to elements in the array. The comparison function shall return an integer less than, equal to, or greater than zero if the first argument is less than, equal to, or greater than the second argument.
Returns : The qsort function returns no value.
See Also : bsearch
Example :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *CharVect[] = { "last", "middle", "first" };
int compare( const void *op1, const void *op2 )
{
const char **p1 = (const char **) op1;
const char **p2 = (const char **) op2;
return( strcmp( *p1, *p2 ) );
}
void main( )
{
qsort ( CharVect, sizeof(CharVect)/sizeof (char*), sizeof (char *), compare );
printf( "%s %s %s\n", CharVect[0], CharVect [1], CharVect [2] );
}
produces the following :
first last middle
Classification : ANSI
Systems : All
raise
Synopsis : #include <signal.h>
int raise( int condition );
Description : The raise function signals the exceptional condition indicated by the condition argument. The possible conditions are defined in the <signal.h> header file and are documented with the signal function. The signal function can be used to specify the action which is to take place when such a condition occurs.
Returns : The raise function returns zero when the condition is successfully raised and a non-zero value otherwise. There may be no return of control following the function call if the action for that condition is to terminate the program or to transfer control using the longjmp function.
See Also : signal
Example :
/* This program loops until a SIGINT signal is received or a count is exceeded. */
#include <stdio.h>
#include <signal.h>
sig_atomic_t signal_count;
sig_atomic_t signal_number;
static void alarm_handler( int signum )
{
++signal_count;
signal_number = signum;
}
void main( )
{
unsigned long i;
signal_count = 0;
signal_number = 0;
signal( SIGINT, alarm_handler );
printf( "Program looping. Press Ctrl/c.\n" );
for( i = 0; i < 3000000; i++ ) {
if( 1 > 2000000 ) raise( SIGINT );
if( signal_count > 0 ) (
printf( "Signal %d received\n", signal_number );
break;
}
}
}
Classification : ANSI
Systems : All
rand
Synopsis : #include <stdlib.h>
int rand( void );
Description : The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX (32767). The sequence can be started at different values by calling the srand function.
Returns : The rand function returns a pseudo-random integer.
See Also : srand
Example :
#include <stdio.h>
#include <stdlib.h>
void main( )
{
int i;
for( i=1; i < 10; ++i ) {
printf( "%d\n", rand( ) );
}
}
Classification : ANSI
Systems : All
read
Synopsis : #include <io.h>
int read( int handle, void *buffer, unsigned len );
Description : The read function reads data at the operating system level. The number of bytes transmitted is given by len and the data is transmitted starting at the address specified by buffer.
The handle value is returned by the open function. The access mode must have included either O_RDONLY or O_RDWR when the open function was invoked. The data is read starting at the current file position for the file in question. This file position can be determined with the tell function and can be set with the lseek function.
When O_BINARY is included in the access mode, the data is transmitted unchanged. When O_TEXT is included in the access mode, the data is transmitted with the extra carriage return character removed before each linefeed character encountered in the original data.
Returns : The read function returns the number of bytes of data transmitted from the file to the buffer (this does not include any carriage-return characters that were removed during the transmission).
Normally, this is the number given by the len argument. When the end of the file is encountered before the read completes, the return value will be less than the number of bytes requested.
A value of -1 is returned when an input/output error is detected. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : close, creat, fread, open, write
Example :
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
void main( )
{
int handle;
int size_read;
char buffer[80];
/* open a file for input */
handle = open( "file", O_RDONLY | O_TEXT );
if( handle != -1 ) {
/* read the text */
size_read = read( handle, buffer, sizeof( buffer ) ) ;
/* test for error */
if( size_read == -1 ) {
printf( "Error reading file\n" );
}
/* close the file */
close( handle );
}
}
Classification : POSIX 1003.1
Systems : All
readdir
Synopsis : #include <direct.h>
struct dirent *readdir( DIR *dirp );
Description : The readdir function obtains information about the next matching file name from the argument dirp. The argument dirp is the value returned from the opendir function. The readdir function can be called repeatedly to obtain the list of file names contained in the directory specified by the pathname given to opendir. The function closedir must be called to close the directory and free the memory allocated by opendir.
The file <direct.h> contains definitions for the structure dirent.
typedef struct dirent {
char d_dta[21]; /* disk transfer area */
char d_attr; /* file's attribute */
unsigned short int d_time; /* file's time */
unsigned short int d_date; /* file's date */
long d_size; /* file's size */
char d_name[13]; /* file's name */
ino_t d_ino; /* serial number */
char d_first; /* flag for 1st time */
} DIR;
Returns : When successful, readdir returns a pointer to an object of type struct dirent. When an error occurs, readdir returns the value NULL and errno is set to indicate the error. When the end of the directory is encountered, readdir returns the value NULL and errno is unchanged.
Errors : When an error has occurred, errno contains a value indicating the type of error that has been detected.
EBADF The argument dirp does not refer to an open directory stream.
See Also : closedir, _dos_find Functions, opendir
Example :
To get a list of files contained in the directory \watcom\h on your default disk:
#include <stdio.h>
#include <direct.h>
void main( )
{
DIR *dirp;
struct dirent *direntp;
dirp = opendir( "\\watcom\\h" );
if( dirp != NULL ) {
for( ; ; ) {
direntp = readdir( dirp );
if( direntp == NULL ) break;
printf( "%s\n", direntp->d_name );
}
closedir( dirp );
}
}
Note the use of two adjacent backslash characters(\) within character-string constants to signify a single backslash.
Classification : POSIX 1003.1
Systems : All
This manual describes the WATCOM C library for DOS, Windows, and OS/2, It includes the Standard C Library (as defined in the ANSI C Standard).
WATCOM C Language Reference manual describes the ANSI C Programming language and extensions to it which are supported by WATCOM C/C++ (32bit)