/***{{{*******************************************************************
* This file is part of libco - object library for C *
* Copyright (c) 2002 *
* Peter Rockai (yenar) <yenar@host.sk> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, *
* Boston, MA 02111-1307 USA *
*******************************************************************}}}***/
/* {{{ file description */
/**
* @file co_filesystem.c
* @brief File and filename manipulation and utility functions.
**/
/* }}} */
#if 0
/* {{{ includes */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h> // unlink
#include <errno.h>
#include "co_debug.h"
#include "co_util.h"
#include "co_filesystem.h"
/* }}} */
/* {{{ */
int fcopy (const char *src, const char *dest)
/// Copy file \a src to file \a dest.
{
register int inb, outb;
FILE *in, *out;
char blkbuf[4096];
if ((in = fopen(src, "r")) == NULL)
return -1;
if ((out = fopen(dest, "w")) == NULL) {
fclose(in);
return -1;
}
do {
inb = fread(blkbuf, 1, 4096, in);
outb = fwrite(blkbuf, 1, inb, out);
} while (outb == inb && inb == 4096);
fclose(out);
fclose(in);
return outb == inb ? 0 : -1;
}
/* }}} */
/* {{{ */
int frename (const char *src, const char *dest)
/// Rename or move file, even if not on same filesystem
/** \param src name of file to move
* \param dest destination filename */
{
CO_DEBUG (1, "rename %s -> %s", src, dest);
if (rename (src, dest)) {
if (errno == EXDEV)
return fcopy (src, dest) || unlink (src);
return 1;
}
return 0;
}
/* }}} */
/* {{{ */
char *fnewsuff (const char *name, const char *suff)
/// Strip old filename suffix and replace it with new one.
/** \param name original filename
* \param suff new_p suffix
* \return (newly allocated) string with result */
{
char *filedot;
char *toret = new_ar (char, strlen (name) + strlen (suff) + 1);
strcpy (toret, name);
filedot = strrchr (toret, '.');
if (filedot) (*filedot)=0; // strip suffix
strcat (toret, suff);
return toret;
}
/* }}} */
/* {{{ */
char *ffind (const char *fname, int perm, ...)
/// Get filename of first accessible file in set of paths.
/** \param fname filename
* \param perm minimal permission requirements
* \param varargs directories to try, list terminated by 0 argument
* \return malloced filename if found, 0 otherwise */
{
va_list ap; char *tmp, *ca;
va_start (ap, perm);
tmp = new_ar (char, 256);
while ((ca = va_arg (ap, char *))) {
strcpy (tmp, ca);
strcat (tmp, fname);
if (!access (tmp, perm)) {
va_end (ap);
return tmp;
}
}
va_end (ap);
free (tmp);
return 0;
}
/* }}} */
#endif /* 0 */
Platon Group <platon@platon.org> http://platon.org/
|