Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygpath.cc « utils « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81cd69ee613c47ecb8c765b4a6b0f5d33fbc831b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* cygpath.cc -- convert pathnames between Windows and Unix format
   Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#define NOCOMATTRIBUTE

#include <shlobj.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <getopt.h>
#include <windows.h>
#include <io.h>
#include <sys/fcntl.h>
#include <sys/cygwin.h>
#include <ctype.h>

static char *prog_name;
static char *file_arg;
static char *close_arg;
static int path_flag, unix_flag, windows_flag, absolute_flag;
static int shortname_flag, ignore_flag, allusers_flag, output_flag;

static struct option long_options[] =
{
  { (char *) "help", no_argument, NULL, 'h' },
  { (char *) "absolute", no_argument, NULL, 'a'},
  { (char *) "option", no_argument, NULL, 'o'},
  { (char *) "path", no_argument, NULL, 'p' },
  { (char *) "close", required_argument, (int *) &close_arg, 'c'},
  { (char *) "unix", no_argument, NULL, 'u' },
  { (char *) "file", required_argument, (int *) &file_arg, 'f'},
  { (char *) "version", no_argument, NULL, 'v' },
  { (char *) "windows", no_argument, NULL, 'w' },
  { (char *) "short-name", no_argument, NULL, 's' },
  { (char *) "windir", no_argument, NULL, 'W' },
  { (char *) "sysdir", no_argument, NULL, 'S' },
  { (char *) "ignore", no_argument, NULL, 'i' },
  { (char *) "allusers", no_argument, NULL, 'A' },
  { (char *) "desktop", no_argument, NULL, 'D' },
  { (char *) "smprograms", no_argument, NULL, 'P' },
  { 0, no_argument, 0, 0 }
};

static void
usage (FILE *stream, int status)
{
  if (!ignore_flag || !status)
    fprintf (stream, "\
Usage: %s [-p|--path] (-u|--unix)|(-w|--windows [-s|--short-name]) filename\n\
  -a|--absolute		output absolute path\n\
  -c|--close handle	close handle (for use in captured process)\n\
  -f|--file file	read file for input path information\n\
  -i|--ignore		ignore missing argument\n\
  -p|--path		filename argument is a path\n\
  -s|--short-name	print Windows short form of filename\n\
  -u|--unix		print Unix form of filename\n\
  -v|--version		output version information and exit\n\
  -w|--windows		print Windows form of filename\n\
  -A|--allusers		use `All Users' instead of current user for -D, -P\n\
  -D|--desktop		output `Desktop' directory and exit\n\
  -P|--smprograms	output Start Menu `Programs' directory and exit\n\
  -S|--sysdir		output system directory and exit\n\
  -W|--windir		output `Windows' directory and exit\n",
	   prog_name);
  exit (ignore_flag ? 0 : status);
}

static char *
get_short_paths (char *path)
{
  char *sbuf;
  char *sptr;
  char *next;
  char *ptr = path;
  char *end = strrchr (path, 0);
  DWORD acc = 0;
  DWORD len;

  while (ptr != NULL)
  {
    next = ptr;
    ptr = strchr (ptr, ';');
    if (ptr)
      *ptr++ = 0;
    len = GetShortPathName (next, NULL, 0);
    if (len == ERROR_INVALID_PARAMETER)
    {
      fprintf (stderr, "%s: cannot create short name of %s\n", prog_name, next);
      exit (2);
    }
    acc += len+1;
  }
  sptr = sbuf = (char *) malloc(acc+1);
  if (sbuf == NULL)
  {
    fprintf (stderr, "%s: out of memory\n", prog_name);
    exit (1);
  }
  ptr = path;
  for(;;)
  {
    if (GetShortPathName (ptr, sptr, acc) == ERROR_INVALID_PARAMETER)
    {
      fprintf (stderr, "%s: cannot create short name of %s\n", prog_name, ptr);
      exit (2);
    }

    ptr = strrchr (ptr, 0);
    sptr = strrchr (sptr, 0);
    if (ptr == end)
      break;
    *sptr = ';';
    ++ptr, ++sptr;
  }
  return sbuf;
}

static char *
get_short_name (const char *filename)
{
  char *sbuf;
  DWORD len = GetShortPathName (filename, NULL, 0);
  if (len == ERROR_INVALID_PARAMETER)
  {
    fprintf (stderr, "%s: cannot create short name of %s\n", prog_name, filename);
    exit (2);
  }
  sbuf = (char *) malloc(++len);
  if (sbuf == NULL)
  {
    fprintf (stderr, "%s: out of memory\n", prog_name);
    exit (1);
  }
  if (GetShortPathName (filename, sbuf, len) == ERROR_INVALID_PARAMETER)
  {
    fprintf (stderr, "%s: cannot create short name of %s\n", prog_name, filename);
    exit (2);
  }
  return sbuf;
}

static void
doit (char *filename)
{
  char *buf;
  size_t len;
  int retval;
  int (*conv_func)(const char *, char *);

  if (path_flag)
    {
      if (cygwin_posix_path_list_p (filename)
	  ? unix_flag
	  : windows_flag)
	{
	  /* The path is already in the right format.  */
	  puts (filename);
	  exit (0);
	}
    }

  if (! path_flag)
    {
      len = strlen (filename) + 100;
      if (len == 100)
        {
          if (!ignore_flag)
            {
              fprintf(stderr, "%s: can't convert empty path\n", prog_name);
              exit (1);
            }
          else
            exit (0);
        }
    }
  else
    {
      if (unix_flag)
	len = cygwin_win32_to_posix_path_list_buf_size (filename);
      else
	len = cygwin_posix_to_win32_path_list_buf_size (filename);
    }

  if (len < PATH_MAX)
    len = PATH_MAX;

  buf = (char *) malloc (len);
  if (buf == NULL)
    {
      fprintf (stderr, "%s: out of memory\n", prog_name);
      exit (1);
    }

  if (path_flag)
    {
      if (unix_flag)
	cygwin_win32_to_posix_path_list (filename, buf);
      else
      {
	cygwin_posix_to_win32_path_list (filename, buf);
	if (shortname_flag)
	  buf = get_short_paths (buf);
      }
    }
  else
    {
      if (unix_flag)
	conv_func = (absolute_flag ? cygwin_conv_to_full_posix_path : 
                     cygwin_conv_to_posix_path);
      else
        conv_func = (absolute_flag ? cygwin_conv_to_full_win32_path :
                     cygwin_conv_to_win32_path);
      retval = conv_func (filename, buf);
      if (retval < 0)
        {
          fprintf (stderr, "%s: error converting \"%s\"\n",
                   prog_name, filename);
          exit (1);
        }
      if (!unix_flag && shortname_flag)
        buf = get_short_name (buf);
    }

  puts (buf);
}

int
main (int argc, char **argv)
{
  int c, o;
  int options_from_file_flag;
  char *filename;
  char buf[MAX_PATH], buf2[MAX_PATH];
  WIN32_FIND_DATA w32_fd;
  LPITEMIDLIST id;

  prog_name = strrchr (argv[0], '/');
  if (prog_name == NULL)
    prog_name = strrchr (argv[0], '\\');
  if (prog_name == NULL)
    prog_name = argv[0];
  else
    prog_name++;

  path_flag = 0;
  unix_flag = 0;
  windows_flag = 0;
  shortname_flag = 0;
  ignore_flag = 0;
  options_from_file_flag = 0;
  allusers_flag = 0;
  output_flag = 0;
  while ((c = getopt_long (argc, argv, (char *) "hac:f:opsSuvwWiDPA", long_options, (int *) NULL))
	 != EOF)
    {
      switch (c)
	{
	case 'a':
	  absolute_flag = 1;
	  break;

	case 'c':
	  CloseHandle ((HANDLE) strtoul (optarg, NULL, 16));
	  break;

	case 'f':
	  file_arg = optarg;
	  break;

	case 'o':
	  options_from_file_flag = 1;
	  break;

	case 'p':
	  path_flag = 1;
	  break;

	case 'u':
	  if (unix_flag || windows_flag)
	    usage (stderr, 1);
	  unix_flag = 1;
	  break;

	case 'w':
	  if (unix_flag || windows_flag)
	    usage (stderr, 1);
	  windows_flag = 1;
	  break;

	case 's':
	  if (unix_flag)
	    usage (stderr, 1);
	  shortname_flag = 1;
	  break;

	case 'A':
	  allusers_flag = 1;
	  break;

	case 'D':
	  if (output_flag)
	    usage (stderr, 1);
	  output_flag = 1;
	  o = 'D';
	  break;

	case 'P':
	  if (output_flag)
	    usage (stderr, 1);
	  output_flag = 1;
	  o = 'P';
          break;

	case 'S':
	  if (output_flag)
	    usage (stderr, 1);
	  output_flag = 1;
	  o = 'S';
          break;

	case 'W':
	  if (output_flag)
	    usage (stderr, 1);
	  output_flag = 1;
	  o = 'W';
	  break;

	case 'i':
	  ignore_flag = 1;
	  break;

	case 'h':
	  usage (stdout, 0);
	  break;

	case 'v':
	  printf ("Cygwin path conversion version 1.2\n");
	  printf ("Copyright 1998-2002 Red Hat, Inc.\n");
	  exit (0);

	default:
	  usage (stderr, 1);
	  break;
	}

    }

  if (output_flag) 
    {
      switch(o) {
	case 'D':
	  if (!allusers_flag)
            SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY,&id);
	  else
            SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_DESKTOPDIRECTORY,&id);
          SHGetPathFromIDList(id, buf);
          /* This if clause is a Fix for Win95 without any "All Users" */
          if ( strlen(buf) == 0 ) {
            SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY,&id);
            SHGetPathFromIDList(id, buf);
          }
	  if (!windows_flag)
	    cygwin_conv_to_posix_path(buf, buf2);
	  else
	    strcpy(buf2, buf);
          printf("%s\n", buf2);
          exit(0);

	case 'P':
	  if (!allusers_flag)
            SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &id);
	  else
            SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_PROGRAMS, &id);
          SHGetPathFromIDList(id, buf);
          /* This if clause is a Fix for Win95 without any "All Users" */
          if ( strlen(buf) == 0 ) {
            SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &id);
            SHGetPathFromIDList(id, buf);
          }
	  if (!windows_flag)
	    cygwin_conv_to_posix_path(buf, buf2);
	  else
	    strcpy(buf2, buf);
          printf("%s\n", buf2);
          exit(0);
 
	case 'S':
	  GetSystemDirectory(buf, MAX_PATH);
	  FindFirstFile(buf, &w32_fd);
	  strcpy(strrchr(buf, '\\')+1, w32_fd.cFileName);
	  if (!windows_flag)
	    cygwin_conv_to_posix_path(buf, buf2);
	  else
	    strcpy(buf2, buf);
          printf("%s\n", buf2);
          exit(0);

	case 'W':
	  GetWindowsDirectory(buf, MAX_PATH);
	  if (!windows_flag)
	    cygwin_conv_to_posix_path(buf, buf2);
	  else
	    strcpy(buf2, buf);
          printf("%s\n", buf2);
          exit(0);
 
	default:
	  fprintf(stderr, "ERROR: main: switch(o)!\n");
      }
  }

  if (options_from_file_flag && !file_arg)
    usage (stderr, 1);

  if (! unix_flag && ! windows_flag && !options_from_file_flag)
    usage (stderr, 1);

  if (!file_arg)
    {
      if (optind != argc - 1)
	usage (stderr, 1);

      filename = argv[optind];
      doit (filename);
    }
  else
    {
      FILE *fp;
      char buf[PATH_MAX * 2 + 1];

      if (argv[optind])
	usage (stderr, 1);

      if (strcmp (file_arg, "-") != 0)
	fp = fopen (file_arg, "rt");
      else
	{
	  fp = stdin;
	  setmode (0, O_TEXT);
	}
      if (fp == NULL)
	{
	  perror ("cygpath");
	  exit (1);
	}

      setbuf (stdout, NULL);
      while (fgets (buf, sizeof (buf), fp) != NULL)
	{
	  char *s = buf;
	  char *p = strchr (s, '\n');
	  if (p)
	    *p = '\0';
	  if (options_from_file_flag && *s == '-')
	    {
	      char c;
	      for (c = *++s; c && !isspace (c); c = *++s)
		switch (c)
		  {
		  case 'a':
		    absolute_flag = 1;
		    break;
		  case 'i':
		    ignore_flag = 1;
		    break;
		  case 's':
		    shortname_flag = 1;
		    break;
		  case 'w':
		    unix_flag = 0;
		    windows_flag = 1;
		    break;
		  case 'u':
		    windows_flag = 0;
		    unix_flag = 1;
		    break;
		  case 'p':
		    path_flag = 1;
		  }
	      if (*s)
		do
		  s++;
		while (*s && isspace (*s));
	    }
	  if (*s)
	    doit (s);
	}
    }

  exit (0);
}