Pagina 1 di 2

strnstr

Inviato: mar 30 set 2008, 17:44
da conraid
Chiedo aiuto ai programmatori C, io non ci capisco niente

Volevo compilare un programma che fa uso della funziona strnstr. Da quel che ho capito è una funzione compresa nella libreria C BSD, ma non in quella GNU
L'autore del programma mi ha detto di sistemare tramite automake ed autoconf, ma sinceramente non so dove andare a parare

come posso fare?

Tra l'altro avrei il sorgente si strnstr.c

Il pezzo di codice è

Codice: Seleziona tutto

        if (search_string) {
                uint8_t *body;
                uint32_t bodylen;
                if (strnstr((char *)body, search_string, bodylen) == 0) {
                        return (0);
                }

        }

Il config.log mi da questo

Codice: Seleziona tutto

onftest.c:72: undefined reference to `strnstr'
collect2: ld returned 1 exit status
configure:5205: $? = 1
configure: failed program was:
| /* confdefs.h.  */
| #define PACKAGE_NAME ""
| #define PACKAGE_TARNAME ""
| #define PACKAGE_VERSION ""
| #define PACKAGE_STRING ""
| #define PACKAGE_BUGREPORT ""
| #define PACKAGE ""
| #define VERSION "1.0"
| #define __USE_BSD 1
| #define HAVE_LIBNSL 1
| #define STDC_HEADERS 1
| #define HAVE_SYS_WAIT_H 1
| #define HAVE_SYS_TYPES_H 1
| #define HAVE_SYS_STAT_H 1
| #define HAVE_STDLIB_H 1
| #define HAVE_STRING_H 1
| #define HAVE_MEMORY_H 1
| #define HAVE_STRINGS_H 1
| #define HAVE_INTTYPES_H 1
| #define HAVE_STDINT_H 1
| #define HAVE_UNISTD_H 1
| #define HAVE_FCNTL_H 1
| #define HAVE_SYS_IOCTL_H 1
| #define HAVE_SYS_TIME_H 1
| #define HAVE_UNISTD_H 1
| #define TIME_WITH_SYS_TIME 1
| #define RETSIGTYPE void
| #define HAVE_GETTIMEOFDAY 1
| #define HAVE_SELECT 1
| #define HAVE_SOCKET 1
| #define HAVE_STRDUP 1
| #define HAVE_STRERROR 1
| #define HAVE_STRTOL 1
| #define HAVE_SETEUID 1
| #define HAVE_ERR 1
| #define HAVE_STRSEP 1
| /* end confdefs.h.  */
| /* Define strnstr to an innocuous variant, in case <limits.h> declares strnstr.
|    For example, HP-UX 11i <limits.h> declares gettimeofday.  */
| #define strnstr innocuous_strnstr
|
| /* System header to define __stub macros and hopefully few prototypes,
|     which can conflict with char strnstr (); below.
|     Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
|     <limits.h> exists even on freestanding compilers.  */
|
| #ifdef __STDC__
| # include <limits.h>
| #else
| # include <assert.h>
| #endif
|
| #undef strnstr
|
| /* Override any GCC internal prototype to avoid an error.
|    Use char because int might match the return type of a GCC
|    builtin and then its argument prototype would still apply.  */
| #ifdef __cplusplus
| extern "C"
| #endif
| char strnstr ();
| /* The GNU C library defines this for functions which it implements
|     to always fail with ENOSYS.  Some functions are actually named
|     something starting with __ and the normal name is an alias.  */
| #if defined __stub_strnstr || defined __stub___strnstr
| choke me
| #endif
|
| int
| main ()
| {
| return strnstr ();
|   ;
|   return 0;
| }
configure:5223: result: no


Re: strnstr

Inviato: mar 30 set 2008, 18:03
da targzeta
Se mi dai il link ai sorgenti vedo se riesco a compilarlo io.

Spina

Re: strnstr

Inviato: mar 30 set 2008, 18:15
da Mario Vanoni
Scusa Corrado
man strstr
quindi
strcasestr(3)
sotto Linux.

Re: strnstr

Inviato: mar 30 set 2008, 18:16
da conraid
spina ha scritto:Se mi dai il link ai sorgenti vedo se riesco a compilarlo io.

Spina
Ho aggiunto una mia funzione fatta così nel sorgente, secondo te va bene?

Codice: Seleziona tutto

char *
strnstr(const char *body, const char *string_search, size_t bodylen)
{
    char *str = strstr(body, string_search);
    if (!str)
        return NULL;
    if (str-body+strlen(string_search) <= bodylen)
        return str;
    else
        return NULL;
}
da dei warning adesso, ma compila


il programma lo trovi qui
http://monkey.org/~provos/dnsscan-1.0.tar.gz

Re: strnstr

Inviato: mar 30 set 2008, 18:18
da conraid
Mario Vanoni ha scritto:Scusa Corrado
man strstr
quindi
strcasestr(3)
sotto Linux.
Scusa Mario, strcasestr c'è anche in BSD
dal man di linux

Codice: Seleziona tutto

       The  strstr()  function  finds the first occurrence of the substring needle in the string haystack.  The
       terminating '\0' characters are not compared.

       The strcasestr() function is like strstr(), but ignores the case of both arguments.

mentre invece dal man di BSD

Codice: Seleziona tutto

     The strstr() function locates the first occurrence of the null-terminated
     string s2 in the null-terminated string s1.

     The strcasestr() function is similar to strstr(), but ignores the case of
     both strings.

     The strnstr() function locates the first occurrence of the null-termi-
     nated string s2 in the string s1, where not more than n characters are
     searched.  Characters that appear after a `\0' character are not
     searched.  Since the strnstr() function is a FreeBSD specific API, it
     should only be used when portability is not a concern.

Re: strnstr

Inviato: mar 30 set 2008, 18:20
da conraid
conraid ha scritto:
Mario Vanoni ha scritto:Scusa Corrado
man strstr
quindi
strcasestr(3)
sotto Linux.
Scusa Mario, strcasestr c'è anche in BSD
dal man di linux

Codice: Seleziona tutto

       The  strstr()  function  finds the first occurrence of the substring needle in the string haystack.  The
       terminating '\0' characters are not compared.

       The strcasestr() function is like strstr(), but ignores the case of both arguments.

mentre invece dal man di BSD

Codice: Seleziona tutto

     The strstr() function locates the first occurrence of the null-terminated
     string s2 in the null-terminated string s1.

     The strcasestr() function is similar to strstr(), but ignores the case of
     both strings.

     The strnstr() function locates the first occurrence of the null-termi-
     nated string s2 in the string s1, where not more than n characters are
     searched.  Characters that appear after a `\0' character are not
     searched.  Since the strnstr() function is a FreeBSD specific API, it
     should only be used when portability is not a concern.
Dove dice chiaramente
Since the strnstr() function is a FreeBSD specific API, it
should only be used when portability is not a concern.

Re: strnstr

Inviato: mar 30 set 2008, 18:24
da conraid
Questo è il sorgente di strnstr.c su bsd

Codice: Seleziona tutto

#include <sys/types.h>
 #include <string.h>

  char *
  strnstr(const char *s, const char *find, size_t slen)
  {
  	char c, sc;
  	size_t len;
  
  	if ((c = *find++) != '\0') {
  		len = strlen(find);
  		do {
  			do {
  				if (slen < 1 || (sc = *s) == '\0')
 					return (NULL);
 				--slen;
 				++s;
 			} while (sc != c);
 			if (len > slen)
 				return (NULL);
 		} while (strncmp(s, find, len) != 0);
 		s--;
 	}
	return (__DECONST(char *, s));
 }
 

Re: strnstr

Inviato: mar 30 set 2008, 18:35
da targzeta
Ora sono a lavoro, quando torno a casa provo, ma, come dice il programmatore, è probabile che ti basti usare autoconf e automake.

Spina

Re: strnstr

Inviato: mar 30 set 2008, 18:50
da Mario Vanoni
Scusa Corrado

intendevo
strncasecmp(3)

lapsus linguae

Re: strnstr

Inviato: mar 30 set 2008, 18:56
da conraid
spina ha scritto:Ora sono a lavoro, quando torno a casa provo, ma, come dice il programmatore, è probabile che ti basti usare autoconf e automake.
ma come? :-(

Re: strnstr

Inviato: mar 30 set 2008, 19:13
da conraid
Il programmatore mi ha scritto dicendo che come ha cercato di dirmi fino alla noia il programma ha già una funziona chiamata strnstr e si trova nel file strnstr.c, ma io nei sorgenti non lo trovo :-(

Comunque facendo

Codice: Seleziona tutto

 sed -i 's/strnstr/strncasecmp/g' dnsreader.c  
secondo il suggerimento di Mario il sistema compila


Ho riscritto al programmatore, ora mi manda a quel paese :-)

Re: strnstr

Inviato: mar 30 set 2008, 20:49
da targzeta
conraid ha scritto:
spina ha scritto:Ora sono a lavoro, quando torno a casa provo, ma, come dice il programmatore, è probabile che ti basti usare autoconf e automake.
ma come? :-(
Entra nella directory dei sorgenti e fai

Codice: Seleziona tutto

autoreconf
configure
make
Dovrebbe bastare,
Spina

Re: strnstr

Inviato: mar 30 set 2008, 20:54
da conraid
spina ha scritto: Entra nella directory dei sorgenti e fai

Codice: Seleziona tutto

autoreconf
configure
make
Dovrebbe bastare,
Spina
No, da lo stesso errore

Re: strnstr

Inviato: mar 30 set 2008, 21:32
da targzeta
conraid ha scritto:...
No, da lo stesso errore
Si, hai ragione!!! Dopo aver compilato e installato libevent e libdnet con grande soddisfazione ho scoperto che anche a me da lo stesso errore :D.
Sembra che il codice non tenga conto della mancanza della strnstr, nonostante il configure.in ne lo faccia. Bhò.

Spina

Re: strnstr

Inviato: mar 30 set 2008, 21:33
da conraid
spina ha scritto:
conraid ha scritto:...
No, da lo stesso errore
Si, hai ragione!!! Dopo aver compilato e installato libevent e libdnet con grande soddisfazione ho scoperto che anche a me da lo stesso errore :D.
Sembra che il codice non tenga conto della mancanza della strnstr, nonostante il configure.in ne lo faccia. Bhò.

Spina
Secondo l'autore dovrebbe esserci un file strnstr.c e mi ha detto che me l'ha ripetuto fino alla noia.
Tu lo vedi?