kernel/vm.c
int
copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
{
  uint64 n, va0, pa0;
  int got_null = 0;

  while(got_null == 0 && max > 0){
    va0 = PGROUNDDOWN(srcva);
    pa0 = walkaddr(pagetable, va0);
    if(pa0 == NULL)
      return -1;
    n = PGSIZE - (srcva - va0);
    if(n > max)
      n = max;

    char *p = (char *) (pa0 + (srcva - va0));
    while(n > 0){
      if(*p == '\0'){
        *dst = '\0';
        got_null = 1;
        break;
      } else {
        *dst = *p;
      }
      --n;
      --max;
      p++;
      dst++;
    }

    srcva = va0 + PGSIZE;
  }
  if(got_null){
    return 0;
  } else {
    return -1;
  }
}
// Copy a null-terminated string from user to kernel.
// Copy bytes to dst from virtual address srcva in a given page table,
// until a '\0', or max.
// Return 0 on success, -1 on error.
Copy a null-terminated string from user to kernel.
Copy bytes to dst from virtual address srcva in a given page table,
until a '\0', or max.
Return 0 on success, -1 on error.
Copy a null - terminated string from user to kernel . Copy bytes to dst from virtual address srcva in a given page table until a ' \ 0 ' or max . Return 0 on success - 1 on error .
int
copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
{
  uint64 n, va0, pa0;
  int got_null = 0;
  while(got_null == 0 && max > 0){
    va0 = PGROUNDDOWN(srcva);
    pa0 = walkaddr(pagetable, va0);
    if(pa0 == NULL)
      return -1;
    n = PGSIZE - (srcva - va0);
    if(n > max)
      n = max;
    char *p = (char *) (pa0 + (srcva - va0));
    while(n > 0){
      if(*p == '\0'){
        *dst = '\0';
        got_null = 1;
        break;
      } else {
        *dst = *p;
      }
      --n;
      --max;
      p++;
      dst++;
    }
    srcva = va0 + PGSIZE;
  }
  if(got_null){
    return 0;
  } else {
    return -1;
  }
}
int copyinstr ( pagetable_t pagetable , char * dst , uint64 srcva , uint64 max ) { uint64 n , va0 , pa0 ; int got_null = 0 ; while ( got_null == 0 && max > 0 ) { va0 = PGROUNDDOWN ( srcva ) ; pa0 = walkaddr ( pagetable , va0 ) ; if ( pa0 == NULL ) return -1 ; n = PGSIZE - ( srcva - va0 ) ; if ( n > max ) n = max ; char * p = ( char * ) ( pa0 + ( srcva - va0 ) ) ; while ( n > 0 ) { if ( * p == ' \0 ' ) { * dst = ' \0 ' ; got_null = 1 ; break ; } else { * dst = * p ; } -- n ; -- max ; p ++ ; dst ++ ; } srcva = va0 + PGSIZE ; } if ( got_null ) { return 0 ; } else { return -1 ; } }
Copy a null-terminated string from user to kernel.
Copy a null - terminated string from user to kernel .