All pastes #1064091 Raw Edit

mkdtemp.c

public c v1 · immutable
#1064091 ·published 2008-07-06 20:55 UTC
rendered paste body
/* This file is part of the KDE Libraries. *  * Copyright (C) 2008 Stefan Teleman <teleman@kde.org> *  * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. *  * This library 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 * Library General Public License for more details. *  * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB.  If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <mkdtemp.h>#include <pthread.h>#if defined(_BSD_SOURCE)#ifdef __cplusplusextern "C" {#endifextern int errno;static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;char* mkdtemp(char* template){    char* tmpname;    int ret;    pthread_mutex_lock(&mutex);    errno = 0;    tmpname = mktemp(template);    if (tmpname == NULL)        goto einval;    if ((mkdir((const char *) tmpname, (mode_t) 0700)) != 0)        goto edirfail;    pthread_mutex_unlock(&mutex);    return tmpname;einval:    errno = EINVAL;    pthread_mutex_unlock(&mutex);    return NULL;edirfail:    pthread_mutex_unlock(&mutex);    return NULL;}#ifdef __cplusplus}#endif#endif /* _BSD_SOURCE */