updated syslog(), added setenv()

This commit is contained in:
Ben Gras 2006-04-03 15:03:07 +00:00
parent 321f95f6c5
commit d464faf987
3 changed files with 32 additions and 11 deletions

View file

@ -61,6 +61,7 @@ libc_FILES=" \
putw.c \ putw.c \
random.c \ random.c \
rindex.c \ rindex.c \
setenv.c \
setgroups.c \ setgroups.c \
settimeofday.c \ settimeofday.c \
stderr.c \ stderr.c \

25
lib/other/setenv.c Executable file
View file

@ -0,0 +1,25 @@
#include <stdlib.h>
#include <string.h>
int
setenv(const char *name, const char *val, int overwrite)
{
char *bf;
int r;
if(!overwrite && getenv(name))
return 0;
if(!(bf=malloc(strlen(name)+strlen(val)+2)))
return -1;
strcpy(bf, name);
strcat(bf, "=");
strcat(bf, val);
r = putenv(bf);
return r == 0 ? 0 : -1;
}

View file

@ -39,14 +39,6 @@
* Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997 * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997
* Rewritten by G. Falzoni <gfalzoni@inwind.it> for porting to Minix * Rewritten by G. Falzoni <gfalzoni@inwind.it> for porting to Minix
* *
* $Log$
* Revision 1.1 2005/10/31 14:31:05 beng
* Giovanni's symlink (+syslog+flock) patches.
*
* Revision 1.2 2001/01/04 11:54:26 root
* Removed variable to store PID which is computed
* at each call to avoid reporting parent's pid.
*
* $Id$ * $Id$
*/ */
#include <sys/types.h> #include <sys/types.h>
@ -68,6 +60,7 @@
#include <errno.h> #include <errno.h>
#include <net/gen/inet.h> #include <net/gen/inet.h>
static int LogPid = (-1);
static int nfd = (-1); static int nfd = (-1);
static int LogFacility = LOG_USER; static int LogFacility = LOG_USER;
static int LogFlags = 0; static int LogFlags = 0;
@ -86,6 +79,8 @@ void openlog(const char *ident, int option, int facility)
/* Stores logging flags */ /* Stores logging flags */
LogFlags = option & (LOG_PID | LOG_PERROR | LOG_CONS); LogFlags = option & (LOG_PID | LOG_PERROR | LOG_CONS);
/* Stores process id. if LOG_PID was specified */
if (option & LOG_PID) LogPid = getpid();
/* Stores the requested facility */ /* Stores the requested facility */
LogFacility = facility; LogFacility = facility;
/* Stores log tag if supplied */ /* Stores log tag if supplied */
@ -134,14 +129,14 @@ void syslog(int lprty, const char *msg,...)
int len, rc; int len, rc;
va_list ap; va_list ap;
/* First log message opens a channel to syslog */ /* First log message open chnnel to syslog */
if (nfd < 0) openlog(TagBuffer, LogFlags | LOG_NDELAY, LogFacility); if (nfd < 0) openlog(TagBuffer, LogFlags | LOG_NDELAY, LogFacility);
time(&now); time(&now);
len = sprintf(buff, "<%d>%.15s %s: ", len = sprintf(buff, "<%d>%.15s %s: ",
LogFacility | lprty, ctime(&now) + 4, TagBuffer); LogFacility | lprty, ctime(&now) + 4, TagBuffer);
if (LogFlags & LOG_PID) { if (LogFlags & LOG_PID) {
len -= 2; len -= 2;
len += sprintf(buff + len, "[%d]: ", getpid()); len += sprintf(buff + len, "[%d]: ", LogPid);
} }
va_start(ap, msg); va_start(ap, msg);
len += vsprintf(buff + len, msg, ap); len += vsprintf(buff + len, msg, ap);
@ -163,7 +158,7 @@ void closelog(void)
{ {
close(nfd); close(nfd);
nfd = -1; LogPid = nfd = -1;
LogFacility = LOG_USER; LogFacility = LOG_USER;
LogFlags = 0; LogFlags = 0;
return; return;