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 >
Wrap
C/C++ Source or Header
|
1994-11-11
|
2KB
|
135 lines
/* NOTE: this is an OS9 format rename! 2nd arg is name only, no path */
#include "stglib.h"
#ifdef _UNIX
_rename(from,to)
char *from;
char *to;
{
return(rename(from,to));
}
#endif
#ifdef _OSK
#include <rbf.h>
#define ERR (-1)
extern int errno;
struct rbf_opt rbf;
_rename(from,to)
char *from;
char *to;
{
char dirpath[128];
char toname[32];
int path,dir;
int n;
char *p,*d;
/* parse name for validity */
n=_prsnam(to);
if (n==ERR)
return(ERR);
/* name must not have /'s */
errno=235;
if (*to=='/' || !n || n!=strlen(to) || n>28)
return(ERR);
/* put in buffer and hit bit 7 in last char */
strcpy(toname,to);
toname[n-1]|=0x80;
/* get name of directory to 'from' file */
strcpy(dirpath,from);
d=0;
p=dirpath;
while (*p)
{
if (*p=='/')
d=p;
p++;
}
if (!d)
{
strcpy(dirpath,".");
d=dirpath+1;
}
else
*d=0;
/* check for to file existing */
strcat(dirpath,"/");
strcat(dirpath,to);
path=open(dirpath,0x00);
if (path!=ERR)
{
close(path);
errno=218;
return(ERR);
}
path=open(dirpath,0x80);
if (path!=ERR)
{
close(path);
errno=218;
return(ERR);
}
*d=0;
/* open from file */
path=open(from,0x00);
if (path==ERR)
return(ERR);
/* get file info */
if (_gs_opt(path,&rbf)==ERR)
{
close(path);
return(ERR);
}
/* must be RBF type */
if (rbf.pd_dtp!=1)
{
close(path);
errno=249; /* bad type */
return(ERR);
}
/* open directory */
dir=open(dirpath,0x83);
if (dir==ERR)
{
close(path);
return(ERR);
}
/* seek to directory entry */
if (lseek(dir,(long)rbf.pd_dcp,0)==ERR)
{
close(path);
close(dir);
return(ERR);
}
/* write new name */
if (write(dir,toname,n)==ERR)
{
close(path);
close(dir);
return(ERR);
}
/* file has been renamed */
close(dir);
close(path);
return(0);
}
#endif