#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "techrep.h"
#include "util.h"
#include "com-util.h"


/* 
 *  techrep runs set-user, so if the source file has restricted 
 *  file permissions, then we can't read it. This is overcome 
 *  by changing the effective user id to open the source file.
 *  The destination file is opened normally.
 */
/* Added mode 13Nov92 - CLV */
int
userToArchiveFileCopy(char* file2, char* file1, int mode)
{
   int oldeuid,n,f1,f2;
   char buf[STRING_SIZE];
   int status = 0;
   int oldMask;

   /* open dst file as techrep */
   if ((f2 = open(file2, O_WRONLY | O_CREAT | O_TRUNC, mode))== -1) {
      return (-1);
   }

   oldeuid=geteuid();/* this is done to overcome a permissions problem */
   seteuid(getuid());

   /* open src file as user */   
   if ((f1 = open(file1,  0, 0))== -1) {
      close (f2);
      return (-1);
   }
   
   while ((n = read(f1, buf, 255)) > 0)
      if (write(f2, buf,n) != n) {
         status = -1;
         break;
      }
   close(f1);
   close(f2);
   seteuid(oldeuid);

   return (status);
}

/* 
 *  Similar problem as above, except here we open the source file 
 *  as techrep, and open the destination file as the user.
 */
int 
archiveToUserFileCopy(char* file2, char* file1, int mode)
{
   int oldeuid, n, f1, f2;
   char buf[STRING_SIZE];
   int status = 0;

   /* open src file as techrep */
   if ((f1 = open(file1,  0, 0))== -1) {
      return (-1);
   }
   
   oldeuid=geteuid();/* this is done to overcome a permissions problem */
   seteuid(getuid());

   /* open dst file as user */
   if ((f2 = open(file2, O_WRONLY | O_CREAT | O_TRUNC, mode))== -1) {
      close (f1);
      return (-1);
   }
   
   /* copy the file */
   while ((n = read(f1, buf, 255)) >0)
      if (write(f2, buf,n) != n) {
         status = -1;
         break;
      }
   close(f1);
   close(f2);
   seteuid(oldeuid);

   return (status);;
}

int 
fileCopy(char* file2, char* file1, int mode)
{
   int n, f1, f2;
   char buf[STRING_SIZE];
   int status = 0;

   /* open src file */
   if ((f1 = open(file1,  0, 0))== -1) {
      return (-1);
   }
   
   /* open dst file */
   if ((f2 = open(file2, O_WRONLY | O_CREAT | O_TRUNC, mode))== -1) {
      close (f1);
      return (-1);
   }
   
   /* copy the file */
   while ((n = read(f1, buf, 255)) >0)
      if (write(f2, buf,n) != n) {
         status = -1;
         break;
      }
   close(f1);
   close(f2);

   return (status);
}

/*
 * Use the file as a semaphore. Existence of the file means 
 * the semaphore == 1. Not exactly like a wait(), because we 
 * don't block.
 */
int get_semaphore(char *semaphore_file)
{
   FILE *sem_fp;

   if ((creat(semaphore_file,0000))==-1) sleep(2);
   else return;
   if ((creat(semaphore_file,0000))==-1) sleep(2);
   else return;
   if ((creat(semaphore_file,0000))==-1) sleep(2);
   else return;
   if ((creat(semaphore_file,0000))==-1){
      return(ERROR);
   }
}

/* 
 * Release the semphore by deleting the file.
 */
int release_semaphore(char *semaphore_file)
{
   unlink (semaphore_file);
}


int 
NumberofAuthors(char array[N_STRINGS][STRING_SIZE], char *ptr)
{
   int             x;
   for (x = 1; strpbrk(ptr, AUTHOR_DELIMITER) != NULL; x++) {
      strncpy(array[x], ptr, (strpbrk(ptr, AUTHOR_DELIMITER) - ptr));
      array[x][strpbrk(ptr, AUTHOR_DELIMITER) - ptr] = '\0';
      ptr = strpbrk(ptr, AUTHOR_DELIMITER) + 1;
      for (; isspace(*ptr) != 0; ptr++);	/* eliminate leading blanks */
      if (strlen(array[x]) < 1)
         x--;
   }
   strcpy(array[x], ptr);
   if (strlen(array[x]) < 1)
      x--;
   return (x);
}

int 
NumberofLines(char array[N_STRINGS][STRING_SIZE], char *ptr)
{
   int             x;
   char           *tmp;
   for (x = 1; strchr(ptr, LINE_BREAK) != NULL; x++) {
      strcpy(array[x], ptr);
      while ((tmp = strchr(array[x], LINE_BREAK)) != NULL)
         *tmp = NULL;	/* remove `;' and '\n' from author */
      ptr = strchr(ptr, LINE_BREAK) + 1;
   }
   strcpy(array[x], ptr);
   return (x);
}
