Accessing C library functions using Critcl

I had the need to access some C library functions that weren't available via standard Tcl or TclX, so I wrote a simple wrapper using Critcl.

These are wrappers for some of the set/get uid functions in Unix, but you could use the same approach for most functions - stevel April 5 2004

Compile using

   $ critcl -pkg uid.tcl

You'll end up with a lib/uid directory containing a package ready for dropping into a Starkit lib directory, or anywhere on your auto_path.

package require critcl
package provide uid 1.0

if {![critcl::compiling]} {
    puts stderr "This extension cannot be compiled without critcl enabled"
    exit 1
}

critcl::ccode {
    #include <sys/types.h>
    #include <unistd.h>
    #include <pwd.h>
}

critcl::cproc setusergroup {char* name} int {
    struct passwd *pwd = getpwnam(name);
    if (pwd == NULL) {
            return 0;
    }
    initgroups(name,pwd->pw_gid);
    setgid(pwd->pw_gid);
    setuid(pwd->pw_uid);
    return 1;
}
 
critcl::cproc setuid {char* name} int {
    struct passwd *pwd = getpwnam(name);
    if (pwd == NULL) {
        return 0;
    }
    setuid(pwd->pw_uid);
    return 1;
}
  
critcl::cproc seteuid {char* name} int {
    struct passwd *pwd = getpwnam(name);
    if (pwd == NULL) {
        return 0;
    }
    seteuid(pwd->pw_uid);
    return 1;
}

critcl::cproc getuid {} int {
    return getuid();
}

critcl::cproc geteuid {} int {
    return geteuid();
}

critcl::cproc setsid {} int {
    return setsid();
}