C bzero() to memset() issue

Hi guys,

my tool works fine in gentoo, ubuntu now im trying to port it to windows but bzero/bcopy I read aren't working on windows and for better portability I should of use memset() so im trying to translate

bzero(buffer,256);

in

printf("MAIL TO");

        
    strcpy(buffer, rcp);

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

to memset so I did,

memset(buffer, 0, 256);

and im getting "Error unrecognised command" when it reach the smtp...

tried also

memset(buffer, '/0', 256);
memset(buffer, 0, sizeof(buffer));

All ends up with the same error

Unrecognised command... but with

bzero(buffer,256);

Anyone can shed so light on this please?

Thanks

Do you have the correct includes? bzero() is defined in "strings.h", memset() is defined in "string.h" (note the lack of a second "s" in the later).

By the way, bzero() is considered deprecated in almost all OS.

Yeah got <string.h> here's my entire code I know its sloppy but its doing what i want so far on gentoo. Its just a draft so far i'll put some order in it as im going and im fixing things and learning as im doing it :slight_smile:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}


double menu(); 
char id1[4];
char amail[20] = "@primus.ca\r\n";
char id2[20];
char rcp[20] = "RCPT TO: ";


int a;

int main(int argc, char *argv[])
{

/*-----------------------------------------------------*/
 
  FILE *in;
  extern FILE *popen();
  char buff[1024];
  char newline[100];
  char nstat[1024];
  char nping[1024];
  char tracert[2048];
  char iconfig[1024];
  strcpy(newline, "\r\n");  



  if (!(in = popen("ping -c 3 www.primus.ca", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(nping, buff);
    }

  strcat(nping, newline); 
 

  pclose(in);

 /*-------------------------------------------*/

  if (!(in = popen("netstat -wan", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(nstat, buff);
    }

  strcat(nstat, newline); 
 

  pclose(in);

 /*-------------------------------------------*/

  if (!(in = popen("ifconfig", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(iconfig, buff);
    }

  strcat(iconfig, newline); 
 

  pclose(in);

 /*-------------------------------------------*/

  if (!(in = popen("traceroute www.primus.ca", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(tracert, buff);
    }

  strcat(tracert, newline); 
 

  pclose(in);





/*--------------------------------------------------------*/

    
    printf("%", menu());

    printf("Enter The Tech Support Agent ID: ");
    scanf("%s", id1);

    strcpy(id2, id1); 

    strcat(id2, amail);

    strcat(rcp, id2);


    
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[1024];
    if (argc < 1) {
       printf("error\n");
       exit(0);
    }

    argv[1] = "smtp.primus.ca";
    argv[2] = "25";    

    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    memset( (char *)&serv_addr, 0, sizeof( serv_addr ) );
    serv_addr.sin_family = AF_INET;
    memcpy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

memset(buffer, '\0', 256);


printf("Successfully connected to SMTP.PRIMUS.cA\n");

system("clear");


/*------------------------------*/

printf("EHLO");

    strcpy(buffer,"ehlo smtp.primus.ca\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/    

/*------------------------------*/

printf("AUTH");

    strcpy(buffer,"AUTH LOGIN\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

printf("AUTH UID");

    strcpy(buffer,"amR1bnBoeUBwcmltdXMuY2E=\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

printf("AUTH PWD");

    strcpy(buffer,"YXAwbGwwMTE=\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

printf("MAIL FROM");

    strcpy(buffer,"MAIL FROM: Test_Report@primus.ca\r\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

printf("MAIL TO");

        
    strcpy(buffer, rcp);

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, 0, 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);

/*------------------------------*/       

/*------------------------------*/

printf("DATA");

    strcpy(buffer,"DATA\r\n");

    n = write(sockfd,buffer,strlen(buffer));

    strcpy(buffer,"Subject: test\r\n");

    n = write(sockfd,buffer,strlen(buffer));


/*-----------add content bypass spamcontrol.ca----------*/

    strcpy(buffer,"[-------------------PING TEST----------------------]\n\n");

    n = write(sockfd,buffer,strlen(buffer));


/*----------------------sending ping-------------------------*/


    strcpy(buffer, nping);

    n = write(sockfd,buffer,strlen(buffer));


/*-----------------------------------------------------------*/


strcpy(buffer,"\n\n[-------------------NETSTAT TEST-------------------]\n\n");

    n = write(sockfd,buffer,strlen(buffer));

/*----------------------sending nstat-------------------------*/

   strcpy(buffer, nstat);

    n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,"\n\n[-------------------IFCONFIG TEST------------------]\n\n");

    n = write(sockfd,buffer,strlen(buffer));

/*----------------------sending ifconfig-------------------------*/

   strcpy(buffer, iconfig);

    n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,"\n\n[-------------------TRACEROUTE TEST----------------]\n\n");

    n = write(sockfd,buffer,strlen(buffer));

/*----------------------sending TRACEROUTE-------------------------*/

   strcpy(buffer, tracert);

    n = write(sockfd,buffer,strlen(buffer));

/*--------------------------------------------------------*/


    strcpy(buffer,"\n\n");

    n = write(sockfd,buffer,strlen(buffer));

    strcpy(buffer,".\n");

    n = write(sockfd,buffer,strlen(buffer));


/*------------------------------*/ 

/*------------------------------*/ 

printf("space 2");

    strcpy(buffer,"quit\n");

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    memset(buffer, '\0', 256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);


/*---------Finishing and exit msg---------------------*/

printf("Task complete disconnecting from SMTP.PRIMUS.cA\n");

system("clear");

printf("Report successfully sent to agent ID# %s \n\n", id1);

return 0;





}

double menu()
{

int bar();
int ping();
int ipconfig();
int netstat();
int traceroute();
int g,a;

        // Menu prompt//
        system("clear");
        printf(" -----------------------------------\n");
	printf("|===================================|\n");
        printf("|=| _ \\ _ \\_ _|  \\/  | | | / __|====|\n");
        printf("|=|  _/   /| || |\\/| | |_| \\__ \\====|\n");
        printf("|=|_| |_|_\\___|_|  |_|\\___/|___/====|\n");
        printf("|===================================|\n");
        printf("| Tech Support Network Swissknife   |\n");
        printf("|   VERSION: Alpha 1.0              |\n");
 	printf(" ----------------------------------- \n\n");
	printf(" ----------------------------------- \n");
        printf("|1: Complete Network Test           |\n"); 
        printf("|2: Reset TCP/ip and Reboot         |\n");
        printf("|3: Windows IP Settings reset       |\n");
        printf("|4: Winsock Reset                   |\n");
        printf("|5: Reset and Reconfigure Modem     |\n");
	printf("|6: Exit 		            |\n");	
	printf(" ---------------------------------- \n\n");
	printf("Enter your selection: ");
        scanf("%d", &g);


        system("clear");

         // TEST //


        printf("[----------------Initializing Network Tests----------------]\n");
        printf("\r");
        fflush(stdout);
        usleep(59000);
   

/* -----------Print the bar() function----------- */

printf("%", bar());

printf("\n");
printf("[----------------------------------------------------------]\n\n");

/* -----------Test report section---------------- */

int c;
char *d="#";

for(c=0;c<=100;c++)

{
        printf("GENERATING TEST REPORT: [%s %d%%]", d,c);
        printf("\r");
        fflush(stdout);
        usleep(59000);
}

        printf("\n");
	printf("\n");
	printf("\n");



        

     /* READ INPUT FROM MENU */



     if(a == 6)

    return 0;

}


/* ----------- BAR -------------- */

bar()
{

printf("%", ping());
printf("%", ipconfig());
printf("%", netstat());
printf("%", traceroute());

}




/* ---------------------PING bar function---------------- */

ping()
{
int i,j,k,z;

printf("\n");
for(i=0;i<=10;i++)
{
printf("PING..........");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");

if(z == 100)
{
printf("\r");
printf("PING..........[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}

/* ---------------------IPCONFIG bar function---------------- */

ipconfig()
{
int i,j,k,z;

printf("\n");
for(i=0;i<=10;i++)
{
printf("IPCONFIG......");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");

if(z == 100)
{
printf("\r");
printf("IPCONFIG......[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}

/* ---------------------NETSTAT bar function---------------- */

netstat()
{
int i,j,k,z;

printf("\n");
for(i=0;i<=10;i++)
{
printf("NETSTAT.......");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");

if(z == 100)
{
printf("\r");
printf("NETSTAT.......[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}

/* ---------------------TRACEROUTE bar function---------------- */

traceroute()
{
int i,j,k,z;

printf("\n");
for(i=0;i<=10;i++)
{
printf("TRACEROUTE....");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");

if(z == 100)
{
printf("\r");
printf("TRACEROUTE....[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
printf("\n");
}

Waitaminute, I just read your original post again. You've said you get an "Error unrecognised command" error. Do you get that error when compiling, or when running your program? If it's the later, it's not a problem with memset or bzero, but rather with the fact that Windows doesn't know about netstat, traceroute, and the ping command you're using.

Sorry I should of been clearer, I "currently" running the codes on a gentoo machine... I wanted to translate the bzero() bcopy() before attempting to port it to windows. Now I am on a Gentoo machine and with bzero/bcopy instead of memset it works fine... whenever i switch bzero(buffer, 256); to memset(buffer, '\0' , 256); SMTP server replies "Error: Unrecognized command" for all commands sent to the SMTP with memset() replacing bzero()

---------- Post updated at 06:21 AM ---------- Previous update was at 04:41 AM ----------

God... I can't believe I did that read the man too fast i guess... lol I fixed it it wasn't in

bzero(buffer, 256)

issue came from a little higher up with

 bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);

has just changed bcopy for memcpy not knowing the way it copied bits from which location to where... fixed it by

memcpy((char *)&serv_addr.sin_addr.s_addr, 
         (char *)server->h_addr,
         server->h_length);