Article View: comp.std.c
Article #3719Re: gets() and getchar
From: Peter Holzer
Date: Mon, 14 Aug 1995 12:24
Date: Mon, 14 Aug 1995 12:24
48 lines
2357 bytes
2357 bytes
wirzeniu@cc.Helsinki.FI (Lars Wirzenius) writes:
>One thing I would like to see (if we're going to have a new standard
>anyway, that is) is something that does what strncpy should have done.
>I call mine strmaxcpy:
>char *strmaxcpy(char *tgt, const char *src, size_t n) {
Yes, something like that is clearly needed. In my own personal library
I have Karl Heuer's (I think) nstring functions.
nstrlimcpy is the equivalent to your strmaxcpy there, except that
it returns a pointer to the _end_ of the target (so you can
concatenate strings with linear cost) and it takes a pointer to
the end of the target area (which stays the same during concatenation).
To create a file name out of several components you would do something
like the following:
char filename[FILENAME_MAX];
char *filename_end = filename + FILENAME_MAX;
char *p;
p = nstrlimcpy (filename, dir, filename_end);
p = nstrlimcpy (p, "/", filename_end);
p = nstrlimcpy (p, basename, filename_end);
p = nstrlimcpy (p, ".", filename_end);
p = nstrlimcpy (p, extension, filename_end);
To do something like this safely is currently a lot more difficult than
it has to be.
Also does anyone else feel that strncpy and strncat aren't very useful?
strncpy is at least useful for one purpose (which IMHO rarely happens
in real programs), but to get a useful 3rd parameter for strncat I
have to know the length of the target. If I already know that I see no
reason why strcat should look for the end of the target again.
Both functions (and a lot of others) should be marked obsolete in the
next revision of the standard.
hp
--
_ | Peter Holzer | Systemadministrator WSR | hjp@wsr.ac.at
|_|_) |-------------------------------------------------------
| | | ...and it's finished! It only has to be written.
__/ | -- Karl Lehenbauer
Technical Details
Message-ID:
<40nf9d$8nh@wsrcom.wsr.ac.at>Path:
arc1-ca.pugleaf.net!wsrcom.wsr.ac.at!wsrdb!hjpReferences:
<Pine.ULT.3.91.950807194041.19202A-100000@mars> <KANZE.95Aug9103309@slsvhdt.lts.sel.alcatel.de> <40bqih$bft@caip.rutgers.edu> <1995Aug11.002333.20047@dcs.warwick.ac.uk> <40fn05$686@kruuna.helsinki.fi>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!wsrcom.wsr.ac.at!wsrdb!hjp From: hjp@wsrdb.wsr.ac.at (Peter Holzer) Newsgroups: comp.std.c Subject: Re: gets() and getchar Date: 14 Aug 1995 12:24:13 GMT Organization: WSR, Vienna, Austria Message-ID: <40nf9d$8nh@wsrcom.wsr.ac.at> References: <Pine.ULT.3.91.950807194041.19202A-100000@mars> <KANZE.95Aug9103309@slsvhdt.lts.sel.alcatel.de> <40bqih$bft@caip.rutgers.edu> <1995Aug11.002333.20047@dcs.warwick.ac.uk> <40fn05$686@kruuna.helsinki.fi> NNTP-Posting-Host: wsrdb.wsr.ac.at