OzBob wrote:
> "Michael Mair" <> wrote in message
> news:...
>
>>OzBob wrote:
>>
>>>I am developing some basic string / file manipulation C progams at home
>>>(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
>>>running HP-UX 11.11 and c99 compatible compiler). However I seem to have
>>>encountered problems performing the most basic of manipulations and
>>>comparisons.
>>>
>>>I have posted my test code below, and the results I get on each compiler.
>>>The crux of the matter is that the 'strstr' function differs wildly in
>>>its behaviour on different platforms, and precious few man pages will
>>>actually provide a worked example of any function, let alone this one. I
>>>have had to write the "has_slash" function and use the reserved work
>>>SLASH already, to get around the problems that strstr() gives me.
>>>
>>>I know C is touted as an standardised development environment that
>>>provides portability (the immortal phrase "we converted to another
>>>platform by typing 'make'" springs to mind); I'm not seeing that right
>>>now.
>>>
>>>Any advise / comments? Share and Enjoy, Ian
>>
>>
>>Yes: Compile with the highest warning level and in a standard C
>>mode. E.g.
>>
>>$ gcc -std=c99 -pedantic -W -Wall -O strstr.c -c
>>strstr.c: In function `has_slash':
>>strstr.c:9: warning: comparison between pointer and integer
>>strstr.c: At top level:
>>strstr.c:24: warning: return type of 'main' is not `int'
>>strstr.c: In function `main':
>>strstr.c:35: warning: implicit declaration of function `strcpy'
>>strstr.c:36: warning: implicit declaration of function `strstr'
>>strstr.c:36: error: missing terminating " character
>>strstr.c:37: error: stray '\' in program
>>strstr.c:37: error: `slash' undeclared (first use in this function)
>>strstr.c:37: error: (Each undeclared identifier is reported only once
>>strstr.c:37: error: for each function it appears in.)
>>strstr.c:37: error: parse error before "n"
>>strstr.c:36: warning: empty body in an if-statement
>>strstr.c:37: error: missing terminating " character
>>strstr.c:38: error: `n' undeclared (first use in this function)
>>strstr.c:38: error: missing terminating " character
>>strstr.c:39: error: missing terminating " character
>>
>>where the include directive is on line 1.
>>
>>Your program does not compile -- so how can you expect us to
>>look at it?
>>
<snip>
> Micheal,
I'm not that Irish
> Have submitted the commands for gcc, with the following response. Have also
> checked the code and string.h is not included. I have since modified the
> comments at the end, included the <string.h> library at the start, and
> succesfully compiled using the commands provided. I even went a step further
> and removed the "-c" option from the command string to generate "a.out",
> which (in the interests of consistency) fails at the same point with the
> same error.
>
> I figure its the strstr command and the assignment of a pointer, which
> again, works fine on the HP-UX 'cc' compiler (the full one, not the inbuilt
> kernel-only one) with the '-AC99' c99 compatibility option, yet bails on the
> gcc compiler.
>
> Any further ideas? Share and Enjoy, OzBob
>
> /* original compiler output */
> # gcc -std=c99 -pedantic -W -Wall -O test.c -c
> test.c:24: warning: return type of `main' is not `int'
> test.c: In function `main':
> test.c:35: warning: implicit declaration of function `strcpy'
> test.c:36: warning: implicit declaration of function `strstr'
> test.c:53:1: unterminated comment
>
> /* included <string.h>, terminated comment */
> # gcc -std=c99 -pedantic -W -Wall -O test.c
> test.c:25: warning: return type of `main' is not `int'
Note: This is not to be taken lightly -- in principle, all
bets are off for a "void main" main().
> ls # -l a.out
> -rwxr-xr-x 1 root other 7280 Jan 15 15:04 a.out
> # ./a.out
> In function - i = 6
> Has slash = true
> By self- not found slash
> Segmentation Fault - core dumped
> #
<snip>
Look at this one; it is yours with slight changes and works
as I expect:
#include <stdio.h>
#include <string.h>
#define SLASH "/"
int has_slash (const char *in_str);
int main (void)
{
int test_true;
test_true = has_slash("copy /f file");
if (test_true != 0)
printf("Has slash = true\n");
/* New test, strstr results */
char *str_result;
char test_date[24];
strcpy(test_date, "02_04_2005");
if (strstr(test_date, SLASH) != NULL)
printf(" By self- found slash\n");
else
printf(" By self- not found slash\n");
str_result=strstr(test_date, "/");
if (str_result != NULL)
printf("Found slash: %s[%s]\n",
str_result, test_date);
if (str_result == NULL)
printf("Not found slash: \'\\0\'[%s]\n",
test_date);
return 0;
}
int has_slash (const char *in_str)
{
int i=0;
while (in_str[i] != *SLASH && in_str[i] != '\0'
/* && i < 100 */)
i++;
/* wrong format */
i = (in_str[i] != *SLASH) ? 0 : i+1;
printf("In function - i = %i\n", i);
return(i);
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.