Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2385 → Rev 2386

/branches/fs/uspace/fs/utility.c
0,0 → 1,110
/* This file contains a few general purpose utility routines. */
 
/* Methods:
* fetch_name: go get a path name from user space
* no_sys: reject a system call that FS does not handle
* conv2: does byte swapping on a 16-bit int
* conv4: does byte swapping on a 32-bit long
* fs_strncmp: same like strncmp
*/
 
 
#include <unistd.h>
#include "fs.h"
#include "block.h"
#include "file.h"
#include "fproc.h"
#include "inode.h"
 
 
int fetch_name(char* path, int len)
{
/* Go get path and put it in 'user_path' */
 
register char *rpu, *rpm;
 
/* Check name length for validity */
if (len <= 0) {
err_code = FS_EINVAL;
return FS_EGENERIC;
}
 
if (len > PATH_MAX) {
err_code = FS_ENAMETOOLONG;
return FS_EGENERIC;
}
 
rpu = &user_path[0];
rpm = path;
do { *rpu++ = *rpm++;
} while (--len);
return OK;
}
int no_sys()
{
/* Somebody has used an illegal system call number */
print_console("FS_NOSYS called, illegal system call number!\n");
return FS_EINVAL;
}
unsigned conv2(int norm, int w)
{
/* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
 
if (norm) /* TRUE if no swap, FALSE for byte swap */
return((unsigned) w & 0xFFFF);
return(((w&BYTE) << 8) | ( (w>>8) & BYTE));
}
 
long conv4(int norm, long x)
{
/* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
unsigned lo, hi;
long l;
if (norm) /* byte order was already ok */
return(x);
lo = conv2(FALSE, (int) x & 0xFFFF); /* low-order half, byte swapped */
hi = conv2(FALSE, (int) (x>>16) & 0xFFFF); /* high-order half, swapped */
l = ((long) lo <<16) | hi;
return l;
}
 
int fs_strncmp(const char *src, const char *dst, size_t len)
{
int i, src_len, dst_len;
if (src == NULL) {
if (dst == NULL)
return 0;
return -1;
}
if (dst == NULL)
return 1;
 
src_len = strlen(src);
dst_len = strlen(dst);
 
for (i = 0; i < len && i < src_len && i < dst_len; i++) {
if (src[i] > dst[i])
return 1;
if (src[i] < dst[i])
return -1;
}
 
return 0;
}