File: 302actions-strings.d

package info (click to toggle)
dtrace 2.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 24,408 kB
  • sloc: ansic: 61,247; sh: 17,997; asm: 1,717; lex: 947; awk: 754; yacc: 695; perl: 37; sed: 17; makefile: 15
file content (52 lines) | stat: -rwxr-xr-x 1,498 bytes parent folder | download
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
/*
 * Linux DTrace
 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */

#!/usr/sbin/dtrace -s

#pragma D option quiet

/*
 *  SYNOPSIS
 *    sudo ./302actions-strings.d
 *
 *  DESCRIPTION
 *    There are functions to manipulate strings.
 */

BEGIN
{
	/* find the length of a string */
	printf("length of 'abcde' is %d\n", strlen("abcde"));

	/* various other functions also resemble libc */
	printf("text starts with first 'd': %s\n", strchr("abcdefedbca", 'd'));
	printf("text starts with last 'd': %s\n", strrchr("abcdefedbca", 'd'));
	printf("text starts with 'def': %s\n", strstr("abcdefedbca", "def"));

	/* break a string up into tokens */
	printf("token #1: %s\n", strtok("hello world", " "));
	printf("token #2: %s\n", strtok(NULL, " "));

	/* join (concatenate) strings */
	printf("join 'abc' and 'def': %s\n", strjoin("abc", "def"));

	/* dump part of a string */
	printf("start at char #4: %s\n", substr("abcdefghij", 4));

	/* dump part of a string, only 2 chars */
	printf("start at char #4: %s\n", substr("abcdefghij", 4, 2));

	/* find index of a substring */
	printf("found at char #%d\n", index("abcdefghij", "def"));
	printf("found at char #%d\n", rindex("abcdefghij", "def"));
	printf("found at char #%d\n", index("abcdefghij", "XXXX"));

	/* convert an integer to a string */
	printf("123000 + 456 = %s\n", lltostr(123000 + 456));

	exit(0);
}