C bzero() to memset() issue


 
Thread Tools Search this Thread
Top Forums Programming C bzero() to memset() issue
# 1  
Old 01-21-2010
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

Code:
bzero(buffer,256);

in

Code:
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,

Code:
memset(buffer, 0, 256);

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

tried also

Code:
memset(buffer, '/0', 256);

Code:
memset(buffer, 0, sizeof(buffer));

All ends up with the same error

Unrecognised command... but with

Code:
bzero(buffer,256);

Anyone can shed so light on this please?

Thanks
# 2  
Old 01-21-2010
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.
# 3  
Old 01-21-2010
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 Smilie

Code:
#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");
}

# 4  
Old 01-21-2010
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.
# 5  
Old 01-21-2010
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

Code:
bzero(buffer, 256)

issue came from a little higher up with

Code:
 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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Memset fails on Solaris

Hi, memset call is failing on solaris for me. I wrote below code and that also fails. Any hints? void *memset(void *dst, int c, size_t n) { if (n) { char *d = dst; do { *d++ = c; } while (--n); } return dst; } (2 Replies)
Discussion started by: skyineyes
2 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

Need assistance with a file issue and a terminal issue

Hello everyone, I'm in need of some assistance. I'm currently enrolled in an introductory UNIX shell programming course and, well halfway through the semester, we are receiving our first actual assignment. I've somewhat realized now that I've fallen behind, and I'm working to get caught up, but for... (1 Reply)
Discussion started by: MrMagoo22
1 Replies

4. UNIX for Dummies Questions & Answers

ISSUE and ISSUE.NET files

In LINUX(CentOS, RedHat) is there a way to have the banner statement appear before the logon instead of after the logon? In UNIX and Windows the banner appears before a person actually logs on, what I'm seeing in LINUX is that it appears after the login(ftp, telnet, SSH). Thanks (0 Replies)
Discussion started by: ejjones
0 Replies

5. Solaris

application Crashes on memset ?? any suggestions

Hi All, we have an application that is written in 'C' programming to connects to various servers in the organization. The bellow code establish a TCP connection to connect to the remote servers. the application works perfectly ok, but, after some time the entire process get's crashed and... (2 Replies)
Discussion started by: sudharma
2 Replies

6. UNIX for Advanced & Expert Users

memset vs calloc

Dear Friends, Can any one tell me the difference between memset and calloc function in C. Regards, Selvi (7 Replies)
Discussion started by: salvi
7 Replies

7. Shell Programming and Scripting

Unix Arithmatic operation issue , datatype issue

Hi, I have a shell scripting. This will take 7 digit number in each line and add 7 digit number with next subsequent lines ( normal addition ). Eg: 0000001 0000220 0001235 0000022 0000023 ........... ......... ........ Like this i am having around 1500000 records. After adding... (23 Replies)
Discussion started by: thambi
23 Replies

8. UNIX for Dummies Questions & Answers

questions in memset

HI all , please find the piece of code below char *t; char *f; char buf; memset(buf,0,50); after that i am assigning memory for (i=0; i<100; i++) { t = buf+(i*6); f = "ARUN"; } my question .. 1) i have run this it is... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

9. Programming

about memset fuction

Dear all, In my code,i am planning to use memset function to re-initialise an array before populating it everytime. Will using memset function be an overload to the program? (3 Replies)
Discussion started by: ranj@chn
3 Replies

10. Programming

bzero in socket

Sirs, I am new to socket programming.I have to do bzero and bcopy in my programs what is the use of it. It will be very usefull for me to know it. thanks, arunkumar (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question