Ich versuche, diese Dateien auf meinem Haupt-C-Code enthalten:Doppelte Deklarationen. Fehler: Konflikt-Typen für 'funktionsname'
who.c:
/* who3.c - who with buffered reads
* - surpresses empty records
* - formats time nicely
* - buffers input (using utmplib)
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <utmp.h>
#include <fcntl.h>
#include <time.h>
#include "utmplib.h"
#define SHOWHOST
void show_info(struct utmp *);
void showtime(time_t);
int Who()
{
struct utmp *utbufp, /* holds pointer to next rec */
*utmp_next(); /* returns pointer to next */
if (utmp_open(UTMP_FILE) == -1){
perror(UTMP_FILE);
exit(1);
}
while ((utbufp = utmp_next()) != ((struct utmp *) NULL))
show_info(utbufp);
utmp_close();
return 0;
}
/*
* show info()
* displays the contents of the utmp struct
* in human readable form
* * displays nothing if record has no user name
*/
void show_info(struct utmp *utbufp)
{
printf("%-8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("%-8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
showtime(utbufp->ut_time); /* display time */
#ifdef SHOWHOST
if (utbufp->ut_host[0] != '\0')
printf(" (%s)", utbufp->ut_host); /* the host */
#endif
printf("\n"); /* newline */
}
void showtime(time_t timeval)
/*
* displays time in a format fit for human consumption
* uses ctime to build a string then picks parts out of it
* Note: %12.12s prints a string 12 chars wide and LIMITS
* it to 12chars.
*/
{
char *ctime(); /* convert long to ascii */
char *cp; /* to hold address of time */
cp = ctime(&timeval); /* convert time to string */
/* string looks like */
/* Mon Feb 4 00:46:40 EST 1991 */
/*. */
printf("%12.12s", cp+4); /* pick 12 chars from pos 4 */
}
who.h:
#ifndef WHO_H
#define WHO_H
/* This file was automatically generated. Do not edit! */
int Who();
void showtime(time_t);
void showtime(time_t timeval);
void show_info(struct utmp *);
void show_info(struct utmp *utbufp);
#endif
utmplib.c:
/* utmplib.c - functions to buffer reads from utmp file
*
* functions are
* utmp_open(filename) - open file
* returns -1 on error
* utmp_next() - return pointer to next struct
* returns NULL on eof
* utmp_close() - close file
*
* reads NRECS per read and then doles them out from the buffer
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utmp.h>
#define NRECS 1
#define NULLUT ((struct utmp *)NULL)
#define UTSIZE (sizeof(struct utmp))
static char utmpbuf[NRECS * UTSIZE]; /* storage */
static int num_recs; /* num stored */
static int cur_rec; /* next to go */
static int fd_utmp = -1; /* read from */
utmp_open(char *filename)
{
fd_utmp = open(filename, O_RDONLY); /* open it */
cur_rec = num_recs = 0; /* no recs yet */
return fd_utmp; /* report */
}
struct utmp *utmp_next()
{
struct utmp *recp;
//struct utmp *nextRecp;
int match = 0;
if(recp->ut_type == USER_PROCESS)
{
recp = (struct utmp *) &utmpbuf[cur_rec * UTSIZE];
}
while(recp->ut_type!= USER_PROCESS)
{
if (fd_utmp == -1) /* error ? */
return NULLUT;
if (cur_rec==num_recs && utmp_reload()==0) /* any more ? */
return NULLUT;
/* get address of next record */
recp = (struct utmp *) &utmpbuf[cur_rec * UTSIZE];
cur_rec++;
}
return recp;
}
int utmp_reload()
/*
* read next bunch of records into buffer
*/
{
int amt_read;
/* read them in */
amt_read = read(fd_utmp , utmpbuf, NRECS * UTSIZE);
/* how many did we get? */
num_recs = amt_read/UTSIZE;
/* reset pointer */
cur_rec = 0;
return num_recs;
}
utmp_close()
{
if (fd_utmp != -1) /* don't close if not */
close(fd_utmp); /* open */
}
utmplib.h:
#ifndef UTMPLIB_H
#define UTMPLIB_H
/* This file was automatically generated. Do not edit! */
utmp_close();
int utmp_reload();
struct utmp *utmp_next();
utmp_open(char *filename);
#endif
Aber ich diese Dateien nicht kompiliert werden kann, bin ich diesen Fehler:
who.h:9:6: error: conflicting types for ‘show_info’
void show_info(struct utmp *utbufp);
Ich habe diese beiden Datei nicht schreiben. Vor allem, warum gibt es doppelte Erklärungen? Und wie kann ich es reparieren?
Ich generierte Header-Dateien mit makeheaders.
Eigentlich funktionieren diese Dateien Beispiel für who
Befehl in Linux oder Basisversion von Who.c in GNU Core utils. Taken from here
who.c ohne Fehler kompilieren und arbeiten nahtlos (wenn Sie Wer mit Haupt-Funktionsnamen ändern):
cc who.c utmplib.c -o who
who
Diese 'struct utmp * utbufp, * utmp_next();' ist schrecklich Programmierstil! Warum haben Sie so viele Prototypen für Ihre Funktionen? –
Was meinst du mit Prototyp? Ich bin eigentlich neu in C. –
Funktion Deklarationen zum Beispiel 'void showtime (time_t);' und 'void showtime (time_t timeval);' sind genau gleich (* fast *). –