home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs17.zoo / makebuf.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  2.2 KB  |  82 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified for GNU iostream by Per Bothner 1991.
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "%W% (Berkeley) %G%";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <ioprivat.h>
  25. #include <fstream.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28.  
  29. #ifdef atarist
  30. extern "C" unsigned long __DEFAULT_BUFSIZ__;
  31. #undef BUFSIZ
  32. #define BUFSIZ __DEFAULT_BUFSIZ__
  33. #endif
  34.  
  35. #ifndef S_ISCHR
  36. #  define S_ISCHR(X) (((X) & S_IFMT) == S_IFCHR)
  37. #endif
  38.  
  39. /*
  40.  * Allocate a file buffer, or switch to unbuffered I/O.
  41.  * Per the ANSI C standard, ALL tty devices default to line buffered.
  42.  *
  43.  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
  44.  * optimisation) right after the _fstat() that finds the buffer size.
  45.  */
  46. int filebuf::doallocate()
  47. {
  48.     register size_t size, couldbetty;
  49.     register char *p;
  50.     struct stat st;
  51.  
  52.     if (fd() < 0 || _fstat(fd(), &st) < 0) {
  53.     couldbetty = 0;
  54.     size = _G_BUFSIZ;
  55. #if 0
  56.     /* do not try to optimise fseek() */
  57.     fp->_flags |= __SNPT;
  58. #endif
  59.     } else {
  60.     couldbetty = S_ISCHR(st.st_mode);
  61. #if (!defined(atarist)) && _G_HAVE_ST_BLKSIZE
  62.     size = st.st_blksize <= 0 ? _G_BUFSIZ : st.st_blksize;
  63. #else
  64.     size = _G_BUFSIZ;
  65. #endif
  66.     }
  67. #ifdef USE_MALLOC_BUF
  68.     if ((p = (char *)malloc(size)) == NULL) {
  69.     unbuffered(1);
  70. //    fp->_bf._base = fp->_p = fp->_nbuf;
  71. //    fp->_bf._size = 1;
  72.     return EOF;
  73.     }
  74. #else
  75.     p = ALLOC_BUF(size);
  76. #endif
  77.     setb(p, p+size, 1);
  78.     if (couldbetty && _isatty(fd()))
  79.     _flags |= _S_LINE_BUF;
  80.     return 1;
  81. }
  82.