summaryrefslogtreecommitdiff
path: root/systems/knuth/local/bdemo_ext.c
blob: 91fedf8f61cdcbb4066843b9561ec3e2d2c6b4bf (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* External procedures for the binary demo program using GPC */

#include "GPCtypes.h" /* defines GNU Pascal I/O structure */
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#define namelength 100          /* should agree with binarydemo.p */

bool Argv(int n, char* str)
{
  register char *s,*t;
  if (n<0 || n>=_p_argc) return FALSE;
  s=_p_argv[n];
  if (strlen(s) >= namelength) return FALSE;
  for (t=str; *s; s++,t++) *t=*s;
  *t='\0';
  return TRUE;
}

void Bread(FDR filep, char *x)
{ 
  /* reads a character WITHOUT using the Pascal runtime buffers etc */
  register char c;
  c=getc(filep->FilJfn);
  *x=c;
}

bool Testeof(FDR filep)
{
  register int c;
  register FILE *iop; /* stdio-style FILE pointer */

  iop = filep->FilJfn;
  if (feof(iop)) return TRUE;
  c = getc(iop);
  if (c == EOF) return TRUE;
  else {
    ungetc(c,iop);
    return FALSE;
  }
}

bool Testreadaccess(char *fn)
{
  return access(fn,R_OK)? FALSE: TRUE;
}

bool Testwriteaccess(fn)
  char  *fn;
{
  int f;
  f = creat(fn,0666);   /* we try to create a new file, because
		simply testing write access with access(fn,W_OK)
		will fail when the file doesn't exist */
  if (f >= 0) {
    close(f); return TRUE;
    } else return FALSE;
}

int Flength(FDR filep)
{
  register FILE	*iop;	/* stdio-style FILE pointer */
  register long	pos;    /* current file position */
  register int	len;	/* the file length */

  iop = filep->FilJfn;
  pos = ftell(iop);
  fseek(iop,0L,SEEK_END);
  len = ftell(iop);
  fseek(iop,pos,0);
  return len;
}

void Movetobyte(FDR filep,int cnt)
{
  register FILE *iop; /* stdio-style FILE pointer */

  iop = filep->FilJfn;
  fseek(iop,(long)cnt,SEEK_SET);
}