Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/plt.c

raw
1 #define _GNU_SOURCE
2 #include <string.h>
3 #include <libgen.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <printf.h>
10
11 #define SIZE 256
12
13 /* Examples from libc.info */
14
15 #if defined (LIBPLT2) || defined (NOLIBPLT)
16 void *
17 fatal (const char *ptr)
18 {
19 puts (ptr);
20 exit (1);
21 }
22
23
24 void *
25 xmalloc (size_t size)
26 {
27 register void *value = malloc (size);
28 if (value == 0)
29 fatal ("virtual memory exhausted");
30 return value;
31 }
32
33 char *
34 savestring (const char *ptr)
35 {
36 size_t len = sizeof (ptr);
37 register char *value = (char *) xmalloc (len + 1);
38 value[len] = '\0';
39 return (char *) memcpy (value, ptr, len);
40 }
41
42 int
43 open2 (char *str1, char *str2, int flags)
44 {
45 char *end;
46 char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
47 end = stpcpy (stpcpy (name, str1), str2);
48 return open (name, flags) && end == name;
49 }
50
51 int
52 open3 (char *str1, char *str2, int flags)
53 {
54 char *end;
55 char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);
56 int desc;
57 if (name == 0)
58 fatal ("virtual memory exceeded");
59 end = stpcpy (stpcpy (name, str1), str2);
60 desc = open (name, flags);
61 free (name);
62 return desc && end == str1;
63 }
64
65 char *
66 basename2 (char *prog)
67 {
68 return basename (prog);
69 }
70
71 /* Define an array of critters to sort. */
72
73 struct critter
74 {
75 const char *name;
76 const char *species;
77 };
78
79 struct critter muppets[] =
80 {
81 {"Kermit", "frog"},
82 {"Piggy", "pig"},
83 {"Gonzo", "whatever"},
84 {"Fozzie", "bear"},
85 {"Sam", "eagle"},
86 {"Robin", "frog"},
87 {"Animal", "animal"},
88 {"Camilla", "chicken"},
89 {"Sweetums", "monster"},
90 {"Dr. Strangepork", "pig"},
91 {"Link Hogthrob", "pig"},
92 {"Zoot", "human"},
93 {"Dr. Bunsen Honeydew", "human"},
94 {"Beaker", "human"},
95 {"Swedish Chef", "human"}
96 };
97
98 int count = sizeof (muppets) / sizeof (struct critter);
99
100 /* This is the comparison function used for sorting and searching. */
101
102 int
103 critter_cmp (const void *c1, const void *c2)
104 {
105 return strcmp (((const struct critter*)c1)->name, ((const struct critter*)c2)->name);
106 }
107
108
109 /* Print information about a critter. */
110
111 void
112 print_critter (const struct critter *c)
113 {
114 printf ("%s, the %s\n", c->name, c->species);
115 }
116
117
118 /* Do the lookup into the sorted array. */
119
120 void
121 find_critter (const char *name)
122 {
123 struct critter target, *result;
124 target.name = name;
125 result = bsearch (&target, muppets, count, sizeof (struct critter),
126 critter_cmp);
127 if (result)
128 print_critter (result);
129 else
130 printf ("Couldn't find %s.\n", name);
131 }
132
133 int
134 critters (void)
135 {
136 int i;
137
138 for (i = 0; i < count; i++)
139 print_critter (&muppets[i]);
140 printf ("\n");
141
142 qsort (muppets, count, sizeof (struct critter), critter_cmp);
143 for (i = 0; i < count; i++)
144 print_critter (&muppets[i]);
145 printf ("\n");
146
147 find_critter ("Kermit");
148 find_critter ("Gonzo");
149 find_critter ("Janice");
150
151 return 0;
152 }
153
154 typedef struct
155 {
156 char *name;
157 }
158 Widget;
159
160 int
161 print_widget (FILE *stream,
162 const struct printf_info *info,
163 const void *const *args)
164 {
165 const Widget *w;
166 char *buffer;
167 int len;
168
169 /* Format the output into a string. */
170 w = *((const Widget **) (args[0]));
171 len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
172 if (len == -1)
173 return -1;
174
175 /* Pad to the minimum field width and print to the stream. */
176 len = fprintf (stream, "%*s",
177 (info->left ? -info->width : info->width),
178 buffer);
179 /* Clean up and return. */
180 free (buffer);
181 return len;
182 }
183
184
185 /* Type of a printf specifier-arginfo function.
186 INFO gives information about the format specification.
187 N, ARGTYPES, *SIZE has to contain the size of the parameter for
188 user-defined types, and return value are as for parse_printf_format
189 except that -1 should be returned if the handler cannot handle
190 this case. This allows to partially overwrite the functionality
191 of existing format specifiers. */
192
193 int
194 print_widget_arginfo (const struct printf_info *info, size_t n,
195 int *argtypes)
196 {
197 /* We always take exactly one argument and this is a pointer to the
198 structure.. */
199 if (n > 0)
200 argtypes[0] = PA_POINTER;
201 return 1;
202 }
203
204
205 int
206 widgets (void)
207 {
208 /* Make a widget to print. */
209 Widget mywidget;
210 mywidget.name = "mywidget";
211
212 /* Register the print function for widgets. */
213 register_printf_function ('W', print_widget, print_widget_arginfo);
214
215 /* Now print the widget. */
216 printf ("|%W|\n", &mywidget);
217 printf ("|%35W|\n", &mywidget);
218 printf ("|%-35W|\n", &mywidget);
219
220 return 0;
221 }
222
223
224 int
225 datetime (void)
226 {
227 char buffer[SIZE];
228 time_t curtime;
229 struct tm *loctime;
230
231 /* Get the current time. */
232 curtime = time (NULL);
233
234 /* Convert it to local time representation. */
235 loctime = localtime (&curtime);
236
237 /* Print out the date and time in the standard format. */
238 fputs (asctime (loctime), stdout);
239
240 /* Print it out in a nice format. */
241 strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
242 fputs (buffer, stdout);
243 strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
244 fputs (buffer, stdout);
245
246 return 0;
247 }
248 #endif
249
250
251 #if defined (LIBPLT1) // decls for LIBPLT2 pieces
252 void * fatal (const char *ptr);
253 void * xmalloc (size_t size);
254 char * savestring (const char *ptr);
255 int open2 (char *str1, char *str2, int flags);
256 int open3 (char *str1, char *str2, int flags);
257 char * basename2 (char *prog);
258 int critters (void);
259 int widgets (void);
260 int datetime (void);
261 #endif
262
263
264
265 #if defined (LIBPLT1) || defined (NOLIBPLT)
266 void
267 zenme ()
268 {
269 xmalloc (32);
270 savestring ("abcdefghijklmnopqrstuvwxyz");
271 open2 ("/dev", "/null", O_RDONLY);
272 open3 ("/dev", "/null", O_RDONLY);
273 basename2 ("/dev/null");
274 critters();
275 widgets();
276 datetime();
277 }
278 #endif
279
280 #if defined (NOLIBPLT) || defined (ONLY_MAIN)
281 void zenme (void);
282
283 int
284 main ()
285 {
286 zenme ();
287 return 0;
288 }
289 #endif