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: 653d41ceca068d3d8dbcd124eb6aea0a02b63fa7 (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
/* pathconv.cc -- convert pathnames between Windows and Unix format
   Copyright 1998 Cygnus Solutions.
   Written by Ian Lance Taylor <ian@cygnus.com>.

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. */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <getopt.h>
#include <sys/cygwin.h>

static char *prog_name;

static struct option long_options[] =
{
  { (char *) "help", no_argument, NULL, 'h' },
  { (char *) "path", no_argument, NULL, 'p' },
  { (char *) "unix", no_argument, NULL, 'u' },
  { (char *) "version", no_argument, NULL, 'v' },
  { (char *) "windows", no_argument, NULL, 'w' },
  { 0, no_argument, 0, 0 }
};

static void
usage (FILE *stream, int status)
{
  fprintf (stream, "\
Usage: %s [-p|--path] (-u|--unix)|(-w|--windows) filename\n\
  -u|--unix     print Unix form of filename\n\
  -w|--windows  print Windows form of filename\n\
  -p|--path     filename argument is a path\n",
	   prog_name);
  exit (status);
}

int
main (int argc, char **argv)
{
  int path_flag, unix_flag, windows_flag;
  int c;
  char *filename;
  size_t len;
  char *buf;

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

  path_flag = 0;
  unix_flag = 0;
  windows_flag = 0;
  while ((c = getopt_long (argc, argv, (char *) "hpuvw", long_options, (int *) NULL))
	 != EOF)
    {
      switch (c)
	{
	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 'h':
	  usage (stdout, 0);
	  break;

	case 'v':
	  printf ("Cygwin pathconv version 1.0\n");
	  printf ("Copyright 1998 Cygnus Solutions\n");
	  exit (0);

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

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

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

  filename = argv[optind];

  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;
  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);
    }
  else
    {
      if (unix_flag)
	cygwin_conv_to_posix_path (filename, buf);
      else
	cygwin_conv_to_win32_path (filename, buf);
    }

  puts (buf);

  exit (0);
}