BRL-CAD
bu_dirname.c
Go to the documentation of this file.
1 /* T E S T _ D I R N A M E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_LIBGEN_H /* for dirname */
27 # include <libgen.h>
28 #endif
29 
30 #include "bu.h"
31 
32 
33 /* Test against basename UNIX tool */
34 int
35 automatic_test(const char *input)
36 {
37 #ifdef HAVE_DIRNAME
38  char *ans = NULL;
39  char buf_input[1000];
40  char dirname_buf_input[1000];
41  char *res = NULL;
42  int pass = 0;
43 
44  if (input) {
45  bu_strlcpy(buf_input, input, strlen(input)+1);
46  bu_strlcpy(dirname_buf_input, input, strlen(input)+1);
47  }
48 
49  /* build UNIX 'dirname' command */
50  if (!input)
51  ans = dirname(NULL);
52  else
53  ans = dirname(dirname_buf_input);
54 
55  if (!input)
56  res = bu_dirname(NULL);
57  else
58  res = bu_dirname(buf_input);
59 
60  if (BU_STR_EQUAL(res, ans)) {
61  printf("%24s -> %24s [PASSED]\n", input, res);
62  pass = 1;
63  } else {
64  printf("%24s -> %24s (should be: %s) [FAIL]\n", input, res, ans);
65  }
66 
67  bu_free(res, NULL);
68  return pass;
69 #else
70  printf("%s untested - dirname not implemented on this platform\n", input);
71  return 1;
72 #endif
73 }
74 
75 
76 int
77 main(int argc, char *argv[])
78 {
79  if (argc == 1)
80  return !automatic_test(NULL);
81 
82  if (argc > 2)
83  fprintf(stderr,"Usage: %s test_string\n", argv[0]);
84 
85  return !automatic_test(argv[1]);
86 }
87 
88 
89 /*
90  * Local Variables:
91  * mode: C
92  * tab-width: 8
93  * indent-tabs-mode: t
94  * c-file-style: "stroustrup"
95  * End:
96  * ex: shiftwidth=4 tabstop=8
97  */
int main(int argc, char *argv[])
Definition: bu_dirname.c:77
Header file for the BRL-CAD common definitions.
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
int automatic_test(const char *input)
Definition: bu_dirname.c:35
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
char * bu_dirname(const char *path)
Definition: dirname.c:30
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126