home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume36
/
msend
/
part01
/
misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-22
|
3KB
|
155 lines
/* misc.c:
*
* miscellaneous functions
*
* (c) Copyright 1988, 1989, 1990 Jim Frost. All Rights Reserved. Please see
* the accompanying file "Copyright" for more information.
*/
#include "Copyright"
#include "config.h"
#include "msend.h"
#ifdef M_SYSV
#include <unistd.h> /* Unix Standard definitions */
#endif
#if defined(_AIX) && defined(USE_LOCKF)
#include <sys/lockf.h>
#endif
void error();
/* easy way to build error messages
*/
void blderr(ri,errno,msg)
struct rimsg *ri;
int errno;
char *msg;
{ ri->h.errno= errno;
ri->h.msglen= strlen(msg);
strcpy(ri->msg,msg);
}
void die(i)
int i;
{ error("md terminated");
exit(i);
}
/* when we have a problem, call this
*/
void error(s)
char *s;
{ int uid;
long t;
char when[30];
FILE *f;
time(&t);
strcpy(when,ctime(&t));
when[strlen(when)-1]= '\0';
if (getuid() == ROOTUID) {
uid= geteuid();
seteuid(ROOTUID);
}
f= fopen(LOGFILE,"a");
if (getuid() == ROOTUID)
seteuid(uid);
if (f != NULL) {
#ifndef USE_LOCKF
flock(fileno(f),LOCK_EX);
#else
lockf(fileno(f),F_TLOCK, 0);
#endif
fprintf(f,"%s: %s\n",when,s);
#ifndef USE_LOCKF
flock(fileno(f),LOCK_UN);
#else
lockf(fileno(f),F_ULOCK, 0);
#endif
fclose(f);
}
else
printf("%s: %s\n",when,s);
}
/* this returns the port number to use for communication
*/
int portnum()
{ struct servent *se;
int p;
/* if possible, return the port number in /etc/services; if not,
* use hardcoded default
*/
if ((se= getservbyname("msend","tcp")) == NULL)
p= PORTNUM;
else
p= ntohs(se->s_port);
/* oops, someone forgot to make me setuid
*/
if ((p < 1024) && geteuid()) {
printf("portnum: not setuid\n");
exit(1);
}
return(p);
}
/* find the host name within an address, put it in an array, and truncate
* the address at the hostname.
*/
char *striphost(addr,host)
char addr[];
char *host;
{ int a;
for (a= strlen(addr); (a >= 0) && (addr[a] != '@'); a--)
;
if (a >= 0) {
strcpy(host,&addr[a+1]);
addr[a]= '\0';
return(host);
}
host[0]= '\0';
return(NULL);
}
char *gethome(user)
char *user;
{ struct passwd *pw;
if (! (pw= getpwnam(user)))
return(NULL);
return(pw->pw_dir);
}
int getid(user)
char *user;
{ struct passwd *pw;
if (! (pw= getpwnam(user)))
return(-1);
return(pw->pw_uid);
}
#ifdef NEEDS_LOCK
/* if the system needs flock, put the correct locking function in here. if
* you leave it like this it's possible that you'll get some conflict in
* writing to spool files and such, but it's not likely and it won't hurt
* anything much.
*/
int flock(fd, how)
int fd, how;
{
return(0);
}
#endif