HDTenEightyP/NetNews huggingface dataset:
From: gordon@sneaky.lonestar.org (Gordon Burditt)
Subject: Re: strcpy implementation question
Organization: Gordon Burditt
Date: Wed, 26 Aug 1992 22:49:04 GMT

>That's the problem.  I claim that copying past the end of the string is
>wrong, as the user may be building a string up backwards.  The compiler
>writers claim that there is nothing in the ANSI spec that claims that the
>memory past the destination's terminator is inviolate.

You won't find an explicit statement that the source string is inviolate,
either.  (On destructive-read machines using, say, actual core memory, you
might save time not re-writing the data you read.  This would be a very
wierd "optimization".)  Nor is there a statement that argc or any other 
random variable in the program is inviolate.  And there isn't any statement 
that evaluating "1+2" doesn't launch missiles.  But there's not much point 
in having a standard if doing anything can cause random side effects - the 
standard uses terms like "undefined behavior" or "implementation-defined 
behavior", and states limits on when these can happen.  Unless the source 
and destination overlap, the behavior is supposed to be defined.

>2. The user may be relying on a side effect.  By only copying until the end
>   of src, rather than until the end of the string, the string may not be
>   completely copied.  For example:

A better example would be the infamous "variable-length struct".
You declare something like:

	struct foo {
		int	type;
		... bunch of other stuff here ...
		char	name[1];
	} *fp;
	char	x[100];

and then use it like:

	fp = (struct foo *) malloc(sizeof(struct foo) + strlen(x));
	strcpy(fp->name, x);

"name" has supposedly known size.  x has supposedly known size.
The compiler probably aligned both of them.  But if the copying stops 
at 1 char or 1 word when x contains a 70-char string, I'm going to be 
annoyed.  And a lot of programs will break.

					Gordon L. Burditt
					sneaky.lonestar.org!gordon
From: jos@and.nl (Jos Horsmeier)
Newsgroups: comp.lang.c
Subject: Re: Only one char (on UNIX 386)
Keywords: char
Date: 30 Aug 92 12:23:29 GMT
Distribution: comp
Organization: AND Software BV Rotterdam

In article <1992Aug29.205157.6110@generic.uucp> vaso@generic.uucp (Vaso Karac) writes:
|I have problem with get only one character from keyboard,
|without using <return>.
|It will be same function as kbhit() on DOS.

|  /* it works only with <return> */
|  ret=read(_iob[0]._ptr,&buff,1);
| 
|Comment:
| #define stdin &_iob[0]       /* in stdio.h */

Just a few things: 

- never, ever, ever use the replacement text of a macro defined somewhere
  in a (system) header file. You're (and me neither) not supposed to
  know what's in there. Let the compiler do the dirty work. Just imagine
  what would happen if you're trying to port your code to a system where 
  `stdin' is defined in terms of something completely different and where 
  there's no _iob thingy defined anywhere ...

- try to get a copy of the FAQ list. Section 12.1 answers your question.
  Well actually it doesn't 'cause there is no portable way of doing this.
  Manipulating devices (like reading from a keyboard) is very system
  dependent and it has nothing to do with the C language actually.

- _if_ you're running on some Un*x box, read the ioctl(2) and fcntl(2) manual
  pages, and follow the `SEE ALSO' threads. 

- _if_ you're running some Un*x box and all else fails, try this:


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

int raw_key()
{
struct sgttyb tty, tty_org;
char          key;

	/* Read and save the original device settings */
	ioctl(fileno(stdin), TIOCGETP, &tty_org);
	tty = tty_org;

	/* Turn of all fancy translation modes and echo */
	tty.sg_flags |= CRMOD;
	tty.sg_flags &= ~ECHO;
	tty.sg_flags &= ~XTABS;
	tty.sg_flags |= RAW;

	ioctl(fileno(stdin), TIOCSETP, &tty);

	/* Get a key stroke */
	key= getchar();

	/* Back to the previous (probably sane) mode again */
	ioctl(fileno(stdin), TIOCSETP, &tty_org);

	return key;

}

kind regards,

Jos aka jos@and.nl
Newsgroups: comp.lang.c
From: blume@atomic (Matthias Blume)
Subject: Re: Novice Questions
Organization: Princeton University, Dept. of Computer Science
Date: Sun, 30 Aug 1992 23:21:45 GMT


View it this way:  array-names are (with a few exceptions regarding sizeof)
CONSTANT POINTERS to the beginning of the array.

The exceptions, however, are really confusing, though useful!

-m.b
From: jott@crash.cts.com (Joan Tine)
Newsgroups: comp.lang.c
Subject: Re: Serial routines for the PC ...
Summary: Eliminate the middleman...
Keywords: serial, comm, Turbo UART Anthony Villiers
Date: 25 Aug 92 07:54:02 GMT
Followup-To: comp.lang.c
Distribution: usa
Organization: CTS Network Services (crash, ctsnet), El Cajon, CA

In article <1992Aug19.134437.55@wronz.org.nz> mentink@wronz.org.nz writes:
>
>        Can anyone help please!
>
>        I am after some 'good' serial routines in 'C' for the IBM PC.
>        I've given up on writing calls to the BIOS routines, always
>        seem to work differently on diff machines.
>
>        Can anyone please supply some general purpose source for
>        setting up BAUD rate etc and some simple serial routines
>        that work.
>
>        I have been writing in 'C or C++' ( Borlandc ).
>        Please email me the source of post follow up article. Ta        
>
>        Thanks in advance.
>        
>        Bernie Mentink
>        W.R.O.N.Z
>        New zealand
>        email: mentink@wronz.org.nz

Well, the SAMS book "C Programmer's Guide to Serial Communications" by 
Campbell is pretty good...he starts out by implimenting a UART in 
C code(!) but the best code I've found (it works, the rest doesn't)
is in the MS-DOS Developer's Guide from SAMS.  After struggling again
and again with code which was "supposed" to work under Borland, I entered
and compiled the "serio.c" file in the latter, (compiled under Microsoft C)
and it ran perfectly.  I know a lot of people (I'm one) don't love uC, but
it does seem to work better in this instance.  Also, the "ring buffer" 
code is right there, so you don't  have to paw thru a data structures
book or (shudder) dig out your class notes.
From: mjn@pseudo.uucp (Murray Nesbitt)
Subject: Re: dynam mem alloc
Organization: Private system in Toronto, ON
Date: Wed, 26 Aug 1992 09:50:09 GMT


ecm@casbah.acns.nwu.edu (Edward Malthouse) writes:

>Suppose I have declared a pointer which I intend to eventully use to point to
>a chunk of memory allocated from the system heap using malloc.  How can I
>tell whether or not I have already allocated memory for this object?  I have
>been using the following:
>
>int *x;
>if(n == NULL) /* memory has not been allocated */
>
>This works fine as long as the pointer variable was declared as global or in
>main().  If it is declared elsewhere this statement does not work.  For
>example:

Any object with static storage duration that is not explicitly
initialized will be implicitly initialized as if each member were
assigned 0 or NULL, for arithmetic types and pointer types
respectively.

>#include <stdio.h>
>
>int *x;

This object will be initialized to NULL, as it has static storage
duration.

>
>main()
>{
>int *y;

This one will not be, as it has automatic storage duration.

>printstuff(y);
>}
>
>printstuff(y)
>int *y;
>{
>int *z;

Same for this one.

>
>printf("x=%d, y=%d, z=%d\n",x,y,z);
>}
>
>The output of this program is,
>x=0, y=0, z=-559038737
>
>Is there a reason why C does not initialize pointers declared in functions
>other than main() to NULL?  Is there portable way around this problem?

Yes, there is a portable way.  Use the storage-class specifier
``static''.

-- 
Murray
From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
Newsgroups: comp.lang.c
Subject: Re: Kwik escape from recursive loop, Possible?
Date: 30 Aug 92 04:25:00 GMT
Distribution: usa
Organization: Arizona State University, Tempe, AZ
News-Software: VAX/VMS VNEWS 1.4-b1

In article <1992Aug29.221941.20788@eng.ufl.edu>, 
zzang@whale.uucp (Zhuo Zang[~{j0WA~}]) writes...

> I am writing a program.  In the program, I use a 'while (c!=EOF)'
> statement to write char.  I also have a recursive call to get input char
> in the program, this caused problem. the recursive call piles up a huge
> stack when the c finally reaches the EOF; then it returns one by one, so
> my output file ended with many(48 in one eg.)  EOF's (the 0xff in DOS).

     Does the input routine have to be recursive?  If possible, find an
iterative solution.

> my question is:  is it possible to escape from the loop immediately
> without returning the garbages once c reaches the EOF ?

     Look up the documentation for the functions "setjmp" and "longjmp" in
the C library (I think the names are correct).  They allow you to perform
non-local GOTO's.

---
Phi-Long Tran
ptran@asuvax.eas.asu.edu
Arizona State University
From: jfw@ksr.com (John F. Woods)
Newsgroups: comp.lang.c
Subject: Re: Most difficult part of learning C?
Date: 27 Aug 92 13:24:12 EDT

goldstei@cgi.com ("Morris Goldstein") writes:
>I am conducting research for a little project concerning difficulties in
>learning C and how to improve the teaching of C so as to circumvent these
>problems.
>When learning C, what things did you find the most confussing/difficult to
>understand?

I don't remember anything being particularly confusing or difficult, but I
do remember that my most embarrasing mistake was assuming that pointers could
be treated as unsigned integers in a UNIX kernel modification I mailed out to
the UNIX-WIZARDS mailing list.  Fortunately, that happened 12 years ago, so
I've managed to live it down :-).
From: 6500mack@ucsbuxa.ucsb.edu (Michael P. Mack)
Newsgroups: comp.lang.c
Subject: How is NaN recognized?
Date: 29 Aug 92 19:06:29 GMT

I am using scanf() to read in a float that is entered by the user.  If
an invalid string is typed such as "-" or "." then the variable is
assigned -0.NaN(DFD5480000000000... or -0.NaN(DFD9400000000000000...

How do I detect when the float is a NaN so that I can set it to 0.0?


TxInAdv

Mike Mack
6500mack@ucsbuxa.ucsb.edu

Newsgroups: comp.lang.c,comp.unix.programmer
From: swanger@ducvax.auburn.edu
Subject: Realloc question
Organization: Auburn University, AL
Date: Fri, 4 Sep 1992 15:02:03 GMT

I think I need to use realloc in a program, but I'm not sure I understand
how to use it.  My program will dynamically allocate a two-dimension array
of strings using calloc statements.  There is a chance that I might need
to increase the size of this array later in the program, so I guess I need
to use realloc.  Here is a code fragment:

-----------------------------------------------------------------------

int num_strings, string_size, loop,;
char **strings;

num_strings = 100  /* will actually be entered by user */
string_size = 100  /* ditto                            */

strings = (char **) calloc(num_strings, sizeof(char*));

for (loop = 0; loop < num_strings; loop++)
{
   strings[loop] = (char *) calloc(string_size, sizeof(char));
}

-----------------------------------------------------------------------


Somewhere later in the program, I discover that I need more more strings. 
Is the following syntax correct?


strings = (char **) realloc((char **) strings, new_num_strings);


I don't want the existing strings to be longer, I just want to allocate more
pointers that can also be calloc'ed as strings.

I read the man pages for realloc on my system (SUN IPC, SUNOS 4.1.2) and I
THINK this is how to use it.  I would definitely appreciate help with this.

Thanks in advance!!
-- 
David Swanger,   Auburn University, Alabama,   swanger@ducvax.auburn.edu 
                                               swanger@mail.auburn.edu
Newsgroups: comp.lang.c
From: jaime_guerrero@afs.com
Subject: Re: Politically Correct C (was: Re: Freedom of Speech (was: Mister Ed Nilges)
Reply-To: jaime_guerrero@afs.com
Distribution: usa
Date: Tue, 1 Sep 1992 17:40:05 GMT

In article <19978@plains.NoDak.edu> hennebry@plains.NoDak.edu (Michael J.  
Hennebry) writes:
|  In article <1992Aug28.150040.21463@Princeton.EDU>  
egnilges@phoenix.Princeton.EDU (Ed Nilges) writes:
|  >but it is inexcusable to code
|  
|  >     if (!(farkle))
|  
|  >since it is obvious even to the tyro that the "scope" of the not
|  >operator ! is "farkle" (what else COULD it be??)  
|  
|  Without the "extra" parentheses it could be "fire" as in the following
|  example:
|  
|  boss.h: /* immutable if peon wants to keep his job */
|  
|  #define farkle fire || flood
|  
|  peon.c:
|  
|      if( !farkle ) {
|         fprintf(stderr, "Surprise.  ( !farkle ) =/= ( !(farkle) )\n");
|      }
|  -- 
|  Mike   hennebry@plains.NoDak.edu
|  "I still thing it's garbage, but beautifully cooked."   --  TP on TP

Safe macro practice would require you to write

   #define farkle (fire || flood)

because you never know in what context your macro will be expanded.
Similarly, you should always parenthesize macro arguments

   #define MULT(a,b)  ((a) * (b))

So that if someone uses MULT(1+4,5+2) you get 50 as expected not 23.

Also, to remind yourself and other programmers that you are using a macro with  
possible side effects and other dangers, their names should be in all caps,  
unlike the example given.

See chap 6 of _C Traps and Pitfalls_ by Andrew Koenig for the many dangers of  
careless use of macros.

--
Jaime F. Guerrero                              "How well do we use
Anderson Financial Systems, Inc.                The freedom to choose
Springhouse, PA, USA                            The illusions we create?"
jaime@afs.com          [NeXTmail accepted]      -Timbuk 3
Newsgroups: comp.lang.c
From: Ketan.Deshpande@bbs.oit.unc.edu (Ketan Deshpande)
Subject: Re: New Comer - Please Help !
Keywords: Help NewComer
Organization: Extended Bulletin Board Service
Date: Tue, 25 Aug 1992 17:13:17 GMT

In article <1992Aug25.000613.3610@geovision.gvc.com> pt@geovision.gvc.com writes:
>doug@neumann.une.edu.au (Doug Colman) writes:
>
>>DEAR FELLOW PROGRAMMERS:
>>I have not done a lot in "C" and would very much appreciate 
>>some of the basic tools that took a while to build for 
>>Pascal when I was using it. Things like "FileExists",
>>and other OP-system type things as well as a basic Mouse
>>Library, etc, if anyone would be so kind !
>>I am using Turbo C. Many Thanks !
>>-- DOUG.
>
>I would suggest you actually _buy_ TurboC instead of pirating it, and then
>RTFM.  There is a huge library of functions already there, and FileExists
>As for mouse libraries, sorry - my TurboC manual doesn't mention it.
>
>-- 
>Paul Tomblin, pt@geovision.gvc.com or {uunet,revcan}!geovision!pt

I don't see how Doug's letter leads to Paul's statement about _buying_ TurboC.
Can you help me out?
Ketan.

--
   The opinions expressed are not necessarily those of the University of
     North Carolina at Chapel Hill, the Campus Office for Information
        Technology, or the Experimental Bulletin Board Service.
           internet:  bbs.oit.unc.edu or 152.2.22.80
Newsgroups: comp.lang.c
From: steve@taumet.com (Steve Clamage)
Subject: Re: Why should POINTERS be so damn hard to understand ?
Organization: TauMetric Corporation
Date: Thu, 27 Aug 1992 16:12:45 GMT

chas@castle.ed.ac.uk (C Spencer) writes:


>Why does a pointer need to know what it's pointing to? Surely it's just
>an address - if there was only one kind of pointer, there wouldn't be so
>much to learn, and be confused by.

This has to do with mixing levels of abstraction, and how you choose
to look at programming.

Case 1: Programming is a method of causing a particular machine
	to behave in a particular way.
You don't care about types, and you probably  want a typeless langauge.
The language is just a convenient shorthand for machine instructions,
maybe with some control abstractions (if, loop, case).  This is quite
suitable for small embedded computers -- device controllers, for example.

Case 2: Programming is a method of expressing an algorithm or
	procedure for communication to humans and machines.
You want an abstract description, machine-independent, of the algorithm
or process.  You want types in the language, so that it can be made
plain what the interactions are intended to be.  This improves
communication among humans.  Stronger typing also leads to more help
from the compiler in finding many kinds of simple errors in writing
the code -- errors which would be much harder to find at run-time.

To answer the question more directly:

1.  When you read code and see that 's' points to a string, 'q' to a
floating-point number, 'p' to a structured object, isn't that more
help in understanding the program than when you have a bunch of
pointers and you don't know what they are pointing to?

2.  A pointer is not necessarily just an address.  On many machines
the size of and the interpretation of the bits in a pointer depend
on what it points to.  The type of the object pointed to is embedded,
at least in part, in the pointer.  The machine can detect at runtime
misuse of pointers.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16
Newsgroups: comp.lang.c
From: chans@lillee.cs.curtin.edu.au (Sean Chan)
Subject: Re: fscanf problem...
Organization: Curtin University of Technology
Date: Fri, 28 Aug 1992 04:49:59 GMT

chans@lillee.cs.curtin.edu.au (Sean Chan) writes:

>I'm trying to get fscanf to read each integer at a time but I get a core
>dump. It's used like this :-

>      fscanf (infile, "%d", val);
                             &val

Sorry to bother you guys...
Sean
Newsgroups: comp.lang.c
From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
Subject: Re: Giles' Manual Mania (Was - Re: About the 'F' in RTFM)
Organization: Texas Instruments Inc
Date: Wed, 26 Aug 1992 21:04:46 GMT

In <1992Aug25.161037.29594@newshost.lanl.gov> jlg@cochiti.lanl.gov (Jim Giles) writes:

>In article <1992Aug13.175711.27749@mksol.dseg.ti.com>, mccall@mksol.dseg.ti.com (fred j mccall 575-3539) writes:
>|> [...]
>|> Find another job, Jim.  One that lets you use something nice and
>|> simple like MS-DOS.

>This is an example of UNIX-speak.  This comment is frequently encountered,

No, Jim.  This is an example of how to speak to someone who is without
a clue and whining about that state.  

>though the proposed alternative varies.  The implication intended is that
>the only alternatives to UNIX are single-user systems like MS/DOS, or are
>old-line systems of the same obsolete 20 year old vintage of UNIX like
>DEC/VMS.  Even if these *were* the *only* alternatives, that would only

No, Jim.  The implication intended is that since I assume that you put
at least as much effort into LEARNING UNIX as you do into bitching
about it and still don't seem to get it, that perhaps you should stick
to something nice and simple.  About on the order of a stick and a
rock. 


-- 
"Insisting on perfect safety is for people who don't have the balls to live
 in the real world."   -- Mary Shafer, NASA Ames Dryden
------------------------------------------------------------------------------
Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
Newsgroups: comp.lang.c
From: edge@unislc.uucp (Brian Edginton)
Subject: Linking Microsoft lib with Borland .o's
Organization: Unisys Corporation SLC
Date: Fri, 4 Sep 1992 12:32:28 GMT

Sorry for posting here, no better place could I c.

I have picked up a job that has a toolkit compiled and lib'd 
for Microsoft 5.x.  I use Borland.  Is there a way to link
the .o's in the library into Borland produced objects or do
I have to go out and purchase the Microsoft compiler?

Any help appreciated.

Thanks,
 edge

-- 
Brian Edginton                | {hpda, sun, uplherc}!unislc!edge
Unisys                        | unislc!edge@cs.utah.edu
320 North 2200 West B2E10     | (801) 594-6009 Human (usually)
Salt Lake City, Utah 84116    | (801) 594-3827 Fax
Newsgroups: comp.lang.c
From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
Subject: Re: Only one char (on UNIX 386)
Originator: jlnance@volt.ece.ncsu.edu
Keywords: char
Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
Organization: North Carolina State University, Project Eos
Distribution: comp
Date: Mon, 31 Aug 1992 12:14:51 GMT


In article <1992Aug29.205157.6110@generic.uucp>, vaso@generic.uucp (Vaso Karac) writes:
|> I have problem with get only one character from keyboard,
|> without using <return>.
|> It will be same function as kbhit() on DOS.
|>  
|> I was trying with:
|> 

[ Code deleted ]

Unix uses buffered input for its input functions, including getchar().  This is
so you can use the backspace key to erase what you have typed in, and your
program does not ever see the backspace charactors being pressed.  The following
code will allow you to read in single charactors:

#include	<sgtty.h>
#include	<fcntl.h>

struct sgttyb normal_mode, cbreak_mode;

set_trap()
{
static int first_time = 1;
if (first_time)
	{
	ioctl(0, TIOCGETP, &normal_mode);
	cbreak_mode = normal_mode;
	cbreak_mode.sg_flags |= CBREAK;	/* make characters available as typed */
	cbreak_mode.sg_flags &= ~ECHO;	/* turn off echo */
	first_time = 0;
	}
ioctl(0, TIOCSETP, &cbreak_mode);
fcntl(0, F_SETFL, FNDELAY);
}

unset_trap()
{
fcntl(0, F_SETFL, 0);
ioctl(0, TIOCSETP, &normal_mode);
}

chk_buf()	/* read a character from the keyboard buffer */
		/* return '\0' if nothing available */
{
char c;
int status;
status = read(0, &c, 1);
if (status == -1) return(0);
else return(c & 0x7f);
}

Hope this helps,

Jim  Nance


PS The original author of this code is Dr. Tom Miller of North Carolina State
   University
Newsgroups: comp.lang.c
From: vaso@generic.uucp (Vaso Karac)
Subject: Only one char (on UNIX 386)
Distribution: comp
Date: Sat, 29 Aug 1992 20:51:57 GMT
Keywords: char

I have problem with get only one character from keyboard,
without using <return>.
It will be same function as kbhit() on DOS.
 
I was trying with:

#include <stdio.h>
#include <fcntl.h>

main()
{
 int x,ret,i;
 unsigned char buff;
 buff=0;

 for(;;){
  /* it works only with <return> */
  ret=read(_iob[0]._ptr,&buff,1);
  printf("%d  %X\n",ret,buff);
 
  /* it doesn't works */ 
  ret=read(_iob[0],&buff,1);
  printf("%d  %X\n",ret,buff);
 
  /* it doesn't works */
  ret=read(&_iob[0],&buff,1);
  printf("%d  %X\n",ret,buff);
 };
}
 
Comment:
 #define stdin &_iob[0]       /* in stdio.h */
 
Program could be stoped with interrupt

Thank's 
  Vaso


+
+-------------------------------------------------------------------+
I                                                                   I
I  Vaso Karac                                                       I
I                                                                   I
I           vaso@generic.UUCP                                       I
I                                                                   I
+-------------------------------------------------------------------+
Newsgroups: comp.lang.c,comp.sys.sun.misc