Example #1
0
/* csrgraph_create: Allocate memory for graph. */
csrgraph_t *csrgraph_create(int nvertices, int nedges)
{
	int err = 0;
	csrgraph_t *graph = NULL;

	if ( (graph = malloc(sizeof(*graph))) == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	graph->vparents = NULL;
	graph->vweights = NULL;
	graph->vmap = NULL;

	graph->nvertices = nvertices;
	graph->nedges = nedges;

	/* Allocate memory for graph */
	graph->adjindexes = malloc(sizeof(int) * (graph->nvertices + 1));
	if (graph->adjindexes == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}

	/* Allocate memory for weights of edges */
	graph->nedges *= 2;
	graph->edges = malloc(sizeof(int) * (graph->nedges + 1));
	if (graph->edges == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}

	graph->adjv = malloc(sizeof(int) * (graph->nedges + 1));
	if (graph->adjv == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}

errhandler:
	if (err) {
		if (graph) {
			free(graph->adjindexes);
			free(graph->adjv);
			free(graph->edges);
			free(graph);
			graph = NULL;
		}
	}
	return graph;
}
Example #2
0
JNIEXPORT jlong JNICALL Java_jxtn_core_unix_NativeString_memmem(JNIEnv *env, jclass thisObj,
        jlong haystack, jlong haystacklen, jbyteArray needle) {
    if (haystack == 0L || needle == 0L)
        return SETERR(EFAULT);
    size_t needlelen = (size_t) (*env)->GetArrayLength(env, needle);
    return (jlong) memmem((void*) haystack, UL(haystacklen), resolveBA(needle), needlelen);
}
Example #3
0
File: pam.c Project: SkyLandTW/JXTN
JNIEXPORT jint JNICALL Java_jxtn_core_unix_NativePAM_authenticate(JNIEnv *env, jclass thisObj,
        jbyteArray service, jbyteArray username, jbyteArray password) {
    char* c_service = ACOPY_CS(service);
    char* c_username = ACOPY_CS(username);
    char* c_password = ACOPY_CS(password);
    int pipefd[2];
    if (pipe(pipefd) != 0)
        return ERR(-1);
    pid_t pid = fork();
    if (pid == 0) {
        if (prctl(PR_SET_PDEATHSIG, SIGTERM, 0L, 0L, 0L) != 0) {
            perror("PR_SET_PDEATHSIG");
        }
        close(0);
        dup2(pipefd[0], 0);
        close(pipefd[1]);
        char* argv[] = { "libjxtn-core-unix.so", "pam", c_service, c_username, NULL };
        if (execve("/usr/lib/jni/libjxtn-core-unix.so", argv, environ) == -1) {
            perror("execve");
        }
        _exit(-1);
        return -1;
    } else {
        close(pipefd[0]);
        if (write(pipefd[1], c_password, strlen(c_password)) == -1L) {
            goto ABORT;
        }
        char eol = '\n';
        if (write(pipefd[1], &eol, 1) == -1L) {
            goto ABORT;
        }
        int status = 0;
        if (waitpid(pid, &status, 0) == -1) {
            goto ABORT;
        }
        if (WIFEXITED(status)) {
            return WEXITSTATUS(status);
        } else {
            return SETERR(ENOTRECOVERABLE);
        }
        ABORT: {
            int err = errno;
            kill(pid, SIGKILL);
            return SETERR(err);
        }
    }
}
Example #4
0
JNIEXPORT jint JNICALL Java_jxtn_core_unix_NativeDirs_getdents64(JNIEnv *env, jclass thisObj,
        jint fd, jbyteArray dirp) {
    if (dirp == NULL) {
        return SETERR(EFAULT);
    }
    size_t count = UL((*env)->GetArrayLength(env, dirp));
    return ERR((int) syscall(SYS_getdents64, fd, resolveBA(dirp), count));
}
Example #5
0
/*
 * fcopy
 *
 * copy file (binary)
 *
 * params: oldname,newname...filename of input/output files
 * result: value of type ``fcopy_t'', see "ugly/ufile.h" for details
 *
 * NOTE: you can use ``errno'' to obtain details about the error
 */
fcopy_t fcopy(CONSTRPTR oldname, CONSTRPTR newname)
{
#define BUFSIZE_FCOPY (16*1024)
#define SETERR(code) {err = (code); nowErrno = errno; }
    fcopy_t err = FC_OK;        /* function result */
    FILE *inpf = fopen(oldname, "rb");  /* input file */
    FILE *outf = fopen(newname, "wb");  /* output file */
    char *buf = (char *) umalloc(sizeof(char) * BUFSIZE_FCOPY);         /* copy buffer */
    int nowErrno = 0;

    /* check for errors */
    if (!buf)
    {
        SETERR(FC_ERR_NoMemory);
    }
    else if (!inpf)
    {
        SETERR(FC_ERR_OpenInput);
    }
    else if (!outf)
    {
        SETERR(FC_ERR_OpenOutput);
    }
    else
    {
        /* copy data */
        BOOL quit = FALSE;
        do
        {
            size_t byteRead = fread(buf, sizeof(char), BUFSIZE_FCOPY, inpf);

            if (ferror(inpf))
            {
                SETERR(FC_ERR_Read);
            }
            else if (byteRead == 0)
            {
                quit = TRUE;
            }
            else
            {
                fwrite(buf, sizeof(char), byteRead, outf);
                if (ferror(outf))
                {
                    SETERR(FC_ERR_Write);
                }
            }
        }
        while (!quit && (err == FC_OK));
    }

    /* cleanup */
    if (inpf)
        fclose(outf);
    if (inpf)
        fclose(inpf);
    if (buf)
        ufree(buf);

    /* set errno to value where error occured first */
    if (nowErrno)
        errno = nowErrno;

    return err;
}
Example #6
0
/* csrgraph_load: Read graph in CSR format from file. */
csrgraph_t *csrgraph_load(const char *filename)
{
	int err = 0;
	FILE *fin = NULL;
	csrgraph_t *graph = NULL;
	char *line = NULL, *buf = NULL, *ptr = NULL;
	int i, j, v, w;
	
	fprintf(stderr,"load graph step 1\n");
	if ( (fin = fopen(filename, "r")) == NULL) {
	        fprintf(stderr, "Cant' load graph file\n");
		SETERR(err, 1);
		goto errhandler;
	}
	fprintf(stderr,"load graph step 2\n");
	if ( (graph = malloc(sizeof(*graph))) == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	graph->vparents = NULL;
	graph->vweights = NULL;
	graph->vmap = NULL;
        fprintf(stderr,"load graph step 3\n");

	if ( (buf = malloc(sizeof(*buf) * LINESIZE_MAX)) == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	fprintf(stderr,"load graph step 4\n");
	if (fscanf(fin, "%d %d %d\n", &graph->nvertices, 
		                          &graph->nedges, &i) != 3) 
	{
		SETERR(err, 1);
		goto errhandler;
	}
        fprintf(stderr,"load graph step 5\n");
	/* Allocate memory for graph */
	graph->adjindexes = malloc(sizeof(int) * (graph->nvertices + 1));
	if (graph->adjindexes == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	fprintf(stderr,"load graph step 6\n");
	/* Allocate memory for weights of edges */
	graph->nedges *= 2;
	graph->edges = malloc(sizeof(int) * graph->nedges);
	if (graph->edges == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	fprintf(stderr,"load graph step 7\n");
	graph->adjv = malloc(sizeof(int) * graph->nedges);
	if (graph->adjv == NULL) {
		SETERR(err, 1);
		goto errhandler;
	}
	fprintf(stderr,"load graph step 8\n");
	for (i = 0, j = 0; i < graph->nvertices; i++) {
		
		if (fgets(buf, LINESIZE_MAX, fin) == NULL) {
			SETERR(err, 1);
			goto errhandler;
		}

		buf[strlen(buf) - 1] = '\0';
		if (strlen(buf) > 0) {
			line = buf;
			
			/* Read adj. list of vertex i */
			graph->adjindexes[i] = j;
			do {			
				errno = 0;
				v = (int)strtol(buf, &ptr, 10);
				if (errno != 0) {
					SETERR(err, 1);
					goto errhandler;				
				}
				buf = ptr;

				errno = 0;
				w = strtol(buf, &ptr, 10);
				if (errno != 0) {
					SETERR(err, 1);
					goto errhandler;				
				}
				buf = ptr;
				
				graph->adjv[j] = v - 1;
				graph->edges[j] = w;
				j++;
				
				while (*buf == ' ')
					buf++;
			} while (*buf != '\0');
			
			buf = line;
		}
	}	
	graph->adjindexes[graph->nvertices] = graph->nedges;
	fprintf(stderr,"load graph step 9\n");
errhandler:
	if (fin)
		fclose(fin);
	free(buf);
	
	if (err) {
		if (graph) { 
			free(graph->adjindexes);
			free(graph->adjv);
			free(graph->edges);
			free(graph);
			graph = NULL;
		}
	}
	return graph;
}