int sys_read_string(char *dir_name, char *file_name, char *str, int max_len) { char path[256], *s; int fd, r; snprintf(path, sizeof(path), "%s/%s", dir_name, file_name); if ((fd = open(path, O_RDONLY)) < 0) return ret_code(); if ((r = read(fd, str, max_len)) < 0) { int e = errno; close(fd); errno = e; return ret_code(); } str[(r < max_len) ? r : max_len - 1] = 0; if ((s = strrchr(str, '\n'))) *s = 0; close(fd); return 0; }
int sys_read_string(const char *dir_name, const char *file_name, char *str, int max_len) { char path[256], *s; size_t len; snprintf(path, sizeof(path), "%s/%s", dir_name, file_name); len = max_len; if (sysctlbyname(PATH_TO_SYS(path), str, &len, NULL, 0) == -1) return ret_code(); str[(len < max_len) ? len : max_len - 1] = 0; if ((s = strrchr(str, '\n'))) *s = 0; return 0; }
static int ret_string(char* value, char** rbuf, int rlen) { int msg_code = INTL_STRING; /* 2 = integer */ char* dst = *rbuf; char* src = value; int len = 0; if (src == NULL) return ret_code(errno, rbuf, rlen); if (rlen < strlen(value)+1) { int alen = strlen(value)+1; if ((*rbuf = driver_alloc(alen)) == NULL) return -1; dst = *rbuf; } *dst++ = msg_code; len++; while(*src) { *dst++ = *src++; len++; } return len; }
static int intl_ctl(ErlDrvData drv_data, unsigned int command, char *buf, int len, char **rbuf, int rlen) { void* d = (void*)drv_data; char* aptr = buf; int alen = len; char* str1; char* str2; char* str3; int int1; int int2; unsigned int uint1; switch(command) { case INTL_GETTEXT: /* arguments: string msgid */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(gettext(str1), rbuf, rlen); case INTL_NGETTEXT: /* arguments: string msgid, string msgid_plural, int n */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_uinteger(&aptr, &alen, &uint1) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(ngettext(str1,str2,uint1), rbuf, rlen); case INTL_DGETTEXT: /* arguments: string domain, string msgid */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(dgettext(str1,str2), rbuf, rlen); case INTL_DNGETTEXT: /* arguments: string domain, string msgid, string msgid_plural, integer n */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str3) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_uinteger(&aptr, &alen, &uint1) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(dngettext(str1,str2,str3,uint1), rbuf, rlen); case INTL_DCGETTEXT: /* arguments: string domain, string msgid, int category */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_integer(&aptr,&alen, &int1) < 0) return ret_code(EINVAL, rbuf, rlen); if ((int1 = get_category(int1)) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(dcgettext(str1,str2,int1),rbuf,rlen); case INTL_DCNGETTEXT: /* arguments: string domain, string msgid, strinf msgid_plural, unsigned n, int category */ if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str3) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_uinteger(&aptr,&alen, &uint1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_integer(&aptr,&alen, &int1) < 0) return ret_code(EINVAL, rbuf, rlen); if ((int1 = get_category(int1)) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(dcngettext(str1,str2,str3,uint1,int1),rbuf,rlen); case INTL_TEXTDOMAIN: /* arguments: string domainname */ if (len == 0) return ret_string(textdomain(NULL), rbuf, rlen); if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(textdomain(str1), rbuf, rlen); case INTL_BINDTEXTDOMAIN: /* arguments: string domainname, string dirname */ if (len == 0) return ret_string(textdomain(NULL), rbuf, rlen); if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(bindtextdomain(str1,str2), rbuf, rlen); case INTL_BIND_TEXTDOMAIN_CODESET: /* arguments: string domainname, string dirname */ if (len == 0) return ret_string(textdomain(NULL), rbuf, rlen); if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str2) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(bind_textdomain_codeset(str1,str2), rbuf, rlen); case INTL_SETLOCALE: if (get_integer(&aptr,&alen, &int1) < 0) return ret_code(EINVAL, rbuf, rlen); if ((int1 = get_category(int1)) < 0) return ret_code(EINVAL, rbuf, rlen); if (get_string(&aptr, &alen, &str1) < 0) return ret_code(EINVAL, rbuf, rlen); return ret_string(setlocale(int1,str1), rbuf, rlen); } return -1; }