summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/adrians-java/Old_robot/c_c_file.c
blob: 7a68eccf9bf997123809faff4988ed38ce8b5592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "c_c_file.h"

#define SL 200

JNIEXPORT jint JNICALL Java_c_1c_1file_c_1open (JNIEnv *env, jobject jo, jstring s)
{
    const char* str = (*env)->GetStringUTFChars(env, s, 0);
    int fd = open(str, O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == -1)
      printf("Java_cartesian_c_1open: error opening file %s.\n", str);
    fcntl(fd, F_SETFL, 0);
    return fd;
}

JNIEXPORT void JNICALL Java_c_1c_1file_c_1fcntl (JNIEnv *env, jobject jo, jint fd, jint wait)
{
  if(wait)
    fcntl(fd, F_SETFL, 0);
  else
    fcntl(fd, F_SETFL, FNDELAY);
}

JNIEXPORT jint JNICALL Java_c_1c_1file_c_1write (JNIEnv *env, jobject jo, jint fd, jstring s, jint len)
{
   const char* str = (*env)->GetStringUTFChars(env, s, 0);
   int n = write(fd, str, len);
   return n;
}

static int n;

JNIEXPORT jstring JNICALL Java_c_1c_1file_c_1read (JNIEnv *env, jobject jo, jint fd)
{
  char str[SL];
  n = read(fd, str, SL);
  str[n] = 0;
  jstring s = (*env)->NewStringUTF(env, str);
  return s;

}

JNIEXPORT jint JNICALL Java_c_1c_1file_c_1n_1read (JNIEnv *env, jobject jo)
{
  return n;
}