|
| 1 | +/* |
| 2 | + * Copyright 2018, University Corporation for Atmospheric Research |
| 3 | + * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
| 4 | + */ |
| 5 | + |
| 6 | +#ifdef HAVE_CONFIG_H |
| 7 | +#include "config.h" |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <stdlib.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +#ifdef HAVE_UNISTD_H |
| 15 | +#include <unistd.h> |
| 16 | +#endif |
| 17 | +#ifdef HAVE_GETOPT_H |
| 18 | +#include <getopt.h> |
| 19 | +#endif |
| 20 | + |
| 21 | +#if defined(_WIN32) && !defined(__MINGW32__) |
| 22 | +#include "XGetopt.h" |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "ncpathmgr.h" |
| 26 | + |
| 27 | +/* |
| 28 | +Synopsis |
| 29 | +
|
| 30 | +pathcvt [-u|-w|-m|-c] [-e] PATH |
| 31 | +
|
| 32 | +Options |
| 33 | + -e add backslash escapes to '\' and ' ' |
| 34 | +Output type options: |
| 35 | + -u convert to Unix form of path |
| 36 | + -w convert to Windows form of path |
| 37 | + -m convert to MSYS form of path |
| 38 | + -c convert to Cygwin form of path |
| 39 | + |
| 40 | +Default is to convert to the format used by the platform. |
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +#define DEBUG |
| 45 | + |
| 46 | +struct Options { |
| 47 | + int target; |
| 48 | + int escape; |
| 49 | + int debug; |
| 50 | +} cvtoptions; |
| 51 | + |
| 52 | +static char* escape(const char* path); |
| 53 | +static void usage(const char* msg); |
| 54 | + |
| 55 | +int |
| 56 | +main(int argc, char** argv) |
| 57 | +{ |
| 58 | + int c; |
| 59 | + char* cvtpath = NULL; |
| 60 | + char* inpath; |
| 61 | + |
| 62 | + memset((void*)&cvtoptions,0,sizeof(cvtoptions)); |
| 63 | + |
| 64 | + while ((c = getopt(argc, argv, "cD:ehmuw")) != EOF) { |
| 65 | + switch(c) { |
| 66 | + case 'c': cvtoptions.target = NCPD_CYGWIN; break; |
| 67 | + case 'e': cvtoptions.escape = 1; break; |
| 68 | + case 'h': usage(NULL); break; |
| 69 | + case 'm': cvtoptions.target = NCPD_MSYS; break; |
| 70 | + case 'u': cvtoptions.target = NCPD_NIX; break; |
| 71 | + case 'w': cvtoptions.target = NCPD_WIN; break; |
| 72 | + case 'D': |
| 73 | + sscanf(optarg,"%d",&cvtoptions.debug); |
| 74 | + break; |
| 75 | + case '?': |
| 76 | + usage("unknown option"); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + argc -= optind; |
| 82 | + argv += optind; |
| 83 | + |
| 84 | + /* If no file arguments left or more than one, print usage message. */ |
| 85 | + if (argc == 0) |
| 86 | + usage("no path specified"); |
| 87 | + if (argc > 1) |
| 88 | + usage("more than one path specified"); |
| 89 | + inpath = argv[0]; |
| 90 | + if(cvtoptions.target == NCPD_UNKNOWN) |
| 91 | + cvtpath = NCpathcvt(inpath); |
| 92 | + else |
| 93 | + cvtpath = NCpathcvt_test(inpath,cvtoptions.target,'c'); |
| 94 | + if(cvtpath && cvtoptions.escape) { |
| 95 | + char* path = cvtpath; cvtpath = NULL; |
| 96 | + cvtpath = escape(path); |
| 97 | + free(path); |
| 98 | + } |
| 99 | + printf("%s",cvtpath); |
| 100 | + if(cvtpath) free(cvtpath); |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +static void |
| 105 | +usage(const char* msg) |
| 106 | +{ |
| 107 | + if(msg != NULL) fprintf(stderr,"%s\n",msg); |
| 108 | + fprintf(stderr,"pathcvt [-u|-w|-m|-c] PATH\n"); |
| 109 | + if(msg == NULL) exit(0); else exit(1); |
| 110 | +} |
| 111 | + |
| 112 | +static char* |
| 113 | +escape(const char* path) |
| 114 | +{ |
| 115 | + size_t slen = strlen(path); |
| 116 | + const char* p; |
| 117 | + char* q; |
| 118 | + char* epath = NULL; |
| 119 | + |
| 120 | + epath = (char*)malloc((2*slen) + 1); |
| 121 | + if(epath == NULL) usage("out of memtory"); |
| 122 | + p = path; |
| 123 | + q = epath; |
| 124 | + for(;*p;p++) { |
| 125 | + switch (*p) { |
| 126 | + case '\\': case ' ': |
| 127 | + *q++ = '\\'; |
| 128 | + /* fall thru */ |
| 129 | + default: |
| 130 | + *q++ = *p; |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + *q = '\0'; |
| 135 | + return epath; |
| 136 | +} |
| 137 | + |
0 commit comments