|
Packit |
15a96c |
/*
|
|
Packit |
15a96c |
* common.c Functions common to minicom and runscript programs
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* This file is part of the minicom communications package,
|
|
Packit |
15a96c |
* Copyright 1991-1995 Miquel van Smoorenburg,
|
|
Packit |
15a96c |
* 1997-1998 Jukka Lahtinen.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* This program is free software; you can redistribute it or
|
|
Packit |
15a96c |
* modify it under the terms of the GNU General Public License
|
|
Packit |
15a96c |
* as published by the Free Software Foundation; either version
|
|
Packit |
15a96c |
* 2 of the License, or (at your option) any later version.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* Functions
|
|
Packit |
15a96c |
* char *pfix_home(char *) - prefix filename with home directory
|
|
Packit |
15a96c |
* void do_log(const char *) - write a line to the logfile
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* moved from config.c to a separate file, so they are easier
|
|
Packit |
15a96c |
* to use in both the Minicom main program and runscript.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* You should have received a copy of the GNU General Public License along
|
|
Packit |
15a96c |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
Packit |
15a96c |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
*
|
|
Packit |
15a96c |
* 27.10.98 jl converted do_log to use stdarg
|
|
Packit |
15a96c |
*/
|
|
Packit |
15a96c |
#ifdef HAVE_CONFIG_H
|
|
Packit |
15a96c |
#include <config.h>
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#include <limits.h>
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#include "port.h"
|
|
Packit |
15a96c |
#include "minicom.h"
|
|
Packit |
15a96c |
#include <stdarg.h>
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* Prefix a non-absolute file with the home directory. */
|
|
Packit |
15a96c |
char *pfix_home(char *s)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#if defined(FILENAME_MAX)
|
|
Packit |
15a96c |
static char buf[FILENAME_MAX];
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
static char buf[256];
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (s && *s != '/') {
|
|
Packit |
15a96c |
snprintf(buf, sizeof(buf),"%s/%s", homedir, s);
|
|
Packit |
15a96c |
return buf;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
return s;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
void do_log(const char *line, ...)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#ifdef LOGFILE
|
|
Packit |
15a96c |
/* Write a line to the log file. jl 22.06.97 */
|
|
Packit |
15a96c |
FILE *logfile;
|
|
Packit |
15a96c |
char *logname = pfix_home(logfname);
|
|
Packit |
15a96c |
struct tm *ptr;
|
|
Packit |
15a96c |
time_t ttime;
|
|
Packit |
15a96c |
va_list ap;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (logfname[0] == 0)
|
|
Packit |
15a96c |
return;
|
|
Packit |
15a96c |
logfile = fopen(logname,"a");
|
|
Packit |
15a96c |
if (!logfile)
|
|
Packit |
15a96c |
return;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
va_start(ap, line);
|
|
Packit |
15a96c |
ttime = time(NULL);
|
|
Packit |
15a96c |
ptr = localtime(&ttime);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
fprintf(logfile,"%04d%02d%02d %02d:%02d:%02d ",
|
|
Packit |
15a96c |
(ptr->tm_year)+1900, (ptr->tm_mon)+1, ptr->tm_mday,
|
|
Packit |
15a96c |
ptr->tm_hour, ptr->tm_min, ptr->tm_sec);
|
|
Packit |
15a96c |
vfprintf(logfile, line, ap);
|
|
rpm-build |
b26ddc |
va_end(ap);
|
|
Packit |
15a96c |
fprintf(logfile, "\n");
|
|
Packit |
15a96c |
fclose(logfile);
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
/* dummy, don't do anything */
|
|
Packit |
15a96c |
(void)line;
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* mbtowc (), except that mbtowc (.. , "", ..) == 1, errors are treated as
|
|
Packit |
15a96c |
* (wchar_t)*s */
|
|
Packit |
15a96c |
size_t one_mbtowc(wchar_t *pwc, const char *s, size_t n)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
int len;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
len = mbtowc(pwc, s, n);
|
|
Packit |
15a96c |
if (len == -1)
|
|
Packit |
15a96c |
*pwc = *s;
|
|
Packit |
15a96c |
if (len <= 0)
|
|
Packit |
15a96c |
len = 1;
|
|
Packit |
15a96c |
return len;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* wctomb (), except that mbtowc (.. , 0) == 1, errors are treated as
|
|
Packit |
15a96c |
* *s = (char)wchar */
|
|
Packit |
15a96c |
size_t
|
|
Packit |
15a96c |
one_wctomb(char *s, wchar_t wchar)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
int len;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
len = wctomb(s, wchar);
|
|
Packit |
15a96c |
if (len == -1)
|
|
Packit |
15a96c |
s[0] = (char)wchar;
|
|
Packit |
15a96c |
if (len <= 0)
|
|
Packit |
15a96c |
len = 1;
|
|
Packit |
15a96c |
return len;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* Number of characters in S */
|
|
Packit |
15a96c |
size_t
|
|
Packit |
15a96c |
mbslen(const char *s)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
size_t len;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
len = 0;
|
|
Packit |
15a96c |
while (*s != 0) {
|
|
Packit |
15a96c |
wchar_t wc;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
s += one_mbtowc(&wc, s, MB_LEN_MAX);
|
|
Packit |
15a96c |
len++;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
return len;
|
|
Packit |
15a96c |
}
|