param_key: fix container of when no struct member is referenced
[smatch.git] / smatch_type.c
blob35468d4aaa862fe808699532f8a10c2e5503dc93
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The idea here is that you have an expression and you
20 * want to know what the type is for that.
23 #include "smatch.h"
24 #include "smatch_slist.h"
26 struct symbol *get_real_base_type(struct symbol *sym)
28 struct symbol *ret;
30 if (!sym)
31 return NULL;
32 if (sym->type == SYM_BASETYPE)
33 return sym;
34 ret = get_base_type(sym);
35 if (!ret)
36 return NULL;
37 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
38 return get_real_base_type(ret);
39 return ret;
42 int type_bytes(struct symbol *type)
44 int bits;
46 if (type && type->type == SYM_ARRAY)
47 return array_bytes(type);
49 bits = type_bits(type);
50 if (bits < 0)
51 return 0;
52 return bits_to_bytes(bits);
55 int array_bytes(struct symbol *type)
57 if (!type || type->type != SYM_ARRAY)
58 return 0;
59 if (!type->array_size)
60 return 0;
61 return bits_to_bytes(type->bit_size);
64 static struct symbol *get_binop_type(struct expression *expr)
66 struct symbol *left, *right;
68 left = get_type(expr->left);
69 if (!left)
70 return NULL;
72 if (expr->op == SPECIAL_LEFTSHIFT ||
73 expr->op == SPECIAL_RIGHTSHIFT) {
74 if (type_positive_bits(left) < 31)
75 return &int_ctype;
76 return left;
78 right = get_type(expr->right);
79 if (!right)
80 return NULL;
82 if (type_is_fp(left)) {
83 if (type_is_fp(right)) {
84 if (type_bits(left) > type_bits(right))
85 return left;
86 return right;
88 return left;
91 if (type_is_fp(right)) {
92 if (type_is_fp(left)) {
93 if (type_bits(right) > type_bits(left))
94 return right;
95 return left;
97 return right;
100 if (expr->op == '-' &&
101 (is_ptr_type(left) && is_ptr_type(right)))
102 return ssize_t_ctype;
104 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
105 return left;
106 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
107 return right;
109 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
110 return &int_ctype;
112 if (type_positive_bits(left) > type_positive_bits(right))
113 return left;
114 return right;
117 static struct symbol *get_type_symbol(struct expression *expr)
119 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
120 return NULL;
122 return get_real_base_type(expr->symbol);
125 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
127 struct symbol *tmp, *sub;
129 FOR_EACH_PTR(symbol_list, tmp) {
130 if (!tmp->ident) {
131 sub = get_real_base_type(tmp);
132 sub = get_member_symbol(sub->symbol_list, member);
133 if (sub)
134 return sub;
135 continue;
137 if (tmp->ident == member)
138 return tmp;
139 } END_FOR_EACH_PTR(tmp);
141 return NULL;
144 static struct symbol *get_symbol_from_deref(struct expression *expr)
146 struct ident *member;
147 struct symbol *sym;
149 if (!expr || expr->type != EXPR_DEREF)
150 return NULL;
152 member = expr->member;
153 sym = get_type(expr->deref);
154 if (!sym) {
155 // sm_msg("could not find struct type");
156 return NULL;
158 if (sym->type == SYM_PTR)
159 sym = get_real_base_type(sym);
160 sym = get_member_symbol(sym->symbol_list, member);
161 if (!sym)
162 return NULL;
163 return get_real_base_type(sym);
166 static struct symbol *handle__builtin_choose_expr(struct expression *expr)
168 struct expression *const_expr, *expr1, *expr2;
169 sval_t sval;
171 const_expr = get_argument_from_call_expr(expr->args, 0);
172 expr1 = get_argument_from_call_expr(expr->args, 1);
173 expr2 = get_argument_from_call_expr(expr->args, 2);
175 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
176 return NULL;
177 if (sval.value)
178 return get_type(expr1);
179 else
180 return get_type(expr2);
183 static struct symbol *get_return_type(struct expression *expr)
185 struct symbol *tmp;
187 if (sym_name_is("__builtin_choose_expr", expr->fn))
188 return handle__builtin_choose_expr(expr);
190 tmp = get_type(expr->fn);
191 if (!tmp)
192 return NULL;
193 /* this is to handle __builtin_constant_p() */
194 if (tmp->type != SYM_FN)
195 tmp = get_base_type(tmp);
196 return get_real_base_type(tmp);
199 static struct symbol *get_expr_stmt_type(struct statement *stmt)
201 if (stmt->type != STMT_COMPOUND)
202 return NULL;
203 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
204 if (stmt->type == STMT_LABEL)
205 stmt = stmt->label_statement;
206 if (stmt->type != STMT_EXPRESSION)
207 return NULL;
208 return get_type(stmt->expression);
211 static struct symbol *get_select_type(struct expression *expr)
213 struct symbol *one, *two;
215 one = get_type(expr->cond_true);
216 two = get_type(expr->cond_false);
217 if (!one || !two)
218 return NULL;
220 * This is a hack. If the types are not equiv then we
221 * really don't know the type. But I think guessing is
222 * probably Ok here.
224 if (type_positive_bits(one) > type_positive_bits(two))
225 return one;
226 return two;
229 struct symbol *get_pointer_type(struct expression *expr)
231 struct symbol *sym;
233 sym = get_type(expr);
234 if (!sym)
235 return NULL;
236 if (sym->type == SYM_NODE) {
237 sym = get_real_base_type(sym);
238 if (!sym)
239 return NULL;
241 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
242 return NULL;
243 return get_real_base_type(sym);
246 static struct symbol *fake_pointer_sym(struct expression *expr)
248 struct symbol *sym;
249 struct symbol *base;
251 sym = alloc_symbol(expr->pos, SYM_PTR);
252 expr = expr->unop;
253 base = get_type(expr);
254 if (!base)
255 return NULL;
256 sym->ctype.base_type = base;
257 return sym;
260 static struct symbol *get_type_helper(struct expression *expr)
262 struct symbol *ret;
264 expr = strip_parens(expr);
265 if (!expr)
266 return NULL;
268 if (expr->ctype)
269 return expr->ctype;
271 switch (expr->type) {
272 case EXPR_STRING:
273 ret = &string_ctype;
274 break;
275 case EXPR_SYMBOL:
276 ret = get_type_symbol(expr);
277 break;
278 case EXPR_DEREF:
279 ret = get_symbol_from_deref(expr);
280 break;
281 case EXPR_PREOP:
282 case EXPR_POSTOP:
283 if (expr->op == '&')
284 ret = fake_pointer_sym(expr);
285 else if (expr->op == '*')
286 ret = get_pointer_type(expr->unop);
287 else
288 ret = get_type(expr->unop);
289 break;
290 case EXPR_ASSIGNMENT:
291 ret = get_type(expr->left);
292 break;
293 case EXPR_CAST:
294 case EXPR_FORCE_CAST:
295 case EXPR_IMPLIED_CAST:
296 ret = get_real_base_type(expr->cast_type);
297 break;
298 case EXPR_COMPARE:
299 case EXPR_BINOP:
300 ret = get_binop_type(expr);
301 break;
302 case EXPR_CALL:
303 ret = get_return_type(expr);
304 break;
305 case EXPR_STATEMENT:
306 ret = get_expr_stmt_type(expr->statement);
307 break;
308 case EXPR_CONDITIONAL:
309 case EXPR_SELECT:
310 ret = get_select_type(expr);
311 break;
312 case EXPR_SIZEOF:
313 ret = &ulong_ctype;
314 break;
315 case EXPR_LOGICAL:
316 ret = &int_ctype;
317 break;
318 case EXPR_OFFSETOF:
319 ret = &ulong_ctype;
320 break;
321 default:
322 return NULL;
325 if (ret && ret->type == SYM_TYPEOF)
326 ret = get_type(ret->initializer);
328 expr->ctype = ret;
329 return ret;
332 static struct symbol *get_final_type_helper(struct expression *expr)
335 * The problem is that I wrote a bunch of Smatch to think that
336 * you could do get_type() on an expression and it would give
337 * you what the comparison was type promoted to. This is wrong
338 * but fixing it is a big of work... Hence this horrible hack.
342 expr = strip_parens(expr);
343 if (!expr)
344 return NULL;
346 if (expr->type == EXPR_COMPARE)
347 return &int_ctype;
348 if (expr->type == EXPR_PREOP && expr->op == '!')
349 return &int_ctype;
351 return NULL;
354 struct symbol *get_type(struct expression *expr)
356 return get_type_helper(expr);
359 struct symbol *get_comparison_type(struct expression *expr)
362 * Eventually we will probably have to figure out how to make get_type()
363 * return &int_ctype so let's create a helper function to transition to.
365 return get_type_helper(expr);
368 struct symbol *get_final_type(struct expression *expr)
370 struct symbol *ret;
372 ret = get_final_type_helper(expr);
373 if (ret)
374 return ret;
375 return get_type_helper(expr);
378 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
380 struct symbol *ret = &int_ctype;
382 if (type_positive_bits(left) > type_positive_bits(ret))
383 ret = left;
384 if (type_positive_bits(right) > type_positive_bits(ret))
385 ret = right;
387 if (type_is_ptr(left))
388 ret = left;
389 if (type_is_ptr(right))
390 ret = right;
392 return ret;
395 int type_signed(struct symbol *base_type)
397 if (!base_type)
398 return 0;
399 if (base_type->ctype.modifiers & MOD_SIGNED)
400 return 1;
401 return 0;
404 int expr_unsigned(struct expression *expr)
406 struct symbol *sym;
408 sym = get_type(expr);
409 if (!sym)
410 return 0;
411 if (type_unsigned(sym))
412 return 1;
413 return 0;
416 int expr_signed(struct expression *expr)
418 struct symbol *sym;
420 sym = get_type(expr);
421 if (!sym)
422 return 0;
423 if (type_signed(sym))
424 return 1;
425 return 0;
428 int returns_unsigned(struct symbol *sym)
430 if (!sym)
431 return 0;
432 sym = get_base_type(sym);
433 if (!sym || sym->type != SYM_FN)
434 return 0;
435 sym = get_base_type(sym);
436 return type_unsigned(sym);
439 int is_pointer(struct expression *expr)
441 return type_is_ptr(get_final_type(expr));
444 int returns_pointer(struct symbol *sym)
446 if (!sym)
447 return 0;
448 sym = get_base_type(sym);
449 if (!sym || sym->type != SYM_FN)
450 return 0;
451 sym = get_base_type(sym);
452 if (sym && sym->type == SYM_PTR)
453 return 1;
454 return 0;
457 static sval_t fp_max(struct symbol *type)
459 sval_t ret = { .type = type };
461 if (type == &float_ctype)
462 ret.fvalue = FLT_MAX;
463 else if (type == &double_ctype)
464 ret.dvalue = DBL_MAX;
465 else
466 ret.ldvalue = LDBL_MAX;
468 return ret;
471 sval_t sval_type_max(struct symbol *base_type)
473 sval_t ret;
475 if (type_is_fp(base_type))
476 return fp_max(base_type);
478 if (!base_type || !type_bits(base_type))
479 base_type = &llong_ctype;
480 ret.type = base_type;
482 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
483 return ret;
486 static sval_t fp_min(struct symbol *type)
488 sval_t ret = { .type = type };
490 if (type == &float_ctype)
491 ret.fvalue = -FLT_MAX;
492 else if (type == &double_ctype)
493 ret.dvalue = -DBL_MAX;
494 else
495 ret.ldvalue = -LDBL_MAX;
497 return ret;
500 sval_t sval_type_min(struct symbol *base_type)
502 sval_t ret;
504 if (type_is_fp(base_type))
505 return fp_min(base_type);
507 if (!base_type || !type_bits(base_type))
508 base_type = &llong_ctype;
509 ret.type = base_type;
511 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
512 ret.value = 0;
513 return ret;
516 ret.value = (~0ULL) << type_positive_bits(base_type);
518 return ret;
521 int nr_bits(struct expression *expr)
523 struct symbol *type;
525 type = get_type(expr);
526 if (!type)
527 return 0;
528 return type_bits(type);
531 int is_void_pointer(struct expression *expr)
533 struct symbol *type;
535 type = get_type(expr);
536 if (!type || type->type != SYM_PTR)
537 return 0;
538 type = get_real_base_type(type);
539 if (type == &void_ctype)
540 return 1;
541 return 0;
544 int is_char_pointer(struct expression *expr)
546 struct symbol *type;
548 type = get_type(expr);
549 if (!type || type->type != SYM_PTR)
550 return 0;
551 type = get_real_base_type(type);
552 if (type == &char_ctype)
553 return 1;
554 return 0;
557 int is_string(struct expression *expr)
559 expr = strip_expr(expr);
560 if (!expr || expr->type != EXPR_STRING)
561 return 0;
562 if (expr->string)
563 return 1;
564 return 0;
567 bool is_struct_ptr(struct symbol *type)
569 if (!type || type->type != SYM_PTR)
570 return false;
571 type = get_real_base_type(type);
572 if (!type || type->type != SYM_STRUCT)
573 return false;
574 return true;
577 int is_static(struct expression *expr)
579 char *name;
580 struct symbol *sym;
581 int ret = 0;
583 name = expr_to_str_sym(expr, &sym);
584 if (!name || !sym)
585 goto free;
587 if (sym->ctype.modifiers & MOD_STATIC)
588 ret = 1;
589 free:
590 free_string(name);
591 return ret;
594 static struct expression *get_symbol_expr(struct expression *expr)
596 if (!expr)
597 return NULL;
598 while (expr && expr->type == EXPR_DEREF && expr->op == '.')
599 expr = strip_expr(expr->deref);
600 return expr;
603 bool is_local_variable(struct expression *expr)
605 struct symbol *sym;
607 expr = get_symbol_expr(expr);
608 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
609 return false;
610 sym = expr->symbol;
611 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
612 return true;
613 return false;
616 int types_equiv(struct symbol *one, struct symbol *two)
618 if (!one && !two)
619 return 1;
620 if (!one || !two)
621 return 0;
622 if (one->type != two->type)
623 return 0;
624 if (one->type == SYM_PTR)
625 return types_equiv(get_real_base_type(one), get_real_base_type(two));
626 if (type_positive_bits(one) != type_positive_bits(two))
627 return 0;
628 return 1;
631 bool type_fits(struct symbol *type, struct symbol *test)
633 if (!type || !test)
634 return false;
636 if (type == test)
637 return true;
639 if (type_bits(test) > type_bits(type))
640 return false;
641 if (type_signed(test) && !type_signed(type))
642 return false;
643 if (type_positive_bits(test) > type_positive_bits(type))
644 return false;
645 return true;
648 int fn_static(void)
650 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
653 const char *global_static(void)
655 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
656 return "static";
657 else
658 return "global";
661 struct symbol *cur_func_return_type(void)
663 struct symbol *sym;
665 sym = get_real_base_type(cur_func_sym);
666 if (!sym || sym->type != SYM_FN)
667 return NULL;
668 sym = get_real_base_type(sym);
669 return sym;
672 struct symbol *get_arg_type(struct expression *fn, int arg)
674 struct symbol *fn_type;
675 struct symbol *tmp;
676 struct symbol *arg_type;
677 int i;
679 fn_type = get_type(fn);
680 if (!fn_type)
681 return NULL;
682 if (fn_type->type == SYM_PTR)
683 fn_type = get_real_base_type(fn_type);
684 if (fn_type->type != SYM_FN)
685 return NULL;
687 i = 0;
688 FOR_EACH_PTR(fn_type->arguments, tmp) {
689 arg_type = get_real_base_type(tmp);
690 if (i == arg) {
691 return arg_type;
693 i++;
694 } END_FOR_EACH_PTR(tmp);
696 return NULL;
699 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
701 struct symbol *tmp, *sub;
702 int chunk_len;
704 if (strncmp(name, ".", 1) == 0)
705 name += 1;
706 else if (strncmp(name, "->", 2) == 0)
707 name += 2;
709 FOR_EACH_PTR(symbol_list, tmp) {
710 if (!tmp->ident) {
711 sub = get_real_base_type(tmp);
712 sub = get_member_from_string(sub->symbol_list, name);
713 if (sub)
714 return sub;
715 continue;
718 if (strcmp(tmp->ident->name, name) == 0)
719 return tmp;
721 chunk_len = tmp->ident->len;
722 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
723 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
724 sub = get_real_base_type(tmp);
725 if (sub->type == SYM_PTR)
726 sub = get_real_base_type(sub);
727 return get_member_from_string(sub->symbol_list, name + chunk_len);
730 } END_FOR_EACH_PTR(tmp);
732 return NULL;
735 static struct symbol *get_type_from_container_of_key(struct expression *expr, const char *key)
737 char *new_key;
739 expr = map_container_of_to_simpler_expr_key(expr, key, &new_key);
740 if (!expr)
741 return NULL;
742 return get_member_type_from_key(expr, new_key);
745 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
747 struct symbol *sym;
748 int star = 0;
749 int i;
751 if (strcmp(key, "$") == 0)
752 return get_type(expr);
754 if (strcmp(key, "*$") == 0) {
755 sym = get_type(expr);
756 if (!sym)
757 return NULL;
758 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
759 return NULL;
760 return get_real_base_type(sym);
763 if (strstr(key, "<~$"))
764 return get_type_from_container_of_key(expr, key);
766 sym = get_type(expr);
767 if (!sym)
768 return NULL;
769 if (sym->type == SYM_PTR)
770 sym = get_real_base_type(sym);
772 while (*key == '*') {
773 key++;
774 star++;
777 if (*key != '$')
778 return NULL;
779 key++;
781 sym = get_member_from_string(sym->symbol_list, key);
782 if (!sym)
783 return NULL;
784 if (sym->type == SYM_RESTRICT || sym->type == SYM_NODE)
785 sym = get_real_base_type(sym);
786 for (i = 0; i < star; i++) {
787 if (!sym || sym->type != SYM_PTR)
788 return NULL;
789 sym = get_real_base_type(sym);
791 return sym;
794 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
796 struct symbol *type;
798 if (!key)
799 return NULL;
800 if (strcmp(key, "$") == 0)
801 return get_arg_type(fn, param);
802 if (strcmp(key, "*$") == 0) {
803 type = get_arg_type(fn, param);
804 if (!type || type->type != SYM_PTR)
805 return NULL;
806 return get_real_base_type(type);
808 return get_member_type_from_key(arg, key);
811 int is_struct(struct expression *expr)
813 struct symbol *type;
815 type = get_type(expr);
816 if (type && type->type == SYM_STRUCT)
817 return 1;
818 return 0;
821 static struct {
822 struct symbol *sym;
823 const char *name;
824 } base_types[] = {
825 {&bool_ctype, "bool"},
826 {&void_ctype, "void"},
827 {&type_ctype, "type"},
828 {&char_ctype, "char"},
829 {&schar_ctype, "schar"},
830 {&uchar_ctype, "uchar"},
831 {&short_ctype, "short"},
832 {&sshort_ctype, "sshort"},
833 {&ushort_ctype, "ushort"},
834 {&int_ctype, "int"},
835 {&sint_ctype, "sint"},
836 {&uint_ctype, "uint"},
837 {&long_ctype, "long"},
838 {&slong_ctype, "slong"},
839 {&ulong_ctype, "ulong"},
840 {&llong_ctype, "llong"},
841 {&sllong_ctype, "sllong"},
842 {&ullong_ctype, "ullong"},
843 {&int128_ctype, "lllong"},
844 {&sint128_ctype, "slllong"},
845 {&uint128_ctype, "ulllong"},
846 {&float_ctype, "float"},
847 {&double_ctype, "double"},
848 {&ldouble_ctype, "ldouble"},
849 {&string_ctype, "string"},
850 {&ptr_ctype, "ptr"},
851 {&lazy_ptr_ctype, "lazy_ptr"},
852 {&incomplete_ctype, "incomplete"},
853 {&label_ctype, "label"},
854 {&bad_ctype, "bad"},
855 {&null_ctype, "null"},
858 static const char *base_type_str(struct symbol *sym)
860 int i;
862 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
863 if (sym == base_types[i].sym)
864 return base_types[i].name;
866 return "<unknown>";
869 static int type_str_helper(char *buf, int size, struct symbol *type)
871 int n;
873 if (!type)
874 return snprintf(buf, size, "<null type>");
876 if (type->type == SYM_BASETYPE) {
877 return snprintf(buf, size, "%s", base_type_str(type));
878 } else if (type->type == SYM_PTR) {
879 type = get_real_base_type(type);
880 n = type_str_helper(buf, size, type);
881 if (n > size)
882 return n;
883 return n + snprintf(buf + n, size - n, "*");
884 } else if (type->type == SYM_ARRAY) {
885 type = get_real_base_type(type);
886 n = type_str_helper(buf, size, type);
887 if (n > size)
888 return n;
889 return n + snprintf(buf + n, size - n, "[]");
890 } else if (type->type == SYM_STRUCT) {
891 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
892 } else if (type->type == SYM_UNION) {
893 if (type->ident)
894 return snprintf(buf, size, "union %s", type->ident->name);
895 else
896 return snprintf(buf, size, "anonymous union");
897 } else if (type->type == SYM_FN) {
898 struct symbol *arg, *return_type, *arg_type;
899 int i;
901 return_type = get_real_base_type(type);
902 n = type_str_helper(buf, size, return_type);
903 if (n > size)
904 return n;
905 n += snprintf(buf + n, size - n, "(*)(");
906 if (n > size)
907 return n;
909 i = 0;
910 FOR_EACH_PTR(type->arguments, arg) {
911 if (i++)
912 n += snprintf(buf + n, size - n, ", ");
913 if (n > size)
914 return n;
915 arg_type = get_real_base_type(arg);
916 n += type_str_helper(buf + n, size - n, arg_type);
917 if (n > size)
918 return n;
919 } END_FOR_EACH_PTR(arg);
921 return n + snprintf(buf + n, size - n, ")");
922 } else if (type->type == SYM_NODE) {
923 n = snprintf(buf, size, "node {");
924 if (n > size)
925 return n;
926 type = get_real_base_type(type);
927 n += type_str_helper(buf + n, size - n, type);
928 if (n > size)
929 return n;
930 return n + snprintf(buf + n, size - n, "}");
931 } else if (type->type == SYM_ENUM) {
932 return snprintf(buf, size, "enum %s", type->ident ? type->ident->name : "<unknown>");
933 } else {
934 return snprintf(buf, size, "<type %d>", type->type);
938 char *type_to_str(struct symbol *type)
940 static char buf[256];
942 buf[0] = '\0';
943 type_str_helper(buf, sizeof(buf), type);
944 return alloc_sname(buf);