Subversion Repositories HelenOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* This file contains a few general purpose utility routines. */
  2.  
  3. /* Methods:
  4.  * fetch_name:  go get a path name from user space
  5.  * no_sys:      reject a system call that FS does not handle
  6.  * conv2:       does byte swapping on a 16-bit int
  7.  * conv4:       does byte swapping on a 32-bit long
  8.  * fs_strncmp:  same like strncmp
  9.  */
  10.  
  11.  
  12. #include <unistd.h>
  13. #include "fs.h"
  14. #include "block.h"
  15. #include "file.h"
  16. #include "fproc.h"
  17. #include "inode.h"
  18.  
  19.  
  20. int fetch_name(char* path, int len)
  21. {
  22.    
  23.     /* Go get path and put it in 'user_path' */
  24.  
  25.     register char *rpu, *rpm;
  26.  
  27.     /* Check name length for validity */
  28.     if (len <= 0) {
  29.         err_code = FS_EINVAL;
  30.         return FS_EGENERIC;
  31.     }
  32.  
  33.     if (len > PATH_MAX) {
  34.         err_code = FS_ENAMETOOLONG;
  35.         return FS_EGENERIC;
  36.     }
  37.  
  38.     rpu = &user_path[0];
  39.     rpm = path;
  40.     do { *rpu++ = *rpm++;
  41.         } while (--len);
  42.      
  43.     return OK;
  44. }
  45.    
  46. int no_sys()
  47. {
  48.    
  49.     /* Somebody has used an illegal system call number */
  50.          
  51.     print_console("FS_NOSYS called, illegal system call number!\n");
  52.    
  53.     return FS_EINVAL;
  54. }
  55.    
  56. unsigned conv2(int norm, int w)
  57. {
  58.    
  59.     /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
  60.  
  61.     if (norm)   /* TRUE if no swap, FALSE for byte swap */
  62.         return((unsigned) w & 0xFFFF);
  63.        
  64.     return(((w&BYTE) << 8) | ( (w>>8) & BYTE));
  65. }
  66.  
  67. long conv4(int norm, long x)
  68. {
  69.    
  70.     /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
  71.    
  72.     unsigned lo, hi;
  73.     long l;
  74.      
  75.     if (norm)       /* byte order was already ok */
  76.         return(x);
  77.                        
  78.     lo = conv2(FALSE, (int) x & 0xFFFF);        /* low-order half, byte swapped */
  79.     hi = conv2(FALSE, (int) (x>>16) & 0xFFFF);  /* high-order half, swapped */
  80.     l = ((long) lo <<16) | hi;
  81.      
  82.     return l;
  83. }
  84.  
  85. int fs_strncmp(const char *src, const char *dst, size_t len)
  86. {
  87.    
  88.     int i, src_len, dst_len;
  89.        
  90.     if (src == NULL) {
  91.         if (dst == NULL)
  92.             return 0;
  93.         return -1; 
  94.     }
  95.    
  96.     if (dst == NULL)
  97.         return 1;
  98.  
  99.     src_len = strlen(src);
  100.     dst_len = strlen(dst);
  101.  
  102.     for (i = 0; i < len && i < src_len && i < dst_len; i++) {
  103.         if (src[i] > dst[i])
  104.             return 1;
  105.         if (src[i] < dst[i])
  106.             return -1;
  107.     }
  108.  
  109.     return 0;
  110. }
  111.