strccpy

P

proxy foxy

Hi,

I understand from the solaris man page that strccpy compress the c
escape sequence from the source and copy the string to target.
I wrote the following program to realize the same. But the output
seams to be same. Please let me know in what way can I realize the
output:

$> gcc temp.c -o temp -lgen

int main ()
{
const char *s1="Hello\nWorld";
char *s2 = (char *)malloc (30);
printf ("s1[%d] : %s\n", strlen(s1), s1);
s2 = strccpy (s2, s1);

printf ("s2[%d] : %s\n", strlen(s2), s2);

}

I know this isn't a standard C function. Thought of seeking help
however
 
G

Gene

Hi,

I understand from the solaris man page that strccpy compress the c
escape sequence from the source and copy the string to target.
I wrote the following program to realize the same. But the output
seams to be same. Please let me know in what way can I realize the
output:

$> gcc temp.c -o temp -lgen

int main ()
{
        const char *s1="Hello\nWorld";
        char *s2 = (char *)malloc (30);
        printf ("s1[%d] : %s\n", strlen(s1), s1);
        s2 = strccpy (s2, s1);

        printf ("s2[%d] : %s\n", strlen(s2), s2);

}

I know this isn't a standard C function. Thought of seeking help
however

No. The compiler honors the escape sequences during scanning and
parsing of a C string literal. By the time strcpy() runs, the escapes
are long gone.

The 's2 =' in

s2 = strccpy (s2, s1);

is redundant but not erroneous.


The (char *) in the line with malloc() is redundant.

You could replace the malloc() and strcpy() with strdup(). But this
isn't strictly required.
 
P

proxy foxy

I understand from the solaris man page that strccpy compress the c
escape sequence from the source and copy the string to target.
I wrote the following program to realize the same. But the output
seams to be same. Please let me know in what way can I realize the
output:
$> gcc temp.c -o temp -lgen
int main ()
{
        const char *s1="Hello\nWorld";
        char *s2 = (char *)malloc (30);
        printf ("s1[%d] : %s\n", strlen(s1), s1);
        s2 = strccpy (s2, s1);
        printf ("s2[%d] : %s\n", strlen(s2), s2);

I know this isn't a standard C function. Thought of seeking help
however

No.  The compiler honors the escape sequences during scanning and
parsing of a C string literal. By  the time strcpy() runs, the escapes
are long gone.

The 's2 =' in

       s2 = strccpy (s2, s1);

is redundant but not erroneous.

The (char *) in the line with malloc() is redundant.

You could replace the malloc() and strcpy() with strdup().  But this
isn't strictly required.

This is strccpy and not strcpy. For 'char *', I have non-ANSI
compiler. For 's2 =', I agree with you.
 
N

Nick Keighley

I understand from the solaris man page that strccpy compress the c
escape sequence from the source and copy the string to target.
I wrote the following program to realize the same. But the output
seams to be same. Please let me know in what way can I realize the
output:
$> gcc temp.c -o temp -lgen
int main ()
{
        const char *s1="Hello\nWorld";
        char *s2 = (char *)malloc (30);
        printf ("s1[%d] : %s\n", strlen(s1), s1);
        s2 = strccpy (s2, s1);
        printf ("s2[%d] : %s\n", strlen(s2), s2);
}
I know this isn't a standard C function. Thought of seeking help
however

No.  The compiler honors the escape sequences during scanning and
parsing of a C string literal. By  the time strcpy() runs, the escapes
are long gone.

The 's2 =' in

       s2 = strccpy (s2, s1);

is redundant but not erroneous.

The (char *) in the line with malloc() is redundant.

You could replace the malloc() and strcpy() with strdup().  But this
isn't strictly required.

and strdup() is non-standard
 
J

Jens Thoms Toerring

proxy foxy said:
I understand from the solaris man page that strccpy compress the c
escape sequence from the source and copy the string to target.
I wrote the following program to realize the same. But the output
seams to be same. Please let me know in what way can I realize the
output:
$> gcc temp.c -o temp -lgen
int main ()
{
const char *s1="Hello\nWorld";
char *s2 = (char *)malloc (30);
printf ("s1[%d] : %s\n", strlen(s1), s1);
s2 = strccpy (s2, s1);
printf ("s2[%d] : %s\n", strlen(s2), s2);

I know this isn't a standard C function. Thought of seeking help
however

I can't say for sure (since it's a non-standard function and
I don't have any strccpy() function) but it looks to me as if
your example program isn't a good use case of that function.
If you would start out with

const char *s1="Hello\\nWorld";

(note the double backslash) then the string will contain, after
the compiler is done with it, a backslash character directly
followed by a 'n'. And I guess that this strccpy() function is
meant to convert character sequences like this. In this case it
replaces the backslash and the 'n' by the single character '\n'.
So this function might be useful in cases where you have input
consisting of C code (or some other language that uses the same
escape character convention) and you want to extract the strings
there-in and convert them to strings like the compiler would do
it. Might be nice to have when you write an interpretor or com-
piler.
Regards, Jens
 
C

Curtis Dyer

proxy foxy said:
Hi,

I understand from the solaris man page that strccpy compress
the c escape sequence from the source and copy the string to
target. I wrote the following program to realize the same. But
the output seams to be same. Please let me know in what way can
I realize the output:

As you mention, it's not a Standard C function, but from what I can
tell, it seems you have reversed strccpy()'s concept (see below).
$> gcc temp.c -o temp -lgen

Do you have the necessary #include's in your actual code? In the
code you post here, you're missing <stdio.h>, <string.h>, and
int main ()
{
const char *s1="Hello\nWorld";

Try changing this to:

const char *s1 = "Hello\\nWorld";

I think you'll see the slip in your reasoning.
char *s2 = (char *)malloc (30);

You should check the return value of malloc(). Personally, I
generally like to do so even in most test programs.

Also, the cast here is unnecessary. If you've indeed forgotten to
the cast would likely mask a diagnostic that said:
printf ("s1[%d] : %s\n", strlen(s1), s1);

I don't think it's an issue in this program, but it seems pretty
typical to use `%lu' for size_t values. In C99, I believe `%zu'
would be the appropriate conversion specifier for representing
size_t objects' values.

printf("s1[%lu] : %s\n", (unsigned long)strlen(s1), s1);

I know this isn't a standard C function. Thought of seeking
help however

You might try checking the mailing lists of the vendors of
particular libraries. In this case, there may be a newsgroup that
covers Solaris programming.
 
P

proxy foxy

proxy foxy said:
I understand from the solaris man page that strccpy compress
the c escape sequence from the source and copy the string to
target.  I wrote the following program to realize the same. But
the output seams to be same. Please let me know in what way can
I realize the output:

As you mention, it's not a Standard C function, but from what I can
tell, it seems you have reversed strccpy()'s concept (see below).
$> gcc temp.c -o temp -lgen

Do you have the necessary #include's in your actual code?  In the
code you post here, you're missing <stdio.h>, <string.h>, and
int main ()
{
        const char *s1="Hello\nWorld";

Try changing this to:

  const char *s1 = "Hello\\nWorld";

I think you'll see the slip in your reasoning.
        char *s2 = (char *)malloc (30);

You should check the return value of malloc().  Personally, I
generally like to do so even in most test programs.

Also, the cast here is unnecessary.  If you've indeed forgotten to
the cast would likely mask a diagnostic that said:
        printf ("s1[%d] : %s\n", strlen(s1), s1);

I don't think it's an issue in this program, but it seems pretty
typical to use `%lu' for size_t values.  In C99, I believe `%zu'
would be the appropriate conversion specifier for representing
size_t objects' values.

  printf("s1[%lu] : %s\n", (unsigned long)strlen(s1), s1);

I know this isn't a standard C function. Thought of seeking
help however

You might try checking the mailing lists of the vendors of
particular libraries.  In this case, there may be a newsgroup that
covers Solaris programming.


Thanks to you all. I got the same reply from comp.unix.solaris
http://groups.google.com/group/comp.unix.solaris/browse_thread/thread/8f3a71ef29706e51#
 
B

Ben Bacarisse

For 'char *', I have non-ANSI compiler.

So what does malloc return, then, in this non-ANSI implementation? All
the non-ANSI systems I've used have malloc return a char * so the cast
would be redundant in those cases as well.

I suspect you have not fully understood what is happening will malloc
and its return value/type. The program you showed would only be correct
if malloc were defined to return an int and that is most unlikely.

The reason this matters is that getting function return types and/or
prototypes wrong is important -- more important than the \\
misunderstanding that caused you to post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top