4

I wrote simple c program using system call and it worked fine when I compiled it with gcc. But when I tried to generate shell code (shellforge) using command

./sf.py username.c

I got error stating

Undefined reference to memset

How to resolve this? Here is my code

#define O_CREAT     0100
#define O_WRONLY    01
#define O_APPEND    02000
#define IDLEN 100
int main(void)
{
    char user_name[IDLEN]="";
    char pass1[IDLEN]="";
    char pass2[IDLEN]="";
    int fd,a,b,c;

    //to give user direction
    char user[]="Please type your username and hit enter:\n";
    char p1[]="Please type password and hit enter:\n";
    char p2[]="Please type password and hit enter:\n";

    write(1,user,sizeof(user));
    a=read(0,user_name,IDLEN);
    user_name[a-1]='\0';
    while(1)
    {
        write(1,p1,sizeof(p1));
        b=read(0,pass1,IDLEN);
        pass1[b-1]='\0';
        write(1,p2,sizeof(p2));
        c=read(0,pass2,IDLEN);
        pass2[c-1]='\0';

        int temp=0;
        while(pass1[temp] == pass2[temp])
        {
            if(pass1[temp] == '\0' || pass2[temp] == '\0')
            {
                break;
            }
            temp++;
        }
        if(pass1[temp] == '\0' && pass2[temp] == '\0')
        {
            break;
        }else{
            char error[] = "password does not match! please type your password again";
            write(1,error,sizeof(error));
        }
    }

    fd = open("user.txt",O_CREAT|O_WRONLY|O_APPEND,0666);
    if(fd<0)
    {
        return 1;
    }

    char file_user[]="\nUsername:";
    char file_pass[]="\nPassword:";
    write(fd,file_user,sizeof(file_user)-1);
    write(fd,user_name,a-1);
    write(fd,file_pass,sizeof(file_pass)-1);
    write(fd,pass1,b-1);
}
surendra
  • 119
  • 1
  • 2
  • 9
  • Missing `#include ` – EOF Jul 27 '16 at 21:09
  • include the string.h header file. – clarasoft-it Jul 27 '16 at 21:10
  • 1
    Why? OP doesn't include `memset` in his code. Regardless, OP doesn't have the include for `write` and `read`. Why would gcc not return an error for this? Why would shellforge then return an error for memset, but not this? – Daniel Margosian Jul 27 '16 at 21:15
  • adding string.h is giving other errors nw file:///home/ubuntu/Desktop/Screenshot%20(789).png – surendra Jul 27 '16 at 21:22
  • gcc is not required to issue errors for calling functions before declaring them for compatibility reasons to pre C89 code. If you use a function before declaring them, it assumes the function returns int and infers the types of the arguments based on the call. See http://stackoverflow.com/a/2575186/3914029 – miravalls Jul 27 '16 at 21:25
  • yes it works fine when I execute it using gcc but it gives error only when i am trying to execute it using shellforge – surendra Jul 27 '16 at 21:26
  • 1
    Which version of shellforge are you using? depending on the sflib version you're using, the memset() may be implicitly called under the system call wrappers defined in the shellforge include headers. – pah Jul 27 '16 at 21:29
  • @threadp I am using v4 – surendra Jul 27 '16 at 21:34

2 Answers2

5

include <string.h>; memset() is usually a macro and not a symbol in the standard libraries.

EDIT: although not called explicitly, the compiler might call it for char user_name[IDLEN]=""; and the other string variables.

You should compile with '-W -Wall' to detect such issues.

ensc
  • 6,704
  • 14
  • 22
2

I modified my code from

char user_name[IDLEN]="";
char pass1[IDLEN]="";
char pass2[IDLEN]="";

to

char user_name[IDLEN];
char pass1[IDLEN];
char pass2[IDLEN];

and it worked like a charm! :)

surendra
  • 119
  • 1
  • 2
  • 9