--- path_test.c 2002/08/03 10:23:41 1.2
+++ path_test.c 2002/08/23 15:33:18 1.3
@@ -37,33 +37,108 @@
#include "path.h"
+static void usage(const char *argv0, const char *error)
+{
+ if (error != NULL)
+ fprintf(stderr, "error: %s\n", error);
+ fprintf(stderr, "usage: %s abs2rel <path> [<base>]\n", argv0);
+ fprintf(stderr, "usage: %s rel2abs <path> [<base>]\n", argv0);
+ fprintf(stderr, "usage: %s resolve <path>\n", argv0);
+ fprintf(stderr, "usage: %s dirname <path>\n", argv0);
+ fprintf(stderr, "usage: %s basename <path>\n", argv0);
+ fprintf(stderr, "usage: %s temp dir|file [<template>]\n", argv0);
+ fprintf(stderr, "usage: %s self\n", argv0);
+ if (error != NULL)
+ exit(1);
+ else
+ exit(0);
+}
+
int main(int argc, char *argv[])
{
- char result[MAXPATHLEN];
- char cwd[MAXPATHLEN];
+ char res[MAXPATHLEN];
+ char cwd[MAXPATHLEN];
char *rv;
- if (argc < 3) {
- fprintf(stderr, "usage: test abs2rel|rel2abs path [base]\n");
- exit(1);
- }
- if (argc == 3) {
- if (getcwd(cwd, MAXPATHLEN) == NULL) {
- fprintf(stderr, "cannot get current directory.\n");
- exit(1);
- }
- } else
- strcpy(cwd, argv[3]);
-
- if (strcmp(argv[1], "abs2rel") == 0)
- rv = path_abs2rel(result, MAXPATHLEN, argv[2], cwd);
- else
- rv = path_rel2abs(result, MAXPATHLEN, argv[2], cwd);
-
- if (rv != NULL)
- printf("%s\n", result);
- else
- printf("ERROR\n");
- exit(0);
+ if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "abs2rel") == 0) {
+ if (argc == 3) {
+ if (getcwd(cwd, MAXPATHLEN) == NULL) {
+ fprintf(stderr, "error: cannot get current directory\n");
+ exit(1);
+ }
+ } else
+ strcpy(cwd, argv[3]);
+ rv = path_abs2rel(res, sizeof(res), argv[2], cwd);
+ if (rv != NULL)
+ printf("%s\n", res);
+ else
+ printf("ERROR\n");
+ }
+ else if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "rel2abs") == 0) {
+ if (argc == 3) {
+ if (getcwd(cwd, MAXPATHLEN) == NULL) {
+ fprintf(stderr, "error: cannot get current directory\n");
+ exit(1);
+ }
+ } else
+ strcpy(cwd, argv[3]);
+ rv = path_rel2abs(res, sizeof(res), argv[2], cwd);
+ if (rv != NULL)
+ printf("%s\n", res);
+ else
+ printf("ERROR\n");
+ }
+ else if (argc == 3 && strcmp(argv[1], "resolve") == 0) {
+ rv = path_resolve(res, sizeof(res), argv[2]);
+ if (rv != NULL)
+ printf("%s\n", res);
+ else
+ printf("ERROR\n");
+ }
+ else if (argc == 3 && strcmp(argv[1], "dirname") == 0) {
+ rv = path_dirname(res, sizeof(res), argv[2]);
+ if (rv != NULL)
+ printf("%s\n", res);
+ else
+ printf("ERROR\n");
+ }
+ else if (argc == 3 && strcmp(argv[1], "basename") == 0) {
+ rv = path_basename(res, sizeof(res), argv[2]);
+ if (rv != NULL)
+ printf("%s\n", res);
+ else
+ printf("ERROR\n");
+ }
+ else if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "temp") == 0) {
+ path_temp_t id = 0;
+ char *tmpl;
+ char *result;
+ size_t result_len;
+ if (strcmp(argv[2], "dir") == 0)
+ id = PATH_TEMP_DIR;
+ else if (strcmp(argv[2], "file") == 0)
+ id = PATH_TEMP_FILE;
+ else
+ usage(argv[0], "invalid temp id");
+ tmpl = NULL;
+ if (argc == 4)
+ tmpl = argv[3];
+ result = res;
+ if (path_temp(id, tmpl, &result, &result_len, NULL) != PATH_OK)
+ fprintf(stderr, "ERROR\n");
+ else {
+ fprintf(stderr, "%s\n", res);
+ unlink(res);
+ }
+ }
+ else if (argc == 2 && strcmp(argv[1], "self") == 0) {
+ if (path_self(res, sizeof(res), argv[0]) != PATH_OK)
+ fprintf(stderr, "ERROR\n");
+ else
+ fprintf(stderr, "%s\n", res);
+ }
+ else
+ usage(argv[0], "invalid usage");
+ exit(0);
}
|