提交 3b885564 编写于 作者: O openharmony_ci 提交者: Gitee

!97 支持sys模块api接口:ftok、nice、getlogin、getgrgid、getgrnam等

Merge pull request !97 from wangjianjun/sys
#include <sys/ipc.h>
#include <sys/stat.h>
#include <unsupported_api.h>
key_t ftok(const char *path, int id)
{
struct stat st;
unsupported_api(__FUNCTION__);
if (stat(path, &st) < 0) return -1;
return ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 0xffu) << 24));
}
#include "pwf.h"
#include <pthread.h>
#include <unsupported_api.h>
#define FIX(x) (gr->gr_##x = gr->gr_##x-line+buf)
static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
{
char *line = 0;
size_t len = 0;
char **mem = 0;
size_t nmem = 0;
int rv = 0;
size_t i;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
if (*res && size < len + (nmem+1)*sizeof(char *) + 32) {
*res = 0;
rv = ERANGE;
}
if (*res) {
buf += (16-(uintptr_t)buf)%16;
gr->gr_mem = (void *)buf;
buf += (nmem+1)*sizeof(char *);
memcpy(buf, line, len);
FIX(name);
FIX(passwd);
for (i=0; mem[i]; i++)
gr->gr_mem[i] = mem[i]-line+buf;
gr->gr_mem[i] = 0;
}
free(mem);
free(line);
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
}
int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size, struct group **res)
{
unsupported_api(__FUNCTION__);
return getgr_r(name, 0, gr, buf, size, res);
}
int getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
{
return getgr_r(0, gid, gr, buf, size, res);
}
#include <unsupported_api.h>
#include "pwf.h"
static FILE *f;
static char *line, **mem;
static struct group gr;
void setgrent()
{
unsupported_api(__FUNCTION__);
if (f) fclose(f);
f = 0;
}
weak_alias(setgrent, endgrent);
struct group *getgrent()
{
struct group *res;
size_t size=0, nmem=0;
unsupported_api(__FUNCTION__);
if (!f) f = fopen("/etc/group", "rbe");
if (!f) return 0;
__getgrent_a(f, &gr, &line, &size, &mem, &nmem, &res);
return res;
}
struct group *getgrgid(gid_t gid)
{
struct group *res;
size_t size=0, nmem=0;
unsupported_api(__FUNCTION__);
__getgr_a(0, gid, &gr, &line, &size, &mem, &nmem, &res);
return res;
}
struct group *getgrnam(const char *name)
{
struct group *res;
size_t size=0, nmem=0;
__getgr_a(name, 0, &gr, &line, &size, &mem, &nmem, &res);
return res;
}
#include "pwf.h"
#include <pthread.h>
#include <unsupported_api.h>
#define FIX(x) (pw->pw_##x = pw->pw_##x-line+buf)
static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
{
char *line = 0;
size_t len = 0;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
rv = __getpw_a(name, uid, pw, &line, &len, res);
if (*res && size < len) {
*res = 0;
rv = ERANGE;
}
if (*res) {
memcpy(buf, line, len);
FIX(name);
FIX(passwd);
FIX(gecos);
FIX(dir);
FIX(shell);
}
free(line);
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
}
int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res)
{
unsupported_api(__FUNCTION__);
return getpw_r(name, 0, pw, buf, size, res);
}
int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
{
return getpw_r(0, uid, pw, buf, size, res);
}
#include "pwf.h"
#include <unsupported_api.h>
static FILE *f;
static char *line;
static struct passwd pw;
static size_t size;
void setpwent()
{
unsupported_api(__FUNCTION__);
if (f) fclose(f);
f = 0;
}
weak_alias(setpwent, endpwent);
struct passwd *getpwent()
{
struct passwd *res;
unsupported_api(__FUNCTION__);
if (!f) f = fopen("/etc/passwd", "rbe");
if (!f) return 0;
__getpwent_a(f, &pw, &line, &size, &res);
return res;
}
struct passwd *getpwuid(uid_t uid)
{
struct passwd *res;
unsupported_api(__FUNCTION__);
__getpw_a(0, uid, &pw, &line, &size, &res);
return res;
}
struct passwd *getpwnam(const char *name)
{
struct passwd *res;
__getpw_a(name, 0, &pw, &line, &size, &res);
return res;
}
#include <unistd.h>
#include <stdlib.h>
#include <unsupported_api.h>
char *getlogin(void)
{
unsupported_api(__FUNCTION__);
return getenv("LOGNAME");
}
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <unsupported_api.h>
int getlogin_r(char *name, size_t size)
{
char *logname = getlogin();
unsupported_api(__FUNCTION__);
if (!logname) return ENXIO; /* or...? */
if (strlen(logname) >= size) return ERANGE;
strcpy(name, logname);
return 0;
}
#include <unistd.h>
#include <sys/resource.h>
#include <limits.h>
#include "syscall.h"
#include <unsupported_api.h>
int nice(int inc)
{
int prio = inc;
unsupported_api(__FUNCTION__);
// Only query old priority if it can affect the result.
// This also avoids issues with integer overflow.
if (inc > -2*NZERO && inc < 2*NZERO)
prio += getpriority(PRIO_PROCESS, 0);
if (prio > NZERO-1) prio = NZERO-1;
if (prio < -NZERO) prio = -NZERO;
return setpriority(PRIO_PROCESS, 0, prio) ? -1 : prio;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册