home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / ARCHIVERS / lhasrc.lzh / rename.c < prev    next >
Text File  |  1992-05-13  |  2KB  |  104 lines

  1. /* rename - change the name of file
  2.  * 91.11.02 by Tomohiro Ishikawa (ishikawa@gaia.cow.melco.CO.JP)
  3.  * 92.01.20 little modified (added #ifdef) by Masaru Oki
  4.  */
  5.  
  6. #ifdef NOFTRUNCATE
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. int
  12. rename(from, to)
  13. char *from, *to;
  14. {
  15.     struct    stat s1,s2;
  16.     extern    int errno;
  17.  
  18.     if (stat(from, &s1) < 0)
  19.         return(-1);
  20.     /* is 'FROM' file a directory? */
  21.     if ((s1.st_mode & S_IFMT) == S_IFDIR){
  22.         errno = ENOTDIR;
  23.         return(-1);
  24.     }
  25.     if (stat(to, &s2) >= 0) { /* 'TO' exists! */
  26.         /* is 'TO' file a directory? */
  27.         if ((s2.st_mode & S_IFMT) == S_IFDIR){
  28.             errno=EISDIR;
  29.             return(-1);
  30.         }
  31.         if (unlink(to) < 0)
  32.             return(-1);
  33.     }
  34.     if (link(from, to) < 0)
  35.         return(-1);
  36.     if (unlink(from) < 0)
  37.         return(-1);
  38.     return(0);
  39. }
  40. #endif /* NOFTRUNCATE */
  41. #ifdef NOMKDIR
  42. #ifndef NOFTRUNCATE
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #endif
  48. #ifndef MKDIRPATH
  49. #define MKDIRPATH    "/bin/mkdir"
  50. #endif
  51. #ifndef RMDIRPATH
  52. #define RMDIRPATH    "/bin/rmdir"
  53. #endif
  54. int
  55. rmdir(path)
  56. char *path;
  57. {
  58.     int    stat,rtn=0;
  59.     char    *cmdname;
  60.     if ( (cmdname=(char *)malloc(strlen(RMDIRPATH)+1+strlen(path)+1)) == 0)
  61.         return(-1);
  62.     strcpy(cmdname,RMDIRPATH);
  63.     *(cmdname+strlen(RMDIRPATH))=' ';
  64.     strcpy(cmdname+strlen(RMDIRPATH)+1,path);
  65.     if ((stat = system(cmdname))<0)
  66.         rtn=-1;    /* fork or exec error */
  67.     else if(stat){        /* RMDIR command error */
  68.         errno = EIO;
  69.         rtn=-1;
  70.     }
  71.     free(cmdname);
  72.     return(rtn);
  73. }
  74. int
  75. mkdir(path,mode)
  76. char   *path;
  77. int    mode;
  78. {
  79.     int  child, stat;
  80.     char *cmdname,*cmdpath=MKDIRPATH;
  81.     if ( (cmdname=(char *)strrchr(cmdpath,'/'))==(char *)0)
  82.         cmdname=cmdpath;
  83.     if ((child = fork())<0)
  84.         return(-1);    /* fork error */
  85.     else if(child) {    /* parent process */
  86.         while (child != wait(&stat))    /* ignore signals */
  87.             continue;
  88.     }
  89.     else{            /* child process */
  90.         int    maskvalue;
  91.         maskvalue = umask(0);    /* get current umask() value */
  92.         umask(maskvalue | (0777 & ~mode)); /* set it! */
  93.         execl(cmdpath, cmdname, path, (char *)0);
  94.         /* never come here except execl is error */
  95.         return(-1);
  96.     }
  97.     if (stat != 0) {
  98.         errno = EIO;    /* cannot get error num. */
  99.         return(-1);
  100.     }
  101.     return(0);
  102. }
  103. #endif
  104.