O
OzBob
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
#include <stdio.h>
#define SLASH "/"
int has_slash(char in_str[24])
{
int i=0;
while (in_str != *SLASH && in_str != NULL && i < 100) i++;
/* wrong format */
if (in_str != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);
}
void main()
{
int test_true;
/* PROVEN ON BOTH PA_RISC AND SPARC */
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");
if (strstr(test_date, SLASH) == NULL) printf(" By self- not found
slash\
n");
str_result=strstr(test_date, "/");
if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */
/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/
(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
#include <stdio.h>
#define SLASH "/"
int has_slash(char in_str[24])
{
int i=0;
while (in_str != *SLASH && in_str != NULL && i < 100) i++;
/* wrong format */
if (in_str != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);
}
void main()
{
int test_true;
/* PROVEN ON BOTH PA_RISC AND SPARC */
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");
if (strstr(test_date, SLASH) == NULL) printf(" By self- not found
slash\
n");
str_result=strstr(test_date, "/");
if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */
/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/