#ident "$Id: tool_common.c,v 1.2 2007/02/22 02:16:49 craig Exp $"
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * Functions common to many of the tools.
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#else
#define fseeko(file, where, whence)     fseeko64(file, where, whence)
#define ftello(file)                    ftello64(file)
#endif

#include "types.h"

#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>          // For off_t, and other things
#include <unistd.h>             // For stat()

extern char     *ProgName;

/* ------------------------------------------------------------------------ */

/*
 * Returns true and the file size in 'fileSize' on success.
 * Returns false on failure.
 */
bool
fileSize(const char* const fileName, off_t *fileSize)
{
    struct stat statBuf;

    if (stat(fileName, &statBuf) == -1) {
        return(false);
    }
    else {
        *fileSize = statBuf.st_size;
        return(true);
    }
}

/* ------------------------------------------------------------------------ */

/*
 * Aligns 'address' to be a multiple of the argument
 * 'alignment'.  If the address is not already aligned, the correct
 * value is added to make it aligned.
 */
off_t
alignAddress(off_t address, const u32 alignment)
{
    off_t         alignmentMask;

    alignmentMask = alignment - 1;

    if ((address & alignmentMask) == 0) {
        return(address);
    }
    else {
        address &= ~alignmentMask;
        address += alignment;
        return(address);
    }
}
/* ------------------------------------------------------------------------ */
