Help needed with C extension

Hi,

I’m writing my first Ruby extension in C, which will be an interface
to libcrack for checking passwords.

Here’s the code of ruby_crack.c:

#include “ruby.h”
#include “crack.h”

static VALUE passwd_init(VALUE self, VALUE passwd) {
fprintf(stderr, “passwd = %s\n”, STR2CSTR(passwd));
return passwd;
}

static VALUE passwd_check(VALUE self, VALUE dict) {
char *string;

fprintf(stderr, “%s\n”, STR2CSTR(self));
string = FascistCheck(STR2CSTR(self), STR2CSTR(dict));
return rb_str_new2(string);
}

VALUE cCrack;

void Init_Crack() {
fprintf(stderr, “In Init_Crack()\n”);
cCrack = rb_define_class(“Password”, rb_cString);
rb_define_method(cCrack, “initialize”, passwd_init, 1);
rb_define_method(cCrack, “check”, passwd_check, 1);
return;
}

However, when I call it from Ruby, the initialisation doesn’t happen
properly:

[ianmacd@jiskefet]$ irb ~/src/ruby-crack
irb(main):001:0> require 'Crack’
In Init_Crack()
true
irb(main):002:0> foo = Password.new(“foobar”)
passwd = foobar
"“
irb(main):003:0> foo
”"
irb(main):004:0>

The extension is properly loaded and initialised, but when calling
passwd_init(), foo gets set to an empty string, not “foobar”. I can’t
figure out why this is. Password is supposed to be a subclass of
String.

I’m sure I’m overlooking something very basic here, but what?

Ian

···


Ian Macdonald | I’d like to see the government get out of
ian@caliban.org | war altogether and leave the whole field to
> private industry. – Joseph Heller
>
>

Hi,

···

At Mon, 17 Jun 2002 18:06:02 +0900, Ian Macdonald wrote:

The extension is properly loaded and initialised, but when calling
passwd_init(), foo gets set to an empty string, not “foobar”. I can’t
figure out why this is. Password is supposed to be a subclass of
String.

You don’t call String#initialize, you need to call
rb_enable_super() in Init_Crack() and rb_call_super() in
passwd_init().


Nobu Nakada

Great; I knew it would be something simple.

Thank you very much.

Ian

···

On Mon 17 Jun 2002 at 19:11:46 +0900, nobu.nokada@softhome.net wrote:

At Mon, 17 Jun 2002 18:06:02 +0900, > Ian Macdonald wrote:

The extension is properly loaded and initialised, but when calling
passwd_init(), foo gets set to an empty string, not “foobar”. I can’t
figure out why this is. Password is supposed to be a subclass of
String.

You don’t call String#initialize, you need to call
rb_enable_super() in Init_Crack() and rb_call_super() in
passwd_init().


Ian Macdonald | I was born in a Hostess Cupcake factory
ian@caliban.org | before the sexual revolution!
>
>
>