1
/* $Id: plugin_file.c 782 2007-03-21 05:01:11Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_file.c $
4
* plugin to perform simple file operations
6
* Copyright (C) 2006 Chris Maj <cmaj@freedomcorpse.com>
7
* Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30
* int plugin_init_file (void)
31
* adds various functions
36
/* define the include files you need */
42
/* these should always be included */
51
/* function 'readline' */
52
/* takes two arguments, file name and line number */
53
/* returns text of that line */
55
static void my_readline(RESULT * result, RESULT * arg1, RESULT * arg2)
57
char value[80], val2[80];
62
fp = fopen(R2S(arg1), "r");
64
error("readline couldn't open file '%s'", R2S(arg1));
68
while (!feof(fp) && i++ < reqline) {
69
fgets(val2, sizeof(val2), fp);
70
size = strcspn(val2, "\r\n");
71
strncpy(value, val2, size);
73
/* more than 80 chars, chew up rest of line */
74
while (!feof(fp) && strchr(val2, '\n') == NULL) {
75
fgets(val2, sizeof(val2), fp);
80
error("readline requested line %d but file only had %d lines", reqline, i - 1);
86
SetResult(&result, R_STRING, &value);
89
/* plugin initialization */
90
/* MUST NOT be declared 'static'! */
91
int plugin_init_file(void)
94
/* register all our cool functions */
95
/* the second parameter is the number of arguments */
96
/* -1 stands for variable argument list */
97
AddFunction("file::readline", 2, my_readline);
102
void plugin_exit_file(void)
104
/* free any allocated memory */
105
/* close filedescriptors */