2021-05-30 07:14:16 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
#include "XGetopt.h"
|
2021-09-03 07:04:26 +08:00
|
|
|
#else
|
|
|
|
#include <getopt.h>
|
2021-05-30 07:14:16 +08:00
|
|
|
#endif
|
|
|
|
|
2021-09-03 07:04:26 +08:00
|
|
|
#include "netcdf.h"
|
2021-05-30 07:14:16 +08:00
|
|
|
#include "ncpathmgr.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Synopsis
|
|
|
|
|
2021-06-06 04:12:21 +08:00
|
|
|
pathcvt [-u|-w|-m|-c] [-e] [-d <driveletter>] PATH
|
2021-05-30 07:14:16 +08:00
|
|
|
|
|
|
|
Options
|
2021-05-31 06:12:40 +08:00
|
|
|
-e add backslash escapes to '\' and ' '
|
2021-06-06 04:12:21 +08:00
|
|
|
-d <driveletter> use driveletter when needed; defaults to 'c'
|
2021-05-30 07:14:16 +08:00
|
|
|
Output type options:
|
2021-05-30 10:51:09 +08:00
|
|
|
-u convert to Unix form of path
|
2021-05-30 07:14:16 +08:00
|
|
|
-w convert to Windows form of path
|
|
|
|
-m convert to MSYS form of path
|
|
|
|
-c convert to Cygwin form of path
|
2021-05-31 06:12:40 +08:00
|
|
|
|
2021-05-30 10:51:09 +08:00
|
|
|
Default is to convert to the format used by the platform.
|
|
|
|
|
2021-05-30 07:14:16 +08:00
|
|
|
*/
|
|
|
|
|
2021-09-03 07:04:26 +08:00
|
|
|
#undef DEBUG
|
2021-05-30 07:14:16 +08:00
|
|
|
|
|
|
|
struct Options {
|
|
|
|
int target;
|
2021-06-06 04:12:21 +08:00
|
|
|
int escapes;
|
|
|
|
int drive;
|
2021-05-30 07:14:16 +08:00
|
|
|
int debug;
|
|
|
|
} cvtoptions;
|
|
|
|
|
2021-05-31 06:12:40 +08:00
|
|
|
static char* escape(const char* path);
|
|
|
|
static void usage(const char* msg);
|
2021-05-30 07:14:16 +08:00
|
|
|
|
2021-09-03 07:04:26 +08:00
|
|
|
static void
|
|
|
|
usage(const char* msg)
|
|
|
|
{
|
|
|
|
if(msg != NULL) fprintf(stderr,"%s\n",msg);
|
|
|
|
fprintf(stderr,"pathcvt [-u|-w|-m|-c] PATH\n");
|
|
|
|
if(msg == NULL) exit(0); else exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
escape(const char* path)
|
|
|
|
{
|
|
|
|
size_t slen = strlen(path);
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
char* epath = NULL;
|
|
|
|
const char* escapes = " \\";
|
|
|
|
|
|
|
|
epath = (char*)malloc((2*slen) + 1);
|
|
|
|
if(epath == NULL) usage("out of memtory");
|
|
|
|
p = path;
|
|
|
|
q = epath;
|
|
|
|
for(;*p;p++) {
|
|
|
|
if(strchr(escapes,*p) != NULL)
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return epath;
|
|
|
|
}
|
|
|
|
|
2021-05-30 07:14:16 +08:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char* cvtpath = NULL;
|
2021-09-03 07:04:26 +08:00
|
|
|
char* inpath, *canon;
|
2021-05-30 07:14:16 +08:00
|
|
|
|
|
|
|
memset((void*)&cvtoptions,0,sizeof(cvtoptions));
|
2021-06-06 04:12:21 +08:00
|
|
|
cvtoptions.drive = 'c';
|
2021-05-30 07:14:16 +08:00
|
|
|
|
2021-06-06 04:12:21 +08:00
|
|
|
while ((c = getopt(argc, argv, "cD:d:ehmuw")) != EOF) {
|
2021-05-30 07:14:16 +08:00
|
|
|
switch(c) {
|
|
|
|
case 'c': cvtoptions.target = NCPD_CYGWIN; break;
|
2021-06-06 04:12:21 +08:00
|
|
|
case 'd': cvtoptions.drive = optarg[0]; break;
|
|
|
|
case 'e': cvtoptions.escapes = 1; break;
|
2021-05-30 07:14:16 +08:00
|
|
|
case 'h': usage(NULL); break;
|
|
|
|
case 'm': cvtoptions.target = NCPD_MSYS; break;
|
|
|
|
case 'u': cvtoptions.target = NCPD_NIX; break;
|
|
|
|
case 'w': cvtoptions.target = NCPD_WIN; break;
|
|
|
|
case 'D':
|
|
|
|
sscanf(optarg,"%d",&cvtoptions.debug);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
usage("unknown option");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* If no file arguments left or more than one, print usage message. */
|
|
|
|
if (argc == 0)
|
|
|
|
usage("no path specified");
|
|
|
|
if (argc > 1)
|
|
|
|
usage("more than one path specified");
|
|
|
|
inpath = argv[0];
|
2021-09-03 07:04:26 +08:00
|
|
|
|
|
|
|
/* Canonicalize */
|
|
|
|
if(NCpathcanonical(inpath,&canon))
|
|
|
|
usage("Could not convert to canonical form");
|
|
|
|
|
2021-05-30 10:51:09 +08:00
|
|
|
if(cvtoptions.target == NCPD_UNKNOWN)
|
2021-09-03 07:04:26 +08:00
|
|
|
cvtpath = NCpathcvt(canon);
|
2021-05-30 10:51:09 +08:00
|
|
|
else
|
2021-09-03 07:04:26 +08:00
|
|
|
cvtpath = NCpathcvt_test(canon,cvtoptions.target,(char)cvtoptions.drive);
|
2021-06-06 04:12:21 +08:00
|
|
|
if(cvtpath && cvtoptions.escapes) {
|
2021-05-31 06:12:40 +08:00
|
|
|
char* path = cvtpath; cvtpath = NULL;
|
|
|
|
cvtpath = escape(path);
|
|
|
|
free(path);
|
|
|
|
}
|
2021-05-30 07:14:16 +08:00
|
|
|
printf("%s",cvtpath);
|
2021-09-03 07:04:26 +08:00
|
|
|
if(canon) free(canon);
|
2021-05-30 07:14:16 +08:00
|
|
|
if(cvtpath) free(cvtpath);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-31 06:12:40 +08:00
|
|
|
|