/*
 * misc.c  -  Miscellaneous routines
 *
 * Copyright (C) 2002-2003 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: misc.c,v 1.2 2003/01/25 23:29:43 gkminix Exp $
 */

#define NEED_BINARY 1
#define NEED_DIR 1
#define NEED_TIME 1
#include <common.h>
#include <nblib.h>
#include "mknbi.h"
#include "dir.h"

#ifndef _MKNBI_H_DOS_
#error Included wrong header file
#endif



/*
 * Find a file in the directory tree, or just in the current
 * directory.
 */
struct file_struct *findfile(dsp, recursive, name, ext)
struct dir_struct *dsp;
int recursive;
char *name;
char *ext;
{
  struct file_struct *fsp = dsp->files;
  struct dir_struct *subdir;

  /* Search for file in current directory */
  if (!recursive) {
	while (fsp != NULL) {
		if (!strncmp(name, fsp->name.name, 8) &&
		    !strncmp(ext, fsp->name.ext, 3))
			return(fsp);
		fsp = fsp->next;
	}
	return(NULL);
  }

  /* Scan all directory subtrees */
  fsp = findfile(dsp, FALSE, name, ext);
  if (fsp == NULL) {
	subdir = dsp->subdirs;
	while (subdir != NULL) {
		fsp = findfile(subdir, TRUE, name, ext);
		if (fsp != NULL)
			break;
		subdir = subdir->next;
	}
  }
  return(fsp);
}



/*
 * Get file modification time and convert it into MS-DOS format
 */
void gettime(mtime, date, time)
time_t mtime;
unsigned int *date;
unsigned int *time;
{
  struct tm *tmp;

  tmp = localtime(&mtime);
  *time = tmp->tm_hour * 2048 + tmp->tm_min * 32 + tmp->tm_sec / 2;
  *date = (tmp->tm_year - 80) * 512 + (tmp->tm_mon + 1) * 32 + tmp->tm_mday;
}



syntax highlighted by Code2HTML, v. 0.9.1