home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / rename.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  2KB  |  135 lines

  1. /* NOTE: this is an OS9 format rename! 2nd arg is name only, no path */
  2.  
  3. #include "stglib.h"
  4.  
  5. #ifdef _UNIX
  6. _rename(from,to)
  7. char *from;
  8. char *to;
  9. {
  10.     return(rename(from,to));
  11. }
  12. #endif
  13.  
  14. #ifdef _OSK
  15.  
  16. #include <rbf.h>
  17.  
  18. #define ERR (-1)
  19. extern int errno;
  20.  
  21. struct rbf_opt rbf;
  22.  
  23. _rename(from,to)
  24. char *from;
  25. char *to;
  26. {
  27.     char dirpath[128];
  28.     char toname[32];
  29.     int path,dir;
  30.     int n;
  31.     char *p,*d;
  32.  
  33.     /* parse name for validity */
  34.     n=_prsnam(to);
  35.     if (n==ERR)
  36.         return(ERR);
  37.  
  38.     /* name must not have /'s */
  39.     errno=235;
  40.     if (*to=='/' || !n || n!=strlen(to) || n>28)
  41.         return(ERR);
  42.  
  43.     /* put in buffer and hit bit 7 in last char */
  44.     strcpy(toname,to);
  45.     toname[n-1]|=0x80;
  46.  
  47.     /* get name of directory to 'from' file */
  48.     strcpy(dirpath,from);
  49.     d=0;
  50.     p=dirpath;
  51.     while (*p)
  52.     {
  53.         if (*p=='/')
  54.             d=p;
  55.         p++;
  56.     }
  57.     if (!d)
  58.     {
  59.         strcpy(dirpath,".");
  60.         d=dirpath+1;
  61.     }
  62.     else
  63.         *d=0;
  64.  
  65.     /* check for to file existing */
  66.     strcat(dirpath,"/");
  67.     strcat(dirpath,to);
  68.     path=open(dirpath,0x00);
  69.     if (path!=ERR)
  70.     {
  71.         close(path);
  72.         errno=218;
  73.         return(ERR);
  74.     }
  75.     path=open(dirpath,0x80);
  76.     if (path!=ERR)
  77.     {
  78.         close(path);
  79.         errno=218;
  80.         return(ERR);
  81.     }
  82.     *d=0;
  83.  
  84.     /* open from file */
  85.     path=open(from,0x00);
  86.     if (path==ERR)
  87.         return(ERR);
  88.  
  89.     /* get file info */
  90.     if (_gs_opt(path,&rbf)==ERR)
  91.     {
  92.         close(path);
  93.         return(ERR);
  94.     }
  95.  
  96.     /* must be RBF type */
  97.     if (rbf.pd_dtp!=1)
  98.     {
  99.         close(path);
  100.         errno=249;    /* bad type */
  101.         return(ERR);
  102.     }
  103.  
  104.     /* open directory */
  105.     dir=open(dirpath,0x83);
  106.     if (dir==ERR)
  107.     {
  108.         close(path);
  109.         return(ERR);
  110.     }
  111.  
  112.     /* seek to directory entry */
  113.     if (lseek(dir,(long)rbf.pd_dcp,0)==ERR)
  114.     {
  115.         close(path);
  116.         close(dir);
  117.         return(ERR);
  118.     }
  119.  
  120.     /* write new name */
  121.     if (write(dir,toname,n)==ERR)
  122.     {
  123.         close(path);
  124.         close(dir);
  125.         return(ERR);
  126.     }
  127.  
  128.     /* file has been renamed */
  129.     close(dir);
  130.     close(path);
  131.     return(0);
  132. }
  133.  
  134. #endif
  135.