Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105078
| From | fred flintstone <fred@server.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: writing a c program to substitute text |
| Date | 2017-02-28 04:20 -0800 |
| Message-ID | <ehl892F1h4hU1@mid.individual.net> (permalink) |
| References | <ehfv4oFu00sU1@mid.individual.net> <6b1df453-b6df-3cf8-eb6e-255c37ef241a@verizon.net> |
On 02/26/2017 03:55 PM, James R. Kuyper wrote:
> On 02/26/2017 07:14 AM, fred flintstone wrote:
> ...
>> So there's 3DBESNUB type things ...
>
> Is this
> <https://www.physics.byu.edu/faculty/christensen/Physics%20121/3D%20BE%20SNUB.htm>
> what you're referring to?
Yeah. I'm trying to paint the pictures that I need to to make it around
the bases on this one. Better usenet practice is when OP does not force
readers to follow links, as these might be suspect. I'm left to paint a
picture with words.
I want to draw the attention of respondents down here, as I announce
myself safe on some type of first base, fulfilling D1 of the three D's.
D1 might be a portrait of the client of the C application. The client
is, first and foremost, me, and then everyone I take care of with the
same methods. Many are simple pioneers, who don't need to be dished out
to what's Out There.
>
>> ... Right
>> at the top is that I don't know how to declare strings in c in a safe
>> way.
>>
>> Let's say that you're hard-coding a string between single quotes, for
>
> If it's between single quotes, it's not a string, it's a single
> character constant. "The value of an integer character constant
> containing more than one character (e.g., 'ab'), or containing a
> character or escape sequence that does not map to a single-byte
> execution character, is implementation-defined." (6.4.4.4p10). Such
> constants are very seldom used. The fact that their value is
> implementation-defined makes them useless for portable code. I'd guess
> that most C programmers are unaware that such a construct is even
> permitted. Therefore, I doubt that it's what you actually meant. You
> probably meant double quotes, and that's what my answers farther down
> assume.
>
> In case you're wondering what character constants containing multiple
> characters are useful for, here's some examples of what's permitted:
>
> Two trivial possibilities are that 'arrow' == 'a', or 'arrow' == 'w'.
>
> Note that character constants, despite their name, have values of type
> 'int'. Therefore, on a system where char is unsigned, and int has more
> than 5*CHAR_BIT bits, an implementation could choose to define that
>
> 'arrow' == (((('a'*CHAR_MAX + 'r')*CHAR_MAX + 'r')*CHAR_MAX + 'o')*
> CHAR_MAX + 'w')
>
Thank you for comment which brought me off single quotes.
>> example:
>>
>> 'arrow'
>>
>> , how do I declare this string to exist safely?
>
> That depends upon which danger you're hoping to avoid. Define the
> danger, and we can tell you the best way to minimize it. However, don't
> expect too much. C is a very low-level language - it does very little to
> protect you from shooting yourself in the foot. It's the language used
> to efficiently implement many of the safer languages. Here's several
> different options, each of which has different advantages and
> disadvantages:
>
> #define ARROW "arrow"
>
> const char *parrow = "arrow";
> char arrow[] = "arrow";
> // Either of those definitions could be declared static
> // Either of those definitions could appear at either file scope or
> // block scope.
>
> parrow = malloc(sizeof "arrow"));
> memcpy(parrow, "arrow", sizeof "arrow");
>
Listen to me james. Have you ever used 'malloc' as a verb? If you have a
declaration like:
const char *parrow = "arrow";
, can one say that it has been malloced, or is it only correct to use
the grown-up term of "allocated?" (Might there be a malloc right under
the patina?)
>> Do I malloc a 1000 because someone has to make an adult decision?
>
> That depends entirely upon what danger you're trying to protect against.
> As a general rule, if that solution helps you avoid a danger, then that
> danger involves bad coding practices. In that case, you're better off
> avoiding that danger by by improving your coding practices than by
> allocating 1000 bytes to hold a 5-character string.
This source considers some minimal cases. I like to write quines.
Douglas Hofstadter's Goedel Escher Bach set me back 44 marks, a lot of
money to me.
Anyways, the idea is that the source is reproduced faithfully. I'm not
doing it in a strict mathematical sense (yet). I'll begin with a source
listing of the driving .sh, avec warnings, the output thereof, and then
ask questions. So, tja:
$ sh toolchain2.sh
sub1.c: In function ‘main’:
sub1.c:13:15: warning: unused variable ‘ext’ [-Wunused-variable]
const char *ext = "sh";
^
sub1.c:12:15: warning: unused variable ‘base’ [-Wunused-variable]
const char *base = "toolchain";
^
sub1.c:11:15: warning: unused variable ‘executable2’ [-Wunused-variable]
const char *executable2 = "mahershala";
^
sub1.c:10:15: warning: unused variable ‘executable1’ [-Wunused-variable]
const char *executable1 = "arrow";
^
sub1.c:8:7: warning: unused variable ‘version’ [-Wunused-variable]
int version, c, i;
^
$ pwd
/home/bob/Documents/programming/Book Examples/ch02/command_line
$ ls -l
total 44
-rw-rw-r-- 1 bob bob 1804 Feb 26 01:03 arrow.c
-rw-rw-r-- 1 bob bob 1008 Feb 27 17:42 awk3
-rwxrwxr-x 1 bob bob 9104 Feb 27 18:25 sub1
-rw-rw-r-- 1 bob bob 950 Feb 27 18:24 sub1.c
-rw-rw-r-- 1 bob bob 1030 Feb 27 18:15 sub1.c~
-rw-rw-r-- 1 bob bob 969 Feb 27 18:25 text1.txt
-rw-rw-r-- 1 bob bob 103 Feb 27 18:22 toolchain2.sh
-rwxrwxr-x 1 bob bob 273 Feb 26 01:51 toolchain3.sh
-rw-rw-r-- 1 bob bob 273 Feb 27 18:25 toolchain4.sh
$ cat toolchain2.sh
gcc -Wall -Wextra -o sub1 sub1.c >text1.txt
./sub1 arrow mahershala >>text1.txt
cat sub1.c >>text1.txt
$ cat text1.txt
3
arrow
mahershala
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (int argc, char *argv[])
{
int version, c, i;
FILE *fptr, *gptr;
const char *executable1 = "arrow";
const char *executable2 = "mahershala";
const char *base = "toolchain";
const char *ext = "sh";
printf ("%d\n", argc);
for (i = 1; i < argc; i++) {
printf ("%s\n", argv[i]);
}
fptr = fopen ("toolchain3.sh", "r");
if (fptr == NULL) {
printf ("fopen no bueno");
exit (1);
}
gptr = fopen ("toolchain4.sh", "w");
if (gptr == NULL) {
printf ("gopen no bueno");
exit (1);
}
while (1) {
c = fgetc (fptr);
if (c == EOF)
break;
else
putc (c, gptr);
}
fclose (fptr);
fclose (gptr);
return 0;
}
char *
mystdrdup (const char *str)
{
size_t len = strlen (str);
char *answer = malloc (len + 1);
if (answer)
strcpy (answer, str);
else { /* error handling here */
};
return answer;
}
$
Q1) Is this standard, portable, contemporary C?
Q2) This program shall be legal C. What constraints does that put on the
cumulative size on what comes from argv?
The specification of this program will handle the case that what is
passed to it exceeds the allowable parameters. Is it INT_MIN?
Q3) I have Plauger's _Standard C Library_ under my bed somewhere. How
would the list of necessary .h files differ from then to contemporary C?
Thank you for your comment, and remember, some people warm up to a topic....
--
fred flintstone
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-26 04:14 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-26 04:55 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-26 14:28 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 03:58 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:11 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 13:33 +0000
Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 17:12 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 17:59 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:05 +0100
Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 18:37 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 11:40 -0800
Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:03 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 05:33 -0800
Re: writing a c program to substitute text "D. Aaron Sawyer" <aaron@110.net> - 2017-03-01 10:32 -0400
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 06:59 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 15:24 +0000
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:47 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 11:05 +0000
Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 13:33 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:10 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:07 +0100
Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 18:23 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 14:46 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:27 -0500
Re: writing a c program to substitute text GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-27 15:28 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:38 -0500
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 15:44 +0100
Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 10:36 -0500
Re: writing a c program to substitute text Anglican <h.toms@qmul.ac.uk> - 2017-02-27 15:50 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 17:13 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 16:30 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:08 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:20 -0800
Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:24 -0800
Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:00 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:22 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:11 +0100
Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-02-28 15:41 -0800
solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:25 -0800
Re: solved Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-03-02 11:28 +0100
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 10:54 +0000
Re: solved Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-03-02 15:28 +0100
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 14:51 +0000
Re: solved Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-03-02 14:18 -0800
Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 23:55 +0000
Re: solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-14 18:32 -0700
Re: writing a c program to substitute text Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 07:04 -0700
Re: writing a c program to substitute text jladasky@itu.edu - 2017-02-27 13:48 -0800
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 14:16 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 08:47 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 01:59 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 11:39 +0100
Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-28 12:03 +0000
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:33 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:43 +0100
Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:59 +0000
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 04:26 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 23:29 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 15:10 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 06:42 -0800
Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-26 16:54 +0000
[OT] "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 00:56 +0800
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 13:36 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 14:53 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 00:53 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 17:30 -0800
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 17:44 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 01:46 +0000
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 09:29 +0100
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 02:12 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 13:11 +0100
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 04:41 -0800
Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 05:04 -0800
Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:25 +0000
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:56 -0800
Re: writing a c program to substitute text "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 01:00 +0800
Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-26 18:55 -0500
Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 04:20 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:38 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:33 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 19:38 +0100
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 11:40 -0800
Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 21:03 +0100
Re: gcc optimization levels Noob <root@127.0.0.1> - 2017-02-28 22:55 +0100
Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-28 11:43 -0500
Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:46 -0800
csiph-web