libsmooth: Make it its own package.

This commit is contained in:
Michael Tremer
2014-07-30 22:02:35 +02:00
parent fa6c8fdcd0
commit 765b17bc52
14 changed files with 583 additions and 979 deletions

57
src/libsmooth/Makefile Normal file
View File

@@ -0,0 +1,57 @@
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
CC = gcc
CFLAGS = -O2 -Wall
INCLUDE =
LD = ld
LDFLAGS = -i
COMPILE = $(CC) -c $(INCLUDE) $(CFLAGS)
LINK = $(LD) $(LIBRARY) $(LDFLAGS)
all : programs
programs : lang_smooth libsmooth.o
clean :
-rm -f *.o core langs.h
lang_smooth :
for i in /usr/src/langs/*/install/lang_*.c ; do \
cp $$i ../libsmooth/; \
done
#build the tr_strings include file
awk 'BEGIN{ print"enum trstrings{" } $$0 ~/\/\* (TR_[A-Z0-9_]*)/ {print $$2"," }\
END{ print "};" }'\
../libsmooth/lang_en.c > ../libsmooth/langs.h
######
# Language modules are directly included in main.c
OBJS=main.o netstuff.o varval.o
libsmooth.o: $(OBJS)
$(LINK) $(OBJS) -o $@ $(LIBS)
%.o : %.c
$(COMPILE) $< -o $@

81
src/libsmooth/Makefile.am Normal file
View File

@@ -0,0 +1,81 @@
# This file is part of the libsmooth library.
#
# libsmooth is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
AM_MAKEFLAGS = --no-print-directory
AUTOMAKE_OPTIONS = color-tests parallel-tests
SUBDIRS = .
# remove targets if the command fails
.DELETE_ON_ERROR:
# keep intermediate files
.SECONDARY:
LIBSMOOTH_CURRENT=1
LIBSMOOTH_REVISION=0
LIBSMOOTH_AGE=0
pkgconfiglibdir=$(libdir)/pkgconfig
CLEANFILES =
EXTRA_DIST =
AM_CPPFLAGS = \
-include $(top_builddir)/config.h \
-I $(top_srcdir)/include \
$(OUR_CPPFLAGS)
AM_CFLAGS = $(OUR_CFLAGS)
AM_CXXFLAGS = $(OUR_CXXFLAGS)
AM_LDFLAGS = $(OUR_LDFLAGS)
lib_LTLIBRARIES =
include_HEADERS =
#- libsmooth -------------------------------------------------------------------
lib_LTLIBRARIES += \
libsmooth.la
libsmooth_la_SOURCES = \
main.c \
varval.c
libsmooth_la_LDFLAGS = \
$(AM_LDFLAGS) \
-version-info $(LIBSMOOTH_CURRENT):$(LIBSMOOTH_REVISION):$(LIBSMOOTH_AGE)
include_HEADERS += \
libsmooth.h
pkgconfiglib_DATA = \
libsmooth.pc
CLEANFILES += \
libsmooth.pc
EXTRA_DIST += \
libsmooth.pc.in
#-------------------------------------------------------------------------------
substitutions = \
'|PACKAGE_NAME=$(PACKAGE_NAME)|' \
'|PACKAGE_VERSION=$(PACKAGE_VERSION)|' \
'|prefix=$(prefix)|' \
'|exec_prefix=$(exec_prefix)|' \
'|libdir=$(libdir)|' \
'|includedir=$(includedir)|'
SED_PROCESS = \
$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
$(SED) $(subst '|,-e 's|@,$(subst =,\@|,$(subst |',|g',$(substitutions)))) < $< > $@
%.pc: %.pc.in Makefile
$(SED_PROCESS)

3
src/libsmooth/autogen.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
autoreconf --force --install -I m4

View File

@@ -0,0 +1,58 @@
# This file is part of libsmooth.
#
# libsmooth is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
AC_PREREQ([2.64])
AC_INIT([libsmooth],
[001],
[],
[libsmooth],
[http://git.ipfire.org/?p=ipfire-2.x.git;a=summary])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([
foreign
1.11
-Wall
-Wno-portability
silent-rules
tar-pax
no-dist-gzip
dist-xz
subdir-objects
])
AM_SILENT_RULES([yes])
LT_PREREQ(2.2)
LT_INIT([disable-static])
AC_PROG_SED
AC_PROG_CC
AC_PROG_CC_C99
AC_PROG_CC_C_O
AC_PATH_PROG([M4], [m4])
# This makes sure pkg.m4 is available.
m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-config])
AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT
AC_MSG_RESULT([
$PACKAGE_NAME $VERSION
CFLAGS: ${OUR_CFLAGS} ${CFLAGS}
CPPFLAGS: ${OUR_CPPFLAGS} ${CPPFLAGS}
LDFLAGS: ${OUR_LDFLAGS} ${LDFLAGS}
])

68
src/libsmooth/libsmooth.h Normal file
View File

@@ -0,0 +1,68 @@
/* SmoothWall libsmooth.
*
* This program is distributed under the terms of the GNU General Public
* Licence. See the file COPYING for details.
*
* (c) Lawrence Manning, 2001
* Contains prototypes for library functions.
*
*/
#ifndef ___LIBSMOOTH_H
#define ___LIBSMOOTH_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include <locale.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <newt.h>
#include <dirent.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#define STRING_SIZE 1024
struct keyvalue {
char key[STRING_SIZE];
char value[STRING_SIZE];
struct keyvalue *next;
};
/* libsmooth.c */
void stripnl(char *s);
int mysystem(const char *command);
void errorbox(char *message);
int statuswindowscroll(int width, int height, const char *title, const char *text, ...);
int disclaimerbox(char *message);
void statuswindow(int width, int height, const char *title, const char *text, ...);
int runcommandwithprogress(int width, int height, const char *title, const char *command,
int lines, char *text, ...);
int runcommandwithstatus(const char *command, const char* title, const char *message);
int runhiddencommandwithstatus(const char *command, const char* title, const char *message);
int checkformodule(const char *module);
int replace(char filename1[], char *from, char *to);
char* get_version(void);
/* varval.c */
struct keyvalue *initkeyvalues(void);
void freekeyvalues(struct keyvalue *head);
int readkeyvalues(struct keyvalue *head, char *filename);
int writekeyvalues(struct keyvalue *head, char *filename);
int findkey(struct keyvalue *head, char *key, char *value);
void appendkeyvalue(struct keyvalue *head, char *key, char *value);
void replacekeyvalue(struct keyvalue *head, char *key, char *value);
#endif

View File

@@ -0,0 +1,18 @@
# This file is part of the libsmooth library.
#
# libsmooth is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libsmooth
Description: libsmooth library
URL: @PACKAGE_URL@
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lsmooth
Cflags: -I${includedir}

View File

@@ -0,0 +1,288 @@
dnl Macros to check the presence of generic (non-typed) symbols.
dnl Copyright (c) 2006-2008 Diego Pettenò <flameeyes@gmail.com>
dnl Copyright (c) 2006-2008 xine project
dnl Copyright (c) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com>
dnl
dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2, or (at your option)
dnl any later version.
dnl
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dnl GNU General Public License for more details.
dnl
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
dnl 02110-1301, USA.
dnl
dnl As a special exception, the copyright owners of the
dnl macro gives unlimited permission to copy, distribute and modify the
dnl configure scripts that are the output of Autoconf when processing the
dnl Macro. You need not follow the terms of the GNU General Public
dnl License when using or distributing such scripts, even though portions
dnl of the text of the Macro appear in them. The GNU General Public
dnl License (GPL) does govern all other use of the material that
dnl constitutes the Autoconf Macro.
dnl
dnl This special exception to the GPL applies to versions of the
dnl Autoconf Macro released by this project. When you make and
dnl distribute a modified version of the Autoconf Macro, you may extend
dnl this special exception to the GPL to apply to your modified version as
dnl well.
dnl Check if FLAG in ENV-VAR is supported by compiler and append it
dnl to WHERE-TO-APPEND variable
dnl CC_CHECK_FLAG_APPEND([WHERE-TO-APPEND], [ENV-VAR], [FLAG])
AC_DEFUN([CC_CHECK_FLAG_APPEND], [
AC_CACHE_CHECK([if $CC supports flag $3 in envvar $2],
AS_TR_SH([cc_cv_$2_$3]),
[eval "AS_TR_SH([cc_save_$2])='${$2}'"
eval "AS_TR_SH([$2])='-Werror $3'"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([int a = 0; int main(void) { return a; } ])],
[eval "AS_TR_SH([cc_cv_$2_$3])='yes'"],
[eval "AS_TR_SH([cc_cv_$2_$3])='no'"])
eval "AS_TR_SH([$2])='$cc_save_$2'"])
AS_IF([eval test x$]AS_TR_SH([cc_cv_$2_$3])[ = xyes],
[eval "$1='${$1} $3'"])
])
dnl CC_CHECK_FLAGS_APPEND([WHERE-TO-APPEND], [ENV-VAR], [FLAG1 FLAG2])
AC_DEFUN([CC_CHECK_FLAGS_APPEND], [
for flag in $3; do
CC_CHECK_FLAG_APPEND($1, $2, $flag)
done
])
dnl Check if the flag is supported by linker (cacheable)
dnl CC_CHECK_LDFLAGS([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
AC_DEFUN([CC_CHECK_LDFLAGS], [
AC_CACHE_CHECK([if $CC supports $1 flag],
AS_TR_SH([cc_cv_ldflags_$1]),
[ac_save_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS $1"
AC_LINK_IFELSE([int main() { return 1; }],
[eval "AS_TR_SH([cc_cv_ldflags_$1])='yes'"],
[eval "AS_TR_SH([cc_cv_ldflags_$1])="])
LDFLAGS="$ac_save_LDFLAGS"
])
AS_IF([eval test x$]AS_TR_SH([cc_cv_ldflags_$1])[ = xyes],
[$2], [$3])
])
dnl define the LDFLAGS_NOUNDEFINED variable with the correct value for
dnl the current linker to avoid undefined references in a shared object.
AC_DEFUN([CC_NOUNDEFINED], [
dnl We check $host for which systems to enable this for.
AC_REQUIRE([AC_CANONICAL_HOST])
case $host in
dnl FreeBSD (et al.) does not complete linking for shared objects when pthreads
dnl are requested, as different implementations are present; to avoid problems
dnl use -Wl,-z,defs only for those platform not behaving this way.
*-freebsd* | *-openbsd*) ;;
*)
dnl First of all check for the --no-undefined variant of GNU ld. This allows
dnl for a much more readable commandline, so that people can understand what
dnl it does without going to look for what the heck -z defs does.
for possible_flags in "-Wl,--no-undefined" "-Wl,-z,defs"; do
CC_CHECK_LDFLAGS([$possible_flags], [LDFLAGS_NOUNDEFINED="$possible_flags"])
break
done
;;
esac
AC_SUBST([LDFLAGS_NOUNDEFINED])
])
dnl Check for a -Werror flag or equivalent. -Werror is the GCC
dnl and ICC flag that tells the compiler to treat all the warnings
dnl as fatal. We usually need this option to make sure that some
dnl constructs (like attributes) are not simply ignored.
dnl
dnl Other compilers don't support -Werror per se, but they support
dnl an equivalent flag:
dnl - Sun Studio compiler supports -errwarn=%all
AC_DEFUN([CC_CHECK_WERROR], [
AC_CACHE_CHECK(
[for $CC way to treat warnings as errors],
[cc_cv_werror],
[CC_CHECK_CFLAGS_SILENT([-Werror], [cc_cv_werror=-Werror],
[CC_CHECK_CFLAGS_SILENT([-errwarn=%all], [cc_cv_werror=-errwarn=%all])])
])
])
AC_DEFUN([CC_CHECK_ATTRIBUTE], [
AC_REQUIRE([CC_CHECK_WERROR])
AC_CACHE_CHECK([if $CC supports __attribute__(( ifelse([$2], , [$1], [$2]) ))],
AS_TR_SH([cc_cv_attribute_$1]),
[ac_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $cc_cv_werror"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([$3])],
[eval "AS_TR_SH([cc_cv_attribute_$1])='yes'"],
[eval "AS_TR_SH([cc_cv_attribute_$1])='no'"])
CFLAGS="$ac_save_CFLAGS"
])
AS_IF([eval test x$]AS_TR_SH([cc_cv_attribute_$1])[ = xyes],
[AC_DEFINE(
AS_TR_CPP([SUPPORT_ATTRIBUTE_$1]), 1,
[Define this if the compiler supports __attribute__(( ifelse([$2], , [$1], [$2]) ))]
)
$4],
[$5])
])
AC_DEFUN([CC_ATTRIBUTE_CONSTRUCTOR], [
CC_CHECK_ATTRIBUTE(
[constructor],,
[void __attribute__((constructor)) ctor() { int a; }],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_FORMAT], [
CC_CHECK_ATTRIBUTE(
[format], [format(printf, n, n)],
[void __attribute__((format(printf, 1, 2))) printflike(const char *fmt, ...) { fmt = (void *)0; }],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_FORMAT_ARG], [
CC_CHECK_ATTRIBUTE(
[format_arg], [format_arg(printf)],
[char *__attribute__((format_arg(1))) gettextlike(const char *fmt) { fmt = (void *)0; }],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_VISIBILITY], [
CC_CHECK_ATTRIBUTE(
[visibility_$1], [visibility("$1")],
[void __attribute__((visibility("$1"))) $1_function() { }],
[$2], [$3])
])
AC_DEFUN([CC_ATTRIBUTE_NONNULL], [
CC_CHECK_ATTRIBUTE(
[nonnull], [nonnull()],
[void __attribute__((nonnull())) some_function(void *foo, void *bar) { foo = (void*)0; bar = (void*)0; }],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_UNUSED], [
CC_CHECK_ATTRIBUTE(
[unused], ,
[void some_function(void *foo, __attribute__((unused)) void *bar);],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_SENTINEL], [
CC_CHECK_ATTRIBUTE(
[sentinel], ,
[void some_function(void *foo, ...) __attribute__((sentinel));],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_DEPRECATED], [
CC_CHECK_ATTRIBUTE(
[deprecated], ,
[void some_function(void *foo, ...) __attribute__((deprecated));],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_ALIAS], [
CC_CHECK_ATTRIBUTE(
[alias], [weak, alias],
[void other_function(void *foo) { }
void some_function(void *foo) __attribute__((weak, alias("other_function")));],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_MALLOC], [
CC_CHECK_ATTRIBUTE(
[malloc], ,
[void * __attribute__((malloc)) my_alloc(int n);],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_PACKED], [
CC_CHECK_ATTRIBUTE(
[packed], ,
[struct astructure { char a; int b; long c; void *d; } __attribute__((packed));],
[$1], [$2])
])
AC_DEFUN([CC_ATTRIBUTE_CONST], [
CC_CHECK_ATTRIBUTE(
[const], ,
[int __attribute__((const)) twopow(int n) { return 1 << n; } ],
[$1], [$2])
])
AC_DEFUN([CC_FLAG_VISIBILITY], [
AC_REQUIRE([CC_CHECK_WERROR])
AC_CACHE_CHECK([if $CC supports -fvisibility=hidden],
[cc_cv_flag_visibility],
[cc_flag_visibility_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $cc_cv_werror"
CC_CHECK_CFLAGS_SILENT([-fvisibility=hidden],
cc_cv_flag_visibility='yes',
cc_cv_flag_visibility='no')
CFLAGS="$cc_flag_visibility_save_CFLAGS"])
AS_IF([test "x$cc_cv_flag_visibility" = "xyes"],
[AC_DEFINE([SUPPORT_FLAG_VISIBILITY], 1,
[Define this if the compiler supports the -fvisibility flag])
$1],
[$2])
])
AC_DEFUN([CC_FUNC_EXPECT], [
AC_REQUIRE([CC_CHECK_WERROR])
AC_CACHE_CHECK([if compiler has __builtin_expect function],
[cc_cv_func_expect],
[ac_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $cc_cv_werror"
AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[int some_function() {
int a = 3;
return (int)__builtin_expect(a, 3);
}])],
[cc_cv_func_expect=yes],
[cc_cv_func_expect=no])
CFLAGS="$ac_save_CFLAGS"
])
AS_IF([test "x$cc_cv_func_expect" = "xyes"],
[AC_DEFINE([SUPPORT__BUILTIN_EXPECT], 1,
[Define this if the compiler supports __builtin_expect() function])
$1],
[$2])
])
AC_DEFUN([CC_ATTRIBUTE_ALIGNED], [
AC_REQUIRE([CC_CHECK_WERROR])
AC_CACHE_CHECK([highest __attribute__ ((aligned ())) supported],
[cc_cv_attribute_aligned],
[ac_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $cc_cv_werror"
for cc_attribute_align_try in 64 32 16 8 4 2; do
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
int main() {
static char c __attribute__ ((aligned($cc_attribute_align_try))) = 0;
return c;
}])], [cc_cv_attribute_aligned=$cc_attribute_align_try; break])
done
CFLAGS="$ac_save_CFLAGS"
])
if test "x$cc_cv_attribute_aligned" != "x"; then
AC_DEFINE_UNQUOTED([ATTRIBUTE_ALIGNED_MAX], [$cc_cv_attribute_aligned],
[Define the highest alignment supported])
fi
])

276
src/libsmooth/main.c Normal file
View File

@@ -0,0 +1,276 @@
/* SmoothWall libsmooth.
*
* This program is distributed under the terms of the GNU General Public
* Licence. See the file COPYING for details.
*
* (c) Lawrence Manning, 2001
* Contains library functions.
*/
#include "libsmooth.h"
#include <libintl.h>
#define _(x) dgettext("libsmooth", x)
extern FILE *flog;
extern char *mylog;
extern char **ctr;
/* stripnl(). Replaces \n with \0 */
void stripnl(char *s) {
char *t = strchr(s, '\n');
if (t)
*t = '\0';
}
/* Little wrapper. */
int mysystem(const char *command) {
char mycommand[STRING_SIZE];
snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog);
fprintf(flog, "Running command: %s\n", command);
return system(mycommand);
}
void errorbox(char *message) {
newtWinMessage(_("Error"), _("OK"), message);
}
void statuswindow(int width, int height, const char *title, const char *text, ...) {
newtComponent t, f;
char *buf = NULL;
int size = 0;
int i = 0;
va_list args;
va_start(args, text);
do {
size += 1000;
if (buf) free(buf);
buf = malloc(size);
i = vsnprintf(buf, size, text, args);
} while (i == size);
va_end(args);
newtCenteredWindow(width, height, title);
t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
newtTextboxSetText(t, buf);
f = newtForm(NULL, NULL, 0);
free(buf);
newtFormAddComponent(f, t);
newtDrawForm(f);
newtRefresh();
newtFormDestroy(f);
}
int runcommandwithstatus(const char *command, const char* title, const char *message) {
statuswindow(60, 4, title, message);
int rc = mysystem(command);
newtPopWindow();
return rc;
}
int runhiddencommandwithstatus(const char *command, const char* title, const char *message) {
statuswindow(60, 4, title, message);
char mycommand[STRING_SIZE];
snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog);
fprintf(flog, "Running command: ***** HIDDEN *****\n");
int rc = system(mycommand);
newtPopWindow();
return rc;
}
/* This one borrowed from redhat installer. */
int runcommandwithprogress(int width, int height, const char *title, const char *command,
int lines, char *text, ...) {
newtComponent t, f, s;
char *buf = NULL;
int size = 0;
int i = 0;
va_list args;
int rc = 0;
FILE *p;
char buffer[STRING_SIZE];
int progress = 0;
char mycommand[STRING_SIZE];
va_start(args, text);
do {
size += 1000;
if (buf) free(buf);
buf = malloc(size);
i = vsnprintf(buf, size, text, args);
} while (i == size);
va_end(args);
newtCenteredWindow(width, height, title);
t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
newtTextboxSetText(t, buf);
f = newtForm(NULL, NULL, 0);
free(buf);
newtFormAddComponent(f, t);
s = newtScale(1, 3, width - 2, lines);
newtScaleSet(s, progress);
newtFormAddComponent(f, s);
newtDrawForm(f);
newtRefresh();
snprintf(mycommand, STRING_SIZE, "%s 2>>%s", command, mylog);
fprintf(flog, "Running command: %s\n", command);
if (!(p = popen(command, "r")))
{
rc = 1;
goto EXIT;
}
setvbuf(p, NULL, _IOLBF, 255);
while (fgets(buffer, STRING_SIZE, p))
{
newtScaleSet(s, ++progress);
newtRefresh();
fprintf(flog, "%s", buffer);
}
rc = pclose(p);
EXIT:
newtFormDestroy(f);
newtPopWindow();
return rc;
}
int checkformodule(const char *module) {
FILE *file;
char buffer[STRING_SIZE];
int result = 0;
if (!(file = fopen("/proc/modules", "r")))
{
fprintf(flog, "Unable to open /proc/modules in checkformodule()\n");
return 0;
}
while (fgets(buffer, STRING_SIZE, file))
{
if (strncmp(buffer, module, strlen(module)) == 0)
{
if (buffer[strlen(module)] == ' ')
{
result = 1;
goto EXIT;
}
}
}
EXIT:
fclose(file);
return result;
}
int _replace_string(char string[], char *from, char *to)
{
int fromlen = strlen(from);
int tolen = strlen(to);
char *start, *p1, *p2;
for(start = string; *start != '\0'; start++)
{
p1 = from;
p2 = start;
while(*p1 != '\0')
{
if(*p1 != *p2)
break;
p1++;
p2++;
}
if(*p1 == '\0')
{
if(fromlen != tolen)
{
memmove(start + tolen, start + fromlen,
strlen(start + fromlen) + 1);
}
for(p1 = to; *p1 != '\0'; p1++)
*start++ = *p1;
return 1;
}
}
return 0;
}
int replace(char filename1[], char *from, char *to) {
FILE *file1, *file2;
char filename2[1000];
char temp[1000];
int ret = 0;
/* Open the source and destination files */
strcpy (filename2, filename1);
strcat (filename2, ".new");
if (!(file1 = fopen (filename1, "r"))) return 1;
if (!(file2 = fopen (filename2, "w"))) {
fclose(file1);
return -1;
}
/* Start reading in lines */
while (fgets (temp, 1000, file1) != NULL) {
if (strlen(to) > 0) {
/* Replace string */
ret = _replace_string (temp, from, to);
/* Write string to new file */
fputs(temp, file2);
} else {
/* Remove string when to is NULL */
if (!strstr(temp, from))
fputs(temp, file2);
}
}
/* Close source and destination */
fclose (file1);
fclose (file2);
/* Move the file */
rename (filename2, filename1);
return (ret);
}
// returns a pointer to the actual running version number of IPFire.
// Successive updates increase effective version but not VERSION !
char g_title[STRING_SIZE] = "";
char* get_version(void) {
FILE *f_title;
if ((f_title = fopen ("/etc/issue", "r"))) {
fgets (g_title, STRING_SIZE, f_title);
fclose (f_title);
if (g_title[strlen(g_title) - 1] == '\n') g_title[strlen(g_title) - 1] = '\0';
}
return g_title;
}

197
src/libsmooth/varval.c Normal file
View File

@@ -0,0 +1,197 @@
/* SmoothWall libsmooth.
*
* This program is distributed under the terms of the GNU General Public
* Licence. See the file COPYING for details.
*
* (c) Lawrence Manning, 2001
* Contains functions for manipulation files full of VAR=VAL pairs.
*
* 2003-07-27 Robert Kerr - Added cooperative file locking to prevent any
* clashes between setuid programs reading configuration and cgi scripts
* trying to write it
*
*/
#include "libsmooth.h"
/* Sets up the list. First entry is a dummy one to avoid having to special
* case empty lists. */
struct keyvalue *initkeyvalues(void)
{
struct keyvalue *head = malloc(sizeof(struct keyvalue));
strcpy(head->key, "KEY");
strcpy(head->value, "VALUE");
head->next = NULL;
return head;
}
/* Splats all the entries in a list. */
void freekeyvalues(struct keyvalue *head)
{
struct keyvalue *cur = head->next;
struct keyvalue *next;
while (cur)
{
next = cur->next;
free(cur);
cur = next;
}
}
/* Reads from a file into a new list. Uses appendkeyvalue to add entries.
* Will bomb out on a error (eg bad format line). */
int readkeyvalues(struct keyvalue *head, char *filename)
{
FILE *file;
char buffer[STRING_SIZE];
char *temp;
char *key, *value;
if (!(file = fopen(filename, "r")))
return 0;
if (flock(fileno(file), LOCK_SH))
{
fclose(file);
return 0;
}
while (fgets(buffer, STRING_SIZE, file))
{
temp = buffer;
while (*temp)
{
if (*temp =='\n') *temp = '\0';
temp++;
}
if (!strlen(buffer))
continue;
if (!(temp = strchr(buffer, '=')))
{
flock(fileno(file), LOCK_UN);
fclose(file);
return 0;
}
*temp = '\0';
key = buffer; value = temp + 1;
/* See if string is quoted. If so, skip first quote, and
* nuke the one at the end. */
if (value[0] == '\'')
{
value++;
if ((temp = strrchr(value, '\'')))
*temp = '\0';
else
{
flock(fileno(file), LOCK_UN);
fclose(file);
return 0;
}
}
if (strlen(key))
appendkeyvalue(head, key, value);
}
flock(fileno(file), LOCK_UN);
fclose(file);
return 1;
}
/* Writes out a list to a file. Easy. */
int writekeyvalues(struct keyvalue *head, char *filename)
{
FILE *file;
struct keyvalue *cur = head->next;
if (!(file = fopen(filename, "w")))
return 0;
if (flock(fileno(file), LOCK_EX))
{
fclose(file);
return 0;
}
while (cur)
{
/* No space in value? If there is, we need to quote the value
* so the shell can read it. */
if (!strchr(cur->value, ' '))
fprintf(file, "%s=%s\n", cur->key, cur->value);
else
fprintf(file, "%s=\'%s\'\n", cur->key, cur->value);
cur = cur->next;
}
flock(fileno(file), LOCK_UN);
fclose(file);
return 1;
}
/* Finds a key and copies the value back. value must be at least STRING_SIZE
* long. Would be nice to have a func that just returns a pointer to the value?
*/
int findkey(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *cur = head->next;
while (cur)
{
if (strcmp(key, cur->key) == 0)
{
strncpy(value, cur->value, STRING_SIZE);
value[STRING_SIZE-1] = '\0';
return 1;
}
cur = cur->next;
}
return 0;
}
/* Appends a entry. Not very efficent because it rescans the list looking
* for the end. Maybe fix this later. */
void appendkeyvalue(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *new = malloc(sizeof(struct keyvalue));
struct keyvalue *cur = head->next;
struct keyvalue *tail = head;
strncpy(new->key, key, STRING_SIZE);
strncpy(new->value, value, STRING_SIZE);
new->key[STRING_SIZE-1] = '\0';
new->value[STRING_SIZE-1] = '\0';
new->next = NULL;
while (cur)
{
tail = cur;
cur = cur->next;
}
tail->next = new;
}
/* Otherwrites a key with a new value, or if it dosn't exist, appends it
* on the end. */
void replacekeyvalue(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *cur = head->next;
while (cur)
{
if (strcmp(cur->key, key) == 0)
{
strncpy(cur->value, value, STRING_SIZE);
cur->value[STRING_SIZE-1] = '\0';
return;
}
cur = cur->next;
}
appendkeyvalue(head, key, value);
}