#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>

#include "util.h"

void getword(char *word, char *line, char stop) {
    int x = 0,y;

    for(x=0;((line[x]) && (line[x] != stop));x++)
        word[x] = line[x];

    word[x] = '\0';
    if(line[x]) ++x;
    y=0;

    while((line[y++] = line[x++]));
}

char *makeword(char *line, char stop) {
    int x = 0,y;
    char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));

    for(x=0;((line[x]) && (line[x] != stop));x++)
        word[x] = line[x];

    word[x] = '\0';
    if(line[x]) ++x;
    y=0;

    while((line[y++] = line[x++]));
    return word;
}

char *fmakeword(FILE *f, char stop, int *cl) {
   int wsize;
   char *word;
   int ll;

   wsize = 102400;
   ll=0;
   word = (char *) malloc(sizeof(char) * (wsize + 1));

   while(1) {
      word[ll] = (char)fgetc(f);
      if(ll==wsize) {
         word[ll+1] = '\0';
         wsize+=102400;
         word = (char *)realloc(word,sizeof(char)*(wsize+1));
      }
      --(*cl);
      if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
         if(word[ll] != stop) ll++;
         word[ll] = '\0';
         return word;
      }
      ++ll;
   }
}

char x2c(char *what) {
    register char digit;

    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
    digit *= 16;
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
    return(digit);
}

void unescape_url(char *url) {
    register int x,y;

    for(x=0,y=0;url[y];++x,++y) {
        if((url[x] = url[y]) == '%') {
            url[x] = x2c(&url[y+1]);
            y+=2;
        }
    }
    url[x] = '\0';
}

void plustospace(char *str) {
    register int x;

    for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}

int rind(char *s, char c) {
    register int x;
    for(x=strlen(s) - 1;x != -1; x--)
        if(s[x] == c) return x;
    return -1;
}

int getline(char *s, int n, FILE *f) {
    register int i=0;

    while(1) {
        s[i] = (char)fgetc(f);

        if(s[i] == CR)
            s[i] = fgetc(f);

        if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
            s[i] = '\0';
            return (feof(f) ? 1 : 0);
        }
        ++i;
    }
}

void send_fd(FILE *f, FILE *fd)
{
    char c;

    while (1) {
        c = fgetc(f);
        if(feof(f))
            return;
        fputc(c,fd);
    }
}

char *	
stripTrailingBlanks (char *str)
{
   int i;

   i = strlen(str)-1;
   while ((i >= 0) && isspace(str[i])) {
      str[i] = '\0';
      i--;
   }
   return (str);
}

char *
stripLeadingBlanks (char *str)
{
   while (isspace(str[0]))
      str++;
   return (str);
}

char *
stripBlankLines (char *str)
{
   int i, len;
   
   len = strlen(str);
   i = 1;
   while (i < len) {
      if (((str[i] == 10)||(str[i] == 13))  &&
          ((str[i-1] == 10)||(str[i-1] == 13))) 
         str[i-1] = ' ';
      i++;
   }
   str = stripTrailingBlanks(str);
   return (str);
}

char *
strlcase (char *s)
{
   int i;
   int len;

   len = strlen(s);
   for (i=0; i<len; i++) {
      s[i] = tolower(s[i]);
   }
   return (s);
}


/*
 * Returns TRUE if file is there and has non-zero length.
 */
int
fileExists (char *file)
{
   struct stat buf;

   if ((stat (file, &buf) == 0) && (buf.st_size > 0)) {
      return (TRUE);
   } else {
      return (FALSE);
   }
}

/* returns a string containing the date in rfc1123 format */
char *
rfc1123date()
{
   struct tm *GMT;
   time_t curTime;
   char buf[256];
   int len;

   curTime = time(0);
   GMT = gmtime(&curTime);
   len = strftime (buf, 256, "%a, %d %h %Y %T GMT", GMT);
   return (strdup (buf));
}

char *tilde_sub(char *fname)
{
   struct passwd  *pwdinfo;
   char            newfname[STRING_SIZE];
   char           *home, *pos;
   char            name[STRING_SIZE];
    
   strcpy(newfname, fname);

   /* perform tilde expansion */
   if (fname[0] == '~' && fname[1] == '/') {	/* current user's home dir */
      home = getenv("HOME");
      if (home == NULL)
         home = getenv("LOGDIR");
      if (home != NULL) {
         sprintf(newfname, "%s%s", home, fname + 1);
      }
   }

   /* different user's home dir */
   if (fname[0] == '~' && fname[1] != '/' && strlen(fname) > 1) {
      pos = strchr(fname, '/');
/*      printf ("%d %d\n", fname, pos); */
       
      strncpy (name, fname+1, pos-fname-1);
      name[pos-fname-1] = '\0';
       
/*      printf ("%s %s %s\n", fname, name, pos); */
      pwdinfo = getpwnam(name);
      if (pwdinfo == NULL) {
         return (NULL);
      } else {
         sprintf(newfname, "%s%s", pwdinfo->pw_dir, pos);
      }
   }

   return (strdup(newfname));
}

