home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft_Programmers_Library.7z / MPL / msc / mscrtme.txt < prev    next >
Encoding:
Text File  |  2013-11-08  |  1.2 MB  |  43,320 lines

Text Truncated. Only the first 1MB is shown below. Download the file for the complete contents.
  1.  Microsoft  C - RUN-TIME LIBRARY REFERENCE
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  ────────────────────────────────────────────────────────────────────────────
  11.                  Microsoft (R) C - RUN-TIME LIBRARY REFERENCE
  12.  
  13.                               FOR THE MS-DOS (R)
  14.                                OPERATING SYSTEM
  15.  ────────────────────────────────────────────────────────────────────────────
  16.  
  17.  
  18.                             MICROSOFT CORPORATION
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  PUBLISHED BY
  28.  Microsoft Press
  29.  A Division of Microsoft Corporation
  30.  One Microsoft Way, Redmond, Washington 98052-6399
  31.  
  32.  Copyright (C)  1990 by Microsoft Press
  33.  
  34.  All rights reserved. No part of the contents of this book may be reproduced
  35.  or transmitted in any form or by any means without the written permission of
  36.  the publisher.
  37.  
  38.  Library of Congress Cataloging-in-Publication Data
  39.  
  40.  Microsoft C run-time library reference.
  41.  
  42.  Includes  index.
  43.  1. C (Computer program language) 2. Microsoft C
  44.  (Computer program) 3. Macro instructions (Electronic
  45.  computers) I. Microsoft.
  46.  QA76.73.C15M52  1990         005.13'3         89-12240
  47.  ISBN 1-55615-225-6
  48.  
  49.  Printed and bound in the United States of America.
  50.  
  51.  1 2 3 4 5 6 7 8 9  HCHC 3 2 1 0 9
  52.  
  53.  Distributed to the book trade in Canada by General Publishing Company, Ltd.
  54.  
  55.  Distributed to the book trade outside the United States and Canada by
  56.  Penguin
  57.  Books  Ltd.
  58.  
  59.  Penguin Books Ltd., Harmondsworth, Middlesex, England
  60.  Penguin Books Australia Ltd., Ringwood, Victoria, Australia
  61.  Penguin Books N.Z. Ltd., 182-190 Wairau Road, Auckland 10, New Zealand
  62.  
  63.  British Cataloging in Publication Data available.
  64.  
  65.                                              Sample
  66.  Writers:           Editors:                 Programs:
  67.  Phil Nelson        Amanda Clark             Bruce McKinney
  68.  Terry Ward         Moira Macdonald
  69.                     Marjorie Manwaring
  70.                     Bill Nolan
  71.  
  72.  Microsoft, the Microsoft logo, MS-DOS, QuickC, and XENIX are
  73.  registered trademarks and Windows is a trademark of Microsoft Corporation.
  74.  
  75.  AT&T and UNIX are registered trademarks of American Telephone
  76.  and Telegraph Company.
  77.  
  78.  Hercules is a registered trademark of Hercules Computer
  79.  Technology.
  80.  
  81.  IBM is a registered trademark of International Business
  82.  Machines Corporation.
  83.  
  84.  Olivetti is a registered trademark of Ing. C. Olivetti.
  85.  
  86.  Document No. 410840021-520-R00-1088
  87.  Part No. 04412
  88.  
  89.  
  90.  
  91.  
  92.  Table of Contents
  93.  ────────────────────────────────────────────────────────────────────────────
  94.  
  95.  
  96.  
  97.  Introduction
  98.       About the C Run-Time Library
  99.              ANSI C Compatibility
  100.              OS/2 and XENIX(R) Programming
  101.              Expanded Graphics Library
  102.       About This Book
  103.       Other Books of Interest
  104.       Document Conventions
  105.  
  106.  
  107.  PART I  Overview
  108.  ────────────────────────────────────────────────────────────────────────────
  109.  
  110.  
  111.  Chapter 1  Using C Library Routines
  112.  
  113.       1.1   Calling Library Routines
  114.       1.2   Using Header Files
  115.              1.2.1    Including Necessary Definitions
  116.              1.2.2    Including Function Declarations
  117.       1.3   File Names and Path Names
  118.       1.4   Choosing Between Functions and Macros
  119.       1.5   Stack Checking on Entry
  120.       1.6   Handling Errors
  121.       1.7   Operating-System Considerations
  122.       1.8   Floating-Point Support
  123.       1.9   Using Huge Arrays with Library Functions
  124.  
  125.  Chapter 2  Run-Time Routines by Category
  126.  
  127.       2.1   Buffer Manipulation
  128.       2.2   Character Classification and Conversion
  129.       2.3   Data Conversion
  130.       2.4   Directory Control
  131.       2.5   File Handling
  132.       2.6   Graphics
  133.              2.6.1    Low-Level Graphics and Character-Font Functions
  134.              2.6.2    Presentation-Graphics Functions
  135.       2.7   Input and Output
  136.              2.7.1    Text and Binary Modes
  137.              2.7.2    Stream Routines
  138.              2.7.3    Low-Level Routines
  139.              2.7.4    Console and Port I/O
  140.       2.8   Internationalization
  141.       2.9   Math
  142.       2.10  Memory Allocation
  143.              2.10.1   Near and Far Heaps
  144.              2.10.2   Based Heaps
  145.       2.11  Process and Environment Control
  146.       2.12  Searching and Sorting
  147.       2.13  String Manipulation
  148.       2.14  System Calls
  149.              2.14.1   BIOS Interface
  150.              2.14.2   DOS Interface
  151.       2.15  Time
  152.       2.16  Variable-Length Argument Lists
  153.  
  154.  Chapter 3  Global Variables and Standard Types
  155.  
  156.       3.1   _amblksiz
  157.       3.2   daylight, timezone, tzname
  158.       3.3   _doserrno, errno, sys_errlist, sys_nerr
  159.       3.4   _fmode
  160.       3.5   _osmajor, _osminor, _osmode, _osversion
  161.       3.6   environ
  162.       3.7   _psp
  163.       3.8   Standard Types
  164.  
  165.  
  166.  PART II  Run-Time Functions
  167.  ────────────────────────────────────────────────────────────────────────────
  168.  
  169.       About the Run-Time Reference
  170.              abort
  171.              abs
  172.              access
  173.              acos Functions
  174.              alloca
  175.              _arc Functions
  176.              asctime
  177.              asin Functions
  178.              assert
  179.              atan Functions
  180.              atexit
  181.              atof, atoi, atol, _atold
  182.              bdos
  183.              _beginthread
  184.              Bessel Functions
  185.              _bfreeseg
  186.              _bheapseg
  187.              _bios_disk
  188.              _bios_equiplist
  189.              _bios_keybrd
  190.              _bios_memsize
  191.              _bios_printer
  192.              _bios_serialcom
  193.              _bios_timeofday
  194.              bsearch
  195.              cabs, cabsl
  196.              calloc Functions
  197.              ceil, ceill
  198.              _cexit, _c_exit
  199.              cgets
  200.              _chain_intr
  201.              chdir
  202.              _chdrive
  203.              chmod
  204.              chsize
  205.              _clear87
  206.              clearerr
  207.              _clearscreen
  208.              clock
  209.              close
  210.              _control87
  211.              cos Functions
  212.              cprintf
  213.              cputs
  214.              creat
  215.              cscanf
  216.              ctime
  217.              cwait
  218.              dieeetomsbin, dmsbintoieee
  219.              difftime
  220.              _displaycursor
  221.              div
  222.              _dos_allocmem
  223.              _dos_close
  224.              _dos_creat Functions
  225.              _dos_find Functions
  226.              _dos_freemem
  227.              _dos_getdate
  228.              _dos_getdiskfree
  229.              _dos_getdrive
  230.              _dos_getfileattr
  231.              _dos_getftime
  232.              _dos_gettime
  233.              _dos_getvect
  234.              _dos_keep
  235.              _dos_open
  236.              _dos_read
  237.              _dos_setblock
  238.              _dos_setdate
  239.              _dos_setdrive
  240.              _dos_setfileattr
  241.              _dos_setftime
  242.              _dos_settime
  243.              _dos_setvect
  244.              _dos_write
  245.              dosexterr
  246.              dup, dup2
  247.              ecvt
  248.              _ellipse Functions
  249.              _enable
  250.              _endthread
  251.              eof
  252.              exec Functions
  253.              exit, _exit
  254.              exp, expl
  255.              _expand Functions
  256.              fabs, fabsl
  257.              fclose, fcloseall
  258.              fcvt
  259.              fdopen
  260.              feof
  261.              ferror
  262.              fflush
  263.              fgetc, fgetchar
  264.              fgetpos
  265.              fgets
  266.              fieeetomsbin, fmsbintoieee
  267.              filelength
  268.              fileno
  269.              _floodfill, _floodfill_w
  270.              floor, floorl
  271.              flushall
  272.              fmod, fmodl
  273.              fopen
  274.              FP_OFF, FP_SEG
  275.              _fpreset
  276.              fprintf
  277.              fputc, fputchar
  278.              fputs
  279.              fread
  280.              free Functions
  281.              _freect
  282.              freopen
  283.              frexp, frexpl
  284.              fscanf
  285.              fseek
  286.              fsetpos
  287.              _fsopen
  288.              fstat
  289.              ftell
  290.              ftime
  291.              _fullpath
  292.              fwrite
  293.              gcvt
  294.              _getactivepage
  295.              _getarcinfo
  296.              _getbkcolor
  297.              getc, getchar
  298.              getch, getche
  299.              _getcolor
  300.              _getcurrentposition Functions
  301.              getcwd
  302.              _getdcwd
  303.              _getdrive
  304.              getenv
  305.              _getfillmask
  306.              _getfontinfo
  307.              _getgtextextent
  308.              _getimage Functions
  309.              _getlinestyle
  310.              _getphyscoord
  311.              getpid
  312.              _getpixel Functions
  313.              gets
  314.              _gettextcolor
  315.              _gettextcursor
  316.              _gettextposition
  317.              _gettextwindow
  318.              _getvideoconfig
  319.              _getviewcoord Functions
  320.              _getvisualpage
  321.              getw
  322.              _getwindowcoord
  323.              _getwritemode
  324.              gmtime
  325.              _grstatus
  326.              halloc
  327.              _hard Functions
  328.              _heapadd Functions
  329.              _heapchk Functions
  330.              _heapmin Functions
  331.              _heapset Functions
  332.              _heapwalk Functions
  333.              hfree
  334.              hypot, hypotl
  335.              _imagesize Functions
  336.              inp, inpw
  337.              int86
  338.              int86x
  339.              intdos
  340.              intdosx
  341.              is Functions
  342.              isatty
  343.              itoa
  344.              kbhit
  345.              labs
  346.              ldexp, ldexpl
  347.              ldiv
  348.              lfind
  349.              _lineto Functions
  350.              localeconv
  351.              localtime
  352.              locking
  353.              log Functions
  354.              long double Functions
  355.              longjmp
  356.              _lrotl, _lrotr
  357.              lsearch
  358.              lseek
  359.              ltoa
  360.              _makepath
  361.              malloc Functions
  362.              matherr, _matherrl
  363.              max
  364.              _memavl
  365.              memccpy, _fmemccpy
  366.              memchr, _fmemchr
  367.              memcmp, _fmemcmp
  368.              memcpy, _fmemcpy
  369.              memicmp, _fmemicmp
  370.              _memmax
  371.              memmove, _fmemmove
  372.              memset, _fmemset
  373.              min
  374.              mkdir
  375.              mktemp
  376.              mktime
  377.              modf, modfl
  378.              movedata
  379.              _moveto Functions
  380.              _msize Functions
  381.              onexit
  382.              open
  383.              _outgtext
  384.              _outmem
  385.              outp, outpw
  386.              _outtext
  387.              _pclose
  388.              perror
  389.              _pg_analyzechart Functions
  390.              _pg_analyzepie
  391.              _pg_analyzescatter Functions
  392.              _pg_chart Functions
  393.              _pg_chartscatter Functions
  394.              _pg_chartpie
  395.              _pg_defaultchart
  396.              _pg_getchardef
  397.              _pg_getpalette
  398.              _pg_getstyleset
  399.              _pg_hlabelchart
  400.              _pg_initchart
  401.              _pg_resetpalette
  402.              _pg_resetstyleset
  403.              _pg_setchardef
  404.              _pg_setpalette
  405.              _pg_setstyleset
  406.              _pg_vlabelchart
  407.              _pie Functions
  408.              _pipe
  409.              _polygon Functions
  410.              _popen
  411.              pow Functions
  412.              printf
  413.              putc, putchar
  414.              putch
  415.              putenv
  416.              _putimage Functions
  417.              puts
  418.              putw
  419.              qsort
  420.              raise
  421.              rand
  422.              read
  423.              realloc Functions
  424.              _rectangle Functions
  425.              _registerfonts
  426.              _remapallpalette, _remappalette
  427.              remove
  428.              rename
  429.              rewind
  430.              rmdir
  431.              rmtmp
  432.              _rotl, _rotr
  433.              scanf
  434.              _scrolltextwindow
  435.              _searchenv
  436.              segread
  437.              _selectpalette
  438.              _setactivepage
  439.              _setbkcolor
  440.              setbuf
  441.              _setcliprgn
  442.              _setcolor
  443.              _setfillmask
  444.              _setfont
  445.              _setgtextvector
  446.              setjmp
  447.              _setlinestyle
  448.              setlocale
  449.              setmode
  450.              _setpixel Functions
  451.              _settextcolor
  452.              _settextcursor
  453.              _settextposition
  454.              _settextrows
  455.              _settextwindow
  456.              setvbuf
  457.              _setvideomode
  458.              _setvideomoderows
  459.              _setvieworg
  460.              _setviewport
  461.              _setvisualpage
  462.              _setwindow
  463.              _setwritemode
  464.              signal
  465.              sin Functions
  466.              sopen
  467.              spawn Functions
  468.              _splitpath
  469.              sprintf
  470.              sqrt, sqrtl
  471.              srand
  472.              sscanf
  473.              stackavail
  474.              stat
  475.              _status87
  476.              strcat, _fstrcat
  477.              strchr, _fstrchr
  478.              strcmp, _fstrcmp
  479.              strcmpi
  480.              strcoll
  481.              strcpy, _fstrcpy
  482.              strcspn, _fstrcspn
  483.              _strdate
  484.              strdup Functions
  485.              strerror, _strerror
  486.              strftime
  487.              stricmp, _fstricmp
  488.              strlen, _fstrlen
  489.              strlwr, _fstrlwr
  490.              strncat, _fstrncat
  491.              strncmp, _fstrncmp
  492.              strncpy, _fstrncpy
  493.              strnicmp, _fstrnicmp
  494.              strnset, _fstrnset
  495.              strpbrk, _fstrpbrk
  496.              strrchr, _fstrrchr
  497.              strrev, _fstrrev
  498.              strset, _fstrset
  499.              strspn, _fstrspn
  500.              strstr, _fstrstr
  501.              _strtime
  502.              strtod, strtol, _strtold, strtoul
  503.              strtok, _fstrtok
  504.              strupr, _fstrupr
  505.              strxfrm
  506.              swab
  507.              system
  508.              tan Functions
  509.              tell
  510.              tempnam, tmpnam
  511.              time
  512.              tmpfile
  513.              toascii, tolower, toupper Functions
  514.              tzset
  515.              ultoa
  516.              umask
  517.              ungetc
  518.              ungetch
  519.              unlink
  520.              _unregisterfonts
  521.              utime
  522.              va_arg, va_end, va_start
  523.              vfprintf, vprintf, vsprintf
  524.              wait
  525.              _wrapon
  526.              write
  527.  
  528.  Index
  529.  
  530.  
  531.  
  532.  
  533.  Introduction
  534.  ────────────────────────────────────────────────────────────────────────────
  535.  
  536.  The Microsoft(R) C Run-Time Library is a set of over 500 ready-to-use
  537.  functions and macros designed for use in C programs. The run-time library
  538.  makes programming easier by providing
  539.  
  540.  
  541.    ■   Fast and efficient routines to perform common programming tasks (such
  542.        as string manipulation), sparing you the time and effort needed to
  543.        write such routines
  544.  
  545.    ■   Reliable methods of performing operating-system functions (such as
  546.        opening and closing files)
  547.  
  548.  
  549.  The C run-time library is important because it provides basic functions not
  550.  provided by the C language itself. These functions include input and output,
  551.  memory allocation, process control, graphics, and many others.
  552.  
  553.  This book describes the Microsoft C run-time library routines included with
  554.  the Microsoft Professional Development System version 6.0. These comprise
  555.  all of the routines included with earlier versions of Microsoft C, as well
  556.  as many new routines.
  557.  
  558.  ────────────────────────────────────────────────────────────────────────────
  559.  NOTE
  560.  
  561.  Microsoft documentation uses the term "OS/2" to refer to the OS/2
  562.  systems─Microsoft Operating System/2 (MS(R) OS/2) and IBM(R) OS/2.
  563.  Similarly, the term "DOS" refers to both the MS-DOS(R) and IBM Personal
  564.  Computer DOS operating systems. The name of a specific operating system is
  565.  used when it is necessary to note features that are unique to that system.
  566.  ────────────────────────────────────────────────────────────────────────────
  567.  
  568.  
  569.  About the C Run-Time Library
  570.  
  571.  The Microsoft C run-time library contains a number of new routines and
  572.  features which support American National Standards Institute (ANSI) C
  573.  compatibility, OS/2 and XENIX(R) programming, and sophisticated graphics
  574.  programming.
  575.  
  576.  To ease the task of transporting programs from one operating system to
  577.  another, the description of each library routine includes compatibility
  578.  boxes, which show at a glance whether the routine is compatible with ANSI C,
  579.  MS-DOS, OS/2, UNIX(R), and XENIX. (In this book, references to XENIX systems
  580.  also encompass UNIX and other UNIX-like systems.)
  581.  
  582.  
  583.  ANSI C Compatibility
  584.  
  585.  The C run-time library routines are designed for compatibility with the ANSI
  586.  C standard, which Microsoft C compilers support. The major innovation of
  587.  ANSI C is to permit argument-type lists in function prototypes
  588.  (declarations). Given the information in the function prototype, the
  589.  compiler can check later references to the function to make sure that the
  590.  references use the correct number and type of arguments and the correct
  591.  return value.
  592.  
  593.  To take advantage of the compiler's type-checking ability, the include files
  594.  that accompany the C run-time library have been expanded. In addition to the
  595.  definitions and declarations required by library routines, the include files
  596.  now contain function declarations with argument-type lists. Several new
  597.  include files have also been added. The names of these files are chosen to
  598.  maximize compatibility with the ANSI C standard and with XENIX and UNIX
  599.  names.
  600.  
  601.  
  602.  OS/2 and XENIX(R) Programming
  603.  
  604.  Microsoft C run-time library routines are designed to maintain maximum
  605.  compatibility between MS-DOS, OS/2, and XENIX or UNIX systems. The library
  606.  offers a number of operating-system interface routines that allow you to
  607.  take advantage of specific DOS and OS/2 features.
  608.  
  609.  Most of the functions in the C library for DOS and OS/2 are compatible with
  610.  like-named routines in the C library for XENIX. For additional
  611.  compatibility, the math library functions have been extended to provide
  612.  exception handling in the same manner as the UNIX System V math functions.
  613.  
  614.  
  615.  Expanded Graphics Library
  616.  
  617.  The Microsoft C run-time library now contains over one hundred graphics
  618.  routines. The core of this library consists of several dozen low-level
  619.  graphics routines, which allow your programs to select video modes, set
  620.  points, draw lines, change colors, and draw shapes such as rectangles and
  621.  ellipses. You can display real-valued data, such as floating-point values,
  622.  within windows of different sizes by using various coordinate systems.
  623.  
  624.  Recent additions to the graphics library include presentation graphics and
  625.  fonts. The presentation-graphics library provides powerful tools for adding
  626.  presentation-quality graphics to your programs. These routines can display
  627.  data as a variety of graphs, including pie charts, bar and column charts,
  628.  line graphs, and scatter diagrams.
  629.  
  630.  The fonts library allows your programs to display various styles and sizes
  631.  of text in graphics images or charts. You can use font-manipulation routines
  632.  with any graphics routines that display text, including presentation
  633.  graphics.
  634.  
  635.  
  636.  About This Book
  637.  
  638.  This book assumes that you understand the C language and know how to compile
  639.  and link programs. If you have questions about these subjects, consult your
  640.  compiler documentation.
  641.  
  642.  This book has two parts. Part 1, "Overview," introduces the Microsoft C
  643.  library. It describes general rules for using the library and summarizes the
  644.  main categories of library routines. Part 1 contains the following chapters:
  645.  
  646.  
  647.  
  648.    ■   Chapter 1, "Using C Library Routines," gives general rules for
  649.        understanding and using C library routines and mentions special
  650.        considerations that apply to certain routines. It is recommended that
  651.        you read this chapter before using the run-time library; you may also
  652.        want to turn to Chapter 1 when you have questions about library
  653.        procedures.
  654.  
  655.    ■   Chapter 2, "Run-Time Routines by Category," lists the C library
  656.        routines by category and discusses considerations that apply to each
  657.        category. This chapter makes it easy to locate routines by task. Once
  658.        you find the routine you want, turn to the reference page in Part 2
  659.        for a detailed description.
  660.  
  661.    ■   Chapter 3, "Global Variables and Standard Types," describes variables
  662.        and types that are used by library routines. Global variables and
  663.        standard types are also described in the reference descriptions of the
  664.        routines that use them.
  665.  
  666.  
  667.  Part 2, "Run-Time Functions," describes the library routines in alphabetical
  668.  order. Once you are familiar with the C library rules and procedures, you
  669.  will probably use this part most often.
  670.  
  671.  
  672.  Other Books of Interest
  673.  
  674.  This book provides a guide to the C run-time library provided with the
  675.  Microsoft C Professional Development System version 6.0.
  676.  
  677.  The following books cover a variety of topics that you may find useful. They
  678.  are listed only for your convenience. With the exception of its own
  679.  publications, Microsoft does not endorse these books or recommend them over
  680.  others on the same subject.
  681.  
  682.  
  683.    ■   Barkakati, Nabajyoti. The Waite Group's Microsoft C Bible.
  684.        Indianapolis, IN: Howard W. Sams, 1988.
  685.  
  686.        A topical guide to the Microsoft C run-time library. A similar volume
  687.        is available for the Microsoft QuickC(R) product.
  688.  
  689.    ■   Campbell, Joe. C Programmer's Guide to Serial Communications.
  690.        Indianapolis, IN: Howard W. Sams & Company, 1987.
  691.  
  692.        A comprehensive guide to the specialized area of serial communication
  693.        programming in C.
  694.  
  695.    ■   Hansen, Augie. Proficient C: The Microsoft Guide to Intermediate &
  696.        Advanced C Programming. Redmond, WA: Microsoft Press, 1987.
  697.  
  698.        An intermediate-level guide to C programming.
  699.  
  700.    ■   Harbison, Samuel P., and Guy L. Steele, Jr. C: A Reference Manual, 2d
  701.        ed. Englewood Cliffs, NJ: Prentice Hall, 1987.
  702.  
  703.        A comprehensive guide to the C language and the standard library.
  704.  
  705.    ■   Kernighan, Brian W., and Dennis M. Ritchie. The C Programming
  706.        Language, 2d ed. Englewood Cliffs, NJ: Prentice Hall, 1988.
  707.  
  708.        The first edition of this book is the classic definition of the C
  709.        language. The second edition includes new information on the proposed
  710.        ANSI C standard.
  711.  
  712.    ■   Lafore, Robert. Microsoft C Programming for the IBM. Indianapolis, IN:
  713.        Howard W. Sams & Company, 1987.
  714.  
  715.        The first half of this book teaches C. The second half concentrates on
  716.        specifics of the PC environment, such as BIOS calls, memory, and video
  717.        displays.
  718.  
  719.    ■   Mark Williams Company. ANSI C: A Lexical Guide. Englewood Cliffs, NJ:
  720.        Prentice Hall, 1988.
  721.  
  722.        A dictionary-style guide to the ANSI C standard.
  723.  
  724.    ■   Plauger, P. J., and Jim Brodie. Standard C. Redmond, WA: Microsoft
  725.        Press, 1989.
  726.  
  727.        A quick reference guide to the ANSI C implementation by the secretary
  728.        and chairman of the ANSI-authorized C Programming Language Standards
  729.        Committee.
  730.  
  731.    ■   Plum, Thomas. Reliable Data Structures in C. Cardiff, NJ: Plum Hall,
  732.        1985.
  733.  
  734.        An intermediate-level look at data structures using the C language.
  735.  
  736.    ■   Plum, Thomas, and Jim Brodie. Efficient C. Cardiff, NJ: Plum Hall,
  737.        1985.
  738.  
  739.        A guide to techniques for increasing the efficiency of C programs.
  740.  
  741.    ■   Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William
  742.        T. Vetterling. Numerical Recipes in C: The Art of Scientific
  743.        Computing. New York: Cambridge University Press, 1988.
  744.  
  745.        A comprehensive look at numerical techniques using the C language.
  746.  
  747.    ■   Schustack, Steve. Variations in C: Programming Techniques for
  748.        Developing Efficient Professional Applications. Redmond, WA: Microsoft
  749.        Press, 1985.
  750.  
  751.        An intermediate-level guide to developing business applications in C.
  752.  
  753.    ■   Ward, Robert. Debugging C. Indianapolis, IN: Que Corporation, 1986.
  754.  
  755.        An advanced guide to the theory and practice of debugging C programs.
  756.  
  757.    ■   Wilton, Richard. Programmer's Guide to PC and PS/2 Video
  758.        Systems:Maximum Video Performance from the EGA, VGA, HGC, & MCGA.
  759.        Redmond, WA: Microsoft Press, 1987.
  760.  
  761.        An advanced guide to all the PC and PS/2 video modes.
  762.  
  763.  
  764.  
  765.  Document Conventions
  766.  
  767.  This book uses the following document conventions :
  768.  
  769.  Example                           Description
  770.  ────────────────────────────────────────────────────────────────────────────
  771.  STDIO.H                           Uppercase letters indicate file names,
  772.                                    segment names, registers, and terms used
  773.                                    at the operating-system command level.
  774.  
  775.  _far                              Boldface letters indicate C keywords,
  776.                                    operators, language-specific characters,
  777.                                    and library routines. Within discussions
  778.                                    of syntax, bold type indicates that the
  779.                                    text must be entered exactly as shown.
  780.  
  781.  expression                        Words in italics indicate placeholders
  782.                                    for information you must supply, such as
  783.                                    a file name. Italics are also
  784.                                    occasionally used for emphasis in the
  785.                                    text.
  786.  
  787.  [[option]]                        Items inside double square brackets are
  788.                                    optional.
  789.  
  790.  #pragma pack {1|2}                Braces and a vertical bar indicate a
  791.                                    choice among two or more items. You must
  792.                                    choose one of these items unless double
  793.                                    square brackets surround the braces.
  794.  
  795.  #include <io.h>                   This font is used for examples, user
  796.                                    input, program output, and error
  797.                                    messages in text.
  798.  
  799.  CL options [[files...]]           Three dots following an item indicate
  800.                                    that more items having the same form may
  801.                                    appear.
  802.  
  803.  while()                           A column of three dots tells you that
  804.  {                                 part of the example program has been
  805.     .                              intentionally omitted.
  806.     .
  807.     .
  808.  }
  809.  
  810.  CTRL+ENTER                        Small capital letters are used for the
  811.                                    names of keys on the keyboard. When you
  812.                                    see a plus sign (+) between two key
  813.                                    names, you should hold down the first
  814.                                    key while pressing the second.
  815.  
  816.                                    The carriage-return key, sometimes
  817.                                    marked as a bent arrow on the keyboard,
  818.                                    is called ENTER.
  819.  
  820.  "argument"                        Quotation marks enclose a new term the
  821.                                    first time it is defined in text.
  822.  
  823.  "C string"                        Some C constructs, such as strings,
  824.                                    require quotation marks. Quotation marks
  825.                                    required by the language have the form "
  826.                                    " and ' ' rather than " " and ' '.
  827.  
  828.  Color Graphics Adapter (CGA)      The first time an acronym is used, it is
  829.                                    often spelled out.
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  PART I  Overview
  837.  ────────────────────────────────────────────────────────────────────────────
  838.  
  839.  The first part of this book provides an overview of the run-time library
  840.  provided with the Microsoft C Professional Development System.
  841.  
  842.  Chapter 1 is a general guide to the use of the run-time library routines.
  843.  
  844.  Chapter 2 lists the routines by category.
  845.  
  846.  Chapter 3 tells how to access global variables and types defined in the
  847.  run-time library.
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  Chapter 1  Using C Library Routines
  855.  ────────────────────────────────────────────────────────────────────────────
  856.  
  857.  This chapter provides basic information about how to use Microsoft C library
  858.  routines. It also describes some special rules, such as file- and path-name
  859.  conventions, that apply to particular routines. You should read this chapter
  860.  before you begin to use C library routines, and you may also want to refer
  861.  back to it if you have questions about library procedures.
  862.  
  863.  
  864.  1.1  Calling Library Routines
  865.  
  866.  To use a C library routine, simply call it in your program, just as if it is
  867.  defined there. For instance, suppose you write the following program and
  868.  name it SAMPLE.C:
  869.  
  870.    #include <stdio.h>
  871.    main()
  872.    {
  873.       printf( "Microsoft C" );
  874.    }
  875.  
  876.  The program prints  Microsoft C  by calling the printf routine, which is
  877.  part of the standard C library. Calling a library routine normally involves
  878.  two groups of files:
  879.  
  880.  
  881.    1.  Header ("include") files that contain declarations and type
  882.        definitions required by library routines
  883.  
  884.    2.  Library files that contain the library routines in compiled form
  885.  
  886.  
  887.  Header files and library files are both included with Microsoft C. Header
  888.  files are used when compiling, and library files are used when linking.
  889.  
  890.  You include the necessary header files in your program source code with
  891.  #include directives. The description of each library routine in Part 2,
  892.  "Reference," tells you what header file the routine requires. Since printf
  893.  requires the STDIO.H header file, the SAMPLE.C program contains the
  894.  following line:
  895.  
  896.    #include <stdio.h>
  897.  
  898.  This line causes the compiler to insert the contents of STDIO.H into the
  899.  source file SAMPLE.C.
  900.  
  901.  After you compile the source file, you link the resulting object (.OBJ) file
  902.  with the appropriate library (.LIB) file to create an executable (.EXE)
  903.  file. Your object file contains the name of every routine that your program
  904.  calls, including library routines. If a routine is not defined in your
  905.  program, the linker searches for its code in a library file and includes
  906.  that code in the executable file.
  907.  
  908.  Normally, the code for standard library routines is contained in the
  909.  "default library" that you create when installing Microsoft C. Since the
  910.  linker automatically searches the default library, you do not need to
  911.  specify that library's name when linking your program. The following command
  912.  links the example program with the default library:
  913.  
  914.    link sample,,,;
  915.  
  916.  If you call a library routine that is not contained in the default library,
  917.  you must give the linker the name of the library file that contains the
  918.  routine. For instance, suppose your program uses a Microsoft C graphics
  919.  routine and you did not make GRAPHICS.LIB part of your default library when
  920.  installing Microsoft C. You would then link the program using a line like
  921.  the following:
  922.  
  923.    link sample,,, graphics.lib;
  924.  
  925.  For more information about libraries and linking, consult the installation
  926.  documentation for your compiler.
  927.  
  928.  
  929.  1.2  Using Header Files
  930.  
  931.  As stated in the previous section, you should include C header files when
  932.  using library routines. This section describes particular reasons why header
  933.  files are required.
  934.  
  935.  
  936.  1.2.1  Including Necessary Definitions
  937.  
  938.  Many C library routines use constants, type definitions, or macros defined
  939.  in a header file. To use the routine, you must include the header file
  940.  containing the needed definition(s). The following list gives examples:
  941.  
  942.  Definition                        Example
  943.  ────────────────────────────────────────────────────────────────────────────
  944.  Macro                             If a library routine is implemented as a
  945.                                    macro, the macro definition appears in a
  946.                                    header file. For instance, the toupper
  947.                                    macro is defined in the header file
  948.                                    CTYPE.H.
  949.  
  950.  Manifest constant                 Many library routines refer to constants
  951.                                    that are defined in header files. For
  952.                                    instance, the open routine uses
  953.                                    constants such as O_CREAT, which is
  954.                                    defined in the header file FCNTL.H.
  955.  
  956.  Type definition                   Some library routines return a structure
  957.                                    or take a structure as an argument. For
  958.                                    example, stream input/output routines
  959.                                    use a structure of type FILE, which is
  960.                                    defined in STDIO.H.
  961.  
  962.  
  963.  1.2.2  Including Function Declarations
  964.  
  965.  The Microsoft C header files also contain function declarations for every
  966.  function in the C library. These declarations are in the style recommended
  967.  by the ANSI C standard. Given these declarations, the compiler can perform
  968.  "type checking" on every reference to a library function, making sure that
  969.  you have used the correct return type and arguments. Function declarations
  970.  are sometimes called "prototypes," since the declaration serves as a
  971.  prototype or template for every subsequent reference to the function.
  972.  
  973.  A function declaration lists the name of the function, its return type, and
  974.  the number and type of its arguments. For instance, below is the declaration
  975.  of the pow library function from the header file MATH.H:
  976.  
  977.    double pow( double x, double y );
  978.  
  979.  The example declares that pow returns a value of type double and takes two
  980.  arguments of type double. Given this declaration, the compiler can check
  981.  every reference to pow in your program to ensure that the reference passes
  982.  two double arguments to pow and takes a return value of type double.
  983.  
  984.  The compiler can perform type checking only for function references that
  985.  appear after the function declaration. Because of this, function
  986.  declarations normally appear near the beginning of the source file, prior to
  987.  any use of the functions they declare.
  988.  
  989.  Function declarations are especially important for functions that return a
  990.  value of some type other than int, which is the default. For example, the
  991.  pow function returns a double value. If you do not declare such a function,
  992.  the compiler treats its return value as int, which can cause unexpected
  993.  results.
  994.  
  995.  It is also a good practice to provide declarations for functions that you
  996.  write. If you do not want to type the declarations by hand, you can generate
  997.  them automatically by using the /Zg compiler option. This option causes the
  998.  compiler to generate ANSI-standard function declarations for every function
  999.  defined in the current source file. Redirect this output to a file, then
  1000.  insert the file near the beginning of your source file.
  1001.  
  1002.  Your program can contain more than one declaration of the same function, as
  1003.  long as the declarations do not conflict. This is important if you have old
  1004.  programs whose function declarations do not contain argument-type lists. For
  1005.  instance, if your program contains the declaration
  1006.  
  1007.    char *calloc( );
  1008.  
  1009.  you can later include the following declaration:
  1010.  
  1011.    char *calloc(unsigned, unsigned);
  1012.  
  1013.  Because the two declarations are compatible, even though they are not
  1014.  identical, no conflict occurs. The second declaration simply gives more
  1015.  information about function arguments than the second. A conflict would
  1016.  arise, however, if the declarations gave a different number of arguments or
  1017.  gave arguments of different types.
  1018.  
  1019.  Some library functions can take a variable number of arguments. For
  1020.  instance, the printf function can take one argument or several. The compiler
  1021.  can perform only limited type checking on such functions, a factor that
  1022.  affects the following library functions:
  1023.  
  1024.  
  1025.    ■   In calls to cprintf, cscanf, printf, and scanf, only the first
  1026.        argument (the format string) is type checked.
  1027.  
  1028.    ■   In calls to fprintf, fscanf, sprintf, and sscanf, only the first two
  1029.        arguments (the file or buffer and the format string) are type checked.
  1030.  
  1031.    ■   In calls to open, only the first two arguments (the path name and the
  1032.        open flag) are type checked.
  1033.  
  1034.    ■   In calls to sopen, only the first three arguments (the path name, the
  1035.        open flag, and the sharing mode) are type checked.
  1036.  
  1037.    ■   In calls to execl, execle, execlp, and execlpe, only the first two
  1038.        arguments (the path name and the first argument pointer) are type
  1039.        checked.
  1040.  
  1041.    ■   In calls to spawnl, spawnle, spawnlp, and spawnlpe, only the first
  1042.        three arguments (the mode flag, the path name, and the first argument
  1043.        pointer) are type checked.
  1044.  
  1045.  
  1046.  
  1047.  1.3  File Names and Path Names
  1048.  
  1049.  Many library routines take strings representing paths and file names as
  1050.  arguments. If you plan to transport your programs to the XENIX operating
  1051.  system, you should remember that XENIX uses file- and path-name conventions
  1052.  that are different from those used by DOS and OS/2. If you do not plan to
  1053.  transport your programs to XENIX, you can skip this section.
  1054.  
  1055.  
  1056.  Case Sensitivity
  1057.  
  1058.  The DOS and OS/2 operating systems are not case sensitive (they do not
  1059.  distinguish between uppercase and lowercase letters). Thus, SAMPLE.C and
  1060.  Sample.C refer to the same file in DOS and OS/2. However, the XENIX
  1061.  operating system is case sensitive. In XENIX, SAMPLE.C and Sample.C refer to
  1062.  different files. To transport programs to XENIX, choose file and path names
  1063.  that work correctly in XENIX, since either case works in DOS and OS/2. For
  1064.  instance, the following directives are identical in DOS and OS/2, but only
  1065.  the second works in XENIX:
  1066.  
  1067.    #include <STDIO.H>
  1068.    #include <stdio.h>
  1069.  
  1070.  
  1071.  Subdirectory Conventions
  1072.  
  1073.  Under XENIX, certain header files are normally placed in a subdirectory
  1074.  named SYS. Microsoft C follows this convention to ease the process of
  1075.  transporting programs to XENIX. If you do not plan to transport your
  1076.  programs, you can place the SYS header files elsewhere.
  1077.  
  1078.  
  1079.  Path-Name Delimiters
  1080.  
  1081.  XENIX uses the slash (/) in path names, while DOS and OS/2 use the backslash
  1082.  (\). To transport programs to XENIX, it is advantageous to use path-name
  1083.  delimiters that are compatible with XENIX whenever possible.
  1084.  
  1085.  
  1086.  1.4  Choosing Between Functions and Macros
  1087.  
  1088.  This book uses the words "routine" and "function" interchangeably. However,
  1089.  the term "routine" actually encompasses both functions and macros. Because
  1090.  functions and macros have different properties, you should pay attention to
  1091.  which form you are using. The descriptions in the reference section indicate
  1092.  whether routines are implemented as functions or as macros.
  1093.  
  1094.  Most routines in the Microsoft C library are functions. They consist of
  1095.  compiled C code or assembled Microsoft Macro Assembler (MASM) code. However,
  1096.  a few library routines are implemented as macros that behave like functions.
  1097.  You can pass arguments to library macros and invoke them in the same way you
  1098.  invoke functions.
  1099.  
  1100.  The main benefit of using macros is faster execution time. A macro is
  1101.  expanded (replaced by its definition) during preprocessing, creating in-line
  1102.  code. Thus, macros do not have the overhead associated with function calls.
  1103.  On the other hand, each use of a macro inserts the same code in your
  1104.  program, whereas a function definition occurs only once regardless of how
  1105.  many times it is called. Functions and macros thus offer a trade-off between
  1106.  speed and size.
  1107.  
  1108.  Apart from speed and size issues, macros and functions have some other
  1109.  important differences:
  1110.  
  1111.  
  1112.    ■   Some macros treat arguments with side effects incorrectly when the
  1113.        macro evaluates its arguments more than once (see the example that
  1114.        follows this list). Not every macro has this effect. To determine if a
  1115.        macro handles side effects as desired, examine its definition in the
  1116.        appropriate header file.
  1117.  
  1118.    ■   A function name evaluates to an address, but a macro name does not.
  1119.        Thus, you cannot use a macro name in contexts requiring a function
  1120.        pointer. For instance, you can declare a pointer to a function, but
  1121.        you cannot declare a pointer to a macro.
  1122.  
  1123.    ■   You can declare functions, but you cannot declare macros. Thus, the
  1124.        compiler cannot perform type checking of macro arguments as it does of
  1125.        function arguments. However, the compiler can detect when you pass the
  1126.        wrong number of arguments to a macro.
  1127.  
  1128.    ■   You must always include the appropriate header file when using a
  1129.        library macro. Every library macro is defined with a #define directive
  1130.        in a header file. If you do not include the header file, the macro is
  1131.        undefined.
  1132.  
  1133.  
  1134.  The following example demonstrates how some macros can produce unwanted side
  1135.  effects. It uses the toupper routine from the standard C library.
  1136.  
  1137.    #include <ctype.h>
  1138.  
  1139.    int a = 'm';
  1140.    a = toupper(a++);
  1141.  
  1142.  The example increments  a  when passing it as an argument to the toupper
  1143.  routine, which is implemented as a macro. It is defined in CTYPE.H:
  1144.  
  1145.    #define toupper(c)  ( (islower(c)) ? _toupper(c) : (c) )
  1146.  
  1147.  The definition uses the conditional operator (? :). The conditional
  1148.  expression evaluates the argument  c  twice: once to check if it is
  1149.  lowercase and again to create the result. This macro evaluates the argument
  1150.  a++  twice, increasing  a  by 2 instead of 1. As a result, the value
  1151.  operated on by islower differs from the value operated on by _toupper.
  1152.  
  1153.  Like some other library routines, toupper is provided in both macro and
  1154.  function versions. The header file CTYPE.H not only declares the toupper
  1155.  function but also defines the toupper macro.
  1156.  
  1157.  Choosing between the macro version and function version of such routines is
  1158.  easy. If you wish to use the macro version, you can simply include the
  1159.  header file that contains the macro definition. Because the macro definition
  1160.  of the routine always appears after the function declaration, the macro
  1161.  definition normally takes precedence. Thus, if your program includes CTYPE.H
  1162.  and then calls toupper, the compiler uses the toupper macro:
  1163.  
  1164.    #include <ctype.h>
  1165.  
  1166.    int a = 'm';
  1167.    a = toupper(a);
  1168.  
  1169.  You can force the compiler to use the function version of a routine by
  1170.  enclosing the routine's name in parentheses:
  1171.  
  1172.    #include <ctype.h>
  1173.  
  1174.    int a = 'm';
  1175.    a = (toupper) (a);
  1176.  
  1177.  Because the name toupper is not immediately followed by a left parenthesis,
  1178.  the compiler cannot interpret it as a macro name. It must use the toupper
  1179.  function.
  1180.  
  1181.  A second way to do this is to "undefine" the macro definition with the
  1182.  #undef directive:
  1183.  
  1184.    #include <ctype.h>
  1185.    #undef toupper
  1186.  
  1187.  Since the macro definition no longer exists, subsequent references to
  1188.  toupper use the function version.
  1189.  
  1190.  A third way to make sure the compiler uses the function version is to
  1191.  declare the function explicitly:
  1192.  
  1193.    #include <ctype.h>
  1194.    int toupper(int _c);
  1195.  
  1196.  Since this function declaration appears after the macro definition in
  1197.  CTYPE.H, it causes the compiler to use the toupper function.
  1198.  
  1199.  
  1200.  1.5  Stack Checking on Entry
  1201.  
  1202.  For certain library routines, the compiler performs stack checking on entry.
  1203.  (The "stack" is a memory area used for temporary storage.) Upon entry to
  1204.  such a routine, the stack is checked to determine if it has enough room for
  1205.  the local variables used by that routine. If it does, space is allocated by
  1206.  adjusting the stack pointer. Otherwise, a "stack overflow" run-time error
  1207.  occurs. If stack checking is disabled, the compiler assumes there is enough
  1208.  stack space; if there is not, you might overwrite memory locations in the
  1209.  data segment and receive no warning.
  1210.  
  1211.  Typically, stack checking is enabled only for functions with large
  1212.  local-variable requirements (more than about 150 bytes), since there is
  1213.  enough free space between the stack and data segments to handle functions
  1214.  with smaller requirements. If the function is called many times, stack
  1215.  checking slows execution slightly.
  1216.  
  1217.  Stack checking is enabled for the following library functions:
  1218.  
  1219.  execvp          printf          spawnvpe        system
  1220.  execvpe         scanf           sprintf         vprintf
  1221.  fprintf         spawnvp         sscanf          write
  1222.  fscanf
  1223.  
  1224.  1.6  Handling Errors
  1225.  
  1226.  Many library routines return a value that indicates an error condition. To
  1227.  avoid unexpected results, your code should always check such error values
  1228.  and handle all of the possible error conditions. The description of each
  1229.  library routine in the reference section lists the routine's return
  1230.  value(s).
  1231.  
  1232.  Some library functions do not have a set error return. These include
  1233.  functions that return nothing and functions whose range of return values
  1234.  makes it impossible to return a unique error value. To aid in error
  1235.  handling, some functions in this category set the value of a global variable
  1236.  named errno.
  1237.  
  1238.  If the reference description of a routine states that it sets the errno
  1239.  variable, you can use errno in two ways:
  1240.  
  1241.  
  1242.    1.  Compare errno to the values defined in the header file ERRNO.H.
  1243.  
  1244.    2.  Handle errno with the perror or strerror library routines. The perror
  1245.        routine prints a system error message to the standard error (stderr).
  1246.        The strerror routine stores the same information in a string for later
  1247.        use.
  1248.  
  1249.  
  1250.  When you use errno, perror, and strerror, remember that the value of errno
  1251.  reflects the error value for the last call that set errno. To avoid
  1252.  confusion, you should always test the return value to verify that an error
  1253.  actually occurred. Once you determine that an error has occurred, use errno
  1254.  or perror immediately. Otherwise, the value of errno may be changed by
  1255.  intervening calls.
  1256.  
  1257.  Library math routines set errno by calling the matherr or _matherrl library
  1258.  routines, which are described in the reference section. If you wish to
  1259.  handle math errors differently from these routines, you can write your own
  1260.  routine and name it matherr or _matherrl. Your routine must follow the rules
  1261.  listed in the matherr reference description.
  1262.  
  1263.  The ferror library routine allows you to check for errors in stream
  1264.  input/output operations. This routine checks if an error indicator has been
  1265.  set for a given stream. Closing or rewinding the stream automatically clears
  1266.  the error indicator. You can also reset the error indicator by calling the
  1267.  clearerr library routine.
  1268.  
  1269.  The feof library routine tests for end-of-file on a given stream. An
  1270.  end-of-file condition in low-level input and output can be detected with the
  1271.  eof routine or when a read operation returns 0 as the number of bytes read.
  1272.  
  1273.  
  1274.  The _grstatus library routine allows you to check for errors after calling
  1275.  certain graphics library operations. See the reference page on the _grstatus
  1276.  function for details.
  1277.  
  1278.  
  1279.  1.7  Operating-System Considerations
  1280.  
  1281.  The library routines listed in this section behave differently under
  1282.  different operating system versions. For more information on an individual
  1283.  routine, see the description of that routine in the reference section.
  1284.  
  1285.  Routine                           Restrictions
  1286.  ────────────────────────────────────────────────────────────────────────────
  1287.  locking                           These routines are effective only in
  1288.  sopen                             OS/2 and in DOS versions 3.0 and later.
  1289.  _fsopen
  1290.  
  1291.  dosexterr                         The dosexterr routine provides error
  1292.                                    handling for system call 0x59 (get
  1293.                                    extended error) in DOS versions 3.0 and
  1294.                                    later.
  1295.  
  1296.  dup                               The dup and dup2 routines can cause
  1297.  dup2                              unexpected results in DOS versions
  1298.                                    earlier than 3.0. If you use dup or dup2
  1299.                                    to create a duplicate file handle for
  1300.                                    stdin, stdout, stderr, stdaux, or stdprn
  1301.                                    , calling the close function with one
  1302.                                    handle causes errors in later I/O
  1303.                                    operations that use the other handle.
  1304.                                    This anomaly does not occur in OS/2 or
  1305.                                    in DOS versions 3.0 and later.
  1306.  
  1307.  exec                              When using the exec and spawn families
  1308.  spawn                             of functions under DOS versions earlier
  1309.                                    than 3.0, the value of the arg0 argument
  1310.                                    (or argv[0] to the child process) is not
  1311.                                    available to the user; a null string (""
  1312.                                    ) is stored in that position instead. In
  1313.                                    OS/2, the arg0 argument contains the
  1314.                                    command name; in DOS versions 3.0 and
  1315.                                    later, it contains the complete command
  1316.                                    path.
  1317.  
  1318.  Microsoft C defines global variables that indicate the version of the
  1319.  current operating system. You can use these to determine the
  1320.  operating-system version in which a program is executing. See Chapter 3,
  1321.  "Global Variables and Standard Types," for more information.
  1322.  
  1323.  
  1324.  1.8  Floating-Point Support
  1325.  
  1326.  Microsoft math library routines require floating-point support to perform
  1327.  calculations with real numbers (numbers that can contain fractions). This
  1328.  support can be provided by the floating-point libraries that accompany your
  1329.  compiler software or by an 8087, 80287, or 80387 coprocessor. The names of
  1330.  the functions that require floating-point support are listed below:
  1331.  
  1332.  acos            cos             fmod            modfl
  1333.  acosl           cosl            fmodl           pow
  1334.  asin            cosh            fmsbintoieee    powl
  1335.  asinl           coshl           _fpreset        sin
  1336.  atan            dieeetomsbin    frexp           sinl
  1337.  atanl           difftime        frexpl          sinh
  1338.  atan2           dmsbintoieee    gcvt            sinhl
  1339.  atan2l          ecvt            hypot           sqrt
  1340.  atof            exp             hypotl          sqrtl
  1341.  _atold          expl            ldexp           _status87
  1342.  bessel          fabs            ldexpl          strtod
  1343.  cabs            fabsl           log             _strtold
  1344.  cabsl           fcvt            logl            tan
  1345.  ceil            fieeetomsbin    log10           tanl
  1346.  ceill           floor           log10l          tanh
  1347.  _clear87        floorl          modf            tanhl
  1348.  _control87
  1349.  
  1350.  Note that the bessel routine does not correspond to a single function, but
  1351.  to twelve functions named j0, j1, jn, y0, y1, yn, _j0l, _j1l, _jnl, _y0l,
  1352.  _y1l, and _ynl. Also note that the _clear87 and _control87 functions are not
  1353.  available with the /FPa compiler option.
  1354.  
  1355.  Also requiring floating-point support is the printf family of functions
  1356.  (cprintf, fprintf, printf, sprintf, vfprintf, vprintf, and vsprintf). These
  1357.  functions require support for floating-point input and output if used to
  1358.  print floating-point values.
  1359.  
  1360.  The C compiler tries to detect whether floating-point values are used in a
  1361.  program so that supporting functions are loaded only if required. This
  1362.  behavior saves a considerable amount of space for programs that do not
  1363.  require floating-point support.
  1364.  
  1365.  When you use a floating-point type specifier in the format string for a
  1366.  printf or scanf call, make sure you specify floating-point values or
  1367.  pointers to floating-point values in the argument list. These must
  1368.  correspond to any floating-point
  1369.  
  1370.  type specifiers in the format string. The presence of floating-point
  1371.  arguments allows the compiler to detect that floating-point support code is
  1372.  required. If a floating-point type specifier is used to print an integer
  1373.  argument, for example, floating-point values will not be detected because
  1374.  the compiler does not actually read the format string used in the printf and
  1375.  scanf functions. For instance, the following program produces an error at
  1376.  run time:
  1377.  
  1378.    main( ) /* This example causes an error */
  1379.     {
  1380.      long f = 10L;
  1381.      printf("%f", f);
  1382.     }
  1383.  
  1384.  In the preceding example, the functions for floating-point support are not
  1385.  loaded because
  1386.  
  1387.  
  1388.    ■   No floating-point arguments are given in the call to printf.
  1389.  
  1390.    ■   No floating-point values are used elsewhere in the program.
  1391.  
  1392.  
  1393.  As a result, the following error occurs:
  1394.  
  1395.    Floating point not loaded
  1396.  
  1397.  Here is a corrected version of the above call to printf in which the long
  1398.  integer value is cast to double:
  1399.  
  1400.    main( ) /* This example works correctly */
  1401.     {
  1402.      long f = 10L;
  1403.      printf("%f", (double) f);
  1404.     }
  1405.  
  1406.  
  1407.  1.9  Using Huge Arrays with Library Functions
  1408.  
  1409.  In programs that use small, compact, medium, and large memory models,
  1410.  Microsoft C allows you to use arrays exceeding the 64K (kilobyte) limit of
  1411.  physical memory in these models by explicitly declaring the arrays as _huge.
  1412.  However, generally, you cannot pass _huge data items as arguments to C
  1413.  library functions. In the compact-model library used by compact-model
  1414.  programs and in the large-model library used by both large-model and
  1415.  huge-model programs, only the functions listed below use argument arithmetic
  1416.  that works with _huge items:
  1417.  
  1418.  bsearch         _fmemcmp        _fmemset        lsearch
  1419.  fread           _fmemcpy        halloc          memccpy
  1420.  fwrite          _fmemicmp       hfree           memchr
  1421.  _fmemccpy       _fmemmove       lfind
  1422.  _fmemchr
  1423.  
  1424.  With this set of functions, you can read from, write to, search, sort, copy,
  1425.  initialize, compare, or dynamically allocate and free _huge arrays; the
  1426.  _huge array can be passed without difficulty to any of these functions in a
  1427.  compact-, large-, or huge-model program. The model-independent routines in
  1428.  the above list (those beginning with _f) are available in all memory models.
  1429.  
  1430.  
  1431.  The memset, memcpy, and memcmplibrary routines are available in two
  1432.  versions: as C functions and as intrinsic (in-line) code. The function
  1433.  versions of these routines support huge pointers in compact and large memory
  1434.  models, but the intrinsic versions do not support huge pointers. (The
  1435.  function version of such routines generates a call to a library function,
  1436.  whereas the intrinsic version inserts in-line code into your program. Your
  1437.  compiler documentation explains how to select the intrinsic versions of
  1438.  library routines.)
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  Chapter 2  Run-Time Routines by Category
  1444.  ────────────────────────────────────────────────────────────────────────────
  1445.  
  1446.  Microsoft C library routines handle various kinds of tasks. If you know the
  1447.  type of task you need done, but don't know exactly which routine to use, the
  1448.  categorized lists of routines in this chapter can help.
  1449.  
  1450.  The descriptions here are intended only to give you a brief overview of the
  1451.  capabilities of the run-time library. For a complete description of the
  1452.  behavior, syntax, and use of each routine, see Part 2, "Run-Time Functions."
  1453.  
  1454.  
  1455.  The main categories of library routines are
  1456.  
  1457.  
  1458.    ■   Buffer manipulation
  1459.  
  1460.    ■   Character classification and conversion
  1461.  
  1462.    ■   Data conversion
  1463.  
  1464.    ■   Directory control
  1465.  
  1466.    ■   File handling
  1467.  
  1468.    ■   Graphics
  1469.  
  1470.    ■   Input and output
  1471.  
  1472.    ■   Internationalization
  1473.  
  1474.    ■   Math
  1475.  
  1476.    ■   Memory allocation
  1477.  
  1478.    ■   Process and environment control
  1479.  
  1480.    ■   Searching and sorting
  1481.  
  1482.    ■   String manipulation
  1483.  
  1484.    ■   System calls
  1485.  
  1486.    ■   Time
  1487.  
  1488.    ■   Variable-length argument lists
  1489.  
  1490.  
  1491.  
  1492.  2.1  Buffer Manipulation
  1493.  
  1494.  The buffer-manipulation routines are useful for working with areas of memory
  1495.  on a character-by-character basis. A "buffer" is an array of characters,
  1496.  similar to a character string. However, unlike strings, buffers are not
  1497.  usually terminated with a null character ('\0'). Therefore, the
  1498.  buffer-manipulation routines always take a length or count argument.
  1499.  Function declarations for the buffermanipulation routines are given in the
  1500.  include files MEMORY.H and STRING.H, with an exception being the swab
  1501.  function, which appears in STDLIB.H.
  1502.  
  1503.  Routines beginning with _f  are model independent; the _f  stands for far.
  1504.  These routines are useful in writing mixed-model programs because they can
  1505.  be called from any program, regardless of the memory model being used.
  1506.  
  1507.  Routine                           Use
  1508.  ────────────────────────────────────────────────────────────────────────────
  1509.  memccpy,  _fmemccpy               Copy characters from one buffer to
  1510.                                    another until a given character or a
  1511.                                    given number of characters has been
  1512.                                    copied
  1513.  
  1514.  memchr,  _fmemchr                 Return a pointer to the first
  1515.                                    occurrence, within a specified number of
  1516.                                    characters, of a given character in the
  1517.                                    buffer
  1518.  
  1519.  memcmp,  _fmemcmp                 Compare a specified number of characters
  1520.                                    from two buffers
  1521.  
  1522.  memcpy,  _fmemcpy                 Copy a specified number of characters
  1523.                                    from one buffer to another
  1524.  
  1525.  memicmp, _fmemicmp                Compare a specified number of characters
  1526.                                    from two buffers without regard to the
  1527.                                    case of the letters (uppercase and
  1528.                                    lowercase treated as equivalent)
  1529.  
  1530.  memmove,                          Copy a specified number of characters
  1531.  _fmemmove                         from one buffer to another
  1532.  
  1533.  memset,  _fmemset                 Use a given character to initialize a
  1534.                                    specified number of bytes in the buffer
  1535.  
  1536.  
  1537.  swab                              Swaps bytes of data and stores them at
  1538.                                    the specified location
  1539.  
  1540.  When the source and target areas overlap, only the memmove and _fmemmove
  1541.  functions are guaranteed to copy the full source properly. (The memcpy and
  1542.  _fmemcpy routines do not always copy the full source in such cases.)
  1543.  
  1544.  
  1545.  2.2  Character Classification and Conversion
  1546.  
  1547.  The character classification and conversion routines allow you to test
  1548.  individual characters in a variety of ways and to convert between uppercase
  1549.  and lowercase characters.
  1550.  
  1551.  Routine                           Use
  1552.  ────────────────────────────────────────────────────────────────────────────
  1553.  isalnum                           Tests for alphanumeric character
  1554.  
  1555.  isalpha                           Tests for alphabetic character
  1556.  
  1557.  isascii                           Tests for ASCII character
  1558.  
  1559.  iscntrl                           Tests for control character
  1560.  
  1561.  isdigit                           Tests for decimal digit
  1562.  
  1563.  isgraph                           Tests for printable character except
  1564.                                    space
  1565.  
  1566.  islower                           Tests for lowercase character
  1567.  
  1568.  isprint                           Tests for printable character
  1569.  
  1570.  ispunct                           Tests for punctuation character
  1571.  
  1572.  isspace                           Tests for white-space character
  1573.  
  1574.  isupper                           Tests for uppercase character
  1575.  
  1576.  isxdigit                          Tests for hexadecimal digit
  1577.  
  1578.  toascii                           Converts character to ASCII code
  1579.  
  1580.  tolower                           Tests character and converts to
  1581.                                    lowercase if uppercase
  1582.  
  1583.  _tolower                          Converts character to lowercase
  1584.                                    (unconditional)
  1585.  
  1586.  toupper                           Tests character and converts to
  1587.                                    uppercase if
  1588.                                    lowercase
  1589.  
  1590.  _toupper                          Converts character to uppercase
  1591.                                    (unconditional)
  1592.  
  1593.  The classification routines identify characters by finding them in a table
  1594.  of classification codes. Using these routines to classify characters is
  1595.  generally faster than writing a test expression such as the following:
  1596.  
  1597.    if ((c >= 0) || c <= 0x7f))
  1598.  
  1599.  All of these routines are implemented in two versions: as functions and as
  1600.  macros. The function prototypes and macro definitions appear in CTYPE.H.
  1601.  Section 1.4, "Choosing Between Functions and Macros," explains how to choose
  1602.  the appropriate version. The toupper and tolower functions are also declared
  1603.  in the STDLIB.H header file.
  1604.  
  1605.  
  1606.  2.3  Data Conversion
  1607.  
  1608.  The data-conversion routines convert numbers to strings of ASCII characters
  1609.  and vice versa. These routines are implemented as functions, all of which
  1610.  are declared in the include file STDLIB.H. The atof function, which converts
  1611.  a string to a floating-point value, is also declared in MATH.H.
  1612.  
  1613.  Routine                           Use
  1614.  ────────────────────────────────────────────────────────────────────────────
  1615.  abs                               Finds absolute value of integer
  1616.  
  1617.  atof                              Converts string to float
  1618.  
  1619.  atoi                              Converts string to int
  1620.  
  1621.  atol                              Converts string to long
  1622.  
  1623.  _atold                            Converts string to long double
  1624.  
  1625.  ecvt                              Converts double to string
  1626.  
  1627.  fcvt                              Converts double to string
  1628.  
  1629.  gcvt                              Converts double to string
  1630.  
  1631.  itoa                              Converts int to string
  1632.  
  1633.  labs                              Finds absolute value of long integer
  1634.  
  1635.  ltoa                              Converts long to string
  1636.  
  1637.  strtod                            Converts string to double
  1638.  
  1639.  strtol                            Converts string to a long integer
  1640.  
  1641.  _strtold                          Converts string to long double
  1642.  
  1643.  strtoul                           Converts string to an unsigned long
  1644.                                    integer
  1645.  
  1646.  ultoa                             Converts unsigned long to string
  1647.  
  1648.  
  1649.  2.4  Directory Control
  1650.  
  1651.  The directory-control routines let a program access, modify, and obtain
  1652.  information about the directory structure. These routines are functions and
  1653.  are declared in DIRECT.H.
  1654.  
  1655.  Routine                           Use
  1656.  ────────────────────────────────────────────────────────────────────────────
  1657.  chdir                             Changes current working directory
  1658.  
  1659.  _chdrive                          Changes current drive
  1660.  
  1661.  getcwd                            Gets current working directory
  1662.  
  1663.  _getdcwd                          Gets current working directory for the
  1664.                                    specified drive
  1665.  
  1666.  _getdrive                         Gets the current disk drive
  1667.  
  1668.  mkdir                             Makes a new directory
  1669.  
  1670.  rmdir                             Removes a directory
  1671.  
  1672.  _searchenv                        Searches for a given file on specified
  1673.                                    paths
  1674.  
  1675.  
  1676.  2.5  File Handling
  1677.  
  1678.  The file-handling routines let you create, manipulate, and delete files.
  1679.  They also set and check file-access permissions.
  1680.  
  1681.  File-handling routines work on a file designated by a path name or by a
  1682.  "file handle," an integer assigned by the operating system that identifies
  1683.  an open file. These routines modify or give information about the designated
  1684.  file. Most of them are declared in the include file IO.H, with the
  1685.  exceptions being the fstat and stat functions (declared in SYS\STAT.H), the
  1686.  _fullpath routine (declared in DIRECT.H), and the remove and rename
  1687.  functions (also declared in STDIO.H).
  1688.  
  1689.  Routine                           Use
  1690.  ────────────────────────────────────────────────────────────────────────────
  1691.  access                            Checks file-permission setting
  1692.  
  1693.  chmod                             Changes file-permission setting
  1694.  
  1695.  chsize                            Changes file size
  1696.  
  1697.  filelength                        Gets file length
  1698.  
  1699.  fstat                             Gets file-status information on handle
  1700.  
  1701.  _fullpath                         Makes an absolute path name from a
  1702.                                    relative path name
  1703.  
  1704.  isatty                            Checks for character device
  1705.  
  1706.  locking                           Locks areas of file (available with OS/2
  1707.                                    and
  1708.                                    DOS versions 3.0 and later)
  1709.  
  1710.  _makepath                         Merges path-name components into a
  1711.                                    single, full path name
  1712.  
  1713.  mktemp                            Creates unique file name
  1714.  
  1715.  remove                            Deletes file
  1716.  
  1717.  rename                            Renames file
  1718.  
  1719.  setmode                           Sets file-translation mode
  1720.  
  1721.  _splitpath                        Splits a path name into component pieces
  1722.  
  1723.  stat                              Gets file-status information on named
  1724.                                    file
  1725.  
  1726.  umask                             Sets default-permission mask
  1727.  
  1728.  unlink                            Deletes file
  1729.  
  1730.  The access, chmod, _fullpath, _makepath, remove, rename, _splitpath, stat,
  1731.  and unlink routines operate on files specified by a path name or file name.
  1732.  
  1733.  
  1734.  The chsize, filelength, fstat, isatty, locking, and setmode routines work
  1735.  with files designated by a file handle.
  1736.  
  1737.  The mktemp and umask routines have functions that are slightly different
  1738.  from the other routines. The mktemp routine creates a unique file name, and
  1739.  the programmer can use mktemp to create unique file names that do not
  1740.  conflict with the names of existing files. The umask routine sets the
  1741.  default permission mask for any new files created in a program. The mask can
  1742.  override the permission setting given in the open or creat call for the new
  1743.  file.
  1744.  
  1745.  
  1746.  2.6  Graphics
  1747.  
  1748.  Microsoft C graphics routines offer a wide variety of graphics functions,
  1749.  low-level graphics primitives, font functions, and presentation graphics
  1750.  (displays such as graphs and pie charts).
  1751.  
  1752.  Graphics functions are supplied in two libraries that must be explicitly
  1753.  linked with your program. The GRAPHICS.LIB library provides support for
  1754.  low-level graphics and character-font routines. The library PGCHART.LIB
  1755.  supports presentation-graphics routines.
  1756.  
  1757.  
  1758.  2.6.1  Low-Level Graphics and Character-Font Functions
  1759.  
  1760.  The low-level graphics and font functions are declared in the include file
  1761.  GRAPH.H.
  1762.  
  1763.  The library can be divided into the eight categories listed below, which
  1764.  correspond to the different tasks involved in creating and manipulating
  1765.  graphic objects.
  1766.  
  1767.  Most graphics routines work only in DOS. Two categories of routines
  1768.  ("configuring mode and environment" and "creating text output") work in OS/2
  1769.  as well as DOS.
  1770.  
  1771.  Category                          Task
  1772.  ────────────────────────────────────────────────────────────────────────────
  1773.  Configuring mode and environment  Select the proper display mode for the
  1774.  (OS/2 and DOS)                    hardware and establish memory areas for
  1775.                                    writing and displaying of images
  1776.  
  1777.  Setting coordinates               Specify the logical origin and the
  1778.                                    active display area within the screen
  1779.  
  1780.  Setting low-level graphics        Specify a palette mapping for low-level
  1781.  palettes                          graphics routines
  1782.  
  1783.  Setting attributes                Specify background and foreground
  1784.                                    colors, fill masks, and line styles for
  1785.                                    low-level graphics routines
  1786.  
  1787.  Creating graphics                 Draw and fill figures
  1788.  output
  1789.  
  1790.  Creating text output (OS/2 and    Write text on the screen
  1791.  DOS)
  1792.  
  1793.  Transferring images               Store images in memory and retrieve them
  1794.  
  1795.  Displaying fonts                  Display text in character fonts
  1796.                                    compatible with Microsoft Windows(tm)
  1797.  
  1798.  The following sections explain each of these categories.
  1799.  
  1800.  
  1801.  2.6.1.1  Configuring Mode and Environment
  1802.  
  1803.  Routines that configure the mode and environment establish the graphics or
  1804.  text mode of operation, determine the current graphics environment, and
  1805.  control the display of the cursor.
  1806.  
  1807.  All of the routines listed in this section are available in OS/2 as well as
  1808.  DOS.
  1809.  
  1810.  Routine                           Use
  1811.  ────────────────────────────────────────────────────────────────────────────
  1812.  _clearscreen                      Erases the screen and fills it with the
  1813.                                    current background color
  1814.  
  1815.  _getactivepage                    Gets the current active page number
  1816.  
  1817.  _getbkcolor                       Returns the current background color
  1818.  
  1819.  _getvideoconfig                   Obtains status of current graphics
  1820.                                    environment
  1821.  
  1822.  _getvisualpage                    Gets the current visual page number
  1823.  
  1824.  _grstatus                         Returns the status of the most recent
  1825.                                    graphics function call
  1826.  
  1827.  _setactivepage                    Sets memory area for the active page for
  1828.                                    writing
  1829.                                    images
  1830.  
  1831.  _setbkcolor                       Sets the current background color
  1832.  
  1833.  _settextrows                      Sets the number of text rows
  1834.  
  1835.  _setvideomode                     Selects an operating mode for the
  1836.                                    display screen
  1837.  
  1838.  _setvideomoderows                 Sets the video mode and the number of
  1839.                                    rows for text operations
  1840.  
  1841.  _setvisualpage                    Sets memory area for the current visual
  1842.                                    page
  1843.  
  1844.  
  1845.  2.6.1.2  Setting Coordinates
  1846.  
  1847.  The "set coordinates" routines set the current text or graphics position and
  1848.  convert pixel coordinates between the various graphic coordinate systems.
  1849.  
  1850.  The Microsoft C graphics functions recognize three sets of coordinates:
  1851.  
  1852.  
  1853.    1.  Fixed physical coordinates
  1854.  
  1855.    2.  View coordinates defined by the application
  1856.  
  1857.    3.  Window coordinates that can include floating-point values
  1858.  
  1859.  
  1860.  The functions in this category establish window and view coordinate systems
  1861.  and translate between physical, view, and window coordinate systems.
  1862.  
  1863.  Routine                           Use
  1864.  ────────────────────────────────────────────────────────────────────────────
  1865.  _getcurrentposition               Determines current position in view
  1866.                                    coordinates
  1867.  
  1868.  _getcurrentposition_w             Determines current position in window
  1869.                                    coordinates
  1870.  
  1871.  _getphyscoord                     Converts view coordinates to physical
  1872.                                    coordinates
  1873.  
  1874.  _getviewcoord                     Converts physical coordinates to view
  1875.                                    coordinates
  1876.  
  1877.  _getviewcoord_w                   Converts window coordinates to view
  1878.                                    coordinates
  1879.  
  1880.  _getviewcoord_wxy                 Converts window coordinates in _wxycoord
  1881.                                    structure to view coordinates
  1882.  
  1883.  _getwindowcoord                   Converts view coordinates to window
  1884.                                    coordinates
  1885.  
  1886.  _setcliprgn                       Limits graphic output to a region of the
  1887.                                    screen
  1888.  
  1889.  _setvieworg                       Positions the view-coordinate origin
  1890.  
  1891.  _setviewport                      Limits graphics output to a region of
  1892.                                    the screen and positions the
  1893.                                    view-coordinate origin to the upper-left
  1894.                                    corner of that region
  1895.  
  1896.  _setwindow                        Defines a floating-point window
  1897.                                    coordinate system
  1898.  
  1899.  The default view coordinate system is identical to the physical screen
  1900.  coordinate system. The physical origin (0, 0) is always in the upper-left
  1901.  corner of the display. The x axis extends in the positive direction left to
  1902.  right, while the y axis extends in the positive direction top to bottom.
  1903.  
  1904.  The physical horizontal and vertical dimensions depend on the hardware
  1905.  display configuration and the selected mode. These values are accessible at
  1906.  run time by examining the numxpixels and numypixels fields of the
  1907.  videoconfig structure returned by _getvideoconfig. (The _getvideoconfig
  1908.  routine is listed in the previous section.)
  1909.  
  1910.  The _setvieworg function allows you to move the viewport origin to a new
  1911.  position relative to the physical screen.
  1912.  
  1913.  Routines that refer to coordinates on the physical screen or viewport
  1914.  require integer values. However, in real-world graphing applications, you
  1915.  might wish to use floating-point values, such as stock prices or average
  1916.  rainfall. The window coordinate system allows you to display graphics using
  1917.  floating-point values instead of integers.
  1918.  
  1919.  The _getcurrentposition and _getcurrentposition_w routines allow you to
  1920.  determine the location of the current graphics-output point.
  1921.  
  1922.  The _setcliprgn function defines a restricted active display area on the
  1923.  screen. The _setviewport function does the same thing and also resets the
  1924.  viewport origin to the upper-left corner of the restricted active display
  1925.  area.
  1926.  
  1927.  The physical coordinates of any view-coordinate point can be determined with
  1928.  the _getphyscoord function, and the view coordinates of any physical point
  1929.  can be determined with the _getviewcoord function.
  1930.  
  1931.  The view coordinates of any window coordinate can be determined with the
  1932.  _getviewcoord_w and _getviewcoord_wxy functions. The window coordinates of
  1933.  any view coordinate can be determined with the _getwindowcoord function.
  1934.  
  1935.  The _setwindow function defines the current viewport as a real-coordinate
  1936.  window bound by the specified floating-point values.
  1937.  
  1938.  
  1939.  2.6.1.3  Setting Low-Level Graphics Palettes
  1940.  
  1941.  Use the low-level palette routines to select or remap color palettes.
  1942.  
  1943.  Routine                           Use
  1944.  ────────────────────────────────────────────────────────────────────────────
  1945.  _remapallpalette                  Changes all color indexes in the current
  1946.                                    palette
  1947.  
  1948.  _remappalette                     Changes a single color index in the
  1949.                                    current palette
  1950.  
  1951.  _selectpalette                    Selects a predefined palette
  1952.  
  1953.  Some video modes support a "color palette," which is a table of the color
  1954.  values that can be displayed together on the screen at any given time. A
  1955.  "color value" is a long integer representing a color that can be displayed
  1956.  on your system.
  1957.  
  1958.  In CGA color graphics modes, you can use the _selectpalette routine to
  1959.  choose one of several predefined palettes.
  1960.  
  1961.  On EGA and VGA video systems, you can "remap" (change) the palette using the
  1962.  _remappalette or _remapallpalette routines. For instance, the EGA _ERESCOLOR
  1963.  mode offers a total of 64 color values, of which 16 can be displayed at a
  1964.  time. In this mode, the palette contains 16 "color indices," or slots to
  1965.  which you can assign color values.
  1966.  
  1967.  The _remappalette routine changes a single color index to a specified color
  1968.  value. The _remapallpalette routine changes all of the available palette
  1969.  entries simultaneously.
  1970.  
  1971.  
  1972.  2.6.1.4  Setting Attributes
  1973.  
  1974.  The low-level output functions that draw lines, arcs, ellipses, and other
  1975.  basic figures do not specify color or line-style information. Instead, the
  1976.  low-level graphics functions rely on a set of attributes that are set
  1977.  independently by the following functions:
  1978.  
  1979.  Routine                           Use
  1980.  ────────────────────────────────────────────────────────────────────────────
  1981.  _getarcinfo                       Determines the endpoints in viewport
  1982.                                    coordinates of the most recently drawn
  1983.                                    arc or pie
  1984.  
  1985.  _getcolor                         Gets the current color
  1986.  
  1987.  _getfillmask                      Gets the current fill mask
  1988.  
  1989.  _getlinestyle                     Gets the current line-style mask
  1990.  
  1991.  _getwritemode                     Gets the current logical write mode
  1992.  
  1993.  _setcolor                         Sets the current color
  1994.  
  1995.  _setfillmask                      Sets the current fill mask
  1996.  
  1997.  _setlinestyle                     Sets the current line-style mask
  1998.  
  1999.  _setwritemode                     Sets logical write mode for line drawing
  2000.  
  2001.  The _getcolor and _setcolor functions get or set the current color index for
  2002.  graphics and font output. The _getbkcolor and _setbkcolor functions get or
  2003.  set the current background color.
  2004.  
  2005.  The _getfillmask and _setfillmask functions get or set the current fill
  2006.  mask. The mask is an 8-by-8-bit template array, with each bit representing a
  2007.  pixel. If a bit is 0, the pixel in memory is left untouched, as the mask is
  2008.  transparent to that pixel. If a bit is 1, the pixel is assigned the current
  2009.  color value. The template is repeated as necessary over the entire fill
  2010.  area.
  2011.  
  2012.  The _getlinestyle and _setlinestyle functions get or set the current line
  2013.  style. The line style is determined by a 16-bit template buffer with each
  2014.  bit corresponding to a pixel. If a bit is 1, the pixel is set to the current
  2015.  color. If a bit is 0, the pixel is not changed. The template is repeated for
  2016.  the length of the line.
  2017.  
  2018.  The _getwritemode and _setwritemode functions get or set the logical write
  2019.  mode for straight line drawing. The default mode, _GPSET, causes lines to be
  2020.  drawn in the current graphics color. Other modes combine the current
  2021.  graphics color and the original screen image using various logical
  2022.  operations.
  2023.  
  2024.  
  2025.  2.6.1.5  Creating Graphics Output
  2026.  
  2027.  The graphics output functions use a set of specified coordinates and draw
  2028.  various figures. They use the current or default attributes for line-style
  2029.  mask, fill mask, write mode, background color, and foreground color.
  2030.  
  2031.  The name of each function announces its task or the figure it draws, as the
  2032.  following list indicates:
  2033.  
  2034.  Routine                           Use
  2035.  ────────────────────────────────────────────────────────────────────────────
  2036.  _arc,  _arc_w,  _arc_wxy          Draw an arc
  2037.  
  2038.  _ellipse,  _ellipse_w,            Draw an ellipse or circle
  2039.  _ellipse_wxy
  2040.  
  2041.  _floodfill,  _floodfill_w         Flood-fill an area of the screen with
  2042.                                    the current color
  2043.  
  2044.  _getcurrentposition,              Obtain the current graphic-output
  2045.  _getcurrentposition_w             position used by _lineto and _outgtext
  2046.  
  2047.  _getpixel, _getpixel_w            Obtain a pixel's color
  2048.  
  2049.  _lineto,  _lineto_w               Draw a line from the current graphic
  2050.                                    output position to a specified point
  2051.  
  2052.  _moveto,  _moveto_w               Move the current graphic-output position
  2053.                                    to a specified point
  2054.  
  2055.  _pie,  _pie_w,  _pie_wxy          Draw a pie-slice-shaped figure
  2056.  
  2057.  _polygon,  _polygon_w,            Draw or scan-fill a polygon
  2058.  _polygon_wxy
  2059.  
  2060.  _rectangle,  _rectangle_w,        Draw or scan-fill a rectangle
  2061.  _rectangle_wxy
  2062.  
  2063.  _setpixel,  _setpixel_w           Set a pixel's color
  2064.  
  2065.  Most of these routines are available in several forms, which are indicated
  2066.  by their names. Output functions without a suffix use the view coordinate
  2067.  system. Functions that end with _w take double values as arguments and use
  2068.  the window coordinate system. Functions that end with _wxy use _wxycoord
  2069.  structures to define the coordinates and use the window coordinate system.
  2070.  
  2071.  Circular figures, such as arcs and ellipses, are centered within a "bounding
  2072.  rectangle" specified by two points that define the diagonally opposed
  2073.  corners of the rectangle. The center of the rectangle becomes the center of
  2074.  the figure, and the rectangle's borders determine the size of the figure.
  2075.  
  2076.  
  2077.  2.6.1.6  Creating Text Output
  2078.  
  2079.  The next group of routines provides text output in both graphics and text
  2080.  modes. Unlike the standard console I/O library routines, these functions
  2081.  recognize text-window boundaries and use the current text color.
  2082.  
  2083.  All of the routines listed in this section work in OS/2 as well as DOS.
  2084.  
  2085.  Routine                           Use
  2086.  ────────────────────────────────────────────────────────────────────────────
  2087.  _displaycursor                    Sets the cursor on or off upon exit from
  2088.                                    a graphics routine
  2089.  
  2090.  _gettextcolor                     Obtains the current text color
  2091.  
  2092.  _gettextcursor                    Returns the current cursor attribute
  2093.                                    (text modes only)
  2094.  
  2095.  _gettextposition                  Obtains the current text-output position
  2096.  
  2097.  _gettextwindow                    Gets the current text window boundaries
  2098.  
  2099.  _outmem                           Prints text of a specified length from a
  2100.                                    memory
  2101.                                    buffer
  2102.  
  2103.  _outtext                          Outputs a text string to the screen at
  2104.                                    the current text position
  2105.  
  2106.  _scrolltextwindow                 Scrolls the current text window up or
  2107.                                    down
  2108.  
  2109.  _settextcolor                     Sets the current text color
  2110.  
  2111.  _settextcursor                    Sets the current cursor attribute (text
  2112.                                    modes only)
  2113.  
  2114.  _settextposition                  Relocates the current text position
  2115.  
  2116.  _settextwindow                    Defines the current text-display window
  2117.  
  2118.  _wrapon                           Enables or disables line wrap
  2119.  
  2120.  The _outtext and _outmem routines provide no formatting. If you want to
  2121.  output integer or floating-point values, you must convert the values into a
  2122.  string variable (using the sprintf function) before calling these routines.
  2123.  
  2124.  
  2125.  The _outtext routine recognizes the \n (newline character) and \r (carriage
  2126.  return) sequences. The _outmem routine treats these sequences as printable
  2127.  graphics characters.
  2128.  
  2129.  
  2130.  2.6.1.7  Transferring Images
  2131.  
  2132.  The functions in this category transfer screen images between memory and the
  2133.  display, using a buffer allocated by the application, or determine the size
  2134.  in bytes of the buffer needed to store a given image.
  2135.  
  2136.  The functions that end with _w or _wxy use window coordinates; the other
  2137.  functions in this set use view coordinates.
  2138.  
  2139.  Routine                           Use
  2140.  ────────────────────────────────────────────────────────────────────────────
  2141.  _getimage,                        Store a screen image in memory
  2142.  _getimage_w,
  2143.  _getimage_wxy
  2144.  
  2145.  _imagesize,                       Return the size (in bytes) of the buffer
  2146.  _imagesize_w,                     needed to store the image
  2147.  _imagesize_wxy
  2148.  
  2149.  _putimage,                        Retrieve an image from memory and
  2150.  _putimage_w                       display it
  2151.  
  2152.  In some cases, the buffer needed to store an image with the _getimage
  2153.  functions must be larger than 64K (65,535) bytes. Use the halloc routine to
  2154.  allocate a buffer larger than 64K.
  2155.  
  2156.  
  2157.  2.6.1.8  Displaying Fonts
  2158.  
  2159.  The functions listed in this section control the display of font-based
  2160.  characters on the screen.
  2161.  
  2162.  Routine                           Use
  2163.  ────────────────────────────────────────────────────────────────────────────
  2164.  _getfontinfo                      Obtains the current font characteristics
  2165.  
  2166.  _getgtextextent                   Determines the width in pixels of
  2167.                                    specified text in the current font
  2168.  
  2169.  _getgtextvector                   Gets orientation of font text output
  2170.  
  2171.  _outgtext                         Outputs text in the current font to the
  2172.                                    screen at the specified pixel position
  2173.  
  2174.  _registerfonts                    Initializes font library
  2175.  
  2176.  _setfont                          Finds a single font that matches a
  2177.                                    specified set of characteristics and
  2178.                                    makes this font the current font for use
  2179.                                    by the _outgtext function
  2180.  
  2181.  _setgtextvector                   Sets the current orientation for font
  2182.                                    text output
  2183.  
  2184.  _unregisterfonts                  Frees memory allocated by _registerfonts
  2185.  
  2186.  
  2187.  2.6.2  Presentation-Graphics Functions
  2188.  
  2189.  The presentation-graphics functions are declared in the PGCHART.H include
  2190.  file. The library can be divided into the three categories listed below,
  2191.  corresponding to the different tasks involved in creating and manipulating
  2192.  graphic objects:
  2193.  
  2194.  Category                          Task
  2195.  ────────────────────────────────────────────────────────────────────────────
  2196.  Displaying presentation graphics  Initialize video structures for
  2197.                                    presentation graphics and establishes
  2198.                                    the default chart type. Display
  2199.                                    presentation-graphics chart: bar,
  2200.                                    column, pie, scatter, or line chart.
  2201.  
  2202.  Analyzing                         Analyze data (does not display chart).
  2203.  presentation-graphics data
  2204.  
  2205.  Manipulating                      Modify basic chart structures (e.g.,
  2206.  presentation-graphics structures  palettes, cross-hatching styles).
  2207.  
  2208.  
  2209.  2.6.2.1  Displaying Presentation Graphics
  2210.  
  2211.  The functions listed in this section initialize the presentation-graphics
  2212.  library and display the specified graph type.
  2213.  
  2214.  Because the _pg_initchart routine initializes the presentation-graphics
  2215.  library, it must be called before any other function in the
  2216.  presentation-graphics library. The _pg_defaultchart function initializes the
  2217.  variables in the chart environment.
  2218.  
  2219.  The other routines in this category display the specified graph. The
  2220.  single-series versions plot one set of data, and the multiseries versions
  2221.  (those ending with an ms suffix) plot several sets of data in the same chart
  2222.  style.
  2223.  
  2224.  Presentation-graphics programs can display text in different font sizes by
  2225.  taking advantage of font-based characters (see Section 2.6.1.8, "Displaying
  2226.  Fonts.") Call the _registerfonts and _setfont routines to select a font
  2227.  before calling the _pginitchart routine. Subsequent charts use the selected
  2228.  font. You can later call the _unregisterfonts routine to restore the default
  2229.  character font and free the memory previously allocated for fonts.
  2230.  
  2231.  Routine                           Use
  2232.  ────────────────────────────────────────────────────────────────────────────
  2233.  _pg_chart                         Displays a single-series bar, column, or
  2234.                                    line chart
  2235.  
  2236.  _pg_chartms                       Displays a multiseries bar, column, or
  2237.                                    line chart
  2238.  
  2239.  _pg_chartpie                      Displays a pie chart
  2240.  
  2241.  _pg_chartscatter                  Displays a scatter diagram for a single
  2242.                                    series of data
  2243.  
  2244.  _pg_chartscatterms                Displays a scatter diagram for more than
  2245.                                    one series of data
  2246.  
  2247.  _pg_defaultchart                  Initializes all necessary variables in
  2248.                                    the chart environment for a specified
  2249.                                    chart type
  2250.  
  2251.  _pg_initchart                     Initializes the presentation-graphics
  2252.                                    library
  2253.  
  2254.  
  2255.  2.6.2.2  Analyzing Presentation-Graphics Charts
  2256.  
  2257.  These routines calculate default values for the specified graph type but do
  2258.  not display the chart. The single-series versions analyze one set of data,
  2259.  and the multiseries versions analyze several sets of data in the same chart
  2260.  style.
  2261.  
  2262.  Routine                           Use
  2263.  ────────────────────────────────────────────────────────────────────────────
  2264.  _pg_analyzechart                  Analyzes a single series of data for a
  2265.                                    bar, column, or line chart
  2266.  
  2267.  _pg_analyzechartms                Analyzes a multiseries of data for a
  2268.                                    bar, column, or line chart
  2269.  
  2270.  _pg_analyzepie                    Analyzes data for a pie chart
  2271.  
  2272.  _pg_analyzescatter                Analyzes a single series of data for a
  2273.                                    scatter diagram
  2274.  
  2275.  _pg_analyzescatterms              Analyzes a multiseries of data for a
  2276.                                    scatter diagram
  2277.  
  2278.  
  2279.  2.6.2.3  Manipulating Presentation-Graphics Structures
  2280.  
  2281.  These functions control low-level aspects of the presentation-graphics
  2282.  package.
  2283.  
  2284.  Routine                           Use
  2285.  ────────────────────────────────────────────────────────────────────────────
  2286.  _pg_hlabelchart                   Writes text horizontally on the screen
  2287.  
  2288.  _pg_vlabelchart                   Writes text vertically on the screen
  2289.  
  2290.  _pg_getpalette                    Retrieves current colors, line styles,
  2291.                                    fill patterns, and plot characters for
  2292.                                    all presentation-graphics palettes
  2293.  
  2294.  _pg_setpalette                    Sets current colors, line styles, fill
  2295.                                    patterns, and plot characters for all
  2296.                                    presentation-graphics palettes
  2297.  
  2298.  _pg_resetpalette                  Sets current colors, line styles, fill
  2299.                                    patterns, and plot characters to the
  2300.                                    default values for the current screen
  2301.                                    mode
  2302.  
  2303.  _pg_getstyleset                   Retrieves the contents of the current
  2304.                                    styleset
  2305.  
  2306.  _pg_setstyleset                   Sets the contents of the current
  2307.                                    styleset
  2308.  
  2309.  _pg_resetstyleset                 Resets the contents of the current
  2310.                                    styleset to the default value for the
  2311.                                    current screen mode
  2312.  
  2313.  _pg_getchardef                    Retrieves the current 8-by-8-pixel bit
  2314.                                    map for a specified character
  2315.  
  2316.  _pg_setchardef                    Sets the 8-by-8-pixel bit map for a
  2317.                                    specified
  2318.                                    character
  2319.  
  2320.  
  2321.  2.7  Input and Output
  2322.  
  2323.  The input and output (I/O) routines of the standard C library allow you to
  2324.  read and write data to and from files and devices. In C, there are no
  2325.  predefined file structures; all data items are treated as sequences of
  2326.  bytes. The following three types of I/O functions are available:
  2327.  
  2328.  
  2329.    1.  Stream
  2330.  
  2331.    2.  Low-level
  2332.  
  2333.    3.  Console and port
  2334.  
  2335.  
  2336.  The "stream" I/O functions treat data as a stream of individual characters.
  2337.  By choosing among the many stream functions available, you can process data
  2338.  in different sizes and formats, from single characters to large data
  2339.  structures. Stream I/O also provides buffering, which can significantly
  2340.  improve performance.
  2341.  
  2342.  The "low-level" I/O routines do not perform buffering and formatting.
  2343.  Instead, they invoke the operating system's input and output capabilities
  2344.  directly. These routines let you access files and peripheral devices at a
  2345.  more basic level than the stream functions.
  2346.  
  2347.  The "console and port" I/O routines allow you to read or write directly to a
  2348.  console (keyboard and screen) or an I/O port (such as a printer port). The
  2349.  port I/O routines simply read and write data in bytes. With console I/O
  2350.  routines, some additional options are available, such as detecting whether a
  2351.  character has been typed at the console. You can also choose between echoing
  2352.  characters to the screen as they are read or reading characters without
  2353.  echoing.
  2354.  
  2355.  The C library also provides a number of direct DOS I/O system call routines.
  2356.  These are described in Section 2.14, "System Calls."
  2357.  
  2358.  File I/O operations can be performed in two modes: text or binary. The
  2359.  following section describes these modes and their use.
  2360.  
  2361.  ────────────────────────────────────────────────────────────────────────────
  2362.  WARNING
  2363.  
  2364.  Because stream routines are buffered and low-level routines are not, the two
  2365.  types of routines are generally incompatible. You should use either stream
  2366.  or low-level routines consistently for processing a given file.
  2367.  ────────────────────────────────────────────────────────────────────────────
  2368.  
  2369.  
  2370.  2.7.1  Text and Binary Modes
  2371.  
  2372.  Many C programs use data files for input and output. Under DOS and OS/2,
  2373.  data files are normally processed in text mode. In this mode, each
  2374.  carriage-return-line-feed (CR-LF) combination is translated into a single
  2375.  line-feed character during input. During output, each line-feed character is
  2376.  translated into a CR-LF combination.
  2377.  
  2378.  Sometimes you may want to process a file without making those translations.
  2379.  In these cases you use binary mode, which suppresses CR-LF translations.
  2380.  
  2381.  You can control the file translation mode in the following ways:
  2382.  
  2383.  
  2384.    ■   To process a few selected files in binary mode, while retaining the
  2385.        default text mode for most files, you can specify binary mode when you
  2386.        open the selected files. The fopen routine opens a file in binary mode
  2387.        when you specify the letter b in the access-mode string for the file.
  2388.        The open routine opens a file in binary mode when you specify the
  2389.        O_BINARY flag in the oflag argument. For more information about fopen
  2390.        and open, see the reference description of each routine.
  2391.  
  2392.    ■   To process most or all files in binary mode, you can change the
  2393.        default mode to binary. The global variable _fmode controls the
  2394.        default translation mode, which is normally text. If you set _fmode to
  2395.        O_BINARY, the default mode is binary except for stdaux and stdprn,
  2396.        which are opened in binary mode by default.
  2397.  
  2398.  
  2399.  You can change the value of _fmode in two ways:
  2400.  
  2401.  
  2402.    1.  Link with the file BINMODE.OBJ (supplied with Microsoft C). This
  2403.        changes the initial setting of _fmode to the O_BINARY flag, causing
  2404.        all files except stdin, stdout, and stderr to be opened in binary
  2405.        mode.
  2406.  
  2407.    2.  Change the value of _fmode directly by setting it to the O_BINARY flag
  2408.        in your program. This has the same effect as linking with BINMODE.OBJ.
  2409.  
  2410.  
  2411.  You can still override the default mode (now binary) for a particular file
  2412.  by opening it in text mode. Specify the letter t when using fopen, or
  2413.  specify the O_TEXT flag when using open.
  2414.  
  2415.  By default, the stdin, stdout, and stderr files are opened in text mode, and
  2416.  the stdaux and stdprn files are opened in binary mode. The setmode routine
  2417.  allows you to change these defaults or change the mode of a file after it
  2418.  has been opened. See the reference description of setmode for details.
  2419.  
  2420.  
  2421.  2.7.2  Stream Routines
  2422.  
  2423.  Stream I/O functions handle data as a continuous stream of characters. To
  2424.  use the stream functions, you must include the file STDIO.H in your program.
  2425.  This file defines constants, types, and structures used in the stream
  2426.  functions, and contains function declarations and macro definitions for the
  2427.  stream  routines.
  2428.  
  2429.  When a file is opened for I/O using the stream functions, the opened file is
  2430.  associated with a structure of type FILE (defined in STDIO.H) containing
  2431.  basic information about the file. A pointer to the FILE structure is
  2432.  returned when the stream is opened. Subsequent operations use this pointer
  2433.  (also called the "stream pointer," or just "stream") to refer to the file.
  2434.  
  2435.  The stream functions provide for buffered, formatted, or unformatted input
  2436.  and output. When a stream is buffered, data that is read from or written to
  2437.  the stream is collected in an intermediate storage location called a
  2438.  "buffer". In write operations, the output buffer's contents are written to
  2439.  the appropriate final location when the buffer is full, the stream is
  2440.  closed, or the program terminates normally. The buffer is said to be
  2441.  "flushed" when this occurs. In read operations, a block of data is placed in
  2442.  the input buffer read from the buffer; when the input buffer is empty, the
  2443.  next block of data is transferred into the buffer.
  2444.  
  2445.  Buffering produces efficient I/O because the system can transfer a large
  2446.  block of data in a single operation rather than performing an I/O operation
  2447.  each time a data item is read from or written to a stream. However, if a
  2448.  program terminates abnormally, output buffers may not be flushed, resulting
  2449.  in loss of data.
  2450.  
  2451.  Some of the constants defined in STDIO.H may be useful in your program. The
  2452.  manifest constant EOF is defined to be the value returned at end-of-file.
  2453.  NULL is the null pointer. FILE is the structure that maintains information
  2454.  about a stream. BUFSIZ defines the default size of stream buffers, in bytes.
  2455.  
  2456.  
  2457.  Routine                           Use
  2458.  ────────────────────────────────────────────────────────────────────────────
  2459.  clearerr                          Clears the error indicator for a stream
  2460.  
  2461.  fclose                            Closes a stream
  2462.  
  2463.  fcloseall                         Closes all open streams
  2464.  
  2465.  fdopen                            Associates a stream with an open file
  2466.                                    handle
  2467.  
  2468.  feof                              Tests for end-of-file on a stream
  2469.  
  2470.  ferror                            Tests for error on a stream
  2471.  
  2472.  fflush                            Flushes a stream
  2473.  
  2474.  fgetc                             Reads a character from a stream
  2475.                                    (function version)
  2476.  
  2477.  fgetchar                          Reads a character from stdin (function
  2478.                                    version)
  2479.  
  2480.  fgetpos                           Gets the position indicator of a stream
  2481.  
  2482.  fgets                             Reads a string from a stream
  2483.  
  2484.  fileno                            Gets the file handle associated with a
  2485.                                    stream
  2486.  
  2487.  flushall                          Flushes all streams
  2488.  
  2489.  fopen                             Opens a stream
  2490.  
  2491.  fprintf                           Writes formatted data to a stream
  2492.  
  2493.  fputc                             Writes a character to a stream (function
  2494.                                    version)
  2495.  
  2496.  fputchar                          Writes a character to stdout (function
  2497.                                    version)
  2498.  
  2499.  fputs                             Writes a string to a stream
  2500.  
  2501.  fread                             Reads unformatted data from a stream
  2502.  
  2503.  freopen                           Reassigns a FILE pointer to a new file
  2504.  
  2505.  fscanf                            Reads formatted data from a stream
  2506.  
  2507.  fseek                             Moves file position to a given location
  2508.  
  2509.  fsetpos                           Sets the position indicator of a stream
  2510.  
  2511.  _fsopen                           Opens a stream with file sharing
  2512.  
  2513.  ftell                             Gets current file position
  2514.  
  2515.  fwrite                            Writes unformatted data items to a
  2516.                                    stream
  2517.  
  2518.  getc                              Reads a character from a stream
  2519.  
  2520.  getchar                           Reads a character from stdin
  2521.  
  2522.  gets                              Reads a line from stdin
  2523.  
  2524.  getw                              Reads a binary int item from a stream
  2525.  
  2526.  printf                            Writes formatted data to stdout
  2527.  
  2528.  putc                              Writes a character to a stream
  2529.  
  2530.  putchar                           Writes a character to stdout
  2531.  
  2532.  puts                              Writes a line to a stream
  2533.  
  2534.  putw                              Writes a binary int item to a stream
  2535.  
  2536.  rewind                            Moves file position to beginning of a
  2537.                                    stream
  2538.  
  2539.  rmtmp                             Removes temporary files created by
  2540.                                    tmpfile
  2541.  
  2542.  scanf                             Reads formatted data from stdin
  2543.  
  2544.  setbuf                            Controls stream buffering
  2545.  
  2546.  setvbuf                           Controls stream buffering and buffer
  2547.                                    size
  2548.  
  2549.  sprintf                           Writes formatted data to a string
  2550.  
  2551.  sscanf                            Reads formatted data from a string
  2552.  
  2553.  tempnam                           Generates a temporary file name in given
  2554.                                    directory
  2555.  
  2556.  tmpfile                           Creates a temporary file
  2557.  
  2558.  tmpnam                            Generates a temporary file name
  2559.  
  2560.  ungetc                            Places a character in the buffer
  2561.  
  2562.  vfprintf                          Writes formatted data to a stream
  2563.  
  2564.  vprintf                           Writes formatted data to stdout
  2565.  
  2566.  vsprintf                          Writes formatted data to a string
  2567.  
  2568.  
  2569.  2.7.2.1  Opening a Stream
  2570.  
  2571.  A stream must be opened using the fdopen, fopen, freopen, or _fsopen
  2572.  function before input and output can be performed on that stream. When
  2573.  opening a stream, the named stream can be opened for reading, writing, or
  2574.  both, and can be opened in either text or binary mode.
  2575.  
  2576.  The fdopen, fopen, freopen, and _fsopen functions return a FILE pointer. You
  2577.  normally assign the pointer value to a variable and use the variable to
  2578.  refer to the opened stream. For instance, if your program contains the lines
  2579.  
  2580.  
  2581.    FILE *infile
  2582.    infile = fopen ("test.dat", "r");
  2583.  
  2584.  you can use the FILE pointer variable  infile  to refer to the stream.
  2585.  
  2586.  
  2587.  2.7.2.2  Using Predefined Stream Pointers
  2588.  
  2589.  When a program begins execution, the C start-up code automatically opens
  2590.  several streams: standard input, standard output, and standard error. By
  2591.  default, the standard input, standard output, and standard error streams are
  2592.  directed to the console (keyboard and screen). This means that when a
  2593.  program expects input from the "standard input," it receives that input from
  2594.  the console. Similarly, a program that writes to the "standard output"
  2595.  prints its data to the console. Error messages generated by the library
  2596.  routines are sent to the "standard error," meaning that error messages
  2597.  appear on the user's console.
  2598.  
  2599.  Under DOS, two additional streams are opened: standard auxiliary and
  2600.  standard print. (These streams are not available in OS/2.) The assignment of
  2601.  standard auxiliary and standard print depends on the machine configuration.
  2602.  These streams usually refer to the first serial port and a printer port, but
  2603.  those ports may not be available on some systems. Be sure to check your
  2604.  machine configuration before using these streams.
  2605.  
  2606.  You can refer to the standard streams with the following predefined stream
  2607.  pointers:
  2608.  
  2609.  Pointer                           Stream
  2610.  ────────────────────────────────────────────────────────────────────────────
  2611.  stdin                             Standard input
  2612.  
  2613.  stdout                            Standard output
  2614.  
  2615.  stderr                            Standard error
  2616.  
  2617.  stdaux                            Standard auxiliary (DOS only)
  2618.  
  2619.  stdprn                            Standard print (DOS only)
  2620.  
  2621.  You can use these pointers in any function that requires a stream pointer as
  2622.  an argument. Some functions, such as getchar and putchar, are designed to
  2623.  use stdin or stdout automatically. The pointers stdin, stdout, stderr,
  2624.  stdaux, and stdprn are constants, not variables; do not try to assign them a
  2625.  new stream pointer value.
  2626.  
  2627.  DOS and OS/2 allow you to redirect a program's standard input and standard
  2628.  output at the operating-system command level. OS/2 also allows you to
  2629.  redirect a program's standard error. See your operating system user's manual
  2630.  for a complete discussion of redirection.
  2631.  
  2632.  Within your program, you can use freopen to redirect stdin, stdout, stderr,
  2633.  stdaux, or stdprn so that it refers to a disk file or to a device. See the
  2634.  reference description of freopen for more details.
  2635.  
  2636.  
  2637.  2.7.2.3  Controlling Stream Buffering
  2638.  
  2639.  As mentioned earlier, stream routines can use in-memory buffers to speed I/O
  2640.  operations. Files opened using the stream routines are buffered by default,
  2641.  except for stdaux and stdprn, which are normally unbuffered. The stdout and
  2642.  stderr streams are flushed whenever they are full or (if you are writing to
  2643.  a character device) after each library call.
  2644.  
  2645.  By using the setbuf or setvbuf function, you can cause a stream to be
  2646.  unbuffered, or you can associate a buffer with an unbuffered stream. Buffers
  2647.  allocated by the system are not accessible to you, but buffers allocated
  2648.  with setbuf or  setvbuf refer to arrays in your program and can be
  2649.  manipulated. Buffers can be any size up to 32,767 bytes. This size is set by
  2650.  the manifest constant BUFSIZ in STDIO.H if you use seftbuf; if you use
  2651.  setvbuf, you can set the size of the buffer yourself. (See the descriptions
  2652.  of setbuf and setvbuf in the reference section for more details.)
  2653.  
  2654.  ────────────────────────────────────────────────────────────────────────────
  2655.  NOTE
  2656.  
  2657.  These routines affect only buffers created by C library routines. They have
  2658.  no effect on buffers created by the operating system.
  2659.  ────────────────────────────────────────────────────────────────────────────
  2660.  
  2661.  
  2662.  2.7.2.4  Closing Streams
  2663.  
  2664.  The fclose and fcloseall functions close a stream or streams. The fclose
  2665.  routine closes a single specified stream; fcloseall closes all open streams
  2666.  except stdin, stdout, stderr, stdaux, and stdprn. If your program does not
  2667.  explicitly close a stream, the stream is automatically closed when the
  2668.  program terminates. How-ever, it is a good practice to close a stream when
  2669.  your program is finished with it, as the number of streams that can be open
  2670.  at a given time is limited.
  2671.  
  2672.  
  2673.  2.7.2.5  Reading and Writing Data
  2674.  
  2675.  The stream functions allow you to transfer data in a variety of ways. You
  2676.  can read and write binary data (a sequence of bytes), or specify reading and
  2677.  writing by characters, lines, or more complicated formats.
  2678.  
  2679.  Reading and writing operations on streams always begin at the current
  2680.  position of the stream, known as the "file pointer" for the stream. The file
  2681.  pointer is changed to reflect the new position after a read or write
  2682.  operation takes place. For example, if you read a single character from a
  2683.  stream, the file pointer is increased by one byte so that the next operation
  2684.  begins with the first unread character. If a stream is opened for appending,
  2685.  the file pointer is automatically positioned at the end of the file before
  2686.  each write operation.
  2687.  
  2688.  The fseek and fsetpos functions allow you to position the file pointer
  2689.  anywhere in a file. The next operation occurs at the position you specified.
  2690.  The rewind routine positions the file pointer at the beginning of the file.
  2691.  Use the ftell or fgetpos routine to determine the current position of the
  2692.  file pointer.
  2693.  
  2694.  The feof macro detects an end-of-file condition on a stream. Once the
  2695.  end-of-file indicator is set, it remains set until the file is closed, or
  2696.  until clearerr, fseek, fsetpos, or rewind is called.
  2697.  
  2698.  Streams associated with a character-oriented device (such as a console) do
  2699.  not have file pointers. Data coming from or going to a console cannot be
  2700.  accessed randomly. Routines that set or get the file-pointer position (such
  2701.  as fseek, fgetpos, fsetpos, ftell, or rewind) have undefined results if used
  2702.  on a stream associated with a character-oriented device.
  2703.  
  2704.  
  2705.  2.7.2.6  Detecting Errors
  2706.  
  2707.  When an error occurs in a stream operation, an error indicator for the
  2708.  stream is set. You can use the ferror macro to test the error indicator and
  2709.  determine whether an error has occurred. Once an error has occurred, the
  2710.  error indicator for the stream remains set until the stream is closed, or
  2711.  until you explicitly clear the error indicator by calling clearerr or
  2712.  rewind.
  2713.  
  2714.  
  2715.  2.7.3  Low-Level Routines
  2716.  
  2717.  Low-level input and output calls do not buffer or format data. Declarations
  2718.  for the low-level functions are given in the include files IO.H, FCNTL.H,
  2719.  SYS\TYPES.H, and SYS\STAT.H. Unlike the stream functions, low-level
  2720.  functions do not require the include file STDIO.H. However, some common
  2721.  constants are defined in STDIO.H; for example, the end-of-file indicator
  2722.  (EOF) may be useful. If your program requires these constants, you must
  2723.  include STDIO.H.
  2724.  
  2725.  Routine                           Use
  2726.  ────────────────────────────────────────────────────────────────────────────
  2727.  close                             Closes a file
  2728.  
  2729.  creat                             Creates a file
  2730.  
  2731.  dup                               Creates a second handle for a file
  2732.  
  2733.  dup2                              Reassigns a handle to a file
  2734.  
  2735.  eof                               Tests for end-of-file
  2736.  
  2737.  lseek                             Repositions file pointer to a given
  2738.                                    location
  2739.  
  2740.  open                              Opens a file
  2741.  
  2742.  read                              Reads data from a file
  2743.  
  2744.  sopen                             Opens a file for file sharing
  2745.  
  2746.  tell                              Gets current file-pointer position
  2747.  
  2748.  umask                             Sets default file-permission mask
  2749.  
  2750.  write                             Writes data to a file
  2751.  
  2752.  
  2753.  2.7.3.1  Opening a File
  2754.  
  2755.  You must open a file before performing I/O functions on it. The open
  2756.  function opens a file; it can also create the file when opening it. In OS/2
  2757.  and DOS versions 3.0 and later, you can use sopen to open a file with
  2758.  file-sharing attributes. The creat function can create and open a file.
  2759.  
  2760.  The file can be opened for reading, writing, or both, and opened in either
  2761.  text or binary mode (see Section 2.7.1, "Text and Binary Modes"). The
  2762.  include file FCNTL.H must be included when opening a file, as it contains
  2763.  definitions for flags used in open. In some cases, the files SYS\TYPES.H and
  2764.  SYS\STAT.H must also be included; for more information, see the reference
  2765.  description for the open function.
  2766.  
  2767.  These functions return a file handle, which is normally assigned to an
  2768.  integer variable. You use the variable to refer to the opened file.
  2769.  
  2770.  
  2771.  2.7.3.2  Reading and Writing Data
  2772.  
  2773.  Use the read and write routines to read and write to files. These operations
  2774.  begin at the current position in the file. The current position is updated
  2775.  each time a read or write operation occurs.
  2776.  
  2777.  The lseek function allows you to place the file pointer anywhere in the
  2778.  file. The next operation occurs at the position you specified. The tell
  2779.  function indicates the current position of the file pointer. The eof routine
  2780.  tests for the end of the file.
  2781.  
  2782.  Low-level I/O routines set the errno variable when an error occurs. Chapter
  2783.  3, "Global Variables and Standard Types," describes errno.
  2784.  
  2785.  Character-oriented devices, such as the console, do not have file pointers.
  2786.  The lseek and tell routines have undefined results if used on a handle
  2787.  associated with a device.
  2788.  
  2789.  
  2790.  2.7.3.3  Closing Files
  2791.  
  2792.  The close function closes an open file. Open files are automatically closed
  2793.  when a program terminates. However, it is a good practice to close a file
  2794.  when your program is finished with it, as there is a limit to the number of
  2795.  files that can be open at one time.
  2796.  
  2797.  
  2798.  2.7.3.4  Using Predefined Handles
  2799.  
  2800.  When a program begins execution, three files are automatically opened:
  2801.  standard input, standard output, and standard error. In DOS, two additional
  2802.  files are opened: standard auxiliary and standard print. (These files are
  2803.  not available in OS/2.)
  2804.  
  2805.  Low-level routines can access these files using the following predefined
  2806.  handles:
  2807.  
  2808.  Stream                            Handle
  2809.  ────────────────────────────────────────────────────────────────────────────
  2810.  stdin                             0
  2811.  
  2812.  stdout                            1
  2813.  
  2814.  stderr                            2
  2815.  
  2816.  stdaux (DOS only)                 3
  2817.  
  2818.  stdprn (DOS only)                 4
  2819.  
  2820.  You can use these file handles without previously opening the files. The
  2821.  files are opened and the handles are assigned when the program starts.
  2822.  
  2823.  The dup and dup2 functions allow you to assign multiple handles for the same
  2824.  file. These functions are typically used to associate the predefined file
  2825.  handles with different files.
  2826.  
  2827.  In DOS and OS/2, you can redirect the standard input and standard output at
  2828.  the operating-system command level. OS/2 also allows you to redirect the
  2829.  standard error. See your operating system user's manual for a complete
  2830.  discussion of redirection.
  2831.  
  2832.  
  2833.  2.7.4  Console and Port I/O
  2834.  
  2835.  The console and port I/O routines are implemented as functions and are
  2836.  declared in the include file CONIO.H. These functions perform reading and
  2837.  writing operations on your console or on the specified port. The cgets,
  2838.  cscanf, getch, getche, and kbhit routines take input from the console, while
  2839.  cprintf, cputs, putch, and ungetch write to the console. The input or output
  2840.  of these functions can be redirected.
  2841.  
  2842.  Routine                           Use
  2843.  ────────────────────────────────────────────────────────────────────────────
  2844.  cgets                             Reads a string from the console
  2845.  
  2846.  cprintf                           Writes formatted data to the console
  2847.  
  2848.  cputs                             Writes a string to the console
  2849.  
  2850.  cscanf                            Reads formatted data from the console
  2851.  
  2852.  getch                             Reads a character from the console
  2853.  
  2854.  getche                            Reads a character from the console and
  2855.                                    echoes it
  2856.  
  2857.  inp                               Reads one byte from the specified I/O
  2858.                                    port
  2859.  
  2860.  inpw                              Reads a two-byte word from the specified
  2861.                                    I/O port
  2862.  
  2863.  kbhit                             Checks for a keystroke at the console
  2864.  
  2865.  outp                              Writes one byte to the specified I/O
  2866.                                    port
  2867.  
  2868.  outpw                             Writes a two-byte word to the specified
  2869.                                    I/O port
  2870.  
  2871.  putch                             Writes a character to the console
  2872.  
  2873.  ungetch                           "Ungets" the last character read from
  2874.                                    the console so that it becomes the next
  2875.                                    character read
  2876.  
  2877.  ────────────────────────────────────────────────────────────────────────────
  2878.  NOTE
  2879.  
  2880.  Programs that need only run under DOS can also use a number of direct DOS
  2881.  I/O system calls ( _dos_open, _dos_read, _dos_close, etc.) These are
  2882.  described in detail in Section 2.14, "System Calls."
  2883.  ────────────────────────────────────────────────────────────────────────────
  2884.  
  2885.  The console or port does not have to be opened or closed before I/O is
  2886.  performed, so there are no open or close routines in this category. The port
  2887.  I/O routines inp and outp read or write one byte at a time from the
  2888.  specified port. The inpw and outpw routines read and write two-byte words,
  2889.  respectively.
  2890.  
  2891.  The console I/O routines allow reading and writing of strings (cgets and
  2892.  cputs), formatted data (cscanf and cprintf), and characters. Several options
  2893.  are available when reading and writing characters.
  2894.  
  2895.  The putch routine writes a single character to the console. The getch and
  2896.  getche routines read a single character from the console; getche echoes the
  2897.  character back to the console, while getch does not. The ungetch routine
  2898.  "ungets" the last character read; the next read operation on the console
  2899.  begins with the "ungotten" character.
  2900.  
  2901.  The kbhit routine determines whether a key has been struck at the console.
  2902.  This routine allows you to test for keyboard input before you attempt to
  2903.  read from the console.
  2904.  
  2905.  ────────────────────────────────────────────────────────────────────────────
  2906.  NOTE
  2907.  
  2908.  The console I/O routines are not compatible with stream or low-level library
  2909.  routines and should not be used with them.
  2910.  ────────────────────────────────────────────────────────────────────────────
  2911.  
  2912.  
  2913.  2.8  Internationalization
  2914.  
  2915.  Internationalization routines are useful for creating different versions of
  2916.  a program for international markets. These routines are declared in the
  2917.  header file LOCALE.H, except for strftime, which is declared in TIME.H.
  2918.  
  2919.  Routine                           Use
  2920.  ────────────────────────────────────────────────────────────────────────────
  2921.  localeconv                        Sets a structure with appropriate values
  2922.                                    for formatting numeric quantities
  2923.  
  2924.  setlocale                         Selects the appropriate locale for the
  2925.                                    program
  2926.  
  2927.  strcoll                           Compares strings using locale-specific
  2928.                                    information
  2929.  
  2930.  strftime                          Formats a date and time string
  2931.  
  2932.  strxfrm                           Transforms a string based on
  2933.                                    locale-specific
  2934.                                    information
  2935.  
  2936.  
  2937.  2.9  Math
  2938.  
  2939.  The math routines allow you to perform common mathematical calculations. All
  2940.  math routines work with floating-point values and therefore require
  2941.  floating-point support (see Section 1.8, "Floating-Point Support").
  2942.  
  2943.  
  2944.  The math library provides two versions of some routines. The first version
  2945.  of the routine supports double arguments and return values. The second
  2946.  version supports an 80-bit data type, allowing the routine to take long
  2947.  double arguments and return a long double value. The second version usually
  2948.  has the same name with the suffix l. For instance, the acos routine supports
  2949.  double arguments and return values, while acosl supports long double
  2950.  arguments and return values.
  2951.  
  2952.  Routines which support long double values are not available when you compile
  2953.  with the /Fpa (alternate math) compiler option. The same is true of the
  2954.  _clear 87, _control87, and _status87 routines.
  2955.  
  2956.  
  2957.  Most math declarations are in the include file MATH.H. However, the
  2958.  _clear87, _control87, _fpreset, and _status87 routines are defined in
  2959.  FLOAT.H, the abs and labs functions are defined in MATH.H and STDLIB.H, and
  2960.  the div and ldiv routines are declared in STDLIB.H.
  2961.  
  2962.  
  2963.  Routine                           Use
  2964.  ────────────────────────────────────────────────────────────────────────────
  2965.  acos, acosl                       Calculate the arccosine
  2966.  
  2967.  asin, asinl                       Calculate the arcsine
  2968.  
  2969.  atan, atanl                       Calculate the arctangent
  2970.  
  2971.  atan2, atan2l                     Calculate the arctangent
  2972.  
  2973.  bessel                            Calculates Bessel functions
  2974.  
  2975.  cabs, cabsl                       Find the absolute value of a complex
  2976.                                    number
  2977.  
  2978.  ceil, ceill                       Find the integer ceiling
  2979.  
  2980.  _clear87                          Gets and clears the floating-point
  2981.                                    status word
  2982.  
  2983.  _control87                        Gets the old floating-point control word
  2984.                                    and sets a new control-word value
  2985.  
  2986.  cos, cosl                         Calculate the cosine
  2987.  
  2988.  cosh, coshl                       Calculate the hyperbolic cosine
  2989.  
  2990.  dieeetomsbin                      Converts IEEE double-precision number to
  2991.                                    Microsoft (MS) binary format
  2992.  
  2993.  div                               Divides one integer by another,
  2994.                                    returning the quotient and remainder
  2995.  
  2996.  dmsbintoieee                      Converts Microsoft binary
  2997.                                    double-precision number to IEEE format
  2998.  
  2999.  exp, expl                         Calculate the exponential function
  3000.  
  3001.  fabs, fabsl                       Find the absolute value
  3002.  
  3003.  fieeetomsbin                      Converts IEEE single-precision number to
  3004.                                    Microsoft binary format
  3005.  
  3006.  floor, floorl                     Find the largest integer less than or
  3007.                                    equal to the
  3008.                                    argument
  3009.  
  3010.  fmod, fmodl                       Find the floating-point remainder
  3011.  
  3012.  fmsbintoieee                      Converts Microsoft binary
  3013.                                    single-precision number to IEEE format
  3014.  
  3015.  _fpreset                          Reinitializes the floating-point-math
  3016.                                    package
  3017.  
  3018.  frexp, frexpl                     Calculate an exponential value
  3019.  
  3020.  hypot, hypotl                     Calculate the hypotenuse of right
  3021.                                    triangle
  3022.  
  3023.  ldexp, ldexpl                     Calculate the product of the argument
  3024.                                    and 2exp
  3025.  
  3026.  ldiv                              Divides one long integer by another,
  3027.                                    returning the quotient and remainder
  3028.  
  3029.  log, logl                         Calculate the natural logarithm
  3030.  
  3031.  log10, log10l                     Calculate the base-10 logarithm
  3032.  
  3033.  _lrotl,  _lrotr                   Shift an unsigned long int item left (
  3034.                                    _lrotl) or right ( _lrotr)
  3035.  
  3036.  matherr,  _matherrl               Handle math errors
  3037.  
  3038.  max, min                          Return the larger or smaller of two
  3039.                                    values
  3040.  
  3041.  modf, modfl                       Break down the argument into integer and
  3042.                                    fractional parts
  3043.  
  3044.  pow, powl                         Calculate a value raised to a power
  3045.  
  3046.  rand                              Gets a pseudorandom number
  3047.  
  3048.  _rotl,  _rotr                     Shift an unsigned int item left ( _rotl)
  3049.                                    or right
  3050.                                    ( _rotr)
  3051.  
  3052.  sin, sinl                         Calculate the sine
  3053.  
  3054.  sinh, sinhl                       Calculate the hyperbolic sine
  3055.  
  3056.  sqrt, sqrtl                       Find the square root
  3057.  
  3058.  srand                             Initializes a pseudorandom series
  3059.  
  3060.  _status87                         Gets the floating-point status word
  3061.  
  3062.  tan, tanl                         Calculate the tangent
  3063.  
  3064.  tanh, tanhl                       Calculate the hyperbolic tangent
  3065.  
  3066.  The bessel routine does not correspond to a single function, but to twelve
  3067.  functions named j0, j1, jn, y0, y1, yn,  _j0l,  _j1l,  _jnl,  _y0l,  _y1l,
  3068.  and  _ynl.
  3069.  
  3070.  The matherr and _matherrl routines are invoked by the math functions when
  3071.  errors occur. The matherr routine handles functions that return a double
  3072.  value and _matherrl handles routines that return a long double.
  3073.  
  3074.  These routines are defined in the library, but you can redefine them for
  3075.  different error-handling. The user-defined function, if given, must follow
  3076.  the rules given in the reference description of matherr and _matherrl.
  3077.  
  3078.  You are not required to supply a definition for the matherr routines. If no
  3079.  definition is present, the default error returns for each routine are used.
  3080.  The reference description of each routine describes that routine's error
  3081.  returns.
  3082.  
  3083.  
  3084.  2.10  Memory Allocation
  3085.  
  3086.  The memory-allocation routines allow you to allocate, free, and reallocate
  3087.  blocks of memory. Memory-allocation routines are declared in the include
  3088.  file MALLOC.H.
  3089.  
  3090.  Routine                           Use
  3091.  ────────────────────────────────────────────────────────────────────────────
  3092.  alloca                            Allocates a block of memory from the
  3093.                                    program's stack
  3094.  
  3095.  _bfreeseg                         Frees a based heap
  3096.  
  3097.  _bheapseg                         Allocates a based heap
  3098.  
  3099.  calloc,  _bcalloc,  _fcalloc,     Allocate storage for an array
  3100.  _ncalloc
  3101.  
  3102.  _expand,  _bexpand,  _fexpand,    Expand or shrink a block of memory
  3103.  _nexpand                          without moving its location
  3104.  
  3105.  free,  _bfree,  _ffree,  _nfree   Free an allocated block
  3106.  
  3107.  _freect                           Returns approximate number of items of
  3108.                                    given size that could be allocated in
  3109.                                    the near heap
  3110.  
  3111.  halloc                            Allocates storage for huge array
  3112.  
  3113.  _heapadd,  _bheapadd              Add memory to a heap
  3114.  
  3115.  _heapchk,  _bheapchk,  _fheapchk  Check a heap for consistency
  3116.  , _nheapchk
  3117.  
  3118.  _heapmin,  _bheapmin,             Release unused memory in a heap
  3119.  _fheapmin,  _nheapmin
  3120.  
  3121.  _heapset,  _bheapset,  _fheapset  Fill free heap entries with a specified
  3122.  , _nheapset                       value
  3123.  
  3124.  _heapwalk,  _bheapwalk,           Return information about each entry in a
  3125.  _fheapwalk, _nheapwalk            heap
  3126.  
  3127.  hfree                             Frees a block allocated by halloc
  3128.  
  3129.  malloc,  _bmalloc,  _fmalloc,     Allocate a block of memory
  3130.  _nmalloc
  3131.  
  3132.  _memavl                           Returns approximate number of bytes
  3133.                                    available for allocation in the near
  3134.                                    heap
  3135.  
  3136.  _memmax                           Returns size of largest contiguous free
  3137.                                    block in the near heap
  3138.  
  3139.  _msize,  _bmsize,  _fmsize,       Return size of an allocated block
  3140.  _nmsize
  3141.  
  3142.  realloc,  _brealloc,  _frealloc,  Reallocate a block to a new size
  3143.  _nrealloc
  3144.  
  3145.  stackavail                        Returns size of stack space available
  3146.                                    for allocation with alloca
  3147.  
  3148.  Some memory-management routines, such as malloc, are available in different
  3149.  versions that begin with _b, _f, or _n. These variations are described in
  3150.  the following section.
  3151.  
  3152.  The malloc and free routines allocate and free memory space, respectively,
  3153.  while a program runs. The malloc routine allocates memory from the "heap,"
  3154.  which is a pool of memory not otherwise used by your program. In tiny-,
  3155.  small-, and medium-model programs, the heap consists of unused memory in
  3156.  your program's default data segment. In compact-, large-, and huge-model
  3157.  programs, it is unused memory outside the default data segment.
  3158.  
  3159.  The malloc and free routines satisfy the memory-allocation requirements of
  3160.  most programs. More specialized memory-management routines are discussed
  3161.  below.
  3162.  
  3163.  The realloc and _expand routines can expand or shrink an allocated memory
  3164.  block. They behave differently in cases in which there is not enough room to
  3165.  expand the block in its current location. In this case, realloc moves the
  3166.  block as needed, but _expand does not.
  3167.  
  3168.  The calloc routine allocates memory for an array and initializes every byte
  3169.  in the allocated block to 0.
  3170.  
  3171.  The halloc routine is similar to calloc, except that it can allocate memory
  3172.  for a huge array (one that exceeds 64K in size). This routine is useful when
  3173.  you need a very large data object, or if you need to return allocated memory
  3174.  to the operating system for subsequent calls to the spawn family of
  3175.  functions.
  3176.  
  3177.  
  3178.  2.10.1  Near and Far Heaps
  3179.  
  3180.  As mentioned in the previous section, heap memory can reside inside or
  3181.  outside your program's default data segment, depending on what memory model
  3182.  your program uses. When it lies inside the default data segment, the heap is
  3183.  called the "near heap," since it can be accessed with near pointers. The
  3184.  "far heap" is memory that spans one or more segments outside the default
  3185.  data segment. The far heap can be accessed only with far pointers.
  3186.  
  3187.  In various memory models, malloc automatically allocates memory from the
  3188.  near heap or far heap, as appropriate. The C library also includes near and
  3189.  far versions of malloc, free, and other memory-management routines, which
  3190.  allow you to specify the near and far heaps explicitly. These have the same
  3191.  names as standard memory routines, but are preceded by _n (for near) or _f
  3192.  (for far).
  3193.  
  3194.  For instance, the _nmalloc routine always allocates memory from the near
  3195.  heap and returns a near pointer, no matter which memory model your program
  3196.  uses. Use _nfree to release memory allocated with _nmalloc.
  3197.  
  3198.  Similarly, _fmalloc always allocates memory from the far heap and returns a
  3199.  far pointer, regardless of memory model. Use the _ffree routine to release
  3200.  memory allocated with _fmalloc.
  3201.  
  3202.  
  3203.  2.10.2  Based Heaps
  3204.  
  3205.  You can also allocate memory from a "based heap," which is a single segment
  3206.  that lies outside the default data segment. Based-heap routines generally
  3207.  use the same names as standard memory routines, but begin with _b. For
  3208.  instance, _bmalloc allocates a memory block from the based heap and _bfree
  3209.  frees the block.
  3210.  
  3211.  Based heaps offer the following advantages:
  3212.  
  3213.  
  3214.    ■   Localized data. Based heaps allow you to group related data in a
  3215.        single segment. This can simplify the management of related data. In
  3216.        OS/2, based heaps can also minimize the risk of general protection
  3217.        faults and improve performance.
  3218.  
  3219.    ■   Faster pointer arithmetic. Although the based heap lies in the far
  3220.        data segment, pointers to its data items are the same size as near
  3221.        pointers. Thus, pointer arithmetic on items in a based heap is faster
  3222.        than pointer arithmetic on items in the far heap.
  3223.  
  3224.  
  3225.  The _bheapseg routine allocates a based heap segment, from which you can
  3226.  then allocate blocks of memory. You can call _bheapseg more than once to
  3227.  allocate as many based-heap segments as needed (within the confines of
  3228.  available memory).
  3229.  
  3230.  The _bfreeseg routine frees a based-heap segment. This routine frees every
  3231.  block in the based-heap segment, whether or not you previously freed the
  3232.  blocks individually.
  3233.  
  3234.  ────────────────────────────────────────────────────────────────────────────
  3235.  NOTE
  3236.  Near- , far- , and based-heap calls are not ANSI compatible and will make
  3237.  your program less portable.
  3238.  ────────────────────────────────────────────────────────────────────────────
  3239.  
  3240.  
  3241.  2.11  Process and Environment Control
  3242.  
  3243.  The process-control routines allow you to start, stop, and manage processes
  3244.  from within a program. Environment-control routines allow you to get and
  3245.  change information about the operating-system environment.
  3246.  
  3247.  A "process" is a program being executed by the operating system. It consists
  3248.  of the program's code and data, plus information about the process, such as
  3249.  the number of open files. Whenever you execute a program at the
  3250.  operating-system level, you start a process.
  3251.  
  3252.  All process-control functions except signal are declared in the include file
  3253.  PROCESS.H. The signal function is declared in SIGNAL.H. The abort, exit, and
  3254.  system functions are also declared in the STDLIB.H include file. The
  3255.  environment-control routines (getenv and putenv) are declared in STDLIB.H.
  3256.  
  3257.  Routine                           Use
  3258.  ────────────────────────────────────────────────────────────────────────────
  3259.  abort                             Aborts a process without flushing
  3260.                                    buffers or calling functions registered
  3261.                                    by atexit and onexit
  3262.  
  3263.  assert                            Tests for logic error
  3264.  
  3265.  atexit                            Schedules routines for execution at
  3266.                                    program
  3267.                                    termination
  3268.  
  3269.  _beginthread                      Creates an execution thread (OS/2 only)
  3270.  
  3271.  _cexit                            Performs the exit termination procedures
  3272.                                    (such as flushing buffers) and returns
  3273.                                    control to the calling program
  3274.  
  3275.  _c_exit                           Performs the _exit termination
  3276.                                    procedures and returns control to the
  3277.                                    calling program
  3278.  
  3279.  cwait                             Suspends the calling process until a
  3280.                                    specified child process terminates (OS/2
  3281.                                    only)
  3282.  
  3283.  _endthread                        Terminates an execution thread (OS/2
  3284.                                    only)
  3285.  
  3286.  execl                             Executes child process with argument
  3287.                                    list
  3288.  
  3289.  execle                            Executes child process with argument
  3290.                                    list and given environment
  3291.  
  3292.  execlp                            Executes child process using PATH
  3293.                                    variable and argument list
  3294.  
  3295.  execlpe                           Executes child process using PATH
  3296.                                    variable, given environment, and
  3297.                                    argument list
  3298.  
  3299.  execv                             Executes child process with argument
  3300.                                    array
  3301.  
  3302.  execve                            Executes child process with argument
  3303.                                    array and given environment
  3304.  
  3305.  execvp                            Executes child process using PATH
  3306.                                    variable and argument array
  3307.  
  3308.  execvpe                           Executes child process using PATH
  3309.                                    variable, given environment, and
  3310.                                    argument array
  3311.  
  3312.  exit                              Calls functions registered by atexit and
  3313.                                    onexit, then flushes all buffers and
  3314.                                    closes all open files before terminating
  3315.                                    the process
  3316.  
  3317.  _exit                             Terminates process without processing
  3318.                                    atexit or onexit functions or flushing
  3319.                                    buffers
  3320.  
  3321.  getenv                            Gets the value of an environment
  3322.                                    variable
  3323.  
  3324.  getpid                            Gets process ID number
  3325.  
  3326.  longjmp                           Restores a saved stack environment
  3327.  
  3328.  onexit                            Schedules routines for execution at
  3329.                                    program
  3330.                                    termination
  3331.  
  3332.  _pclose                           Waits for a child command and closes a
  3333.                                    pipe on the associated stream
  3334.  
  3335.  perror                            Prints error message
  3336.  
  3337.  _pipe                             Creates a pipe
  3338.  
  3339.  _popen                            Creates a pipe and asynchronously
  3340.                                    executes a child copy of the command
  3341.                                    processor
  3342.  
  3343.  putenv                            Adds or changes the value of an
  3344.                                    environment
  3345.                                    variable
  3346.  
  3347.  raise                             Sends a signal to the calling process
  3348.  
  3349.  setjmp                            Saves a stack environment
  3350.  
  3351.  signal                            Handles an interrupt signal
  3352.  
  3353.  spawnl                            Executes child process with argument
  3354.                                    list
  3355.  
  3356.  spawnle                           Executes child process with argument
  3357.                                    list and given environment
  3358.  
  3359.  spawnlp                           Executes child process using PATH
  3360.                                    variable and argument list
  3361.  
  3362.  spawnlpe                          Executes child process using PATH
  3363.                                    variable, given environment, and
  3364.                                    argument list
  3365.  
  3366.  spawnv                            Executes child process with argument
  3367.                                    array
  3368.  
  3369.  spawnve                           Executes child process with argument
  3370.                                    array and given environment
  3371.  
  3372.  spawnvp                           Executes child process using PATH
  3373.                                    variable and argument array
  3374.  
  3375.  spawnvpe                          Executes child process using PATH
  3376.                                    variable, given environment, and
  3377.                                    argument array
  3378.  
  3379.  system                            Executes an operating system command
  3380.  
  3381.  wait                              Suspends the calling process until any
  3382.                                    of the caller's immediate child
  3383.                                    processes terminate (OS/2 only)
  3384.  
  3385.  The atexit and onexit routines create a list of functions to be executed
  3386.  when the calling program terminates. The only difference between the two is
  3387.  that atexit is part of the ANSI standard. The onexit function is offered for
  3388.  compatibility with previous versions of Microsoft C.
  3389.  
  3390.  The _exit routine terminates a process immediately, whereas exit terminates
  3391.  the process only after flushing buffers and calling any functions previously
  3392.  registered by atexit and onexit. The _cexit and _c_exit routines are
  3393.  identical to exit and _exit, respectively, except that they return control
  3394.  to the calling program without terminating the process.
  3395.  
  3396.  The setjmp and longjmp routines save and restore a stack environment. These
  3397.  allow you to execute a nonlocal goto.
  3398.  
  3399.  The exec and spawn routines start a new process called the "child" process.
  3400.  The difference between the exec and spawn routines is that the spawn
  3401.  routines are capable of returning control from the child process to its
  3402.  caller (the "parent" process). Both the parent process and the child process
  3403.  are present in memory (unless P_OVERLAY is specified). In the exec routines,
  3404.  the child process overlays the parent process, so returning control to the
  3405.  parent process is impossible (unless an error occurs when attempting to
  3406.  start execution of the child process).
  3407.  
  3408.  There are eight forms each of the spawn and exec routines (see Table 2.1).
  3409.  The differences among the forms involve the method of locating the file to
  3410.  be executed as the child process, the method for passing arguments to the
  3411.  child process, and the method of setting the environment.
  3412.  
  3413.  Passing an argument list means that the arguments to the child process are
  3414.  listed separately in the exec or spawn call. Passing an argument array means
  3415.  that the arguments are stored in an array, and a pointer to the array is
  3416.  passed to the child process. The argument-list method is typically used when
  3417.  the number of arguments is constant or is known at compile time. The
  3418.  argument-array method is useful when the number of arguments must be
  3419.  determined at run time.
  3420.  
  3421.  Several process-control routines take advantage of the multitasking
  3422.  capability of OS/2. The _beginthread and _endthread routines create and
  3423.  terminate execution threads. The cwait and wait routines suspend the calling
  3424.  process until one child process terminates. The _pipe, _popen, and _pclose
  3425.  routines create and manipulate pipes, which link processes for sequential
  3426.  execution.
  3427.  
  3428.  Table 2.1  Forms of the spawn and exec Routines
  3429.  
  3430. ╓┌───────────────────┌──────────────────┌────────────────────┌───────────────
  3431.                                         Argument-Passing
  3432.  Routines            Locating the File  Convention           Environment
  3433.                                                              Settings
  3434.  ─────────────────────────────────────────────────────────────────────────────
  3435.  execl, spawnl       Do not use PATH    Argument list        Inherited from
  3436.                                                              parent
  3437.  
  3438.  execle, spawnle     Do not use PATH    Argument list        Pointer to
  3439.                                                              environment table
  3440.                                                              for child process
  3441.                                                              passed as last
  3442.                                                              argument
  3443.  
  3444.  execlp, spawnlp     Use PATH           Argument list        Inherited from
  3445.                                                              parent
  3446.                                         Argument-Passing
  3447.  Routines            Locating the File  Convention           Environment
  3448.                                                              Settings
  3449.                                                             parent
  3450.  
  3451.  execlpe, spawnlpe   Use PATH           Argument list        Pointer to
  3452.                                                              environment table
  3453.                                                              for child process
  3454.                                                              passed as last
  3455.                                                              argument
  3456.  
  3457.  execv, spawnv       Do not use PATH    Argument array       Inherited from
  3458.                                                              parent
  3459.  
  3460.  execve, spawnve     Do not use PATH    Argument array       Pointer to
  3461.                                                              environment table
  3462.                                                              for child process
  3463.                                                              passed as last
  3464.                                                              argument
  3465.  
  3466.  execvp, spawnvp     Use PATH           Argument array       Inherited from
  3467.                                         Argument-Passing
  3468.  Routines            Locating the File  Convention           Environment
  3469.                                                              Settings
  3470. execvp, spawnvp     Use PATH           Argument array       Inherited from
  3471.                                                              parent
  3472.  
  3473.  execvpe, spawnvpe   Use PATH           Argument array       Pointer to
  3474.                                                              environment table
  3475.                                                              for child process
  3476.                                                              passed as last
  3477.                                                              argument
  3478.  
  3479.  ─────────────────────────────────────────────────────────────────────────────
  3480.  
  3481.  
  3482.  
  3483.  The assert macro is typically used to test for logic errors. It prints a
  3484.  message when a given "assertion" fails to hold true. Defining the identifier
  3485.  NDEBUG to any value causes occurrences of assert to be removed from the
  3486.  source file, thus allowing you to turn off assertion checking without
  3487.  modifying the source file.
  3488.  
  3489.  
  3490.  2.12  Searching and Sorting
  3491.  
  3492.  Search and sort routines provide binary-search, linear-search, and
  3493.  quick-sort capabilities. They are all declared in SEARCH.H.
  3494.  
  3495.  Routine                           Use
  3496.  ────────────────────────────────────────────────────────────────────────────
  3497.  bsearch                           Performs binary search
  3498.  
  3499.  lfind                             Performs linear search for given value
  3500.  
  3501.  lsearch                           Performs linear search for given value,
  3502.                                    which is added to array if not found
  3503.  
  3504.  qsort                             Performs quick sort
  3505.  
  3506.  
  3507.  2.13  String Manipulation
  3508.  
  3509.  The string functions are declared in the include file STRING.H. They allow
  3510.  you to compare strings, copy them, search for strings and characters, and
  3511.  perform various other operations.
  3512.  
  3513.  
  3514.  Routines beginning with _f are model-independent versions of the
  3515.  corresponding routines and are useful in mixed-model programs. These
  3516.  routines can be called from any point in the program, regardless of which
  3517.  model is being used.
  3518.  
  3519.  Routine                           Use
  3520.  ────────────────────────────────────────────────────────────────────────────
  3521.  strcat,   _fstrcat                Append one string to another
  3522.  
  3523.  strchr,  _fstrchr                 Find first occurrence of a given
  3524.                                    character in a string
  3525.  
  3526.  strcmp,  _fstrcmp                 Compare two strings
  3527.  
  3528.  strcpy,  _fstrcpy                 Copy one string to another
  3529.  
  3530.  strcspn,  _fstrcspn               Find first occurrence of a character
  3531.                                    from a given character set in a string
  3532.  
  3533.  strdup,  _fstrdup, _nstrdup       Duplicate a string
  3534.  
  3535.  strerror                          Maps an error number to a message string
  3536.  
  3537.  _strerror                         Maps a user-defined error message to a
  3538.                                    string
  3539.  
  3540.  stricmp,  _fstricmp               Compare two strings without regard to
  3541.                                    case
  3542.  
  3543.  strlen,  _fstrlen                 Find length of string
  3544.  
  3545.  strlwr,  _fstrlwr                 Convert string to lowercase
  3546.  
  3547.  strncat,  _fstrncat               Append characters of a string
  3548.  
  3549.  strncmp,  _fstrncmp               Compare characters of two strings
  3550.  
  3551.  strncpy,  _fstrncpy               Copy characters of one string to another
  3552.  
  3553.  strnicmp,  _fstrnicmp             Compare characters of two strings
  3554.                                    without regard
  3555.                                    to case
  3556.  
  3557.  strnset,  _fstrnset               Set characters of a string to a given
  3558.                                    character
  3559.  
  3560.  strpbrk,  _fstrpbrk               Find first occurrence of a character
  3561.                                    from one string in another
  3562.  
  3563.  strrchr,  _fstrrchr               Find last occurrence of a given
  3564.                                    character in string
  3565.  
  3566.  strrev,  _fstrrev                 Reverse string
  3567.  
  3568.  strset,  _fstrset                 Set all characters of a string to a
  3569.                                    given character
  3570.  
  3571.  strspn,  _fstrspn                 Find first substring from a given
  3572.                                    character set in a string
  3573.  
  3574.  strstr,  _fstrstr                 Find first occurrence of a given string
  3575.                                    in another string
  3576.  
  3577.  strtok,  _fstrtok                 Find next token in a string
  3578.  
  3579.  strupr,  _fstrupr                 Convert a string to uppercase
  3580.  
  3581.  All string functions work on null-terminated character strings. When working
  3582.  with character arrays that do not end with a null character, you can use the
  3583.  buffer-manipulation routines, described in Section 2.1.
  3584.  
  3585.  
  3586.  2.14  System Calls
  3587.  
  3588.  The following routines give access to IBM-PC BIOS interrupts and DOS system
  3589.  calls. Except for the FP_OFF, FP_SEG, and segread routines, these routines
  3590.  are for DOS application programs only; they do not work under OS/2.
  3591.  
  3592.  
  3593.  2.14.1  BIOS Interface
  3594.  
  3595.  The functions in this category provide direct access to the BIOS interrupt
  3596.  services. They are all declared in BIOS.H.
  3597.  
  3598.  Routine                           Use
  3599.  ────────────────────────────────────────────────────────────────────────────
  3600.  _bios_disk                        Issues service requests for both hard
  3601.                                    and floppy disks, using INT 0x13
  3602.  
  3603.  _bios_equiplist                   Performs an equipment check, using INT
  3604.                                    0x11
  3605.  
  3606.  _bios_keybrd                      Provides access to keyboard services,
  3607.                                    using
  3608.                                    INT 0x16
  3609.  
  3610.  _bios_memsize                     Obtains information about available
  3611.                                    memory, using INT 0x12
  3612.  
  3613.  _bios_printer                     Performs printer output services, using
  3614.                                    INT 0x17
  3615.  
  3616.  _bios_serialcom                   Performs serial communications tasks,
  3617.                                    using
  3618.                                    INT 0x14
  3619.  
  3620.  _bios_timeofday                   Provides access to system clock, using
  3621.                                    INT 0x1A
  3622.  
  3623.  ────────────────────────────────────────────────────────────────────────────
  3624.  NOTE
  3625.  BIOS routines are hardware dependent. Some of them may not work as expected
  3626.  on machines whose hardware differs from the IBM PC.
  3627.  ────────────────────────────────────────────────────────────────────────────
  3628.  
  3629.  
  3630.  2.14.2  DOS Interface
  3631.  
  3632.  These routines are implemented as functions and declared in DOS.H.
  3633.  
  3634.  Routine                           Use
  3635.  ────────────────────────────────────────────────────────────────────────────
  3636.  bdos                              Invokes DOS system call; uses only DX
  3637.                                    and AL registers
  3638.  
  3639.  _chain_intr                       Chains one interrupt handler to another
  3640.  
  3641.  _disable                          Disables interrupts
  3642.  
  3643.  _dos_allocmem                     Allocates a block of memory, using DOS
  3644.                                    system call 0x48
  3645.  
  3646.  _dos_close                        Closes a file, using DOS system call
  3647.                                    0x3E
  3648.  
  3649.  _dos_creat                        Creates a new file and erases any
  3650.                                    existing file having the same name,
  3651.                                    using DOS system call 0x3C
  3652.  
  3653.  _dos_creatnew                     Creates a new file and returns an error
  3654.                                    if a file having the same name exists,
  3655.                                    using DOS system
  3656.                                    call 0x5B
  3657.  
  3658.  _dos_findfirst                    Finds first occurrence of a given file,
  3659.                                    using DOS system call 0x4E
  3660.  
  3661.  _dos_findnext                     Finds subsequent occurrences of a given
  3662.                                    file, using DOS system call 0x4F
  3663.  
  3664.  _dos_freemem                      Frees a block of memory, using DOS
  3665.                                    system
  3666.                                    call 0x49
  3667.  
  3668.  _dos_getdate                      Gets the system date, using DOS system
  3669.                                    call 0x2A
  3670.  
  3671.  _dos_getdiskfree                  Gets information on a disk volume, using
  3672.                                    DOS system call 0x36
  3673.  
  3674.  _dos_getdrive                     Gets the current default drive, using
  3675.                                    DOS system call 0x19
  3676.  
  3677.  _dos_getfileattr                  Gets current attributes of a file or
  3678.                                    directory, using DOS system call 0x43
  3679.  
  3680.  _dos_getftime                     Gets the date and time a file was last
  3681.                                    written, using DOS system call 0x57
  3682.  
  3683.  _dos_gettime                      Gets the current system time, using DOS
  3684.                                    system
  3685.                                    call 0x2C
  3686.  
  3687.  _dos_getvect                      Gets the current value of a specified
  3688.                                    interrupt vector, using DOS system call
  3689.                                    0x35
  3690.  
  3691.  _dos_keep                         Installs terminate-and-stay-resident
  3692.                                    (TSR) programs using DOS system call
  3693.                                    0x31
  3694.  
  3695.  _dos_open                         Opens an existing file, using DOS system
  3696.                                    call 0x3D
  3697.  
  3698.  _dos_read                         Reads a file, using DOS system call 0x3F
  3699.  
  3700.  
  3701.  _dos_setblock                     Changes the size of a previously
  3702.                                    allocated block, using DOS system call
  3703.                                    0x4A
  3704.  
  3705.  _dos_setdate                      Sets the current system date, using DOS
  3706.                                    system
  3707.                                    call 0x2B
  3708.  
  3709.  _dos_setdrive                     Sets the default disk drive, using DOS
  3710.                                    system
  3711.                                    call 0x0E
  3712.  
  3713.  _dos_setfileattr                  Sets the current attributes of a file,
  3714.                                    using DOS system call 0x43
  3715.  
  3716.  _dos_setftime                     Sets the date and time that the
  3717.                                    specified file was last written, using
  3718.                                    DOS system call 0x57
  3719.  
  3720.  _dos_settime                      Sets the system time, using DOS system
  3721.                                    call 0x2D
  3722.  
  3723.  _dos_setvect                      Sets a new value for the specified
  3724.                                    interrupt vector, using DOS system call
  3725.                                    0x25
  3726.  
  3727.  _dos_write                        Sends output to a file, using DOS system
  3728.                                    call 0x40
  3729.  
  3730.  dosexterr                         Obtains in-depth error information from
  3731.                                    DOS system call 0x59
  3732.  
  3733.  _enable                           Enables interrupts
  3734.  
  3735.  FP_OFF                            Returns offset portion of a far pointer
  3736.                                    (OS/2
  3737.                                    and DOS)
  3738.  
  3739.  FP_SEG                            Returns segment portion of a far pointer
  3740.                                    (OS/2
  3741.                                    and DOS)
  3742.  
  3743.  _harderr                          Establishes a hardware error handler
  3744.  
  3745.  _hardresume                       Returns to DOS after a hardware error
  3746.  
  3747.  _hardretn                         Returns to the application after a
  3748.                                    hardware error
  3749.  
  3750.  int86                             Invokes DOS interrupts
  3751.  
  3752.  int86x                            Invokes DOS interrupts with segment
  3753.                                    register values
  3754.  
  3755.  intdos                            Invokes DOS system call using registers
  3756.                                    other than DX and AL
  3757.  
  3758.  intdosx                           Invokes DOS system call using registers
  3759.                                    other than DX and AL with segment
  3760.                                    register values
  3761.  
  3762.  segread                           Returns current values of segment
  3763.                                    registers (OS/2 and DOS)
  3764.  
  3765.  The _harderr routine is used to define a hardware-error interrupt handler.
  3766.  The _hardresume and _hardretn routines are used within a hardware error
  3767.  handler to define the return from the error.
  3768.  
  3769.  The dosexterr function obtains and stores the error information returned by
  3770.  DOS system call 0x59 (extended error handling). This function is provided
  3771.  for use with DOS versions 3.0 and later.
  3772.  
  3773.  The bdos routine is useful for invoking DOS calls that use either or both of
  3774.  the DX (DH/DL) and AL registers for arguments. However, bdos should not be
  3775.  used to invoke system calls that return an error code in AX if the carry
  3776.  flag is set; since your program cannot detect whether the carry flag is set,
  3777.  it cannot determine whether the value in AX is a legitimate value or an
  3778.  error value. In this case, the intdos routine should be used instead, since
  3779.  it allows the program to detect whether the carry flag is set. The intdos
  3780.  routine can also be used to invoke DOS calls that use registers other than
  3781.  DX and AL.
  3782.  
  3783.  The intdosx routine is similar to the intdos routine, but is used when ES is
  3784.  required by the system call, when DS must contain a value other than the
  3785.  default data segment (for instance, when a far pointer is used), or when
  3786.  making the system call in a large-model program. When calling intdosx, give
  3787.  an argument that specifies the segment values to be used in the call.
  3788.  
  3789.  The int86 routine can be used to invoke any interrupt. The int86x routine is
  3790.  similar; however, like the intdosx routine, it is designed to work with
  3791.  large-model programs and far items, as described in the preceding paragraph.
  3792.  
  3793.  
  3794.  The FP_OFF and FP_SEG routines allow easy access to the segment and offset
  3795.  portions of a far pointer value. FP_OFF and FP_SEG are implemented as macros
  3796.  and defined in DOS.H. You can use these macros in OS/2 as well as DOS.
  3797.  
  3798.  The segread routine returns the current values of the segment registers.
  3799.  This routine is typically used with the intdosx and int86x routines to
  3800.  obtain the correct segment values.
  3801.  
  3802.  The _chain_intr routine is useful for chaining interrupt handlers together.
  3803.  The _enable routine enables interrupts, while the _disable routine disables
  3804.  interrupts.
  3805.  
  3806.  The routines prefixed with  _dos_  are all direct system interfaces that use
  3807.  the system calls noted above. More detailed information on these system
  3808.  calls can be found in the MS-DOS Encyclopedia (Duncan, ed.; Redmond, WA:
  3809.  Microsoft Press, 1988)or the Programmer's PC Sourcebook (Hogan; Redmond, WA:
  3810.  Microsoft Press, 1988).
  3811.  
  3812.  ────────────────────────────────────────────────────────────────────────────
  3813.  NOTE
  3814.  
  3815.  The DOS interface I/O routines are generally incompatible with console,
  3816.  low-level, and stream I/O routines. Do not mix different types of I/O
  3817.  routines in the same source file.
  3818.  ────────────────────────────────────────────────────────────────────────────
  3819.  
  3820.  
  3821.  2.15  Time
  3822.  
  3823.  The time functions allow you to obtain the current time, then convert and
  3824.  store it according to your particular needs. The current time is always
  3825.  taken from the system time.
  3826.  
  3827.  Routine                           Use
  3828.  ────────────────────────────────────────────────────────────────────────────
  3829.  asctime                           Converts time from type struct tm to a
  3830.                                    character string
  3831.  
  3832.  clock                             Returns the elapsed CPU time for a
  3833.                                    process
  3834.  
  3835.  ctime                             Converts time from a long integer to a
  3836.                                    character string
  3837.  
  3838.  difftime                          Computes the difference between two
  3839.                                    times
  3840.  
  3841.  ftime                             Puts current system time in variable of
  3842.                                    type
  3843.                                    struct tm
  3844.  
  3845.  gmtime                            Converts time from integer to struct tm
  3846.  
  3847.  localtime                         Converts time from integer to struct tm
  3848.                                    with local correction
  3849.  
  3850.  mktime                            Converts time to a calendar value
  3851.  
  3852.  _strdate                          Returns the current system date as a
  3853.                                    string
  3854.  
  3855.  strftime                          Formats a date and time string
  3856.  
  3857.  _strtime                          Returns the current system time as a
  3858.                                    string
  3859.  
  3860.  time                              Gets current system time as a long
  3861.                                    integer
  3862.  
  3863.  tzset                             Sets external time variables from the
  3864.                                    environment time variable
  3865.  
  3866.  utime                             Sets file-modification time
  3867.  
  3868.  The time and ftime functions return the current time as the number of
  3869.  seconds elapsed since midnight Universal Coordinated Time (UTC) on January
  3870.  1, 1970. This value can be converted, adjusted, and stored in a variety of
  3871.  ways by using the asctime, ctime, gmtime, localtime, and mktime functions.
  3872.  The utime function sets the modification time for a specified file, using
  3873.  either the current time or a time value stored in a structure.
  3874.  
  3875.  The clock function returns the elapsed CPU time for the calling process.
  3876.  
  3877.  The ftime function requires two files: SYS\TYPES.H and SYS\TIMEB.H. It is
  3878.  declared in SYS\TIMEB.H. The utime function also requires two include files:
  3879.  SYS\TYPES.H and SYS\UTIME.H. It is declared in SYS\UTIME.H. The remainder of
  3880.  the time functions are declared in the include file TIME.H.
  3881.  
  3882.  When you want to use ftime or localtime to make adjustments for local time,
  3883.  you must define an environment variable named TZ. Section 3.2, which
  3884.  describes the global variables daylight, timezone, and tzname, includes a
  3885.  discussion of the TZ variable. TZ is also described on the tzset reference
  3886.  page in Part 2 of this book.
  3887.  
  3888.  The _strdate and _strtime routines return strings containing the current
  3889.  date and time, respectively, in the DOS and OS/2 date and time format rather
  3890.  than in the XENIX-style formats.
  3891.  
  3892.  The stfrtime function is useful for creating international versions of a
  3893.  program. See Section 2.8, "Internationalization."
  3894.  
  3895.  
  3896.  2.16  Variable-Length Argument Lists
  3897.  
  3898.  The va_arg, va_end, and va_start routines are macros that provide a portable
  3899.  way to access the arguments to a function when the function takes a variable
  3900.  number of arguments. Two versions of the macros are available: the macros
  3901.  defined in the VARARG.H include file, which are compatible with the UNIX
  3902.  System V definition, and the macros defined in STDARG.H, which conform to
  3903.  the ANSI C standard.
  3904.  
  3905.  Routine                           Use
  3906.  ────────────────────────────────────────────────────────────────────────────
  3907.  va_arg                            Retrieves argument from list
  3908.  
  3909.  va_end                            Resets pointer
  3910.  
  3911.  va_start                          Sets pointer to beginning of argument
  3912.                                    list
  3913.  
  3914.  For more information on the differences between the two versions and for an
  3915.  explanation of how to use the macros, see their descriptions in Part 2 of
  3916.  this book.
  3917.  
  3918.  
  3919.  
  3920.  
  3921.  
  3922.  
  3923.  Chapter 3  Global Variables and Standard Types
  3924.  ────────────────────────────────────────────────────────────────────────────
  3925.  
  3926.  The Microsoft C Run-Time Library contains definitions for a number of
  3927.  variables and standard types used by library routines. You can access these
  3928.  variables and types by including in your program the files in which they are
  3929.  declared, or by giving appropriate declarations in your program, as shown in
  3930.  the following sections.
  3931.  
  3932.  
  3933.  3.1  _amblksiz
  3934.  
  3935.  The _amblksiz variable controls memory heap granularity.
  3936.  
  3937.  It is declared in the MALLOC.H include file as follows:
  3938.  
  3939.    extern unsigned int _amblksiz;
  3940.  
  3941.  The _amblksiz variable controls the amount of memory used in the heap for
  3942.  dynamic memory allocation.
  3943.  
  3944.  Memory space is always requested from the operating system in blocks
  3945.  containing _amblksiz bytes. The first time a program calls a
  3946.  memory-allocation function such as malloc, the operating system allocates a
  3947.  block of heap memory. The size of this block is defined by _amblksiz, which
  3948.  has a default value of 8K (8,192 bytes).
  3949.  
  3950.  Later memory requests are satisfied from the original block. When that block
  3951.  is exhausted, another block of _amblksiz bytes is allocated. If your C
  3952.  program allocates a block larger than _amblksiz, multiple blocks that are
  3953.  each of size _amblksiz are allocated until the request is satisfied.
  3954.  
  3955.  To change the size of the default memory block, assign the desired size to
  3956.  the _amblksiz variable, as in the following example:
  3957.  
  3958.    _amblksiz = 2048;
  3959.  
  3960.  The heap allocator always rounds the operating-system request to the nearest
  3961.  power of 2 greater than or equal to _amblksiz. The above statement allocates
  3962.  memory in multiples of 2K (2,048 bytes).
  3963.  
  3964.  Fewer system calls are required if you set _amblksiz to a large value, but
  3965.  your program may use more memory than needed. If program speed is important,
  3966.  set _amblksiz to a large value. If size is important, set _amblksiz to a
  3967.  smaller value.
  3968.  
  3969.  Note that adjusting the value of _amblksiz affects allocation in the near,
  3970.  far, and based heaps. The value of _amblksiz has no effect on huge memory
  3971.  blocks (those allocated with halloc and similar functions).
  3972.  
  3973.  
  3974.  3.2  daylight, timezone, tzname
  3975.  
  3976.  The daylight, timezone, and tzname variables are global timezone variables
  3977.  used in time functions.
  3978.  
  3979.  They are declared in the TIME.H include files as follows:
  3980.  
  3981.    extern int daylight;
  3982.  
  3983.    extern long timezone;
  3984.  
  3985.    extern char *tzname [2];
  3986.  
  3987.  Some time and date routines use the daylight, timezone, and tzname variables
  3988.  to make local-time adjustments. Whenever a program calls the ftime,
  3989.  localtime, or tzset function, the value of daylight, timezone, and tzname is
  3990.  determined from the value of the TZ environment variable. If you do not
  3991.  explicitly set the value of TZ, the default value of PST8PDT is used. The
  3992.  following list shows each variable and its value:
  3993.  
  3994.  Variable                          Value
  3995.  ────────────────────────────────────────────────────────────────────────────
  3996.  daylight                          Nonzero if a daylight-saving-time zone
  3997.                                    (DST) is specified in TZ; otherwise
  3998.                                    zero. Default value is one.
  3999.  
  4000.  timezone                          Difference in seconds between Greenwich
  4001.                                    mean time and the local time. Default
  4002.                                    value is 28,800.
  4003.  
  4004.  tzname[0]                         Three-letter time zone name derived from
  4005.                                    the TZ environment variable. Default
  4006.                                    value is "PST" (Pacific standard time).
  4007.  
  4008.  tzname[1]                         Three-letter daylight-saving-time zone
  4009.                                    name derived from the TZ environment
  4010.                                    variable. Default value is PDT. If the
  4011.                                    DST zone is omitted from TZ, tzname[1]
  4012.                                    is an empty string.
  4013.  
  4014.  
  4015.  3.3  _doserrno, errno, sys_errlist, sys_nerr
  4016.  
  4017.  The _doserrno, errno, sys_errlist, and sys_nerr variables contain error
  4018.  codes, and are used by the perror and _strerror routines to print error
  4019.  information.
  4020.  
  4021.  These variables are declared in the STDLIB.H include file. Manifest
  4022.  constants for the errno variables are declared in the ERRNO.H include file.
  4023.  The declarations are as follows:
  4024.  
  4025.    extern int _doserrno;
  4026.  
  4027.    extern int errno;
  4028.  
  4029.    extern char *sys_errlist[ ];
  4030.  
  4031.    extern int sys_nerr;
  4032.  
  4033.  The errno variable is set to an integer value to reflect the type of error
  4034.  that has occurred in a system-level call. Each errno value is associated
  4035.  with an error message, which can be printed with the perror routine or
  4036.  stored in a string with the strerror routine.
  4037.  
  4038.  Note that only some routines set the errno variable. If a routine sets
  4039.  errno, the description of the routine in the reference section says so
  4040.  explicitly.
  4041.  
  4042.  The value of errno reflects the error value for the last call that set
  4043.  errno. However, this value is not necessarily reset by later successful
  4044.  calls. To avoid confusion, test for errors immediately after a call.
  4045.  
  4046.  The include file ERRNO.H contains the definitions of the errno values.
  4047.  However, not all of the definitions given in ERRNO.H are used in DOS and
  4048.  OS/2. Some of the values in ERRNO.H are present to maintain compatibility
  4049.  with XENIX and UNIX operating systems.
  4050.  
  4051.  The errno values in DOS and OS/2 are a subset of the values for errno in
  4052.  XENIX systems. Thus, the errno value is not necessarily the same as the
  4053.  actual error code returned by a DOS or OS/2 system call. To access the
  4054.  actual DOS and OS/2 error code, use the _doserrno variable, which contains
  4055.  this value.
  4056.  
  4057.  In general, you should use _doserrno only for error detection in operations
  4058.  involving input and output, since the errno values for input and output
  4059.  errors have DOS and OS/2 error-code equivalents. In other cases, the value
  4060.  of _doserrno is undefined.
  4061.  
  4062.  The syserrlist variable is an array; the perror and strerror routines use it
  4063.  to process error information. The sys_nerr variable tells how many elements
  4064.  the sys_errlist array contains.
  4065.  
  4066.  Table 3.1 gives the errno values for DOS and OS/2, the system error message
  4067.  for each value, and the value of each constant. Note that only the ERANGE
  4068.  and EDOM constants are specified in the ANSI standard.
  4069.  
  4070.  Table   3.1 errno Values and Their Meanings
  4071.  
  4072. ╓┌───────────┌──────────────────────────────────────────────────────────┌────
  4073.  Constant    Meaning                                                    Value
  4074.  ─────────────────────────────────────────────────────────────────────────────
  4075.  E2BIG       Argument list too long                                     7
  4076.  Constant    Meaning                                                    Value
  4077.  ─────────────────────────────────────────────────────────────────────────────
  4078.  E2BIG       Argument list too long                                     7
  4079.  
  4080.  EACCES      Permission denied                                          13
  4081.  
  4082.  EBADF       Bad file number                                            9
  4083.  
  4084.  EDEADLOCK   Resource deadlock would occur                              36
  4085.  
  4086.  EDOM        Math argument                                              33
  4087.  
  4088.  EEXIST      File exists                                                17
  4089.  
  4090.  EINVAL      Invalid argument                                           22
  4091.  
  4092.  EMFILE      Too many open files                                        24
  4093.  
  4094.  ENOENT      No such file or directory                                  2
  4095.  
  4096.  ENOEXEC     Exec format error                                          8
  4097.  Constant    Meaning                                                    Value
  4098.  ─────────────────────────────────────────────────────────────────────────────
  4099. ENOEXEC     Exec format error                                          8
  4100.  
  4101.  ENOMEM      Not enough memory                                          12
  4102.  
  4103.  ENOSPC      No space left on device                                    28
  4104.  
  4105.  ERANGE      Result too large                                           34
  4106.  
  4107.  EXDEV       Cross-device link                                          18
  4108.  
  4109.  ─────────────────────────────────────────────────────────────────────────────
  4110.  
  4111.  
  4112.  
  4113.  
  4114.  3.4  _fmode
  4115.  
  4116.  The _fmode variable controls the default file-translation mode.
  4117.  
  4118.  It is declared in the STDLIB.H include file as follows:
  4119.  
  4120.    extern int _fmode;
  4121.  
  4122.  By default, the value of _fmode is O_TEXT, causing files to be translated in
  4123.  text mode (unless specifically opened or set to binary mode). When _fmode is
  4124.  set to O_BINARY, the default mode is binary. You can set _fmode to the flag
  4125.  O_BINARY by linking with BINMODE.OBJ or by assigning it the O_BINARY value.
  4126.  
  4127.  
  4128.  
  4129.  3.5  _osmajor, _osminor, _osmode, _osversion
  4130.  
  4131.  The _osmajor, _osminor, _osmode, and _osversion variables specify the
  4132.  version number of the operating system or the current mode of operation.
  4133.  
  4134.  They are declared in the STDLIB.H include file as follows:
  4135.  
  4136.    extern unsigned char _osmajor;
  4137.  
  4138.    extern unsigned char _osminor;
  4139.  
  4140.    extern unsigned char _osmode;
  4141.  
  4142.    extern unsigned char _osversion;
  4143.  
  4144.  The _osmajor, _osminor, and _osversion variables specify the version number
  4145.  of DOS or OS/2 currently in use. The _osmajor variable holds the "major"
  4146.  version number and the _osminor variable stores the "minor" version number.
  4147.  Thus, under DOS version 3.20, _osmajor is 3 and _osminor is 20. The
  4148.  _osversion variable holds both values; its low byte contains the major
  4149.  version number and its high byte the minor version number.
  4150.  
  4151.  These variables are useful for creating programs that run in different
  4152.  versions of DOS and OS/2. For example, you can test the _osmajor variable
  4153.  before making a call to sopen; if the major version number is earlier (less)
  4154.  than 3, open should be used instead of sopen.
  4155.  
  4156.  The _osmode variable indicates whether the program is in OS/2 protected mode
  4157.  or in real mode (DOS or OS/2 real mode). An _osmode value of DOS_MODE
  4158.  indicates real mode operation and a value of OS2_MODE indicates protected
  4159.  operation.
  4160.  
  4161.  
  4162.  3.6  environ
  4163.  
  4164.  The environ variable is a pointer to the strings in the process environment.
  4165.  
  4166.  
  4167.  It is declared in the STDLIB.H include file as follows:
  4168.  
  4169.    extern char *environ [ ];
  4170.  
  4171.  The environ variable provides access to memory areas containing
  4172.  process-specific information.
  4173.  
  4174.  The environ variable is an array of pointers to the strings that constitute
  4175.  the process environment. The environment consists of one or more entries of
  4176.  the form
  4177.  
  4178.  NAME=string
  4179.  
  4180.  where NAME is the name of an environment variable and string is the value of
  4181.  that variable. The string may be empty. The initial environment settings are
  4182.  taken from the operating-system environment at the time of program
  4183.  execution.
  4184.  
  4185.  The getenv and putenv routines use the environ variable to access and modify
  4186.  the environment table. When putenv is called to add or delete environment
  4187.  settings, the environment table changes size; its location in memory may
  4188.  also change, depending on the program's memory requirements. The environ
  4189.  variable is adjusted in these cases and always points to the correct table
  4190.  location.
  4191.  
  4192.  
  4193.  3.7  _psp
  4194.  
  4195.  The _psp variable contains the segment address of the program segment prefix
  4196.  (PSP) for the process.
  4197.  
  4198.  It is declared in the STDLIB.H include file as follows:
  4199.  
  4200.    extern unsigned int _psp;
  4201.  
  4202.  The PSP contains execution information about the process, such as a copy of
  4203.  the command line that invoked the process and the return address on process
  4204.  termination or interrupt. The _psp variable can be used to form a long
  4205.  pointer to the PSP, where _psp is the segment value and 0 is the offset
  4206.  value.
  4207.  
  4208.  Note that the _psp variable is supported only in DOS.
  4209.  
  4210.  
  4211.  3.8  Standard Types
  4212.  
  4213.  A number of library routines use values whose types are defined in include
  4214.  files. In the following list, these types are described, and the include
  4215.  file defining each type is given.
  4216.  
  4217.  Standard Type                     Description
  4218.  ────────────────────────────────────────────────────────────────────────────
  4219.  clock_t                           The clock_t type, defined in TIME.H,
  4220.                                    stores time values. It is used by the
  4221.                                    clock function.
  4222.  
  4223.  complex                           The complex structure, defined in
  4224.                                    MATH.H, stores the real and imaginary
  4225.                                    parts of complex numbers. It is used by
  4226.                                    the cabs function.
  4227.  
  4228.  diskfree_t                        The diskfree_t structure, defined in
  4229.                                    DOS.H, stores disk information used by
  4230.                                    the _dos_getdiskfree routine.
  4231.  
  4232.  diskinfo_t                        The diskinfo_t structure, defined in
  4233.                                    BIOS.H, records information about disk
  4234.                                    drives returned by the _bios_disk
  4235.                                    routine.
  4236.  
  4237.  div_t, ldiv_t                     The div_t and ldiv_t structures, defined
  4238.                                    in STDLIB.H, store the values returned
  4239.                                    by the div and ldiv functions,
  4240.                                    respectively.
  4241.  
  4242.  dosdate_t                         The dosdate_t structure, defined in
  4243.                                    DOS.H, records the current system date
  4244.                                    used in the _dos_getdate and
  4245.                                    _dos_setdate routines.
  4246.  
  4247.  dostime_t                         The dostime_t structure, defined in
  4248.                                    DOS.H, records the current system time
  4249.                                    used in the _dos_gettime and
  4250.                                    _dos_settime routines.
  4251.  
  4252.  DOSERROR                          The DOSERROR structure, defined in
  4253.                                    DOS.H, stores values returned by DOS
  4254.                                    system call 59H (available under DOS
  4255.                                    versions 3.0 and later).
  4256.  
  4257.  exception                         The exception structure, defined in
  4258.                                    MATH.H, stores error information for
  4259.                                    math routines. It is used by the matherr
  4260.                                    routine.
  4261.  
  4262.  FILE                              The FILE structure, defined in STDIO.H,
  4263.                                    is the structure used in all stream
  4264.                                    input and output operations. The fields
  4265.                                    of the FILE structure store information
  4266.                                    about the current state of the stream.
  4267.  
  4268.  find_t                            The find_t structure, defined in DOS.H,
  4269.                                    stores file-attribute information
  4270.                                    returned by the _dos_findfirst and
  4271.                                    _dos_findnext routines.
  4272.  
  4273.  fpos_t                            The fgetpos and fsetpos functions use
  4274.                                    the fpos_t object type, defined in
  4275.                                    STDIO.H, to record all the information
  4276.                                    necessary to uniquely specify every
  4277.                                    position within the file.
  4278.  
  4279.  jmp_buf                           The jmp_buf type, defined in SETJMP.H,
  4280.                                    is an array type rather than a structure
  4281.                                    type. A buffer of this type is used by
  4282.                                    the setjmp and longjmp routines to save
  4283.                                    and restore the program
  4284.                                    environment.
  4285.  
  4286.  lconv                             The lconv type is a structure containing
  4287.                                    formatting rules for numeric values in
  4288.                                    different countries. It is defined in
  4289.                                    LOCALE.H.
  4290.  
  4291.  onexit_t                          The onexit routine is declared as an
  4292.                                    onexit_t pointer type, which is defined
  4293.                                    in STDLIB.H.
  4294.  
  4295.  ptrdiff_t                         The ptrdiff_t type is used for the
  4296.                                    signed integral result of the
  4297.                                    subtraction of two pointers.
  4298.  
  4299.  REGS                              The REGS union, defined in DOS.H, stores
  4300.                                    byte and word register values to be
  4301.                                    passed to and returned from calls to the
  4302.                                    DOS interface functions.
  4303.  
  4304.  sig_atomic_t                      The sig_atomic_t type, defined in
  4305.                                    SIGNAL.H, is the integral type of an
  4306.                                    object that can be modified as an atomic
  4307.                                    entity, even in the presence of
  4308.                                    asynchronous interrupts. It is used in
  4309.                                    conjunction with the signal routine.
  4310.  
  4311.  size_t                            The size_t type, defined in STDDEF.H and
  4312.                                    several other include files, is the
  4313.                                    unsigned integral result of the sizeof
  4314.                                    operator.
  4315.  
  4316.  SREGS                             The SREGS structure, defined in DOS.H,
  4317.                                    stores the values of the ES, CS, SS, and
  4318.                                    DS registers. This structure is used by
  4319.                                    the DOS interface functions that require
  4320.                                    segment register values (int86x, intdosx
  4321.                                    , and segread).
  4322.  
  4323.  stat                              The stat structure, defined in
  4324.                                    SYS\STAT.H, contains file-status
  4325.                                    information returned by the stat and
  4326.                                    fstat routines.
  4327.  
  4328.  time_t                            The time_t type, defined in TIME.H,
  4329.                                    represents time values in the mktime and
  4330.                                    time routines.
  4331.  
  4332.  timeb                             The timeb structure, defined in
  4333.                                    SYS\TIMEB.H, is used by the ftime
  4334.                                    routine to store the current system
  4335.                                    time.
  4336.  
  4337.  tm                                The tm structure, defined in TIME.H, is
  4338.                                    used by the asctime, gmtime, and
  4339.                                    localtime functions to store and
  4340.                                    retrieve time information.
  4341.  
  4342.  utimbuf                           The utimbuf structure, defined in
  4343.                                    SYS\UTIME.H, stores file access and
  4344.                                    modification times used by the utime
  4345.                                    function to change file-modification
  4346.                                    dates.
  4347.  
  4348.  va_list                           The va_list array type, defined in
  4349.                                    STDARG.H, is used to hold information
  4350.                                    needed by the va_arg macro and the
  4351.                                    va_end routine. The called function
  4352.                                    declares a variable of type va_list,
  4353.                                    which may be passed as an argument to
  4354.                                    another function.
  4355.  
  4356.  
  4357.  
  4358.  
  4359.  
  4360.  
  4361.  PART II  Run-Time Functions
  4362.  ────────────────────────────────────────────────────────────────────────────
  4363.  
  4364.  The second part of this book is the reference section. It describes, in
  4365.  alphabetical order, each function of the run-time library provided with the
  4366.  Microsoft C Professional Development System.
  4367.  
  4368.  Each reference entry gives syntax, return values, and other useful
  4369.  information about the library functions. Information on compatibility is
  4370.  supplied to assist you in writing portable programs.
  4371.  
  4372.  
  4373.  About the Run-Time Reference
  4374.  
  4375.  The following pages describe, in alphabetical order, the more than 400
  4376.  functions in the Microsoft run-time library. In some cases, related routines
  4377.  are clustered in the same description. For example, the based, near, and far
  4378.  versions of _heapwalk are in the same discussion, as are the regular and
  4379.  long double versions of the math functions, such as acos and atan.
  4380.  Differences are noted where appropriate. Refer to Chapter 2, "Run-Time
  4381.  Routines by Category," or to the index to locate any function that does not
  4382.  appear in the expected position within the alphabetical reference.
  4383.  
  4384.  The discussion of each function (or group of functions) is divided into the
  4385.  following sections:
  4386.  
  4387.  
  4388.    ■   Description. Summarizes the routine's effect, names the include
  4389.        file(s) containing its declaration, illustrates the syntax, and
  4390.        briefly describes the arguments.
  4391.  
  4392.    ■   Remarks. Gives a more detailed description of the routine and how it
  4393.        is used.
  4394.  
  4395.    ■   Return Value. Describes the value returned by the routine.
  4396.  
  4397.    ■   Compatibility. Tells whether the routine is compatible with ANSI C,
  4398.        MS-DOS, OS/2, UNIX, and XENIX.
  4399.  
  4400.    ■   See Also. Names related routines.
  4401.  
  4402.    ■   Example. Gives a complete program showing the use of the routine.
  4403.  
  4404.  
  4405.  
  4406.  
  4407.  
  4408.  
  4409.  abort
  4410.  ────────────────────────────────────────────────────────────────────────────
  4411.  
  4412.  
  4413.  Description
  4414.  
  4415.  Aborts the current process and returns an error code.
  4416.  
  4417.  #include <process.h>              Required only for function declarations;
  4418.  #include <stdlib.h>               use either PROCESS.H or STDLIB.H
  4419.  
  4420.    void abort( void );
  4421.  
  4422.  
  4423.  Remarks
  4424.  
  4425.  The abort function prints the message
  4426.  
  4427.    abnormal program termination
  4428.  
  4429.  to stderr, then calls raise(SIGABRT). The action taken in response to the
  4430.  SIGABRT signal depends on what action has been defined for that signal in a
  4431.  prior call to the signal function. The default SIGABRT action is for the
  4432.  calling process to terminate with exit code 3, returning control to the
  4433.  parent process or operating system.
  4434.  
  4435.  The abort function does not flush stream buffers or do atexit/onexit
  4436.  processing.
  4437.  
  4438.  
  4439.  Return Value
  4440.  
  4441.  The abort function does not return control to the caller. Rather, it
  4442.  terminates the process and, by default, returns an exit code of 3 to the
  4443.  parent process.
  4444.  
  4445.  
  4446.  Compatibility
  4447.  
  4448.   ANSI   DOS   OS/2   UNIX   XENIX
  4449.  
  4450.  
  4451.  In multithread libraries, the abort function does not call raise(SIGABRT).
  4452.  Instead, it simply terminates the process with exit code 3.
  4453.  
  4454.  
  4455.  See Also
  4456.  
  4457.  exec functions, exit, _exit, raise, signal, spawn functions
  4458.  
  4459.  
  4460.  Example
  4461.  
  4462.    /* ABORT.C:  This tries to open a file and aborts if the attempt fails. */
  4463.  
  4464.    #include <stdio.h>
  4465.    #include <stdlib.h>
  4466.  
  4467.    void main()
  4468.    {
  4469.  
  4470.       FILE *stream;
  4471.  
  4472.       if( (stream = fopen( "NOSUCHF.ILE", "r" )) == NULL )
  4473.       {
  4474.          perror( "Couldn't open file" );
  4475.          abort();
  4476.       }
  4477.       else
  4478.          fclose( stream );
  4479.    }
  4480.  
  4481.  
  4482.  Output
  4483.  
  4484.  
  4485.  
  4486.    Couldn't open file: No such file or directory
  4487.  
  4488.    abnormal program termination
  4489.  
  4490.  
  4491.  
  4492.  
  4493.  
  4494.  
  4495.  abs
  4496.  ────────────────────────────────────────────────────────────────────────────
  4497.  
  4498.  
  4499.  Description
  4500.  
  4501.  Calculates the absolute value.
  4502.  
  4503.  #include <stdlib.h>               Required only for function declarations;
  4504.  #include <math.h>                 use either STDLIB.H or MATH.H
  4505.  
  4506.    int abs( int n );
  4507.  
  4508.  n                                 Integer value
  4509.  
  4510.  
  4511.  Remarks
  4512.  
  4513.  The abs function returns the absolute value of its integer argument n.
  4514.  
  4515.  
  4516.  Return Value
  4517.  
  4518.  The abs function returns the absolute value of its argument. There is no
  4519.  error return.
  4520.  
  4521.  
  4522.  Compatibility
  4523.  
  4524.   ANSI   DOS   OS/2   UNIX   XENIX
  4525.  
  4526.  
  4527.  
  4528.  See Also
  4529.  
  4530.  cabs, fabs, labs
  4531.  
  4532.  
  4533.  Example
  4534.  
  4535.    /* ABS.C: This program computes and displays the absolute values of
  4536.     * several numbers.
  4537.     */
  4538.  
  4539.    #include <stdio.h>
  4540.    #include <math.h>
  4541.    #include <stdlib.h>
  4542.  
  4543.    void main()
  4544.    {
  4545.       int    ix = -4, iy;
  4546.       long   lx = -41567L, ly;
  4547.       double dx = -3.141593, dy;
  4548.  
  4549.       iy = abs( ix );
  4550.       printf( "The absolute value of %d is %d\n", ix, iy);
  4551.  
  4552.       ly = labs( lx );
  4553.       printf( "The absolute value of %ld is %ld\n", lx, ly);
  4554.  
  4555.       dy = fabs( dx );
  4556.       printf( "The absolute value of %f is %f\n", dx, dy );
  4557.    }
  4558.  
  4559.  
  4560.  Output
  4561.  
  4562.  
  4563.  
  4564.    The absolute value of -4 is 4
  4565.    The absolute value of -41567 is 41567
  4566.    The absolute value of -3.141593 is 3.141593
  4567.  
  4568.  
  4569.  
  4570.  
  4571.  
  4572.  access
  4573.  ────────────────────────────────────────────────────────────────────────────
  4574.  
  4575.  
  4576.  Description
  4577.  
  4578.  Determines file-access permission.
  4579.  
  4580.  #include <io.h>                   Required only for function declarations
  4581.  
  4582.  #include <errno.h>                Required for definition of errno
  4583.                                    constants
  4584.  
  4585.    int access( char *pathname, int mode );
  4586.  
  4587.  pathname                          File or directory path name
  4588.  
  4589.  mode                              Permission setting
  4590.  
  4591.  
  4592.  Remarks
  4593.  
  4594.  With files, the access function determines whether the specified file exists
  4595.  and can be accessed in mode. The possible mode values and their meanings in
  4596.  the access call are as follows:
  4597.  
  4598.  Value                             Meaning
  4599.  ────────────────────────────────────────────────────────────────────────────
  4600.  00                                Check for existence only
  4601.  
  4602.  02                                Check for write permission
  4603.  
  4604.  04                                Check for read permission
  4605.  
  4606.  06                                Check for read and write permission
  4607.  
  4608.  With directories, access determines only whether the specified directory
  4609.  exists; under DOS and OS/2, all directories have read and write access.
  4610.  
  4611.  
  4612.  Return Value
  4613.  
  4614.  The access function returns the value 0 if the file has the given mode. A
  4615.  return value of -1 indicates that the named file does not exist or is not
  4616.  accessible in the given mode, and errno is set to one of the following
  4617.  values:
  4618.  
  4619.  Value                             Meaning
  4620.  ────────────────────────────────────────────────────────────────────────────
  4621.  EACCES                            Access denied: the file's permission
  4622.                                    setting does not allow the specified
  4623.                                    access.
  4624.  
  4625.  ENOENT                            File or path name not found.
  4626.  
  4627.  
  4628.  Compatibility
  4629.  
  4630.   ANSI   DOS   OS/2   UNIX   XENIX
  4631.  
  4632.  
  4633.  
  4634.  See Also
  4635.  
  4636.  chmod, fstat, open, stat
  4637.  
  4638.  
  4639.  Example
  4640.  
  4641.    /* ACCESS.C: This example uses access to check the file named "data"
  4642.     * to see if it exists and if writing is allowed.
  4643.     */
  4644.  
  4645.    #include <io.h>
  4646.    #include <stdio.h>
  4647.    #include <stdlib.h>
  4648.  
  4649.    void main()
  4650.    {
  4651.       /* Check for existence */
  4652.       if( (access( "access.c", 0 )) != -1 )
  4653.       {
  4654.          printf( "File exists\n" );
  4655.  
  4656.          /* Check for write permission */
  4657.          if( (access( "access.c", 2 )) != -1 )
  4658.             printf( "File has write permission\n" );
  4659.       }
  4660.    }
  4661.  
  4662.  
  4663.  Output
  4664.  
  4665.  
  4666.  
  4667.    File exists
  4668.    File has write permission
  4669.  
  4670.  
  4671.  
  4672.  
  4673.  
  4674.  acos Functions
  4675.  ────────────────────────────────────────────────────────────────────────────
  4676.  
  4677.  
  4678.  Description
  4679.  
  4680.  Calculate the arccosine.
  4681.  
  4682.  #include <math.h>
  4683.  
  4684.  #include <errno.h>                Required for definition of errno
  4685.                                    constant
  4686.  
  4687.    double acos( double x );
  4688.  
  4689.    long double acosl( long double x );
  4690.  
  4691.  x                                 Value whose arccosine is to be
  4692.                                    calculated
  4693.  
  4694.  
  4695.  Remarks
  4696.  
  4697.  The acos functions return the arccosine of x in the range 0 to π radians.
  4698.  The value of x must be between -1 and 1. The acosl function is the 80-bit
  4699.  counterpart, which uses an 80-bit, 10-byte coprocessor form of arguments and
  4700.  return values. See the reference page on the long double functions for more
  4701.  details on this data type.
  4702.  
  4703.  
  4704.  Return Value
  4705.  
  4706.  The acos functions return the arccosine result. If x is less than -1 or
  4707.  greater than 1, the function sets errno to EDOM, prints a DOMAIN error
  4708.  message to stderr, and returns 0. Error handling can be modified with the
  4709.  matherr (or _matherrl) routine.
  4710.  
  4711.  
  4712.  Compatibility
  4713.  
  4714.  acos
  4715.  
  4716.   ANSI   DOS   OS/2   UNIX   XENIX
  4717.  
  4718.  
  4719.  acosl
  4720.  
  4721.   ANSI   DOS   OS/2   UNIX   XENIX
  4722.  
  4723.  
  4724.  
  4725.  See Also
  4726.  
  4727.  asin functions, atan functions, cos functions, matherr, sin functions, tan
  4728.  functions
  4729.  
  4730.  
  4731.  Example
  4732.  
  4733.    /* ASINCOS.C: This program prompts for a value in the range -1 to 1.
  4734.     * Input values outside this range will produce DOMAIN error messages.
  4735.     * If a valid value is entered, the program prints the arcsine and the
  4736.     * arccosine of that value.
  4737.     */
  4738.  
  4739.    #include <math.h>
  4740.    #include <stdio.h>
  4741.    #include <stdlib.h>
  4742.    #include <errno.h>
  4743.  
  4744.    void main()
  4745.    {
  4746.       double x, y;
  4747.  
  4748.       printf( "Enter a real number between -1 and 1: " );
  4749.       scanf( "%lf", &x );
  4750.       y = asin( x );
  4751.       printf( "Arcsine of %f = %f\n", x, y );
  4752.       y = acos( x );
  4753.       printf( "Arccosine of %f = %f\n", x, y );
  4754.    }
  4755.  
  4756.  
  4757.  Output
  4758.  
  4759.  
  4760.  
  4761.    Enter a real number between -1 and 1: .32696
  4762.    Arcsine of 0.326960 = 0.333085
  4763.    Arccosine of 0.326960 = 1.237711
  4764.  
  4765.  
  4766.  
  4767.  
  4768.  
  4769.  alloca
  4770.  ────────────────────────────────────────────────────────────────────────────
  4771.  
  4772.  
  4773.  Description
  4774.  
  4775.  Allocates memory on the stack.
  4776.  
  4777.  #include <malloc.h>               Required only for function declarations
  4778.  
  4779.    void *alloca( size_t size );
  4780.  
  4781.  size                              Bytes to be allocated from stack
  4782.  
  4783.  
  4784.  Remarks
  4785.  
  4786.  The alloca routine allocates size bytes from the program's stack. The
  4787.  allocated space is automatically freed when the calling function is exited.
  4788.  
  4789.  
  4790.  When you compile with optimization on (either by default or by using one of
  4791.  the /O options), the stack pointer may not be restored properly in functions
  4792.  that have no local variables and that also reference the alloca function.
  4793.  The following program demonstrates the problem:
  4794.  
  4795.    /* Compile with CL /Lp /AM /Ox /Fc */
  4796.    #include <malloc.h>
  4797.  
  4798.    void main( void )
  4799.    {
  4800.       func( 10 );
  4801.    }
  4802.    void func( register int i )
  4803.    {
  4804.       alloca( i );
  4805.    }
  4806.  
  4807.  To ensure that the stack pointer is properly restored, make sure that any
  4808.  function referencing alloca declares at least one local variable.
  4809.  
  4810.  The pointer value returned by alloca should never be passed as an argument
  4811.  to free, nor should alloca be used in an expression that is an argument to a
  4812.  function.
  4813.  
  4814.  
  4815.  Return Value
  4816.  
  4817.  The alloca routine returns a void pointer to the allocated space, which is
  4818.  guaranteed to be suitably aligned for storage of any type of object. To get
  4819.  a pointer to a type other than char, use a type cast on the return value.
  4820.  The return value is NULL if the space cannot be allocated.
  4821.  
  4822.  
  4823.  Compatibility
  4824.  
  4825.   ANSI   DOS   OS/2   UNIX   XENIX
  4826.  
  4827.  
  4828.  
  4829.  See Also
  4830.  
  4831.  calloc functions, malloc functions, realloc functions
  4832.  
  4833.  
  4834.  Example
  4835.  
  4836.    /* ALLOCA.C: This program checks the stack space available before
  4837.     * and after using the alloca function to allocate space on the stack.
  4838.     */
  4839.  
  4840.    #include <malloc.h>
  4841.    #include <stdio.h>
  4842.  
  4843.    void main()
  4844.    {
  4845.       char *buffer;
  4846.  
  4847.       printf( "Bytes available on stack: %u\n", stackavail() );
  4848.  
  4849.       /* Allocate memory for string. */
  4850.       buffer = alloca( 120 * sizeof( char ) );
  4851.       printf( "Enter a string: " );
  4852.       gets( buffer );
  4853.       printf( "You entered: %s\n", buffer );
  4854.  
  4855.       printf( "Bytes available on stack: %u\n", stackavail() );
  4856.    }
  4857.  
  4858.  
  4859.  Output
  4860.  
  4861.  
  4862.  
  4863.    Bytes available on stack: 2028
  4864.    Enter a string: How much stack space will this string take?
  4865.    You entered: How much stack space will this string take?
  4866.    Bytes available on stack: 1902
  4867.  
  4868.  
  4869.  
  4870.  
  4871.  
  4872.  _arc Functions
  4873.  ────────────────────────────────────────────────────────────────────────────
  4874.  
  4875.  
  4876.  Description
  4877.  
  4878.  Draw elliptical arcs.
  4879.  
  4880.    #include <graph.h>
  4881.  
  4882.    short _far _arc( short x1, short y1, short x2, short y2, short x3,
  4883.      short y3, short x4, short y4 );
  4884.  
  4885.    short _far _arc_w( double x1, double y1, double x2, double y2, double x3,
  4886.      double y3, double x4, double y4 );
  4887.  
  4888.    short _far _arc_wxy( struct _wxycoord _far *pwxy1,
  4889.      struct _wxycoord _far *pwxy2,
  4890.      struct _wxycoord _far *pwxy3, struct _wxycoord _far *pwxy4);
  4891.  
  4892.  x1, y1                            Upper-left corner of bounding rectangle
  4893.  
  4894.  x2, y2                            Lower-right corner of bounding rectangle
  4895.  
  4896.  x3, y3                            Second point of start vector (center of
  4897.                                    bounding rectangle is first point)
  4898.  
  4899.  x4, y4                            Second point of end vector (center of
  4900.                                    bounding rectangle is first point)
  4901.  
  4902.  pwxy1                             Upper-left corner of bounding rectangle
  4903.  
  4904.  pwxy2                             Lower-right corner of bounding rectangle
  4905.  
  4906.  pwxy3                             Second point of start vector (center of
  4907.                                    bounding rectangle is first point)
  4908.  
  4909.  pwxy4                             Second point of end vector (center of
  4910.                                    bounding rectangle is first point)
  4911.  
  4912.  
  4913.  Remarks
  4914.  
  4915.  The _arc functions draw elliptical arcs. The center of the arc is the center
  4916.  of the bounding rectangle, which is defined by points (x1, y1) and (x2, y2)
  4917.  for _arc and _arc_w and by points pwxy1 and pwxy2 for _arc_wxy. The arc
  4918.  starts where it intersects an imaginary line extending from the center of
  4919.  the arc through (x3, y3) for _arc and _arc_w and through pwxy3 for _arc_wxy.
  4920.  It is drawn counterclockwise about the center of the arc, ending where it
  4921.  intersects an imaginary line extending from the center of the arc through
  4922.  (x4, y4) for _arc  and _arc_w and through pwxy4 for _arc_wxy.
  4923.  
  4924.  The _arc routine uses the view coordinate system. The _arc_w and _arc_wxy
  4925.  functions use the real-valued window coordinate system.
  4926.  
  4927.  In each case, the arc is drawn using the current color. Since an arc does
  4928.  not define a closed area, it is not filled.
  4929.  
  4930.  
  4931.  Return Value
  4932.  
  4933.  These functions return a nonzero value if the arc is successfully drawn;
  4934.  otherwise, they return 0.
  4935.  
  4936.  
  4937.  Compatibility
  4938.  
  4939.   ANSI   DOS   OS/2   UNIX   XENIX
  4940.  
  4941.  
  4942.  
  4943.  See Also
  4944.  
  4945.  _ellipse functions,  _lineto functions,  _pie functions,  _rectangle
  4946.  functions,  _setcolor
  4947.  
  4948.  
  4949.  Example
  4950.  
  4951.    /* ARC.C: This program draws a simple arc. */
  4952.  
  4953.    #include <graph.h>
  4954.    #include <stdlib.h>
  4955.    #include <conio.h>
  4956.  
  4957.    void main()
  4958.    {
  4959.       short x, y;
  4960.       struct xycoord xystart, xyend, xyfill;
  4961.  
  4962.       /* Find a valid graphics mode */
  4963.       if( !_setvideomode( _MAXRESMODE ) )
  4964.          exit( 1 );
  4965.  
  4966.       /* Draw arcs         */
  4967.       x = 100; y = 100;
  4968.       _arc( x - 60, y - 60, x, y, x - 30, y - 60, x - 60, y - 30 );
  4969.       _arc( x + 60, y + 60, x, y, x,      y + 30, x + 30, y );
  4970.  
  4971.       /* Get endpoints of second arc and enclose the figure, then fill it. */
  4972.       _getarcinfo( &xystart, &xyend, &xyfill );
  4973.       _moveto( xystart.xcoord, xystart.ycoord );
  4974.       _lineto( xyend.xcoord,   xyend.ycoord );
  4975.       _floodfill( xyfill.xcoord, xyfill.ycoord, _getcolor() );
  4976.  
  4977.       getch();
  4978.       _setvideomode( _DEFAULTMODE );
  4979.    }
  4980.  
  4981.  
  4982.  
  4983.  
  4984.  
  4985.  asctime
  4986.  ────────────────────────────────────────────────────────────────────────────
  4987.  
  4988.  
  4989.  Description
  4990.  
  4991.  Converts a tm time structure to a character string.
  4992.  
  4993.    #include <time.h>
  4994.  
  4995.    char *asctime( const struct tm *timeptr );
  4996.  
  4997.  timeptr                           Time/date structure
  4998.  
  4999.  
  5000.  Remarks
  5001.  
  5002.  The asctime function converts a time stored as a structure to a character
  5003.  string. The timeptr value is usually obtained from a call to gmtime or
  5004.  localtime, both of which return a pointer to a tm structure, defined in
  5005.  TIME.H. (See gmtime for a complete description of the tm structure fields.)
  5006.  
  5007.  
  5008.  The tm structure contains the following elements:
  5009.  
  5010.  Element                           Description
  5011.  ────────────────────────────────────────────────────────────────────────────
  5012.  int tm_sec                        Seconds after the minute (0 -59)
  5013.  
  5014.  int tm_min                        Minutes after the hour (0 -59)
  5015.  
  5016.  int tm_hour                       Hours since midnight (0 -23)
  5017.  
  5018.  int tm_mday                       Day of the month (0 -31)
  5019.  
  5020.  int tm_mon                        Months since January (0 -11)
  5021.  
  5022.  int tm_year                       Years since 1900
  5023.  
  5024.  int tm_wday                       Days since Sunday (0 - 6)
  5025.  
  5026.  int tm_yday                       Days since January 1 (0 -365)
  5027.  
  5028.  int tm_isdst                      Daylight-saving-time flag
  5029.  
  5030.  The string result produced by asctime contains exactly 26 characters and has
  5031.  the form of the following example:
  5032.  
  5033.    Wed Jan 02 02:03:55 1980\n\0
  5034.  
  5035.  A 24-hour clock is used. All fields have a constant width. The newline
  5036.  character (\n) and the null character ('\0') occupy the last two positions
  5037.  of the string. The asctime function uses a single statically allocated
  5038.  buffer to hold the return string. Each call to this routine destroys the
  5039.  result of the previous call.
  5040.  
  5041.  
  5042.  Return Value
  5043.  
  5044.  The asctime function returns a pointer to the character string result. There
  5045.  is no error return.
  5046.  
  5047.  
  5048.  Compatibility
  5049.  
  5050.   ANSI   DOS   OS/2   UNIX   XENIX
  5051.  
  5052.  
  5053.  
  5054.  See Also
  5055.  
  5056.  ctime, ftime, gmtime, localtime, time, tzset
  5057.  
  5058.  
  5059.  Example
  5060.  
  5061.    /* ASCTIME.C: This program places the system time in the long integer
  5062.    aclock,
  5063.     * translates it into the structure newtime and then converts it to
  5064.     * string form for output, using the asctime function.
  5065.     */
  5066.  
  5067.    #include <time.h>
  5068.    #include <stdio.h>
  5069.  
  5070.    struct tm *newtime;
  5071.    time_t aclock;
  5072.  
  5073.    void main()
  5074.    {
  5075.       time( &aclock );                    /* Get time in seconds */
  5076.  
  5077.       newtime = localtime( &aclock );     /* Convert time to struct tm form
  5078.  */
  5079.  
  5080.       /* Print local time as a string */
  5081.       printf( "The current date and time are: %s\n", asctime( newtime ) );
  5082.    }
  5083.  
  5084.  
  5085.  Output
  5086.  
  5087.  
  5088.  
  5089.    The current date and time are: Thu Jun 15 06:57:59 1989
  5090.  
  5091.  
  5092.  
  5093.  
  5094.  
  5095.  asin Functions
  5096.  ────────────────────────────────────────────────────────────────────────────
  5097.  
  5098.  
  5099.  Description
  5100.  
  5101.  Calculate the arcsine.
  5102.  
  5103.    #include <math.h>
  5104.  
  5105.    #include <errno.h>
  5106.  
  5107.    double asin( double x );
  5108.  
  5109.    long double asinl( long double x );
  5110.  
  5111.  x                                 Value whose arcsine is to be calculated
  5112.  
  5113.  
  5114.  Remarks
  5115.  
  5116.  The asin functions calculate the arcsine of x in the range -π/2 to π/2
  5117.  radians. The value of x must be between -1 and 1. The asinl function is the
  5118.  80-bit counterpart, which uses an 80-bit, 10-byte coprocessor form of
  5119.  arguments and return values. See the reference page on the long double
  5120.  functions for more details on this data type.
  5121.  
  5122.  
  5123.  Return Value
  5124.  
  5125.  The asin functions return the arcsine result. If x is less than -1 or
  5126.  greater than 1, asin sets errno to EDOM, prints a DOMAIN error message to
  5127.  stderr, and returns 0.
  5128.  
  5129.  Error handling can be modified by using the matherr (or _matherrl) routine.
  5130.  
  5131.  
  5132.  
  5133.  Compatibility
  5134.  
  5135.  asin
  5136.  
  5137.   ANSI   DOS   OS/2   UNIX   XENIX
  5138.  
  5139.  
  5140.  asinl
  5141.  
  5142.   ANSI   DOS   OS/2   UNIX   XENIX
  5143.  
  5144.  
  5145.  
  5146.  See Also
  5147.  
  5148.  acos functions, atan functions, cos functions, matherr, sin functions, tan
  5149.  functions
  5150.  
  5151.  
  5152.  Example
  5153.  
  5154.    /* ASINCOS.C: This program prompts for a value in the range -1 to 1.
  5155.     * Input values outside this range will produce DOMAIN error messages.
  5156.     * If a valid value is entered, the program prints the arcsine and the
  5157.     * arccosine of that value.
  5158.     */
  5159.  
  5160.    #include <math.h>
  5161.    #include <stdio.h>
  5162.    #include <stdlib.h>
  5163.    #include <errno.h>
  5164.  
  5165.    void main()
  5166.    {
  5167.       double x, y;
  5168.  
  5169.       printf( "Enter a real number between -1 and 1: " );
  5170.       scanf( "%lf", &x );
  5171.       y = asin( x );
  5172.       printf( "Arcsine of %f = %f\n", x, y );
  5173.       y = acos( x );
  5174.       printf( "Arccosine of %f = %f\n", x, y );
  5175.    }
  5176.  
  5177.  
  5178.  Output
  5179.  
  5180.  
  5181.  
  5182.    Enter a real number between -1 and 1: .32696
  5183.    Arcsine of 0.326960 = 0.333085
  5184.    Arccosine of 0.326960 = 1.237711
  5185.  
  5186.  
  5187.  
  5188.  
  5189.  
  5190.  assert
  5191.  ────────────────────────────────────────────────────────────────────────────
  5192.  
  5193.  
  5194.  Description
  5195.  
  5196.  Prints an error message and aborts the program.
  5197.  
  5198.  #include <assert.h>
  5199.  
  5200.  #include <stdio.h>
  5201.  
  5202.    void assert( int expression );
  5203.  
  5204.  expression                        C expression specifying assertion being
  5205.                                    tested
  5206.  
  5207.  
  5208.  Remarks
  5209.  
  5210.  The assert routine prints a diagnostic message and calls the abort routine
  5211.  if expression is false (0). The diagnostic message has the form
  5212.  
  5213.    Assertion failed: expression, file filename, line linenumber
  5214.  
  5215.  where filename is the name of the source file and linenumber is the line
  5216.  number of the assertion that failed in the source file. No action is taken
  5217.  if expression is true (nonzero).
  5218.  
  5219.  The assert routine is typically used in program development to identify
  5220.  program logic errors. The given expression should be chosen so that it holds
  5221.  true only if the program is operating as intended. After a program has been
  5222.  debugged, the special "no debug" identifier NDEBUG can be used to remove
  5223.  assert calls from the program. If NDEBUG is defined (by any value) with a /D
  5224.  command-line option or with a #define directive, the C preprocessor removes
  5225.  all assert calls from the program source.
  5226.  
  5227.  The assert routine is implemented as a macro.
  5228.  
  5229.  
  5230.  Return Value
  5231.  
  5232.  None.
  5233.  
  5234.  
  5235.  Compatibility
  5236.  
  5237.   ANSI   DOS   OS/2   UNIX   XENIX
  5238.  
  5239.  
  5240.  
  5241.  See Also
  5242.  
  5243.  abort, raise, signal
  5244.  
  5245.  
  5246.  Example
  5247.  
  5248.    /* ASSERT.C: In this program, the analyze_string function uses the
  5249.     * assert function to test several conditions related to string and
  5250.     * length. If any of the conditions fails, the program prints a
  5251.     * message indicating what caused the failure.
  5252.     */
  5253.  
  5254.    #include <stdio.h>
  5255.    #include <assert.h>
  5256.    #include <string.h>
  5257.  
  5258.    void analyze_string( char *string );   /* Prototype */
  5259.  
  5260.    void main()
  5261.    {
  5262.       char  test1[] = "abc", *test2 = NULL, test3[] = "";
  5263.  
  5264.       printf ( "Analyzing string '%s'\n", test1 );
  5265.       analyze_string( test1 );
  5266.       printf ( "Analyzing string '%s'\n", test2 );
  5267.       analyze_string( test2 );
  5268.       printf ( "Analyzing string '%s'\n", test3 );
  5269.       analyze_string( test3 );
  5270.    }
  5271.  
  5272.    /* Tests a string to see if it is NULL, empty, or longer than 0 characters
  5273.  */
  5274.    void analyze_string( char * string )
  5275.    {
  5276.       assert( string != NULL );        /* Cannot be NULL */
  5277.       assert( *string != '\0' );       /* Cannot be empty */
  5278.       assert( strlen( string ) > 2 );  /* Length must be greater than 2 */
  5279.    }
  5280.  
  5281.  
  5282.  Output
  5283.  
  5284.  
  5285.  
  5286.    Analyzing string 'abc'
  5287.    Analyzing string '(null)'
  5288.    Assertion failed: string != NULL, file assert.c, line 28
  5289.  
  5290.    abnormal program termination
  5291.  
  5292.  
  5293.  
  5294.  
  5295.  
  5296.  atan Functions
  5297.  ────────────────────────────────────────────────────────────────────────────
  5298.  
  5299.  
  5300.  Description
  5301.  
  5302.  Calculate the arctangent of x (atan and atanl) and the arctangent of y/x
  5303.  (atan2 and atan2l).
  5304.  
  5305.    #include <math.h>
  5306.  
  5307.    double atan( double x );
  5308.  
  5309.    double atan2( double y, double x );
  5310.  
  5311.    long double atanl( long double x );
  5312.  
  5313.    long double atan2l( long double y, long double x );
  5314.  
  5315.  x, y                              Any number
  5316.  
  5317.  
  5318.  Remarks
  5319.  
  5320.  The atan family of functions calculates the arctangent of x, and the atan2
  5321.  family of functions calculates the arctangent of y/x. The atan group returns
  5322.  a value in the range -π/2 to π/2 radians, and the atan2 group returns a
  5323.  value in the range -π toπ radians. The atan2 functions use the signs of both
  5324.  arguments to determine the quadrant of the return value.
  5325.  
  5326.  
  5327.  Return Value
  5328.  
  5329.  The atan family of functions returns the arctangent result. If both
  5330.  arguments of atan2 or atan2l are 0, the function sets errno to EDOM, prints
  5331.  a DOMAIN error message to stderr, and returns 0.
  5332.  
  5333.  Error handling can be modified by using the matherr (or _matherrl) routine.
  5334.  
  5335.  
  5336.  
  5337.  Compatibility
  5338.  
  5339.  atan, atan2
  5340.  
  5341.   ANSI   DOS   OS/2   UNIX   XENIX
  5342.  
  5343.  
  5344.  atanl, atan2l
  5345.  
  5346.   ANSI   DOS   OS/2   UNIX   XENIX
  5347.  
  5348.  
  5349.  
  5350.  See Also
  5351.  
  5352.  acos functions, asin functions, cos functions, matherr, sin functions, tan
  5353.  functions
  5354.  
  5355.  
  5356.  Example
  5357.  
  5358.    /* ATAN.C: This program calculates the arctangent of 1 and -1. */
  5359.  
  5360.    #include <math.h>
  5361.    #include <stdio.h>
  5362.    #include <errno.h>
  5363.  
  5364.    void main()
  5365.    {
  5366.       double x1, x2, y;
  5367.  
  5368.       printf( "Enter a real number: " );
  5369.       scanf( "%lf", &x1 );
  5370.       y = atan( x1 );
  5371.       printf( "Arctangent of %f: %f\n", x1, y );
  5372.       printf( "Enter a second real number: " );
  5373.       scanf( "%lf", &x2 );
  5374.       y = atan2( x1, x2 );
  5375.       printf( "Arctangent of %f / %f: %f\n", x1, x2, y );
  5376.    }
  5377.  
  5378.  
  5379.  Output
  5380.  
  5381.  
  5382.  
  5383.    Enter a real number: -862.42
  5384.    Arctangent of -862.420000: -1.569637
  5385.    Enter a second real number: 78.5149
  5386.    Arctangent of -862.420000 / 78.514900: -1.480006
  5387.  
  5388.  
  5389.  
  5390.  
  5391.  
  5392.  atexit
  5393.  ────────────────────────────────────────────────────────────────────────────
  5394.  
  5395.  
  5396.  Description
  5397.  
  5398.  Processes the specified function at exit.
  5399.  
  5400.  #include <stdlib.h>               Required only for function declarations
  5401.  
  5402.    int atexit( void ( *func )( void ) );
  5403.  
  5404.  func                              Function to be called
  5405.  
  5406.  
  5407.  Remarks
  5408.  
  5409.  The atexit function is passed the address of a function (func) to be called
  5410.  when the program terminates normally. Successive calls to atexit create a
  5411.  register of functions that are executed in LIFO (last-in-first-out) order.
  5412.  No more than 32 functions can be registered with atexit or onexit. The
  5413.  functions passed to atexit cannot take parameters.
  5414.  
  5415.  All routines passed to atexit should have the _loadds attribute if used in
  5416.  multithread dynamic-link libraries.
  5417.  
  5418.  
  5419.  Return Value
  5420.  
  5421.  The atexit function returns 0 if it is successful, or a nonzero value if an
  5422.  error occurs (e.g., if there are already 32 exit functions defined).
  5423.  
  5424.  
  5425.  Compatibility
  5426.  
  5427.   ANSI   DOS   OS/2   UNIX   XENIX
  5428.  
  5429.  
  5430.  Use the ANSI-standard atexit function (rather than the similar onexit
  5431.  function) whenever ANSI portability is desired.
  5432.  
  5433.  In the OS/2 environment, the atexit function calls the OS/2 function
  5434.  DosExitList.
  5435.  
  5436.  
  5437.  See Also
  5438.  
  5439.  abort, exit, _exit, onexit
  5440.  
  5441.  
  5442.  Example
  5443.  
  5444.    /* ATEXIT.C: This program pushes four functions onto the stack of
  5445.    functions
  5446.     * to be executed when atexit is called. When the program exits, these
  5447.     * programs are executed on a "last in, first out" basis.
  5448.     */
  5449.  
  5450.    #include <stdlib.h>
  5451.    #include <stdio.h>
  5452.  
  5453.    void fn1( void ), fn2( void ), fn3( void ), fn4( void );
  5454.  
  5455.    void main()
  5456.    {
  5457.       atexit( fn1 );
  5458.       atexit( fn2 );
  5459.       atexit( fn3 );
  5460.       atexit( fn4 );
  5461.       printf( "This is executed first.\n" );
  5462.    }
  5463.  
  5464.    void fn1()
  5465.    {
  5466.       printf( "next.\n" );
  5467.    }
  5468.  
  5469.    void fn2()
  5470.    {
  5471.       printf( "executed " );
  5472.    }
  5473.  
  5474.    void fn3()
  5475.    {
  5476.       printf( "is " );
  5477.    }
  5478.  
  5479.    void fn4()
  5480.    {
  5481.       printf( "This " );
  5482.    }
  5483.  
  5484.  
  5485.  Output
  5486.  
  5487.  
  5488.  
  5489.    This is executed first.
  5490.    This is executed next.
  5491.  
  5492.  
  5493.  
  5494.  
  5495.  
  5496.  atof, atoi, atol, _atold
  5497.  ────────────────────────────────────────────────────────────────────────────
  5498.  
  5499.  
  5500.  Description
  5501.  
  5502.  Convert strings to double (atof), long double (_atold) integer (atoi), or
  5503.  long (atol).
  5504.  
  5505.  #include <math.h>                 atof, _atold
  5506.  
  5507.  #include <stdlib.h>               atof, _atold, atoi, atol
  5508.  
  5509.    double atof( const char *string );
  5510.  
  5511.    long double _atold( const char *string );
  5512.  
  5513.    int atoi( const char *string );
  5514.  
  5515.    long atol( const char *string );
  5516.  
  5517.  string                            String to be converted
  5518.  
  5519.  
  5520.  Remarks
  5521.  
  5522.  These functions convert a character string to a double-precision
  5523.  floating-point value (atof), an integer value (atoi), a long integer value
  5524.  (atol), or a long double value (_atold). The input string is a sequence of
  5525.  characters that can be interpreted as a numerical value of the specified
  5526.  type.
  5527.  
  5528.  The string size that can be handled by the atof or _atold function is
  5529.  limited to 100 characters.
  5530.  
  5531.  The function stops reading the input string at the first character that it
  5532.  cannot recognize as part of a number. This character may be the null
  5533.  character ('\0') terminating the string.
  5534.  
  5535.  The atof and _atold functions expect string to have the following form:
  5536.  
  5537.    [[whitespace]] [[{sign}]] [[ IK0digits]] [[.digits]]
  5538.    [[{d| D | e | E}[[sign]digits]]
  5539.  
  5540.  A whitespace consists of space and/or tab characters, which are ignored;
  5541.  sign is either plus (+) or minus (-); and digits are one or more decimal
  5542.  digits. If no digits appear before the decimal point, at least one must
  5543.  appear after the decimal point. The decimal digits may be followed by an
  5544.  exponent, which consists of an introductory letter (d, D, e, or E) and an
  5545.  optionally signed decimal integer.
  5546.  
  5547.  The atoi and atol functions do not recognize decimal points or exponents.
  5548.  The string argument for these functions has the form
  5549.  
  5550.    [[whitespace]] [[sign]]digits
  5551.  
  5552.  where whitespace, sign, and digits are exactly as described above for atof.
  5553.  
  5554.  
  5555.  
  5556.  Return Value
  5557.  
  5558.  Each function returns the double, long double, int, or long value produced
  5559.  by interpreting the input characters as a number. The return value is 0 (for
  5560.  atoi), 0L (for atol), and 0.0 (for atof and _atold) if the input cannot be
  5561.  converted to a value of that type. The return value is undefined in case of
  5562.  overflow.
  5563.  
  5564.  
  5565.  Compatibility
  5566.  
  5567.  atof, atoi, atol
  5568.  
  5569.   ANSI   DOS   OS/2   UNIX   XENIX
  5570.  
  5571.  
  5572.  _atold
  5573.  
  5574.   ANSI   DOS   OS/2   UNIX   XENIX
  5575.  
  5576.  
  5577.  
  5578.  See Also
  5579.  
  5580.  ecvt, fcvt, gcvt
  5581.  
  5582.  
  5583.  Example
  5584.  
  5585.    /* ATOF.C: This program shows how numbers stored as strings can be
  5586.     * converted to numeric values using the atof, atoi, and atol functions.
  5587.     */
  5588.  
  5589.    #include <stdlib.h>
  5590.    #include <stdio.h>
  5591.  
  5592.    void main()
  5593.    {
  5594.       char *s; double x; int i; long l;
  5595.  
  5596.       s = "  -2309.12E-15";    /* Test of atof */
  5597.       x = atof( s );
  5598.       printf( "atof test:  ASCII string: %s\tfloat:     %e\n", s, x );
  5599.  
  5600.       s = "7.8912654773d210";  /* Test of atof */
  5601.       x = atof( s );
  5602.       printf( "atof test:  ASCII string: %s\tfloat:     %e\n", s, x );
  5603.  
  5604.       s = "  -9885 pigs";      /* Test of atoi */
  5605.       i = atoi( s );
  5606.       printf( "atoi test:  ASCII string: %s\t\tinteger: %d\n", s, i );
  5607.  
  5608.       s = "98854 dollars";     /* Test of atol */
  5609.       l = atol( s );
  5610.       printf( "atol test:  ASCII string: %s\t\tlong:    %ld\n", s, l );
  5611.    }
  5612.  
  5613.  
  5614.  Output
  5615.  
  5616.  
  5617.  
  5618.    atof test:  ASCII string:   -2309.12E-15        float:     -2.309120e-012
  5619.    atof test:  ASCII string: 7.8912654773d210      float:     7.891265e+210
  5620.    atoi test:  ASCII string:   -9885 pigs          integer: -9885
  5621.    atol test:  ASCII string: 98854 dollars         long:    98854
  5622.  
  5623.  
  5624.  
  5625.  
  5626.  
  5627.  bdos
  5628.  ────────────────────────────────────────────────────────────────────────────
  5629.  
  5630.  
  5631.  Description
  5632.  
  5633.  Invokes the DOS system call.
  5634.  
  5635.  #include <dos.h>
  5636.  
  5637.    int bdos( int dosfunc, unsigned int dosdx, unsigned int dosal );
  5638.  
  5639.  dosfunc                           Function number
  5640.  
  5641.  dosdx                             DX register value
  5642.  
  5643.  dosal                             AL register value
  5644.  
  5645.  
  5646.  Remarks
  5647.  
  5648.  The bdos function invokes the DOS system call specified by dosfunc after
  5649.  placing the values specified by dosdx and dosal in the DX and AL registers,
  5650.  respectively. The bdos function executes an INT 21H instruction to invoke
  5651.  the system call. When the system call is complete, bdos returns the contents
  5652.  of the AX register.
  5653.  
  5654.  The bdos function is intended to be used to invoke DOS system calls that
  5655.  either take no arguments or take arguments only in the DX (DH, DL) and/or AL
  5656.  registers.
  5657.  
  5658.  Do not use the bdos function to call interrupts that modify the DS register.
  5659.  Instead, use the intdosx or int86x function. The intdosx and int86x
  5660.  functions load the DS and ES registers from the segregs parameter and also
  5661.  store the DS and ES registers into segregs after the function call.
  5662.  
  5663.  This call should not be used to invoke system calls that indicate errors by
  5664.  setting the carry flag. Since C programs do not have access to this flag,
  5665.  your program cannot determine whether the return value is an error code. The
  5666.  intdos function should be used in these cases.
  5667.  
  5668.  
  5669.  Return Value
  5670.  
  5671.  The bdos function returns the value of the AX register after the system call
  5672.  has completed.
  5673.  
  5674.  
  5675.  Compatibility
  5676.  
  5677.   ANSI   DOS   OS/2   UNIX   XENIX
  5678.  
  5679.  
  5680.  
  5681.  See Also
  5682.  
  5683.  intdos, intdosx
  5684.  
  5685.  
  5686.  Example
  5687.  
  5688.    /* BDOS.C: This example calls DOS function 0x9 (display string)
  5689.     * to display a $-terminated string.
  5690.     */
  5691.  
  5692.    #include <dos.h>
  5693.  
  5694.    /* Function 0x09 assumes that DS will contain segment of the string.
  5695.     * This will be true for all memory models if the string is declared near.
  5696.     */
  5697.    char _near str[] = "Hello world!\r\n$";
  5698.  
  5699.    void main()
  5700.    {
  5701.       /* Offset of string must be in DX, segment in DS. AL is not needed,
  5702.        * so 0 is used.
  5703.        */
  5704.       bdos( 0x09, (int)str, 0 );
  5705.    }
  5706.  
  5707.  
  5708.  Output
  5709.  
  5710.  
  5711.  
  5712.    Hello world!
  5713.  
  5714.  
  5715.  
  5716.  
  5717.  
  5718.  _beginthread
  5719.  ────────────────────────────────────────────────────────────────────────────
  5720.  
  5721.  
  5722.  Description
  5723.  
  5724.  Begins thread in OS/2 process.
  5725.  
  5726.  #include <process.h>              Multithread version of PROCESS.H
  5727.  
  5728.  #include <stddef.h>               Declaration of threadid variable
  5729.  
  5730.    int _far  _beginthread( void( _far *start_address )( void _far * ),
  5731.    void  _far *stack_bottom, unsigned stack_size, void  _far *arglist );
  5732.  
  5733.  start_address                     Starting address
  5734.  
  5735.  stack_bottom                      Address of the thread stack
  5736.  
  5737.  stack_size                        Stack size for thread
  5738.  
  5739.  arglist                           Argument list for thread
  5740.  
  5741.  
  5742.  Remarks
  5743.  
  5744.  The _beginthread function creates a thread that begins execution of a far
  5745.  routine at start_address. When the thread returns from that far routine, it
  5746.  is terminated automatically. The user can also terminate the thread by
  5747.  calling _endthread.
  5748.  
  5749.  The address of the thread stack is given by stack_bottom. If stack_bottom is
  5750.  set to NULL, the run-time library code will allocate and deallocate the
  5751.  thread stack as needed. Since the _beginthread function can determine the
  5752.  current status of all thread IDs, it can free the old stack and allocate a
  5753.  new stack whenever a thread is reused.
  5754.  
  5755.  If it is not NULL, the stack_bottom argument must specify a word address,
  5756.  and the stack must be at least as long as specified by the stack_size
  5757.  argument. Usually this memory is either a global array or memory returned by
  5758.  malloc or _fmalloc.
  5759.  
  5760.  The stack_size argument must be even and nonzero.
  5761.  
  5762.  If you are writing multithread programs that make C run-time calls from
  5763.  child threads, be sure to allocate a sufficiently large stack. For example,
  5764.  the C function printf requires more than 500 bytes of stack space. To be
  5765.  safe, allocate at least 2,048 bytes for a thread's stack. (If your child
  5766.  thread makes no run-time calls, stack space is generally not a problem.)
  5767.  
  5768.  As a general rule, you should have 2K of stack space free when calling any
  5769.  API (Applications Program Interface) routine (e.g., OS/2 system calls).
  5770.  
  5771.  The arglist is a parameter, the size of a far pointer, to be passed to the
  5772.  newly created thread. Typically it is the address of a data item, such as a
  5773.  character string, to be passed to the new thread. The arglist may be NULL if
  5774.  not needed, but _beginthread should be provided with some value to pass to
  5775.  the child thread.
  5776.  
  5777.  All threads will be terminated if any thread calls abort, exit, _exit, or
  5778.  DosExit. A good practice in multithread programming is to make the first
  5779.  thread the main thread and wait until other threads have terminated before
  5780.  exiting the program.
  5781.  
  5782.  The OS/2 function DosCreateThread should not be called directly to create
  5783.  threads. The _beginthread function performs initialization procedures
  5784.  required to call other C run-time library functions safely.
  5785.  
  5786.  
  5787.  Return Value
  5788.  
  5789.  The function returns the thread identification number of the new thread, if
  5790.  successful. A return value of -1 indicates an error, and errno is set to one
  5791.  of the following values:
  5792.  
  5793.  Value                             Meaning
  5794.  ────────────────────────────────────────────────────────────────────────────
  5795.  EAGAIN                            Too many threads
  5796.  
  5797.  EINVAL                            Invalid argument, "bad stack"
  5798.  
  5799.  
  5800.  Compatibility
  5801.  
  5802.   ANSI   DOS   OS/2   UNIX   XENIX
  5803.  
  5804.  
  5805.  
  5806.  See Also
  5807.  
  5808.  _endthread
  5809.  
  5810.  
  5811.  Example
  5812.  
  5813.    /* BEGTHRD.C illustrates multiple threads using functions:
  5814.     *      _beginthread            _endthread
  5815.     *
  5816.     * Also the global variable:
  5817.     *      _threadid
  5818.     *
  5819.     * This program requires the multithread library. For example, compile
  5820.     * with the following command line:
  5821.     *      CL /MT THREADS.C
  5822.     */
  5823.  
  5824.    #define INCL_NOCOMMON
  5825.    #define INCL_NOPM
  5826.    #define INCL_DOSPROCESS
  5827.    #define INCL_VIO
  5828.    #include <os2.h>
  5829.    #include <process.h>    /* _beginthread, _endthread */
  5830.    #include <stddef.h>     /* _threadid                */
  5831.    #include <stdlib.h>
  5832.    #include <conio.h>
  5833.  
  5834.    void Bounce( int c );       /* Prototypes */
  5835.    void CheckKey( void *dummy );
  5836.  
  5837.    /* GetRandom returns a random integer between min and max. */
  5838.    #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) +
  5839.  (min))
  5840.  
  5841.    #define STACK_SIZE   4096
  5842.  
  5843.    BOOL repeat = TRUE;         /* Global repeat flag and video variable */
  5844.    VIOMODEINFO vmi = { sizeof( VIOMODEINFO ) };
  5845.  
  5846.    void main()
  5847.    {
  5848.        PCHAR   stack;
  5849.        CHAR    ch = 'A';
  5850.  
  5851.        /* Get display screen's text row and column information. */
  5852.        VioGetMode( &vmi, 0 );
  5853.  
  5854.        /* Launch CheckKey thread to check for terminating keystroke. */
  5855.        _beginthread( CheckKey, NULL, STACK_SIZE, NULL );
  5856.  
  5857.        /* Loop until CheckKey terminates program. */
  5858.        while( repeat )
  5859.        {
  5860.            /* On first loops, launch character threads. */
  5861.            _beginthread( Bounce, NULL, STACK_SIZE, (void *)ch++ );
  5862.  
  5863.            /* Wait one second between loops. */
  5864.            DosSleep( 1000L );
  5865.        }
  5866.    }
  5867.  
  5868.    /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
  5869.    void CheckKey( void *dummy )
  5870.    {
  5871.        getch();
  5872.        repeat = 0;      /* _endthread implied */
  5873.    }
  5874.  
  5875.    /* Bounce - Thread to create and control a colored letter that moves
  5876.     * around on the screen.
  5877.     *
  5878.     * Params: ch - the letter to be moved
  5879.     */
  5880.    void Bounce( int ch )
  5881.    {
  5882.        /* Generate letter and color attribute from thread argument. */
  5883.        char      blankcell[2] = { 0x20, 0x07 };
  5884.        char      blockcell[2] = { ch , (ch % 16) + 1 };
  5885.        int       xold, xcur, yold, ycur;
  5886.        BOOL      first = TRUE;
  5887.  
  5888.    /* Seed random number generator and get initial location. */
  5889.        srand( *_threadid );
  5890.        xcur = GetRandom( 0, vmi.col - 1 );
  5891.        ycur = GetRandom( 0, vmi.row - 1 );
  5892.        while( repeat )
  5893.        {
  5894.            /* Pause between loops. */
  5895.            DosSleep( 100L );
  5896.  
  5897.            /* Blank out our old position on the screen, and draw new letter.
  5898.  */
  5899.            if( first )
  5900.                first = FALSE;
  5901.            else
  5902.                VioWrtCellStr( blankcell, 2, yold, xold, 0 );
  5903.            VioWrtCellStr( blockcell, 2, ycur, xcur, 0 );
  5904.  
  5905.            /* Increment the coordinate for next placement of the block. */
  5906.            xold = xcur;
  5907.            yold = ycur;
  5908.            xcur += GetRandom( -1, 1 );
  5909.            ycur += GetRandom( -1, 1 );
  5910.  
  5911.            /* Correct placement (and beep) if about to go off the screen. */
  5912.            if( xcur < 0 )
  5913.                xcur = 1;
  5914.            else if( xcur == vmi.col )
  5915.                xcur = vmi.col - 2;
  5916.            else if( ycur < 0 )
  5917.                ycur = 1;
  5918.            else if( ycur == vmi.row )
  5919.                ycur = vmi.row - 2;
  5920.  
  5921.            /* If not at screen border, continue, otherwise beep. */
  5922.            else
  5923.                continue;
  5924.            DosBeep( (ch - 'A') * 100, 175 );
  5925.        }
  5926.        /* _endthread given (but not really needed) to terminate. */
  5927.        _endthread();
  5928.    }
  5929.  
  5930.  
  5931.  
  5932.  
  5933.  
  5934.  Bessel Functions
  5935.  ────────────────────────────────────────────────────────────────────────────
  5936.  
  5937.  
  5938.  Description
  5939.  
  5940.  
  5941.  Compute the Bessel function.
  5942.  
  5943.  #include <math.h>
  5944.  
  5945.    double j0( double x );
  5946.  
  5947.    double j1( double x );
  5948.  
  5949.    double jn( int n, double x );
  5950.  
  5951.    double y0( double x );
  5952.  
  5953.    double y1( double x );
  5954.  
  5955.    double yn( int n, double x );
  5956.  
  5957.    long double _j0l( long double x );
  5958.  
  5959.    long double _jnl( int n, long double x );
  5960.  
  5961.    long double _j1l( long double x );
  5962.  
  5963.    long double _y0l( long double x );
  5964.  
  5965.    long double _y1l( long double x );
  5966.  
  5967.    long double _ynl( int n, long double x );
  5968.  
  5969.  x                                 Floating-point value
  5970.  
  5971.  n                                 Integer order
  5972.  
  5973.  
  5974.  Remarks
  5975.  
  5976.  The j0, j1, and jn routines return Bessel functions of the first kind─orders
  5977.  0, 1, and n, respectively.
  5978.  
  5979.  The y0, y1, and yn routines return Bessel functions of the second
  5980.  kind─orders 0, 1, and n, respectively. The argument x must be positive.
  5981.  
  5982.  The long double versions of these functions are the 80-bit counterparts and
  5983.  use the 80-bit, 10-byte coprocessor form of arguments and return values. See
  5984.  the reference page on the long double functions for more details on this
  5985.  data type.
  5986.  
  5987.  The Bessel functions are explained more fully in most mathematics reference
  5988.  books, such as the Handbook of Mathematical Functions (Abramowitz and
  5989.  Stegun; Washington: U.S. Government Printing Office, 1964). These functions
  5990.  are commonly used in the mathematics of electromagnetic wave theory.
  5991.  
  5992.  
  5993.  Return Value
  5994.  
  5995.  These functions return the result of a Bessel function of x.
  5996.  
  5997.  For y0, y1, or yn, if x is negative, the routine sets errno to EDOM, prints
  5998.  a DOMAIN error message to stderr, and returns -HUGE_VAL.
  5999.  
  6000.  Error handling can be modified by using the matherr (or _matherrl) routine.
  6001.  
  6002.  
  6003.  
  6004.  Compatibility
  6005.  
  6006.  j0, j1, jn, y0, y1, yn
  6007.  
  6008.   ANSI   DOS   OS/2   UNIX   XENIX
  6009.  
  6010.  
  6011.  _j0l,  _j1l,  _jnl,  _y0l,  _y1l,  _ynl
  6012.  
  6013.   ANSI   DOS   OS/2   UNIX   XENIX
  6014.  
  6015.  
  6016.  
  6017.  See Also
  6018.  
  6019.  matherr
  6020.  
  6021.  
  6022.  Example
  6023.  
  6024.    /* BESSEL.C: This program illustrates Bessel functions, including:
  6025.     *      j0          j1          jn          y0          y1          yn
  6026.     */
  6027.  
  6028.    #include <math.h>
  6029.    #include <stdio.h>
  6030.  
  6031.    void main()
  6032.    {
  6033.        double x = 2.387;
  6034.        int n = 3, c;
  6035.  
  6036.        printf( "Bessel functions for x = %f:\n", x );
  6037.        printf( "  Kind\t\tOrder\t\Function\tResult\n\n" );
  6038.        printf( "  First\t\t0\tj0( x )\t\t%f\n", j0( x ) );
  6039.        printf( "  First\t\t1\tj1( x )\t\t%f\n", j1( x ) );
  6040.        for( c = 2; c < 5; c++ )
  6041.            printf( "  First\t\t%d\tjn( n, x )\t%f\n", c, jn( c, x ) );
  6042.  
  6043.        printf( "  Second\t0\ty0( x )\t\t%f\n", y0( x ) );
  6044.        printf( "  Second\t1\ty1( x )\t\t%f\n", y1( x ) );
  6045.        for( c = 2; c < 5; c++ )
  6046.            printf( "  Second\t%d\tyn( n, x )\t%f\n", c, yn( c, x ) );
  6047.    }
  6048.  
  6049.  
  6050.  Output
  6051.  
  6052.  
  6053.  
  6054.    Bessel functions for x = 2.387000:
  6055.      Kind          Order   Function        Result
  6056.  
  6057.      First         0       j0( x )         0.009288
  6058.      First         1       j1( x )         0.522941
  6059.      First         2       jn( n, x )      0.428870
  6060.      First         3       jn( n, x )      0.195734
  6061.      First         4       jn( n, x )      0.063131
  6062.      Second        0       y0( x )         0.511681
  6063.      Second        1       y1( x )         0.094374
  6064.      Second        2       yn( n, x )      -0.432608
  6065.      Second        3       yn( n, x )      -0.819314
  6066.      Second        4       yn( n, x )      -1.626833
  6067.  
  6068.  
  6069.  
  6070.  
  6071.  
  6072.  _bfreeseg
  6073.  ────────────────────────────────────────────────────────────────────────────
  6074.  
  6075.  
  6076.  Description
  6077.  
  6078.  Frees a specified based heap.
  6079.  
  6080.  #include <malloc.h>               Required only for function declarations
  6081.  
  6082.    int _bfreeseg( _segment seg );
  6083.  
  6084.  seg                               Segment selected
  6085.  
  6086.  
  6087.  Remarks
  6088.  
  6089.  The _bfreeseg function frees a based heap. The seg argument is a based heap
  6090.  returned by an earlier call to _bheapseg. It specifies the based heap to be
  6091.  freed.
  6092.  
  6093.  The number of bytes freed is the number of bytes specified when the block
  6094.  was allocated. After the call, the freed heap is again available for
  6095.  allocation.
  6096.  
  6097.  
  6098.  Return Value
  6099.  
  6100.  The _bfreeseg function returns 0 if successful and -1 in the case of an
  6101.  error.
  6102.  
  6103.  
  6104.  Compatibility
  6105.  
  6106.   ANSI   DOS   OS/2   UNIX   XENIX
  6107.  
  6108.  
  6109.  
  6110.  See Also
  6111.  
  6112.  _bheapseg, calloc functions, free functions, malloc functions, realloc
  6113.  functions
  6114.  
  6115.  
  6116.  Example
  6117.  
  6118.    /* BHEAPSEG.C: This program C illustrates dynamic allocation of based
  6119.     * memory using functions _bheapseg, _bfreeseg, _bmalloc, and _bfree.
  6120.     */
  6121.  
  6122.    #include <stdio.h>
  6123.    #include <malloc.h>
  6124.    #include <stdlib.h>
  6125.    #include <string.h>
  6126.  
  6127.    void main()
  6128.    {
  6129.        _segment seg;
  6130.        char _based( seg ) *outstr, _based( seg ) *instr;
  6131.        char _based( seg ) *pout,   _based( seg ) *pin;
  6132.        char tmpstr[80];
  6133.        int  len;
  6134.  
  6135.        printf( "Enter a string: " );
  6136.        gets( tmpstr );
  6137.  
  6138.        /* Request a based heap. Use based so that memory won't be taken from
  6139.         * near heap.
  6140.         */
  6141.        if( (seg = _bheapseg( 1000 )) == _NULLSEG )
  6142.            exit( 1 );
  6143.  
  6144.        /* Allocate based memory for two strings. */
  6145.        len = strlen( tmpstr );
  6146.        if( ((instr  = _bmalloc( seg, len + 1 )) == _NULLOFF) ||
  6147.            ((outstr = _bmalloc( seg, len + 1 )) == _NULLOFF) )
  6148.            exit( 1 );
  6149.  
  6150.        /* Copy a lowercased string to dynamic memory. The based memory is
  6151.         * far when addressed as a whole.
  6152.         */
  6153.        _fstrlwr( _fstrcpy( (char _far *)instr, (char _far *)tmpstr ) );
  6154.  
  6155.        /* Copy input string to output string in reversed order. When reading
  6156.         * and writing individual characters from a based heap, the compiler
  6157.  will
  6158.         * try to process them as near, thus speeding up the processing.
  6159.         */
  6160.        for( pin = instr + len - 1, pout = outstr;
  6161.                    pout < outstr + len; pin--, pout++ )
  6162.            *pout = *pin;
  6163.        *pout = '\0';
  6164.  
  6165.        /* Display strings. Again strings as a whole are far. */
  6166.        printf( "Input:  %Fs\n", (char _far *)instr );
  6167.        printf( "Output: %Fs\n", (char _far *)outstr );
  6168.  
  6169.        /* Free blocks and release based heap. */
  6170.        _bfree( seg, instr );
  6171.        _bfree( seg, outstr );
  6172.        _bfreeseg( seg );
  6173.    }
  6174.  
  6175.  
  6176.  Output
  6177.  
  6178.  
  6179.  
  6180.    Enter a string: Was I god
  6181.    Input:  was i god
  6182.    Output: dog i saw
  6183.  
  6184.  
  6185.  
  6186.  
  6187.  
  6188.  _bheapseg
  6189.  ────────────────────────────────────────────────────────────────────────────
  6190.  
  6191.  
  6192.  Description
  6193.  
  6194.  Allocates a based heap.
  6195.  
  6196.  #include <malloc.h>               Required only for function declarations
  6197.  
  6198.    _segment _bheapseg( size_t size );
  6199.  
  6200.  size                              Segment size to allocate
  6201.  
  6202.  
  6203.  Remarks
  6204.  
  6205.  The _bheapseg function allocates a based-heap segment of at least size
  6206.  bytes. (The block may be larger than size bytes because of space required
  6207.  for alignment and for maintenance information.)
  6208.  
  6209.  The heap code will try to enlarge the heap as necessary. If the original
  6210.  block of memory is depleted (e.g., by calls to _bmalloc and _brealloc), the
  6211.  run-time code will try to enlarge the heap as necessary.
  6212.  
  6213.  The value returned by _bheapseg is the identifier of the based-heap segment.
  6214.  This value should be saved and used in subsequent calls to other based-heap
  6215.  functions.
  6216.  
  6217.  The _bheapseg function can be called repeatedly. For each call, the C
  6218.  library will allocate a new based-heap segment.
  6219.  
  6220.  
  6221.  Return Value
  6222.  
  6223.  The _bheapseg function returns the newly allocated segment selector that the
  6224.  user must save for use in subsequent based-heap functions. A return value of
  6225.  -1 indicates failure.
  6226.  
  6227.  Always check the return from the _bheapseg function (especially when it is
  6228.  used in real mode), even if the amount of memory requested is small.
  6229.  
  6230.  
  6231.  Compatibility
  6232.  
  6233.   ANSI   DOS   OS/2   UNIX   XENIX
  6234.  
  6235.  
  6236.  
  6237.  See Also
  6238.  
  6239.  calloc functions, free functions, malloc functions, realloc functions
  6240.  
  6241.  
  6242.  Example
  6243.  
  6244.    /* BHEAPSEG.C: This program C illustrates dynamic allocation of based
  6245.     * memory using functions _bheapseg, _bfreeseg, _bmalloc, and _bfree.
  6246.     */
  6247.  
  6248.    #include <stdio.h>
  6249.    #include <malloc.h>
  6250.    #include <stdlib.h>
  6251.    #include <string.h>
  6252.  
  6253.    void main()
  6254.    {
  6255.        _segment seg;
  6256.        char _based( seg ) *outstr, _based( seg ) *instr;
  6257.        char _based( seg ) *pout,   _based( seg ) *pin;
  6258.        char tmpstr[80];
  6259.        int  len;
  6260.  
  6261.        printf( "Enter a string: " );
  6262.        gets( tmpstr );
  6263.  
  6264.        /* Request a based heap. Use based so that memory won't be taken from
  6265.         * near heap.
  6266.         */
  6267.        if( (seg = _bheapseg( 1000 )) == _NULLSEG )
  6268.            exit( 1 );
  6269.  
  6270.        /* Allocate based memory for two strings. */
  6271.        len = strlen( tmpstr );
  6272.        if( ((instr  = _bmalloc( seg, len + 1 )) == _NULLOFF) ||
  6273.            ((outstr = _bmalloc( seg, len + 1 )) == _NULLOFF) )
  6274.            exit( 1 );
  6275.  
  6276.        /* Copy a lowercased string to dynamic memory. The based memory is
  6277.         * far when addressed as a whole.
  6278.         */
  6279.        _fstrlwr( _fstrcpy( (char _far *)instr, (char _far *)tmpstr ) );
  6280.  
  6281.        /* Copy input string to output string in reversed order. When reading
  6282.         * and writing individual characters from a based heap, the compiler
  6283.  will
  6284.         * try to process them as near, thus speeding up the processing.
  6285.         */
  6286.        for( pin = instr + len - 1, pout = outstr;
  6287.                    pout < outstr + len; pin--, pout++ )
  6288.            *pout = *pin;
  6289.        *pout = '\0';
  6290.  
  6291.        /* Display strings. Again, strings as a whole are far. */
  6292.        printf( "Input:  %Fs\n", (char _far *)instr );
  6293.        printf( "Output: %Fs\n", (char _far *)outstr );
  6294.  
  6295.        /* Free blocks and release based heap. */
  6296.        _bfree( seg, instr );
  6297.        _bfree( seg, outstr );
  6298.        _bfreeseg( seg );
  6299.    }
  6300.  
  6301.  
  6302.  Output
  6303.  
  6304.  
  6305.  
  6306.    Enter a string: Was I god
  6307.    Input:  was i god
  6308.    Output: dog i saw
  6309.  
  6310.  
  6311.  
  6312.  
  6313.  
  6314.  _bios_disk
  6315.  ────────────────────────────────────────────────────────────────────────────
  6316.  
  6317.  
  6318.  Description
  6319.  
  6320.  Calls BIOS disk services using system call INT 0x13.
  6321.  
  6322.  #include <bios.h>
  6323.  
  6324.    unsigned _bios_disk( unsigned service, struct diskinfo_t  *diskinfo );
  6325.  
  6326.  service                           Disk function desired
  6327.  
  6328.  diskinfo                          Disk parameters
  6329.  
  6330.  
  6331.  Remarks
  6332.  
  6333.  The _bios_disk routine uses system call INT 0x13 to provide several
  6334.  disk-access functions. The service parameter selects the function desired,
  6335.  while the diskinfo structure provides the necessary parameters. Note that
  6336.  the low-level disk operations allowed by the _bios_disk routine are very
  6337.  dangerous to use because they allow direct manipulation of the disk.
  6338.  
  6339.  The diskinfo structure provides the following parameters:
  6340.  
  6341.  Element                           Description
  6342.  ────────────────────────────────────────────────────────────────────────────
  6343.  unsigned drive                    Drive number
  6344.  
  6345.  unsigned head                     Head number
  6346.  
  6347.  unsigned track                    Track number
  6348.  
  6349.  unsigned sector                   Starting sector number
  6350.  
  6351.  unsigned nsectors                 Number of sectors to read, write, or
  6352.                                    compare
  6353.  
  6354.  void far *buffer                  Memory location to write to, read from,
  6355.                                    or compare
  6356.  
  6357.  The service argument can be set to one of the following manifest constants:
  6358.  
  6359.  
  6360.  Constant                          Function
  6361.  ────────────────────────────────────────────────────────────────────────────
  6362.  _DISK_FORMAT                      Formats the track specified by diskinfo.
  6363.                                    The head and track fields indicate the
  6364.                                    track to format. Only one track can be
  6365.                                    formatted in a single call. The buffer
  6366.                                    field points to a set of sector markers.
  6367.                                    The format of the markers depends on the
  6368.                                    type of disk drive; see a technical
  6369.                                    reference to the PC BIOS to determine
  6370.                                    the marker format. There is no return
  6371.                                    value.
  6372.  
  6373.  _DISK_READ                        Reads one or more disk sectors into
  6374.                                    memory. This service uses all fields of
  6375.                                    the structure pointed to by diskinfo, as
  6376.                                    defined earlier in this section. If no
  6377.                                    error occurs, the function returns 0 in
  6378.                                    the high-order byte and the number of
  6379.                                    sectors read in the low-order byte. If
  6380.                                    there is an error, the high-order byte
  6381.                                    will contain a set of status flags. If
  6382.                                    there is an error, the high-order byte
  6383.                                    will contain a set of status flags, as
  6384.                                    defined under _DISK_READ. Status is
  6385.                                    returned in the 8 high-order bits of the
  6386.                                    return value, as listed below:
  6387.  
  6388.                                    Bits          Meaning
  6389.  ────────────────────────────────────────────────────────────────────────────
  6390.                                    0x01**        Invalid request or a bad
  6391.                                    command
  6392.  
  6393.                                    0x02**        Address mark not found
  6394.  
  6395.                                    0x04**        Sector not found
  6396.  
  6397.                                    0x05**        Reset failed
  6398.  
  6399.                                    0x07**        Drive parameter activity
  6400.                                    failed
  6401.  
  6402.                                    0x09**        Direct Memory Access (DMA)
  6403.                                    overrun
  6404.  
  6405.                                    0x0A**        Bad sector flag detected
  6406.  
  6407.                                    0x10**        Data read (ECC) error
  6408.  
  6409.                                    0x11**        Corrected data read (ECC)
  6410.                                    error
  6411.  
  6412.                                    0x20**        Controller failure
  6413.  
  6414.                                    0x40**        Seek error
  6415.  
  6416.                                    0x80**        Disk timed out or failed
  6417.                                    to respond
  6418.  
  6419.                                    0xAA**        Drive not ready
  6420.  
  6421.                                    0xBB**        Undefined error
  6422.  
  6423.                                    0xCC**        Write fault on drive
  6424.  
  6425.                                    0xE0**        Status error
  6426.  
  6427.  _DISK_RESET                       Forces the disk controller to do a hard
  6428.                                    reset, preparing for floppy-disk I/O.
  6429.                                    This is useful after an error occurs in
  6430.                                    another operation, such as a read. If
  6431.                                    this service is specified, the
  6432.                                    diskinfo argument is ignored.
  6433.  
  6434.  _DISK_STATUS                      Obtains the status of the last disk
  6435.                                    operation. If this service is specified,
  6436.                                    the diskinfo argument is ignored.
  6437.  
  6438.  _DISK_VERIFY                      Checks the disk to be sure the specified
  6439.                                    sectors exist and can be read. It also
  6440.                                    runs a CRC (cyclic redundancy check)
  6441.                                    test. This service uses all fields
  6442.                                    (except buffer) of the structure pointed
  6443.                                    to by diskinfo, as defined earlier in
  6444.                                    this section. If no error occurs, the
  6445.                                    function returns 0 in the high-order
  6446.                                    byte and the number of sectors compared
  6447.                                    in the low-order byte. If there is an
  6448.                                    error, the high-order byte will contain
  6449.                                    a set of status flags, as defined under
  6450.                                    _DISK_READ (above).
  6451.  
  6452.  _DISK_WRITE                       Writes data from memory to one or more
  6453.                                    disk sectors. This service uses all
  6454.                                    fields of the structure pointed to by
  6455.                                    diskinfo, as defined earlier in this
  6456.                                    section. If no error occurs, the
  6457.                                    function returns 0 in the high-order
  6458.                                    byte and the number of sectors written
  6459.                                    in the low-order byte. If there is an
  6460.                                    error, the high-order byte will contain
  6461.                                    a set of status flags, as defined under
  6462.                                    _DISK_READ (above).
  6463.  
  6464.  
  6465.  Return Value
  6466.  
  6467.  The _bios_disk function returns the value in the AX register after the BIOS
  6468.  interrupt.
  6469.  
  6470.  
  6471.  Compatibility
  6472.  
  6473.   ANSI   DOS   OS/2   UNIX   XENIX
  6474.  
  6475.  
  6476.  
  6477.  Example
  6478.  
  6479.    /* BDISK.C: This program first attempts to verify a disk by using an
  6480.     * invalid disk head number. After printing the return value error code,
  6481.     * the program verifies the disk by using a valid disk head code.
  6482.     */
  6483.  
  6484.    #include <conio.h>
  6485.    #include <stdio.h>
  6486.    #include <bios.h>
  6487.  
  6488.    void main()
  6489.    {
  6490.       unsigned status = 0;
  6491.       struct diskinfo_t disk_info;
  6492.  
  6493.       disk_info.drive    = 0;
  6494.       disk_info.head     = 10;   /* Invalid head number */
  6495.       disk_info.track    = 1;
  6496.       disk_info.sector   = 2;
  6497.       disk_info.nsectors = 8;
  6498.  
  6499.    printf( "Insert disk in drive A: and press any key\n" );
  6500.       getch();
  6501.       status = _bios_disk( _DISK_VERIFY, &disk_info );
  6502.       printf( "Return value: 0x%.4x\n", status );
  6503.       if( status & 0xff00 )      /* Error if high byte is 0 */
  6504.          printf( "Seek error\n" );
  6505.       else
  6506.          printf( "No seek error\n" );
  6507.  
  6508.       printf( "Press any key\n" );
  6509.       getch();
  6510.       disk_info.head = 0;        /* Valid head number */
  6511.       status = _bios_disk( _DISK_VERIFY, &disk_info );
  6512.       printf( "Return value: 0x%.4x\n", status );
  6513.       if( status & 0xff00 )      /* Error if high byte is 0 */
  6514.          printf( "Seek error\n" );
  6515.       else
  6516.          printf( "No seek error\n" );
  6517.    }
  6518.  
  6519.  
  6520.  Output
  6521.  
  6522.  
  6523.  
  6524.    Insert disk in drive A: and press any key
  6525.    Return value: 0x0400
  6526.    Seek error
  6527.    Press any key
  6528.    Return value: 0x0008
  6529.    No seek error
  6530.  
  6531.  
  6532.  
  6533.  
  6534.  
  6535.  _bios_equiplist
  6536.  ────────────────────────────────────────────────────────────────────────────
  6537.  
  6538.  
  6539.  Description
  6540.  
  6541.  Calls BIOS equipment-list service, using system call INT 0x11.
  6542.  
  6543.  #include <bios.h>
  6544.  
  6545.    unsigned _bios_equiplist( void );
  6546.  
  6547.  
  6548.  Remarks
  6549.  
  6550.  The _bios_equiplist routine uses system call INT 0x11 to determine what
  6551.  hardware and peripherals are currently installed on the machine.
  6552.  
  6553.  
  6554.  Return Value
  6555.  
  6556.  The function returns a set of bits indicating what is installed, as defined
  6557.  below:
  6558.  
  6559.  Bits                              Meaning
  6560.  ────────────────────────────────────────────────────────────────────────────
  6561.  0                                 Any disk drive installed if true
  6562.  
  6563.  1                                 True (1) if math coprocessor installed
  6564.  
  6565.  2 -3                              System RAM in 16K blocks (16-64K)
  6566.  
  6567.  4 -5                              Initial video mode
  6568.  
  6569.  6 -7                              Number of floppy-disk drives installed
  6570.                                    (00 = 1,  01 = 2, etc.)
  6571.  
  6572.  8                                 False (0) if and only if a Direct Memory
  6573.                                    Access (DMA) chip is installed
  6574.  
  6575.  9 -11                             Number of RS232 serial ports installed
  6576.  
  6577.  12                                True (1) if and only if a game adapter
  6578.                                    is installed
  6579.  
  6580.  13                                True (1) if and only if an internal
  6581.                                    modem is installed
  6582.  
  6583.  14 -15                            Number of printers installed
  6584.  
  6585.  
  6586.  Compatibility
  6587.  
  6588.   ANSI   DOS   OS/2   UNIX   XENIX
  6589.  
  6590.  
  6591.  
  6592.  Example
  6593.  
  6594.    /* BEQUIPLI.C: This program checks for the presence of diskettes. */
  6595.  
  6596.    #include <bios.h>
  6597.    #include <stdio.h>
  6598.  
  6599.    void main()
  6600.    {
  6601.       unsigned equipment;
  6602.  
  6603.       equipment = _bios_equiplist();
  6604.       printf( "Equipment bits: 0x%.4x\n", equipment );
  6605.       if( equipment & 0x1000 )      /* Check for game adapter bit */
  6606.          printf( "Game adapter installed\n" );
  6607.       else
  6608.          printf( "No game adapter installed\n" );
  6609.    }
  6610.  
  6611.  
  6612.  Output
  6613.  
  6614.  
  6615.  
  6616.    Equipment bits: 0x4061
  6617.    No game adapter installed
  6618.  
  6619.  
  6620.  
  6621.  
  6622.  
  6623.  _bios_keybrd
  6624.  ────────────────────────────────────────────────────────────────────────────
  6625.  
  6626.  
  6627.  Description
  6628.  
  6629.  Calls BIOS keyboard services, using INT 0x16.
  6630.  
  6631.  #include <bios.h>
  6632.  
  6633.    unsigned _bios_keybrd( unsigned service );
  6634.  
  6635.  service                           Keyboard function desired
  6636.  
  6637.  
  6638.  Remarks
  6639.  
  6640.  The _bios_keybrd routine uses system call INT 0x16 to access the keyboard
  6641.  services. The service argument can be any of the following manifest
  6642.  constants:
  6643.  
  6644.  Constant                          Meaning
  6645.  ────────────────────────────────────────────────────────────────────────────
  6646.  _KEYBRD_READ,                     Reads the next character from the
  6647.  _NKEYBRD_READ                     keyboard. If no character has been
  6648.                                    typed, the call will wait for one. If
  6649.                                    the low-order byte of the return value
  6650.                                    is nonzero, the call contains the ASCII
  6651.                                    value of the character typed. The
  6652.                                    high-order byte contains the keyboard
  6653.                                    scan code for the character. The
  6654.                                    _NKEYBRD_READ constant is used with
  6655.                                    enhanced keyboards to obtain the scan
  6656.                                    codes for function keys F11 and F12 and
  6657.                                    the cursor control keys.
  6658.  
  6659.  _KEYBRD_READY,                    Checks whether a keystroke is waiting to
  6660.  _NKEYBRD_READY                    be read and, if so, reads it. The return
  6661.                                    value is 0 if no keystroke is waiting,
  6662.                                    or it is the character waiting to be
  6663.                                    read, in the same format as the
  6664.                                    _KEYBRD_READ or  _NKEYBRD_READY return.
  6665.                                    This service does not remove the waiting
  6666.                                    character from the input buffer, as does
  6667.                                    the _KEYBRD_READ or _NKEYBRD_READ
  6668.                                    service.  The _NKEYBRD_READY constant is
  6669.                                    used with enhanced keyboards to obtain
  6670.                                    the scan codes for function keys F11 and
  6671.                                    F12 and the cursor control keys.
  6672.  
  6673.  _KEYBRD_SHIFTSTATUS,              Returns the current SHIFT-key status.
  6674.  _NKEYBRD_SHIFTSTATUS              Only the low-order byte of the return
  6675.                                    value is affected. The
  6676.                                    _NKEYBRD_SHIFTSTATUS constant is used to
  6677.                                    get a full 16-bit status value. Any
  6678.                                    combination of the following bits may be
  6679.                                    set:
  6680.  
  6681.                                    Bit         Meaning if True
  6682.  ────────────────────────────────────────────────────────────────────────────
  6683.                                    00H         Rightmost SHIFT key pressed
  6684.  
  6685.                                    01H         Leftmost SHIFT key pressed
  6686.  
  6687.                                    02H         Either CTRL key pressed
  6688.  
  6689.                                    3H          Either ALT key pressed
  6690.  
  6691.                                    04H         SCROLL LOCK on
  6692.  
  6693.                                    05H         NUM LOCK on
  6694.  
  6695.                                    06H         CAPS LOCK on
  6696.  
  6697.                                    07H         In insert mode (INS)
  6698.  
  6699.                                    08H         Left CTRL key pressed
  6700.  
  6701.                                    09H         Left ALT key pressed
  6702.  
  6703.                                    0AH         Right CTRL key pressed
  6704.  
  6705.                                    0BH         Right ALT key pressed
  6706.  
  6707.                                    0CH         SCROLL LOCK key pressed
  6708.  
  6709.                                    0DH         NUM LOCK key pressed
  6710.  
  6711.                                    0EH         CAPS LOCK key pressed
  6712.  
  6713.                                    0FH         SYS REQ key pressed
  6714.  
  6715.  
  6716.  Return Value
  6717.  
  6718.  With the ...READ and ...SHIFTSTATUS arguments, the _bios_keybrd function
  6719.  returns the contents of the AX register after the BIOS call.
  6720.  
  6721.  With the ...READY argument, _bios_keybrd returns 0 if there is no key. If
  6722.  there is a key, _bios_keybrd returns the key waiting to be read (i.e. the
  6723.  same value as _KEYBRD_READ).
  6724.  
  6725.  With the ...READ and the ...READY arguments, the _bios_keybrd function
  6726.  returns -1 if CTRL+BREAK has been pressed and is the next keystroke to be
  6727.  read.
  6728.  
  6729.  
  6730.  Compatibility
  6731.  
  6732.   ANSI   DOS   OS/2   UNIX   XENIX
  6733.  
  6734.  
  6735.  
  6736.  Example
  6737.  
  6738.    /* BKEYBRD.C: This program prints a message on the screen until the
  6739.     * right SHIFT key is pressed.
  6740.     */
  6741.  
  6742.    #include <bios.h>
  6743.    #include <stdio.h>
  6744.  
  6745.    void main()
  6746.    {
  6747.       while( !(_bios_keybrd( _KEYBRD_SHIFTSTATUS ) & 0001) )
  6748.          printf( "Use the right SHIFT key to stop this message\n" );
  6749.       printf( "Right SHIFT key pressed\n" );
  6750.    }
  6751.  
  6752.  
  6753.  Output
  6754.  
  6755.  
  6756.  
  6757.    Use the right SHIFT key to stop this message
  6758.    Use the right SHIFT key to stop this message
  6759.    Use the right SHIFT key to stop this message
  6760.    Use the right SHIFT key to stop this message
  6761.    Right SHIFT key pressed
  6762.  
  6763.  
  6764.  
  6765.  
  6766.  
  6767.  _bios_memsize
  6768.  ────────────────────────────────────────────────────────────────────────────
  6769.  
  6770.  
  6771.  Description
  6772.  
  6773.  Calls the BIOS memory-size service, using system call INT 0x12.
  6774.  
  6775.  #include <bios.h>
  6776.  
  6777.    unsigned _bios_memsize( void );
  6778.  
  6779.  
  6780.  Remarks
  6781.  
  6782.  The _bios_memsize routine uses system call INT 0x12 to determine the total
  6783.  amount of main memory installed.
  6784.  
  6785.  
  6786.  Return Value
  6787.  
  6788.  The routine returns the total amount of installed memory in 1K blocks. The
  6789.  maximum return value is 640, representing 640K of main memory.
  6790.  
  6791.  
  6792.  Compatibility
  6793.  
  6794.   ANSI   DOS   OS/2   UNIX   XENIX
  6795.  
  6796.  
  6797.  
  6798.  Example
  6799.  
  6800.    /* BMEMSIZE.C: This program displays the amount of memory installed. */
  6801.  
  6802.    #include <bios.h>
  6803.    #include <stdio.h>
  6804.  
  6805.    void main()
  6806.    {
  6807.       unsigned memory;
  6808.  
  6809.       memory = _bios_memsize();
  6810.       printf ( "The amount of memory installed is: %dK\n", memory );
  6811.    }
  6812.  
  6813.  
  6814.  Output
  6815.  
  6816.  
  6817.  
  6818.    The amount of memory installed is: 639K
  6819.  
  6820.  
  6821.  
  6822.  
  6823.  
  6824.  _bios_printer
  6825.  ────────────────────────────────────────────────────────────────────────────
  6826.  
  6827.  
  6828.  Description
  6829.  
  6830.  Calls BIOS printer services using system call INT 0x17.
  6831.  
  6832.  #include <bios.h>
  6833.  
  6834.    unsigned _bios_printer( unsigned service, unsigned printer, unsigned data
  6835.    );
  6836.  
  6837.  service                           Printer function desired
  6838.  
  6839.  printer                           Target printer port
  6840.  
  6841.  data                              Output data
  6842.  
  6843.  
  6844.  Remarks
  6845.  
  6846.  The _bios_printer routine uses system call INT 0x17 to perform printer
  6847.  output services for parallel printers. The printer argument specifies the
  6848.  affected printer, where 0 is LPT1, 1 is LPT2, and so forth.
  6849.  
  6850.  Some printers do not support the full set of signals. As a result, the "Out
  6851.  of Paper" condition, for example, may not be returned to your program.
  6852.  
  6853.  The service argument can be any of the following manifest constants:
  6854.  
  6855.  Constant                          Meaning
  6856.  ────────────────────────────────────────────────────────────────────────────
  6857.  _PRINTER_INIT                     Initializes the selected printer. The
  6858.                                    data argument is ignored. The return
  6859.                                    value is the low-order status byte
  6860.                                    defined below.
  6861.  
  6862.  _PRINTER_STATUS                   Returns the printer status in the
  6863.                                    low-order status byte defined below. The
  6864.                                    data argument is ignored.
  6865.  
  6866.  _PRINTER_WRITE                    Sends the low-order byte of data to the
  6867.                                    printer specified by printer. The
  6868.                                    low-order byte of the return value
  6869.                                    indicates the printer status after the
  6870.                                    operation, as defined below:
  6871.  
  6872.                                    Bit      Meaning if True
  6873.  ────────────────────────────────────────────────────────────────────────────
  6874.                                    0         Printer timed out
  6875.  
  6876.                                    1         Not used
  6877.  
  6878.                                    2         Not used
  6879.  
  6880.                                    3         I/O error
  6881.  
  6882.                                    4         Printer selected
  6883.  
  6884.                                    5         Out of paper
  6885.  
  6886.                                    6         Acknowledge
  6887.  
  6888.                                    7         Printer not busy
  6889.  
  6890.  
  6891.  
  6892.  
  6893.  Return Value
  6894.  
  6895.  The _bios_printer function returns the value in the AX register after the
  6896.  BIOS interrupt.
  6897.  
  6898.  
  6899.  Compatibility
  6900.  
  6901.   ANSI   DOS   OS/2   UNIX   XENIX
  6902.  
  6903.  
  6904.  
  6905.  Example
  6906.  
  6907.    /* BPRINTER.C: This program checks the status of the printer attached to
  6908.     * LPT1 when it is off line, then initializes the printer.
  6909.     */
  6910.  
  6911.    #include <bios.h>
  6912.    #include <conio.h>
  6913.    #include <stdio.h>
  6914.  
  6915.    #define LPT1 0
  6916.  
  6917.    void main()
  6918.    {
  6919.       unsigned status;
  6920.  
  6921.       printf ( "Place printer off line and press any key\n" );
  6922.       getch();
  6923.  
  6924.       status = _bios_printer( _PRINTER_STATUS, LPT1, 0 );
  6925.       printf( "Status with printer off line: 0x%.4x\n\n", status );
  6926.       printf( "Put the printer on line and then\n" );
  6927.       printf( "Press any key to initialize printer\n" );
  6928.       getch();
  6929.  
  6930.       status = _bios_printer( _PRINTER_INIT, LPT1, 0 );
  6931.       printf( "Status after printer initialized: 0x%.4x\n", status );
  6932.    }
  6933.  
  6934.  
  6935.  Output
  6936.  
  6937.  
  6938.  
  6939.    Place printer off line and press any key
  6940.    Status with printer off line: 0x0018
  6941.  
  6942.    Put the printer on line and then
  6943.    Press any key to initialize printer
  6944.    Status after printer initialized: 0x0090
  6945.  
  6946.  
  6947.  
  6948.  
  6949.  
  6950.  _bios_serialcom
  6951.  ────────────────────────────────────────────────────────────────────────────
  6952.  
  6953.  
  6954.  Description
  6955.  
  6956.  Calls BIOS communications services, using system call INT 0x14.
  6957.  
  6958.  #include <bios.h>
  6959.  
  6960.    unsigned _bios_serialcom( unsigned service, unsigned serial_port, unsigned
  6961.    data );
  6962.  
  6963.  service                           Communications service
  6964.  
  6965.  serial_port                       Serial port to use
  6966.  
  6967.  data                              Port configuration bits
  6968.  
  6969.  
  6970.  Remarks
  6971.  
  6972.  The _bios_serialcom routine uses system call INT 0x14 to provide serial
  6973.  communications services. The serial_port argument is set to 0 for COM1, to 1
  6974.  for COM2, and so on.
  6975.  
  6976.  The _bios_serialcom routine may not be able to establish reliable
  6977.  communications at baud rates in excess of 1,200 baud ( _COM_1200) due to the
  6978.  overhead associated with servicing computer interrupts. Faster data
  6979.  communication rates are possible with more direct programming of serial-port
  6980.  controllers. See C Programmer's Guide to Serial Communications for more
  6981.  details on serial-communications programming in C.
  6982.  
  6983.  The service argument can be set to one of the following manifest constants:
  6984.  
  6985.  
  6986.  Constant                          Service
  6987.  ────────────────────────────────────────────────────────────────────────────
  6988.  _COM_INIT                         Sets the port to the parameters
  6989.                                    specified in the data argument
  6990.  
  6991.  _COM_SEND                         Transmits the data characters over the
  6992.                                    selected serial port
  6993.  
  6994.  _COM_RECEIVE                      Accepts an input character from the
  6995.                                    selected serial port
  6996.  
  6997.  _COM_STATUS                       Returns the current status of the
  6998.                                    selected serial port
  6999.  
  7000.  The data argument is ignored if service is set to _COM_RECEIVE or
  7001.  _COM_STATUS. The data argument for _COM_INIT is created by combining (with
  7002.  the OR operator) one or more of the following constants:
  7003.  
  7004.  Constant                          Meaning
  7005.  ────────────────────────────────────────────────────────────────────────────
  7006.  _COM_CHR7                         7 data bits
  7007.  
  7008.  _COM_CHR8                         8 data bits
  7009.  
  7010.  _COM_STOP1                        1 stop bit
  7011.  
  7012.  _COM_STOP2                        2 stop bits
  7013.  
  7014.  _COM_NOPARITY                     No parity
  7015.  
  7016.  _COM_EVENPARITY                   Even parity
  7017.  
  7018.  _COM_ODDPARITY                    Odd parity
  7019.  
  7020.  _COM_110                          110 baud
  7021.  
  7022.  _COM_150                          150 baud
  7023.  
  7024.  _COM_300                          300 baud
  7025.  
  7026.  _COM_600                          600 baud
  7027.  
  7028.  _COM_1200                         1,200 baud
  7029.  
  7030.  _COM_2400                         2,400 baud
  7031.  
  7032.  _COM_4800                         4,800 baud
  7033.  
  7034.  _COM_9600                         9,600 baud
  7035.  
  7036.  The default value of data is 1 stop bit, no parity, and 110 baud.
  7037.  
  7038.  
  7039.  Return Value
  7040.  
  7041.  The function returns a 16-bit integer whose high-order byte contains status
  7042.  bits. The meaning of the low-order byte varies, depending on the service
  7043.  value. The high-order bits have the following meanings:
  7044.  
  7045.  Bit                               Meaning if Set
  7046.  ────────────────────────────────────────────────────────────────────────────
  7047.  15                                Timed out
  7048.  
  7049.  14                                Transmission-shift register empty
  7050.  
  7051.  13                                Transmission-hold register empty
  7052.  
  7053.  12                                Break detected
  7054.  
  7055.  11                                Framing error
  7056.  
  7057.  10                                Parity error
  7058.  
  7059.  9                                 Overrun error
  7060.  
  7061.  8                                 Data ready
  7062.  
  7063.  When service is _COM_SEND, bit 15 will be set if data could not be sent.
  7064.  
  7065.  When service is _COM_RECEIVE, the byte read will be returned in the
  7066.  low-order bits if the call is successful. If an error occurs, any of the
  7067.  bits 9, 10, 11, or 15 will be set.
  7068.  
  7069.  When service is _COM_INIT or _COM_STATUS, the low-order bits are defined as
  7070.  follows:
  7071.  
  7072.  Bit                               Meaning if Set
  7073.  ────────────────────────────────────────────────────────────────────────────
  7074.  7                                 Receive-line signal detected
  7075.  
  7076.  6                                 Ring indicator
  7077.  
  7078.  5                                 Data set ready
  7079.  
  7080.  4                                 Clear to send
  7081.  
  7082.  3                                 Change in receive-line signal detected
  7083.  
  7084.  2                                 Trailing-edge ring indicator
  7085.  
  7086.  1                                 Change in data-set-ready status
  7087.  
  7088.  0                                 Change in clear-to-send status
  7089.  
  7090.  Note that this function works only with IBM personal computers and true
  7091.  compatibles.
  7092.  
  7093.  
  7094.  Compatibility
  7095.  
  7096.   ANSI   DOS   OS/2   UNIX   XENIX
  7097.  
  7098.  
  7099.  
  7100.  Example
  7101.  
  7102.    /* BSERIALC.C: This program checks the status of serial port COM1. */
  7103.  
  7104.    #include <bios.h>
  7105.    #include <stdio.h>
  7106.  
  7107.    void main()
  7108.    {
  7109.       unsigned com1_status;
  7110.  
  7111.       com1_status =  _bios_serialcom( _COM_STATUS, 0, 0 );
  7112.       printf ( "COM1 status: 0x%.4x\n", com1_status );
  7113.    }
  7114.  
  7115.  
  7116.  Output
  7117.  
  7118.  
  7119.  
  7120.    COM1 status: 0x6000
  7121.  
  7122.  
  7123.  
  7124.  
  7125.  
  7126.  _bios_timeofday
  7127.  ────────────────────────────────────────────────────────────────────────────
  7128.  
  7129.  
  7130.  Description
  7131.  
  7132.  Calls BIOS time and date services, using system call INT 0x1A.
  7133.  
  7134.  #include <bios.h>
  7135.  
  7136.    unsigned _bios_timeofday( unsigned service, long *timeval );
  7137.  
  7138.  service                           Time function desired
  7139.  
  7140.  timeval                           Clock count
  7141.  
  7142.  
  7143.  Remarks
  7144.  
  7145.  The _bios_timeofday routine uses system call INT 0x1A to get or set the
  7146.  clock count. The service argument can be either of the following manifest
  7147.  constants:
  7148.  
  7149.  Constant                          Meaning
  7150.  ────────────────────────────────────────────────────────────────────────────
  7151.  _TIME_GETCLOCK                    Copies the current value of the clock
  7152.                                    count to the location pointed to by
  7153.                                    timeval. If midnight has not passed
  7154.                                    since the last time the system clock was
  7155.                                    read or set, the function returns 0;
  7156.                                    otherwise, the function returns 1.
  7157.  
  7158.  _TIME_SETCLOCK                    Sets the current value of the system
  7159.                                    clock to the value in the location
  7160.                                    pointed to by timeval. There is no
  7161.                                    return value.
  7162.  
  7163.  
  7164.  Return Value
  7165.  
  7166.  The _bios_timeofday function returns the value in the AX register after the
  7167.  BIOS interrupt.
  7168.  
  7169.  
  7170.  Compatibility
  7171.  
  7172.   ANSI   DOS   OS/2   UNIX   XENIX
  7173.  
  7174.  
  7175.  
  7176.  Example
  7177.  
  7178.    /* BTIMEOFD.C: This program gets the current system clock count before and
  7179.    after
  7180.     * a "do-nothing" loop and displays the difference.
  7181.     */
  7182.  
  7183.    #include <bios.h>
  7184.    #include <stdio.h>
  7185.  
  7186.    void main()
  7187.    {
  7188.       long i, begin_tick, end_tick;
  7189.  
  7190.       _bios_timeofday( _TIME_GETCLOCK, &begin_tick );
  7191.       printf( "Beginning tick count: %lu\n", begin_tick );
  7192.       for( i = 1; i <= 900000; i++ )
  7193.          ;
  7194.       _bios_timeofday( _TIME_GETCLOCK, &end_tick );
  7195.       printf( "Ending tick count:    %lu\n", end_tick );
  7196.       printf( "Elapsed ticks:        %lu\n", end_tick - begin_tick );
  7197.    }
  7198.  
  7199.  
  7200.  Output
  7201.  
  7202.  
  7203.  
  7204.    Beginning tick count: 1114255
  7205.    Ending tick count:    1114287
  7206.    Elapsed ticks:        32
  7207.  
  7208.  
  7209.  
  7210.  
  7211.  
  7212.  bsearch
  7213.  ────────────────────────────────────────────────────────────────────────────
  7214.  
  7215.  
  7216.  Description
  7217.  
  7218.  Performs binary search of a sorted array.
  7219.  
  7220.  #include <stdlib.h>               Required for ANSI compatibility
  7221.  
  7222.  #include <search.h>               Required only for function declarations
  7223.  
  7224.    void *bsearch( const void *key, const void *base, size_t num, size_t
  7225.    width,
  7226.    int ( *compare )( const void *elem1, const void *elem2 ) );
  7227.  
  7228.  key                               Object to search for
  7229.  
  7230.  base                              Pointer to base of search data
  7231.  
  7232.  num                               Number of elements
  7233.  
  7234.  width                             Width of elements
  7235.  
  7236.  compare                           Function that compares two elements:
  7237.                                    elem1 and elem2
  7238.  
  7239.  elem1                             Pointer to the key for the search
  7240.  
  7241.  elem2                             Pointer to the array element to be
  7242.                                    compared with the key
  7243.  
  7244.  
  7245.  Remarks
  7246.  
  7247.  The bsearch function performs a binary search of a sorted array of num
  7248.  elements, each of width bytes in size. The base value is a pointer to the
  7249.  base of the array to be searched, and key is the value being sought.
  7250.  
  7251.  The compare argument is a pointer to a user-supplied routine that compares
  7252.  two array elements and returns a value specifying their relationship. The
  7253.  bsearch function calls the compare routine one or more times during the
  7254.  search, passing pointers to two array elements on each call. The routine
  7255.  compares the elements, then returns one of the following values:
  7256.  
  7257.  Value                             Meaning
  7258.  ────────────────────────────────────────────────────────────────────────────
  7259.  < 0                               elem1 less than elem2
  7260.  
  7261.  = 0                               elem1 identical to elem2
  7262.  
  7263.  > 0                               elem1 greater than elem2
  7264.  
  7265.  If the array you are searching is not in ascending sort order, bsearch does
  7266.  not work properly. If the array contains duplicate records with identical
  7267.  keys, there is no way to predict which of the duplicate records will be
  7268.  located by bsearch.
  7269.  
  7270.  
  7271.  Return Value
  7272.  
  7273.  The bsearch function returns a pointer to the first occurrence of key in the
  7274.  array pointed to by base. If key is not found, the function returns NULL.
  7275.  
  7276.  
  7277.  Compatibility
  7278.  
  7279.   ANSI   DOS   OS/2   UNIX   XENIX
  7280.  
  7281.  
  7282.  
  7283.  See Also
  7284.  
  7285.  lfind, lsearch, qsort
  7286.  
  7287.  
  7288.  Example
  7289.  
  7290.    /* BSEARCH.C: This program reads the command-line arguments, sorting them
  7291.     * with qsort, and then uses bsearch to find the word "cat."
  7292.     */
  7293.  
  7294.    #include <search.h>
  7295.    #include <string.h>
  7296.    #include <stdio.h>
  7297.  
  7298.    int compare( char **arg1, char **arg2 );  /* Declare a function for
  7299.  compare */
  7300.  
  7301.    void main( int argc, char **argv )
  7302.    {
  7303.  
  7304.       char **result;
  7305.       char *key = "cat";
  7306.       int i;
  7307.  
  7308.       /* Sort using Quicksort algorithm: */
  7309.       qsort( (char *)argv, argc, sizeof( char * ), compare );
  7310.  
  7311.       for( i = 0; i < argc; ++i )        /* Output sorted list */
  7312.          printf( "%s ", argv[i] );
  7313.  
  7314.       /*  Find the word "cat" using a binary search algorithm: */
  7315.       result = (char **)bsearch( (char *) &key, (char *)argv, argc,
  7316.                                  sizeof( char * ), compare );
  7317.       if( result )
  7318.          printf( "\n%s found at %Fp\n", *result, result );
  7319.       else
  7320.          printf( "\nCat not found!\n" );
  7321.    }
  7322.  
  7323.    int compare( char **arg1, char **arg2 )
  7324.    {
  7325.       /* Compare all of both strings: */
  7326.       return strcmpi( *arg1, *arg2 );
  7327.    }
  7328.  
  7329.  
  7330.  Output
  7331.  
  7332.  
  7333.  
  7334.    [C:\LIBREF] bsearch dog pig horse cat human rat cow goat
  7335.    bsearch cat cow dog goat horse human pig rat
  7336.    cat found at 0292:0FD0
  7337.  
  7338.  
  7339.  
  7340.  
  7341.  
  7342.  cabs, cabsl
  7343.  ────────────────────────────────────────────────────────────────────────────
  7344.  
  7345.  
  7346.  Description
  7347.  
  7348.  Calculate absolute value of a complex number.
  7349.  
  7350.    #include <math.h>
  7351.  
  7352.    double cabs( struct complex z );
  7353.  
  7354.    long double cabsl( struct _complexl z );
  7355.  
  7356.  z                                 Complex number
  7357.  
  7358.  
  7359.  Remarks
  7360.  
  7361.  The cabs and cabsl functions calculate the absolute value of a complex
  7362.  number, which must be a structure of type complex (or _complexl). The
  7363.  structure z is composed of a real component x and an imaginary component y.
  7364.  A call to one of the cabs routines is equivalent to the following:
  7365.  
  7366.  
  7367.    sqrt( z.x*z.x + z.y*z.y )
  7368.  
  7369.  The cabsl function is the 80-bit counterpart and it uses the 80-bit, 10-byte
  7370.  coprocessor form of arguments and return values. See the reference page on
  7371.  the long double functions for more details on this data type.
  7372.  
  7373.  
  7374.  Return Value
  7375.  
  7376.  On overflow, these functions call matherr or _matherrl, return HUGE_VAL, and
  7377.  set errno to ERANGE.
  7378.  
  7379.  
  7380.  Compatibility
  7381.  
  7382.    cabs
  7383.  
  7384.   ANSI   DOS   OS/2   UNIX   XENIX
  7385.  
  7386.  
  7387.    cabsl
  7388.  
  7389.   ANSI   DOS   OS/2   UNIX   XENIX
  7390.  
  7391.  
  7392.  
  7393.  See Also
  7394.  
  7395.  abs, fabs, labs
  7396.  
  7397.  
  7398.  Example
  7399.  
  7400.    /* CABS.C: Using cabs, this program calculates the absolute value of
  7401.     * a complex number.
  7402.     */
  7403.  
  7404.    #include <math.h>
  7405.    #include <stdio.h>
  7406.  
  7407.    void main()
  7408.    {
  7409.       struct complex number = { 3.0, 4.0 };
  7410.       double d;
  7411.  
  7412.       d = cabs( number );
  7413.       printf( "The absolute value of %f + %fi is %f\n",
  7414.               number.x, number.y, d );
  7415.    }
  7416.  
  7417.  
  7418.  Output
  7419.  
  7420.  
  7421.  
  7422.    The absolute value of 3.000000 + 4.000000i is 5.000000
  7423.  
  7424.  
  7425.  
  7426.  
  7427.  
  7428.  calloc Functions
  7429.  ────────────────────────────────────────────────────────────────────────────
  7430.  
  7431.  
  7432.  Description
  7433.  
  7434.  Allocate an array in memory with elements initialized to 0.
  7435.  
  7436.  #include <stdlib.h>               For ANSI compatibility (calloc only)
  7437.  
  7438.  #include <malloc.h>               Required only for function declarations
  7439.  
  7440.    void *calloc( size_t num, size_t size );
  7441.  
  7442.    void _based( void ) *_bcalloc( _segment seg, size_t num, size_t size );
  7443.  
  7444.    void _far *_fcalloc( size_t num, size_t size );
  7445.  
  7446.    void _near *_ncalloc( size_t num, size_t size );
  7447.  
  7448.  num                               Number of elements
  7449.  
  7450.  size                              Length in bytes of each element
  7451.  
  7452.  seg                               Segment selector
  7453.  
  7454.  
  7455.  Remarks
  7456.  
  7457.  The calloc family of functions allocates storage space for an array of num
  7458.  elements, each of length size bytes. Each element is initialized to 0.
  7459.  
  7460.  In large data models (compact-, large-, and huge-model programs), calloc
  7461.  maps to  _fcalloc. In small data models (tiny-, small-, and medium-model
  7462.  programs), calloc maps to _ncalloc.
  7463.  
  7464.  The various calloc functions allocate storage space in the data segments
  7465.  shown in the list below:
  7466.  
  7467.  Function                          Data Segment
  7468.  ────────────────────────────────────────────────────────────────────────────
  7469.  calloc                            Depends on data model of program
  7470.  
  7471.  _bcalloc                          Based heap, specified by seg segment
  7472.                                    selector
  7473.  
  7474.  _fcalloc                          Far heap (outside default data segment)
  7475.  
  7476.  _ncalloc                          Near heap (inside default data segment)
  7477.  
  7478.  
  7479.  Return Value
  7480.  
  7481.  The calloc functions return a pointer to the allocated space. The storage
  7482.  space pointed to by the return value is guaranteed to be suitably aligned
  7483.  for storage of any type of object. To get a pointer to a type other than
  7484.  void, use a type cast on the return value.
  7485.  
  7486.  The _fcalloc and _ncalloc functions return NULL if there is insufficient
  7487.  memory available or if num or size is 0. The _bcalloc function returns
  7488.  _NULLOFF in this case.
  7489.  
  7490.  
  7491.  Compatibility
  7492.  
  7493.    calloc
  7494.  
  7495.   ANSI   DOS   OS/2   UNIX   XENIX
  7496.  
  7497.  
  7498.  _bcalloc, _fcalloc, _ncalloc
  7499.  
  7500.   ANSI   DOS   OS/2   UNIX   XENIX
  7501.  
  7502.  
  7503.  
  7504.  See Also
  7505.  
  7506.  free functions, halloc, hfree, malloc functions, realloc functions
  7507.  
  7508.  
  7509.  Example
  7510.  
  7511.    /* CALLOC.C: This program uses calloc to allocate space for 40 long
  7512.    integers.
  7513.     * It initializes each element to zero.
  7514.     */
  7515.  
  7516.    #include <stdio.h>
  7517.    #include <malloc.h>
  7518.  
  7519.    void main()
  7520.    {
  7521.       long *buffer;
  7522.  
  7523.       buffer = (long *)calloc( 40, sizeof( long ) );
  7524.       if( buffer != NULL )
  7525.          printf( "Allocated 40 long integers\n" );
  7526.       else
  7527.          printf( "Can't allocate memory\n" );
  7528.       free( buffer );
  7529.    }
  7530.  
  7531.  
  7532.  Output
  7533.  
  7534.  
  7535.  
  7536.    Allocated 40 long integers
  7537.  
  7538.  
  7539.  
  7540.  
  7541.  
  7542.  ceil, ceill
  7543.  ────────────────────────────────────────────────────────────────────────────
  7544.  
  7545.  
  7546.  Description
  7547.  
  7548.  Calculate the ceiling of a value.
  7549.  
  7550.    #include <math.h>
  7551.  
  7552.    double ceil( double x );
  7553.  
  7554.    long double ceill( long double x );
  7555.  
  7556.  x                                 Floating-point value
  7557.  
  7558.  
  7559.  Remarks
  7560.  
  7561.  The ceil and ceill functions return a double (or long double) value
  7562.  representing the smallest integer that is greater than or equal to x.
  7563.  
  7564.  The ceill function is the 80-bit counterpart and it uses the 80-bit, 10-byte
  7565.  coprocessor form of arguments and return values. See the reference page on
  7566.  the long double functions for more details on this data type.
  7567.  
  7568.  
  7569.  Return Value
  7570.  
  7571.  These functions return the double or long double result. There is no error
  7572.  return.
  7573.  
  7574.  
  7575.  Compatibility
  7576.  
  7577.    ceil
  7578.  
  7579.   ANSI   DOS   OS/2   UNIX   XENIX
  7580.  
  7581.  
  7582.    ceill
  7583.  
  7584.   ANSI   DOS   OS/2   UNIX   XENIX
  7585.  
  7586.  
  7587.  
  7588.  See Also
  7589.  
  7590.  floor, fmod
  7591.  
  7592.  
  7593.  Example
  7594.  
  7595.    /* FLOOR.C: This example displays the largest integers less than or equal
  7596.     * to the floating-point values 2.8 and -2.8. It then shows the smallest
  7597.     * integers greater than or equal to 2.8 and -2.8.
  7598.     */
  7599.  
  7600.    #include <math.h>
  7601.    #include <stdio.h>
  7602.  
  7603.    void main()
  7604.    {
  7605.       double y;
  7606.  
  7607.       y = floor( 2.8 );
  7608.       printf( "The floor of 2.8 is %f\n", y );
  7609.       y = floor( -2.8 );
  7610.       printf( "The floor of -2.8 is %f\n", y );
  7611.  
  7612.       y = ceil( 2.8 );
  7613.       printf( "The ceil of 2.8 is %f\n", y );
  7614.       y = ceil( -2.8 );
  7615.       printf( "The ceil of -2.8 is %f\n", y );
  7616.    }
  7617.  
  7618.  
  7619.  Output
  7620.  
  7621.  
  7622.  
  7623.    The floor of 2.8 is 2.000000
  7624.    The floor of -2.8 is -3.000000
  7625.    The ceil of 2.8 is 3.000000
  7626.    The ceil of -2.8 is -2.000000
  7627.  
  7628.  
  7629.  
  7630.  
  7631.  
  7632.  _cexit, _c_exit
  7633.  ────────────────────────────────────────────────────────────────────────────
  7634.  
  7635.  
  7636.  Description
  7637.  
  7638.  Perform clean-up operations and return without terminating the process.
  7639.  
  7640.    #include <process.h>
  7641.  
  7642.    void _cexit( void );
  7643.  
  7644.    void _c_exit( void );
  7645.  
  7646.  
  7647.  Remarks
  7648.  
  7649.  The _cexit function calls, in LIFO ("last in, first out") order, the
  7650.  functions registered by atexit and onexit. Then the _cexit function flushes
  7651.  all I/O buffers and closes all open files before returning.
  7652.  
  7653.  The _c_exit function returns to the calling process without processing
  7654.  atexit or onexit functions or flushing stream buffers.
  7655.  
  7656.  The behavior of the exit, _exit, _cexit, and _c_exit functions is described
  7657.  in the following list:
  7658.  
  7659.  Function                          Action
  7660.  ────────────────────────────────────────────────────────────────────────────
  7661.  exit                              Performs complete C library termination
  7662.                                    procedures, terminates the process, and
  7663.                                    exits with the supplied status code
  7664.  
  7665.  _exit                             Performs "quick" C library termination
  7666.                                    procedures, terminates the process, and
  7667.                                    exits with the supplied status code
  7668.  
  7669.  _cexit                            Performs complete C library termination
  7670.                                    procedures and returns to caller, but
  7671.                                    does not terminate the process
  7672.  
  7673.  _c_exit                           Performs "quick" C library termination
  7674.                                    procedures and returns to caller, but
  7675.                                    does not terminate the process
  7676.  
  7677.  
  7678.  Return Value
  7679.  
  7680.  None.
  7681.  
  7682.  
  7683.  Compatibility
  7684.  
  7685.   ANSI   DOS   OS/2   UNIX   XENIX
  7686.  
  7687.  
  7688.  
  7689.  See Also
  7690.  
  7691.  abort, atexit, exec functions, exit, onexit, spawn functions, system
  7692.  
  7693.  
  7694.  
  7695.  
  7696.  
  7697.  cgets
  7698.  ────────────────────────────────────────────────────────────────────────────
  7699.  
  7700.  
  7701.  Description
  7702.  
  7703.  Gets a character string from the console.
  7704.  
  7705.  #include <conio.h>                Required only for function declarations
  7706.  
  7707.    char *cgets( char *buffer );
  7708.  
  7709.  buffer                            Storage location for data
  7710.  
  7711.  
  7712.  Remarks
  7713.  
  7714.  The cgets function reads a string of characters directly from the console
  7715.  and stores the string and its length in the location pointed to by buffer.
  7716.  The buffer argument must be a pointer to a character array. The first
  7717.  element of the array, buffer[0], must contain the maximum length (in
  7718.  characters) of the string to be read. The array must contain enough elements
  7719.  to hold the string, a terminating null character ('\0'), and two additional
  7720.  bytes.
  7721.  
  7722.  The cgets function continues to read characters until a
  7723.  carriage-return-line-feed (CR-LF) combination is read, or the specified
  7724.  number of characters is read. The string is stored starting at str[2]. If a
  7725.  CR-LF combination is read, it is replaced with a null character ('\0')
  7726.  before being stored. The cgets function then stores the actual length of the
  7727.  string in the second array element, buffer[1].
  7728.  
  7729.  Because all DOS editing keys are active when you call cgets, pressing F3
  7730.  repeats the last entry.
  7731.  
  7732.  
  7733.  Return Value
  7734.  
  7735.  The cgets function returns a pointer to the start of the string, at
  7736.  buffer[2]. There is no error return.
  7737.  
  7738.  
  7739.  Compatibility
  7740.  
  7741.   ANSI   DOS   OS/2   UNIX   XENIX
  7742.  
  7743.  
  7744.  
  7745.  See Also
  7746.  
  7747.  getch, getche
  7748.  
  7749.  
  7750.  Example
  7751.  
  7752.    /* CGETS.C: This program creates a buffer and initializes the first byte
  7753.     * to the size of the buffer - 2. Next, the program accepts an input
  7754.  string
  7755.     * using cgets and displays the size and text of that string.
  7756.     */
  7757.  
  7758.    #include <conio.h>
  7759.    #include <stdio.h>
  7760.  
  7761.    void main()
  7762.    {
  7763.       char buffer[82] = { 80 };  /* Maximum characters in first byte */
  7764.       char *result;
  7765.  
  7766.       printf( "Input line of text, followed by carriage return:\n");
  7767.       result = cgets( buffer );  /* Input a line of text */
  7768.       printf( "\nLine length = %d\nText = %s\n", buffer[1], result );
  7769.    }
  7770.  
  7771.  
  7772.  Output
  7773.  
  7774.  
  7775.  
  7776.    Input line of text, followed by carriage return:
  7777.    This is some text
  7778.    Line length = 17
  7779.    Text = This is some text
  7780.  
  7781.  
  7782.  
  7783.  
  7784.  
  7785.  _chain_intr
  7786.  ────────────────────────────────────────────────────────────────────────────
  7787.  
  7788.  
  7789.  Description
  7790.  
  7791.  Chains an interrupt from one handler to another.
  7792.  
  7793.    #include <dos.h>
  7794.  
  7795.    void _chain_intr( void( _interrupt _far *target )());
  7796.  
  7797.  target                            Target interrupt routine
  7798.  
  7799.  
  7800.  Remarks
  7801.  
  7802.  The _chain_intr routine passes control from one interrupt handler to
  7803.  another. The stack and the registers of the first routine are passed to the
  7804.  second, allowing the second routine to return as if it had been called
  7805.  directly.
  7806.  
  7807.  The _chain_intr routine is generally used when a user-defined interrupt
  7808.  handler begins processing, then chains to the original interrupt handler to
  7809.  finish processing.
  7810.  
  7811.  Chaining is one of two techniques, listed below, that can be used to
  7812.  transfer control from a new interrupt routine to an old one:
  7813.  
  7814.  
  7815.    1.  Call _chain_intr with the interrupt routine as an argument. Do this if
  7816.        your routine is finished and you want the second interrupt routine to
  7817.        terminate the interrupt call.
  7818.  
  7819.        void _interrupt new_int( unsigned _es, unsigned _ds,
  7820.           unsigned _di, unsigned _si,... )
  7821.        {
  7822.            ++_di;                  /* Initial processing here  */
  7823.            _chain_intr( old_int ); /* New DI passed to old_int */
  7824.            --_di;                  /* This is never executed   */
  7825.        }
  7826.  
  7827.  
  7828.    2.  Call the interrupt routine (after casting it to an interrupt function
  7829.        if necessary). Do this if you need to do further processing after the
  7830.        second interrupt routine finishes.
  7831.  
  7832.        void _interrupt new_int( unsigned _es, unsigned _ds,
  7833.           unsigned _di, unsigned _si,... )
  7834.        {
  7835.            ++_di;                   /* Initial processing here  */
  7836.            (*old_int)();            /* New DI passed to old_int */
  7837.            _asm mov _di, di         /* Put real DI from old_int */
  7838.                                     /*   into _di for return    */
  7839.        }
  7840.  
  7841.  
  7842.  
  7843.  Note that the real registers set by the old interrupt function are not
  7844.  automatically set to the pseudoregisters of the new routine.
  7845.  
  7846.  Use the _chain_intr function when you do not want to replace the default
  7847.  interrupt handler, but you do need to see its input. An example is a TSR
  7848.  (terminate-and-stay-resident) program that checks all keyboard input for a
  7849.  particular "hot key" sequence.
  7850.  
  7851.  The _chain_intr function should be used only with C functions that have been
  7852.  declared with type _interrupt. The _interrupt declaration ensures that the
  7853.  procedure's entry/exit sequence is appropriate for an interrupt handler.
  7854.  
  7855.  
  7856.  Return Value
  7857.  
  7858.  The _chain_intr function does not return to the caller.
  7859.  
  7860.  
  7861.  Compatibility
  7862.  
  7863.   ANSI   DOS   OS/2   UNIX   XENIX
  7864.  
  7865.  
  7866.  
  7867.  See Also
  7868.  
  7869.  _dos_getvect,  _dos_keep,  _dos_setvect
  7870.  
  7871.  
  7872.  
  7873.  
  7874.  
  7875.  chdir
  7876.  ────────────────────────────────────────────────────────────────────────────
  7877.  
  7878.  
  7879.  Description
  7880.  
  7881.  Changes the current working directory.
  7882.  
  7883.  #include <direct.h>               Required only for function declarations
  7884.  
  7885.  #include <errno.h>                Required for errno constants
  7886.  
  7887.    int chdir( char *dirname );
  7888.  
  7889.  dirname                           Path name of new working directory
  7890.  
  7891.  
  7892.  Remarks
  7893.  
  7894.  The chdir function changes the current working directory to the directory
  7895.  specified by  dirname. The dirname argument must refer to an existing
  7896.  directory.
  7897.  
  7898.  This function can change the current working directory on any drive; it
  7899.  cannot be used to change the default drive itself. For example, if A: is the
  7900.  default drive and \BIN is the current working directory, the following call
  7901.  changes the current working directory for drive C:
  7902.  
  7903.    chdir("c:\\temp");
  7904.  
  7905.  Notice that you must place two backslashes ( \\ ) in a C string in order to
  7906.  represent a single backslash ( \ ); the backslash is the escape character
  7907.  for C strings and therefore requires special handling.
  7908.  
  7909.  This function call has no apparent immediate effect. However, when the
  7910.  _chdrive function is called to change the default drive to C:, the current
  7911.  working directory becomes C:\TEMP.
  7912.  
  7913.  In OS/2 protected mode, the current working directory is local to a process
  7914.  rather than system-wide. When a process terminates, the current working
  7915.  directory is restored to its original value. Under DOS, the new directory
  7916.  set by the program becomes the new current working directory.
  7917.  
  7918.  
  7919.  Return Value
  7920.  
  7921.  The chdir function returns a value of 0 if the working directory is
  7922.  successfully changed. A return value of -1 indicates an error, in which case
  7923.  errno is set to ENOENT, indicating that the specified path name could not be
  7924.  found.
  7925.  
  7926.  
  7927.  Compatibility
  7928.  
  7929.   ANSI   DOS   OS/2   UNIX   XENIX
  7930.  
  7931.  
  7932.  
  7933.  See Also
  7934.  
  7935.  _dos_setdrive, mkdir, rmdir, system
  7936.  
  7937.  
  7938.  Example
  7939.  
  7940.    /* CHGDIR.C: This program uses the chdir function to verify that a
  7941.     * given directory exists. Under real mode that directory also becomes
  7942.     * the current directory. Under protected mode, it is only the default
  7943.     * directory for the current process.
  7944.     */
  7945.  
  7946.    #include <direct.h>
  7947.    #include <stdio.h>
  7948.    #include <stdlib.h>
  7949.  
  7950.    void main( int argc, char *argv[] )
  7951.    {
  7952.       if( chdir( argv[1] )   )
  7953.          printf( "Unable to locate the directory: %s\n", argv[1] );
  7954.       else
  7955.          system( "dir *.c" );
  7956.    }
  7957.  
  7958.  
  7959.  Output
  7960.  
  7961.  
  7962.  
  7963.    [C:\LIBREF] chgdir \tmp
  7964.  
  7965.     The volume label in drive C is OS2.
  7966.     Directory of C:\TMP
  7967.  
  7968.    DUP      C        232   4-18-89  11:18a
  7969.    TEST     C        713   4-07-88   2:49p
  7970.         2 File(s)   14155776 bytes free
  7971.  
  7972.  
  7973.  
  7974.  
  7975.  
  7976.  _chdrive
  7977.  ────────────────────────────────────────────────────────────────────────────
  7978.  
  7979.  
  7980.  Description
  7981.  
  7982.  Changes the current working drive.
  7983.  
  7984.  #include <direct.h>               Required only for function declarations
  7985.  
  7986.    int _chdrive( int drive );
  7987.  
  7988.  drive                             Number of new working drive
  7989.  
  7990.  
  7991.  Remarks
  7992.  
  7993.  The _chdrive function changes the current working drive to the drive
  7994.  specified by drive. The drive argument uses an integer to specify the new
  7995.  working drive (1=A, 2=B, etc.).
  7996.  
  7997.  This function changes only the working drive; the chdir function changes the
  7998.  working directory.
  7999.  
  8000.  In OS/2 protected mode, the working drive is local to a process rather than
  8001.  system-wide. When a process terminates, the working drive is restored to its
  8002.  original value. Under DOS, the new drive set by the program becomes the new
  8003.  working drive.
  8004.  
  8005.  
  8006.  Return Value
  8007.  
  8008.  The _chdrive function returns a value of 0 if the working drive is
  8009.  successfully changed. A return value of -1 indicates an error.
  8010.  
  8011.  
  8012.  Compatibility
  8013.  
  8014.   ANSI   DOS   OS/2   UNIX   XENIX
  8015.  
  8016.  
  8017.  
  8018.  See Also
  8019.  
  8020.  chdir,  _dos_setdrive,  _fullpath,  _getcwd,  _getdrive, mkdir, rmdir,
  8021.  system
  8022.  
  8023.  
  8024.  Example
  8025.  
  8026.    /* GETDRIVE.C illustrates drive functions including:
  8027.     *      _getdrive       _chdrive        _getdcwd
  8028.     */
  8029.  
  8030.    #include <stdio.h>
  8031.    #include <conio.h>
  8032.    #include <direct.h>
  8033.    #include <stdlib.h>
  8034.  
  8035.    void main()
  8036.    {
  8037.       int ch, drive, curdrive;
  8038.       static char path[_MAX_PATH];
  8039.  
  8040.       /* Save current drive. */
  8041.       curdrive = _getdrive();
  8042.  
  8043.       printf( "Available drives are: \n" );
  8044.  
  8045.       /* If we can switch to the drive, it exists. */
  8046.       for( drive = 1; drive <= 26; drive++ )
  8047.          if( !_chdrive( drive ) )
  8048.             printf( "%c: ", drive + 'A' - 1 );
  8049.  
  8050.       while( 1 )
  8051.       {
  8052.          printf( "\nType drive letter to check or ESC to quit: " );
  8053.          ch = getch();
  8054.          if( ch == 27 )
  8055.             break;
  8056.          if( isalpha( ch ) )
  8057.             putch( ch );
  8058.          if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
  8059.             printf( "\nCurrent directory on that drive is %s\n", path );
  8060.       }
  8061.  
  8062.       /* Restore original drive. This is only necessary for DOS. Under OS/2
  8063.        * the current drive of the calling process is always restored.
  8064.        */
  8065.       _chdrive( curdrive );
  8066.       printf( "\n" );
  8067.    }
  8068.  
  8069.  
  8070.  Output
  8071.  
  8072.  
  8073.  
  8074.    Available drives are:
  8075.    A: B: C:
  8076.    Type drive letter to check or ESC to quit: q
  8077.    Type drive letter to check or ESC to quit: a
  8078.    Current directory on that drive is A:\
  8079.  
  8080.    Type drive letter to check or ESC to quit: c
  8081.    Current directory on that drive is C:\LIBREF
  8082.  
  8083.    Type drive letter to check or ESC to quit:
  8084.  
  8085.  
  8086.  
  8087.  
  8088.  
  8089.  chmod
  8090.  ────────────────────────────────────────────────────────────────────────────
  8091.  
  8092.  
  8093.  Description
  8094.  
  8095.  Changes the file-permission settings.
  8096.  
  8097.  #include <sys\types.h>
  8098.  
  8099.  #include <sys\stat.h>
  8100.  
  8101.  #include <errno.h>
  8102.  
  8103.  #include <io.h>                   Required only for function declarations
  8104.  
  8105.    int chmod( char *filename, int pmode );
  8106.  
  8107.  filename                          Path name of existing file
  8108.  
  8109.  pmode                             Permission setting for file
  8110.  
  8111.  
  8112.  Remarks
  8113.  
  8114.  The chmod function changes the permission setting of the file specified by
  8115.  filename. The permission setting controls read and write access to the file.
  8116.  The constant expression pmode contains one or both of the manifest constants
  8117.  S_IWRITE and S_IREAD, defined in SYS\STAT.H. Any other values for pmode are
  8118.  ignored. When both constants are given, they are joined with the bitwise-OR
  8119.  operator ( | ). The meaning of the pmode argument is as follows:
  8120.  
  8121.  Value                             Meaning
  8122.  ────────────────────────────────────────────────────────────────────────────
  8123.  S_IWRITE                          Writing permitted
  8124.  
  8125.  S_IREAD                           Reading permitted
  8126.  
  8127.  S_IREAD |  S_IWRITE               Reading and writing permitted
  8128.  
  8129.  If write permission is not given, the file is read-only. Under DOS and OS/2,
  8130.  all files are readable; it is not possible to give write-only permission.
  8131.  Thus the modes S_IWRITE and S_IREAD  |  S_IWRITE are equivalent.
  8132.  
  8133.  
  8134.  Return Value
  8135.  
  8136.  The chmod function returns the value 0 if the permission setting is
  8137.  successfully changed. A return value of -1 indicates an error; in this case,
  8138.  errno is set to ENOENT, indicating that the specified file could not be
  8139.  found.
  8140.  
  8141.  
  8142.  Compatibility
  8143.  
  8144.   ANSI   DOS   OS/2   UNIX   XENIX
  8145.  
  8146.  
  8147.  
  8148.  See Also
  8149.  
  8150.  access, creat, fstat, open, stat
  8151.  
  8152.  
  8153.  Example
  8154.  
  8155.    /* CHMOD.C: This program uses chmod to change the mode of a file to
  8156.     * read-only. It then attempts to modify the file.
  8157.     */
  8158.  
  8159.    #include <sys\types.h>
  8160.    #include <sys\stat.h>
  8161.    #include <io.h>
  8162.    #include <stdio.h>
  8163.    #include <stdlib.h>
  8164.  
  8165.    void main()
  8166.    {
  8167.       /* Make file read-only: */
  8168.       if( chmod( "CHMOD.C", S_IREAD ) == -1 )
  8169.          perror( "File not found\n" );
  8170.       else
  8171.          printf( "Mode changed to read-only\n" );
  8172.       system( "echo /* End of file */ >> CHMOD.C" );
  8173.  
  8174.       /* Change back to read/write: */
  8175.       if( chmod( "CHMOD.C", S_IWRITE ) == -1 )
  8176.          perror( "File not found\n" );
  8177.       else
  8178.          printf( "Mode changed to read/write\n" );
  8179.    }
  8180.  
  8181.  
  8182.  Output
  8183.  
  8184.  
  8185.  
  8186.    Mode changed to read-only
  8187.    Access denied
  8188.    Mode changed to read/write
  8189.  
  8190.  
  8191.  
  8192.  
  8193.  
  8194.  chsize
  8195.  ────────────────────────────────────────────────────────────────────────────
  8196.  
  8197.  
  8198.  Description
  8199.  
  8200.  Changes the file size.
  8201.  
  8202.  #include <io.h>                   Required only for function declarations
  8203.  
  8204.  #include <errno.h>
  8205.  
  8206.    int chsize( int handle, long size );
  8207.  
  8208.  handle                            Handle referring to open file
  8209.  
  8210.  size                              New length of file in bytes
  8211.  
  8212.  
  8213.  Remarks
  8214.  
  8215.  The chsize function extends or truncates the file associated with handle to
  8216.  the length specified by size. The file must be open in a mode that permits
  8217.  writing. Null characters ('\0') are appended if the file is extended. If the
  8218.  file is truncated, all data from the end of the shortened file to the
  8219.  original length of the file is lost.
  8220.  
  8221.  In DOS, the directory update is done when a file is closed. Consequently,
  8222.  while a program is running, requests to determine the amount of free disk
  8223.  space may receive inaccurate results.
  8224.  
  8225.  
  8226.  Return Value
  8227.  
  8228.  The chsize function returns the value 0 if the file size is successfully
  8229.  changed. A return value of -1 indicates an error, and errno is set to one of
  8230.  the following values:
  8231.  
  8232.  Value                             Meaning
  8233.  ────────────────────────────────────────────────────────────────────────────
  8234.  EACCES                            Specified file is locked against access
  8235.                                    (OS/2 and DOS versions 3.0 and later
  8236.                                    only).
  8237.  
  8238.  EBADF                             Specified file is read-only or an
  8239.                                    invalid file handle.
  8240.  
  8241.  ENOSPC                            No space is left on device.
  8242.  
  8243.  
  8244.  Compatibility
  8245.  
  8246.   ANSI   DOS   OS/2   UNIX   XENIX
  8247.  
  8248.  
  8249.  
  8250.  See Also
  8251.  
  8252.  close, creat, open
  8253.  
  8254.  
  8255.  Example
  8256.  
  8257.    /* CHSIZE.C: This program uses filelength to report the size of a
  8258.     * file before and after modifying it with chsize.
  8259.     */
  8260.  
  8261.    #include <io.h>
  8262.    #include <fcntl.h>
  8263.    #include <sys\types.h>
  8264.    #include <sys\stat.h>
  8265.    #include <stdio.h>
  8266.  
  8267.    void main()
  8268.    {
  8269.       int fh, result;
  8270.       unsigned int nbytes = BUFSIZ;
  8271.  
  8272.       /* Open a file */
  8273.       if( (fh = open( "data", O_RDWR | O_CREAT, S_IREAD | S_IWRITE )) != -1 )
  8274.       {
  8275.          printf( "File length before: %ld\n", filelength( fh ) );
  8276.          if( chsize( fh, 329678 ) == 0 )
  8277.             printf( "Size successfully changed\n" );
  8278.          else
  8279.             printf( "Problem in changing the size\n" );
  8280.          printf( "File length after:  %ld\n", filelength( fh ) );
  8281.          close( fh );
  8282.       }
  8283.    }
  8284.  
  8285.  
  8286.  Output
  8287.  
  8288.  
  8289.  
  8290.    File length before: 0
  8291.    Size successfully changed
  8292.    File length after:  329678
  8293.  
  8294.  
  8295.  
  8296.  
  8297.  
  8298.  _clear87
  8299.  ────────────────────────────────────────────────────────────────────────────
  8300.  
  8301.  
  8302.  Description
  8303.  
  8304.  Gets and clears the floating-point status word.
  8305.  
  8306.    #include <float.h>
  8307.  
  8308.    unsigned int _clear87( void );
  8309.  
  8310.  
  8311.  Remarks
  8312.  
  8313.  The _clear87 function gets and clears the floating-point status word. The
  8314.  floating-point status word is a combination of the 8087/80287 status word
  8315.  and other conditions detected by the 8087/80287 exception handler, such as
  8316.  floating-point stack overflow and underflow.
  8317.  
  8318.  
  8319.  Return Value
  8320.  
  8321.  The bits in the value returned indicate the floating-point status. See the
  8322.  FLOAT.H include file for a complete definition of the bits returned by
  8323.  _clear87.
  8324.  
  8325.  Many of the math library functions modify the 8087/80287 status word, with
  8326.  unpredictable results. Return values from _clear87 and _status87 become more
  8327.  reliable as fewer floating-point operations are performed between known
  8328.  states of the floating-point status word.
  8329.  
  8330.  
  8331.  Compatibility
  8332.  
  8333.   ANSI   DOS   OS/2   UNIX   XENIX
  8334.  
  8335.  
  8336.  
  8337.  See Also
  8338.  
  8339.  _control87, _status87
  8340.  
  8341.  
  8342.  Example
  8343.  
  8344.    /* CLEAR87.C: This program creates various floating-point problems,
  8345.     * then uses _clear87 to report on these problems.
  8346.     */
  8347.  
  8348.    #include <stdio.h>
  8349.    #include <float.h>
  8350.  
  8351.    void main()
  8352.    {
  8353.       double a = 1e-40, b;
  8354.       float x, y;
  8355.  
  8356.       printf( "Status: %.4x - clear\n", _clear87()  );
  8357.  
  8358.       /* Store into y is inexact and underflows: */
  8359.       y = a;
  8360.       printf( "Status: %.4x - inexact, underflow\n", _clear87() );
  8361.  
  8362.    /* y is denormal: */
  8363.       b = y;
  8364.       printf( "Status: %.4x - denormal\n", _clear87() );
  8365.    }
  8366.  
  8367.  
  8368.  Output
  8369.  
  8370.  
  8371.  
  8372.    Status: 0000 - clear
  8373.    Status: 0030 - inexact, underflow
  8374.    Status: 0002 - denormal
  8375.  
  8376.  
  8377.  
  8378.  
  8379.  
  8380.  clearerr
  8381.  ────────────────────────────────────────────────────────────────────────────
  8382.  
  8383.  
  8384.  Description
  8385.  
  8386.  Resets the error indicator for a stream.
  8387.  
  8388.    #include <stdio.h>
  8389.  
  8390.    void clearerr( FILE *stream );
  8391.  
  8392.  stream                            Pointer to FILE structure
  8393.  
  8394.  
  8395.  Remarks
  8396.  
  8397.  The clearerr function resets the error indicator and end-of-file indicator
  8398.  for stream. Error indicators are not automatically cleared; once the error
  8399.  indicator for a specified stream is set, operations on that stream continue
  8400.  to return an error value until clearerr, fseek, fsetpos, or rewind is
  8401.  called.
  8402.  
  8403.  
  8404.  Return Value
  8405.  
  8406.  None.
  8407.  
  8408.  
  8409.  Compatibility
  8410.  
  8411.   ANSI   DOS   OS/2   UNIX   XENIX
  8412.  
  8413.  
  8414.  
  8415.  See Also
  8416.  
  8417.  eof, feof, ferror, perror
  8418.  
  8419.  
  8420.  Example
  8421.  
  8422.    /* CLEARERR.C: This program creates an error on the standard input
  8423.     * stream, then clears it so that future reads won't fail.
  8424.     */
  8425.  
  8426.    #include <stdio.h>
  8427.  
  8428.    void main()
  8429.    {
  8430.       int c;
  8431.  
  8432.       /* Create an error by writing to standard input. */
  8433.       putc( 'c', stdin );
  8434.       if( ferror( stdin ) )
  8435.       {
  8436.          perror( "Write error" );
  8437.          clearerr( stdin );
  8438.       }
  8439.  
  8440.    /* See if read causes an error. */
  8441.       printf( "Will input cause an error? " );
  8442.       c = getc( stdin );
  8443.       if( ferror( stdin ) )
  8444.       {
  8445.          perror( "Read error" );
  8446.          clearerr( stdin );
  8447.       }
  8448.    }
  8449.  
  8450.  
  8451.  Output
  8452.  
  8453.  
  8454.  
  8455.    Write error: Error 0
  8456.    Will input cause an error? n
  8457.  
  8458.  
  8459.  
  8460.  
  8461.  
  8462.  _clearscreen
  8463.  ────────────────────────────────────────────────────────────────────────────
  8464.  
  8465.  
  8466.  Description
  8467.  
  8468.  Clears the specified area of the screen.
  8469.  
  8470.    #include <graph.h>
  8471.  
  8472.    void _far _clearscreen( short area );
  8473.  
  8474.  area                              Target area
  8475.  
  8476.  
  8477.  Remarks
  8478.  
  8479.  The _clearscreen function erases the target area, filling it with the
  8480.  current background color. The area parameter can be one of the following
  8481.  manifest constants (defined in GRAPH.H):
  8482.  
  8483.  Constant                          Action
  8484.  ────────────────────────────────────────────────────────────────────────────
  8485.  _GCLEARSCREEN                     Clears and fills the entire screen
  8486.  
  8487.  _GVIEWPORT                        Clears and fills only within the current
  8488.                                    view port
  8489.  
  8490.  _GWINDOW                          Clears and fills only within the current
  8491.                                    text window
  8492.  
  8493.  
  8494.  Return Value
  8495.  
  8496.  None.
  8497.  
  8498.  
  8499.  Compatibility
  8500.  
  8501.   ANSI   DOS   OS/2   UNIX   XENIX
  8502.  
  8503.  
  8504.  
  8505.  See Also
  8506.  
  8507.  _getbkcolor,  _setbkcolor
  8508.  
  8509.  
  8510.  Example
  8511.  
  8512.    /* CLRSCRN.C */
  8513.    #include <conio.h>
  8514.    #include <graph.h>
  8515.    #include <stdlib.h>
  8516.  
  8517.    void main()
  8518.    {
  8519.       short xhalf, yhalf, xquar, yquar;
  8520.       struct videoconfig vc;
  8521.  
  8522.    /* Find a valid graphics mode. */
  8523.       if( !_setvideomode( _MAXRESMODE ) )
  8524.          exit( 1 );
  8525.  
  8526.       _getvideoconfig( &vc );
  8527.  
  8528.       xhalf = vc.numxpixels / 2;
  8529.       yhalf = vc.numypixels / 2;
  8530.       xquar = xhalf / 2;
  8531.       yquar = yhalf / 2;
  8532.  
  8533.       _setviewport( 0, 0, xhalf - 1, yhalf - 1 );
  8534.       _rectangle( _GBORDER, 0,  0, xhalf - 1, yhalf - 1 );
  8535.       _ellipse( _GFILLINTERIOR, xquar / 4, yquar / 4,
  8536.                           xhalf - (xquar / 4), yhalf - (yquar / 4) );
  8537.       getch();
  8538.       _clearscreen( _GVIEWPORT );
  8539.  
  8540.       getch();
  8541.       _setvideomode( _DEFAULTMODE );
  8542.    }
  8543.  
  8544.  
  8545.  
  8546.  
  8547.  
  8548.  clock
  8549.  ────────────────────────────────────────────────────────────────────────────
  8550.  
  8551.  
  8552.  Description
  8553.  
  8554.  Calculates the time used by the calling process.
  8555.  
  8556.    #include <time.h>
  8557.  
  8558.    clock_t clock( void );
  8559.  
  8560.  
  8561.  Remarks
  8562.  
  8563.  The clock function tells how much processor time has been used by the
  8564.  calling process. The time in seconds is approximated by dividing the clock
  8565.  return value by the value of the CLOCKS_PER_SEC constant.
  8566.  
  8567.  In other words, the clock function returns the number of processor timer
  8568.  ticks that have elapsed. A timer tick is approximately equal to
  8569.  1/CLOCKS_PER_SEC seconds.
  8570.  
  8571.  
  8572.  Return Value
  8573.  
  8574.  The clock function returns the product of the time in seconds and the value
  8575.  of the CLOCKS_PER_SEC constant. If the processor time is not available, the
  8576.  function returns the value -1, cast as clock_t.
  8577.  
  8578.  
  8579.  Compatibility
  8580.  
  8581.   ANSI   DOS   OS/2   UNIX   XENIX
  8582.  
  8583.  
  8584.  In both DOS and OS/2, clock returns the time elapsed since the process
  8585.  started. This may not be equal to the actual processor time used by the
  8586.  process.
  8587.  
  8588.  In previous versions of Microsoft C, the CLOCKS_PER_SEC constant was called
  8589.  CLK_TCK.
  8590.  
  8591.  
  8592.  See Also
  8593.  
  8594.  difftime, time
  8595.  
  8596.  
  8597.  Example
  8598.  
  8599.    /* CLOCK.C: This example prompts for how long the program is to run and
  8600.     * then continuously displays the elapsed time for that period.
  8601.     */
  8602.  
  8603.    #include <stdio.h>
  8604.    #include <stdlib.h>
  8605.    #include <time.h>
  8606.  
  8607.    void sleep( clock_t wait );
  8608.  
  8609.    void main()
  8610.    {
  8611.       long    i = 600000L;
  8612.       clock_t start, finish;
  8613.       double  duration;
  8614.  
  8615.       /* Delay for a specified time. */
  8616.       printf( "Delay for three seconds\n" );
  8617.       sleep( (clock_t)3 * CLOCKS_PER_SEC );
  8618.       printf( "Done!\n" );
  8619.  
  8620.       /* Measure the duration of an event. */
  8621.       printf( "Time to do %ld empty loops is ", i );
  8622.       start = clock();
  8623.       while( i-- )
  8624.          ;
  8625.       finish = clock();
  8626.       duration = (double)(finish - start) / CLOCKS_PER_SEC;
  8627.       printf( "%2.1f seconds\n", duration );
  8628.    }
  8629.  
  8630.    /* Pauses for a specified number of microseconds. */
  8631.    void sleep( clock_t wait )
  8632.    {
  8633.        clock_t goal;
  8634.  
  8635.        goal = wait + clock();
  8636.        while( goal > clock() )
  8637.            ;
  8638.    }
  8639.  
  8640.  
  8641.  Output
  8642.  
  8643.  
  8644.  
  8645.    Delay for five seconds
  8646.    Done!
  8647.    Time to do 900000 empty loops is 2.0 seconds
  8648.  
  8649.  
  8650.  
  8651.  
  8652.  
  8653.  close
  8654.  ────────────────────────────────────────────────────────────────────────────
  8655.  
  8656.  
  8657.  Description
  8658.  
  8659.  Closes a file.
  8660.  
  8661.  #include <io.h>                   Required only for function declarations
  8662.  
  8663.  #include <errno.h>
  8664.  
  8665.    int close( int handle );
  8666.  
  8667.  handle                            Handle referring to open file
  8668.  
  8669.  
  8670.  Remarks
  8671.  
  8672.  The close function closes the file associated with handle.
  8673.  
  8674.  
  8675.  Return Value
  8676.  
  8677.  The close function returns 0 if the file was successfully closed. A return
  8678.  value of -1 indicates an error, and errno is set to EBADF, indicating an
  8679.  invalid file-handle argument.
  8680.  
  8681.  
  8682.  Compatibility
  8683.  
  8684.   ANSI   DOS   OS/2   UNIX   XENIX
  8685.  
  8686.  
  8687.  
  8688.  See Also
  8689.  
  8690.  chsize, creat, dup, dup2, open, unlink
  8691.  
  8692.  
  8693.  Example
  8694.  
  8695.    /* OPEN.C: This program uses open to open a file named OPEN.C for input
  8696.     * and a file named OPEN.OUT for output. The files are then closed.
  8697.     */
  8698.  
  8699.    #include <fcntl.h>
  8700.    #include <sys\types.h>
  8701.    #include <sys\stat.h>
  8702.    #include <io.h>
  8703.    #include <stdio.h>
  8704.  
  8705.    void main()
  8706.    {
  8707.       int fh1, fh2;
  8708.       fh1 = open( "OPEN.C", O_RDONLY );
  8709.       if( fh1 == -1 )
  8710.          perror( "open failed on input file" );
  8711.       else
  8712.       {
  8713.          printf( "open succeeded on input file\n" );
  8714.          close( fh1 );
  8715.       }
  8716.  
  8717.    fh2 = open( "OPEN.OUT", O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );
  8718.       if( fh2 == -1 )
  8719.          perror( "open failed on output file" );
  8720.       else
  8721.       {
  8722.          printf( "open succeeded on output file\n" );
  8723.          close( fh2 );
  8724.       }
  8725.    }
  8726.  
  8727.  
  8728.  Output
  8729.  
  8730.  
  8731.  
  8732.    open succeeded on input file
  8733.    open succeeded on output file
  8734.  
  8735.  
  8736.  
  8737.  
  8738.  
  8739.  _control87
  8740.  ────────────────────────────────────────────────────────────────────────────
  8741.  
  8742.  
  8743.  Description
  8744.  
  8745.  Gets and sets the floating-point control word.
  8746.  
  8747.    #include <float.h>
  8748.  
  8749.    unsigned int _control87( unsigned int new, unsigned int mask );
  8750.  
  8751.  new                               New control-word bit values
  8752.  
  8753.  mask                              Mask for new control-word bits to set
  8754.  
  8755.  
  8756.  Remarks
  8757.  
  8758.  The _control87 function gets and sets the floating-point control word. The
  8759.  floating-point control word allows the program to change the precision,
  8760.  rounding, and infinity modes in the floating-point-math package.
  8761.  Floating-point exceptions can also be masked or unmasked using the
  8762.  _control87 function.
  8763.  
  8764.  If the value for mask is equal to 0, then _control87 gets the floating-point
  8765.  control word. If mask is nonzero, then a new value for the control word is
  8766.  set in the following manner: for any bit that is on (equal to 1) in mask,
  8767.  the corresponding bit in new is used to update the control word. To put it
  8768.  another way,
  8769.  
  8770.    fpcntrl = ((fpcntrl & ~mask) | (new & mask))
  8771.  
  8772.  where  fpcntrl  is the floating-point control word.
  8773.  
  8774.  The possible values for the mask constant (mask) and new control values
  8775.  (new) are shown in Table R.1.
  8776.  
  8777.  Table   R.1 Hex Values
  8778.  
  8779. ╓┌────────────┌──────────┌────────────────┌──────────────────────────────────╖
  8780.  Mask         Hex Value  Constant         Hex Value
  8781.  ────────────────────────────────────────────────────────────────────────────
  8782.  MCW_EM       0x003F
  8783.  (Interrupt
  8784.  exception)
  8785.  
  8786.                          EM_INVALID       0x0001
  8787.  
  8788.                          EM_DENORMAL      0x0002
  8789.  
  8790.                          EM_ZERODIVIDE    0x0004
  8791.  
  8792.                          EM_OVERFLOW      0x0008
  8793.  
  8794.                          EM_UNDERFLOW     0x0010
  8795.  
  8796.                          EM_INEXACT       0x0020
  8797.  
  8798.  
  8799.  
  8800.  MCW_IC       0x1000
  8801.  Mask         Hex Value  Constant         Hex Value
  8802.  ────────────────────────────────────────────────────────────────────────────
  8803. MCW_IC       0x1000
  8804.  (Infinity
  8805.  control)
  8806.  
  8807.                          IC_AFFINE        0x1000
  8808.  
  8809.                          IC_PROJECTIVE    0x0000
  8810.  
  8811.  
  8812.  
  8813.  MCW_RC       0x0C00
  8814.  (Rounding
  8815.  control)
  8816.  
  8817.                          RC_CHOP          0x0C00
  8818.  
  8819.                          RC_UP            0x0800
  8820.  
  8821.                          RC_DOWN          0x0400
  8822.  Mask         Hex Value  Constant         Hex Value
  8823.  ────────────────────────────────────────────────────────────────────────────
  8824.                         RC_DOWN          0x0400
  8825.  
  8826.                          RC_NEAR          0x0000
  8827.  
  8828.  
  8829.  
  8830.  MCW_PC       0x0300
  8831.  (Precision
  8832.  control)
  8833.  
  8834.                          PC_24 (24 bits)  0x0000
  8835.  
  8836.                          PC_53 (53 bits)  0x0200
  8837.  
  8838.                          PC_64 (64 bits)  0x0300
  8839.  
  8840.  ────────────────────────────────────────────────────────────────────────────
  8841.  
  8842.  
  8843.  
  8844.  
  8845.  Return Value
  8846.  
  8847.  The bits in the value returned indicate the floating-point control state.
  8848.  See the FLOAT.H include file for a complete definition of the bits returned
  8849.  by _control87.
  8850.  
  8851.  
  8852.  Compatibility
  8853.  
  8854.   ANSI   DOS   OS/2   UNIX   XENIX
  8855.  
  8856.  
  8857.  
  8858.  See Also
  8859.  
  8860.  _clear87,  _status87
  8861.  
  8862.  
  8863.  Example
  8864.  
  8865.    /* CNTRL87.C: This program uses _control87 to output the control word,
  8866.     * set the precision to 24 bits, and reset the status to the default.
  8867.     */
  8868.  
  8869.    #include <stdio.h>
  8870.    #include <float.h>
  8871.  
  8872.    void main()
  8873.    {
  8874.       double a = 0.1;
  8875.  
  8876.       /* Show original control word and do calculation. */
  8877.       printf( "Original: 0x%.4x\n", _control87( 0, 0 ) );
  8878.       printf( "%1.1f * %1.1f = %.15e\n", a, a, a * a );
  8879.  
  8880.       /* Set precision to 24 bits and recalculate. */
  8881.       printf( "24-bit:   0x%.4x\n", _control87( PC_24, MCW_PC ) );
  8882.       printf( "%1.1f * %1.1f = %.15e\n", a, a, a * a );
  8883.  
  8884.       /* Restore to default and recalculate. */
  8885.       printf( "Default:  0x%.4x\n", _control87( CW_DEFAULT, 0xffff ) );
  8886.       printf( "%1.1f * %1.1f = %.15e\n", a, a, a * a );
  8887.    }
  8888.  
  8889.  
  8890.  Output
  8891.  
  8892.  
  8893.  
  8894.    Original: 0x1332
  8895.    0.1 * 0.1 = 1.000000000000000e-002
  8896.    24-bit:   0x1332
  8897.    0.1 * 0.1 = 9.999999776482582e-003
  8898.    Default:  0x1032
  8899.    0.1 * 0.1 = 1.000000000000000e-002
  8900.  
  8901.  
  8902.  
  8903.  
  8904.  
  8905.  cos Functions
  8906.  ────────────────────────────────────────────────────────────────────────────
  8907.  
  8908.  
  8909.  Description
  8910.  
  8911.  Calculate the cosine (cos and cosl) or hyperbolic cosine (cosh and coshl).
  8912.  
  8913.    #include <math.h>
  8914.  
  8915.    double cos( double x );
  8916.  
  8917.    double cosh( double x );
  8918.  
  8919.    long double cosl( long double x );
  8920.  
  8921.    long double coshl( long double x );
  8922.  
  8923.  x                                 Angle in radians
  8924.  
  8925.  
  8926.  Remarks
  8927.  
  8928.  The cos and cosh functions return the cosine and hyperbolic cosine,
  8929.  respectively, of x.
  8930.  
  8931.  The cosl and coshl functions are the 80-bit counterparts and use the 80-bit,
  8932.  10-byte coprocessor form of arguments and return values. See the reference
  8933.  page on the long double functions for more details on this data type.
  8934.  
  8935.  
  8936.  Return Value
  8937.  
  8938.  If x is large, a partial loss of significance in the result may occur in a
  8939.  call to cos, in which case the function generates a PLOSS error. If x is so
  8940.  large that significance is completely lost, cos prints a TLOSS message to
  8941.  stderr and returns 0. In both cases, errno is set to ERANGE.
  8942.  
  8943.  If the result is too large in a cosh call, the function returns HUGE_VAL and
  8944.  sets errno to ERANGE.
  8945.  
  8946.  
  8947.  Compatibility
  8948.  
  8949.  cos, cosh
  8950.  
  8951.   ANSI   DOS   OS/2   UNIX   XENIX
  8952.  
  8953.  
  8954.  cosl, coshl
  8955.  
  8956.   ANSI   DOS   OS/2   UNIX   XENIX
  8957.  
  8958.  
  8959.  
  8960.  See Also
  8961.  
  8962.  acos functions, asin functions, atan functions, matherr, sin functions, tan
  8963.  functions
  8964.  
  8965.  
  8966.  Example
  8967.  
  8968.    /* SINCOS.C: This program displays the sine, hyperbolic sine, cosine,
  8969.     * and hyperbolic cosine of pi / 2.
  8970.     */
  8971.  
  8972.    #include <math.h>
  8973.    #include <stdio.h>
  8974.  
  8975.    void main()
  8976.    {
  8977.       double pi = 3.1415926535;
  8978.       double x, y;
  8979.  
  8980.       x = pi / 2;
  8981.       y = sin( x );
  8982.       printf( "sin( %f ) = %f\n", x, y );
  8983.       y = sinh( x );
  8984.       printf( "sinh( %f ) = %f\n",x, y );
  8985.       y = cos( x );
  8986.       printf( "cos( %f ) = %f\n", x, y );
  8987.       y = cosh( x );
  8988.       printf( "cosh( %f ) = %f\n",x, y );
  8989.    }
  8990.  
  8991.  
  8992.  Output
  8993.  
  8994.  
  8995.  
  8996.    sin( 1.570796 ) = 1.000000
  8997.    sinh( 1.570796 ) = 2.301299
  8998.    cos( 1.570796 ) = 0.000000
  8999.    cosh( 1.570796 ) = 2.509178
  9000.  
  9001.  
  9002.  
  9003.  
  9004.  
  9005.  cprintf
  9006.  ────────────────────────────────────────────────────────────────────────────
  9007.  
  9008.  
  9009.  Description
  9010.  
  9011.  Formats and prints to the console.
  9012.  
  9013.  #include <conio.h>                Required only for function declarations
  9014.  
  9015.    int cprintf( char *format [[, argument]] ... );
  9016.  
  9017.  format                            Format control string
  9018.  
  9019.  argument                          Optional arguments
  9020.  
  9021.  
  9022.  Remarks
  9023.  
  9024.  The cprintf function formats and prints a series of characters and values
  9025.  directly to the console, using the putch function to output characters. Each
  9026.  argument (if any) is converted and output according to the corresponding
  9027.  format specification in format. The format has the same form and function as
  9028.  the format argument for the printf function; see printf for a description of
  9029.  the format and arguments.
  9030.  
  9031.  Note that unlike the fprintf, printf, and sprintf functions, cprintf does
  9032.  not translate line-feed characters into carriage-return-line-feed
  9033.  combinations on output.
  9034.  
  9035.  
  9036.  Return Value
  9037.  
  9038.  The cprintf function returns the number of characters printed.
  9039.  
  9040.  
  9041.  Compatibility
  9042.  
  9043.   ANSI   DOS   OS/2   UNIX   XENIX
  9044.  
  9045.  
  9046.  
  9047.  See Also
  9048.  
  9049.  cscanf, fprintf, printf, sprintf, vprintf
  9050.  
  9051.  
  9052.  Example
  9053.  
  9054.    /* CPRINTF.C: This program displays some variables to the console. */
  9055.  
  9056.    #include <conio.h>
  9057.  
  9058.    void main()
  9059.    {
  9060.       int      i = -16, h = 29;
  9061.       unsigned u = 62511;
  9062.       char     c = 'A';
  9063.       char     s[] = "Test";
  9064.  
  9065.    /* Note that console output does not translate \n as
  9066.        * standard output does. Use \r\n instead.
  9067.        */
  9068.       cprintf( "%d  %.4x  %u  %c %s\r\n", i, h, u, c, s );
  9069.  
  9070.    }
  9071.  
  9072.  
  9073.  Output
  9074.  
  9075.  
  9076.  
  9077.    -16  001d  62511  A Test
  9078.  
  9079.  
  9080.  
  9081.  
  9082.  
  9083.  cputs
  9084.  ────────────────────────────────────────────────────────────────────────────
  9085.  
  9086.  
  9087.  Description
  9088.  
  9089.  Puts a string to the console.
  9090.  
  9091.  #include <conio.h>                Required only for function declarations
  9092.  
  9093.    int cputs( char *string );
  9094.  
  9095.  string                            Output string
  9096.  
  9097.  
  9098.  Remarks
  9099.  
  9100.  The cputs function writes the null-terminated string pointed to by string
  9101.  directly to the console. Note that a carriage-return-line-feed (CR-LF)
  9102.  combination is not automatically appended to the string.
  9103.  
  9104.  
  9105.  Return Value
  9106.  
  9107.  If successful, cputs returns a 0. If the function fails, it returns a
  9108.  nonzero value.
  9109.  
  9110.  
  9111.  Compatibility
  9112.  
  9113.   ANSI   DOS   OS/2   UNIX   XENIX
  9114.  
  9115.  
  9116.  
  9117.  See Also
  9118.  
  9119.  putch
  9120.  
  9121.  
  9122.  Example
  9123.  
  9124.    /* CPUTS.C: This program first displays a string to the console. */
  9125.  
  9126.    #include <conio.h>
  9127.  
  9128.    void main()
  9129.    {
  9130.       /* String to print at console. Note the \r (return) character. */
  9131.       char *buffer = "Hello world (courtesy of cputs)!\r\n";
  9132.  
  9133.       cputs( buffer );
  9134.    }
  9135.  
  9136.  
  9137.  Output
  9138.  
  9139.  
  9140.  
  9141.    Hello world (courtesy of cputs)!
  9142.  
  9143.  
  9144.  
  9145.  
  9146.  
  9147.  creat
  9148.  ────────────────────────────────────────────────────────────────────────────
  9149.  
  9150.  
  9151.  Description
  9152.  
  9153.  Creates a new file.
  9154.  
  9155.  #include <sys\types.h>
  9156.  
  9157.  #include <sys\stat.h>
  9158.  
  9159.  #include <errno.h>
  9160.  
  9161.  #include <io.h>                   Required only for function declarations
  9162.  
  9163.    int creat( char *filename, int pmode );
  9164.  
  9165.  filename                          Path name of new file
  9166.  
  9167.  pmode                             Permission setting
  9168.  
  9169.  
  9170.  Remarks
  9171.  
  9172.  The creat function either creates a new file or opens and truncates an
  9173.  existing file. If the file specified by filename does not exist, a new file
  9174.  is created with the given permission setting and is opened for writing. If
  9175.  the file already exists and its permission setting allows writing, creat
  9176.  truncates the file to length 0, destroying the previous contents, and opens
  9177.  it for writing.
  9178.  
  9179.  The permission setting, pmode, applies to newly created files only. The new
  9180.  file receives the specified permission setting after it is closed for the
  9181.  first time. The integer expression pmode contains one or both of the
  9182.  manifest constants S_IWRITE and S_IREAD, defined in SYS\STAT.H. When both of
  9183.  the constants are given, they are joined with the bitwise-OR operator ( | ).
  9184.  The pmode argument is set to one of the following values:
  9185.  
  9186.  Value                             Meaning
  9187.  ────────────────────────────────────────────────────────────────────────────
  9188.  S_IWRITE                          Writing permitted
  9189.  
  9190.  S_IREAD                           Reading permitted
  9191.  
  9192.  S_IREAD | S_IWRITE                Reading and writing permitted
  9193.  
  9194.  If write permission is not given, the file is read-only. Under DOS and OS/2,
  9195.  it is not possible to give write-only permission. Thus, the S_IWRITE and
  9196.  S_IREAD | S_IWRITE modes are equivalent. Under DOS versions 3.0 and later,
  9197.  files opened using creat are always opened in compatibility mode (see
  9198.  sopen).
  9199.  
  9200.  The creat function applies the current file-permission mask to pmode before
  9201.  setting the permissions (see umask).
  9202.  
  9203.  Note that the creat routine is provided primarily for compatibility with
  9204.  previous libraries. A call to open with O_CREAT and O_TRUNC in the oflag
  9205.  argument is equivalent to creat and is preferable for new code.
  9206.  
  9207.  
  9208.  Return Value
  9209.  
  9210.  If successful, creat returns a handle for the created file. Otherwise, it
  9211.  returns -1 and sets errno to one of the following constants:
  9212.  
  9213.  Value                             Meaning
  9214.  ────────────────────────────────────────────────────────────────────────────
  9215.  EACCES                            Path name specifies an existing
  9216.                                    read-only file or specifies a directory
  9217.                                    instead of a file
  9218.  
  9219.  EMFILE                            No more handles available (too many open
  9220.                                    files)
  9221.  
  9222.  ENOENT                            Path name not found
  9223.  
  9224.  
  9225.  Compatibility
  9226.  
  9227.   ANSI   DOS   OS/2   UNIX   XENIX
  9228.  
  9229.  
  9230.  
  9231.  See Also
  9232.  
  9233.  chmod, chsize, close, dup, dup2, open, sopen, umask
  9234.  
  9235.  
  9236.  Example
  9237.  
  9238.    /* CREAT.C: This program uses creat to create the file (or truncate the
  9239.     * existing file) named data and open it for writing.
  9240.     */
  9241.  
  9242.    #include <sys\types.h>
  9243.    #include <sys\stat.h>
  9244.    #include <io.h>
  9245.    #include <stdio.h>
  9246.    #include <stdlib.h>
  9247.  
  9248.    void main()
  9249.    {
  9250.       int fh;
  9251.  
  9252.       fh = creat( "data", S_IREAD | S_IWRITE );
  9253.       if( fh == -1 )
  9254.          perror( "Couldn't create data file" );
  9255.       else
  9256.       {
  9257.          printf( "Created data file.\n" );
  9258.          close( fh );
  9259.       }
  9260.    }
  9261.  
  9262.  
  9263.  Output
  9264.  
  9265.  
  9266.  
  9267.    Created data file.
  9268.  
  9269.  
  9270.  
  9271.  
  9272.  
  9273.  cscanf
  9274.  ────────────────────────────────────────────────────────────────────────────
  9275.  
  9276.  
  9277.  Description
  9278.  
  9279.  Reads formatted data from the console.
  9280.  
  9281.  #include <conio.h>                Required only for function declarations
  9282.  
  9283.    int cscanf( char *format [[, argument]] ... );
  9284.  
  9285.  format                            Format-control string
  9286.  
  9287.  argument                          Optional arguments
  9288.  
  9289.  
  9290.  Remarks
  9291.  
  9292.  The cscanf function reads data directly from the console into the locations
  9293.  given by argument. The getche function is used to read characters. Each
  9294.  optional argument must be a pointer to a variable with a type that
  9295.  corresponds to a type specifier in format. The format controls the
  9296.  interpretation of the input fields and has the same form and function as the
  9297.  format argument for the scanf function; see scanf for a description of
  9298.  format.
  9299.  
  9300.  While cscanf normally echoes the input character, it will not do so if the
  9301.  last call was to ungetch.
  9302.  
  9303.  
  9304.  Return Value
  9305.  
  9306.  The cscanf function returns the number of fields that were successfully
  9307.  converted and assigned. The return value does not include fields that were
  9308.  read but not assigned.
  9309.  
  9310.  The return value is EOF for an attempt to read at end-of-file. This may
  9311.  occur when keyboard input is redirected at the operating system command-line
  9312.  level. A return value of 0 means that no fields were assigned.
  9313.  
  9314.  
  9315.  Compatibility
  9316.  
  9317.   ANSI   DOS   OS/2   UNIX   XENIX
  9318.  
  9319.  
  9320.  
  9321.  See Also
  9322.  
  9323.  cprintf, fscanf, scanf, sscanf
  9324.  
  9325.  
  9326.  Example
  9327.  
  9328.    /* CSCANF.C: This program prompts for a string and uses cscanf to read
  9329.     * in the response. Then cscanf returns the number of items matched,
  9330.     * and the program displays that number.
  9331.     */
  9332.  
  9333.    #include <stdio.h>
  9334.    #include <conio.h>
  9335.  
  9336.    void main()
  9337.    {
  9338.       int   result, i[3];
  9339.  
  9340.       cprintf( "Enter three integers: ");
  9341.       result = cscanf( "%i %i %i", &i[0], &i[1], &i[2] );
  9342.       cprintf( "\r\nYou entered " );
  9343.       while( result-- )
  9344.          cprintf( "%i ", i[result] );
  9345.       cprintf( "\r\n" );
  9346.  
  9347.    }
  9348.  
  9349.  
  9350.  Output
  9351.  
  9352.  
  9353.  
  9354.    Enter three integers: 34 43 987k
  9355.    You entered 987 43 34
  9356.  
  9357.  
  9358.  
  9359.  
  9360.  
  9361.  ctime
  9362.  ────────────────────────────────────────────────────────────────────────────
  9363.  
  9364.  
  9365.  Description
  9366.  
  9367.  Converts a time stored as a time_t value to a character string.
  9368.  
  9369.  #include <time.h>                 Required only for function declarations
  9370.  
  9371.    char *ctime( const time_t *timer );
  9372.  
  9373.  timer                             Pointer to stored time
  9374.  
  9375.  
  9376.  Remarks
  9377.  
  9378.  The ctime function converts a time stored as a time_t value to a character
  9379.  string. The timer value is usually obtained from a call to time, which
  9380.  returns the number of seconds elapsed since 00:00:00 Greenwich mean time,
  9381.  January 1, 1970.
  9382.  
  9383.  The string result produced by ctime contains exactly 26 characters and has
  9384.  the form of the following example:
  9385.  
  9386.    Wed Jan 02 02:03:55 1980\n\0
  9387.  
  9388.  A 24-hour clock is used. All fields have a constant width. The newline
  9389.  character (\n) and the null character ('\0') occupy the last two positions
  9390.  of the string.
  9391.  
  9392.  Calls to the ctime function modify the single statically allocated buffer
  9393.  used by the gmtime and the localtime functions. Each call to one of these
  9394.  routines destroys the result of the previous call. The ctime function also
  9395.  shares a static buffer with the asctime function. Thus, a call to ctime
  9396.  destroys the results of any previous call to asctime, localtime, or gmtime.
  9397.  
  9398.  
  9399.  
  9400.  Return Value
  9401.  
  9402.  The ctime function returns a pointer to the character string result. If time
  9403.  represents a date before 1980, ctime returns NULL.
  9404.  
  9405.  
  9406.  Compatibility
  9407.  
  9408.   ANSI   DOS   OS/2   UNIX   XENIX
  9409.  
  9410.  
  9411.  
  9412.  See Also
  9413.  
  9414.  asctime, ftime, gmtime, localtime, time
  9415.  
  9416.  
  9417.  Example
  9418.  
  9419.    /* ASCTIME.C: This program places the system time in the long integer
  9420.    aclock,
  9421.     * translates it into the structure newtime and then converts it to
  9422.     * string form for output, using the asctime function.
  9423.     */
  9424.  
  9425.    #include <time.h>
  9426.    #include <stdio.h>
  9427.  
  9428.    struct tm *newtime;
  9429.    time_t aclock;
  9430.  
  9431.    void main()
  9432.    {
  9433.       time( &aclock );                    /* Get time in seconds. */
  9434.  
  9435.       newtime = localtime( &aclock );     /* Convert time to struct tm form.
  9436.  */
  9437.  
  9438.       /* Print local time as a string. */
  9439.       printf( "The current date and time are: %s\n", asctime( newtime ) );
  9440.    }
  9441.  
  9442.  
  9443.  Output
  9444.  
  9445.  
  9446.  
  9447.    The current date and time are: Thu Jun 15 06:57:59 1989
  9448.  
  9449.  
  9450.  
  9451.  
  9452.  
  9453.  cwait
  9454.  ────────────────────────────────────────────────────────────────────────────
  9455.  
  9456.  
  9457.  Description
  9458.  
  9459.  Waits until the child process terminates.
  9460.  
  9461.    #include <process.h>
  9462.  
  9463.    int cwait( int *termstat, int procid, int action );
  9464.  
  9465.  termstat                          Address for termination status code
  9466.  
  9467.  procid                            Process ID of child
  9468.  
  9469.  action                            Action code
  9470.  
  9471.  
  9472.  Remarks
  9473.  
  9474.  The cwait function suspends the calling process until the specified child
  9475.  process terminates.
  9476.  
  9477.  If not NULL, termstat points to a buffer where cwait will place the
  9478.  termination-status word and the return code of the terminated child process.
  9479.  
  9480.  
  9481.  The termination-status word indicates whether or not the child process
  9482.  terminated normally by calling the OS/2 DosExit function. (Programs that
  9483.  terminate with exit or by "falling off the end of main" use DosExit
  9484.  internally.) If the process did terminate normally, the low-order and
  9485.  high-order bytes of the termination-status word are as follows:
  9486.  
  9487.  Byte                              Contents
  9488.  ────────────────────────────────────────────────────────────────────────────
  9489.  High order                        Contains the low-order byte of the
  9490.                                    result code that the child code passed
  9491.                                    to DosExit. The DosExit function is
  9492.                                    called if the child process called exit
  9493.                                    or _exit, returned from main, or reached
  9494.                                    the end of main. The low-order byte of
  9495.                                    the result code is either the low-order
  9496.                                    byte of the argument to _exit or exit,
  9497.                                    the low-order byte of the return value
  9498.                                    from main, or a random value if the
  9499.                                    child process reached the end of main.
  9500.  
  9501.  Low order                         0 (normal termination).
  9502.  
  9503.  If the child process terminates without calling DosExit, the high-order and
  9504.  low-order bytes of the termination-status word are as follows:
  9505.  
  9506.  Byte                              Contents
  9507.  ────────────────────────────────────────────────────────────────────────────
  9508.  High order                        0
  9509.  
  9510.  Low order                         Termination code from DosCWait:
  9511.  
  9512.                                    Code        Meaning
  9513.  ────────────────────────────────────────────────────────────────────────────
  9514.                                    1           Hard-error abort
  9515.  
  9516.                                    2           Trap operation
  9517.  
  9518.                                    3           SIGTERM signal not
  9519.                                                intercepted
  9520.  
  9521.  The procid argument specifies which child-process termination to wait for.
  9522.  This value is returned by the call to the spawn function that started the
  9523.  child process. If the specified child process terminates before cwait is
  9524.  called, the function returns immediately.
  9525.  
  9526.  The action argument specifies when the parent process resumes execution, as
  9527.  shown in the following list:
  9528.  
  9529.  Value                             Meaning
  9530.  ────────────────────────────────────────────────────────────────────────────
  9531.  WAIT_CHILD                        The parent process waits until the
  9532.                                    specified child process has ended.
  9533.  
  9534.  WAIT_GRANDCHILD                   The parent process waits until the
  9535.                                    specified child process and all child
  9536.                                    processes of that child process have
  9537.                                    ended.
  9538.  
  9539.  The WAIT_CHILD and WAIT_GRANDCHILD action codes are defined in PROCESS.H.
  9540.  
  9541.  
  9542.  Return Value
  9543.  
  9544.  If the cwait function returns after normal termination of the child process,
  9545.  it returns the child's process ID.
  9546.  
  9547.  If the cwait function returns after abnormal termination of the child
  9548.  process, it returns -1 and sets errno to EINTR.
  9549.  
  9550.  Otherwise, the cwait function returns -1 immediately and sets errno to one
  9551.  of the following error codes:
  9552.  
  9553.  Value                             Meaning
  9554.  ────────────────────────────────────────────────────────────────────────────
  9555.  ECHILD                            No child process exists, or invalid
  9556.                                    process ID
  9557.  
  9558.  EINVAL                            Invalid action code
  9559.  
  9560.  
  9561.  Compatibility
  9562.  
  9563.   ANSI   DOS   OS/2   UNIX   XENIX
  9564.  
  9565.  
  9566.  Note that the OS/2 DosExit function allows programs to return a 16-bit
  9567.  result code. However, the wait and cwait functions return only the low-order
  9568.  byte of that result code.
  9569.  
  9570.  
  9571.  See Also
  9572.  
  9573.  exit, _exit, spawn functions, wait
  9574.  
  9575.  
  9576.  Example
  9577.  
  9578.    /* CWAIT.C: This program launches several child processes and waits
  9579.     * for a specified process to finish.
  9580.     */
  9581.  
  9582.    #define INCL_NOPM
  9583.    #define INCL_NOCOMMON
  9584.    #define INCL_DOSPROCESS
  9585.    #include <os2.h>        /* DosSleep */
  9586.    #include <process.h>    /* cwait    */
  9587.    #include <stdlib.h>
  9588.    #include <stdio.h>
  9589.    #include <time.h>
  9590.  
  9591.    /* Macro to get a random integer within a specified range */
  9592.    #define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) +
  9593.  (min))
  9594.  
  9595.    struct  CHILD
  9596.    {
  9597.        int     pid;
  9598.        char    name[10];
  9599.    } child[4] = { { 0, "Ann" }, { 0, "Beth"  }, { 0, "Carl" }, { 0, "Dave" }
  9600.  };
  9601.  
  9602.    void main( int argc, char *argv[] )
  9603.    {
  9604.        int     termstat, pid, c, tmp;
  9605.  
  9606.        srand( (unsigned)time( NULL ) );               /* Seed randomizer */
  9607.        /* If no arguments, this is the parent. */
  9608.        if( argc == 1 )
  9609.        {
  9610.            /* Spawn children in numeric order. */
  9611.            for( c = 0; c < 4; c++ )
  9612.                child[c].pid = spawnl( P_NOWAIT, argv[0], argv[0],
  9613.                                       child[c].name, NULL );
  9614.  
  9615.    /* Wait for randomly specified child, and respond when done. */
  9616.            c = getrandom( 0, 3 );
  9617.            printf( "Come here, %s\n", child[c].name );
  9618.            cwait( &termstat, child[c].pid, WAIT_CHILD );
  9619.            printf( "Thank you, %s\n", child[c].name );
  9620.        }
  9621.  
  9622.        /* If there are arguments, this must be a child. */
  9623.        else
  9624.        {
  9625.            /* Delay for a period determined by process number. */
  9626.            DosSleep( (argv[1][0] - 'A' + 1) * 1000L );
  9627.            printf( "Hi, dad. It's %s.\n", argv[1] );
  9628.        }
  9629.    }
  9630.  
  9631.  
  9632.  Output
  9633.  
  9634.  
  9635.  
  9636.    Come here, Carl
  9637.    Hi, dad. It's Ann.
  9638.    Hi, dad. It's Beth.
  9639.    Hi, dad. It's Carl.
  9640.    Thank you, Carl
  9641.    Hi, dad. It's Dave.
  9642.  
  9643.  
  9644.  
  9645.  
  9646.  
  9647.  dieeetomsbin, dmsbintoieee
  9648.  ────────────────────────────────────────────────────────────────────────────
  9649.  
  9650.  
  9651.  Description
  9652.  
  9653.  Convert between IEEE double value and Microsoft (MS) binary double value.
  9654.  
  9655.    #include <math.h>
  9656.  
  9657.    int dieeetomsbin( double *src8, double *dst8 );
  9658.  
  9659.    int dmsbintoieee( double *src8, double *dst8 );
  9660.  
  9661.  src8                              Buffer containing value to convert
  9662.  
  9663.  dst8                              Buffer to store converted value
  9664.  
  9665.  
  9666.  Remarks
  9667.  
  9668.  The dieeetomsbin routine converts a double-precision number in IEEE
  9669.  (Institute of Electrical and Electronic Engineers) format to Microsoft (MS)
  9670.  binary format. The routine dmsbintoieee converts a double-precision number
  9671.  in MS binary format to IEEE format.
  9672.  
  9673.  These routines allow C programs (which store floating-point numbers in the
  9674.  IEEE format) to use numeric data in random-access data files created with
  9675.  those versions of Microsoft BASIC that store floating-point numbers in MS
  9676.  binary format, and vice versa.
  9677.  
  9678.  The argument src8 is a pointer to the double value to be converted. The
  9679.  result is stored at the location given by dst8.
  9680.  
  9681.  These routines do not handle IEEE NANs ("not a number") and infinities. IEEE
  9682.  denormals are treated as 0 in the conversions.
  9683.  
  9684.  
  9685.  Return Value
  9686.  
  9687.  These functions return 0 if the conversion is successful and 1 if the
  9688.  conversion causes an overflow.
  9689.  
  9690.  
  9691.  Compatibility
  9692.  
  9693.   ANSI   DOS   OS/2   UNIX   XENIX
  9694.  
  9695.  
  9696.  
  9697.  See Also
  9698.  
  9699.  fieeetomsbin, fmsbintoieee
  9700.  
  9701.  
  9702.  
  9703.  
  9704.  
  9705.  difftime
  9706.  ────────────────────────────────────────────────────────────────────────────
  9707.  
  9708.  
  9709.  Description
  9710.  
  9711.  Finds the difference between two times.
  9712.  
  9713.  #include <time.h>                 Required only for function declarations
  9714.  
  9715.    double difftime( time_t timer1, time_t timer0 );
  9716.  
  9717.  timer0                            Beginning time
  9718.  
  9719.  timer1                            Ending time
  9720.  
  9721.  
  9722.  Remarks
  9723.  
  9724.  The difftime function computes the difference between the supplied time
  9725.  values, timer0 and timer1.
  9726.  
  9727.  
  9728.  Return Value
  9729.  
  9730.  The difftime function returns, in seconds, the elapsed time from timer0 to
  9731.  timer1. The value returned is a double-precision number.
  9732.  
  9733.  
  9734.  Compatibility
  9735.  
  9736.   ANSI   DOS   OS/2   UNIX   XENIX
  9737.  
  9738.  
  9739.  
  9740.  See Also
  9741.  
  9742.  time
  9743.  
  9744.  
  9745.  Example
  9746.  
  9747.    /* DIFFTIME.C: This program calculates the amount of time needed to
  9748.     * do a floating-point multiply 50000 times.
  9749.     */
  9750.  
  9751.    #include <stdio.h>
  9752.    #include <stdlib.h>
  9753.    #include <time.h>
  9754.  
  9755.    void main()
  9756.    {
  9757.       time_t   start, finish;
  9758.       unsigned loop;
  9759.       double   result, elapsed_time;
  9760.  
  9761.       printf( "This program will do a floating point multiply 50000 times\n"
  9762.  );
  9763.       printf( "Working...\n" );
  9764.  
  9765.       time( &start );
  9766.       for( loop = 0; loop < 50000L; loop++ )
  9767.          result = 3.63 * 5.27;
  9768.       time( &finish );
  9769.  
  9770.    elapsed_time = difftime( finish, start );
  9771.       printf( "\nProgram takes %6.2f seconds.\n", elapsed_time );
  9772.    }
  9773.  
  9774.  
  9775.  Output
  9776.  
  9777.  
  9778.  
  9779.    This program will do a floating point multiply 50000 times
  9780.    Working...
  9781.  
  9782.    Program takes   4.00 seconds.
  9783.  
  9784.  
  9785.  
  9786.  
  9787.  
  9788.  _displaycursor
  9789.  ────────────────────────────────────────────────────────────────────────────
  9790.  
  9791.  
  9792.  Description
  9793.  
  9794.  Sets the cursor toggle for graphics functions.
  9795.  
  9796.    #include <graph.h>
  9797.  
  9798.    short _far _displaycursor( short toggle );
  9799.  
  9800.  toggle                            Cursor state
  9801.  
  9802.  
  9803.  Remarks
  9804.  
  9805.  Upon entry into each graphic routine, the screen cursor is turned off. The
  9806.  _displaycursor function determines whether the cursor will be turned back on
  9807.  when programs exit graphic routines. If toggle is set to _GCURSORON, the
  9808.  cursor will be restored on exit. If toggle is set to _GCURSOROFF, the cursor
  9809.  will be left off.
  9810.  
  9811.  
  9812.  Return Value
  9813.  
  9814.  The function returns the previous value of toggle. There is no error return.
  9815.  
  9816.  
  9817.  
  9818.  Compatibility
  9819.  
  9820.   ANSI   DOS   OS/2   UNIX   XENIX
  9821.  
  9822.  
  9823.  
  9824.  See Also
  9825.  
  9826.  _gettextcursor, _settextcursor
  9827.  
  9828.  
  9829.  Example
  9830.  
  9831.    /* DISCURS.C: This program changes the cursor shape using _gettextcursor
  9832.     * and _settextcursor, and hides the cursor using _displaycursor.
  9833.     */
  9834.  
  9835.    #include <conio.h>
  9836.    #include <graph.h>
  9837.  
  9838.    void main()
  9839.    {
  9840.       short oldcursor;
  9841.       short newcursor = 0x007;        /* Full block cursor */
  9842.  
  9843.       /* Save old cursor shape and make sure cursor is on */
  9844.       oldcursor = _gettextcursor();
  9845.       _clearscreen( _GCLEARSCREEN );
  9846.       _displaycursor( _GCURSORON );
  9847.       _outtext( "\nOld cursor shape: " );
  9848.       getch();
  9849.  
  9850.       /* Change cursor shape */
  9851.       _outtext( "\nNew cursor shape: " );
  9852.       _settextcursor( newcursor );
  9853.       getch();
  9854.  
  9855.    /* Restore original cursor shape */
  9856.       _outtext( "\n" );
  9857.       _settextcursor( oldcursor );
  9858.    }
  9859.  
  9860.  
  9861.  
  9862.  
  9863.  
  9864.  div
  9865.  ────────────────────────────────────────────────────────────────────────────
  9866.  
  9867.  
  9868.  Description
  9869.  
  9870.  Computes the quotient and the remainder of two integer values.
  9871.  
  9872.    #include <stdlib.h>
  9873.  
  9874.    div_t div( int numer, int denom );
  9875.  
  9876.  numer                             Numerator
  9877.  
  9878.  denom                             Denominator
  9879.  
  9880.  
  9881.  Remarks
  9882.  
  9883.  The div function divides numer by denom, computing the quotient and the
  9884.  remainder. The div_t structure contains the following elements:
  9885.  
  9886.  Element                           Description
  9887.  ────────────────────────────────────────────────────────────────────────────
  9888.  int quot                          Quotient
  9889.  
  9890.  int rem                           Remainder
  9891.  
  9892.  The sign of the quotient is the same as that of the mathematical quotient.
  9893.  Its absolute value is the largest integer that is less than the absolute
  9894.  value of the mathematical quotient. If the denominator is 0, the program
  9895.  will terminate with an error message.
  9896.  
  9897.  
  9898.  Return Value
  9899.  
  9900.  The div function returns a structure of type div_t, comprising both the
  9901.  quotient and the remainder. The structure is defined in STDLIB.H.
  9902.  
  9903.  
  9904.  Compatibility
  9905.  
  9906.   ANSI   DOS   OS/2   UNIX   XENIX
  9907.  
  9908.  
  9909.  
  9910.  See Also
  9911.  
  9912.  ldiv
  9913.  
  9914.  
  9915.  Example
  9916.  
  9917.    /* DIV.C: This example takes two integers as command-line arguments and
  9918.     * displays the results of the integer division. This program accepts
  9919.     * two arguments on the command line following the program name, then
  9920.     * calls div to divide the first argument by the second. Finally,
  9921.     * it prints the structure members quot and rem.
  9922.     */
  9923.  
  9924.    #include <stdlib.h>
  9925.    #include <stdio.h>
  9926.    #include <math.h>
  9927.  
  9928.    void main( int argc, char *argv[] )
  9929.    {
  9930.       int x,y;
  9931.       div_t div_result;
  9932.  
  9933.       x = atoi( argv[1] );
  9934.       y = atoi( argv[2] );
  9935.  
  9936.       printf( "x is %d, y is %d\n", x, y );
  9937.       div_result = div( x, y );
  9938.       printf( "The quotient is %d, and the remainder is %d\n",
  9939.               div_result.quot, div_result.rem );
  9940.    }
  9941.  
  9942.  
  9943.  Output
  9944.  
  9945.  
  9946.  
  9947.    [C:\LIBREF] div 876 13
  9948.    x is 876, y is 13
  9949.    The quotient is 67, and the remainder is 5
  9950.  
  9951.  
  9952.  
  9953.  
  9954.  
  9955.  _dos_allocmem
  9956.  ────────────────────────────────────────────────────────────────────────────
  9957.  
  9958.  
  9959.  Description
  9960.  
  9961.  Allocates a block of memory, using DOS service 0x48.
  9962.  
  9963.    #include <dos.h>
  9964.  
  9965.    #include <errno.h>
  9966.  
  9967.    unsigned _dos_allocmem( unsigned size, unsigned *seg );
  9968.  
  9969.  size                              Block size to allocate
  9970.  
  9971.  seg                               Return buffer for segment descriptor
  9972.  
  9973.  
  9974.  Remarks
  9975.  
  9976.  The _dos_allocmem function uses DOS service 0x48 to allocate a block of
  9977.  memory size paragraphs long. (A paragraph is 16 bytes.) Allocated blocks are
  9978.  always paragraph aligned. The segment descriptor for the initial segment of
  9979.  the new block is returned in the word that seg points to. If the request
  9980.  cannot be satisfied, the maximum possible size (in paragraphs) is returned
  9981.  in this word instead.
  9982.  
  9983.  
  9984.  Return Value
  9985.  
  9986.  If successful, the _dos_allocmem returns 0. Otherwise, it returns the DOS
  9987.  error code and sets errno to ENOMEM, indicating insufficient memory or
  9988.  invalid arena (memory area) headers.
  9989.  
  9990.  
  9991.  Compatibility
  9992.  
  9993.   ANSI   DOS   OS/2   UNIX   XENIX
  9994.  
  9995.  
  9996.  
  9997.  See Also
  9998.  
  9999.  alloca, calloc functions, _dos_freemem, _dos_setblock, halloc, malloc
  10000.  functions
  10001.  
  10002.  
  10003.  Example
  10004.  
  10005.    /* DALOCMEM.C: This program allocates 20 paragraphs of memory, increases
  10006.     * the allocation to 40 paragraphs, and then frees the memory space.
  10007.     */
  10008.  
  10009.    #include <dos.h>
  10010.    #include <stdio.h>
  10011.  
  10012.    void main()
  10013.    {
  10014.       unsigned segment;
  10015.       unsigned maxsize;
  10016.  
  10017.    /* Allocate 20 paragraphs */
  10018.       if( _dos_allocmem( 20, &segment ) != 0 )
  10019.          printf( "allocation failed\n" );
  10020.       else
  10021.          printf( "allocation successful\n" );
  10022.  
  10023.       /* Increase allocation to 40 paragraphs */
  10024.       if( _dos_setblock( 40, segment, &maxsize ) != 0 )
  10025.          printf( "allocation increase failed\n" );
  10026.       else
  10027.          printf( "allocation increase successful\n" );
  10028.  
  10029.       /* free memory */
  10030.       if( _dos_freemem( segment ) != 0 )
  10031.          printf( "free memory failed\n" );
  10032.       else
  10033.          printf( "free memory successful\n" );
  10034.    }
  10035.  
  10036.  
  10037.  Output
  10038.  
  10039.  
  10040.  
  10041.    allocation successful
  10042.    allocation increase successful
  10043.    free memory successful
  10044.  
  10045.  
  10046.  
  10047.  
  10048.  
  10049.  _dos_close
  10050.  ────────────────────────────────────────────────────────────────────────────
  10051.  
  10052.  
  10053.  Description
  10054.  
  10055.  Closes a file using system call INT 0x3E.
  10056.  
  10057.  #include <dos.h>
  10058.  
  10059.  #include <errno.h>
  10060.  
  10061.    unsigned _dos_close( int handle );
  10062.  
  10063.  handle                            Target file handle
  10064.  
  10065.  
  10066.  Remarks
  10067.  
  10068.  The _dos_close function uses system call 0x3E to close the file indicated by
  10069.  handle. The file's handle argument is returned by the call that created or
  10070.  last opened the file.
  10071.  
  10072.  
  10073.  Return Value
  10074.  
  10075.  The function returns 0 if successful. Otherwise, it returns the DOS error
  10076.  code and sets errno to EBADF, indicating an invalid file handle.
  10077.  
  10078.  Do not use the DOS interface I/O routines with the console, low-level, or
  10079.  stream I/O routines.
  10080.  
  10081.  
  10082.  Compatibility
  10083.  
  10084.   ANSI   DOS   OS/2   UNIX   XENIX
  10085.  
  10086.  
  10087.  
  10088.  See Also
  10089.  
  10090.  close, creat,  _dos_creat functions,  _dos_open,  _dos_read,  _dos_write,
  10091.  dup, open
  10092.  
  10093.  
  10094.  Example
  10095.  
  10096.    /* DOPEN.C: This program uses DOS I/O functions to open and close a file.
  10097.    */
  10098.  
  10099.    #include <fcntl.h>
  10100.    #include <stdio.h>
  10101.    #include <dos.h>
  10102.  
  10103.    void main()
  10104.    {
  10105.       int fh;
  10106.  
  10107.       /* Open file with _dos_open function */
  10108.       if( _dos_open( "data1", O_RDONLY, &fh ) != 0 )
  10109.          perror( "Open failed on input file\n" );
  10110.       else
  10111.          printf( "Open succeeded on input file\n" );
  10112.  
  10113.    /* Close file with _dos_close function */
  10114.       if( _dos_close( fh ) != 0 )
  10115.          perror( "Close failed\n" );
  10116.       else
  10117.          printf( "File successfully closed\n" );
  10118.    }
  10119.  
  10120.  
  10121.  Output
  10122.  
  10123.  
  10124.  
  10125.    Open succeeded on input file
  10126.    File successfully closed
  10127.  
  10128.  
  10129.  
  10130.  
  10131.  
  10132.  _dos_creat Functions
  10133.  ────────────────────────────────────────────────────────────────────────────
  10134.  
  10135.  
  10136.  Description
  10137.  
  10138.  Create a new file.
  10139.  
  10140.  #include <dos.h>
  10141.  
  10142.  #include <errno.h>
  10143.  
  10144.    unsigned _dos_creat( char *filename, unsigned attrib, int *handle );
  10145.  
  10146.    unsigned _dos_creatnew( char *filename, unsigned attrib, int *handle );
  10147.  
  10148.  filename                          File path name
  10149.  
  10150.  attrib                            File attributes
  10151.  
  10152.  handle                            Handle return buffer
  10153.  
  10154.  
  10155.  Remarks
  10156.  
  10157.  The _dos_creat and _dos_creatnew routines create and open a new file named
  10158.  filename; this new file has the access attributes specified in the attrib
  10159.  argument. The new file's handle is copied into the integer location pointed
  10160.  to by handle. The file is opened for both read and write access. If file
  10161.  sharing is installed, the file is opened in compatibility mode.
  10162.  
  10163.  The _dos_creat routine uses system call INT 0x3C, and the _dos_creatnew
  10164.  routine uses system call INT 0x5B. If the file already exists, _dos_creat
  10165.  erases its contents and leaves its attributes unchanged; however, the
  10166.  _dos_creatnew routine fails if the file already exists.
  10167.  
  10168.  
  10169.  Return Value
  10170.  
  10171.  If successful, both routines return 0. Otherwise, they return the DOS error
  10172.  code and set errno to one of the following values:
  10173.  
  10174.  Constant                          Meaning
  10175.  ────────────────────────────────────────────────────────────────────────────
  10176.  EACCES                            Access denied because the directory is
  10177.                                    full or, for _dos_creat only, the file
  10178.                                    exists and cannot be overwritten
  10179.  
  10180.  EEXIST                            File already exists (_dos_creatnew only)
  10181.  
  10182.  EMFILE                            Too many open file handles
  10183.  
  10184.  ENOENT                            Path or file not found
  10185.  
  10186.  
  10187.  Compatibility
  10188.  
  10189.   ANSI   DOS   OS/2   UNIX   XENIX
  10190.  
  10191.  
  10192.  
  10193.  Example
  10194.  
  10195.    /* DCREAT.C: This program creates a file using the _dos_creat function.
  10196.    The
  10197.     * program cannot create a new file using the _dos_creatnew function
  10198.     * because it already exists.
  10199.     */
  10200.  
  10201.    #include <stdio.h>
  10202.    #include <stdlib.h>
  10203.    #include <dos.h>
  10204.  
  10205.    void main()
  10206.    {
  10207.       int fh1, fh2;
  10208.       int result;
  10209.  
  10210.       if( _dos_creat( "data", _A_NORMAL, &fh1 ) != 0 )
  10211.          printf( "Couldn't create data file\n" );
  10212.       else
  10213.       {
  10214.          printf( "Created data file.\n" );
  10215.  
  10216.          /* If _dos_creat is successful, the _dos_creatnew call
  10217.           * will fail since the file exists
  10218.           */
  10219.          if( _dos_creatnew( "data", _A_RDONLY, &fh2 ) != 0 )
  10220.             printf( "Couldn't create data file\n" );
  10221.          else
  10222.          {
  10223.             printf( "Created data file.\n" );
  10224.             _dos_close( fh2 );
  10225.          }
  10226.          _dos_close( fh1 );
  10227.       }
  10228.    }
  10229.  
  10230.  
  10231.  Output
  10232.  
  10233.  
  10234.  
  10235.    Created data file.
  10236.    Couldn't create data file
  10237.  
  10238.  
  10239.  
  10240.  
  10241.  
  10242.  _dos_find Functions
  10243.  ────────────────────────────────────────────────────────────────────────────
  10244.  
  10245.  
  10246.  Description
  10247.  
  10248.  Find the file with the specified attributes or find the next file with the
  10249.  specified attributes.
  10250.  
  10251.    #include <dos.h>
  10252.  
  10253.    #include <errno.h>
  10254.  
  10255.    unsigned _dos_findfirst( char *filename, unsigned attrib, struct find_t
  10256.    *fileinfo );
  10257.  
  10258.    unsigned _dos_findnext( struct find_t *fileinfo );
  10259.  
  10260.  filename                          Target file name
  10261.  
  10262.  attrib                            Target attributes
  10263.  
  10264.  fileinfo                          File-information buffer
  10265.  
  10266.  
  10267.  Remarks
  10268.  
  10269.  The _dos_findfirst routine uses system call INT 0x4E to return information
  10270.  about the first instance of a file whose name and attributes match filename
  10271.  and attrib.
  10272.  
  10273.  The filename argument may use wildcards (* and ?). The attrib argument can
  10274.  be any of the following manifest constants:
  10275.  
  10276.  Constant                          Meaning
  10277.  ────────────────────────────────────────────────────────────────────────────
  10278.  _A_ARCH                           Archive. Set whenever the file is
  10279.                                    changed, and cleared by the DOS BACKUP
  10280.                                    command.
  10281.  
  10282.  _A_HIDDEN                         Hidden file. Cannot be found with the
  10283.                                    DOS DIR command. Returns information
  10284.                                    about normal files as well as about
  10285.                                    files with this attribute.
  10286.  
  10287.  _A_NORMAL                         Normal. File can be read or written
  10288.                                    without restriction.
  10289.  
  10290.  _A_RDONLY                         Read-only. File cannot be opened for
  10291.                                    writing, and a file with the same name
  10292.                                    cannot be created. Returns information
  10293.                                    about normal files as well as about
  10294.                                    files with this attribute.
  10295.  
  10296.  _A_SUBDIR                         Subdirectory. Returns information about
  10297.                                    normal files as well as about files with
  10298.                                    this attribute.
  10299.  
  10300.  _A_SYSTEM                         System file. Cannot be found with the
  10301.                                    DOS DIR command. Returns information
  10302.                                    about normal files as well as about
  10303.                                    files with this attribute.
  10304.  
  10305.  _A_VOLID                          Volume ID. Only one file can have this
  10306.                                    attribute, and it must be in the root
  10307.                                    directory.
  10308.  
  10309.  Multiple constants can be combined (with the OR operator), using the
  10310.  vertical-bar ( | ) character.
  10311.  
  10312.  If the attributes argument to either of these functions is _A_RDONLY,
  10313.  _A_HIDDEN, _A_SYSTEM, or _A_SUBDIR, the function also returns any normal
  10314.  attribute files that match the filename argument. That is, a normal file
  10315.  does not have a read-only, hidden, system, or directory attribute.
  10316.  
  10317.  Information is returned in a find_t structure, defined in DOS.H. The find_t
  10318.  structure contains the following elements:
  10319.  
  10320.  Element                           Description
  10321.  ────────────────────────────────────────────────────────────────────────────
  10322.  char reserved[21]                 Reserved for use by DOS
  10323.  
  10324.  char attrib                       Attribute byte for matched path
  10325.  
  10326.  unsigned wr_time                  Time of last write to file
  10327.  
  10328.  unsigned wr_date                  Date of last write to file
  10329.  
  10330.  long size                         Length of file in bytes
  10331.  
  10332.  char name[13]                     Null-terminated name of matched
  10333.                                    file/directory, without
  10334.                                    the path
  10335.  
  10336.  The formats for the wr_time and wr_date elements are in DOS format and are
  10337.  not usable by any other C run-time function. The time format is shown below:
  10338.  
  10339.  
  10340.  Bits                              Contents
  10341.  ────────────────────────────────────────────────────────────────────────────
  10342.  0 - 4                             Number of 2-second increments (0 - 29)
  10343.  
  10344.  5 -10                             Minutes (0 - 59)
  10345.  
  10346.  11-15                             Hours (0 - 23)
  10347.  
  10348.  The date format is shown below:
  10349.  
  10350.  Bits                              Contents
  10351.  ────────────────────────────────────────────────────────────────────────────
  10352.  0 - 4                             Day of month (1-31)
  10353.  
  10354.  5 - 8                             Month (1-12)
  10355.  
  10356.  9 -15                             Year (relative to 1980)
  10357.  
  10358.  Do not alter the contents of the buffer between a call to _dos_findfirst and
  10359.  a subsequent call to the _dos_findnext function. Also, the buffer should not
  10360.  be altered between calls to _dos_findnext.
  10361.  
  10362.  The _dos_findnext routine uses system call 0x4F to find the next name, if
  10363.  any, that matches the filename and attrib arguments specified in a prior
  10364.  call to _dos_findfirst. The fileinfo argument must point to a structure
  10365.  initialized by a previous call to _dos_findfirst. The contents of the
  10366.  structure will be altered as described above if a match is found.
  10367.  
  10368.  
  10369.  Return Value
  10370.  
  10371.  If successful, both functions return 0. Otherwise, they return the DOS error
  10372.  code and set errno to ENOENT, indicating that  filename could not be
  10373.  matched.
  10374.  
  10375.  
  10376.  Compatibility
  10377.  
  10378.   ANSI   DOS   OS/2   UNIX   XENIX
  10379.  
  10380.  
  10381.  
  10382.  Example
  10383.  
  10384.    /* DFIND.C: This program finds and prints all files in the current
  10385.    directory with
  10386.     * the .c extension.
  10387.     */
  10388.  
  10389.    #include <stdio.h>
  10390.    #include <dos.h>
  10391.  
  10392.    main()
  10393.    {
  10394.       struct find_t  c_file;
  10395.  
  10396.       /* find first .c file in current directory */
  10397.       _dos_findfirst( "*.c", _A_NORMAL, &c_file );
  10398.  
  10399.       printf( "Listing of .c files\n\n" );
  10400.       printf( "File: %s is %ld bytes\n", c_file.name, c_file.size );
  10401.  
  10402.       /* find the rest of the .c files */
  10403.       while( _dos_findnext( &c_file ) == 0 )
  10404.          printf( "File: %s is %ld bytes\n", c_file.name, c_file.size );
  10405.    }
  10406.  
  10407.  
  10408.  Output
  10409.  
  10410.  
  10411.  
  10412.    Listing of .c files
  10413.  
  10414.    File: CHDIR.C is 524 bytes
  10415.    File: SIGFP.C is 2674 bytes
  10416.    File: MAX.C is 258 bytes
  10417.    File: CGETS.C is 577 bytes
  10418.    File: FWRITE.C is 1123 bytes
  10419.  
  10420.  
  10421.  
  10422.  
  10423.  
  10424.  _dos_freemem
  10425.  ────────────────────────────────────────────────────────────────────────────
  10426.  
  10427.  
  10428.  Description
  10429.  
  10430.  Releases a block of memory (INT 0x49).
  10431.  
  10432.    #include <dos.h>
  10433.  
  10434.    #include <errno.h>
  10435.  
  10436.    unsigned _dos_freemem( unsigned seg );
  10437.  
  10438.  seg                               Block to be released
  10439.  
  10440.  
  10441.  Remarks
  10442.  
  10443.  The _dos_freemem function uses system call 0x49 to release a block of memory
  10444.  previously allocated by _dos_allocmem. The seg argument is a value returned
  10445.  by a previous call to _dos_allocmem. The freed memory may no longer be used
  10446.  by the application program.
  10447.  
  10448.  
  10449.  Return Value
  10450.  
  10451.  If successful, _dos_freemem returns 0. Otherwise, it returns the DOS error
  10452.  code and sets errno to ENOMEM, indicating a bad segment value (one that does
  10453.  not correspond to a segment returned by a previous _dos_allocmem call) or
  10454.  invalid arena headers.
  10455.  
  10456.  
  10457.  Compatibility
  10458.  
  10459.   ANSI   DOS   OS/2   UNIX   XENIX
  10460.  
  10461.  
  10462.  
  10463.  See Also
  10464.  
  10465.  _dos_allocmem,  _dos_setblock, free functions
  10466.  
  10467.  
  10468.  Example
  10469.  
  10470.    /* DALOCMEM.C: This program allocates 20 paragraphs of memory, increases
  10471.     * the allocation to 40 paragraphs, and then frees the memory space.
  10472.     */
  10473.  
  10474.    #include <dos.h>
  10475.    #include <stdio.h>
  10476.  
  10477.    void main()
  10478.    {
  10479.       unsigned segment;
  10480.       unsigned maxsize;
  10481.  
  10482.       /* Allocate 20 paragraphs */
  10483.       if( _dos_allocmem( 20, &segment ) != 0 )
  10484.          printf( "allocation failed\n" );
  10485.       else
  10486.          printf( "allocation successful\n" );
  10487.  
  10488.    /* Increase allocation to 40 paragraphs */
  10489.       if( _dos_setblock( 40, segment, &maxsize ) != 0 )
  10490.          printf( "allocation increase failed\n" );
  10491.       else
  10492.          printf( "allocation increase successful\n" );
  10493.  
  10494.       /* Free memory */
  10495.       if( _dos_freemem( segment ) != 0 )
  10496.          printf( "free memory failed\n" );
  10497.       else
  10498.          printf( "free memory successful\n" );
  10499.    }
  10500.  
  10501.  
  10502.  Output
  10503.  
  10504.  
  10505.  
  10506.    allocation successful
  10507.    allocation increase successful
  10508.    free memory successful
  10509.  
  10510.  
  10511.  
  10512.  
  10513.  
  10514.  _dos_getdate
  10515.  ────────────────────────────────────────────────────────────────────────────
  10516.  
  10517.  
  10518.  Description
  10519.  
  10520.  Gets current system date using system call INT 0x2A.
  10521.  
  10522.    #include <dos.h>
  10523.  
  10524.    void _dos_getdate( struct dosdate_t *date );
  10525.  
  10526.  date                              Current system date
  10527.  
  10528.  
  10529.  Remarks
  10530.  
  10531.  The _dos_getdate routine uses system call 0x2A to obtain the current system
  10532.  date. The date is returned in a dosdate_t structure, defined in DOS.H.
  10533.  
  10534.  The dosdate_t structure contains the following elements:
  10535.  
  10536.  Element                           Description
  10537.  ────────────────────────────────────────────────────────────────────────────
  10538.  unsigned char day                 1-31
  10539.  
  10540.  unsigned char month               1-12
  10541.  
  10542.  unsigned int year                 1980 - 2099
  10543.  
  10544.  unsigned char dayofweek           0 - 6 (0 = Sunday)
  10545.  
  10546.  
  10547.  Return Value
  10548.  
  10549.  None.
  10550.  
  10551.  
  10552.  Compatibility
  10553.  
  10554.   ANSI   DOS   OS/2   UNIX   XENIX
  10555.  
  10556.  
  10557.  
  10558.  See Also
  10559.  
  10560.  _dos_gettime,  _dos_setdate,  _dos_settime, gmtime, localtime, mktime,
  10561.  _strdate, _strtime, time
  10562.  
  10563.  
  10564.  Example
  10565.  
  10566.    /* DGTIME.C: This program gets and displays current date and time values.
  10567.    */
  10568.  
  10569.    #include <stdio.h>
  10570.    #include <dos.h>
  10571.  
  10572.    void main()
  10573.    {
  10574.       struct dosdate_t date;
  10575.       struct dostime_t time;
  10576.  
  10577.    /* Get current date and time values */
  10578.  
  10579.       _dos_getdate( &date );
  10580.       _dos_gettime( &time );
  10581.  
  10582.       printf( "Today's date is %d-%d-%d\n", date.month, date.day, date.year
  10583.  );
  10584.       printf( "The time is %02d:%02d\n", time.hour, time.minute );
  10585.    }
  10586.  
  10587.  
  10588.  Output
  10589.  
  10590.  
  10591.  
  10592.    Today's date is 6-15-1989
  10593.    The time is 18:07
  10594.  
  10595.  
  10596.  
  10597.  
  10598.  
  10599.  _dos_getdiskfree
  10600.  ────────────────────────────────────────────────────────────────────────────
  10601.  
  10602.  
  10603.  Description
  10604.  
  10605.  Gets disk information using system call INT 0x36.
  10606.  
  10607.    #include <dos.h>
  10608.  
  10609.    #include <errno.h>
  10610.  
  10611.    unsigned _dos_getdiskfree( unsigned drive, struct diskfree_t *diskspace );
  10612.  
  10613.  drive                             Drive number (default is 0)
  10614.  
  10615.  diskspace                         Buffer to hold disk information
  10616.  
  10617.  
  10618.  Remarks
  10619.  
  10620.  The _dos_getdiskfree routine uses system call 0x36 to obtain information on
  10621.  the disk drive specified by drive. The default drive is 0, drive A is 1,
  10622.  drive B is 2, and so on. Information is returned in the diskfree_t structure
  10623.  (defined in DOS.H) pointed to by diskspace.
  10624.  
  10625.  The struct diskfree_t structure contains the following elements:
  10626.  
  10627.  Element                           Description
  10628.  ────────────────────────────────────────────────────────────────────────────
  10629.  unsigned total_clusters           Total clusters on disk
  10630.  
  10631.  unsigned avail_clusters           Available clusters on disk
  10632.  
  10633.  unsigned sectors_per_cluster      Sectors per cluster
  10634.  
  10635.  unsigned bytes_per_sector         Bytes per sector
  10636.  
  10637.  
  10638.  Return Value
  10639.  
  10640.  If successful, the function returns 0. Otherwise, it returns a nonzero value
  10641.  and sets errno to EINVAL, indicating that an invalid drive was specified.
  10642.  
  10643.  
  10644.  Compatibility
  10645.  
  10646.   ANSI   DOS   OS/2   UNIX   XENIX
  10647.  
  10648.  
  10649.  
  10650.  See Also
  10651.  
  10652.  _dos_getdrive, _dos_setdrive
  10653.  
  10654.  
  10655.  Example
  10656.  
  10657.    /* DGDISKFR.C: This program displays information about the default disk
  10658.    drive. */
  10659.  
  10660.    #include <stdio.h>
  10661.    #include <dos.h>
  10662.  
  10663.    main()
  10664.    {
  10665.       struct diskfree_t drive;
  10666.  
  10667.       /* Get information on default disk drive 0 */
  10668.  
  10669.       _dos_getdiskfree( 0, &drive );
  10670.       printf( "total clusters: %d\n", drive.total_clusters );
  10671.       printf( "available clusters: %d\n", drive.avail_clusters );
  10672.       printf( "sectors per cluster: %d\n", drive.sectors_per_cluster );
  10673.       printf( "bytes per sector: %d\n", drive.bytes_per_sector );
  10674.    }
  10675.  
  10676.  
  10677.  Output
  10678.  
  10679.  
  10680.  
  10681.    total clusters: 9013
  10682.    available clusters: 6030
  10683.    sectors per cluster: 4
  10684.    bytes per sector: 512
  10685.  
  10686.  
  10687.  
  10688.  
  10689.  
  10690.  _dos_getdrive
  10691.  ────────────────────────────────────────────────────────────────────────────
  10692.  
  10693.  
  10694.  Description
  10695.  
  10696.  Gets the current disk drive using system call INT 0x19.
  10697.  
  10698.    #include <dos.h>
  10699.  
  10700.    void _dos_getdrive( unsigned *drive );
  10701.  
  10702.  drive                             Current-drive return buffer
  10703.  
  10704.  
  10705.  Remarks
  10706.  
  10707.  The _dos_getdrive routine uses system call 0x19 to obtain the current disk
  10708.  drive. The current drive is returned in the word that drive points to: 1 =
  10709.  drive A, 2 = drive B, and so on.
  10710.  
  10711.  
  10712.  Return Value
  10713.  
  10714.  None.
  10715.  
  10716.  
  10717.  Compatibility
  10718.  
  10719.   ANSI   DOS   OS/2   UNIX   XENIX
  10720.  
  10721.  
  10722.  
  10723.  See Also
  10724.  
  10725.  _dos_getdiskfree,  _dos_setdrive,  _getdrive
  10726.  
  10727.  
  10728.  Example
  10729.  
  10730.    /* DGDRIVE.C: This program prints the letter of the current drive,
  10731.     * changes the default drive to A, then returns the number of disk drives.
  10732.     */
  10733.  
  10734.    #include <stdio.h>
  10735.    #include <dos.h>
  10736.  
  10737.    void main()
  10738.    {
  10739.       unsigned olddrive, newdrive;
  10740.       unsigned number_of_drives;
  10741.  
  10742.       /* Print current default drive information */
  10743.       _dos_getdrive( &olddrive );
  10744.       printf( "The current drive is: %c\n", 'A' + olddrive1 );
  10745.  
  10746.       /* Set default drive to be drive A */
  10747.       printf( "Changing default drive to A\n");
  10748.       _dos_setdrive( 1, &number_of_drives );
  10749.  
  10750.       /* Get new default drive information and total number of drives */
  10751.       _dos_getdrive( &newdrive );
  10752.       printf( "The current drive is: %c\n", 'A' + newdrive1 );
  10753.       printf( "Number of logical drives: %d\n", number_of_drives );
  10754.  
  10755.    /* Restore default drive */
  10756.       _dos_setdrive( olddrive, &number_of_drives );
  10757.    }
  10758.  
  10759.  
  10760.  Output
  10761.  
  10762.  
  10763.  
  10764.    The current drive is: C
  10765.    Changing default drive to A
  10766.    The current drive is: A
  10767.    Number of logical drives: 26
  10768.  
  10769.  
  10770.  
  10771.  
  10772.  
  10773.  _dos_getfileattr
  10774.  ────────────────────────────────────────────────────────────────────────────
  10775.  
  10776.  
  10777.  Description
  10778.  
  10779.  Gets the current attributes of a file or directory, using system call INT
  10780.  0x43.
  10781.  
  10782.    #include <dos.h>
  10783.  
  10784.    #include <errno.h>
  10785.  
  10786.    unsigned _dos_getfileattr( char *pathname, unsigned *attrib );
  10787.  
  10788.  pathname                          Full path of target file/directory
  10789.  
  10790.  attrib                            Word to store attributes in
  10791.  
  10792.  
  10793.  Remarks
  10794.  
  10795.  The _dos_getfileattr routine uses system call 0x43 to obtain the current
  10796.  attributes of the file or directory pointed to by pathname . The attributes
  10797.  are copied to the low-order byte of the attrib word. Attributes are
  10798.  represented by manifest constants, as described below:
  10799.  
  10800.  Constant                          Meaning
  10801.  ────────────────────────────────────────────────────────────────────────────
  10802.  _A_ARCH                           Archive. Set whenever the file is
  10803.                                    changed, or cleared by the DOS BACKUP
  10804.                                    command.
  10805.  
  10806.  _A_HIDDEN                         Hidden file. Cannot be found by a
  10807.                                    directory search.
  10808.  
  10809.  _A_NORMAL                         Normal. File can be read or written
  10810.                                    without restriction.
  10811.  
  10812.  _A_RDONLY                         Read-only. File cannot be opened for a
  10813.                                    write, and a file with the same name
  10814.                                    cannot be created.
  10815.  
  10816.  _A_SUBDIR                         Subdirectory.
  10817.  
  10818.  _A_SYSTEM                         System file. Cannot be found by a
  10819.                                    directory search.
  10820.  
  10821.  _A_VOLID                          Volume ID. Only one file can have this
  10822.                                    attribute, and it must be in the root
  10823.                                    directory.
  10824.  
  10825.  
  10826.  
  10827.  
  10828.  Return Value
  10829.  
  10830.  If successful, the function returns 0. Otherwise, it returns the DOS error
  10831.  code and sets errno to ENOENT, indicating that the target file or directory
  10832.  could be found.
  10833.  
  10834.  
  10835.  Compatibility
  10836.  
  10837.   ANSI   DOS   OS/2   UNIX   XENIX
  10838.  
  10839.  
  10840.  
  10841.  See Also
  10842.  
  10843.  access, chmod, _dos_setfileattr, umask
  10844.  
  10845.  
  10846.  Example
  10847.  
  10848.    /* DGFILEAT.C: This program creates a file with the specified attributes,
  10849.     * then prints this information before changing the file attributes back
  10850.     * to normal.
  10851.     */
  10852.  
  10853.    #include <stdio.h>
  10854.    #include <dos.h>
  10855.  
  10856.    void main()
  10857.    {
  10858.       unsigned oldattrib, newattrib;
  10859.       int fh;
  10860.  
  10861.       /* Get and display file attribute */
  10862.       _dos_getfileattr( "DGFILEAT.C", &oldattrib );
  10863.       printf( "Attribute: 0x%.4x\n", oldattrib );
  10864.       if( ( oldattrib & _A_RDONLY ) != 0 )
  10865.          printf( "Read only file\n" );
  10866.       else
  10867.          printf( "Not a read only file.\n" );
  10868.  
  10869.       /* Reset file attribute to normal file */
  10870.       _dos_setfileattr( "DGFILEAT.C", _A_RDONLY );
  10871.       _dos_getfileattr( "DGFILEAT.C", &newattrib );
  10872.       printf( "Attribute: 0x%.4x\n", newattrib );
  10873.  
  10874.       /* Restore file attribute */
  10875.       _dos_setfileattr( "DGFILEAT.C", oldattrib );
  10876.       _dos_getfileattr( "DGFILEAT.C", &newattrib );
  10877.       printf( "Attribute: 0x%.4x\n", newattrib );
  10878.    }
  10879.  
  10880.  
  10881.  Output
  10882.  
  10883.  
  10884.  
  10885.    Attribute: 0x0020
  10886.    Not a read only file.
  10887.    Attribute: 0x0001
  10888.    Attribute: 0x0020
  10889.  
  10890.  
  10891.  
  10892.  
  10893.  
  10894.  _dos_getftime
  10895.  ────────────────────────────────────────────────────────────────────────────
  10896.  
  10897.  
  10898.  Description
  10899.  
  10900.  Gets the date and time a file was last written, using system call INT 0x57.
  10901.  
  10902.  
  10903.    #include <dos.h>
  10904.  
  10905.    #include <errno.h>
  10906.  
  10907.    unsigned _dos_getftime( int handle, unsigned *date, unsigned *time );
  10908.  
  10909.  handle                            Target file
  10910.  
  10911.  date                              Date-return buffer
  10912.  
  10913.  time                              Time-return buffer
  10914.  
  10915.  
  10916.  Remarks
  10917.  
  10918.  The _dos_getftime routine uses system call 0x57 to get the date and time
  10919.  that the specified file was last written. The file must have been opened
  10920.  with a call to _dos_open or _dos_creat prior to calling _dos_getftime. The
  10921.  date and time are returned in the words pointed to by date and time. The
  10922.  values appear in the DOS date and time format:
  10923.  
  10924.  Time Bits                         Meaning
  10925.  ────────────────────────────────────────────────────────────────────────────
  10926.  0 - 4                             Number of 2-second increments (0 -29)
  10927.  
  10928.  5 -10                             Minutes (0 -59)
  10929.  
  10930.  11-15                             Hours (0 -23)
  10931.  
  10932.  Date Bits                         Meaning
  10933.  ────────────────────────────────────────────────────────────────────────────
  10934.  0 - 4                             Day (1-31)
  10935.  
  10936.  5 - 8                             Month (1-12)
  10937.  
  10938.  9 -15                             Year (1980 -2099)
  10939.  
  10940.  
  10941.  Return Value
  10942.  
  10943.  If successful, the function returns 0. Otherwise, it returns the DOS error
  10944.  code and sets errno to EBADF, indicating that an invalid file handle was
  10945.  passed.
  10946.  
  10947.  
  10948.  Compatibility
  10949.  
  10950.   ANSI   DOS   OS/2   UNIX   XENIX
  10951.  
  10952.  
  10953.  
  10954.  See Also
  10955.  
  10956.  _dos_setftime, fstat, stat
  10957.  
  10958.  
  10959.  Example
  10960.  
  10961.    /* DGFTIME.C: This program displays and modifies the date and time
  10962.     * fields of a file.
  10963.     */
  10964.  
  10965.    #include <fcntl.h>
  10966.    #include <stdio.h>
  10967.    #include <stdlib.h>
  10968.    #include <dos.h>
  10969.  
  10970.    void main()
  10971.    {
  10972.                                     /* FEDC BA98 7654 3210          */
  10973.       unsigned new_date = 0x184f;   /* 0001 1000 0100 1111  2/15/92 */
  10974.       unsigned new_time = 0x48e0;   /* 0100 1000 1110 0000  9:07 AM */
  10975.       unsigned old_date, old_time;
  10976.  
  10977.       int fh;
  10978.  
  10979.       /* Open file with _dos_open function */
  10980.       if( _dos_open( "dgftime.obj", O_RDONLY, &fh ) != 0 )
  10981.          exit( 1 );
  10982.  
  10983.       /* Get file date and time */
  10984.       _dos_getftime( fh, &old_date, &old_time );
  10985.       printf( "Old date field: 0x%.4x\n", old_date );
  10986.       printf( "Old time field: 0x%.4x\n", old_time );
  10987.       system( "dir dgftime.obj" );
  10988.  
  10989.       /* Modify file date and time */
  10990.       if( !_dos_setftime( fh, new_date, new_time ) )
  10991.       {
  10992.          _dos_getftime( fh, &new_date, &new_time );
  10993.          printf( "New date field: 0x%.4x\n", new_date );
  10994.          printf( "New time field: 0x%.4x\n", new_time );
  10995.          system( "dir dgftime.obj" );
  10996.  
  10997.          /* Restore date and time */
  10998.          _dos_setftime( fh, old_date, old_time );
  10999.       }
  11000.       _dos_close( fh );
  11001.    }
  11002.  
  11003.  
  11004.  Output
  11005.  
  11006.  
  11007.  
  11008.    Old date field: 0x12cf
  11009.    Old time field: 0x94bb
  11010.  
  11011.     Volume in drive C is OS2
  11012.     Directory of  C:\LIBREF
  11013.  
  11014.    DGFTIME  OBJ     3923   6-15-89   6:37p
  11015.            1 File(s)  13676544 bytes free
  11016.  
  11017.    New date field: 0x184f
  11018.    New time field: 0x48e0
  11019.  
  11020.     Volume in drive C is OS2
  11021.     Directory of  C:\LIBREF
  11022.  
  11023.    DGFTIME  OBJ     3923   2-15-92   9:07a
  11024.            1 File(s)  13676544 bytes free
  11025.  
  11026.  
  11027.  
  11028.  
  11029.  
  11030.  _dos_gettime
  11031.  ────────────────────────────────────────────────────────────────────────────
  11032.  
  11033.  
  11034.  Description
  11035.  
  11036.  Gets the current system time, using system call INT 0x2C.
  11037.  
  11038.    #include <dos.h>
  11039.  
  11040.    void _dos_gettime( struct dostime_t *time );
  11041.  
  11042.  time                              Current system time
  11043.  
  11044.  
  11045.  Remarks
  11046.  
  11047.  The _dos_gettime routine uses system call 0x2C to obtain the current system
  11048.  time. The time is returned in a dostime_t structure, defined in DOS.H.
  11049.  
  11050.  The dostime_t structure contains the following elements:
  11051.  
  11052.  Element                           Description
  11053.  ────────────────────────────────────────────────────────────────────────────
  11054.  unsigned char hour                0 -23
  11055.  
  11056.  unsigned char minute              0 -59
  11057.  
  11058.  unsigned char second              0 -59
  11059.  
  11060.  unsigned char hsecond             1/100 second; 0 -99
  11061.  
  11062.  
  11063.  Return Value
  11064.  
  11065.  None.
  11066.  
  11067.  
  11068.  Compatibility
  11069.  
  11070.   ANSI   DOS   OS/2   UNIX   XENIX
  11071.  
  11072.  
  11073.  
  11074.  See Also
  11075.  
  11076.  _dos_getdate,  _dos_setdate,  _dos_settime, gmtime, localtime,  _strtime
  11077.  
  11078.  
  11079.  Example
  11080.  
  11081.    /* DGTIME.C: This program gets and displays current date and time values.
  11082.    */
  11083.  
  11084.    #include <stdio.h>
  11085.    #include <dos.h>
  11086.  
  11087.    void main()
  11088.    {
  11089.       struct dosdate_t date;
  11090.       struct dostime_t time;
  11091.  
  11092.    /* Get current date and time values */
  11093.  
  11094.       _dos_getdate( &date );
  11095.       _dos_gettime( &time );
  11096.  
  11097.       printf( "Today's date is %d-%d-%d\n", date.month, date.day, date.year
  11098.  );
  11099.       printf( "The time is %02d:%02d\n", time.hour, time.minute );
  11100.    }
  11101.  
  11102.  
  11103.  Output
  11104.  
  11105.  
  11106.  
  11107.    Today's date is 6-15-1989
  11108.    The time is 18:07
  11109.  
  11110.  
  11111.  
  11112.  
  11113.  
  11114.  _dos_getvect
  11115.  ────────────────────────────────────────────────────────────────────────────
  11116.  
  11117.  
  11118.  Description
  11119.  
  11120.  Gets the current value of the interrupt vector, using system call INT 0x35.
  11121.  
  11122.  
  11123.    #include <dos.h>
  11124.  
  11125.    void ( _interrupt _far *_dos_getvect( unsigned intnum))();
  11126.  
  11127.  intnum                            Target interrupt vector
  11128.  
  11129.  
  11130.  Remarks
  11131.  
  11132.  The _dos_getvect routine uses system call INT 0x35 to get the current value
  11133.  of the interrupt vector specified by intnum.
  11134.  
  11135.  This routine is typically used in conjunction with the _dos_setvect
  11136.  function. To replace an interrupt vector, first save the current vector of
  11137.  the interrupt using _dos_getvect. Then set the vector to your own interrupt
  11138.  routine with _dos_setvect. The saved vector can later be restored, if
  11139.  necessary, using _dos_setvect. The user-defined routine may also need the
  11140.  original vector in order to call that vector or chain to it with
  11141.  _chain_intr.
  11142.  
  11143.  
  11144.  Return Value
  11145.  
  11146.  The function returns a far pointer for the intnum interrupt to the current
  11147.  handler, if there is one.
  11148.  
  11149.  
  11150.  Compatibility
  11151.  
  11152.   ANSI   DOS   OS/2   UNIX   XENIX
  11153.  
  11154.  
  11155.  
  11156.  See Also
  11157.  
  11158.  _chain_intr,  _dos_keep,  _dos_setvect
  11159.  
  11160.  
  11161.  
  11162.  
  11163.  
  11164.  _dos_keep
  11165.  ────────────────────────────────────────────────────────────────────────────
  11166.  
  11167.  
  11168.  Description
  11169.  
  11170.  Installs TSR (terminate-and-stay-resident) programs in memory, using system
  11171.  call INT 0x31.
  11172.  
  11173.    #include <dos.h>
  11174.  
  11175.    void _dos_keep( unsigned retcode, unsigned memsize );
  11176.  
  11177.  retcode                           Exit status code
  11178.  
  11179.  memsize                           Allocated resident memory (in 16-byte
  11180.                                    paragraphs)
  11181.  
  11182.  
  11183.  Remarks
  11184.  
  11185.  The _dos_keep routine installs TSRs (terminate-and-stay-resident programs)
  11186.  in memory, using system call INT 0x31.
  11187.  
  11188.  The routine first exits the calling process, leaving it in memory. It then
  11189.  returns the low-order byte of retcode to the parent of the calling process.
  11190.  Before returning execution to the parent process, _dos_keep sets the
  11191.  allocated memory for the now-resident process to memsize 16-byte paragraphs.
  11192.  Any excess memory is returned to the system.
  11193.  
  11194.  The _dos_keep function calls the same internal routines called by exit. It
  11195.  therefore takes the following actions:
  11196.  
  11197.  
  11198.    ■   Calls atexit and onexit if defined.
  11199.  
  11200.    ■   Flushes all file buffers.
  11201.  
  11202.    ■   Restores interrupt vectors replaced by the C start-up code. The
  11203.        primary one is interrupt 0 (divide by zero). If the emulator math
  11204.        library is used and there is no coprocessor, interrupts 0x34 through
  11205.        0x3D are restored. If there is a coprocessor, interrupt 2 is restored.
  11206.  
  11207.  
  11208.  The _dos_keep function does not automatically close files; you should do
  11209.  this specifically unless you want files opened by the TSR installation code
  11210.  to remain open for the TSR.
  11211.  
  11212.  Do not use the emulator math library in TSRs unless you are familiar with
  11213.  the C start-up code and the coprocessor. Use the alternate math package (not
  11214.  supplied with Microsoft QuickC) if the TSR must do floating-point math.
  11215.  
  11216.  Do not run programs that use _dos_keep from inside the Microsoft
  11217.  Programmer's WorkBench environment, since doing so causes subsequent memory
  11218.  problems. The _dos_keep function terminates the program when executed in the
  11219.  Programmer's WorkBench environment.
  11220.  
  11221.  
  11222.  Return Value
  11223.  
  11224.  None.
  11225.  
  11226.  
  11227.  Compatibility
  11228.  
  11229.   ANSI   DOS   OS/2   UNIX   XENIX
  11230.  
  11231.  
  11232.  
  11233.  See Also
  11234.  
  11235.  _cexit,  _chain_intr,  _dos_getvect,  _dos_setvect,  _exit
  11236.  
  11237.  
  11238.  
  11239.  
  11240.  
  11241.  _dos_open
  11242.  ────────────────────────────────────────────────────────────────────────────
  11243.  
  11244.  
  11245.  Description
  11246.  
  11247.  Opens a file, using system call INT 0x3D.
  11248.  
  11249.  #include <dos.h>
  11250.  
  11251.  #include <errno.h>
  11252.  
  11253.  #include <fcntl.h>                Access mode constants
  11254.  
  11255.  #include <share.h>                Sharing mode constants
  11256.  
  11257.    unsigned _dos_open( char *filename, unsigned mode, int *handle );
  11258.  
  11259.  filename                          Path to an existing file
  11260.  
  11261.  mode                              Permissions
  11262.  
  11263.  handle                            Pointer to integer
  11264.  
  11265.  
  11266.  Remarks
  11267.  
  11268.  The _dos_open routine uses system call INT 0x3D to open the existing file
  11269.  pointed to by filename. The handle for the opened file is copied into the
  11270.  integer pointed to by handle. The mode argument specifies the file's access,
  11271.  sharing, and inheritance modes by combining (with the OR operator) manifest
  11272.  constants from the three groups shown below. At most, one access mode and
  11273.  one sharing mode can be specified at a time.
  11274.  
  11275. ╓┌─────────────┌────────────────────────────────┌────────────────────────────
  11276.  Constant      Mode                             Meaning
  11277.  ─────────────────────────────────────────────────────────────────────────────
  11278.  O_RDONLY      Access                           Read-only
  11279.  Constant      Mode                             Meaning
  11280.  ─────────────────────────────────────────────────────────────────────────────
  11281.  O_RDONLY      Access                           Read-only
  11282.  
  11283.  O_WRONLY      Access                           Write-only
  11284.  
  11285.  O_RDWR        Access                           Both read and write
  11286.  
  11287.  SH_COMPAT     Sharing                          Compatibility
  11288.  
  11289.  SH_DENYRW     Sharing                          Deny reading and writing
  11290.  
  11291.  SH_DENYWR     Sharing                          Deny writing
  11292.  
  11293.  SH_DENYRD     Sharing                          Deny reading
  11294.  
  11295.  SH_DENYNO     Sharing                          Deny neither
  11296.  
  11297.  O_NOINHERIT   Inheritance by the child         File is not inherited
  11298.                process
  11299.  
  11300.  Constant      Mode                             Meaning
  11301.  ─────────────────────────────────────────────────────────────────────────────
  11302. 
  11303.  
  11304.  
  11305.  Do not use the DOS interface I/O routines in conjunction with the console,
  11306.  low-level, or stream I/O routines.
  11307.  
  11308.  
  11309.  Return Value
  11310.  
  11311.  If successful, the function returns 0. Otherwise, it returns the DOS error
  11312.  code and sets errno to one of the following manifest constants:
  11313.  
  11314.  Constant                          Meaning
  11315.  ────────────────────────────────────────────────────────────────────────────
  11316.  EACCES                            Access denied (possible reasons include
  11317.                                    specifying a directory or volume ID for
  11318.                                    filename, or opening a read-only file
  11319.                                    for write access)
  11320.  
  11321.  EINVAL                            Sharing mode specified when file sharing
  11322.                                    not installed, or
  11323.                                    access-mode value is invalid
  11324.  
  11325.  EMFILE                            Too many open file handles
  11326.  
  11327.  ENOENT                            Path or file not found
  11328.  
  11329.  
  11330.  Compatibility
  11331.  
  11332.   ANSI   DOS   OS/2   UNIX   XENIX
  11333.  
  11334.  
  11335.  
  11336.  See Also
  11337.  
  11338.  _dos_close,  _dos_read,  _dos_write
  11339.  
  11340.  
  11341.  Example
  11342.  
  11343.    /* DOPEN.C: This program uses DOS I/O functions to open and close a file.
  11344.    */
  11345.  
  11346.    #include <fcntl.h>
  11347.    #include <stdio.h>
  11348.    #include <dos.h>
  11349.  
  11350.    void main()
  11351.    {
  11352.       int fh;
  11353.  
  11354.       /* Open file with _dos_open function */
  11355.       if( _dos_open( "data1", O_RDONLY, &fh ) != 0 )
  11356.          perror( "Open failed on input file\n" );
  11357.       else
  11358.          printf( "Open succeeded on input file\n" );
  11359.  
  11360.       /* Close file with _dos_close function */
  11361.       if( _dos_close( fh ) != 0 )
  11362.          perror( "Close failed\n" );
  11363.       else
  11364.          printf( "File successfully closed\n" );
  11365.    }
  11366.  
  11367.  
  11368.  Output
  11369.  
  11370.  
  11371.  
  11372.    Open succeeded on input file
  11373.    File successfully closed
  11374.  
  11375.  
  11376.  
  11377.  
  11378.  
  11379.  _dos_read
  11380.  ────────────────────────────────────────────────────────────────────────────
  11381.  
  11382.  
  11383.  Description
  11384.  
  11385.  Reads data from a file, using system call INT 0x3F.
  11386.  
  11387.    #include <dos.h>
  11388.  
  11389.    unsigned _dos_read( int handle, void _far *buffer, unsigned count,
  11390.    unsigned *numread );
  11391.  
  11392.  handle                            File to read
  11393.  
  11394.  buffer                            Buffer to write to
  11395.  
  11396.  count                             Number of bytes to read
  11397.  
  11398.  numread                           Number of bytes actually read
  11399.  
  11400.  
  11401.  Remarks
  11402.  
  11403.  The _dos_read routine uses system call INT 0x3F to read count bytes of data
  11404.  from the file specified by handle. The routine then copies the data to the
  11405.  buffer pointed to by buffer. The integer pointed to by numread will show the
  11406.  number of bytes actually read, which may be less than the number requested
  11407.  in count. If the number of bytes actually read is 0, it means the routine
  11408.  tried to read at end-of-file.
  11409.  
  11410.  Do not use the DOS interface I/O routines in conjunction with the console,
  11411.  low-level, or stream I/O routines.
  11412.  
  11413.  
  11414.  Return Value
  11415.  
  11416.  If successful, the function returns 0. Otherwise, it returns the DOS error
  11417.  code and sets errno to one of the following constants:
  11418.  
  11419.  Constant                          Meaning
  11420.  ────────────────────────────────────────────────────────────────────────────
  11421.  EACCES                            Access denied (handle is not open for
  11422.                                    read access)
  11423.  
  11424.  EBADF                             File handle is invalid
  11425.  
  11426.  
  11427.  Compatibility
  11428.  
  11429.   ANSI   DOS   OS/2   UNIX   XENIX
  11430.  
  11431.  
  11432.  
  11433.  See Also
  11434.  
  11435.  _dos_close,  _dos_open,  _dos_write, read
  11436.  
  11437.  
  11438.  Example
  11439.  
  11440.    /* DREAD.C: This program uses the DOS I/O operations to read the contents
  11441.     * of a file.
  11442.     */
  11443.  
  11444.    #include <fcntl.h>
  11445.    #include <stdlib.h>
  11446.    #include <stdio.h>
  11447.    #include <dos.h>
  11448.  
  11449.    void main()
  11450.    {
  11451.       int fh;
  11452.       char buffer[50];
  11453.       unsigned number_read;
  11454.  
  11455.       /* Open file with _dos_open function */
  11456.       if( _dos_open( "dread.c", O_RDONLY, &fh ) != 0 )
  11457.          perror( "Open failed on input file\n" );
  11458.       else
  11459.          printf( "Open succeeded on input file\n" );
  11460.  
  11461.       /* Read data with _dos_read function */
  11462.       _dos_read( fh, buffer, 50, &number_read );
  11463.       printf( "First 40 characters are: %.40s\n\n", buffer );
  11464.  
  11465.       /* Close file with _dos_close function */
  11466.       _dos_close( fh );
  11467.    }
  11468.  
  11469.  
  11470.  Output
  11471.  
  11472.  
  11473.  
  11474.    Open succeeded on input file
  11475.    First 40 characters are: /* DREAD.C: This program uses the DOS I/
  11476.  
  11477.  
  11478.  
  11479.  
  11480.  
  11481.  _dos_setblock
  11482.  ────────────────────────────────────────────────────────────────────────────
  11483.  
  11484.  
  11485.  Description
  11486.  
  11487.  Changes the size of a memory segment, using system call INT 0x4A.
  11488.  
  11489.    #include <dos.h>
  11490.  
  11491.    unsigned _dos_setblock( unsigned size, unsigned seg, unsigned *maxsize );
  11492.  
  11493.  size                              New segment size
  11494.  
  11495.  seg                               Target segment
  11496.  
  11497.  maxsize                           Maximum-size buffer
  11498.  
  11499.  
  11500.  Remarks
  11501.  
  11502.  The _dos_setblock routine uses system call INT 0x4A to change the size of
  11503.  seg, previously allocated by _dos_allocmem, to size paragraphs. If the
  11504.  request cannot be satisfied, the maximum possible segment size is copied to
  11505.  the buffer pointed to by maxsize.
  11506.  
  11507.  
  11508.  Return Value
  11509.  
  11510.  The function returns 0 if successful. If the call fails, it returns the DOS
  11511.  error code and sets errno to ENOMEM, indicating a bad segment value was
  11512.  passed. A bad segment value is one that does not correspond to a segment
  11513.  returned from a previous _dos_allocmem call, or one that contains invalid
  11514.  arena headers.
  11515.  
  11516.  
  11517.  Compatibility
  11518.  
  11519.   ANSI   DOS   OS/2   UNIX   XENIX
  11520.  
  11521.  
  11522.  
  11523.  See Also
  11524.  
  11525.  _dos_allocmem,  _dos_freemem, realloc functions
  11526.  
  11527.  
  11528.  Example
  11529.  
  11530.    /* DALOCMEM.C: This program allocates 20 paragraphs of memory, increases
  11531.     * the allocation to 40 paragraphs, and then frees the memory space.
  11532.     */
  11533.  
  11534.    #include <dos.h>
  11535.    #include <stdio.h>
  11536.  
  11537.    void main()
  11538.    {
  11539.       unsigned segment;
  11540.       unsigned maxsize;
  11541.  
  11542.    /* Allocate 20 paragraphs */
  11543.       if( _dos_allocmem( 20, &segment ) != 0 )
  11544.          printf( "allocation failed\n" );
  11545.       else
  11546.          printf( "allocation successful\n" );
  11547.  
  11548.       /* Increase allocation to 40 paragraphs */
  11549.       if( _dos_setblock( 40, segment, &maxsize ) != 0 )
  11550.          printf( "allocation increase failed\n" );
  11551.       else
  11552.          printf( "allocation increase successful\n" );
  11553.  
  11554.       /* Free memory */
  11555.       if( _dos_freemem( segment ) != 0 )
  11556.          printf( "free memory failed\n" );
  11557.       else
  11558.          printf( "free memory successful\n" );
  11559.    }
  11560.  
  11561.  
  11562.  Output
  11563.  
  11564.  
  11565.  
  11566.    allocation successful
  11567.    allocation increase successful
  11568.    free memory successful
  11569.  
  11570.  
  11571.  
  11572.  
  11573.  
  11574.  _dos_setdate
  11575.  ────────────────────────────────────────────────────────────────────────────
  11576.  
  11577.  
  11578.  Description
  11579.  
  11580.  Sets the current system date, using system call INT 0x2B.
  11581.  
  11582.    #include <dos.h>
  11583.  
  11584.    unsigned _dos_setdate( struct dosdate_t *date );
  11585.  
  11586.  date                              New system date
  11587.  
  11588.  
  11589.  Remarks
  11590.  
  11591.  The _dos_setdate routine uses system call INT 0x2B to set the current system
  11592.  date. The date is stored in the dosdate_t structure pointed to by date,
  11593.  defined in DOS.H. The dosdate_t structure contains the following elements:
  11594.  
  11595.  Element                           Description
  11596.  ────────────────────────────────────────────────────────────────────────────
  11597.  unsigned char day                 1 -31
  11598.  
  11599.  unsigned char month               1 -12
  11600.  
  11601.  unsigned int year                 1980 - 2099
  11602.  
  11603.  unsigned char dayofweek           0 - 6 (0 = Sunday)
  11604.  
  11605.  
  11606.  Return Value
  11607.  
  11608.  If successful, the function returns 0. Otherwise, it returns a nonzero value
  11609.  and sets errno to EINVAL, indicating an invalid date was specified.
  11610.  
  11611.  
  11612.  Compatibility
  11613.  
  11614.   ANSI   DOS   OS/2   UNIX   XENIX
  11615.  
  11616.  
  11617.  
  11618.  See Also
  11619.  
  11620.  _dos_gettime,  _dos_setdate,  _dos_settime, gmtime, localtime, mktime,
  11621.  _strdate, _strtime, time
  11622.  
  11623.  
  11624.  Example
  11625.  
  11626.    /* DSTIME.C: This program changes the time and date values and displays
  11627.    the
  11628.     * new date and time values.
  11629.     */
  11630.  
  11631.    #include <dos.h>
  11632.    #include <conio.h>
  11633.    #include <stdio.h>
  11634.    #include <time.h>
  11635.  
  11636.    void main()
  11637.    {
  11638.       struct dosdate_t olddate, newdate = { { 4 }, { 7 }, { 1984 } };
  11639.       struct dostime_t oldtime, newtime = { { 3 }, { 45 }, { 30 }, { 0 } };
  11640.       char   datebuf[40], timebuf[40];
  11641.  
  11642.       /* Get current date and time values */
  11643.       _dos_getdate( &olddate );
  11644.       _dos_gettime( &oldtime );
  11645.       printf( "%s    %s\n" , _strdate( datebuf ), _strtime( timebuf ) );
  11646.  
  11647.       /* Modify date and time structures */
  11648.       _dos_setdate( &newdate );
  11649.       _dos_settime( &newtime );
  11650.       printf( "%s    %s\n" , _strdate( datebuf ), _strtime( timebuf ) );
  11651.  
  11652.       /* Restore old date and time */
  11653.       _dos_setdate( &olddate );
  11654.       _dos_settime( &oldtime );
  11655.    }
  11656.  
  11657.  
  11658.  Output
  11659.  
  11660.  
  11661.  
  11662.    06/15/89    18:26:09
  11663.    07/04/84    03:45:30
  11664.  
  11665.  
  11666.  
  11667.  
  11668.  
  11669.  _dos_setdrive
  11670.  ────────────────────────────────────────────────────────────────────────────
  11671.  
  11672.  
  11673.  Description
  11674.  
  11675.  Sets the default drive, using system call INT 0x0E.
  11676.  
  11677.    #include <dos.h>
  11678.  
  11679.    void _dos_setdrive( unsigned drive, unsigned *numdrives );
  11680.  
  11681.  drive                             New default drive
  11682.  
  11683.  numdrives                         Total drives available
  11684.  
  11685.  
  11686.  Remarks
  11687.  
  11688.  The _dos_setdrive routine uses system call INT 0x0E to set the current
  11689.  default drive to the drive argument: 1 = drive A, 2 = drive B, and so on.
  11690.  The numdrives argument indicates the total number of drives in the system.
  11691.  If this value is 4, for example, it does not mean the drives are designated
  11692.  A, B, C, and D; it means only that four drives are in the system.
  11693.  
  11694.  
  11695.  Return Value
  11696.  
  11697.  There is no return value. If an invalid drive number is passed, the function
  11698.  fails without indication. Use the _dos_getdrive routine to verify whether
  11699.  the desired drive has been set.
  11700.  
  11701.  
  11702.  Compatibility
  11703.  
  11704.   ANSI   DOS   OS/2   UNIX   XENIX
  11705.  
  11706.  
  11707.  
  11708.  See Also
  11709.  
  11710.  _dos_getdiskfree,  _dos_getdrive
  11711.  
  11712.  
  11713.  Example
  11714.  
  11715.    /* DGDRIVE.C: This program prints the letter of the current drive,
  11716.     * changes the default drive to A, then returns the number of disk drives.
  11717.     */
  11718.  
  11719.    #include <stdio.h>
  11720.    #include <dos.h>
  11721.  
  11722.    void main()
  11723.    {
  11724.       unsigned olddrive, newdrive;
  11725.       unsigned number_of_drives;
  11726.  
  11727.       /* Print current default drive information */
  11728.       _dos_getdrive( &olddrive );
  11729.       printf( "The current drive is: %c\n", 'A' + olddrive1 );
  11730.  
  11731.       /* Set default drive to be drive A */
  11732.       printf( "Changing default drive to A\n");
  11733.       _dos_setdrive( 1, &number_of_drives );
  11734.  
  11735.       /* Get new default drive information and total number of drives */
  11736.       _dos_getdrive( &newdrive );
  11737.       printf( "The current drive is: %c\n", 'A' + newdrive1 );
  11738.       printf( "Number of logical drives: %d\n", number_of_drives );
  11739.  
  11740.       /* Restore default drive */
  11741.       _dos_setdrive( olddrive, &number_of_drives );
  11742.    }
  11743.  
  11744.  
  11745.  Output
  11746.  
  11747.  
  11748.  
  11749.    The current drive is: C
  11750.    Changing default drive to A
  11751.    The current drive is: A
  11752.    Number of logical drives: 26
  11753.  
  11754.  
  11755.  
  11756.  
  11757.  
  11758.  _dos_setfileattr
  11759.  ────────────────────────────────────────────────────────────────────────────
  11760.  
  11761.  
  11762.  Description
  11763.  
  11764.  Sets the attributes of the file or directory, using system call INT 0x43.
  11765.  
  11766.    #include <dos.h>
  11767.  
  11768.    unsigned _dos_setfileattr( char *pathname, unsigned attrib );
  11769.  
  11770.  pathname                          Full path of target file/directory
  11771.  
  11772.  attrib                            New attributes
  11773.  
  11774.  
  11775.  Remarks
  11776.  
  11777.  The _dos_setfileattr routine uses system call INT 0x43 to set the attributes
  11778.  of the file or directory pointed to by pathname. The actual attributes are
  11779.  contained in the low-order byte of the attrib word. Attributes are
  11780.  represented by manifest constants, as described below:
  11781.  
  11782.  Constant                          Meaning
  11783.  ────────────────────────────────────────────────────────────────────────────
  11784.  _A_ARCH                           Archive. Set whenever the file is
  11785.                                    changed, or cleared by the DOS BACKUP
  11786.                                    command.
  11787.  
  11788.  _A_HIDDEN                         Hidden file. Cannot be found by a
  11789.                                    directory search.
  11790.  
  11791.  _A_NORMAL                         Normal. File can be read or written to
  11792.                                    without restriction.
  11793.  
  11794.  _A_RDONLY                         Read-only. File cannot be opened for
  11795.                                    writing, and a file with the same name
  11796.                                    cannot be created.
  11797.  
  11798.  _A_SUBDIR                         Subdirectory.
  11799.  
  11800.  _A_SYSTEM                         System file. Cannot be found by a
  11801.                                    directory search.
  11802.  
  11803.  _A_VOLID                          Volume ID. Only one file can have this
  11804.                                    attribute, and it must be in the root
  11805.                                    directory.
  11806.  
  11807.  
  11808.  Return Value
  11809.  
  11810.  The function returns 0 if successful. Otherwise, it returns the DOS error
  11811.  code and sets errno to one of the following:
  11812.  
  11813.  Constant                          Meaning
  11814.  ────────────────────────────────────────────────────────────────────────────
  11815.  EACCES                            Access denied; cannot change the volume
  11816.                                    ID or the
  11817.                                    subdirectory.
  11818.  
  11819.  ENOENT                            No file or directory matching the target
  11820.                                    was found.
  11821.  
  11822.  
  11823.  Compatibility
  11824.  
  11825.   ANSI   DOS   OS/2   UNIX   XENIX
  11826.  
  11827.  
  11828.  
  11829.  See Also
  11830.  
  11831.  _dos_getfileattr
  11832.  
  11833.  
  11834.  Example
  11835.  
  11836.    /* DGFILEAT.C: This program creates a file with the specified attributes,
  11837.     * then prints this information before changing the file attributes back
  11838.     * to normal.
  11839.     */
  11840.  
  11841.    #include <stdio.h>
  11842.    #include <dos.h>
  11843.  
  11844.    void main()
  11845.    {
  11846.       unsigned oldattrib, newattrib;
  11847.       int fh;
  11848.  
  11849.       /* Get and display file attribute */
  11850.       _dos_getfileattr( "DGFILEAT.C", &oldattrib );
  11851.       printf( "Attribute: 0x%.4x\n", oldattrib );
  11852.       if( ( oldattrib & _A_RDONLY ) != 0 )
  11853.          printf( "Read only file\n" );
  11854.       else
  11855.          printf( "Not a read only file.\n" );
  11856.  
  11857.       /* Reset file attribute to normal file */
  11858.       _dos_setfileattr( "DGFILEAT.C", _A_RDONLY );
  11859.       _dos_getfileattr( "DGFILEAT.C", &newattrib );
  11860.       printf( "Attribute: 0x%.4x\n", newattrib );
  11861.  
  11862.       /* Restore file attribute */
  11863.       _dos_setfileattr( "DGFILEAT.C", oldattrib );
  11864.       _dos_getfileattr( "DGFILEAT.C", &newattrib );
  11865.       printf( "Attribute: 0x%.4x\n", newattrib );
  11866.    }
  11867.  
  11868.  
  11869.  Output
  11870.  
  11871.  
  11872.  
  11873.    Attribute: 0x0020
  11874.    Not a read only file.
  11875.    Attribute: 0x0001
  11876.    Attribute: 0x0020
  11877.  
  11878.  
  11879.  
  11880.  
  11881.  
  11882.  _dos_setftime
  11883.  ────────────────────────────────────────────────────────────────────────────
  11884.  
  11885.  
  11886.  Description
  11887.  
  11888.  Sets the date and time for a file, using system call INT 0x57.
  11889.  
  11890.    #include <dos.h>
  11891.  
  11892.    unsigned _dos_setftime( int handle, unsigned date, unsigned time );
  11893.  
  11894.  handle                            Target file
  11895.  
  11896.  date                              Date of last write
  11897.  
  11898.  time                              Time of last write
  11899.  
  11900.  
  11901.  Remarks
  11902.  
  11903.  The _dos_setftime routine uses system call INT 0x57 to set the date and time
  11904.  at which the file identified by handle was last written to. These values
  11905.  appear in the DOS date and time format, described in the following lists:
  11906.  
  11907.  Time Bits                         Meaning
  11908.  ────────────────────────────────────────────────────────────────────────────
  11909.  0 - 4                             Number of two-second increments (0 -29)
  11910.  
  11911.  5 - 10                            Minutes (0 -59)
  11912.  
  11913.  11-15                             Hours (0 -23)
  11914.  
  11915.  Date Bits                         Meaning
  11916.  ────────────────────────────────────────────────────────────────────────────
  11917.  0 - 4                             Day (1-31)
  11918.  
  11919.  5 -8                              Month (1-12)
  11920.  
  11921.  9 -15                             Year since 1980 (for example, 1989 is
  11922.                                    stored as 9)
  11923.  
  11924.  
  11925.  Return Value
  11926.  
  11927.  If successful, the function returns 0. Otherwise, it returns the DOS error
  11928.  code and sets errno to EBADF, indicating that an invalid file handle was
  11929.  passed.
  11930.  
  11931.  
  11932.  Compatibility
  11933.  
  11934.   ANSI   DOS   OS/2   UNIX   XENIX
  11935.  
  11936.  
  11937.  
  11938.  See Also
  11939.  
  11940.  _dos_getftime, fstat, stat
  11941.  
  11942.  
  11943.  Example
  11944.  
  11945.    /* DGFTIME.C: This program displays and modifies the date and time
  11946.     * fields of a file.
  11947.     */
  11948.  
  11949.    #include <fcntl.h>
  11950.    #include <stdio.h>
  11951.    #include <stdlib.h>
  11952.    #include <dos.h>
  11953.  
  11954.    void main()
  11955.    {
  11956.                                     /* FEDC BA98 7654 3210          */
  11957.       unsigned new_date = 0x184f;   /* 0001 1000 0100 1111  2/15/92 */
  11958.       unsigned new_time = 0x48e0;   /* 0100 1000 1110 0000  9:07 AM */
  11959.       unsigned old_date, old_time;
  11960.  
  11961.       int fh;
  11962.  
  11963.       /* Open file with _dos_open function */
  11964.       if( _dos_open( "dgftime.obj", O_RDONLY, &fh ) != 0 )
  11965.          exit( 1 );
  11966.  
  11967.       /* Get file date and time */
  11968.       _dos_getftime( fh, &old_date, &old_time );
  11969.       printf( "Old date field: 0x%.4x\n", old_date );
  11970.       printf( "Old time field: 0x%.4x\n", old_time );
  11971.       system( "dir dgftime.obj" );
  11972.  
  11973.       /* Modify file date and time */
  11974.       if( !_dos_setftime( fh, new_date, new_time ) )
  11975.       {
  11976.          _dos_getftime( fh, &new_date, &new_time );
  11977.          printf( "New date field: 0x%.4x\n", new_date );
  11978.          printf( "New time field: 0x%.4x\n", new_time );
  11979.          system( "dir dgftime.obj" );
  11980.  
  11981.          /* Restore date and time */
  11982.          _dos_setftime( fh, old_date, old_time );
  11983.       }
  11984.       _dos_close( fh );
  11985.    }
  11986.  
  11987.  
  11988.  Output
  11989.  
  11990.  
  11991.  
  11992.    Old date field: 0x12cf
  11993.    Old time field: 0x94bb
  11994.  
  11995.     Volume in drive C is OS2
  11996.     Directory of  C:\LIBREF
  11997.  
  11998.    DGFTIME  OBJ     3923   6-15-89   6:37p
  11999.            1 File(s)  13676544 bytes free
  12000.  
  12001.    New date field: 0x184f
  12002.    New time field: 0x48e0
  12003.  
  12004.     Volume in drive C is OS2
  12005.     Directory of  C:\LIBREF
  12006.  
  12007.    DGFTIME  OBJ     3923   2-15-92   9:07a
  12008.            1 File(s)  13676544 bytes free
  12009.  
  12010.  
  12011.  
  12012.  
  12013.  
  12014.  _dos_settime
  12015.  ────────────────────────────────────────────────────────────────────────────
  12016.  
  12017.  
  12018.  Description
  12019.  
  12020.  Sets the current system time, using system call INT 0x2D.
  12021.  
  12022.    #include <dos.h>
  12023.  
  12024.    unsigned _dos_settime( struct dostime_t *time );
  12025.  
  12026.  time                              New system time
  12027.  
  12028.  
  12029.  Remarks
  12030.  
  12031.  The _dos_settime routine uses system call INT 0x2D to set the current system
  12032.  time to the value stored in the dostime_t structure that time points to, as
  12033.  defined in DOS.H. The dostime_t structure contains the following elements:
  12034.  
  12035.  Element                           Description
  12036.  ────────────────────────────────────────────────────────────────────────────
  12037.  unsigned char hour                0 -23
  12038.  
  12039.  unsigned char minute              0 -59
  12040.  
  12041.  unsigned char second              0 -59
  12042.  
  12043.  unsigned char hsecond             Hundredths of a second; 0 -99
  12044.  
  12045.  
  12046.  Return Value
  12047.  
  12048.  If successful, the function returns 0. Otherwise, it returns a nonzero value
  12049.  and sets errno to EINVAL, indicating an invalid time was specified.
  12050.  
  12051.  
  12052.  Compatibility
  12053.  
  12054.   ANSI   DOS   OS/2   UNIX   XENIX
  12055.  
  12056.  
  12057.  
  12058.  See Also
  12059.  
  12060.  _dos_getdate,  _dos_gettime,  _dos_setdate, gmtime, localtime, mktime,
  12061.  _strdate,  _strtime
  12062.  
  12063.  
  12064.  Example
  12065.  
  12066.    /* DSTIME.C: This program changes the time and date values and displays
  12067.    the
  12068.     * new date and time values.
  12069.     */
  12070.  
  12071.    #include <dos.h>
  12072.    #include <conio.h>
  12073.    #include <stdio.h>
  12074.    #include <time.h>
  12075.  
  12076.    void main()
  12077.    {
  12078.       struct dosdate_t olddate, newdate = { { 4 }, { 7 }, { 1984 } };
  12079.       struct dostime_t oldtime, newtime = { { 3 }, { 45 }, { 30 }, { 0 } };
  12080.       char   datebuf[40], timebuf[40];
  12081.  
  12082.       /* Get current date and time values */
  12083.       _dos_getdate( &olddate );
  12084.       _dos_gettime( &oldtime );
  12085.       printf( "%s    %s\n" , _strdate( datebuf ), _strtime( timebuf ) );
  12086.  
  12087.       /* Modify date and time structures */
  12088.       _dos_setdate( &newdate );
  12089.       _dos_settime( &newtime );
  12090.       printf( "%s    %s\n" , _strdate( datebuf ), _strtime( timebuf ) );
  12091.  
  12092.       /* Restore old date and time */
  12093.       _dos_setdate( &olddate );
  12094.       _dos_settime( &oldtime );
  12095.    }
  12096.  
  12097.  
  12098.  Output
  12099.  
  12100.  
  12101.  
  12102.    06/15/89    18:26:09
  12103.    07/04/84    03:45:30
  12104.  
  12105.  
  12106.  
  12107.  
  12108.  
  12109.  _dos_setvect
  12110.  ────────────────────────────────────────────────────────────────────────────
  12111.  
  12112.  
  12113.  Description
  12114.  
  12115.  Sets the current value of the interrupt vector, using system call INT 0x25.
  12116.  
  12117.  
  12118.    #include <dos.h>
  12119.  
  12120.    void _dos_setvect( unsigned intnum, void( _interrupt _far *handler)( ));
  12121.  
  12122.  intnum                            Target-interrupt vector
  12123.  
  12124.  handler                           Interrupt handler for which to assign
  12125.                                    intnum
  12126.  
  12127.  
  12128.  Remarks
  12129.  
  12130.  The _dos_setvect routine uses system call INT 0x25 to set the current value
  12131.  of the interrupt vector intnum to the function pointed to by handler.
  12132.  Subsequently, whenever the intnum interrupt is generated, the handler
  12133.  routine will be called. If handler is a C function, it must have been
  12134.  previously declared with the interrupt attribute. Otherwise, you must make
  12135.  sure that the function satisfies the requirements for an interrupt-handling
  12136.  routine. For example, if handler is an assembler function, it must be a far
  12137.  routine that returns with an IRET instead of a RET.
  12138.  
  12139.  The interrupt attribute indicates that the function is an interrupt handler.
  12140.  The compiler generates appropriate entry and exit sequences for the
  12141.  interrupt-handling function, including saving and restoring all registers
  12142.  and executing an IRET instruction to return.
  12143.  
  12144.  The _dos_setvect routine is generally used with the _dos_getvect function.
  12145.  To replace an interrupt vector, first save the current vector of the
  12146.  interrupt using _dos_getvect. Then set the vector to your own interrupt
  12147.  routine with _dos_setvect. The saved vector can later be restored, if
  12148.  necessary, using _dos_setvect. The user-defined routine may also need the
  12149.  original vector in order to call it or to chain to it with _chain_intr.
  12150.  
  12151.  
  12152.  Registers and Interrupt Functions
  12153.  
  12154.  When you call an interrupt function, the DS register is initialized to the C
  12155.  data segment. This allows you to access global variables from within an
  12156.  interrupt function.
  12157.  
  12158.  In addition, all registers except SS are saved on the stack. You can access
  12159.  these registers within the function if you declare a function parameter list
  12160.  containing a formal parameter for each saved register. The following example
  12161.  illustrates such a declaration:
  12162.  
  12163.    void _interrupt _far int_handler( unsigned _es, unsigned _ds,
  12164.                                      unsigned _di, unsigned _si,
  12165.                                      unsigned _bp, unsigned _sp,
  12166.                                      unsigned _bx, unsigned _dx,
  12167.                                     unsigned _cx, unsigned _ax,
  12168.                                      unsigned _ip, unsigned _cs,
  12169.                                     unsigned _flags )
  12170.    {
  12171.    .
  12172.    .
  12173.    .
  12174.    }
  12175.  
  12176.  The formal parameters must appear in the opposite order from which they are
  12177.  pushed onto the stack. You can omit parameters from the end of the list in a
  12178.  declaration, but not from the beginning. For example, if your handler needs
  12179.  to use only DI and SI, you must still provide ES and DS, but not necessarily
  12180.  BX or DX.
  12181.  
  12182.  You can pass additional arguments if your interrupt handler will be called
  12183.  directly from C rather than by an INT instruction. To do this, you must
  12184.  declare all register parameters and then declare your parameter at the end
  12185.  of the list.
  12186.  
  12187.  The compiler always saves and restores registers in the same, fixed order.
  12188.  Thus, no matter what names you use in the formal parameter list, the first
  12189.  parameter in the list refers to ES, the second refers to DS, and so on. If
  12190.  your interrupt routines will use in-line assembler, you should distinguish
  12191.  the parameter names so that they will not be the same as the real register
  12192.  names.
  12193.  
  12194.  If you change any of the register parameters of an interrupt function while
  12195.  the function is executing, the corresponding register contains the changed
  12196.  value when the function returns. For example:
  12197.  
  12198.    void _interrupt _far int_handler( unsigned _es, unsigned _ds,
  12199.                                     unsigned _di, unsigned _si )
  12200.    {
  12201.        _di = -1;
  12202.    }
  12203.  
  12204.  This code causes the DI register to contain -1 when the handler function
  12205.  returns. It is not a good idea to modify the values of the parameters
  12206.  representing the IP and CS registers in interrupt functions. If you must
  12207.  modify a particular flag (such as the carry flag for certain DOS and BIOS
  12208.  interrupt routines), use the OR operator ( | ) so that other bits in the
  12209.  flag register are not changed.
  12210.  
  12211.  When an interrupt function is called by an INT instruction, the
  12212.  interrupt-enable flag is cleared. If your interrupt function needs to do
  12213.  significant processing, you should use the _enable function to set the
  12214.  interrupt flag so that interrupts can be handled.
  12215.  
  12216.  
  12217.  Precautions for Interrupt Functions
  12218.  
  12219.  Since DOS is not reentrant (a DOS interrupt cannot be called from inside a
  12220.  DOS interrupt), it is usually not safe to call from inside an interrupt
  12221.  function any standard library function that calls DOS INT 21H. Similar
  12222.  precautions apply to many BIOS functions. Functions that rely on INT 21H
  12223.  calls include I/O functions and the _dos family of functions. Functions that
  12224.  rely on the machine's BIOS include graphics functions and the _bios family
  12225.  of functions. It is usually safe to use functions that do not rely on INT
  12226.  21H or BIOS, such as string-handling functions. Before using a standard
  12227.  library function in an interrupt function, be sure that you are familiar
  12228.  with the action of the library function.
  12229.  
  12230.  
  12231.  Return Value
  12232.  
  12233.  None.
  12234.  
  12235.  
  12236.  Compatibility
  12237.  
  12238.   ANSI   DOS   OS/2   UNIX   XENIX
  12239.  
  12240.  
  12241.  
  12242.  See Also
  12243.  
  12244.  _chain_intr,  _dos_getvect,  _dos_keep
  12245.  
  12246.  
  12247.  
  12248.  
  12249.  
  12250.  _dos_write
  12251.  ────────────────────────────────────────────────────────────────────────────
  12252.  
  12253.  
  12254.  Description
  12255.  
  12256.  Writes a buffer to a file, using system call INT 0x40.
  12257.  
  12258.    #include <dos.h>
  12259.  
  12260.    unsigned _dos_write( int handle, void _far *buffer, unsigned count,
  12261.    unsigned *numwrt );
  12262.  
  12263.  handle                            File to write to
  12264.  
  12265.  buffer                            Buffer to write from
  12266.  
  12267.  count                             Number of bytes to write
  12268.  
  12269.  numwrt                            Number of bytes actually written
  12270.  
  12271.  
  12272.  Remarks
  12273.  
  12274.  The _dos_write routine uses system call INT 0x40 to write data to the file
  12275.  that handle references; count bytes of data from the buffer to which buffer
  12276.  points are written to the file. The integer pointed to by numwrt will be the
  12277.  number of bytes actually written, which may be less than the number
  12278.  requested.
  12279.  
  12280.  Do not use the DOS interface routines with the console, low-level, or stream
  12281.  I/O routines.
  12282.  
  12283.  
  12284.  Return Value
  12285.  
  12286.  If successful, the function returns 0. Otherwise, it returns the DOS error
  12287.  code and sets errno to one of the following manifest constants:
  12288.  
  12289.  Constant                          Meaning
  12290.  ────────────────────────────────────────────────────────────────────────────
  12291.  EACCES                            Access denied (handle references a file
  12292.                                    not open for write
  12293.                                    access)
  12294.  
  12295.  EBADF                             Invalid file handle
  12296.  
  12297.  
  12298.  Compatibility
  12299.  
  12300.   ANSI   DOS   OS/2   UNIX   XENIX
  12301.  
  12302.  
  12303.  
  12304.  See Also
  12305.  
  12306.  _dos_close,  _dos_open,  _dos_read, write
  12307.  
  12308.  
  12309.  Example
  12310.  
  12311.    /* DWRITE.C: This program uses DOS I/O functions to write to a file. */
  12312.  
  12313.    #include <fcntl.h>
  12314.    #include <stdio.h>
  12315.    #include <stdlib.h>
  12316.    #include <dos.h>
  12317.  
  12318.    void main()
  12319.    {
  12320.       char out_buffer[] = "Hello";
  12321.       int  fh;
  12322.       unsigned n_written;
  12323.  
  12324.       /* Open file with _dos_creat function */
  12325.       if( _dos_creat( "data", _A_NORMAL, &fh ) == 0 )
  12326.       {
  12327.          /* Write data with _dos_write function */
  12328.          _dos_write( fh, out_buffer, 5, &n_written );
  12329.          printf( "Number of characters written: %d\n", n_written );
  12330.  
  12331.          _dos_close( fh );
  12332.          printf( "Contents of file are:\n" );
  12333.          system( "type data" );
  12334.       }
  12335.    }
  12336.  
  12337.  
  12338.  Output
  12339.  
  12340.  
  12341.  
  12342.    Number of characters written: 5
  12343.    Contents of file are:
  12344.    Hello
  12345.  
  12346.  
  12347.  
  12348.  
  12349.  
  12350.  dosexterr
  12351.  ────────────────────────────────────────────────────────────────────────────
  12352.  
  12353.  
  12354.  Description
  12355.  
  12356.  Gets register values returned by INT 0x59.
  12357.  
  12358.    #include <dos.h>
  12359.  
  12360.    int dosexterr( struct DOSERROR *errorinfo );
  12361.  
  12362.  errorinfo                         Extended DOS error information
  12363.  
  12364.  
  12365.  Remarks
  12366.  
  12367.  The dosexterr function obtains the extended error information returned by
  12368.  the DOS system call INT 0x59 and stores the values in the structure pointed
  12369.  to by errorinfo. This function is useful when making system calls under DOS
  12370.  versions 3.0 or later, which offer extended error handling.
  12371.  
  12372.  The structure type DOSERROR is defined in DOS.H. The DOSERROR structure
  12373.  contains the following elements:
  12374.  
  12375.  Element                           Description
  12376.  ────────────────────────────────────────────────────────────────────────────
  12377.  int exterror                      AX register contents
  12378.  
  12379.  char class                        BH register contents
  12380.  
  12381.  char action                       BL register contents
  12382.  
  12383.  char locus                        CH register contents
  12384.  
  12385.  Giving a NULL pointer argument causes dosexterr to return the value in AX
  12386.  without filling in the structure fields. See MS-DOS Encyclopedia  (Duncan,
  12387.  ed.; Redmond, WA: Microsoft Press, 1988) or Programmer's PC Sourcebook
  12388.  (Hogan; Redmond, WA: Microsoft Press, 1988) for more information on the
  12389.  register contents.
  12390.  
  12391.  
  12392.  Return Value
  12393.  
  12394.  The dosexterr function returns the value in the AX register (identical to
  12395.  the value in the exterror structure field).
  12396.  
  12397.  
  12398.  Compatibility
  12399.  
  12400.   ANSI   DOS   OS/2   UNIX   XENIX
  12401.  
  12402.  
  12403.  The dosexterr function should be used only under DOS versions 3.0 or later.
  12404.  
  12405.  
  12406.  
  12407.  See Also
  12408.  
  12409.  perror
  12410.  
  12411.  
  12412.  Example
  12413.  
  12414.    /* DOSEXERR.C: This program tries to open the file test.dat. If the
  12415.     * attempted open operation fails, the program uses dosexterr to display
  12416.     * extended error information.
  12417.     */
  12418.  
  12419.    #include <dos.h>
  12420.    #include <io.h>
  12421.    #include <fcntl.h>
  12422.    #include <stdio.h>
  12423.  
  12424.    void main()
  12425.    {
  12426.       struct DOSERROR doserror;
  12427.       int fd;
  12428.  
  12429.       /* Attempt to open a non-existent file */
  12430.       if( (fd = open( "NOSUCHF.ILE", O_RDONLY )) == -1 )
  12431.       {
  12432.          dosexterr( &doserror );
  12433.          printf( "Error: %d  Class: %d  Action: %d  Locus: %d\n",
  12434.                  doserror.exterror, doserror.class,
  12435.                  doserror.action,   doserror.locus );
  12436.       }
  12437.       else
  12438.       {
  12439.          printf( "Open succeeded so no extended information printed\n" );
  12440.          close( fd );
  12441.       }
  12442.    }
  12443.  
  12444.  
  12445.  Output
  12446.  
  12447.  
  12448.  
  12449.    Error: 2  Class: 8  Action: 3  Locus: 2
  12450.  
  12451.  
  12452.  
  12453.  
  12454.  
  12455.  dup, dup2
  12456.  ────────────────────────────────────────────────────────────────────────────
  12457.  
  12458.  
  12459.  Description
  12460.  
  12461.  Create a second handle for an open file (dup), or reassign a file handle
  12462.  (dup2).
  12463.  
  12464.  #include <io.h                    Required only for function declarations
  12465.  
  12466.    int dup( int handle );
  12467.  
  12468.    int dup2( int handle1, int handle2 );
  12469.  
  12470.  handle, handle1                   Handle referring to open file
  12471.  
  12472.  handle2                           Any handle value
  12473.  
  12474.  
  12475.  Remarks
  12476.  
  12477.  The dup and dup2 functions cause a second file handle to be associated with
  12478.  a currently open file. Operations on the file can be carried out using
  12479.  either file handle. The type of access allowed for the file is unaffected by
  12480.  the creation of a new handle.
  12481.  
  12482.  The dup function returns the next available file handle for the given file.
  12483.  The dup2 function forces handle2 to refer to the same file as handle1. If
  12484.  handle2 is associated with an open file at the time of the call, that file
  12485.  is closed.
  12486.  
  12487.  
  12488.  Return Value
  12489.  
  12490.  The dup function returns a new file handle. The dup2 function returns 0 to
  12491.  indicate success. Both functions return -1 if an error occurs and set errno
  12492.  to one of the following values:
  12493.  
  12494.  Value                             Meaning
  12495.  ────────────────────────────────────────────────────────────────────────────
  12496.  EBADF                             Invalid file handle
  12497.  
  12498.  EMFILE                            No more file handles available (too many
  12499.                                    open files)
  12500.  
  12501.  
  12502.  Compatibility
  12503.  
  12504.   ANSI   DOS   OS/2   UNIX   XENIX
  12505.  
  12506.  
  12507.  
  12508.  See Also
  12509.  
  12510.  close, creat, open
  12511.  
  12512.  
  12513.  Example
  12514.  
  12515.    /* DUP.C: This program uses the variable old to save the original stdout.
  12516.     * It then opens a new file named new and forces stdout to refer
  12517.     * to it. Finally, it restores stdout to its original state.
  12518.     */
  12519.  
  12520.    #include <io.h>
  12521.    #include <stdlib.h>
  12522.    #include <stdio.h>
  12523.    void main()
  12524.    {
  12525.       int old;
  12526.       FILE *new;
  12527.  
  12528.       old = dup( 1 );   /* "old" now refers to "stdout" */
  12529.                         /* Note:  file handle 1 == "stdout" */
  12530.       if( old == -1 )
  12531.       {
  12532.          perror( "dup( 1 ) failure" );
  12533.          exit( 1 );
  12534.       }
  12535.       write( old, "This goes to stdout first\r\n", 27 );
  12536.       if( ( new = fopen( "data", "w" ) ) == NULL )
  12537.       {
  12538.          puts( "Can't open file 'data'\n" );
  12539.          exit( 1 );
  12540.       }
  12541.  
  12542.       /* stdout now refers to file "data" */
  12543.       if( -1 == dup2( fileno( new ), 1 ) )
  12544.       {
  12545.          perror( "Can't dup2 stdout" );
  12546.          exit( 1 );
  12547.       }
  12548.       puts( "This goes to file 'data'\r\n" );
  12549.  
  12550.       /* Flush stdout stream buffer so it goes to correct file */
  12551.       fflush( stdout );
  12552.       fclose( new );
  12553.  
  12554.       /* Restore original stdout */
  12555.       dup2( old, 1 );
  12556.       puts( "This goes to stdout\n" );
  12557.       puts( "The file 'data' contains:" );
  12558.       system( "type data" );
  12559.    }
  12560.  
  12561.  
  12562.  Output
  12563.  
  12564.  
  12565.  
  12566.    This goes to stdout first
  12567.    This goes to stdout
  12568.  
  12569.    The file 'data' contains:
  12570.    This goes to file 'data'
  12571.  
  12572.  
  12573.  
  12574.  
  12575.  
  12576.  ecvt
  12577.  ────────────────────────────────────────────────────────────────────────────
  12578.  
  12579.  
  12580.  Description
  12581.  
  12582.  Converts a double number to a string.
  12583.  
  12584.  #include <stdlib.h>               Required only for function declarations
  12585.  
  12586.    char *ecvt( double value, int count, int *dec, int *sign );
  12587.  
  12588.  value                             Number to be converted
  12589.  
  12590.  count                             Number of digits stored
  12591.  
  12592.  dec                               Stored decimal-point position
  12593.  
  12594.  sign                              Sign of converted number
  12595.  
  12596.  
  12597.  Remarks
  12598.  
  12599.  The ecvt function converts a floating-point number to a character string.
  12600.  The value argument is the floating-point number to be converted. The ecvt
  12601.  function stores up to count digits of value as a string and appends a null
  12602.  character ('\0'). If the number of digits in value exceeds count, the
  12603.  low-order digit is rounded. If there are fewer than count digits, the string
  12604.  is padded with zeros.
  12605.  
  12606.  Only digits are stored in the string. The position of the decimal point and
  12607.  the sign of value can be obtained from dec and sign after the call. The dec
  12608.  argument points to an integer value giving the position of the decimal point
  12609.  with respect to the beginning of the string. A 0 or negative integer value
  12610.  indicates that the decimal point lies to the left of the first digit. The
  12611.  sign argument points to an integer indicating the sign of the converted
  12612.  number. If the integer value is 0, the number is positive. Otherwise, the
  12613.  number is negative.
  12614.  
  12615.  The ecvt and fcvt functions use a single statically allocated buffer for the
  12616.  conversion. Each call to one of these routines destroys the result of the
  12617.  previous call.
  12618.  
  12619.  
  12620.  Return Value
  12621.  
  12622.  The ecvt function returns a pointer to the string of digits. There is no
  12623.  error return.
  12624.  
  12625.  
  12626.  Compatibility
  12627.  
  12628.   ANSI   DOS   OS/2   UNIX   XENIX
  12629.  
  12630.  
  12631.  
  12632.  See Also
  12633.  
  12634.  atof, atoi, atol, fcvt, gcvt
  12635.  
  12636.  
  12637.  Example
  12638.  
  12639.    /* ECVT.C: This program uses ecvt to convert a floating-point
  12640.     * number to a character string.
  12641.     */
  12642.  
  12643.    #include <stdlib.h>
  12644.    #include <stdio.h>
  12645.  
  12646.    void main()
  12647.    {
  12648.       int     decimal, sign;
  12649.       char    *buffer;
  12650.       int     precision = 10;
  12651.       double  source = 3.1415926535;
  12652.  
  12653.       buffer = ecvt( source, precision, &decimal, &sign );
  12654.       printf( "source: %2.10f   buffer: '%s'  decimal: %d   sign: %d\n",
  12655.               source, buffer, decimal, sign );
  12656.    }
  12657.  
  12658.  
  12659.  Output
  12660.  
  12661.  
  12662.  
  12663.    source: 3.1415926535   buffer: '3141592654'  decimal: 1   sign: 0
  12664.  
  12665.  
  12666.  
  12667.  
  12668.  
  12669.  _ellipse Functions
  12670.  ────────────────────────────────────────────────────────────────────────────
  12671.  
  12672.  
  12673.  Description
  12674.  
  12675.  Draw ellipses.
  12676.  
  12677.    #include <graph.h>
  12678.  
  12679.    short _far _ellipse( short control, short x1, short y1, short x2, short y2
  12680.    );
  12681.  
  12682.    short _far _ellipse_w( short control, double wx1, double wy1, double wx2,
  12683.    double wy2 );
  12684.  
  12685.    short _far _ellipse_wxy( short control, struct _wxycoord _far *pwxy1,
  12686.    struct _wxycoord _far *pwxy2 );
  12687.  
  12688.  control                           Fill flag
  12689.  
  12690.  x1, y1                            Upper-left corner of bounding rectangle
  12691.  
  12692.  x2, y2                            Lower-right corner of bounding rectangle
  12693.  
  12694.  wx1, wy1                          Upper-left corner of bounding rectangle
  12695.  
  12696.  wx2, wy2                          Lower-right corner of bounding rectangle
  12697.  
  12698.  pwxy1                             Upper-left corner of bounding rectangle
  12699.  
  12700.  pwxy2                             Lower-right corner of bounding rectangle
  12701.  
  12702.  
  12703.  Remarks
  12704.  
  12705.  The _ellipse functions draw ellipses or circles. The borders are drawn in
  12706.  the current color. In the _ellipse function, the center of the ellipse is
  12707.  the center of the bounding rectangle defined by the view-coordinate points
  12708.  (x1, y1) and (x2, y2).
  12709.  
  12710.  In the _ellipse_w function, the center of the ellipse is the center of the
  12711.  bounding rectangle defined by the window-coordinate points (wx1, wy1) and
  12712.  (wx2, wy2).
  12713.  
  12714.  In the _ellipse_wxy function, the center of the ellipse is the center of the
  12715.  bounding rectangle defined by the window-coordinate pairs (pwxy1) and
  12716.  (pwxy2).
  12717.  
  12718.  If the bounding-rectangle arguments define a point or a vertical or
  12719.  horizontal line, no figure is drawn.
  12720.  
  12721.  The control argument can be one of the following manifest constants:
  12722.  
  12723.  Constant                          Action
  12724.  ────────────────────────────────────────────────────────────────────────────
  12725.  _GFILLINTERIOR                    Fills the ellipse using the current fill
  12726.                                    mask
  12727.  
  12728.  _GBORDER                          Does not fill the ellipse
  12729.  
  12730.  The control option given by _GFILLINTERIOR is equivalent to a subsequent
  12731.  call to the _floodfill function, using the center of the ellipse as the
  12732.  starting point and the current color (set by _setcolor) as the boundary
  12733.  color.
  12734.  
  12735.  
  12736.  Return Value
  12737.  
  12738.  The _ellipse functions return a nonzero value if the ellipse is drawn
  12739.  successfully; otherwise, they return 0.
  12740.  
  12741.  
  12742.  Compatibility
  12743.  
  12744.   ANSI   DOS   OS/2   UNIX   XENIX
  12745.  
  12746.  
  12747.  
  12748.  See Also
  12749.  
  12750.  _arc functions,  _floodfill,  _grstatus,  _lineto functions,  _pie
  12751.  functions, _polygon functions,  _rectangle functions,  _setcolor,
  12752.  _setfillmask
  12753.  
  12754.  
  12755.  Example
  12756.  
  12757.    /* ELLIPSE.C: This program draws a simple ellipse. */
  12758.  
  12759.    #include <conio.h>
  12760.    #include <stdlib.h>
  12761.    #include <graph.h>
  12762.  
  12763.    void main()
  12764.    {
  12765.       /* Find a valid graphics mode. */
  12766.       if( !_setvideomode( _MAXRESMODE ) )
  12767.          exit( 1 );
  12768.  
  12769.       _ellipse( _GFILLINTERIOR, 80, 50, 240, 150 );
  12770.  
  12771.       /* Strike any key to clear screen. */
  12772.       getch();
  12773.       _setvideomode( _DEFAULTMODE );
  12774.    }
  12775.  
  12776.  
  12777.  
  12778.  
  12779.  
  12780.  _enable
  12781.  ────────────────────────────────────────────────────────────────────────────
  12782.  
  12783.  
  12784.  Description
  12785.  
  12786.  Enables interrupts.
  12787.  
  12788.    #include <dos.h>
  12789.  
  12790.    void _enable( void );
  12791.  
  12792.  
  12793.  Remarks
  12794.  
  12795.  The _enable routine enables interrupts by executing an 8086 STI machine
  12796.  instruction.
  12797.  
  12798.  
  12799.  Return Value
  12800.  
  12801.  None.
  12802.  
  12803.  
  12804.  Compatibility
  12805.  
  12806.   ANSI   DOS   OS/2   UNIX   XENIX
  12807.  
  12808.  
  12809.  
  12810.  See Also
  12811.  
  12812.  _disable
  12813.  
  12814.  
  12815.  
  12816.  
  12817.  
  12818.  _endthread
  12819.  ────────────────────────────────────────────────────────────────────────────
  12820.  
  12821.  
  12822.  Description
  12823.  
  12824.  Terminates an OS/2 thread.
  12825.  
  12826.  #include <process.h>              Multithread version of PROCESS.H
  12827.  
  12828.    void _far _endthread( void );
  12829.  
  12830.  
  12831.  Description
  12832.  
  12833.  The _endthread function terminates a thread created by _beginthread.
  12834.  
  12835.  Because threads terminate automatically, the _endthread function is normally
  12836.  not required. It is used to terminate a thread conditionally.
  12837.  
  12838.  The OS/2 function DosExit should not be used to terminate threads created by
  12839.  the _beginthread function. If DosExit is used, the results are
  12840.  unpredictable.
  12841.  
  12842.  
  12843.  Return Value
  12844.  
  12845.  None.
  12846.  
  12847.  
  12848.  Compatibility
  12849.  
  12850.   ANSI   DOS   OS/2   UNIX   XENIX
  12851.  
  12852.  
  12853.  
  12854.  See Also
  12855.  
  12856.  _beginthread
  12857.  
  12858.  
  12859.  Example
  12860.  
  12861.  See the example for _beginthread.
  12862.  
  12863.  
  12864.  
  12865.  
  12866.  
  12867.  eof
  12868.  ────────────────────────────────────────────────────────────────────────────
  12869.  
  12870.  
  12871.  Description
  12872.  
  12873.  Tests for end-of-file.
  12874.  
  12875.  #include <io.h>                   Required only for function declarations
  12876.  
  12877.    int eof( int handle );
  12878.  
  12879.  handle                            Handle referring to open file
  12880.  
  12881.  
  12882.  Remarks
  12883.  
  12884.  The eof function determines whether the end of the file associated with
  12885.  handle has been reached.
  12886.  
  12887.  
  12888.  Return Value
  12889.  
  12890.  The eof function returns the value 1 if the current position is end-of-file,
  12891.  or 0 if it is not. A return value of -1 indicates an error; in this case,
  12892.  errno is set to EBADF, indicating an invalid file handle.
  12893.  
  12894.  
  12895.  Compatibility
  12896.  
  12897.   ANSI   DOS   OS/2   UNIX   XENIX
  12898.  
  12899.  
  12900.  
  12901.  See Also
  12902.  
  12903.  clearerr, feof, ferror, perror
  12904.  
  12905.  
  12906.  Example
  12907.  
  12908.    /* EOF.C: This program reads data from a file ten bytes at a time
  12909.     * until the end of the file is reached or an error is encountered.
  12910.     */
  12911.  
  12912.    #include <io.h>
  12913.    #include <fcntl.h>
  12914.    #include <stdio.h>
  12915.    #include <stdlib.h>
  12916.  
  12917.    void main()
  12918.    {
  12919.       int  fh, count, total = 0;
  12920.       char buf[10];
  12921.  
  12922.       if( (fh = open( "eof.c", O_RDONLY )) == - 1 )
  12923.          exit( 1 );
  12924.  
  12925.    /* Cycle until end of file reached: */
  12926.       while( !eof( fh ) )
  12927.       {
  12928.          /* Attempt to read in 10 bytes: */
  12929.          if( (count = read( fh, buf, 10 )) == -1 )
  12930.          {
  12931.             perror( "Read error" );
  12932.             break;
  12933.          }
  12934.  
  12935.          /* Total up actual bytes read */
  12936.          total += count;
  12937.       }
  12938.       printf( "Number of bytes read = %d\n", total );
  12939.       close( fh );
  12940.    }
  12941.  
  12942.  
  12943.  Output
  12944.  
  12945.  
  12946.  
  12947.    Number of bytes read = 715
  12948.  
  12949.  
  12950.  
  12951.  
  12952.  
  12953.  exec Functions
  12954.  ────────────────────────────────────────────────────────────────────────────
  12955.  
  12956.  
  12957.  Description
  12958.  
  12959.  Load and execute new child processes.
  12960.  
  12961.  #include <process.h>              Required only for function declarations
  12962.  
  12963.    int execl( char *cmdname, char *arg0, ... char *argn, NULL );
  12964.  
  12965.    int execle( char *cmdname, char *arg0, ... char *argn, NULL,
  12966.    char **envp );
  12967.  
  12968.    int execlp( char *cmdname, char *arg0, ... char *argn, NULL  );
  12969.  
  12970.    int execlpe( char *cmdname, char *arg0, ... char *argn, NULL,
  12971.    char **envp  );
  12972.  
  12973.    int execv( char *cmdname, char **argv );
  12974.  
  12975.    int execve( char *cmdname, char **argv, char **envp );
  12976.  
  12977.    int execvp( char *cmdname, char **argv );
  12978.  
  12979.    int execvpe( char *cmdname, char **argv, char **envp );
  12980.  
  12981.  cmdname                           Path name of file to be executed
  12982.  
  12983.  arg0, ... argn                    List of pointers to arguments
  12984.  
  12985.  argv                              Array of pointers to arguments
  12986.  
  12987.  envp                              Array of pointers to environment
  12988.                                    settings
  12989.  
  12990.  
  12991.  Remarks
  12992.  
  12993.  The exec functions load and execute new child processes. When the call is
  12994.  successful in DOS, the child process is placed in the memory previously
  12995.  occupied by the calling process. Under OS/2, calling an exec function is
  12996.  equivalent to calling the corresponding function with the P_NOWAITO argument
  12997.  specified, followed by a call to the exit function. Sufficient memory must
  12998.  be available for loading and executing the child process.
  12999.  
  13000.  All of the exec functions use the same operating system function. The
  13001.  letter(s) at the end of the function name determine the specific variation,
  13002.  as shown in the following list:
  13003.  
  13004.  Letter                            Variation
  13005.  ────────────────────────────────────────────────────────────────────────────
  13006.  e                                 An array of pointers to environment
  13007.                                    arguments is explicitly passed to the
  13008.                                    child process.
  13009.  
  13010.  l                                 Command-line arguments are passed
  13011.                                    individually to the exec function.
  13012.  
  13013.  p                                 Uses the PATH environment variable to
  13014.                                    find the file to be
  13015.                                    executed.
  13016.  
  13017.  v                                 Command-line arguments are passed to the
  13018.                                    exec function as an array of pointers.
  13019.  
  13020.  The cmdname argument specifies the file to be executed as the child process.
  13021.  It can specify a full path (from the root), a partial path (from the current
  13022.  working directory), or just a file name. If cmdname does not have a
  13023.  file-name extension or does not end with a period (.), the exec function
  13024.  searches for the named file; if the search is unsuccessful, it tries the
  13025.  same base name, first with the extension .COM, then with the extension .EXE.
  13026.  If cmdname has an extension, only that extension is used in the search. If
  13027.  cmdname ends with a period, the exec calls search for cmdname with no
  13028.  extension. The execlp, execlpe, execvp, and execvpe routines search for
  13029.  cmdname (using the same procedures) in the directories specified by the PATH
  13030.  environment variable.
  13031.  
  13032.  If cmdname contains a drive specifier or any slashes (i.e., if it is a
  13033.  relative path name), the exec call searches only for the specified file and
  13034.  no path searching is done.
  13035.  
  13036.  Arguments are passed to the new process by giving one or more pointers to
  13037.  character strings as arguments in the exec call. These character strings
  13038.  form the argument list for the child process. The combined length of the
  13039.  strings forming the argument list for the new process must not exceed 128
  13040.  bytes (in real mode only). The terminating null character ('\0') for each
  13041.  string is not included in the count, but space characters (inserted
  13042.  automatically to separate the arguments) are counted.
  13043.  
  13044.  The argument pointers can be passed as separate arguments (execl, execle,
  13045.  execlp, and execlpe) or as an array of pointers (execv, execve, execvp, and
  13046.  execvpe). At least one argument, arg0, must be passed to the child process;
  13047.  this argument is argv[0] of the child process. Usually, this argument is a
  13048.  copy of the cmdname argument. (A different value will not produce an error.)
  13049.  Under versions of DOS earlier than 3.0, the passed value of arg0 is not
  13050.  available for use in the child process. However, under OS/2 and under DOS
  13051.  versions 3.0 and later, cmdname is available as arg0.
  13052.  
  13053.  The execl, execle, execlp, and execlpe calls are typically used when the
  13054.  number of arguments is known in advance. The argument arg0 is usually a
  13055.  pointer to cmdname. The arguments arg1 through argn point to the character
  13056.  strings forming the new argument list. A null pointer must follow argn to
  13057.  mark the end of the argument list.
  13058.  
  13059.  The execv, execve, execvp, and execvpe calls are useful when the number of
  13060.  arguments to the new process is variable. Pointers to the arguments are
  13061.  passed as an array, argv. The argument argv[0] is usually a pointer to
  13062.  cmdname. The arguments argv[1] through argv[n] point to the character
  13063.  strings forming the new argument list. The argument argv[n+1] must be a NULL
  13064.  pointer to mark the end of the argument list.
  13065.  
  13066.  Files that are open when an exec call is made remain open in the new
  13067.  process. In the execl, execlp, execv, and execvp calls, the child process
  13068.  inherits the environment of the parent. The execle, execlpe, execve, and
  13069.  execvpe calls allow the user to alter the environment for the child process
  13070.  by passing a list of environment settings through the envp argument. The
  13071.  argument envp is an array of character pointers, each element of which
  13072.  (except for the final element) points to a null-terminated string defining
  13073.  an environment variable. Such a string usually has the form
  13074.  
  13075.  NAME=value
  13076.  
  13077.  where NAME is the name of an environment variable and value is the string
  13078.  value to which that variable is set. (Note that value is not enclosed in
  13079.  double quotation marks.) The final element of the envp array should be NULL.
  13080.  When envp itself is NULL, the child process inherits the environment
  13081.  settings of the parent process.
  13082.  
  13083.  A program executed with one of the exec family of functions is always loaded
  13084.  into memory as if the "maximum allocation" field in the program's .EXE file
  13085.  header is set to the default value of 0FFFFH. You can use the EXEMOD utility
  13086.  to change the maximum allocation field of a program; however, such a program
  13087.  invoked with one of the exec functions may behave differently from a program
  13088.  invoked directly from the operating-system command line or with one of the
  13089.  spawn functions.
  13090.  
  13091.  The exec calls do not preserve the translation modes of open files. If the
  13092.  child process must use files inherited from the parent, the setmode routine
  13093.  should be used to set the translation mode of these files to the desired
  13094.  mode.
  13095.  
  13096.  You must explicitly flush (using fflush or flushall) or close any stream
  13097.  prior to the exec function call.
  13098.  
  13099.  Signal settings are not preserved in child processes that are created by
  13100.  calls to exec routines. The signal settings are reset to the default in the
  13101.  child process.
  13102.  
  13103.  
  13104.  Return Value
  13105.  
  13106.  The exec functions do not normally return to the calling process. If an exec
  13107.  function returns, an error has occurred and the return value is -1. The
  13108.  errno variable is set to one of the following values:
  13109.  
  13110.  Value                             Meaning
  13111.  ────────────────────────────────────────────────────────────────────────────
  13112.  E2BIG                             The argument list exceeds 128 bytes, or
  13113.                                    the space required for the environment
  13114.                                    information exceeds 32K.
  13115.  
  13116.  EACCES                            The specified file has a locking or
  13117.                                    sharing violation
  13118.                                    (OS/2, and DOS versions 3.0 or later).
  13119.  
  13120.  EMFILE                            Too many files open (the specified file
  13121.                                    must be opened to determine whether it
  13122.                                    is executable).
  13123.  
  13124.  ENOENT                            File or path name not found.
  13125.  
  13126.  ENOEXEC                           The specified file is not executable or
  13127.                                    has an invalid
  13128.                                    executable-file format.
  13129.  
  13130.  ENOMEM                            Not enough memory is available to
  13131.                                    execute the child process; or the
  13132.                                    available memory has been corrupted; or
  13133.                                    an invalid block exists, indicating that
  13134.                                    the parent process was not allocated
  13135.                                    properly.
  13136.  
  13137.  
  13138.  Compatibility
  13139.  
  13140.   ANSI   DOS   OS/2   UNIX   XENIX
  13141.  
  13142.  
  13143.  Because of differences in DOS versions 2.0 and 2.1, child processes
  13144.  generated by the exec family of functions (or by the equivalent spawn
  13145.  functions with the P_OVERLAY argument) may cause fatal system errors when
  13146.  they exit. If you are running DOS 2.0 or 2.1, you must upgrade to DOS
  13147.  version 3.0 or later to use these functions.
  13148.  
  13149.  Bound programs cannot use the exec family of functions in real mode.
  13150.  
  13151.  
  13152.  See Also
  13153.  
  13154.  abort, atexit, exit, _exit, onexit, spawn functions, system
  13155.  
  13156.  
  13157.  Example
  13158.  
  13159.    /* EXEC.C: This program accepts a number in the range 1 through 8 from the
  13160.     * command line. Based on the number it receives, it executes one of the
  13161.     * eight different procedures that spawn the process named child. For
  13162.     * some of these procedures, the child.exe file must be in the same
  13163.     * directory; for others, it need only be in the same path.
  13164.     */
  13165.  
  13166.    #include <stdio.h>
  13167.    #include <process.h>
  13168.  
  13169.    char *my_env[] = {
  13170.                  "THIS=environment will be",
  13171.                  "PASSED=to child.exe by the",
  13172.                  "EXECLE=and",
  13173.                  "EXECLPE=and",
  13174.                  "EXECVE=and",
  13175.                  "EXECVPE=functions",
  13176.                  NULL
  13177.                  };
  13178.  
  13179.    void main( int argc, char *argv[] )
  13180.    {
  13181.       char *args[4];
  13182.       int result;
  13183.  
  13184.       args[0] = "child";     /* Set up parameters to send */
  13185.       args[1] = "execv??";
  13186.       args[2] = "two";
  13187.       args[3] = NULL;
  13188.  
  13189.    switch( argv[1][0] )   /* Based on first letter of argument */
  13190.       {
  13191.          case '1':
  13192.             execl( argv[2], argv[2], "execl", "two", NULL );
  13193.             break;
  13194.          case '2':
  13195.             execle( argv[2], argv[2], "execle", "two", NULL, my_env );
  13196.             break;
  13197.          case '3':
  13198.             execlp( argv[2], argv[2], "execlp", "two", NULL );
  13199.             break;
  13200.          case '4':
  13201.             execlpe( argv[2], argv[2], "execlpe", "two", NULL, my_env );
  13202.             break;
  13203.          case '5':
  13204.             execv( argv[2], args );
  13205.             break;
  13206.          case '6':
  13207.             execve( argv[2], args, my_env );
  13208.             break;
  13209.          case '7':
  13210.             execvp( argv[2], args );
  13211.             break;
  13212.          case '8':
  13213.             execvpe( argv[2], args, my_env );
  13214.             break;
  13215.          default:
  13216.             printf( "SYNTAX: EXEC <1-8> <childprogram>\n" );
  13217.             exit( 1 );
  13218.       }
  13219.       printf( "Process was not spawned.\n" );
  13220.       printf( "Program 'child' was not found." );
  13221.    }
  13222.  
  13223.  
  13224.  
  13225.  
  13226.  
  13227.  exit, _exit
  13228.  ────────────────────────────────────────────────────────────────────────────
  13229.  
  13230.  
  13231.  Description
  13232.  
  13233.  Terminate the calling process after cleanup (exit) or immediately ( _exit).
  13234.  
  13235.  
  13236.  #include <process.h>              Required only for function declarations
  13237.  
  13238.  #include <stdlib.h>               Use either PROCESS.H or STDLIB.H
  13239.  
  13240.    void exit( int status );
  13241.  
  13242.    void _exit( int status );
  13243.  
  13244.  status                            Exit status
  13245.  
  13246.  
  13247.  Remarks
  13248.  
  13249.  The exit and _exit functions terminate the calling process. The exit
  13250.  function first calls, in LIFO (last-in-first-out) order, the functions
  13251.  registered by atexit and onexit, then flushes all file buffers before
  13252.  terminating the process. The _exit function terminates the process without
  13253.  processing atexit or onexit functions or flushing stream buffers. The status
  13254.  value is typically set to 0 to indicate a normal exit and set to some other
  13255.  value to indicate an error.
  13256.  
  13257.  Although the exit and _exit calls do not return a value, the low-order byte
  13258.  of status is made available to the waiting parent process, if one exists,
  13259.  after the calling process exits. The status value is available to the
  13260.  operating-system batch command ERRORLEVEL.
  13261.  
  13262.  The behavior of the exit, _exit, _cexit, and _c_exit functions is as
  13263.  follows:
  13264.  
  13265.  Function                          Action
  13266.  ────────────────────────────────────────────────────────────────────────────
  13267.  exit                              Performs complete C library termination
  13268.                                    procedures, terminates the process, and
  13269.                                    exits with the supplied status code.
  13270.  
  13271.  _exit                             Performs "quick" C library termination
  13272.                                    procedures, terminates the process, and
  13273.                                    exits with the supplied status code.
  13274.  
  13275.  _cexit                            Performs complete C library termination
  13276.                                    procedures and returns to caller, but
  13277.                                    does not terminate the process.
  13278.  
  13279.  _c_exit                           Performs "quick" C library termination
  13280.                                    procedures and returns to caller, but
  13281.                                    does not terminate the process.
  13282.  
  13283.  
  13284.  Return Value
  13285.  
  13286.  None.
  13287.  
  13288.  
  13289.  Compatibility
  13290.  
  13291.  exit
  13292.  
  13293.   ANSI   DOS   OS/2   UNIX   XENIX
  13294.  
  13295.  
  13296.  _exit
  13297.  
  13298.   ANSI   DOS   OS/2   UNIX   XENIX
  13299.  
  13300.  
  13301.  
  13302.  See Also
  13303.  
  13304.  abort, atexit, _cexit, exec functions, onexit, spawn functions, system
  13305.  
  13306.  
  13307.  Example
  13308.  
  13309.    /* EXITER.C: This program prompts the user for a yes or no and returns
  13310.     * a DOS error code of 1 if the user answers Y or y; otherwise it
  13311.     * returns 0. The error code could be tested in a batch file.
  13312.     */
  13313.  
  13314.    #include <conio.h>
  13315.    #include <stdlib.h>
  13316.  
  13317.    void main()
  13318.    {
  13319.       char  ch;
  13320.  
  13321.       cputs( "Yes or no? " );
  13322.       ch = getch();
  13323.       cputs( "\r\n" );
  13324.       if( toupper( ch ) == 'Y' )
  13325.          exit( 1 );
  13326.       else
  13327.          exit( 0 );
  13328.    }
  13329.  
  13330.  
  13331.  
  13332.  
  13333.  
  13334.  exp, expl
  13335.  ────────────────────────────────────────────────────────────────────────────
  13336.  
  13337.  
  13338.  Description
  13339.  
  13340.  Calculate the exponential.
  13341.  
  13342.    #include <math.h>
  13343.  
  13344.    double exp( double x );
  13345.  
  13346.    long double expl( long double x );
  13347.  
  13348.  x                                 Floating-point value
  13349.  
  13350.  
  13351.  Remarks
  13352.  
  13353.  The exp and expl functions return the exponential function of their
  13354.  floating-point arguments (x).
  13355.  
  13356.  The expl function is the 80-bit counterpart; it uses an 80-bit, 10-byte
  13357.  coprocessor form of arguments and return values. See the reference page on
  13358.  the long double functions for more details on this data type.
  13359.  
  13360.  
  13361.  Return Value
  13362.  
  13363.  These functions return ex. The functions return HUGE_VAL on overflow and set
  13364.  errno to ERANGE; on underflow, they return 0 but do not set errno.
  13365.  
  13366.  
  13367.  Compatibility
  13368.  
  13369.  exp
  13370.  
  13371.   ANSI   DOS   OS/2   UNIX   XENIX
  13372.  
  13373.  
  13374.  expl
  13375.  
  13376.   ANSI   DOS   OS/2   UNIX   XENIX
  13377.  
  13378.  
  13379.  
  13380.  See Also
  13381.  
  13382.  log functions
  13383.  
  13384.  
  13385.  Example
  13386.  
  13387.    /* EXP.C */
  13388.    #include <math.h>
  13389.    #include <stdio.h>
  13390.  
  13391.    void main()
  13392.    {
  13393.       double x = 2.302585093, y;
  13394.  
  13395.       y = exp( x );
  13396.       printf( "exp( %f ) = %f\n", x, y );
  13397.    }
  13398.  
  13399.  
  13400.  Output
  13401.  
  13402.  
  13403.  
  13404.    exp( 2.302585 ) = 10.000000
  13405.  
  13406.  
  13407.  
  13408.  
  13409.  
  13410.  _expand Functions
  13411.  ────────────────────────────────────────────────────────────────────────────
  13412.  
  13413.  
  13414.  Description
  13415.  
  13416.  Changes the size of a memory block.
  13417.  
  13418.  #include <malloc.h>               Required only for function declarations
  13419.  
  13420.    void *_expand( void *memblock, size_t size );
  13421.  
  13422.    void _based( void ) *_bexpand( _segment seg, void _based( void )
  13423.    *memblock,
  13424.    size_t size );
  13425.  
  13426.    void _far *_fexpand( void _far *memblock, size_t size );
  13427.  
  13428.    void _near *_nexpand( void _near *memblock, size_t size );
  13429.  
  13430.  memblock                          Pointer to previously allocated memory
  13431.                                    block
  13432.  
  13433.  size                              New size in bytes
  13434.  
  13435.  seg                               Value of base segment
  13436.  
  13437.  
  13438.  Remarks
  13439.  
  13440.  The _expand family of functions changes the size of a previously allocated
  13441.  memory block by attempting to expand or contract the block without moving
  13442.  its location in the heap. The memblock argument points to the beginning of
  13443.  the block. The size argument gives the new size of the block, in bytes. The
  13444.  contents of the block are unchanged up to the shorter of the new and old
  13445.  sizes.
  13446.  
  13447.  The memblock argument can also point to a block that has been freed, as long
  13448.  as there has been no intervening call to calloc, _expand, malloc, or
  13449.  realloc. If memblock points to a freed block, the block remains free after a
  13450.  call to one of the _expand functions.
  13451.  
  13452.  The seg argument is the segment address of the _based heap.
  13453.  
  13454.  In large data models (compact-, large-, and huge-model programs), _expand
  13455.  maps to _fexpand. In small data models ( tiny-, small-, and medium-model
  13456.  programs), expand maps to _nexpand.
  13457.  
  13458.  The various _expand functions change the size of the storage block in the
  13459.  data segments shown in the list below:
  13460.  
  13461.  Function                          Data Segment
  13462.  ────────────────────────────────────────────────────────────────────────────
  13463.  _expand                           Depends on data model of program
  13464.  
  13465.  _bexpand                          Based heap specified by seg, or in all
  13466.                                    based heaps if seg
  13467.                                    is zero
  13468.  
  13469.  _fexpand                          Far heap (outside default data segment)
  13470.  
  13471.  _nexpand                          Near heap (inside default data segment)
  13472.  
  13473.  
  13474.  Return Value
  13475.  
  13476.  The _expand family of functions returns a void pointer to the reallocated
  13477.  memory block. Unlike realloc, _expand cannot move a block to change its
  13478.  size. This means the memblock argument to _expand is the same as the return
  13479.  value if there is sufficient memory available to expand the block without
  13480.  moving it.
  13481.  
  13482.  With the exception of the _bexpand function, these functions return NULL if
  13483.  there is insufficient memory available to expand the block to the given size
  13484.  without moving it. The _bexpand function returns _NULLOFF if insufficient
  13485.  memory is available. The item pointed to by memblock will have been expanded
  13486.  as much as possible in its current location.
  13487.  
  13488.  The storage space pointed to by the return value is guaranteed to be
  13489.  suitably aligned for storage of any type of object. The new size of the item
  13490.  can be checked with the _msize function. To get a pointer to a type other
  13491.  than void, use a type cast on the return value.
  13492.  
  13493.  
  13494.  Compatibility
  13495.  
  13496.   ANSI   DOS   OS/2   UNIX   XENIX
  13497.  
  13498.  
  13499.  
  13500.  See Also
  13501.  
  13502.  calloc functions, free functions, malloc functions, _msize functions,
  13503.  realloc functions
  13504.  
  13505.  
  13506.  Example
  13507.  
  13508.    /* EXPAND.C */
  13509.    #include <stdio.h>
  13510.    #include <malloc.h>
  13511.    #include <stdlib.h>
  13512.  
  13513.    void main()
  13514.    {
  13515.       char *bufchar;
  13516.  
  13517.       printf( "Allocate a 512 element buffer\n" );
  13518.       if( (bufchar = (char *)calloc( 512, sizeof( char ) )) == NULL )
  13519.          exit( 1 );
  13520.       printf( "Allocated %d bytes at %Fp\n",
  13521.             _msize( bufchar ), (void _far *)bufchar );
  13522.  
  13523.       if( (bufchar = (char *)_expand( bufchar, 1024 )) == NULL )
  13524.          printf( "Can't expand" );
  13525.       else
  13526.          printf( "Expanded block to %d bytes at %Fp\n",
  13527.                _msize( bufchar ), (void _far *)bufchar );
  13528.  
  13529.       /* Free memory */
  13530.       free( bufchar );
  13531.       exit( 0 );
  13532.    }
  13533.  
  13534.  
  13535.  Output
  13536.  
  13537.  
  13538.  
  13539.    Allocate a 512 element buffer
  13540.    Allocated 512 bytes at 0067:142A
  13541.    Expanded block to 1024 bytes at 0067:142A
  13542.  
  13543.  
  13544.  
  13545.  
  13546.  
  13547.  fabs, fabsl
  13548.  ────────────────────────────────────────────────────────────────────────────
  13549.  
  13550.  
  13551.  Description
  13552.  
  13553.  Calculate the absolute value of their floating-point arguments.
  13554.  
  13555.    #include <math.h>
  13556.  
  13557.    double fabs( double x );
  13558.  
  13559.    long double fabsl( long double x );
  13560.  
  13561.  x                                 Floating-point value
  13562.  
  13563.  
  13564.  Remarks
  13565.  
  13566.  The fabs and fabsl functions calculate the absolute value of their
  13567.  floating-point arguments.
  13568.  
  13569.  The fabsl function is the 80-bit counterpart; it uses an 80-bit, 10-byte
  13570.  coprocessor form of arguments and return values. See the reference page on
  13571.  the long double functions for more details on this data type.
  13572.  
  13573.  
  13574.  Return Value
  13575.  
  13576.  These functions return the absolute value of their arguments. There is no
  13577.  error return.
  13578.  
  13579.  
  13580.  Compatibility
  13581.  
  13582.  fabs
  13583.  
  13584.   ANSI   DOS   OS/2   UNIX   XENIX
  13585.  
  13586.  
  13587.  fabsl
  13588.  
  13589.   ANSI   DOS   OS/2   UNIX   XENIX
  13590.  
  13591.  
  13592.  
  13593.  See Also
  13594.  
  13595.  abs, cabs, labs
  13596.  
  13597.  
  13598.  Example
  13599.  
  13600.    /* ABS.C: This program computes and displays the absolute values of
  13601.     * several numbers.
  13602.     */
  13603.  
  13604.    #include <stdio.h>
  13605.    #include <math.h>
  13606.    #include <stdlib.h>
  13607.  
  13608.    void main()
  13609.    {
  13610.       int    ix = -4, iy;
  13611.       long   lx = -41567L, ly;
  13612.       double dx = -3.141593, dy;
  13613.  
  13614.       iy = abs( ix );
  13615.       printf( "The absolute value of %d is %d\n", ix, iy);
  13616.  
  13617.       ly = labs( lx );
  13618.       printf( "The absolute value of %ld is %ld\n", lx, ly);
  13619.  
  13620.       dy = fabs( dx );
  13621.       printf( "The absolute value of %f is %f\n", dx, dy );
  13622.    }
  13623.  
  13624.  
  13625.  Output
  13626.  
  13627.  
  13628.  
  13629.    The absolute value of -4 is 4
  13630.    The absolute value of -41567 is 41567
  13631.    The absolute value of -3.141593 is 3.141593
  13632.  
  13633.  
  13634.  
  13635.  
  13636.  
  13637.  fclose, fcloseall
  13638.  ────────────────────────────────────────────────────────────────────────────
  13639.  
  13640.  
  13641.  Description
  13642.  
  13643.  Closes a stream (fclose) or closes all open streams (fcloseall).
  13644.  
  13645.    #include <stdio.h>
  13646.  
  13647.    int fclose( FILE *stream );
  13648.  
  13649.    int fcloseall( void );
  13650.  
  13651.  stream                            Pointer to FILE structure
  13652.  
  13653.  
  13654.  Remarks
  13655.  
  13656.  The fclose function closes stream. The fcloseall function closes all open
  13657.  streams except stdin, stdout, stderr (and in DOS, stdaux and stdprn). It
  13658.  also closes and deletes any temporary files created by tmpfile.
  13659.  
  13660.  In both functions, all buffers associated with the stream are flushed prior
  13661.  to closing. System-allocated buffers are released when the stream is closed.
  13662.  Buffers assigned by the user with setbuf and setvbuf are not automatically
  13663.  released.
  13664.  
  13665.  
  13666.  Return Value
  13667.  
  13668.  The fclose function returns 0 if the stream is successfully closed. The
  13669.  fcloseall function returns the total number of streams closed. Both
  13670.  functions return EOF to indicate an error.
  13671.  
  13672.  
  13673.  Compatibility
  13674.  
  13675.  fclose
  13676.  
  13677.   ANSI   DOS   OS/2   UNIX   XENIX
  13678.  
  13679.  
  13680.  fcloseall
  13681.  
  13682.   ANSI   DOS   OS/2   UNIX   XENIX
  13683.  
  13684.  
  13685.  
  13686.  See Also
  13687.  
  13688.  close, fdopen, fflush, fopen, freopen
  13689.  
  13690.  
  13691.  Example
  13692.  
  13693.    /* FOPEN.C: This program opens files named "data" and "data2". It uses
  13694.     * fclose to close "data" and fcloseall to close all remaining files.
  13695.     */
  13696.  
  13697.    #include <stdio.h>
  13698.  
  13699.    FILE *stream, *stream2;
  13700.  
  13701.    void main()
  13702.    {
  13703.       int numclosed;
  13704.  
  13705.       /* Open for read (will fail if 'data does not exist) */
  13706.       if( (stream  = fopen( "data", "r" )) == NULL )
  13707.          printf( "The file 'data' was not opened\n" );
  13708.       else
  13709.          printf( "The file 'data' was opened\n" );
  13710.  
  13711.       /* Open for write */
  13712.       if( (stream2 = fopen( "data2", "w+" )) == NULL )
  13713.          printf( "The file 'data2' was not opened\n" );
  13714.       else
  13715.          printf( "The file 'data2' was opened\n" );
  13716.  
  13717.       /* Close stream */
  13718.       if( fclose( stream ) )
  13719.          printf( "The file 'data' was not closed\n" );
  13720.  
  13721.       /* All other files are closed: */
  13722.       numclosed = fcloseall( );
  13723.       printf( "Number of files closed by fcloseall: %u\n", numclosed );
  13724.    }
  13725.  
  13726.  
  13727.  Output
  13728.  
  13729.  
  13730.  
  13731.    The file 'data' was opened
  13732.    The file 'data2' was opened
  13733.    Number of files closed by fcloseall: 1
  13734.  
  13735.  
  13736.  
  13737.  
  13738.  
  13739.  fcvt
  13740.  ────────────────────────────────────────────────────────────────────────────
  13741.  
  13742.  
  13743.  Description
  13744.  
  13745.  Converts a floating-point number to a string.
  13746.  
  13747.  #include <stdlib.h>               Required only for function declarations
  13748.  
  13749.    char *fcvt( double value, int count, int *dec, int *sign );
  13750.  
  13751.  value                             Number to be converted
  13752.  
  13753.  count                             Number of digits after decimal point
  13754.  
  13755.  dec                               Pointer to stored decimal-point position
  13756.  
  13757.  sign                              Pointer to stored sign indicator
  13758.  
  13759.  
  13760.  Remarks
  13761.  
  13762.  The fcvt function converts a floating-point number to a null-terminated
  13763.  character string. The value argument is the floating-point number to be
  13764.  converted. The fcvt function stores the digits of value as a string and
  13765.  appends a null character ('\0'). The count argument specifies the number of
  13766.  digits to be stored after the decimal point. Excess digits are rounded off
  13767.  to count places. If there are fewer than count digits of precision, the
  13768.  string is padded with zeros.
  13769.  
  13770.  Only digits are stored in the string. The position of the decimal point and
  13771.  the sign of value can be obtained from dec and sign after the call. The dec
  13772.  argument points to an integer value; this integer value gives the position
  13773.  of the decimal point with respect to the beginning of the string. A zero or
  13774.  negative integer value indicates that the decimal point lies to the left of
  13775.  the first digit. The argument sign points to an integer indicating the sign
  13776.  of value. The integer is set to 0 if value is positive and is set to a
  13777.  nonzero number if value is negative.
  13778.  
  13779.  The ecvt and fcvt functions use a single statically allocated buffer for the
  13780.  conversion. Each call to one of these routines destroys the results of the
  13781.  previous call.
  13782.  
  13783.  
  13784.  Return Value
  13785.  
  13786.  The fcvt function returns a pointer to the string of digits. There is no
  13787.  error return.
  13788.  
  13789.  
  13790.  Compatibility
  13791.  
  13792.   ANSI   DOS   OS/2   UNIX   XENIX
  13793.  
  13794.  
  13795.  
  13796.  See Also
  13797.  
  13798.  atof, atoi, atol, ecvt, gcvt
  13799.  
  13800.  
  13801.  Example
  13802.  
  13803.    /* FCVT.C: This program converts the constant 3.1415926535 to a string and
  13804.     * sets the pointer *buffer to point to that string.
  13805.     */
  13806.  
  13807.    #include <stdlib.h>
  13808.    #include <stdio.h>
  13809.  
  13810.    void main()
  13811.    {
  13812.       int  decimal, sign;
  13813.       char *buffer;
  13814.       double source = 3.1415926535;
  13815.  
  13816.       buffer = fcvt( source, 7, &decimal, &sign );
  13817.       printf( "source: %2.10f   buffer: '%s'   decimal: %d   sign: %d\n",
  13818.               source, buffer, decimal, sign );
  13819.    }
  13820.  
  13821.  
  13822.  Output
  13823.  
  13824.  
  13825.  
  13826.    source: 3.1415926535   buffer: '31415927'   decimal: 1   sign: 0
  13827.  
  13828.  
  13829.  
  13830.  
  13831.  
  13832.  fdopen
  13833.  ────────────────────────────────────────────────────────────────────────────
  13834.  
  13835.  
  13836.  Description
  13837.  
  13838.  Opens a stream using a handle.
  13839.  
  13840.    #include <stdio.h>
  13841.  
  13842.    FILE *fdopen( int handle, char *mode );
  13843.  
  13844.  handle                            Handle referring to open file
  13845.  
  13846.  mode                              Type of access permitted
  13847.  
  13848.  
  13849.  Remarks
  13850.  
  13851.  The fdopen function associates an input/output stream with the file
  13852.  identified by handle, thus allowing a file opened for "low-level" I/O to be
  13853.  buffered and formatted. (See Section 2.7, "Input and Output," for an
  13854.  explanation of stream I/O and low-level I/O.) The mode character string
  13855.  specifies the type of access requested for the file, as shown below. The
  13856.  following list gives the mode string used in the fopen and fdopen functions
  13857.  and the corresponding oflag arguments used in the open and sopen functions.
  13858.  A complete description of the mode string argument is given in the remarks
  13859.  section of the fopen function.
  13860.  
  13861.  Type String                       Equivalent Value for open/sopen
  13862.  ────────────────────────────────────────────────────────────────────────────
  13863.  "r"                               O_RDONLY
  13864.  
  13865.  "w"                               O_WRONLY (usually O_WRONLY | O_CREAT |
  13866.                                    O_TRUNC)
  13867.  
  13868.  "a"                               O_WRONLY | O_APPEND (usually O_WRONLY |
  13869.                                    O_CREAT | O_APPEND)
  13870.  
  13871.  "r+"                              O_RDWR
  13872.  
  13873.  "w+"                              O_RDWR (usually O_RDWR | O_CREAT |
  13874.                                    O_TRUNC)
  13875.  
  13876.  "a+"                              O_RDWR | O_APPEND (usually O_RDWR |
  13877.                                    O_APPEND | O_CREAT )
  13878.  
  13879.  In addition to the values listed above, one of the following characters can
  13880.  be included in the mode string to specify the translation mode for newlines.
  13881.  These characters correspond to the constants used in the open and sopen
  13882.  functions, as shown below:
  13883.  
  13884.  Mode                              Equivalent Value for open/sopen
  13885.  ────────────────────────────────────────────────────────────────────────────
  13886.  t                                 O_TEXT
  13887.  
  13888.  b                                 O_BINARY
  13889.  
  13890.  If t or b is not given in the mode string, the translation mode is defined
  13891.  by the default-mode variable _fmode.
  13892.  
  13893.  The t option is not part of the ANSI standard for fopen and fpopen, but is
  13894.  instead a Microsoft extension and should not be used where ANSI portability
  13895.  is desired.
  13896.  
  13897.  
  13898.  Return Value
  13899.  
  13900.  The fdopen function returns a pointer to the open stream. A null pointer
  13901.  value indicates an error.
  13902.  
  13903.  
  13904.  Compatibility
  13905.  
  13906.   ANSI   DOS   OS/2   UNIX   XENIX
  13907.  
  13908.  
  13909.  
  13910.  See Also
  13911.  
  13912.  dup, dup2, fclose, fcloseall, fopen, freopen, open
  13913.  
  13914.  
  13915.  Example
  13916.  
  13917.    /* FDOPEN.C: This program opens a file using low-level I/O, then uses
  13918.     * fdopen to switch to stream access. It counts the lines in the file.
  13919.     */
  13920.  
  13921.    #include <stdlib.h>
  13922.    #include <stdio.h>
  13923.    #include <fcntl.h>
  13924.    #include <io.h>
  13925.  
  13926.    void main()
  13927.    {
  13928.       FILE *stream;
  13929.       int  fh, count = 0;
  13930.       char inbuf[128];
  13931.  
  13932.       /* Open a file handle. */
  13933.       if( (fh = open( "fdopen.c", O_RDONLY )) == -1 )
  13934.          exit( 1 );
  13935.  
  13936.       /* Change handle access to stream access. */
  13937.       if( (stream = fdopen( fh, "r" )) == NULL )
  13938.          exit( 1 );
  13939.  
  13940.       while( fgets( inbuf, 128, stream ) != NULL )
  13941.          count++;
  13942.  
  13943.    /* After fdopen, close with fclose, not close. */
  13944.       fclose( stream );
  13945.  
  13946.       printf( "Lines in file: %d\n", count );
  13947.    }
  13948.  
  13949.  
  13950.  Output
  13951.  
  13952.  
  13953.  
  13954.    Lines in file: 31
  13955.  
  13956.  
  13957.  
  13958.  
  13959.  
  13960.  feof
  13961.  ────────────────────────────────────────────────────────────────────────────
  13962.  
  13963.  
  13964.  Description
  13965.  
  13966.  Tests for end-of-file on a stream.
  13967.  
  13968.    #include <stdio.h>
  13969.  
  13970.    int feof( FILE *stream );
  13971.  
  13972.  stream                            Pointer to FILE structure
  13973.  
  13974.  
  13975.  Remarks
  13976.  
  13977.  The feof routine (implemented as a macro) determines whether the end of
  13978.  stream has been reached. Once the end of the file is reached, read
  13979.  operations return an end-of-file indicator until the stream is closed or
  13980.  until rewind, fsetpos, fseek, or clearerr is called against it.
  13981.  
  13982.  
  13983.  Return Value
  13984.  
  13985.  The feof function returns a nonzero value after the first read operation
  13986.  that attempts to read past the end of the file. It returns 0 if the current
  13987.  position is not end-of-file. There is no error return.
  13988.  
  13989.  
  13990.  Compatibility
  13991.  
  13992.   ANSI   DOS   OS/2   UNIX   XENIX
  13993.  
  13994.  
  13995.  
  13996.  See Also
  13997.  
  13998.  clearerr, eof, ferror, perror
  13999.  
  14000.  
  14001.  Example
  14002.  
  14003.    /* FEOF.C: This program uses feof to indicate when it reaches the end
  14004.     * of the file FEOF.C. It also checks for errors with ferror.
  14005.     */
  14006.  
  14007.    #include <stdio.h>
  14008.    #include <stdlib.h>
  14009.  
  14010.    void main()
  14011.    {
  14012.       int  count, total = 0;
  14013.       char buffer[100];
  14014.       FILE *stream;
  14015.  
  14016.       if( (stream = fopen( "feof.c", "r" )) == NULL )
  14017.          exit( 1 );
  14018.  
  14019.    /* Cycle until end of file reached: */
  14020.       while( !feof( stream ) )
  14021.       {
  14022.          /* Attempt to read in 10 bytes: */
  14023.          count = fread( buffer, sizeof( char ), 100, stream );
  14024.          if( ferror( stream ) )
  14025.          {
  14026.             perror( "Read error" );
  14027.             break;
  14028.          }
  14029.  
  14030.          /* Total up actual bytes read */
  14031.          total += count;
  14032.       }
  14033.       printf( "Number of bytes read = %d\n", total );
  14034.       fclose( stream );
  14035.    }
  14036.  
  14037.  
  14038.  Output
  14039.  
  14040.  
  14041.  
  14042.    Number of bytes read = 697
  14043.  
  14044.  
  14045.  
  14046.  
  14047.  
  14048.  ferror
  14049.  ────────────────────────────────────────────────────────────────────────────
  14050.  
  14051.  
  14052.  Description
  14053.  
  14054.  Tests for an error on a stream.
  14055.  
  14056.    #include <stdio.h>
  14057.  
  14058.    int ferror( FILE *stream );
  14059.  
  14060.  stream                            Pointer to FILE structure
  14061.  
  14062.  
  14063.  Remarks
  14064.  
  14065.  The ferror routine (implemented as a macro) tests for a reading or writing
  14066.  error on the file associated with stream. If an error has occurred, the
  14067.  error indicator for the stream remains set until the stream is closed or
  14068.  rewound, or until clearerr is called against it.
  14069.  
  14070.  
  14071.  Return Value
  14072.  
  14073.  If no error has occurred on stream, ferror returns 0. Otherwise, it returns
  14074.  a nonzero value.
  14075.  
  14076.  
  14077.  Compatibility
  14078.  
  14079.   ANSI   DOS   OS/2   UNIX   XENIX
  14080.  
  14081.  
  14082.  
  14083.  See Also
  14084.  
  14085.  clearerr, eof, feof, fopen, perror
  14086.  
  14087.  
  14088.  Example
  14089.  
  14090.    /* FEOF.C: This program uses feof to indicate when it reaches the end
  14091.     * of the file FEOF.C. It also checks for errors with ferror.
  14092.     */
  14093.  
  14094.    #include <stdio.h>
  14095.    #include <stdlib.h>
  14096.  
  14097.    void main()
  14098.    {
  14099.       int  count, total = 0;
  14100.       char buffer[100];
  14101.       FILE *stream;
  14102.  
  14103.       if( (stream = fopen( "feof.c", "r" )) == NULL )
  14104.          exit( 1 );
  14105.  
  14106.    /* Cycle until end of file reached: */
  14107.       while( !feof( stream ) )
  14108.       {
  14109.          /* Attempt to read in 10 bytes: */
  14110.          count = fread( buffer, sizeof( char ), 100, stream );
  14111.          if( ferror( stream ) )
  14112.          {
  14113.             perror( "Read error" );
  14114.             break;
  14115.          }
  14116.  
  14117.          /* Total up actual bytes read */
  14118.          total += count;
  14119.       }
  14120.       printf( "Number of bytes read = %d\n", total );
  14121.       fclose( stream );
  14122.    }
  14123.  
  14124.  
  14125.  Output
  14126.  
  14127.  
  14128.  
  14129.    Number of bytes read = 697
  14130.  
  14131.  
  14132.  
  14133.  
  14134.  
  14135.  fflush
  14136.  ────────────────────────────────────────────────────────────────────────────
  14137.  
  14138.  
  14139.  Description
  14140.  
  14141.  Flushes a stream.
  14142.  
  14143.    #include <stdio.h>
  14144.  
  14145.    int fflush( FILE *stream );
  14146.  
  14147.  stream                            Pointer to FILE structure
  14148.  
  14149.  
  14150.  Remarks
  14151.  
  14152.  If the file associated with stream is open for output, fflush writes to that
  14153.  file the contents of the buffer associated with the stream. If the stream is
  14154.  open for input, fflush clears the contents of the buffer. The fflush
  14155.  function negates the effect of any prior call to ungetc against stream.
  14156.  
  14157.  Buffers are automatically flushed when they are full, when the stream is
  14158.  closed, or when a program terminates normally without closing the stream.
  14159.  
  14160.  The stream remains open after the call. The fflush function has no effect on
  14161.  an unbuffered stream.
  14162.  
  14163.  
  14164.  Return Value
  14165.  
  14166.  The fflush function returns the value 0 if the buffer was successfully
  14167.  flushed. The value 0 is also returned in cases in which the specified stream
  14168.  has no buffer or is open for reading only. A return value of EOF indicates
  14169.  an error.
  14170.  
  14171.  
  14172.  Compatibility
  14173.  
  14174.   ANSI   DOS   OS/2   UNIX   XENIX
  14175.  
  14176.  
  14177.  
  14178.  See Also
  14179.  
  14180.  fclose, flushall, setbuf
  14181.  
  14182.  
  14183.  Example
  14184.  
  14185.    /* FFLUSH.C */
  14186.    #include <stdio.h>
  14187.    #include <conio.h>
  14188.  
  14189.    void main()
  14190.    {
  14191.       int integer;
  14192.       char string[81];
  14193.  
  14194.    /* Read each word as a string. */
  14195.       printf( "Enter a sentence of four words with scanf: " );
  14196.       for( integer = 0; integer < 4; integer++ )
  14197.       {
  14198.          scanf( "%s", string );
  14199.          printf( "%s\n", string );
  14200.       }
  14201.  
  14202.       /* You must flush the input buffer before using gets. */
  14203.       fflush( stdin );
  14204.       printf( "Enter the same sentence with gets: " );
  14205.       gets( string );
  14206.       printf( "%s\n", string );
  14207.    }
  14208.  
  14209.  
  14210.  Output
  14211.  
  14212.  
  14213.  
  14214.    Enter a sentence of four words with scanf: This is a test
  14215.    This
  14216.    is
  14217.    a
  14218.    test
  14219.    Enter the same sentence with gets: This is a test
  14220.    This is a test
  14221.  
  14222.  
  14223.  
  14224.  
  14225.  
  14226.  fgetc, fgetchar
  14227.  ────────────────────────────────────────────────────────────────────────────
  14228.  
  14229.  
  14230.  Description
  14231.  
  14232.  Read a character from a stream (fgetc) or stdin (fgetchar).
  14233.  
  14234.    #include <stdio.h>
  14235.  
  14236.    int fgetc( FILE *stream );
  14237.  
  14238.    int fgetchar( void );
  14239.  
  14240.  stream                            Pointer to FILE structure
  14241.  
  14242.  
  14243.  Remarks
  14244.  
  14245.  The fgetc function reads a single character from the current position of the
  14246.  file associated with stream. The character is converted and returned as an
  14247.  int. The function then increments the associated file pointer (if any) to
  14248.  point to the next character. The fgetchar function is equivalent to
  14249.  fgetc(stdin).
  14250.  
  14251.  The fgetc and fgetchar routines are identical to getc and getchar, but they
  14252.  are functions rather than macros.
  14253.  
  14254.  
  14255.  Return Value
  14256.  
  14257.  The fgetc and fgetchar functions return the character read. They return EOF
  14258.  to indicate an error or end-of-file. Use feof or ferror to distinguish
  14259.  between an error and an end-of-file condition.
  14260.  
  14261.  
  14262.  Compatibility
  14263.  
  14264.  fgetc
  14265.  
  14266.   ANSI   DOS   OS/2   UNIX   XENIX
  14267.  
  14268.  
  14269.  fgetchar
  14270.  
  14271.   ANSI   DOS   OS/2   UNIX   XENIX
  14272.  
  14273.  
  14274.  
  14275.  See Also
  14276.  
  14277.  fputc, fputchar, getc, getchar
  14278.  
  14279.  
  14280.  Example
  14281.  
  14282.    /* FGETC.C: This program uses getc to read the first 80 input characters
  14283.     * (or until the end of input) and place them into a string named buffer.
  14284.     */
  14285.  
  14286.    #include <stdio.h>
  14287.    #include <stdlib.h>
  14288.  
  14289.    void main()
  14290.    {
  14291.       FILE *stream;
  14292.       char buffer[81];
  14293.       int  i, ch;
  14294.  
  14295.       /* Open file to read line from: */
  14296.       if( (stream = fopen( "fgetc.c", "r" )) == NULL )
  14297.          exit( 0 );
  14298.  
  14299.       /* Read in first 80 characters and place them in "buffer": */
  14300.       ch = fgetc( stream );
  14301.       for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
  14302.       {
  14303.          buffer[i] = ch;
  14304.          ch = fgetc( stream );
  14305.       }
  14306.       /* Add null to end string */
  14307.       buffer[i] = '\0';
  14308.       printf( "%s\n", buffer );
  14309.       fclose( stream );
  14310.    }
  14311.  
  14312.  
  14313.  Output
  14314.  
  14315.  
  14316.  
  14317.    /* FGETC.C: This program uses getc to read the first 80 input characters
  14318.    /* (or
  14319.  
  14320.  
  14321.  
  14322.  
  14323.  
  14324.  
  14325.  
  14326.  fgetpos
  14327.  ────────────────────────────────────────────────────────────────────────────
  14328.  
  14329.  
  14330.  Description
  14331.  
  14332.  Gets a stream's file-position indicator.
  14333.  
  14334.    #include <stdio.h>
  14335.  
  14336.    int fgetpos( FILE *stream, fpos_t *pos );
  14337.  
  14338.  stream                            Target stream
  14339.  
  14340.  pos                               Position-indicator storage
  14341.  
  14342.  
  14343.  Remarks
  14344.  
  14345.  The fgetpos function gets the current value of the stream argument's
  14346.  file-position indicator and stores it in the object pointed to by pos. The
  14347.  fsetpos function can later use information stored in pos to reset the stream
  14348.  argument's pointer to its position at the time fgetpos was called.
  14349.  
  14350.  The pos value is stored in an internal format and is intended for use only
  14351.  by the fgetpos and fsetpos functions.
  14352.  
  14353.  
  14354.  Return Value
  14355.  
  14356.  If successful, the fgetpos function returns 0. On failure, it returns a
  14357.  nonzero value and sets errno to one of the following manifest constants
  14358.  (defined in STDIO.H):
  14359.  
  14360.  Constant                          Meaning
  14361.  ────────────────────────────────────────────────────────────────────────────
  14362.  EBADF                             The specified stream is not a valid file
  14363.                                    handle or is not
  14364.                                    accessible.
  14365.  
  14366.  EINVAL                            The stream value is invalid.
  14367.  
  14368.  
  14369.  Compatibility
  14370.  
  14371.   ANSI   DOS   OS/2   UNIX   XENIX
  14372.  
  14373.  
  14374.  
  14375.  See Also
  14376.  
  14377.  fsetpos
  14378.  
  14379.  
  14380.  Example
  14381.  
  14382.    /* FGETPOS.C: This program opens a file and reads bytes at several
  14383.     * different locations.
  14384.     */
  14385.  
  14386.    #include <stdio.h>
  14387.  
  14388.    void main()
  14389.    {
  14390.       FILE   *stream;
  14391.       fpos_t pos;
  14392.       int    val;
  14393.       char   buffer[20];
  14394.  
  14395.       if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )
  14396.          printf( "Trouble opening file\n" );
  14397.       else
  14398.       {
  14399.          /* Read some data and then check the position. */
  14400.          fread( buffer, sizeof( char ), 10, stream );
  14401.          if( fgetpos( stream, &pos ) != 0 )
  14402.             perror( "fgetpos error" );
  14403.          else
  14404.          {
  14405.             fread( buffer, sizeof( char ), 10, stream );
  14406.             printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
  14407.          }
  14408.  
  14409.          /* Set a new position and read more data */
  14410.          pos = 140;
  14411.          if( fsetpos( stream, &pos ) != 0 )
  14412.             perror( "fsetpos error" );
  14413.  
  14414.          fread( buffer, sizeof( char ), 10, stream );
  14415.             printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
  14416.  
  14417.          fclose( stream );
  14418.       }
  14419.    }
  14420.  
  14421.  
  14422.  Output
  14423.  
  14424.  
  14425.  
  14426.    10 bytes at byte 10: .C: This p
  14427.    10 bytes at byte 140:   FILE   *
  14428.  
  14429.  
  14430.  
  14431.  
  14432.  
  14433.  fgets
  14434.  ────────────────────────────────────────────────────────────────────────────
  14435.  
  14436.  
  14437.  Description
  14438.  
  14439.  Gets a string from a stream.
  14440.  
  14441.    #include <stdio.h>
  14442.  
  14443.    char *fgets( char *string, int n, FILE *stream );
  14444.  
  14445.  string                            Storage location for data
  14446.  
  14447.  n                                 Number of characters stored
  14448.  
  14449.  stream                            Pointer to FILE structure
  14450.  
  14451.  
  14452.  Remarks
  14453.  
  14454.  The fgets function reads a string from the input stream argument and stores
  14455.  it in string. Characters are read from the current stream position up to and
  14456.  including the first newline character ('\n'), up to the end of the stream,
  14457.  or until the number of characters read is equal to n - 1, whichever comes
  14458.  first. The result is stored in string, and a null character ('\0') is
  14459.  appended. The newline character, if read, is included in the string. If n is
  14460.  equal to 1, string is empty (""). The fgets function is similar to the gets
  14461.  function; however, gets replaces the newline character with NULL.
  14462.  
  14463.  
  14464.  Return Value
  14465.  
  14466.  If successful, the fgets function returns string. It returns NULL to
  14467.  indicate either an error or end-of-file condition. Use feof or ferror to
  14468.  determine whether an error occurred.
  14469.  
  14470.  
  14471.  Compatibility
  14472.  
  14473.   ANSI   DOS   OS/2   UNIX   XENIX
  14474.  
  14475.  
  14476.  
  14477.  See Also
  14478.  
  14479.  fputs, gets, puts
  14480.  
  14481.  
  14482.  Example
  14483.  
  14484.    /* FGETS.C: This program uses fgets to display a line from a file on the
  14485.     * screen.
  14486.     */
  14487.  
  14488.    #include <stdio.h>
  14489.  
  14490.    FILE *stream;
  14491.  
  14492.    void main()
  14493.    {
  14494.       char line[100], *result;
  14495.  
  14496.    if( (stream = fopen( "fgets.c", "r" )) != NULL )
  14497.       {
  14498.          if( fgets( line, 100, stream ) == NULL)
  14499.             printf( "fgets error\n" );
  14500.          else
  14501.             printf( "%s", line);
  14502.          fclose( stream );
  14503.       }
  14504.    }
  14505.  
  14506.  
  14507.  Output
  14508.  
  14509.  
  14510.  
  14511.    /* FGETS.C: This program uses fgets to display a line from a file on the
  14512.  
  14513.  
  14514.  
  14515.  
  14516.  
  14517.  fieeetomsbin, fmsbintoieee
  14518.  ────────────────────────────────────────────────────────────────────────────
  14519.  
  14520.  
  14521.  Description
  14522.  
  14523.  Convert floating-point numbers between IEEE and Microsoft binary formats.
  14524.  
  14525.    #include <math.h>
  14526.  
  14527.    int fieeetomsbin( float *src4,  float *dst4 );
  14528.  
  14529.    int fmsbintoieee(  float *src4,  float *dst4 );
  14530.  
  14531.  scr4                              Value to be converted
  14532.  
  14533.  dst4                              Converted value
  14534.  
  14535.  
  14536.  Remarks
  14537.  
  14538.  The fieeetomsbin routine converts a single-precision floating-point number
  14539.  in IEEE (Institute of Electrical and Electronic Engineers) format to
  14540.  Microsoft (MS) binary format.
  14541.  
  14542.  The fmsbintoieee routine converts a floating-point number in Microsoft
  14543.  binary format to IEEE format.
  14544.  
  14545.  These routines allow C programs (which store floating-point numbers in the
  14546.  IEEE format) to use numeric data in random-access data files created with
  14547.  Microsoft BASIC (which stores floating-point numbers in the Microsoft binary
  14548.  format), and vice versa.
  14549.  
  14550.  The argument src4 points to the float value to be converted. The result is
  14551.  stored at the location given by dst4.
  14552.  
  14553.  These routines do not handle IEEE NANs ("not a number") and infinities. IEEE
  14554.  denormals are treated as 0 in the conversions.
  14555.  
  14556.  
  14557.  Return Value
  14558.  
  14559.  These functions return 0 if the conversion is successful and 1 if the
  14560.  conversion causes an overflow.
  14561.  
  14562.  
  14563.  Compatibility
  14564.  
  14565.   ANSI   DOS   OS/2   UNIX   XENIX
  14566.  
  14567.  
  14568.  
  14569.  See Also
  14570.  
  14571.  dieeetomsbin, dmsbintoieee
  14572.  
  14573.  
  14574.  
  14575.  
  14576.  
  14577.  filelength
  14578.  ────────────────────────────────────────────────────────────────────────────
  14579.  
  14580.  
  14581.  Description
  14582.  
  14583.  Gets the length of a file.
  14584.  
  14585.  #include <io.h>                   Required only for function declarations
  14586.  
  14587.    long filelength( int handle );
  14588.  
  14589.  handle                            Target file handle
  14590.  
  14591.  
  14592.  Remarks
  14593.  
  14594.  The filelength function returns the length, in bytes, of the target file
  14595.  associated with handle.
  14596.  
  14597.  
  14598.  Return Value
  14599.  
  14600.  The filelength function returns the file length in bytes. A return value of
  14601.  -1L indicates an error, and an invalid handle sets errno to EBADF.
  14602.  
  14603.  
  14604.  Compatibility
  14605.  
  14606.   ANSI   DOS   OS/2   UNIX   XENIX
  14607.  
  14608.  
  14609.  
  14610.  See Also
  14611.  
  14612.  chsize, fileno, fstat, stat
  14613.  
  14614.  
  14615.  Example
  14616.  
  14617.    /* CHSIZE.C: This program uses filelength to report the size of a
  14618.     * file before and after modifying it with chsize.
  14619.     */
  14620.  
  14621.    #include <io.h>
  14622.    #include <fcntl.h>
  14623.    #include <sys\types.h>
  14624.    #include <sys\stat.h>
  14625.    #include <stdio.h>
  14626.  
  14627.    void main()
  14628.    {
  14629.       int fh, result;
  14630.       unsigned int nbytes = BUFSIZ;
  14631.  
  14632.    /* Open a file */
  14633.       if( (fh = open( "data", O_RDWR | O_CREAT, S_IREAD | S_IWRITE )) != -1 )
  14634.       {
  14635.          printf( "File length before: %ld\n", filelength( fh ) );
  14636.          if( chsize( fh, 329678 ) == 0 )
  14637.             printf( "Size successfully changed\n" );
  14638.          else
  14639.             printf( "Problem in changing the size\n" );
  14640.          printf( "File length after:  %ld\n", filelength( fh ) );
  14641.          close( fh );
  14642.       }
  14643.    }
  14644.  
  14645.  
  14646.  Output
  14647.  
  14648.  
  14649.  
  14650.    File length before: 0
  14651.    Size successfully changed
  14652.    File length after:  329678
  14653.  
  14654.  
  14655.  
  14656.  
  14657.  
  14658.  fileno
  14659.  ────────────────────────────────────────────────────────────────────────────
  14660.  
  14661.  
  14662.  Description
  14663.  
  14664.  Gets the file handle associated with a stream.
  14665.  
  14666.    #include <stdio.h>
  14667.  
  14668.    int fileno( FILE *stream );
  14669.  
  14670.  stream                            Pointer to FILE structure
  14671.  
  14672.  
  14673.  Remarks
  14674.  
  14675.  The fileno routine returns the file handle currently associated with stream.
  14676.  This routine is implemented as a macro.
  14677.  
  14678.  
  14679.  Return Value
  14680.  
  14681.  The fileno routine returns the file handle. There is no error return. The
  14682.  result is undefined if stream does not specify an open file.
  14683.  
  14684.  
  14685.  Compatibility
  14686.  
  14687.   ANSI   DOS   OS/2   UNIX   XENIX
  14688.  
  14689.  
  14690.  
  14691.  See Also
  14692.  
  14693.  fdopen, filelength, fopen, freopen
  14694.  
  14695.  
  14696.  Example
  14697.  
  14698.    /* FILENO.C: This program uses fileno to obtain the file handle for
  14699.     * some standard C streams.
  14700.     */
  14701.  
  14702.    #include <stdio.h>
  14703.  
  14704.    void main()
  14705.    {
  14706.       printf( "The file handle for stdin is %d\n", fileno( stdin ) );
  14707.       printf( "The file handle for stdout is %d\n", fileno( stdout ) );
  14708.       printf( "The file handle for stderr is %d\n", fileno( stderr ) );
  14709.    }
  14710.  
  14711.  
  14712.  Output
  14713.  
  14714.  
  14715.  
  14716.    The file handle for stdin is 0
  14717.    The file handle for stdout is 1
  14718.    The file handle for stderr is 2
  14719.  
  14720.  
  14721.  
  14722.  
  14723.  
  14724.  _floodfill, _floodfill_w
  14725.  ────────────────────────────────────────────────────────────────────────────
  14726.  
  14727.  
  14728.  Description
  14729.  
  14730.  Fill an area of a display using the current color and fill mask
  14731.  
  14732.    #include <graph.h>
  14733.  
  14734.    short _far _floodfill( short x, short y, short boundary );
  14735.  
  14736.    short _far _floodfill_w( double wx, double wy, short boundary );
  14737.  
  14738.  x, y                              Start point
  14739.  
  14740.  wx, wy                            Start point
  14741.  
  14742.  boundary                          Boundary color of area to be filled
  14743.  
  14744.  
  14745.  Remarks
  14746.  
  14747.  The functions in the _floodfill family fill an area of the display, using
  14748.  the current color and fill mask. The _floodfill routine begins filling at
  14749.  the view-coordinate point (x, y). The _floodfill_w routine begins filling at
  14750.  the window-coordinate point (wx, wy).
  14751.  
  14752.  If this point lies inside the figure, the interior is filled; if it lies
  14753.  outside the figure, the background is filled. The point must be inside or
  14754.  outside the figure to be filled, not on the figure boundary itself. Filling
  14755.  occurs in all directions, stopping at the color of boundary.
  14756.  
  14757.  
  14758.  Return Value
  14759.  
  14760.  The _floodfill functions return a nonzero value if the fill is successful.
  14761.  It returns 0 if the fill could not be completed, the starting point lies on
  14762.  the boundary color, or the start point lies outside the clipping region.
  14763.  
  14764.  
  14765.  Compatibility
  14766.  
  14767.   ANSI   DOS   OS/2   UNIX   XENIX
  14768.  
  14769.  
  14770.  
  14771.  See Also
  14772.  
  14773.  _ellipse functions,  _getcolor,  _getfillmask,  _grstatus,  _pie functions,
  14774.  _setfillmask, _setcliprgn,  _setcolor
  14775.  
  14776.  
  14777.  Example
  14778.  
  14779.    /* FLOODFIL.C: This program draws a series of nested rectangles in
  14780.     * different colors, constantly changing the background color.
  14781.     */
  14782.  
  14783.    #include <conio.h>
  14784.    #include <stdlib.h>
  14785.    #include <graph.h>
  14786.  
  14787.    void main()
  14788.    {
  14789.       int loop;
  14790.       int xvar, yvar;
  14791.  
  14792.       /* find a valid graphics mode */
  14793.       if( !_setvideomode( _MAXCOLORMODE ) )
  14794.          exit( 1 );
  14795.  
  14796.       for( xvar = 163, loop = 0; xvar < 320; loop++, xvar += 3 )
  14797.       {
  14798.          _setcolor( loop % 16 );
  14799.          yvar = xvar * 5 / 8;
  14800.          _rectangle( _GBORDER, 320-xvar, 200-yvar, xvar, yvar );
  14801.          _setcolor( rand() % 16 );
  14802.          _floodfill( 0, 0, loop % 16 );
  14803.       }
  14804.       getch();
  14805.       _setvideomode( _DEFAULTMODE );
  14806.    }
  14807.  
  14808.  
  14809.  
  14810.  
  14811.  
  14812.  floor, floorl
  14813.  ────────────────────────────────────────────────────────────────────────────
  14814.  
  14815.  
  14816.  Description
  14817.  
  14818.  Calculate the floor of a value.
  14819.  
  14820.    #include <math.h>
  14821.  
  14822.    double floor( double x );
  14823.  
  14824.    long double floorl( long double x );
  14825.  
  14826.  x                                 Floating-point value
  14827.  
  14828.  
  14829.  Remarks
  14830.  
  14831.  The floor and floorl functions return a floating-point value representing
  14832.  the largest integer that is less than or equal to x.
  14833.  
  14834.  The floorl function is the 80-bit counterpart, and it uses the 80-bit,
  14835.  10-byte coprocessor form of arguments and return values. See the reference
  14836.  page on the long double functions for more details on this data type.
  14837.  
  14838.  
  14839.  Return Value
  14840.  
  14841.  These functions return the floating-point result. There is no error return.
  14842.  
  14843.  
  14844.  
  14845.  Compatibility
  14846.  
  14847.  floor
  14848.  
  14849.   ANSI   DOS   OS/2   UNIX   XENIX
  14850.  
  14851.  
  14852.  floorl
  14853.  
  14854.   ANSI   DOS   OS/2   UNIX   XENIX
  14855.  
  14856.  
  14857.  
  14858.  See Also
  14859.  
  14860.  ceil, fmod
  14861.  
  14862.  
  14863.  Example
  14864.  
  14865.    /* FLOOR.C: This example displays the largest integers less than or equal
  14866.     * to the floating-point values 2.8 and -2.8. It then shows the smallest
  14867.     * integers greater than or equal to 2.8 and -2.8.
  14868.     */
  14869.  
  14870.    #include <math.h>
  14871.    #include <stdio.h>
  14872.  
  14873.    void main()
  14874.    {
  14875.       double y;
  14876.  
  14877.       y = floor( 2.8 );
  14878.       printf( "The floor of 2.8 is %f\n", y );
  14879.       y = floor( -2.8 );
  14880.       printf( "The floor of -2.8 is %f\n", y );
  14881.  
  14882.       y = ceil( 2.8 );
  14883.       printf( "The ceil of 2.8 is %f\n", y );
  14884.       y = ceil( -2.8 );
  14885.       printf( "The ceil of -2.8 is %f\n", y );
  14886.    }
  14887.  
  14888.  
  14889.  Output
  14890.  
  14891.  
  14892.  
  14893.    The floor of 2.8 is 2.000000
  14894.    The floor of -2.8 is -3.000000
  14895.    The ceil of 2.8 is 3.000000
  14896.    The ceil of -2.8 is -2.000000
  14897.  
  14898.  
  14899.  
  14900.  
  14901.  
  14902.  flushall
  14903.  ────────────────────────────────────────────────────────────────────────────
  14904.  
  14905.  
  14906.  Description
  14907.  
  14908.  Flushes all streams; clears all buffers.
  14909.  
  14910.    #include <stdio.h>
  14911.  
  14912.    int flushall( void );
  14913.  
  14914.  
  14915.  Remarks
  14916.  
  14917.  The flushall function writes to its associated files the contents of all
  14918.  buffers associated with open output streams. All buffers associated with
  14919.  open input streams are cleared of their current contents. The next read
  14920.  operation (if there is one) then reads new data from the input files into
  14921.  the buffers.
  14922.  
  14923.  Buffers are automatically flushed when they are full, when streams are
  14924.  closed, or when a program terminates normally without closing streams.
  14925.  
  14926.  All streams remain open after the call to flushall.
  14927.  
  14928.  
  14929.  Return Value
  14930.  
  14931.  The flushall function returns the number of open streams (input and output).
  14932.  There is no error return.
  14933.  
  14934.  
  14935.  Compatibility
  14936.  
  14937.   ANSI   DOS   OS/2   UNIX   XENIX
  14938.  
  14939.  
  14940.  
  14941.  See Also
  14942.  
  14943.  fflush
  14944.  
  14945.  
  14946.  Example
  14947.  
  14948.    /* FLUSHALL.C: This program uses flushall to flush all open buffers. */
  14949.  
  14950.    #include <stdio.h>
  14951.  
  14952.    void main()
  14953.    {
  14954.       int numflushed;
  14955.  
  14956.       numflushed = flushall();
  14957.       printf( "There were %d streams flushed\n", numflushed );
  14958.    }
  14959.  
  14960.  
  14961.  Output
  14962.  
  14963.  
  14964.  
  14965.    There were 3 streams flushed
  14966.  
  14967.  
  14968.  
  14969.  
  14970.  
  14971.  fmod, fmodl
  14972.  ────────────────────────────────────────────────────────────────────────────
  14973.  
  14974.  
  14975.  Description
  14976.  
  14977.  Calculates the floating-point remainder.
  14978.  
  14979.    #include <math.h>
  14980.  
  14981.    double fmod( double x, double y );
  14982.  
  14983.    long double fmodl( long double x, long double y );
  14984.  
  14985.  x, y                              Floating-point values
  14986.  
  14987.  
  14988.  Remarks
  14989.  
  14990.  The fmod and fmodl functions calculate the floating-point remainder f of x /
  14991.  y  such that x = i * y + f, where i is an integer,  f  has the same sign as
  14992.  x, and the absolute value of  f  is less than the absolute value of y.
  14993.  
  14994.  The fmodl function is the 80-bit counterpart; it uses the 80-bit, 10-byte
  14995.  coprocessor form of arguments and return values. See the discussion of the
  14996.  long double functions for more details on this data type.
  14997.  
  14998.  
  14999.  Return Value
  15000.  
  15001.  These functions return the floating-point remainder. If y is 0, the function
  15002.  returns 0.
  15003.  
  15004.  
  15005.  Compatibility
  15006.  
  15007.  fmod
  15008.  
  15009.   ANSI   DOS   OS/2   UNIX   XENIX
  15010.  
  15011.  
  15012.  fmodl
  15013.  
  15014.   ANSI   DOS   OS/2   UNIX   XENIX
  15015.  
  15016.  
  15017.  
  15018.  See Also
  15019.  
  15020.  ceil, fabs, floor
  15021.  
  15022.  
  15023.  Example
  15024.  
  15025.    /* FMOD.C: This program displays a floating-point remainder. */
  15026.  
  15027.    #include <math.h>
  15028.    #include <stdio.h>
  15029.  
  15030.    void main()
  15031.    {
  15032.       double x = -10.0, y = 3.0, z;
  15033.  
  15034.       z = fmod( x, y );
  15035.       printf( "The remainder of %.2f / %.2f is %f\n", x, y, z );
  15036.    }
  15037.  
  15038.  
  15039.  Output
  15040.  
  15041.  
  15042.  
  15043.    The remainder of -10.00 / 3.00 is -1.000000
  15044.  
  15045.  
  15046.  
  15047.  
  15048.  
  15049.  fopen
  15050.  ────────────────────────────────────────────────────────────────────────────
  15051.  
  15052.  
  15053.  Description
  15054.  
  15055.  Opens a file.
  15056.  
  15057.    #include <stdio.h>
  15058.  
  15059.    FILE *fopen( const char *filename, const char *mode );
  15060.  
  15061.  filename                          Path name of file
  15062.  
  15063.  mode                              Type of access permitted
  15064.  
  15065.  
  15066.  Remarks
  15067.  
  15068.  The fopen function opens the file specified by filename. The character
  15069.  string mode specifies the type of access requested for the file, as follows:
  15070.  
  15071.  
  15072.  Type                              Description
  15073.  ────────────────────────────────────────────────────────────────────────────
  15074.  "r"                               Opens for reading. If the file does not
  15075.                                    exist or cannot be found, the fopen call
  15076.                                    will fail.
  15077.  
  15078.  "w"                               Opens an empty file for writing. If the
  15079.                                    given file exists, its contents are
  15080.                                    destroyed.
  15081.  
  15082.  "a"                               Opens for writing at the end of the file
  15083.                                    (appending); creates the file first if
  15084.                                    it doesn't exist.
  15085.  
  15086.  "r+"                              Opens for both reading and writing. (The
  15087.                                    file must exist.)
  15088.  
  15089.  "w+"                              Opens an empty file for both reading and
  15090.                                    writing. If the given file exists, its
  15091.                                    contents are destroyed.
  15092.  
  15093.  "a+"                              Opens for reading and appending; creates
  15094.                                    the file first if it doesn't exist.
  15095.  
  15096.  When a file is opened with the "a" or "a+" access type, all write operations
  15097.  occur at the end of the file. Although the file pointer can be repositioned
  15098.  using fseek or rewind, the file pointer is always moved back to the end of
  15099.  the file before any write operation is carried out. Thus, existing data
  15100.  cannot be overwritten.
  15101.  
  15102.  When the "r+", "w+", or "a+" access type is specified, both reading and
  15103.  writing are allowed (the file is said to be open for "update"). However,
  15104.  when you switch between reading and writing, there must be an intervening
  15105.  fsetpos, fseek, or rewind operation. The current position can be specified
  15106.  for the fsetpos or fseek operation, if desired.
  15107.  
  15108.  In addition to the values listed above, one of the following characters can
  15109.  be included in mode to specify the translation mode for newline characters:
  15110.  
  15111.  
  15112.  Mode                              Meaning
  15113.  ────────────────────────────────────────────────────────────────────────────
  15114.  t                                 Open in text (translated) mode. In this
  15115.                                    mode, carriage-return-line-feed (CR-LF)
  15116.                                    combinations are translated into single
  15117.                                    line feeds (LF) on input and LF
  15118.                                    characters are translated to CR-LF
  15119.                                    combinations on output. Also, CTRL+Z is
  15120.                                    interpreted as an end-of-file character
  15121.                                    on input. In files opened for reading or
  15122.                                    for reading/writing, fopen checks for a
  15123.                                    CTRL+Z at the end of the file and
  15124.                                    removes it, if possible. This is done
  15125.                                    because using the fseek and ftell
  15126.                                    functions to move within a file that
  15127.                                    ends with a CTRL+Z may cause fseek to
  15128.                                    behave improperly near the end of the
  15129.                                    file.
  15130.  
  15131.  b                                 Open in binary (untranslated) mode; the
  15132.                                    above translations are suppressed.
  15133.  
  15134.  If t or b is not given in mode, the translation mode is defined by the
  15135.  default-mode variable _fmode. If t or b is prefixed to the argument, the
  15136.  function will fail and return NULL.
  15137.  
  15138.  See Section 2.7, "Input and Output," for a discussion of text and binary
  15139.  modes.
  15140.  
  15141.  
  15142.  Return Value
  15143.  
  15144.  The fopen function returns a pointer to the open file. A null pointer value
  15145.  indicates an error.
  15146.  
  15147.  
  15148.  Compatibility
  15149.  
  15150.   ANSI   DOS   OS/2   UNIX   XENIX
  15151.  
  15152.  
  15153.  Note that the t option is not part of the ANSI standard for fopen; it is a
  15154.  Microsoft extension and should not be used where ANSI portability is
  15155.  desired.
  15156.  
  15157.  
  15158.  See Also
  15159.  
  15160.  fclose, fcloseall, fdopen, ferror, fileno, freopen, open, setmode
  15161.  
  15162.  
  15163.  Example
  15164.  
  15165.    /* FOPEN.C: This program opens files named "data" and "data2". It uses
  15166.     * fclose to close "data" and fcloseall to close all remaining files.
  15167.     */
  15168.  
  15169.    #include <stdio.h>
  15170.  
  15171.    FILE *stream, *stream2;
  15172.  
  15173.    void main()
  15174.    {
  15175.       int numclosed;
  15176.  
  15177.       /* Open for read (will fail if 'data' does not exist) */
  15178.       if( (stream  = fopen( "data", "r" )) == NULL )
  15179.          printf( "The file 'data' was not opened\n" );
  15180.       else
  15181.          printf( "The file 'data' was opened\n" );
  15182.  
  15183.       /* Open for write */
  15184.       if( (stream2 = fopen( "data2", "w+" )) == NULL )
  15185.          printf( "The file 'data2' was not opened\n" );
  15186.       else
  15187.          printf( "The file 'data2' was opened\n" );
  15188.  
  15189.       /* Close stream */
  15190.       if( fclose( stream ) )
  15191.          printf( "The file 'data' was not closed\n" );
  15192.  
  15193.       /* All other files are closed: */
  15194.       numclosed = fcloseall( );
  15195.       printf( "Number of files closed by fcloseall: %u\n", numclosed );
  15196.    }
  15197.  
  15198.  
  15199.  Output
  15200.  
  15201.  
  15202.  
  15203.    The file 'data' was opened
  15204.    The file 'data2' was opened
  15205.    Number of files closed by fcloseall: 1
  15206.  
  15207.  
  15208.  
  15209.  
  15210.  
  15211.  FP_OFF, FP_SEG
  15212.  ────────────────────────────────────────────────────────────────────────────
  15213.  
  15214.  
  15215.  Description
  15216.  
  15217.  Get or set a far-pointer offset (FP_OFF) or a far-pointer segment (FP_SEG).
  15218.  
  15219.  
  15220.    #include <dos.h>
  15221.  
  15222.    unsigned FP_OFF( void _far *address );
  15223.  
  15224.    unsigned FP_SEG( void _far *address );
  15225.  
  15226.  address                           Far pointer to memory address
  15227.  
  15228.  
  15229.  Remarks
  15230.  
  15231.  The FP_OFF and FP_SEG macros can be used to set or get the offset and
  15232.  segment, respectively, of the far pointer at address.
  15233.  
  15234.  
  15235.  Return Value
  15236.  
  15237.  The FP_OFF macro returns an offset. The FP_SEG macro returns a segment
  15238.  address.
  15239.  
  15240.  
  15241.  Compatibility
  15242.  
  15243.   ANSI   DOS   OS/2   UNIX   XENIX
  15244.  
  15245.  
  15246.  
  15247.  Example
  15248.  
  15249.    /* FP_SEG.C: This program uses FP_SEG and FP_OFF to obtain
  15250.     * the segment and offset of the long pointer p.
  15251.     */
  15252.  
  15253.    #include <dos.h>
  15254.    #include <malloc.h>
  15255.    #include <stdio.h>
  15256.  
  15257.  
  15258.    void main()
  15259.    {
  15260.       void _far *p;
  15261.       unsigned int seg_val;
  15262.       unsigned int off_val;
  15263.  
  15264.       p = _fmalloc( 100 );        /* Points pointer at something */
  15265.  
  15266.       seg_val = FP_SEG( p );      /* Gets address pointed to */
  15267.       off_val = FP_OFF( p );
  15268.  
  15269.       printf( "Segment is %.4X; Offset is %.4X\n", seg_val, off_val );
  15270.    }
  15271.  
  15272.  
  15273.  Output
  15274.  
  15275.  
  15276.  
  15277.    Segment is 00C7; Offset is 0016
  15278.  
  15279.  
  15280.  
  15281.  
  15282.  
  15283.  _fpreset
  15284.  ────────────────────────────────────────────────────────────────────────────
  15285.  
  15286.  
  15287.  Description
  15288.  
  15289.  Resets the floating-point package.
  15290.  
  15291.    #include <float.h>
  15292.  
  15293.    void _fpreset( void );
  15294.  
  15295.  
  15296.  Remarks
  15297.  
  15298.  The _fpreset function reinitializes the floating-point-math package. This
  15299.  function is usually used in conjunction with signal, system, or the exec or
  15300.  spawn functions.
  15301.  
  15302.  If a program traps floating-point error signals (SIGFPE) with signal, it can
  15303.  safely recover from floating-point errors by invoking _fpreset and using
  15304.  longjmp.
  15305.  
  15306.  In DOS versions prior to 3.0, a child process executed by exec, spawn, or
  15307.  system may affect the floating-point state of the parent process if an 8087
  15308.  or 80287 coprocessor is used. If you are using either coprocessor, the
  15309.  following precautions are recommended:
  15310.  
  15311.  
  15312.    ■   The exec, spawn, and system functions should not be called during the
  15313.        evaluation of a floating-point expression.
  15314.  
  15315.    ■   The _fpreset function should be called after these routines if there
  15316.        is a possibility of the child process performing any floating-point
  15317.        operations.
  15318.  
  15319.  
  15320.  
  15321.  Return Value
  15322.  
  15323.  None.
  15324.  
  15325.  
  15326.  Compatibility
  15327.  
  15328.   ANSI   DOS   OS/2   UNIX   XENIX
  15329.  
  15330.  
  15331.  
  15332.  See Also
  15333.  
  15334.  exec functions, signal, spawn functions
  15335.  
  15336.  
  15337.  Example
  15338.  
  15339.    /* FPRESET.C: This program uses signal to set up a routine for handling
  15340.     * floating-point errors.
  15341.     */
  15342.  
  15343.    #include <stdio.h>
  15344.    #include <signal.h>
  15345.    #include <setjmp.h>
  15346.    #include <stdlib.h>
  15347.    #include <float.h>
  15348.    #include <math.h>
  15349.    #include <string.h>
  15350.  
  15351.    jmp_buf mark;                       /* Address for long jump to jump to */
  15352.    int  fperr;                         /* Global error number */
  15353.  
  15354.    void fphandler( int sig, int num ); /* Prototypes */
  15355.    void fpcheck( void );
  15356.  
  15357.    void main()
  15358.    {
  15359.        double n1, n2, r;
  15360.        int jmpret;
  15361.  
  15362.        /* Set up floating point error handler. */
  15363.        if( signal( SIGFPE, fphandler ) == SIG_ERR )
  15364.        {
  15365.            fprintf( stderr, "Couldn't set SIGFPE\n" );
  15366.            abort();
  15367.        }
  15368.  
  15369.        /* Save stack environment for return in case of error. First time
  15370.         * through, jmpret is 0, so true conditional is executed. If an
  15371.         * error occurs, jmpret will be set to -1 and false conditional
  15372.         * will be executed.
  15373.         */
  15374.        jmpret = setjmp( mark );
  15375.        if( jmpret == 0 )
  15376.        {
  15377.            printf( "Test for invalid operation - " );
  15378.            printf( "enter two numbers: " );
  15379.            scanf( "%lf %lf", &n1, &n2 );
  15380.  
  15381.            r = n1 / n2;
  15382.            /* This won't be reached if error occurs. */
  15383.            printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );
  15384.  
  15385.            r = n1 * n2;
  15386.            /* This won't be reached if error occurs. */
  15387.            printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );
  15388.        }
  15389.        else
  15390.            fpcheck();
  15391.    }
  15392.  
  15393.    /* Handles SIGFPE (floating point error) interrupt. */
  15394.    void fphandler( int sig, int num )
  15395.    {
  15396.        /* Set global for outside check since we don't want to do I/O in the
  15397.         * handler.
  15398.         */
  15399.        fperr = num;
  15400.  
  15401.        /* Initialize floating-point package. */
  15402.        _fpreset();
  15403.  
  15404.        /* Restore calling environment and jump back to setjmp. Return -1
  15405.         * so that setjmp will return false for conditional test.
  15406.         */
  15407.        longjmp( mark, -1 );
  15408.    }
  15409.  
  15410.    void fpcheck()
  15411.    {
  15412.        char fpstr[30];
  15413.  
  15414.        switch( fperr )
  15415.        {
  15416.            case FPE_INVALID:
  15417.                strcpy( fpstr, "Invalid number" );
  15418.                break;
  15419.  
  15420.            case FPE_OVERFLOW:
  15421.                strcpy( fpstr, "Overflow" );
  15422.                break;
  15423.  
  15424.            case FPE_UNDERFLOW:
  15425.                strcpy( fpstr, "Underflow" );
  15426.                break;
  15427.  
  15428.            case FPE_ZERODIVIDE:
  15429.                strcpy( fpstr, "Divide by zero" );
  15430.                break;
  15431.  
  15432.            default:
  15433.                strcpy( fpstr, "Other floating point error" );
  15434.                break;
  15435.        }
  15436.        printf( "Error %d: %s\n", fperr, fpstr );
  15437.    }
  15438.  
  15439.  
  15440.  Output
  15441.  
  15442.  
  15443.  
  15444.    Test for invalid operation - enter two numbers: 5 0
  15445.    Error 131: Divide by zero
  15446.  
  15447.  
  15448.  
  15449.  
  15450.  
  15451.  fprintf
  15452.  ────────────────────────────────────────────────────────────────────────────
  15453.  
  15454.  
  15455.  Description
  15456.  
  15457.  Prints formatted data to a stream.
  15458.  
  15459.    #include <stdio.h>
  15460.  
  15461.    int fprintf( FILE *stream, const char *format [[, argument]]... );
  15462.  
  15463.  stream                            Pointer to FILE structure
  15464.  
  15465.  format                            Format-control string
  15466.  
  15467.  argument                          Optional arguments
  15468.  
  15469.  
  15470.  Remarks
  15471.  
  15472.  The fprintf function formats and prints a series of characters and values to
  15473.  the output stream. Each argument (if any) is converted and output according
  15474.  to the corresponding format specification in format.
  15475.  
  15476.  The format argument has the same form and function that it does for the
  15477.  printf function; see the Remarks section for the printf function for more
  15478.  information on format and argument.
  15479.  
  15480.  
  15481.  Return Value
  15482.  
  15483.  The fprintf function returns the number of characters printed, or a negative
  15484.  value in the case of an output error.
  15485.  
  15486.  
  15487.  Compatibility
  15488.  
  15489.   ANSI   DOS   OS/2   UNIX   XENIX
  15490.  
  15491.  
  15492.  
  15493.  See Also
  15494.  
  15495.  cprintf, fscanf, printf, sprintf
  15496.  
  15497.  
  15498.  Example
  15499.  
  15500.    /* FPRINTF.C: This program uses fprintf to format various data and
  15501.     * print them to the file named FPRINTF.OUT. It then displays
  15502.     * FPRINTF.OUT on the screen using the system function to invoke
  15503.     * the DOS TYPE command.
  15504.     */
  15505.  
  15506.    #include <stdio.h>
  15507.    #include <process.h>
  15508.  
  15509.    FILE *stream;
  15510.  
  15511.    void main()
  15512.    {
  15513.       int    i = 10;
  15514.       double fp = 1.5;
  15515.       char   s[] = "this is a string";
  15516.       char   c = '\n';
  15517.  
  15518.       stream = fopen( "fprintf.out", "w" );
  15519.       fprintf( stream, "%s%c", s, c );
  15520.       fprintf( stream, "%d\n", i );
  15521.       fprintf( stream, "%f\n", fp );
  15522.       fclose( stream );
  15523.       system( "type fprintf.out" );
  15524.    }
  15525.  
  15526.  
  15527.  Output
  15528.  
  15529.  
  15530.  
  15531.    this is a string
  15532.    10
  15533.    1.500000
  15534.  
  15535.  
  15536.  
  15537.  
  15538.  
  15539.  fputc, fputchar
  15540.  ────────────────────────────────────────────────────────────────────────────
  15541.  
  15542.  
  15543.  Description
  15544.  
  15545.  Write a character to a stream (fputc) or to stdout (fputchar).
  15546.  
  15547.    #include <stdio.h>
  15548.  
  15549.    int fputc( int c, FILE *stream );
  15550.  
  15551.    int fputchar( int c );
  15552.  
  15553.  c                                 Character to be written
  15554.  
  15555.  stream                            Pointer to FILE structure
  15556.  
  15557.  
  15558.  Remarks
  15559.  
  15560.  The fputc function writes the single character c to the output stream at the
  15561.  current position. The fputchar function is equivalent to fputc(c, stdout).
  15562.  
  15563.  The fputc and fputchar routines are similar to putc and putchar, but are
  15564.  functions rather than macros.
  15565.  
  15566.  
  15567.  Return Value
  15568.  
  15569.  The fputc and fputchar functions return the character written. A return
  15570.  value of EOF indicates an error.
  15571.  
  15572.  
  15573.  Compatibility
  15574.  
  15575.  fputc
  15576.  
  15577.   ANSI   DOS   OS/2   UNIX   XENIX
  15578.  
  15579.  
  15580.  fputchar
  15581.  
  15582.   ANSI   DOS   OS/2   UNIX   XENIX
  15583.  
  15584.  
  15585.  
  15586.  See Also
  15587.  
  15588.  fgetc, fgetchar, putc, putchar
  15589.  
  15590.  
  15591.  Example
  15592.  
  15593.    /* FPUTC.C: This program uses fputc and fputchar to send a character
  15594.     * array to stdout.
  15595.     */
  15596.  
  15597.    #include <stdio.h>
  15598.  
  15599.    void main()
  15600.    {
  15601.       char strptr1[] = "This is a test of fputc!!\n";
  15602.       char strptr2[] = "This is a test of fputchar!!\n";
  15603.       char *p;
  15604.  
  15605.       /* Print line to stream using fputc. */
  15606.       p = strptr1;
  15607.       while( (*p != '\0') && fputc( *(p++), stdout ) != EOF )
  15608.          ;
  15609.  
  15610.       /* Print line to stream using fputchar. */
  15611.       p = strptr2;
  15612.       while( (*p != '\0') && fputchar( *(p++) ) != EOF )
  15613.          ;
  15614.    }
  15615.  
  15616.  
  15617.  Output
  15618.  
  15619.  
  15620.  
  15621.    This is a test of fputc!!
  15622.    This is a test of fputchar!!
  15623.  
  15624.  
  15625.  
  15626.  
  15627.  
  15628.  fputs
  15629.  ────────────────────────────────────────────────────────────────────────────
  15630.  
  15631.  
  15632.  Description
  15633.  
  15634.  Writes a string to a stream.
  15635.  
  15636.    #include <stdio.h>
  15637.  
  15638.    int fputs( const char *string, FILE *stream );
  15639.  
  15640.  string                            String to be output
  15641.  
  15642.  stream                            Pointer to FILE structure
  15643.  
  15644.  
  15645.  Remarks
  15646.  
  15647.  The fputs function copies string to the output stream at the current
  15648.  position. The terminating null character ('\0') is not copied.
  15649.  
  15650.  
  15651.  Return Value
  15652.  
  15653.  The fputs function returns a nonnegative value if it is successful. If an
  15654.  error occurs, it returns EOF.
  15655.  
  15656.  
  15657.  Compatibility
  15658.  
  15659.   ANSI   DOS   OS/2   UNIX   XENIX
  15660.  
  15661.  
  15662.  
  15663.  See Also
  15664.  
  15665.  fgets, gets, puts
  15666.  
  15667.  
  15668.  Example
  15669.  
  15670.    /* FPUTS.C: This program uses fputs to write a single line to the
  15671.     * stdout stream.
  15672.     */
  15673.  
  15674.    #include <stdio.h>
  15675.  
  15676.    void main()
  15677.    {
  15678.       fputs( "Hello world from fputs.\n", stdout );
  15679.    }
  15680.  
  15681.  
  15682.  Output
  15683.  
  15684.  
  15685.  
  15686.    Hello world from fputs.
  15687.  
  15688.  
  15689.  
  15690.  
  15691.  
  15692.  fread
  15693.  ────────────────────────────────────────────────────────────────────────────
  15694.  
  15695.  
  15696.  Description
  15697.  
  15698.  Reads data from a stream.
  15699.  
  15700.    #include <stdio.h>
  15701.  
  15702.    size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
  15703.  
  15704.  buffer                            Storage location for data
  15705.  
  15706.  size                              Item size in bytes
  15707.  
  15708.  count                             Maximum number of items to be read
  15709.  
  15710.  stream                            Pointer to FILE structure
  15711.  
  15712.  
  15713.  Remarks
  15714.  
  15715.  The fread function reads up to count items of size bytes from the input
  15716.  stream and stores them in buffer. The file pointer associated with stream
  15717.  (if there is one) is increased by the number of bytes actually read.
  15718.  
  15719.  If the given stream is opened in text mode, carriage-return-line-feed pairs
  15720.  are replaced with single line-feed characters. The replacement has no effect
  15721.  on the file pointer or the return value.
  15722.  
  15723.  The file-pointer position is indeterminate if an error occurs. The value of
  15724.  a partially read item cannot be determined.
  15725.  
  15726.  
  15727.  Return Value
  15728.  
  15729.  The fread function returns the number of full items actually read, which may
  15730.  be less than count if an error occurs or if the file end is encountered
  15731.  before reaching count.
  15732.  
  15733.  The feof or ferror function should be used to distinguish a read error from
  15734.  an end-of-file condition. If size or count is 0, fread returns 0 and the
  15735.  buffer contents are unchanged.
  15736.  
  15737.  
  15738.  Compatibility
  15739.  
  15740.   ANSI   DOS   OS/2   UNIX   XENIX
  15741.  
  15742.  
  15743.  
  15744.  See Also
  15745.  
  15746.  fwrite, read
  15747.  
  15748.  
  15749.  Example
  15750.  
  15751.    /* FREAD.C: This program opens a file named FREAD.OUT and writes 25
  15752.     * characters to the file. It then tries to open FREAD.OUT and
  15753.     * read in 25 characters. If the attempt succeeds, the program
  15754.     * displays the number of actual items read.
  15755.     */
  15756.  
  15757.    #include <stdio.h>
  15758.  
  15759.    void main()
  15760.    {
  15761.       FILE *stream;
  15762.       char list[30];
  15763.       int  i, numread, numwritten;
  15764.  
  15765.       /* Open file in text mode: */
  15766.       if( (stream = fopen( "fread.out", "w+t" )) != NULL )
  15767.       {
  15768.          for ( i = 0; i < 25; i++ )
  15769.             list[i] = 'z' - i;
  15770.          /* Write 25 characters to stream */
  15771.          numwritten = fwrite( list, sizeof( char ), 25, stream );
  15772.          printf( "Wrote %d items\n", numwritten );
  15773.          fclose( stream );
  15774.       }
  15775.       else
  15776.          printf( "Problem opening the file\n" );
  15777.  
  15778.       if( (stream = fopen( "fread.out", "r+t" )) != NULL )
  15779.       {
  15780.          /* Attempt to read in 25 characters */
  15781.          numread = fread( list, sizeof( char ), 25, stream );
  15782.          printf( "Number of items read = %d\n", numread );
  15783.          printf( "Contents of buffer = %.25s\n", list );
  15784.          fclose( stream );
  15785.       }
  15786.       else
  15787.          printf( "Was not able to open the file\n" );
  15788.    }
  15789.  
  15790.  
  15791.  Output
  15792.  
  15793.  
  15794.  
  15795.    Wrote 25 items
  15796.    Number of items read = 25
  15797.    Contents of buffer = zyxwvutsrqponmlkjihgfedcb
  15798.  
  15799.  
  15800.  
  15801.  
  15802.  
  15803.  free Functions
  15804.  ────────────────────────────────────────────────────────────────────────────
  15805.  
  15806.  
  15807.  Description
  15808.  
  15809.  Deallocate a memory block.
  15810.  
  15811.  #include <stdlib.h>               For ANSI compatibility (free only)
  15812.  
  15813.  #include <malloc.h>               Required only for function declarations
  15814.  
  15815.    void free( void *memblock );
  15816.  
  15817.    void _bfree( _segment seg, void _based( void ) *memblock );
  15818.  
  15819.    void _ffree( void _far *memblock );
  15820.  
  15821.    void _nfree( void _near *memblock );
  15822.  
  15823.  memblock                          Allocated memory block
  15824.  
  15825.  seg                               Based-heap segment selector
  15826.  
  15827.  
  15828.  Remarks
  15829.  
  15830.  The free family of functions deallocates a memory block. The argument
  15831.  memblock points to a memory block previously allocated through a call to
  15832.  calloc, malloc, or realloc. The number of bytes freed is the number of bytes
  15833.  specified when the block was allocated (or reallocated, in the case of
  15834.  realloc). After the call, the freed block is available for allocation.
  15835.  
  15836.  The seg argument specifies the based heap containing the memory block to be
  15837.  freed by the _bfree function.
  15838.  
  15839.  Attempting to free an invalid pointer may affect subsequent allocation and
  15840.  cause errors. An invalid pointer is one not allocated with the appropriate
  15841.  call.
  15842.  
  15843.  The following restrictions apply to use of the free, _bfree, _ffree, and
  15844.  _nfree functions:
  15845.  
  15846.  Blocks allocated with:            Should be freed with:
  15847.  ────────────────────────────────────────────────────────────────────────────
  15848.  calloc, malloc, realloc           free
  15849.  
  15850.  _bcalloc, _bmalloc, _brealloc     _bfree
  15851.  
  15852.  _fcalloc, _fmalloc, _frealloc     _ffree
  15853.  
  15854.  _ncalloc, _nmalloc, _nrealloc     _nfree
  15855.  
  15856.  A NULL pointer argument is ignored.
  15857.  
  15858.  In large data models (compact-, large-, and huge-model programs), free maps
  15859.  to _ffree. In small data models (tiny-, small-, and medium-model programs),
  15860.  free maps to _nfree.
  15861.  
  15862.  The various free functions deallocate a memory block in the segments shown
  15863.  in the list below:
  15864.  
  15865.  Function                          Data Segment
  15866.  ────────────────────────────────────────────────────────────────────────────
  15867.  free                              Depends on data model of program
  15868.  
  15869.  _bfree                            Based heap specified by seg value
  15870.  
  15871.  _ffree                            Far heap (outside default data segment)
  15872.  
  15873.  _nfree                            Near heap (inside default data segment)
  15874.  
  15875.  
  15876.  Return Value
  15877.  
  15878.  None.
  15879.  
  15880.  
  15881.  Compatibility
  15882.  
  15883.  free
  15884.  
  15885.   ANSI   DOS   OS/2   UNIX   XENIX
  15886.  
  15887.  
  15888.  _bfree, _ffree, _nfree
  15889.  
  15890.   ANSI   DOS   OS/2   UNIX   XENIX
  15891.  
  15892.  
  15893.  
  15894.  See Also
  15895.  
  15896.  calloc functions, malloc functions, realloc functions
  15897.  
  15898.  
  15899.  Example
  15900.  
  15901.    /* MALLOC.C: This program allocates memory with malloc, then frees
  15902.     * the memory with free.
  15903.     */
  15904.  
  15905.    #include <stdlib.h>         /* Definition of _MAX_PATH */
  15906.    #include <stdio.h>
  15907.    #include <malloc.h>
  15908.  
  15909.    void main()
  15910.    {
  15911.       char *string;
  15912.  
  15913.       /* Allocate space for a path name */
  15914.       string = malloc( _MAX_PATH );
  15915.       if( string == NULL )
  15916.          printf( "Insufficient memory available\n" );
  15917.       else
  15918.          printf( "Memory space allocated for path name\n" );
  15919.       free( string );
  15920.       printf( "Memory freed\n" );
  15921.    }
  15922.  
  15923.  
  15924.  Output
  15925.  
  15926.  
  15927.  
  15928.    Memory space allocated for path name
  15929.    Memory freed
  15930.  
  15931.  
  15932.  
  15933.  
  15934.  
  15935.  _freect
  15936.  ────────────────────────────────────────────────────────────────────────────
  15937.  
  15938.  
  15939.  Description
  15940.  
  15941.  Returns the amount of memory available for memory allocation.
  15942.  
  15943.  #include <malloc.h>               Required only for function declarations
  15944.  
  15945.    unsigned int _freect( size_t size );
  15946.  
  15947.  size                              Item size in bytes
  15948.  
  15949.  
  15950.  Remarks
  15951.  
  15952.  The _freect function tells you how much memory is available for dynamic
  15953.  memory allocation in the near heap. It does so by returning the approximate
  15954.  number of times your program can call _nmalloc (or malloc in small data
  15955.  models) to allocate an item size bytes long in the near heap (default data
  15956.  segment).
  15957.  
  15958.  
  15959.  Return Value
  15960.  
  15961.  The _freect function returns the number of calls as an unsigned integer.
  15962.  
  15963.  
  15964.  Compatibility
  15965.  
  15966.   ANSI   DOS   OS/2   UNIX   XENIX
  15967.  
  15968.  
  15969.  
  15970.  See Also
  15971.  
  15972.  calloc functions, _expand functions, malloc functions, _memavl, _msize
  15973.  functions,  realloc functions
  15974.  
  15975.  
  15976.  Example
  15977.  
  15978.    /* FREECT.C: This program determines how much free space is available for
  15979.     * integers in the default data segment. Then it allocates space for
  15980.     * 1,000 integers and checks the space again, using _freect.
  15981.     */
  15982.  
  15983.    #include <malloc.h>
  15984.    #include <stdio.h>
  15985.  
  15986.    void main()
  15987.    {
  15988.       int i;
  15989.  
  15990.       /* First report on the free space: */
  15991.       printf( "Integers (approximate) available on heap: %u\n\n",
  15992.               _freect( sizeof( int ) ) );
  15993.  
  15994.       /* Allocate space for 1000 integers: */
  15995.       for( i = 0; i < 1000; ++i )
  15996.          malloc( sizeof( int ) );
  15997.  
  15998.    /* Report again on the free space: */
  15999.       printf( "After allocating space for 1000 integers:\n" );
  16000.       printf( "Integers (approximate) available on heap: %u\n\n",
  16001.               _freect( sizeof( int ) ) );
  16002.  
  16003.    }
  16004.  
  16005.  
  16006.  Output
  16007.  
  16008.  
  16009.  
  16010.    Integers (approximate) available on heap: 15212
  16011.  
  16012.    After allocating space for 1000 integers:
  16013.    Integers (approximate) available on heap: 14084
  16014.  
  16015.  
  16016.  
  16017.  
  16018.  
  16019.  freopen
  16020.  ────────────────────────────────────────────────────────────────────────────
  16021.  
  16022.  
  16023.  Description
  16024.  
  16025.  Reassigns a file pointer.
  16026.  
  16027.    #include <stdio.h>
  16028.  
  16029.    FILE *freopen( const char *filename, const char *mode, FILE *stream );
  16030.  
  16031.  filename                          Path name of new file
  16032.  
  16033.  mode                              Type of access permitted
  16034.  
  16035.  stream                            Pointer to FILE structure
  16036.  
  16037.  
  16038.  Remarks
  16039.  
  16040.  The freopen function closes the file currently associated with stream and
  16041.  reassigns stream to the file specified by filename. The freopen function is
  16042.  typically used to redirect the pre-opened files stdin, stdout, and stderr to
  16043.  files specified by the user. The new file associated with stream is opened
  16044.  with mode, which is a character string specifying the type of access
  16045.  requested for the file, as follows:
  16046.  
  16047.  Type                              Description
  16048.  ────────────────────────────────────────────────────────────────────────────
  16049.  "r"                               Opens for reading. If the file does not
  16050.                                    exist or cannot be found, the freopen
  16051.                                    call fails.
  16052.  
  16053.  "w"                               Opens an empty file for writing. If the
  16054.                                    given file exists, its contents are
  16055.                                    destroyed.
  16056.  
  16057.  "a"                               Opens for writing at the end of the file
  16058.                                    (appending); creates the file first if
  16059.                                    it does not exist.
  16060.  
  16061.  "r+"                              Opens for both reading and writing. (The
  16062.                                    file must exist.)
  16063.  
  16064.  "w+"                              Opens an empty file for both reading and
  16065.                                    writing. If the given file exists, its
  16066.                                    contents are destroyed.
  16067.  
  16068.  "a+"                              Opens for reading and appending; creates
  16069.                                    the file first if it does not exist.
  16070.  
  16071.  Use the "w" and "w+" types with care, as they can destroy existing files.
  16072.  
  16073.  When a file is opened with the "a" or "a+" access type, all write operations
  16074.  take place at the end of the file. Although the file pointer can be
  16075.  repositioned using fseek or rewind, the file pointer is always moved back to
  16076.  the end of the file before any write operation is carried out. Thus,
  16077.  existing data cannot be overwritten.
  16078.  
  16079.  When the "r+", "w+", or "a+" access type is specified, both reading and
  16080.  writing are allowed (the file is said to be open for "update"). However,
  16081.  when you switch between reading and writing, there must be an intervening
  16082.  fsetpos, fseek, or rewind operation. The current position can be specified
  16083.  for the fsetpos or fseek operation, if desired.
  16084.  
  16085.  In addition to the values listed above, one of the following characters may
  16086.  be included in the mode string to specify the translation mode for newlines.
  16087.  
  16088.  
  16089.  Mode                              Meaning
  16090.  ────────────────────────────────────────────────────────────────────────────
  16091.  t                                 Open in text (translated) mode;
  16092.                                    carriage-return-line-feed
  16093.                                    (CR-LF) combinations are translated into
  16094.                                    single line-feed (LF) characters on
  16095.                                    input; LF characters are translated to
  16096.                                    CR-LF combinations on output. Also,
  16097.                                    CTRL+Z is interpreted as an end-of-file
  16098.                                    character on input. In files opened for
  16099.                                    reading, or writing and reading, the
  16100.                                    run-time library checks for a CTRL+Z at
  16101.                                    the end of the file and removes it, if
  16102.                                    possible. This is done because using the
  16103.                                    fseek and ftell functions to move within
  16104.                                    a file may cause fseek to behave
  16105.                                    improperly near the end of the file.
  16106.  
  16107.  b                                 Open in binary (untranslated) mode; the
  16108.                                    above translations are suppressed.
  16109.  
  16110.  If t or b is not given in the mode string, the translation mode is defined
  16111.  by the default mode variable _fmode.
  16112.  
  16113.  See Section 2.7, "Input and Output," for a discussion of text and binary
  16114.  modes.
  16115.  
  16116.  
  16117.  Return Value
  16118.  
  16119.  The freopen function returns a pointer to the newly opened file. If an error
  16120.  occurs, the original file is closed and the function returns a NULL pointer
  16121.  value.
  16122.  
  16123.  
  16124.  Compatibility
  16125.  
  16126.   ANSI   DOS   OS/2   UNIX   XENIX
  16127.  
  16128.  
  16129.  The t option is not part of the ANSI standard for freopen; it is a Microsoft
  16130.  extension that should not be used where ANSI portability is desired.
  16131.  
  16132.  
  16133.  See Also
  16134.  
  16135.  fclose, fcloseall, fdopen, fileno, fopen, open, setmode
  16136.  
  16137.  
  16138.  Example
  16139.  
  16140.    /* FREOPEN.C: This program reassigns stdaux to the file
  16141.     * named FREOPEN.OUT and writes a line to that file.
  16142.     */
  16143.  
  16144.    #include <stdio.h>
  16145.    #include <stdlib.h>
  16146.  
  16147.    FILE *stream;
  16148.  
  16149.    void main()
  16150.    {
  16151.  
  16152.       /* Reassign "stdaux" to "freopen.out": */
  16153.       stream = freopen( "freopen.out", "w", stdaux );
  16154.  
  16155.       if( stream == NULL )
  16156.          fprintf( stdout, "error on freopen\n" );
  16157.       else
  16158.       {
  16159.          fprintf( stream, "This will go to the file 'freopen.out'\n" );
  16160.          fprintf( stdout, "successfully reassigned\n" );
  16161.          fclose( stream );
  16162.       }
  16163.       system( "type freopen.out" );
  16164.    }
  16165.  
  16166.  
  16167.  Output
  16168.  
  16169.  
  16170.  
  16171.    successfully reassigned
  16172.    This will go to the file 'freopen.out'
  16173.  
  16174.  
  16175.  
  16176.  
  16177.  
  16178.  frexp, frexpl
  16179.  ────────────────────────────────────────────────────────────────────────────
  16180.  
  16181.  
  16182.  Description
  16183.  
  16184.  Get the mantissa and exponent of a floating-point number.
  16185.  
  16186.    #include <math.h>
  16187.  
  16188.    double frexp( double x, int *expptr );
  16189.  
  16190.    long double frexpl( long double x, int *expptr );
  16191.  
  16192.  x                                 Floating-point value
  16193.  
  16194.  expptr                            Pointer to stored integer exponent
  16195.  
  16196.  
  16197.  Remarks
  16198.  
  16199.  The frexp and frexpl functions break down the floating-point value (x) into
  16200.  a mantissa (m) and an exponent (n), such that the absolute value of m is
  16201.  greater than or equal to 0.5 and less than 1.0, and x = m*2n. The integer
  16202.  exponent n is stored at the location pointed to by expptr.
  16203.  
  16204.  The frexpl function is the 80-bit counterpart and uses an 80-bit, 10-byte
  16205.  coprocessor form of arguments and return values. See the reference page on
  16206.  the long double functions for more details on this data type.
  16207.  
  16208.  
  16209.  Return Value
  16210.  
  16211.  These functions return the mantissa. If x is 0, the function returns 0 for
  16212.  both the mantissa and the exponent. There is no error return.
  16213.  
  16214.  
  16215.  Compatibility
  16216.  
  16217.  frexp
  16218.  
  16219.   ANSI   DOS   OS/2   UNIX   XENIX
  16220.  
  16221.  
  16222.  frexpl
  16223.  
  16224.   ANSI   DOS   OS/2   UNIX   XENIX
  16225.  
  16226.  
  16227.  
  16228.  See Also
  16229.  
  16230.  ldexp functions, modf
  16231.  
  16232.  
  16233.  Example
  16234.  
  16235.    /* FREXP.C: This program calculates frexp( 16.4, &n ), then displays y
  16236.     * and n.
  16237.     */
  16238.  
  16239.    #include <math.h>
  16240.    #include <stdio.h>
  16241.  
  16242.    void main()
  16243.    {
  16244.       double x, y;
  16245.       int n;
  16246.  
  16247.       x = 16.4;
  16248.       y = frexp( x, &n );
  16249.       printf( "frexp( %f, &n ) = %f, n = %d\n", x, y, n );
  16250.    }
  16251.  
  16252.  
  16253.  Output
  16254.  
  16255.  
  16256.  
  16257.    frexp( 16.400000, &n ) = 0.512500, n = 5
  16258.  
  16259.  
  16260.  
  16261.  
  16262.  
  16263.  fscanf
  16264.  ────────────────────────────────────────────────────────────────────────────
  16265.  
  16266.  
  16267.  Description
  16268.  
  16269.  Reads formatted data from a stream.
  16270.  
  16271.    #include <stdio.h>
  16272.  
  16273.    int fscanf( FILE *stream, const char *format [[, argument]]...  )
  16274.  
  16275.  stream                            Pointer to FILE structure
  16276.  
  16277.  format                            Format-control string
  16278.  
  16279.  argument                          Optional arguments
  16280.  
  16281.  
  16282.  Remarks
  16283.  
  16284.  The fscanf function reads data from the current position of stream into the
  16285.  locations given by argument (if any). Each argument must be a pointer to a
  16286.  variable with a type that corresponds to a type specifier in format. The
  16287.  format controls the interpretation of the input fields and has the same form
  16288.  and function as the format argument for the scanf function; see scanf for a
  16289.  description of format.
  16290.  
  16291.  
  16292.  Return Value
  16293.  
  16294.  The fscanf function returns the number of fields that were successfully
  16295.  converted and assigned. The return value does not include fields that were
  16296.  read but not assigned.
  16297.  
  16298.  The return value is EOF for an error or end-of-file on stream before the
  16299.  first conversion. A return value of 0 means that no fields were assigned.
  16300.  
  16301.  
  16302.  Compatibility
  16303.  
  16304.   ANSI   DOS   OS/2   UNIX   XENIX
  16305.  
  16306.  
  16307.  
  16308.  See Also
  16309.  
  16310.  cscanf, fprintf, scanf, sscanf
  16311.  
  16312.  
  16313.  Example
  16314.  
  16315.    /* FSCANF.C: This program writes formatted data to a file. It
  16316.     * then uses fscanf to read the various data back from the file.
  16317.     */
  16318.  
  16319.    #include <stdio.h>
  16320.  
  16321.    FILE *stream;
  16322.  
  16323.    void main()
  16324.    {
  16325.       long l;
  16326.       float fp;
  16327.       char s[81];
  16328.       char c;
  16329.       int result;
  16330.  
  16331.       stream = fopen( "fscanf.out", "w+" );
  16332.       if( stream == NULL )
  16333.          printf( "The file fscanf.out was not opened\n" );
  16334.       else
  16335.       {
  16336.          fprintf( stream, "%s %ld %f%c", "a-string", 65000, 3.14159, 'x' );
  16337.  
  16338.          /* Set pointer to beginning of file: */
  16339.          fseek( stream, 0L, SEEK_SET );
  16340.  
  16341.          /* Read data back from file: */
  16342.          fscanf( stream, "%s", s );
  16343.          fscanf( stream, "%ld", &l );
  16344.          fscanf( stream, "%f", &fp );
  16345.          fscanf( stream, "%c", &c );
  16346.  
  16347.          /* Output data read: */
  16348.          printf( "%s\n", s );
  16349.          printf( "%ld\n", l );
  16350.          printf( "%f\n", fp );
  16351.          printf( "%c\n", c );
  16352.  
  16353.          fclose( stream );
  16354.       }
  16355.    }
  16356.  
  16357.  
  16358.  Output
  16359.  
  16360.  
  16361.  
  16362.    a-string
  16363.    65000
  16364.    3.141590
  16365.    x
  16366.  
  16367.  
  16368.  
  16369.  
  16370.  
  16371.  fseek
  16372.  ────────────────────────────────────────────────────────────────────────────
  16373.  
  16374.  
  16375.  Description
  16376.  
  16377.  Moves the file pointer to a specified location.
  16378.  
  16379.    #include <stdio.h>
  16380.  
  16381.    int fseek( FILE *stream, long offset, int origin );
  16382.  
  16383.  stream                            Pointer to FILE structure
  16384.  
  16385.  offset                            Number of bytes from origin
  16386.  
  16387.  origin                            Initial position
  16388.  
  16389.  
  16390.  Remarks
  16391.  
  16392.  The fseek function moves the file pointer (if any) associated with stream to
  16393.  a new location that is offset bytes from origin. The next operation on the
  16394.  stream takes place at the new location. On a stream open for update, the
  16395.  next operation can be either a read or a write.
  16396.  
  16397.  The argument origin must be one of the following constants defined in
  16398.  STDIO.H:
  16399.  
  16400.  Origin                            Definition
  16401.  ────────────────────────────────────────────────────────────────────────────
  16402.  SEEK_CUR                          Current position of file pointer
  16403.  
  16404.  SEEK_END                          End of file
  16405.  
  16406.  SEEK_SET                          Beginning of file
  16407.  
  16408.  The fseek function can be used to reposition the pointer anywhere in a file.
  16409.  The pointer can also be positioned beyond the end of the file. However, an
  16410.  attempt to position the pointer in front of the beginning of the file causes
  16411.  an error.
  16412.  
  16413.  The fseek function clears the end-of-file indicator and negates the effect
  16414.  of any prior ungetc calls against stream.
  16415.  
  16416.  When a file is opened for appending data, the current file position is
  16417.  determined by the last I/O operation, not by where the next write would
  16418.  occur. If no I/O operation has yet occurred on a file opened for appending,
  16419.  the file position is the start of the file.
  16420.  
  16421.  For streams opened in text mode, fseek has limited use because
  16422.  carriage-return-line-feed translations can cause fseek to produce unexpected
  16423.  results. The only fseek operations guaranteed to work on streams opened in
  16424.  text mode are the following:
  16425.  
  16426.  
  16427.    ■   Seeking with an offset of 0 relative to any of the origin values
  16428.  
  16429.    ■   Seeking from the beginning of the file with an offset value returned
  16430.        from a call to ftell
  16431.  
  16432.  
  16433.  
  16434.  Return Value
  16435.  
  16436.  If successful, fseek returns 0. Otherwise, it returns a nonzero value. On
  16437.  devices incapable of seeking, the return value is undefined.
  16438.  
  16439.  
  16440.  Compatibility
  16441.  
  16442.   ANSI   DOS   OS/2   UNIX   XENIX
  16443.  
  16444.  
  16445.  
  16446.  See Also
  16447.  
  16448.  ftell, lseek, rewind
  16449.  
  16450.  
  16451.  Example
  16452.  
  16453.    /* FSEEK.C: This program opens the file FSEEK.OUT and
  16454.     * moves the pointer to the file's beginning.
  16455.     */
  16456.  
  16457.    #include <stdio.h>
  16458.  
  16459.    void main()
  16460.    {
  16461.       FILE *stream;
  16462.       char line[81];
  16463.       int  result;
  16464.  
  16465.       stream = fopen( "fseek.out", "w+" );
  16466.       if( stream == NULL )
  16467.          printf( "The file fseek.out was not opened\n" );
  16468.       else
  16469.       {
  16470.          fprintf( stream, "The fseek begins here: "
  16471.                           "This is the file 'fseek.out'.\n" );
  16472.          result = fseek( stream, 23L, SEEK_SET);
  16473.          if( result )
  16474.             perror( "Fseek failed" );
  16475.          else
  16476.          {
  16477.             printf( "File pointer is set to middle of first line.\n" );
  16478.             fgets( line, 80, stream );
  16479.             printf( "%s", line );
  16480.          }
  16481.          fclose( stream );
  16482.       }
  16483.    }
  16484.  
  16485.  
  16486.  Output
  16487.  
  16488.  
  16489.  
  16490.    File pointer is set to middle of first line.
  16491.    This is the file 'fseek.out'.
  16492.  
  16493.  
  16494.  
  16495.  
  16496.  
  16497.  fsetpos
  16498.  ────────────────────────────────────────────────────────────────────────────
  16499.  
  16500.  
  16501.  Description
  16502.  
  16503.  Sets the stream-position indicator.
  16504.  
  16505.    #include <stdio.h>
  16506.  
  16507.    int fsetpos( FILE *stream, const fpos_t *pos ) ;
  16508.  
  16509.  stream                            Target stream
  16510.  
  16511.  pos                               Position-indicator storage
  16512.  
  16513.  
  16514.  Remarks
  16515.  
  16516.  The fsetpos function sets the file-position indicator for stream to the
  16517.  value of pos, which is obtained in a prior call to fgetpos against stream.
  16518.  
  16519.  The function clears the end-of-file indicator and undoes any effects of the
  16520.  ungetc function on stream. After calling fsetpos, the next operation on
  16521.  stream may be either input or output.
  16522.  
  16523.  
  16524.  Return Value
  16525.  
  16526.  If successful, the fsetpos function returns 0. On failure, the function
  16527.  returns a nonzero value and sets errno to one of the following manifest
  16528.  constants (defined in ERRNO.H):
  16529.  
  16530.  Constant                          Meaning
  16531.  ────────────────────────────────────────────────────────────────────────────
  16532.  EBADF                             The object that stream points to is not
  16533.                                    a valid file handle, or the file is not
  16534.                                    accessible.
  16535.  
  16536.  EINVAL                            An invalid stream value was passed.
  16537.  
  16538.  
  16539.  Compatibility
  16540.  
  16541.   ANSI   DOS   OS/2   UNIX   XENIX
  16542.  
  16543.  
  16544.  
  16545.  See Also
  16546.  
  16547.  fgetpos
  16548.  
  16549.  
  16550.  Example
  16551.  
  16552.    /* FGETPOS.C: This program opens a file and reads bytes at several
  16553.     * different locations.
  16554.     */
  16555.  
  16556.    #include <stdio.h>
  16557.  
  16558.    void main()
  16559.    {
  16560.       FILE   *stream;
  16561.       fpos_t pos;
  16562.       int    val;
  16563.       char   buffer[20];
  16564.  
  16565.       if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )
  16566.          printf( "Trouble opening file\n" );
  16567.       else
  16568.       {
  16569.          /* Read some data and then check the position. */
  16570.          fread( buffer, sizeof( char ), 10, stream );
  16571.          if( fgetpos( stream, &pos ) != 0 )
  16572.             perror( "fgetpos error" );
  16573.          else
  16574.          {
  16575.             fread( buffer, sizeof( char ), 10, stream );
  16576.             printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
  16577.          }
  16578.  
  16579.          /* Set a new position and read more data. */
  16580.          pos = 140;
  16581.          if( fsetpos( stream, &pos ) != 0 )
  16582.             perror( "fsetpos error" );
  16583.  
  16584.          fread( buffer, sizeof( char ), 10, stream );
  16585.             printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
  16586.  
  16587.          fclose( stream );
  16588.       }
  16589.    }
  16590.  
  16591.  
  16592.  Output
  16593.  
  16594.  
  16595.  
  16596.    10 bytes at byte 10: .C: This p
  16597.    10 bytes at byte 140:   FILE   *
  16598.  
  16599.  
  16600.  
  16601.  
  16602.  
  16603.  _fsopen
  16604.  ────────────────────────────────────────────────────────────────────────────
  16605.  
  16606.  
  16607.  Description
  16608.  
  16609.  Opens a stream with file sharing.
  16610.  
  16611.  #include <stdio.h>
  16612.  
  16613.  #include <share.h>                shflag constants
  16614.  
  16615.    FILE *_fsopen( const char *filename, const char *mode, int shflag );
  16616.  
  16617.  filename                          File name to open
  16618.  
  16619.  mode                              Type of access permitted
  16620.  
  16621.  shflag                            Type of sharing allowed
  16622.  
  16623.  
  16624.  Remarks
  16625.  
  16626.  The _fsopen function opens the file specified by filename as a stream and
  16627.  prepares the file for subsequent shared reading or writing, as defined by
  16628.  the mode and shflag arguments.
  16629.  
  16630.  The character string mode specifies the type of access requested for the
  16631.  file, as follows:
  16632.  
  16633.  Type                              Description
  16634.  ────────────────────────────────────────────────────────────────────────────
  16635.  "r"                               Opens for reading. If the file does not
  16636.                                    exist or cannot be found, the _fsopen
  16637.                                    call will fail.
  16638.  
  16639.  "w"                               Opens an empty file for writing. If the
  16640.                                    given file exists, its contents are
  16641.                                    destroyed.
  16642.  
  16643.  "a"                               Opens for writing at the end of the file
  16644.                                    (appending); creates the file first if
  16645.                                    it does not exist.
  16646.  
  16647.  "r+"                              Opens for both reading and writing. (The
  16648.                                    file must exist.)
  16649.  
  16650.  "w+"                              Opens an empty file for both reading and
  16651.                                    writing. If the given file exists, its
  16652.                                    contents are destroyed.
  16653.  
  16654.  "a+"                              Opens for reading and appending; creates
  16655.                                    the file first if it does not exist.
  16656.  
  16657.  Use the "w" and "w+" types with care, as they can destroy existing files.
  16658.  
  16659.  When a file is opened with the "a" or "a+" access type, all write operations
  16660.  occur at the end of the file. Although the file pointer can be repositioned
  16661.  using fseek or rewind, the file pointer is always moved back to the end of
  16662.  the file before any write operation is carried out. Thus, existing data
  16663.  cannot be overwritten.
  16664.  
  16665.  When the "r+", "w+", or "a+"  access type is specified, both reading and
  16666.  writing are allowed (the file is said to be open for "update"). However,
  16667.  when switching between reading and writing, there must be an intervening
  16668.  fsetpos, fseek, or rewind operation. The current position can be specified
  16669.  for the fsetpos or fseek operation, if desired.
  16670.  
  16671.  In addition to the values listed above, one of the following characters can
  16672.  be included in mode to specify the translation mode for newlines:
  16673.  
  16674.  Mode                              Meaning
  16675.  ────────────────────────────────────────────────────────────────────────────
  16676.  t                                 Open in text (translated) mode. In this
  16677.                                    mode, carriage-return-line-feed (CR-LF)
  16678.                                    combinations are translated into single
  16679.                                    line feeds (LF) on input and LF
  16680.                                    characters are translated to CR-LF
  16681.                                    combinations on output. Also, CTRL+Z is
  16682.                                    interpreted as an end-of-file character
  16683.                                    on input. In files opened for reading or
  16684.                                    reading/writing, _fsopen checks for a
  16685.                                    CTRL+Z at the end of the file and
  16686.                                    removes it, if possible. This is done
  16687.                                    because using the fseek and ftell
  16688.                                    functions to move within a file that
  16689.                                    ends with a CTRL+Z may cause fseek to
  16690.                                    behave improperly near the end of the
  16691.                                    file.
  16692.  
  16693.  b                                 Open in binary (untranslated) mode; the
  16694.                                    above translations are suppressed.
  16695.  
  16696.  If t or b is not given in mode, the translation mode is defined by the
  16697.  default-mode variable _fmode. If t or b is prefixed to the argument, the
  16698.  function will fail and will return NULL.
  16699.  
  16700.  See Section 2.7, "Input and Output," for a discussion of text and binary
  16701.  modes.
  16702.  
  16703.  The argument shflag is a constant expression consisting of one of the
  16704.  following manifest constants, defined in SHARE.H.  If SHARE.COM ─or
  16705.  SHARE.EXE for some versions of DOS─ is not installed, DOS ignores the
  16706.  sharing mode. (See your system documentation for detailed information about
  16707.  sharing modes.)
  16708.  
  16709.  Constant                          Meaning
  16710.  ────────────────────────────────────────────────────────────────────────────
  16711.  SH_COMPAT                         Sets compatibility mode (not available
  16712.                                    in OS/2)
  16713.  
  16714.  SH_DENYNO                         Permits read and write access
  16715.  
  16716.  SH_DENYRD                         Denies read access to file
  16717.  
  16718.  SH_DENYRW                         Denies read and write access to file
  16719.  
  16720.  SH_DENYWR                         Denies write access to file
  16721.  
  16722.  The _fsopen function should be used only under OS/2 and DOS versions 3.0 and
  16723.  later. Under earlier versions of DOS, the shflag argument is ignored.
  16724.  
  16725.  
  16726.  Return Value
  16727.  
  16728.  The _fsopen function returns a pointer to the stream. A NULL pointer value
  16729.  indicates an error.
  16730.  
  16731.  
  16732.  Compatibility
  16733.  
  16734.   ANSI   DOS   OS/2   UNIX   XENIX
  16735.  
  16736.  
  16737.  
  16738.  See Also
  16739.  
  16740.  fclose, fcloseall, fdopen, ferror, fileno, fopen, freopen, open, setmode,
  16741.  sopen
  16742.  
  16743.  
  16744.  Example
  16745.  
  16746.    /* FSOPEN.C: This program opens files named "data" and "data2". It uses
  16747.     * fclose to close "data" and fcloseall to close all remaining files.
  16748.     */
  16749.  
  16750.    #include <stdio.h>
  16751.    #include <share.h>
  16752.  
  16753.    FILE *stream;
  16754.  
  16755.    void main()
  16756.    {
  16757.       FILE *stream;
  16758.  
  16759.       /* Open output file for writing. Using _fsopen allows us to ensure
  16760.        * that no one else writes to the file while we are writing to it.
  16761.        */
  16762.       if( (stream = _fsopen( "outfile", "wt", SH_DENYWR )) != NULL )
  16763.       {
  16764.          fprintf( stream, "No one else in the network can write "
  16765.                           "to this file until we are done.\n" );
  16766.          fclose( stream );
  16767.       }
  16768.       /* Now others can write to the file while we read it. */
  16769.       system( "type outfile" );
  16770.    }
  16771.  
  16772.  
  16773.  Output
  16774.  
  16775.  
  16776.  
  16777.    No one else in the network can write to this file until we are done.
  16778.  
  16779.  
  16780.  
  16781.  
  16782.  
  16783.  fstat
  16784.  ────────────────────────────────────────────────────────────────────────────
  16785.  
  16786.  
  16787.  Description
  16788.  
  16789.  Gets information about an open file.
  16790.  
  16791.    #include <sys\types.h>
  16792.  
  16793.    #include <sys\stat.h>
  16794.  
  16795.    int fstat( int handle, struct stat *buffer );
  16796.  
  16797.  handle                            Handle of open file
  16798.  
  16799.  buffer                            Pointer to structure to store results
  16800.  
  16801.  
  16802.  Remarks
  16803.  
  16804.  The fstat function obtains information about the open file associated with
  16805.  handle and stores it in the structure pointed to by buffer. The structure,
  16806.  whose type stat is defined in SYS\STAT.H, contains the following fields:
  16807.  
  16808.  
  16809.  Field                             Value
  16810.  ────────────────────────────────────────────────────────────────────────────
  16811.  st_atime                          Time of last modification of file (same
  16812.                                    as st_mtime and st_ctime).
  16813.  
  16814.  st_ctime                          Time of last modification of file (same
  16815.                                    as st_atime and st_mtime).
  16816.  
  16817.  st_dev                            Either the drive number of the disk
  16818.                                    containing the file, or handle in the
  16819.                                    case of a device (same as st_rdev).
  16820.  
  16821.  st_mode                           Bit mask for file-mode information. The
  16822.                                    S_IFCHR bit is set if
  16823.                                    handle refers to a device. The S_IFREG
  16824.                                    bit is set if handle refers to an
  16825.                                    ordinary file. The read/write bits are
  16826.                                    set according to the file's permission
  16827.                                    mode. (S_IFCHR and other constants are
  16828.                                    defined in SYS\ STAT.H.)
  16829.  
  16830.  st_mtime                          Time of last modification of file (same
  16831.                                    as st_atime and st_ctime).
  16832.  
  16833.  st_nlink                          Always 1.
  16834.  
  16835.  st_rdev                           Either the drive number of the disk
  16836.                                    containing the file, or handle in the
  16837.                                    case of a device (same as st_dev).
  16838.  
  16839.  st_size                           Size of the file in bytes.
  16840.  
  16841.  If handle refers to a device, the size and time fields in the stat structure
  16842.  are not meaningful.
  16843.  
  16844.  
  16845.  Return Value
  16846.  
  16847.  The fstat function returns the value 0 if the file-status information is
  16848.  obtained. A return value of -1 indicates an error; in this case, errno is
  16849.  set to EBADF, indicating an invalid file handle.
  16850.  
  16851.  
  16852.  Compatibility
  16853.  
  16854.   ANSI   DOS   OS/2   UNIX   XENIX
  16855.  
  16856.  
  16857.  In OS/2, the st_dev field does not contain meaningful information. In fact,
  16858.  it is set to zero. OS/2 provides no way to recover the host drive from just
  16859.  the open file handle.
  16860.  
  16861.  
  16862.  See Also
  16863.  
  16864.  access, chmod, filelength, stat
  16865.  
  16866.  
  16867.  Example
  16868.  
  16869.    /* FSTAT.C: This program uses fstat to report the size of a file
  16870.     * named FSTAT.OUT.
  16871.     */
  16872.  
  16873.    #include <io.h>
  16874.    #include <fcntl.h>
  16875.    #include <time.h>
  16876.    #include <sys\types.h>
  16877.    #include <sys\stat.h>
  16878.    #include <stdio.h>
  16879.    #include <stdlib.h>
  16880.    #include <string.h>
  16881.  
  16882.    void main()
  16883.    {
  16884.       struct stat buf;
  16885.       int fh, result;
  16886.       char buffer[] = "A line to output";
  16887.  
  16888.       if( (fh = open( "fstat.out", O_CREAT | O_WRONLY | O_TRUNC )) == -1 )
  16889.          exit( 1 );
  16890.       write( fh, buffer, strlen( buffer ) );
  16891.  
  16892.       /* Get data associated with "fh": */
  16893.  
  16894.       result = fstat( fh, &buf );
  16895.  
  16896.    /* Check if statistics are valid: */
  16897.       if( result != 0 )
  16898.          printf( "Bad file handle\n" );
  16899.       else
  16900.       {
  16901.          printf( "File size     : %ld\n", buf.st_size );
  16902.          printf( "Drive number  : %d\n", buf.st_dev );
  16903.          printf( "Time modified : %s", ctime( &buf.st_atime ) );
  16904.       }
  16905.       close( fh );
  16906.    }
  16907.  
  16908.  
  16909.  Output
  16910.  
  16911.  
  16912.  
  16913.    File size     : 16
  16914.    Drive number  : 0
  16915.    Time modified : Thu Jun 15 21:38:46 1989
  16916.  
  16917.  
  16918.  
  16919.  
  16920.  
  16921.  ftell
  16922.  ────────────────────────────────────────────────────────────────────────────
  16923.  
  16924.  
  16925.  Description
  16926.  
  16927.  Gets the current position of a file pointer.
  16928.  
  16929.    #include <stdio.h>
  16930.  
  16931.    long ftell( FILE *stream );
  16932.  
  16933.  stream                            Target FILE structure
  16934.  
  16935.  
  16936.  Remarks
  16937.  
  16938.  The ftell function gets the current position of the file pointer (if any)
  16939.  associated with stream. The position is expressed as an offset relative to
  16940.  the beginning of the stream.
  16941.  
  16942.  Note that when a file is opened for appending data, the current file
  16943.  position is determined by the last I/O operation, not by where the next
  16944.  write would occur. For example, if a file is opened for an append and the
  16945.  last operation was a read, the file position is the point where the next
  16946.  read operation would start, not where the next write would start. (When a
  16947.  file is opened for appending, the file position is moved to end-of-file
  16948.  before any write operation.) If no I/O operation has yet occurred on a file
  16949.  opened for appending, the file position is the beginning of the file.
  16950.  
  16951.  
  16952.  Return Value
  16953.  
  16954.  The ftell function returns the current file position. The value returned by
  16955.  ftell may not reflect the physical byte offset for streams opened in text
  16956.  mode, since text mode causes carriage-return-line-feed translation. Use
  16957.  ftell in conjunction with the fseek function to return to file locations
  16958.  correctly. On error, the function returns -1L and errno is set to one of the
  16959.  following constants, defined in ERRNO.H:
  16960.  
  16961.  Constant                          Description
  16962.  ────────────────────────────────────────────────────────────────────────────
  16963.  EBADF                             Bad file number. The stream argument is
  16964.                                    not a valid file-handle value or does
  16965.                                    not refer to an open file.
  16966.  
  16967.  EINVAL                            Invalid argument. An invalid stream
  16968.                                    argument was passed to the function.
  16969.  
  16970.  On devices incapable of seeking (such as terminals and printers), or when
  16971.  stream does not refer to an open file, the return value is undefined.
  16972.  
  16973.  
  16974.  Compatibility
  16975.  
  16976.   ANSI   DOS   OS/2   UNIX   XENIX
  16977.  
  16978.  
  16979.  
  16980.  See Also
  16981.  
  16982.  fgetpos, fseek, lseek, tell
  16983.  
  16984.  
  16985.  Example
  16986.  
  16987.    /* FTELL.C: This program opens a file named FTELL.C for reading and
  16988.     * tries to read 100 characters. It then uses ftell to determine the
  16989.     * position of the file pointer and displays this position.
  16990.     */
  16991.  
  16992.    #include <stdio.h>
  16993.  
  16994.    FILE *stream;
  16995.  
  16996.    void main()
  16997.    {
  16998.       long position;
  16999.       char list[100];
  17000.  
  17001.       if( (stream = fopen( "ftell.c", "rb" )) != NULL )
  17002.       {
  17003.          /* Move the pointer by reading data: */
  17004.          fread( list, sizeof( char ), 100, stream );
  17005.  
  17006.          /* Get position after read: */
  17007.          position = ftell( stream );
  17008.          printf( "Position after trying to read 100 bytes: %ld\n", position
  17009.  );
  17010.          fclose( stream );
  17011.       }
  17012.    }
  17013.  
  17014.  
  17015.  Output
  17016.  
  17017.  
  17018.  
  17019.    Position after trying to read 100 bytes: 100
  17020.  
  17021.  
  17022.  
  17023.  
  17024.  
  17025.  ftime
  17026.  ────────────────────────────────────────────────────────────────────────────
  17027.  
  17028.  
  17029.  Description
  17030.  
  17031.  Gets the current time.
  17032.  
  17033.    #include <sys\types.h>
  17034.  
  17035.    #include <sys\timeb.h>
  17036.  
  17037.    void ftime( struct timeb *timeptr );
  17038.  
  17039.  timeptr                           Pointer to structure defined in
  17040.                                    SYS\TIMEB.H
  17041.  
  17042.  
  17043.  Remarks
  17044.  
  17045.  The ftime function gets the current time and stores it in the structure
  17046.  pointed to by timeptr. The timeb structure is defined in SYS\TIMEB.H. It
  17047.  contains four fields (dstflag, millitm, time, and timezone), which have the
  17048.  following values:
  17049.  
  17050.  Field                             Value
  17051.  ────────────────────────────────────────────────────────────────────────────
  17052.  dstflag                           Nonzero if daylight saving time is
  17053.                                    currently in effect for the local time
  17054.                                    zone. (See tzset for an explanation of
  17055.                                    how daylight saving time is determined.)
  17056.  
  17057.  millitm                           Fraction of a second in milliseconds.
  17058.                                    The last digit is always 0 since millitm
  17059.                                    is incremented to the nearest
  17060.                                    one-hundredth of a second.
  17061.  
  17062.  time                              Time in seconds since 00:00:00 Greenwich
  17063.                                    mean time, January 1, 1970.
  17064.  
  17065.  timezone                          Difference in minutes, moving westward,
  17066.                                    between Greenwich mean time and local
  17067.                                    time. The value of timezone is set from
  17068.                                    the value of the global variable
  17069.                                    timezone (see tzset).
  17070.  
  17071.  
  17072.  Return Value
  17073.  
  17074.  The ftime function gives values to the fields in the structure pointed to by
  17075.  timeptr. It does not return a value.
  17076.  
  17077.  
  17078.  Compatibility
  17079.  
  17080.   ANSI   DOS   OS/2   UNIX   XENIX
  17081.  
  17082.  
  17083.  
  17084.  See Also
  17085.  
  17086.  asctime, ctime, gmtime, localtime, time, tzset
  17087.  
  17088.  
  17089.  Example
  17090.  
  17091.    /* FTIME.C: This program uses ftime to obtain the current time
  17092.     * and then stores this time in timebuffer.
  17093.     */
  17094.  
  17095.    #include <stdio.h>
  17096.    #include <sys\timeb.h>
  17097.    #include <time.h>
  17098.  
  17099.    void main()
  17100.    {
  17101.  
  17102.       struct timeb timebuffer;
  17103.       char *timeline;
  17104.  
  17105.       ftime( &timebuffer );
  17106.       timeline = ctime( & ( timebuffer.time ) );
  17107.  
  17108.       printf( "The time is %.19s.%hu %s",
  17109.               timeline, timebuffer.millitm, &timeline[20] );
  17110.    }
  17111.  
  17112.  
  17113.  Output
  17114.  
  17115.  
  17116.  
  17117.    The time is Thu Jun 15 21:40:34.870 1989
  17118.  
  17119.  
  17120.  
  17121.  
  17122.  
  17123.  _fullpath
  17124.  ────────────────────────────────────────────────────────────────────────────
  17125.  
  17126.  
  17127.  Description
  17128.  
  17129.  Makes an absolute path name from a relative path name.
  17130.  
  17131.    #include <stdlib.h>
  17132.  
  17133.    char *_fullpath( char *buffer, const char *pathname, size_t maxlen );
  17134.  
  17135.  buffer                            Full path-name buffer
  17136.  
  17137.  pathname                          Relative path name
  17138.  
  17139.  maxlen                            Length of the buffer pointed to by
  17140.                                    buffer
  17141.  
  17142.  
  17143.  Remarks
  17144.  
  17145.  The _fullpath routine converts the partial path stored in pathname to a
  17146.  fully qualified path that is stored in buffer. Unlike _makepath, the
  17147.  _fullpath routine can be used with .\ and ..\ in the path.
  17148.  
  17149.  If the length of the fully qualified path is greater than the value of
  17150.  maxlen, then NULL is returned; otherwise, the address of buffer is returned.
  17151.  
  17152.  
  17153.  If the buffer is NULL, _fullpath will allocate a buffer of MAX_PATH size and
  17154.  the maxlen argument is ignored.
  17155.  
  17156.  If the pathname argument specifies a disk drive, the current directory of
  17157.  this drive is combined with the path. If the drive is not valid, _fullpath
  17158.  returns NULL.
  17159.  
  17160.  
  17161.  Return Value
  17162.  
  17163.  The _fullpath function returns a pointer to the buffer containing the
  17164.  absolute path (buffer). If there is an error, _fullpath returns NULL.
  17165.  
  17166.  
  17167.  Compatibility
  17168.  
  17169.   ANSI   DOS   OS/2   UNIX   XENIX
  17170.  
  17171.  
  17172.  
  17173.  See Also
  17174.  
  17175.  getcwd,  _getdcwd,  _makepath,  _splitpath
  17176.  
  17177.  
  17178.  Example
  17179.  
  17180.    /* FULLPATH.C: This program demonstrates how _fullpath creates a full
  17181.     * path from a partial path.
  17182.     */
  17183.  
  17184.    #include <stdio.h>
  17185.    #include <conio.h>
  17186.    #include <stdlib.h>
  17187.    #include <direct.h>
  17188.  
  17189.    char full[_MAX_PATH], part[_MAX_PATH];
  17190.  
  17191.    void main()
  17192.    {
  17193.        while( 1 )
  17194.        {
  17195.            printf( "Enter partial path or ENTER to quit: " );
  17196.            gets( part );
  17197.            if( part[0] == 0 )
  17198.                break;
  17199.  
  17200.            if( _fullpath( full, part, _MAX_PATH ) != NULL )
  17201.                printf( "Full path is: %s\n", full );
  17202.            else
  17203.                printf( "Invalid path\n" );
  17204.        }
  17205.    }
  17206.  
  17207.  
  17208.  Output
  17209.  
  17210.  
  17211.  
  17212.    Enter partial path or ENTER to quit: ..
  17213.    Full path is: C:\
  17214.    Enter partial path or ENTER to quit: ..\include
  17215.    Full path is: C:\include
  17216.    Enter partial path or ENTER to quit: p:
  17217.    Full path is: P:\
  17218.    Enter partial path or ENTER to quit: fullpath.c
  17219.    Full path is: C:\LIBREF\fullpath.c
  17220.    Enter partial path or ENTER to quit:
  17221.  
  17222.  
  17223.  
  17224.  
  17225.  
  17226.  fwrite
  17227.  ────────────────────────────────────────────────────────────────────────────
  17228.  
  17229.  
  17230.  Description
  17231.  
  17232.  Writes data to a stream.
  17233.  
  17234.    #include <stdio.h>
  17235.  
  17236.    size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream
  17237.    );
  17238.  
  17239.  buffer                            Pointer to data to be written
  17240.  
  17241.  size                              Item size in bytes
  17242.  
  17243.  count                             Maximum number of items to be written
  17244.  
  17245.  stream                            Pointer to FILE structure
  17246.  
  17247.  
  17248.  Remarks
  17249.  
  17250.  The fwrite function writes up to count items, of length size each, from
  17251.  buffer to the output stream. The file pointer associated with stream (if
  17252.  there is one) is incremented by the number of bytes actually written.
  17253.  
  17254.  If stream is opened in text mode, each carriage return is replaced with a
  17255.  carriage-return-line-feed pair. The replacement has no effect on the return
  17256.  value.
  17257.  
  17258.  
  17259.  Return Value
  17260.  
  17261.  The fwrite function returns the number of full items actually written, which
  17262.  may be less than count if an error occurs. Also, if an error occurs, the
  17263.  file-position indicator cannot be determined.
  17264.  
  17265.  
  17266.  Compatibility
  17267.  
  17268.   ANSI   DOS   OS/2   UNIX   XENIX
  17269.  
  17270.  
  17271.  
  17272.  See Also
  17273.  
  17274.  fread, write
  17275.  
  17276.  
  17277.  Example
  17278.  
  17279.    /* FREAD.C: This program opens a file named FREAD.OUT and writes 25
  17280.     * characters to the file. It then tries to open FREAD.OUT and
  17281.     * read in 25 characters. If the attempt succeeds, the program
  17282.     * displays the number of actual items read.
  17283.     */
  17284.  
  17285.    #include <stdio.h>
  17286.  
  17287.  
  17288.  
  17289.    void main()
  17290.    {
  17291.       FILE *stream;
  17292.       char list[30];
  17293.       int  i, numread, numwritten;
  17294.  
  17295.       /* Open file in text mode: */
  17296.       if( (stream = fopen( "fread.out", "w+t" )) != NULL )
  17297.       {
  17298.          for ( i = 0; i < 25; i++ )
  17299.             list[i] = 'z' - i;
  17300.          /* Write 25 characters to stream */
  17301.          numwritten = fwrite( list, sizeof( char ), 25, stream );
  17302.          printf( "Wrote %d items\n", numwritten );
  17303.          fclose( stream );
  17304.       }
  17305.       else
  17306.          printf( "Problem opening the file\n" );
  17307.  
  17308.       if( (stream = fopen( "fread.out", "r+t" )) != NULL )
  17309.       {
  17310.          /* Attempt to read in 25 characters */
  17311.          numread = fread( list, sizeof( char ), 25, stream );
  17312.          printf( "Number of items read = %d\n", numread );
  17313.          printf( "Contents of buffer = %.25s\n", list );
  17314.          fclose( stream );
  17315.       }
  17316.       else
  17317.          printf( "Was not able to open the file\n" );
  17318.    }
  17319.  
  17320.  
  17321.  Output
  17322.  
  17323.  
  17324.  
  17325.    Wrote 25 items
  17326.    Number of items read = 25
  17327.    Contents of buffer = zyxwvutsrqponmlkjihgfedcb
  17328.  
  17329.  
  17330.  
  17331.  
  17332.  
  17333.  gcvt
  17334.  ────────────────────────────────────────────────────────────────────────────
  17335.  
  17336.  
  17337.  Description
  17338.  
  17339.  Converts a floating-point value to a string, which it stores in a buffer.
  17340.  
  17341.  #include <stdlib.h>               Required only for function declarations
  17342.  
  17343.    char *gcvt( double value, int digits, char *buffer );
  17344.  
  17345.  value                             Value to be converted
  17346.  
  17347.  digits                            Number of significant digits stored
  17348.  
  17349.  buffer                            Storage location for result
  17350.  
  17351.  
  17352.  Remarks
  17353.  
  17354.  The gcvt function converts a floating-point value to a character string and
  17355.  stores the string in buffer. The buffer should be large enough to
  17356.  accommodate the converted value plus a terminating null character ('\0'),
  17357.  which is appended automatically. There is no provision for overflow.
  17358.  
  17359.  The gcvt function attempts to produce digits significant digits in decimal
  17360.  format. If this is not possible, it produces digits significant digits in
  17361.  exponential format. Trailing zeros may be suppressed in the conversion.
  17362.  
  17363.  
  17364.  Return Value
  17365.  
  17366.  The gcvt function returns a pointer to the string of digits. There is no
  17367.  error return.
  17368.  
  17369.  
  17370.  Compatibility
  17371.  
  17372.   ANSI   DOS   OS/2   UNIX   XENIX
  17373.  
  17374.  
  17375.  
  17376.  See Also
  17377.  
  17378.  atof, atoi, atol, ecvt, fcvt
  17379.  
  17380.  
  17381.  Example
  17382.  
  17383.    /* GCVT.C: This program converts -3.1415e5 to its string representation.
  17384.    */
  17385.  
  17386.    #include <stdlib.h>
  17387.    #include <stdio.h>
  17388.  
  17389.  
  17390.    void main()
  17391.    {
  17392.       char buffer[50];
  17393.       double source = -3.1415e5;
  17394.  
  17395.       gcvt( source, 7, buffer );
  17396.       printf( "source: %f  buffer: '%s'\n", source, buffer );
  17397.    }
  17398.  
  17399.  
  17400.  Output
  17401.  
  17402.  
  17403.  
  17404.    source: -314150.000000  buffer: '-314150.'
  17405.  
  17406.  
  17407.  
  17408.  
  17409.  
  17410.  _getactivepage
  17411.  ────────────────────────────────────────────────────────────────────────────
  17412.  
  17413.  
  17414.  Description
  17415.  
  17416.  Gets the current active page number.
  17417.  
  17418.    #include <graph.h>
  17419.  
  17420.    short _far _getactivepage( void );
  17421.  
  17422.  
  17423.  Remarks
  17424.  
  17425.  The _getactivepage function returns the number of the current active page.
  17426.  
  17427.  
  17428.  Return Value
  17429.  
  17430.  The function returns the number of the current active page. All hardware
  17431.  combinations support at least one page (page number 0). In OS/2, only page 0
  17432.  is valid.
  17433.  
  17434.  
  17435.  Compatibility
  17436.  
  17437.   ANSI   DOS   OS/2   UNIX   XENIX
  17438.  
  17439.  
  17440.  
  17441.  See Also
  17442.  
  17443.  _getactivepage,  _getvideoconfig,  _getvisualpage,  _grstatus,
  17444.  _setactivepage, _setvideomode,  _setvisualpage
  17445.  
  17446.  
  17447.  Example
  17448.  
  17449.    /* PAGE.C illustrates video page functions including:
  17450.     *      _getactivepage  _getvisualpage  _setactivepage  _setvisualpage
  17451.     */
  17452.  
  17453.    #include <conio.h>
  17454.    #include <graph.h>
  17455.    #include <stdlib.h>
  17456.  
  17457.    void main()
  17458.    {
  17459.       short  oldvpage, oldapage, page, row, col, line;
  17460.       struct videoconfig vc;
  17461.       char   buf[80];
  17462.  
  17463.       _getvideoconfig( &vc );
  17464.       if( vc.numvideopages < 4 )
  17465.           exit( 1 );              /* Fail for OS/2 or monochrome. */
  17466.       oldapage  = _getactivepage();
  17467.       oldvpage  = _getvisualpage();
  17468.       _displaycursor( _GCURSOROFF );
  17469.  
  17470.  
  17471.    /* Draw arrows in different place on each page. */
  17472.       for( page = 1; page < 4; page++ )
  17473.       {
  17474.          _setactivepage( page );
  17475.          _settextposition( 12, 16 * page );
  17476.          _outtext( ">>>>>>>>" );
  17477.       }
  17478.  
  17479.       while( !kbhit() )
  17480.          /* Cycle through pages 1 to 3 to show moving image. */
  17481.          for( page = 1; page < 4; page++ )
  17482.              _setvisualpage( page );
  17483.       getch();
  17484.  
  17485.       /* Restore original page (normally 0) to restore screen. */
  17486.       _setactivepage( oldapage );
  17487.       _setvisualpage( oldvpage );
  17488.       _displaycursor( _GCURSORON );
  17489.    }
  17490.  
  17491.  
  17492.  
  17493.  
  17494.  
  17495.  _getarcinfo
  17496.  ────────────────────────────────────────────────────────────────────────────
  17497.  
  17498.  
  17499.  Description
  17500.  
  17501.  Determines the endpoints in viewport coordinates of the most recently drawn
  17502.  arc or pie.
  17503.  
  17504.    #include  <graph.h>
  17505.  
  17506.    short _far _getarcinfo( struct xycoord _far *start, struct xycoord _far
  17507.    *end,
  17508.    struct xycoord _far *fillpoint );
  17509.  
  17510.  start                             Starting point of arc
  17511.  
  17512.  end                               Ending point of arc
  17513.  
  17514.  fillpoint                         Point at which pie fill will begin
  17515.  
  17516.  
  17517.  Remarks
  17518.  
  17519.  The _getarcinfo function determines the endpoints in viewport coordinates of
  17520.  the most recently drawn arc or pie.
  17521.  
  17522.  If successful, the _getarcinfo function updates the start and end xycoord
  17523.  structures to contain the endpoints (in viewport coordinates) of the arc
  17524.  drawn by the most recent call to one of the _arc or _pie functions.
  17525.  
  17526.  In addition, fillpoint specifies a point from which a pie can be filled.
  17527.  This is useful for filling a pie in a color different from the border color.
  17528.  After a call to _getarcinfo, change colors using the _setcolor function. Use
  17529.  the color, along with the coordinates in fillpoint, as arguments for the
  17530.  floodfill function.
  17531.  
  17532.  
  17533.  Return Value
  17534.  
  17535.  The _getarcinfo function returns a nonzero value if successful. If neither
  17536.  the _arc nor the _pie function has been successfully called since the last
  17537.  time the screen was cleared or a new graphics mode or viewport was selected,
  17538.  the _getarcinfo function returns 0.
  17539.  
  17540.  
  17541.  Compatibility
  17542.  
  17543.   ANSI   DOS   OS/2   UNIX   XENIX
  17544.  
  17545.  
  17546.  
  17547.  See Also
  17548.  
  17549.  _arc functions,  _floodfill,  _getvideoconfig,  _grstatus,  _pie functions
  17550.  
  17551.  
  17552.  Example
  17553.  
  17554.  See the example for _arc.
  17555.  
  17556.  
  17557.  
  17558.  
  17559.  
  17560.  _getbkcolor
  17561.  ────────────────────────────────────────────────────────────────────────────
  17562.  
  17563.  
  17564.  Description
  17565.  
  17566.  Gets the current background color.
  17567.  
  17568.    #include <graph.h>
  17569.  
  17570.    long _far _getbkcolor( void );
  17571.  
  17572.  
  17573.  Remarks
  17574.  
  17575.  The _getbkcolor function returns the current background color. The default
  17576.  is 0.
  17577.  
  17578.  In a color text mode such as _TEXTC80, _setbkcolor accepts, and _getbkcolor
  17579.  returns, a color index. For example, _setbkcolor(2L) sets the background
  17580.  color to color index 2. The actual color displayed depends on the palette
  17581.  mapping for color index 2. The default for color index 2 is green in a color
  17582.  text mode.
  17583.  
  17584.  In a color graphics mode such as _ERESCOLOR, _setbkcolor accepts and
  17585.  _getbkcolor returns a color value (as used in _remappalette). The value for
  17586.  the simplest background colors is given by the manifest constants defined in
  17587.  the GRAPH.H include file. For example, _setbkcolor( _GREEN) sets the
  17588.  background color in a graphics mode to green. These manifest constants are
  17589.  provided as a convenience in defining and manipulating the most common
  17590.  colors. In general, the actual range of colors is much greater.
  17591.  
  17592.  In most cases, whenever an argument is long, it refers to a color value, and
  17593.  whenever it is short, it refers to a color index. The two exceptions are
  17594.  _setbkcolor and _getbkcolor, described above. For a more complete discussion
  17595.  of colors, see _remappalette.
  17596.  
  17597.  
  17598.  Return Value
  17599.  
  17600.  The function returns the current background color value. There is no error
  17601.  return.
  17602.  
  17603.  
  17604.  Compatibility
  17605.  
  17606.   ANSI   DOS   OS/2   UNIX   XENIX
  17607.  
  17608.  
  17609.  
  17610.  See Also
  17611.  
  17612.  _remappalette,  _setbkcolor
  17613.  
  17614.  
  17615.  Example
  17616.  
  17617.  See the example for _getcolor.
  17618.  
  17619.  
  17620.  
  17621.  
  17622.  
  17623.  getc, getchar
  17624.  ────────────────────────────────────────────────────────────────────────────
  17625.  
  17626.  
  17627.  Description
  17628.  
  17629.  Reads a character from a stream (getc), or gets a character from stdin
  17630.  (getchar).
  17631.  
  17632.    #include <stdio.h>
  17633.  
  17634.    int getc( FILE *stream );
  17635.  
  17636.    int getchar( void );
  17637.  
  17638.  stream                            Current stream
  17639.  
  17640.  
  17641.  Remarks
  17642.  
  17643.  The getc macro reads a single character from the stream position and
  17644.  increments the associated file pointer (if there is one) to point to the
  17645.  next character. The getchar macro is identical to getc(stdin).
  17646.  
  17647.  The getc and getchar routines are similar to fgetc and fgetchar,
  17648.  respectively, but are macros rather than functions.
  17649.  
  17650.  
  17651.  Return Value
  17652.  
  17653.  The getc and getchar macros return the character read. A return value of EOF
  17654.  indicates an error or end-of-file condition. Use ferror or feof to determine
  17655.  whether an error or end-of-file occurred.
  17656.  
  17657.  
  17658.  Compatibility
  17659.  
  17660.  getc
  17661.  
  17662.   ANSI   DOS   OS/2   UNIX   XENIX
  17663.  
  17664.  
  17665.  getchar
  17666.  
  17667.   ANSI   DOS   OS/2   UNIX   XENIX
  17668.  
  17669.  
  17670.  
  17671.  See Also
  17672.  
  17673.  fgetc, fgetchar, getch, getche, putc, putchar, ungetc
  17674.  
  17675.  
  17676.  Example
  17677.  
  17678.    /* GETC.C: This program uses getchar to read a single line of input
  17679.     * from stdin, places this input in buffer, then terminates the
  17680.     * string before printing it to the screen.
  17681.     */
  17682.  
  17683.    #include <stdio.h>
  17684.  
  17685.  
  17686.    void main()
  17687.    {
  17688.       char buffer[81];
  17689.       int i, ch;
  17690.  
  17691.       printf( "Enter a line: " );
  17692.  
  17693.       /* Read in single line from "stdin": */
  17694.       for( i = 0; (i < 80) &&  ((ch = getchar()) != EOF) && (ch != '\n'); i++
  17695.  )
  17696.          buffer[i] = ch;
  17697.  
  17698.       /* Terminate string with null character: */
  17699.       buffer[i] = '\0';
  17700.       printf( "%s\n", buffer );
  17701.    }
  17702.  
  17703.  
  17704.  Output
  17705.  
  17706.  
  17707.  
  17708.    Enter a line: This is a line of text.
  17709.    This is a line of text.
  17710.  
  17711.  
  17712.  
  17713.  
  17714.  
  17715.  getch, getche
  17716.  ────────────────────────────────────────────────────────────────────────────
  17717.  
  17718.  
  17719.  Description
  17720.  
  17721.  Get a character from the console without echo (getch) or with echo (getche).
  17722.  
  17723.  
  17724.  
  17725.  
  17726.  #include <conio.h>                Required only for function declarations
  17727.  
  17728.    int getch( void );
  17729.  
  17730.    int getche( void );
  17731.  
  17732.  
  17733.  Remarks
  17734.  
  17735.  The getch function reads a single character from the console without
  17736.  echoing. The getche function reads a single character from the console and
  17737.  echoes the character read. Neither function can be used to read CTRL+C.
  17738.  
  17739.  When reading a function key or cursor-moving key, the getch and getche
  17740.  functions must be called twice; the first call returns 0 or 0xE0, and the
  17741.  second call returns the actual key code.
  17742.  
  17743.  
  17744.  Return Value
  17745.  
  17746.  The getch function returns the character read. There is no error return.
  17747.  
  17748.  
  17749.  Compatibility
  17750.  
  17751.   ANSI   DOS   OS/2   UNIX   XENIX
  17752.  
  17753.  
  17754.  
  17755.  See Also
  17756.  
  17757.  cgets, getchar, ungetch
  17758.  
  17759.  
  17760.  Example
  17761.  
  17762.    /* GETCH.C: This program reads characters from the keyboard until it
  17763.     * receives a 'Y' or 'y'.
  17764.     */
  17765.  
  17766.    #include <conio.h>
  17767.    #include <ctype.h>
  17768.  
  17769.  
  17770.    void main()
  17771.    {
  17772.       int ch;
  17773.  
  17774.       cputs( "Type 'Y' when finished typing keys: " );
  17775.       do
  17776.       {
  17777.          ch = getch();
  17778.          ch = toupper( ch );
  17779.       } while( ch != 'Y' );
  17780.  
  17781.       putch( ch );
  17782.       putch( '\r' );    /* Carriage return */
  17783.       putch( '\n' );    /* Line feed       */
  17784.    }
  17785.  
  17786.  
  17787.  Output
  17788.  
  17789.  
  17790.  
  17791.    Type 'Y' when finished typing keys: Y
  17792.  
  17793.  
  17794.  
  17795.  
  17796.  
  17797.  _getcolor
  17798.  ────────────────────────────────────────────────────────────────────────────
  17799.  
  17800.  
  17801.  Description
  17802.  
  17803.  Gets the current color.
  17804.  
  17805.    #include <graph.h>
  17806.  
  17807.    short _far _getcolor( void );
  17808.  
  17809.  
  17810.  Remarks
  17811.  
  17812.  The _getcolor function returns the current graphics color index. The default
  17813.  is the highest legal value of the current palette.
  17814.  
  17815.  
  17816.  Return Value
  17817.  
  17818.  The _getcolor function returns the current color index.
  17819.  
  17820.  
  17821.  Compatibility
  17822.  
  17823.   ANSI   DOS   OS/2   UNIX   XENIX
  17824.  
  17825.  
  17826.  
  17827.  See Also
  17828.  
  17829.  _setcolor
  17830.  
  17831.  
  17832.  Example
  17833.  
  17834.    /* OUTTXT.C: This example illustrates text output functions:
  17835.     *    _gettextcolor   _getbkcolor   _gettextposition   _outtext
  17836.     *    _settextcolor   _setbkcolor   _settextposition
  17837.     */
  17838.  
  17839.    #include <conio.h>
  17840.    #include <stdio.h>
  17841.    #include <graph.h>
  17842.  
  17843.    char buffer [80];
  17844.  
  17845.    void main()
  17846.    {
  17847.  
  17848.       /* Save original foreground, background, and text position. */
  17849.       short blink, fgd, oldfgd;
  17850.       long  bgd, oldbgd;
  17851.       struct rccoord oldpos;
  17852.  
  17853.       /* Save original foreground, background, and text position. */
  17854.       oldfgd = _gettextcolor();
  17855.       oldbgd = _getbkcolor();
  17856.       oldpos = _gettextposition();
  17857.       _clearscreen( _GCLEARSCREEN );
  17858.  
  17859.  
  17860.    /* First time no blink, second time blinking. */
  17861.       for( blink = 0; blink <= 16; blink += 16 )
  17862.       {
  17863.          /* Loop through 8 background colors. */
  17864.          for( bgd = 0; bgd < 8; bgd++ )
  17865.          {
  17866.             _setbkcolor( bgd );
  17867.             _settextposition( (short)bgd + ((blink / 16) * 9) + 3, 1 );
  17868.             _settextcolor( 7 );
  17869.             sprintf(buffer, "Back: %d Fore:", bgd );
  17870.             _outtext( buffer );
  17871.  
  17872.             /* Loop through 16 foreground colors. */
  17873.             for( fgd = 0; fgd < 16; fgd++ )
  17874.             {
  17875.                _settextcolor( fgd + blink );
  17876.                sprintf( buffer, " %2d ", fgd + blink );
  17877.                _outtext( buffer );
  17878.             }
  17879.          }
  17880.       }
  17881.       getch();
  17882.  
  17883.       /* Restore original foreground, background, and text position. */
  17884.       _settextcolor( oldfgd );
  17885.       _setbkcolor( oldbgd );
  17886.       _clearscreen( _GCLEARSCREEN );
  17887.       _settextposition( oldpos.row, oldpos.col );
  17888.    }
  17889.  
  17890.  
  17891.  
  17892.  
  17893.  
  17894.  _getcurrentposition Functions
  17895.  ────────────────────────────────────────────────────────────────────────────
  17896.  
  17897.  
  17898.  Description
  17899.  
  17900.  Get the current position and return it as a structure.
  17901.  
  17902.    #include <graph.h>
  17903.  
  17904.    struct xycoord _far _getcurrentposition( void );
  17905.  
  17906.    struct _wxycoord _far _getcurrentposition_w( void );
  17907.  
  17908.  
  17909.  Remarks
  17910.  
  17911.  The _getcurrentposition functions return the coordinates of the current
  17912.  graphics output position. The _getcurrentposition function returns the
  17913.  position as an xycoord structure, defined in GRAPH.H.
  17914.  
  17915.  The xycoord structure contains the following elements:
  17916.  
  17917.  Element                           Description
  17918.  ────────────────────────────────────────────────────────────────────────────
  17919.  short xcoord                      x coordinate
  17920.  
  17921.  short ycoord                      y coordinate
  17922.  
  17923.  The _getcurrentposition_w function returns the position as an _wxycoord
  17924.  structure, defined in GRAPH.H.
  17925.  
  17926.  The _wxycoord structure contains the following elements:
  17927.  
  17928.  Element                           Description
  17929.  ────────────────────────────────────────────────────────────────────────────
  17930.  double wx                         window x coordinate
  17931.  
  17932.  double wy                         window y coordinate
  17933.  
  17934.  The current position can be changed by the _lineto, _moveto, and _outgtext
  17935.  functions.
  17936.  
  17937.  The default position, set by _setvideomode, _setvideomoderows, or
  17938.  _setviewport, is the center of the viewport.
  17939.  
  17940.  Only graphics output starts at the current position; these functions do not
  17941.  affect text output, which begins at the current text position. (See
  17942.  _settextposition for more information.)
  17943.  
  17944.  
  17945.  Return Value
  17946.  
  17947.  The _getcurrentposition function returns the coordinates of the current
  17948.  graphics output position. There is no error return.
  17949.  
  17950.  
  17951.  Compatibility
  17952.  
  17953.   ANSI   DOS   OS/2   UNIX   XENIX
  17954.  
  17955.  
  17956.  
  17957.  See Also
  17958.  
  17959.  _grstatus,  _lineto functions,  _moveto functions,  _outgtext
  17960.  
  17961.  
  17962.  Example
  17963.  
  17964.    /* GCURPOS.C: This program sets a random current location, then gets that
  17965.     * location with _getcurrentposition.
  17966.     */
  17967.  
  17968.    #include <stdio.h>
  17969.    #include <stdlib.h>
  17970.    #include <conio.h>
  17971.    #include <graph.h>
  17972.  
  17973.    char   buffer[255];
  17974.  
  17975.    void main()
  17976.    {
  17977.       struct videoconfig vc;
  17978.       struct xycoord position;
  17979.  
  17980.       /* Find a valid graphics mode. */
  17981.       if( !_setvideomode( _MAXRESMODE ) )
  17982.          exit( 1 );
  17983.       _getvideoconfig( &vc );
  17984.  
  17985.       /* Move to random location and report that location. */
  17986.       _moveto( rand() % vc.numxpixels, rand() % vc.numypixels );
  17987.       position = _getcurrentposition();
  17988.       sprintf( buffer, "x = %d, y = %d", position.xcoord, position.ycoord );
  17989.       _settextposition( 1, 1 );
  17990.       _outtext( buffer );
  17991.  
  17992.       getch();
  17993.       _setvideomode( _DEFAULTMODE );
  17994.    }
  17995.  
  17996.  
  17997.  
  17998.  
  17999.  
  18000.  getcwd
  18001.  ────────────────────────────────────────────────────────────────────────────
  18002.  
  18003.  
  18004.  Description
  18005.  
  18006.  Gets the current working directory.
  18007.  
  18008.  #include <direct.h>               Required only for function declarations
  18009.  
  18010.    char *getcwd( char *buffer, int maxlen );
  18011.  
  18012.  buffer                            Storage location for path name
  18013.  
  18014.  maxlen                            Maximum length of path name
  18015.  
  18016.  
  18017.  Remarks
  18018.  
  18019.  The getcwd function gets the full path name of the current working directory
  18020.  and stores it at buffer. The integer argument maxlen specifies the maximum
  18021.  length for the path name. An error occurs if the length of the path name
  18022.  (including the terminating null character) exceeds maxlen.
  18023.  
  18024.  The buffer argument can be NULL; a buffer of at least size maxlen (more only
  18025.  if necessary) will automatically be allocated, using malloc, to store the
  18026.  path name. This buffer can later be freed by calling free and passing it the
  18027.  getcwd return value (a pointer to the allocated buffer).
  18028.  
  18029.  
  18030.  Return Value
  18031.  
  18032.  The getcwd function returns a pointer to buffer. A NULL return value
  18033.  indicates an error, and errno is set to one of the following values:
  18034.  
  18035.  Value                             Meaning
  18036.  ────────────────────────────────────────────────────────────────────────────
  18037.  ENOMEM                            Insufficient memory to allocate maxlen
  18038.                                    bytes (when a NULL argument is given as
  18039.                                    buffer)
  18040.  
  18041.  ERANGE                            Path name longer than maxlen characters
  18042.  
  18043.  
  18044.  Compatibility
  18045.  
  18046.   ANSI   DOS   OS/2   UNIX   XENIX
  18047.  
  18048.  
  18049.  
  18050.  See Also
  18051.  
  18052.  chdir, mkdir, rmdir
  18053.  
  18054.  
  18055.  Example
  18056.  
  18057.    /* This program places the name of the current directory in the buffer
  18058.     * array, then displays the name of the current directory on the screen.
  18059.     * Specifying a length of _MAX_DIR leaves room for the longest legal
  18060.     * directory name.
  18061.     */
  18062.  
  18063.    #include <direct.h>
  18064.    #include <stdlib.h>
  18065.    #include <stdio.h>
  18066.  
  18067.    void main()
  18068.    {
  18069.       char buffer[_MAX_DIR];
  18070.  
  18071.       /* Get the current working directory: */
  18072.       if( getcwd( buffer, _MAX_DIR ) == NULL )
  18073.          perror( "getcwd error" );
  18074.       else
  18075.          printf( "%s\n", buffer );
  18076.    }
  18077.  
  18078.  
  18079.  Output
  18080.  
  18081.  
  18082.  
  18083.    C:\LIBREF
  18084.  
  18085.  
  18086.  
  18087.  
  18088.  
  18089.  _getdcwd
  18090.  ────────────────────────────────────────────────────────────────────────────
  18091.  
  18092.  
  18093.  Description
  18094.  
  18095.  Gets full path name of current working directory, including disk drive.
  18096.  
  18097.  #include <direct.h>               Required only for function declarations
  18098.  
  18099.    char *_getdcwd( int drive, char *buffer, int maxlen );
  18100.  
  18101.  drive                             Disk drive
  18102.  
  18103.  buffer                            Storage location for path name
  18104.  
  18105.  maxlen                            Maximum length of path name
  18106.  
  18107.  
  18108.  Remarks
  18109.  
  18110.  The _getdcwd function gets the full path name of the current working
  18111.  directory, including disk drive specification, and stores it at buffer. The
  18112.  argument maxlen specifies the maximum length for the path name. An error
  18113.  occurs if the length of the path name (including the terminating null
  18114.  character) exceeds maxlen.
  18115.  
  18116.  The drive argument specifies the drive (0 = default drive, 1=A, 2=B, etc.).
  18117.  The buffer argument can be NULL; a buffer of at least size maxlen (more only
  18118.  if necessary) will automatically be allocated, using malloc, to store the
  18119.  path name. This buffer can later be freed by calling free and passing it the
  18120.  _getdcwd return value (a pointer to the allocated buffer).
  18121.  
  18122.  
  18123.  Return Value
  18124.  
  18125.  The _getdcwd function returns buffer. A NULL return value indicates an
  18126.  error, and errno is set to one of the following values:
  18127.  
  18128.  Value                             Meaning
  18129.  ────────────────────────────────────────────────────────────────────────────
  18130.  ENOMEM                            Insufficient memory to allocate maxlen
  18131.                                    bytes (when a NULL argument is given as
  18132.                                    buffer)
  18133.  
  18134.  ERANGE                            Path name longer than maxlen characters
  18135.  
  18136.  
  18137.  Compatibility
  18138.  
  18139.   ANSI   DOS   OS/2   UNIX   XENIX
  18140.  
  18141.  
  18142.  
  18143.  See Also
  18144.  
  18145.  chdir, getcwd, _getdrive, mkdir, rmdir
  18146.  
  18147.  
  18148.  Example
  18149.  
  18150.    /* GETDRIVE.C illustrates drive functions including:
  18151.     *      _getdrive       _chdrive        _getdcwd
  18152.     */
  18153.  
  18154.    #include <stdio.h>
  18155.    #include <conio.h>
  18156.    #include <direct.h>
  18157.    #include <stdlib.h>
  18158.  
  18159.    void main()
  18160.    {
  18161.       int ch, drive, curdrive;
  18162.       static char path[_MAX_PATH];
  18163.  
  18164.       /* Save current drive. */
  18165.       curdrive = _getdrive();
  18166.  
  18167.       printf( "Available drives are: \n" );
  18168.  
  18169.       /* If we can switch to the drive, it exists. */
  18170.       for( drive = 1; drive <= 26; drive++ )
  18171.          if( !_chdrive( drive ) )
  18172.             printf( "%c: ", drive + 'A' - 1 );
  18173.  
  18174.       while( 1 )
  18175.       {
  18176.          printf( "\nType drive letter to check or ESC to quit: " );
  18177.          ch = getch();
  18178.          if( ch == 27 )
  18179.             break;
  18180.          if( isalpha( ch ) )
  18181.             putch( ch );
  18182.          if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
  18183.             printf( "\nCurrent directory on that drive is %s\n", path );
  18184.       }
  18185.  
  18186.       /* Restore original drive. This is only necessary for DOS. Under OS/2
  18187.        * the current drive of the calling process is always restored.
  18188.        */
  18189.       _chdrive( curdrive );
  18190.       printf( "\n" );
  18191.    }
  18192.  
  18193.  
  18194.  Output
  18195.  
  18196.  
  18197.  
  18198.    Available drives are:
  18199.    A: B: C:
  18200.    Type drive letter to check or ESC to quit: q
  18201.    Type drive letter to check or ESC to quit: a
  18202.    Current directory on that drive is A:\
  18203.  
  18204.    Type drive letter to check or ESC to quit: c
  18205.    Current directory on that drive is C:\LIBREF
  18206.  
  18207.    Type drive letter to check or ESC to quit:
  18208.  
  18209.  
  18210.  
  18211.  
  18212.  
  18213.  _getdrive
  18214.  ────────────────────────────────────────────────────────────────────────────
  18215.  
  18216.  
  18217.  Description
  18218.  
  18219.  Gets the current disk drive.
  18220.  
  18221.    #include <direct.h>
  18222.  
  18223.    int _getdrive( void );
  18224.  
  18225.  
  18226.  Remarks
  18227.  
  18228.  The _getdrive function returns the current working drive (1=A, 2=B, etc.).
  18229.  
  18230.  
  18231.  Return Value
  18232.  
  18233.  The return value is stated above. There is no error return.
  18234.  
  18235.  
  18236.  Compatibility
  18237.  
  18238.   ANSI   DOS   OS/2   UNIX   XENIX
  18239.  
  18240.  
  18241.  
  18242.  See Also
  18243.  
  18244.  _chdrive,  _dos_getdrive,  _dos_setdrive,  _getcwd,  _getdcwd
  18245.  
  18246.  
  18247.  Example
  18248.  
  18249.  See the example for _getdcwd.
  18250.  
  18251.  
  18252.  
  18253.  
  18254.  
  18255.  getenv
  18256.  ────────────────────────────────────────────────────────────────────────────
  18257.  
  18258.  
  18259.  Description
  18260.  
  18261.  Gets a value from the environment table.
  18262.  
  18263.  #include <stdlib.h>               Required only for function declarations
  18264.  
  18265.    char *getenv( const char *varname );
  18266.  
  18267.  varname                           Name of environment variable
  18268.  
  18269.  
  18270.  Remarks
  18271.  
  18272.  The getenv function searches the list of environment variables for an entry
  18273.  corresponding to varname. Environment variables define the environment in
  18274.  which a process executes. (For example, the LIB environment variable defines
  18275.  the default search path for libraries to be linked with a program.) Because
  18276.  the getenv function is case sensitive, the varname variable should match the
  18277.  case of the environment variable.
  18278.  
  18279.  The getenv function returns a pointer to an entry in the environment table.
  18280.  It is, however, only safe to retrieve the value of the environment variable
  18281.  using the returned pointer. To modify the value of an environmental
  18282.  variable, use the putenv function.
  18283.  
  18284.  The getenv and putenv functions use the copy of the environment contained in
  18285.  the global variable environ to access the environment. Programs that use the
  18286.  envp argument to main and the putenv function may retrieve invalid
  18287.  information. The safest programming practice is to use getenv and putenv.
  18288.  
  18289.  
  18290.  Return Value
  18291.  
  18292.  The getenv function returns a pointer to the environment table entry
  18293.  containing the current string value of varname. The return value is NULL if
  18294.  the given variable is not currently defined.
  18295.  
  18296.  
  18297.  Compatibility
  18298.  
  18299.   ANSI   DOS   OS/2   UNIX   XENIX
  18300.  
  18301.  
  18302.  The getenv function operates only on the data structures accessible to the
  18303.  run-time library and not on the environment "segment" created for a process
  18304.  by DOS or OS/2.
  18305.  
  18306.  
  18307.  See Also
  18308.  
  18309.  putenv
  18310.  
  18311.  
  18312.  Example
  18313.  
  18314.    /* GETENV.C: This program uses getenv to retrieve the LIB environment
  18315.     * variable and then uses putenv to change it to a new value.
  18316.     */
  18317.  
  18318.    #include <stdlib.h>
  18319.    #include <stdio.h>
  18320.  
  18321.    main()
  18322.    {
  18323.       char *libvar;
  18324.  
  18325.       /* Get the value of the LIB environment variable. */
  18326.       libvar = getenv( "LIB" );
  18327.       if( libvar != NULL )
  18328.          printf( "Original LIB variable is: %s\n", libvar );
  18329.  
  18330.       /* Attempt to change path. Note that this only affects the environment
  18331.        * variable of the current process. The command processor's environment
  18332.        * is not changed.
  18333.        */
  18334.       putenv( "LIB=c:\\mylib;c:\\yourlib" );
  18335.  
  18336.       /* Get new value. */
  18337.       libvar = getenv( "LIB" );
  18338.       if( libvar != NULL )
  18339.          printf( "New LIB variable is: %s\n", libvar );
  18340.  
  18341.    }
  18342.  
  18343.  
  18344.  Output
  18345.  
  18346.  
  18347.  
  18348.    Original LIB variable is: C:\LIB
  18349.    New LIB variable is: c:\mylib;c:\yourlib
  18350.  
  18351.  
  18352.  
  18353.  
  18354.  
  18355.  _getfillmask
  18356.  ────────────────────────────────────────────────────────────────────────────
  18357.  
  18358.  
  18359.  Description
  18360.  
  18361.  Gets the current fill mask for some graphics routines.
  18362.  
  18363.    #include <graph.h>
  18364.  
  18365.    unsigned char _far * _far _getfillmask( unsigned char _far *mask );
  18366.  
  18367.  mask                              Mask array
  18368.  
  18369.  
  18370.  Remarks
  18371.  
  18372.  Some graphics routines (_ellipse, _floodfill, _pie, _polygon, and
  18373.  _rectangle) can fill part or all of the screen with the current color or
  18374.  background color. The fill mask controls the pattern used for filling.
  18375.  
  18376.  The _getfillmask function returns the current fill mask. The mask is an
  18377.  8-by-8-bit array, in which each bit represents a pixel. If the bit is 1, the
  18378.  corresponding pixel is set to the current color; if the bit is 0, the pixel
  18379.  is left unchanged. The mask is repeated over the entire fill area. If no
  18380.  fill mask is set, or if mask is NULL, a solid (unpatterned) fill is
  18381.  performed using the current color.
  18382.  
  18383.  
  18384.  Return Value
  18385.  
  18386.  If no mask is set, the function returns NULL.
  18387.  
  18388.  
  18389.  Compatibility
  18390.  
  18391.   ANSI   DOS   OS/2   UNIX   XENIX
  18392.  
  18393.  
  18394.  
  18395.  See Also
  18396.  
  18397.  _ellipse functions,  _floodfill,  _pie functions,  _polygon functions,
  18398.  _rectangle functions, _setfillmask
  18399.  
  18400.  
  18401.  Example
  18402.  
  18403.    /* GFILLMSK.C: This program illustrates _getfillmask and _setfillmask. */
  18404.  
  18405.    #include <conio.h>
  18406.    #include <stdlib.h>
  18407.    #include <graph.h>
  18408.  
  18409.  
  18410.    void ellipsemask( short x1, short y1, short x2, short y2, char _far
  18411.    *newmask );
  18412.  
  18413.    unsigned char mask1[8] = { 0x43, 0x23, 0x7c, 0xf7, 0x8a, 0x4d, 0x78, 0x39
  18414.  };
  18415.    unsigned char mask2[8] = { 0x18, 0xad, 0xc0, 0x79, 0xf6, 0xc4, 0xa8, 0x23
  18416.  };
  18417.    char oldmask[8];
  18418.  
  18419.    void main()
  18420.    {
  18421.       int loop;
  18422.  
  18423.       /* Find a valid graphics mode. */
  18424.       if( !_setvideomode( _MAXRESMODE ) )
  18425.          exit( 1 );
  18426.  
  18427.       /* Set first fill mask and draw rectangle. */
  18428.       _setfillmask( mask1 );
  18429.       _rectangle( _GFILLINTERIOR, 20, 20, 100, 100 );
  18430.       getch();
  18431.  
  18432.       /* Call routine that saves and restores mask. */
  18433.       ellipsemask( 60, 60, 150, 150, mask2 );
  18434.       getch();
  18435.  
  18436.       /* Back to original mask. */
  18437.       _rectangle( _GFILLINTERIOR, 120, 120, 190, 190 );
  18438.       getch();
  18439.  
  18440.       _setvideomode( _DEFAULTMODE );
  18441.    }
  18442.  
  18443.    /* Draw an ellipse with a specified fill mask. */
  18444.    void ellipsemask( short x1, short y1, short x2, short y2, char _far
  18445.  *newmask )
  18446.    {
  18447.       unsigned char savemask[8];
  18448.  
  18449.       _getfillmask( savemask );                    /* Save mask         */
  18450.       _setfillmask( newmask );                     /* Set new mask      */
  18451.       _ellipse( _GFILLINTERIOR, x1, y1, x2, y2 );  /* Use new mask      */
  18452.       _setfillmask( savemask );                    /* Restore original  */
  18453.    }
  18454.  
  18455.  
  18456.  
  18457.  
  18458.  
  18459.  _getfontinfo
  18460.  ────────────────────────────────────────────────────────────────────────────
  18461.  
  18462.  
  18463.  Description
  18464.  
  18465.  Gets the current font characteristics.
  18466.  
  18467.    #include <graph.h>
  18468.  
  18469.    short _far _getfontinfo( struct _fontinfo _far *fontbuffer );
  18470.  
  18471.  fontbuffer                        Buffer to hold font information
  18472.  
  18473.  
  18474.  Remarks
  18475.  
  18476.  The _getfontinfo function gets the current font characteristics and stores
  18477.  them in a _fontinfo structure, defined in GRAPH.H.
  18478.  
  18479.  The _fontinfo structure contains the following elements:
  18480.  
  18481.  Element                           Contents
  18482.  ────────────────────────────────────────────────────────────────────────────
  18483.  int type                          Specifies vector (1) or bit-mapped (0)
  18484.                                    font
  18485.  
  18486.  int ascent                        Specifies pixel distance from top to
  18487.                                    baseline
  18488.  
  18489.  int pixwidth                      Specifies the character width in pixels;
  18490.                                    0 indicates a proportional font
  18491.  
  18492.  int pixheight                     Specifies the character height in pixels
  18493.  
  18494.  int avgwidth                      Specifies the average character width in
  18495.                                    pixels
  18496.  
  18497.  char filename [81]                Specifies the file name, including the
  18498.                                    path
  18499.  
  18500.  char facename [32]                Specifies the font name
  18501.  
  18502.  
  18503.  Return Value
  18504.  
  18505.  The _getfontinfo function returns a negative number if a font has not been
  18506.  registered or loaded.
  18507.  
  18508.  
  18509.  Compatibility
  18510.  
  18511.   ANSI   DOS   OS/2   UNIX   XENIX
  18512.  
  18513.  
  18514.  
  18515.  See Also
  18516.  
  18517.  _getgtextextent,  _outgtext,  _registerfonts,  _setfont,  _setgtextvector,
  18518.  _unregisterfonts
  18519.  
  18520.  
  18521.  Example
  18522.  
  18523.  See the example for _outgtext.
  18524.  
  18525.  
  18526.  
  18527.  
  18528.  
  18529.  _getgtextextent
  18530.  ────────────────────────────────────────────────────────────────────────────
  18531.  
  18532.  
  18533.  Description
  18534.  
  18535.  Gets the width in pixels of font-based text.
  18536.  
  18537.    #include <graph.h>
  18538.  
  18539.    short _far _getgtextextent( unsigned char _far *text );
  18540.  
  18541.  text                              Text to be analyzed
  18542.  
  18543.  
  18544.  Remarks
  18545.  
  18546.  The _getgtextextent function returns the width in pixels that would be
  18547.  required to print the text string using _outgtext with the current font.
  18548.  
  18549.  This function is particularly useful for determining the size of text that
  18550.  uses proportionally spaced fonts.
  18551.  
  18552.  
  18553.  Return Value
  18554.  
  18555.  The _getgtextextent function returns the width in pixels. It returns -1 if a
  18556.  font has not been registered.
  18557.  
  18558.  
  18559.  Compatibility
  18560.  
  18561.   ANSI   DOS   OS/2   UNIX   XENIX
  18562.  
  18563.  
  18564.  
  18565.  See Also
  18566.  
  18567.  _getfontinfo,  _outgtext,  _registerfonts,  _setfont,  _unregisterfonts
  18568.  
  18569.  
  18570.  Example
  18571.  
  18572.  See the example for _outgtext.
  18573.  
  18574.  
  18575.  
  18576.  
  18577.  
  18578.  _getimage Functions
  18579.  ────────────────────────────────────────────────────────────────────────────
  18580.  
  18581.  
  18582.  Description
  18583.  
  18584.  Store images in buffers.
  18585.  
  18586.    #include <graph.h>
  18587.  
  18588.    void _far _getimage( short x1, short y1, short x2, short y2, char _huge
  18589.    *image );
  18590.  
  18591.    void _far _getimage_w( double wx1, double wy1, double wx2, double wy2,
  18592.    char _huge *image );
  18593.  
  18594.    void _far _getimage_wxy( struct_wxycoord _far *pwxy1,
  18595.    struct_wxycoord _far *pwxy2, char _huge *image );
  18596.  
  18597.  x1, y1                            Upper-left corner of bounding rectangle
  18598.  
  18599.  x2, y2                            Lower-right corner of bounding rectangle
  18600.  
  18601.  wx1, wy1                          Upper-left corner of bounding rectangle
  18602.  
  18603.  wx2, wy2                          Lower-right corner of bounding rectangle
  18604.  
  18605.  pwxy1                             Upper-left corner of bounding rectangle
  18606.  
  18607.  pwxy2                             Lower-right corner of bounding rectangle
  18608.  
  18609.  image                             Storage buffer for screen image
  18610.  
  18611.  
  18612.  Remarks
  18613.  
  18614.  The _getimage functions store the screen image defined by a specified
  18615.  bounding rectangle into the buffer pointed to by image.
  18616.  
  18617.  The _getimage function defines the bounding rectangle with the view
  18618.  coordinates (x1, y1) and (x2, y2).
  18619.  
  18620.  The _getimage_w function defines the bounding rectangle with the window
  18621.  coordinates (wx1, wy1) and (wx2, wy2).
  18622.  
  18623.  The _getimage_wxy function defines the bounding rectangle with the
  18624.  window-coordinate pairs pwxy1 and pwxy2.
  18625.  
  18626.  The buffer must be large enough to hold the image. You can determine the
  18627.  size by calling the appropriate _imagesize function at run time, or by using
  18628.  the formula described on the _imagesize reference page.
  18629.  
  18630.  
  18631.  Return Value
  18632.  
  18633.  None. Use _grstatus to check success.
  18634.  
  18635.  
  18636.  Compatibility
  18637.  
  18638.   ANSI   DOS   OS/2   UNIX   XENIX
  18639.  
  18640.  
  18641.  
  18642.  See Also
  18643.  
  18644.  _grstatus,  _imagesize functions,  _putimage functions
  18645.  
  18646.  
  18647.  Example
  18648.  
  18649.    /* GIMAGE.C: This example illustrates animation routines including:
  18650.     *          _imagesize     _getimage     _putimage
  18651.     */
  18652.  
  18653.    #include <conio.h>
  18654.    #include <stddef.h>
  18655.    #include <stdlib.h>
  18656.    #include <malloc.h>
  18657.    #include <graph.h>
  18658.  
  18659.    short action[5]  = { _GPSET,   _GPRESET, _GXOR,    _GOR,     _GAND     };
  18660.    char *descrip[5] = {  "PSET  ", "PRESET", "XOR   ", "OR    ", "AND   " };
  18661.  
  18662.    void exitfree( char _huge *buffer );
  18663.  
  18664.    void main()
  18665.    {
  18666.        char  _huge *buffer;   /* Far pointer (with _fmalloc) could be used.
  18667.  */
  18668.        long  imsize;
  18669.        short i, x, y = 30;
  18670.  
  18671.        if( !_setvideomode( _MAXRESMODE ) )
  18672.            exit( 1 );
  18673.  
  18674.        /* Measure the image to be drawn and allocate memory for it. */
  18675.        imsize = (size_t)_imagesize( -16, -16, +16, +16 );
  18676.        buffer = halloc( imsize, sizeof( char ) );
  18677.        if ( buffer == (char _far *)NULL )
  18678.            exit( 1 );
  18679.  
  18680.        _setcolor( 3 );
  18681.        for ( i = 0; i < 5; i++ )
  18682.        {
  18683.            /* Draw ellipse at new position and get a copy of it. */
  18684.            x = 50; y += 40;
  18685.            _ellipse( _GFILLINTERIOR, x - 15, y - 15, x + 15, y + 15 );
  18686.            _getimage( x - 16, y - 16, x + 16, y + 16, buffer );
  18687.            if( _grstatus() )
  18688.                exitfree( buffer );        /* Quit on error
  18689.  */
  18690.  
  18691.  
  18692.    /* Display action type and copy a row of ellipses with that type. */
  18693.            _settextposition( 1, 1 );
  18694.            _outtext( descrip[i] );
  18695.            while( x < 260 )
  18696.            {
  18697.                x += 5;
  18698.                _putimage( x - 16, y - 16, buffer, action[i] );
  18699.                if( _grstatus() < 0 )      /* Ignore warnings, quit on errors.
  18700.  */
  18701.                    exitfree( buffer );
  18702.            }
  18703.            getch();
  18704.        }
  18705.        exitfree( buffer );
  18706.    }
  18707.  
  18708.    void exitfree( char _huge *buffer )
  18709.    {
  18710.        hfree( buffer );
  18711.        exit( !_setvideomode( _DEFAULTMODE ) );
  18712.    }
  18713.  
  18714.  
  18715.  
  18716.  
  18717.  
  18718.  _getlinestyle
  18719.  ────────────────────────────────────────────────────────────────────────────
  18720.  
  18721.  
  18722.  Description
  18723.  
  18724.  Gets the current line style.
  18725.  
  18726.    #include <graph.h>
  18727.  
  18728.    unsigned short _far _getlinestyle( void );
  18729.  
  18730.  
  18731.  Remarks
  18732.  
  18733.  Some graphics routines ( _lineto, _polygon, and _rectangle) output straight
  18734.  lines to the screen. The type of line can be controlled with the current
  18735.  line-style mask.
  18736.  
  18737.  The _getlinestyle function returns the current line-style mask. The mask is
  18738.  a 16-bit array in which each bit represents a pixel in the line being drawn.
  18739.  If the bit is 1, the corresponding pixel is set to the color of the line
  18740.  (the current color). If the bit is 0, the corresponding pixel is left
  18741.  unchanged. The mask is repeated over the length of the line. The default
  18742.  mask is 0xFFFF (a solid line).
  18743.  
  18744.  
  18745.  Return Value
  18746.  
  18747.  If no mask has been set, _getlinestyle returns the default mask.
  18748.  
  18749.  
  18750.  Compatibility
  18751.  
  18752.   ANSI   DOS   OS/2   UNIX   XENIX
  18753.  
  18754.  
  18755.  
  18756.  See Also
  18757.  
  18758.  _lineto functions,  _polygon functions,  _rectangle functions,
  18759.  _setlinestyle, _setwritemode
  18760.  
  18761.  
  18762.  Example
  18763.  
  18764.    /* GLINESTY.C: This program illustrates _setlinestyle and _getlinestyle.
  18765.    */
  18766.  
  18767.    #include <conio.h>
  18768.    #include <stdlib.h>
  18769.    #include <graph.h>
  18770.  
  18771.    void zigzag( short x1, short y1, short size );
  18772.  
  18773.    void main()
  18774.    {
  18775.  
  18776.       /* Find a valid graphics mode. */
  18777.       if( !_setvideomode( _MAXCOLORMODE ) )
  18778.          exit( 1 );
  18779.  
  18780.       /* Set line style and draw rectangle. */
  18781.       _setlinestyle( 0x4d );
  18782.       _rectangle( _GBORDER, 10, 10, 60, 60 );
  18783.       getch();
  18784.  
  18785.  
  18786.       /* Draw figure with function that changes and restores line style. */
  18787.       zigzag( 100, 100, 90 );
  18788.       getch();
  18789.  
  18790.       /* Original style reused. */
  18791.       _rectangle( _GBORDER, 190, 190, 130, 130 );
  18792.       getch();
  18793.  
  18794.       _setvideomode( _DEFAULTMODE );
  18795.    }
  18796.  
  18797.    /* Draw box with changing line styles. Restore original style. */
  18798.    void zigzag( short x1, short y1, short size )
  18799.    {
  18800.       short x, y, oldcolor;
  18801.       unsigned short oldstyle;
  18802.       unsigned short style[16] = { 0x0001, 0x0003, 0x0007, 0x000f,
  18803.                                    0x001f, 0x003f, 0x007f, 0x00ff,
  18804.                                    0x01ff, 0x03ff, 0x07ff, 0x0fff,
  18805.                                    0x1fff, 0x3fff, 0x7fff, 0xffff };
  18806.  
  18807.       oldcolor = _getcolor();
  18808.       oldstyle = _getlinestyle();            /* Save old line style.
  18809.  */
  18810.       for( x = 3, y = 3; x < size; x += 3, y += 3 )
  18811.       {
  18812.          _setcolor( x % 16 );
  18813.          _setlinestyle( style[x % 16] );     /* Set and use new line styles
  18814.  */
  18815.          _rectangle( _GBORDER, x1 - x, y1 - y, x1 + x, y1 + y );
  18816.       }
  18817.       _setlinestyle( oldstyle );             /* Restore old line style.
  18818.  */
  18819.       _setcolor( oldcolor );
  18820.    }
  18821.  
  18822.  
  18823.  
  18824.  
  18825.  
  18826.  _getphyscoord
  18827.  ────────────────────────────────────────────────────────────────────────────
  18828.  
  18829.  
  18830.  Description
  18831.  
  18832.  Gets physical coordinates.
  18833.  
  18834.    #include <graph.h>
  18835.  
  18836.    struct xycoord _far _getphyscoord( short x, short y );
  18837.  
  18838.  x, y                              View coordinates to translate
  18839.  
  18840.  
  18841.  Remarks
  18842.  
  18843.  The _getphyscoord function translates the view coordinates (x, y) to
  18844.  physical coordinates and returns them in an xycoord structure, defined in
  18845.  GRAPH.H.
  18846.  
  18847.  The xycoord structure contains the following elements:
  18848.  
  18849.  Element                           Description
  18850.  ────────────────────────────────────────────────────────────────────────────
  18851.  short xcoord                      x coordinate
  18852.  
  18853.  short ycoord                      y coordinate
  18854.  
  18855.  
  18856.  Return Value
  18857.  
  18858.  None.
  18859.  
  18860.  
  18861.  Compatibility
  18862.  
  18863.   ANSI   DOS   OS/2   UNIX   XENIX
  18864.  
  18865.  
  18866.  
  18867.  See Also
  18868.  
  18869.  _getviewcoord functions,  _grstatus,  _setvieworg,  _setviewport
  18870.  
  18871.  
  18872.  Example
  18873.  
  18874.  See the example for _setwindow.
  18875.  
  18876.  
  18877.  
  18878.  
  18879.  
  18880.  getpid
  18881.  ────────────────────────────────────────────────────────────────────────────
  18882.  
  18883.  
  18884.  Description
  18885.  
  18886.  Gets the process identification.
  18887.  
  18888.  #include <process.h>              Required only for function declarations
  18889.  
  18890.    int getpid( void );
  18891.  
  18892.  
  18893.  Remarks
  18894.  
  18895.  The getpid function returns the process ID, an integer that uniquely
  18896.  identifies the calling process.
  18897.  
  18898.  
  18899.  Return Value
  18900.  
  18901.  The getpid function returns the process ID. There is no error return.
  18902.  
  18903.  
  18904.  Compatibility
  18905.  
  18906.   ANSI   DOS   OS/2   UNIX   XENIX
  18907.  
  18908.  
  18909.  
  18910.  See Also
  18911.  
  18912.  mktemp
  18913.  
  18914.  
  18915.  Example
  18916.  
  18917.    /* GETPID.C: This program uses getpid to obtain the process ID and
  18918.     * then prints the ID.
  18919.     */
  18920.  
  18921.    #include <stdio.h>
  18922.    #include <process.h>
  18923.  
  18924.    void main( )
  18925.    {
  18926.       /* If run from DOS, shows different ID for DOS than for DOS shell.
  18927.        * If execed or spawned, shows ID of parent.
  18928.        */
  18929.       printf( "\nProcess id of parent: %d\n", getpid() );
  18930.    }
  18931.  
  18932.  
  18933.  Output
  18934.  
  18935.  
  18936.  
  18937.  
  18938.    Process id of parent: 828
  18939.  
  18940.  
  18941.  
  18942.  
  18943.  
  18944.  _getpixel Functions
  18945.  ────────────────────────────────────────────────────────────────────────────
  18946.  
  18947.  
  18948.  Description
  18949.  
  18950.  Get pixel values.
  18951.  
  18952.    #include <graph.h>
  18953.  
  18954.    short _far _getpixel( short x, short y );
  18955.  
  18956.    short _far _getpixel_w( double wx, double wy );
  18957.  
  18958.  x, y                              Pixel position
  18959.  
  18960.  wx, wy                            Pixel position
  18961.  
  18962.  
  18963.  Remarks
  18964.  
  18965.  The functions in the _getpixel family return the pixel value (a color index)
  18966.  at a specified location. The _getpixel function uses the view coordinate (x,
  18967.  y). The _getpixel_w function uses the window coordinate (wx, wy). The range
  18968.  of possible pixel values is determined by the current video mode. The color
  18969.  translation of pixel values is determined by the current palette.
  18970.  
  18971.  
  18972.  Return Value
  18973.  
  18974.  If successful, the function returns the color index. If the function fails
  18975.  (for example, the point lies outside the clipping region, or the program is
  18976.  in a text mode), it returns -1.
  18977.  
  18978.  
  18979.  Compatibility
  18980.  
  18981.   ANSI   DOS   OS/2   UNIX   XENIX
  18982.  
  18983.  
  18984.  
  18985.  See Also
  18986.  
  18987.  _getvideoconfig,  _grstatus,  _remapallpalette,  _remappalette,
  18988.  _selectpalette,  _setpixel functions,  _setvideomode
  18989.  
  18990.  
  18991.  Example
  18992.  
  18993.    /* GPIXEL.C: This program assigns different colors to randomly
  18994.     * selected pixels.
  18995.     */
  18996.  
  18997.    #include <conio.h>
  18998.    #include <stdlib.h>
  18999.    #include <graph.h>
  19000.  
  19001.    void main()
  19002.    {
  19003.       short xvar, yvar;
  19004.       struct videoconfig vc;
  19005.  
  19006.  
  19007.    /* Find a valid graphics mode. */
  19008.       if( !_setvideomode( _MAXCOLORMODE ) )
  19009.          exit( 1 );
  19010.       _getvideoconfig( &vc );
  19011.  
  19012.       /* Draw filled ellipse to turn on certain pixels. */
  19013.       _ellipse( _GFILLINTERIOR, vc.numxpixels / 6, vc.numypixels / 6,
  19014.                                 vc.numxpixels / 6 * 5, vc.numypixels / 6 * 5
  19015.                                               );
  19016.  
  19017.       /* Draw random pixels in random colors... */
  19018.       while( !kbhit() )
  19019.       {
  19020.          /* ...but only if they are already on (inside the ellipse). */
  19021.          xvar = rand() % vc.numxpixels;
  19022.          yvar = rand() % vc.numypixels;
  19023.          if( _getpixel( xvar, yvar ) != 0 )
  19024.          {
  19025.             _setcolor( rand() % 16 );
  19026.             _setpixel( xvar, yvar );
  19027.          }
  19028.       }
  19029.  
  19030.       getch();          /* Throw away the keystroke. */
  19031.       _setvideomode( _DEFAULTMODE );
  19032.    }
  19033.  
  19034.  
  19035.  
  19036.  
  19037.  
  19038.  gets
  19039.  ────────────────────────────────────────────────────────────────────────────
  19040.  
  19041.  
  19042.  Description
  19043.  
  19044.  Gets a line from the stdin stream.
  19045.  
  19046.    #include <stdio.h>
  19047.  
  19048.    char *gets( char *buffer );
  19049.  
  19050.  buffer                            Storage location for input string
  19051.  
  19052.  
  19053.  Remarks
  19054.  
  19055.  The gets function reads a line from the standard input stream stdin and
  19056.  stores it in buffer. The line consists of all characters up to and including
  19057.  the first newline character (\n). The gets function then replaces the
  19058.  newline character with a null character ('\0') before returning the line. In
  19059.  contrast, the fgets function retains the newline character.
  19060.  
  19061.  
  19062.  Return Value
  19063.  
  19064.  If successful, the gets function returns its argument. A NULL pointer
  19065.  indicates an error or end-of-file condition. Use ferror or feof to determine
  19066.  which one has occurred.
  19067.  
  19068.  
  19069.  Compatibility
  19070.  
  19071.   ANSI   DOS   OS/2   UNIX   XENIX
  19072.  
  19073.  
  19074.  
  19075.  See Also
  19076.  
  19077.  fgets, fputs, puts
  19078.  
  19079.  
  19080.  Example
  19081.  
  19082.    /* GETS.C */
  19083.  
  19084.    #include <stdio.h>
  19085.  
  19086.    void main()
  19087.    {
  19088.       char line[81];
  19089.  
  19090.       printf( "Input a string: " );
  19091.       gets( line );
  19092.       printf( "The line entered was: %s\n", line );
  19093.    }
  19094.  
  19095.  
  19096.  Output
  19097.  
  19098.  
  19099.  
  19100.    Input a string: This is a string
  19101.    The line entered was: This is a string
  19102.  
  19103.  
  19104.  
  19105.  
  19106.  
  19107.  _gettextcolor
  19108.  ────────────────────────────────────────────────────────────────────────────
  19109.  
  19110.  
  19111.  Description
  19112.  
  19113.  Gets the current text color.
  19114.  
  19115.    #include <graph.h>
  19116.  
  19117.    short _far _gettextcolor( void );
  19118.  
  19119.  
  19120.  Remarks
  19121.  
  19122.  The _gettextcolor function returns the color index of the current text
  19123.  color. The text color is set by the _settextcolor function and affects text
  19124.  output with the _outtext and _outmem functions only. The _setcolor function
  19125.  sets the color for font text output using the  _outgtext function.
  19126.  
  19127.  The default is 7 in test modes; it is the highest legal color index of the
  19128.  current palette in graphics modes.
  19129.  
  19130.  
  19131.  Return Value
  19132.  
  19133.  The _gettextcolor function returns the color index of the current text
  19134.  color.
  19135.  
  19136.  
  19137.  Compatibility
  19138.  
  19139.   ANSI   DOS   OS/2   UNIX   XENIX
  19140.  
  19141.  
  19142.  
  19143.  See Also
  19144.  
  19145.  _getvideoconfig,  _remappalette,  _selectpalette,  _setcolor,  _settextcolor
  19146.  
  19147.  
  19148.  
  19149.  Example
  19150.  
  19151.  See the example for _gettextposition.
  19152.  
  19153.  
  19154.  
  19155.  
  19156.  
  19157.  _gettextcursor
  19158.  ────────────────────────────────────────────────────────────────────────────
  19159.  
  19160.  
  19161.  Description
  19162.  
  19163.  Gets the current cursor attribute.
  19164.  
  19165.    #include <graph.h>
  19166.  
  19167.    short _far _gettextcursor( void );
  19168.  
  19169.  
  19170.  Remarks
  19171.  
  19172.  The _gettextcursor function returns the current cursor attribute (i.e., the
  19173.  shape). This function works only in text video modes.
  19174.  
  19175.  
  19176.  Return Value
  19177.  
  19178.  The function returns the current cursor attribute, or -1 if an error occurs
  19179.  (such as a call to the function in a graphics mode).
  19180.  
  19181.  
  19182.  Compatibility
  19183.  
  19184.   ANSI   DOS   OS/2   UNIX   XENIX
  19185.  
  19186.  
  19187.  
  19188.  See Also
  19189.  
  19190.  _displaycursor,  _grstatus,  _settextcursor
  19191.  
  19192.  
  19193.  Example
  19194.  
  19195.  See the example for _settextcursor.
  19196.  
  19197.  
  19198.  
  19199.  
  19200.  
  19201.  _gettextposition
  19202.  ────────────────────────────────────────────────────────────────────────────
  19203.  
  19204.  
  19205.  Description
  19206.  
  19207.  Gets the current text position.
  19208.  
  19209.    #include <graph.h>
  19210.  
  19211.    struct rccoord _far _gettextposition( void );
  19212.  
  19213.  
  19214.  Remarks
  19215.  
  19216.  The _gettextposition function returns the current text position as an
  19217.  rccoord structure, defined in GRAPH.H.
  19218.  
  19219.  The rccoord structure contains the following elements:
  19220.  
  19221.  Element                           Description
  19222.  ────────────────────────────────────────────────────────────────────────────
  19223.  short row                         Row coordinate
  19224.  
  19225.  short col                         Column coordinate
  19226.  
  19227.  
  19228.  Remarks
  19229.  
  19230.  The text position given by the coordinates (1,1) is defined as the
  19231.  upper-left corner of the text window.
  19232.  
  19233.  Text output from the _outtext and _outmem functions begins at the current
  19234.  text position. Font text is not affected by the current text position. Font
  19235.  text output begins at the current graphics output position, which is a
  19236.  separate position.
  19237.  
  19238.  
  19239.  Return Value
  19240.  
  19241.  None.
  19242.  
  19243.  
  19244.  Compatibility
  19245.  
  19246.   ANSI   DOS   OS/2   UNIX   XENIX
  19247.  
  19248.  
  19249.  
  19250.  See Also
  19251.  
  19252.  _getcurrentposition functions,  _moveto functions,  _outmem,  _outtext,
  19253.  _settextposition,  _settextwindow,  _wrapon
  19254.  
  19255.  
  19256.  Example
  19257.  
  19258.    /* OUTTXT.C: This example illustrates text output functions:
  19259.     *    _gettextcolor   _getbkcolor   _gettextposition   _outtext
  19260.     *    _settextcolor   _setbkcolor   _settextposition
  19261.     */
  19262.  
  19263.    #include <conio.h>
  19264.    #include <stdio.h>
  19265.    #include <graph.h>
  19266.  
  19267.    char buffer [80];
  19268.  
  19269.    void main()
  19270.    {
  19271.  
  19272.       /* Save original foreground, background, and text position. */
  19273.       short blink, fgd, oldfgd;
  19274.       long  bgd, oldbgd;
  19275.       struct rccoord oldpos;
  19276.  
  19277.       /* Save original foreground, background, and text position. */
  19278.       oldfgd = _gettextcolor();
  19279.       oldbgd = _getbkcolor();
  19280.       oldpos = _gettextposition();
  19281.       _clearscreen( _GCLEARSCREEN );
  19282.  
  19283.       /* First time no blink, second time blinking. */
  19284.       for( blink = 0; blink <= 16; blink += 16 )
  19285.       {
  19286.          /* Loop through 8 background colors. */
  19287.          for( bgd = 0; bgd < 8; bgd++ )
  19288.          {
  19289.             _setbkcolor( bgd );
  19290.             _settextposition( (short)bgd + ((blink / 16) * 9) + 3, 1 );
  19291.             _settextcolor( 7 );
  19292.             sprintf(buffer, "Back: %d Fore:", bgd );
  19293.             _outtext( buffer );
  19294.  
  19295.             /* Loop through 16 foreground colors. */
  19296.             for( fgd = 0; fgd < 16; fgd++ )
  19297.             {
  19298.                _settextcolor( fgd + blink );
  19299.                sprintf( buffer, " %2d ", fgd + blink );
  19300.                _outtext( buffer );
  19301.             }
  19302.          }
  19303.       }
  19304.       getch();
  19305.  
  19306.       /* Restore original foreground, background, and text position. */
  19307.       _settextcolor( oldfgd );
  19308.       _setbkcolor( oldbgd );
  19309.       _clearscreen( _GCLEARSCREEN );
  19310.       _settextposition( oldpos.row, oldpos.col );
  19311.    }
  19312.  
  19313.  
  19314.  
  19315.  
  19316.  
  19317.  _gettextwindow
  19318.  ────────────────────────────────────────────────────────────────────────────
  19319.  
  19320.  
  19321.  Description
  19322.  
  19323.  Gets the boundaries of the current text window.
  19324.  
  19325.    #include <graph.h>
  19326.  
  19327.    void _far _gettextwindow( short _far *r1, short _far *c1, short _far *r2,
  19328.    short _far *c2 );
  19329.  
  19330.  r1                                Top row of current text window
  19331.  
  19332.  c1                                Leftmost column of current text window
  19333.  
  19334.  r2                                Bottom row of current text window
  19335.  
  19336.  c2                                Rightmost column of current text window
  19337.  
  19338.  
  19339.  Remarks
  19340.  
  19341.  The _gettextwindow function finds the boundaries of the current text window.
  19342.  The text window is the region of the screen to which output from the
  19343.  _outtext and _outmem functions is limited. By default, this is the entire
  19344.  screen, unless it has been redefined by the _settextwindow function.
  19345.  
  19346.  The window defined by _settextwindow has no effect on output from the
  19347.  _outgtext function. Text displayed with _outgtext is limited to the current
  19348.  viewport.
  19349.  
  19350.  
  19351.  Return Value
  19352.  
  19353.  None.
  19354.  
  19355.  
  19356.  Compatibility
  19357.  
  19358.   ANSI   DOS   OS/2   UNIX   XENIX
  19359.  
  19360.  
  19361.  
  19362.  See Also
  19363.  
  19364.  _gettextposition,  _outmem,  _outtext,  _scrolltextwindow,
  19365.  _settextposition, _settextwindow,  _wrapon
  19366.  
  19367.  
  19368.  Example
  19369.  
  19370.  See the example for _scrolltextwindow.
  19371.  
  19372.  
  19373.  
  19374.  
  19375.  
  19376.  _getvideoconfig
  19377.  ────────────────────────────────────────────────────────────────────────────
  19378.  
  19379.  
  19380.  Description
  19381.  
  19382.  Gets graphics video configuration information.
  19383.  
  19384.    #include <graph.h>
  19385.  
  19386.    struct videoconfig _far * _far _getvideoconfig( struct videoconfig _far
  19387.    *config );
  19388.  
  19389.  config                            Configuration information
  19390.  
  19391.  
  19392.  Remarks
  19393.  
  19394.  The _getvideoconfig function returns the current graphics environment
  19395.  configuration in a videoconfig structure, defined in GRAPH.H.
  19396.  
  19397.  The values returned reflect the currently active video adapter and monitor,
  19398.  as well as the current video mode.
  19399.  
  19400.  The videoconfig structure contains the following members, each of which is
  19401.  of type short:
  19402.  
  19403.  Member                            Contents
  19404.  ────────────────────────────────────────────────────────────────────────────
  19405.  adapter                           Active display adapter
  19406.  
  19407.  bitsperpixel                      Number of bits per pixel
  19408.  
  19409.  memory                            Adapter video memory in kilobytes
  19410.  
  19411.  mode                              Current video mode
  19412.  
  19413.  monitor                           Active display monitor
  19414.  
  19415.  numcolors                         Number of color indices
  19416.  
  19417.  numtextcols                       Number of text columns available
  19418.  
  19419.  numtextrows                       Number of text rows available
  19420.  
  19421.  numvideopages                     Number of available video pages
  19422.  
  19423.  numxpixels                        Number of pixels on the x axis
  19424.  
  19425.  numypixels                        Number of pixels on the y axis
  19426.  
  19427.  
  19428.  
  19429.  The values for the adapter member of the videoconfig structure are given by
  19430.  the manifest constants shown in the list below. For any applicable adapter (
  19431.  _CGA, _EGA, or _VGA), the corresponding Olivetti(R) adapter ( _OCGA, _OEGA,
  19432.  or _OVGA) represents a superset of graphics capabilities.
  19433.  
  19434.  Adapter Constant                  Meaning
  19435.  ────────────────────────────────────────────────────────────────────────────
  19436.  _CGA                              Color Graphics Adapter
  19437.  
  19438.  _EGA                              Enhanced Graphics Adapter
  19439.  
  19440.  _HGC                              Hercules(R) Graphics Card
  19441.  
  19442.  _MCGA                             Multicolor Graphics Array
  19443.  
  19444.  _MDPA                             Monochrome Display Printer Adapter
  19445.  
  19446.  _OCGA                             Olivetti (AT&T(R)) Color Graphics
  19447.                                    Adapter
  19448.  
  19449.  _OEGA                             Olivetti (AT&T) Enhanced Graphics
  19450.                                    Adapter
  19451.  
  19452.  _OVGA                             Olivetti (AT&T) Video Graphics Array
  19453.  
  19454.  _VGA                              Video Graphics Array
  19455.  
  19456.  The values for the monitor member of the videoconfig structure are given by
  19457.  the manifest constants listed below:
  19458.  
  19459.  Monitor Constant                  Meaning
  19460.  ────────────────────────────────────────────────────────────────────────────
  19461.  _ANALOG                           Analog monochrome and color
  19462.  
  19463.  _ANALOGCOLOR                      Analog color only
  19464.  
  19465.  _ANALOGMONO                       Analog monochrome only
  19466.  
  19467.  _COLOR                            Color (or enhanced monitor emulating a
  19468.                                    color monitor)
  19469.  
  19470.  _ENHCOLOR                         Enhanced color
  19471.  
  19472.  _MONO                             Monochrome monitor
  19473.  
  19474.  In every text mode, including monochrome, the _getvideoconfig function
  19475.  returns the value 32 for the number of available colors. The value 32
  19476.  indicates the range of values (0 -31) accepted by the _settextcolor
  19477.  function. This includes 16 normal colors (0 -15) and 16 blinking colors (16
  19478.  -31). Blinking is selected by adding 16 to the normal color index. Because
  19479.  monochrome text mode has fewer unique display attributes, some color indices
  19480.  are redundant. However, because blinking is selected in the same manner,
  19481.  monochrome text mode has the same range (0 -31) as other text modes.
  19482.  
  19483.  
  19484.  Return Value
  19485.  
  19486.  The _getvideoconfig function returns the video configuration information in
  19487.  a structure, as noted above. There is no error return.
  19488.  
  19489.  
  19490.  Compatibility
  19491.  
  19492.   ANSI   DOS   OS/2   UNIX   XENIX
  19493.  
  19494.  
  19495.  
  19496.  See Also
  19497.  
  19498.  _setvideomode,  _setvideomoderows
  19499.  
  19500.  
  19501.  Example
  19502.  
  19503.    /* GVIDCFG.C: This program displays information about the current
  19504.     * video configuration.
  19505.     */
  19506.  
  19507.    #include <stdio.h>
  19508.    #include <graph.h>
  19509.  
  19510.    void main()
  19511.    {
  19512.       struct videoconfig vc;
  19513.       short  c;
  19514.       char   b[500];                        /* Buffer for string */
  19515.  
  19516.       _getvideoconfig( &vc );
  19517.  
  19518.       /* Write all information to a string, then output string. */
  19519.       c  = sprintf( b,     "X pixels:     %d\n", vc.numxpixels );
  19520.       c += sprintf( b + c, "Y pixels:     %d\n", vc.numypixels );
  19521.       c += sprintf( b + c, "Text columns: %d\n", vc.numtextcols );
  19522.       c += sprintf( b + c, "Text rows:    %d\n", vc.numtextrows );
  19523.       c += sprintf( b + c, "Colors:       %d\n", vc.numcolors );
  19524.       c += sprintf( b + c, "Bits/pixel:   %d\n", vc.bitsperpixel );
  19525.       c += sprintf( b + c, "Video pages:  %d\n", vc.numvideopages );
  19526.       c += sprintf( b + c, "Mode:         %d\n", vc.mode );
  19527.       c += sprintf( b + c, "Adapter:      %d\n", vc.adapter );
  19528.       c += sprintf( b + c, "Monitor:      %d\n", vc.monitor );
  19529.       c += sprintf( b + c, "Memory:       %d\n", vc.memory );
  19530.       _outtext( b );
  19531.    }
  19532.  
  19533.  
  19534.  Output
  19535.  
  19536.  
  19537.  
  19538.    X pixels:     0
  19539.    Y pixels:     0
  19540.    Text columns: 80
  19541.    Text rows:    25
  19542.    Colors:       32
  19543.    Bits/pixel:   0
  19544.    Video pages:  1
  19545.    Mode:         3
  19546.    Adapter:      8
  19547.    Monitor:      24
  19548.    Memory:       256
  19549.  
  19550.  
  19551.  
  19552.  
  19553.  
  19554.  _getviewcoord Functions
  19555.  ────────────────────────────────────────────────────────────────────────────
  19556.  
  19557.  
  19558.  Description
  19559.  
  19560.  Translate coordinates to view coordinates.
  19561.  
  19562.    #include <graph.h>
  19563.  
  19564.    struct xycoord _far _getviewcoord( short x, short y );
  19565.  
  19566.    struct xycoord _far _getviewcoord_w( double wx, double wy );
  19567.  
  19568.    struct xycoord _far _getviewcoord_wxy( struct _wxycoord _far *pwxy1 );
  19569.  
  19570.  x, y                              Physical point to translate
  19571.  
  19572.  wx, wy                            Window point to translate
  19573.  
  19574.  pwxy1                             Window point to translate
  19575.  
  19576.  
  19577.  Remarks
  19578.  
  19579.  The _getviewcoord routines translate the specified coordinates (x, y) from
  19580.  one coordinate system to view coordinates and then return them in an xycoord
  19581.  structure, defined in GRAPH.H. The xycoord structure contains the following
  19582.  elements:
  19583.  
  19584.  Element                           Description
  19585.  ────────────────────────────────────────────────────────────────────────────
  19586.  short xcoord                      x coordinate
  19587.  
  19588.  short ycoord                      y coordinate
  19589.  
  19590.  The various _getviewcoord routines translate in the following manner:
  19591.  
  19592.  Routine                           Translation
  19593.  ────────────────────────────────────────────────────────────────────────────
  19594.  _getviewcoord                     Physical coordinates (x, y) to view
  19595.                                    coordinates
  19596.  
  19597.  _getviewcoord_w                   Window coordinates (wx, wy) to view
  19598.                                    coordinates
  19599.  
  19600.  _getviewcoord_wxy                 Window coordinates structure (pwxy1) to
  19601.                                    view coordinates
  19602.  
  19603.  ────────────────────────────────────────────────────────────────────────────
  19604.  C 5.1 Version Difference
  19605.  In Microsoft C version 5.1, the function _getviewcoord was called
  19606.  _getlogcoord.
  19607.  ────────────────────────────────────────────────────────────────────────────
  19608.  
  19609.  
  19610.  Return Value
  19611.  
  19612.  The _getviewcoord function returns the coordinates as noted above. There is
  19613.  no error  return.
  19614.  
  19615.  
  19616.  Compatibility
  19617.  
  19618.   ANSI   DOS   OS/2   UNIX   XENIX
  19619.  
  19620.  
  19621.  
  19622.  See Also
  19623.  
  19624.  _getphyscoord,  _getwindowcoord,  _grstatus
  19625.  
  19626.  
  19627.  Example
  19628.  
  19629.  See the example for _setwindow.
  19630.  
  19631.  
  19632.  
  19633.  
  19634.  
  19635.  _getvisualpage
  19636.  ────────────────────────────────────────────────────────────────────────────
  19637.  
  19638.  
  19639.  Description
  19640.  
  19641.  Gets the current visual page number.
  19642.  
  19643.    #include <graph.h>
  19644.  
  19645.    short _far _getvisualpage( void );
  19646.  
  19647.  
  19648.  Remarks
  19649.  
  19650.  The _getvisualpage function returns the current visual page number.
  19651.  
  19652.  
  19653.  Return Value
  19654.  
  19655.  The function returns the number of the current visual page. All hardware
  19656.  combinations support at least one page (page number 0). In OS/2, only page 0
  19657.  is available.
  19658.  
  19659.  
  19660.  Compatibility
  19661.  
  19662.   ANSI   DOS   OS/2   UNIX   XENIX
  19663.  
  19664.  
  19665.  
  19666.  See Also
  19667.  
  19668.  _getactivepage,  _gettextcolor,  _gettextposition,  _outtext,
  19669.  _setactivepage, _settextcolor,  _settextposition,  _settextwindow,
  19670.  _setvideomode, _setvisualpage,  _wrapon
  19671.  
  19672.  
  19673.  Example
  19674.  
  19675.  See the example for _getactivepage.
  19676.  
  19677.  
  19678.  
  19679.  
  19680.  
  19681.  getw
  19682.  ────────────────────────────────────────────────────────────────────────────
  19683.  
  19684.  
  19685.  Description
  19686.  
  19687.  Gets an integer from a stream.
  19688.  
  19689.    #include <stdio.h>
  19690.  
  19691.    int getw( FILE *stream );
  19692.  
  19693.  stream                            Pointer to FILE structure
  19694.  
  19695.  
  19696.  Remarks
  19697.  
  19698.  The getw function reads the next binary value of type int from the file
  19699.  associated with stream and increments the associated file pointer (if there
  19700.  is one) to point to the next unread character. The getw function does not
  19701.  assume any special alignment of items in the stream.
  19702.  
  19703.  
  19704.  Return Value
  19705.  
  19706.  The getw function returns the integer value read. A return value of EOF may
  19707.  indicate an error or end-of-file. However, since the EOF value is also a
  19708.  legitimate integer value, feof or ferror should be used to verify an
  19709.  end-of-file or error condition.
  19710.  
  19711.  
  19712.  Compatibility
  19713.  
  19714.   ANSI   DOS   OS/2   UNIX   XENIX
  19715.  
  19716.  
  19717.  The getw function is provided primarily for compatibility with previous
  19718.  libraries. Note that portability problems may occur with getw, since the
  19719.  size of the int type and the ordering of bytes within the int type differ
  19720.  across systems.
  19721.  
  19722.  
  19723.  See Also
  19724.  
  19725.  putw
  19726.  
  19727.  
  19728.  Example
  19729.  
  19730.    /* GETW.C: This program uses getw to read a word from a stream,
  19731.     * then performs an error check.
  19732.     */
  19733.  
  19734.    #include <stdio.h>
  19735.    #include <stdlib.h>
  19736.  
  19737.    void main()
  19738.    {
  19739.  
  19740.       FILE *stream;
  19741.       int i;
  19742.  
  19743.  
  19744.    if( (stream = fopen( "getw.c", "rb" )) == NULL )
  19745.          printf( "Couldn't open file\n" );
  19746.       else
  19747.       {
  19748.          /* Read a word from the stream: */
  19749.          i = getw( stream );
  19750.  
  19751.          /* If there is an error... */
  19752.          if( ferror( stream ) )
  19753.          {
  19754.             printf( "getw failed\n" );
  19755.             clearerr( stream );
  19756.          }
  19757.          else
  19758.             printf( "First data word in file: 0x%.4x\n", i );
  19759.          fclose( stream );
  19760.       }
  19761.    }
  19762.  
  19763.  
  19764.  Output
  19765.  
  19766.  
  19767.  
  19768.    First data word in file: 0x2a2f
  19769.  
  19770.  
  19771.  
  19772.  
  19773.  
  19774.  _getwindowcoord
  19775.  ────────────────────────────────────────────────────────────────────────────
  19776.  
  19777.  
  19778.  Description
  19779.  
  19780.  Translates view coordinates to window coordinates.
  19781.  
  19782.    #include <graph.h>
  19783.  
  19784.    struct _wxycoord _far _getwindowcoord( short x, short y );
  19785.  
  19786.  x, y                              Physical point to translate
  19787.  
  19788.  
  19789.  Remarks
  19790.  
  19791.  The _getwindowcoord function translates the view coordinates (x, y) to
  19792.  window coordinates and returns them in the _wxycoord structure, defined in
  19793.  GRAPH.H.
  19794.  
  19795.  The _wxycoord structure contains the following elements:
  19796.  
  19797.  Element                           Description
  19798.  ────────────────────────────────────────────────────────────────────────────
  19799.  double wx                         x coordinate
  19800.  
  19801.  double wy                         y coordinate
  19802.  
  19803.  
  19804.  Return Value
  19805.  
  19806.  The function returns the coordinates in the _wxycoord structure. There is no
  19807.  error return.
  19808.  
  19809.  
  19810.  Compatibility
  19811.  
  19812.   ANSI   DOS   OS/2   UNIX   XENIX
  19813.  
  19814.  
  19815.  
  19816.  See Also
  19817.  
  19818.  _getphyscoord,  _getviewcoord functions,  _moveto functions,  _setwindow
  19819.  
  19820.  
  19821.  Example
  19822.  
  19823.  See the example for _setwindow.
  19824.  
  19825.  
  19826.  
  19827.  
  19828.  
  19829.  _getwritemode
  19830.  ────────────────────────────────────────────────────────────────────────────
  19831.  
  19832.  
  19833.  Description
  19834.  
  19835.  Gets the current logical mode for line drawing.
  19836.  
  19837.    #include  <graph.h>
  19838.  
  19839.    short _far _getwritemode( void );
  19840.  
  19841.  
  19842.  Remarks
  19843.  
  19844.  The _getwritemode function returns the current logical write mode, which is
  19845.  used when drawing lines with the _lineto, _polygon, and _rectangle
  19846.  functions.
  19847.  
  19848.  The default value is _GPSET, which causes lines to be drawn in the current
  19849.  graphics color. The other possible return values are _GXOR, _GAND, _GOR, and
  19850.  _GPRESET. See _putimage for more details on these manifest constants.
  19851.  
  19852.  
  19853.  Return Value
  19854.  
  19855.  The _getwritemode function returns the current logical write mode, or -1 if
  19856.  not in graphics mode.
  19857.  
  19858.  
  19859.  Compatibility
  19860.  
  19861.   ANSI   DOS   OS/2   UNIX   XENIX
  19862.  
  19863.  
  19864.  
  19865.  See Also
  19866.  
  19867.  _grstatus,  _lineto functions,  _putimage functions,  _rectangle functions,
  19868.  _setcolor,  _setlinestyle,  _setwritemode
  19869.  
  19870.  
  19871.  Example
  19872.  
  19873.    /* GWRMODE.C: This program illustrates _getwritemode and _setwritemode. */
  19874.  
  19875.    #include <conio.h>
  19876.    #include <stdlib.h>
  19877.    #include <graph.h>
  19878.  
  19879.    short wmodes[5]  = { _GPSET,   _GPRESET, _GXOR,    _GOR,     _GAND    };
  19880.    char *wmstr[5]   = { "PSET  ", "PRESET", "XOR   ", "OR    ", "AND   " };
  19881.  
  19882.    void box( short x, short y, short size, short writemode, short fillmode );
  19883.  
  19884.    void main()
  19885.    {
  19886.       short i, x, y;
  19887.  
  19888.  
  19889.    /* Find a valid graphics mode. */
  19890.       if( !_setvideomode( _MAXCOLORMODE ) )
  19891.          exit( 1 );
  19892.  
  19893.       x = y = 70;
  19894.       box( x, y, 50, _GPSET, _GFILLINTERIOR );
  19895.       _setcolor( 2 );
  19896.       box( x, y, 40, _GPSET, _GFILLINTERIOR );
  19897.       for( i = 0; i < 5; i++ )
  19898.       {
  19899.          _settextposition( 1, 1 );
  19900.          _outtext( wmstr[i] );
  19901.          box( x += 12, y += 12, 50, wmodes[i], _GBORDER );
  19902.          getch();
  19903.       }
  19904.       _setvideomode( _DEFAULTMODE );
  19905.    }
  19906.  
  19907.    void box( short x, short y, short size, short writemode, short fillmode )
  19908.    {
  19909.        short wm, side;
  19910.  
  19911.        wm = _getwritemode();           /* Save write mode and set new. */
  19912.        _setwritemode( writemode );
  19913.        _rectangle( fillmode, x - size, y - size, x + size, y + size );
  19914.        _setwritemode( wm );            /* Restore original write mode. */
  19915.    }
  19916.  
  19917.  
  19918.  
  19919.  
  19920.  
  19921.  gmtime
  19922.  ────────────────────────────────────────────────────────────────────────────
  19923.  
  19924.  
  19925.  Description
  19926.  
  19927.  Converts a time value to a structure.
  19928.  
  19929.    #include <time.h>
  19930.  
  19931.    struct tm *gmtime( const time_t *timer );
  19932.  
  19933.  timer                             Pointer to stored time
  19934.  
  19935.  
  19936.  Remarks
  19937.  
  19938.  The gmtime function converts the timer value to a structure. The timer
  19939.  argument represents the seconds elapsed since 00:00:00, January 1, 1970,
  19940.  Greenwich mean time. This value is usually obtained from a call to the timer
  19941.  function.
  19942.  
  19943.  The gmtime function breaks down the timer value and stores it in a structure
  19944.  of type tm, defined in TIME.H. (See asctime for a description of the
  19945.  structure members.) The structure result reflects Greenwich mean time, not
  19946.  local time.
  19947.  
  19948.  The fields of the structure type tm store the following values, each of
  19949.  which is an int:
  19950.  
  19951.  Field                             Value Stored
  19952.  ────────────────────────────────────────────────────────────────────────────
  19953.  tm_sec                            Seconds
  19954.  
  19955.  tm_min                            Minutes
  19956.  
  19957.  tm_hour                           Hours (0-24)
  19958.  
  19959.  tm_mday                           Day of month (1-31)
  19960.  
  19961.  tm_mon                            Month (0-11; January = 0)
  19962.  
  19963.  tm_year                           Year (current year minus 1900)
  19964.  
  19965.  tm_wday                           Day of week (0-6; Sunday = 0)
  19966.  
  19967.  tm_yday                           Day of year (0-365; January 1 = 0)
  19968.  
  19969.  tm_isdst                          Always 0 for gmtime
  19970.  
  19971.  The gmtime, mktime, and localtime functions use a single statically
  19972.  allocated structure to hold the result. Each call to one of these routines
  19973.  destroys the result of any previous call.
  19974.  
  19975.  DOS and OS/2 do not accommodate dates prior to 1980. If timer represents a
  19976.  date prior to 1980, gmtime returns NULL.
  19977.  
  19978.  
  19979.  Return Value
  19980.  
  19981.  The gmtime function returns a pointer to the structure result. There is no
  19982.  error return.
  19983.  
  19984.  
  19985.  Compatibility
  19986.  
  19987.   ANSI   DOS   OS/2   UNIX   XENIX
  19988.  
  19989.  
  19990.  
  19991.  See Also
  19992.  
  19993.  asctime, ctime, ftime, localtime, time
  19994.  
  19995.  
  19996.  Example
  19997.  
  19998.    /* GMTIME.C: This program uses gmtime to convert a long-integer
  19999.     * representation of Greenwich mean time to a structure named newtime,
  20000.     * then uses asctime to convert this structure to an output string.
  20001.     */
  20002.  
  20003.    #include <time.h>
  20004.    #include <stdio.h>
  20005.  
  20006.    void main()
  20007.    {
  20008.       struct tm *newtime;
  20009.       long ltime;
  20010.  
  20011.       time( <ime );
  20012.  
  20013.       /* Obtain Greenwich mean time: */
  20014.       newtime = gmtime( <ime );
  20015.       printf( "Greenwich mean time is %s\n", asctime( newtime ) );
  20016.    }
  20017.  
  20018.  
  20019.  Output
  20020.  
  20021.  
  20022.  
  20023.    Greenwich mean time is Fri Jun 16 16:37:53 1989
  20024.  
  20025.  
  20026.  
  20027.  
  20028.  
  20029.  _grstatus
  20030.  ────────────────────────────────────────────────────────────────────────────
  20031.  
  20032.  
  20033.  Description
  20034.  
  20035.  Returns the status of the most recent graphics function call.
  20036.  
  20037.    #include  <graph.h>
  20038.  
  20039.    short _far _grstatus( void );
  20040.  
  20041.  
  20042.  Remarks
  20043.  
  20044.  The _grstatus function returns the status of the most recently used graphics
  20045.  function. The _grstatus function can be used immediately following a call to
  20046.  a graphics routine to determine if errors or warnings were generated. Return
  20047.  values less than 0 are errors, and values greater than 0 are warnings.
  20048.  
  20049.  The following manifest constants are defined in the GRAPH.H header file for
  20050.  use with the _grstatus function:
  20051.  
  20052. ╓┌──────┌──────────────────────┌─────────────────────────────────────────────
  20053.  Value  Constant               Meaning
  20054.  ─────────────────────────────────────────────────────────────────────────────
  20055.  0      _GROK                  Success
  20056.  
  20057.  Value  Constant               Meaning
  20058.  ─────────────────────────────────────────────────────────────────────────────
  20059. 
  20060.  -1     _GRERROR               Graphics error
  20061.  
  20062.  -2     _GRMODENOTSUPPORTED    Requested video mode not supported
  20063.  
  20064.  -3     _GRNOTINPROPERMODE     Requested routine only works in certain video
  20065.                                modes
  20066.  
  20067.  -4     _GRINVALIDPARAMETER    One or more parameters invalid
  20068.  
  20069.  -5     _GRFONTFILENOTFOUND    No matching font file found
  20070.  
  20071.  -6     _GRINVALIDFONTFILE     One or more font files invalid
  20072.  
  20073.  -7     _GRCORRUPTEDFONTFILE   One or more font files inconsistent
  20074.  
  20075.  -8     _GRINSUFFICIENTMEMORY  Not enough memory to allocate buffer or to
  20076.                                complete a _floodfill operation
  20077.  
  20078.  Value  Constant               Meaning
  20079.  ─────────────────────────────────────────────────────────────────────────────
  20080. 
  20081.  -9     _GRINVALIDIMAGEBUFFER  Image buffer data inconsistent
  20082.  
  20083.  1      _GRMOOUTPUT            No action taken
  20084.  
  20085.  2      _GRCLIPPED             Output was clipped to viewport
  20086.  
  20087.  3      _GRPARAMETERALTERED    One or more input parameters was altered to be
  20088.                                within range, or pairs of parameters were
  20089.                                interchanged to be in the proper order
  20090.  
  20091.  ─────────────────────────────────────────────────────────────────────────────
  20092.  
  20093.  
  20094.  
  20095.  After a graphics call, use an if statement to compare the return value of
  20096.  _grstatus to _GROK. For example:
  20097.  
  20098.    if( _grstatus < _GROK )
  20099.        /*handle graphics error*/ ;
  20100.  
  20101.  The functions listed below cannot cause errors, and they all set _grstatus
  20102.  to GROK:
  20103.  
  20104. ╓┌────────────────┌─────────────────┌────────────────────────────────────────╖
  20105.  ────────────────────────────────────────────────────────────────────────────
  20106.  _displaycursor   _gettextposition  _outmem
  20107.  
  20108.  _getactivepage   _gettextwindow    _outtext
  20109.  
  20110.  _getgtextvector  _getvideoconfig   _unregisterfonts
  20111.  
  20112.  _gettextcolor    _getvisualpage    _wrapon
  20113.  
  20114.  
  20115.  
  20116.   See the list below for the graphics functions that affect _grstatus. The
  20117.  list shows error or warning messages that can be set by the graphics
  20118.  function. In addition to the error codes listed, all of these functions can
  20119.  produce the _GRERROR error code.
  20120.  
  20121. ╓┌───────────────────────────────────┌───────────────────────────────────────
  20122.  Function                            Possible _grstatus
  20123.  G                                   Error Codes
  20124.  ─────────────────────────────────────────────────────────────────────────────
  20125.  _arc functions                      _GRNOTINPROPERMODE,
  20126.                                      _GRINVALIDPARAMETER
  20127.  
  20128.  _clearscreen                        _GRNOTINPROPERMODE,
  20129.                                      _GRINVALIDPARAMETER
  20130.  
  20131.  _ellipse functions                  _GRNOTINPROPERMODE,
  20132.                                      _GRINVALIDPARAMETER,
  20133.                                      _GRINSUFFICIENTMEMORY
  20134.  
  20135.  _getarcinfo                         _GRNOTINPROPERMODE
  20136.  
  20137.  _getcurrentposition                 _GRNOTINPROPERMODE
  20138.  functions
  20139.  
  20140.  _getfontinfo                        ( _GRERROR only)
  20141.  Function                            Possible _grstatus
  20142.  G                                   Error Codes
  20143.  ─────────────────────────────────────────────────────────────────────────────
  20144. _getfontinfo                        ( _GRERROR only)
  20145.  
  20146.  _getgtextextent                     ( _GRERROR only)
  20147.  
  20148.  _getgtextvector                     _GRPARAMETERALTERED
  20149.  
  20150.  _getimage                           _GRNOTINPROPERMODE
  20151.  
  20152.  _getphyscoord                       _GRNOTINPROPERMODE
  20153.  
  20154.  _getpixel                           _GRNOTINPROPERMODE
  20155.  
  20156.  _gettextcursor                      _GRNOTINPROPERMODE
  20157.  
  20158.  _getviewcoord functions             _GRNOTINPROPERMODE
  20159.  
  20160.  Continued on next page
  20161.  
  20162.  Function                            Possible _grstatus
  20163.  G                                   Error Codes
  20164.  ─────────────────────────────────────────────────────────────────────────────
  20165. 
  20166.  _getwindowcoord                     _GRNOTINPROPERMODE
  20167.  
  20168.  _getwritemode                       _GRNOTINPROPERMODE
  20169.  
  20170.  _imagesize functions                _GRNOTINPROPERMODE
  20171.  
  20172.  _lineto functions                   _GRNOTINPROPERMODE
  20173.  
  20174.  
  20175.  _moveto functions                   _GRNOTINPROPERMODE
  20176.  
  20177.  _outgtext                           _GRNOTINPROPERMODE
  20178.  
  20179.  
  20180.  _pie functions                      _GRNOTINPROPERMODE,
  20181.                                      _GRINVALIDPARAMETER,
  20182.                                      _GRINSUFFICIENTMEMORY
  20183.  Function                            Possible _grstatus
  20184.  G                                   Error Codes
  20185.  ─────────────────────────────────────────────────────────────────────────────
  20186.                                     _GRINSUFFICIENTMEMORY
  20187.  
  20188.  _polygon functions                  _GRNOTINPROPERMODE,
  20189.                                      _GRINVALIDPARAMETER,
  20190.                                      _GRINSUFFICIENTMEMORY
  20191.  
  20192.  _putimage functions                 _GRERROR,
  20193.                                      _GRNOTINPROPERMODE,
  20194.                                      _GRINVALIDPARAMETER,
  20195.                                      _GRINVALIDIMAGEBUFFER
  20196.  
  20197.  _rectangle functions                _GRNOTINPROPERMODE,
  20198.                                      _GRINVALIDPARAMETER,
  20199.                                      _GRINSUFFICIENTMEMORY
  20200.  
  20201.  _registerfonts                      _GRCORRUPTEDFONTFILE,
  20202.                                      _GRFONTFILENOTFOUND,
  20203.                                      _GRINSUFFICIENTMEMORY,
  20204.  Function                            Possible _grstatus
  20205.  G                                   Error Codes
  20206.  ─────────────────────────────────────────────────────────────────────────────
  20207.                                     _GRINSUFFICIENTMEMORY,
  20208.                                      _GRINVALIDFONTFILE
  20209.  
  20210.  _scrolltextwindow
  20211.  
  20212.  _selectpalette                      _GRNOTINPROPERMODE,
  20213.                                      _GRINVALIDPARAMETER
  20214.  
  20215.  _setactivepage                      _GRINVALIDPARAMETER
  20216.  
  20217.  _setbkcolor                         _GRINVALIDPARAMETER
  20218.  
  20219.  _setcliprgn                         _GRNOTINPROPERMODE
  20220.  
  20221.  _setcolor                           _GRNOTINPROPERMODE
  20222.  
  20223.  _setfont                            _GRERROR,
  20224.                                      _GRFONTFILENOTFOUND,
  20225.  Function                            Possible _grstatus
  20226.  G                                   Error Codes
  20227.  ─────────────────────────────────────────────────────────────────────────────
  20228.                                     _GRFONTFILENOTFOUND,
  20229.                                      _GRINSUFFICIENTMEMORY,
  20230.                                      _GRPARAMETERALTERED
  20231.  
  20232.  Continued on next page
  20233.  
  20234.  _setgtextvector                     _GRPARAMETERALTERED
  20235.  
  20236.  _settextcolor
  20237.  
  20238.  _settextcursor                      _GRNOTINPROPERMODE
  20239.  
  20240.  _settextposition
  20241.  
  20242.  _settextrows                        _GRINVALIDPARAMETER
  20243.  
  20244.  _settextwindow
  20245.  
  20246.  Function                            Possible _grstatus
  20247.  G                                   Error Codes
  20248.  ─────────────────────────────────────────────────────────────────────────────
  20249. 
  20250.  _setvideomode                       _GRERROR,
  20251.                                      _GRMODENOTSUPPORTED,
  20252.                                      _GRINVALIDPARAMETER
  20253.  
  20254.  _setvideomoderows                   _GRERROR,
  20255.                                      _GRMODENOTSUPPORTED,
  20256.                                      _GRINVALIDPARAMETER
  20257.  
  20258.  _setvieworg                         _GRNOTINPROPERMODE
  20259.  
  20260.  _setviewport                        _GRNOTINPROPERMODE
  20261.  
  20262.  _setvisualpage                      _GRINVALIDPARAMETER
  20263.  
  20264.  _setwindow                          _GRNOTINPROPERMODE,
  20265.                                      _GRINVALIDPARAMETER
  20266.  
  20267.  Function                            Possible _grstatus
  20268.  G                                   Error Codes
  20269.  ─────────────────────────────────────────────────────────────────────────────
  20270. 
  20271.  _setwritemode                       _GRNOTINPROPERMODE,
  20272.                                      _GRINVALIDPARAMETER
  20273.  
  20274.  ─────────────────────────────────────────────────────────────────────────────
  20275.  
  20276.  
  20277.  
  20278.  
  20279.  Return Value
  20280.  
  20281.  The _grstatus function returns the status of the most recently used graphics
  20282.  function.
  20283.  
  20284.  
  20285.  See Also
  20286.  
  20287.  _arc functions,  _ellipse functions,  _floodfill,  _lineto functions,  _pie
  20288.  functions, _remapallpalette,  _setactivepage,  _setbkcolor,  _setcolor,
  20289.  _setpixel functions, _settextcolor,  _settextcursor,  _setvisualpage,
  20290.  _setwindow,  _setwritemode
  20291.  
  20292.  
  20293.  Compatibility
  20294.  
  20295.   ANSI   DOS   OS/2   UNIX   XENIX
  20296.  
  20297.  
  20298.  
  20299.  
  20300.  
  20301.  
  20302.  halloc
  20303.  ────────────────────────────────────────────────────────────────────────────
  20304.  
  20305.  
  20306.  Description
  20307.  
  20308.  Allocates a huge memory block.
  20309.  
  20310.  #include <malloc.h>               Required only for function declarations
  20311.  
  20312.    void _huge *halloc( long num, size_t size );
  20313.  
  20314.  num                               Number of elements
  20315.  
  20316.  size                              Length in bytes of each element
  20317.  
  20318.  
  20319.  Remarks
  20320.  
  20321.  The halloc function allocates a huge array from the operating system
  20322.  consisting of num elements, each of which is size bytes long. Each element
  20323.  is initialized to 0. If the size of the array is greater than 128K (131,072
  20324.  bytes), the size of an array element must then be a power of 2.
  20325.  
  20326.  
  20327.  Return Value
  20328.  
  20329.  The halloc function returns a void huge pointer to the allocated space,
  20330.  which is guaranteed to be suitably aligned for storage of any type of
  20331.  object. To get a pointer to a type other than void huge, use a type cast on
  20332.  the return value. If the request cannot be satisfied, the return value is
  20333.  NULL.
  20334.  
  20335.  
  20336.  Compatibility
  20337.  
  20338.   ANSI   DOS   OS/2   UNIX   XENIX
  20339.  
  20340.  
  20341.  
  20342.  See Also
  20343.  
  20344.  calloc functions, free functions, hfree, malloc functions
  20345.  
  20346.  
  20347.  Example
  20348.  
  20349.    /* HALLOC.C: This program uses halloc to allocate space for 30,000 long
  20350.     * integers, then uses hfree to deallocate the memory.
  20351.     */
  20352.  
  20353.    #include <stdio.h>
  20354.    #include <stdlib.h>
  20355.    #include <malloc.h>
  20356.  
  20357.    void main()
  20358.    {
  20359.       long _huge *hbuf;
  20360.  
  20361.    /* Allocate huge buffer */
  20362.       hbuf = (long _huge *)halloc( 30000L, sizeof( long ) );
  20363.       if ( hbuf == NULL )
  20364.          printf( "Insufficient memory available\n" );
  20365.       else
  20366.          printf( "Memory successfully allocated\n" );
  20367.  
  20368.       /* Free huge buffer */
  20369.       hfree( hbuf );
  20370.    }
  20371.  
  20372.  
  20373.  Output
  20374.  
  20375.  
  20376.  
  20377.    Memory successfully allocated
  20378.  
  20379.  
  20380.  
  20381.  
  20382.  
  20383.  _hard Functions
  20384.  ────────────────────────────────────────────────────────────────────────────
  20385.  
  20386.  
  20387.  Description
  20388.  
  20389.  Handle critical error conditions.
  20390.  
  20391.    #include <dos.h>
  20392.  
  20393.    void _harderr( void( _far *handler )());
  20394.  
  20395.    void _hardresume( int result );
  20396.  
  20397.    void _hardretn( int error );
  20398.  
  20399.  handler ( )                       New INT 0x24 handler
  20400.  
  20401.  result                            Handler return parameter
  20402.  
  20403.  error                             Error to return from
  20404.  
  20405.  
  20406.  Remarks
  20407.  
  20408.  These three functions are used to handle critical error conditions that use
  20409.  DOS interrupt 0x24. The _harderr function installs a new critical-error
  20410.  handler for interrupt 0x24.
  20411.  
  20412.  The _hardresume and _hardreturn functions control how the program will
  20413.  return from the new critical-error handler installed by _harderr. The
  20414.  _hardresume function returns to DOS from a user-installed critical-error
  20415.  handler, and the _hardreturn function returns directly to the application
  20416.  program from a user-installed critical-error handler.
  20417.  
  20418.  The _harderr function does not directly install the handler pointed to by
  20419.  handler; instead, _harderr installs a handler that calls the function
  20420.  referenced by handler. The handler calls the function with the following
  20421.  parameters:
  20422.  
  20423.    handler(unsigned deverror, unsigned errcode, unsigned far *devhdr);
  20424.  
  20425.  
  20426.  The deverror argument is the device error code. It contains the AX register
  20427.  value passed by DOS to the INT 0x24 handler. The errcode argument is the DI
  20428.  register value that DOS passes to the handler. The low-order byte of errcode
  20429.  can be one of the following values:
  20430.  
  20431.  Code                              Meaning
  20432.  ────────────────────────────────────────────────────────────────────────────
  20433.  0                                 Attempt to write to a write-protected
  20434.                                    disk
  20435.  
  20436.  1                                 Unknown unit
  20437.  
  20438.  2                                 Drive not ready
  20439.  
  20440.  3                                 Unknown command
  20441.  
  20442.  4                                 Cyclic-redundancy-check error in data
  20443.  
  20444.  5                                 Bad drive-request structure length
  20445.  
  20446.  6                                 Seek error
  20447.  
  20448.  7                                 Unknown media type
  20449.  
  20450.  8                                 Sector not found
  20451.  
  20452.  9                                 Printer out of paper
  20453.  
  20454.  10                                Write fault
  20455.  
  20456.  11                                Read fault
  20457.  
  20458.  12                                General failure
  20459.  
  20460.  The devhdr argument is a far pointer to a device header that contains
  20461.  descriptive information about the device on which the error occurred. The
  20462.  user-defined handler must not change the information in the device-header
  20463.  control block.
  20464.  
  20465.  
  20466.  Errors on Disk Devices
  20467.  
  20468.  If the error occurred on a disk device, the high-order bit (bit 15) of the
  20469.  deverror argument will be set to 0, and the deverror argument will indicate
  20470.  the following:
  20471.  
  20472.  Bit                               Meaning
  20473.  ────────────────────────────────────────────────────────────────────────────
  20474.  15                                Disk error if false (0).
  20475.  
  20476.  14                                Not used.
  20477.  
  20478.  13                                "Ignore" response not allowed if false
  20479.                                    (0).
  20480.  
  20481.  12                                "Retry" response not allowed if false
  20482.                                    (0).
  20483.  
  20484.  11                                "Fail" response not allowed if false
  20485.                                    (0). Note that DOS changes "fail" to
  20486.                                    "abort".
  20487.  
  20488.  10, 9
  20489.  
  20490.                                    Code      Location
  20491.  ────────────────────────────────────────────────────────────────────────────
  20492.                                    00          DOS
  20493.  
  20494.                                    01          File allocation table
  20495.  
  20496.                                    10          Directory
  20497.  
  20498.                                    11          Data area
  20499.  
  20500.  
  20501.  8                                 Read error if false; write error if
  20502.                                    true.
  20503.  
  20504.  The low-order byte of deverror indicates the drive in which the error
  20505.  occurred (0 = drive A, 1 = drive B, etc.).
  20506.  
  20507.  
  20508.  Errors on Other Devices
  20509.  
  20510.  If the error occurs on a device other than a disk drive, the high-order bit
  20511.  (bit 15) of the deverror argument is 1. The attribute word located at offset
  20512.  4 in the device-header block indicates the type of device that had the
  20513.  error. If bit 15 of the attribute word is 0, the error is a bad memory image
  20514.  of the file allocation table. If the bit is 1, the error occurred on a
  20515.  character device and bits 0-3 of the attribute word indicate the type of
  20516.  device, as shown in the following list:
  20517.  
  20518.  Bit                               Meaning
  20519.  ────────────────────────────────────────────────────────────────────────────
  20520.  0                                 Current standard input
  20521.  
  20522.  1                                 Current standard output
  20523.  
  20524.  2                                 Current null device
  20525.  
  20526.  3                                 Current clock device
  20527.  
  20528.  
  20529.  Restrictions on Handler Functions
  20530.  
  20531.  The user-defined handler function can issue only system calls 0x01 through
  20532.  0x0C, or 0x59. Thus, many of the standard C run-time functions (such as
  20533.  stream I/O and low-level I/O) cannot be used in a hardware error handler.
  20534.  Function 0x59 can be used to obtain further information about the error that
  20535.  occurred.
  20536.  
  20537.  
  20538.  Using _hardresume and _harderr
  20539.  
  20540.  If the handler returns, it can do so -
  20541.    1.  From the return statement
  20542.  
  20543.    2.  From the _hardresume function
  20544.  
  20545.    3.  From the _hardretn function
  20546.  
  20547.  
  20548.  
  20549.  If the handler returns from _hardresume or from a return statement, the
  20550.  handler returns to DOS.
  20551.  
  20552.  The _hardresume function should be called only from within the user-defined
  20553.  hardware error-handler function. The result supplied to _hardresume must be
  20554.  one of the following constants:
  20555.  
  20556.  Constant                          Action
  20557.  ────────────────────────────────────────────────────────────────────────────
  20558.  _HARDERR_ABORT                    Abort the program by issuing INT 0x23
  20559.  
  20560.  _HARDERR_FAIL                     Fail the system call that is in progress
  20561.                                    (this is not supported on DOS 2. x)
  20562.  
  20563.  _HARDERR_IGNORE                   Ignore the error
  20564.  
  20565.  _HARDERR_RETRY                    Retry the operation
  20566.  
  20567.  The _hardretn function allows the user-defined hardware error handler to
  20568.  return directly to the application program rather than returning to DOS. The
  20569.  application resumes at the point just after the failing I/O function
  20570.  request. The _hardretn function should be called only from within a
  20571.  user-defined hardware error-handler function.
  20572.  
  20573.  The error parameter of _hardretn should be a DOS error code, as opposed to
  20574.  the XENIX-style error code that is available in errno. Refer to MS-DOS
  20575.  Encyclopedia (Duncan, ed.; Redmond, Wa.: Microsoft Press, 1988) or
  20576.  Programmer's PC Sourcebook (Hogan; Redmond, Wa.: Microsoft Press, 1988) for
  20577.  information about the DOS error codes that may be returned by a given DOS
  20578.  function call.
  20579.  
  20580.  If the failing I/O function request is an INT 0x21 function greater than or
  20581.  equal to function 0x38, _hardretn will then return to the application with
  20582.  the carry flag set and the AX register set to the _hardretn error parameter.
  20583.  If the failing INT 0x21 function request is less than function 0x38 and the
  20584.  function can return an error, the AL register will be set to 0xFF on return
  20585.  to the application. If the failing INT 0x21 does not have a way of returning
  20586.  an error condition (which is true of certain INT 0x21 functions below 0x38),
  20587.  the error parameter of _hardretn is not used, and no error code is returned
  20588.  to the application.
  20589.  
  20590.  
  20591.  Return Value
  20592.  
  20593.  None.
  20594.  
  20595.  
  20596.  Compatibility
  20597.  
  20598.   ANSI   DOS   OS/2   UNIX   XENIX
  20599.  
  20600.  
  20601.  
  20602.  See Also
  20603.  
  20604.  _chain_intr,  _dos_getvect,  _dos_setvect
  20605.  
  20606.  
  20607.  
  20608.  
  20609.  
  20610.  _heapadd Functions
  20611.  ────────────────────────────────────────────────────────────────────────────
  20612.  
  20613.  
  20614.  Description
  20615.  
  20616.  Add memory to the heap (_heapadd) or to the based heap (_bheapadd).
  20617.  
  20618.  #include <malloc.h>               Required only for function declarations
  20619.  
  20620.    int _heapadd( void _far *memblock, size_t size );
  20621.  
  20622.    int _bheapadd( _segment seg, void _based (void) *memblock, size_t size );
  20623.  
  20624.  seg                               Based-heap segment selector
  20625.  
  20626.  buffer                            Pointer to heap memory
  20627.  
  20628.  size                              Size in bytes of memory to add
  20629.  
  20630.  
  20631.  Remarks
  20632.  
  20633.  The _heapadd and _bheapadd functions add an unused piece of memory to the
  20634.  heap. The _bheapadd function adds the memory to the based heap specified by
  20635.  seg. The _heapadd function looks at the segment value and, if it is DGROUP,
  20636.  adds the memory to the near heap. Otherwise, _heapadd adds the memory to the
  20637.  far heap.
  20638.  
  20639.  
  20640.  Return Value
  20641.  
  20642.  These functions return 0 if successful, or -1 if an error occurred.
  20643.  
  20644.  
  20645.  Compatibility
  20646.  
  20647.   ANSI   DOS   OS/2   UNIX   XENIX
  20648.  
  20649.  
  20650.  
  20651.  See Also
  20652.  
  20653.  free functions, halloc, hfree, malloc functions, realloc functions
  20654.  
  20655.  
  20656.  Example
  20657.  
  20658.    /* HEAPMIN.C: This program illustrates heap management using
  20659.     * _heapadd and _heapmin.
  20660.     */
  20661.  
  20662.    #include <stdio.h>
  20663.    #include <conio.h>
  20664.    #include <process.h>
  20665.    #include <malloc.h>
  20666.  
  20667.    void heapdump( char *msg );     /* Prototype */
  20668.  
  20669.    char s1[] = { "Here are some strings that we use at first, then don't\n"
  20670.  };
  20671.    char s2[] = { "need any more. We'll give their space to the heap.\n" };
  20672.  
  20673.    void main()
  20674.    {
  20675.        int *p[3], i;
  20676.  
  20677.        printf( "%s%s", s1, s2 );
  20678.        heapdump( "Initial heap" );
  20679.  
  20680.        /* Give space of used strings to heap. */
  20681.        _heapadd( s1, sizeof( s1 ) );
  20682.        _heapadd( s2, sizeof( s2 ) );
  20683.        heapdump( "After adding used strings" );
  20684.  
  20685.        /* Allocate some blocks. Some may use string blocks from _heapadd. */
  20686.        for( i = 0; i < 2; i++ )
  20687.            if( (p[i] = (int *)calloc( 10 * (i + 1), sizeof( int ) )) == NULL
  20688.  )
  20689.            {
  20690.                --i;
  20691.                break;
  20692.            }
  20693.        heapdump( "After allocating memory" );
  20694.  
  20695.        /* Free some of the blocks. */
  20696.        free( p[1] );
  20697.        free( p[2] );
  20698.        heapdump( "After freeing memory" );
  20699.  
  20700.        /* Minimize heap. */
  20701.        _heapmin();
  20702.        heapdump( "After compacting heap" );
  20703.    }
  20704.  
  20705.    /* Walk through heap entries, displaying information about each block. */
  20706.    void heapdump( char *msg )
  20707.    {
  20708.        struct _heapinfo hi;
  20709.  
  20710.        printf( "%s\n", msg );
  20711.        hi._pentry = NULL;
  20712.        while( _heapwalk( &hi ) == _HEAPOK )
  20713.            printf( "\t%s block at %Fp of size %u\t\n",
  20714.                    hi._useflag == _USEDENTRY ? "USED" : "FREE",
  20715.                    hi._pentry,
  20716.                    hi._size );
  20717.        getch();
  20718.    }
  20719.  
  20720.  
  20721.  Output
  20722.  
  20723.  
  20724.  
  20725.    Here are some strings that we use at first, then don't
  20726.    need any more. We'll give their space to the heap.
  20727.    Initial heap
  20728.       USED block at 2D39:0E9C of size 364
  20729.       USED block at 2D39:100A of size 36
  20730.       USED block at 2D39:1030 of size 512
  20731.       FREE block at 2D39:1232 of size 460
  20732.    After adding used strings
  20733.       FREE block at 2D39:0044 of size 52
  20734.       FREE block at 2D39:007A of size 50
  20735.       USED block at 2D39:00AE of size 3564
  20736.       USED block at 2D39:0E9C of size 364
  20737.       USED block at 2D39:100A of size 36
  20738.       USED block at 2D39:1030 of size 512
  20739.       FREE block at 2D39:1232 of size 460
  20740.    After allocating memory
  20741.       USED block at 2D39:0044 of size 20
  20742.       USED block at 2D39:005A of size 40
  20743.       FREE block at 2D39:0084 of size 40
  20744.       USED block at 2D39:00AE of size 3564
  20745.       USED block at 2D39:0E9C of size 364
  20746.       USED block at 2D39:100A of size 36
  20747.       USED block at 2D39:1030 of size 512
  20748.       FREE block at 2D39:1232 of size 460
  20749.    After freeing memory
  20750.       USED block at 2D39:0044 of size 20
  20751.       FREE block at 2D39:005A of size 40
  20752.       FREE block at 2D39:0084 of size 40
  20753.       USED block at 2D39:00AE of size 3564
  20754.       USED block at 2D39:0E9C of size 364
  20755.       USED block at 2D39:100A of size 36
  20756.       USED block at 2D39:1030 of size 512
  20757.       FREE block at 2D39:1232 of size 460
  20758.    After compacting heap
  20759.       USED block at 2D39:0044 of size 20
  20760.       FREE block at 2D39:005A of size 82
  20761.       USED block at 2D39:00AE of size 3564
  20762.       USED block at 2D39:0E9C of size 364
  20763.       USED block at 2D39:100A of size 36
  20764.       USED block at 2D39:1030 of size 512
  20765.       FREE block at 2D39:1232 of size 12
  20766.  
  20767.  
  20768.  
  20769.  
  20770.  
  20771.  _heapchk Functions
  20772.  ────────────────────────────────────────────────────────────────────────────
  20773.  
  20774.  
  20775.  Description
  20776.  
  20777.  Run consistency checks on the heap.
  20778.  
  20779.    #include <malloc.h>
  20780.  
  20781.    int _heapchk( void );
  20782.  
  20783.    int _bheapchk( _segment seg );
  20784.  
  20785.    int _fheapchk( void );
  20786.  
  20787.    int _nheapchk( void );
  20788.  
  20789.  seg                               Specified base heap
  20790.  
  20791.  
  20792.  Remarks
  20793.  
  20794.  The _heapchk routines help to debug heap-related problems by checking for
  20795.  minimal consistency of the heap.
  20796.  
  20797.  Each function checks a particular heap, as listed below:
  20798.  
  20799.  Function                          Heap Checked
  20800.  ────────────────────────────────────────────────────────────────────────────
  20801.  _heapchk                          Depends on data model of program
  20802.  
  20803.  _bheapchk                         Based heap specified by seg value
  20804.  
  20805.  _fheapchk                         Far heap (outside the default data
  20806.                                    segment)
  20807.  
  20808.  _nheapchk                         Near heap (inside the default data
  20809.                                    segment)
  20810.  
  20811.  In large data models (that is, compact-, large-, and huge-model programs),
  20812.  _heapchk maps to _fheapchk. In small data models (tiny-, small-, and
  20813.  medium-model programs), _heapchk maps to _nheapchk.
  20814.  
  20815.  
  20816.  Return Value
  20817.  
  20818.  All four routines return an integer value that is one of the following
  20819.  manifest constants (defined in MALLOC.H):
  20820.  
  20821.  Constant                          Meaning
  20822.  ────────────────────────────────────────────────────────────────────────────
  20823.  _HEAPBADBEGIN                     Initial header information cannot be
  20824.                                    found, or it is bad
  20825.  
  20826.  _HEAPBADNODE                      Bad node has been found, or the heap is
  20827.                                    damaged
  20828.  
  20829.  _HEAPEMPTY                        Heap has not been initialized
  20830.  
  20831.  _HEAPOK                           Heap appears to be consistent
  20832.  
  20833.  
  20834.  Compatibility
  20835.  
  20836.   ANSI   DOS   OS/2   UNIX   XENIX
  20837.  
  20838.  
  20839.  
  20840.  See Also
  20841.  
  20842.  _heapset functions,  _heapwalk functions
  20843.  
  20844.  
  20845.  Example
  20846.  
  20847.    /* HEAPCHK.C: This program checks the heap for consistency
  20848.     * and prints an appropriate message.
  20849.     */
  20850.  
  20851.    #include <malloc.h>
  20852.    #include <stdio.h>
  20853.  
  20854.    void main()
  20855.    {
  20856.       int  heapstatus;
  20857.       char *buffer;
  20858.  
  20859.       /* Allocate and deallocate some memory */
  20860.       if( (buffer = (char *)malloc( 100 )) != NULL )
  20861.          free( buffer );
  20862.  
  20863.       /* Check heap status */
  20864.       heapstatus = _heapchk();
  20865.       switch( heapstatus )
  20866.       {
  20867.          case _HEAPOK:
  20868.             printf(" OK - heap is fine\n" );
  20869.             break;
  20870.          case _HEAPEMPTY:
  20871.             printf(" OK - heap is empty\n" );
  20872.             break;
  20873.          case _HEAPBADBEGIN:
  20874.             printf( "ERROR - bad start of heap\n" );
  20875.             break;
  20876.          case _HEAPBADNODE:
  20877.             printf( "ERROR - bad node in heap\n" );
  20878.             break;
  20879.       }
  20880.    }
  20881.  
  20882.  
  20883.  Output
  20884.  
  20885.  
  20886.  
  20887.     OK - heap is fine
  20888.  
  20889.  
  20890.  
  20891.  
  20892.  
  20893.  _heapmin Functions
  20894.  ────────────────────────────────────────────────────────────────────────────
  20895.  
  20896.  
  20897.  Description
  20898.  
  20899.  Release unused heap memory to the operating system.
  20900.  
  20901.    #include <malloc.h>
  20902.  
  20903.    int _heapmin( void );
  20904.  
  20905.    int _bheapmin( _segment seg )
  20906.  
  20907.    int _fheapmin( void );
  20908.  
  20909.    int _nheapmin( void );
  20910.  
  20911.  seg                               Specified based-heap selector
  20912.  
  20913.  
  20914.  Remarks
  20915.  
  20916.  The _heapmin functions minimize the heap by releasing unused heap memory to
  20917.  the operating system.
  20918.  
  20919.  The various _heapmin functions release unused memory in these heaps:
  20920.  
  20921.  Function                          Heap Minimized
  20922.  ────────────────────────────────────────────────────────────────────────────
  20923.  _heapmin                          Depends on data model of program
  20924.  
  20925.  _bheapmin                         Based heap specified by seg value;
  20926.                                    _NULLSEG specifies all based heaps
  20927.  
  20928.  _fheapmin                         Far heap (outside default data segment)
  20929.  
  20930.  _nheapmin                         Near heap (inside default data segment)
  20931.  
  20932.  In large data models (that is, compact-, large-, and huge-model programs),
  20933.  _heapmin maps to _fheapmin. In small data models (tiny-, small-, and
  20934.  medium-model programs), _heapmin maps to _nheapmin.
  20935.  
  20936.  Based-heap segments are never freed (i.e., unlinked from the based heap list
  20937.  and released back to the operating system) by the _bheapmin function. The
  20938.  _bfreeseg function is used for that purpose.
  20939.  
  20940.  
  20941.  Return Value
  20942.  
  20943.  The _heapmin functions return 0 if the function completed successfully, or
  20944.  -1 in the case of an error.
  20945.  
  20946.  
  20947.  Compatibility
  20948.  
  20949.   ANSI   DOS   OS/2   UNIX   XENIX
  20950.  
  20951.  
  20952.  
  20953.  See Also
  20954.  
  20955.  _bfreeseg, free functions, malloc functions
  20956.  
  20957.  
  20958.  
  20959.  
  20960.  
  20961.  _heapset Functions
  20962.  ────────────────────────────────────────────────────────────────────────────
  20963.  
  20964.  
  20965.  Description
  20966.  
  20967.  Check heaps for minimal consistency and set the free entries to a specified
  20968.  value.
  20969.  
  20970.    #include <malloc.h>
  20971.  
  20972.    int _heapset( unsigned int fill );
  20973.  
  20974.    int _bheapset( _segment seg, unsigned int fill );
  20975.  
  20976.    int _fheapset( unsigned int fill );
  20977.  
  20978.    int _nheapset( unsigned int fill );
  20979.  
  20980.  fill                              Fill character
  20981.  
  20982.  seg                               Specified based-heap segment selector
  20983.  
  20984.  
  20985.  Remarks
  20986.  
  20987.  The _heapset family of routines helps debug heap-related problems in
  20988.  programs by showing free memory locations or nodes unintentionally
  20989.  overwritten.
  20990.  
  20991.  The _heapset routines first check for minimal consistency on the heap in a
  20992.  manner identical to that of the _heapchk functions. After the consistency
  20993.  check, the _heapset functions set each byte of the heap's free entries to
  20994.  the fill value. This known value shows which memory locations of the heap
  20995.  contain free nodes and which locations contain data that were
  20996.  unintentionally written to freed memory.
  20997.  
  20998.  The various _heapset functions check and fill these heaps:
  20999.  
  21000.  Function                          Heap Filled
  21001.  ────────────────────────────────────────────────────────────────────────────
  21002.  _heapset                          Depends on data model of program
  21003.  
  21004.  _bheapset                         Based heap specified by seg value;
  21005.                                    _NULLSEG specifies all based heaps
  21006.  
  21007.  _fheapset                         Far heap (outside default data segment)
  21008.  
  21009.  _nheapset                         Near heap (inside default data segment)
  21010.  
  21011.  In large data models (that is, compact-, large-, and huge-model programs),
  21012.  _heapset maps to _fheapset. In small data models (tiny-, small-, and
  21013.  medium-model programs), _heapset maps to _nheapset.
  21014.  
  21015.  
  21016.  Return Value
  21017.  
  21018.  All four routines return an int whose value is one of the following manifest
  21019.  constants (defined in MALLOC.H):
  21020.  
  21021.  Constant                          Meaning
  21022.  ────────────────────────────────────────────────────────────────────────────
  21023.  _HEAPBADBEGIN                     Initial header information cannot be
  21024.                                    found, or it is invalid
  21025.  
  21026.  _HEAPBADNODE                      Bad node has been found, or the heap is
  21027.                                    damaged
  21028.  
  21029.  _HEAPEMPTY                        Heap has not been initialized
  21030.  
  21031.  _HEAPOK                           Heap appears to be consistent
  21032.  
  21033.  
  21034.  
  21035.  
  21036.  Compatibility
  21037.  
  21038.   ANSI   DOS   OS/2   UNIX   XENIX
  21039.  
  21040.  
  21041.  
  21042.  See Also
  21043.  
  21044.  _heapchk functions,  _heapwalk functions
  21045.  
  21046.  
  21047.  Example
  21048.  
  21049.    /* HEAPSET.C: This program checks the heap and fills in free entries
  21050.     * with the character 'Z'.
  21051.     */
  21052.  
  21053.    #include <malloc.h>
  21054.    #include <stdio.h>
  21055.    #include <stdlib.h>
  21056.  
  21057.    void main()
  21058.    {
  21059.       int heapstatus;
  21060.       char *buffer;
  21061.  
  21062.    if( (buffer = malloc( 1 )) == NULL )   /* Make sure heap is initialized */
  21063.          exit( 0 );
  21064.       heapstatus = _heapset( 'Z' );          /* Fill in free entries */
  21065.       switch( heapstatus )
  21066.       {
  21067.          case _HEAPOK:
  21068.             printf( "OK - heap is fine\n" );
  21069.             break;
  21070.          case _HEAPEMPTY:
  21071.             printf( "OK - heap is empty\n" );
  21072.             break;
  21073.          case _HEAPBADBEGIN:
  21074.             printf( "ERROR - bad start of heap\n" );
  21075.             break;
  21076.          case _HEAPBADNODE:
  21077.             printf( "ERROR - bad node in heap\n" );
  21078.             break;
  21079.       }
  21080.       free( buffer );
  21081.    }
  21082.  
  21083.  
  21084.  Output
  21085.  
  21086.  
  21087.  
  21088.    OK - heap is fine
  21089.  
  21090.  
  21091.  
  21092.  
  21093.  
  21094.  _heapwalk Functions
  21095.  ────────────────────────────────────────────────────────────────────────────
  21096.  
  21097.  
  21098.  Description
  21099.  
  21100.  Traverse the heap and return information about the next entry.
  21101.  
  21102.    include <malloc.h>
  21103.  
  21104.    int _heapwalk( _HEAPINFO *entryinfo );
  21105.  
  21106.    int _bheapwalk( _segment seg, _HEAPINFO *entryinfo );
  21107.  
  21108.    int _fheapwalk( _HEAPINFO *entryinfo );
  21109.  
  21110.    int _nheapwalk(_HEAPINFO *entryinfo);
  21111.  
  21112.  entryinfo                         Buffer to contain heap information
  21113.  
  21114.  seg                               Based-heap segment selector
  21115.  
  21116.  
  21117.  Remarks
  21118.  
  21119.  The _heapwalk family of routines helps debug heap-related problems in
  21120.  programs.
  21121.  
  21122.  The _heapwalk routines walk through the heap, traversing one entry per call,
  21123.  and return a pointer to a _heapinfo structure that contains information
  21124.  about the next heap entry. The _heapinfo structure, defined in MALLOC.H,
  21125.  contains the following elements:
  21126.  
  21127.  Element                           Description
  21128.  ────────────────────────────────────────────────────────────────────────────
  21129.  int far *_pentry                  Heap entry pointer
  21130.  
  21131.  size_t  _size                     Size of heap entry
  21132.  
  21133.  int _useflag                      Entry "in use" flag
  21134.  
  21135.  A call to _heapwalk that returns _HEAPOK stores the size of the entry in the
  21136.  _size field and sets the _useflag field to either _FREEENTRY or _USEDENTRY
  21137.  (both are constants defined in MALLOC.H). To obtain this information about
  21138.  the first entry in the heap, pass the _heapwalk routine a pointer to a
  21139.  _heapinfo structure whose _pentry field is NULL.
  21140.  
  21141.  The various _heapwalk functions walk through and gather information on these
  21142.  heaps:
  21143.  
  21144.  Function                          Heap Walked
  21145.  ────────────────────────────────────────────────────────────────────────────
  21146.  _heapwalk                         Depends on data model of program
  21147.  
  21148.  _bheapwalk                        Based heap specified by seg value;
  21149.                                    _NULLSEG specifies all based heaps
  21150.  
  21151.  _fheapwalk                        Far heap (outside default data segment)
  21152.  
  21153.  _nheapwalk                        Near heap (inside default data segment)
  21154.  
  21155.  In large data models (that is, compact-, large-, and huge-model programs),
  21156.  _heapwalk maps to _fheapwalk. In small data models (tiny-, small-, and
  21157.  medium-model programs), _heapwalk maps to _nheapwalk.
  21158.  
  21159.  
  21160.  Return Value
  21161.  
  21162.  All three routines return one of the following manifest constants (defined
  21163.  in MALLOC.H):
  21164.  
  21165.  Constant                          Meaning
  21166.  ────────────────────────────────────────────────────────────────────────────
  21167.  _HEAPBADBEGIN                     The initial header information cannot be
  21168.                                    found, or it is invalid.
  21169.  
  21170.  _HEAPBADNODE                      A bad node has been found, or the heap
  21171.                                    is damaged.
  21172.  
  21173.  _HEAPBADPTR                       The _pentry field of the _heapinfo
  21174.                                    structure does not contain a valid
  21175.                                    pointer into the heap.
  21176.  
  21177.  _HEAPEND                          The end of the heap has been reached
  21178.                                    successfully.
  21179.  
  21180.  _HEAPEMPTY                        The heap has not been initialized.
  21181.  
  21182.  _HEAPOK                           No errors so far; the _heapinfo
  21183.                                    structure contains information about the
  21184.                                    next entry.
  21185.  
  21186.  
  21187.  Compatibility
  21188.  
  21189.   ANSI   DOS   OS/2   UNIX   XENIX
  21190.  
  21191.  
  21192.  
  21193.  See Also
  21194.  
  21195.  _heapchk functions,  _heapset functions
  21196.  
  21197.  
  21198.  Example
  21199.  
  21200.    /* HEAPWALK.C: This program "walks" the heap, starting at the beginning
  21201.     * (_pentry = NULL). It prints out each heap entry's use, location,
  21202.     * and size. It also prints out information about the overall state
  21203.     * of the heap as soon as _heapwalk returns a value other than _HEAPOK.
  21204.     */
  21205.  
  21206.    #include <stdio.h>
  21207.    #include <malloc.h>
  21208.  
  21209.    void heapdump( void );
  21210.  
  21211.    void main()
  21212.    {
  21213.       char *buffer;
  21214.  
  21215.       heapdump();
  21216.       if( (buffer = malloc( 59 )) != NULL )
  21217.       {
  21218.          heapdump();
  21219.          free( buffer );
  21220.       }
  21221.       heapdump();
  21222.    }
  21223.  
  21224.    void heapdump( void )
  21225.    {
  21226.       struct _heapinfo hinfo;
  21227.       int heapstatus;
  21228.  
  21229.       hinfo._pentry = NULL;
  21230.       while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK )
  21231.       {
  21232.          printf( "%6s block at %Fp of size %4.4X\n",
  21233.             ( hinfo._useflag == _USEDENTRY ? "USED" : "FREE" ),
  21234.             hinfo._pentry, hinfo._size );
  21235.       }
  21236.  
  21237.       switch( heapstatus )
  21238.       {
  21239.          case _HEAPEMPTY:
  21240.             printf( "OK - empty heap\n" );
  21241.             break;
  21242.          case _HEAPEND:
  21243.             printf( "OK - end of heap\n" );
  21244.             break;
  21245.          case _HEAPBADPTR:
  21246.             printf( "ERROR - bad pointer to heap\n" );
  21247.             break;
  21248.          case _HEAPBADBEGIN:
  21249.             printf( "ERROR - bad start of heap\n" );
  21250.             break;
  21251.          case _HEAPBADNODE:
  21252.             printf( "ERROR - bad node in heap\n" );
  21253.             break;
  21254.       }
  21255.    }
  21256.  
  21257.  
  21258.  Output
  21259.  
  21260.  
  21261.  
  21262.      USED block at 0067:103E of size 000E
  21263.      USED block at 0067:104E of size 01F4
  21264.      USED block at 0067:1244 of size 0026
  21265.      USED block at 0067:126C of size 0200
  21266.      FREE block at 0067:146E of size 0B90
  21267.    OK - end of heap
  21268.      USED block at 0067:103E of size 000E
  21269.      USED block at 0067:104E of size 01F4
  21270.      USED block at 0067:1244 of size 0026
  21271.      USED block at 0067:126C of size 0200
  21272.      USED block at 0067:146E of size 003C
  21273.      FREE block at 0067:14AC of size 0B52
  21274.    OK - end of heap
  21275.      USED block at 0067:103E of size 000E
  21276.      USED block at 0067:104E of size 01F4
  21277.      USED block at 0067:1244 of size 0026
  21278.      USED block at 0067:126C of size 0200
  21279.      FREE block at 0067:146E of size 003C
  21280.      FREE block at 0067:14AC of size 0B52
  21281.    OK - end of heap
  21282.  
  21283.  
  21284.  
  21285.  
  21286.  
  21287.  hfree
  21288.  ────────────────────────────────────────────────────────────────────────────
  21289.  
  21290.  
  21291.  Description
  21292.  
  21293.  Frees a huge memory block.
  21294.  
  21295.  #include <malloc.h>               Required only for function declarations
  21296.  
  21297.    void hfree( void _huge *memblock );
  21298.  
  21299.  memblock                          Pointer to allocated memory block
  21300.  
  21301.  
  21302.  Remarks
  21303.  
  21304.  The hfree function deallocates a memory block; the freed memory is returned
  21305.  to the operating system. The memblock argument points to a memory block
  21306.  previously allocated through a call to halloc. The number of bytes freed is
  21307.  the number of bytes specified when the block was allocated.
  21308.  
  21309.  Note that attempting to free an invalid memblock argument (one not allocated
  21310.  with halloc) may affect subsequent allocation and cause errors.
  21311.  
  21312.  
  21313.  Return Value
  21314.  
  21315.  None.
  21316.  
  21317.  
  21318.  Compatibility
  21319.  
  21320.   ANSI   DOS   OS/2   UNIX   XENIX
  21321.  
  21322.  
  21323.  
  21324.  See Also
  21325.  
  21326.  halloc
  21327.  
  21328.  
  21329.  Example
  21330.  
  21331.    /* HALLOC.C: This program uses halloc to allocate space for 30,000 long
  21332.     * integers, then uses hfree to deallocate the memory.
  21333.     */
  21334.  
  21335.    #include <stdio.h>
  21336.    #include <stdlib.h>
  21337.    #include <malloc.h>
  21338.  
  21339.    void main()
  21340.    {
  21341.       long _huge *hbuf;
  21342.  
  21343.       /* Allocate huge buffer */
  21344.       hbuf = (long _huge *)halloc( 30000L, sizeof( long ) );
  21345.       if ( hbuf == NULL )
  21346.          printf( "Insufficient memory available\n" );
  21347.       else
  21348.          printf( "Memory successfully allocated\n" );
  21349.  
  21350.    /* Free huge buffer */
  21351.       hfree( hbuf );
  21352.    }
  21353.  
  21354.  
  21355.  Output
  21356.  
  21357.  
  21358.  
  21359.    Memory successfully allocated
  21360.  
  21361.  
  21362.  
  21363.  
  21364.  
  21365.  hypot, hypotl
  21366.  ────────────────────────────────────────────────────────────────────────────
  21367.  
  21368.  
  21369.  Description
  21370.  
  21371.  Calculate the hypotenuse.
  21372.  
  21373.    #include <math.h>
  21374.  
  21375.    double hypot( double x, double y );
  21376.  
  21377.    long double hypotl( long double x, long double y );
  21378.  
  21379.  x, y                              Floating-point values
  21380.  
  21381.  
  21382.  Remarks
  21383.  
  21384.  The hypot and hypotl functions calculate the length of the hypotenuse of a
  21385.  right triangle, given the length of the two sides x and y (or xl and yl). A
  21386.  call to hypot is equivalent to the following:
  21387.  
  21388.    sqrt(x*x + y*y);
  21389.  
  21390.  
  21391.  The hypotl function uses the 80-bit, 10-byte coprocessor form of arguments
  21392.  and return values. See the reference page on the long double functions for
  21393.  more details on this data type.
  21394.  
  21395.  
  21396.  Return Value
  21397.  
  21398.  The functions return the length of the hypotenuse. If an overflow results,
  21399.  the functions return HUGE_VAL and set errno to ERANGE.
  21400.  
  21401.  
  21402.  Compatibility
  21403.  
  21404.  hypot
  21405.  
  21406.   ANSI   DOS   OS/2   UNIX   XENIX
  21407.  
  21408.  
  21409.  hypotl
  21410.  
  21411.   ANSI   DOS   OS/2   UNIX   XENIX
  21412.  
  21413.  
  21414.  
  21415.  See Also
  21416.  
  21417.  cabs
  21418.  
  21419.  
  21420.  Example
  21421.  
  21422.    /* HYPOT.C: This program prints the hypotenuse of a right triangle. */
  21423.  
  21424.    #include <math.h>
  21425.    #include <stdio.h>
  21426.  
  21427.    void main()
  21428.    {
  21429.       double x = 3.0, y = 4.0;
  21430.  
  21431.       printf( "If a right triangle has sides %2.1f and %2.1f, "
  21432.               "its hypotenuse is %2.1f\n", x, y, hypot( x, y ) );
  21433.    }
  21434.  
  21435.  
  21436.  Output
  21437.  
  21438.  
  21439.  
  21440.    If a right triangle has sides 3.0 and 4.0, its hypotenuse is 5.0
  21441.  
  21442.  
  21443.  
  21444.  
  21445.  
  21446.  _imagesize Functions
  21447.  ────────────────────────────────────────────────────────────────────────────
  21448.  
  21449.  
  21450.  Description
  21451.  
  21452.  Get amount of memory required to store graphics images.
  21453.  
  21454.    #include <graph.h>
  21455.  
  21456.    long _far _imagesize( short x1, short y1, short x2, short y2 );
  21457.  
  21458.    long _far _imagesize_w( double wx1, double wy1, double wx2, double wy2 );
  21459.  
  21460.    long _far _imagesize_wxy( struct _wxycoord _far *pwxy1,
  21461.    struct _wxycoord _far *pwxy2 );
  21462.  
  21463.  x1, y1                            Upper-left corner of bounding rectangle
  21464.  
  21465.  x2, y2                            Lower-right corner of bounding rectangle
  21466.  
  21467.  wx1, wy1                          Upper-left corner of bounding rectangle
  21468.  
  21469.  wx2, wy2                          Lower-right corner of bounding rectangle
  21470.  
  21471.  pwxy1                             Upper-left corner of bounding rectangle
  21472.  
  21473.  pwxy2                             Lower-right corner of bounding rectangle
  21474.  
  21475.  
  21476.  Remarks
  21477.  
  21478.  The functions in the _imagesize family return the number of bytes needed to
  21479.  store the image defined by the bounding rectangle and specified by the
  21480.  coordinates given in the function call.
  21481.  
  21482.  The _imagesize function defines the bounding rectangle in terms of
  21483.  view-coordinate points (x1, y1) and (x2, y2).
  21484.  
  21485.  The _imagesize_w function defines the bounding rectangle in terms of
  21486.  window-coordinate points (x1, y1) and (x2, y2).
  21487.  
  21488.  The _imagesize_wxy function defines the bounding rectangle in terms of the
  21489.  window-coordinate pairs pwxy1 and pwxy2.
  21490.  
  21491.  The number of bytes needed to store the image is determined by the following
  21492.  formula:
  21493.  
  21494.    xwid = abs(x1-x2)+1;
  21495.    ywid = abs(y1-y2)+1;
  21496.    size = 4+((long)((xwid*bits_per_pixel+7)/8)*(long)ywid);
  21497.  
  21498.  A call to _getvideoconfig stores the  bits_per_pixel  information in the
  21499.  bitsperpixel field of a videoconfig structure.
  21500.  
  21501.  
  21502.  Return Value
  21503.  
  21504.  The function returns the storage size of the image in bytes. There is no
  21505.  error return.
  21506.  
  21507.  
  21508.  Compatibility
  21509.  
  21510.   ANSI   DOS   OS/2   UNIX   XENIX
  21511.  
  21512.  
  21513.  
  21514.  See Also
  21515.  
  21516.  _getimage functions,  _getvideoconfig,  _putimage functions
  21517.  
  21518.  
  21519.  Example
  21520.  
  21521.  See the example for _getimage.
  21522.  
  21523.  
  21524.  
  21525.  
  21526.  
  21527.  inp, inpw
  21528.  ────────────────────────────────────────────────────────────────────────────
  21529.  
  21530.  
  21531.  Description
  21532.  
  21533.  Input a byte (inp) or a word (inpw) from a port.
  21534.  
  21535.  #include <conio.h>                Required only for function declarations
  21536.  
  21537.    int inp( unsigned port );
  21538.  
  21539.    unsigned inpw( unsigned port );
  21540.  
  21541.  port                              Port number
  21542.  
  21543.  
  21544.  Remarks
  21545.  
  21546.  The inp and inpw functions read a byte and a word, respectively, from the
  21547.  specified input port. The input value can be any unsigned integer in the
  21548.  range 0 - 65,535.
  21549.  
  21550.  To use inp and inpw in OS/2 protected mode, you must use a .DEF file to
  21551.  declare the IOSEG segment that the run-time library uses to perform
  21552.  input/output on the port. In addition, the intrinsic (/Oi) versions of these
  21553.  functions do not work unless you put the code in a segment that is marked
  21554.  with the IOPL keyword in the .DEF file.
  21555.  
  21556.  Because you cannot do IOPL from a regular code segment, the run-time library
  21557.  declares a separate code segment called _IOSEG. In order to use inp, inpw,
  21558.  outp, or outpw in any of the protected-mode run-time libraries (?LIBCP,
  21559.  LLIBCDLL, LLIBCMT, or CDLLOBJS-based DLL), you must have a .DEF file
  21560.  containing this line:
  21561.  
  21562.    SEGMENTS _IOSEG CLASS 'IOSEG_CODE' IOPL
  21563.  
  21564.  
  21565.  Return Value
  21566.  
  21567.  The functions return the byte or word read from port. There is no error
  21568.  return.
  21569.  
  21570.  
  21571.  Compatibility
  21572.  
  21573.   ANSI   DOS   OS2   UNIX   XENIX
  21574.  
  21575.  
  21576.  
  21577.  See Also
  21578.  
  21579.  outp, outpw
  21580.  
  21581.  
  21582.  Example
  21583.  
  21584.  See the example for outp.
  21585.  
  21586.  
  21587.  
  21588.  
  21589.  
  21590.  int86
  21591.  ────────────────────────────────────────────────────────────────────────────
  21592.  
  21593.  
  21594.  Description
  21595.  
  21596.  Executes the 8086 interrupt.
  21597.  
  21598.    #include <dos.h>
  21599.  
  21600.    int int86( int intnum, union REGS *inregs, union REGS *outregs );
  21601.  
  21602.  intnum                            Interrupt number
  21603.  
  21604.  inregs                            Register values on call
  21605.  
  21606.  outregs                           Register values on return
  21607.  
  21608.  
  21609.  Remarks
  21610.  
  21611.  The int86 function executes the 8086-processor-family interrupt specified by
  21612.  the interrupt number intnum. Before executing the interrupt, int86 copies
  21613.  the contents of inregs to the corresponding registers. After the interrupt
  21614.  returns, the function copies the current register values to outregs. It also
  21615.  copies the status of the system carry flag to the cflag field in the outregs
  21616.  argument. The inregs and outregs arguments are unions of type REGS. The
  21617.  union type is defined in the include file DOS.H.
  21618.  
  21619.  Do not use the int86 function to call interrupts that modify the DS
  21620.  register. Instead, use the int86x function. The int86x function loads the DS
  21621.  and ES registers from the segregs parameter and also stores the DS and ES
  21622.  registers into segregs after the function call.
  21623.  
  21624.  The REGS type is defined in the include file DOS.H.
  21625.  
  21626.  
  21627.  Return Value
  21628.  
  21629.  The return value is the value in the AX register after the interrupt
  21630.  returns. If the cflag field in outregs is nonzero, an error has occurred; in
  21631.  such cases, the _doserrno variable is also set to the corresponding error
  21632.  code.
  21633.  
  21634.  
  21635.  Compatibility
  21636.  
  21637.   ANSI   DOS   OS/2   UNIX   XENIX
  21638.  
  21639.  
  21640.  
  21641.  See Also
  21642.  
  21643.  bdos, int86x, intdos, intdosx
  21644.  
  21645.  
  21646.  Example
  21647.  
  21648.    /* INT86.C: This program uses int86 to call the BIOS video service
  21649.     * (INT 10H) to get information about the cursor.
  21650.     */
  21651.  
  21652.    #include <dos.h>
  21653.    #include <stdio.h>
  21654.  
  21655.    void main()
  21656.    {
  21657.       union REGS inregs, outregs;
  21658.  
  21659.       /* Set up to get cursor information. */
  21660.       inregs.h.ah = 3;       /* Get Cursor Position function */
  21661.       inregs.h.bh = 0;       /* Page 0 */
  21662.  
  21663.       /* Execute video interrupt: */
  21664.       int86( 0x10, &inregs, &outregs );
  21665.  
  21666.       /* Display results. */
  21667.       printf( "Cursor position\n\tRow: %d\n\tColumn: %d\n",
  21668.               outregs.h.dh, outregs.h.dl );
  21669.       printf( "Cursor shape\n\tStart: %d\n\tEnd: %d\n",
  21670.               outregs.h.ch, outregs.h.cl );
  21671.    }
  21672.  
  21673.  
  21674.  Output
  21675.  
  21676.  
  21677.  
  21678.    Cursor position
  21679.            Row: 2
  21680.            Column: 0
  21681.    Cursor shape
  21682.            Start: 6
  21683.            End: 7
  21684.  
  21685.  
  21686.  
  21687.  
  21688.  
  21689.  int86x
  21690.  ────────────────────────────────────────────────────────────────────────────
  21691.  
  21692.  
  21693.  Description
  21694.  
  21695.  Executes the 8086 interrupt; accepts segment-register values.
  21696.  
  21697.    #include <dos.h>
  21698.  
  21699.    int int86x( int intnum, union REGS *inregs, union REGS *outregs,
  21700.    struct SREGS *segregs );
  21701.  
  21702.  intnum                            Interrupt number
  21703.  
  21704.  inregs                            Register values on call
  21705.  
  21706.  outregs                           Register values on return
  21707.  
  21708.  segregs                           Segment-register values on call
  21709.  
  21710.  
  21711.  Remarks
  21712.  
  21713.  The int86x function executes the 8086-processor-family interrupt specified
  21714.  by the interrupt number intnum. Unlike the int86 function, int86x accepts
  21715.  segment-register values in segregs, enabling programs that use large-model
  21716.  data segments or far pointers to specify which segment or pointer should be
  21717.  used during the system call.
  21718.  
  21719.  Before executing the specified interrupt, int86x copies the contents of
  21720.  inregs and segregs to the corresponding registers. Only the DS and ES
  21721.  register values in segregs are used. After the interrupt returns, the
  21722.  function copies the current register values to outregs, cop-ies the current
  21723.  ES and DS values to segregs, and restores DS. It also copies the status of
  21724.  the system carry flag to the cflag field in outregs.
  21725.  
  21726.  The REGS and SREGS types are defined in the include file DOS.H.
  21727.  
  21728.  Segment values for the segregs argument can be obtained by using either the
  21729.  segread function or the FP_SEG macro.
  21730.  
  21731.  
  21732.  Return Value
  21733.  
  21734.  The return value is the value in the AX register after the interrupt
  21735.  returns. If the cflag field in outregs is nonzero, an error has occurred; in
  21736.  such cases, the _doserrno variable is also set to the corresponding error
  21737.  code.
  21738.  
  21739.  
  21740.  Compatibility
  21741.  
  21742.   ANSI   DOS   OS/2   UNIX   XENIX
  21743.  
  21744.  
  21745.  
  21746.  See Also
  21747.  
  21748.  bdos, FP_SEG, int86, intdos, intdosx, segread
  21749.  
  21750.  
  21751.  Example
  21752.  
  21753.    /* INT86X.C: In this program, int86x executes an INT 21H instruction
  21754.     * to invoke DOS system call 43H (change file attributes). The program
  21755.     * uses int86x because the file, which is referenced with a far pointer,
  21756.     * may be in a segment other than the default data segment. Thus, the
  21757.     * program must explicitly set the DS register with the SREGS structure.
  21758.     */
  21759.  
  21760.    #include <signal.h>
  21761.    #include <dos.h>
  21762.    #include <stdio.h>
  21763.    #include <process.h>
  21764.  
  21765.    char _far *filename = "int86x.c";
  21766.  
  21767.    void main()
  21768.    {
  21769.       union  REGS inregs, outregs;
  21770.       struct SREGS segregs;
  21771.       int    result;
  21772.  
  21773.       inregs.h.ah = 0x43;      /* DOS function to change attributes    */
  21774.       inregs.h.al = 0;         /* Subfunction 0 to get attributes)     */
  21775.       inregs.x.dx = FP_OFF( filename );   /* DS:DX points to file name */
  21776.       segregs.ds  = FP_SEG( filename );
  21777.       result = int86x( 0x21, &inregs, &outregs, &segregs );
  21778.       if( outregs.x.cflag )
  21779.          printf( "Can't get file attributes; error no. %d\n", result);
  21780.       else
  21781.          printf( "Attribs = 0x%.4x\n", outregs.x.cx );
  21782.    }
  21783.  
  21784.  
  21785.  Output
  21786.  
  21787.  
  21788.  
  21789.    Attribs = 0x0020
  21790.  
  21791.  
  21792.  
  21793.  
  21794.  
  21795.  intdos
  21796.  ────────────────────────────────────────────────────────────────────────────
  21797.  
  21798.  
  21799.  Description
  21800.  
  21801.  Executes the DOS system call.
  21802.  
  21803.    #include <dos.h>
  21804.  
  21805.    int intdos( union REGS *inregs, union REGS *outregs );
  21806.  
  21807.  inregs                            Register values on call
  21808.  
  21809.  outregs                           Register values on return
  21810.  
  21811.  
  21812.  Remarks
  21813.  
  21814.  The intdos function invokes the DOS system call specified by register values
  21815.  defined in inregs and returns the effect of the system call in outregs. The
  21816.  inregs and outregs arguments are unions of type REGS. The REGS type is
  21817.  defined in the include file DOS.H.
  21818.  
  21819.  To invoke a system call, intdos executes an INT 21H instruction. Before
  21820.  executing the instruction, the function copies the contents of inregs to the
  21821.  corresponding registers. After the INT instruction returns, intdos copies
  21822.  the current register values to outregs. It also copies the status of the
  21823.  system carry flag to the cflag field in outregs. A nonzero cflag field
  21824.  indicates the flag was set by the system call and also indicates an error
  21825.  condition.
  21826.  
  21827.  The intdos function is used to invoke DOS system calls that take arguments
  21828.  for input or output in registers other than DX (DH/DL) and AL. The intdos
  21829.  function is also used to invoke system calls that indicate errors by setting
  21830.  the carry flag. Under any other conditions, the bdos function can be used.
  21831.  
  21832.  Do not use the intdos function to call interrupts that modify the DS
  21833.  register. Instead, use the intdosx or int86x function.
  21834.  
  21835.  
  21836.  Return Value
  21837.  
  21838.  The intdos function returns the value of the AX register after the system
  21839.  call is completed. If the cflag field in outregs is nonzero, an error has
  21840.  occurred and _doserrno is also set to the corresponding error code.
  21841.  
  21842.  
  21843.  Compatibility
  21844.  
  21845.   ANSI   DOS   OS/2   UNIX   XENIX
  21846.  
  21847.  
  21848.  
  21849.  See Also
  21850.  
  21851.  bdos, intdosx
  21852.  
  21853.  
  21854.  Example
  21855.  
  21856.    /* INTDOS.C: This program uses intdos to invoke DOS system call 2AH
  21857.     * (gets the current date).
  21858.     */
  21859.  
  21860.    #include <dos.h>
  21861.    #include <stdio.h>
  21862.  
  21863.    void main()
  21864.    {
  21865.       union REGS inregs, outregs;
  21866.  
  21867.       inregs.h.ah = 0x2a;           /* DOS Get Date function: */
  21868.       intdos( &inregs, &outregs );
  21869.       printf( "Date: %d/%d/%d\n", outregs.h.dh, outregs.h.dl, outregs.x.cx );
  21870.    }
  21871.  
  21872.  
  21873.  Output
  21874.  
  21875.  
  21876.  
  21877.    Date: 6/16/1989
  21878.  
  21879.  
  21880.  
  21881.  
  21882.  
  21883.  intdosx
  21884.  ────────────────────────────────────────────────────────────────────────────
  21885.  
  21886.  
  21887.  Description
  21888.  
  21889.  Executes the DOS system call; accepts segment-register values.
  21890.  
  21891.    #include <dos.h>
  21892.  
  21893.    int intdosx( union REGS *inregs, union REGS *outregs, struct SREGS
  21894.    *segregs );
  21895.  
  21896.  inregs                            Register values on call
  21897.  
  21898.  outregs                           Register values on return
  21899.  
  21900.  segregs                           Segment-register values on call
  21901.  
  21902.  
  21903.  Remarks
  21904.  
  21905.  The intdosx function invokes the DOS system call specified by register
  21906.  values defined in inregs and returns the results of the system call in
  21907.  outregs. Unlike the intdos function, intdosx accepts segment-register values
  21908.  in segregs, enabling programs that use large-model data segments or far
  21909.  pointers to specify which segment or pointer should be used during the
  21910.  system call. The REGS and SREGS types are defined in the include file DOS.H.
  21911.  
  21912.  
  21913.  To invoke a system call, intdosx executes an INT 21H instruction. Before
  21914.  executing the instruction, the function copies the contents of inregs and
  21915.  segregs to the corresponding registers. Only the DS and ES register values
  21916.  in segregs are used. After the INT instruction returns, intdosx copies the
  21917.  current register values to outregs and restores DS. It also copies the
  21918.  status of the system carry flag to the cflag field in outregs. A nonzero
  21919.  cflag field indicates the flag was set by the system call and also indicates
  21920.  an error condition.
  21921.  
  21922.  The intdosx function is used to invoke DOS system calls that take an
  21923.  argument in the ES register or that take a DS register value different from
  21924.  the default data segment.
  21925.  
  21926.  Segment values for the segregs argument can be obtained by using either the
  21927.  segread function or the FP_SEG macro.
  21928.  
  21929.  
  21930.  Return Value
  21931.  
  21932.  The intdosx function returns the value of the AX register after the system
  21933.  call is completed. If the cflag field in outregs is nonzero, an error has
  21934.  occurred; in such cases, _doserrno is also set to the corresponding error
  21935.  code.
  21936.  
  21937.  
  21938.  Compatibility
  21939.  
  21940.   ANSI   DOS   OS/2   UNIX   XENIX
  21941.  
  21942.  
  21943.  
  21944.  See Also
  21945.  
  21946.  bdos, FP_SEG, intdos, segread
  21947.  
  21948.  
  21949.  Example
  21950.  
  21951.    /* INTDOSX.C */
  21952.    #include <dos.h>
  21953.    #include <stdio.h>
  21954.  
  21955.    char _far *buffer = "Dollar-sign terminated string\n\r\n\r$";
  21956.  
  21957.    void main()
  21958.    {
  21959.       union  REGS inregs, outregs;
  21960.       struct SREGS segregs;
  21961.  
  21962.       /* Print a $-terminated string on the screen using DOS function 0x09.
  21963.  */
  21964.       inregs.h.ah = 0x9;
  21965.       inregs.x.dx = FP_OFF( buffer );
  21966.       segregs.ds  = FP_SEG( buffer );
  21967.       intdosx( &inregs, &outregs, &segregs );
  21968.    }
  21969.  
  21970.  
  21971.  Output
  21972.  
  21973.  
  21974.  
  21975.    Dollar-sign terminated string
  21976.  
  21977.  
  21978.  
  21979.  
  21980.  
  21981.  is Functions
  21982.  ────────────────────────────────────────────────────────────────────────────
  21983.  
  21984.  
  21985.  Description
  21986.  
  21987.  
  21988.  Test characters for specified conditions.
  21989.  
  21990.    #include <ctype.h>
  21991.  
  21992.    int isalnum( int c );
  21993.  
  21994.    int isalpha( int c );
  21995.  
  21996.    int isascii( int c );
  21997.  
  21998.    int iscntrl( int c );
  21999.  
  22000.    int isdigit( int c );
  22001.  
  22002.    int isgraph( int c );
  22003.  
  22004.    int islower( int c );
  22005.  
  22006.    int isprint( int c );
  22007.  
  22008.    int ispunct( int c );
  22009.  
  22010.    int isspace( int c );
  22011.  
  22012.    int isupper( int c );
  22013.  
  22014.    int isxdigit( int c );
  22015.  
  22016.  c                                 Integer to be tested
  22017.  
  22018.  
  22019.  Remarks
  22020.  
  22021.  Each function in the is family tests a given integer value, returning a
  22022.  nonzero value if the integer satisfies the test condition and 0 if it does
  22023.  not. The ASCII character set is assumed.
  22024.  
  22025.  The is functions and their test conditions are listed below:
  22026.  
  22027.  Function                          Test Condition
  22028.  ────────────────────────────────────────────────────────────────────────────
  22029.  isalnum                           Alphanumeric ('A'-'Z', 'a'-'z', or
  22030.                                    '0'-'9')
  22031.  
  22032.  isalpha                           Letter ('A'-'Z' or 'a'-'z')
  22033.  
  22034.  isascii                           ASCII character (0x00 - 0x7F)
  22035.  
  22036.  iscntrl                           Control character (0x00 - 0x1F or 0x7F)
  22037.  
  22038.  isdigit                           Digit ('0'-'9')
  22039.  
  22040.  isgraph                           Printable character except space (' ')
  22041.  
  22042.  islower                           Lowercase letter ('a'-'z')
  22043.  
  22044.  isprint                           Printable character (0x20 - 0x7E)
  22045.  
  22046.  ispunct                           Punctuation character
  22047.  
  22048.  isspace                           White-space character (0x09 - 0x0D or
  22049.                                    0x20)
  22050.  
  22051.  isupper                           Uppercase letter ('A'-'Z')
  22052.  
  22053.  isxdigit                          Hexadecimal digit ('A'-'F','a'-'f', or
  22054.                                    '0'-'9')
  22055.  
  22056.  The isascii routine produces meaningful results for all integer values.
  22057.  However, the remaining routines produce a defined result only for integer
  22058.  values corresponding to the ASCII character set (that is, only where isascii
  22059.  holds true) or for the non-ASCII value EOF (defined in STDIO.H).
  22060.  
  22061.  These routines are implemented both as functions and as macros. For details
  22062.  on choosing a function or a macro implementation, see Section 1.4, "Choosing
  22063.  Between Functions and Macros."
  22064.  
  22065.  
  22066.  Return Value
  22067.  
  22068.  These routines return a nonzero value if the integer satisfies the test
  22069.  condition and 0 if it does not.
  22070.  
  22071.  
  22072.  Compatibility
  22073.  
  22074.  isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct,
  22075.  isspace, isupper, isxdigit
  22076.  
  22077.   ANSI   DOS   OS/2   UNIX   XENIX
  22078.  
  22079.  
  22080.  isascii
  22081.  
  22082.   ANSI   DOS   OS/2   UNIX   XENIX
  22083.  
  22084.  
  22085.  
  22086.  See Also
  22087.  
  22088.  toascii, tolower, toupper functions
  22089.  
  22090.  
  22091.  Example
  22092.  
  22093.    /* ISFAM.C: This program tests all characters between 0x0 and 0x7F,
  22094.     * then displays each character with abbreviations for the character-type
  22095.     * codes that apply.
  22096.     */
  22097.  
  22098.    #include <stdio.h>
  22099.    #include <ctype.h>
  22100.  
  22101.    void main()
  22102.    {
  22103.       int ch;
  22104.       for( ch = 0; ch <= 0x7F; ch++ )
  22105.       {
  22106.          printf( "%.2x ", ch );
  22107.          printf( " %c", isprint( ch )  ? ch   : '\0' );
  22108.          printf( "%4s", isalnum( ch )  ? "AN" : "" );
  22109.          printf( "%3s", isalpha( ch )  ? "A"  : "" );
  22110.          printf( "%3s", isascii( ch )  ? "AS" : "" );
  22111.          printf( "%3s", iscntrl( ch )  ? "C"  : "" );
  22112.          printf( "%3s", isdigit( ch )  ? "D"  : "" );
  22113.          printf( "%3s", isgraph( ch )  ? "G"  : "" );
  22114.          printf( "%3s", islower( ch )  ? "L"  : "" );
  22115.          printf( "%3s", ispunct( ch )  ? "PU" : "" );
  22116.          printf( "%3s", isspace( ch )  ? "S"  : "" );
  22117.          printf( "%3s", isprint( ch )  ? "PR" : "" );
  22118.          printf( "%3s", isupper( ch )  ? "U"  : "" );
  22119.          printf( "%3s", isxdigit( ch ) ? "X"  : "" );
  22120.          printf( "\n" );
  22121.       }
  22122.    }
  22123.  
  22124.  
  22125.  Output
  22126.  
  22127.  
  22128.  
  22129.    00          AS  C
  22130.    01          AS  C
  22131.    02          AS  C
  22132.    .
  22133.    .
  22134.    .
  22135.    38  8  AN    AS     D  G          PR     X
  22136.    39  9  AN    AS     D  G          PR     X
  22137.    3a  :        AS        G    PU    PR
  22138.    3b  ;        AS        G    PU    PR
  22139.    3c  <        AS        G    PU    PR
  22140.    3d  =        AS        G    PU    PR
  22141.    3e  >        AS        G    PU    PR
  22142.    3f  ?        AS        G    PU    PR
  22143.    40  @        AS        G    PU    PR
  22144.    41  A  AN  A AS        G          PR  U  X
  22145.    42  B  AN  A AS        G          PR  U  X
  22146.    .
  22147.    .
  22148.    .
  22149.  
  22150.  
  22151.  
  22152.  
  22153.  
  22154.  isatty
  22155.  ────────────────────────────────────────────────────────────────────────────
  22156.  
  22157.  
  22158.  Description
  22159.  
  22160.  Checks for a character device.
  22161.  
  22162.  #include <io.h>                   Required only for function declarations
  22163.  
  22164.    int isatty( int handle );
  22165.  
  22166.  handle                            Handle referring to device to be tested
  22167.  
  22168.  
  22169.  Remarks
  22170.  
  22171.  The isatty function determines whether handle is associated with a character
  22172.  device (a terminal, console, printer, or serial port).
  22173.  
  22174.  
  22175.  Return Value
  22176.  
  22177.  The isatty function returns a nonzero value if the device is a character
  22178.  device. Otherwise, the return value is 0.
  22179.  
  22180.  
  22181.  Compatibility
  22182.  
  22183.   ANSI   DOS   OS/2   UNIX   XENIX
  22184.  
  22185.  
  22186.  
  22187.  Example
  22188.  
  22189.    /* ISATTY.C: This program checks to see whether stdout has been
  22190.     * redirected to a file.
  22191.     */
  22192.  
  22193.    #include <stdio.h>
  22194.    #include <io.h>
  22195.  
  22196.    void main()
  22197.    {
  22198.       if( isatty( fileno( stdout ) ) )
  22199.          printf( "stdout has not been redirected to a file\n" );
  22200.       else
  22201.          printf( "stdout has been redirected to a file\n");
  22202.    }
  22203.  
  22204.  
  22205.  Output
  22206.  
  22207.  
  22208.  
  22209.    stdout has not been redirected to a file
  22210.  
  22211.  
  22212.  
  22213.  
  22214.  
  22215.  itoa
  22216.  ────────────────────────────────────────────────────────────────────────────
  22217.  
  22218.  
  22219.  Description
  22220.  
  22221.  Converts an integer to a string.
  22222.  
  22223.  #include <stdlib.h>               Required only for function declarations
  22224.  
  22225.    char *itoa( int value, char *string, int radix );
  22226.  
  22227.  value                             Number to be converted
  22228.  
  22229.  string                            String result
  22230.  
  22231.  radix                             Base of value
  22232.  
  22233.  
  22234.  Remarks
  22235.  
  22236.  The itoa function converts the digits of the given value argument to a
  22237.  null-terminated character string and stores the result (up to 17 bytes) in
  22238.  string. The radix argument specifies the base of value; it must be in the
  22239.  range 2-36. If radix equals 10 and value is negative, the first character of
  22240.  the stored string is the minus sign (-).
  22241.  
  22242.  
  22243.  Return Value
  22244.  
  22245.  The itoa function returns a pointer to string. There is no error return.
  22246.  
  22247.  
  22248.  Compatibility
  22249.  
  22250.   ANSI   DOS   OS/2   UNIX   XENIX
  22251.  
  22252.  
  22253.  
  22254.  See Also
  22255.  
  22256.  ltoa, ultoa
  22257.  
  22258.  
  22259.  Example
  22260.  
  22261.    /* ITOA.C: This program converts integers of various sizes to strings
  22262.     * in various radixes.
  22263.     */
  22264.  
  22265.    #include <stdlib.h>
  22266.    #include <stdio.h>
  22267.  
  22268.    void main()
  22269.    {
  22270.       char buffer[20];
  22271.       int  i = 3445;
  22272.       long l = -344115L;
  22273.       unsigned long ul = 1234567890UL;
  22274.  
  22275.    itoa( i, buffer, 10 );
  22276.       printf( "String of integer %d (radix 10): %s\n", i, buffer );
  22277.       itoa( i, buffer, 16 );
  22278.       printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
  22279.       itoa( i, buffer, 2  );
  22280.       printf( "String of integer %d (radix 2): %s\n", i, buffer );
  22281.  
  22282.       ltoa( l, buffer, 16 );
  22283.       printf( "String of long int %ld (radix 16): 0x%s\n", l, buffer );
  22284.  
  22285.       ultoa( ul, buffer, 16 );
  22286.       printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul, buffer );
  22287.    }
  22288.  
  22289.  
  22290.  Output
  22291.  
  22292.  
  22293.  
  22294.    String of integer 3445 (radix 10): 3445
  22295.    String of integer 3445 (radix 16): 0xd75
  22296.    String of integer 3445 (radix 2): 110101110101
  22297.    String of long int -344115 (radix 16): 0xfffabfcd
  22298.    String of unsigned long 1234567890 (radix 16): 0x499602d2
  22299.  
  22300.  
  22301.  
  22302.  
  22303.  
  22304.  kbhit
  22305.  ────────────────────────────────────────────────────────────────────────────
  22306.  
  22307.  
  22308.  Description
  22309.  
  22310.  Checks the console for keyboard input.
  22311.  
  22312.  #include <conio.h>                Required only for function declarations
  22313.  
  22314.    int kbhit( void );
  22315.  
  22316.  
  22317.  Remarks
  22318.  
  22319.  The kbhit function checks the console for a recent keystroke. If the
  22320.  function returns a nonzero value, a keystroke is waiting in the buffer. The
  22321.  program can then call getch or getche to get the keystroke.
  22322.  
  22323.  
  22324.  Return Value
  22325.  
  22326.  The kbhit function returns a nonzero value if a key has been pressed.
  22327.  Otherwise, it re-turns 0.
  22328.  
  22329.  
  22330.  Compatibility
  22331.  
  22332.   ANSI   DOS   OS/2   UNIX   XENIX
  22333.  
  22334.  
  22335.  
  22336.  Example
  22337.  
  22338.    /* KBHIT.C: This program loops until the user presses a key.
  22339.     * If kbhit returns nonzero, a keystroke is waiting in the buffer.
  22340.     * The program can call getch or getche to get the keystroke.
  22341.     */
  22342.  
  22343.    #include <conio.h>
  22344.    #include <stdio.h>
  22345.  
  22346.    void main()
  22347.    {
  22348.       /* Display message until key is pressed. */
  22349.       while( !kbhit() )
  22350.          cputs( "Hit me!! " );
  22351.  
  22352.       /* Use getch to throw key away. */
  22353.       printf( "\nKey struck was '%c'\n", getch() );
  22354.       getch();
  22355.    }
  22356.  
  22357.  
  22358.  Output
  22359.  
  22360.  
  22361.  
  22362.    Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!!
  22363.    Key struck was 'k'
  22364.  
  22365.  
  22366.  
  22367.  
  22368.  
  22369.  labs
  22370.  ────────────────────────────────────────────────────────────────────────────
  22371.  
  22372.  
  22373.  Description
  22374.  
  22375.  Calculates the absolute value of a long integer.
  22376.  
  22377.  #include <stdlib.h>               Required only for function declarations
  22378.  
  22379.  #include <math.h>
  22380.  
  22381.    long labs( long n );
  22382.  
  22383.  n                                 Long-integer value
  22384.  
  22385.  
  22386.  Remarks
  22387.  
  22388.  The labs function produces the absolute value of its long-integer argument
  22389.  n.
  22390.  
  22391.  
  22392.  Return Value
  22393.  
  22394.  The labs function returns the absolute value of its argument. There is no
  22395.  error return.
  22396.  
  22397.  
  22398.  Compatibility
  22399.  
  22400.   ANSI   DOS   OS/2   UNIX   XENIX
  22401.  
  22402.  
  22403.  
  22404.  See Also
  22405.  
  22406.  abs, cabs, fabs
  22407.  
  22408.  
  22409.  Example
  22410.  
  22411.    /* ABS.C: This program computes and displays the absolute values of
  22412.     * several numbers.
  22413.     */
  22414.  
  22415.    #include <stdio.h>
  22416.    #include <math.h>
  22417.    #include <stdlib.h>
  22418.  
  22419.    void main()
  22420.    {
  22421.       int    ix = -4, iy;
  22422.       long   lx = -41567L, ly;
  22423.       double dx = -3.141593, dy;
  22424.  
  22425.       iy = abs( ix );
  22426.       printf( "The absolute value of %d is %d\n", ix, iy);
  22427.  
  22428.       ly = labs( lx );
  22429.       printf( "The absolute value of %ld is %ld\n", lx, ly);
  22430.  
  22431.       dy = fabs( dx );
  22432.       printf( "The absolute value of %f is %f\n", dx, dy );
  22433.    }
  22434.  
  22435.  
  22436.  Output
  22437.  
  22438.  
  22439.  
  22440.    The absolute value of -4 is 4
  22441.    The absolute value of -41567 is 41567
  22442.    The absolute value of -3.141593 is 3.141593
  22443.  
  22444.  
  22445.  
  22446.  
  22447.  
  22448.  ldexp, ldexpl
  22449.  ────────────────────────────────────────────────────────────────────────────
  22450.  
  22451.  
  22452.  Description
  22453.  
  22454.  Compute a real number from the mantissa and exponent.
  22455.  
  22456.    #include <math.h>
  22457.  
  22458.    double ldexp( double x, int exp );
  22459.  
  22460.    long double ldexpl( long double x, int exp );
  22461.  
  22462.  x                                 Floating-point value
  22463.  
  22464.  exp                               Integer exponent
  22465.  
  22466.  
  22467.  Remarks
  22468.  
  22469.  The ldexp and ldexpl functions calculate the value of x * 2exp.
  22470.  
  22471.  
  22472.  Return Value
  22473.  
  22474.  The ldexp and ldexpl functions return x * 2exp. If an overflow results, the
  22475.  functions return ± HUGE_VAL (depending on the sign of x) and set errno to
  22476.  ERANGE.
  22477.  
  22478.  The ldexpl function uses the 80-bit, 10-byte coprocessor form of arguments
  22479.  and return values. See the reference page on the long double functions for
  22480.  more details on this data type.
  22481.  
  22482.  
  22483.  Compatibility
  22484.  
  22485.  ldexp
  22486.  
  22487.   ANSI   DOS   OS/2   UNIX   XENIX
  22488.  
  22489.  
  22490.  ldexpl
  22491.  
  22492.   ANSI   DOS   OS/2   UNIX   XENIX
  22493.  
  22494.  
  22495.  
  22496.  See Also
  22497.  
  22498.  frexp, modf
  22499.  
  22500.  
  22501.  Example
  22502.  
  22503.    /* LDEXP.C */
  22504.    #include <math.h>
  22505.    #include <stdio.h>
  22506.  
  22507.    void main()
  22508.    {
  22509.       double x = 4.0, y;
  22510.       int p = 3;
  22511.  
  22512.       y = ldexp( x, p );
  22513.       printf( "%2.1f times two to the power of %d is %2.1f\n", x, p, y );
  22514.    }
  22515.  
  22516.  
  22517.  Output
  22518.  
  22519.  
  22520.  
  22521.    4.0 times two to the power of 3 is 32.0
  22522.  
  22523.  
  22524.  
  22525.  
  22526.  
  22527.  ldiv
  22528.  ────────────────────────────────────────────────────────────────────────────
  22529.  
  22530.  
  22531.  Description
  22532.  
  22533.  Computes the quotient and remainder of a long integer.
  22534.  
  22535.    #include <stdlib.h>
  22536.  
  22537.    ldiv_t ldiv ( long int numer, long int denom );
  22538.  
  22539.  numer                             Numerator
  22540.  
  22541.  denom                             Denominator
  22542.  
  22543.  
  22544.  Remarks
  22545.  
  22546.  The ldiv function divides numer by denom, computing the quotient and the
  22547.  remainder. The sign of the quotient is the same as that of the mathematical
  22548.  quotient. Its absolute value is the largest integer that is less than the
  22549.  absolute value of the mathematical quotient. If the denominator is 0, the
  22550.  program will terminate with an error message.
  22551.  
  22552.  The ldiv function is similar to the div function, with the difference being
  22553.  that the arguments and the members of the returned structure are all of type
  22554.  long int.
  22555.  
  22556.  The ldiv_t structure, defined in STDLIB.H, contains the following elements:
  22557.  
  22558.  
  22559.  Element                           Description
  22560.  ────────────────────────────────────────────────────────────────────────────
  22561.  long int quot                     Quotient
  22562.  
  22563.  long int rem                      Remainder
  22564.  
  22565.  
  22566.  Return Value
  22567.  
  22568.  The ldiv function returns a structure of type ldiv_t, comprising both the
  22569.  quotient and the remainder.
  22570.  
  22571.  
  22572.  Compatibility
  22573.  
  22574.   ANSI   DOS   OS/2   UNIX   XENIX
  22575.  
  22576.  
  22577.  
  22578.  See Also
  22579.  
  22580.  div
  22581.  
  22582.  
  22583.  Example
  22584.  
  22585.    /* LDIV.C: This program takes two long integers as command-line
  22586.     * arguments and displays the results of the integer division.
  22587.     */
  22588.  
  22589.    #include <stdlib.h>
  22590.    #include <math.h>
  22591.    #include <stdio.h>
  22592.  
  22593.    void main()
  22594.    {
  22595.       long x = 5149627, y = 234879;
  22596.       ldiv_t div_result;
  22597.  
  22598.       div_result = ldiv( x, y );
  22599.       printf( "For %ld / %ld, the quotient is %ld, and the remainder is
  22600.  %ld\n",
  22601.               x, y, div_result.quot, div_result.rem );
  22602.    }
  22603.  
  22604.  
  22605.  Output
  22606.  
  22607.  
  22608.  
  22609.    For 5149627 / 234879, the quotient is 21, and the remainder is 217168
  22610.  
  22611.  
  22612.  
  22613.  
  22614.  
  22615.  lfind
  22616.  ────────────────────────────────────────────────────────────────────────────
  22617.  
  22618.  
  22619.  Description
  22620.  
  22621.  Performs a linear search for the specified key.
  22622.  
  22623.  #include <search.h>               Required only for function declarations
  22624.  
  22625.    char *lfind( const void *key, const void *base, unsigned int *num,
  22626.    unsigned int width,
  22627.    int ( *compare )( const void *elem1, const void *elem2 ) );
  22628.  
  22629.  key                               Object to search for
  22630.  
  22631.  base                              Pointer to base of search data
  22632.  
  22633.  num                               Number of array elements
  22634.  
  22635.  width                             Width of array elements
  22636.  
  22637.  compare( )                        Pointer to comparison routine
  22638.  
  22639.  elem1                             Pointer to the key for the search
  22640.  
  22641.  elem2                             Pointer to the array element to be
  22642.                                    compared with the key
  22643.  
  22644.  
  22645.  Remarks
  22646.  
  22647.  The lfind function performs a linear search for the value key in an array of
  22648.  num elements; each element is width bytes in size. (Unlike bsearch, lfind
  22649.  does not require the array to be sorted.) The base argument is a pointer to
  22650.  the base of the array to be searched.
  22651.  
  22652.  The compare argument is a pointer to a user-supplied routine that compares
  22653.  two array elements and then returns a value specifying their relationship.
  22654.  The lfind function calls the compare routine one or more times during the
  22655.  search, passing pointers to two array elements on each call. This routine
  22656.  must compare the elements, then return one of the following values:
  22657.  
  22658.  Value                             Meaning
  22659.  ────────────────────────────────────────────────────────────────────────────
  22660.  Nonzero                           Elements are different
  22661.  
  22662.  0                                 Elements are identical
  22663.  
  22664.  
  22665.  Return Value
  22666.  
  22667.  If the key is found, lfind returns a pointer to the element of the array at
  22668.  base that matches key. If the key is not found, lfind returns NULL.
  22669.  
  22670.  
  22671.  Compatibility
  22672.  
  22673.   ANSI   DOS   OS/2   UNIX   XENIX
  22674.  
  22675.  
  22676.  
  22677.  See Also
  22678.  
  22679.  bsearch, lsearch, qsort
  22680.  
  22681.  
  22682.  Example
  22683.  
  22684.    /* LFIND.C: This program uses lfind to search for the word "hello"
  22685.     * in the command-line arguments.
  22686.     */
  22687.  
  22688.    #include <search.h>
  22689.    #include <string.h>
  22690.    #include <stdio.h>
  22691.  
  22692.    int compare( char **arg1, char **arg2 );
  22693.  
  22694.    void main( int argc, char **argv )
  22695.    {
  22696.       char **result;
  22697.       char *key = "hello";
  22698.  
  22699.       result = (char **)lfind( (char *)&key, (char *)argv,
  22700.                                &argc, sizeof( char * ), compare );
  22701.       if( result )
  22702.          printf( "%s found\n", *result );
  22703.       else
  22704.          printf( "hello not found!\n" );
  22705.    }
  22706.  
  22707.    int compare(char ** arg1, char **arg2 )
  22708.    {
  22709.       return( strcmpi( *arg1, *arg2 ) );
  22710.    }
  22711.  
  22712.  
  22713.  Output
  22714.  
  22715.  
  22716.  
  22717.  
  22718.  
  22719.  
  22720.    [C:\LIBREF] lfind What if I said Hello world
  22721.    Hello found
  22722.  
  22723.  
  22724.  
  22725.  
  22726.  
  22727.  _lineto Functions
  22728.  ────────────────────────────────────────────────────────────────────────────
  22729.  
  22730.  
  22731.  Description
  22732.  
  22733.  Draw lines to specified points.
  22734.  
  22735.    #include <graph.h>
  22736.  
  22737.    short _far _lineto( short x, short y );
  22738.  
  22739.    short _far _lineto_w( double wx, double wy );
  22740.  
  22741.  x, y                              End point
  22742.  
  22743.  wx, wy                            End point
  22744.  
  22745.  
  22746.  Remarks
  22747.  
  22748.  The functions in the _lineto family draw a line from the current graphics
  22749.  position up to and including the destination point. The destination point
  22750.  for the _lineto function is given by the view-coordinate point (x, y). The
  22751.  destination point for the _lineto_w function is given by the
  22752.  window-coordinate point (wx, wy).
  22753.  
  22754.  The line is drawn using the current color, logical write mode, and line
  22755.  style. If no error occurs, _lineto sets the current graphics position to the
  22756.  view-coordinate point (x, y); _lineto_w sets the current position to the
  22757.  window-coordinate point (wx, wy).
  22758.  
  22759.  If you use _floodfill to fill in a closed figure drawn with _lineto calls,
  22760.  the figure must be drawn with a solid line-style pattern.
  22761.  
  22762.  
  22763.  Return Value
  22764.  
  22765.  The _lineto and _lineto_w routines return a nonzero value if anything is
  22766.  drawn; otherwise, they return 0.
  22767.  
  22768.  
  22769.  Compatibility
  22770.  
  22771.   ANSI   DOS   OS/2   UNIX   XENIX
  22772.  
  22773.  
  22774.  
  22775.  See Also
  22776.  
  22777.  _getcurrentposition functions,  _moveto functions,  _setlinestyle
  22778.  
  22779.  
  22780.  Example
  22781.  
  22782.    /* MOVETO.C: This program draws line segments of different colors. */
  22783.  
  22784.    #include <graph.h>
  22785.    #include <stdlib.h>
  22786.    #include <conio.h>
  22787.  
  22788.    void main()
  22789.    {
  22790.       short x, y, xinc, yinc, color = 1;
  22791.       struct videoconfig v;
  22792.  
  22793.       /* Find a valid graphics mode. */
  22794.       if( !_setvideomode( _MAXCOLORMODE ) )
  22795.          exit( 1 );
  22796.       _getvideoconfig( &v );
  22797.       xinc = v.numxpixels / 50;
  22798.       yinc = v.numypixels / 50;
  22799.  
  22800.       for( x = 0, y = v.numypixels - 1; x < v.numxpixels; x += xinc, y -=
  22801.  yinc )
  22802.       {
  22803.          _setcolor( color++ % 16 );
  22804.          _moveto( x, 0 );
  22805.          _lineto( 0, y );
  22806.       }
  22807.       getch();
  22808.  
  22809.       _setvideomode( _DEFAULTMODE );
  22810.    }
  22811.  
  22812.  
  22813.  
  22814.  
  22815.  
  22816.  localeconv
  22817.  ────────────────────────────────────────────────────────────────────────────
  22818.  
  22819.  
  22820.  Description
  22821.  
  22822.  Gets detailed information on locale settings.
  22823.  
  22824.    #include <locale.h>
  22825.  
  22826.    struct lconv *localeconv( void );
  22827.  
  22828.  
  22829.  Remarks
  22830.  
  22831.  The localeconv function gets detailed information on the locale-specific
  22832.  settings for numeric formatting of the program's current locale. This
  22833.  information is stored in a structure of type lconv.
  22834.  
  22835.  The lconv structure, defined in LOCALE.H, contains the following elements:
  22836.  
  22837.  Element                           Description
  22838.  ────────────────────────────────────────────────────────────────────────────
  22839.  char *decimal_point               Decimal-point character for nonmonetary
  22840.                                    quantities.
  22841.  
  22842.  char *thousands_sep               Character used to separate groups of
  22843.                                    digits to the left of the decimal point
  22844.                                    for non-monetary quantities.
  22845.  
  22846.  char *grouping                    Size of each group of digits in
  22847.                                    non-monetary quantities.
  22848.  
  22849.  char *int_curr_symbol             International currency symbol for the
  22850.                                    current locale. The first three
  22851.                                    characters specify the alphabetic
  22852.                                    international currency symbol as defined
  22853.                                    in the ISO 4217 Codes for the
  22854.                                    Representation of Currency and Funds
  22855.                                    standard. The fourth character
  22856.                                    (immediately preceding the null
  22857.                                    character) is used to separate the
  22858.                                    international currency symbol from the
  22859.                                    monetary quantity.
  22860.  
  22861.  char *currency_symbol             Local currency symbol for the current
  22862.                                    locale.
  22863.  
  22864.  char *mon_decimal_point           Decimal-point character for monetary
  22865.                                    quantities.
  22866.  
  22867.  char *mon_thousands_sep           Separator for groups of digits to the
  22868.                                    left of the decimal place in monetary
  22869.                                    quantities.
  22870.  
  22871.  char *mon_grouping                Size of each group of digits in monetary
  22872.                                    quantities.
  22873.  
  22874.  char *positive_sign               String denoting sign for nonnegative
  22875.                                    monetary quantities.
  22876.  
  22877.  char *negative_sign               String denoting sign for negative
  22878.                                    monetary
  22879.                                    quantities.
  22880.  
  22881.  char int_frac_digits              Number of digits to the right of the
  22882.                                    decimal point in internationally
  22883.                                    formatted
  22884.                                    monetary quantities.
  22885.  
  22886.  char frac_digits                  Number of digits to the right of the
  22887.                                    decimal point in formatted monetary
  22888.                                    quantities.
  22889.  
  22890.  char p_cs_precedes                Set to 1 if the currency symbol precedes
  22891.                                    the value for a nonnegative formatted
  22892.                                    monetary quantity. Set to 0 if the
  22893.                                    symbol follows the value.
  22894.  
  22895.  char p_sep_by_space               Set to 1 if the currency symbol is
  22896.                                    separated by a space from the value for
  22897.                                    a non-negative formatted monetary
  22898.                                    quantity. Set to 0 if there is no space
  22899.                                    separation.
  22900.  
  22901.  char n_cs_precedes                Set to 1 if the currency symbol precedes
  22902.                                    the value for a negative formatted
  22903.                                    monetary quantity. Set to 0 if the
  22904.                                    symbol succeeds the value.
  22905.  
  22906.  char n_sep_by_space               Set to 1 if the currency symbol is
  22907.                                    separated by a space from the value for
  22908.                                    a negative formatted monetary quantity.
  22909.                                    Set to 0 if there is no space
  22910.                                    separation.
  22911.  
  22912.  char p_sign_posn                  Position of positive sign in nonnegative
  22913.                                    formatted monetary quantities.
  22914.  
  22915.  char n_sign_posn                  Position of positive sign in negative
  22916.                                    formatted monetary quantities.
  22917.  
  22918.  The elements of grouping and mon_grouping are interpreted according to the
  22919.  following rules:
  22920.  
  22921.  Value                             Interpretation
  22922.  ────────────────────────────────────────────────────────────────────────────
  22923.  CHAR_MAX                          No further grouping is to be performed.
  22924.  
  22925.  0                                 The previous element is to be repeatedly
  22926.                                    used for the remainder of the digits.
  22927.  
  22928.  n                                 The integer value n is the number of
  22929.                                    digits that make up the current group.
  22930.                                    The next element is examined to
  22931.                                    determine the size of the next group of
  22932.                                    digits before the current group.
  22933.  
  22934.  The values for p_sign_posn and n_sign_posn are interpreted according to the
  22935.  following rules:
  22936.  
  22937.  Value                             Interpretation
  22938.  ────────────────────────────────────────────────────────────────────────────
  22939.  0                                 Parentheses surround the quantity and
  22940.                                    currency symbol
  22941.  
  22942.  1                                 Sign string precedes the quantity and
  22943.                                    currency symbol
  22944.  
  22945.  2                                 Sign string follows the quantity and
  22946.                                    currency symbol
  22947.  
  22948.  3                                 Sign string immediately precedes the
  22949.                                    currency symbol
  22950.  
  22951.  4                                 Sign string immediately follows the
  22952.                                    currency symbol
  22953.  
  22954.  
  22955.  Return Value
  22956.  
  22957.  The localeconv function returns a pointer to a structure of lconv type.
  22958.  Calls to the setlocale function with category values of LC_ALL, LC_MONETARY,
  22959.  or LC_NUMERIC will overwrite the contents of the structure.
  22960.  
  22961.  
  22962.  Compatibility
  22963.  
  22964.   ANSI   DOS   OS/2   UNIX   XENIX
  22965.  
  22966.  
  22967.  
  22968.  See Also
  22969.  
  22970.  setlocale, strcoll, strftime, strxfrm
  22971.  
  22972.  
  22973.  
  22974.  
  22975.  
  22976.  localtime
  22977.  ────────────────────────────────────────────────────────────────────────────
  22978.  
  22979.  
  22980.  Description
  22981.  
  22982.  Converts a time value and corrects for the local time zone.
  22983.  
  22984.    #include <time.h>
  22985.  
  22986.    struct tm *localtime( const time_t *timer );
  22987.  
  22988.  timer                             Pointer to stored time
  22989.  
  22990.  
  22991.  Remarks
  22992.  
  22993.  The localtime function converts a time stored as a time_t value and stores
  22994.  the result in a structure of type tm. The long value timer represents the
  22995.  seconds elapsed since 00:00:00, January 1, 1970, Greenwich mean time; this
  22996.  value is usually obtained from the time function.
  22997.  
  22998.  The fields of the structure type tm store the following values:
  22999.  
  23000.  Element                           Value Stored
  23001.  ────────────────────────────────────────────────────────────────────────────
  23002.  int tm_sec                        Seconds
  23003.  
  23004.  int tm_min                        Minutes
  23005.  
  23006.  int tm_hour                       Hours (0 -24)
  23007.  
  23008.  int tm_mday                       Day of month (1-31)
  23009.  
  23010.  int tm_mon                        Month (0 -11; January = 0)
  23011.  
  23012.  int tm_year                       Year (current year minus 1900)
  23013.  
  23014.  int tm_wday                       Day of week (0 - 6; Sunday = 0)
  23015.  
  23016.  int tm_yday                       Day of year (0 -365; January 1 = 0)
  23017.  
  23018.  int tm_isdst                      Nonzero if daylight saving time is in
  23019.                                    effect, otherwise 0
  23020.  
  23021.  Note that the gmtime, mktime, and localtime functions use a single
  23022.  statically allocated tm structure for the conversion. Each call to one of
  23023.  these routines destroys the result of the previous call.
  23024.  
  23025.  The localtime function makes corrections for the local time zone if the user
  23026.  first sets the environment variable TZ. When TZ is set, three other
  23027.  environment variables (timezone, daylight, and tzname) are automatically set
  23028.  as well. See tzset for a description of these variables.
  23029.  
  23030.  The TZ variable is not part of the ANSI standard definition of localtime but
  23031.  is a Microsoft extension.
  23032.  
  23033.  
  23034.  Return Value
  23035.  
  23036.  The localtime function returns a pointer to the structure result. DOS and
  23037.  OS/2 do not accommodate dates prior to 1980. If the value in timer
  23038.  represents a date prior to January 1, 1980, the function returns NULL.
  23039.  
  23040.  
  23041.  Compatibility
  23042.  
  23043.   ANSI   DOS   OS/2   UNIX   XENIX
  23044.  
  23045.  
  23046.  
  23047.  See Also
  23048.  
  23049.  asctime, ctime, ftime, gmtime, time, tzset
  23050.  
  23051.  
  23052.  Example
  23053.  
  23054.    /* LOCALTIM.C: This program uses time to get the current time and
  23055.     * then uses localtime to convert this time to a structure representing
  23056.     * the local time. The program converts the result from a 24-hour clock
  23057.     * to a 12-hour clock and determines the proper extension (AM or PM).
  23058.     */
  23059.  
  23060.    #include <stdio.h>
  23061.    #include <string.h>
  23062.    #include <time.h>
  23063.  
  23064.    void main()
  23065.    {
  23066.       struct tm *newtime;
  23067.       char  am_pm[] = "AM";
  23068.       time_t long_time;
  23069.  
  23070.       time( &long_time );                 /* Get time as long integer. */
  23071.       newtime = localtime( &long_time );  /* Convert to local time. */
  23072.  
  23073.       if( newtime->tm_hour < 12 )         /* Set up extension. */
  23074.          strcpy( am_pm, "AM" );
  23075.       if( newtime->tm_hour > 12 )         /* Convert from 24-hour */
  23076.          newtime->tm_hour -=12;           /*   to 12-hour clock.  */
  23077.  
  23078.       printf( "%.19s %s\n", asctime( newtime ), am_pm );
  23079.    }
  23080.  
  23081.  
  23082.  Output
  23083.  
  23084.  
  23085.  
  23086.    Fri Jun 16 06:27:02 AM
  23087.  
  23088.  
  23089.  
  23090.  
  23091.  
  23092.  locking
  23093.  ────────────────────────────────────────────────────────────────────────────
  23094.  
  23095.  
  23096.  Description
  23097.  
  23098.  Locks or unlocks bytes of a file.
  23099.  
  23100.    #include <sys\locking.h>
  23101.  
  23102.  #include <io.h>                   Required only for function declarations
  23103.  
  23104.    int locking( int handle, int mode, long nbytes );
  23105.  
  23106.  handle                            File handle
  23107.  
  23108.  mode                              File-locking mode
  23109.  
  23110.  nbytes                            Number of bytes to lock
  23111.  
  23112.  
  23113.  Remarks
  23114.  
  23115.  The locking function locks or unlocks nbytes bytes of the file specified by
  23116.  handle. Locking bytes in a file prevents access to those bytes by other
  23117.  processes. All locking or unlocking begins at the current position of the
  23118.  file pointer and proceeds for the next nbytes bytes. It is possible to lock
  23119.  bytes past the end of the file.
  23120.  
  23121.  The mode argument specifies the locking action to be performed. It must be
  23122.  one of the following manifest constants:
  23123.  
  23124.  Constant                          Action
  23125.  ────────────────────────────────────────────────────────────────────────────
  23126.  LK_LOCK                           Locks the specified bytes. If the bytes
  23127.                                    cannot be locked, immediately tries
  23128.                                    again after 1 second. If, after 10
  23129.                                    attempts, the bytes cannot be locked,
  23130.                                    returns an error.
  23131.  
  23132.  LK_NBLCK                          Locks the specified bytes. If bytes
  23133.                                    cannot be locked, returns an error.
  23134.  
  23135.  LK_NBRLCK                         Same as LK_NBLCK.
  23136.  
  23137.  LK_RLCK                           Same as LK_LOCK.
  23138.  
  23139.  LK_UNLCK                          Unlocks the specified bytes. (The bytes
  23140.                                    must have been previously locked.)
  23141.  
  23142.  More than one region of a file can be locked, but no overlapping regions are
  23143.  allowed.
  23144.  
  23145.  When a region of a file is being unlocked, it must correspond to a region
  23146.  that was previously locked. The locking function does not merge adjacent
  23147.  regions; if two locked regions are adjacent, each region must be unlocked
  23148.  separately.
  23149.  
  23150.  Regions should be locked only briefly and should be unlocked before closing
  23151.  a file or exiting the program.
  23152.  
  23153.  The locking function should be used only under OS/2 or under DOS versions
  23154.  3.0 and later; it has no effect under earlier versions of DOS. Also, file
  23155.  sharing must be loaded to use the locking function. Note that under DOS
  23156.  versions 3.0 and 3.1, the files locked by parent processes may become
  23157.  unlocked when child processes exit.
  23158.  
  23159.  
  23160.  Return Value
  23161.  
  23162.  The locking function returns 0 if successful. A return value of -1 indicates
  23163.  failure, and errno is set to one of the following values:
  23164.  
  23165.  Value                             Meaning
  23166.  ────────────────────────────────────────────────────────────────────────────
  23167.  EACCES                            Locking violation (file already locked
  23168.                                    or unlocked).
  23169.  
  23170.  EBADF                             Invalid file handle.
  23171.  
  23172.  EDEADLOCK                         Locking violation. This is returned when
  23173.                                    the LK_LOCK or LK_RLCK flag is specified
  23174.                                    and the file cannot be locked after 10
  23175.                                    attempts.
  23176.  
  23177.  EINVAL                            An invalid argument was given to the
  23178.                                    function.
  23179.  
  23180.  
  23181.  Compatibility
  23182.  
  23183.   ANSI   DOS   OS/2   UNIX   XENIX
  23184.  
  23185.  
  23186.  
  23187.  See Also
  23188.  
  23189.  creat, open
  23190.  
  23191.  
  23192.  Example
  23193.  
  23194.    /* LOCKING.C: This program opens a file with sharing. It locks some
  23195.     * bytes before reading them, then unlocks them. Note that the program
  23196.     * works correctly only if the following conditions are met:
  23197.     *     - The file exists
  23198.     *     - The program is run under OS/2, under DOS 3.0 or later
  23199.     *       with file sharing installed (SHARE.COM or SHARE.EXE), or
  23200.     *       if a Microsoft Networks compatible network is running
  23201.     */
  23202.  
  23203.    #include <io.h>
  23204.    #include <sys\types.h>
  23205.    #include <sys\stat.h>
  23206.    #include <sys\locking.h>
  23207.    #include <share.h>
  23208.    #include <fcntl.h>
  23209.    #include <stdio.h>
  23210.    #include <stdlib.h>
  23211.  
  23212.    void main()
  23213.    {
  23214.       int  fh, numread;
  23215.       long pos, result;
  23216.       char buffer[40];
  23217.  
  23218.       /* Quit if can't open file or DOS version doesn't support sharing. */
  23219.       fh = sopen( "locking.c", O_RDWR, SH_DENYNO, S_IREAD | S_IWRITE );
  23220.       if( (fh == -1) || (_osmajor < 3) )
  23221.          exit( 1 );
  23222.  
  23223.       /* Lock some bytes and read them. Then unlock. */
  23224.       if( locking( fh, LK_NBLCK, 30L ) != -1 )
  23225.       {
  23226.          printf( "No one can change these bytes while I'm reading them\n" );
  23227.          numread = read( fh, buffer, 30 );
  23228.          printf( "%d bytes read: %.30s\n", numread, buffer );
  23229.          locking( fh, LK_UNLCK, 30L );
  23230.          printf( "Now I'm done. Do what you will with them\n" );
  23231.       }
  23232.       else
  23233.          perror( "Locking failed\n" );
  23234.  
  23235.       close( fh );
  23236.    }
  23237.  
  23238.  
  23239.  Output
  23240.  
  23241.  
  23242.  
  23243.    No one can change these bytes while I'm reading them
  23244.    30 bytes read: /* LOCKING.C: This program ope
  23245.    Now I'm done. Do what you will with them
  23246.  
  23247.  
  23248.  
  23249.  
  23250.  
  23251.  log Functions
  23252.  ────────────────────────────────────────────────────────────────────────────
  23253.  
  23254.  
  23255.  Description
  23256.  
  23257.  Calculate logarithms.
  23258.  
  23259.    #include <math.h>
  23260.  
  23261.    double log( double x );
  23262.  
  23263.    double log10( double x );
  23264.  
  23265.    long double logl( long double x );
  23266.  
  23267.    long double log10l( long double x );
  23268.  
  23269.  x                                 Value whose logarithm is to be found
  23270.  
  23271.  
  23272.  Remarks
  23273.  
  23274.  The log and log10 functions calculate the natural logarithm and the base-10
  23275.  logarithm, respectively, of x. The logl and log10l functions are the 80-bit
  23276.  counterparts and use the 80-bit, 10-byte coprocessor form of arguments and
  23277.  return values. See the reference page on the long double functions for more
  23278.  details on this data type.
  23279.  
  23280.  
  23281.  Return Value
  23282.  
  23283.  The log functions return the logarithm of the argument x. If x is negative,
  23284.  the functions print a DOMAIN error message to stderr, return the value
  23285.  -HUGE_VAL, and set errno to EDOM. If x is 0, the functions print a SING
  23286.  error message to stderr, return the value -HUGE_VAL, and set errno to
  23287.  ERANGE.
  23288.  
  23289.  Error handling can be modified by using the matherr or _matherrl routine.
  23290.  
  23291.  
  23292.  Compatibility
  23293.  
  23294.  log, log10
  23295.  
  23296.   ANSI   DOS   OS/2   UNIX   XENIX
  23297.  
  23298.  
  23299.  logl, log10l
  23300.  
  23301.   ANSI   DOS   OS/2   UNIX   XENIX
  23302.  
  23303.  
  23304.  
  23305.  See Also
  23306.  
  23307.  exp, matherr, pow functions
  23308.  
  23309.  
  23310.  Example
  23311.  
  23312.    /* LOG.C: This program uses log and log10 to calculate the natural
  23313.     * logarithm and the base-10 logarithm of 9,000.
  23314.     */
  23315.  
  23316.    #include <math.h>
  23317.    #include <stdio.h>
  23318.  
  23319.    void main()
  23320.    {
  23321.       double x = 9000.0;
  23322.       double y;
  23323.  
  23324.       y = log( x );
  23325.       printf( "log( %.2f ) = %f\n", x, y );
  23326.       y = log10( x );
  23327.       printf( "log10( %.2f ) = %f\n", x, y );
  23328.    }
  23329.  
  23330.  
  23331.  Output
  23332.  
  23333.  
  23334.  
  23335.    log( 9000.00 ) = 9.104980
  23336.    log10( 9000.00 ) = 3.954243
  23337.  
  23338.  
  23339.  
  23340.  
  23341.  
  23342.  long double Functions
  23343.  ────────────────────────────────────────────────────────────────────────────
  23344.  
  23345.  The 8087 family of numeric coprocessor chips supports the 80-bit precision
  23346.  floating-point data type. In Microsoft C, version 6.0, the long double
  23347.  functions, whose names end with l, map the C long double type into this
  23348.  80-bit, 10-byte form. Unlike the regular floating-point functions (such as
  23349.  acos), which return values of type double, these long double functions (such
  23350.  as acosl) return values of type long double. The long double functions also
  23351.  return their values on the coprocessor stack for all calling conventions.
  23352.  
  23353.  The long double type is also supported by the addition of the "L" prefix for
  23354.  a floating-point format specification in the printf and scanf family of
  23355.  functions.
  23356.  
  23357.  The long double versions are described on the reference pages for their
  23358.  regular counterparts. These are the regular C run-time math functions with
  23359.  corresponding long double equivalents:
  23360.  
  23361.  Regular Function                  Long Double Form
  23362.  ────────────────────────────────────────────────────────────────────────────
  23363.  acos                              acosl
  23364.  
  23365.  asin                              asinl
  23366.  
  23367.  atan                              atanl
  23368.  
  23369.  atan2                             atan2l
  23370.  
  23371.  atof                              _atold
  23372.  
  23373.  cabs                              cabsl
  23374.  
  23375.  ceil                              ceill
  23376.  
  23377.  cos                               cosl
  23378.  
  23379.  cosh                              coshl
  23380.  
  23381.  exp                               expl
  23382.  
  23383.  fabs                              fabsl
  23384.  
  23385.  floor                             floorl
  23386.  
  23387.  fmod                              fmodl
  23388.  
  23389.  frexp                             frexpl
  23390.  
  23391.  hypot                             hypotl
  23392.  
  23393.  ldexp                             ldexpl
  23394.  
  23395.  log                               logl
  23396.  
  23397.  log10                             log10l
  23398.  
  23399.  matherr                           _matherrl
  23400.  
  23401.  modf                              modfl
  23402.  
  23403.  pow                               powl
  23404.  
  23405.  sin                               sinl
  23406.  
  23407.  sinh                              sinhl
  23408.  
  23409.  sqrt                              sqrtl
  23410.  
  23411.  tan                               tanl
  23412.  
  23413.  tanh                              tanhl
  23414.  
  23415.  
  23416.  
  23417.  
  23418.  
  23419.  longjmp
  23420.  ────────────────────────────────────────────────────────────────────────────
  23421.  
  23422.  
  23423.  Description
  23424.  
  23425.  Restores stack environment and execution locale.
  23426.  
  23427.    #include <setjmp.h>
  23428.  
  23429.    void longjmp( jmp_buf env, int value );
  23430.  
  23431.  env                               Variable in which environment is stored
  23432.  
  23433.  value                             Value to be returned to setjmp call
  23434.  
  23435.  
  23436.  Remarks
  23437.  
  23438.  The longjmp function restores a stack environment and execution locale
  23439.  previously saved in env by setjmp. The setjmp and longjmp functions provide
  23440.  a way to execute a nonlocal goto; they are typically used to pass execution
  23441.  control to error handling or recovery code in a previously called routine
  23442.  without using the normal call and return conventions.
  23443.  
  23444.  A call to setjmp causes the current stack environment to be saved in env. A
  23445.  subsequent call to longjmp restores the saved environment and returns
  23446.  control to the point immediately following the corresponding setjmp call.
  23447.  Execution resumes as if value had just been returned by the setjmp call. The
  23448.  values of all variables (except register variables) that are accessible to
  23449.  the routine receiving control contain the values they had when longjmp was
  23450.  called. The values of register variables are unpredictable.
  23451.  
  23452.  The longjmp function must be called before the function that called setjmp
  23453.  returns. If longjmp is called after the function calling setjmp returns,
  23454.  unpredictable program behavior results.
  23455.  
  23456.  The value returned by setjmp must be nonzero. If value is passed as 0, the
  23457.  value 1 is substituted in the actual return.
  23458.  
  23459.  Observe the following three restrictions when using longjmp:
  23460.  
  23461.  
  23462.    1.  Do not assume that the values of the register variables will remain
  23463.        the same. The values of register variables in the routine calling
  23464.        setjmp may not be restored to the proper values after longjmp is
  23465.        executed.
  23466.  
  23467.    2.  Do not use longjmp to transfer control from within one overlay to
  23468.        within another. The overlay manager keeps the overlay in memory after
  23469.        a call to longjmp.
  23470.  
  23471.    3.  Do not use longjmp to transfer control out of an interrupt-handling
  23472.        routine unless the interrupt is caused by a floating-point exception.
  23473.        In this case, a program may return from an interrupt handler via
  23474.        longjmp if it first reinitializes the floating-point math package by
  23475.        calling _fpreset.
  23476.  
  23477.  
  23478.  
  23479.  Return Value
  23480.  
  23481.  None.
  23482.  
  23483.  
  23484.  Compatibility
  23485.  
  23486.   ANSI   DOS   OS/2   UNIX   XENIX
  23487.  
  23488.  
  23489.  
  23490.  See Also
  23491.  
  23492.  setjmp
  23493.  
  23494.  
  23495.  Example
  23496.  
  23497.  See the example for _fpreset.
  23498.  
  23499.  
  23500.  
  23501.  
  23502.  
  23503.  _lrotl, _lrotr
  23504.  ────────────────────────────────────────────────────────────────────────────
  23505.  
  23506.  
  23507.  Description
  23508.  
  23509.  Rotate bits to the left (_lrotl) or right (_lrotr).
  23510.  
  23511.    #include <stdlib.h>
  23512.  
  23513.    unsigned long _lrotl( unsigned long value, int shift );
  23514.  
  23515.    unsigned long _lrotr( unsigned long value, int shift );
  23516.  
  23517.  value                             Value to be rotated
  23518.  
  23519.  shift                             Number of bits to shift
  23520.  
  23521.  
  23522.  Remarks
  23523.  
  23524.  The _lrotl and _lrotr functions rotate value by shift bits. The _lrotl
  23525.  function rotates the value left. The _lrotr function rotates the value
  23526.  right. Both functions "wrap" bits rotated off one end of value to the other
  23527.  end.
  23528.  
  23529.  
  23530.  Return Value
  23531.  
  23532.  Both functions return the rotated value. There is no error return.
  23533.  
  23534.  
  23535.  Compatibility
  23536.  
  23537.   ANSI   DOS   OS/2   UNIX   XENIX
  23538.  
  23539.  
  23540.  
  23541.  See Also
  23542.  
  23543.  _rotl,  _rotr
  23544.  
  23545.  
  23546.  Example
  23547.  
  23548.    /* LROT.C */
  23549.    #include <stdlib.h>
  23550.    #include <stdio.h>
  23551.  
  23552.    void main()
  23553.    {
  23554.       unsigned long val = 0x0fac35791;
  23555.  
  23556.       printf( "0x%8.8lx rotated left eight times is 0x%8.8lx\n",
  23557.               val, _lrotl( val, 8 ) );
  23558.       printf( "0x%8.8lx rotated right four times is 0x%8.8lx\n",
  23559.               val, _lrotr( val, 4 ) );
  23560.    }
  23561.  
  23562.  
  23563.  Output
  23564.  
  23565.  
  23566.  
  23567.    0xfac35791 rotated left eight times is 0xc35791fa
  23568.    0xfac35791 rotated right four times is 0x1fac3579
  23569.  
  23570.  
  23571.  
  23572.  
  23573.  
  23574.  lsearch
  23575.  ────────────────────────────────────────────────────────────────────────────
  23576.  
  23577.  
  23578.  Description
  23579.  
  23580.  Performs a linear search for a value; adds to end of list if not found.
  23581.  
  23582.  #include <search.h>               Required only for function declarations
  23583.  
  23584.    char *lsearch( const void *key, const void *base, unsigned int *num,
  23585.    unsigned int width, int ( *compare )( const void *elem1, const void *elem2
  23586.  ) );
  23587.  
  23588.  key                               Object to search for
  23589.  
  23590.  base                              Pointer to base of search data
  23591.  
  23592.  num                               Number of elements
  23593.  
  23594.  width                             Width of elements
  23595.  
  23596.  compare                           Pointer to comparison routine
  23597.  
  23598.  elem1                             Pointer to the key for the search
  23599.  
  23600.  elem2                             Pointer to the array element to be
  23601.                                    compared with the key
  23602.  
  23603.  
  23604.  Remarks
  23605.  
  23606.  The lsearch function performs a linear search for the value key in an array
  23607.  of num elements, each of width bytes in size. (Unlike bsearch, lsearch does
  23608.  not require the array to be sorted.) The base argument is a pointer to the
  23609.  base of the array to be searched.
  23610.  
  23611.  If key is not found, lsearch adds it to the end of the array.
  23612.  
  23613.  The compare argument is a pointer to a user-supplied routine that compares
  23614.  two array elements and returns a value specifying their relationship. The
  23615.  lsearch function calls the compare routine one or more times during the
  23616.  search, passing pointers to two array elements on each call. This routine
  23617.  must compare the elements, then return one of the following values:
  23618.  
  23619.  Value                             Meaning
  23620.  ────────────────────────────────────────────────────────────────────────────
  23621.  Nonzero                           Elements are different
  23622.  
  23623.  0                                 Elements are identical
  23624.  
  23625.  
  23626.  Return Value
  23627.  
  23628.  If the key is found, lsearch returns a pointer to the element of the array
  23629.  at base that matches key. If the key is not found, lsearch returns a pointer
  23630.  to the newly added item at the end of the array.
  23631.  
  23632.  
  23633.  Compatibility
  23634.  
  23635.   ANSI   DOS   OS/2   UNIX   XENIX
  23636.  
  23637.  
  23638.  
  23639.  See Also
  23640.  
  23641.  bsearch, lfind
  23642.  
  23643.  
  23644.  Example
  23645.  
  23646.  See the example for lfind.
  23647.  
  23648.  
  23649.  
  23650.  
  23651.  
  23652.  lseek
  23653.  ────────────────────────────────────────────────────────────────────────────
  23654.  
  23655.  
  23656.  Description
  23657.  
  23658.  Moves a file pointer to the specified location.
  23659.  
  23660.  #include <io.h>                   Required only for function declarations
  23661.  
  23662.  #include <stdio.h>
  23663.  
  23664.    long lseek( int handle, long offset, int origin );
  23665.  
  23666.  handle                            Handle referring to open file
  23667.  
  23668.  offset                            Number of bytes from origin
  23669.  
  23670.  origin                            Initial position
  23671.  
  23672.  
  23673.  Remarks
  23674.  
  23675.  The lseek function moves the file pointer associated with handle to a new
  23676.  location that is offset bytes from origin. The next operation on the file
  23677.  occurs at the new location. The origin argument must be one of the following
  23678.  constants, which are defined in STDIO.H:
  23679.  
  23680.  Origin                            Definition
  23681.  ────────────────────────────────────────────────────────────────────────────
  23682.  SEEK_SET                          Beginning of file
  23683.  
  23684.  SEEK_CUR                          Current position of file pointer
  23685.  
  23686.  SEEK_END                          End of file
  23687.  
  23688.  The lseek function can be used to reposition the pointer anywhere in a file.
  23689.  The pointer can also be positioned beyond the end of the file. However, an
  23690.  attempt to position the pointer before the beginning of the file causes an
  23691.  error.
  23692.  
  23693.  
  23694.  Return Value
  23695.  
  23696.  The lseek function returns the offset, in bytes, of the new position from
  23697.  the beginning of the file. The function returns -1L to indicate an error and
  23698.  sets errno to one of the following values:
  23699.  
  23700.  Value                             Meaning
  23701.  ────────────────────────────────────────────────────────────────────────────
  23702.  EBADF                             Invalid file handle
  23703.  
  23704.  EINVAL                            Invalid value for origin, or position
  23705.                                    specified by offset is before the
  23706.                                    beginning of the file
  23707.  
  23708.  On devices incapable of seeking (such as terminals and printers), the return
  23709.  value is undefined.
  23710.  
  23711.  
  23712.  Compatibility
  23713.  
  23714.   ANSI   DOS   OS/2   UNIX   XENIX
  23715.  
  23716.  
  23717.  
  23718.  See Also
  23719.  
  23720.  fseek, tell
  23721.  
  23722.  
  23723.  Example
  23724.  
  23725.    /* LSEEK.C: This program first opens a file named LSEEK.C.
  23726.     * It then uses lseek to find the beginning of the file,
  23727.     * to find the current position in the file, and to find
  23728.     * the end of the file.
  23729.     */
  23730.  
  23731.    #include <io.h>
  23732.    #include <fcntl.h>
  23733.    #include <stdlib.h>
  23734.    #include <stdio.h>
  23735.  
  23736.  
  23737.    void main()
  23738.    {
  23739.       int fh;
  23740.       long pos;               /* Position of file pointer */
  23741.       char buffer[10];
  23742.  
  23743.       fh = open( "lseek.c", O_RDONLY );
  23744.  
  23745.       /* Seek the beginning of the file: */
  23746.       pos = lseek( fh, 0L, SEEK_SET );
  23747.       if( pos == -1L )
  23748.          perror( "lseek to beginning failed" );
  23749.       else
  23750.          printf( "Position for beginning of file seek = %ld\n", pos );
  23751.  
  23752.       /* Move file pointer a little */
  23753.       read( fh, buffer, 10 );
  23754.  
  23755.       /* Find current position: */
  23756.       pos = lseek( fh, 0L, SEEK_CUR );
  23757.       if( pos == -1L )
  23758.          perror( "lseek to current position failed" );
  23759.       else
  23760.          printf( "Position for current position seek = %ld\n", pos );
  23761.  
  23762.  
  23763.    /* Set the end of the file: */
  23764.       pos = lseek( fh, 0L, SEEK_END );
  23765.       if( pos == -1L )
  23766.          perror( "lseek to end failed" );
  23767.       else
  23768.          printf( "Position for end of file seek = %ld\n", pos );
  23769.  
  23770.       close( fh );
  23771.    }
  23772.  
  23773.  
  23774.  Output
  23775.  
  23776.  
  23777.  
  23778.    Position for beginning of file seek = 0
  23779.    Position for current position seek = 10
  23780.    Position for end of file seek = 1183
  23781.  
  23782.  
  23783.  
  23784.  
  23785.  
  23786.  ltoa
  23787.  ────────────────────────────────────────────────────────────────────────────
  23788.  
  23789.  
  23790.  Description
  23791.  
  23792.  Converts a long integer to a string.
  23793.  
  23794.  #include <stdlib.h>               Required only for function declarations
  23795.  
  23796.    char *ltoa( long value, char *string, int radix );
  23797.  
  23798.  value                             Number to be converted
  23799.  
  23800.  string                            String result
  23801.  
  23802.  radix                             Base of value
  23803.  
  23804.  
  23805.  Remarks
  23806.  
  23807.  The ltoa function converts the digits of value to a null-terminated
  23808.  character string and stores the result (up to 33 bytes) in string. The radix
  23809.  argument specifies the base of value, which must be in the range 2-36. If
  23810.  radix equals 10 and value is negative, the first character of the stored
  23811.  string is the minus sign (-).
  23812.  
  23813.  
  23814.  Return Value
  23815.  
  23816.  The ltoa function returns a pointer to string. There is no error return.
  23817.  
  23818.  
  23819.  Compatibility
  23820.  
  23821.   ANSI   DOS   OS/2   UNIX   XENIX
  23822.  
  23823.  
  23824.  
  23825.  See Also
  23826.  
  23827.  itoa, ultoa
  23828.  
  23829.  
  23830.  Example
  23831.  
  23832.    /* ITOA.C: This program converts integers of various sizes to strings
  23833.     * in various radixes.
  23834.     */
  23835.  
  23836.    #include <stdlib.h>
  23837.    #include <stdio.h>
  23838.  
  23839.    void main()
  23840.    {
  23841.       char buffer[20];
  23842.       int  i = 3445;
  23843.       long l = -344115L;
  23844.       unsigned long ul = 1234567890UL;
  23845.  
  23846.  
  23847.    itoa( i, buffer, 10 );
  23848.       printf( "String of integer %d (radix 10): %s\n", i, buffer );
  23849.       itoa( i, buffer, 16 );
  23850.       printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
  23851.       itoa( i, buffer, 2  );
  23852.       printf( "String of integer %d (radix 2): %s\n", i, buffer );
  23853.  
  23854.       ltoa( l, buffer, 16 );
  23855.       printf( "String of long int %ld (radix 16): 0x%s\n", l, buffer );
  23856.  
  23857.       ultoa( ul, buffer, 16 );
  23858.       printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul, buffer );
  23859.    }
  23860.  
  23861.  
  23862.  Output
  23863.  
  23864.  
  23865.  
  23866.    String of integer 3445 (radix 10): 3445
  23867.    String of integer 3445 (radix 16): 0xd75
  23868.    String of integer 3445 (radix 2): 110101110101
  23869.    String of long int -344115 (radix 16): 0xfffabfcd
  23870.    String of unsigned long 1234567890 (radix 16): 0x499602d2
  23871.  
  23872.  
  23873.  
  23874.  
  23875.  
  23876.  _makepath
  23877.  ────────────────────────────────────────────────────────────────────────────
  23878.  
  23879.  
  23880.  Description
  23881.  
  23882.  Creates a single path name.
  23883.  
  23884.    #include <stdlib.h>
  23885.  
  23886.    void _makepath( char *path, char *drive, char *dir, char *fname, char *ext
  23887.    );
  23888.  
  23889.  path                              Full path-name buffer
  23890.  
  23891.  drive                             Drive letter
  23892.  
  23893.  dir                               Directory path
  23894.  
  23895.  fname                             File name
  23896.  
  23897.  ext                               File extension
  23898.  
  23899.  
  23900.  Remarks
  23901.  
  23902.  The _makepath routine creates a single path name, composed of a drive
  23903.  letter, directory path, file name, and file-name extension. The path
  23904.  argument should point to an empty buffer large enough to hold the complete
  23905.  path name. The constant _MAX_PATH, defined in STDLIB.H, specifies the
  23906.  maximum size path that the _makepath function can handle. The other
  23907.  arguments point to buffers containing the path-name elements:
  23908.  
  23909.  Buffer                            Description
  23910.  ────────────────────────────────────────────────────────────────────────────
  23911.  drive                             The drive argument contains a letter (A,
  23912.                                    B, etc.) corresponding to the desired
  23913.                                    drive and an optional trailing colon.
  23914.                                    The _makepath routine will insert the
  23915.                                    colon automatically in the composite
  23916.                                    path name if it is missing. If drive is
  23917.                                    a null character or an empty string, no
  23918.                                    drive letter and colon will appear in
  23919.                                    the composite path string.
  23920.  
  23921.  dir                               The dir argument contains the path of
  23922.                                    directories, not including the drive
  23923.                                    designator or the actual file name. The
  23924.                                    trailing slash is optional, and either
  23925.                                    forward slashes ( / ) or backslashes ( \
  23926.                                    ) or both may be used in a single dir
  23927.                                    argument. If a trailing slash ( / or \ )
  23928.                                    is not specified, it will be inserted
  23929.                                    automatically. If dir is a null
  23930.                                    character or an empty string, no slash
  23931.                                    is inserted in the composite path
  23932.                                    string.
  23933.  
  23934.  fname                             The fname argument contains the base
  23935.                                    file name without any extensions. If
  23936.                                    fname is NULL or points to an empty
  23937.                                    string, no file name is inserted in the
  23938.                                    composite path string.
  23939.  
  23940.  ext                               The ext argument contains the actual
  23941.                                    file-name extension, with or without a
  23942.                                    leading period (.). The _makepath
  23943.                                    routine will insert the period
  23944.                                    automatically if it does not appear in
  23945.                                    ext. If ext is a null character or an
  23946.                                    empty string, no period is inserted in
  23947.                                    the composite path string.
  23948.  
  23949.  There are no size limits on any of the above four fields. However, the
  23950.  composite path must be no larger than the _MAX_PATH constant. The _MAX_PATH
  23951.  limit permits a path name much larger than any of the current versions of
  23952.  DOS or OS/2 will handle.
  23953.  
  23954.  
  23955.  Return Value
  23956.  
  23957.  None.
  23958.  
  23959.  
  23960.  Compatibility
  23961.  
  23962.   ANSI    DOS    OS/2    UNIX    XENIX
  23963.  
  23964.  
  23965.  
  23966.  See Also
  23967.  
  23968.  _fullpath,  _splitpath
  23969.  
  23970.  
  23971.  Example
  23972.  
  23973.    /* MAKEPATH.C */
  23974.    #include <stdlib.h>
  23975.    #include <stdio.h>
  23976.  
  23977.    void main()
  23978.    {
  23979.       char path_buffer[_MAX_PATH];
  23980.       char drive[_MAX_DRIVE];
  23981.       char dir[_MAX_DIR];
  23982.       char fname[_MAX_FNAME];
  23983.       char ext[_MAX_EXT];
  23984.  
  23985.       _makepath( path_buffer, "c", "\\c60\\clibref\\", "makepath", "c" );
  23986.       printf( "Path created with _makepath: %s\n\n", path_buffer );
  23987.       _splitpath( path_buffer, drive, dir, fname, ext );
  23988.       printf( "Path extracted with _splitpath:\n" );
  23989.       printf( "  Drive: %s\n", drive );
  23990.       printf( "  Dir: %s\n", dir );
  23991.       printf( "  Filename: %s\n", fname );
  23992.       printf( "  Ext: %s\n", ext );
  23993.    }
  23994.  
  23995.  
  23996.  Output
  23997.  
  23998.  
  23999.  
  24000.    Path created with _makepath: c:\c60\clibref\makepath.c
  24001.  
  24002.    Path extracted with _splitpath:
  24003.      Drive: c:
  24004.      Dir: \c60\clibref\
  24005.      Filename: makepath
  24006.      Ext: .c
  24007.  
  24008.  
  24009.  
  24010.  
  24011.  
  24012.  malloc Functions
  24013.  ────────────────────────────────────────────────────────────────────────────
  24014.  
  24015.  
  24016.  Description
  24017.  
  24018.  Allocate memory blocks.
  24019.  
  24020.  
  24021.  
  24022.  #include <stdlib.h>               For ANSI compatibility (malloc only)
  24023.  
  24024.  #include <malloc.h>               Required only for function declarations
  24025.  
  24026.    void *malloc( size_t size );
  24027.  
  24028.    void _based(void) *_bmalloc( _segment seg, size_t size );
  24029.  
  24030.    void _far *_fmalloc( size_t size );
  24031.  
  24032.    void _near *_nmalloc( size_t size );
  24033.  
  24034.  size                              Bytes to allocate
  24035.  
  24036.  seg                               Based heap segment selector
  24037.  
  24038.  
  24039.  Remarks
  24040.  
  24041.  Functions in the malloc family allocate a memory block of at least size
  24042.  bytes. The block may be larger than size bytes because of space required for
  24043.  alignment and maintenance information. If size is 0, each of these functions
  24044.  allocates a zero-length item in the heap and returns a valid pointer to that
  24045.  item.
  24046.  
  24047.  The storage space pointed to by the return value is guaranteed to be
  24048.  suitably aligned for storage of any type of object. To get a pointer to a
  24049.  type other than void, use a type cast on the return value.
  24050.  
  24051.  In large data models (compact-, large-, and huge-model programs), malloc
  24052.  maps to _fmalloc. In small data models (tiny-, small-, and medium-model
  24053.  programs), malloc maps to _nmalloc.
  24054.  
  24055.  The _fmalloc function allocates a memory block of at least size bytes in the
  24056.  far heap, which is outside the default data segment. The return value is a
  24057.  far pointer to void.
  24058.  
  24059.  The _bmalloc function allocates a memory block of at least size bytes in the
  24060.  based heap segment specified by the segment selector seg.
  24061.  
  24062.  The malloc functions allocate memory in the heap segment specified below.
  24063.  
  24064.  Function                          Heap Segment
  24065.  ────────────────────────────────────────────────────────────────────────────
  24066.  malloc                            Depends on data model of program
  24067.  
  24068.  _bmalloc                          Based heap segment specified by seg
  24069.                                    value
  24070.  
  24071.  _fmalloc                          Far heap (outside default data segment)
  24072.  
  24073.  _nmalloc                          Near heap (within default data segment)
  24074.  
  24075.  If you are creating programs to run in both real mode and protected mode,
  24076.  you should probably bind with APILMR.OBJ as well as API.LIB and OS2.LIB.
  24077.  This is necessary if a program will use the _nmalloc function.
  24078.  
  24079.  The functions listed below call the malloc family of routines. In addition,
  24080.  the C start-up code uses malloc to allocate storage for the environ/envp and
  24081.  argv strings and arrays.
  24082.  
  24083.  The following routines call malloc:
  24084.  
  24085. ╓┌─────────┌─────────┌───────────────────────────────────────────────────────╖
  24086.  ────────────────────────────────────────────────────────────────────────────
  24087.  calloc    fseek     _searchenv
  24088.  
  24089.  ────────────────────────────────────────────────────────────────────────────
  24090. 
  24091.  execv     fsetpos   spawnv
  24092.  
  24093.  execve    fullpath  spawnve
  24094.  
  24095.  execvp    fwrite    spawnvp
  24096.  
  24097.  execvpe   getc      spawnvpe
  24098.  
  24099.  execl     getchar   spawnl
  24100.  
  24101.  execle    getcwd    spawnle
  24102.  
  24103.  execlp    _getcwd   spawnlp
  24104.  
  24105.  execlpe   gets      spawnlpe
  24106.  
  24107.  fgetc     getw      strdup
  24108.  
  24109.  fgetchar  _popen    system
  24110.  ────────────────────────────────────────────────────────────────────────────
  24111. fgetchar  _popen    system
  24112.  
  24113.  fgets     printf    scanf
  24114.  
  24115.  fprint    putc      setvbuf
  24116.  
  24117.  fputc     putchar   tempnam
  24118.  
  24119.  fputchar  putenv    ungetc
  24120.  
  24121.  fputs     puts      vfprintf
  24122.  
  24123.  fread     putw      vprintf
  24124.  
  24125.  fscanf
  24126.  
  24127.  
  24128.  
  24129.  The following routines call malloc only in the multithread run-time
  24130.  libraries (LLIBCMT, LLIBCDLL, CDLLOBJS), not in the regular run-time
  24131.  libraries:
  24132.  
  24133. ╓┌─────────────┌──────────┌──────────────────────────────────────────────────╖
  24134.  ────────────────────────────────────────────────────────────────────────────
  24135.  asctime       localtime  _strerrpr
  24136.  
  24137.  _beginthread  mktime     tmpfile
  24138.  
  24139.  ctime         sterror    tmpnam
  24140.  
  24141.  gmtime
  24142.  
  24143.  
  24144.  
  24145.  The following routines call _nmalloc:
  24146.  
  24147.  ────────────────────────────────────────────────────────────────────────────
  24148.  _nrealloc
  24149.  
  24150.  _ncalloc
  24151.  
  24152.  _nstrdup
  24153.  
  24154.  realloc (in small data models)
  24155.  
  24156.  
  24157.  The following routines call _fmalloc:
  24158.  
  24159.  ────────────────────────────────────────────────────────────────────────────
  24160.  _frealloc
  24161.  
  24162.  _fcalloc
  24163.  
  24164.  _fstrdup
  24165.  
  24166.  realloc (in large data models)
  24167.  
  24168.  
  24169.  ────────────────────────────────────────────────────────────────────────────
  24170.  C5.1 Differences
  24171.  In Microsoft C version 5.1, the _fmalloc function would retry allocating
  24172.  within the default data segment (i.e., in the near heap) if sufficient
  24173.  memory was not available outside the default data segment. Version 6.0
  24174.  returns NULL under these conditions.
  24175.  In version 5.1, the start-up code used malloc only if wild-card expansion
  24176.  was used.
  24177.  
  24178.  The _freect, _memavl, and _memmax functions called malloc in version 5.1 but
  24179.  do not do so in version 6.0.
  24180.  ────────────────────────────────────────────────────────────────────────────
  24181.  
  24182.  
  24183.  Return Value
  24184.  
  24185.  The malloc function returns a void pointer to the allocated space. The
  24186.  _nmalloc function returns a ( void _near * ) and _fmalloc returns a ( void
  24187.  _far * ). The _bmalloc function returns a ( void _based( void ) * ).
  24188.  
  24189.  The _malloc, _fmalloc and _nmalloc functions return NULL if there is
  24190.  insufficient memory available. The _bmalloc function returns _NULLOFF if
  24191.  there is insufficient memory available.
  24192.  
  24193.  Always check the return from the malloc function, even if the amount of
  24194.  memory requested is small.
  24195.  
  24196.  
  24197.  Compatibility
  24198.  
  24199.  malloc
  24200.  
  24201.   ANSI   DOS    OS/2    UNIX    XENIX
  24202.  
  24203.  
  24204.  _bmalloc, _fmalloc, _nmalloc
  24205.  
  24206.   ANSI    DOS    OS/2    UNIX    XENIX
  24207.  
  24208.  
  24209.  
  24210.  See Also
  24211.  
  24212.  calloc functions, free functions, realloc functions
  24213.  
  24214.  
  24215.  Example
  24216.  
  24217.    /* MALLOC.C: This program allocates memory with malloc, then frees
  24218.     * the memory with free.
  24219.     */
  24220.  
  24221.    #include <stdlib.h>         /* Definition of _MAX_PATH */
  24222.    #include <stdio.h>
  24223.    #include <malloc.h>
  24224.  
  24225.    void main()
  24226.    {
  24227.       char *string;
  24228.  
  24229.       /* Allocate space for a path name */
  24230.       string = malloc( _MAX_PATH );
  24231.       if( string == NULL )
  24232.          printf( "Insufficient memory available\n" );
  24233.       else
  24234.          printf( "Memory space allocated for pathname\n" );
  24235.       free( string );
  24236.       printf( "Memory freed\n" );
  24237.    }
  24238.  
  24239.  
  24240.  Output
  24241.  
  24242.  
  24243.  
  24244.    Memory space allocated for pathname
  24245.    Memory freed
  24246.  
  24247.  
  24248.  
  24249.  
  24250.  
  24251.  matherr, _matherrl
  24252.  ────────────────────────────────────────────────────────────────────────────
  24253.  
  24254.  
  24255.  Description
  24256.  
  24257.  Handle math errors.
  24258.  
  24259.    #include <math.h>
  24260.  
  24261.    int matherr( struct exception *except );
  24262.  
  24263.    int _matherrl( struct _exceptionl *except );
  24264.  
  24265.  except                            Pointer to structure containing error
  24266.                                    information
  24267.  
  24268.  
  24269.  Remarks
  24270.  
  24271.  The matherr functions process errors generated by the functions of the math
  24272.  library. The math functions call the appropriate matherr routine whenever an
  24273.  error is detected. The _matherrl function uses the 80-bit, 10-byte
  24274.  coprocessor form of arguments and return values. See the reference page on
  24275.  the long double functions for more details on this data type.
  24276.  
  24277.  The user can provide a different definition of the matherr or _matherrl
  24278.  function to carry out special error handling.
  24279.  
  24280.  When an error occurs in a math routine, matherr is called with a pointer to
  24281.  an exception type structure (defined in MATH.H) as an argument.
  24282.  
  24283.  The exception structure contains the following elements:
  24284.  
  24285.  Element                           Description
  24286.  ────────────────────────────────────────────────────────────────────────────
  24287.  int type                          Exception type
  24288.  
  24289.  char *name                        Name of function where error occurred
  24290.  
  24291.  double arg1, arg2                 First and second (if any) argument to
  24292.                                    function
  24293.  
  24294.  double retval                     Value to be returned by function
  24295.  
  24296.  The type specifies the type of math error. It is one of the following
  24297.  values, defined in MATH.H:
  24298.  
  24299.  Value                             Meaning
  24300.  ────────────────────────────────────────────────────────────────────────────
  24301.  DOMAIN                            Argument domain error
  24302.  
  24303.  SING                              Argument singularity
  24304.  
  24305.  OVERFLOW                          Overflow range error
  24306.  
  24307.  PLOSS                             Partial loss of significance
  24308.  
  24309.  TLOSS                             Total loss of significance
  24310.  
  24311.  UNDERFLOW                         Underflow range error
  24312.  
  24313.  The structure member name is a pointer to a null-terminated string
  24314.  containing the name of the function that caused the error. The structure
  24315.  members arg1 and arg2 specify the values that caused the error. (If only one
  24316.  argument is given, it is stored in arg1.)
  24317.  
  24318.  The default return value for the given error is retval. If you change the
  24319.  return value, remember that the return value must specify whether an error
  24320.  actually occurred. If the matherr function returns 0, an error message is
  24321.  displayed and errno is set to an appropriate error value. If matherr returns
  24322.  a nonzero value, no error message is displayed, and errno remains unchanged.
  24323.  
  24324.  
  24325.  
  24326.  Return Value
  24327.  
  24328.  The matherr functions should return 0 to indicate an error, and a nonzero
  24329.  value to indicate successful corrective action.
  24330.  
  24331.  
  24332.  Compatibility
  24333.  
  24334.  matherr
  24335.  
  24336.   ANSI   DOS    OS/2    UNIX    XENIX
  24337.  
  24338.  
  24339.  _matherrl
  24340.  
  24341.   ANSI   DOS   OS/2    UNIX    XENIX
  24342.  
  24343.  
  24344.  
  24345.  See Also
  24346.  
  24347.  acos functions, asin functions, atan functions, bessel functions, cabs, cos
  24348.  functions, exp, hypot, log functions, pow, sin functions, sqrt, tan
  24349.  functions
  24350.  
  24351.  
  24352.  Example
  24353.  
  24354.    /* MATHERR.C: To use matherr, you must turn off the Extended Dictionary
  24355.     * flag within the Microsoft Programmer's WorkBench environment, or use
  24356.  the
  24357.     * /NOE linker option outside the environment. For example:
  24358.     *      CL matherr.c /link /NOE
  24359.     */
  24360.  
  24361.    #include <math.h>
  24362.    #include <string.h>
  24363.    #include <stdio.h>
  24364.  
  24365.    void main()
  24366.    {
  24367.       /* Do several math operations that cause errors. The matherr
  24368.        * routine handles DOMAIN errors, but lets the system handle
  24369.        * other errors normally.
  24370.        */
  24371.       printf( "log( -2.0 ) = %e\n", log( -2.0 ) );
  24372.       printf( "log10( -5.0 ) = %e\n", log10( -5.0 ) );
  24373.       printf( "log( 0.0 ) = %e\n", log( 0.0 ) );
  24374.    }
  24375.  
  24376.    /* Handle several math errors caused by passing a negative argument
  24377.     * to log or log10 (DOMAIN errors). When this happens, matherr returns
  24378.     * the natural or base-10 logarithm of the absolute value of the
  24379.     * argument and suppresses the usual error message.
  24380.     */
  24381.    int matherr( struct exception *except )
  24382.    {
  24383.        /* Handle DOMAIN errors for log or log10. */
  24384.        if( except->type == DOMAIN )
  24385.        {
  24386.            if( strcmp( except->name, "log" ) == 0 )
  24387.            {
  24388.                except->retval = log( -(except->arg1) );
  24389.                printf( "Special: using absolute value: %s: DOMAIN error\n",
  24390.                        except->name );
  24391.                return 1;
  24392.            }
  24393.            else if( strcmp( except->name, "log10" ) == 0 )
  24394.            {
  24395.                except->retval = log10( -(except->arg1) );
  24396.                printf( "Special: using absolute value: %s: DOMAIN error\n",
  24397.                        except->name );
  24398.                return 1;
  24399.            }
  24400.        }
  24401.        else
  24402.        {
  24403.            printf( "Normal: " );
  24404.            return 0;    /* Else use the default actions */
  24405.        }
  24406.    }
  24407.  
  24408.  
  24409.  Output
  24410.  
  24411.  
  24412.  
  24413.    Special: using absolute value: log: DOMAIN error
  24414.    log( -2.0 ) = 6.931472e-001
  24415.    Special: using absolute value: log10: DOMAIN error
  24416.    log10( -5.0 ) = 6.989700e-001
  24417.    Normal: log: SING error
  24418.    log( 0.0 ) = -1.797693e+308
  24419.  
  24420.  
  24421.  
  24422.  
  24423.  
  24424.  max
  24425.  ────────────────────────────────────────────────────────────────────────────
  24426.  
  24427.  
  24428.  Description
  24429.  
  24430.  Returns the larger of two values.
  24431.  
  24432.    #include <stdlib.h>
  24433.  
  24434.    type max( type a, type b );
  24435.  
  24436.  type                              Any numeric data type
  24437.  
  24438.  a, b                              Values of any numeric type to be
  24439.                                    compared
  24440.  
  24441.  
  24442.  Remarks
  24443.  
  24444.  The max macro compares two values and returns the value of the larger one.
  24445.  The arguments can be of any numeric data type, signed or unsigned. Both
  24446.  arguments and the return value must be of the same data type.
  24447.  
  24448.  
  24449.  Return Value
  24450.  
  24451.  The macro returns the larger of the two arguments.
  24452.  
  24453.  
  24454.  Compatibility
  24455.  
  24456.   ANSI    DOS    OS/2    UNIX    XENIX
  24457.  
  24458.  
  24459.  
  24460.  See Also
  24461.  
  24462.  min
  24463.  
  24464.  
  24465.  Example
  24466.  
  24467.    /* MINMAX.C */
  24468.    #include <stdlib.h>
  24469.    #include <stdio.h>
  24470.  
  24471.    void main()
  24472.    {
  24473.       int a = 10;
  24474.       int b = 21;
  24475.  
  24476.       printf( "The larger of %d and %d is %d\n",  a, b, max( a, b ) );
  24477.       printf( "The smaller of %d and %d is %d\n", a, b, min( a, b ) );
  24478.    }
  24479.  
  24480.  
  24481.  Output
  24482.  
  24483.  
  24484.  
  24485.    The larger of 10 and 21 is 21
  24486.    The smaller of 10 and 21 is 10
  24487.  
  24488.  
  24489.  
  24490.  
  24491.  
  24492.  _memavl
  24493.  ────────────────────────────────────────────────────────────────────────────
  24494.  
  24495.  
  24496.  Description
  24497.  
  24498.  Returns the size of memory available.
  24499.  
  24500.    #include <malloc.h> Required only for function declarations
  24501.  
  24502.    size_t _memavl( void );
  24503.  
  24504.  
  24505.  Remarks
  24506.  
  24507.  The _memavl function returns the approximate size, in bytes, of the memory
  24508.  available for dynamic memory allocation in the near heap (default data
  24509.  segment). The _memavl function can be used with calloc, malloc, or realloc
  24510.  in tiny, small, and medium memory models and with _ncalloc, _nmalloc or
  24511.  _nrealloc in any memory model.
  24512.  
  24513.  The number returned by the _memavl function may not be the number of
  24514.  contiguous bytes. Consequently, a call to malloc requesting allocation of
  24515.  the size returned by _memavl may not succeed. Use the _memmax function to
  24516.  find the size of the largest available contiguous block of memory.
  24517.  
  24518.  
  24519.  Return Value
  24520.  
  24521.  The _memavl function returns the size in bytes as an unsigned integer.
  24522.  
  24523.  
  24524.  Compatibility
  24525.  
  24526.   ANSI    DOS    OS/2    UNIX    XENIX
  24527.  
  24528.  
  24529.  
  24530.  See Also
  24531.  
  24532.  calloc functions,  _freect, malloc functions,  _memmax, realloc functions
  24533.  
  24534.  
  24535.  Example
  24536.  
  24537.    /* MEMAVL.C: This program uses _memavl to determine the amount of
  24538.     * memory available for dynamic allocation. It then uses malloc to
  24539.     * allocate space for 5,000 long integers and uses _memavl again to
  24540.     * determine the new amount of available memory.
  24541.     */
  24542.  
  24543.    #include <malloc.h>
  24544.    #include <stdio.h>
  24545.  
  24546.    void main()
  24547.    {
  24548.       long *longptr;
  24549.  
  24550.       printf( "Memory available before _nmalloc = %u\n", _memavl() );
  24551.       if( (longptr = _nmalloc( 5000 * sizeof( long ) )) != NULL )
  24552.       {
  24553.          printf( "Memory available after _nmalloc = %u\n", _memavl() );
  24554.          _nfree( longptr );
  24555.       }
  24556.    }
  24557.  
  24558.  
  24559.  Output
  24560.  
  24561.  
  24562.  
  24563.    Memory available before _nmalloc = 60906
  24564.    Memory available after _nmalloc = 40390
  24565.  
  24566.  
  24567.  
  24568.  
  24569.  
  24570.  memccpy, _fmemccpy
  24571.  ────────────────────────────────────────────────────────────────────────────
  24572.  
  24573.  
  24574.  Description
  24575.  
  24576.  Copy characters from a buffer.
  24577.  
  24578.  #include <memory.h>               Required only for function declarations
  24579.  
  24580.  #include <string.h>               Use either STRING.H or MEMORY.H
  24581.  
  24582.    void *memccpy( void *dest, void *src, int c, unsigned int count );
  24583.  
  24584.    void _far * _far _fmemccpy( void _far *dest, void _far *src, int c,
  24585.    unsigned int count );
  24586.  
  24587.  dest                              Pointer to destination
  24588.  
  24589.  src                               Pointer to source
  24590.  
  24591.  c                                 Last character to copy
  24592.  
  24593.  count                             Number of characters
  24594.  
  24595.  
  24596.  Remarks
  24597.  
  24598.  The memccpy and _fmemccpy functions copy 0 or more bytes of src to dest,
  24599.  halting when the character c has been copied or when count bytes have been
  24600.  copied, whichever comes first.
  24601.  
  24602.  The _fmemccpy function is a model-independent (large-model) form of the
  24603.  memccpy function. It can be called from any point in any program.
  24604.  
  24605.  
  24606.  Return Value
  24607.  
  24608.  If the character c is copied, memccpy or _fmemccpy returns a pointer (or far
  24609.  pointer) to the byte in dest that immediately follows the character. If c is
  24610.  not copied, memccpy returns NULL.
  24611.  
  24612.  
  24613.  Compatibility
  24614.  
  24615.  memccpy
  24616.  
  24617.   ANSI    DOS    OS/2   UNIX    XENIX
  24618.  
  24619.  
  24620.  _fmemccpy
  24621.  
  24622.   ANSI    DOS    OS/2    UNIX    XENIX
  24623.  
  24624.  
  24625.  
  24626.  See Also
  24627.  
  24628.  memchr, memcmp, memcpy, memset
  24629.  
  24630.  
  24631.  Example
  24632.  
  24633.    /* MEMCCPY.C */
  24634.    #include <memory.h>
  24635.    #include <stdio.h>
  24636.    #include <string.h>
  24637.  
  24638.    char string1[60] = "The quick brown dog jumps over the lazy fox";
  24639.  
  24640.    void main()
  24641.    {
  24642.       char buffer[61];
  24643.       char *pdest;
  24644.  
  24645.       printf( "Function:\tmemccpy 60 characters or to character 's'\n" );
  24646.       printf( "Source:\t\t%s\n", string1 );
  24647.       pdest = memccpy( buffer, string1, 's', 60 );
  24648.       *pdest = '\0';
  24649.       printf( "Result:\t\t%s\n", buffer );
  24650.       printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );
  24651.    }
  24652.  
  24653.  
  24654.  Output
  24655.  
  24656.  
  24657.  
  24658.    Function:       memccpy 60 characters or to character 's'
  24659.    Source:         The quick brown dog jumps over the lazy fox
  24660.    Result:         The quick brown dog jumps
  24661.    Length:         25 characters
  24662.  
  24663.  
  24664.  
  24665.  
  24666.  
  24667.  memchr, _fmemchr
  24668.  ────────────────────────────────────────────────────────────────────────────
  24669.  
  24670.  
  24671.  Description
  24672.  
  24673.  Find characters in a buffer.
  24674.  
  24675.  #include <memory.h>               Required only for function declarations
  24676.  
  24677.  #include <string.h>               Use either STRING.H (for ANSI
  24678.                                    compatibility) or MEMORY.H
  24679.  
  24680.    void *memchr( const void *buf, int c, size_t count );
  24681.  
  24682.    void _far * _far _fmemchr( const void _far *buf, int c, size_t count );
  24683.  
  24684.  buf                               Pointer to buffer
  24685.  
  24686.  c                                 Character to look for
  24687.  
  24688.  count                             Number of characters
  24689.  
  24690.  
  24691.  Remarks
  24692.  
  24693.  The memchr and _fmemchr functions look for the first occurrence of c in the
  24694.  first count bytes of buf. They stop when they find c or when they have
  24695.  checked the first count bytes.
  24696.  
  24697.  The _fmemchr function is a model-independent (large-model) form of the
  24698.  memchr function. It can be called from any point in any program.
  24699.  
  24700.  
  24701.  Return Value
  24702.  
  24703.  If successful, memchr or _fmemchr returns a pointer (or a far pointer) to
  24704.  the first location of c in buf. Otherwise, they return NULL.
  24705.  
  24706.  
  24707.  Compatibility
  24708.  
  24709.  memchr
  24710.  
  24711.   ANSI    DOS    OS/2    UNIX    XENIX
  24712.  
  24713.  
  24714.  _fmemchr
  24715.  
  24716.   ANSI    DOS    OS/2    UNIX    XENIX
  24717.  
  24718.  
  24719.  
  24720.  See Also
  24721.  
  24722.  memccpy, memcmp, memcpy, memset, strchr
  24723.  
  24724.  
  24725.  Example
  24726.  
  24727.    /* MEMCHR.C */
  24728.    #include <memory.h>
  24729.    #include <stdio.h>
  24730.  
  24731.    int  ch = 'r';
  24732.    char str[] =    "lazy";
  24733.    char string[] = "The quick brown dog jumps over the lazy fox";
  24734.    char fmt1[] =   "         1         2         3         4         5";
  24735.    char fmt2[] =   "12345678901234567890123456789012345678901234567890";
  24736.  
  24737.    void main()
  24738.    {
  24739.       char *pdest;
  24740.       int result;
  24741.  
  24742.       printf( "String to be searched:\n\t\t%s\n", string );
  24743.       printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
  24744.  
  24745.       printf( "Search char:\t%c\n", ch );
  24746.       pdest = memchr( string, ch, sizeof( string ) );
  24747.       result = pdest - string + 1;
  24748.       if( pdest != NULL )
  24749.          printf( "Result:\t\t%c found at position %d\n\n", ch, result );
  24750.       else
  24751.          printf( "Result:\t\t%c not found\n" );
  24752.    }
  24753.  
  24754.  
  24755.  Output
  24756.  
  24757.  
  24758.  
  24759.    String to be searched:
  24760.                    The quick brown dog jumps over the lazy fox
  24761.                             1         2         3         4         5
  24762.                    12345678901234567890123456789012345678901234567890
  24763.  
  24764.    Search char:    r
  24765.    Result:         r found at position 12
  24766.  
  24767.  
  24768.  
  24769.  
  24770.  
  24771.  memcmp, _fmemcmp
  24772.  ────────────────────────────────────────────────────────────────────────────
  24773.  
  24774.  
  24775.  Description
  24776.  
  24777.  Compare characters in two buffers.
  24778.  
  24779.  #include <memory.h>               Required only for function declarations
  24780.  
  24781.  #include <string.h>               Use either STRING.H (for ANSI
  24782.                                    compatibility) or MEMORY.H
  24783.  
  24784.    int memcmp( const void *buf1, const void *buf2, size_t count );
  24785.  
  24786.    int _far _fmemcmp( const void _far *buf1, const void _far *buf2, size_t
  24787.    count );
  24788.  
  24789.  buf1                              First buffer
  24790.  
  24791.  buf2                              Second buffer
  24792.  
  24793.  count                             Number of characters
  24794.  
  24795.  
  24796.  Remarks
  24797.  
  24798.  The memcmp and _fmemcmp functions compare the first count bytes of buf1 and
  24799.  buf2 and return a value indicating their relationship, as follows:
  24800.  
  24801.  Value                             Meaning
  24802.  ────────────────────────────────────────────────────────────────────────────
  24803.  < 0                               buf1 less than buf2
  24804.  
  24805.  = 0                               buf1 identical to buf2
  24806.  
  24807.  > 0                               buf1 greater than buf2
  24808.  
  24809.  The _fmemcmp function is a model-independent (large-model) form of the
  24810.  memcmp function. It can be called from any point in program.
  24811.  
  24812.  There is a semantic difference between the function version of memcmp and
  24813.  its intrinsic version. The function version supports huge pointers in
  24814.  compact-, large-, and huge-model programs, but the intrinsic version does
  24815.  not.
  24816.  
  24817.  
  24818.  Return Value
  24819.  
  24820.  The memcmp function returns an integer value, as described above.
  24821.  
  24822.  
  24823.  Compatibility
  24824.  
  24825.  memcmp
  24826.  
  24827.   ANSI    DOS    OS/2    UNIX    XENIX
  24828.  
  24829.  
  24830.  _fmemcmp
  24831.  
  24832.   ANSI    DOS    OS/2    UNIX    XENIX
  24833.  
  24834.  
  24835.  
  24836.  See Also
  24837.  
  24838.  memccpy, memchr, memcpy, memset, strcmp, strncmp
  24839.  
  24840.  
  24841.  Example
  24842.  
  24843.    /* MEMCMP.C: This program uses memcmp to compare the strings named
  24844.     * first and second. If the first 19 bytes of the strings are
  24845.     * equal, the program considers the strings to be equal.
  24846.     */
  24847.  
  24848.    #include <string.h>
  24849.    #include <stdio.h>
  24850.  
  24851.    void main()
  24852.    {
  24853.       char first[]  = "12345678901234567890";
  24854.       char second[] = "12345678901234567891";
  24855.       int result;
  24856.  
  24857.       printf( "Compare '%.19s' to '%.19s':\n", first, second );
  24858.       result = memcmp( first, second, 19 );
  24859.       if( result < 0 )
  24860.          printf( "First is less than second.\n" );
  24861.       else if( result == 0 )
  24862.          printf( "First is equal to second.\n" );
  24863.       else if( result > 0 )
  24864.          printf( "First is greater than second.\n" );
  24865.       printf( "Compare '%.20s' to '%.20s':\n", first, second );
  24866.       result = memcmp( first, second, 20 );
  24867.       if( result < 0 )
  24868.          printf( "First is less than second.\n" );
  24869.       else if( result == 0 )
  24870.          printf( "First is equal to second.\n" );
  24871.       else if( result > 0 )
  24872.          printf( "First is greater than second.\n" );
  24873.    }
  24874.  
  24875.  
  24876.  Output
  24877.  
  24878.  
  24879.  
  24880.    Compare '1234567890123456789' to '1234567890123456789':
  24881.    First is equal to second.
  24882.    Compare '12345678901234567890' to '12345678901234567891':
  24883.    First is less than second.
  24884.  
  24885.  
  24886.  
  24887.  
  24888.  
  24889.  memcpy, _fmemcpy
  24890.  ────────────────────────────────────────────────────────────────────────────
  24891.  
  24892.  
  24893.  Description
  24894.  
  24895.  Copy characters between buffers.
  24896.  
  24897.  #include <memory.h>               Required only for function declarations
  24898.  
  24899.  #include <string.h>               Use either STRING.H (for ANSI
  24900.                                    compatibility) or MEMORY.H
  24901.  
  24902.    void *memcpy( void *dest, const void *src, size_t count );
  24903.  
  24904.    void _far  * _far _fmemcpy( void _far *dest, const void _far *src, size_t
  24905.    count );
  24906.  
  24907.  dest                              New buffer
  24908.  
  24909.  src                               Buffer to copy from
  24910.  
  24911.  count                             Number of characters to copy
  24912.  
  24913.  
  24914.  Remarks
  24915.  
  24916.  The memcpy and _fmemcpy functions copy count bytes of src to dest. If the
  24917.  source and destination overlap, these functions do not ensure that the
  24918.  original source bytes in the overlapping region are copied before being
  24919.  overwritten. Use memmove to handle overlapping regions.
  24920.  
  24921.  The _fmemcpy function is a model-independent (large-model) form of the
  24922.  memcpy function. It can be called from any point in any program.
  24923.  
  24924.  There is a semantic difference between the function version of memcpy and
  24925.  its intrinsic version. The function version supports huge pointers in
  24926.  compact-, large-, and huge-model programs, but the intrinsic version does
  24927.  not.
  24928.  
  24929.  
  24930.  Return Value
  24931.  
  24932.  The memcpy and _fmemcpy functions return a pointer to dest.
  24933.  
  24934.  
  24935.  Compatibility
  24936.  
  24937.  memcpy
  24938.  
  24939.   ANSI    DOS    OS/2    UNIX    XENIX
  24940.  
  24941.  
  24942.  _fmemcpy
  24943.  
  24944.   ANSI    DOS    OS/2    UNIX    XENIX
  24945.  
  24946.  
  24947.  
  24948.  See Also
  24949.  
  24950.  memccpy, memchr, memcmp, memmove, memset, strcpy, strncpy
  24951.  
  24952.  
  24953.  Example
  24954.  
  24955.    /* MEMCPY.C. Illustrate overlapping copy: memmove handles it
  24956.     * correctly; memcpy does not.
  24957.     */
  24958.    #include <memory.h>
  24959.    #include <string.h>
  24960.    #include <stdio.h>
  24961.  
  24962.    char string1[60] = "The quick brown dog jumps over the lazy fox";
  24963.    char string2[60] = "The quick brown fox jumps over the lazy dog";
  24964.    /*                           1         2         3         4         5
  24965.     *                  12345678901234567890123456789012345678901234567890
  24966.     */
  24967.    void main()
  24968.    {
  24969.       printf( "Function:\tmemcpy without overlap\n" );
  24970.       printf( "Source:\t\t%s\n", string1 + 40 );
  24971.       printf( "Destination:\t%s\n", string1 + 16 );
  24972.       memcpy( string1 + 16, string1 + 40, 3 );
  24973.       printf( "Result:\t\t%s\n", string1 );
  24974.       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
  24975.  
  24976.       /* Restore string1 to original contents */
  24977.       memcpy( string1 + 16, string2 + 40, 3 );
  24978.  
  24979.       printf( "Function:\tmemmove with overlap\n" );
  24980.       printf( "Source:\t\t%s\n", string2 + 4 );
  24981.       printf( "Destination:\t%s\n", string2 + 10 );
  24982.       memmove( string2 + 10, string2 + 4, 40 );
  24983.       printf( "Result:\t\t%s\n", string2 );
  24984.       printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
  24985.  
  24986.       printf( "Function:\tmemcpy with overlap\n" );
  24987.       printf( "Source:\t\t%s\n", string1 + 4 );
  24988.       printf( "Destination:\t%s\n", string1 + 10 );
  24989.       memcpy( string1 + 10, string1 + 4, 40 );
  24990.       printf( "Result:\t\t%s\n", string1 );
  24991.       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
  24992.  
  24993.    }
  24994.  
  24995.  
  24996.  Output
  24997.  
  24998.  
  24999.  
  25000.    Function:       memcpy without overlap
  25001.    Source:         fox
  25002.    Destination:    dog jumps over the lazy fox
  25003.    Result:         The quick brown fox jumps over the lazy fox
  25004.    Length:         43 characters
  25005.  
  25006.    Function:       memmove with overlap
  25007.    Source:         quick brown fox jumps over the lazy dog
  25008.    Destination:    brown fox jumps over the lazy dog
  25009.    Result:         The quick quick brown fox jumps over the lazy dog
  25010.    Length:         49 characters
  25011.  
  25012.    Function:       memcpy with overlap
  25013.    Source:         quick brown dog jumps over the lazy fox
  25014.    Destination:    brown dog jumps over the lazy fox
  25015.    Result:         The quick quick quick quick quick quick quick quic
  25016.    Length:         50 characters
  25017.  
  25018.  
  25019.  
  25020.  
  25021.  
  25022.  memicmp, _fmemicmp
  25023.  ────────────────────────────────────────────────────────────────────────────
  25024.  
  25025.  
  25026.  Description
  25027.  
  25028.  Compare characters in two buffers (case-insensitive).
  25029.  
  25030.  #include <memory.h>               Required only for function declarations
  25031.  
  25032.  #include <string.h>               Use either STRING.H or MEMORY.H
  25033.  
  25034.    int memicmp( void *buf1, void *buf2, unsigned int count );
  25035.  
  25036.    int _far _fmemicmp( void _far *buf1, void _far *buf2, unsigned int count
  25037.    );
  25038.  
  25039.  buf1                              First buffer
  25040.  
  25041.  buf2                              Second buffer
  25042.  
  25043.  count                             Number of characters
  25044.  
  25045.  
  25046.  Remarks
  25047.  
  25048.  The memicmp and _fmemicmp functions compare the first count characters of
  25049.  the two buffers buf1 and buf2 byte-by-byte. The comparison is made without
  25050.  regard to the case of letters in the two buffers; that is, uppercase and
  25051.  lowercase letters are considered equivalent. The memicmp and _fmemicmp
  25052.  functions return a value indicating the relationship of the two buffers, as
  25053.  follows:
  25054.  
  25055.  Value                             Meaning
  25056.  ────────────────────────────────────────────────────────────────────────────
  25057.  < 0                               buf1 less than buf2
  25058.  
  25059.  = 0                               buf1 identical to buf2
  25060.  
  25061.  > 0                               buf1 greater than buf2
  25062.  
  25063.  The _fmemicmp function is a model-independent (large-model) form of the
  25064.  memicmp function. It can be called from any point in any program.
  25065.  
  25066.  
  25067.  Return Value
  25068.  
  25069.  The memicmp and _fmemicmp functions return an integer value, as described
  25070.  above.
  25071.  
  25072.  
  25073.  Compatibility
  25074.  
  25075.  memicmp
  25076.  
  25077.   ANSI    DOS    OS/2    UNIX    XENIX
  25078.  
  25079.  
  25080.  _fmemicmp
  25081.  
  25082.   ANSI    DOS    OS/2    UNIX    XENIX
  25083.  
  25084.  
  25085.  
  25086.  See Also
  25087.  
  25088.  memccpy, memchr, memcmp, memcpy, memset, stricmp, strnicmp
  25089.  
  25090.  
  25091.  Example
  25092.  
  25093.    /* MEMICMP.C: This program uses memicmp to compare the first
  25094.     * 29 letters of the strings named first and second without
  25095.     * regard to the case of the letters.
  25096.     */
  25097.  
  25098.    #include <memory.h>
  25099.    #include <stdio.h>
  25100.    #include <string.h>
  25101.  
  25102.    void main()
  25103.    {
  25104.       int result;
  25105.       char first[]  = "Those Who Will Not Learn from History";
  25106.       char second[] = "THOSE WHO WILL NOT LEARN FROM their mistakes";
  25107.       /* Note that the 29th character is right here ^ */
  25108.  
  25109.       printf( "Compare '%.29s' to '%.29s'\n", first, second );
  25110.       result = memicmp( first, second, 29 );
  25111.       if( result < 0 )
  25112.          printf( "First is less than second.\n" );
  25113.       else if( result == 0 )
  25114.          printf( "First is equal to second.\n" );
  25115.       else if( result > 0 )
  25116.          printf( "First is greater than second.\n" );
  25117.    }
  25118.  
  25119.  
  25120.  Output
  25121.  
  25122.  
  25123.  
  25124.    Compare 'Those Who Will Not Learn from' to 'THOSE WHO WILL NOT LEARN FROM'
  25125.    First is equal to second.
  25126.  
  25127.  
  25128.  
  25129.  
  25130.  
  25131.  _memmax
  25132.  ────────────────────────────────────────────────────────────────────────────
  25133.  
  25134.  
  25135.  Description
  25136.  
  25137.  Finds the size of the largest contiguous memory block.
  25138.  
  25139.    #include <malloc.h>
  25140.  
  25141.    size_t _memmax( void );
  25142.  
  25143.  
  25144.  Remarks
  25145.  
  25146.  The _memmax function returns the size (in bytes) of the largest contiguous
  25147.  block of memory that can be allocated from the near heap (i.e., the default
  25148.  data segment). Calling _nmalloc with the value returned by the _memmax
  25149.  function will succeed as long as _memmax returns a nonzero value.
  25150.  
  25151.  
  25152.  Return Value
  25153.  
  25154.  The function returns the block size, if successful. Otherwise, it returns 0,
  25155.  indicating that nothing more can be allocated from the near heap.
  25156.  
  25157.  
  25158.  Compatibility
  25159.  
  25160.   ANSI    DOS    OS/2    UNIX    XENIX
  25161.  
  25162.  
  25163.  
  25164.  See Also
  25165.  
  25166.  malloc functions, msize functions
  25167.  
  25168.  
  25169.  Example
  25170.  
  25171.    /* MEMMAX.C: This program uses _memmax and _nmalloc to allocate
  25172.     * the largest block of memory available in the near heap.
  25173.     */
  25174.  
  25175.    #include <stddef.h>
  25176.    #include <malloc.h>
  25177.    #include <stdio.h>
  25178.  
  25179.    void main()
  25180.    {
  25181.       size_t contig;
  25182.       char *p;
  25183.  
  25184.    /* Determine contiguous memory size */
  25185.       contig = _memmax();
  25186.       printf( "Largest block of available memory is %u bytes long\n", contig
  25187.  );
  25188.       if( contig )
  25189.       {
  25190.          p = _nmalloc( contig * sizeof( int ) );
  25191.          if( p == NULL )
  25192.             printf( "Error with malloc (should never occur)\n" );
  25193.          else
  25194.          {
  25195.             printf( "Maximum allocation succeeded\n" );
  25196.             free( p );
  25197.          }
  25198.       }
  25199.       else
  25200.          printf( "Near heap is already full\n" );
  25201.    }
  25202.  
  25203.  
  25204.  Output
  25205.  
  25206.  
  25207.  
  25208.    Largest block of available memory is 60844 bytes long
  25209.    Maximum allocation succeeded
  25210.  
  25211.  
  25212.  
  25213.  
  25214.  
  25215.  memmove, _fmemmove
  25216.  ────────────────────────────────────────────────────────────────────────────
  25217.  
  25218.  
  25219.  Description
  25220.  
  25221.  Move one buffer to another.
  25222.  
  25223.    #include <string.h>
  25224.  
  25225.    void *memmove( void *dest, const void *src, size_t count );
  25226.  
  25227.    void _far * _far _fmemmove( void _far *dest, const void _far *src, size_t
  25228.    count );
  25229.  
  25230.  dest                              Destination object
  25231.  
  25232.  src                               Source object
  25233.  
  25234.  count                             Number of characters to copy
  25235.  
  25236.  
  25237.  Remarks
  25238.  
  25239.  The memmove and _fmemmove functions copy count characters from the source
  25240.  (src) to the destination (dest). If some regions of the source area and the
  25241.  destination overlap, the memmove and _fmemmove functions ensure that the
  25242.  original source bytes in the overlapping region are copied before being
  25243.  overwritten.
  25244.  
  25245.  The _fmemmove function is a model-independent (large-model) form of the
  25246.  memmove function. It can be called from any point in any program.
  25247.  
  25248.  
  25249.  Return Value
  25250.  
  25251.  The memmove and _fmemmove functions return the value of dest.
  25252.  
  25253.  
  25254.  Compatibility
  25255.  
  25256.  memmove
  25257.  
  25258.   ANSI    DOS    OS/2    UNIX    XENIX
  25259.  
  25260.  
  25261.  _fmemmove
  25262.  
  25263.   ANSI    DOS    OS/2    UNIX    XENIX
  25264.  
  25265.  
  25266.  
  25267.  See Also
  25268.  
  25269.  memccpy, memcpy, strccpy, strncpy
  25270.  
  25271.  
  25272.  Example
  25273.  
  25274.    /* MEMCPY.C. Illustrate overlapping copy: memmove handles it
  25275.     * correctly; memcpy does not.
  25276.     */
  25277.    #include <memory.h>
  25278.    #include <string.h>
  25279.    #include <stdio.h>
  25280.  
  25281.    char string1[60] = "The quick brown dog jumps over the lazy fox";
  25282.    char string2[60] = "The quick brown fox jumps over the lazy dog";
  25283.    /*                           1         2         3         4         5
  25284.     *                  12345678901234567890123456789012345678901234567890
  25285.     */
  25286.    void main()
  25287.    {
  25288.       printf( "Function:\tmemcpy without overlap\n" );
  25289.       printf( "Source:\t\t%s\n", string1 + 40 );
  25290.       printf( "Destination:\t%s\n", string1 + 16 );
  25291.       memcpy( string1 + 16, string1 + 40, 3 );
  25292.       printf( "Result:\t\t%s\n", string1 );
  25293.       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
  25294.  
  25295.       /* Restore string1 to original contents */
  25296.       memcpy( string1 + 16, string2 + 40, 3 );
  25297.  
  25298.       printf( "Function:\tmemmove with overlap\n" );
  25299.       printf( "Source:\t\t%s\n", string2 + 4 );
  25300.       printf( "Destination:\t%s\n", string2 + 10 );
  25301.       memmove( string2 + 10, string2 + 4, 40 );
  25302.       printf( "Result:\t\t%s\n", string2 );
  25303.       printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
  25304.  
  25305.       printf( "Function:\tmemcpy with overlap\n" );
  25306.       printf( "Source:\t\t%s\n", string1 + 4 );
  25307.       printf( "Destination:\t%s\n", string1 + 10 );
  25308.       memcpy( string1 + 10, string1 + 4, 40 );
  25309.       printf( "Result:\t\t%s\n", string1 );
  25310.       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
  25311.  
  25312.    }
  25313.  
  25314.  
  25315.  Output
  25316.  
  25317.  
  25318.  
  25319.    Function:       memcpy without overlap
  25320.    Source:         fox
  25321.    Destination:    dog jumps over the lazy fox
  25322.    Result:         The quick brown fox jumps over the lazy fox
  25323.    Length:         43 characters
  25324.  
  25325.    Function:       memmove with overlap
  25326.    Source:         quick brown fox jumps over the lazy dog
  25327.    Destination:    brown fox jumps over the lazy dog
  25328.    Result:         The quick quick brown fox jumps over the lazy dog
  25329.    Length:         49 characters
  25330.  
  25331.    Function:       memcpy with overlap
  25332.    Source:         quick brown dog jumps over the lazy fox
  25333.    Destination:    brown dog jumps over the lazy fox
  25334.    Result:         The quick quick quick quick quick quick quick quic
  25335.    Length:         50 characters
  25336.  
  25337.  
  25338.  
  25339.  
  25340.  
  25341.  memset, _fmemset
  25342.  ────────────────────────────────────────────────────────────────────────────
  25343.  
  25344.  
  25345.  Description
  25346.  
  25347.  Set buffers to a specified character.
  25348.  
  25349.  #include <memory.h>               Required only for function declarations
  25350.  
  25351.  #include <string.h>               Use either STRING.H (for ANSI
  25352.                                    compatibility) or MEMORY.H
  25353.  
  25354.    void *memset( void *dest, int c, size_t count );
  25355.  
  25356.    void _far * _far _fmemset( void _far *dest, int c, size_t count );
  25357.  
  25358.  dest                              Pointer to destination
  25359.  
  25360.  c                                 Character to set
  25361.  
  25362.  count                             Number of characters
  25363.  
  25364.  
  25365.  Remarks
  25366.  
  25367.  The memset and _fmemset functions set the first count bytes of dest to the
  25368.  character c.
  25369.  
  25370.  The _fmemset function is a model-independent (large-model) form of the
  25371.  memset function. It can be called from any point in any program.
  25372.  
  25373.  There is a semantic difference between the function version of memset and
  25374.  its intrinsic version. The function version supports huge pointers in
  25375.  compact-, large-, and huge-model programs, but the intrinsic version does
  25376.  not.
  25377.  
  25378.  
  25379.  Return Value
  25380.  
  25381.  The memset and _fmemset functions return a pointer to dest.
  25382.  
  25383.  
  25384.  Compatibility
  25385.  
  25386.  memset
  25387.  
  25388.   ANSI    DOS    OS/2    UNIX    XENIX
  25389.  
  25390.  
  25391.  _fmemset
  25392.  
  25393.   ANSI    DOS    OS/2   UNIX    XENIX
  25394.  
  25395.  
  25396.  
  25397.  See Also
  25398.  
  25399.  memccpy, memchr, memcmp, memcpy, strnset
  25400.  
  25401.  
  25402.  Example
  25403.  
  25404.    /* MEMSET.C: This program uses memset to set the first four bytes
  25405.     * of buffer to "*".
  25406.     */
  25407.  
  25408.    #include <memory.h>
  25409.    #include <stdio.h>
  25410.  
  25411.    void main()
  25412.    {
  25413.       char buffer[] = "This is a test of the memset function";
  25414.  
  25415.       printf( "Before: %s\n", buffer );
  25416.       memset( buffer, '*', 4 );
  25417.       printf( "After:  %s\n", buffer );
  25418.    }
  25419.  
  25420.  
  25421.  Output
  25422.  
  25423.  
  25424.  
  25425.    Before: This is a test of the memset function
  25426.    After:  **** is a test of the memset function
  25427.  
  25428.  
  25429.  
  25430.  
  25431.  
  25432.  min
  25433.  ────────────────────────────────────────────────────────────────────────────
  25434.  
  25435.  
  25436.  Description
  25437.  
  25438.  Returns the smaller of two values.
  25439.  
  25440.    #include <stdlib.h>
  25441.  
  25442.    type min( type a, type b );
  25443.  
  25444.  type                              Any numeric data type
  25445.  
  25446.  a, b                              Values of any numeric type to be
  25447.                                    compared
  25448.  
  25449.  
  25450.  Remarks
  25451.  
  25452.  The min macro compares two values and returns the value of the smaller one.
  25453.  The arguments can be of any numeric data type, signed or unsigned. Both
  25454.  arguments and the return value must be of the same data type.
  25455.  
  25456.  
  25457.  Return Value
  25458.  
  25459.  The macro returns the smaller of the two arguments.
  25460.  
  25461.  
  25462.  Compatibility
  25463.  
  25464.   ANSI    DOS    OS/2    UNIX    XENIX
  25465.  
  25466.  
  25467.  
  25468.  See Also
  25469.  
  25470.  max
  25471.  
  25472.  
  25473.  Example
  25474.  
  25475.    /* MINMAX.C */
  25476.    #include <stdlib.h>
  25477.    #include <stdio.h>
  25478.  
  25479.    void main()
  25480.    {
  25481.       int a = 10;
  25482.       int b = 21;
  25483.  
  25484.       printf( "The larger of %d and %d is %d\n",  a, b, max( a, b ) );
  25485.       printf( "The smaller of %d and %d is %d\n", a, b, min( a, b ) );
  25486.    }
  25487.  
  25488.  
  25489.  Output
  25490.  
  25491.  
  25492.  
  25493.    The larger of 10 and 21 is 21
  25494.    The smaller of 10 and 21 is 10
  25495.  
  25496.  
  25497.  
  25498.  
  25499.  
  25500.  mkdir
  25501.  ────────────────────────────────────────────────────────────────────────────
  25502.  
  25503.  
  25504.  Description
  25505.  
  25506.  Creates a new directory.
  25507.  
  25508.  #include <direct.h>               Required only for function declarations
  25509.  
  25510.    int mkdir( char *dirname );
  25511.  
  25512.  dirname                           Path name for new directory
  25513.  
  25514.  
  25515.  Remarks
  25516.  
  25517.  The mkdir function creates a new directory with the specified dirname. Only
  25518.  one directory can be created at a time, so only the last component of
  25519.  dirname can name a new directory.
  25520.  
  25521.  The mkdir function does not do any translation of path-name delimiters. Both
  25522.  DOS and OS/ 2 accept either " \" or "/ "  internally as valid delimiters
  25523.  within path names.
  25524.  
  25525.  
  25526.  Return Value
  25527.  
  25528.  The mkdir function returns the value 0 if the new directory was created. A
  25529.  return value of -1 indicates an error, and errno is set to one of the
  25530.  following values:
  25531.  
  25532.  Value                             Meaning
  25533.  ────────────────────────────────────────────────────────────────────────────
  25534.  EACCES                            Directory not created. The given name is
  25535.                                    the name of an
  25536.                                    existing file, directory, or device.
  25537.  
  25538.  ENOENT                            Path name not found.
  25539.  
  25540.  
  25541.  Compatibility
  25542.  
  25543.   ANSI    DOS    OS/2    UNIX    XENIX
  25544.  
  25545.  
  25546.  
  25547.  See Also
  25548.  
  25549.  chdir, rmdir
  25550.  
  25551.  
  25552.  Example
  25553.  
  25554.    /* MAKEDIR.C */
  25555.    #include <direct.h>
  25556.    #include <stdlib.h>
  25557.    #include <stdio.h>
  25558.  
  25559.    void main()
  25560.    {
  25561.       int result;
  25562.  
  25563.       if( mkdir( "\\testtmp" ) == 0 )
  25564.       {
  25565.          printf( "Directory '\\testtmp' was successfully created\n" );
  25566.          system( "dir \\testtmp" );
  25567.          if( rmdir( "\\testtmp" ) == 0 )
  25568.             printf( "Directory '\\testtmp' was successfully removed\n"  );
  25569.          else
  25570.             printf( "Problem removing directory '\\testtmp'\n" );
  25571.       }
  25572.       else
  25573.          printf( "Problem creating directory '\\testtmp'\n" );
  25574.    }
  25575.  
  25576.  
  25577.  Output
  25578.  
  25579.  
  25580.  
  25581.    Directory '\testtmp' was successfully created
  25582.  
  25583.     The volume label in drive C is OS2.
  25584.     Directory of C:\TESTTMP
  25585.  
  25586.    .            <DIR>      6-19-89  11:20a
  25587.    ..           <DIR>      6-19-89  11:20a
  25588.         2 File(s)   12730368 bytes free
  25589.    Directory '\testtmp' was successfully removed
  25590.  
  25591.  
  25592.  
  25593.  
  25594.  
  25595.  mktemp
  25596.  ────────────────────────────────────────────────────────────────────────────
  25597.  
  25598.  
  25599.  Description
  25600.  
  25601.  Creates a unique file name.
  25602.  
  25603.  #include <io.h>                   Required only for function declarations
  25604.  
  25605.    char *mktemp( char *template );
  25606.  
  25607.  template                          File-name pattern
  25608.  
  25609.  
  25610.  Remarks
  25611.  
  25612.  The mktemp function creates a unique file name by modifying the given
  25613.  template argument. The template argument has the form:
  25614.  
  25615.  baseXXXXXX
  25616.  
  25617.  where base is the part of the new file name that you supply, and the X's are
  25618.  placeholders for the part supplied by mktemp; mktemp preserves base and
  25619.  replaces the six trailing X's with an alphanumeric character followed by a
  25620.  five-digit value. The five-digit value is a unique number identifying the
  25621.  calling process. The alphanumeric character is 0 ('0') the first time mktemp
  25622.  is called with a given template.
  25623.  
  25624.  In subsequent calls from the same process with copies of the same template,
  25625.  mktemp checks to see if previously returned names have been used to create
  25626.  files. If no file exists for a given name, mktemp returns that name. If
  25627.  files exist for all previously returned names, mktemp creates a new name by
  25628.  replacing the alphanumeric character in the name with the next available
  25629.  lowercase letter. For example, if the first name returned is  t012345  and
  25630.  this name is used to create a file, the next name returned will be  ta12345.
  25631.  When creating new names, mktemp uses, in order, '0' and then the lowercase
  25632.  letters 'a' through 'z'.
  25633.  
  25634.  Note that the original template is modified by the first call to mktemp. If
  25635.  you then call the mktemp function again with the same template (i.e., the
  25636.  original one), you will get an error.
  25637.  
  25638.  The mktemp function generates unique file names but does not create or open
  25639.  files.
  25640.  
  25641.  
  25642.  Return Value
  25643.  
  25644.  The mktemp function returns a pointer to the modified template. The return
  25645.  value is NULL if the template argument is badly formed or no more unique
  25646.  names can be created from the given template.
  25647.  
  25648.  
  25649.  Compatibility
  25650.  
  25651.   ANSI    DOS    OS/2    UNIX    XENIX
  25652.  
  25653.  
  25654.  
  25655.  See Also
  25656.  
  25657.  fopen, getpid, open, tempnam, tmpfile
  25658.  
  25659.  
  25660.  Example
  25661.  
  25662.    /* MKTEMP.C: The program uses mktemp to create five unique file names.
  25663.     * It opens each file name to ensure that the next name is unique.
  25664.     */
  25665.  
  25666.    #include <io.h>
  25667.    #include <string.h>
  25668.    #include <stdio.h>
  25669.  
  25670.    char *template = "fnXXXXXX";
  25671.    char *result;
  25672.    char names[5][9];
  25673.  
  25674.    void main()
  25675.    {
  25676.       int i;
  25677.       FILE *fp;
  25678.  
  25679.       for( i = 0; i < 5; i++ )
  25680.       {
  25681.          strcpy( names[i], template );
  25682.  
  25683.          /* Attempt to find a unique file name: */
  25684.          result = mktemp( names[i] );
  25685.          if( result == NULL )
  25686.             printf( "Problem creating the template" );
  25687.          else
  25688.          {
  25689.             if( (fp = fopen( result, "w" )) != NULL )
  25690.                 printf( "Unique file name is %s\n", result );
  25691.             else
  25692.                 printf( "Cannot open %s\n", result );
  25693.             fclose( fp );
  25694.          }
  25695.       }
  25696.    }
  25697.  
  25698.  
  25699.  Output
  25700.  
  25701.  
  25702.  
  25703.    Unique file name is fn000686
  25704.    Unique file name is fna00686
  25705.    Unique file name is fnb00686
  25706.    Unique file name is fnc00686
  25707.    Unique file name is fnd00686
  25708.  
  25709.  
  25710.  
  25711.  
  25712.  
  25713.  mktime
  25714.  ────────────────────────────────────────────────────────────────────────────
  25715.  
  25716.  
  25717.  Description
  25718.  
  25719.  Converts the local time to a calendar value.
  25720.  
  25721.    #include <time.h>
  25722.  
  25723.    time_t mktime( struct tm *timeptr );
  25724.  
  25725.  timeptr                           Pointer to time structure
  25726.  
  25727.  
  25728.  Remarks
  25729.  
  25730.  The mktime function converts the supplied time structure (possibly
  25731.  incomplete) pointed to by timeptr into a fully defined structure with
  25732.  "normalized" values and then converts it to a time_t calendar time value.
  25733.  The structure for the tm is described in the reference page for asctime.
  25734.  
  25735.  The converted time has the same encoding as the values returned by the time
  25736.  function. The original values of the tm_wday and tm_yday components of the
  25737.  timeptr structure are ignored, and the original values of the other
  25738.  components are not restricted to their normal ranges.
  25739.  
  25740.  If successful, mktime sets the values of tm_wday and tm_yday appropriately,
  25741.  and sets the other components to represent the specified calendar time, but
  25742.  with their values forced to the normal ranges; the final value of tm_mday is
  25743.  not set until tm_mon and tm_year are determined.
  25744.  
  25745.  DOS and OS/2 do not accommodate dates prior to 1980. If timeptr references a
  25746.  date before January 1, 1980, mktime returns -1.
  25747.  
  25748.  Note that the gmtime and localtime functions use a single statically
  25749.  allocated buffer for the conversion. If you supply this buffer to mktime,
  25750.  the previous contents will be destroyed.
  25751.  
  25752.  
  25753.  Return Value
  25754.  
  25755.  The mktime function returns the specified calendar time encoded as a value
  25756.  of type time_t. If the calendar time cannot be represented, the function
  25757.  returns the value -1 cast as type time_t.
  25758.  
  25759.  
  25760.  Compatibility
  25761.  
  25762.   ANSI    DOS    OS/2    UNIX    XENIX
  25763.  
  25764.  
  25765.  
  25766.  See Also
  25767.  
  25768.  asctime, gmtime, localtime, time
  25769.  
  25770.  
  25771.  Example
  25772.  
  25773.    /* MKTIME.C: The example takes a number of days as input and returns
  25774.     * the time, the current date, and the specified number of days.
  25775.     */
  25776.  
  25777.    #include <time.h>
  25778.    #include <stdio.h>
  25779.  
  25780.    void main()
  25781.    {
  25782.       struct tm when;
  25783.       time_t now, result;
  25784.       int    days;
  25785.  
  25786.       time( &now );
  25787.       when = *localtime( &now );
  25788.       printf( "Current time is %s\n", asctime( &when ) );
  25789.       printf( "How many days to look ahead: " );
  25790.       scanf( "%d", &days );
  25791.  
  25792.       when.tm_mday = when.tm_mday + days;
  25793.       if( (result = mktime( &when )) != (time_t)-1 )
  25794.          printf( "In %d days the time will be %s\n",
  25795.                  days, asctime( &when ) );
  25796.       else
  25797.          perror( "mktime failed" );
  25798.    }
  25799.  
  25800.  
  25801.  Output
  25802.  
  25803.  
  25804.  
  25805.    Current time is Mon Jun 19 11:45:20 1989
  25806.  
  25807.    How many days to look ahead: 23
  25808.    In 23 days the time will be Wed Jul 12 11:45:20 1989
  25809.  
  25810.  
  25811.  
  25812.  
  25813.  
  25814.  modf, modfl
  25815.  ────────────────────────────────────────────────────────────────────────────
  25816.  
  25817.  
  25818.  Description
  25819.  
  25820.  Split a floating-point value into a mantissa and an exponent.
  25821.  
  25822.    #include <math.h>
  25823.  
  25824.    double modf( double x, double *intptr );
  25825.  
  25826.    long double modfl( long double x, long double *intptr );
  25827.  
  25828.  x                                 Floating-point value
  25829.  
  25830.  intptr                            Pointer to stored integer portion
  25831.  
  25832.  
  25833.  Remarks
  25834.  
  25835.  The modf functions break down the floating-point value x into fractional and
  25836.  integer parts, each of which has the same sign as x. The signed fractional
  25837.  portion of x is returned. The integer portion is stored as a floating-point
  25838.  value at  intptr.
  25839.  
  25840.  The modfl function uses the 80-bit, 10-byte coprocessor form of arguments
  25841.  and return values. See the reference page on the long double functions for
  25842.  more details on this data type.
  25843.  
  25844.  
  25845.  Return Value
  25846.  
  25847.  The modf and modfl functions return the signed fractional portion of x.
  25848.  There is no error return.
  25849.  
  25850.  
  25851.  Compatibility
  25852.  
  25853.  modf
  25854.  
  25855.   ANSI    DOS    OS/2    UNIX    XENIX
  25856.  
  25857.  
  25858.  modfl
  25859.  
  25860.   ANSI    DOS    OS/2    UNIX    XENIX
  25861.  
  25862.  
  25863.  
  25864.  See Also
  25865.  
  25866.  frexp, ldexp
  25867.  
  25868.  
  25869.  Example
  25870.  
  25871.    /* MODF.C */
  25872.    #include <math.h>
  25873.    #include <stdio.h>
  25874.  
  25875.    void main()
  25876.    {
  25877.       double x, y, n;
  25878.  
  25879.       x = -14.87654321;       /* Divide x into its fractional */
  25880.       y = modf( x, &n );      /* and integer parts            */
  25881.  
  25882.       printf( "For %f, the fraction is %f and the integer is %.f\n", x, y, n
  25883.  );
  25884.    }
  25885.  
  25886.  
  25887.  Output
  25888.  
  25889.  
  25890.  
  25891.    For -14.876543, the fraction is -0.876543 and the integer is -14
  25892.  
  25893.  
  25894.  
  25895.  
  25896.  
  25897.  movedata
  25898.  ────────────────────────────────────────────────────────────────────────────
  25899.  
  25900.  
  25901.  Description
  25902.  
  25903.  Moves characters to another segment.
  25904.  
  25905.  #include <memory.h>               Required only for function declarations
  25906.  
  25907.  #include <string.h>               Use either STRING.H (for ANSI
  25908.                                    compatibility) or MEMORY.H
  25909.  
  25910.    void movedata( unsigned int srcseg, unsigned int srcoff, unsigned int
  25911.    destseg,
  25912.    unsigned int destoff, unsigned int count );
  25913.  
  25914.  srcseg                            Segment address of source
  25915.  
  25916.  srcoff                            Segment offset of source
  25917.  
  25918.  destseg                           Segment address of destination
  25919.  
  25920.  destoff                           Segment offset of destination
  25921.  
  25922.  count                             Number of bytes
  25923.  
  25924.  
  25925.  Remarks
  25926.  
  25927.  The movedata function copies count bytes from the source address specified
  25928.  by srcseg:srcoff to the destination address specified by destseg:destoff.
  25929.  
  25930.  The movedata function was intended to move far data in small-data-model
  25931.  programs. The newer model-independent _fmemcpy and _fmemmove functions
  25932.  should be used instead of the movedata function. In large-data-model
  25933.  programs, the memcpy and memmove functions can also be used.
  25934.  
  25935.  Segment values for the srcseg and destseg arguments can be obtained by using
  25936.  either the segread function or the FP_SEG macro.
  25937.  
  25938.  The movedata function does not handle all cases of overlapping moves
  25939.  correctly. These occur when part of the destination is the same memory area
  25940.  as part of the source. The memmove function correctly handles overlapping
  25941.  moves.
  25942.  
  25943.  
  25944.  Return Value
  25945.  
  25946.  None.
  25947.  
  25948.  
  25949.  Compatibility
  25950.  
  25951.   ANSI    DOS    OS/2    UNIX    XENIX
  25952.  
  25953.  
  25954.  
  25955.  See Also
  25956.  
  25957.  FP_OFF, FP_SEG, memcpy, memmove, segread
  25958.  
  25959.  
  25960.  Example
  25961.  
  25962.    /* MOVEDATA.C */
  25963.    #include <memory.h>
  25964.    #include <stdio.h>
  25965.    #include <string.h>
  25966.    #include <dos.h>
  25967.    #include <malloc.h>
  25968.  
  25969.    char _far *src = "This is a test.";
  25970.  
  25971.    void main()
  25972.    {
  25973.       char _far *dest;
  25974.  
  25975.       if( (dest = _fmalloc( 80 )) != NULL )
  25976.       {
  25977.          movedata( FP_SEG( src ),  FP_OFF( src ),
  25978.                    FP_SEG( dest ), FP_OFF( dest ), _fstrlen( src ) + 1 );
  25979.          printf( "The source data at %Fp is '%Fs'\n", src, src );
  25980.          printf( "The destination data at %Fp is '%Fs'\n", dest, dest );
  25981.          _ffree( dest );
  25982.       }
  25983.    }
  25984.  
  25985.  
  25986.  Output
  25987.  
  25988.  
  25989.  
  25990.    The source data at 2D0A:02B8 is 'This is a test.'
  25991.    The destination data at 3D0B:0016 is 'This is a test.'
  25992.  
  25993.  
  25994.  
  25995.  
  25996.  
  25997.  _moveto Functions
  25998.  ────────────────────────────────────────────────────────────────────────────
  25999.  
  26000.  
  26001.  Description
  26002.  
  26003.  Move current graphics positions.
  26004.  
  26005.    #include <graph.h>
  26006.  
  26007.    struct xycoord _far _moveto( short x, short y );
  26008.  
  26009.    struct _wxycoord _far _moveto_w( double wx, double wy );
  26010.  
  26011.  x, y                              View-coordinate point
  26012.  
  26013.  wx, wy                            Window-coordinate point
  26014.  
  26015.  
  26016.  Remarks
  26017.  
  26018.  The _moveto functions move the current position to the specified point. The
  26019.  _moveto function uses the view-coordinate point (x, y) as the current
  26020.  position. The _moveto_w function uses the window-coordinate point (wx, wy)
  26021.  as the current position. No drawing takes place.
  26022.  
  26023.  
  26024.  Return Value
  26025.  
  26026.  The function returns the coordinates of the previous position. The _moveto
  26027.  function returns the coordinates in an xycoord structure. The xycoord
  26028.  structure, defined in GRAPH.H, contains the following elements:
  26029.  
  26030.  Element                           Description
  26031.  ────────────────────────────────────────────────────────────────────────────
  26032.  short xcoord                      x coordinate
  26033.  
  26034.  short ycoord                      y coordinate
  26035.  
  26036.  The _moveto_w function returns the coordinates in an _wxycoord structure,
  26037.  defined in GRAPH.H. The _wxycoord structure contains the following elements:
  26038.  
  26039.  
  26040.  Element                           Description
  26041.  ────────────────────────────────────────────────────────────────────────────
  26042.  double wx                         x window coordinate
  26043.  
  26044.  double wy                         y window coordinate
  26045.  
  26046.  
  26047.  Compatibility
  26048.  
  26049.   ANSI    DOS    OS/2    UNIX    XENIX
  26050.  
  26051.  
  26052.  
  26053.  See Also
  26054.  
  26055.  _lineto functions
  26056.  
  26057.  
  26058.  Example
  26059.  
  26060.    /* MOVETO.C: This program draws line segments of different colors. */
  26061.  
  26062.    #include <graph.h>
  26063.    #include <stdlib.h>
  26064.    #include <conio.h>
  26065.  
  26066.    void main()
  26067.    {
  26068.       short x, y, xinc, yinc, color = 1;
  26069.       struct videoconfig v;
  26070.  
  26071.       /* Find a valid graphics mode. */
  26072.       if( !_setvideomode( _MAXCOLORMODE ) )
  26073.          exit( 1 );
  26074.       _getvideoconfig( &v );
  26075.       xinc = v.numxpixels / 50;
  26076.       yinc = v.numypixels / 50;
  26077.  
  26078.       for( x = 0, y = v.numypixels - 1; x < v.numxpixels; x += xinc, y -=
  26079.  yinc )
  26080.       {
  26081.          _setcolor( color++ % 16 );
  26082.          _moveto( x, 0 );
  26083.          _lineto( 0, y );
  26084.       }
  26085.       getch();
  26086.  
  26087.       _setvideomode( _DEFAULTMODE );
  26088.    }
  26089.  
  26090.  
  26091.  
  26092.  
  26093.  
  26094.  
  26095.  _msize Functions
  26096.  ────────────────────────────────────────────────────────────────────────────
  26097.  
  26098.  
  26099.  Description
  26100.  
  26101.  Return the size of a memory block allocated in the heap.
  26102.  
  26103.  #include <malloc.h>               Required only for function declarations
  26104.  
  26105.    size_t  _msize( void *memblock );
  26106.  
  26107.    size_t _bmsize( _segment seg, void _based( void ) *memblock );
  26108.  
  26109.    size_t _fmsize( void _far *memblock );
  26110.  
  26111.    size_t _nmsize( void _near *memblock );
  26112.  
  26113.  memblock                          Pointer to memory block
  26114.  
  26115.  seg                               Based-heap segment selector
  26116.  
  26117.  
  26118.  Remarks
  26119.  
  26120.  The _msize family of functions returns the size, in bytes, of the memory
  26121.  block allocated by a call to the appropriate version of the calloc, malloc,
  26122.  or realloc functions.
  26123.  
  26124.  In large data models (compact-, large-, and huge-model programs), _msize
  26125.  maps to _fmsize. In small data models (tiny-, small-, and medium-model
  26126.  programs), _msize maps to _nmsize.
  26127.  
  26128.  The _nmsize function returns the size (in bytes) of the memory block
  26129.  allocated by a call to _nmalloc, and the _fmsize function returns the size
  26130.  (in bytes) of the memory block allocated by a call to _fmalloc or _frealloc.
  26131.  The _bmsize function returns the size of a block allocated in segment seg by
  26132.  a call to _bmalloc, _bcalloc, or _brealloc.
  26133.  
  26134.  The location of the memory block is indicated below:
  26135.  
  26136.  Function                          Data Segment
  26137.  ────────────────────────────────────────────────────────────────────────────
  26138.  _msize                            Depends on data model of program
  26139.  
  26140.  _bmsize                           Based heap segment specified by seg
  26141.                                    value
  26142.  
  26143.  _fmsize                           Far heap segment (outside default data
  26144.                                    segment)
  26145.  
  26146.  _nmsize                           Default data segment (inside near heap)
  26147.  
  26148.  
  26149.  Return Value
  26150.  
  26151.  All four functions return the size (in bytes) as an unsigned integer.
  26152.  
  26153.  
  26154.  Compatibility
  26155.  
  26156.   ANSI    DOS    OS/2    UNIX    XENIX
  26157.  
  26158.  
  26159.  
  26160.  See Also
  26161.  
  26162.  calloc functions,  _expand functions, malloc functions, realloc functions
  26163.  
  26164.  
  26165.  Example
  26166.  
  26167.    /* REALLOC.C: This program allocates a block of memory for buffer
  26168.     * and then uses _msize to display the size of that block. Next, it
  26169.     * uses realloc to expand the amount of memory used by buffer
  26170.     * and then calls _msize again to display the new amount of
  26171.     * memory allocated to buffer.
  26172.     */
  26173.  
  26174.    #include <stdio.h>
  26175.    #include <malloc.h>
  26176.    #include <stdlib.h>
  26177.  
  26178.    void main()
  26179.    {
  26180.       long *buffer;
  26181.       size_t size;
  26182.  
  26183.       if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
  26184.          exit( 1 );
  26185.  
  26186.       size = _msize( buffer );
  26187.       printf( "Size of block after malloc of 1000 longs: %u\n", size );
  26188.  
  26189.       /* Reallocate and show new size: */
  26190.       if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) ==
  26191.  NULL )
  26192.          exit( 1 );
  26193.       size = _msize( buffer );
  26194.       printf( "Size of block after realloc of 1000 more longs: %u\n", size );
  26195.  
  26196.       free( buffer );
  26197.    }
  26198.  
  26199.  
  26200.  Output
  26201.  
  26202.  
  26203.  
  26204.    Size of block after malloc of 1000 longs: 4000
  26205.    Size of block after realloc of 1000 more longs: 8000
  26206.  
  26207.  
  26208.  
  26209.  
  26210.  
  26211.  onexit
  26212.  ────────────────────────────────────────────────────────────────────────────
  26213.  
  26214.  
  26215.  Description
  26216.  
  26217.  Registers a routine to be called at exit time.
  26218.  
  26219.    #include <stdlib.h>
  26220.  
  26221.    onexit_t onexit( onexit_t func );
  26222.  
  26223.  func                              Pointer to function to be called at exit
  26224.  
  26225.  
  26226.  Remarks
  26227.  
  26228.  The onexit function is passed the address of a function (func) to be called
  26229.  when the program terminates normally. Successive calls to onexit create a
  26230.  register of functions that is executed in LIFO (last-in-first-out) order. No
  26231.  more than 32 functions can be registered with onexit; onexit returns the
  26232.  value NULL if the number of functions exceeds 32. The functions passed to
  26233.  onexit cannot take parameters.
  26234.  
  26235.  The onexit function is not part of the ANSI definition, but is instead a
  26236.  Microsoft extension. The ANSI-standard atexit function does the same thing
  26237.  as onexit, and should be used instead of onexit when ANSI portability is
  26238.  desired.
  26239.  
  26240.  All routines passed to onexit should have the _loadds attribute if used in
  26241.  multithread dynamic-link libraries.
  26242.  
  26243.  
  26244.  Return Value
  26245.  
  26246.  The onexit function returns a pointer to the function if successful and
  26247.  returns NULL if there is no space left to store the function pointer.
  26248.  
  26249.  
  26250.  Compatibility
  26251.  
  26252.   ANSI   DOS   OS/2   UNIX   XENIX
  26253.  
  26254.  
  26255.  
  26256.  See Also
  26257.  
  26258.  exit
  26259.  
  26260.  
  26261.  Example
  26262.  
  26263.    /* ONEXIT.C */
  26264.    #include <stdlib.h>
  26265.    #include <stdio.h>
  26266.  
  26267.    /* Prototypes */
  26268.    void fn1( void ), fn2( void ), fn3( void ),  fn4( void );
  26269.  
  26270.    void main()
  26271.    {
  26272.       onexit( fn1 );
  26273.       onexit( fn2 );
  26274.       onexit( fn3 );
  26275.       onexit( fn4 );
  26276.       printf( "This is executed first.\n" );
  26277.    }
  26278.  
  26279.    void fn1()
  26280.    {
  26281.       printf( "next.\n" );
  26282.    }
  26283.  
  26284.    void fn2()
  26285.    {
  26286.       printf( "executed " );
  26287.    }
  26288.  
  26289.    void fn3()
  26290.    {
  26291.       printf( "is " );
  26292.    }
  26293.  
  26294.    void fn4()
  26295.    {
  26296.       printf( "This " );
  26297.    }
  26298.  
  26299.  
  26300.  Output
  26301.  
  26302.  
  26303.  
  26304.    This is executed first.
  26305.    This is executed next.
  26306.  
  26307.  
  26308.  
  26309.  
  26310.  
  26311.  open
  26312.  ────────────────────────────────────────────────────────────────────────────
  26313.  
  26314.  
  26315.  Description
  26316.  
  26317.  Opens a file.
  26318.  
  26319.  #include <fcntl.h>
  26320.  
  26321.  #include <sys\types.h>
  26322.  
  26323.  #include <sys\stat.h>
  26324.  
  26325.  #include <io.h>                   Required only for function declarations
  26326.  
  26327.    int open( char *filename, int oflag [[, int pmode]] );
  26328.  
  26329.  filename                          File name
  26330.  
  26331.  oflag                             Type of operations allowed
  26332.  
  26333.  pmode                             Permission mode
  26334.  
  26335.  
  26336.  Remarks
  26337.  
  26338.  The open function opens the file specified by filename and prepares the file
  26339.  for subsequent reading or writing, as defined by oflag. The oflag argument
  26340.  is an integer expression formed from one or more of the manifest constants
  26341.  defined in FCNTL.H (listed below). When two or more manifest constants are
  26342.  used to form the oflag argument, the constants are combined with the
  26343.  bitwise-OR operator ( | ). See Section 2.5, "File Handling," for a
  26344.  discussion of binary and text modes.
  26345.  
  26346.  The FCNTL.H file defines the following manifest constants:
  26347.  
  26348.  Constant                          Meaning
  26349.  ────────────────────────────────────────────────────────────────────────────
  26350.  O_APPEND                          Repositions the file pointer to the end
  26351.                                    of the file before every write
  26352.                                    operation.
  26353.  
  26354.  O_BINARY                          Opens file in binary (untranslated)
  26355.                                    mode.
  26356.  
  26357.  O_CREAT                           Creates and opens a new file for
  26358.                                    writing; this has no effect if the file
  26359.                                    specified by filename exists.
  26360.  
  26361.  O_EXCL                            Returns an error value if the file
  26362.                                    specified by filename exists. Only
  26363.                                    applies when used with O_CREAT.
  26364.  
  26365.  O_RDONLY                          Opens file for reading only; if this
  26366.                                    flag is given, neither O_RDWR nor
  26367.                                    O_WRONLY can be given.
  26368.  
  26369.  O_RDWR                            Opens file for both reading and writing;
  26370.                                    if this flag is given, neither O_RDONLY
  26371.                                    nor O_WRONLY can be given.
  26372.  
  26373.  O_TEXT                            Opens file in text (translated) mode.
  26374.  
  26375.  O_TRUNC                           Opens and truncates an existing file to
  26376.                                    zero length; the file must have write
  26377.                                    permission. The contents of the file are
  26378.                                    destroyed. If this flag is given, you
  26379.                                    cannot specify O_RDONLY.
  26380.  
  26381.  O_WRONLY                          Opens file for writing only; if this
  26382.                                    flag is given, neither O_RDONLY nor
  26383.                                    O_RDWR can be given.
  26384.  
  26385.  ────────────────────────────────────────────────────────────────────────────
  26386.  WARNING
  26387.  
  26388.  Use the O_TRUNC flag with care, as it destroys the complete contents of an
  26389.  existing file.
  26390.  ────────────────────────────────────────────────────────────────────────────
  26391.  
  26392.  Either O_RDONLY, O_RDWR, or O_WRONLY must be given to specify the access
  26393.  mode. There is no default value for the access mode.
  26394.  
  26395.  The pmode argument is required only when O_CREAT is specified. If the file
  26396.  exists, pmode is ignored. Otherwise, pmode specifies the file's permission
  26397.  settings, which are set when the new file is closed for the first time. The
  26398.  pmode is an integer expression containing one or both of the manifest
  26399.  constants S_IWRITE and S_IREAD, defined in SYS\STAT.H. When both constants
  26400.  are given, they are joined with the bitwise-OR operator ( | ). The meaning
  26401.  of the pmode argument is as follows:
  26402.  
  26403.  Value                             Meaning
  26404.  ────────────────────────────────────────────────────────────────────────────
  26405.  S_IWRITE                          Writing permitted
  26406.  
  26407.  S_IREAD                           Reading permitted
  26408.  
  26409.  S_IREAD |  S_IWRITE               Reading and writing permitted
  26410.  
  26411.  If write permission is not given, the file is read-only. Under DOS and OS/2,
  26412.  all files are readable; it is not possible to give write-only permission.
  26413.  Thus the modes S_IWRITE and S_IREAD | S_IWRITE are equivalent.
  26414.  
  26415.  The open function applies the current file-permission mask to pmode before
  26416.  setting the permissions (see umask).
  26417.  
  26418.  The filename argument used in the open function is affected by the DOS
  26419.  APPEND command.
  26420.  
  26421.  Note that under DOS versions 3.0 and later, a problem occurs when SHARE is
  26422.  installed and a new file is opened with oflag set to O_CREAT | O_RDONLY or
  26423.  O_CREAT | O _WRONLY and pmode set to S_IREAD. Under these conditions, the
  26424.  operating system prematurely closes the file during system calls made within
  26425.  open. This problem does not occur under OS/2.
  26426.  
  26427.  To work around the problem, open the file with the pmode argument set to
  26428.  S_IWRITE. Then close the file and use chmod to change the access mode back
  26429.  to S_IREAD. Another work-around is to open the file with pmode set to
  26430.  S_IREAD and oflag set to O_CREAT | O_RDWR.
  26431.  
  26432.  
  26433.  Return Value
  26434.  
  26435.  The open function returns a file handle for the opened file. A return value
  26436.  of -1 indicates an error, and errno is set to one of the following values:
  26437.  
  26438.  Value                             Meaning
  26439.  ────────────────────────────────────────────────────────────────────────────
  26440.  EACCES                            Given path name is a directory; or an
  26441.                                    attempt was made to open a read-only
  26442.                                    file for writing; or a sharing violation
  26443.                                    occurred (the file's sharing mode does
  26444.                                    not allow the specified operations).
  26445.  
  26446.  EEXIST                            The O_CREAT and O_EXCL flags are
  26447.                                    specified, but the named file already
  26448.                                    exists.
  26449.  
  26450.  EINVAL                            An invalid oflag or pmode argument was
  26451.                                    given.
  26452.  
  26453.  EMFILE                            No more file handles available (too many
  26454.                                    open files).
  26455.  
  26456.  ENOENT                            File or path name not found.
  26457.  
  26458.  
  26459.  Compatibility
  26460.  
  26461.   ANSI   DOS   OS/2   UNIX   XENIX
  26462.  
  26463.  
  26464.  
  26465.  See Also
  26466.  
  26467.  access, chmod, close, creat, dup, dup2, fopen, sopen, umask
  26468.  
  26469.  
  26470.  Example
  26471.  
  26472.    /* OPEN.C: This program uses open to open a file named OPEN.C for input
  26473.     * and a file named OPEN.OUT for output. The files are then closed.
  26474.     */
  26475.  
  26476.    #include <fcntl.h>
  26477.    #include <sys\types.h>
  26478.    #include <sys\stat.h>
  26479.    #include <io.h>
  26480.    #include <stdio.h>
  26481.  
  26482.    void main()
  26483.    {
  26484.       int fh1, fh2;
  26485.  
  26486.    fh1 = open( "OPEN.C", O_RDONLY );
  26487.       if( fh1 == -1 )
  26488.          perror( "open failed on input file" );
  26489.       else
  26490.       {
  26491.          printf( "open succeeded on input file\n" );
  26492.          close( fh1 );
  26493.       }
  26494.  
  26495.       fh2 = open( "OPEN.OUT", O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );
  26496.       if( fh2 == -1 )
  26497.          perror( "open failed on output file" );
  26498.       else
  26499.       {
  26500.          printf( "open succeeded on output file\n" );
  26501.          close( fh2 );
  26502.       }
  26503.    }
  26504.  
  26505.  
  26506.  Output
  26507.  
  26508.  
  26509.  
  26510.    open succeeded on input file
  26511.    open succeeded on output file
  26512.  
  26513.  
  26514.  
  26515.  
  26516.  
  26517.  _outgtext
  26518.  ────────────────────────────────────────────────────────────────────────────
  26519.  
  26520.  
  26521.  Description
  26522.  
  26523.  Prints font-based text in graphics mode.
  26524.  
  26525.    #include <graph.h>
  26526.  
  26527.    void _far_outgtext( unsigned char_far *text );
  26528.  
  26529.  text                              Text string to output
  26530.  
  26531.  
  26532.  Remarks
  26533.  
  26534.  The _outgtext function outputs on the screen the null-terminated string that
  26535.  text points to. The text is output using the current font at the current
  26536.  graphics position and in the current color.
  26537.  
  26538.  No formatting is provided, in contrast to the standard console I/O library
  26539.  routines such as printf.
  26540.  
  26541.  After it outputs the text, _outgtext updates the current graphics position.
  26542.  
  26543.  
  26544.  The _outgtext function operates only in graphics video modes (e.g.,
  26545.  _MRES4COLOR). Because it is a graphics function, the color of text is set by
  26546.  the _setcolor function, not by the _settextcolor function.
  26547.  
  26548.  
  26549.  Return Value
  26550.  
  26551.  None.
  26552.  
  26553.  
  26554.  Compatibility
  26555.  
  26556.   ANSI   DOS   OS/2   UNIX   XENIX
  26557.  
  26558.  
  26559.  
  26560.  See Also
  26561.  
  26562.  _moveto functions,  _setcolor,  _setfont
  26563.  
  26564.  
  26565.  Example
  26566.  
  26567.    /* OUTGTXT.C illustrates font output using functions:
  26568.     *   _registerfonts        _setfont            _outgtext
  26569.     *   _unregisterfonts      _getfontinfo        _getgtextextent
  26570.     *   _setgtextvector
  26571.     */
  26572.  
  26573.    #include <conio.h>
  26574.    #include <stdio.h>
  26575.    #include <stdlib.h>
  26576.    #include <string.h>
  26577.    #include <graph.h>
  26578.  
  26579.    #define NFONTS 6
  26580.  
  26581.    unsigned char *face[NFONTS] =
  26582.    {
  26583.        "Courier", "Helvetica", "Times Roman", "Modern", "Script", "Roman"
  26584.    };
  26585.    unsigned char *options[NFONTS] =
  26586.    {
  26587.        "courier", "helv", "tms rmn", "modern", "script", "roman"
  26588.    };
  26589.  
  26590.    void main()
  26591.    {
  26592.        unsigned char list[20];
  26593.        char fondir[_MAX_PATH];
  26594.        struct videoconfig vc;
  26595.        struct _fontinfo fi;
  26596.        short fontnum, x, y;
  26597.  
  26598.        /* Read header info from all .FON files in current or given directory.
  26599.  */
  26600.        if( _registerfonts( "*.FON" ) <= 0 )
  26601.        {
  26602.            _outtext( "Enter full path where .FON files are located: " );
  26603.            gets( fondir );
  26604.            strcat( fondir, "\\*.FON" );
  26605.            if( _registerfonts( fondir ) <= 0 )
  26606.            {
  26607.                _outtext( "Error: can't register fonts" );
  26608.                exit( 1 );
  26609.            }
  26610.        }
  26611.  
  26612.        /* Set highest available graphics mode and get configuration. */
  26613.        if( !_setvideomode( _MAXRESMODE ) )
  26614.            exit( 1 );
  26615.        _getvideoconfig( &vc );
  26616.  
  26617.        /* Display each font name centered on screen. */
  26618.        for( fontnum = 0; fontnum < NFONTS; fontnum++ )
  26619.        {
  26620.            /* Build options string. */
  26621.            strcat( strcat( strcpy( list, "t'" ), options[fontnum] ), "'");
  26622.            strcat( list, "h30w24b" );
  26623.  
  26624.            _clearscreen( _GCLEARSCREEN );
  26625.            if( _setfont( list ) >= 0 )
  26626.            {
  26627.  
  26628.    /* Use length of text and height of font to center text. */
  26629.                x = (vc.numxpixels / 2) - (_getgtextextent( face[fontnum] ) /
  26630.  2);
  26631.                y = (vc.numypixels / 2) + (_getgtextextent( face[fontnum] ) /
  26632.  2);
  26633.                if( _getfontinfo( &fi ) )
  26634.                {
  26635.                    _outtext( "Error: Can't get font information" );
  26636.                    break;
  26637.                }
  26638.                _moveto( x, y );
  26639.                if( vc.numcolors > 2 )
  26640.                    _setcolor( fontnum + 2 );
  26641.  
  26642.                /* Rotate and display text. */
  26643.                _setgtextvector( 1, 0 );
  26644.                _outgtext( face[fontnum] );
  26645.                _setgtextvector( 0, 1 );
  26646.                _outgtext( face[fontnum] );
  26647.                _setgtextvector( -1, 0 );
  26648.                _outgtext( face[fontnum] );
  26649.                _setgtextvector( 0, -1 );
  26650.                _outgtext( face[fontnum] );
  26651.            }
  26652.            else
  26653.            {
  26654.                _outtext( "Error: Can't set font: " );
  26655.                _outtext( list );
  26656.            }
  26657.            getch();
  26658.        }
  26659.        _unregisterfonts();
  26660.        _setvideomode( _DEFAULTMODE );
  26661.    }
  26662.  
  26663.  
  26664.  
  26665.  
  26666.  
  26667.  _outmem
  26668.  ────────────────────────────────────────────────────────────────────────────
  26669.  
  26670.  
  26671.  Description
  26672.  
  26673.  Prints text of a specified length in graphics mode.
  26674.  
  26675.    #include <graph.h>
  26676.  
  26677.    void _far_outmem( unsigned char_far *text, short length );
  26678.  
  26679.  text                              Text string to output
  26680.  
  26681.  length                            Length of string to output
  26682.  
  26683.  
  26684.  Remarks
  26685.  
  26686.  The _outmem function outputs the string that text points to. The length
  26687.  argument specifies the number of characters to output.
  26688.  
  26689.  Unlike _outtext, the _outmem function prints all characters literally,
  26690.  including ASCII 10, 13, and 0 as the equivalent graphics characters. No
  26691.  formatting is provided. Text is printed using the current text color,
  26692.  starting at the current text position.
  26693.  
  26694.  To output text using special fonts, you must use the _outgtext function.
  26695.  
  26696.  
  26697.  Return Value
  26698.  
  26699.  None.
  26700.  
  26701.  
  26702.  Compatibility
  26703.  
  26704.   ANSI   DOS   OS/2   UNIX   XENIX
  26705.  
  26706.  
  26707.  
  26708.  See Also
  26709.  
  26710.  _outtext,  _settextcolor,  _settextposition,  _settextwindow
  26711.  
  26712.  
  26713.  Example
  26714.  
  26715.    /* OUTMEM.C illustrates:
  26716.     *    _outmem
  26717.     */
  26718.  
  26719.    #include <stdio.h>
  26720.    #include <graph.h>
  26721.  
  26722.    void main()
  26723.    {
  26724.        int  i, len;
  26725.        char tmp[10];
  26726.  
  26727.    _clearscreen( _GCLEARSCREEN );
  26728.        for( i = 0; i < 256; i++ )
  26729.        {
  26730.            _settextposition( (i % 24) + 1, (i / 24) * 7 );
  26731.            len = sprintf( tmp, "%3d %c", i, i );
  26732.            _outmem( tmp, len );
  26733.        }
  26734.        _settextposition( 24, 1 );
  26735.    }
  26736.  
  26737.  
  26738.  
  26739.  
  26740.  
  26741.  outp, outpw
  26742.  ────────────────────────────────────────────────────────────────────────────
  26743.  
  26744.  
  26745.  Description
  26746.  
  26747.  Outputs a byte (outp) or a word (outpw) at a port.
  26748.  
  26749.    #include <conio.h>    Required only for function declarations
  26750.  
  26751.    int outp( unsigned port, int databyte );
  26752.  
  26753.    unsigned outpw( unsigned port, unsigned dataword );
  26754.  
  26755.  port                              Port number
  26756.  
  26757.  databyte                          Output value
  26758.  
  26759.  dataword                          Output value
  26760.  
  26761.  
  26762.  Remarks
  26763.  
  26764.  The outp and outpw functions write a byte and a word, respectively, to the
  26765.  specified output port. The port argument can be any unsigned integer in the
  26766.  range 0 - 65,535; byte  can be any integer in the range 0 - 255; and
  26767.  dataword can be any value in the range 0 - 65,535.
  26768.  
  26769.  Both outp and outpw are supported in OS/2. You must use a .DEF file to
  26770.  declare the IOSEG segment the run-time library uses to perform input/output
  26771.  on the port. In addition, the intrinsic (/Oi) versions of these functions do
  26772.  not work unless you put the code in a segment that is marked with the IOPL
  26773.  keyword in the .DEF file.
  26774.  
  26775.  You cannot do IOPL from a regular code segment, so the run-time library has
  26776.  declared a separate code segment called _IOSEG. In order to use inp, inpw,
  26777.  outp, or outp in any of the protected mode run-time libraries (?LIBCP,
  26778.  LLIBCDLL, LLIBCMT, or CDLLOBJS-based DLL), you must have a .DEF file with
  26779.  this line in it:
  26780.  
  26781.    SEGMENTS _IOSEG CLASS 'IOSEG_CODE' IOPL
  26782.  
  26783.  
  26784.  Return Value
  26785.  
  26786.  The functions return the data output. There is no error return.
  26787.  
  26788.  
  26789.  Compatibility
  26790.  
  26791.   ANSI   DOS   OS/2   UNIX   XENIX
  26792.  
  26793.  
  26794.  
  26795.  See Also
  26796.  
  26797.  inp, inpw
  26798.  
  26799.  
  26800.  Example
  26801.  
  26802.    /* OUTP.C: This program uses inp and outp to make sound of variable tone
  26803.     * and duration.
  26804.     */
  26805.  
  26806.    #include <conio.h>
  26807.    #include <stdio.h>
  26808.    #include <time.h>
  26809.  
  26810.    void Beep( unsigned duration, unsigned frequency ); /* Prototypes */
  26811.    void Sleep( clock_t wait );
  26812.  
  26813.    void main ()
  26814.    {
  26815.        Beep( 698, 700 );
  26816.        Beep( 523, 500 );
  26817.    }
  26818.  
  26819.    /* Sounds the speaker for a time specified in microseconds by duration
  26820.     * at a pitch specified in hertz by frequency.
  26821.     */
  26822.    void Beep( unsigned frequency, unsigned duration )
  26823.    {
  26824.        int control;
  26825.  
  26826.        /* If frequency is 0, Beep doesn't try to make a sound. */
  26827.        if( frequency )
  26828.        {
  26829.            /* 75 is about the shortest reliable duration of a sound. */
  26830.            if( duration < 75 )
  26831.                duration = 75;
  26832.  
  26833.            /* Prepare timer by sending 10111100 to port 43. */
  26834.            outp( 0x43, 0xb6 );
  26835.  
  26836.            /* Divide input frequency by timer ticks per second and
  26837.             * write (byte by byte) to timer.
  26838.             */
  26839.            frequency = (unsigned)(1193180L / frequency);
  26840.            outp( 0x42, (char)frequency );
  26841.            outp( 0x42, (char)(frequency >> 8) );
  26842.  
  26843.            /* Save speaker control byte. */
  26844.            control = inp( 0x61 );
  26845.  
  26846.            /* Turn on the speaker (with bits 0 and 1). */
  26847.            outp( 0x61, control | 0x3 );
  26848.        }
  26849.  
  26850.        Sleep( (clock_t)duration );
  26851.  
  26852.        /* Turn speaker back on if necessary. */
  26853.        if( frequency )
  26854.            outp( 0x61, control );
  26855.    }
  26856.  
  26857.    /* Pauses for a specified number of microseconds. */
  26858.    void Sleep( clock_t wait )
  26859.    {
  26860.        clock_t goal;
  26861.  
  26862.        goal = wait + clock();
  26863.        while( goal > clock() )
  26864.            ;
  26865.    }
  26866.  
  26867.  
  26868.  
  26869.  
  26870.  
  26871.  _outtext
  26872.  ────────────────────────────────────────────────────────────────────────────
  26873.  
  26874.  
  26875.  Description
  26876.  
  26877.  Prints text in graphics mode.
  26878.  
  26879.    #include <graph.h>
  26880.  
  26881.    void _far _outtext( unsigned char _far *text );
  26882.  
  26883.  text                              Text string to output
  26884.  
  26885.  
  26886.  Remarks
  26887.  
  26888.  The _outtext function outputs the null-terminated string that text points
  26889.  to. No formatting is provided, in contrast to the standard console I/O
  26890.  library routines such as printf. This function will work in any screen mode.
  26891.  
  26892.  
  26893.  Text output begins at the current text position.
  26894.  
  26895.  To output text using special fonts, you must use the _outgtext function.
  26896.  
  26897.  
  26898.  Return Value
  26899.  
  26900.  None.
  26901.  
  26902.  
  26903.  Compatibility
  26904.  
  26905.   ANSI   DOS   OS/2   UNIX   XENIX
  26906.  
  26907.  
  26908.  
  26909.  See Also
  26910.  
  26911.  _outmem, _settextcolor, _settextposition, _settextwindow
  26912.  
  26913.  
  26914.  Example
  26915.  
  26916.    /* OUTTXT.C: This example illustrates text output functions:
  26917.     *    _gettextcolor   _getbkcolor   _gettextposition   _outtext
  26918.     *    _settextcolor   _setbkcolor   _settextposition
  26919.     */
  26920.  
  26921.    #include <conio.h>
  26922.    #include <stdio.h>
  26923.    #include <graph.h>
  26924.  
  26925.    char buffer [80];
  26926.  
  26927.    void main()
  26928.    {
  26929.  
  26930.       /* Save original foreground, background, and text position */
  26931.       short blink, fgd, oldfgd;
  26932.       long  bgd, oldbgd;
  26933.       struct rccoord oldpos;
  26934.  
  26935.    /* Save original foreground, background, and text position. */
  26936.       oldfgd = _gettextcolor();
  26937.       oldbgd = _getbkcolor();
  26938.       oldpos = _gettextposition();
  26939.       _clearscreen( _GCLEARSCREEN );
  26940.  
  26941.       /* First time no blink, second time blinking. */
  26942.       for( blink = 0; blink <= 16; blink += 16 )
  26943.       {
  26944.          /* Loop through 8 background colors. */
  26945.          for( bgd = 0; bgd < 8; bgd++ )
  26946.          {
  26947.             _setbkcolor( bgd );
  26948.             _settextposition( (short)bgd + ((blink / 16) * 9) + 3, 1 );
  26949.             _settextcolor( 7 );
  26950.             sprintf(buffer, "Back: %d Fore:", bgd );
  26951.             _outtext( buffer );
  26952.  
  26953.             /* Loop through 16 foreground colors. */
  26954.             for( fgd = 0; fgd < 16; fgd++ )
  26955.             {
  26956.                _settextcolor( fgd + blink );
  26957.                sprintf( buffer, " %2d ", fgd + blink );
  26958.                _outtext( buffer );
  26959.             }
  26960.          }
  26961.       }
  26962.       getch();
  26963.  
  26964.       /* Restore original foreground, background, and text position. */
  26965.       _settextcolor( oldfgd );
  26966.       _setbkcolor( oldbgd );
  26967.       _clearscreen( _GCLEARSCREEN );
  26968.       _settextposition( oldpos.row, oldpos.col );
  26969.    }
  26970.  
  26971.  
  26972.  
  26973.  
  26974.  
  26975.  _pclose
  26976.  ────────────────────────────────────────────────────────────────────────────
  26977.  
  26978.  
  26979.  Description
  26980.  
  26981.  Waits for a child command and closes the stream on the associated pipe.
  26982.  
  26983.  #include <stdio.h>                Function declaration
  26984.  
  26985.    int _pclose( FILE *stream );
  26986.  
  26987.  stream                            File stream returned by previous call to
  26988.                                    _popen
  26989.  
  26990.  
  26991.  Remarks
  26992.  
  26993.  The _pclose function waits for a child command and closes the stream on the
  26994.  associated pipe. The argument stream is the return value from a previous
  26995.  call to _popen. The _pclose function looks up the process ID of the child
  26996.  command started by the associated _popen call, closes the stream, executes a
  26997.  cwait call on the child command, and returns the exit status of the child
  26998.  command. See _pipe for a general discussion of pipes in OS/2.
  26999.  
  27000.  
  27001.  Return Value
  27002.  
  27003.  The _pclose function returns the exit status of the child command. The
  27004.  format of the return value is the same as that for cwait, with the exception
  27005.  that the low-order and high-order bytes are swapped. If an error occurs, -1
  27006.  is returned.
  27007.  
  27008.  
  27009.  Compatibility
  27010.  
  27011.   ANSI   DOS   OS/2   UNIX   XENIXA similar function (pclose) is
  27012.  available in the XENIX and UNIX operating environments.
  27013.  
  27014.  
  27015.  
  27016.  See Also
  27017.  
  27018.  cwait,  _pipe,  _popen
  27019.  
  27020.  
  27021.  Example
  27022.  
  27023.  See the example for _popen.
  27024.  
  27025.  
  27026.  
  27027.  
  27028.  
  27029.  perror
  27030.  ────────────────────────────────────────────────────────────────────────────
  27031.  
  27032.  
  27033.  Description
  27034.  
  27035.  Prints an error message.
  27036.  
  27037.  #include <stdio.h>                Required only for function declarations
  27038.  
  27039.    void perror( const char *string );
  27040.  
  27041.  string                            String message to print
  27042.  
  27043.  
  27044.  Remarks
  27045.  
  27046.  The perror function prints an error message to stderr. The string argument
  27047.  is printed first, followed by a colon, then by the system error message for
  27048.  the last library call that produced the error, and finally by a newline
  27049.  character. If string is a null pointer or a pointer to a null string, perror
  27050.  prints only the system error message.
  27051.  
  27052.  The actual error number is stored in the variable errno (defined in
  27053.  ERRNO.H). The system error messages are accessed through the variable
  27054.  sys_errlist, which is an array of messages ordered by error number. The
  27055.  perror function prints the appropriate error message by using the errno
  27056.  value as an index to sys_errlist. The value of the variable sys_nerr is
  27057.  defined as the maximum number of elements in the sys_errlist array.
  27058.  
  27059.  To produce accurate results, perror should be called immediately after a
  27060.  library routine returns with an error. Otherwise, the errno value may be
  27061.  overwritten by subsequent calls.
  27062.  
  27063.  Under DOS and OS/2, some of the errno values listed in ERRNO.H are not used.
  27064.  These additional errno values are reserved for UNIX and XENIX use. See
  27065.  Section 3.3, "_doserrno, errno, sys_errlist, sys_nerr," for a list of errno
  27066.  values used on DOS and OS/2 and the corresponding error messages. The perror
  27067.  function prints an empty string for any errno value not used under the
  27068.  operating system.
  27069.  
  27070.  
  27071.  Return Value
  27072.  
  27073.  None.
  27074.  
  27075.  
  27076.  Compatibility
  27077.  
  27078.   ANSI   DOS   OS/2   UNIX   XENIX
  27079.  
  27080.  
  27081.  
  27082.  See Also
  27083.  
  27084.  clearerr, ferror, strerror
  27085.  
  27086.  
  27087.  Example
  27088.  
  27089.    /* PERROR.C: This program attempts to open a file named NOSUCHF.ILE.
  27090.     * Since this file probably doesn't exist, an error message is displayed.
  27091.     * The same message is created using perror, strerror, and _strerror.
  27092.     */
  27093.  
  27094.    #include <fcntl.h>
  27095.    #include <sys\types.h>
  27096.    #include <sys\stat.h>
  27097.    #include <io.h>
  27098.    #include <stdlib.h>
  27099.    #include <stdio.h>
  27100.    #include <string.h>
  27101.  
  27102.    void main()
  27103.    {
  27104.       int  fh;
  27105.  
  27106.       if( (fh = open( "NOSUCHF.ILE", O_RDONLY )) == -1 )
  27107.       {
  27108.          /* Three ways to create error message: */
  27109.          perror( "perror says open failed" );
  27110.          printf( "strerror says open failed: %s\n", strerror( errno ) );
  27111.          printf( _strerror( "_strerror says open failed" ) );
  27112.       }
  27113.       else
  27114.       {
  27115.          printf( "open succeeded on input file\n" );
  27116.          close( fh );
  27117.       }
  27118.    }
  27119.  
  27120.  
  27121.  Output
  27122.  
  27123.  
  27124.  
  27125.    perror says open failed: No such file or directory
  27126.    strerror says open failed: No such file or directory
  27127.    _strerror says open failed: No such file or directory
  27128.  
  27129.  
  27130.  
  27131.  
  27132.  
  27133.  
  27134.  
  27135.  _pg_analyzechart Functions
  27136.  ────────────────────────────────────────────────────────────────────────────
  27137.  
  27138.  
  27139.  Description
  27140.  
  27141.  Analyze a series of data.
  27142.  
  27143.    #include <pgchart.h>
  27144.  
  27145.    short _far _pg_analyzechart( chartenv _far *env, char _far * _far
  27146.    *categories,
  27147.    float _far *values, short n );
  27148.  
  27149.    short _far _pg_analyzechartms( chartenv _far *env, char _far * _far
  27150.    *categories,
  27151.    float _far *values, short nseries, short n, short arraydim,
  27152.    char _far * _far *serieslabels );
  27153.  
  27154.  env                               Chart environment variable
  27155.  
  27156.  categories                        Array of category variables
  27157.  
  27158.  values                            Array of data values
  27159.  
  27160.  nseries                           Number of series to chart
  27161.  
  27162.  n                                 Number of data values to chart
  27163.  
  27164.  arraydim                          Row dimension of data array
  27165.  
  27166.  serieslabels                      Array of labels for series
  27167.  
  27168.  
  27169.  Remarks
  27170.  
  27171.  The _pg_analyzechart routines analyze a single or multiple series of data
  27172.  without actually displaying the presentation-graphic image.
  27173.  
  27174.  The _pg_analyzechart function fills the chart environment with default
  27175.  values for a single-series bar, column, or line chart, depending on the type
  27176.  specified by the call to the _pg_defaultchart function. The variables
  27177.  calculated by _pg_analyzechart reflect the data given in the arguments
  27178.  categories and values. All arguments are the same as those used with the
  27179.  _pg_chart function.
  27180.  
  27181.  The _pg_analyzechartms function fills the chart environment with default
  27182.  values for a multiseries bar, column, or line chart, depending on which type
  27183.  is specified in the _pg_defaultchart function. The variables calculated by
  27184.  _pg_analyzechartms reflect the data given in the arguments categories and
  27185.  values. All arguments are the same as those used with the _pg_chartms
  27186.  function.
  27187.  
  27188.  Boolean flags in the chart environment, such as AUTOSCALE and LEGEND, should
  27189.  be set to TRUE before calling either _pg_analyzechart function. This will
  27190.  ensure that the function will calculate all defaults.
  27191.  
  27192.  For a discussion of the chart environment and related topics, see Section
  27193.  2.6.2, "Presentation-Graphics Functions."
  27194.  
  27195.  
  27196.  Return Value
  27197.  
  27198.  The _pg_analyzechart and _pg_analyzechartms functions return 0 if there were
  27199.  no errors. A nonzero value indicates a failure.
  27200.  
  27201.  
  27202.  Compatibility
  27203.  
  27204.   ANSI   DOS   OS/2   UNIX   XENIX
  27205.  
  27206.  
  27207.  
  27208.  See Also
  27209.  
  27210.  _pg_chart functions,  _pg_defaultchart,  _pg_initchart
  27211.  
  27212.  
  27213.  Example
  27214.  
  27215.    /* PGACHART.C: This example illustrates presentation-graphics
  27216.     * analyze functions.
  27217.     * The example uses
  27218.     *      _pg_analyzechartms
  27219.     * The same principles apply for
  27220.     *      _pg_analyzepie        _pg_analyzechart
  27221.     *      _pg_analyzescatter    _pg_analyzescatterms
  27222.     */
  27223.  
  27224.    #include <conio.h>
  27225.    #include <string.h>
  27226.    #include <stdlib.h>
  27227.    #include <graph.h>
  27228.    #include <pgchart.h>
  27229.  
  27230.    #define FALSE  0
  27231.    #define TRUE   1
  27232.  
  27233.    /* Note data declared as a single-dimension array. The multiseries
  27234.     * chart functions expect only one dimension. See _pg_chartms
  27235.     * example for alternate method using multidimension array.
  27236.     */
  27237.    #define TEAMS  4
  27238.    #define MONTHS 3
  27239.    float _far values[TEAMS * MONTHS] = { .435,   .522,   .671,
  27240.                                          .533,   .431,   .590,
  27241.                                          .723,   .624,   .488,
  27242.                                          .329,   .226,   .401   };
  27243.    char _far *months[MONTHS] =         { "May",  "June", "July" };
  27244.    char _far *teams[TEAMS] = { "Reds", "Sox", "Cubs", "Mets" };
  27245.  
  27246.    void main()
  27247.    {
  27248.       chartenv env;
  27249.  
  27250.       /* Find a valid graphics mode. */
  27251.       if( !_setvideomode( _MAXRESMODE ) )
  27252.          exit( 1 );
  27253.  
  27254.       _pg_initchart();                    /* Initialize chart system.   */
  27255.       /* Default multiseries bar chart */
  27256.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  27257.       strcpy( env.maintitle.title, "Little League Records - Default" );
  27258.       _pg_chartms( &env, months, values, TEAMS, MONTHS, MONTHS, teams );
  27259.       getch();
  27260.        _clearscreen( _GCLEARSCREEN );
  27261.  
  27262.       /* Analyze multiseries bar chart with autoscale. This sets all
  27263.        * default scale values. We want y axis values to be automatic.
  27264.        */
  27265.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  27266.       strcpy( env.maintitle.title, "Little League Records - Customized" );
  27267.       env.xaxis.autoscale = TRUE;
  27268.       _pg_analyzechartms( &env, months, values, TEAMS, MONTHS, MONTHS, teams
  27269.  );
  27270.  
  27271.       /* Now customize some of the x axis values. Then draw the chart.  */
  27272.       env.xaxis.autoscale = FALSE;
  27273.       env.xaxis.scalemax = 1.0;           /* Make scale show 0.0 to 1.0.   */
  27274.       env.xaxis.ticinterval = 0.2;        /* Don't make scale too crowded. */
  27275.       env.xaxis.ticdecimals = 3;          /* Show three decimals.          */
  27276.       strcpy( env.xaxis.scaletitle.title, "Win/Loss Percentage" );
  27277.       _pg_chartms( &env, months, values, TEAMS, MONTHS, MONTHS, teams );
  27278.       getch();
  27279.  
  27280.       _setvideomode( _DEFAULTMODE );
  27281.    }
  27282.  
  27283.  
  27284.  
  27285.  
  27286.  
  27287.  _pg_analyzepie
  27288.  ────────────────────────────────────────────────────────────────────────────
  27289.  
  27290.  
  27291.  Description
  27292.  
  27293.  Analyzes a single series of data for a pie chart.
  27294.  
  27295.    #include <pgchart.h>
  27296.  
  27297.    short _far _pg_analyzepie( chartenv _far *env, char _far * _far
  27298.    *categories,
  27299.    float _far *values, short _far *explode, short n );
  27300.  
  27301.  env                               Chart environment variable
  27302.  
  27303.  categories                        Array of category variables
  27304.  
  27305.  values                            Array of data values
  27306.  
  27307.  explode                           Array of explode flags
  27308.  
  27309.  n                                 Number of data values to chart
  27310.  
  27311.  
  27312.  Remarks
  27313.  
  27314.  The _pg_analyzepie function analyzes a single series of data without
  27315.  actually displaying the graphic image.
  27316.  
  27317.  The _pg_analyzepie function fills the chart environment for a pie chart
  27318.  using the data contained in the array values. All arguments are the same as
  27319.  those used in the _pg_chartpie function.
  27320.  
  27321.  For a discussion of the chart environment and related topics, see Section
  27322.  2.6.2, "Presentation-Graphics Functions."
  27323.  
  27324.  
  27325.  Return Value
  27326.  
  27327.  The _pg_analyzepie function returns 0 if there were no errors. A nonzero
  27328.  value indicates a failure.
  27329.  
  27330.  
  27331.  Compatibility
  27332.  
  27333.   ANSI   DOS   OS/2   UNIX   XENIX
  27334.  
  27335.  
  27336.  
  27337.  See Also
  27338.  
  27339.  _pg_chartpie,  _pg_defaultchart,  _pg_initchart
  27340.  
  27341.  
  27342.  Example
  27343.  
  27344.  See the example for _pg_analyzechart.
  27345.  
  27346.  
  27347.  
  27348.  
  27349.  
  27350.  _pg_analyzescatter Functions
  27351.  ────────────────────────────────────────────────────────────────────────────
  27352.  
  27353.  
  27354.  Description
  27355.  
  27356.  Analyze a series of data for a scatter chart.
  27357.  
  27358.    #include <pgchart.h>
  27359.  
  27360.    short _far _pg_analyzescatter( chartenv _far *env, float _far *xvalues,
  27361.    float _far *yvalues, short n );
  27362.  
  27363.    short _far _pg_analyzescatterms( chartenv _far *env, float _far *xvalues,
  27364.    float _far *yvalues, short nseries, short n, short rowdim,
  27365.    char _far * _far *serieslabels );
  27366.  
  27367.  env                               Chart environment structure
  27368.  
  27369.  xvalues                           Array of x-axis data values
  27370.  
  27371.  yvalues                           Array of y-axis data values
  27372.  
  27373.  n                                 Number of data values to chart
  27374.  
  27375.  nseries                           Number of series to chart
  27376.  
  27377.  rowdim                            Row dimension of data array
  27378.  
  27379.  serieslabels                      Array of labels for series
  27380.  
  27381.  
  27382.  Remarks
  27383.  
  27384.  The _pg_analyzescatter set of routines analyzes a single or multiple series
  27385.  of data without actually displaying the graphic image.
  27386.  
  27387.  The _pg_analyzescatter function fills the chart environment for a
  27388.  single-series scatter diagram. The variables calculated by this function
  27389.  reflect the data given in the arguments xvalues and yvalues. All arguments
  27390.  are the same as those used in the _pg_chartscatter function.
  27391.  
  27392.  The _pg_analyzescatterms function fills the chart environment for a
  27393.  multiseries scatter diagram. The variables calculated by
  27394.  _pg_analyzescatterms reflect the data given in the arguments xvalues and
  27395.  yvalues. All arguments are the same as those used in the function
  27396.  _pg_chartscatterms.
  27397.  
  27398.  Boolean flags in the chart environment, such as AUTOSCALE and LEGEND, should
  27399.  be set to TRUE before calling _pg_analyzescatterms; this ensures that the
  27400.  function will calculate all defaults.
  27401.  
  27402.  For a discussion of the chart environment and related topics, see Section
  27403.  2.6.2, "Presentation-Graphics Functions."
  27404.  
  27405.  
  27406.  Return Value
  27407.  
  27408.  The _pg_analyzescatter and _pg_analyzescatterms functions return 0 if there
  27409.  were no errors. A nonzero value indicates a failure.
  27410.  
  27411.  
  27412.  Compatibility
  27413.  
  27414.   ANSI   DOS   OS/2   UNIX   XENIX
  27415.  
  27416.  
  27417.  
  27418.  See Also
  27419.  
  27420.  _pg_chartscatter functions,  _pg_defaultchart,  _pg_initchart
  27421.  
  27422.  
  27423.  Example
  27424.  
  27425.  See the example for _pg_analyzechart.
  27426.  
  27427.  
  27428.  
  27429.  
  27430.  
  27431.  _pg_chart Functions
  27432.  ────────────────────────────────────────────────────────────────────────────
  27433.  
  27434.  
  27435.  Description
  27436.  
  27437.  Display single-series or multiseries charts.
  27438.  
  27439.    #include <pgchart.h>
  27440.  
  27441.    short _far _pg_chart( chartenv _far *env, char _far * _far *categories,
  27442.    float _far *values, short n );
  27443.  
  27444.    short _far _pg_chartms( chartenv _far *env, char _far * _far *categories,
  27445.    float _far *values, short nseries, short n, short arraydim,
  27446.    char _far * _far *serieslabels );
  27447.  
  27448.  env                               Chart environment variable
  27449.  
  27450.  categories                        Array of category variables
  27451.  
  27452.  values                            Array of data values
  27453.  
  27454.  n                                 Number of data values to chart
  27455.  
  27456.  nseries                           Number of series to chart
  27457.  
  27458.  arraydim                          Row dimension of data array
  27459.  
  27460.  serieslabels                      Array of labels for series
  27461.  
  27462.  
  27463.  Remarks
  27464.  
  27465.  The _pg_chart function displays a single-series bar, column, or line chart,
  27466.  depending on the type specified in the chart environment variable (env).
  27467.  
  27468.  The _pg_chartms function displays a multiseries bar, column, or line chart,
  27469.  depending on the type specified in the chart environment. All the series
  27470.  must contain the same number of data points, specified by the argument n.
  27471.  
  27472.  The array values is a two-dimensional array containing all value data for
  27473.  every series to be plotted on the chart. Each column of values represents a
  27474.  single series. The parameter rowdim is the integer value used to dimension
  27475.  rows in the array declaration for values.
  27476.  
  27477.  For example, the following code fragment declares the identifier  values  to
  27478.  be a twodimensional floating-point array with 20 rows and 10 columns:
  27479.  
  27480.    #define ARRAYDIM 20
  27481.    float values [ARRAYDIM][10];
  27482.    short rowdim = ARRAYDIM;
  27483.  
  27484.  Note that the number of columns in the values array cannot exceed 10, the
  27485.  maximum number of data series on a single chart. Note also that  rowdim
  27486.  must be greater than or equal to the argument n, and the column dimension in
  27487.  the array declaration must be greater than or equal to the argument nseries.
  27488.  If n and nseries are set to values less than the full dimensional size of
  27489.  the values array, only part of the data contained in values will be plotted.
  27490.  
  27491.  
  27492.  The array serieslabels holds the labels used in the chart legend to identify
  27493.  each series.
  27494.  
  27495.  For a discussion of the chart environment and related topics, see Section
  27496.  2.6.2, "Presentation-Graphics Functions."
  27497.  
  27498.  
  27499.  Return Value
  27500.  
  27501.  The _pg_chart and _pg_chartms functions return 0 if there were no errors. A
  27502.  nonzero value indicates a failure.
  27503.  
  27504.  
  27505.  Compatibility
  27506.  
  27507.   ANSI   DOS   OS/2   UNIX   XENIX
  27508.  
  27509.  
  27510.  
  27511.  See Also
  27512.  
  27513.  _pg_analyzechart functions,  _pg_defaultchart,  _pg_initchart
  27514.  
  27515.  
  27516.  Example
  27517.  
  27518.    /* PGCHART.C: This example illustrates presentation-graphics support
  27519.     * routines and single-series chart routines, including
  27520.     *    _pg_initchart   _pg_defaultchart   _pg_chart   _pg_chartpie
  27521.     */
  27522.  
  27523.    #include <conio.h>
  27524.    #include <graph.h>
  27525.    #include <string.h>
  27526.    #include <stdlib.h>
  27527.    #include <pgchart.h>
  27528.  
  27529.    #define COUNTRIES 5
  27530.    float _far value[COUNTRIES] =    { 42.5,    14.3,    35.2,   21.3,   32.6
  27531.  };
  27532.    char _far *category[COUNTRIES] = { "USSR",  "France","USA",  "UK",
  27533.  "Other" };
  27534.    short _far explode[COUNTRIES] =  { 0,       1,        0,      1,     0
  27535.  };
  27536.  
  27537.    void main()
  27538.    {
  27539.       chartenv env;
  27540.       short mode = _VRES16COLOR;
  27541.  
  27542.       /* Find a valid graphics mode. */
  27543.       if( !_setvideomode( _MAXRESMODE ) )
  27544.          exit( 1 );
  27545.  
  27546.       _pg_initchart();                    /* Initialize chart system. */
  27547.  
  27548.       /* Single-series bar chart */
  27549.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  27550.       strcpy( env.maintitle.title, "Widget Production" );
  27551.       _pg_chart( &env, category, value, COUNTRIES );
  27552.       getch();
  27553.       _clearscreen( _GCLEARSCREEN );
  27554.  
  27555.       /* Single-series column chart */
  27556.       _pg_defaultchart( &env, _PG_COLUMNCHART, _PG_PLAINBARS );
  27557.       strcpy( env.maintitle.title, "Widget Production" );
  27558.       _pg_chart( &env, category, value, COUNTRIES );
  27559.       getch();
  27560.       _clearscreen( _GCLEARSCREEN );
  27561.  
  27562.       /* Pie chart */
  27563.       _pg_defaultchart( &env, _PG_PIECHART, _PG_PERCENT );
  27564.       strcpy( env.maintitle.title, "Widget Production" );
  27565.       _pg_chartpie( &env, category, value, explode, COUNTRIES );
  27566.       getch();
  27567.  
  27568.       _setvideomode( _DEFAULTMODE );
  27569.    }
  27570.  
  27571.  
  27572.  
  27573.  
  27574.  
  27575.  _pg_chartscatter Functions
  27576.  ────────────────────────────────────────────────────────────────────────────
  27577.  
  27578.  
  27579.  Description
  27580.  
  27581.  Display scatter charts.
  27582.  
  27583.    #include <pgchart.h>
  27584.  
  27585.    short _far _pg_chartscatter( chartenv _far *env, float _far *xvalues,
  27586.    float _far *yvalues, short n );
  27587.  
  27588.    short _far _pg_chartscatterms( chartenv _far *env, float _far *xvalues,
  27589.    float _far *yvalues, short nseries, short n, short rowdim,
  27590.    char _far * _far *serieslabels );
  27591.  
  27592.  env                               Chart environment structure
  27593.  
  27594.  xvalues                           Array of x-axis data values
  27595.  
  27596.  yvalues                           Array of y-axis data values
  27597.  
  27598.  n                                 Number of data values to chart
  27599.  
  27600.  nseries                           Number of series to chart
  27601.  
  27602.  rowdim                            Row dimension of data array
  27603.  
  27604.  serieslabels                      Array of labels for series
  27605.  
  27606.  
  27607.  Remarks
  27608.  
  27609.  The _pg_chartscatter function displays a scatter diagram for a single series
  27610.  of data.
  27611.  
  27612.  The _pg_chartscatterms function displays a scatter diagram for more than one
  27613.  series of data.
  27614.  
  27615.  The arguments xvalues and yvalues are two-dimensional arrays containing data
  27616.  for the x axis and y axis, respectively. Columns for each array hold data
  27617.  for individual series; thus the first columns of xvalues and yvalues contain
  27618.  plot data for the first series, the second columns contain plot data for the
  27619.  second series, and so forth.
  27620.  
  27621.  The n, rowdim, nseries, and serieslabels arguments fulfill the same purposes
  27622.  as those used in the _pg_chartms function. See _pg_chartms for an
  27623.  explanation of these arguments.
  27624.  
  27625.  For a discussion of the chart environment and related topics, see Section
  27626.  2.6.2, "Presentation-Graphics Functions."
  27627.  
  27628.  
  27629.  Return Value
  27630.  
  27631.  The _pg_chartscatter and _pg_chartscatterms functions return 0 if there were
  27632.  no errors. A nonzero value indicates a failure.
  27633.  
  27634.  
  27635.  Compatibility
  27636.  
  27637.   ANSI   DOS   OS/2   UNIX   XENIX
  27638.  
  27639.  
  27640.  
  27641.  See Also
  27642.  
  27643.   _pg_analyzescatter functions,  _pg_defaultchart,  _pg_initchart
  27644.  
  27645.  
  27646.  Example
  27647.  
  27648.  See the example for _pg_chart.
  27649.  
  27650.  
  27651.  
  27652.  
  27653.  
  27654.  _pg_chartpie
  27655.  ────────────────────────────────────────────────────────────────────────────
  27656.  
  27657.  
  27658.  Description
  27659.  
  27660.  Displays a pie chart.
  27661.  
  27662.    #include <pgchart.h>
  27663.  
  27664.    short _far _pg_chartpie( chartenv _far *env, char _far * _far *categories,
  27665.  
  27666.    float _far *values, short _far *explode, short n );
  27667.  
  27668.  env                               Chart environment structure
  27669.  
  27670.  categories                        Array of category labels
  27671.  
  27672.  values                            Array of data values
  27673.  
  27674.  explode                           Array of explode flags
  27675.  
  27676.  n                                 Number of data values to chart
  27677.  
  27678.  
  27679.  Remarks
  27680.  
  27681.  The _pg_chartpie function displays a pie chart for the data contained in the
  27682.  array values. Pie charts are formed from a single series of data─there is no
  27683.  multiseries version of pie charts as there is for other chart types.
  27684.  
  27685.  The array explode must be dimensioned so that its length is greater than or
  27686.  equal to the  argument n. All entries in explode are either 0 or 1. If an
  27687.  entry is 1, the corresponding pie slice is displayed slightly removed from
  27688.  the rest of the pie.
  27689.  
  27690.  For example, if the explode array is initialized as
  27691.  
  27692.    short explode[5] = {0, 1, 0, 0, 0};
  27693.  
  27694.  the pie slice corresponding to the second entry of the categories array will
  27695.  be displayed "exploded" from the other four slices.
  27696.  
  27697.  For a discussion of the chart environment and related topics, see Section
  27698.  2.6.2, "Presentation-Graphics Functions."
  27699.  
  27700.  
  27701.  Return Value
  27702.  
  27703.  The _pg_chartpie function returns 0 if there were no errors. A nonzero value
  27704.  indicates a failure.
  27705.  
  27706.  
  27707.  Compatibility
  27708.  
  27709.   ANSI   DOS   OS/2   UNIX   XENIX
  27710.  
  27711.  
  27712.  
  27713.  See Also
  27714.  
  27715.   _pg_analyzepie,  _pg_defaultchart,  _pg_initchart
  27716.  
  27717.  
  27718.  Example
  27719.  
  27720.  See the example for _pg_chart.
  27721.  
  27722.  
  27723.  
  27724.  
  27725.  
  27726.  _pg_defaultchart
  27727.  ────────────────────────────────────────────────────────────────────────────
  27728.  
  27729.  
  27730.  Description
  27731.  
  27732.  Initializes the chart environment.
  27733.  
  27734.    #include <pgchart.h>
  27735.  
  27736.    short _far _pg_defaultchart( chartenv _far *env, short charttype, short
  27737.    chartstyle );
  27738.  
  27739.  env                               Chart environment structure
  27740.  
  27741.  charttype                         Chart type
  27742.  
  27743.  chartstyle                        Chart style
  27744.  
  27745.  
  27746.  Remarks
  27747.  
  27748.  The _pg_defaultchart function initializes all necessary variables in the
  27749.  chart environment for the chart type by the variable charttype.
  27750.  
  27751.  All title fields in the environment structure are blanked. Titles should be
  27752.  set in the proper fields after calling _pg_defaultchart.
  27753.  
  27754.  The charttype variable can be set to one of the following manifest
  27755.  constants:
  27756.  
  27757.  Chart Type                        Description
  27758.  ────────────────────────────────────────────────────────────────────────────
  27759.  _PG_BARCHART                      Bar chart
  27760.  
  27761.  _PG_COLUMNCHART                   Column chart
  27762.  
  27763.  _PG_LINECHART                     Line chart
  27764.  
  27765.  _PG_PIECHART                      Pie chart
  27766.  
  27767.  _PG_SCATTERCHART                  Scatter chart
  27768.  
  27769.  The chartstyle variable specifies the style of the chart with either the
  27770.  number "1" or the number "2." Each of the five types of
  27771.  presentation-graphics charts can appear in two different chart styles, as
  27772.  described below:
  27773.  
  27774. ╓┌───────────┌───────────────────┌───────────────────────────────────────────╖
  27775.  Chart Type  Chart Style 1       Chart Style  2
  27776.  ────────────────────────────────────────────────────────────────────────────
  27777.  Bar         Side by side        Stacked
  27778.  
  27779.  Column      Side by side        Stacked
  27780.  
  27781.  Line        Points with lines   Points only
  27782.  
  27783.  Pie         Percent             No percent
  27784.  
  27785.  Chart Type  Chart Style 1       Chart Style  2
  27786.  ────────────────────────────────────────────────────────────────────────────
  27787. 
  27788.  Scatter     Points with lines   Points only
  27789.  
  27790.  
  27791.  
  27792.  In a pie chart, the pieces are "exploded" according to the explode array
  27793.  argument in the _pg_chartpie function. In the "percent" format, percentages
  27794.  are printed next to each slice. Bar and column charts have only one style
  27795.  when displaying a single series of data. The styles "side by side" and
  27796.  "stacked" are applicable only when more than one series appear on the same
  27797.  chart. The first style arranges the bars or columns for the different series
  27798.  side by side, showing relative heights or lengths. The stacked style
  27799.  emphasizes relative sizes between bars and columns.
  27800.  
  27801.  
  27802.  Return Value
  27803.  
  27804.  The _pg_defaultchart function returns 0 if there were no errors. A nonzero
  27805.  value indicates a failure.
  27806.  
  27807.  
  27808.  Compatibility
  27809.  
  27810.   ANSI   DOS   OS/2   UNIX   XENIX
  27811.  
  27812.  
  27813.  
  27814.  See Also
  27815.  
  27816.  _pg_getchardef,  _pg_getpalette,  _pg_getstyleset,  _pg_hlabelchart,
  27817.  _pg_initchart,  _pg_resetpalette,  _pg_resetstyleset,  _pg_setchardef,
  27818.  _pg_setpalette,  _pg_setstyleset, _pg_vlabelchart
  27819.  
  27820.  
  27821.  Example
  27822.  
  27823.  See the example for _pg_chart.
  27824.  
  27825.  
  27826.  
  27827.  
  27828.  
  27829.  _pg_getchardef
  27830.  ────────────────────────────────────────────────────────────────────────────
  27831.  
  27832.  
  27833.  Description
  27834.  
  27835.  Gets the pixel bit map for the specified character.
  27836.  
  27837.    #include <pgchart.h>
  27838.  
  27839.    short _far _pg_getchardef( short charnum, unsigned char _far *chardef  );
  27840.  
  27841.  charnum                           ASCII number of character
  27842.  
  27843.  chardef                           Pointer to 8-by-8 bit map array
  27844.  
  27845.  
  27846.  Remarks
  27847.  
  27848.  The _pg_getchardef function retrieves the current 8-by-8 pixel bit map for
  27849.  the character having the ASCII number charnum. The bit map is stored in the
  27850.  chardef array.
  27851.  
  27852.  
  27853.  Return Value
  27854.  
  27855.  The _pg_getchardef function returns 0 if there were no errors. A nonzero
  27856.  value indicates an error.
  27857.  
  27858.  
  27859.  Compatibility
  27860.  
  27861.   ANSI   DOS   OS/2   UNIX   XENIX
  27862.  
  27863.  
  27864.  
  27865.  See Also
  27866.  
  27867.  _pg_defaultchart,  _pg_initchart,  _pg_setchardef
  27868.  
  27869.  
  27870.  
  27871.  
  27872.  
  27873.  _pg_getpalette
  27874.  ────────────────────────────────────────────────────────────────────────────
  27875.  
  27876.  
  27877.  Description
  27878.  
  27879.  Gets palette colors, line styles, and patterns.
  27880.  
  27881.    #include <pgchart.h>
  27882.  
  27883.    short _far _pg_getpalette( paletteentry _far *palette );
  27884.  
  27885.  palette                           Pointer to first palette structure in
  27886.                                    array
  27887.  
  27888.  
  27889.  Remarks
  27890.  
  27891.  The _pg_getpalette function retrieves palette colors, line styles, fill
  27892.  patterns, and plot characters for all palettes. The pointer palette points
  27893.  to an array of palette structures that will contain the desired palette
  27894.  values.
  27895.  
  27896.  The palette used by the presentation-graphics routines is independent of the
  27897.  palette used by the low-level graphics routines.
  27898.  
  27899.  
  27900.  Return Value
  27901.  
  27902.  The function _pg_getpalette returns 0 if there were no errors, and it
  27903.  returns the value _BADSCREENMODE if current palettes have not been
  27904.  initialized by a previous call to _pg_setpalette.
  27905.  
  27906.  
  27907.  Compatibility
  27908.  
  27909.   ANSI   DOS   OS/2   UNIX   XENIX
  27910.  
  27911.  
  27912.  
  27913.  See Also
  27914.  
  27915.  _pg_defaultchart,  _pg_initchart,  _pg_resetpalette,  _pg_setpalette
  27916.  
  27917.  
  27918.  Example
  27919.  
  27920.    /* PGGPAL.C: This example illustrates presentation-graphics palettes
  27921.     * and the routines that modify them, including
  27922.     *    _pg_getpalette    _pg_resetpalette    _pg_setstyleset
  27923.     *    _pg_getstyleset   _pg_resetstyleset   _pg_vlabelchart
  27924.     *    _pg_hlabelchart   _pg_setpalette
  27925.     */
  27926.    #include <conio.h>
  27927.    #include <string.h>
  27928.    #include <stdlib.h>
  27929.    #include <graph.h>
  27930.    #include <pgchart.h>
  27931.  
  27932.    #define TEAMS  2
  27933.    #define MONTHS 3
  27934.    float _far values[TEAMS][MONTHS] = { { .435,   .522,   .671  },
  27935.                                         { .533,   .431,   .401  } };
  27936.    char _far *months[MONTHS] =          { "May", "June", "July" };
  27937.    char _far *teams[TEAMS] = { "Cubs", "Reds" };
  27938.  
  27939.    fillmap fill1 = { 0x99, 0x33, 0x66, 0xcc, 0x99, 0x33, 0x66, 0xcc };
  27940.    fillmap fill2 = { 0x99, 0xcc, 0x66, 0x33, 0x99, 0xcc, 0x66, 0x33 };
  27941.    styleset styles;
  27942.    palettetype pal;
  27943.  
  27944.    void main()
  27945.    {
  27946.       chartenv env;
  27947.       short mode = _VRES16COLOR;
  27948.  
  27949.       /* Find a valid graphics mode. */
  27950.       if( !_setvideomode( _MAXRESMODE ) )
  27951.          exit( 1 );
  27952.  
  27953.       _pg_initchart();                         /* Initialize chart system.
  27954.       */
  27955.  
  27956.       /* Modify global set of line styles used for borders, grids, and
  27957.        * data connectors. Note that this change is used before
  27958.        * _pg_defaultchart, which will use the style set.
  27959.        */
  27960.       _pg_getstyleset( styles );               /* Get styles and modify
  27961.       */
  27962.       styles[1] = 0x5555;                      /*   style 1 (used for
  27963.       */
  27964.       _pg_setstyleset( styles );               /*   borders)─then set new.
  27965.       */
  27966.  
  27967.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  27968.  
  27969.       /* Modify palette for data lines, colors, fill patterns, and
  27970.        * characters. Note that the line styles are set in the palette, not
  27971.        * in the style set, so that only data connectors will be affected.
  27972.        */
  27973.       _pg_getpalette( pal );                   /* Get default palette.
  27974.       */
  27975.       pal[1].plotchar = 16;                    /* Set to ASCII 16 and 17.
  27976.       */
  27977.       pal[2].plotchar = 17;
  27978.       memcpy( pal[1].fill, fill1, 8 );         /* Copy fill masks to
  27979.       palette.*/
  27980.       memcpy( pal[2].fill, fill2, 8 );
  27981.       pal[1].color = 3;                        /* Change palette colors.
  27982.       */
  27983.       pal[2].color = 4;
  27984.       pal[1].style = 0xfcfc;                   /* Change palette line
  27985.       styles.*/
  27986.       pal[2].style = 0x0303;
  27987.       _pg_setpalette( pal );                   /* Put modified palette.
  27988.       */
  27989.  
  27990.    /* Multiseries bar chart */
  27991.       strcpy( env.maintitle.title, "Little League Records - Customized" );
  27992.       _pg_chartms( &env, months, (float _far *)values,
  27993.                 TEAMS, MONTHS, MONTHS, teams );
  27994.       getch();
  27995.       _clearscreen( _GCLEARSCREEN );
  27996.  
  27997.       /* Multiseries line chart */
  27998.       _pg_defaultchart( &env, _PG_LINECHART, _PG_POINTANDLINE );
  27999.       strcpy( env.maintitle.title, "Little League Records - Customized" );
  28000.       _pg_chartms( &env, months, (float _far *)values,
  28001.                     TEAMS, MONTHS, MONTHS, teams );
  28002.  
  28003.       /* Print labels. */
  28004.       _pg_hlabelchart( &env, (short)(env.chartwindow.x2 * .75),
  28005.                              (short)(env.chartwindow.y2 * .10),
  28006.                              12, "Up and up!" );
  28007.       _pg_vlabelchart( &env, (short)(env.chartwindow.x2 * .75),
  28008.                              (short)(env.chartwindow.y2 * .45),
  28009.                              13, "Sliding down!" );
  28010.       getch();
  28011.       _clearscreen( _GCLEARSCREEN );
  28012.  
  28013.       _pg_resetpalette();                     /* Restore default palette
  28014.  */
  28015.       _pg_resetstyleset();                    /*   and style set.
  28016.  */
  28017.  
  28018.       /* Multiseries bar chart */
  28019.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  28020.       strcpy( env.maintitle.title, "Little League Records - Default" );
  28021.       _pg_chartms( &env, months, (float _far *)values,
  28022.                    TEAMS, MONTHS, MONTHS, teams );
  28023.       getch();
  28024.       _clearscreen( _GCLEARSCREEN );
  28025.  
  28026.       /* Multiseries line chart */
  28027.       _pg_defaultchart( &env, _PG_LINECHART, _PG_POINTANDLINE );
  28028.       strcpy( env.maintitle.title, "Little League Records - Default" );
  28029.       _pg_chartms( &env, months, (float _far *)values,
  28030.                    TEAMS, MONTHS, MONTHS, teams );
  28031.       getch();
  28032.  
  28033.       _setvideomode( _DEFAULTMODE );
  28034.    }
  28035.  
  28036.  
  28037.  
  28038.  
  28039.  
  28040.  _pg_getstyleset
  28041.  ────────────────────────────────────────────────────────────────────────────
  28042.  
  28043.  
  28044.  Description
  28045.  
  28046.  Gets the current styleset.
  28047.  
  28048.    #include <pgchart.h>
  28049.  
  28050.    void _far _pg_getstyleset( unsigned short _far *styleset );
  28051.  
  28052.  styleset                          Pointer to current styleset
  28053.  
  28054.  
  28055.  Remarks
  28056.  
  28057.  The _pg_getstyleset function retrieves the contents of the current styleset.
  28058.  
  28059.  
  28060.  
  28061.  Return Value
  28062.  
  28063.  None.
  28064.  
  28065.  
  28066.  Compatibility
  28067.  
  28068.   ANSI   DOS   OS/2   UNIX   XENIX
  28069.  
  28070.  
  28071.  
  28072.  See Also
  28073.  
  28074.  _pg_defaultchart,  _pg_initchart,  _pg_resetstyleset,  _pg_setstyleset
  28075.  
  28076.  
  28077.  Example
  28078.  
  28079.  See the example for _pg_getpalette.
  28080.  
  28081.  
  28082.  
  28083.  
  28084.  
  28085.  _pg_hlabelchart
  28086.  ────────────────────────────────────────────────────────────────────────────
  28087.  
  28088.  
  28089.  Description
  28090.  
  28091.  Writes text horizontally on the screen.
  28092.  
  28093.    #include <pgchart.h>
  28094.  
  28095.    short _far _pg_hlabelchart( chartenv _far *env, short x, short y, short
  28096.    color,
  28097.    char _far *label );
  28098.  
  28099.  env                               Chart environment structure
  28100.  
  28101.  x                                 x-coordinate for text
  28102.  
  28103.  y                                 Pixel y-coordinate for text
  28104.  
  28105.  color                             Color code for text
  28106.  
  28107.  label                             Label text
  28108.  
  28109.  
  28110.  Remarks
  28111.  
  28112.  The _pg_hlabelchart function writes text horizontally on the screen. The
  28113.  arguments x and y are pixel coordinates for the beginning location of text
  28114.  relative to the upper-left corner of the chart window.
  28115.  
  28116.  
  28117.  Return Value
  28118.  
  28119.  The _pg_hlabelchart functions return 0 if there were no errors. A nonzero
  28120.  value indicates a failure.
  28121.  
  28122.  
  28123.  Compatibility
  28124.  
  28125.   ANSI   DOS   OS/2   UNIX   XENIX
  28126.  
  28127.  
  28128.  
  28129.  See Also
  28130.  
  28131.  _pg_defaultchart,  _pg_initchart,  _pg_vlabelchart
  28132.  
  28133.  
  28134.  Example
  28135.  
  28136.  See the example for _pg_getpalette.
  28137.  
  28138.  
  28139.  
  28140.  
  28141.  
  28142.  _pg_initchart
  28143.  ────────────────────────────────────────────────────────────────────────────
  28144.  
  28145.  
  28146.  Description
  28147.  
  28148.  Initializes presentation graphics.
  28149.  
  28150.    #include <pgchart.h>
  28151.  
  28152.    short _far _pg_initchart( void );
  28153.  
  28154.  
  28155.  Remarks
  28156.  
  28157.  The _pg_initchart function initializes the presentation-graphics package. It
  28158.  initializes the color and style pools, resets the chartline styleset, builds
  28159.  default palette modes, and reads the presentation-graphics font definition
  28160.  from the disk. This function is required in all programs that use
  28161.  presentation graphics. The _pg_initchart function must be called before any
  28162.  of the other functions in the presentation-graphics library.
  28163.  
  28164.  The _pg_initchart function assumes a valid graphics mode has been
  28165.  established. Therefore, it must be called only after a successful call to
  28166.  the library function _setvideomode.
  28167.  
  28168.  
  28169.  Return Value
  28170.  
  28171.  The _pg_initchart functions return 0 if there were no errors. A nonzero
  28172.  value indicates a failure.
  28173.  
  28174.  
  28175.  Compatibility
  28176.  
  28177.   ANSI   DOS   OS/2   UNIX   XENIX
  28178.  
  28179.  
  28180.  
  28181.  See Also
  28182.  
  28183.  _pg_defaultchart,  _pg_getchardef,  _pg_getpalette,  _pg_getstyleset,
  28184.  _pg_hlabelchart,  _pg_resetpalette,  _resetstyleset,  _pg_setchardef,
  28185.  _pg_setpalette,  _pg_setstyleset,  _pg_vlabelchart,  _setvideomode
  28186.  
  28187.  
  28188.  Example
  28189.  
  28190.  See the example for _pg_chart.
  28191.  
  28192.  
  28193.  
  28194.  
  28195.  
  28196.  _pg_resetpalette
  28197.  ────────────────────────────────────────────────────────────────────────────
  28198.  
  28199.  
  28200.  Description
  28201.  
  28202.  Resets palette colors, line styles, and patterns to default values.
  28203.  
  28204.    #include <pgchart.h>
  28205.  
  28206.    short _far _pg_resetpalette( void );
  28207.  
  28208.  
  28209.  Remarks
  28210.  
  28211.  The _pg_resetpalette function sets the palette colors, line styles, fill
  28212.  patterns, and plot characters for the palette to the default for the current
  28213.  screen mode.
  28214.  
  28215.  The palette used by the presentation-graphics routines is independent of the
  28216.  palette used by the low-level graphics routines.
  28217.  
  28218.  
  28219.  Return Value
  28220.  
  28221.  The _pg_resetpalette function returns 0 if there were no errors. If the
  28222.  screen mode is not valid, the value _BADSCREENMODE is returned.
  28223.  
  28224.  
  28225.  Compatibility
  28226.  
  28227.   ANSI   DOS   OS/2   UNIX   XENIX
  28228.  
  28229.  
  28230.  
  28231.  See Also
  28232.  
  28233.  _pg_defaultchart,  _pg_getpalette,   _pg_initchart,  _pg_setpalette
  28234.  
  28235.  
  28236.  Example
  28237.  
  28238.  See the example for _pg_getpalette.
  28239.  
  28240.  
  28241.  
  28242.  
  28243.  
  28244.  _pg_resetstyleset
  28245.  ────────────────────────────────────────────────────────────────────────────
  28246.  
  28247.  
  28248.  Description
  28249.  
  28250.  Resets styleset to default values.
  28251.  
  28252.    #include <pgchart.h>
  28253.  
  28254.    void _far _pg_resetstyleset( void );
  28255.  
  28256.  
  28257.  Remarks
  28258.  
  28259.  The _pg_resetstyleset function reinitializes the styleset to the default
  28260.  values for the current screen mode.
  28261.  
  28262.  
  28263.  Return Value
  28264.  
  28265.  None.
  28266.  
  28267.  
  28268.  Compatibility
  28269.  
  28270.   ANSI   DOS   OS/2   UNIX   XENIX
  28271.  
  28272.  
  28273.  
  28274.  See Also
  28275.  
  28276.  _pg_defaultchart,  _pg_getstyleset,  _pg_initchart,  _pg_setstyleset
  28277.  
  28278.  
  28279.  Example
  28280.  
  28281.  See the example for _pg_getpalette.
  28282.  
  28283.  
  28284.  
  28285.  
  28286.  
  28287.  _pg_setchardef
  28288.  ────────────────────────────────────────────────────────────────────────────
  28289.  
  28290.  
  28291.  Description
  28292.  
  28293.  Sets the pixel bit map for the specified character.
  28294.  
  28295.    #include <pgchart.h>
  28296.  
  28297.    short _far _pg_setchardef( short charnum, unsigned char _far *chardef  );
  28298.  
  28299.  charnum                           ASCII number of character
  28300.  
  28301.  chardef                           Pointer to an 8-by-8 bit map array for
  28302.                                    the character
  28303.  
  28304.  
  28305.  Remarks
  28306.  
  28307.  The _pg_setchardef function sets the 8-by-8 pixel bit map for the character
  28308.  with the ASCII number charnum. The bit map is stored in the chardef array.
  28309.  
  28310.  
  28311.  Return Value
  28312.  
  28313.  The _pg_setchardef function returns 0 if there was no error. A nonzero value
  28314.  indicates an error.
  28315.  
  28316.  
  28317.  Compatibility
  28318.  
  28319.   ANSI   DOS   OS/2   UNIX   XENIX
  28320.  
  28321.  
  28322.  
  28323.  See Also
  28324.  
  28325.  _pg_defaultchart,  _pg_getchardef,  _pg_initchart
  28326.  
  28327.  
  28328.  
  28329.  
  28330.  
  28331.  _pg_setpalette
  28332.  ────────────────────────────────────────────────────────────────────────────
  28333.  
  28334.  
  28335.  Description
  28336.  
  28337.  Sets palette colors, line styles, and patterns.
  28338.  
  28339.    #include <pgchart.h>
  28340.  
  28341.    short _far _pg_setpalette( paletteentry _far *palette );
  28342.  
  28343.  palette                           Pointer to first palette structure in
  28344.                                    array
  28345.  
  28346.  
  28347.  Remarks
  28348.  
  28349.  The _pg_setpalette function sets palette colors, line styles, fill patterns,
  28350.  and plot characters for all palettes. The pointer palette points to an array
  28351.  of palette structures that contain the desired palette values.
  28352.  
  28353.  The palette used by the presentation-graphics routines is independent of the
  28354.  palette used by the low-level graphics routines.
  28355.  
  28356.  
  28357.  Return Value
  28358.  
  28359.  The _pg_setpalette function returns 0 if there were no errors. If the new
  28360.  palettes are not valid, the value _BADSCREENMODE is returned.
  28361.  
  28362.  
  28363.  Compatibility
  28364.  
  28365.   ANSI   DOS   OS/2   UNIX   XENIX
  28366.  
  28367.  
  28368.  
  28369.  See Also
  28370.  
  28371.  _pg_defaultchart,  _pg_getpalette,  _pg_initchart,  _pg_resetpalette
  28372.  
  28373.  
  28374.  Example
  28375.  
  28376.  See the example for _pg_getpalette.
  28377.  
  28378.  
  28379.  
  28380.  
  28381.  
  28382.  _pg_setstyleset
  28383.  ────────────────────────────────────────────────────────────────────────────
  28384.  
  28385.  
  28386.  Description
  28387.  
  28388.  Sets the current styleset.
  28389.  
  28390.    #include <pgchart.h>
  28391.  
  28392.    void _far _pg_setstyleset( unsigned short _far *styleset );
  28393.  
  28394.  styleset                          Pointer to new styleset
  28395.  
  28396.  
  28397.  Remarks
  28398.  
  28399.  The _pg_setstyleset function sets the current styleset.
  28400.  
  28401.  
  28402.  Return Value
  28403.  
  28404.  None.
  28405.  
  28406.  
  28407.  Compatibility
  28408.  
  28409.   ANSI   DOS   OS/2   UNIX   XENIX
  28410.  
  28411.  
  28412.  
  28413.  See Also
  28414.  
  28415.  _pg_defaultchart,  _pg_getstyleset,  _pg_initchart,  _pg_resetstyleset
  28416.  
  28417.  
  28418.  Example
  28419.  
  28420.  See the example for _pg_getpalette.
  28421.  
  28422.  
  28423.  
  28424.  
  28425.  
  28426.  _pg_vlabelchart
  28427.  ────────────────────────────────────────────────────────────────────────────
  28428.  
  28429.  
  28430.  Description
  28431.  
  28432.  Writes text vertically on the screen.
  28433.  
  28434.    #include <pgchart.h>
  28435.  
  28436.    short _far _pg_vlabelchart( chartenv _far *env, short x, short y, short
  28437.    color,
  28438.    char _far *label );
  28439.  
  28440.  env                               Chart environment structure
  28441.  
  28442.  x                                 Pixel x coordinate for text
  28443.  
  28444.  y                                 Pixel y coordinate for text
  28445.  
  28446.  color                             Color code for text
  28447.  
  28448.  label                             Label text
  28449.  
  28450.  
  28451.  
  28452.  
  28453.  Remarks
  28454.  
  28455.  The _pg_vlabelchart function writes text vertically on the screen. The
  28456.  arguments x and y are pixel coordinates for the beginning location of text
  28457.  relative to the upper-left corner of the chart window.
  28458.  
  28459.  
  28460.  Return Value
  28461.  
  28462.  The _pg_vlabelchart function returns 0 if there were no errors. A nonzero
  28463.  value indicates a failure.
  28464.  
  28465.  
  28466.  Compatibility
  28467.  
  28468.   ANSI   DOS   OS/2   UNIX   XENIX
  28469.  
  28470.  
  28471.  
  28472.  See Also
  28473.  
  28474.  _pg_defaultchart,  _pg_hlabelchart,  _pg_initchart
  28475.  
  28476.  
  28477.  Example
  28478.  
  28479.  See the example for _pg_getpalette.
  28480.  
  28481.  
  28482.  
  28483.  
  28484.  
  28485.  _pie Functions
  28486.  ────────────────────────────────────────────────────────────────────────────
  28487.  
  28488.  
  28489.  Description
  28490.  
  28491.  Draw wedge-shaped figures.
  28492.  
  28493.    #include <graph.h>
  28494.  
  28495.    short _far _pie( short control, short x1, short y1, short x2, short y2,
  28496.    short x3, short y3, short x4, short y4 );
  28497.  
  28498.    short _far _pie_w( short control, double x1, double y1, double x2, double
  28499.    y2,
  28500.    double x3, double y3, double x4, double y4 );
  28501.  
  28502.    short _far _pie_wxy( short control, struct _wxycoord _far *pwxy1,
  28503.    struct _wxycoord _far *pwxy2, struct _wxycoord _far *pwxy3,
  28504.    struct _wxycoord _far*pwxy4 );
  28505.  
  28506.  control                           Fill-control constant
  28507.  
  28508.  x1, y1                            Upper-left corner of bounding rectangle
  28509.  
  28510.  x2, y2                            Lower-right corner of bounding rectangle
  28511.  
  28512.  x3, y3                            Start vector
  28513.  
  28514.  x4, y4                            End vector
  28515.  
  28516.  pwxy1                             Upper-left corner of bounding rectangle
  28517.  
  28518.  pwxy2                             Lower-right corner of bounding rectangle
  28519.  
  28520.  pwxy3                             Start vector
  28521.  
  28522.  pwxy4                             End vector
  28523.  
  28524.  
  28525.  Remarks
  28526.  
  28527.  The _pie functions draw a pie-shaped wedge by drawing an elliptical arc
  28528.  whose center and two endpoints are joined by lines.
  28529.  
  28530.  The _pie function uses the view coordinate system. The center of the arc is
  28531.  the center of the bounding rectangle specified by the view coordinate points
  28532.  (x1, y1) and (x2, y2). The arc starts where it intersects the vector defined
  28533.  by (x3, y3) and ends where it intersects the vector (x4, y4).
  28534.  
  28535.  The _pie_wxy and _pie_w functions use the window coordinate system. The
  28536.  center of the arc is the center of the bounding rectangle specified by the
  28537.  window coordinate pairs pwxy1 and pwxy2 for _pie_wxy, and by the points (x1,
  28538.  y1) and (x2, y2) for _pie_w. The arc starts where it intersects the vector
  28539.  defined by pwxy3 or (x3, y3) and ends where it intersects the vector defined
  28540.  by pwxy4 or (x4, y4).
  28541.  
  28542.  The _wxycoord structure is defined in GRAPH.H and contains the following
  28543.  elements:
  28544.  
  28545.  Element                           Description
  28546.  ────────────────────────────────────────────────────────────────────────────
  28547.  double wx                         Window x coordinate
  28548.  
  28549.  double wy                         Window y coordinate
  28550.  
  28551.  The wedge is drawn using the current color moving in a counterclockwise
  28552.  direction. The control parameter can be one of the following manifest
  28553.  constants:
  28554.  
  28555.  Constant                          Action
  28556.  ────────────────────────────────────────────────────────────────────────────
  28557.  _GFILLINTERIOR                    Fills the figure using the current color
  28558.                                    and fill mask
  28559.  
  28560.  _GBORDER                          Does not fill the figure
  28561.  
  28562.  The control option given by _GFILLINTERIOR is equivalent to a subsequent
  28563.  call to the _floodfill function using the approximate center of the arc as
  28564.  the starting point and the current color (set by _setcolor) as the boundary
  28565.  color.
  28566.  
  28567.  
  28568.  Return Value
  28569.  
  28570.  These functions return a nonzero value if successful; otherwise, they return
  28571.  0.
  28572.  
  28573.  
  28574.  Compatibility
  28575.  
  28576.   ANSI   DOS   OS/2   UNIX   XENIX
  28577.  
  28578.  
  28579.  
  28580.  See Also
  28581.  
  28582.  _arc functions,  _ellipse functions,  _floodfill,  _getcolor,  _lineto
  28583.  functions, _rectangle functions,  _setcolor,  _setfillmask
  28584.  
  28585.  
  28586.  Example
  28587.  
  28588.    /* PIE.C: This program draws a pie-shaped figure. */
  28589.  
  28590.    #include <stdlib.h>
  28591.    #include <conio.h>
  28592.    #include <graph.h>
  28593.  
  28594.    void main()
  28595.    {
  28596.       /* Find a valid graphics mode. */
  28597.       if( !_setvideomode( _MAXRESMODE ) )
  28598.          exit( 1 );
  28599.  
  28600.       _pie( _GBORDER, 80, 50, 240, 150, 240, 12, 0, 150 );
  28601.       getch();
  28602.  
  28603.       _setvideomode( _DEFAULTMODE );
  28604.    }
  28605.  
  28606.  
  28607.  
  28608.  
  28609.  
  28610.  _pipe
  28611.  ────────────────────────────────────────────────────────────────────────────
  28612.  
  28613.  
  28614.  Description
  28615.  
  28616.  Creates a pipe for reading and writing.
  28617.  
  28618.  #include <fcntl.h>                For O_BINARY and O_TEXT definitions
  28619.  
  28620.  #include <errno.h>                errno definitions
  28621.  
  28622.  #include <io.h>                   Prototype declaration
  28623.  
  28624.    int _pipe( int *phandles, unsigned int psize, int textmode );
  28625.  
  28626.  phandles[2]                       Array to hold read and write handles
  28627.  
  28628.  psize                             Amount of memory to reserve
  28629.  
  28630.  textmode                          File mode
  28631.  
  28632.  
  28633.  Remarks
  28634.  
  28635.  A pipe is an artificial file-like I/O channel that a program can create and
  28636.  use to pass information to other programs. A pipe is similar to a file in
  28637.  that it has a file pointer or a file descriptor, or both, and can be read
  28638.  from or written to using the input and output functions of the standard
  28639.  library. Unlike a file, a pipe does not represent a specific file or device.
  28640.  Instead, a pipe represents temporary storage in memory that is independent
  28641.  of the program's own memory and is controlled entirely by the operating
  28642.  system.
  28643.  
  28644.  Pipes may be used to pass information between programs. For example, the
  28645.  command processor in OS/2 creates a pipe when executing a command such as
  28646.  
  28647.    PROGRAM1 | PROGRAM2
  28648.  
  28649.  The standard output handle of PROGRAM1 is attached to the pipe's write
  28650.  handle. The standard input handle of PROGRAM2 is attached to the pipe's read
  28651.  handle. This eliminates the need for creating temporary files to pass
  28652.  information to other programs.
  28653.  
  28654.  The _pipe function creates a pipe. This function is similar to open but
  28655.  opens the pipe for both reading and writing, returning two file handles
  28656.  instead of one. The program can either use both sides of the pipe or close
  28657.  the one it does not need. This function typically opens a pipe in
  28658.  preparation for linking it to a child process.
  28659.  
  28660.  The _pipe function opens a pipe and returns two handles to the pipe in the
  28661.  phandles argument. The element phandles[0] contains the read handle, and the
  28662.  element phandles[1] contains the write handle. Pipe file handles are used in
  28663.  the same way as other file handles. (The low-level input and output
  28664.  functions read and write can read from and write to a pipe.)
  28665.  
  28666.  The psize argument specifies the amount of memory, in bytes, to reserve for
  28667.  the pipe.
  28668.  
  28669.  The textmode argument specifies the translation mode for the pipe. The
  28670.  manifest constant O_TEXT specifies a text translation, and the constant
  28671.  O_BINARY specifies binary translation. (See fopen for a description of text
  28672.  and binary modes.) If the textmode argument is 0, the _pipe function uses
  28673.  the default translation mode specified by the default-mode variable _fmode.
  28674.  
  28675.  
  28676.  In multithread programs, no locking is performed. The handles returned are
  28677.  newly opened and should not be referenced by any thread until after the
  28678.  _pipe call is complete.
  28679.  
  28680.  Under OS/2, a pipe is destroyed when all its handles have been closed. (If
  28681.  all read handles on the pipe have been closed, writing to the pipe will
  28682.  cause an error.) All read and write operations on the pipe wait until there
  28683.  is enough data or enough buffer space to complete the I/O request.
  28684.  
  28685.  
  28686.  Return Value
  28687.  
  28688.  The _pipe function returns 0 if successful. A return value of -1 indicates
  28689.  an error, and errno is set to one of the following values:
  28690.  
  28691.  Value                             Meaning
  28692.  ────────────────────────────────────────────────────────────────────────────
  28693.  EMFILE                            No more file handles available (too many
  28694.                                    open files)
  28695.  
  28696.  ENFILE                            System file table overflow
  28697.  
  28698.  
  28699.  Compatibility
  28700.  
  28701.   ANSI   DOS   OS/2   UNIX   XENIX
  28702.  
  28703.  
  28704.  A similar function (pipe) is available in the XENIX and UNIX operating
  28705.  environments.
  28706.  
  28707.  
  28708.  See Also
  28709.  
  28710.  cwait,  _pclose,  _popen
  28711.  
  28712.  
  28713.  Example
  28714.  
  28715.    /* PIPE.C: This program uses _pipe to pass streams of text to
  28716.     * child processes.
  28717.     */
  28718.  
  28719.    #include <stdlib.h>
  28720.    #include <stdio.h>
  28721.    #include <io.h>
  28722.    #include <fcntl.h>
  28723.    #include <process.h>            /* _pipe */
  28724.    #include <math.h>
  28725.  
  28726.    enum PIPES { READ, WRITE };     /* Constants 0 and 1 for READ and WRITE */
  28727.    #define NUMPROBLEM 8
  28728.  
  28729.    void main( int argc, char *argv[] )
  28730.    {
  28731.        int     hpipe[2];
  28732.        char    hstr[20];
  28733.        int     termstat, pid, problem, c;
  28734.  
  28735.        /* If no arguments, this is the parent. */
  28736.        if( argc == 1 )
  28737.        {
  28738.            /* Open a sets of pipes. */
  28739.            if( _pipe( hpipe, 256, O_BINARY ) == -1 )
  28740.                exit( 1 );
  28741.  
  28742.            /* Convert pipe read handle to string and pass as argument to
  28743.             * spawned child. Program spawns itself (argv[0]).
  28744.             */
  28745.            itoa( hpipe[READ],  hstr,  10 );
  28746.            if( spawnl( P_NOWAIT, argv[0], argv[0], hstr, NULL )  == -1 )
  28747.                printf( "Spawn failed" );
  28748.  
  28749.            /* Put problem in write pipe. Since child is running
  28750.  simultaneously,
  28751.             * first solutions may be done before last problem is given.
  28752.             */
  28753.            for( problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000
  28754.  )
  28755.            {
  28756.                printf( "Son, what is the square root of %d?\n", problem );
  28757.                write( hpipe[WRITE], (char *)&problem, sizeof( int ) );
  28758.            }
  28759.  
  28760.            /* Wait until child is done processing. */
  28761.            wait( &termstat );
  28762.            if( termstat & 0xff )
  28763.                printf( "Child failed\n" );
  28764.  
  28765.            close( hpipe[READ] );
  28766.            close( hpipe[WRITE] );
  28767.        }
  28768.  
  28769.    /* If there is an argument, this must be the child. */
  28770.        else
  28771.        {
  28772.            /* Convert passed string handle to integer handle. */
  28773.            hpipe[READ] = atoi( argv[1] );
  28774.  
  28775.            /* Read problem from pipe and calculate solution. */
  28776.            for( c = 0; c < NUMPROBLEM; c++ )
  28777.            {
  28778.                read( hpipe[READ], (char *)&problem, sizeof( int ) );
  28779.                printf( "Dad, the square root of %d is %3.2f.\n",
  28780.                        problem, sqrt( (double)problem ) );;
  28781.            }
  28782.        }
  28783.    }
  28784.  
  28785.  
  28786.  Output
  28787.  
  28788.  
  28789.  
  28790.    Son, what is the square root of 1000?
  28791.    Dad, the square root of 1000 is 31.62.
  28792.    Son, what is the square root of 2000?
  28793.    Son, what is the square root of 3000?
  28794.    Dad, the square root of 2000 is 44.72.
  28795.    Son, what is the square root of 4000?
  28796.    Dad, the square root of 3000 is 54.77.
  28797.    Son, what is the square root of 5000?
  28798.    Dad, the square root of 4000 is 63.25.
  28799.    Son, what is the square root of 6000?
  28800.    Dad, the square root of 5000 is 70.71.
  28801.    Son, what is the square root of 7000?
  28802.    Dad, the square root of 6000 is 77.46.
  28803.    Son, what is the square root of 8000?
  28804.    Dad, the square root of 7000 is 83.67.
  28805.    Dad, the square root of 8000 is 89.44.
  28806.  
  28807.  
  28808.  
  28809.  
  28810.  
  28811.  _polygon Functions
  28812.  ────────────────────────────────────────────────────────────────────────────
  28813.  
  28814.  
  28815.  Description
  28816.  
  28817.  Draw polygon shapes.
  28818.  
  28819.    #include <graph.h>
  28820.  
  28821.    short _far _polygon( short control, struct xycoord _far *points, short
  28822.    numpoints );
  28823.  
  28824.    short _far _polygon_w( short control, double _far *points, short numpoints
  28825.    );
  28826.  
  28827.    short _far _polygon_wxy( short control, struct _wxycoord _far *points,
  28828.    short numpoints );
  28829.  
  28830.  control                           Fill flag
  28831.  
  28832.  points                            Pointer to an array of structures
  28833.                                    defining the polygon
  28834.  
  28835.  numpoints                         Number of points
  28836.  
  28837.  
  28838.  Remarks
  28839.  
  28840.  The _polygon functions draw polygons. The border of the polygon is drawn in
  28841.  the current color and line style. The _polygon routine uses the view
  28842.  coordinate system (expressed in xycoord structures), and the _polygon_wxy
  28843.  and _polygon_w routines use real-valued window coordinates (expressed in
  28844.  _wxycoord structures and in pairs of double-precision floating-point values,
  28845.  respectively).
  28846.  
  28847.  The argument points is an array of xycoord or _wxycoord structures or pairs
  28848.  of doubles, each of which specifies one of the polygon's vertices. (For
  28849.  _polygon_w, points[0] and points[1] specify the x and y coordinates,
  28850.  respectively, of the first point.) The argument numpoints indicates the
  28851.  number of elements (the number of vertices) in the points array.
  28852.  
  28853.  The control argument can be one of the following manifest constants:
  28854.  
  28855.  Constant                          Action
  28856.  ────────────────────────────────────────────────────────────────────────────
  28857.  _GFILLINTERIOR                    Fills the polygon using the current fill
  28858.                                    mask
  28859.  
  28860.  _GBORDER                          Does not fill the polygon
  28861.  
  28862.  The _setwritemode, _setlinestyle, and _setfillmask functions all affect the
  28863.  output from the_polygon functions.
  28864.  
  28865.  
  28866.  Return Value
  28867.  
  28868.  The _polygon functions return a nonzero value if the arc is successfully
  28869.  drawn; otherwise, they return 0.
  28870.  
  28871.  
  28872.  Compatibility
  28873.  
  28874.   ANSI   DOS   OS/2   UNIX   XENIX
  28875.  
  28876.  
  28877.  
  28878.  See Also
  28879.  
  28880.  _ellipse functions,  _floodfill,  _lineto functions,  _pie functions,
  28881.  _rectangle functions,  _setcolor,  _setfillmask,  _setlinestyle,
  28882.  _setwritemode
  28883.  
  28884.  
  28885.  Example
  28886.  
  28887.    /* POLYGON.C: This program draws a star-shaped polygon. */
  28888.  
  28889.    #include <conio.h>
  28890.    #include <stdlib.h>
  28891.    #include <graph.h>
  28892.    #include <math.h>
  28893.    #include <stdlib.h>
  28894.  
  28895.    #define PI 3.1415
  28896.  
  28897.    void main()
  28898.    {
  28899.       short  side, radius = 90, x = 0, y = 0;
  28900.       double radians;
  28901.       struct xycoord polyside[5];
  28902.       struct videoconfig vc;
  28903.  
  28904.       /* Find a valid graphics mode. */
  28905.       if( !_setvideomode( _MAXRESMODE ) )
  28906.          exit( 1 );
  28907.  
  28908.       _getvideoconfig( &vc );
  28909.       _setvieworg( vc.numxpixels / 2, vc. numypixels / 2 );
  28910.  
  28911.       /* Calculate points of star every 144 degrees, then connect them. */
  28912.       for( side = 0; side < 5; side++ )
  28913.       {
  28914.           radians = 144 * PI / 180;
  28915.           polyside[side].xcoord = x + (short)(cos( side * radians ) *
  28916.  radius);
  28917.           polyside[side].ycoord = y + (short)(sin( side * radians ) *
  28918.  radius);
  28919.       }
  28920.       _polygon( _GFILLINTERIOR, polyside, 5 );
  28921.  
  28922.       getch();
  28923.       _setvideomode( _DEFAULTMODE );
  28924.    }
  28925.  
  28926.  
  28927.  
  28928.  
  28929.  
  28930.  _popen
  28931.  ────────────────────────────────────────────────────────────────────────────
  28932.  
  28933.  
  28934.  Description
  28935.  
  28936.  Creates a pipe and executes a command.
  28937.  
  28938.  #include <stdio.h>                Required for function declarations only
  28939.  
  28940.    FILE *_popen( char *command, char *mode );
  28941.  
  28942.  command                           Command to be executed
  28943.  
  28944.  mode                              Mode of returned stream
  28945.  
  28946.  
  28947.  Remarks
  28948.  
  28949.  The _popen function creates a pipe and asynchronously executes a child copy
  28950.  of the command processor with the specified command string command. See
  28951.  _pipe for a general discussion of pipes in OS/2. The character string mode
  28952.  specifies the type of access requested, as follows:
  28953.  
  28954.  Type                              Description
  28955.  ────────────────────────────────────────────────────────────────────────────
  28956.  "r"                               The calling process can read the child
  28957.                                    command's standard output via the
  28958.                                    returned stream.
  28959.  
  28960.  "w"                               The calling process can write to the
  28961.                                    child command's standard input via the
  28962.                                    returned stream.
  28963.  
  28964.  "b"                               Open in binary mode.
  28965.  
  28966.  "t"                               Open in text mode.
  28967.  
  28968.  See Section 2.7, "Input and Output," for a discussion of text and binary
  28969.  modes.
  28970.  
  28971.  
  28972.  Return Value
  28973.  
  28974.  The _popen function returns a stream associated with one end of the created
  28975.  pipe. The other end of the pipe is associated with the child command's
  28976.  standard input or standard output. If an error occurs, NULL is returned.
  28977.  
  28978.  
  28979.  Compatibility
  28980.  
  28981.   ANSI   DOS   OS/2   UNIX   XENIX
  28982.  
  28983.  
  28984.  A similar function (popen) is available in the XENIX and UNIX operating
  28985.  environments.
  28986.  
  28987.  
  28988.  See Also
  28989.  
  28990.  _pclose,  _pipe
  28991.  
  28992.  
  28993.  Example
  28994.  
  28995.    /* POPEN.C: This program uses _popen and _pclose to receive a stream
  28996.     * of text from a child system process.
  28997.     */
  28998.    #include <stdio.h>
  28999.    #include <stdlib.h>
  29000.  
  29001.    void main()
  29002.    {
  29003.       char    buffer[128];
  29004.       FILE    *chkdsk;
  29005.  
  29006.       /* Run CHKDSK so that it writes its output to a pipe. Open pipe
  29007.        * with read text attribute so that we can read it like a text file.
  29008.        */
  29009.       if( (chkdsk = _popen( "dir po*.c | sort | more", "rt" )) == NULL )
  29010.          exit( 1 );
  29011.  
  29012.       /* Read pipe until end of file. End of file indicates that CHKDSK
  29013.        * closed its standard out (probably meaning it terminated).
  29014.        */
  29015.       while( !feof( chkdsk ) )
  29016.       {
  29017.          if( fgets( buffer, 128, chkdsk ) != NULL )
  29018.             printf( buffer );
  29019.       }
  29020.  
  29021.       /* Close pipe and print return value of CHKDSK. */
  29022.       printf( "\nChild returned %d\n", _pclose( chkdsk ) );
  29023.    }
  29024.  
  29025.  
  29026.  Output
  29027.  
  29028.  
  29029.  
  29030.         3 File(s)   12683264 bytes free
  29031.     Directory of C:\LIBREF
  29032.     The volume label in drive C is OS2.
  29033.    POLYGON  C        921   6-14-89   6:51p
  29034.    POPEN    C        845   6-19-89   2:48p
  29035.    POW      C        190   6-13-89   6:07p
  29036.  
  29037.    Child returned 0
  29038.  
  29039.  
  29040.  
  29041.  
  29042.  
  29043.  pow Functions
  29044.  ────────────────────────────────────────────────────────────────────────────
  29045.  
  29046.  
  29047.  Description
  29048.  
  29049.  Calculate x raised to the power of y.
  29050.  
  29051.    #include <math.h>
  29052.  
  29053.    double pow( double x, double y );
  29054.  
  29055.    long double powl( long double x, long double y );
  29056.  
  29057.  x                                 Number to be raised
  29058.  
  29059.  y                                 Power of x
  29060.  
  29061.  
  29062.  Remarks
  29063.  
  29064.  The pow and powl functions compute x raised to the power of y.
  29065.  
  29066.  The powl function is the 80-bit counterpart, and it uses an 80-bit, 10-byte
  29067.  coprocessor form of arguments and return values. See the reference page on
  29068.  the long double functions for more details on this data type.
  29069.  
  29070.  
  29071.  Return Value
  29072.  
  29073.  The pow and powl functions return the value of xy. If x is not 0.0 and y is
  29074.  0.0, pow and powl return the value 1. If x is 0.0 and y is negative, pow and
  29075.  powl set errno to EDOM and return 0.0. If both x and y are 0.0, or if x is
  29076.  negative and y is not an integer, the function prints a DOMAIN error message
  29077.  to stderr, sets errno to EDOM, and returns 0.0. If an overflow results, the
  29078.  function sets errno to ERANGE and returns±HUGE_VAL. No message is printed on
  29079.  overflow or underflow.
  29080.  
  29081.  The pow function does not recognize integral floating-point values greater
  29082.  than 264, such as  1.0E100.
  29083.  
  29084.  
  29085.  Compatibility
  29086.  
  29087.  pow
  29088.  
  29089.   ANSI   DOS   OS/2   UNIX   XENIX
  29090.  
  29091.  
  29092.  powl
  29093.  
  29094.   ANSI   DOS   OS/2   UNIX   XENIX
  29095.  
  29096.  
  29097.  
  29098.  See Also
  29099.  
  29100.  exp, log functions, sqrt
  29101.  
  29102.  
  29103.  Example
  29104.  
  29105.    /* POW.C */
  29106.    #include <math.h>
  29107.    #include <stdio.h>
  29108.  
  29109.    void main()
  29110.    {
  29111.       double x = 2.0, y = 3.0, z;
  29112.  
  29113.       z = pow( x, y );
  29114.       printf( "%.1f to the power of %.1f is %.1f\n", x, y, z );
  29115.    }
  29116.  
  29117.  
  29118.  Output
  29119.  
  29120.  
  29121.  
  29122.    2.0 to the power of 3.0 is 8.0
  29123.  
  29124.  
  29125.  
  29126.  
  29127.  
  29128.  printf
  29129.  ────────────────────────────────────────────────────────────────────────────
  29130.  
  29131.  
  29132.  Description
  29133.  
  29134.  Prints formatted output to the standard output stream.
  29135.  
  29136.    #include <stdio.h>
  29137.  
  29138.    int printf( const char *format [[, argument]]... );
  29139.  
  29140.  format                            Format control
  29141.  
  29142.  argument                          Optional arguments
  29143.  
  29144.  
  29145.  Remarks
  29146.  
  29147.  The printf function formats and prints a series of characters and values to
  29148.  the standard output stream, stdout. The format argument consists of ordinary
  29149.  characters, escape sequences, and (if arguments follow format) format
  29150.  specifications. The ordinary characters and escape sequences are copied to
  29151.  stdout in order of their appearance. For example, the line
  29152.  
  29153.    printf("Line one\n\t\tLine two\n");
  29154.  
  29155.  produces the output
  29156.  
  29157.    Line one
  29158.            Line two
  29159.  
  29160.  If arguments follow the format string, the format string must contain
  29161.  specifications that determine the output format for the arguments.
  29162.  
  29163.  Format specifications always begin with a percent sign (%) and are read left
  29164.  to right. When the first format specification (if any) is encountered, the
  29165.  value of the first argument after format is converted and output
  29166.  accordingly. The second format specification causes the second argument to
  29167.  be converted and output, and so on. If there are more arguments than there
  29168.  are format specifications, the extra arguments are ignored. The results are
  29169.  undefined if there are not enough arguments for all the format
  29170.  specifications.
  29171.  
  29172.  A format specification, which consists of optional and required fields, has
  29173.  the following form:
  29174.  
  29175.    %[[flags]] [[width]] [[.precision]] [[{F | N | h | l | L}]] type
  29176.  
  29177.  Format Specification Fields
  29178.  
  29179.  Each field of the format specification is a single character or a number
  29180.  signifying a particular format option. The simplest format specification
  29181.  contains only the percent sign and a type character (for example,  %s). The
  29182.  optional fields, which appear before the type character, control other
  29183.  aspects of the formatting. The fields in a printf format specification are
  29184.  described in the following list:
  29185.  
  29186.  Field                             Description
  29187.  ────────────────────────────────────────────────────────────────────────────
  29188.  type                              Required character that determines
  29189.                                    whether the associated argument is
  29190.                                    interpreted as a character, a string, or
  29191.                                    a number. (See Table R.2.)
  29192.  
  29193.  flags                             Optional character or characters that
  29194.                                    control justification of output and
  29195.                                    printing of signs, blanks, decimal
  29196.                                    points, and octal and hexadecimal
  29197.                                    prefixes. (See Table R.3.) More than one
  29198.                                    flag can appear in a format
  29199.                                    specification.
  29200.  
  29201.  width                             Optional number that specifies minimum
  29202.                                    number of characters output.
  29203.  
  29204.  precision                         Optional number that specifies maximum
  29205.                                    number of characters printed for all or
  29206.                                    part of the output field, or minimum
  29207.                                    number of digits printed for integer
  29208.                                    values. (See Table R.4.)
  29209.  
  29210.  F, N                              Optional prefixes that refer to the
  29211.                                    "distance" to the object being printed (
  29212.                                    near or far).
  29213.  
  29214.                                    F and N are not part of the ANSI
  29215.                                    definition for printf. They are
  29216.                                    Microsoft extensions that should not be
  29217.                                    used if ANSI portability is desired.
  29218.  
  29219.  h, l, L                           Optional prefixes that determine the
  29220.                                    size of the argument expected, as shown
  29221.                                    below:
  29222.  
  29223.                                    Prefix      Use
  29224.  ────────────────────────────────────────────────────────────────────────────
  29225.                                    h           Used with the integer types
  29226.                                                d, i, o, x, and X to specify
  29227.                                                that the argument is short
  29228.                                                int, or with u to specify
  29229.                                                short unsigned int. If used
  29230.                                                with %p, it indicates a
  29231.                                                16-bit pointer.
  29232.  
  29233.                                    l           Used with d, i, o, x, and X
  29234.                                                types to specify that the
  29235.                                                argument is long int, or
  29236.                                                with u to specify long
  29237.                                                unsigned int; also used with
  29238.                                                e, E, f, g, and G types to
  29239.                                                specify double rather than
  29240.                                                float. If used with %p, it
  29241.                                                indicates a 32-bit pointer.
  29242.  
  29243.                                    L           Used with e, E, f, g, and G
  29244.                                                types to specify long double
  29245.                                                .
  29246.  
  29247.  If a percent sign is followed by a character that has no meaning as a format
  29248.  field, the character is copied to stdout. For example, to print a
  29249.  percent-sign character, use %%.
  29250.  
  29251.  
  29252.  Type Field Characters
  29253.  
  29254.  The type character is the only required format field for the printf
  29255.  function; it appears after any optional format fields. The type character
  29256.  determines whether the associated argument is interpreted as a character, a
  29257.  string, or a number. (See Table R.2.)
  29258.  
  29259.  Table   R.2   Type Characters for printf
  29260.  
  29261. ╓┌──────────┌────────────┌───────────────────────────────────────────────────╖
  29262.  Character  Type         Output Format
  29263.  ────────────────────────────────────────────────────────────────────────────
  29264.  d          int          Signed decimal integer.
  29265.  
  29266.  i          int          Signed decimal integer.
  29267.  
  29268.  u          int          Unsigned decimal integer.
  29269.  
  29270.  o          int          Unsigned octal integer.
  29271.  
  29272.  x          int          Unsigned hexadecimal integer, using "abcdef."
  29273.  
  29274.  X          int          Unsigned hexadecimal integer, using "ABCDEF."
  29275.  
  29276.  Character  Type         Output Format
  29277.  ────────────────────────────────────────────────────────────────────────────
  29278. 
  29279.  f          double       Signed value having the form [-]dddd.dddd, where
  29280.                          dddd is one or more decimal digits. The number of
  29281.                          digits before the decimal point depends on the
  29282.                          magnitude of the number, and the number of digits
  29283.                          after the decimal point depends on the requested
  29284.                          precision.
  29285.  
  29286.  e          double       Signed value having the form [-]d.dddd e [sign]ddd
  29287.                          , where d is a single decimal digit, dddd is one
  29288.                          or more decimal digits, ddd is exactly three
  29289.                          decimal digits, and sign is + or -.
  29290.  
  29291.  E          double       Identical to the e format, except that E, rather
  29292.                          than e, introduces the exponent.
  29293.  
  29294.  g          double       Signed value printed in f or e format, whichever
  29295.                          is more compact for the given value and precision.
  29296.                          The e format is used only when the exponent of the
  29297.  Character  Type         Output Format
  29298.  ────────────────────────────────────────────────────────────────────────────
  29299.                         The e format is used only when the exponent of the
  29300.                          value is less than - 4 or greater than or equal to
  29301.                          the precision argument. Trailing zeros are
  29302.                          truncated, and the decimal point appears only if
  29303.                          one or more digits follow it.
  29304.  
  29305.  G          double       Identical to the g format, except that G, rather
  29306.                          than g, introduces the exponent (where
  29307.                          appropriate).
  29308.  
  29309.  c          int          Single character.
  29310.  
  29311.  s          String       Characters printed up to the first null character
  29312.                          ('\0') or until the
  29313.                          precision value is reached.
  29314.  
  29315.  n          Pointer to   Number of characters successfully written so far
  29316.             integer      to the stream or buffer; this value is stored in
  29317.                          the integer whose address is given as the
  29318.  Character  Type         Output Format
  29319.  ────────────────────────────────────────────────────────────────────────────
  29320.                         the integer whose address is given as the
  29321.                          argument.
  29322.  
  29323.  p          Far pointer  Prints the address pointed to by the argument in
  29324.             to void      the form xxxx:yyyy, where xxxx is the segment and
  29325.                          yyyy is the offset, and the digits x and y are
  29326.                          uppercase hexadecimal digits; %hp indicates a near
  29327.                          pointer and prints only the offset of the address.
  29328.  
  29329.  ────────────────────────────────────────────────────────────────────────────
  29330.  
  29331.  
  29332.  
  29333.  
  29334.  Flag Directives
  29335.  
  29336.  The first optional field of the format specification is flag. A flag
  29337.  directive is a character that justifies output and prints signs, blanks,
  29338.  decimal points, and octal and hexadecimal prefixes. More than one flag
  29339.  directive may appear in a format specification. (See Table R.3.)
  29340.  
  29341.  Table   R.3   Flag Characters for printf
  29342.  
  29343. ╓┌──────────────────────────┌──────────────────────────┌─────────────────────
  29344.  Flag(, Meaning , Default)
  29345.  ─────────────────────────────────────────────────────────────────────────────
  29346.  -                          Left justify the result    Right justify.
  29347.                             within the given field
  29348.                             width.
  29349.  
  29350.  +                          Prefix the output value    Sign appears only for
  29351.                             with a sign                negative signed values
  29352.                             (+ or -) if the output     (-).
  29353.                             value is of a signed
  29354.                             type.
  29355.  
  29356.  0                          If width is prefixed with  No padding.
  29357.                             0, zeros are added until
  29358.                             the minimum width is
  29359.                             reached. If 0 and -
  29360.  Flag(, Meaning , Default)
  29361.  ─────────────────────────────────────────────────────────────────────────────
  29362.                            reached. If 0 and -
  29363.                             appear, the 0 is
  29364.                             ignored. If 0 is
  29365.                             specified with an
  29366.                             integer format (i, u, x,
  29367.                             X, o, d), the
  29368.                             0 is ignored.
  29369.  
  29370.  blank (' ')                Prefix the output value    No blank appears.
  29371.                             with a blank if the
  29372.                             output value is signed
  29373.                             and positive; the blank
  29374.                             is ignored if both the
  29375.                             blank and + flags appear.
  29376.  
  29377.  #                          When used with the o, x,   No blank appears.
  29378.                             or X format, the # flag
  29379.                             prefixes any nonzero
  29380.                             output value with 0, 0x,
  29381.  Flag(, Meaning , Default)
  29382.  ─────────────────────────────────────────────────────────────────────────────
  29383.                            output value with 0, 0x,
  29384.                             or 0X, respectively.
  29385.  
  29386.                             When used with the e, E,   Decimal point appears
  29387.                             or f format, the # flag    only if digits follow i
  29388.                             forces the output value
  29389.                             to contain a decimal
  29390.                             point in all cases.
  29391.  
  29392.                             When used with the g or G  Decimal point appears
  29393.                             format, the # flag forces  only if digits follow i
  29394.                             the output value to        Trailing zeros are
  29395.                             contain a decimal point    truncated.
  29396.                             in all cases and prevents
  29397.                             the truncation of
  29398.                             trailing zeros.
  29399.  
  29400.                             Ignored when used with c,
  29401.                             d, i, u, or s.
  29402.  Flag(, Meaning , Default)
  29403.  ─────────────────────────────────────────────────────────────────────────────
  29404.                            d, i, u, or s.
  29405.  
  29406.  ─────────────────────────────────────────────────────────────────────────────
  29407.  
  29408.  
  29409.  
  29410.  
  29411.  Width Specification
  29412.  
  29413.  The second optional field of the format specification is the width
  29414.  specification. The width argument is a non-negative decimal integer
  29415.  controlling the minimum number of characters printed. If the number of
  29416.  characters in the output value is less than the specified width, blanks are
  29417.  added to the left or the right of the values─depending on whether the  -
  29418.  flag (for left justification) is specified─until the minimum width is
  29419.  reached. If width is prefixed with 0, zeros are added until the minimum
  29420.  width is reached (not useful for left-justified numbers).
  29421.  
  29422.  The width specification never causes a value to be truncated. If the number
  29423.  of characters in the output value is greater than the specified width, or
  29424.  width is not given, all characters of the value are printed (subject to the
  29425.  precision specification).
  29426.  
  29427.  The width specification may be an asterisk (*), in which case an int
  29428.  argument from the argument list supplies the value. The width argument must
  29429.  precede the value being formatted in the argument list. A nonexistent or
  29430.  small field width does not cause a truncation of a field; if the result of a
  29431.  conversion is wider than the field width, the field expands to contain the
  29432.  conversion result.
  29433.  
  29434.  
  29435.  Precision Specification
  29436.  
  29437.  The third optional field of the format specification is the precision
  29438.  specification. It specifies a non-negative decimal integer, preceded by a
  29439.  period (.), which specifies the number of characters to be printed, the
  29440.  number of decimal places, or the number of significant digits. (See Table
  29441.  R.4.) Unlike the width specification, the precision specification can cause
  29442.  truncation of the output value, or rounding in the case of a floating-point
  29443.  value. If precision is specified as zero and the value to be converted is
  29444.  zero, the result is no characters output, as shown below:
  29445.  
  29446.    printf( "%.0d", 0 );      /* No characters output */
  29447.  
  29448.  The precision specification may be an asterisk (*), in which case an int
  29449.  argument from the argument list supplies the value. The precision argument
  29450.  must precede the value being formatted in the argument list.
  29451.  
  29452.  The interpretation of the precision value and the default when precision is
  29453.  omitted depend on the type, as shown in Table R.4.
  29454.  
  29455.  Table    R.4  How printf Precision Values Affect Type
  29456.  
  29457. ╓┌─────────────────┌──────────────────────────────┌──────────────────────────
  29458.  Type              Meaning                        Default
  29459.  ─────────────────────────────────────────────────────────────────────────────
  29460.  d, i, u, o, x, X  The precision specifies the    If precision is 0 or omitted
  29461.                    minimum number of digits to    entirely, or if the period (
  29462.                    be printed. If the number of   ) appears without a number
  29463.                    digits in the argument is      following it, the precision
  29464.                    less than precision, the       is set to 1.
  29465.  Type              Meaning                        Default
  29466.  ─────────────────────────────────────────────────────────────────────────────
  29467.                   less than precision, the       is set to 1.
  29468.                    output value is padded on the
  29469.                    left with zeros. The value is
  29470.                    not truncated when the number
  29471.                    of digits exceeds precision.
  29472.  
  29473.  e, E              The precision specifies the    Default precision is 6; if
  29474.                    number of digits to be         precision is 0 or the period
  29475.                    printed after the decimal      (.) appears without a number
  29476.                    point. The last printed digit  following it, no decimal
  29477.                    is rounded.                    point is printed.
  29478.  
  29479.  f                 The precision value specifies  Default precision is 6; if
  29480.                    the number of digits after     precision is 0, or if the
  29481.                    the decimal point. If a        period (.) appears without a
  29482.                    decimal point appears, at      number following it, no
  29483.                    least one digit appears        decimal point is printed.
  29484.                    before it. The value is
  29485.                    rounded to the appropriate
  29486.  Type              Meaning                        Default
  29487.  ─────────────────────────────────────────────────────────────────────────────
  29488.                   rounded to the appropriate
  29489.                    number of digits.
  29490.  
  29491.  g, G              The precision specifies the    Six significant digits are
  29492.                    maximum number of significant  printed, with any trailing
  29493.                    digits printed.                zeros truncated.
  29494.  
  29495.  c                 The precision has no effect.   Character is printed.
  29496.  
  29497.  s                 The precision specifies the    Characters are printed until
  29498.                    maximum number of characters   a null character is
  29499.                    to be printed. Characters in   encountered.
  29500.                    excess of precision are not
  29501.                    printed.
  29502.  
  29503.  ─────────────────────────────────────────────────────────────────────────────
  29504.  
  29505.  
  29506.  
  29507.  If the argument corresponding to a floating-point specifier is infinite,
  29508.  indefinite, or not a number (NAN), the printf function gives the following
  29509.  output:
  29510.  
  29511.  Value                             Output
  29512.  ────────────────────────────────────────────────────────────────────────────
  29513.  + infinity                        1.#INFrandom-digits
  29514.  
  29515.  - infinity                        -1.#INFrandom-digits
  29516.  
  29517.  Indefinite                        digit.#INDrandom-digits
  29518.  
  29519.  NAN                               digit.#NANrandom-digits
  29520.  
  29521.  
  29522.  Size and Distance Specification
  29523.  
  29524.  For printf, the format specification fields F and N refer to the "distance"
  29525.  to the object being read (near or far), and h and l refer to the "size" of
  29526.  the object being read (16-bit short or 32-bit long). The following list
  29527.  clarifies this use of F, N, h, l, and L:
  29528.  
  29529.  Program Code                      Action
  29530.  ────────────────────────────────────────────────────────────────────────────
  29531.  printf ("%Ns");                   Print near string
  29532.  
  29533.  printf ("%Fs");                   Print far string
  29534.  
  29535.  printf ("%Nn");                   Store char count in near int
  29536.  
  29537.  printf ("%Fn");                   Store char count in far int
  29538.  
  29539.  printf ("%hp");                   Print a 16-bit pointer (xxxx)
  29540.  
  29541.  printf ("%lp");                   Print a 32-bit pointer (xxxx:xxxx)
  29542.  
  29543.  printf ("%Nhn");                  Store char count in near short int
  29544.  
  29545.  printf ("%Nln");                  Store char count in near long int
  29546.  
  29547.  printf ("%Fhn");                  Store char count in far short int
  29548.  
  29549.  printf ("%Fln");                  Store char count in far int
  29550.  
  29551.  The specifications "%hs" and "%ls" are meaningless to printf. The
  29552.  specifications "%Np" and "%Fp" are aliases for "%hp" and "%lp" for the sake
  29553.  of compatibility with Microsoft C version 4.0.
  29554.  
  29555.  
  29556.  Return Value
  29557.  
  29558.  The printf function returns the number of characters printed, or a negative
  29559.  value in the case of an error.
  29560.  
  29561.  
  29562.  Compatibility
  29563.  
  29564.   ANSI   DOS   OS/2   UNIX   XENIX
  29565.  
  29566.  
  29567.  
  29568.  See Also
  29569.  
  29570.  fprintf, scanf, sprintf, vfprintf, vprintf, vsprintf
  29571.  
  29572.  
  29573.  Example
  29574.  
  29575.    /* PRINTF.C illustrates output formatting with printf. */
  29576.  
  29577.    #include <stdio.h>
  29578.  
  29579.    void main()
  29580.    {
  29581.       char   ch = 'h', *string = "computer";
  29582.       int    count = -9234;
  29583.       double fp = 251.7366;
  29584.  
  29585.       /* Display integers. */
  29586.       printf( "Integer formats:\n"
  29587.               "\tDecimal: %d  Justified: %.6d  Unsigned: %u\n",
  29588.                count, count, count, count );
  29589.  
  29590.       printf( "Decimal %d as:\n\tHex: %Xh  C hex: 0x%x  Octal: %o\n",
  29591.                count, count, count, count );
  29592.  
  29593.       /* Display in different radixes. */
  29594.       printf( "Digits 10 equal:\n\tHex: %i  Octal: %i  Decimal: %i\n",
  29595.                0x10, 010, 10 );
  29596.  
  29597.       /* Display characters. */
  29598.       printf( "Characters in field:\n%10c   %5c\n", ch, ch );
  29599.  
  29600.       /* Display strings. */
  29601.       printf( "Strings in field:\n%25s\n%25.4s\n", string, string );
  29602.  
  29603.       /* Display real numbers. */
  29604.       printf( "Real numbers:\n\t%f    %.2f    %e    %E\n", fp, fp, fp, fp );
  29605.  
  29606.       /* Display pointers. */
  29607.       printf( "Address as:\n\tDefault: %p  Near: %Np  Far: %Fp\n",
  29608.                &count, (int _near *)&count, (int _far *)&count );
  29609.  
  29610.       /* Count characters printed. */
  29611.       printf( "Display to here:\n" );
  29612.       printf( "1234567890123456%n78901234567890\n", &count );
  29613.       printf( "\tNumber displayed: %d\n\n", count );
  29614.  
  29615.    }
  29616.  
  29617.  
  29618.  Output
  29619.  
  29620.  
  29621.  
  29622.    Integer formats:
  29623.            Decimal: -9234  Justified: -009234  Unsigned: 56302
  29624.    Decimal -9234 as:
  29625.            Hex: DBEEh  C hex: 0xdbee  Octal: 155756
  29626.    Digits 10 equal:
  29627.            Hex: 16  Octal: 8  Decimal: 10
  29628.    Characters in field:
  29629.             h       h
  29630.    Strings in field:
  29631.                     computer
  29632.                         comp
  29633.    Real numbers:
  29634.            251.736600    251.74    2.517366e+002    2.517366E+002
  29635.    Address as:
  29636.            Default: 141C  Near: 141C  Far: 0087:141C
  29637.    Display to here:
  29638.    123456789012345678901234567890
  29639.            Number displayed: 16
  29640.  
  29641.  
  29642.  
  29643.  
  29644.  
  29645.  putc, putchar
  29646.  ────────────────────────────────────────────────────────────────────────────
  29647.  
  29648.  
  29649.  Description
  29650.  
  29651.  Writes a character to a stream (putc) or to stdout (putchar).
  29652.  
  29653.    #include <stdio.h>
  29654.  
  29655.    int putc( int c, FILE *stream );
  29656.  
  29657.    int putchar( int c );
  29658.  
  29659.  c                                 Character to be written
  29660.  
  29661.  stream                            Pointer to FILE structure
  29662.  
  29663.  
  29664.  Remarks
  29665.  
  29666.  The putc routine writes the single character c to the output stream at the
  29667.  current position. The putchar routine is identical to putc(c, stdout).
  29668.  
  29669.  These routines are implemented as both macros and functions. See Section
  29670.  1.4, "Choosing Between Functions and Macros," for a discussion of how to
  29671.  select between the macro and function forms.
  29672.  
  29673.  
  29674.  Return Value
  29675.  
  29676.  The putc and putchar routines return the character written, or EOF in the
  29677.  case of an error. Any integer can be passed to putc, but only the lower 8
  29678.  bits are written.
  29679.  
  29680.  
  29681.  Compatibility
  29682.  
  29683.   ANSI   DOS   OS/2   UNIX   XENIX
  29684.  
  29685.  
  29686.  
  29687.  See Also
  29688.  
  29689.  fputc, fputchar, getc, getchar
  29690.  
  29691.  
  29692.  Example
  29693.  
  29694.    /* PUTC.C: This program uses putc to write buffer to a stream.
  29695.     * If an error occurs, the program will stop before writing the
  29696.     * entire buffer.
  29697.     */
  29698.  
  29699.    #include <stdio.h>
  29700.  
  29701.    void main()
  29702.    {
  29703.       FILE *stream;
  29704.       char *p, buffer[] = "This is the line of output\n";
  29705.       int  ch;
  29706.  
  29707.    /* Make standard out the stream and write to it. */
  29708.       stream = stdout;
  29709.       for( p = buffer; (ch != EOF) && (*p != '\0'); p++ )
  29710.          ch = putc( *p, stream );
  29711.    }
  29712.  
  29713.  
  29714.  Output
  29715.  
  29716.  
  29717.  
  29718.    This is the line of output
  29719.  
  29720.  
  29721.  
  29722.  
  29723.  
  29724.  putch
  29725.  ────────────────────────────────────────────────────────────────────────────
  29726.  
  29727.  
  29728.  Description
  29729.  
  29730.  Writes a character to the console.
  29731.  
  29732.  #include <conio.h>                Required only for function declarations
  29733.  
  29734.    int putch( int c );
  29735.  
  29736.  c                                 Character to be output
  29737.  
  29738.  
  29739.  Remarks
  29740.  
  29741.  The putch function writes the character c directly (without buffering) to
  29742.  the console.
  29743.  
  29744.  
  29745.  Return Value
  29746.  
  29747.  The function returns c if successful, and EOF if not.
  29748.  
  29749.  
  29750.  Compatibility
  29751.  
  29752.   ANSI   DOS   OS/2   UNIX   XENIX
  29753.  
  29754.  
  29755.  
  29756.  See Also
  29757.  
  29758.  cprintf, getch, getche
  29759.  
  29760.  
  29761.  Example
  29762.  
  29763.    /* GETCH.C: This program reads characters from the keyboard until it
  29764.     * receives a 'Y' or 'y'.
  29765.     */
  29766.  
  29767.    #include <conio.h>
  29768.    #include <ctype.h>
  29769.  
  29770.    void main()
  29771.    {
  29772.       int ch;
  29773.  
  29774.       cputs( "Type 'Y' when finished typing keys: " );
  29775.       do
  29776.       {
  29777.          ch = getch();
  29778.          ch = toupper( ch );
  29779.       } while( ch != 'Y' );
  29780.  
  29781.       putch( ch );
  29782.       putch( '\r' );    /* Carriage return */
  29783.       putch( '\n' );    /* Line feed       */
  29784.    }
  29785.  
  29786.  
  29787.  Output
  29788.  
  29789.  
  29790.  
  29791.    Type 'Y' when finished typing keys: Y
  29792.  
  29793.  
  29794.  
  29795.  
  29796.  
  29797.  putenv
  29798.  ────────────────────────────────────────────────────────────────────────────
  29799.  
  29800.  
  29801.  Description
  29802.  
  29803.  Creates new environment variables.
  29804.  
  29805.  #include <stdlib.h>               Required only for function declarations
  29806.  
  29807.    int putenv( char *envstring );
  29808.  
  29809.  envstring                         Environment-string definition
  29810.  
  29811.  
  29812.  Remarks
  29813.  
  29814.  The putenv function adds new environment variables or modifies the values of
  29815.  existing environment variables. Environment variables define the environment
  29816.  in which a process executes (for example, the default search path for
  29817.  libraries to be linked with a program).
  29818.  
  29819.  The envstring argument must be a pointer to a string with the form
  29820.  
  29821.  varname=string
  29822.  
  29823.  where varname is the name of the environment variable to be added or
  29824.  modified and string is the variable's value. If varname is already part of
  29825.  the environment, its value is replaced by string; otherwise, the new varname
  29826.  variable and its string value are added to the environment. A variable can
  29827.  be set to an empty value by specifying an empty string.
  29828.  
  29829.  This function affects only the environment that is local to the currently
  29830.  running process; it cannot be used to modify the command-level environment.
  29831.  When the currently running process terminates, the environment reverts to
  29832.  the level of the parent process (in most cases, the operating system level).
  29833.  However, the environment affected by putenv can be passed to any child
  29834.  processes created by spawn, exec, system, or (in OS/2 only) _popen, and
  29835.  these child processes get any new items added by putenv.
  29836.  
  29837.  Never free a pointer to an environment entry, because the environment
  29838.  variable will then point to freed space. A similar problem can occur if you
  29839.  pass putenv a pointer to a local variable, then exit the function in which
  29840.  the variable is declared.
  29841.  
  29842.  The putenv function operates only on data structures accessible to the
  29843.  run-time library and not on the environment "segment" created for a process
  29844.  by DOS or OS/2.
  29845.  
  29846.  Note that environment-table entries must not be changed directly. If an
  29847.  entry must be changed, use putenv. To modify the returned value without
  29848.  affecting the environment table, use strdup or strcpy to make a copy of the
  29849.  string.
  29850.  
  29851.  The getenv and putenv functions use the global variable environ to access
  29852.  the environment table. The putenv function may change the value of environ,
  29853.  thus invalidating the envp argument to the main function. Therefore, it is
  29854.  safer to use the environ variable to access the environment information.
  29855.  
  29856.  
  29857.  Return Value
  29858.  
  29859.  The putenv function returns 0 if it is successful. A return value of -1
  29860.  indicates an error.
  29861.  
  29862.  
  29863.  Compatibility
  29864.  
  29865.   ANSI   DOS   OS/2   UNIX   XENIX
  29866.  
  29867.  
  29868.  
  29869.  See Also
  29870.  
  29871.  getenv
  29872.  
  29873.  
  29874.  Example
  29875.  
  29876.    /* GETENV.C: This program uses getenv to retrieve the LIB environment
  29877.     * variable and then uses putenv to change it to a new value.
  29878.     */
  29879.  
  29880.    #include <stdlib.h>
  29881.    #include <stdio.h>
  29882.  
  29883.    main()
  29884.    {
  29885.       char *libvar;
  29886.  
  29887.       /* Get the value of the LIB environment variable. */
  29888.       libvar = getenv( "LIB" );
  29889.       if( libvar != NULL )
  29890.          printf( "Original LIB variable is: %s\n", libvar );
  29891.  
  29892.       /* Attempt to change path. Note that this only affects the environment
  29893.        * variable of the current process. The command processor's environment
  29894.        * is not changed.
  29895.        */
  29896.       putenv( "LIB=c:\\mylib;c:\\yourlib" );
  29897.  
  29898.       /* Get new value. */
  29899.       libvar = getenv( "LIB" );
  29900.       if( libvar != NULL )
  29901.          printf( "New LIB variable is: %s\n", libvar );
  29902.  
  29903.    }
  29904.  
  29905.  
  29906.  Output
  29907.  
  29908.  
  29909.  
  29910.    Original LIB variable is: C:\LIB
  29911.    New LIB variable is: c:\mylib;c:\yourlib
  29912.  
  29913.  
  29914.  
  29915.  
  29916.  
  29917.  _putimage Functions
  29918.  ────────────────────────────────────────────────────────────────────────────
  29919.  
  29920.  
  29921.  Description
  29922.  
  29923.  Retrieve images from a buffer.
  29924.  
  29925.    #include <graph.h>
  29926.  
  29927.    void _far _putimage( short x, short y, char _huge *image, short action );
  29928.  
  29929.    void _far _putimage_w( double wx, double wy, char _huge *image, short
  29930.    action );
  29931.  
  29932.  x, y                              Position of upper-left corner of image
  29933.  
  29934.  image                             Stored image buffer
  29935.  
  29936.  action                            Interaction with existing screen image
  29937.  
  29938.  wx, wy                            Position of upper-left corner of image
  29939.  
  29940.  
  29941.  Remarks
  29942.  
  29943.  The _putimage function transfers to the screen the image stored in the
  29944.  buffer that image points to.
  29945.  
  29946.  In the _putimage function, the upper-left corner of the image is placed at
  29947.  the view coordinate point (x, y). In the _putimage_w function, the
  29948.  upper-left corner of the image is placed at the window coordinate point (wx,
  29949.  wy).
  29950.  
  29951.  The action argument defines the interaction between the stored image and the
  29952.  one that is already on the screen. It may be any one of the following
  29953.  manifest constants (defined in GRAPH.H):
  29954.  
  29955.  Constant                          Meaning
  29956.  ────────────────────────────────────────────────────────────────────────────
  29957.  _GAND                             Transfers the image over an existing
  29958.                                    image on the screen. The resulting image
  29959.                                    is the logical-AND product of the two
  29960.                                    images: points that had the same color
  29961.                                    in both the existing image and the new
  29962.                                    one will remain the same color, while
  29963.                                    points that have different colors are
  29964.                                    joined by logical-AND.
  29965.  
  29966.  _GOR                              Superimposes the image onto an existing
  29967.                                    image. The new image does not erase the
  29968.                                    previous screen contents.
  29969.  
  29970.  _GPRESET                          Transfers the data point-by-point onto
  29971.                                    the screen. Each point has the inverse
  29972.                                    of the color attribute it had when it
  29973.                                    was taken from the screen by _getimage,
  29974.                                    producing a negative image.
  29975.  
  29976.  _GPSET                            Transfers the data point-by-point onto
  29977.                                    the screen. Each point has the exact
  29978.                                    color attribute it had when it was taken
  29979.                                    from the screen by _getimage.
  29980.  
  29981.  _GXOR                             Causes the points on the screen to be
  29982.                                    inverted where a point exists in the
  29983.                                    image buffer. This behavior is exactly
  29984.                                    like that of the cursor: when an image
  29985.                                    is put against a complex background
  29986.                                    twice, the background is restored
  29987.                                    unchanged. This allows you to move an
  29988.                                    object around without erasing the
  29989.                                    background. The _GXOR constant is a
  29990.                                    special mode often used for animation.
  29991.  
  29992.  
  29993.  Return Value
  29994.  
  29995.  None.
  29996.  
  29997.  
  29998.  Compatibility
  29999.  
  30000.   ANSI   DOS   OS/2   UNIX   XENIX
  30001.  
  30002.  
  30003.  
  30004.  See Also
  30005.  
  30006.  _getimage,  _imagesize
  30007.  
  30008.  
  30009.  Example
  30010.  
  30011.  See the example for _getimage.
  30012.  
  30013.  
  30014.  
  30015.  
  30016.  
  30017.  puts
  30018.  ────────────────────────────────────────────────────────────────────────────
  30019.  
  30020.  
  30021.  Description
  30022.  
  30023.  Writes a string to stdout.
  30024.  
  30025.  #include <stdio.h>
  30026.  
  30027.    int puts( const char *string );
  30028.  
  30029.  string                            String to be output
  30030.  
  30031.  
  30032.  Remarks
  30033.  
  30034.  The puts function writes string to the standard output stream stdout,
  30035.  replacing the string's terminating null character ('\0') with a newline
  30036.  character (\n) in the output stream.
  30037.  
  30038.  
  30039.  Return Value
  30040.  
  30041.  The puts function returns a nonnegative value if it is successful. If the
  30042.  function fails, it returns EOF.
  30043.  
  30044.  
  30045.  Compatibility
  30046.  
  30047.   ANSI   DOS   OS/2   UNIX   XENIX
  30048.  
  30049.  
  30050.  
  30051.  See Also
  30052.  
  30053.  fputs, gets
  30054.  
  30055.  
  30056.  Example
  30057.  
  30058.    /* PUTS.C: This program uses puts to write a string to stdout. */
  30059.  
  30060.    #include <stdio.h>
  30061.  
  30062.    void main()
  30063.    {
  30064.       puts( "Hello world from puts!" );
  30065.    }
  30066.  
  30067.  
  30068.  Output
  30069.  
  30070.  
  30071.  
  30072.    Hello world from puts!
  30073.  
  30074.  
  30075.  
  30076.  
  30077.  
  30078.  putw
  30079.  ────────────────────────────────────────────────────────────────────────────
  30080.  
  30081.  
  30082.  Description
  30083.  
  30084.  Writes an integer to a stream.
  30085.  
  30086.    #include <stdio.h>
  30087.  
  30088.    int putw( int binint, FILE *stream );
  30089.  
  30090.  binint                            Binary integer to be output
  30091.  
  30092.  stream                            Pointer to FILE structure
  30093.  
  30094.  
  30095.  Remarks
  30096.  
  30097.  The putw function writes a binary value of type int to the current position
  30098.  of stream. The putw function does not affect the alignment of items in the
  30099.  stream, nor does it assume any special alignment.
  30100.  
  30101.  The putw function is provided primarily for compatibility with previous
  30102.  libraries. Note that portability problems may occur with putw, since the
  30103.  size of an int and ordering of bytes within an int differ across systems.
  30104.  
  30105.  
  30106.  Return Value
  30107.  
  30108.  The putw function returns the value written. A return value of EOF may
  30109.  indicate an error. Since EOF is also a legitimate integer value, ferror
  30110.  should be used to verify an error.
  30111.  
  30112.  
  30113.  Compatibility
  30114.  
  30115.   ANSI   DOS   OS/2   UNIX   XENIX
  30116.  
  30117.  
  30118.  
  30119.  See Also
  30120.  
  30121.  getw
  30122.  
  30123.  
  30124.  Example
  30125.  
  30126.    /* PUTW.C: This program uses putw to write a word to a stream,
  30127.     * then performs an error check.
  30128.     */
  30129.  
  30130.    #include <stdio.h>
  30131.    #include <stdlib.h>
  30132.  
  30133.    void main()
  30134.    {
  30135.       FILE *stream;
  30136.       unsigned u;
  30137.  
  30138.       if( (stream = fopen( "data.out", "wb" )) == NULL )
  30139.          exit( 1 );
  30140.       for( u = 0; u < 10; u++ )
  30141.       {
  30142.          putw( u + 0x2132, stdout );
  30143.          putw( u + 0x2132, stream );   /* Write word to stream. */
  30144.          if( ferror( stream ) )        /* Make error check. */
  30145.          {
  30146.             printf( "putw failed" );
  30147.             clearerr( stream );
  30148.             exit( 1 );
  30149.          }
  30150.       }
  30151.       printf( "\nWrote ten words\n" );
  30152.       fclose( stream );
  30153.    }
  30154.  
  30155.  
  30156.  Output
  30157.  
  30158.  
  30159.  
  30160.    2!3!4!5!6!7!8!9!:!;!
  30161.    Wrote ten words
  30162.  
  30163.  
  30164.  
  30165.  
  30166.  
  30167.  qsort
  30168.  ────────────────────────────────────────────────────────────────────────────
  30169.  
  30170.  
  30171.  Description
  30172.  
  30173.  Performs a quick sort.
  30174.  
  30175.  #include <stdlib.h>               For ANSI compatibility
  30176.  
  30177.  #include <search.h>               Required only for function declarations
  30178.  
  30179.    void qsort( void *base, size_t num, size_t width,
  30180.    int( *compare ) ( const void *elem1, const void *elem2 ) );
  30181.  
  30182.  base                              Start of target array
  30183.  
  30184.  num                               Array size in elements
  30185.  
  30186.  width                             Element size in bytes
  30187.  
  30188.  compare                           Comparison function
  30189.  
  30190.  elem1                             Pointer to the key for the search
  30191.  
  30192.  elem2                             Pointer to the array element to be
  30193.                                    compared with the key
  30194.  
  30195.  
  30196.  Remarks
  30197.  
  30198.  The qsort function implements a quick-sort algorithm to sort an array of num
  30199.  elements, each of width bytes. The argument base is a pointer to the base of
  30200.  the array to be sorted. The qsort function overwrites this array with the
  30201.  sorted elements.
  30202.  
  30203.  The argument compare is a pointer to a user-supplied routine that compares
  30204.  two array elements and returns a value specifying their relationship. The
  30205.  qsort function calls the compare routine one or more times during the sort,
  30206.  passing pointers to two array elements on each call:
  30207.  
  30208.  compare( (void *) elem1, (void *) elem2 );
  30209.  
  30210.  The routine must compare the elements, then return one of the following
  30211.  values:
  30212.  
  30213.  Value                             Meaning
  30214.  ────────────────────────────────────────────────────────────────────────────
  30215.  < 0                               elem1 less than elem2
  30216.  
  30217.  = 0                               elem1 equivalent to elem2
  30218.  
  30219.  > 0                               elem1 greater than elem2
  30220.  
  30221.  The array is sorted in increasing order, as defined by the comparison
  30222.  function. To sort an array in decreasing order, reverse the sense of
  30223.  "greater than" and "less than" in the comparison function.
  30224.  
  30225.  
  30226.  Return Value
  30227.  
  30228.  None.
  30229.  
  30230.  
  30231.  Compatibility
  30232.  
  30233.   ANSI   DOS   OS/2   UNIX   XENIX
  30234.  
  30235.  
  30236.  
  30237.  See Also
  30238.  
  30239.  bsearch, lsearch
  30240.  
  30241.  
  30242.  Example
  30243.  
  30244.    /* QSORT.C: This program reads the command-line parameters and
  30245.     * uses qsort to sort them. It then displays the sorted arguments.
  30246.     */
  30247.  
  30248.    #include <search.h>
  30249.    #include <string.h>
  30250.    #include <stdio.h>
  30251.  
  30252.    int compare( char **arg1, char **arg2 );  /* Prototype */
  30253.  
  30254.    void main( int argc, char **argv )
  30255.    {
  30256.       int i;
  30257.  
  30258.       /* Eliminate argv[0] from sort: */
  30259.       argv++;
  30260.       argc─;
  30261.  
  30262.       /* Sort remaining args using Quicksort algorithm: */
  30263.       qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );
  30264.  
  30265.       /* Output sorted list: */
  30266.       for( i = 0; i < argc; ++i )
  30267.          printf( "%s ", argv[i] );
  30268.       printf( "\n" );
  30269.    }
  30270.  
  30271.    int compare( char **arg1, char **arg2 )
  30272.    {
  30273.       /* Compare all of both strings: */
  30274.       return strcmpi( *arg1, *arg2 );
  30275.    }
  30276.  
  30277.  
  30278.  Output
  30279.  
  30280.  
  30281.  
  30282.    [C:\LIBREF] qsort every good boy deserves favor
  30283.    boy deserves every favor good
  30284.  
  30285.  
  30286.  
  30287.  
  30288.  
  30289.  raise
  30290.  ────────────────────────────────────────────────────────────────────────────
  30291.  
  30292.  
  30293.  Description
  30294.  
  30295.  Sends a signal to the executing program.
  30296.  
  30297.    #include <signal.h>
  30298.  
  30299.    int raise( int sig );
  30300.  
  30301.  sig                               Signal to be raised
  30302.  
  30303.  
  30304.  Remarks
  30305.  
  30306.  The raise function sends sig to the executing program. If a signal-handling
  30307.  routine for sig has ben installed by a prior call to signal, raise causes
  30308.  that routine to be executed. If no handler routine has been installed, the
  30309.  default action (as listed below) is taken.
  30310.  
  30311.  The signal value sig can be one of the following manifest constants:
  30312.  
  30313. ╓┌──────────────────────┌──────────────────────────┌─────────────────────────
  30314.  Signal                 Meaning                    Default
  30315.  ─────────────────────────────────────────────────────────────────────────────
  30316.  SIGABRT                Abnormal termination.      Terminates the calling
  30317.                                                    program with exit code 3.
  30318.  
  30319.  SIGBREAK               CTRL+ BREAK interrupt.     Terminates the calling
  30320.                                                    program with exit code 3.
  30321.  
  30322.  SIGFPE                 Floating-point error.      Terminates the calling
  30323.                                                    program.
  30324.  
  30325.  SIGILL                 Illegal instruction. This  Terminates the calling
  30326.  Signal                 Meaning                    Default
  30327.  ─────────────────────────────────────────────────────────────────────────────
  30328. SIGILL                 Illegal instruction. This  Terminates the calling
  30329.                         signal is not generated    program.
  30330.                         by DOS or OS/2, but is
  30331.                         supported for ANSI
  30332.                         compatibility.
  30333.  
  30334.  SIGINT                 CTRL+ C interrupt.         Issues INT23H.
  30335.  
  30336.  SIGSEGV                Illegal storage access.    Terminates the calling
  30337.                         This signal is not         program.
  30338.                         generated by DOS or OS/2,
  30339.                         but is supported for ANSI
  30340.                         compatiblity.
  30341.  
  30342.  SIGTERM                Termination request sent   Ignores the signal.
  30343.                         to the program. This
  30344.                         signal is not generated
  30345.                         by DOS or OS/2, but is
  30346.                         supported for ANSI
  30347.  Signal                 Meaning                    Default
  30348.  ─────────────────────────────────────────────────────────────────────────────
  30349.                        supported for ANSI
  30350.                         compatibility.
  30351.  
  30352.  SIGUSR1                User-defined signals.      Ignores the signal.
  30353.  SIGUSR2
  30354.  SIGUSR3
  30355.  
  30356.  
  30357.  
  30358.  
  30359.  Return Value
  30360.  
  30361.  If successful, the raise function returns 0. Otherwise, it returns a nonzero
  30362.  value.
  30363.  
  30364.  
  30365.  Compatibility
  30366.  
  30367.   ANSI   DOS   OS/2   UNIX   XENIX
  30368.  
  30369.  
  30370.  
  30371.  See Also
  30372.  
  30373.  abort, signal
  30374.  
  30375.  
  30376.  Example
  30377.  
  30378.  See the example for signal.
  30379.  
  30380.  
  30381.  
  30382.  
  30383.  
  30384.  rand
  30385.  ────────────────────────────────────────────────────────────────────────────
  30386.  
  30387.  
  30388.  Description
  30389.  
  30390.  Generates a pseudorandom number.
  30391.  
  30392.  #include <stdlib.h>               Required only for function declarations
  30393.  
  30394.    int rand( void );
  30395.  
  30396.  
  30397.  Remarks
  30398.  
  30399.  The rand function returns a pseudorandom integer in the range 0 to RAND_MAX.
  30400.  The srand routine can be used to seed the pseudorandom-number generator
  30401.  before calling rand.
  30402.  
  30403.  
  30404.  Return Value
  30405.  
  30406.  The rand function returns a pseudorandom number, as described above. There
  30407.  is no error return.
  30408.  
  30409.  
  30410.  Compatibility
  30411.  
  30412.   ANSI   DOS   OS/2   UNIX   XENIX
  30413.  
  30414.  
  30415.  
  30416.  See Also
  30417.  
  30418.  srand
  30419.  
  30420.  
  30421.  Example
  30422.  
  30423.    /* RAND.C: This program seeds the random-number generator with the
  30424.     * time, then displays 20 random integers.
  30425.     */
  30426.  
  30427.    #include <stdlib.h>
  30428.    #include <stdio.h>
  30429.    #include <time.h>
  30430.  
  30431.    void main()
  30432.    {
  30433.       int i;
  30434.  
  30435.       /* Seed the random-number generator with current time so that
  30436.        * the numbers will be different every time we run.
  30437.        */
  30438.       srand( (unsigned)time( NULL ) );
  30439.  
  30440.       /* Display 10 numbers. */
  30441.       for( i = 0; i < 10; i++ )
  30442.           printf( "  %6d\n", rand() );
  30443.    }
  30444.  
  30445.  
  30446.  Output
  30447.  
  30448.  
  30449.  
  30450.       19471
  30451.       16395
  30452.        8268
  30453.       15582
  30454.        6489
  30455.       28356
  30456.       27042
  30457.        5276
  30458.       23070
  30459.       10930
  30460.  
  30461.  
  30462.  
  30463.  
  30464.  
  30465.  read
  30466.  ────────────────────────────────────────────────────────────────────────────
  30467.  
  30468.  
  30469.  Description
  30470.  
  30471.  Reads data from a file.
  30472.  
  30473.  #include <io.h>                   Required only for function declarations
  30474.  
  30475.    int read( int handle, void *buffer, unsigned int count );
  30476.  
  30477.  handle                            Handle referring to open file
  30478.  
  30479.  buffer                            Storage location for data
  30480.  
  30481.  count                             Maximum number of bytes
  30482.  
  30483.  
  30484.  Remarks
  30485.  
  30486.  The read function attempts to read count bytes into buffer from the file
  30487.  associated with handle. The read operation begins at the current position of
  30488.  the file pointer associated with the given file. After the read operation,
  30489.  the file pointer points to the next unread character.
  30490.  
  30491.  
  30492.  Return Value
  30493.  
  30494.  The read function returns the number of bytes actually read, which may be
  30495.  less than count if there are fewer than count bytes left in the file, or if
  30496.  the file was opened in text mode (see below). The return value 0 indicates
  30497.  an attempt to read at end-of-file. The return value -1 indicates an error,
  30498.  and errno is set to the following value:
  30499.  
  30500.  Value                             Meaning
  30501.  ────────────────────────────────────────────────────────────────────────────
  30502.  EBADF                             The given handle is invalid; or the file
  30503.                                    is not open for reading; or (DOS
  30504.                                    versions 3.0 and later and OS/2 only)
  30505.                                    the file is locked.
  30506.  
  30507.  If you are reading more than 32K (the maximum size for type int) from a
  30508.  file, the return value should be of type unsigned int (see the example that
  30509.  follows). However, the maximum number of bytes that can be read from a file
  30510.  in one operation is 65,534, since 65,535 (or 0xFFFF) is indistinguishable
  30511.  from -1, and therefore cannot be distinguished from an error return.
  30512.  
  30513.  If the file was opened in text mode, the return value may not correspond to
  30514.  the number of bytes actually read. When text mode is in effect, each
  30515.  carriage-return-line-feed (CR-LF) pair is replaced with a single line-feed
  30516.  character. Only the single line-feed character is counted in the return
  30517.  value. The replacement does not affect the file pointer.
  30518.  
  30519.  Note that under DOS and OS/2, when files are opened in text mode, a CTRL+Z
  30520.  character is treated as an end-of-file indicator. When the CTRL+Z is
  30521.  encountered, the read terminates, and the next read returns 0 bytes. The
  30522.  lseek function will clear the end-of-file indicator.
  30523.  
  30524.  
  30525.  Compatibility
  30526.  
  30527.   ANSI   DOS   OS/2   UNIX   XENIX
  30528.  
  30529.  
  30530.  
  30531.  See Also
  30532.  
  30533.  creat, fread, open, write
  30534.  
  30535.  
  30536.  Example
  30537.  
  30538.    /* READ.C: This program opens a file named READ.C and tries to read 60,000
  30539.     * bytes from that file using read. It then displays the actual
  30540.     * number of bytes read from READ.C.
  30541.     */
  30542.  
  30543.    #include <fcntl.h>      /* Needed only for O_RDWR definition */
  30544.    #include <io.h>
  30545.    #include <stdlib.h>
  30546.    #include <stdio.h>
  30547.  
  30548.    char buffer[60000];
  30549.  
  30550.    void main()
  30551.    {
  30552.       int fh;
  30553.       unsigned int nbytes = 60000, bytesread;
  30554.  
  30555.       /* Open file for input: */
  30556.       if( (fh = open( "read.c", O_RDONLY )) == -1 )
  30557.       {
  30558.          perror( "open failed on input file" );
  30559.          exit( 1 );
  30560.       }
  30561.  
  30562.       /* Read in input: */
  30563.       if( ( bytesread = read( fh, buffer, nbytes ) ) <= 0 )
  30564.          perror( "Problem reading file" );
  30565.       else
  30566.          printf( "Read %u bytes from file\n", bytesread );
  30567.  
  30568.       close( fh );
  30569.    }
  30570.  
  30571.  
  30572.  Output
  30573.  
  30574.  
  30575.  
  30576.    Read 747 bytes from file
  30577.  
  30578.  
  30579.  
  30580.  
  30581.  
  30582.  realloc Functions
  30583.  ────────────────────────────────────────────────────────────────────────────
  30584.  
  30585.  
  30586.  Description
  30587.  
  30588.  
  30589.  #include <stdlib.h>
  30590.  
  30591.                                    For ANSI compatibility (realloc only)
  30592.  
  30593.  #include <malloc.h>               Required only for function declarations
  30594.  
  30595.    void *realloc( void *memblock, size_t size );
  30596.  
  30597.    void _based( void ) *_brealloc( _segment seg, void _based( void )
  30598.    *memblock,
  30599.    size_t size );
  30600.  
  30601.    void _far *_frealloc( void _far *memblock, size_t size );
  30602.  
  30603.    void _near *_nrealloc( void _near *memblock, size_t size );
  30604.  
  30605.  memblock                          Pointer to previously allocated memory
  30606.                                    block
  30607.  
  30608.  size                              New size in bytes
  30609.  
  30610.  seg                               Segment selector
  30611.  
  30612.  
  30613.  Remarks
  30614.  
  30615.  The realloc family of functions changes the size of a previously allocated
  30616.  memory block. The memblock argument points to the beginning of the memory
  30617.  block. If memblock is NULL, realloc functions in the same way as malloc and
  30618.  allocates a new block of size bytes. If memblock is not NULL, it should be a
  30619.  pointer returned by calloc, malloc, or a prior call to realloc.
  30620.  
  30621.  The size argument gives the new size of the block, in bytes. The contents of
  30622.  the block are unchanged up to the shorter of the new and old sizes, although
  30623.  the new block may be in a different location.
  30624.  
  30625.  The memblock argument can also point to a block that has been freed, as long
  30626.  as there has been no intervening call to the corresponding calloc, malloc,
  30627.  _expand, or realloc function. If successful, the reallocated block is marked
  30628.  in use.
  30629.  
  30630.  In large data models (that is, compact-, large-, and huge-model programs),
  30631.  realloc maps to _frealloc. In small data models (tiny-, small-, and
  30632.  medium-model programs), realloc maps to _nrealloc.
  30633.  
  30634.  The various realloc functions reallocate memory in the heap specified in the
  30635.  following list:
  30636.  
  30637.  Function                          Heap
  30638.  ────────────────────────────────────────────────────────────────────────────
  30639.  realloc                           Depends on data model of program
  30640.  
  30641.  _brealloc                         Based heap specified by seg value
  30642.  
  30643.  _frealloc                         Far heap (outside default data segment)
  30644.  
  30645.  _nrealloc                         Near heap (inside default data segment)
  30646.  
  30647.  
  30648.  Return Value
  30649.  
  30650.  The realloc functions return a void pointer to the reallocated (and possibly
  30651.  moved) memory block.
  30652.  
  30653.  The return value is NULL if the size is zero and the buffer argument is not
  30654.  NULL, or if there is not enough available memory to expand the block to the
  30655.  given size. In the first case, the original block is freed. In the second,
  30656.  the original block is unchanged.
  30657.  
  30658.  The storage space pointed to by the return value is guaranteed to be
  30659.  suitably aligned for storage of any type of object. To get a pointer to a
  30660.  type other than void, use a type cast on the return value.
  30661.  
  30662.  
  30663.  Compatibility
  30664.  
  30665.  realloc
  30666.  
  30667.   ANSI   DOS   OS/2   UNIX   XENIX
  30668.  
  30669.  
  30670.  _brealloc,  _frealloc,  _nrealloc
  30671.  
  30672.   ANSI   DOS   OS/2   UNIX   XENIX
  30673.  
  30674.  
  30675.  
  30676.  See Also
  30677.  
  30678.  calloc functions, free functions, malloc functions
  30679.  
  30680.  
  30681.  Example
  30682.  
  30683.    /* REALLOC.C: This program allocates a block of memory for buffer
  30684.     * and then uses _msize to display the size of that block. Next, it
  30685.     * uses realloc to expand the amount of memory used by buffer
  30686.     * and then calls _msize again to display the new amount of
  30687.     * memory allocated to buffer.
  30688.     */
  30689.  
  30690.    #include <stdio.h>
  30691.    #include <malloc.h>
  30692.    #include <stdlib.h>
  30693.  
  30694.    void main()
  30695.    {
  30696.       long *buffer;
  30697.       size_t size;
  30698.  
  30699.       if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
  30700.          exit( 1 );
  30701.  
  30702.       size = _msize( buffer );
  30703.       printf( "Size of block after malloc of 1000 longs: %u\n", size );
  30704.  
  30705.       /* Reallocate and show new size: */
  30706.       if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) ==
  30707.  NULL )
  30708.          exit( 1 );
  30709.       size = _msize( buffer );
  30710.       printf( "Size of block after realloc of 1000 more longs: %u\n", size );
  30711.  
  30712.       free( buffer );
  30713.    }
  30714.  
  30715.  
  30716.  Output
  30717.  
  30718.  
  30719.  
  30720.    Size of block after malloc of 1000 longs: 4000
  30721.    Size of block after realloc of 1000 more longs: 8000
  30722.  
  30723.  
  30724.  
  30725.  
  30726.  
  30727.  _rectangle Functions
  30728.  ────────────────────────────────────────────────────────────────────────────
  30729.  
  30730.  
  30731.  Description
  30732.  
  30733.  Draw rectangles.
  30734.  
  30735.    #include <graph.h>
  30736.  
  30737.    short _far _rectangle( short control, short x1, short y1, short x2, short
  30738.    y2 );
  30739.  
  30740.    short _far _rectangle_w( short control, double wx1, double wy1, double
  30741.    wx2,
  30742.    double wy2 );
  30743.  
  30744.    short _far _rectangle_wxy( short control, struct _wxycoord _far *pwxy1,
  30745.    struct _wxycoord _far *pwxy2 );
  30746.  
  30747.  control                           Fill flag
  30748.  
  30749.  x1, y1                            Upper-left corner
  30750.  
  30751.  x2, y2                            Lower-right corner
  30752.  
  30753.  wx1, wy1                          Upper-left corner
  30754.  
  30755.  wx2, wy2                          Lower-right corner
  30756.  
  30757.  pwxy1                             Upper-left corner
  30758.  
  30759.  pwxy2                             Lower-right corner
  30760.  
  30761.  
  30762.  Remarks
  30763.  
  30764.  The _rectangle functions draw a rectangle with the current line style.
  30765.  
  30766.  The _rectangle function uses the view coordinate system. The view coordinate
  30767.  points  (x1, y1) and (x2, y2) are the diagonally opposed corners of the
  30768.  rectangle.
  30769.  
  30770.  The _rectangle_w function uses the window coordinate system. The window
  30771.  coordinate points (wx1, wy1) and (wx2, wy2) are the diagonally opposed
  30772.  corners of the rectangle.
  30773.  
  30774.  The _rectangle_wxy function uses the window coordinate system. The window
  30775.  coordinate points (pwxy1) and (pwxy2) are the diagonally opposed corners of
  30776.  the rectangle. The coordinates for the _rectangle_wxy routine are given in
  30777.  terms of an _wxycoord structure (defined in GRAPH.H), which contains the
  30778.  following elements:
  30779.  
  30780.  Element                           Description
  30781.  ────────────────────────────────────────────────────────────────────────────
  30782.  double wx                         window x coordinate
  30783.  
  30784.  double wy                         window y coordinate
  30785.  
  30786.  The control parameter can be one of the following manifest constants:
  30787.  
  30788.  Constant                          Action
  30789.  ────────────────────────────────────────────────────────────────────────────
  30790.  _GFILLINTERIOR                    Fills the figure with the current color
  30791.                                    using the current fill mask
  30792.  
  30793.  _GBORDER                          Does not fill the rectangle
  30794.  
  30795.  If the current fill mask is NULL, no mask is used. Instead, the rectangle is
  30796.  filled with the current color.
  30797.  
  30798.  If you try to fill the rectangle with the _floodfill function, the rectangle
  30799.  must be bordered by a solid line-style pattern.
  30800.  
  30801.  
  30802.  Return Value
  30803.  
  30804.  The function returns a nonzero value if the rectangle is drawn successfully,
  30805.  or 0 if not.
  30806.  
  30807.  
  30808.  Compatibility
  30809.  
  30810.   ANSI   DOS   OS/2   UNIX   XENIX
  30811.  
  30812.  
  30813.  
  30814.  See Also
  30815.  
  30816.  _arc functions,  _ellipse functions,  _floodfill,  _getcolor,  _lineto
  30817.  functions, _pie functions,  _setcolor,  _setfillmask
  30818.  
  30819.  
  30820.  Example
  30821.  
  30822.    /* RECT.C: This program draws a rectangle. */
  30823.  
  30824.    #include <conio.h>
  30825.    #include <stdlib.h>
  30826.    #include <graph.h>
  30827.  
  30828.    void main()
  30829.    {
  30830.       /* Find a valid graphics mode. */
  30831.       if( !_setvideomode( _MAXRESMODE ) )
  30832.          exit( 1 );
  30833.  
  30834.       _rectangle( _GBORDER, 80, 50, 240, 150 );
  30835.  
  30836.       getch();
  30837.  
  30838.       _setvideomode( _DEFAULTMODE );
  30839.    }
  30840.  
  30841.  
  30842.  
  30843.  
  30844.  
  30845.  _registerfonts
  30846.  ────────────────────────────────────────────────────────────────────────────
  30847.  
  30848.  
  30849.  Description
  30850.  
  30851.  Initializes the fonts graphics system.
  30852.  
  30853.    #include <graph.h>
  30854.  
  30855.    short _far _registerfonts( unsigned char _far *pathname );
  30856.  
  30857.  pathname                          Path name specifying .FON files to be
  30858.                                    registered
  30859.  
  30860.  
  30861.  Remarks
  30862.  
  30863.  The _registerfonts function initializes the fonts graphics system. Font
  30864.  files must be registered with the _registerfonts function before any other
  30865.  font-related library function (_getgtextextent, _outgtext, _setfont,
  30866.  _unregisterfonts) can be used.
  30867.  
  30868.  The _registerfonts function reads the specified files and loads font header
  30869.  information into memory. Each font header takes up about 140 bytes of
  30870.  memory.
  30871.  
  30872.  The pathname argument is the path specification and file name of valid .FON
  30873.  files. The pathname can contain standard DOS wild-card characters.
  30874.  
  30875.  The font functions affect only the output from the font output function
  30876.  _outgtext; no other C run-time output functions are affected by font usage.
  30877.  
  30878.  
  30879.  
  30880.  Return Value
  30881.  
  30882.  The _registerfonts function returns a positive value which indicates the
  30883.  number of fonts successfully registered. A negative return value indicates
  30884.  failure. The following negative values may be returned:
  30885.  
  30886.  Value                             Meaning
  30887.  ────────────────────────────────────────────────────────────────────────────
  30888.  -1                                No such file or directory.
  30889.  
  30890.  -2                                One or more of the .FON files was not a
  30891.                                    valid, binary .FON file.
  30892.  
  30893.  -3                                One or more of the .FON files is
  30894.                                    damaged.
  30895.  
  30896.  
  30897.  Compatibility
  30898.  
  30899.   ANSI   DOS   OS/2   UNIX   XENIX
  30900.  
  30901.  
  30902.  
  30903.  See Also
  30904.  
  30905.   _getfontinfo,  _getgtextextent,  _outgtext,  _setfont,  _unregisterfonts
  30906.  
  30907.  
  30908.  Example
  30909.  
  30910.  See the example for _outgtext.
  30911.  
  30912.  
  30913.  
  30914.  
  30915.  
  30916.  _remapallpalette, _remappalette
  30917.  ────────────────────────────────────────────────────────────────────────────
  30918.  
  30919.  
  30920.  Description
  30921.  
  30922.  Remap all palette colors.
  30923.  
  30924.    #include <graph.h>
  30925.  
  30926.    short _far _remapallpalette( long _far *colors );
  30927.  
  30928.    long _far _remappalette( short index, long color );
  30929.  
  30930.  colors                            Color value array
  30931.  
  30932.  index                             Color index to reassign
  30933.  
  30934.  color                             Color value to assign color index to
  30935.  
  30936.  
  30937.  Remarks
  30938.  
  30939.  The _remapallpalette function remaps the entire color palette simultaneously
  30940.  to the colors given in the colors array. The colors array is an array of
  30941.  long integers where the size of the array varies from 16 to 64 to 256,
  30942.  depending on the video mode. The number of colors mapped depends on the
  30943.  number of colors supported by the current video mode. The _remapallpalette
  30944.  function works in all video modes (except _ORESCOLOR mode), but only with
  30945.  EGA, MCGA, or VGA hardware.
  30946.  
  30947.  The default color values for a color text on 16-color graphics mode are
  30948.  shown below:
  30949.  
  30950. ╓┌───────┌─────────┌───────┌─────────────────────────────────────────────────╖
  30951.  Number  Color     Number  Color
  30952.  ────────────────────────────────────────────────────────────────────────────
  30953.  0       Black     8       Dark gray
  30954.  
  30955.  1       Blue      9       Light blue
  30956.  Number  Color     Number  Color
  30957.  ────────────────────────────────────────────────────────────────────────────
  30958. 1       Blue      9       Light blue
  30959.  
  30960.  2       Green     10      Light green
  30961.  
  30962.  3       Cyan      11      Light cyan
  30963.  
  30964.  4       Red       12      Light red
  30965.  
  30966.  5       Magenta   13      Light magenta
  30967.  
  30968.  6       Brown     14      Yellow
  30969.  
  30970.  7       White     15      Bright white
  30971.  
  30972.  
  30973.  
  30974.  The first array element specifies the new color value to be associated with
  30975.  color index 0 (the background color in graphics modes). After the call to
  30976.  _remapallpalette, calls to _setcolor will index into the new array of
  30977.  colors. The mapping done by _remapallpalette affects the current display
  30978.  immediately.
  30979.  
  30980.  The colors array can be larger than the number of colors supported by the
  30981.  current video mode, but only the first n elements are used, where n is the
  30982.  number of colors supported by the current video mode, as indicated by the
  30983.  numcolors element of the videoconfig  structure.
  30984.  
  30985.  The long color value is defined by specifying three bytes of data
  30986.  representing the three component colors: red, green, and blue.
  30987.  
  30988.  Each of the three bytes represents the intensity of one of the red, green,
  30989.  or blue component colors, and must be in the range 0-31. In other words, the
  30990.  low-order six bits of each byte specify the component's intensity and the
  30991.  high-order two bits should be zero. The fourth (high-order) byte in the long
  30992.  is unused and should be set to zero. The diagram below shows the ordering of
  30993.  bytes within the long value.
  30994.  
  30995.  For example, to create a lighter shade of blue, start with lots of blue, add
  30996.  some green, and maybe a little bit of red. The three-byte color value would
  30997.  be:
  30998.  
  30999.    blue byte            green byte        red byte
  31000.    00011111             00101111          00011111
  31001.    high ───────────────->  low order
  31002.  
  31003.  Manifest constants are defined in GRAPH.H for the default color values
  31004.  corresponding to color indices 0-15 in color text modes and 16-color
  31005.  graphics modes, as shown below:
  31006.  
  31007. ╓┌──────┌──────────┌──────┌──────────────────────────────────────────────────╖
  31008.  Index  Constant   Index  Constant
  31009.  ────────────────────────────────────────────────────────────────────────────
  31010.  0      _BLACK     8      _GRAY
  31011.  
  31012.  1      _BLUE      9      _LIGHTBLUE
  31013.  
  31014.  2      _GREEN     10     _LIGHTGREEN
  31015.  
  31016.  3      _CYAN      11     _LIGHTCYAN
  31017.  
  31018.  4      _RED       12     _LIGHTRED
  31019.  Index  Constant   Index  Constant
  31020.  ────────────────────────────────────────────────────────────────────────────
  31021. 4      _RED       12     _LIGHTRED
  31022.  
  31023.  5      _MAGENTA   13     _LIGHTMAGENTA
  31024.  
  31025.  6      _BROWN     14     _YELLOW
  31026.  
  31027.  7      _WHITE     15     _BRIGHTWHITE
  31028.  
  31029.  
  31030.  
  31031.  The VGA supports a palette of 262,144 colors (256K) in color modes, and the
  31032.  EGA supports a palette of only 64 different colors. Color values for EGA are
  31033.  specified in exactly the same way as with the VGA; however, the low-order
  31034.  four bits of each byte are simply ignored.
  31035.  
  31036.  The _remappalette function assigns a new color value color to the color
  31037.  index given by index. This remapping affects the current display
  31038.  immediately.
  31039.  
  31040.  The _remappalette function works in all graphics modes, but only with EGA,
  31041.  MCGA, or VGA hardware. An error results if the function is called while
  31042.  using any other configuration.
  31043.  
  31044.  The color value used in _remappalette is defined and used exactly as noted
  31045.  above for _remapallpalette. The range of color indices used with
  31046.  _remappalette depends on the number of colors supported by the video mode.
  31047.  
  31048.  The _remapallpalette and _remappalette functions do not affect the
  31049.  presentation-graphics palettes, which are manipulated with the
  31050.  _pg_getpalette, _pg_setpalette, and _pg_resetpalette functions.
  31051.  
  31052.  If a VGA or MCGA adapter is connected to an analog monochrome monitor, the
  31053.  color value is transformed into its gray-scale equivalent, based on the
  31054.  weighted sum of its red, green, and blue components (30% red + 50% green +
  31055.  11% blue). The original red, green, and blue values are lost.
  31056.  
  31057.  
  31058.  Return Value
  31059.  
  31060.  If successful, _remapallpalette returns -1 (short). In case of an error,
  31061.  _remapallpalette returns 0 (short).
  31062.  
  31063.  If successful, _remappalette returns the color value previously assigned to
  31064.  index, or -1 if the function is inoperative (not EGA, VGA, or MCGA), or if
  31065.  the color index is out of range.
  31066.  
  31067.  Note that _remapallpalette returns a short value and _remappalette returns a
  31068.  long value.
  31069.  
  31070.  
  31071.  Compatibility
  31072.  
  31073.   ANSI   DOS   OS/2   UNIX   XENIX
  31074.  
  31075.  
  31076.  
  31077.  See Also
  31078.  
  31079.  _selectpalette,  _setbkcolor,  _setvideomode
  31080.  
  31081.  
  31082.  Example
  31083.  
  31084.    /* RMPALPAL.C: This example illustrates functions for assigning
  31085.     * color values to color indices. Functions illustrated include:
  31086.     *    _remappalette         _remapallpalette
  31087.     */
  31088.  
  31089.    #include <graph.h>
  31090.    #include <conio.h>
  31091.    #include <stdio.h>
  31092.    #include <stdlib.h>
  31093.  
  31094.    /* Macro for mixing Red, Green, and Blue elements of color */
  31095.    #define RGB(r,g,b) (((long) ((b) << 8 | (g)) << 8) | (r))
  31096.  
  31097.    long tmp, pal[256];
  31098.    void main()
  31099.    {
  31100.       short  red, blue, green;
  31101.       short  inc, i, mode, cells, x, y, xinc, yinc;
  31102.       char   buf[40];
  31103.       struct videoconfig vc;
  31104.  
  31105.       /* Make sure all palette numbers are valid. */
  31106.       for( i = 0; i < 256; i++ )
  31107.          pal[i] = _BLACK;
  31108.  
  31109.       /* Loop through each graphics mode that supports palettes. */
  31110.       for( mode = _MRES4COLOR; mode <= _MRES256COLOR; mode++ )
  31111.       {
  31112.          if( mode == _ERESNOCOLOR )
  31113.             mode++;
  31114.          if( !_setvideomode( mode ) )
  31115.             continue;
  31116.  
  31117.          /* Set variables for each mode. */
  31118.          _getvideoconfig( &vc );
  31119.          switch( vc.numcolors )
  31120.          {
  31121.             case 256:          /* Active bits in this order:           */
  31122.                cells = 13;
  31123.                inc = 12;       /* ???????? ??bbbbbb ??gggggg ??rrrrrr  */
  31124.                break;
  31125.             case  16:
  31126.                cells = 4;
  31127.                if( (vc.mode == _ERESCOLOR) || (vc.mode == _VRES16COLOR) )
  31128.                   inc = 16;    /* ???????? ??bb???? ??gg???? ??rr????  */
  31129.                else
  31130.                   inc = 32;    /* ???????? ??Bb???? ??Gg???? ??Rr????  */
  31131.                break;
  31132.             case   4:
  31133.                cells = 2;
  31134.                inc = 32;       /* ???????? ??Bb???? ??Gg???? ??Rr????  */
  31135.                break;
  31136.             default:
  31137.                 continue;
  31138.          }
  31139.          xinc = vc.numxpixels / cells;
  31140.          yinc = vc.numypixels / cells;
  31141.  
  31142.    /* Fill palette arrays in BGR order. */
  31143.          for( i = 0, blue = 0; blue < 64; blue += inc )
  31144.             for( green = 0; green < 64; green += inc )
  31145.                for( red = 0; red < 64; red += inc )
  31146.                {
  31147.                   pal[i] = RGB( red, green, blue );
  31148.                   /* Special case of using 6 bits to represent 16 colors.
  31149.                    * If both bits are on for any color, intensity is set.
  31150.                    * If one bit is set for a color, the color is on.
  31151.                    */
  31152.                   if( inc == 32 )
  31153.                      pal[i + 8] = pal[i] | (pal[i] >> 1);
  31154.                   i++;
  31155.                }
  31156.  
  31157.          /* If palettes available, remap all palettes at once. */
  31158.          if( !_remapallpalette( pal ) )
  31159.          {
  31160.             _setvideomode( _DEFAULTMODE );
  31161.             _outtext( "Palettes not available with this adapter" );
  31162.             exit( 1 );
  31163.          }
  31164.  
  31165.          /* Draw colored squares. */
  31166.          for( i = 0, x = 0; x < ( xinc * cells ); x += xinc )
  31167.             for( y = 0; y < ( yinc * cells); y += yinc )
  31168.             {
  31169.                _setcolor( i++ );
  31170.                _rectangle( _GFILLINTERIOR, x, y, x + xinc, y + yinc );
  31171.             }
  31172.  
  31173.          /* Note that for 256-color mode, not all colors are shown. The
  31174.  number
  31175.           * of colors from mixing three base colors can never be the same as
  31176.           * the number that can be shown on a two-dimensional grid.
  31177.           */
  31178.          sprintf( buf, "Mode %d has %d colors", vc.mode, vc.numcolors );
  31179.          _setcolor( vc.numcolors / 2 );
  31180.          _outtext( buf );
  31181.          getch();
  31182.  
  31183.    /* Change each palette entry separately in GRB order. */
  31184.          for( i = 0, green = 0; green < 64; green += inc )
  31185.             for( red = 0; red < 64; red += inc )
  31186.                for(blue = 0; blue < 64; blue += inc )
  31187.                {
  31188.                   tmp = RGB( red, green, blue );
  31189.                   _remappalette( i, tmp );
  31190.                   if( inc == 32 )
  31191.                      _remappalette( i + 8, tmp | (tmp >> 1) );
  31192.                   i++;
  31193.                }
  31194.          getch();
  31195.       }
  31196.       _setvideomode( _DEFAULTMODE );
  31197.    }
  31198.  
  31199.  
  31200.  
  31201.  
  31202.  
  31203.  remove
  31204.  ────────────────────────────────────────────────────────────────────────────
  31205.  
  31206.  
  31207.  Description
  31208.  
  31209.  Deletes a file.
  31210.  
  31211.  #include <stdio.h>                Required for ANSI compatibility
  31212.  
  31213.  #include <io.h>                   Use either IO.H or STDIO.H
  31214.  
  31215.    int remove( const char *path );
  31216.  
  31217.  path                              Path name of file to be removed
  31218.  
  31219.  
  31220.  Remarks
  31221.  
  31222.  The remove function deletes the file specified by path.
  31223.  
  31224.  
  31225.  Return Value
  31226.  
  31227.  The function returns 0 if the file is successfully deleted. Otherwise, it
  31228.  returns -1 and sets errno to one of these values:
  31229.  
  31230.  Value                             Meaning
  31231.  ────────────────────────────────────────────────────────────────────────────
  31232.  EACCES                            Path name specifies a read-only file.
  31233.  
  31234.  ENOENT                            File or path name not found, or path
  31235.                                    name specifies a directory.
  31236.  
  31237.  
  31238.  Compatibility
  31239.  
  31240.   ANSI   DOS   OS/2   UNIX   XENIX
  31241.  
  31242.  
  31243.  
  31244.  See Also
  31245.  
  31246.  unlink
  31247.  
  31248.  
  31249.  Example
  31250.  
  31251.    /* REMOVE.C: This program uses remove to delete REMOVE.OBJ. */
  31252.  
  31253.    #include <stdio.h>
  31254.  
  31255.    void main()
  31256.    {
  31257.       if( remove( "remove.obj" ) == -1 )
  31258.          perror( "Could not delete 'REMOVE.OBJ'" );
  31259.       else
  31260.          printf( "Deleted 'REMOVE.OBJ'\n" );
  31261.    }
  31262.  
  31263.  
  31264.  Output
  31265.  
  31266.  
  31267.  
  31268.    Deleted 'REMOVE.OBJ'
  31269.  
  31270.  
  31271.  
  31272.  
  31273.  
  31274.  rename
  31275.  ────────────────────────────────────────────────────────────────────────────
  31276.  
  31277.  
  31278.  Description
  31279.  
  31280.  Renames a file or directory.
  31281.  
  31282.  #include <stdio.h>                Required for ANSI compatibility
  31283.  
  31284.  #include <io.h>                   Use either IO.H or STDIO.H
  31285.  
  31286.    int rename( const char *oldname, const char *newname );
  31287.  
  31288.  oldname                           Pointer to old name
  31289.  
  31290.  newname                           Pointer to new name
  31291.  
  31292.  
  31293.  Remarks
  31294.  
  31295.  The rename function renames the file or directory specified by oldname to
  31296.  the name given by newname. The old name must be the path name of an existing
  31297.  file or directory. The new name must not be the name of an existing file or
  31298.  directory.
  31299.  
  31300.  The rename function can be used to move a file from one directory to another
  31301.  by giving a different path name in the newname argument. However, files
  31302.  cannot be moved from one device to another (for example, from drive A to
  31303.  drive B). Directories can only be renamed, not moved.
  31304.  
  31305.  
  31306.  Return Value
  31307.  
  31308.  The rename function returns 0 if it is successful. On an error, it returns a
  31309.  nonzero value and sets errno to one of the following values:
  31310.  
  31311.  Value                             Meaning
  31312.  ────────────────────────────────────────────────────────────────────────────
  31313.  EACCES                            File or directory specified by newname
  31314.                                    already exists or could not be created
  31315.                                    (invalid path); or oldname is a
  31316.                                    directory and newname specifies a
  31317.                                    different path.
  31318.  
  31319.  ENOENT                            File or path name specified by oldname
  31320.                                    not found.
  31321.  
  31322.  EXDEV                             Attempt to move a file to a different
  31323.                                    device.
  31324.  
  31325.  
  31326.  Compatibility
  31327.  
  31328.   ANSI   DOS   OS/2   UNIX   XENIX
  31329.  
  31330.  
  31331.  
  31332.  Example
  31333.  
  31334.    /* RENAMER.C: This program attempts to rename a file named RENAMER.OBJ to
  31335.     * RENAMER.JBO. For this operation to succeed, a file named RENAMER.OBJ
  31336.     * must exist and a file named RENAMER.JBO must not exist.
  31337.     */
  31338.  
  31339.    #include <stdio.h>
  31340.  
  31341.    void main()
  31342.    {
  31343.       int  result;
  31344.       char old[] = "RENAMER.OBJ", new[] = "RENAMER.JBO";
  31345.  
  31346.       /* Attempt to rename file: */
  31347.       result = rename( old, new );
  31348.       if( result != 0 )
  31349.          printf( "Could not rename '%s'\n", old );
  31350.       else
  31351.          printf( "File '%s' renamed to '%s'\n", old, new );
  31352.    }
  31353.  
  31354.  
  31355.  Output
  31356.  
  31357.  
  31358.  
  31359.    File 'RENAMER.OBJ' renamed to 'RENAMER.JBO'
  31360.  
  31361.  
  31362.  
  31363.  
  31364.  
  31365.  rewind
  31366.  ────────────────────────────────────────────────────────────────────────────
  31367.  
  31368.  
  31369.  Description
  31370.  
  31371.  Repositions the file pointer to the beginning of a file.
  31372.  
  31373.    #include <stdio.h>
  31374.  
  31375.    void rewind( FILE *stream );
  31376.  
  31377.  stream                            Pointer to FILE structure
  31378.  
  31379.  
  31380.  Remarks
  31381.  
  31382.  The rewind function repositions the file pointer associated with stream to
  31383.  the beginning of the file. A call to rewind is equivalent to
  31384.  
  31385.  (void) fseek( stream, 0L, SEEK_SET );
  31386.  
  31387.  except that rewind clears the error indicators for the stream, and fseek
  31388.  does not. Both rewind and fseek clear the end-of-file indicator. Also, fseek
  31389.  returns a value that indicates whether the pointer was successfully moved,
  31390.  but rewind does not return any value.
  31391.  
  31392.  You can also use the rewind function to clear the keyboard buffer. Use the
  31393.  rewind function with the stream stdin, which is associated with the keyboard
  31394.  by default.
  31395.  
  31396.  
  31397.  Return Value
  31398.  
  31399.  The rewind function has no return value.
  31400.  
  31401.  
  31402.  Compatibility
  31403.  
  31404.   ANSI   DOS   OS/2   UNIX   XENIX
  31405.  
  31406.  
  31407.  
  31408.  Example
  31409.  
  31410.    /* REWIND.C: This program first opens a file named REWIND.OUT for input
  31411.    and
  31412.     * output and writes two integers to the file. Next, it uses rewind to
  31413.     * reposition the file pointer to the beginning of the file and reads
  31414.     * the data back in.
  31415.     */
  31416.  
  31417.    #include <stdio.h>
  31418.  
  31419.    void main()
  31420.    {
  31421.       FILE *stream;
  31422.       int data1, data2;
  31423.  
  31424.       data1 = 1;
  31425.       data2 = -37;
  31426.  
  31427.       if( (stream = fopen( "rewind.out", "w+" )) != NULL )
  31428.       {
  31429.          fprintf( stream, "%d %d", data1, data2 );
  31430.          printf( "The values written are: %d and %d\n", data1, data2 );
  31431.          rewind( stream );
  31432.          fscanf( stream, "%d %d", &data1, &data2 );
  31433.          printf( "The values read are: %d and %d\n", data1, data2 );
  31434.          fclose( stream );
  31435.       }
  31436.    }
  31437.  
  31438.  
  31439.  Output
  31440.  
  31441.  
  31442.  
  31443.    The values written are: 1 and -37
  31444.    The values read are: 1 and -37
  31445.  
  31446.  
  31447.  
  31448.  
  31449.  
  31450.  rmdir
  31451.  ────────────────────────────────────────────────────────────────────────────
  31452.  
  31453.  
  31454.  Description
  31455.  
  31456.  Deletes a directory.
  31457.  
  31458.  #include <direct.h>               Required only for function declarations
  31459.  
  31460.    int rmdir( char *dirname );
  31461.  
  31462.  dirname                           Path name of directory to be removed
  31463.  
  31464.  
  31465.  Remarks
  31466.  
  31467.  The rmdir function deletes the directory specified by dirname. The directory
  31468.  must be empty, and it must not be the current working directory or the root
  31469.  directory.
  31470.  
  31471.  
  31472.  Return Value
  31473.  
  31474.  The rmdir function returns the value 0 if the directory is successfully
  31475.  deleted. A return value of -1 indicates an error, and errno is set to one of
  31476.  the following values:
  31477.  
  31478.  Value                             Meaning
  31479.  ────────────────────────────────────────────────────────────────────────────
  31480.  EACCES                            The given path name is not a directory;
  31481.                                    or the directory is not empty; or the
  31482.                                    directory is the current working
  31483.                                    directory or the  root directory.
  31484.  
  31485.  ENOENT                            Path name not found.
  31486.  
  31487.  
  31488.  Compatibility
  31489.  
  31490.   ANSI   DOS   OS/2   UNIX   XENIX
  31491.  
  31492.  
  31493.  
  31494.  See Also
  31495.  
  31496.  chdir, mkdir
  31497.  
  31498.  
  31499.  Example
  31500.  
  31501.    /* MAKEDIR.C */
  31502.    #include <direct.h>
  31503.    #include <stdlib.h>
  31504.    #include <stdio.h>
  31505.  
  31506.    void main()
  31507.    {
  31508.       int result;
  31509.  
  31510.    if( mkdir( "\\testtmp" ) == 0 )
  31511.       {
  31512.          printf( "Directory '\\testtmp' was successfully created\n" );
  31513.          system( "dir \\testtmp" );
  31514.          if( rmdir( "\\testtmp" ) == 0 )
  31515.             printf( "Directory '\\testtmp' was successfully removed\n"  );
  31516.          else
  31517.             printf( "Problem removing directory '\\testtmp'\n" );
  31518.       }
  31519.       else
  31520.          printf( "Problem creating directory '\\testtmp'\n" );
  31521.    }
  31522.  
  31523.  
  31524.  Output
  31525.  
  31526.  
  31527.  
  31528.    Directory '\testtmp' was successfully created
  31529.  
  31530.     The volume label in drive C is OS2.
  31531.     Directory of C:\TESTTMP
  31532.  
  31533.    .            <DIR>      6-19-89  11:20a
  31534.    ..           <DIR>      6-19-89  11:20a
  31535.         2 File(s)   12730368 bytes free
  31536.    Directory '\testtmp' was successfully removed
  31537.  
  31538.  
  31539.  
  31540.  
  31541.  
  31542.  rmtmp
  31543.  ────────────────────────────────────────────────────────────────────────────
  31544.  
  31545.  
  31546.  Description
  31547.  
  31548.  Removes temporary files.
  31549.  
  31550.    #include <stdio.h>
  31551.  
  31552.    int rmtmp( void );
  31553.  
  31554.  
  31555.  Remarks
  31556.  
  31557.  The rmtmp function is used to clean up all the temporary files in the
  31558.  current directory. The function removes only those files created by tmpfile
  31559.  and should be used only in the same directory in which the temporary files
  31560.  were created.
  31561.  
  31562.  
  31563.  Return Value
  31564.  
  31565.  The rmtmp function returns the number of temporary files closed and deleted.
  31566.  
  31567.  
  31568.  
  31569.  Compatibility
  31570.  
  31571.   ANSI   DOS   OS/2   UNIX   XENIX
  31572.  
  31573.  
  31574.  
  31575.  See Also
  31576.  
  31577.  flushall, tmpfile, tmpnam
  31578.  
  31579.  
  31580.  Example
  31581.  
  31582.    /* TMPFILE.C: This program uses tmpfile to create a temporary file,
  31583.     * then deletes this file with rmtmp.
  31584.     */
  31585.  
  31586.    #include <stdio.h>
  31587.  
  31588.    void main()
  31589.    {
  31590.       FILE *stream;
  31591.       char tempstring[] = "String to be written";
  31592.       int  i;
  31593.  
  31594.       /* Create temporary files. */
  31595.       for( i = 1; i <= 10; i++ )
  31596.       {
  31597.          if( (stream = tmpfile()) == NULL )
  31598.             perror( "Could not open new temporary file\n" );
  31599.          else
  31600.             printf( "Temporary file %d was created\n", i );
  31601.       }
  31602.  
  31603.       /* Remove temporary files. */
  31604.       printf( "%d temporary files deleted\n", rmtmp() );
  31605.    }
  31606.  
  31607.  
  31608.  Output
  31609.  
  31610.  
  31611.  
  31612.    Temporary file 1 was created
  31613.    Temporary file 2 was created
  31614.    Temporary file 3 was created
  31615.    Temporary file 4 was created
  31616.    Temporary file 5 was created
  31617.    Temporary file 6 was created
  31618.    Temporary file 7 was created
  31619.    Temporary file 8 was created
  31620.    Temporary file 9 was created
  31621.    Temporary file 10 was created
  31622.    10 temporary files deleted
  31623.  
  31624.  
  31625.  
  31626.  
  31627.  
  31628.  _rotl, _rotr
  31629.  ────────────────────────────────────────────────────────────────────────────
  31630.  
  31631.  
  31632.  Description
  31633.  
  31634.  Rotate bits to the left (_rotl) or right (_rotr).
  31635.  
  31636.    #include <stdlib.h>
  31637.  
  31638.    unsigned int _rotl( unsigned int value, int shift );
  31639.  
  31640.    unsigned int _rotr( unsigned int value, int shift );
  31641.  
  31642.  value                             Value to be rotated
  31643.  
  31644.  shift                             Number of bits to shift
  31645.  
  31646.  
  31647.  Remarks
  31648.  
  31649.  The _rotl and _rotr functions rotate the unsigned value by shift bits. The
  31650.  _rotl function rotates the value left. The _rotr function rotates the value
  31651.  right. Both functions "wrap" bits rotated off one end of value to the other
  31652.  end.
  31653.  
  31654.  
  31655.  Return Value
  31656.  
  31657.  Both functions return the rotated value. There is no error return.
  31658.  
  31659.  
  31660.  Compatibility
  31661.  
  31662.   ANSI   DOS   OS/2   UNIX   XENIX
  31663.  
  31664.  
  31665.  
  31666.  See Also
  31667.  
  31668.  _lrotl,  _lrotr
  31669.  
  31670.  
  31671.  Example
  31672.  
  31673.    /* ROT.C: This program uses _rotr and _rotl with different shift
  31674.     * values to rotate an integer.
  31675.     */
  31676.  
  31677.    #include <stdlib.h>
  31678.    #include <stdio.h>
  31679.  
  31680.    void main()
  31681.    {
  31682.       unsigned val = 0x0fd93;
  31683.  
  31684.       printf( "0x%4.4x rotated left three times is 0x%4.4x\n",
  31685.               val, _rotl( val, 3 ) );
  31686.       printf( "0x%4.4x rotated right four times is 0x%4.4x\n",
  31687.               val, _rotr( val, 4 ) );
  31688.    }
  31689.  
  31690.  
  31691.  Output
  31692.  
  31693.  
  31694.  
  31695.    0xfd93 rotated left three times is 0xec9f
  31696.    0xfd93 rotated right four times is 0x3fd9
  31697.  
  31698.  
  31699.  
  31700.  
  31701.  
  31702.  scanf
  31703.  ────────────────────────────────────────────────────────────────────────────
  31704.  
  31705.  
  31706.  Description
  31707.  
  31708.  Reads formatted data from the standard input stream.
  31709.  
  31710.    #include <stdio.h>
  31711.  
  31712.    int scanf( const char *format [[,argument]]... );
  31713.  
  31714.  format                            Format control
  31715.  
  31716.  argument                          Optional argument
  31717.  
  31718.  
  31719.  Remarks
  31720.  
  31721.  The scanf function reads data from the standard input stream stdin into the
  31722.  locations given by argument. Each argument must be a pointer to a variable
  31723.  with a type that corresponds to a type specifier in format. The format
  31724.  controls the interpretation of the input fields. The format can contain one
  31725.  or more of the following:
  31726.  
  31727.  
  31728.    ■   White-space characters: blank (' '); tab (\t); or newline (\n). A
  31729.        white-space character causes scanf to read, but not store, all
  31730.        consecutive white-space characters in the input up to the next
  31731.        non-white-space character. One white-space character in the format
  31732.        matches any number (including 0) and combination of white-space
  31733.        characters in the input.
  31734.  
  31735.    ■   Non-white-space characters, except for the percent sign (%). A
  31736.        non-white-space character causes scanf to read, but not store, a
  31737.        matching non-white-space character. If the next character in stdin
  31738.        does not match, scanf terminates.
  31739.  
  31740.    ■   Format specifications, introduced by the percent sign (%). A format
  31741.        specification causes scanf to read and convert characters in the input
  31742.        into values of a specified type. The value is assigned to an argument
  31743.        in the argument list.
  31744.  
  31745.  
  31746.  The format is read from left to right. Characters outside format
  31747.  specifications are expected to match the sequence of characters in stdin;
  31748.  the matching characters in stdin are scanned but not stored. If a character
  31749.  in stdin conflicts with the format specification, scanf terminates. The
  31750.  character is left in stdin as if it had not been read.
  31751.  
  31752.  When the first format specification is encountered, the value of the first
  31753.  input field is converted according to this specification and stored in the
  31754.  location that is specified by the first argument. The second format
  31755.  specification causes the second input field to be converted and stored in
  31756.  the second argument, and so on through the end of the format string.
  31757.  
  31758.  An input field is defined as all characters up to the first white-space
  31759.  character (space, tab, or newline), or up to the first character that cannot
  31760.  be converted according to the format specification, or until the field width
  31761.  (if specified) is reached. If there are too many arguments for the given
  31762.  specifications, the extra arguments are evaluated but ignored. The results
  31763.  are unpredictable if there are not enough arguments for the format
  31764.  specification.
  31765.  
  31766.  A format specification has the following form:
  31767.  
  31768.    %[[*]] [[width]] [[{F | N}]] [[{h | l}]]type
  31769.  
  31770.  Each field of the format specification is a single character or a number
  31771.  signifying a particular format option. The type character, which appears
  31772.  after the last optional format field, determines whether the input field is
  31773.  interpreted as a character, a string, or a number. The simplest format
  31774.  specification contains only the percent sign and a type character (for
  31775.  example,  %s ).
  31776.  
  31777.  Each field of the format specification is discussed in detail below. If a
  31778.  percent sign (%) is followed by a character that has no meaning as a
  31779.  format-control character, that character and the following characters (up to
  31780.  the next percent sign) are treated as an ordinary sequence of
  31781.  characters─that is, a sequence of characters that must match the input. For
  31782.  example, to specify that a percent-sign character is to be input, use  %%.
  31783.  
  31784.  An asterisk (*) following the percent sign suppresses assignment of the next
  31785.  input field, which is interpreted as a field of the specified type. The
  31786.  field is scanned but not stored.
  31787.  
  31788.  The width is a positive decimal integer controlling the maximum number of
  31789.  characters to be read from stdin. No more than width characters are
  31790.  converted and stored at the corresponding argument. Fewer than width
  31791.  characters may be read if a white-space character (space, tab, or newline)
  31792.  or a character that cannot be converted according to the given format occurs
  31793.  before width is reached.
  31794.  
  31795.  The optional F and N prefixes allow the user to specify whether the argument
  31796.  is far or near, respectively. F should be prefixed to an argument pointing
  31797.  to a far object, while N should be prefixed to an argument pointing to a
  31798.  near object. Note also that the F and N prefixes are not part of the ANSI
  31799.  definition for scanf, but are instead Microsoft extensions, which should not
  31800.  be used when ANSI portability is desired.
  31801.  
  31802.  The optional prefix l indicates that the long version of the following type
  31803.  is to be used, while the prefix h indicates that the short version is to be
  31804.  used. The corresponding argument should point to a long or double object
  31805.  (with the l character) or a short object (with the h character). The l and h
  31806.  modifiers can be used with the d, i, n, o, x, and u type characters. The l
  31807.  modifier can also be used with the e, f, and g type characters. The l and h
  31808.  modifiers are ignored if specified for any other type.
  31809.  
  31810.  For scanf, N and F refer to the "distance" to the object being read in (near
  31811.  or far) and h and l refer to the "size" of the object being read in (16-bit
  31812.  short or 32-bit long). The list below clarifies this use of N, F, l, and h:
  31813.  
  31814.  
  31815.  Program Code                      Action
  31816.  ────────────────────────────────────────────────────────────────────────────
  31817.  scanf( "%Ns", &x );               Read a string into near memory
  31818.  
  31819.  scanf( "%Fs", &x );               Read a string into far memory
  31820.  
  31821.  scanf( "%Nd", &x );               Read an int into near memory
  31822.  
  31823.  scanf( "%Fd", &x );               Read an int into far memory
  31824.  
  31825.  scanf( "%Nld", &x );              Read a long int into near memory
  31826.  
  31827.  scanf( "%Fld", &x );              Read a long int into far memory
  31828.  
  31829.  scanf( "%Nhp", &x );              Read a 16-bit pointer into near memory
  31830.  
  31831.  scanf( "%Nlp", &x );              Read a 32-bit pointer into near memory
  31832.  
  31833.  scanf( "%Fhp", &x );              Read a 16-bit pointer into far memory
  31834.  
  31835.  scanf( "%Flp", &x );              Read a 32-bit pointer into far memory
  31836.  
  31837.  The type characters and their meanings are described in Table R.5.
  31838.  
  31839.  To read strings not delimited by space characters, a set of characters in
  31840.  brackets ([ ]) can be substituted for the s (string) type character. The
  31841.  corresponding input field is read up to the first character that does not
  31842.  appear in the bracketed character set. If the first character in the set is
  31843.  a caret (^), the effect is reversed: the input field is read up to the first
  31844.  character that does appear in the rest of the character set.
  31845.  
  31846.  Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z].
  31847.  This is a common scanf extension, but note that it is not required by the
  31848.  ANSI specification.
  31849.  
  31850.  To store a string without storing a terminating null character ('\0'), use
  31851.  the specification %nc, where n is a decimal integer. In this case, the c
  31852.  type character indicates that the argument is a pointer to a character
  31853.  array. The next n characters are read from the input stream into the
  31854.  specified location, and no null character ('\0') is appended. If n is not
  31855.  specified, the default value for it is 1.
  31856.  
  31857.  The scanf function scans each input field, character by character. It may
  31858.  stop reading a particular input field before it reaches a space character
  31859.  for a variety of reasons: the specified width has been reached; the next
  31860.  character cannot be converted as specified; the next character conflicts
  31861.  with a character in the control string that it is supposed to match; or the
  31862.  next character fails to appear in a given character set. For whatever
  31863.  reason, when scanf stops reading an input field, the next input field is
  31864.  considered to begin at the first unread character. The conflicting
  31865.  character, if there is one, is considered unread and is the first character
  31866.  of the next input field or the first character in subsequent read operations
  31867.  on stdin.
  31868.  
  31869.  Table   R.5 Type Characters for scanf
  31870.  
  31871. ╓┌──────────────────────────┌──────────────────────────┌─────────────────────
  31872.  Character                  Type of Input Expected     Type of Argument
  31873.  ─────────────────────────────────────────────────────────────────────────────
  31874.  d                          Decimal integer            Pointer to int
  31875.  
  31876.  o                          Octal integer              Pointer to int
  31877.  
  31878.  x                          Hexadecimal integer(1)     Pointer to int
  31879.  
  31880.  Character                  Type of Input Expected     Type of Argument
  31881.  ─────────────────────────────────────────────────────────────────────────────
  31882. 
  31883.  i                          Decimal, hexadecimal, or   Pointer to int
  31884.                             octal integer
  31885.  
  31886.  u                          Unsigned decimal integer   Pointer to unsigned int
  31887.  
  31888.  U                          Unsigned decimal integer   Pointer to unsigned lon
  31889.  
  31890.  e, E                       Floating-point value       Pointer to float
  31891.  f                          consisting of an optional
  31892.  g, G                       sign (+ or -), a series
  31893.                             of one or more decimal
  31894.                             digits containing a
  31895.                             decimal point, and an
  31896.                             optional exponent ("e" or
  31897.                             "E") followed by an
  31898.                             optionally signed integer
  31899.                             value.
  31900.  
  31901.  Character                  Type of Input Expected     Type of Argument
  31902.  ─────────────────────────────────────────────────────────────────────────────
  31903. 
  31904.  c                          Character. White-space     Pointer to char
  31905.                             characters that are
  31906.                             ordinarily skipped are
  31907.                             read when c is specified;
  31908.                             to read the next
  31909.                             non-white-space
  31910.                             character, use  %1s.
  31911.  
  31912.  s                          String                     Pointer to character
  31913.                                                        array large enough for
  31914.                                                        input field plus a
  31915.                                                        terminating null
  31916.                                                        character ('\0'), which
  31917.                                                        is automatically
  31918.                                                        appended.
  31919.  
  31920.  n                          No input read from stream  Pointer to int, into
  31921.                             or buffer.                 which is stored the
  31922.  Character                  Type of Input Expected     Type of Argument
  31923.  ─────────────────────────────────────────────────────────────────────────────
  31924.                            or buffer.                 which is stored the
  31925.                                                        number of characters
  31926.                                                        successfully read from
  31927.                                                        the stream or buffer up
  31928.                                                        to that point in the
  31929.                                                        current call to scanf.
  31930.  
  31931.  p                          Value in the form xxxx:    Pointer to far pointer
  31932.                             yyyy, where the digits x   void
  31933.                             and y are uppercase
  31934.                             hexadecimal digits.
  31935.  
  31936.  ─────────────────────────────────────────────────────────────────────────────
  31937.  
  31938.  
  31939.  
  31940.  1     Since the input for a %x format specifier is always interpreted as a
  31941.  hexadecimal number, the input should     not include a leading 0x. (If 0x is
  31942.  included, the 0 is interpreted as a hexadecimal input value.)
  31943.  
  31944.  
  31945.  
  31946.  Return Value
  31947.  
  31948.  The scanf function returns the number of fields that were successfully
  31949.  converted and assigned. The return value may be less than the number
  31950.  requested in the call to scanf. The return value does not include fields
  31951.  that were read but not assigned.
  31952.  
  31953.  The return value is EOF if the end-of-file or end-of-string is encountered
  31954.  in the first attempt to read a character.
  31955.  
  31956.  
  31957.  Compatibility
  31958.  
  31959.   ANSI   DOS   OS/2   UNIX   XENIX
  31960.  
  31961.  
  31962.  
  31963.  See Also
  31964.  
  31965.  fscanf, printf, sscanf, vfprintf, vprintf, vsprintf
  31966.  
  31967.  
  31968.  Example
  31969.  
  31970.    /* SCANF.C: This program receives formatted input using scanf. */
  31971.    #include <stdio.h>
  31972.  
  31973.    void main()
  31974.    {
  31975.       int   i;
  31976.       float fp;
  31977.       char  c, s[81];
  31978.       int   result;
  31979.  
  31980.       printf( "Enter an integer, a floating-point number, "
  31981.               "a character and a string:\n" );
  31982.       result = scanf( "%d %f %c %s", &i, &fp, &c, s );
  31983.  
  31984.       printf( "\nThe number of fields input is %d\n", result );
  31985.       printf( "The contents are: %d %f %c %s\n", i, fp, c, s );
  31986.    }
  31987.  
  31988.  
  31989.  Output
  31990.  
  31991.  
  31992.  
  31993.    Enter an integer, a floating-point number, a character and a string:
  31994.    71
  31995.    98.6
  31996.    h
  31997.    White space stops input
  31998.  
  31999.    The number of fields input is 4
  32000.    The contents are: 71 98.599998 h White
  32001.  
  32002.  
  32003.  
  32004.  
  32005.  
  32006.  _scrolltextwindow
  32007.  ────────────────────────────────────────────────────────────────────────────
  32008.  
  32009.  
  32010.  Description
  32011.  
  32012.  Scrolls the text in a text window.
  32013.  
  32014.    #include <graph.h>
  32015.  
  32016.    void _far _scrolltextwindow( short lines );
  32017.  
  32018.  lines                             Number of lines to scroll
  32019.  
  32020.  
  32021.  Remarks
  32022.  
  32023.  The _scrolltextwindow function scrolls the text in a text window (previously
  32024.  defined by the _settextwindow function). The lines argument specifies the
  32025.  number of lines to scroll. A positive value of lines scrolls the window up
  32026.  (the usual direction); a negative value scrolls the window down. Specifying
  32027.  a number larger than the height of the current text window is equivalent to
  32028.  calling _clearscreen( _GWINDOW ). A value of 0 for lines has no effect on
  32029.  the text.
  32030.  
  32031.  
  32032.  Return Value
  32033.  
  32034.  None.
  32035.  
  32036.  
  32037.  Compatibility
  32038.  
  32039.   ANSI   DOS   OS/2   UNIX   XENIX
  32040.  
  32041.  
  32042.  
  32043.  See Also
  32044.  
  32045.  _gettextposition,  _outmem,  _outtext,  _settextposition,  _settextwindow
  32046.  
  32047.  
  32048.  Example
  32049.  
  32050.    /* SCRTXWIN.C: This program displays text in text windows and then
  32051.     * scrolls, inserts, and deletes lines.
  32052.     */
  32053.  
  32054.    #include <stdio.h>
  32055.    #include <conio.h>
  32056.    #include <graph.h>
  32057.  
  32058.    void deleteline( void );
  32059.    void insertline( void );
  32060.    void status( char *msg );
  32061.  
  32062.    void main()
  32063.    {
  32064.       short row;
  32065.       char  buf[40];
  32066.  
  32067.       /* Set up screen for scrolling, and put text window around scroll area.
  32068.  */
  32069.       _settextrows( 25 );
  32070.       _clearscreen( _GCLEARSCREEN );
  32071.  
  32072.    for( row = 1; row <= 25; row++ )
  32073.       {
  32074.          _settextposition( row, 1 );
  32075.          sprintf( buf, "Line %c             %2d", row + 'A' - 1, row );
  32076.          _outtext( buf );
  32077.       }
  32078.       getch();
  32079.       _settextwindow( 1, 1, 25, 10 );
  32080.  
  32081.       /* Delete some lines. */
  32082.       _settextposition( 11, 1 );
  32083.       for( row = 12; row < 20; row++ )
  32084.          deleteline();
  32085.       status( "Deleted 8 lines" );
  32086.  
  32087.       /* Insert some lines. */
  32088.       _settextposition( 5, 1 );
  32089.       for( row = 1; row < 6; row++ )
  32090.          insertline();
  32091.       status( "Inserted 5 lines" );
  32092.  
  32093.       /* Scroll up and down. */
  32094.       _scrolltextwindow( -7 );
  32095.       status( "Scrolled down 7 lines" );
  32096.       _scrolltextwindow( 5 );
  32097.       status( "Scrolled up 5 lines" );
  32098.       _setvideomode( _DEFAULTMODE );
  32099.    }
  32100.  
  32101.    /* Delete lines by scrolling them off the top of the current text window.
  32102.     * Save and restore original window.
  32103.     */
  32104.    void deleteline()
  32105.    {
  32106.       short left, top, right, bottom;
  32107.       struct rccoord rc;
  32108.  
  32109.       _gettextwindow( &top, &left, &bottom, &right );
  32110.       rc = _gettextposition();
  32111.       _settextwindow( rc.row, left, bottom, right );
  32112.       _scrolltextwindow( _GSCROLLUP );
  32113.       _settextwindow( top, left, bottom, right );
  32114.       _settextposition( rc.row, rc.col );
  32115.    }
  32116.  
  32117.    /* Insert some lines by scrolling in blank lines from the top of the
  32118.     * current text window. Save and restore original window.
  32119.     */
  32120.    void insertline()
  32121.    {
  32122.       short left, top, right, bottom;
  32123.       struct rccoord rc;
  32124.  
  32125.       _gettextwindow( &top, &left, &bottom, &right );
  32126.       rc = _gettextposition();
  32127.       _settextwindow( rc.row, left, bottom, right );
  32128.       _scrolltextwindow( _GSCROLLDOWN );
  32129.       _settextwindow( top, left, bottom, right );
  32130.       _settextposition( rc.row, rc.col );
  32131.    }
  32132.  
  32133.    /* Display and clear status in its own window. */
  32134.    void status( char *msg )
  32135.    {
  32136.       short left, top, right, bottom;
  32137.       struct rccoord rc;
  32138.  
  32139.       _gettextwindow( &top, &left, &bottom, &right );
  32140.       _settextwindow( 1, 50, 2, 80 );
  32141.       _outtext( msg );
  32142.       getch();
  32143.       _clearscreen( _GWINDOW );
  32144.       _settextwindow( top, left, bottom, right );
  32145.    }
  32146.  
  32147.  
  32148.  
  32149.  
  32150.  
  32151.  _searchenv
  32152.  ────────────────────────────────────────────────────────────────────────────
  32153.  
  32154.  
  32155.  Description
  32156.  
  32157.  Searches for a file using environment paths.
  32158.  
  32159.    #include <stdlib.h>
  32160.  
  32161.    void _searchenv( char *filename, char *varname, char *pathname );
  32162.  
  32163.  filename                          Name of file to search for
  32164.  
  32165.  varname                           Environment to search
  32166.  
  32167.  pathname                          Buffer to store complete path
  32168.  
  32169.  
  32170.  Remarks
  32171.  
  32172.  The _searchenv routine searches for the target file in the specified domain.
  32173.  The varname variable can be any environment variable which specifies a list
  32174.  of directory paths, such as PATH, LIB, INCLUDE, or other user-defined
  32175.  variables. It is most often PATH, which searches for filename on all paths
  32176.  specified in the PATH variable. The _searchenv function is case-sensitive,
  32177.  so the varname variable should match the case of the environment variable.
  32178.  
  32179.  The routine first searches for the file in the current working directory. If
  32180.  it doesn't find the file, it next looks through the directories specified by
  32181.  the environment variable.
  32182.  
  32183.  If the target file is found in one of the directories, the newly created
  32184.  path is copied into the buffer pointed to by pathname. You must ensure that
  32185.  there is sufficient space for the constructed path name. If the filename
  32186.  file is not found, pathname will contain an empty null-terminated string.
  32187.  
  32188.  
  32189.  Return Value
  32190.  
  32191.  The _searchenv function does not return a value.
  32192.  
  32193.  
  32194.  Compatibility
  32195.  
  32196.   ANSI   DOS   OS/2   UNIX   XENIX
  32197.  
  32198.  
  32199.  
  32200.  See Also
  32201.  
  32202.  getenv, putenv
  32203.  
  32204.  
  32205.  Example
  32206.  
  32207.    /* SEARCHEN.C: This program searches for a file in a directory
  32208.     * specified by an environment variable.
  32209.     */
  32210.  
  32211.    #include <stdlib.h>
  32212.    #include <stdio.h>
  32213.  
  32214.    void main()
  32215.    {
  32216.       char pathbuffer[_MAX_PATH];
  32217.       char searchfile[] = "CL.EXE";
  32218.       char envvar[] = "PATH";
  32219.       /* Search for file in PATH environment variable: */
  32220.       _searchenv( searchfile, envvar, pathbuffer );
  32221.       if( *pathbuffer != '\0' )
  32222.          printf( "Path for %s: %s\n", searchfile, pathbuffer );
  32223.       else
  32224.          printf( "%s not found\n", searchfile );
  32225.    }
  32226.  
  32227.  
  32228.  Output
  32229.  
  32230.  
  32231.  
  32232.    Path for CL.EXE: C:\BIN\CL.EXE
  32233.  
  32234.  
  32235.  
  32236.  
  32237.  
  32238.  segread
  32239.  ────────────────────────────────────────────────────────────────────────────
  32240.  
  32241.  
  32242.  Description
  32243.  
  32244.  Gets the current values of segment registers.
  32245.  
  32246.    #include <dos.h>
  32247.  
  32248.    void segread( struct SREGS *segregs );
  32249.  
  32250.  segregs                           Segment-register values
  32251.  
  32252.  
  32253.  Remarks
  32254.  
  32255.  The segread function fills the structure pointed to by segregs with the
  32256.  current contents of the segment registers. The SREGS union is described in
  32257.  the reference section for int86x. This function is intended to be used with
  32258.  the intdosx and int86x functions to retrieve segment-register values for
  32259.  later use.
  32260.  
  32261.  
  32262.  Return Value
  32263.  
  32264.  None.
  32265.  
  32266.  
  32267.  Compatibility
  32268.  
  32269.   ANSI   DOS   OS/2   UNIX   XENIX
  32270.  
  32271.  
  32272.  
  32273.  See Also
  32274.  
  32275.  FP_SEG, intdosx, int86x
  32276.  
  32277.  
  32278.  Example
  32279.  
  32280.    /* SEGREAD.C: This program gets the current segment values with segread.
  32281.    */
  32282.  
  32283.    #include <dos.h>
  32284.    #include <stdio.h>
  32285.  
  32286.    void main()
  32287.    {
  32288.       struct   SREGS segregs;
  32289.       unsigned cs, ds, es, ss;
  32290.  
  32291.       /* Read the segment register values */
  32292.       segread( &segregs );
  32293.       cs = segregs.cs;
  32294.       ds = segregs.ds;
  32295.       es = segregs.es;
  32296.       ss = segregs.ss;
  32297.       printf( "CS = 0x%.4x    DS = 0x%.4x    ES = 0x%.4x    SS = 0x%.4x\n",
  32298.               cs, ds, es, ss );
  32299.    }
  32300.  
  32301.  
  32302.  Output
  32303.  
  32304.  
  32305.  
  32306.    CS = 0x0047    DS = 0x0067    ES = 0x0067    SS = 0x0067
  32307.  
  32308.    CS = 0x2bcc    DS = 0x2ce8    ES = 0x2ba3    SS = 0x2ce8
  32309.  
  32310.  
  32311.  
  32312.  
  32313.  
  32314.  _selectpalette
  32315.  ────────────────────────────────────────────────────────────────────────────
  32316.  
  32317.  
  32318.  Description
  32319.  
  32320.  Selects a graphics palette.
  32321.  
  32322.    #include <graph.h>
  32323.  
  32324.    short _far _selectpalette( short number );
  32325.  
  32326.  number                            Palette number
  32327.  
  32328.  
  32329.  Remarks
  32330.  
  32331.  The _selectpalette function works only under the video modes _MRES4COLOR and
  32332.  _MRESNOCOLOR. A palette consists of a selectable background color (Color 0)
  32333.  and three set colors. Under the _MRES4COLOR mode, the number argument
  32334.  selects one of the four predefined palettes shown in Table R.6.
  32335.  
  32336.  Table   R.6 _MRES4COLOR Palette Colors
  32337.  
  32338. ╓┌───────────────┌─────────────┌───────────────┌─────────────────────────────╖
  32339.  ────────────────────────────────────────────────────────────────────────────
  32340.  
  32341.  Palette Number  Color 1       Color 2         Color 3
  32342.  ────────────────────────────────────────────────────────────────────────────
  32343. Palette Number  Color 1       Color 2         Color 3
  32344.  
  32345.  0               Green         Red             Brown
  32346.  
  32347.  1               Cyan          Magenta         Light gray
  32348.  
  32349.  2               Light green   Light red       Yellow
  32350.  
  32351.  3               Light cyan    Light magenta   White
  32352.  
  32353.  ────────────────────────────────────────────────────────────────────────────
  32354.  
  32355.  
  32356.  
  32357.  The _MRESNOCOLOR video mode is used with black-and-white displays, producing
  32358.  palettes consisting of various shades of gray. It will also produce color
  32359.  when used with a color display. The number of palettes available depends
  32360.  upon whether a CGA or EGA hardware package is employed. Under a CGA
  32361.  configuration, only the two palettes shown in Table R.7 are available.
  32362.  
  32363.  Table   R.7 _MRESNOCOLOR Mode CGA Palette Colors
  32364.  
  32365. ╓┌───────────────┌─────────────┌──────────────┌──────────────────────────────╖
  32366.  ────────────────────────────────────────────────────────────────────────────
  32367.  
  32368.  Palette         Color 1       Color 2        Color 3
  32369.  Number
  32370.  
  32371.  0               Blue          Red            Light gray
  32372.  
  32373.  1               Light blue    Light red      White
  32374.  
  32375.  ────────────────────────────────────────────────────────────────────────────
  32376.  
  32377.  
  32378.  
  32379.  Under the EGA configuration, the three palettes shown in Table R.8 are
  32380.  available in the _MRESNOCOLOR video mode.
  32381.  
  32382.  Table   R.8 _MRESNOCOLOR Mode EGA Palette Colors
  32383.  
  32384. ╓┌───────────────┌─────────────┌─────────────┌───────────────────────────────╖
  32385.  ────────────────────────────────────────────────────────────────────────────
  32386.  
  32387.  Palette         Color 1       Color 2       Color 3
  32388.  Number
  32389.  
  32390.  0               Green         Red           Brown
  32391.  
  32392.  1               Light green   Light red     Yellow
  32393.  
  32394.  2               Light cyan    Light red     Yellow
  32395.  
  32396.  ────────────────────────────────────────────────────────────────────────────
  32397.  
  32398.  
  32399.  
  32400.  Note that with an EGA in _MRESNOCOLOR video mode, Palette 3 is identical to
  32401.  Palette 1.
  32402.  
  32403.  
  32404.  Return Value
  32405.  
  32406.  The function returns the value of the previous palette. There is no error
  32407.  return.
  32408.  
  32409.  
  32410.  Compatibility
  32411.  
  32412.   ANSI   DOS   OS/2   UNIX   XENIX
  32413.  
  32414.  
  32415.  
  32416.  See Also
  32417.  
  32418.  _getvideoconfig,  _setbkcolor,  _setvideomode
  32419.  
  32420.  
  32421.  Example
  32422.  
  32423.    /* SELPAL.C: This program changes the current CGA palette. */
  32424.  
  32425.    #include <stdio.h>
  32426.    #include <stdlib.h>
  32427.    #include <conio.h>
  32428.    #include <graph.h>
  32429.  
  32430.    long bkcolor[8] = { _BLACK,  _BLUE,     _GREEN,  _CYAN,
  32431.                        _RED,    _MAGENTA,  _BROWN,  _WHITE };
  32432.    char *bkname [] = { "BLACK", "BLUE",    "GREEN", "CYAN",
  32433.                        "RED",   "MAGENTA", "BROWN", "WHITE" };
  32434.    void main()
  32435.    {
  32436.       int i, j, k;
  32437.  
  32438.       if ( !_setvideomode( _MRES4COLOR ) )
  32439.       {
  32440.          printf( "No palettes available" );
  32441.          exit( 1 );
  32442.       }
  32443.       for( i = 0; i < 4; i++ )                    /* Palette loop          */
  32444.       {
  32445.           _selectpalette( i );
  32446.           for( k = 0; k < 8; k++ )                /* Background color loop */
  32447.           {
  32448.               _clearscreen( _GCLEARSCREEN );
  32449.               _setbkcolor( bkcolor[k] );
  32450.               _settextposition( 1, 1 );
  32451.               printf( "Background: %s\tPalette: %d", bkname[k], i );
  32452.               for( j = 1; j < 4; j++ )            /* Foreground color loop */
  32453.               {
  32454.                   _setcolor( j );
  32455.                   _ellipse( _GFILLINTERIOR, 100, j * 30, 220, 80 + (j * 30)
  32456.  );
  32457.               }
  32458.               getch();
  32459.           }
  32460.       }
  32461.       _setvideomode( _DEFAULTMODE );
  32462.    }
  32463.  
  32464.  
  32465.  
  32466.  
  32467.  
  32468.  _setactivepage
  32469.  ────────────────────────────────────────────────────────────────────────────
  32470.  
  32471.  
  32472.  Description
  32473.  
  32474.  Sets the active page.
  32475.  
  32476.    #include <graph.h>
  32477.  
  32478.    short _far _setactivepage( short  page );
  32479.  
  32480.  page                              Memory page number
  32481.  
  32482.  
  32483.  Remarks
  32484.  
  32485.  For hardware and mode configurations with enough memory to support multiple
  32486.  screen pages,  _setactivepage specifies the area in memory in which graphics
  32487.  output is written. The page argument selects the current active page. The
  32488.  default page number is 0.
  32489.  
  32490.  Screen animation can be done by alternating the graphics pages displayed.
  32491.  Use the _setvisualpage function to display a completed graphics page while
  32492.  executing graphics statements in another active page.
  32493.  
  32494.  These functions can also be used to control text output if you use the text
  32495.  functions _gettextcursor,  _settextcursor,  _outtext,  _settextposition,
  32496.  _gettextposition,  _settextcolor,  _gettextcolor,  _settextwindow, and
  32497.  _wrapon instead of the standard C-language I/O functions.
  32498.  
  32499.  The CGA hardware configuration has only 16K of RAM available to support
  32500.  multiple video pages, and only in the text mode. The EGA and VGA
  32501.  configurations may be equipped with up to 256K of RAM for multiple video
  32502.  pages in graphics mode.
  32503.  
  32504.  
  32505.  Return Value
  32506.  
  32507.  If successful, the function returns the page number of the previous active
  32508.  page. If the function fails, it returns a negative value.
  32509.  
  32510.  
  32511.  Compatibility
  32512.  
  32513.   ANSI   DOS   OS/2   UNIX   XENIX
  32514.  
  32515.  
  32516.  
  32517.  See Also
  32518.  
  32519.  _getactivepage,  _getvisualpage,  _setvisualpage
  32520.  
  32521.  
  32522.  Example
  32523.  
  32524.    /* PAGE.C illustrates video page functions including:
  32525.     *      _getactivepage  _getvisualpage  _setactivepage  _setvisualpage
  32526.     */
  32527.  
  32528.    #include <conio.h>
  32529.    #include <graph.h>
  32530.    #include <stdlib.h>
  32531.  
  32532.    void main()
  32533.    {
  32534.       short  oldvpage, oldapage, page, row, col, line;
  32535.       struct videoconfig vc;
  32536.       char   buf[80];
  32537.  
  32538.       _getvideoconfig( &vc );
  32539.       if( vc.numvideopages < 4 )
  32540.           exit( 1 );              /* Fail for OS/2 or monochrome */
  32541.       oldapage  = _getactivepage();
  32542.       oldvpage  = _getvisualpage();
  32543.       _displaycursor( _GCURSOROFF );
  32544.  
  32545.       /* Draw arrows in different place on each page. */
  32546.       for( page = 1; page < 4; page++ )
  32547.       {
  32548.          _setactivepage( page );
  32549.          _settextposition( 12, 16 * page );
  32550.          _outtext( ">>>>>>>>" );
  32551.       }
  32552.  
  32553.       while( !kbhit() )
  32554.          /* Cycle through pages 1 to 3 to show moving image. */
  32555.          for( page = 1; page < 4; page++ )
  32556.              _setvisualpage( page );
  32557.       getch();
  32558.  
  32559.       /* Restore original page (normally 0) to restore screen. */
  32560.       _setactivepage( oldapage );
  32561.       _setvisualpage( oldvpage );
  32562.       _displaycursor( _GCURSORON );
  32563.    }
  32564.  
  32565.  
  32566.  
  32567.  
  32568.  
  32569.  _setbkcolor
  32570.  ────────────────────────────────────────────────────────────────────────────
  32571.  
  32572.  
  32573.  Description
  32574.  
  32575.  Sets the current background color.
  32576.  
  32577.    #include <graph.h>
  32578.  
  32579.    long _far _setbkcolor( long color );
  32580.  
  32581.  color                             Desired color
  32582.  
  32583.  
  32584.  Remarks
  32585.  
  32586.  The _setbkcolor function sets the current background color to the color
  32587.  value color.
  32588.  
  32589.  In a color text mode (such as _TEXTC80), _setbkcolor accepts (and
  32590.  _getbkcolor returns) a color index. The value for the default colors is
  32591.  given in a table in the description of the _settextcolor function. For
  32592.  example, _setbkcolor(2L) sets the background color to color index 2. The
  32593.  actual color displayed depends on the palette mapping for color index 2. The
  32594.  default is green in a color text mode.
  32595.  
  32596.  In a color graphics mode (such as _ERESCOLOR), _setbkcolor accepts (and
  32597.  _getbkcolor returns) a color value. The value for the background color is
  32598.  given by the manifest constants defined in the GRAPH.H include file. For
  32599.  example, _setbkcolor( _GREEN) sets the background color in a graphics mode
  32600.  to green. These manifest constants are provided as a convenience in defining
  32601.  and manipulating the most common colors. The actual range of colors is, in
  32602.  general, much greater.
  32603.  
  32604.  In general, whenever an argument is long, it refers to a color value, and
  32605.  whenever it is short, it refers to a color index. The two exceptions are
  32606.  _setbkcolor and _getbkcolor.
  32607.  
  32608.  Since the background color is color index 0, the _remappalette function will
  32609.  act identically to the _setbkcolor function. Unlike _remappalette, however,
  32610.  _setbkcolor does not require an EGA or VGA environment.
  32611.  
  32612.  In a text mode, the _setbkcolor function does not affect anything already
  32613.  appearing on the display; only the subsequent output is affected. In a
  32614.  graphics mode, it immediately changes all background pixels.
  32615.  
  32616.  
  32617.  Return Value
  32618.  
  32619.  In text modes, _setbkcolor returns the color index of the old background
  32620.  color. In graphics modes, _setbkcolor returns the old color value of color
  32621.  index 0. There is no error return.
  32622.  
  32623.  
  32624.  Compatibility
  32625.  
  32626.   ANSI   DOS   OS/2   UNIX   XENIX
  32627.  
  32628.  
  32629.  
  32630.  See Also
  32631.  
  32632.  _getbkcolor,  _remappalette,  _selectpalette
  32633.  
  32634.  
  32635.  Example
  32636.  
  32637.  See the example for _getcolor.
  32638.  
  32639.  
  32640.  
  32641.  setbuf
  32642.  ────────────────────────────────────────────────────────────────────────────
  32643.  
  32644.  
  32645.  Description
  32646.  
  32647.  Controls stream buffering.
  32648.  
  32649.    #include <stdio.h>
  32650.  
  32651.    void setbuf( FILE *stream, char *buffer );
  32652.  
  32653.  stream                            Pointer to FILE structure
  32654.  
  32655.  buffer                            User-allocated buffer
  32656.  
  32657.  
  32658.  Remarks
  32659.  
  32660.  The setbuf function allows the user to control buffering for stream. The
  32661.  stream argument must refer to an open file that has not been read or
  32662.  written. If the buffer argument is NULL, the stream is unbuffered. If not,
  32663.  the buffer must point to a character array of length BUFSIZ, where BUFSIZ is
  32664.  the buffer size as defined in STDIO.H. The user-specified buffer, instead of
  32665.  the default system-allocated buffer for the given stream, is used for I/O
  32666.  buffering.
  32667.  
  32668.  The stderr and (in DOS only) stdaux streams are unbuffered by default, but
  32669.  may be assigned buffers with setbuf.
  32670.  
  32671.  The setbuf function has been subsumed by the setvbuf function, which should
  32672.  be the preferred routine for new code. The setbuf function is retained for
  32673.  compatibility with existing code.
  32674.  
  32675.  
  32676.  Return Value
  32677.  
  32678.  None.
  32679.  
  32680.  
  32681.  Compatibility
  32682.  
  32683.   ANSI   DOS   OS/2   UNIX   XENIX
  32684.  
  32685.  
  32686.  
  32687.  See Also
  32688.  
  32689.  fclose, fflush, fopen, setvbuf
  32690.  
  32691.  
  32692.  Example
  32693.  
  32694.    /* SETBUF.C: This program first opens files named DATA1 and DATA2.
  32695.     * Then it uses setbuf to give DATA1 a user-assigned buffer
  32696.     * and to change DATA2 so that it has no buffer.
  32697.     */
  32698.  
  32699.    #include <stdio.h>
  32700.  
  32701.    void main()
  32702.    {
  32703.       char buf[BUFSIZ];
  32704.       FILE *stream1, *stream2;
  32705.  
  32706.       if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
  32707.           ((stream2 = fopen( "data2", "w" )) != NULL) )
  32708.       {
  32709.          /* "stream1" uses user-assigned buffer: */
  32710.          setbuf( stream1, buf );
  32711.          printf( "stream1 set to user-defined buffer at: %Fp\n", buf );
  32712.  
  32713.          /* "stream2" is unbuffered              */
  32714.          setbuf( stream2, NULL );
  32715.          printf( "stream2 buffering disabled\n" );
  32716.          fcloseall();
  32717.       }
  32718.  
  32719.  
  32720.  Output
  32721.  
  32722.  
  32723.  
  32724.    stream1 set to user-defined buffer at: 0298:0DF2
  32725.    stream2 buffering disabled
  32726.  
  32727.  
  32728.  
  32729.  
  32730.  
  32731.  _setcliprgn
  32732.  ────────────────────────────────────────────────────────────────────────────
  32733.  
  32734.  
  32735.  Description
  32736.  
  32737.  Sets the clipping region for graphics.
  32738.  
  32739.    #include <graph.h>
  32740.  
  32741.    void _far _setcliprgn( short x1, short y1, short x2, short y2 );
  32742.  
  32743.  x1, y1                            Upper-left corner of clip region
  32744.  
  32745.  x2, y2                            Lower-right corner of clip region
  32746.  
  32747.  
  32748.  Remarks
  32749.  
  32750.  The _setcliprgn function limits the display of subsequent graphics output
  32751.  and font text output to an area of the screen called the "clipping region."
  32752.  The physical points (x1, y1) and (x2, y2) are the diagonally opposed sides
  32753.  of a rectangle that defines the clipping region. This function does not
  32754.  change the view coordinate system. Rather, it merely masks the screen.
  32755.  
  32756.  Note that the _setcliprgn function affects graphics and font text output
  32757.  only. To mask the screen for text output, use the _settextwindow function.
  32758.  
  32759.  
  32760.  Return Value
  32761.  
  32762.  None.
  32763.  
  32764.  
  32765.  Compatibility
  32766.  
  32767.   ANSI   DOS   OS/2   UNIX   XENIX
  32768.  
  32769.  
  32770.  
  32771.  See Also
  32772.  
  32773.  _settextwindow,  _setvieworg,  _setviewport,  _setwindow
  32774.  
  32775.  
  32776.  Example
  32777.  
  32778.    /* SCLIPRGN.C */
  32779.    #include <stdlib.h>
  32780.    #include <conio.h>
  32781.    #include <graph.h>
  32782.  
  32783.    void main()
  32784.    {
  32785.       /* Find a valid graphics mode. */
  32786.       if( !_setvideomode( _MAXRESMODE ) )
  32787.          exit( 1 );
  32788.  
  32789.       /* Set clip region, then draw and ellipse larger than the region. */
  32790.       _setcliprgn( 0, 0, 200, 125 );
  32791.       _ellipse( _GFILLINTERIOR, 80, 50, 240, 190 );
  32792.  
  32793.    getch();
  32794.       _setvideomode( _DEFAULTMODE );
  32795.    }
  32796.  
  32797.  
  32798.  
  32799.  
  32800.  
  32801.  _setcolor
  32802.  ────────────────────────────────────────────────────────────────────────────
  32803.  
  32804.  
  32805.  Description
  32806.  
  32807.  Sets the current color.
  32808.  
  32809.    #include <graph.h>
  32810.  
  32811.    short _far _setcolor( short color );
  32812.  
  32813.  color                             Desired color index
  32814.  
  32815.  
  32816.  Remarks
  32817.  
  32818.  The _setcolor function sets the current color index to color. The color
  32819.  parameter is masked but always within range. The following graphics
  32820.  functions use the current color:  _arc,  _ellipse,  _floodfill,  _lineto,
  32821.  _outgtext,  _pie,  _rectangle, and  _setpixel.
  32822.  
  32823.  The _setcolor function accepts an int value as an argument. It is a color
  32824.  index.
  32825.  
  32826.  The default color index is the highest numbered color index in the current
  32827.  palette.
  32828.  
  32829.  Note that the _setcolor function does not affect the output of the
  32830.  presentation-graphics functions.
  32831.  
  32832.  
  32833.  Return Value
  32834.  
  32835.  This function returns the previous color. If the function fails (e.g., if
  32836.  used in a text mode), it returns  -1.
  32837.  
  32838.  
  32839.  Compatibility
  32840.  
  32841.   ANSI   DOS   OS/2   UNIX   XENIX
  32842.  
  32843.  
  32844.  
  32845.  See Also
  32846.  
  32847.  _arc functions,  _ellipse functions,  _floodfill,  _getcolor,  _lineto
  32848.  functions,  _outgtext,  _pie functions,  _rectangle functions,
  32849.  _selectpalette,  _setpixel functions
  32850.  
  32851.  
  32852.  Example
  32853.  
  32854.    /* GPIXEL.C: This program assigns different colors to randomly
  32855.     * selected pixels.
  32856.     */
  32857.  
  32858.    #include <conio.h>
  32859.    #include <stdlib.h>
  32860.    #include <graph.h>
  32861.  
  32862.    void main()
  32863.    {
  32864.       short xvar, yvar;
  32865.       struct videoconfig vc;
  32866.  
  32867.    /* Find a valid graphics mode. */
  32868.       if( !_setvideomode( _MAXCOLORMODE ) )
  32869.          exit( 1 );
  32870.       _getvideoconfig( &vc );
  32871.  
  32872.       /* Draw filled ellipse to turn on certain pixels. */
  32873.       _ellipse( _GFILLINTERIOR, vc.numxpixels / 6, vc.numypixels / 6,
  32874.                                 vc.numxpixels / 6 * 5, vc.numypixels / 6 * 5
  32875.                                               );
  32876.  
  32877.       /* Draw random pixels in random colors... */
  32878.       while( !kbhit() )
  32879.       {
  32880.          /* ...but only if they are already on (inside the ellipse). */
  32881.          xvar = rand() % vc.numxpixels;
  32882.          yvar = rand() % vc.numypixels;
  32883.          if( _getpixel( xvar, yvar ) != 0 )
  32884.          {
  32885.             _setcolor( rand() % 16 );
  32886.             _setpixel( xvar, yvar );
  32887.          }
  32888.       }
  32889.  
  32890.       getch();          /* Throw away the keystroke. */
  32891.       _setvideomode( _DEFAULTMODE );
  32892.    }
  32893.  
  32894.  
  32895.  
  32896.  
  32897.  
  32898.  _setfillmask
  32899.  ────────────────────────────────────────────────────────────────────────────
  32900.  
  32901.  
  32902.  Description
  32903.  
  32904.  Sets the fill mask.
  32905.  
  32906.    #include <graph.h>
  32907.  
  32908.    void _far _setfillmask( unsigned char _far *mask );
  32909.  
  32910.  mask                              Mask array
  32911.  
  32912.  
  32913.  Remarks
  32914.  
  32915.  The _setfillmask function sets the current fill mask, which determines the
  32916.  fill pattern. The mask is an 8-by-8 array of bits in which each bit
  32917.  represents a pixel. A 1 bit sets the corresponding pixel to the current
  32918.  color, while a 0 bit leaves the pixel unchanged. The pattern is repeated
  32919.  over the entire fill area.
  32920.  
  32921.  If no fill mask is set (mask is NULL─the default), only the current color is
  32922.  used in fill operations.
  32923.  
  32924.  
  32925.  Return Value
  32926.  
  32927.  None.
  32928.  
  32929.  
  32930.  Compatibility
  32931.  
  32932.   ANSI   DOS   OS/2   UNIX   XENIX
  32933.  
  32934.  
  32935.  
  32936.  See Also
  32937.  
  32938.  _ellipse functions,  _floodfill,  _getfillmask,  _pie functions,  _rectangle
  32939.  functions
  32940.  
  32941.  
  32942.  Example
  32943.  
  32944.    /* GFILLMSK.C: This program illustrates _getfillmask and _setfillmask. */
  32945.  
  32946.    #include <conio.h>
  32947.    #include <stdlib.h>
  32948.    #include <graph.h>
  32949.  
  32950.    void ellipsemask( short x1, short y1, short x2, short y2, char _far
  32951.  *newmask );
  32952.  
  32953.    unsigned char mask1[8] = { 0x43, 0x23, 0x7c, 0xf7, 0x8a, 0x4d, 0x78, 0x39
  32954.  };
  32955.    unsigned char mask2[8] = { 0x18, 0xad, 0xc0, 0x79, 0xf6, 0xc4, 0xa8, 0x23
  32956.  };
  32957.    char oldmask[8];
  32958.  
  32959.    void main()
  32960.    {
  32961.       int loop;
  32962.  
  32963.       /* Find a valid graphics mode. */
  32964.       if( !_setvideomode( _MAXRESMODE ) )
  32965.          exit( 1 );
  32966.  
  32967.       /* Set first fill mask and draw rectangle. */
  32968.       _setfillmask( mask1 );
  32969.       _rectangle( _GFILLINTERIOR, 20, 20, 100, 100 );
  32970.       getch();
  32971.  
  32972.       /* Call routine that saves and restores mask. */
  32973.       ellipsemask( 60, 60, 150, 150, mask2 );
  32974.       getch();
  32975.  
  32976.       /* Back to original mask. */
  32977.       _rectangle( _GFILLINTERIOR, 120, 120, 190, 190 );
  32978.       getch();
  32979.  
  32980.       _setvideomode( _DEFAULTMODE );
  32981.    }
  32982.  
  32983.    /* Draw an ellipse with a specified fill mask. */
  32984.    void ellipsemask( short x1, short y1, short x2, short y2, char _far
  32985.  *newmask )
  32986.    {
  32987.       unsigned char savemask[8];
  32988.  
  32989.       _getfillmask( savemask );                    /* Save mask         */
  32990.       _setfillmask( newmask );                     /* Set new mask      */
  32991.       _ellipse( _GFILLINTERIOR, x1, y1, x2, y2 );  /* Use new mask      */
  32992.       _setfillmask( savemask );                    /* Restore original  */
  32993.    }
  32994.  
  32995.  
  32996.  
  32997.  
  32998.  
  32999.  
  33000.  _setfont
  33001.  ────────────────────────────────────────────────────────────────────────────
  33002.  
  33003.  
  33004.  Description
  33005.  
  33006.  Finds a single font.
  33007.  
  33008.    #include <graph.h>
  33009.  
  33010.    short _far _setfont( unsigned char _far *options );
  33011.  
  33012.  options                           String describing font characteristics
  33013.  
  33014.  
  33015.  Remarks
  33016.  
  33017.  The _setfont function finds a single font, from the set of registered fonts,
  33018.  that has the characteristics specified by the options string. If a font is
  33019.  found, it is made the current font. The current font is used in all
  33020.  subsequent calls to the _outgtext function. There can be only one active
  33021.  font at any time.
  33022.  
  33023.  The options string is a set of characters that specifies the desired
  33024.  characteristics of the font. The _setfont function searches the list of
  33025.  registered fonts for a font matching the specified characteristics.
  33026.  
  33027.  The characteristics that may be specified in the options string are shown in
  33028.  the list below. Characteristics specified in the options string are not
  33029.  case- or position-sensitive.
  33030.  
  33031.  Characteristic                    Description
  33032.  ────────────────────────────────────────────────────────────────────────────
  33033.  t'fontname'                       Typeface.
  33034.  
  33035.  hx                                Character height, where x is the number
  33036.                                    of pixels.
  33037.  
  33038.  wy                                Character width, where y is the number
  33039.                                    of pixels.
  33040.  
  33041.  f                                 Find only a fixed-space font (should not
  33042.                                    be used with the
  33043.                                    p characteristic).
  33044.  
  33045.  p                                 Find only a proportionally spaced font
  33046.                                    (should not be used with the f
  33047.                                    characteristic).
  33048.  
  33049.  v                                 Find only a vector font (should not be
  33050.                                    used with the r
  33051.                                    characteristic).
  33052.  
  33053.  r                                 Find only a raster-mapped (bit-mapped)
  33054.                                    font (should not be used with the v
  33055.                                    characteristic).
  33056.  
  33057.  b                                 Select a best fit font.
  33058.  
  33059.  nx                                Select font number x, where x is less
  33060.                                    than or equal to the value returned by
  33061.                                    the _registerfonts function. Use this
  33062.                                    option to "step through" an entire set
  33063.                                    of fonts.
  33064.  
  33065.  You can request as many options as desired, except with nx, which should be
  33066.  used alone. If mutually exclusive options are requested (such as the pair
  33067.  f/p or r/v), the _setfont function ignores them. There is no error detection
  33068.  for incompatible parameters used with nx.
  33069.  
  33070.  Options can be separated by blanks in the options string. Any other
  33071.  character is ignored by _setfont.
  33072.  
  33073.  The t (the typeface specification) in options is specified as a "t" followed
  33074.  by fontname in single quotes. Choose fontname from the following list:
  33075.  
  33076.  Fontname                          Description
  33077.  ────────────────────────────────────────────────────────────────────────────
  33078.  Courier                           Fixed-width bit-mapped font with serifs
  33079.  
  33080.  Helv                              Sans serif proportional bit-mapped font
  33081.  
  33082.  Tms Rmn                           Proportional bit-mapped font with serifs
  33083.  
  33084.  Script                            Proportional vector-mapped font of
  33085.                                    slanted characters formed from nearly
  33086.                                    continuous lines
  33087.  
  33088.  Modern                            Proportional vector-mapped font without
  33089.                                    serifs
  33090.  
  33091.  Roman                             Proportional vector-mapped font with
  33092.                                    serifs
  33093.  
  33094.  A b in the options field causes the _setfont routine to automatically select
  33095.  the "best fit" font that matches the other characteristics you have
  33096.  specified. If the b parameter is spec-ified and at least one font is
  33097.  registered, _setfont will always be able to set a font and will return 0 to
  33098.  indicate success.
  33099.  
  33100.  In selecting a font, the _setfont routine uses the following precedence
  33101.  (rated from highest precedence to lowest):
  33102.  
  33103.  
  33104.    1.  Pixel height
  33105.  
  33106.    2.  Typeface
  33107.  
  33108.    3.  Pixel width
  33109.  
  33110.    4.  Fixed or proportional font
  33111.  
  33112.  
  33113.  You can also specify a pixel width and height for fonts. If a nonexistent
  33114.  value is chosen for either, and the b option is specified, the _setfont
  33115.  function will chose the closest match. A smaller font size has precedence
  33116.  over a larger size. If _setfont requests Helv 12 with best fit, and only
  33117.  Helv 10 and Helv 14 are available, _setfont will select Helv 10.
  33118.  
  33119.  If a nonexistent value is chosen for pixel height and width, the _setfont
  33120.  function will apply a magnification factor to a vector-mapped font to obtain
  33121.  a suitable font size. This automatic magnification does not apply if the r
  33122.  (raster-mapped font) option is specified, or if a specific typeface is
  33123.  requested and no best fit (b) option is specified.
  33124.  
  33125.  If you specify the nx parameter, _setfont will ignore any other specified
  33126.  options and supply only the font number corresponding to x.
  33127.  
  33128.  Note that the font functions affect only the output from the font output
  33129.  function _outgtext; no other C run-time output functions are affected by
  33130.  font usage.
  33131.  
  33132.  
  33133.  Return Value
  33134.  
  33135.  The _setfont function returns a 0 to indicate success and a -1 to indicate
  33136.  an error. An error occurs if a request for a specific font fails and the b
  33137.  option was not specified, or if fonts have not yet been registered.
  33138.  
  33139.  
  33140.  Compatibility
  33141.  
  33142.   ANSI   DOS   OS/2   UNIX   XENIX
  33143.  
  33144.  
  33145.  
  33146.  See Also
  33147.  
  33148.  _getfontinfo,  _getgtextextent,  _outgtext,  _registerfonts,
  33149.  _unregisterfonts
  33150.  
  33151.  
  33152.  Example
  33153.  
  33154.  See the example for _outgtext.
  33155.  
  33156.  
  33157.  
  33158.  _setgtextvector
  33159.  ────────────────────────────────────────────────────────────────────────────
  33160.  
  33161.  
  33162.  Description
  33163.  
  33164.  Changes the orientation of font text output.
  33165.  
  33166.    #include <graph.h>
  33167.  
  33168.    struct xycoord _far _setgtextvector( short x, short y );
  33169.  
  33170.  x, y                              Integers specifying font rotation
  33171.  
  33172.  
  33173.  Remarks
  33174.  
  33175.  The _setgtextvector function sets the current orientation for font text
  33176.  output to the vector specified by x and y. The current orientation is used
  33177.  in calls to the _outgtext function.
  33178.  
  33179.  The values of x and y define the vector which determines the direction of
  33180.  rotation of font text on the screen. The text-rotation options are shown
  33181.  below:
  33182.  
  33183.  (x, y)                            Text Orientation
  33184.  ────────────────────────────────────────────────────────────────────────────
  33185.  (0, 0)                            Unchanged
  33186.  
  33187.  (1, 0)                            Horizontal text (default)
  33188.  
  33189.  (0, 1)                            Rotated 90 degrees counterclockwise
  33190.  
  33191.  (-1, 0)                           Rotated 180 degrees
  33192.  
  33193.  (0, -1)                           Rotated 270 degrees counterclockwise
  33194.  
  33195.  If other values are input, only the sign of the input is used. For example,
  33196.  (-3, 0) is interpreted as (-1, 0).
  33197.  
  33198.  
  33199.  Return Value
  33200.  
  33201.  The _setgtextvector function returns the previous vector in a structure of
  33202.  xycoord type. If you pass the _setgtextvector function the values (0, 0),
  33203.  the function returns the current vector values in the xycoord structure.
  33204.  
  33205.  
  33206.  Compatibility
  33207.  
  33208.   ANSI   DOS   OS/2   UNIX   XENIX
  33209.  
  33210.  
  33211.  
  33212.  See Also
  33213.  
  33214.  _getfontinfo,  _getgtextextent,  _grstatus,  _outgtext,  _registerfonts,
  33215.  _setfont, _unregisterfonts
  33216.  
  33217.  
  33218.  Example
  33219.  
  33220.  See the example for _outgtext.
  33221.  
  33222.  
  33223.  
  33224.  setjmp
  33225.  ────────────────────────────────────────────────────────────────────────────
  33226.  
  33227.  
  33228.  Description
  33229.  
  33230.  Saves the current state of the program.
  33231.  
  33232.    #include <setjmp.h>
  33233.  
  33234.    int setjmp( jmp_buf env );
  33235.  
  33236.  env                               Variable in which environment is stored
  33237.  
  33238.  
  33239.  Remarks
  33240.  
  33241.  The setjmp function saves a stack environment that can be subsequently
  33242.  restored using longjmp. Used together this way, setjmp and longjmp provide a
  33243.  way to execute a "non-local goto." They are typically used to pass execution
  33244.  control to error-handling or recovery code in a previously called routine
  33245.  without using the normal calling or return conventions.
  33246.  
  33247.  A call to setjmp causes the current stack environment to be saved in env. A
  33248.  subsequent call to longjmp restores the saved environment and returns
  33249.  control to the point just after the corresponding setjmp call. All variables
  33250.  (except register variables) accessible to the routine receiving control
  33251.  contain the values they had when setjmp was called.
  33252.  
  33253.  
  33254.  Return Value
  33255.  
  33256.  The setjmp function returns 0 after saving the stack environment. If setjmp
  33257.  returns as a result of a longjmp call, it returns the value argument of
  33258.  longjmp, or, if the value argument of longjmp is 0, setjmp returns 1. There
  33259.  is no error return.
  33260.  
  33261.  
  33262.  Compatibility
  33263.  
  33264.   ANSI   DOS   OS/2   UNIX   XENIX
  33265.  
  33266.  
  33267.  
  33268.  See Also
  33269.  
  33270.  longjmp
  33271.  
  33272.  
  33273.  Example
  33274.  
  33275.  See the example for _fpreset.
  33276.  
  33277.  
  33278.  
  33279.  
  33280.  
  33281.  _setlinestyle
  33282.  ────────────────────────────────────────────────────────────────────────────
  33283.  
  33284.  
  33285.  Description
  33286.  
  33287.  Sets the line style.
  33288.  
  33289.    #include <graph.h>
  33290.  
  33291.    void _far _setlinestyle( unsigned short mask );
  33292.  
  33293.  mask                              Desired line-style mask
  33294.  
  33295.  
  33296.  Remarks
  33297.  
  33298.  Some graphics routines ( _lineto and _rectangle) draw straight lines on the
  33299.  screen. The type of line is controlled by the current line-style mask.
  33300.  
  33301.  The _setlinestyle function selects the mask used for line drawing. The mask
  33302.  argument is a 16-bit array, where each bit represents a pixel in the line
  33303.  being drawn. If a bit is 1, the corresponding pixel is set to the color of
  33304.  the line (the current color). If a bit is 0, the corresponding pixel is left
  33305.  unchanged. The template is repeated for the entire length of the line.
  33306.  
  33307.  The default mask is 0xFFFF (a solid line).
  33308.  
  33309.  
  33310.  Return Value
  33311.  
  33312.  None.
  33313.  
  33314.  
  33315.  Compatibility
  33316.  
  33317.   ANSI   DOS   OS/2   UNIX   XENIX
  33318.  
  33319.  
  33320.  
  33321.  See Also
  33322.  
  33323.  _getlinestyle,  _lineto functions,  _rectangle functions
  33324.  
  33325.  
  33326.  Example
  33327.  
  33328.  See the example for _getlinestyle.
  33329.  
  33330.  
  33331.  
  33332.  setlocale
  33333.  ────────────────────────────────────────────────────────────────────────────
  33334.  
  33335.  
  33336.  Description
  33337.  
  33338.  Defines the locale.
  33339.  
  33340.    #include <locale.h>
  33341.  
  33342.    char *setlocale( int category, const char *locale );
  33343.  
  33344.  category                          Category affected by locale
  33345.  
  33346.  locale                            Name of the locale that will control the
  33347.                                    specified category
  33348.  
  33349.  
  33350.  Remarks
  33351.  
  33352.  The setlocale function sets the categories specified by category to the
  33353.  locale specified by locale. The "locale" refers to the locality (country)
  33354.  for which certain aspects of your program can be customized. Some
  33355.  locale-dependent aspects include the formatting of dates and the display
  33356.  format for monetary values.
  33357.  
  33358.  The setlocale function is used to set or get the program's current entire
  33359.  locale or simply portions of the locale information. The category argument
  33360.  specifies which portion of a program's locale information will be used. The
  33361.  manifest constants used for the category argument are listed below:
  33362.  
  33363.  Category                          Parts of Program Affected
  33364.  ────────────────────────────────────────────────────────────────────────────
  33365.  LC_ALL                            All categories listed below.
  33366.  
  33367.  LC_COLLATE                        The strcoll and strxfrm functions.
  33368.  
  33369.  LC_CTYPE                          The character-handling functions (except
  33370.                                    for isdigit and isxdigit, which are
  33371.                                    unaffected).
  33372.  
  33373.  LC_MONETARY                       Monetary formatting information returned
  33374.                                    by the localeconv function.
  33375.  
  33376.  LC_NUMERIC                        Decimal point character for the
  33377.                                    formatted output routines (such as
  33378.                                    printf), for the data conversion
  33379.                                    routines, and for
  33380.                                    the nonmonetary formatting information
  33381.                                    returned by the
  33382.                                    localeconv function.
  33383.  
  33384.  LC_TIME                           The strftime function.
  33385.  
  33386.  The locale argument is a pointer to a string that specifies the name of the
  33387.  locale. If locale points to an empty string, the locale is the
  33388.  implementation-defined native environment. A value of "C" specifies the
  33389.  minimal ANSI conforming environment for C translation. This is the only
  33390.  locale supported in Microsoft C, version 6.0.
  33391.  
  33392.  If the locale argument is a null pointer, setlocale returns a pointer to the
  33393.  string associated with the category of the program's locale. The program's
  33394.  current locale setting is not changed.
  33395.  
  33396.  
  33397.  Return Value
  33398.  
  33399.  If a valid locale and category are given, _setlocale returns a pointer to
  33400.  the string associated with the specified category for the new locale. If the
  33401.  locale or category is invalid, the setlocale function returns a null pointer
  33402.  and the program's current locale settings are not changed.
  33403.  
  33404.  The pointer to a string returned by setlocale can be used in subsequent
  33405.  calls to restore that part of the program's locale information. Later calls
  33406.  to setlocale will overwrite the string.
  33407.  
  33408.  
  33409.  Compatibility
  33410.  
  33411.   ANSI   DOS   OS/2   UNIX   XENIX
  33412.  
  33413.  
  33414.  
  33415.  See Also
  33416.  
  33417.  localeconv, strcoll, strftime, strxfrm
  33418.  
  33419.  
  33420.  
  33421.  
  33422.  
  33423.  setmode
  33424.  ────────────────────────────────────────────────────────────────────────────
  33425.  
  33426.  
  33427.  Description
  33428.  
  33429.  Sets the file translation mode.
  33430.  
  33431.  #include <fcntl.h>
  33432.  
  33433.  #include <io.h>                   Required only for function declarations
  33434.  
  33435.    int setmode ( int handle, int mode );
  33436.  
  33437.  handle                            File handle
  33438.  
  33439.  mode                              New translation mode
  33440.  
  33441.  
  33442.  Remarks
  33443.  
  33444.  The setmode function sets to mode the translation mode of the file given by
  33445.  handle. The mode must be one of the following manifest constants:
  33446.  
  33447.  Constant                          Meaning
  33448.  ────────────────────────────────────────────────────────────────────────────
  33449.  O_TEXT                            Sets text (translated) mode.
  33450.                                    Carriage-return-line-feed (CR-LF)
  33451.                                    combinations are translated into a
  33452.                                    single line-feed (LF) character on
  33453.                                    input. Line-feed characters are
  33454.                                    translated into CR-LF combinations on
  33455.                                    output.
  33456.  
  33457.  O_BINARY                          Sets binary (untranslated) mode. The
  33458.                                    above translations are suppressed.
  33459.  
  33460.  The setmode function is typically used to modify the default translation
  33461.  mode of stdin, stdout, stderr, stdaux, and stdprn, but can be used on any
  33462.  file. If setmode is applied to the file handle for a stream, the setmode
  33463.  function should be called before any input or output operations are
  33464.  performed on the stream.
  33465.  
  33466.  
  33467.  Return Value
  33468.  
  33469.  If successful, setmode returns the previous translation mode. A return value
  33470.  of -1 indicates an error, and errno is set to one of the following values:
  33471.  
  33472.  Value                             Meaning
  33473.  ────────────────────────────────────────────────────────────────────────────
  33474.  EBADF                             Invalid file handle
  33475.  
  33476.  EINVAL                            Invalid mode argument (neither O_TEXTnor
  33477.                                    O_BINARY )
  33478.  
  33479.  
  33480.  Compatibility
  33481.  
  33482.   ANSI   DOS   OS/2   UNIX   XENIX
  33483.  
  33484.  
  33485.  
  33486.  See Also
  33487.  
  33488.  creat, fopen, open
  33489.  
  33490.  
  33491.  Example
  33492.  
  33493.    /* SETMODE.C: This program uses setmode to change stdin from text
  33494.     * mode to binary mode.
  33495.     */
  33496.  
  33497.    #include <stdio.h>
  33498.    #include <fcntl.h>
  33499.    #include <io.h>
  33500.  
  33501.    void main()
  33502.    {
  33503.       int result;
  33504.  
  33505.       /* Set "stdin" to have binary mode: */
  33506.       result = setmode( fileno( stdin ), O_BINARY );
  33507.       if( result == -1 )
  33508.          perror( "Cannot set mode" );
  33509.       else
  33510.          printf( "'stdin' successfully changed to binary mode\n" );
  33511.    }
  33512.  
  33513.  
  33514.  Output
  33515.  
  33516.  
  33517.  Output
  33518.  
  33519.  
  33520.  
  33521.    'stdin' successfully changed to binary mode
  33522.  
  33523.  
  33524.  
  33525.  
  33526.  
  33527.  _setpixel Functions
  33528.  ────────────────────────────────────────────────────────────────────────────
  33529.  
  33530.  
  33531.  Description
  33532.  
  33533.  Set a pixel to the current color.
  33534.  
  33535.    #include <graph.h>
  33536.  
  33537.    short _far _setpixel( short x, short y );
  33538.  
  33539.    short _far _setpixel_w( double wx, double wy );
  33540.  
  33541.  x, y                              Target pixel
  33542.  
  33543.  wx, wy                            Target pixel
  33544.  
  33545.  
  33546.  Remarks
  33547.  
  33548.  The _setpixel and the _setpixel_w functions set a pixel at a specified
  33549.  location to the current color.
  33550.  
  33551.  The _setpixel function sets the pixel at the view-coordinate point (x, y) to
  33552.  the current color.
  33553.  
  33554.  The _setpixel_w function sets the pixel at the window-coordinate point (wx,
  33555.  wy) to the current color.
  33556.  
  33557.  
  33558.  Return Value
  33559.  
  33560.  The function returns the previous value of the target pixel. If the function
  33561.  fails (for example, the point lies outside of the clipping region), it will
  33562.  return -1.
  33563.  
  33564.  
  33565.  Compatibility
  33566.  
  33567.   ANSI   DOS   OS/2   UNIX   XENIX
  33568.  
  33569.  
  33570.  
  33571.  See Also
  33572.  
  33573.  _getpixel functions,  _setcolor
  33574.  
  33575.  
  33576.  Example
  33577.  
  33578.    /* GPIXEL.C: This program assigns different colors to randomly
  33579.     * selected pixels.
  33580.     */
  33581.  
  33582.    #include <conio.h>
  33583.    #include <stdlib.h>
  33584.    #include <graph.h>
  33585.  
  33586.    void main()
  33587.    {
  33588.       short xvar, yvar;
  33589.       struct videoconfig vc;
  33590.  
  33591.  
  33592.       /* Find a valid graphics mode. */
  33593.       if( !_setvideomode( _MAXCOLORMODE ) )
  33594.          exit( 1 );
  33595.       _getvideoconfig( &vc );
  33596.  
  33597.       /* Draw filled ellipse to turn on certain pixels. */
  33598.       _ellipse( _GFILLINTERIOR, vc.numxpixels / 6, vc.numypixels / 6,
  33599.                                 vc.numxpixels / 6 * 5, vc.numypixels / 6 * 5
  33600.                                               );
  33601.  
  33602.       /* Draw random pixels in random colors... */
  33603.       while( !kbhit() )
  33604.       {
  33605.          /* ...but only if they are already on (inside the ellipse). */
  33606.          xvar = rand() % vc.numxpixels;
  33607.          yvar = rand() % vc.numypixels;
  33608.          if( _getpixel( xvar, yvar ) != 0 )
  33609.          {
  33610.             _setcolor( rand() % 16 );
  33611.             _setpixel( xvar, yvar );
  33612.          }
  33613.       }
  33614.  
  33615.       getch();          /* Throw away the keystroke. */
  33616.       _setvideomode( _DEFAULTMODE );
  33617.    }
  33618.  
  33619.  
  33620.  
  33621.  
  33622.  
  33623.  _settextcolor
  33624.  ────────────────────────────────────────────────────────────────────────────
  33625.  
  33626.  
  33627.  Description
  33628.  
  33629.  Sets the current text color.
  33630.  
  33631.    #include <graph.h>
  33632.  
  33633.    short _far _settextcolor( short index );
  33634.  
  33635.  index                             Desired color index
  33636.  
  33637.  
  33638.  Remarks
  33639.  
  33640.  The _settextcolor function sets the current text color to the color index
  33641.  specified by index. The default text color is the same as the maximum color
  33642.  index.
  33643.  
  33644.  The _settextcolor routine sets the color for the _outtext and _outmem
  33645.  functions only. It does not affect the color of the printf function or the
  33646.  color of text output with the _outgtext font routine. Use the _setcolor
  33647.  function to change the color of font output.
  33648.  
  33649.  In text color mode, you can specify a color index in the range 0 -31. The
  33650.  colors in the range 0 -15 are interpreted as normal (non-blinking). The
  33651.  normal color range is defined below:
  33652.  
  33653. ╓┌───────┌─────────┌──────┌──────────────────────────────────────────────────╖
  33654.  Index   Color     Index  Color
  33655.  ────────────────────────────────────────────────────────────────────────────
  33656.  0       Black     8      Dark gray
  33657.  
  33658.  1       Blue      9      Light blue
  33659.  
  33660.  2       Green     10     Light green
  33661.  
  33662.  3       Cyan      11     Light cyan
  33663.  
  33664.  4       Red       12     Light red
  33665.  Index   Color     Index  Color
  33666.  ────────────────────────────────────────────────────────────────────────────
  33667. 4       Red       12     Light red
  33668.  
  33669.  5       Magenta   13     Light magenta
  33670.  
  33671.  6       Brown     14     Yellow
  33672.  
  33673.  7       White     15     Bright white
  33674.  
  33675.  
  33676.  
  33677.  Blinking is selected by adding 16 to the normal color value.
  33678.  
  33679.  In every text mode, including monochrome, _getvideoconfig returns the value
  33680.  32 for the number of available colors. The value 32 indicates the range of
  33681.  values (0 -31) accepted by the _settextcolor function. This includes sixteen
  33682.  normal colors (0 -15) and sixteen blinking colors (16 -31). Monochrome text
  33683.  mode has fewer unique display attributes, so some color values are
  33684.  redundant. However, because blinking is selected in the same manner,
  33685.  monochrome text mode has the same range (0 -31) as other text modes.
  33686.  
  33687.  
  33688.  Return Value
  33689.  
  33690.  The function returns the color index of the previous text color. There is no
  33691.  error return.
  33692.  
  33693.  
  33694.  Compatibility
  33695.  
  33696.   ANSI   DOS   OS/2   UNIX   XENIX
  33697.  
  33698.  
  33699.  
  33700.  See Also
  33701.  
  33702.  _gettextcolor,  _outtext
  33703.  
  33704.  
  33705.  Example
  33706.  
  33707.    /* OUTTXT.C: This example illustrates text output functions:
  33708.     *    _gettextcolor   _getbkcolor   _gettextposition   _outtext
  33709.     *    _settextcolor   _setbkcolor   _settextposition
  33710.     */
  33711.  
  33712.    #include <conio.h>
  33713.    #include <stdio.h>
  33714.    #include <graph.h>
  33715.  
  33716.    char buffer [80];
  33717.  
  33718.    void main()
  33719.    {
  33720.  
  33721.       /* Save original foreground, background, and text position */
  33722.       short blink, fgd, oldfgd;
  33723.       long  bgd, oldbgd;
  33724.       struct rccoord oldpos;
  33725.  
  33726.       /* Save original foreground, background, and text position. */
  33727.       oldfgd = _gettextcolor();
  33728.       oldbgd = _getbkcolor();
  33729.       oldpos = _gettextposition();
  33730.       _clearscreen( _GCLEARSCREEN );
  33731.  
  33732.       /* First time no blink, second time blinking. */
  33733.       for( blink = 0; blink <= 16; blink += 16 )
  33734.       {
  33735.          /* Loop through 8 background colors. */
  33736.          for( bgd = 0; bgd < 8; bgd++ )
  33737.          {
  33738.             _setbkcolor( bgd );
  33739.             _settextposition( (short)bgd + ((blink / 16) * 9) + 3, 1 );
  33740.             _settextcolor( 7 );
  33741.             sprintf(buffer, "Back: %d Fore:", bgd );
  33742.             _outtext( buffer );
  33743.  
  33744.    /* Loop through 16 foreground colors. */
  33745.             for( fgd = 0; fgd < 16; fgd++ )
  33746.             {
  33747.                _settextcolor( fgd + blink );
  33748.                sprintf( buffer, " %2d ", fgd + blink );
  33749.                _outtext( buffer );
  33750.             }
  33751.          }
  33752.       }
  33753.       getch();
  33754.  
  33755.       /* Restore original foreground, background, and text position. */
  33756.       _settextcolor( oldfgd );
  33757.       _setbkcolor( oldbgd );
  33758.       _clearscreen( _GCLEARSCREEN );
  33759.       _settextposition( oldpos.row, oldpos.col );
  33760.    }
  33761.  
  33762.  
  33763.  
  33764.  
  33765.  
  33766.  _settextcursor
  33767.  ────────────────────────────────────────────────────────────────────────────
  33768.  
  33769.  
  33770.  Description
  33771.  
  33772.  Sets the current cursor attribute.
  33773.  
  33774.    #include <graph.h>
  33775.  
  33776.    short _far _settextcursor( short attr );
  33777.  
  33778.  attr                              Cursor attribute
  33779.  
  33780.  
  33781.  Remarks
  33782.  
  33783.  The _settextcursor function sets the cursor attribute (i.e., the shape) to
  33784.  the value specified by attr. The high-order byte of attr determines the top
  33785.  line of the cursor within the character cell. The low-order byte of attr
  33786.  determines the bottom line of the cursor.
  33787.  
  33788.  The _settextcursor function uses the same format as the BIOS routines in
  33789.  setting the cursor. Typical values for the cursor attribute are listed
  33790.  below:
  33791.  
  33792.  Attribute                         Cursor Shape
  33793.  ────────────────────────────────────────────────────────────────────────────
  33794.  0x0707                            Underline
  33795.  
  33796.  0x0007                            Full block cursor
  33797.  
  33798.  0x0607                            Double underline
  33799.  
  33800.  0x2000                            No cursor
  33801.  
  33802.  Note that this function works only in text video modes.
  33803.  
  33804.  
  33805.  Return Value
  33806.  
  33807.  The function returns the previous cursor attribute, or -1 if an error occurs
  33808.  (such as calling the function in a graphics screen mode).
  33809.  
  33810.  
  33811.  Compatibility
  33812.  
  33813.   ANSI   DOS   OS/2   UNIX   XENIX
  33814.  
  33815.  
  33816.  
  33817.  See Also
  33818.  
  33819.  _displaycursor,  _gettextcursor
  33820.  
  33821.  
  33822.  Example
  33823.  
  33824.    /* DISCURS.C: This program changes the cursor shape using _gettextcursor
  33825.     * and _settextcursor, and hides the cursor using _displaycursor.
  33826.     */
  33827.  
  33828.    #include <conio.h>
  33829.    #include <graph.h>
  33830.  
  33831.    void main()
  33832.    {
  33833.       short oldcursor;
  33834.       short newcursor = 0x007;        /* Full block cursor */
  33835.  
  33836.       /* Save old cursor shape and make sure cursor is on. */
  33837.       oldcursor = _gettextcursor();
  33838.       _clearscreen( _GCLEARSCREEN );
  33839.       _displaycursor( _GCURSORON );
  33840.       _outtext( "\nOld cursor shape: " );
  33841.       getch();
  33842.  
  33843.       /* Change cursor shape. */
  33844.       _outtext( "\nNew cursor shape: " );
  33845.       _settextcursor( newcursor );
  33846.       getch();
  33847.  
  33848.       /* Restore original cursor shape. */
  33849.       _outtext( "\n" );
  33850.       _settextcursor( oldcursor );
  33851.    }
  33852.  
  33853.  
  33854.  
  33855.  
  33856.  
  33857.  _settextposition
  33858.  ────────────────────────────────────────────────────────────────────────────
  33859.  
  33860.  
  33861.  Description
  33862.  
  33863.  Sets the text position.
  33864.  
  33865.    #include <graph.h>
  33866.  
  33867.    struct rccoord _far _settextposition( short row, short column );
  33868.  
  33869.  row, column                       New output start position
  33870.  
  33871.  
  33872.  Remarks
  33873.  
  33874.  The _settextposition function sets the current text position to the display
  33875.  point (row, column). The _outtext and _outmem functions (and standard
  33876.  console I/O routines, such as printf) output text at that point.
  33877.  
  33878.  The rccoord structure, defined in GRAPH.H, contains the following elements:
  33879.  
  33880.  
  33881.  Element                           Description
  33882.  ────────────────────────────────────────────────────────────────────────────
  33883.  short row                         Row coordinate
  33884.  
  33885.  short col                         Column coordinate
  33886.  
  33887.  
  33888.  Return Value
  33889.  
  33890.  The function returns the previous text position in an rccoord structure,
  33891.  defined in GRAPH.H.
  33892.  
  33893.  
  33894.  Compatibility
  33895.  
  33896.   ANSI   DOS   OS/2   UNIX   XENIX
  33897.  
  33898.  
  33899.  
  33900.  See Also
  33901.  
  33902.  _gettextposition,  _outtext,  _settextwindow
  33903.  
  33904.  
  33905.  Example
  33906.  
  33907.    /* OUTTXT.C: This example illustrates text output functions:
  33908.     *    _gettextcolor   _getbkcolor   _gettextposition   _outtext
  33909.     *    _settextcolor   _setbkcolor   _settextposition
  33910.     */
  33911.  
  33912.    #include <conio.h>
  33913.    #include <stdio.h>
  33914.    #include <graph.h>
  33915.  
  33916.    char buffer [80];
  33917.  
  33918.    void main()
  33919.    {
  33920.  
  33921.    /* Save original foreground, background, and text position */
  33922.       short blink, fgd, oldfgd;
  33923.       long  bgd, oldbgd;
  33924.       struct rccoord oldpos;
  33925.  
  33926.       /* Save original foreground, background, and text position. */
  33927.       oldfgd = _gettextcolor();
  33928.       oldbgd = _getbkcolor();
  33929.       oldpos = _gettextposition();
  33930.       _clearscreen( _GCLEARSCREEN );
  33931.  
  33932.       /* First time no blink, second time blinking. */
  33933.       for( blink = 0; blink <= 16; blink += 16 )
  33934.       {
  33935.          /* Loop through 8 background colors. */
  33936.          for( bgd = 0; bgd < 8; bgd++ )
  33937.          {
  33938.             _setbkcolor( bgd );
  33939.             _settextposition( (short)bgd + ((blink / 16) * 9) + 3, 1 );
  33940.             _settextcolor( 7 );
  33941.             sprintf(buffer, "Back: %d Fore:", bgd );
  33942.             _outtext( buffer );
  33943.  
  33944.             /* Loop through 16 foreground colors. */
  33945.             for( fgd = 0; fgd < 16; fgd++ )
  33946.             {
  33947.                _settextcolor( fgd + blink );
  33948.                sprintf( buffer, " %2d ", fgd + blink );
  33949.                _outtext( buffer );
  33950.             }
  33951.          }
  33952.       }
  33953.       getch();
  33954.  
  33955.       /* Restore original foreground, background, and text position. */
  33956.       _settextcolor( oldfgd );
  33957.       _setbkcolor( oldbgd );
  33958.       _clearscreen( _GCLEARSCREEN );
  33959.       _settextposition( oldpos.row, oldpos.col );
  33960.    }
  33961.  
  33962.  
  33963.  
  33964.  
  33965.  
  33966.  _settextrows
  33967.  ────────────────────────────────────────────────────────────────────────────
  33968.  
  33969.  
  33970.  Description
  33971.  
  33972.  Sets the number of screen rows for text modes.
  33973.  
  33974.    #include <graph.h>
  33975.  
  33976.    short _far _settextrows( short rows );
  33977.  
  33978.  rows                              Number of text rows
  33979.  
  33980.  
  33981.  Remarks
  33982.  
  33983.  The _settextrows function specifies the number of screen rows to be used in
  33984.  text modes.
  33985.  
  33986.  If the constant _MAXTEXTROWS is specified for the rows argument, the
  33987.  function will choose the maximum number of rows available. In text modes,
  33988.  this is 50 rows on VGA, 43 on EGA, and 25 on others. In graphics modes that
  33989.  support 30 or 60 rows, _MAXTEXTROWS specifies 60 rows.
  33990.  
  33991.  
  33992.  Return Value
  33993.  
  33994.  This function returns the numbers of rows set. The function returns 0 if an
  33995.  error occurred.
  33996.  
  33997.  
  33998.  Compatibility
  33999.  
  34000.   ANSI   DOS   OS/2   UNIX   XENIX
  34001.  
  34002.  
  34003.  
  34004.  See Also
  34005.  
  34006.  _getvideoconfig,  _setvideomode,  _setvideomoderows
  34007.  
  34008.  
  34009.  Example
  34010.  
  34011.    /* STXTROWS.C: This program attempts to set the screen height. It returns
  34012.     * an errorlevel code of 1 (fail) or 0 (success) that could be tested in
  34013.     * a batch file.
  34014.     */
  34015.  
  34016.    #include <graph.h>
  34017.    #include <stdlib.h>
  34018.  
  34019.    void main( int argc, char **argv )
  34020.    {
  34021.       short rows;
  34022.  
  34023.       if( !(rows = atoi( argv[1] )) )
  34024.       {
  34025.          _outtext( "\nSyntax: STXTROWS [ 25 | 43 | 50 ]\n" );
  34026.          exit( 1 );
  34027.       }
  34028.  
  34029.    /* Make sure new rows are the same as requested rows. */
  34030.       if( _settextrows( rows ) != rows )
  34031.       {
  34032.          _outtext( "\nInvalid rows\n" );
  34033.          exit( 1 );
  34034.       }
  34035.       else
  34036.          exit( 0 );
  34037.    }
  34038.  
  34039.  
  34040.  
  34041.  
  34042.  
  34043.  _settextwindow
  34044.  ────────────────────────────────────────────────────────────────────────────
  34045.  
  34046.  
  34047.  Description
  34048.  
  34049.  Creates a text window.
  34050.  
  34051.    #include <graph.h>
  34052.  
  34053.    void _far _settextwindow( short r1, short c1, short r2, short c2 );
  34054.  
  34055.  r1, c1                            Upper-left corner of window
  34056.  
  34057.  r2, c2                            Lower-right corner of window
  34058.  
  34059.  
  34060.  Remarks
  34061.  
  34062.  The _settextwindow function specifies a window in row and column coordinates
  34063.  where all text output to the screen is displayed. The arguments (r1, c1)
  34064.  specify the upper-left corner of the text window, and the arguments (r2, c2)
  34065.  specify the lower-right corner of the text window.
  34066.  
  34067.  Text is output from the top of the text window down. When the text window is
  34068.  full, the uppermost line scrolls up out of it.
  34069.  
  34070.  Note that this function does not affect the output of presentation-graphics
  34071.  text (e.g., labels, axis marks, etc.). It also does not affect the output of
  34072.  the font display routine _outgtext. Use the _setviewport function to control
  34073.  the display area for presentation graphics or fonts.
  34074.  
  34075.  
  34076.  Return Value
  34077.  
  34078.  None.
  34079.  
  34080.  
  34081.  Compatibility
  34082.  
  34083.   ANSI   DOS   OS/2   UNIX   XENIX
  34084.  
  34085.  
  34086.  
  34087.  See Also
  34088.  
  34089.  _gettextposition,  _gettextwindow,  _outtext,  _settextposition
  34090.  
  34091.  
  34092.  Example
  34093.  
  34094.  See the example for _scrolltextwindow.
  34095.  
  34096.  
  34097.  
  34098.  setvbuf
  34099.  ────────────────────────────────────────────────────────────────────────────
  34100.  
  34101.  
  34102.  Description
  34103.  
  34104.  Controls stream buffering and buffer size.
  34105.  
  34106.    #include <stdio.h>
  34107.  
  34108.    int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
  34109.  
  34110.  stream                            Pointer to FILE structure
  34111.  
  34112.  buffer                            User-allocated buffer
  34113.  
  34114.  mode                              Mode of buffering:  _IOFBF (full
  34115.                                    buffering), _IOLBF (line buffering), _
  34116.                                    IONBF (no buffer)
  34117.  
  34118.  size                              Size of buffer
  34119.  
  34120.  
  34121.  Remarks
  34122.  
  34123.  The setvbuf function allows the program to control both buffering and buffer
  34124.  size for stream. The stream must refer to an open file that has not been
  34125.  read from or written to since it was opened. The array pointed to by buffer
  34126.  is used as the buffer, unless it is NULL, and an automatically allocated
  34127.  buffer size bytes long is used.
  34128.  
  34129.  The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF,
  34130.  then size is used as the size of the buffer. If mode is _IONBF, the stream
  34131.  is unbuffered and size and buffer are ignored.
  34132.  
  34133.  Values for mode and their meanings are:
  34134.  
  34135.  Type                              Meaning
  34136.  ────────────────────────────────────────────────────────────────────────────
  34137.  _IOFBF                            Full buffering; that is, buffer is used
  34138.                                    as the buffer and size is used as the
  34139.                                    size of the buffer. If buffer is NULL,
  34140.                                    an automatically allocated buffer size
  34141.                                    bytes long is used.
  34142.  
  34143.  _IOLBF                            Under DOS and OS/2, the same as _IOFBF.
  34144.  
  34145.  _IONBF                            No buffer is used, regardless of buffer
  34146.                                    or size.
  34147.  
  34148.  The legal values for size are greater than 0 and less than 32,768.
  34149.  
  34150.  
  34151.  Return Value
  34152.  
  34153.  The return value for setvbuf is 0 if successful, and a nonzero value if an
  34154.  illegal type or buffer size is specified.
  34155.  
  34156.  
  34157.  Compatibility
  34158.  
  34159.   ANSI   DOS   OS/2   UNIX   XENIX
  34160.  
  34161.  
  34162.  
  34163.  See Also
  34164.  
  34165.   fclose, fflush, fopen, setbuf
  34166.  
  34167.  
  34168.  Example
  34169.  
  34170.    /* SETVBUF.C: This program opens two streams named stream1 and stream2.
  34171.     * It then uses setvbuf to give stream1 a user-defined buffer of 1024
  34172.     * bytes and stream2 no buffer.
  34173.     */
  34174.  
  34175.    #include <stdio.h>
  34176.  
  34177.    void main()
  34178.    {
  34179.       char buf[1024];
  34180.       FILE *stream1, *stream2;
  34181.  
  34182.       if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
  34183.           ((stream2 = fopen( "data2", "w" )) != NULL) )
  34184.       {
  34185.          if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )
  34186.             printf( "Incorrect type or size of buffer for stream1\n" );
  34187.          else
  34188.             printf( "'stream1' now has a buffer of 1024 bytes\n" );
  34189.          if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )
  34190.             printf( "Incorrect type or size of buffer for stream2\n" );
  34191.          else
  34192.             printf( "'stream2' now has no buffer\n" );
  34193.          fcloseall();
  34194.       }
  34195.    }
  34196.  
  34197.  
  34198.  Output
  34199.  
  34200.  
  34201.  
  34202.    'stream1' now has a buffer of 1024 bytes
  34203.    'stream2' now has no buffer
  34204.  
  34205.  
  34206.  
  34207.  
  34208.  
  34209.  
  34210.  _setvideomode
  34211.  ────────────────────────────────────────────────────────────────────────────
  34212.  
  34213.  
  34214.  Description
  34215.  
  34216.  Sets the video mode.
  34217.  
  34218.    #include <graph.h>
  34219.  
  34220.    short _far _setvideomode( short mode );
  34221.  
  34222.  mode                              Desired mode
  34223.  
  34224.  
  34225.  Remarks
  34226.  
  34227.  The _setvideomode function selects a screen mode appropriate for a
  34228.  particular hardware/display configuration. The mode argument can be one of
  34229.  the manifest constants shown in Table R.9 and defined in GRAPH.H.
  34230.  
  34231.  Table   R.9 Manifest Constants for Screen Mode
  34232.  
  34233. ╓┌────────────────────┌──────────────────┌──────────────────┌──────────┌─────
  34234.  Mode                 Type(1)            Size(2)            Colors(3)  Adapter
  34235.  ─────────────────────────────────────────────────────────────────────────────
  34236.  _DEFAULTMODE         Hardware default
  34237.                       mode
  34238.  
  34239.  _MAXRESMODE          Highest
  34240.                       resolution in
  34241.                       graphics mode
  34242.  
  34243.  _MAXCOLORMODE        Maximum colors in
  34244.                       graphics mode
  34245.  
  34246.  _TEXTBW40            M/T                40 x 25            16         CGA
  34247.  
  34248.  _TEXTC40             C/T                40 x 25            16         CGA
  34249.  
  34250.  _TEXTBW80            M/T                80 x 25            16         CGA
  34251.  
  34252.  _TEXTC80             C/T                80 x 25            6          CGA
  34253.  Mode                 Type(1)            Size(2)            Colors(3)  Adapter
  34254.  ─────────────────────────────────────────────────────────────────────────────
  34255. _TEXTC80             C/T                80 x 25            6          CGA
  34256.  
  34257.  _MRES4COLOR          C/G                320 x 200          4          CGA
  34258.  
  34259.  _MRESNOCOLOR         M/G                320 x 200          4          CGA
  34260.  
  34261.  _HRESBW              M/G                640 x 200          2          CGA
  34262.  
  34263.  _TEXTMONO            M/T                80 x 25            1          MDPA
  34264.  
  34265.  _HERCMONO(5)         Hercules graphics  720 x 348          1          HGC
  34266.  
  34267.  _MRES16COLOR         C/G                320 x 200          16         EGA
  34268.  
  34269.  _HRES16COLOR         C/G                640 x 200          16         EGA
  34270.  
  34271.  _ERESNOCOLOR         M/T                640 x 350          1          EGA
  34272.  
  34273.  _ERESCOLOR           C/G                640 x 350          16         EGA
  34274.  Mode                 Type(1)            Size(2)            Colors(3)  Adapter
  34275.  ─────────────────────────────────────────────────────────────────────────────
  34276. _ERESCOLOR           C/G                640 x 350          16         EGA
  34277.  
  34278.  _VRES2COLOR          C/G                640 x 480          2          VGA
  34279.  
  34280.  _VRES16COLOR         C/G                640 x 480          16         VGA
  34281.  
  34282.  _MRES256COLOR        C/G                320 x 200          256        VGA
  34283.  
  34284.  _ORESCOLOR           C/G                640 x 400          1 of 16    OLIV
  34285.  
  34286.  ─────────────────────────────────────────────────────────────────────────────
  34287.  
  34288.  
  34289.  
  34290.  1. M indicates monochrome, C indicates color output, T indicates text, and G
  34291.  indicates graphics generation.
  34292.  2. For text modes, size is given in characters (columns  x  rows). For
  34293.  graphics modes, size is given in pixels   (horizontal  x  vertical).
  34294.  3. For monochrome displays, the number of colors is the number of gray
  34295.  shades.
  34296.  4. Adapters are the IBM (and compatible) Monochrome Adapter (MDPA), Color
  34297.  Graphics Adapter (CGA),    Enhanced Graphics Adapter (EGA), Video Graphics
  34298.  Array (VGA), Hercules-compatible adapter (HGC), and   Olivetti-compatible
  34299.  adapter (OLIV).
  34300.  5.  In _HERCMONO mode, the text dimensions are 80 columns by 25 rows, with a
  34301.  9 by 14 character box. The    bottom two scan lines of row 25 are not
  34302.  visible.
  34303.  
  34304.  
  34305.  Note that only standard hardware is described here, but display hardware
  34306.  that is strictly compatible with IBM, Hercules, or Olivetti hardware should
  34307.  also work as described.
  34308.  
  34309.  
  34310.  _MAXRESMODE and _MAXCOLORMODE
  34311.  
  34312.  The two special modes _MAXRESMODE and _MAXCOLORMODE select the highest
  34313.  resolution or greatest number of colors available with the current hardware,
  34314.  respectively. These two modes fail for adapters that do not support graphics
  34315.  modes.
  34316.  
  34317.  Table R.10 lists the video mode selected for different adapter and monitor
  34318.  combinations when _MAXRESMODE or _MAXCOLORMODE is specified:
  34319.  
  34320.  Table   R.10 Modes Selected by _MAXRESMODE and _MAXCOLORMODE
  34321.  
  34322. ╓┌────────────────┌─────────────┌────────────────────────────────────────────╖
  34323.  Adapter/Monitor  _MAXRESMODE   _MAXCOLORMODE
  34324.  ────────────────────────────────────────────────────────────────────────────
  34325.  MDPA             fails         fails
  34326.  
  34327.  HGC              _HERCMONO     _HERCMONO
  34328.  
  34329.  CGA color*       _HRESBW       _MRES4COLOR
  34330.  
  34331.  CGA noncolor*    _HRESBW       _MRESNOCOLOR
  34332.  
  34333.  OCGA             _ORESCOLOR    _MRES4COLOR
  34334.  
  34335.  OEGA color       _ORESCOLOR    _ERESCOLOR
  34336.  
  34337.  Adapter/Monitor  _MAXRESMODE   _MAXCOLORMODE
  34338.  ────────────────────────────────────────────────────────────────────────────
  34339. 
  34340.  EGA color 256K   _HRES16COLOR  _HRES16COLOR
  34341.  
  34342.  EGA color 64K    _HRES16COLOR  _HRES16COLOR
  34343.  
  34344.  EGA ecd 256K     _ERESCOLOR    _ERESCOLOR
  34345.  
  34346.  EGA ecd 64K      _ERESCOLOR    _HRES16COLOR
  34347.  
  34348.  EGA mono         _ERESNOCOLOR  _ERESNOCOLOR
  34349.  
  34350.  MCGA             _VRES2COLOR   _MRES256COLOR
  34351.  
  34352.  VGA              _VRES16COLOR  _MRES256COLOR
  34353.  
  34354.  OVGA             _VRES16COLOR  _MRES256COLOR
  34355.  
  34356.  ────────────────────────────────────────────────────────────────────────────
  34357.  
  34358.  Adapter/Monitor  _MAXRESMODE   _MAXCOLORMODE
  34359.  ────────────────────────────────────────────────────────────────────────────
  34360. 
  34361.  
  34362.  
  34363.  * Color monitor is assumed if the start-up text mode was TEXTC80 or TEXTC40
  34364.  or if the start-up mode   was graphics mode. Composite or other noncolor CGA
  34365.  monitor is assumed if start-up mode was TEXTBW80 or   TEXTBW40.
  34366.  
  34367.  
  34368.  
  34369.  Hercules Support
  34370.  
  34371.  You must install the Hercules driver MSHERC.COM before running your program.
  34372.  Type MSHERC to load the driver. This can be done from an AUTOEXEC.BAT file.
  34373.  
  34374.  
  34375.  If you have both a Hercules monochrome card and a color video card, you
  34376.  should install MSHERC.COM with the /H (/HALF) option. The /H option causes
  34377.  the driver to use one instead of two graphics pages. This prevents the two
  34378.  video cards from attempting to use the same memory. You do not have to use
  34379.  the /H option if you have only a Hercules card. See your Hercules hardware
  34380.  manuals for more details of compatibility.
  34381.  
  34382.  To use a mouse, you must follow special instructions for Hercules cards in
  34383.  Microsoft Mouse Programmer's Reference Guide. (This is sold separately; it
  34384.  is not supplied with either Microsoft C or the mouse package.)
  34385.  
  34386.  
  34387.  Return Value
  34388.  
  34389.  The function returns the number of text rows if the function is successful.
  34390.  If an error is encountered (that is, the mode selected is not supported by
  34391.  the current hardware configuration), the function returns 0.
  34392.  
  34393.  
  34394.  Compatibility
  34395.  
  34396.   ANSI   DOS   OS/2   UNIX   XENIX
  34397.  
  34398.  
  34399.  In OS/2, only text video modes may be selected by _setvideomode.
  34400.  
  34401.  
  34402.  See Also
  34403.  
  34404.  _getvideoconfig,  _settextrows,  _setvideomoderows
  34405.  
  34406.  
  34407.  Example
  34408.  
  34409.    /* SVIDMODE.C: This program sets a video mode from a string given on the
  34410.     * command line.
  34411.     */
  34412.  
  34413.    #include <graph.h>
  34414.    #include <stdlib.h>
  34415.    #include <string.h>
  34416.  
  34417.    short modes[] = { _TEXTBW40,     _TEXTC40,      _TEXTBW80,
  34418.                      _TEXTC80,      _MRES4COLOR,   _MRESNOCOLOR,
  34419.                      _HRESBW,       _TEXTMONO,     _HERCMONO,
  34420.                      _MRES16COLOR,  _HRES16COLOR,  _ERESNOCOLOR,
  34421.                      _ERESCOLOR,    _VRES2COLOR,   _VRES16COLOR,
  34422.                      _MRES256COLOR, _ORESCOLOR
  34423.                    };
  34424.    char *names[] = { "TEXTBW40",    "TEXTC40",     "TEXTBW80",
  34425.                      "TEXTC80",     "MRES4COLOR",  "MRESNOCOLOR",
  34426.                      "HRESBW",      "TEXTMONO",    "HERCMONO",
  34427.                      "MRES16COLOR", "HRES16COLOR", "ERESNOCOLOR",
  34428.                      "ERESCOLOR",   "VRES2COLOR",  "VRES16COLOR",
  34429.                      "MRES256COLOR","ORESCOLOR"
  34430.                     };
  34431.  
  34432.    void error( char *msg );
  34433.  
  34434.  
  34435.    void main( int argc, char *argv[] )
  34436.    {
  34437.       short i, num = sizeof( modes ) / sizeof( short );
  34438.       struct videoconfig vc;
  34439.  
  34440.       if( argc < 2 )
  34441.          error( "No argument given" );
  34442.  
  34443.       /* If matching name found, change to corresponding mode. */
  34444.       for( i = 0; i < num; i++ )
  34445.       {
  34446.          if( !strcmpi( argv[1], names[i] ) )
  34447.          {
  34448.             _setvideomode( modes[i] );
  34449.             _outtext( "New mode is: " );
  34450.             _outtext( names[i] );
  34451.             exit( 0 );
  34452.          }
  34453.       }
  34454.       error( "Invalid mode string" );
  34455.    }
  34456.  
  34457.    void error( char *msg )
  34458.    {
  34459.       _outtext( msg );
  34460.       exit( 1 );
  34461.    }
  34462.  
  34463.  
  34464.  
  34465.  
  34466.  
  34467.  _setvideomoderows
  34468.  ────────────────────────────────────────────────────────────────────────────
  34469.  
  34470.  
  34471.  Description
  34472.  
  34473.  Sets the video mode and number of text rows for text modes.
  34474.  
  34475.    #include <graph.h>
  34476.  
  34477.    short _far _setvideomoderows( short mode, short rows );
  34478.  
  34479.  mode                              Desired mode
  34480.  
  34481.  rows                              Number of text rows
  34482.  
  34483.  
  34484.  Remarks
  34485.  
  34486.  The _setvideomoderows function selects a screen mode for a particular
  34487.  hardware/display combination. The manifest constants for the screen mode are
  34488.  given in the reference pages for _setvideomode. The _setvideomoderows
  34489.  function also specifies the number of text rows to be used in a text mode.
  34490.  If the constant _MAXTEXTROWS is specified for the rows argument, the
  34491.  function will choose the maximum number of rows available. In text modes,
  34492.  this is 50 rows on VGA, 43 on EGA, and 25 on others. In graphics modes that
  34493.  support 30 or 60 rows, _MAXTEXTROWS specifies 60 rows.
  34494.  
  34495.  
  34496.  Return Value
  34497.  
  34498.  The setvideomoderows function returns the numbers of rows set. The function
  34499.  returns 0 if an error occurred (e.g., if the mode is not supported).
  34500.  
  34501.  
  34502.  Compatibility
  34503.  
  34504.   ANSI   DOS   OS/2   UNIX   XENIX
  34505.  
  34506.  
  34507.  In OS/2, only text video modes may be selected by _setvideomoderows.
  34508.  
  34509.  
  34510.  See Also
  34511.  
  34512.  _getvideoconfig,  _settextrows,  _setvideomode
  34513.  
  34514.  
  34515.  Example
  34516.  
  34517.    /* SVMROWS.C */
  34518.  
  34519.    #include <stdlib.h>
  34520.    #include <conio.h>
  34521.    #include <graph.h>
  34522.  
  34523.    void main()
  34524.    {
  34525.       struct videoconfig config;
  34526.  
  34527.    /* Set 43-line graphics mode if available. */
  34528.       if( !_setvideomoderows( _ERESCOLOR, 43 ) )
  34529.       {
  34530.          _outtext( "EGA or VGA required" );
  34531.          exit( 1 );
  34532.       }
  34533.       _getvideoconfig( &config );
  34534.  
  34535.       /* Set logical origin to center and draw a rectangle. */
  34536.       _setlogorg( config.numxpixels / 2 - 1, config.numypixels / 21 );
  34537.       _rectangle( _GBORDER, -80, -50, 80, 50 );
  34538.  
  34539.       getch();
  34540.       _setvideomode( _DEFAULTMODE );
  34541.    }
  34542.  
  34543.  
  34544.  
  34545.  
  34546.  
  34547.  _setvieworg
  34548.  ────────────────────────────────────────────────────────────────────────────
  34549.  
  34550.  
  34551.  Description
  34552.  
  34553.  Moves the view-coordinate origin to the specified physical point.
  34554.  
  34555.    #include <graph.h>
  34556.  
  34557.    struct xycoord _far _setvieworg( short x, short y );
  34558.  
  34559.  x, y                              New origin point
  34560.  
  34561.  
  34562.  Remarks
  34563.  
  34564.  The _setvieworg function moves the view-coordinate origin (0, 0) to the
  34565.  physical point (x, y). All other view-coordinate points move the same
  34566.  direction and distance.
  34567.  
  34568.  The xycoord structure, defined in GRAPH.H, contains the following elements:
  34569.  
  34570.  
  34571.  Element                           Description
  34572.  ────────────────────────────────────────────────────────────────────────────
  34573.  short xcoord                      x coordinate
  34574.  
  34575.  short ycoord                      y coordinate
  34576.  
  34577.  ────────────────────────────────────────────────────────────────────────────
  34578.  C 5.1 Difference
  34579.  This function replaces the _setlogorg function.
  34580.  ────────────────────────────────────────────────────────────────────────────
  34581.  
  34582.  
  34583.  Return Value
  34584.  
  34585.  The function returns the physical coordinates of the previous view origin in
  34586.  an xycoord structure, defined in GRAPH.H.
  34587.  
  34588.  
  34589.  Compatibility
  34590.  
  34591.   ANSI   DOS   OS/2   UNIX   XENIX
  34592.  
  34593.  
  34594.  
  34595.  See Also
  34596.  
  34597.  _getphyscoord,  _getviewcoord,  _getwindowcoord,  _setcliprgn,  _setviewport
  34598.  
  34599.  
  34600.  
  34601.  Example
  34602.  
  34603.    /* SVORG.C: This program sets the view origin to the center of
  34604.     * the screen, then draws a rectangle using the new origin.
  34605.     */
  34606.  
  34607.    #include <stdlib.h>
  34608.    #include <conio.h>
  34609.    #include <graph.h>
  34610.  
  34611.    void main()
  34612.    {
  34613.       struct videoconfig config;
  34614.  
  34615.    /* Find a valid graphics mode. */
  34616.       if( !_setvideomode( _MAXRESMODE ) )
  34617.          exit( 1 );
  34618.       _getvideoconfig( &config );
  34619.  
  34620.       /* Set view origin to the center of the screen. */
  34621.       _setvieworg( config.numxpixels / 2, config.numypixels / 2 );
  34622.       _rectangle( _GBORDER, -80, -50, 80, 50 );
  34623.  
  34624.       getch();
  34625.       _setvideomode( _DEFAULTMODE );
  34626.    }
  34627.  
  34628.  
  34629.  
  34630.  
  34631.  
  34632.  _setviewport
  34633.  ────────────────────────────────────────────────────────────────────────────
  34634.  
  34635.  
  34636.  Description
  34637.  
  34638.  Creates a viewport.
  34639.  
  34640.    #include <graph.h>
  34641.  
  34642.    void _far _setviewport( short x1, short y1, short x2, short y2 );
  34643.  
  34644.  x1, y1                            Upper-left corner of viewport
  34645.  
  34646.  x2, y2                            Lower-right corner of viewport
  34647.  
  34648.  
  34649.  Remarks
  34650.  
  34651.  The _setviewport function redefines the graphics viewport. The _setviewport
  34652.  function defines a clipping region in exactly the same manner as
  34653.  _setcliprgn, and then sets the view-coordinate origin to the upper-left
  34654.  corner of the region. The physical points (x1, y1) and (x2, y2) are the
  34655.  diagonally opposed corners of the rectangular clipping region. Any window
  34656.  transformation done with the _setwindow function applies only to the
  34657.  viewport and not to the entire screen.
  34658.  
  34659.  
  34660.  Return Value
  34661.  
  34662.  None.
  34663.  
  34664.  
  34665.  Compatibility
  34666.  
  34667.   ANSI   DOS   OS/2   UNIX   XENIX
  34668.  
  34669.  
  34670.  
  34671.  See Also
  34672.  
  34673.  _setcliprgn,  _setvieworg,  _setwindow
  34674.  
  34675.  
  34676.  Example
  34677.  
  34678.    /* SVIEWPRT.C: This program sets a viewport and then draws a rectangle
  34679.     * around it and an ellipse in it.
  34680.     */
  34681.  
  34682.    #include <conio.h>
  34683.    #include <stdlib.h>
  34684.    #include <graph.h>
  34685.  
  34686.    void main()
  34687.    {
  34688.       /* Find a valid graphics mode. */
  34689.       if( !_setvideomode( _MAXRESMODE ) )
  34690.          exit( 1 );
  34691.  
  34692.       _setviewport( 100, 100, 200, 200 );
  34693.       _rectangle( _GBORDER, 0, 0, 100, 100 );
  34694.       _ellipse( _GFILLINTERIOR, 10, 10, 90, 90 );
  34695.  
  34696.    getch();
  34697.       _setvideomode( _DEFAULTMODE );
  34698.    }
  34699.  
  34700.  
  34701.  
  34702.  
  34703.  
  34704.  _setvisualpage
  34705.  ────────────────────────────────────────────────────────────────────────────
  34706.  
  34707.  
  34708.  Description
  34709.  
  34710.  Sets the visual page.
  34711.  
  34712.    #include <graph.h>
  34713.  
  34714.    short _far _setvisualpage( short page );
  34715.  
  34716.  page                              Visual page number
  34717.  
  34718.  
  34719.  Remarks
  34720.  
  34721.  For hardware configurations that have an EGA or a VGA and enough memory to
  34722.  support multiple-screen pages, the _setvisualpage function selects the
  34723.  current visual page. The page argument specifies the current visual page.
  34724.  The default page number is 0.
  34725.  
  34726.  
  34727.  Return Value
  34728.  
  34729.  The function returns the number of the previous visual page. If the function
  34730.  fails, it returns a negative value.
  34731.  
  34732.  
  34733.  Compatibility
  34734.  
  34735.   ANSI   DOS   OS/2   UNIX   XENIX
  34736.  
  34737.  
  34738.  
  34739.  See Also
  34740.  
  34741.  _getactivepage,  _getvisualpage,  _setactivepage,  _setvideomode
  34742.  
  34743.  
  34744.  Example
  34745.  
  34746.  See the example for _setactivepage.
  34747.  
  34748.  
  34749.  
  34750.  _setwindow
  34751.  ────────────────────────────────────────────────────────────────────────────
  34752.  
  34753.  
  34754.  Description
  34755.  
  34756.  Defines a graphics window.
  34757.  
  34758.    #include <graph.h>
  34759.  
  34760.    short _far _setwindow( short finvert, double wx1, double wy1, double wx2,
  34761.    double wy2 );
  34762.  
  34763.  finvert                           Invert flag
  34764.  
  34765.  wx1, wy1                          Upper-left corner of window
  34766.  
  34767.  wx2, wy2                          Lower-right corner of window
  34768.  
  34769.  
  34770.  Remarks
  34771.  
  34772.  The _setwindow function defines a window bounded by the specified
  34773.  coordinates. The arguments (wx1, wy1) specify the upper-left corner of the
  34774.  window, and the arguments (wx2, wy2) specify the lower-right corner of the
  34775.  window.
  34776.  
  34777.  The finvert argument specifies the direction of the coordinates. If finvert
  34778.  is TRUE, the y axis increases from the screen bottom to the screen top
  34779.  (Cartesian coordinates). If finvert is FALSE, the y axis increases from the
  34780.  screen top to the screen bottom (screen coordinates).
  34781.  
  34782.  Any window transformation done with the _setwindow function applies only to
  34783.  the viewport and not to the entire screen.
  34784.  
  34785.  If wx1 equals wx2 or wy1 equals wy2, the function will fail.
  34786.  
  34787.  Note that this function does not affect the output of presentation-graphics
  34788.  text (e.g., labels, axis marks, etc.). It also does not affect the output of
  34789.  the font display routine _outgtext.
  34790.  
  34791.  
  34792.  Return Value
  34793.  
  34794.  The function returns a nonzero value if successful. If the function fails
  34795.  (e.g., if it is not in a graphics mode), it returns 0.
  34796.  
  34797.  
  34798.  Compatibility
  34799.  
  34800.   ANSI   DOS   OS/2   UNIX   XENIX
  34801.  
  34802.  
  34803.  
  34804.  See Also
  34805.  
  34806.  _setviewport
  34807.  
  34808.  
  34809.  Example
  34810.  
  34811.    /* SWINDOW.C: This program illustrates translation between window,
  34812.     * view, and physical coordinates. Functions used include:
  34813.     *      _setwindow         _getwindowcoord
  34814.     *      _getphyscoord      _getviewcoord_wxy
  34815.     */
  34816.  
  34817.    #include <conio.h>
  34818.    #include <stdlib.h>
  34819.    #include <graph.h>
  34820.  
  34821.    enum boolean { FALSE, TRUE };
  34822.    enum display { MOVE, DRAW, ERASE };
  34823.  
  34824.    void main()
  34825.    {
  34826.       struct xycoord view, phys;
  34827.       struct _wxycoord oldwin, newwin;
  34828.       struct videoconfig vc;
  34829.       double xunit, yunit, xinc, yinc;
  34830.       short  color, key, fintersect = FALSE, fdisplay = TRUE;
  34831.  
  34832.       /* Find a valid graphics mode. */
  34833.       if( !_setvideomode( _MAXRESMODE ) )
  34834.          exit( 1 );
  34835.       _getvideoconfig( &vc );
  34836.  
  34837.       /* Set a window using real numbers. */
  34838.       _setwindow( FALSE, -125.0, -100.0, 125.0, 100.0 );
  34839.  
  34840.       /* Calculate the size of one pixel in window coordinates.
  34841.        * Then get the current window coordinates and color.
  34842.        */
  34843.       oldwin = _getwindowcoord( 1, 1 );
  34844.       newwin = _getwindowcoord( 2, 2 );
  34845.       xunit = xinc = newwin.wx - oldwin.wx;
  34846.       yunit = yinc = newwin.wy - oldwin.wy;
  34847.       newwin = oldwin = _getcurrentposition_w();
  34848.       color = _getcolor();
  34849.  
  34850.       while( 1 )
  34851.       {
  34852.          /* Set flag according to whether current pixel is on, then
  34853.           * turn pixel on.
  34854.           */
  34855.          if( _getpixel_w( oldwin.wx, oldwin.wy ) == color )
  34856.             fintersect = TRUE;
  34857.          else
  34858.             fintersect = FALSE;
  34859.          _setcolor( color );
  34860.          _setpixel_w( oldwin.wx, oldwin.wy );
  34861.  
  34862.    /* Get and test key. */
  34863.          key = getch();
  34864.          switch( key )
  34865.          {
  34866.             case 27:                        /* ESC Quit                 */
  34867.                _setvideomode( _DEFAULTMODE );
  34868.                exit( 0 );
  34869.             case 32:                        /* SPACE    Move no color   */
  34870.                fdisplay = MOVE;
  34871.                continue;
  34872.             case 0:                         /* Extended code - get next */
  34873.                key = getch();
  34874.                switch( key )
  34875.                {
  34876.                   case 72:                  /* UP          -y           */
  34877.                      newwin.wy -= yinc;
  34878.                      break;
  34879.                   case 77:                  /* RIGHT    +x              */
  34880.                      newwin.wx += xinc;
  34881.                      break;
  34882.                   case 80:                  /* DOWN        +y           */
  34883.                      newwin.wy += yinc;
  34884.                      break;
  34885.                   case 75:                  /* LEFT     -x              */
  34886.                      newwin.wx -= xinc;
  34887.                      break;
  34888.                   case 82:                  /* INS      Draw white      */
  34889.                      fdisplay = DRAW;
  34890.                      continue;
  34891.                   case 83:                  /* DEL      Draw black      */
  34892.                      fdisplay = ERASE;
  34893.                      continue;
  34894.                }
  34895.                break;
  34896.          }
  34897.  
  34898.          /* Translate window coordinates to view, view to physical.
  34899.           * Then check physical to make sure we're on screen. Update screen
  34900.           * and position if we are. Ignore if not.
  34901.           */
  34902.          view = _getviewcoord_wxy( &newwin );
  34903.          phys = _getphyscoord( view.xcoord, view.ycoord );
  34904.          if( (phys.xcoord >= 0) && (phys.xcoord < vc.numxpixels) &&
  34905.              (phys.ycoord >= 0) && (phys.ycoord < vc.numypixels) )
  34906.          {
  34907.  
  34908.    /* If display on, draw to new position, else move to new. */
  34909.             if( fdisplay != MOVE )
  34910.             {
  34911.                if( fdisplay == ERASE )
  34912.                   _setcolor( 0 );
  34913.                _lineto_w( newwin.wx, newwin.wy );
  34914.             }
  34915.             else
  34916.             {
  34917.                _setcolor( 0 );
  34918.                _moveto_w( newwin.wx, newwin.wy );
  34919.  
  34920.                /* If there was no intersect, erase old pixel. */
  34921.                if( !fintersect )
  34922.                   _setpixel_w( oldwin.wx, oldwin.wy );
  34923.             }
  34924.             oldwin = newwin;
  34925.          }
  34926.          else
  34927.             newwin = oldwin;
  34928.       }
  34929.    }
  34930.  
  34931.  
  34932.  
  34933.  
  34934.  
  34935.  _setwritemode
  34936.  ────────────────────────────────────────────────────────────────────────────
  34937.  
  34938.  
  34939.  Description
  34940.  
  34941.  Sets the current logical mode for line drawing.
  34942.  
  34943.    #include  <graph.h>
  34944.  
  34945.    short _far _setwritemode( short action );
  34946.  
  34947.  action                            Interaction with existing screen image
  34948.  
  34949.  
  34950.  Remarks
  34951.  
  34952.  The _setwritemode function sets the current logical write mode, which is
  34953.  used when drawing lines with the _lineto and _rectangle functions.
  34954.  
  34955.  The action argument defines the write mode. The possible values are _GAND,
  34956.  _GOR, _GPRESET, _GPSET, and _GXOR. See the description of the _putimage
  34957.  function for more details on these manifest constants.
  34958.  
  34959.  
  34960.  Return Value
  34961.  
  34962.  The _setwritemode function returns the previous write mode, or -1 if an
  34963.  error occurs.
  34964.  
  34965.  
  34966.  Compatibility
  34967.  
  34968.   ANSI   DOS   OS/2   UNIX   XENIX
  34969.  
  34970.  
  34971.  
  34972.  See Also
  34973.  
  34974.  _getwritemode,  _grstatus,  _lineto functions,  _putimage functions,
  34975.  _rectangle functions,  _setcolor,  _setlinestyle
  34976.  
  34977.  
  34978.  Example
  34979.  
  34980.  See the example for _getwritemode.
  34981.  
  34982.  
  34983.  
  34984.  
  34985.  
  34986.  signal
  34987.  ────────────────────────────────────────────────────────────────────────────
  34988.  
  34989.  
  34990.  Description
  34991.  
  34992.  Sets interrupt signal handling.
  34993.  
  34994.    #include <signal.h>
  34995.  
  34996.    void ( *signal( int sig, void( *func )( int sig [[, int subcode ]] ) ) ) (
  34997.    int sig );
  34998.  
  34999.  sig                               Signal value
  35000.  
  35001.  func                              Function to be executed
  35002.  
  35003.  subcode                           Optional subcode to the signal number
  35004.  
  35005.  
  35006.  Remarks
  35007.  
  35008.  The signal function allows a process to choose one of several ways to handle
  35009.  an interrupt signal from the operating system.
  35010.  
  35011.  The sig argument must be one of the manifest constants described in Table
  35012.  R.11 and defined in SIGNAL.H.
  35013.  
  35014.  Table   R.11 Signals and Responses
  35015.  
  35016. ╓┌──────────┌────────────────┌──────────────────────┌────────────────────────╖
  35017.  Value      Modes            Meaning                Default Action
  35018.  ────────────────────────────────────────────────────────────────────────────
  35019.  SIGABRT    Real,            Abnormal termination   Terminates the calling
  35020.             protected                               program with exit code
  35021.                                                     3
  35022.  
  35023.  SIGBREAK   Protected        CTRL+BREAK signal      Terminates the calling
  35024.                                                     program with exit code
  35025.                                                     3
  35026.  
  35027.  SIGFPE     Real,            Floating-point error   Terminates the calling
  35028.             protected                               program with exit code
  35029.                                                     3
  35030.  Value      Modes            Meaning                Default Action
  35031.  ────────────────────────────────────────────────────────────────────────────
  35032.                                                    3
  35033.  
  35034.  SIGILL     Real,            Illegal instruction    Terminates the calling
  35035.             protected                               program with exit code
  35036.                                                     3
  35037.  
  35038.  SIGINT     Real,            CTRL+C signal          Terminates the calling
  35039.             protected                               program with exit code
  35040.                                                     3
  35041.  
  35042.  SIGSEGV    Real,            Illegal storage        Terminates the calling
  35043.             protected        access                 program with exit code
  35044.                                                     3
  35045.  
  35046.  SIGTERM    Real,            Termination request    Terminates the calling
  35047.             protected                               program with exit code
  35048.                                                     3
  35049.  
  35050.  SIGUSR1    Protected        OS/2 process flag A    Signal is ignored
  35051.  Value      Modes            Meaning                Default Action
  35052.  ────────────────────────────────────────────────────────────────────────────
  35053. SIGUSR1    Protected        OS/2 process flag A    Signal is ignored
  35054.  
  35055.  SIGUSR2    Protected        OS/2 process flag B    Signal is ignored
  35056.  
  35057.  SIGUSR3    Protected        OS/2 process flag C    Signal is ignored
  35058.  
  35059.  ────────────────────────────────────────────────────────────────────────────
  35060.  
  35061.  
  35062.  
  35063.  SIGUSR1, SIGUSR2, and SIGUSR3 are user-defined signals which can be sent by
  35064.  means of DosFlagProcess. For details, see Microsoft Operating System/2
  35065.  Programmer's Reference.
  35066.  
  35067.  Note that SIGILL, SIGSEGV, and SIGTERM are not generated under DOS and
  35068.  SIGSEGV is not generated under OS/2. They are included for ANSI
  35069.  compatibility. Thus, you may set signal handlers for these signals via
  35070.  signal, and you may also explicitly generate these signals by calling raise.
  35071.  
  35072.  
  35073.  Note also that signal settings are not preserved in child processes created
  35074.  by calls to exec or spawn. The signal settings are reset to the default in
  35075.  the child process.
  35076.  
  35077.  The action taken when the interrupt signal is received depends on the value
  35078.  of func. The func argument must be either a function address or one of the
  35079.  manifest constants defined in SIGNAL.H and listed below:
  35080.  
  35081.  Value                             Meaning
  35082.  ────────────────────────────────────────────────────────────────────────────
  35083.  SIG_ACK                           Acknowledges receipt of a signal (OS/2
  35084.                                    only). This constant is valid only if a
  35085.                                    user-defined signal handler is
  35086.                                    installed. Once a process receives a
  35087.                                    given signal, the operating system does
  35088.                                    not send any more signals of this type
  35089.                                    until it receives a SIG_ACK
  35090.                                    acknowledgment from the process. The
  35091.                                    operating system does not queue up
  35092.                                    signals of a given type; therefore, if
  35093.                                    more than one signal of a given type
  35094.                                    accumulates before the process returns a
  35095.                                    SIG_ACK value, only the most recent
  35096.                                    signal is sent to the process after the
  35097.                                    SIG_ACK value is received by the
  35098.                                    operating system. This option has no
  35099.                                    effect on which handler is installed for
  35100.                                    a given signal. The manifest constant
  35101.                                    SIG_ACK is not supported for SIGFPE
  35102.                                    signals.
  35103.  
  35104.  SIG_DFL                           Uses system-default response. The
  35105.                                    system-default response for all signals
  35106.                                    except SIGUSR1, SIGUSR2, and SIGUSR3 is
  35107.                                    to abort the calling program. The
  35108.                                    calling process is terminated with exit
  35109.                                    code 3, and control returns to DOS or
  35110.                                    OS/2. If the
  35111.                                    calling program uses stream I/O, buffers
  35112.                                    created by the run-time library are not
  35113.                                    flushed, but buffers created by the
  35114.                                    operating system are flushed. The
  35115.                                    default response for SIGUSR1, SIGUSR2,
  35116.                                    and SIGUSR3 is to ignore the signal.
  35117.  
  35118.  SIG_ERR                           Ignores interrupt signal (OS/2 only).
  35119.                                    This constant is equivalent to SIG_IGN,
  35120.                                    except that any process that tries to
  35121.                                    send this signal receives an error. A
  35122.                                    process may use the raise function to
  35123.                                    send a signal to itself. A different
  35124.                                    process may send a signal by means of
  35125.                                    the function DosFlagProcess (if the
  35126.                                    signal is SIGUSR1, SIGUSR2, or SIGUSR3)
  35127.                                    or by means of
  35128.                                    DosKillProcess (if the signal is SIGTERM
  35129.                                    ).
  35130.  
  35131.  SIG_IGN                           Ignores interrupt signal. This value
  35132.                                    should never be given for SIGFPE, since
  35133.                                    the floating-point state of the process
  35134.                                    is left
  35135.                                    undefined.
  35136.  
  35137.  Function address                  Installs the specified function as the
  35138.                                    handler for the given
  35139.                                    signal.
  35140.  
  35141.                                    For all signals except SIGFPE and SIGUSR
  35142.                                    X, the function is passed the sig
  35143.                                    argument SIGINT and executed.
  35144.  
  35145.                                    For SIGFPE signals, the function is
  35146.                                    passed two arguments; namely SIGFPE and
  35147.                                    the floating-point error code
  35148.                                    identifying the type of exception that
  35149.                                    occurred.
  35150.  
  35151.                                    For SIGUSR1, SIGUSR2, and SIGUSR3, the
  35152.                                    function is passed two arguments: the
  35153.                                    signal number and the argument furnished
  35154.                                    by the DosFlagProcess function.
  35155.  
  35156.  For SIGFPE, the function pointed to by func is passed two arguments, SIGFPE
  35157.  and an integer error subcode, FPE_xxx; then the function is executed. (See
  35158.  the include file FLOAT.H for definitions of the FPE_xxx subcodes.) The value
  35159.  of func is not reset upon receiving the signal. To recover from
  35160.  floating-point exceptions, use setjmp in conjunction with longjmp. (See the
  35161.  example under _fpreset.) If the function returns, the calling process
  35162.  resumes execution with the floating-point state of the process left
  35163.  undefined.
  35164.  
  35165.  If the function returns, the calling process resumes execution immediately
  35166.  following the point at which it received the interrupt signal. This is true
  35167.  regardless of the type of signal or operating mode.
  35168.  
  35169.  Before the specified function is executed under DOS versions 3.x or earlier,
  35170.  the value of func is set to SIG_DFL. The next interrupt signal is treated as
  35171.  described above for SIG_DFL, unless an intervening call to signal specifies
  35172.  otherwise. This allows the program to reset signals in the called function.
  35173.  
  35174.  
  35175.  Under OS/2, the signal handler is not reset to the system-default response.
  35176.  Instead, no signals of a given type are received by a process until the
  35177.  process sends a SIG_ACK value to the operating system. The program can
  35178.  restore the system-default response from the handler by first sending
  35179.  SIG_DFL and then sending SIG_ACK to the operating system.
  35180.  
  35181.  Since signal-handler routines are normally called asynchronously when an
  35182.  interrupt occurs, it is possible that your signal-handler function will get
  35183.  control when a C run-time operation is incomplete and in an unknown state.
  35184.  Certain restrictions therefore apply to the C functions that can be used in
  35185.  your signal-handler routine:
  35186.  
  35187.  
  35188.    1.  Do not issue low-level or standard input and output routines (e.g.,
  35189.        printf, read, write, and fread).
  35190.  
  35191.    2.  Do not call heap routines or any routine that uses the heap routines
  35192.        (e.g., malloc, strdup, putenv).
  35193.  
  35194.    3.  Do not use any C function that generates a system call (e.g., getcwd,
  35195.        time).
  35196.  
  35197.    4.  Do not use the longjmp function unless the interrupt is caused by a
  35198.        floating-point exception (i.e., sig is SIGFPE). In this case, the
  35199.        program should first reinitialize the floating-point package by means
  35200.        of a call to _fpreset.
  35201.  
  35202.    5.  Do not use any overlay routines.
  35203.  
  35204.  
  35205.  
  35206.  Return Value
  35207.  
  35208.  The signal function returns the previous value of func associated with the
  35209.  given signal. For example, if the previous value of func was SIG_IGN, the
  35210.  return value will be SIG_IGN. The one exception to this rule is SIG_ACK,
  35211.  which returns the address of the currently installed handler.
  35212.  
  35213.  A return value of -1 indicates an error, and errno is set to EINVAL. The
  35214.  possible error causes are an invalid sig value, an invalid func value (that
  35215.  is, a value that is less than SIG_ACK but not defined), or a func value of
  35216.  SIG_ACK used when no handler is currently installed.
  35217.  
  35218.  
  35219.  Compatibility
  35220.  
  35221.   ANSI   DOS   OS/2   UNIX   XENIX
  35222.  
  35223.  
  35224.  
  35225.  See Also
  35226.  
  35227.  abort, exec functions, exit, _exit, _fpreset, spawn functions
  35228.  
  35229.  
  35230.  Example
  35231.  
  35232.    /* SIGNAL.C illustrates setting up signal interrupt routines. Functions
  35233.     * illustrated include signal and raise.
  35234.     *
  35235.     * Since C I/O functions are not safe inside signal routines, the code
  35236.     * uses conditionals to use system-level DOS and OS/2 services. Another
  35237.     * option is to set global flags and do any I/O operations outside the
  35238.     * signal handler. To compile the OS/2 version, define the symbol OS2.
  35239.     */
  35240.  
  35241.    #include <stdio.h>
  35242.    #include <conio.h>
  35243.    #include <signal.h>
  35244.    #include <process.h>
  35245.    #include <stdlib.h>
  35246.    #if defined( OS2 )
  35247.       #define INCL_NOCOMMON
  35248.       #define INCL_NOPM
  35249.       #define INCL_VIO
  35250.       #define INCL_KBD
  35251.       #include <os2.h>
  35252.       #include <string.h>
  35253.    #else
  35254.       #include <dos.h>
  35255.       #include <bios.h>
  35256.    #endif
  35257.  
  35258.    void ctrlchandler( void );          /* Prototypes */
  35259.    void safeout( char *str );
  35260.    int  safein( void );
  35261.  
  35262.    void main()
  35263.    {
  35264.       int ch;
  35265.  
  35266.       /* Modify CTRL+C behavior. */
  35267.       if( signal( SIGINT, ctrlchandler ) == SIG_ERR )
  35268.       {
  35269.          fprintf( stderr, "Couldn't set SIGINT\n" );
  35270.          abort();
  35271.       }
  35272.  
  35273.       /* Input loop illustrates results. */
  35274.       do
  35275.       {
  35276.          ch = getch();
  35277.          if( ch == 0 )
  35278.          {
  35279.             ch = getch();
  35280.             if( ch == 46 )      /* Treat ALT+C like CTRL+C */
  35281.                raise( SIGINT );
  35282.             else
  35283.                printf( "Extended code: %X\n", ch );
  35284.          }
  35285.          else
  35286.             printf( "ASCII code: %X\n", ch );
  35287.       } while( ch != 27 );        /* ESC code */
  35288.    }
  35289.  
  35290.    /* Handles SIGINT (CTRL+C) interrupt. */
  35291.    void ctrlchandler()
  35292.    {
  35293.       int c;
  35294.       char str[] = " ";
  35295.  
  35296.       /* Disallow CTRL+C during handler. */
  35297.       signal( SIGINT, SIG_IGN );
  35298.  
  35299.       safeout( "User break - abort processing? " );
  35300.       c = safein();
  35301.       str[0] = c;
  35302.       safeout( str );
  35303.       safeout( "\r\n" );
  35304.       if( (c == 'y') || (c == 'Y') )
  35305.          abort();
  35306.       else
  35307.  
  35308.    /* The CTRL+C interrupt must be reset to our handler since
  35309.           * by default it is reset to the system handler.
  35310.           */
  35311.          signal( SIGINT, ctrlchandler );
  35312.    }
  35313.  
  35314.    /* Outputs a string using system level calls. */
  35315.    void safeout( char *str )
  35316.    {
  35317.    #if defined( OS2 )
  35318.       VioWrtTTY( str, strlen( str ), 0 );
  35319.    #else
  35320.       union REGS inregs, outregs;
  35321.  
  35322.       inregs.h.ah = 0x0e;
  35323.       while( *str )
  35324.       {
  35325.          inregs.h.al = *str++;
  35326.          int86( 0x10, &inregs, &outregs );
  35327.       }
  35328.    #endif
  35329.    }
  35330.  
  35331.    /* Inputs a character using system level calls. */
  35332.    int safein()
  35333.    {
  35334.    #if defined( OS2 )
  35335.       KBDKEYINFO kki;
  35336.  
  35337.       KbdCharIn( &kki, IO_WAIT, 0 );
  35338.       return kki.chChar;
  35339.    #else
  35340.       return _bios_keybrd( _KEYBRD_READ ) & 0xff;
  35341.    #endif
  35342.    }
  35343.  
  35344.  
  35345.  Output
  35346.  
  35347.  
  35348.  
  35349.    ASCII code: 74
  35350.    ASCII code: 68
  35351.    ASCII code: 65
  35352.    ^C
  35353.    User break - abort processing? n
  35354.    ASCII code: 62
  35355.    ASCII code: 1B
  35356.  
  35357.  
  35358.  
  35359.  
  35360.  
  35361.  sin Functions
  35362.  ────────────────────────────────────────────────────────────────────────────
  35363.  
  35364.  
  35365.  Description
  35366.  
  35367.  Calculate sines and hyperbolic sines.
  35368.  
  35369.    #include <math.h>
  35370.  
  35371.    double sin( double x );
  35372.  
  35373.    double sinh( double x );
  35374.  
  35375.    long double sinl( long double x );
  35376.  
  35377.    long double sinhl( long double x );
  35378.  
  35379.  x                                 Angle in radians
  35380.  
  35381.  
  35382.  Remarks
  35383.  
  35384.  The sin and sinh functions find the sine and hyperbolic sine of x,
  35385.  respectively. The sinl and sinhl functions are the 80-bit counterparts and
  35386.  use an 80-bit, 10-byte coprocessor form of arguments and return values. See
  35387.  the reference page on the long double functions for more details on this
  35388.  data type.
  35389.  
  35390.  
  35391.  Return Value
  35392.  
  35393.  The sin functions return the sine of x. If x is large, a partial loss of
  35394.  significance in the result may occur, and sin generates a PLOSS error. If x
  35395.  is so large that significance is completely lost, the sin function prints a
  35396.  TLOSS message to stderr and returns 0. In both cases, errno is set to
  35397.  ERANGE.
  35398.  
  35399.  The sinh function returns the hyperbolic sine of x. If the result is too
  35400.  large, sinh sets errno to ERANGE and returns ±HUGE_VAL.
  35401.  
  35402.  
  35403.  Compatibility
  35404.  
  35405.  sin, sinh
  35406.  
  35407.   ANSI   DOS   OS/2   UNIX   XENIX
  35408.  
  35409.  
  35410.  sinl, sinhl
  35411.  
  35412.   ANSI   DOS   OS/2   UNIX   XENIX
  35413.  
  35414.  
  35415.  
  35416.  See Also
  35417.  
  35418.  acos functions, asin functions, atan functions, cos functions, tan functions
  35419.  
  35420.  
  35421.  
  35422.  Example
  35423.  
  35424.    /* SINCOS.C: This program displays the sine, hyperbolic sine, cosine,
  35425.     * and hyperbolic cosine of pi / 2.
  35426.     */
  35427.  
  35428.    #include <math.h>
  35429.    #include <stdio.h>
  35430.  
  35431.    void main()
  35432.    {
  35433.       double pi = 3.1415926535;
  35434.       double x, y;
  35435.  
  35436.       x = pi / 2;
  35437.       y = sin( x );
  35438.       printf( "sin( %f ) = %f\n", x, y );
  35439.       y = sinh( x );
  35440.       printf( "sinh( %f ) = %f\n",x, y );
  35441.       y = cos( x );
  35442.       printf( "cos( %f ) = %f\n", x, y );
  35443.       y = cosh( x );
  35444.       printf( "cosh( %f ) = %f\n",x, y );
  35445.    }
  35446.  
  35447.  
  35448.  Output
  35449.  
  35450.  
  35451.  
  35452.    sin( 1.570796 ) = 1.000000
  35453.    sinh( 1.570796 ) = 2.301299
  35454.    cos( 1.570796 ) = 0.000000
  35455.    cosh( 1.570796 ) = 2.509178
  35456.  
  35457.  
  35458.  
  35459.  
  35460.  
  35461.  sopen
  35462.  ────────────────────────────────────────────────────────────────────────────
  35463.  
  35464.  
  35465.  Description
  35466.  
  35467.  Opens a file for file sharing.
  35468.  
  35469.  #include <fcntl.h>
  35470.  
  35471.  #include <sys\types.h>
  35472.  
  35473.  #include <sys\stat.h>
  35474.  
  35475.  #include <share.h>
  35476.  
  35477.  #include <io.h>                   Required only for function declarations
  35478.  
  35479.    int sopen( char *filename, int oflag, int shflag [[,  int pmode ]] );
  35480.  
  35481.  filename                          File name
  35482.  
  35483.  oflag                             Type of operations allowed
  35484.  
  35485.  shflag                            Type of sharing allowed
  35486.  
  35487.  pmode                             Permission setting
  35488.  
  35489.  
  35490.  Remarks
  35491.  
  35492.  The sopen function opens the file specified by filename and prepares the
  35493.  file for subsequent shared reading or writing, as defined by oflag and
  35494.  shflag. The integer expression oflag is formed by combining one or more of
  35495.  the following manifest constants, defined in the file FCNTL.H. When two or
  35496.  more constants are used to form the argument oflag, the constants are
  35497.  combined with the OR operator ( | ).
  35498.  
  35499.  Constant                          Meaning
  35500.  ────────────────────────────────────────────────────────────────────────────
  35501.  O_APPEND                          Repositions the file pointer to the end
  35502.                                    of the file before every write
  35503.                                    operation.
  35504.  
  35505.  O_BINARY                          Opens file in binary (untranslated)
  35506.                                    mode. (See fopen for a description of
  35507.                                    binary mode.)
  35508.  
  35509.  O_CREAT                           Creates and opens a new file. This has
  35510.                                    no effect if the file specified by
  35511.                                    filename exists.
  35512.  
  35513.  O_EXCL                            Returns an error value if the file
  35514.                                    specified by filename exists. This
  35515.                                    applies only when used with O_CREAT.
  35516.  
  35517.  O_RDONLY                          Opens file for reading only. If this
  35518.                                    flag is given, neither the O_RDWR flag
  35519.                                    nor the O_WRONLY flag can be given.
  35520.  
  35521.  O_RDWR                            Opens file for both reading and writing.
  35522.                                    If this flag is given, neither O_RDONLY
  35523.                                    nor O_WRONLY can be given.
  35524.  
  35525.  O_TEXT                            Opens file in text (translated) mode.
  35526.                                    (See fopen for a description of text
  35527.                                    mode.)
  35528.  
  35529.  O_TRUNC                           Opens and truncates an existing file to
  35530.                                    0 bytes. The file must have write
  35531.                                    permission; the contents of the file are
  35532.                                    destroyed.
  35533.  
  35534.  O_WRONLY                          Opens file for writing only. If this
  35535.                                    flag is given, neither O_RDONLY nor
  35536.                                    O_RDWR can be given.
  35537.  
  35538.  The argument shflag is a constant expression consisting of one of the
  35539.  following manifest constants, defined in SHARE.H. If SHARE.COM (or SHARE.EXE
  35540.  for some versions of DOS) is not installed, DOS ignores the sharing mode.
  35541.  (See your system documentation for detailed information about sharing
  35542.  modes.)
  35543.  
  35544.  Constant                          Meaning
  35545.  ────────────────────────────────────────────────────────────────────────────
  35546.  SH_COMPAT                         Sets compatibility mode (not available
  35547.                                    in OS/2). This is the sharing mode used
  35548.                                    in the open function in DOS.
  35549.  
  35550.  SH_DENYRW                         Denies read and write access to file.
  35551.  
  35552.  SH_DENYWR                         Denies write access to file.
  35553.  
  35554.  SH_DENYRD                         Denies read access to file.
  35555.  
  35556.  SH_DENYNO                         Permits read and write access. This is
  35557.                                    the sharing mode used in the open
  35558.                                    function in OS/2.
  35559.  
  35560.  The sopen function should be used only under OS/2 and DOS versions 3.0 and
  35561.  later. Under earlier versions of DOS, the shflag argument is ignored.
  35562.  
  35563.  The pmode argument is required only when O_CREAT is specified. If the file
  35564.  does not exist, pmode specifies the file's permission settings, which are
  35565.  set when the new file is closed for the first time. Otherwise, the pmode
  35566.  argument is ignored. The pmode argument is an integer expression that
  35567.  contains one or both of the manifest constants S_IWRITE and S_IREAD, defined
  35568.  in SYS\STAT.H. When both constants are given, they are combined with the OR
  35569.  operator ( | ). The meaning of the pmode argument is as follows:
  35570.  
  35571.  Value                             Meaning
  35572.  ────────────────────────────────────────────────────────────────────────────
  35573.  S_IWRITE                          Writing permitted
  35574.  
  35575.  S_IREAD                           Reading permitted
  35576.  
  35577.  S_IREAD | S_IWRITE                Reading and writing permitted
  35578.  
  35579.  If write permission is not given, the file is read-only. Under DOS and OS/2,
  35580.  all files are readable; it is not possible to give write-only permission.
  35581.  Thus, the modes S_IWRITE and S_IREAD | S_IWRITE are equivalent.
  35582.  
  35583.  Note that under DOS versions 3.x with SHARE installed, a problem occurs when
  35584.  opening a new file with sopen under the following sets of conditions:
  35585.  
  35586.  
  35587.    ■   With oflag set to O_CREAT | O_RDONLY or O_CREAT | WRONLY, pmode set to
  35588.        S_IREAD, and shflag set to SH_COMPAT.
  35589.  
  35590.    ■   With oflag set to any combination that includes O_CREAT | O_RDWR,
  35591.        pmode set to S_IREAD, and shflag set to anything other than SH_COMPAT.
  35592.  
  35593.  
  35594.  In either case, the operating system will prematurely close the file during
  35595.  system calls made within sopen, or the system will generate a sharing
  35596.  violation (INT 24H). To avoid the problem, open the file with pmode set to
  35597.  S_IWRITE. After closing the file, call chmod and change the mode back to
  35598.  S_IREAD. Another solution is to open the file with pmode set to S_IREAD,
  35599.  oflag set to O_CREAT | O_RDWR, and shflag set to SH_COMPAT.
  35600.  
  35601.  The sopen function applies the current file-permission mask to pmode before
  35602.  setting the permissions (see umask).
  35603.  
  35604.  
  35605.  Return Value
  35606.  
  35607.  The sopen function returns a file handle for the opened file. A return value
  35608.  of -1 indicates an error, and errno is set to one of the following values:
  35609.  
  35610.  Value                             Meaning
  35611.  ────────────────────────────────────────────────────────────────────────────
  35612.  EACCES                            Given path name is a directory; or the
  35613.                                    file is read-only but an open for
  35614.                                    writing was attempted; or a sharing
  35615.                                    violation occurred (the file's sharing
  35616.                                    mode does not allow the specified
  35617.                                    operations; OS/2 and DOS versions 3.0
  35618.                                    and later only).
  35619.  
  35620.  EEXIST                            The O_CREAT and O_EXCL flags are
  35621.                                    specified, but the named file already
  35622.                                    exists.
  35623.  
  35624.  EINVAL                            An invalid oflag or shflag argument was
  35625.                                    given.
  35626.  
  35627.  EMFILE                            No more file handles available (too many
  35628.                                    open files).
  35629.  
  35630.  ENOENT                            File or path name not found.
  35631.  
  35632.  
  35633.  Compatibility
  35634.  
  35635.   ANSI   DOS   OS/2   UNIX   XENIX
  35636.  
  35637.  
  35638.  
  35639.  See Also
  35640.  
  35641.  close, creat, fopen, _fsopen, open, umask
  35642.  
  35643.  
  35644.  Example
  35645.  
  35646.  See the example for locking.
  35647.  
  35648.  
  35649.  
  35650.  
  35651.  
  35652.  spawn Functions
  35653.  ────────────────────────────────────────────────────────────────────────────
  35654.  
  35655.  
  35656.  Description
  35657.  
  35658.  Create and execute a new child process.
  35659.  
  35660.  #include <stdio.h>
  35661.  
  35662.  #include <process.h>
  35663.  
  35664.    int spawnl( int mode, char *cmdname, char *arg0,  char *arg1, ... char
  35665.    *argn, NULL );
  35666.  
  35667.    int spawnle( int mode, char *cmdname, char *arg0, char *arg1, ... char
  35668.    *argn, NULL, char **envp );
  35669.  
  35670.    int spawnlp( int mode, char *cmdname, char *arg0, char *arg1, ... char
  35671.    *argn, NULL );
  35672.  
  35673.    int spawnlpe( int mode, char *cmdname, char *arg0, char *arg1, ... char
  35674.    *argn, NULL, char **envp );
  35675.  
  35676.    int spawnv( int mode, char *cmdname, char **argv );
  35677.  
  35678.    int spawnve( int mode, char *cmdname, char **argv, char  **envp );
  35679.  
  35680.    int spawnvp( int mode, char *cmdname, char **argv );
  35681.  
  35682.    int spawnvpe( int mode, char *cmdname, char **argv, char **envp );
  35683.  
  35684.  mode                              Execution mode for parent process
  35685.  
  35686.  cmdname                           Path name of file to be executed
  35687.  
  35688.  arg0, ... argn                    List of pointers to arguments
  35689.  
  35690.  argv                              Array of pointers to arguments
  35691.  
  35692.  envp                              Array of pointers to environment
  35693.                                    settings
  35694.  
  35695.  
  35696.  Remarks
  35697.  
  35698.  The spawn family of functions creates and executes a new child process.
  35699.  Enough memory must be available for loading and executing the child process.
  35700.  The mode argument determines the action taken by the parent process before
  35701.  and during spawn. The following values for mode are defined in PROCESS.H:
  35702.  
  35703.  Value                             Meaning
  35704.  ────────────────────────────────────────────────────────────────────────────
  35705.  P_DETACH                          Continues to execute the parent process;
  35706.                                    child process is run in the background
  35707.                                    with no access to the console or
  35708.                                    keyboard. Calls to wait and cwait
  35709.                                    against the child process will fail.
  35710.                                    This is an asynchronous detached spawn
  35711.                                    and is valid only in OS/2 protected
  35712.                                    mode.
  35713.  
  35714.  P_NOWAIT                          Continues to execute parent process
  35715.                                    concurrently with child process
  35716.                                    (asynchronous spawn, valid only in
  35717.                                    protected mode).
  35718.  
  35719.  P_NOWAITO                         Continues to execute parent process and
  35720.                                    ignores wait and cwait calls against
  35721.                                    child process (asynchronous spawn, valid
  35722.                                    only in protected mode).
  35723.  
  35724.  P_OVERLAY                         Overlays parent process with child,
  35725.                                    destroying the parent (same effect as
  35726.                                    exec calls).
  35727.  
  35728.  P_WAIT                            Suspends parent process until execution
  35729.                                    of child process is complete
  35730.                                    (synchronous spawn).
  35731.  
  35732.  The cmdname argument specifies the file which will be executed as the child
  35733.  process, and can specify a full path (from the root), a partial path (from
  35734.  the current working directory), or just a file name. If cmdname does not
  35735.  have a file-name extension or does not end with a period (.), the spawn
  35736.  function first tries the .COM extension, then the .EXE extension, and
  35737.  finally the .BAT extension (or, in OS/2 protected mode, the .CMD extension).
  35738.  This ability to spawn batch files is a new feature in Microsoft C version
  35739.  6.0.
  35740.  
  35741.  If cmdname has an extension, only that extension is used. If cmdname ends
  35742.  with a period, the spawn calls search for cmdname with no extension. The
  35743.  spawnlp, spawnlpe, spawnvp, and spawnvpe routines search for cmdname (using
  35744.  the same procedures) in the directories specified by the PATH environment
  35745.  variable.
  35746.  
  35747.  If cmdname contains a drive specifier or any slashes (i.e., if it is a
  35748.  relative path name), the spawn call searches only for the specified file and
  35749.  no path searching is done.
  35750.  
  35751.  
  35752.  Arguments for the Child Process
  35753.  
  35754.  Arguments are passed to the child process by giving one or more pointers to
  35755.  character strings as arguments in the spawn call. These character strings
  35756.  form the argument list for the child process. The combined length of the
  35757.  strings forming the argument list for the child process must not exceed 128
  35758.  bytes in real mode. The terminating null character ('\0') for each string is
  35759.  not included in the count, but space characters (automatically inserted to
  35760.  separate arguments) are included.
  35761.  
  35762.  The argument pointers may be passed as separate arguments (spawnl, spawnle,
  35763.  spawnlp, and spawnlpe) or as an array of pointers (spawnv, spawnve, spawnvp,
  35764.  and spawnvpe). At least one argument, arg0 or argv[0], must be passed to the
  35765.  child process. By convention, this argument is the name of the program as it
  35766.  might be typed on the command line by the user. (A different value will not
  35767.  produce an error.) In real mode, the argv[0] value is supplied by the
  35768.  operating system and is the fully qualified path name of the executing
  35769.  program. In protected mode, it is usually the program name as it would be
  35770.  typed on the command line.
  35771.  
  35772.  The spawnl, spawnle, spawnlp, and spawnlpe calls are typically used in cases
  35773.  where the number of arguments is known in advance. The arg0 argument is
  35774.  usually a pointer to cmdname. The arguments arg1 through argn are pointers
  35775.  to the character strings forming the new argument list. Following argn,
  35776.  there must be a NULL pointer to mark the end of the argument list.
  35777.  
  35778.  The spawnv, spawnve, spawnvp, and spawnvpe calls are useful when the number
  35779.  of arguments to the child process is variable. Pointers to the arguments are
  35780.  passed as an array, argv. The argument argv[0] is usually a pointer to a
  35781.  path name in real mode or to the program name in protected mode, and argv[1]
  35782.  through argv[n] are pointers to the character strings forming the new
  35783.  argument list. The argument argv[n+1] must be a NULL pointer to mark the end
  35784.  of the argument list.
  35785.  
  35786.  
  35787.  Environment of the Child Process
  35788.  
  35789.  Files that are open when a spawn call is made remain open in the child
  35790.  process. In the spawnl, spawnlp, spawnv, and spawnvp calls, the child
  35791.  process inherits the environment of the parent. The spawnle, spawnlpe,
  35792.  spawnve, and spawnvpe calls allow the user to alter the environment for the
  35793.  child process by passing a list of environment settings through the envp
  35794.  argument. The argument envp is an array of character pointers, each element
  35795.  of which (except for the final element) points to a null-terminated string
  35796.  defining an environment variable. Such a string usually has the form
  35797.  
  35798.  NAME=value
  35799.  
  35800.  where NAME is the name of an environment variable and value is the string
  35801.  value to which that variable is set. (Note that value is not enclosed in
  35802.  double quotation marks.) The final element of the envp array should be NULL.
  35803.  When envp itself is NULL, the child process inherits the environment
  35804.  settings of the parent process.
  35805.  
  35806.  The spawn functions can pass the child process all information about open
  35807.  files, including the translation mode, through the C_FILE_INFO entry in the
  35808.  environment that is passed in real mode (_C_FILE_INFO in protected mode).
  35809.  
  35810.  The C start-up code normally processes this entry and then deletes it from
  35811.  the environment. However, if a spawn function spawns a non-C process (such
  35812.  as CMD.EXE), this entry remains in the environment. Printing the environment
  35813.  shows graphics characters in the definition string for this entry, since the
  35814.  environment information is passed in binary form in real mode. It should not
  35815.  have any other effect on normal operations. In protected mode, the
  35816.  environment information is passed in text form and therefore contains no
  35817.  graphics characters.
  35818.  
  35819.  You must explicitly flush (using fflush or flushall) or close any stream
  35820.  prior to the spawn function call.
  35821.  
  35822.  Starting with Microsoft C version 6.0, you can control whether or not the
  35823.  open file information of a process will be passed to its child processes.
  35824.  The external variable _fileinfo (declared in STDLIB.H) controls the passing
  35825.  of C_FILE_INFO information. If _fileinfo is 0, the C_FILE_INFO information
  35826.  is not passed to the child processes. If _fileinfo is not 0, C_FILE_INFO is
  35827.  passed to child processes.
  35828.  
  35829.  By default, _fileinfo is 0 and thus the C_FILE_INFO information is not
  35830.  passed to child processes. There are two ways to modify the default value of
  35831.  _fileinfo:
  35832.  
  35833.  
  35834.    ■   Link the supplied object file FILEINFO.OBJ into your program. Use the
  35835.        /NOE option to avoid multiple symbol definitions.
  35836.  
  35837.    ■   Set the _fileinfo variable to a nonzero value directly within your C
  35838.        program.
  35839.  
  35840.  
  35841.  
  35842.  Return Value
  35843.  
  35844.  The return value from a synchronous spawn (P_WAIT specified for mode) is the
  35845.  exit status of the child process.
  35846.  
  35847.  The return value from an asynchronous spawn (P_NOWAIT or P_NOWAITO specified
  35848.  for mode) is the process ID. To obtain the exit code for a process spawned
  35849.  with P_NOWAIT, you must call the wait or cwait function and specify the
  35850.  process ID. The exit code cannot be obtained for a process spawned with
  35851.  P_NOWAITO.
  35852.  
  35853.  The exit status is 0 if the process terminated normally. The exit status can
  35854.  be set to a nonzero value if the child process specifically calls the exit
  35855.  routine with a nonzero argument. If the child process did not explicitly set
  35856.  a positive exit status, a positive exit status indicates an abnormal exit
  35857.  with an abort or an interrupt. A return value of -1 indicates an error (the
  35858.  child process is not started). In this case, errno is set to one of the
  35859.  following values:
  35860.  
  35861.  Value                             Meaning
  35862.  ────────────────────────────────────────────────────────────────────────────
  35863.  E2BIG                             In DOS, the argument list exceeds 128
  35864.                                    bytes, or the space
  35865.                                    required for the environment information
  35866.                                    exceeds 32K. In OS/2, the argument list
  35867.                                    and the space required for environment
  35868.                                    information combined exceed 32K.
  35869.  
  35870.  EINVAL                            The mode argument is invalid.
  35871.  
  35872.  ENOENT                            The file or path name is not found.
  35873.  
  35874.  ENOEXEC                           The specified file is not executable or
  35875.                                    has an invalid
  35876.                                    executable-file format.
  35877.  
  35878.  ENOMEM                            Not enough memory is available to
  35879.                                    execute the child process.
  35880.  
  35881.  Note that signal settings are not preserved in child processes created by
  35882.  calls to spawn routines. The signal settings are reset to the default in the
  35883.  child process.
  35884.  
  35885.  
  35886.  Compatibility
  35887.  
  35888.   ANSI   DOS   OS/2   UNIX   XENIX
  35889.  
  35890.  
  35891.  The spawn functions, with P_OVERLAY mode, will not work in OS/2
  35892.  DOScompatibility mode in programs which are bound with FAPI for dual-mode
  35893.  execution.
  35894.  
  35895.  Programs linked as DOS mode .EXE files will work, and protected-mode
  35896.  programs will work. The restriction applies only to bound programs in real
  35897.  mode.
  35898.  
  35899.  In order to ensure proper overlay initialization and termination, do not use
  35900.  the setjmp or longjmp functions to enter or leave an overlay routine.
  35901.  
  35902.  
  35903.  See Also
  35904.  
  35905.  abort, atexit, exec functions, exit, _exit, onexit, system
  35906.  
  35907.  
  35908.  Example
  35909.  
  35910.    /* SPAWN.C: This program accepts a number in the range 18 from the
  35911.     * command line. Based on the number it receives, it executes one of the
  35912.     * eight different procedures that spawn the process named child. For
  35913.     * some of these procedures, the CHILD.EXE file must be in the
  35914.     * same directory; for others, it only has to be in the same path.
  35915.     */
  35916.  
  35917.    #include <stdio.h>
  35918.    #include <process.h>
  35919.  
  35920.    char *my_env[] =
  35921.    {
  35922.       "THIS=environment will be",
  35923.       "PASSED=to child.exe by the",
  35924.       "SPAWNLE=and",
  35925.       "SPAWNLPE=and",
  35926.       "SPAWNVE=and",
  35927.       "SPAWNVPE=functions",
  35928.       NULL
  35929.    };
  35930.  
  35931.    void main( int argc, char *argv[] )
  35932.    {
  35933.       char *args[4];
  35934.       int  result;
  35935.  
  35936.       /* Set up parameters to be sent: */
  35937.       args[0] = "child";
  35938.       args[1] = "spawn??";
  35939.       args[2] = "two";
  35940.       args[3] = NULL;
  35941.       switch (argv[1][0])   /* Based on first letter of argument */
  35942.       {
  35943.          case '1':
  35944.             spawnl( P_WAIT, argv[2], argv[2], "spawnl", "two", NULL );
  35945.             break;
  35946.          case '2':
  35947.             spawnle( P_WAIT, argv[2], argv[2], "spawnle", "two",
  35948.                      NULL, my_env );
  35949.             break;
  35950.          case '3':
  35951.             spawnlp( P_WAIT, argv[2], argv[2], "spawnlp", "two", NULL );
  35952.             break;
  35953.          case '4':
  35954.             spawnlpe( P_WAIT, argv[2], argv[2], "spawnlpe", "two",
  35955.                       NULL, my_env );
  35956.             break;
  35957.          case '5':
  35958.             spawnv( P_OVERLAY, argv[2], args );
  35959.             break;
  35960.          case '6':
  35961.             spawnve( P_OVERLAY, argv[2], args, my_env );
  35962.             break;
  35963.          case '7':
  35964.             spawnvp( P_OVERLAY, argv[2], args );
  35965.             break;
  35966.          case '8':
  35967.             spawnvpe( P_OVERLAY, argv[2], args, my_env );
  35968.             break;
  35969.          default:
  35970.             printf( "SYNTAX: SPAWN <1-8> <childprogram>\n" );
  35971.             exit( 1 );
  35972.       }
  35973.       printf( "\n\nReturned from SPAWN!\n" );
  35974.    }
  35975.  
  35976.  
  35977.  
  35978.  
  35979.  
  35980.  _splitpath
  35981.  ────────────────────────────────────────────────────────────────────────────
  35982.  
  35983.  
  35984.  Description
  35985.  
  35986.  Breaks a path name into components.
  35987.  
  35988.    #include <stdlib.h>
  35989.  
  35990.    void _splitpath( char *path, char *drive, char *dir, char *fname, char
  35991.    *ext );
  35992.  
  35993.  path                              Full path name
  35994.  
  35995.  drive                             D