#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <signal.h>
#include <sac.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "misc.h"
#include "structs.h"
#include "extern.h"
void
error(msg)
char *msg;
{
(void) fprintf(stderr, "%s\n", msg);
quit();
}
void
quit()
{
exit(Saferrno);
}
char *
make_tempname(bname)
char *bname;
{
static char buf[SIZE];
char *p;
p = strrchr(bname, '/');
if (p == NULL)
(void) sprintf(buf, "%s/.%s", HOME, bname);
else {
(void) strcpy(buf, HOME);
*p = '\0';
(void) strcat(buf, "/");
(void) strcat(buf, bname);
(void) strcat(buf, "/.");
(void) strcat(buf, (p + 1));
*p = '/';
}
return(buf);
}
FILE *
open_temp(tname)
char *tname;
{
FILE *fp;
struct sigaction sigact;
sigact.sa_flags = 0;
sigact.sa_handler = SIG_IGN;
(void) sigemptyset(&sigact.sa_mask);
(void) sigaddset(&sigact.sa_mask, SIGHUP);
(void) sigaddset(&sigact.sa_mask, SIGINT);
(void) sigaddset(&sigact.sa_mask, SIGQUIT);
(void) sigaction(SIGHUP, &sigact, NULL);
(void) sigaction(SIGINT, &sigact, NULL);
(void) sigaction(SIGQUIT, &sigact, NULL);
(void) umask(0333);
if (access(tname, 0) != -1) {
Saferrno = E_SAFERR;
error("tempfile busy; try again later");
}
fp = fopen(tname, "w");
if (fp == NULL) {
Saferrno = E_SYSERR;
error("cannot create tempfile");
}
return(fp);
}
void
replace(fname, tname)
char *fname;
char *tname;
{
char buf[SIZE];
(void) sprintf(buf, "%s/%s", HOME, fname);
(void) unlink(buf);
if (rename(tname, buf) < 0) {
Saferrno = E_SYSERR;
(void) unlink(tname);
quit();
}
}
int
copy_file(FILE *fp, FILE *tfp, int start, int finish)
{
int i;
char dummy[SIZE];
rewind(fp);
if (start != 1) {
for (i = 1; i < start; i++)
if (!fgets(dummy, SIZE, fp))
return(-1);
}
if (finish != -1) {
for (i = start; i <= finish; i++) {
if (!fgets(dummy, SIZE, fp))
return(-1);
if (fputs(dummy, tfp) == EOF)
return(-1);
}
}
else {
for (;;) {
if (fgets(dummy, SIZE, fp) == NULL) {
if (feof(fp))
break;
else
return(-1);
}
if (fputs(dummy, tfp) == EOF)
return(-1);
}
}
return(0);
}
int
find_pm(FILE *fp, char *pmtag)
{
char *p;
int line = 0;
struct sactab stab;
char buf[SIZE];
while (fgets(buf, SIZE, fp)) {
line++;
p = trim(buf);
if (*p == '\0')
continue;
parse(p, &stab);
if (!(strcmp(stab.sc_tag, pmtag)))
return(line);
}
if (!feof(fp)) {
Saferrno = E_SYSERR;
error("error reading _sactab");
return (0);
}
else
return(0);
}
int
do_config(char *script, char *basename)
{
FILE *ifp;
FILE *ofp;
struct stat statbuf;
char *tname;
char buf[SIZE];
if (script) {
if (access(script, 0) == 0) {
if (stat(script, &statbuf) < 0) {
Saferrno = E_SYSERR;
(void) fprintf(stderr, "Could not stat <%s>\n", script);
return(1);
}
if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
(void) fprintf(stderr, "warning - %s not a regular file - ignored\n", script);
return(1);
}
}
else {
Saferrno = E_NOEXIST;
(void) fprintf(stderr, "Invalid request, %s does not exist\n", script);
return(1);
}
ifp = fopen(script, "r");
if (ifp == NULL) {
(void) fprintf(stderr, "Invalid request, can not open %s\n", script);
Saferrno = E_SYSERR;
return(1);
}
tname = make_tempname(basename);
ofp = open_temp(tname);
while(fgets(buf, SIZE, ifp)) {
if (fputs(buf, ofp) == EOF) {
(void) unlink(tname);
Saferrno = E_SYSERR;
error("error in writing tempfile");
}
}
(void) fclose(ifp);
if (fclose(ofp) == EOF) {
(void) unlink(tname);
Saferrno = E_SYSERR;
error("error closing tempfile");
}
replace(basename, tname);
return(0);
}
else {
(void) sprintf(buf, "%s/%s", HOME, basename);
if (access(buf, 0) < 0) {
(void) fprintf(stderr, "Invalid request, script does not exist\n");
Saferrno = E_NOEXIST;
return(1);
}
ifp = fopen(buf, "r");
if (ifp == NULL) {
(void) fprintf(stderr, "Invalid request, can not open script\n");
Saferrno = E_SYSERR;
return(1);
}
while (fgets(buf, SIZE, ifp))
(void) fputs(buf, stdout);
(void) fclose(ifp);
return(0);
}
}