473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Variable Argumets

Hi,

I'm writing logging functions.
I want to use them within my demon program.
I'm working on Solaris 7, Sun Sparc Machine.

Here are source code.
------------------------------------------------------------
// log.c
// 2005.03
// Kim Chul Min <cm***@shinbiro.com>

// header files
#include <stdio.h> // freopen()
#include <unistd.h> // access()
#include <string.h> // memset()
#include <strings.h> // strcpy()
#include <varargs.h> // va_*
#include <errno.h>

#include "common.h"

// global
char logfile[MAX_PATH_SZ];
int loglevel = DBGLVL_WARNING;
char *logmode = "a";
char tmbuf[40];

// extern
extern int errno;

// function declarations
int make_logfile(char *path, char *mode)
{
if ( freopen(path, mode, stderr) == NULL ) {
// PRINT ERROR
printf("freopen fail=%d(%s)\n", errno, strerror(errno));
return JOB_FAIL;
}

return JOB_OK;
}

void logmsg(va_alist)
va_dcl
{
int mylevel;
va_list args;
char *fmt;

va_start(args);

mylevel = va_arg(args, int);
if (mylevel < loglevel) {
return;
}
if ( access(logfile, F_OK) ) {
make_logfile(logfile, logmode);
}
fmt = va_arg(args, char *);
vfprintf(stderr, fmt, args);
fflush(stderr);
va_end(ap);
return;
}

char *timestamp(void)
{
time_t timeval;
struct tm *tm;

timeval = time(NULL);
tm = (struct tm *)localtime(&timeval);
strftime(tmbuf, 35, "%Y %b %d %X", tm);

return tmbuf;
}

#ifdef _TEST_
int main(int ac, char **av)
{

if (ac < 2) {
puts("***************************************");
puts("** [USAGE] **");
puts("** $ PGM LOG_FILENAME **");
puts("***************************************");
return 1;
}

memset(logfile, NULL, MAX_PATH_SZ);
strcpy(logfile, av[1]);

printf("logfile = [%s]\n", logfile);

if ( make_logfile(logfile, "a") != JOB_OK ) {
printf("make_logfile() fail.\n");
return 1;
}

logmsg(DBGLVL_WARNING, "[%s] warning logmsg...\n", timestamp());
logmsg(DBGLVL_ERROR, "[%s] error logmsg...\n", timestamp());
logmsg(DBGLVL_FATAL, "[%s] fatal logmsg...\n", timestamp());
logmsg(DBGLVL_ABSOLUTE, "[%s] absolute logmsg...\n", timestamp());

return 0;
}
#endif
------------------------------------------------------------

Here are compile messages.
------------------------------------------------------------
VIS_DEV[yfirst]:/home/yfirst/bear/src/lib> cc -xCC -v -Xa -D_TEST_ -I../h -c log.c
"log.c", line 98: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 99: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 100: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 101: warning: argument mismatch: 3 args passed, 1 expected
------------------------------------------------------------

My questions are (1) why I got a argument mismatch warning messages
during compile and how can I fix it, (2) I found that above source
code is old K&R style. I want to write them with ANSI C Style
if it is possible on Solaris 7. I cannot find "stdargs.h" file in /usr/include

I mean ANSI C Style variable arguments coding like:

#include <stdargs.h>
void Logmsg(char *Format, ...)
{

}
Thank you for your valuable time.
Kim
Seoul, Korea
Nov 14 '05 #1
1 3013
Chul Min Kim wrote:
Hi,

I'm writing logging functions.
I want to use them within my demon program.
I'm working on Solaris 7, Sun Sparc Machine.

Here are source code.
------------------------------------------------------------
// log.c
// 2005.03
// Kim Chul Min <cm***@shinbiro.com>

// header files
#include <stdio.h> // freopen()
#include <unistd.h> // access()
Not a Standard C header, but we'll forgive you.
#include <string.h> // memset()
#include <strings.h> // strcpy()
Ugh, barf, vomit. Get rid of this one; <string.h> has
everything you need.
#include <varargs.h> // va_*
Ugh, barf, vomit, and this is the root of your problem.
The pre-Standard <varargs.h> mechanism has been obsolete for
almost sixteen years; it was replaced by <stdarg.h> in 1989.
#include <errno.h>

#include "common.h"

// global
char logfile[MAX_PATH_SZ];
int loglevel = DBGLVL_WARNING;
char *logmode = "a";
char tmbuf[40];

// extern
extern int errno;
UGH! BARF! VOMIT! You have already included <errno.h>,
and this line attempts to re-declare what <errno.h> already
declared for you! This is wrong, R-O-N-G, wrong! Do *NOT*
try to write "free-hand" declarations of Standard library
components; use the Standard headers and *only* the Standard
headers. It is possible (although stupid) to write your own
declarations -- but in this case your declaration is R-O-N-G.
[...]
void logmsg(va_alist)
va_dcl
{
This is the root of your problem. You are trying to use
the pre-Standard <varargs.h> mechanism, but you ought to be
using the Standard <stdarg.h> facility, which is somewhat
different. I can tell you're trying to do the right thing
because you say
I cannot find "stdargs.h" file in /usr/include


.... and the reason you cannot find "stdargs.h" is because the
correct name of the header is <stdarg.h> -- angle brackets
instead of quotation marks, and only one "s". Switch to
<stdarg.h>, change the logmsg() function to match, and you'll
have solved your problem. (Or your "immediate problem;" there
is no end to fixing code ...)

Disclaimer: I sometimes post from a sun.com address but
I do not speak for Sun Microsystems, not even to people who
mention "Solaris" in their questions.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.