chromium/third_party/fontconfig/src/src/fccompat.c

/*
 * fontconfig/src/fccompat.c
 *
 * Copyright © 2012 Red Hat, Inc.
 *
 * Author(s):
 *  Akira TAGOH
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the author(s) not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  The authors make no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "fcint.h"

#include <errno.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef O_CLOEXEC
#define FC_O_CLOEXEC
#else
#define FC_O_CLOEXEC
#endif
#ifdef O_LARGEFILE
#define FC_O_LARGEFILE
#else
#define FC_O_LARGEFILE
#endif
#ifdef O_BINARY
#define FC_O_BINARY
#else
#define FC_O_BINARY
#endif
#ifdef O_TEMPORARY
#define FC_O_TEMPORARY
#else
#define FC_O_TEMPORARY
#endif
#ifdef O_NOINHERIT
#define FC_O_NOINHERIT
#else
#define FC_O_NOINHERIT
#endif

#ifndef HAVE_UNISTD_H
/* Values for the second argument to access. These may be OR'd together. */
#ifndef R_OK
#define R_OK
#endif
#ifndef W_OK
#define W_OK
#endif
#ifndef F_OK
#define F_OK
#endif

typedef int mode_t;
#endif /* !HAVE_UNISTD_H */

#if !defined (HAVE_MKOSTEMP) && !defined(HAVE_MKSTEMP) && !defined(HAVE__MKTEMP_S)
static int
mkstemp (char *template)
{
    static const char s[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int fd, i;
    size_t l;

    if (template == NULL)
    {
	errno = EINVAL;
	return -1;
    }
    l = strlen (template);
    if (l < 6 || strcmp (&template[l - 6], "XXXXXX") != 0)
    {
	errno = EINVAL;
	return -1;
    }
    do
    {
	errno = 0;
	for (i = l - 6; i < l; i++)
	{
	    int r = FcRandom ();
	    template[i] = s[r % 62];
	}
	fd = FcOpen (template, FC_O_BINARY | O_CREAT | O_EXCL | FC_O_TEMPORARY | FC_O_NOINHERIT | O_RDWR, 0600);
    } while (fd < 0 && errno == EEXIST);
    if (fd >= 0)
	errno = 0;

    return fd;
}
#define HAVE_MKSTEMP
#endif

int
FcOpen(const char *pathname, int flags, ...)
{}

int
FcMakeTempfile (char *template)
{}

int32_t
FcRandom(void)
{}

#ifdef _WIN32
#include <direct.h>
#define mkdir
#endif

FcBool
FcMakeDirectory (const FcChar8 *dir)
{}

ssize_t
FcReadLink (const FcChar8 *pathname,
	    FcChar8       *buf,
	    size_t         bufsiz)
{}

/* On Windows MingW provides dirent.h / openddir(), but MSVC does not */
#ifndef HAVE_DIRENT_H

struct DIR {
    struct dirent d_ent;
    HANDLE handle;
    WIN32_FIND_DATA fdata;
    FcBool valid;
};

FcPrivate DIR *
FcCompatOpendirWin32 (const char *dirname)
{
    size_t len;
    char *name;
    DIR *dir;

    dir = calloc (1, sizeof (struct DIR));
    if (dir == NULL)
        return NULL;

    len = strlen (dirname);
    name = malloc (len + 3);
    if (name == NULL)
    {
      free (dir);
      return NULL;
    }
    memcpy (name, dirname, len);
    name[len++] = FC_DIR_SEPARATOR;
    name[len++] = '*';
    name[len] = '\0';

    dir->handle = FindFirstFileEx (name, FindExInfoBasic, &dir->fdata, FindExSearchNameMatch, NULL, 0);

    free (name);

    if (!dir->handle)
    {
        free (dir);
        dir = NULL;

        if (GetLastError () == ERROR_FILE_NOT_FOUND)
            errno = ENOENT;
        else
            errno = EACCES;
    }

    dir->valid = FcTrue;
    return dir;
}

FcPrivate struct dirent *
FcCompatReaddirWin32 (DIR *dir)
{
    if (dir->valid != FcTrue)
        return NULL;

    dir->d_ent.d_name = dir->fdata.cFileName;

    if ((dir->fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
        dir->d_ent.d_type = DT_DIR;
    else if (dir->fdata.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
        dir->d_ent.d_type = DT_REG;
    else
        dir->d_ent.d_type = DT_UNKNOWN;

    if (!FindNextFile (dir->handle, &dir->fdata))
        dir->valid = FcFalse;

    return &dir->d_ent;
}

FcPrivate int
FcCompatClosedirWin32 (DIR *dir)
{
    if (dir != NULL && dir->handle != NULL)
    {
        FindClose (dir->handle);
        free (dir);
    }
    return 0;
}
#endif /* HAVE_DIRENT_H */

#define __fccompat__
#include "fcaliastail.h"
#undef __fccompat__