/* rename - change the name of file
 * 91.11.02 by Tomohiro Ishikawa (ishikawa@gaia.cow.melco.CO.JP)
 * 92.01.20 little modified (added #ifdef) by Masaru Oki
 */

#ifdef NOFTRUNCATE
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int
rename(from, to)
char *from, *to;
{
    struct    stat s1,s2;
    extern    int errno;

    if (stat(from, &s1) < 0)
        return(-1);
    /* is 'FROM' file a directory? */
    if ((s1.st_mode & S_IFMT) == S_IFDIR){
        errno = ENOTDIR;
        return(-1);
    }
    if (stat(to, &s2) >= 0) { /* 'TO' exists! */
        /* is 'TO' file a directory? */
        if ((s2.st_mode & S_IFMT) == S_IFDIR){
            errno=EISDIR;
            return(-1);
        }
        if (unlink(to) < 0)
            return(-1);
    }
    if (link(from, to) < 0)
        return(-1);
    if (unlink(from) < 0)
        return(-1);
    return(0);
}
#endif /* NOFTRUNCATE */
#ifdef NOMKDIR
#ifndef NOFTRUNCATE
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#ifndef MKDIRPATH
#define MKDIRPATH    "/bin/mkdir"
#endif
#ifndef RMDIRPATH
#define RMDIRPATH    "/bin/rmdir"
#endif
int
rmdir(path)
char *path;
{
    int    stat,rtn=0;
    char    *cmdname;
    if ( (cmdname=(char *)malloc(strlen(RMDIRPATH)+1+strlen(path)+1)) == 0)
        return(-1);
    strcpy(cmdname,RMDIRPATH);
    *(cmdname+strlen(RMDIRPATH))=' ';
    strcpy(cmdname+strlen(RMDIRPATH)+1,path);
    if ((stat = system(cmdname))<0)
        rtn=-1;    /* fork or exec error */
    else if(stat){        /* RMDIR command error */
        errno = EIO;
        rtn=-1;
    }
    free(cmdname);
    return(rtn);
}
int
mkdir(path,mode)
char   *path;
int    mode;
{
    int  child, stat;
    char *cmdname,*cmdpath=MKDIRPATH;
    if ( (cmdname=(char *)strrchr(cmdpath,'/'))==(char *)0)
        cmdname=cmdpath;
    if ((child = fork())<0)
        return(-1);    /* fork error */
    else if(child) {    /* parent process */
        while (child != wait(&stat))    /* ignore signals */
            continue;
    }
    else{            /* child process */
        int    maskvalue;
        maskvalue = umask(0);    /* get current umask() value */
        umask(maskvalue | (0777 & ~mode)); /* set it! */
        execl(cmdpath, cmdname, path, (char *)0);
        /* never come here except execl is error */
        return(-1);
    }
    if (stat != 0) {
        errno = EIO;    /* cannot get error num. */
        return(-1);
    }
    return(0);
}
#endif
