we’ll need to distinguish these for sarge/etch as well
[alioth/jupp.git] / builtin.c
1 /*
2  *      Built-in config files
3  *      Copyright
4  *              (C) 2006 Joseph H. Allen
5  *
6  *      This file is part of JOE (Joe's Own Editor)
7  */
8
9 #include "config.h"
10
11 __RCSID("$MirOS: contrib/code/jupp/builtin.c,v 1.6 2017/12/02 02:07:23 tg Exp $");
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "types.h"
17 #include "builtin.h"
18 #include "utils.h"
19
20 #define zcmp(a,b) strcmp((char *)(a), (char *)(b))
21
22 JFILE *jfopen(unsigned char *name, const char *mode)
23 {
24         if (name[0] == '*') {
25                 int x;
26                 char *xname;
27
28                 xname = strdup((char *)name + 1);
29                 name = (void *)xname;
30                 while ((x = *name++)) {
31                         if (x >= 'A' && x <= 'Z')
32                                 name[-1] = x - 'A' + 'a';
33                 }
34
35                 for (x = 0; builtins[x]; x += 2) {
36                         if (!zcmp(builtins[x], xname)) {
37                                 JFILE *j = (JFILE *)joe_malloc(sizeof(JFILE));
38                                 j->f = 0;
39                                 j->p = builtins[x + 1];
40                                 joe_free(xname);
41                                 return j;
42                         }
43                 }
44                 joe_free(xname);
45                 return 0;
46         } else {
47                 FILE *f = fopen((char *)name, (char *)mode);
48                 if (f) {
49                         JFILE *j = (JFILE *)joe_malloc(sizeof(JFILE));
50                         j->f = f;
51                         j->p = 0;
52                         return j;
53                 } else {
54                         return 0;
55                 }
56         }
57 }
58
59 int jfclose(JFILE *f)
60 {
61         int rtn = 0;
62         if (f->f)
63                 rtn = fclose(f->f);
64         joe_free(f);
65         return rtn;
66 }
67
68 unsigned char *jfgets(unsigned char *buf,int len,JFILE *f)
69 {
70         if (f->f)
71                 return (unsigned char *)fgets((char *)buf, len, f->f);
72         else {
73                 if (f->p[0]) {
74                         int x;
75                         for (x = 0; f->p[x] && f->p[x] != '\n'; ++x)
76                                 buf[x] = f->p[x];
77                         if (f->p[x] == '\n') {
78                                 buf[x++] = '\n';
79                         }
80                         buf[x] = 0;
81                         f->p += x;
82                         return buf;
83                 } else
84                         return 0;
85         }
86 }