aboutsummaryrefslogtreecommitdiffstats
path: root/src/userprog/test.c
blob: 6d884d612cac25da0a4c3d6d3a830dd14406f423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>

typedef __SIZE_TYPE__ size_t;

/* As strlcpy, but only copies the first word from SRC. Worde are
 * delimeted by space. Leading space is also ignored.
 * ( added by klaar@ida )
 */
size_t
strlcpy_first_word (char *dst, const char *src, size_t size) 
{
  size_t i = size;
  
  /* skip leading spaces */
  while (*src != '\0' && *src == ' ')
    ++src;

  /* copy at most size characters */
  if (size > 0)
  {
    while (i > 1)
    {
      if (*src == '\0' || *src == ' ')
        break;
      
      *dst++ = *src++;
      --i;
    }
    *dst = '\0';
  }
  return size - i;
}

int main(int argc, char* argv[])
{
  char word[100];
  int i;
  
  i = strlcpy_first_word(word, argv[1], -1);

  printf("Argv[1]: '%s', (%d characters)\n", argv[1], strlen(argv[1]));
  printf("First w: '%s', (%d characters)\n", word, strlen(word));
  printf("Copied %d characters of max %d\n\n", i, -1);

  
  i = strlcpy_first_word(word, argv[1], 0);

  printf("Argv[1]: '%s', (%d characters)\n", argv[1], strlen(argv[1]));
  printf("First w: '%s', (%d characters)\n", word, strlen(word));
  printf("Copied %d characters of max %d\n\n", i, 0);


  i = strlcpy_first_word(word, argv[1], 1);

  printf("Argv[1]: '%s', (%d characters)\n", argv[1], strlen(argv[1]));
  printf("First w: '%s', (%d characters)\n", word, strlen(word));
  printf("Copied %d characters of max %d\n\n", i, 1);


  i = strlcpy_first_word(word, argv[1], 10);

  printf("Argv[1]: '%s', (%d characters)\n", argv[1], strlen(argv[1]));
  printf("First w: '%s', (%d characters)\n", word, strlen(word));
  printf("Copied %d characters of max %d\n\n", i, 10);

}