2001-12-11 23:51:01 +01:00
|
|
|
/* cygpath.cc -- convert pathnames between Windows and Unix format
|
2002-02-27 22:20:09 +01:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2002-01-17 17:54:01 +01:00
|
|
|
#define NOCOMATTRIBUTE
|
|
|
|
|
|
|
|
#include <shlobj.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <getopt.h>
|
2000-05-18 20:39:11 +02:00
|
|
|
#include <windows.h>
|
2000-04-11 04:57:25 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <sys/fcntl.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/cygwin.h>
|
2000-04-13 07:23:30 +02:00
|
|
|
#include <ctype.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-03-07 04:00:14 +01:00
|
|
|
static const char version[] = "$Revision$";
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
static char *prog_name;
|
2000-04-11 04:57:25 +02:00
|
|
|
static char *file_arg;
|
2000-04-13 07:23:30 +02:00
|
|
|
static char *close_arg;
|
|
|
|
static int path_flag, unix_flag, windows_flag, absolute_flag;
|
2002-05-22 12:05:41 +02:00
|
|
|
static int shortname_flag, longname_flag;
|
|
|
|
static int ignore_flag, allusers_flag, output_flag;
|
2002-06-07 21:25:26 +02:00
|
|
|
static int mixed_flag;
|
|
|
|
static const char *windows_format_arg;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-03-07 04:00:14 +01:00
|
|
|
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'},
|
2002-05-22 12:05:41 +02:00
|
|
|
{(char *) "long-name", no_argument, NULL, 'l'},
|
2002-03-07 04:00:14 +01:00
|
|
|
{(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'},
|
2002-06-07 21:25:26 +02:00
|
|
|
{(char *) "type", required_argument, (int *) &windows_format_arg, 't'},
|
2002-05-15 13:36:00 +02:00
|
|
|
{(char *) "homeroot", no_argument, NULL, 'H'},
|
2002-03-07 04:00:14 +01:00
|
|
|
{0, no_argument, 0, 0}
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2002-03-07 04:00:14 +01:00
|
|
|
usage (FILE * stream, int status)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-19 03:26:45 +02:00
|
|
|
if (!ignore_flag || !status)
|
|
|
|
fprintf (stream, "\
|
2002-05-22 12:05:41 +02:00
|
|
|
Usage: %s (-u|--unix)|(-w|--windows) [options] filename\n\n\
|
|
|
|
-u|--unix print Unix form of filename\n\
|
|
|
|
-w|--windows print Windows form of filename\n\n\
|
|
|
|
Other options:\n\
|
2000-04-13 07:23:30 +02:00
|
|
|
-a|--absolute output absolute path\n\
|
|
|
|
-c|--close handle close handle (for use in captured process)\n\
|
2002-01-17 17:54:01 +01:00
|
|
|
-f|--file file read file for input path information\n\
|
2001-12-04 05:12:39 +01:00
|
|
|
-i|--ignore ignore missing argument\n\
|
2002-05-22 12:05:41 +02:00
|
|
|
-l|--long-name print Windows long form of filename (with -w only)\n\
|
2001-12-04 05:12:39 +01:00
|
|
|
-p|--path filename argument is a path\n\
|
2002-05-22 12:05:41 +02:00
|
|
|
-s|--short-name print Windows short form of filename (with -w only)\n\
|
2002-06-07 21:25:26 +02:00
|
|
|
-t|--type print Windows form of filename with specified\n\
|
|
|
|
dos drive letter with backslashes (C:\\WINNT)\n\
|
|
|
|
mixed drive letter with regular slashes (C:/WINNT)\n\
|
2002-01-17 17:54:01 +01:00
|
|
|
-A|--allusers use `All Users' instead of current user for -D, -P\n\
|
|
|
|
-D|--desktop output `Desktop' directory and exit\n\
|
2002-05-15 13:36:00 +02:00
|
|
|
-H|--homeroot output `Profiles' directory (home root) and exit\n\
|
2002-01-17 17:54:01 +01:00
|
|
|
-P|--smprograms output Start Menu `Programs' directory and exit\n\
|
|
|
|
-S|--sysdir output system directory and exit\n\
|
2002-05-22 12:05:41 +02:00
|
|
|
-W|--windir output `Windows' directory and exit\n\n\
|
|
|
|
Informative output:\n\
|
|
|
|
-h|--help print this help, then exit\n\
|
|
|
|
-v|--version output version information and exit\n", prog_name);
|
2000-09-19 03:26:45 +02:00
|
|
|
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)
|
|
|
|
{
|
2002-03-07 04:00:14 +01:00
|
|
|
next = ptr;
|
|
|
|
ptr = strchr (ptr, ';');
|
|
|
|
if (ptr)
|
|
|
|
*ptr++ = 0;
|
|
|
|
len = GetShortPathName (next, NULL, 0);
|
2002-05-15 13:36:00 +02:00
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
next);
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
acc += len + 1;
|
2000-09-19 03:26:45 +02:00
|
|
|
}
|
2002-03-07 04:00:14 +01:00
|
|
|
sptr = sbuf = (char *) malloc (acc + 1);
|
2000-09-19 03:26:45 +02:00
|
|
|
if (sbuf == NULL)
|
|
|
|
{
|
2002-03-07 04:00:14 +01:00
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
2000-09-19 03:26:45 +02:00
|
|
|
}
|
2002-03-07 04:00:14 +01:00
|
|
|
ptr = path;
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-05-15 13:36:00 +02:00
|
|
|
len = GetShortPathName (ptr, sptr, acc);
|
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
ptr);
|
|
|
|
exit (2);
|
|
|
|
}
|
2000-09-19 03:26:45 +02:00
|
|
|
|
2002-03-07 04:00:14 +01:00
|
|
|
ptr = strrchr (ptr, 0);
|
|
|
|
sptr = strrchr (sptr, 0);
|
|
|
|
if (ptr == end)
|
|
|
|
break;
|
|
|
|
*sptr = ';';
|
|
|
|
++ptr, ++sptr;
|
2002-05-22 12:05:41 +02:00
|
|
|
acc -= len + 1;
|
2002-03-07 04:00:14 +01:00
|
|
|
}
|
2000-09-19 03:26:45 +02:00
|
|
|
return sbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_short_name (const char *filename)
|
|
|
|
{
|
2002-05-22 12:05:41 +02:00
|
|
|
char *sbuf, buf[MAX_PATH];
|
|
|
|
DWORD len = GetShortPathName (filename, buf, MAX_PATH);
|
2002-05-15 13:36:00 +02:00
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
filename);
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
sbuf = (char *) malloc (++len);
|
2000-09-19 03:26:45 +02:00
|
|
|
if (sbuf == NULL)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
2002-05-22 12:05:41 +02:00
|
|
|
return strcpy (sbuf, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD
|
|
|
|
get_long_path_name_w32impl (LPCSTR src, LPSTR sbuf, DWORD)
|
|
|
|
{
|
|
|
|
char buf1[MAX_PATH], buf2[MAX_PATH], *ptr;
|
|
|
|
const char *pelem, *next;
|
|
|
|
WIN32_FIND_DATA w32_fd;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
strcpy (buf1, src);
|
|
|
|
*buf2 = 0;
|
|
|
|
pelem = src;
|
|
|
|
ptr = buf2;
|
|
|
|
while (pelem)
|
|
|
|
{
|
|
|
|
next = pelem;
|
|
|
|
if (*next == '\\')
|
|
|
|
{
|
|
|
|
strcat (ptr++, "\\");
|
|
|
|
pelem++;
|
|
|
|
if (!*pelem)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pelem = strchr (next, '\\');
|
|
|
|
len = pelem ? (pelem++ - next) : strlen (next);
|
|
|
|
strncpy (ptr, next, len);
|
|
|
|
ptr[len] = 0;
|
|
|
|
if (next[1] != ':' && strcmp(next, ".") && strcmp(next, ".."))
|
|
|
|
{
|
|
|
|
if (FindFirstFile (buf2, &w32_fd) != INVALID_HANDLE_VALUE)
|
|
|
|
strcpy (ptr, w32_fd.cFileName);
|
|
|
|
}
|
|
|
|
ptr += strlen (ptr);
|
|
|
|
if (pelem)
|
|
|
|
{
|
|
|
|
*ptr++ = '\\';
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sbuf)
|
|
|
|
strcpy (sbuf, buf2);
|
|
|
|
SetLastError (0);
|
|
|
|
return strlen (buf2) + (sbuf ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_long_paths (char *path)
|
|
|
|
{
|
|
|
|
char *sbuf;
|
|
|
|
char *sptr;
|
|
|
|
char *next;
|
|
|
|
char *ptr = path;
|
|
|
|
char *end = strrchr (path, 0);
|
|
|
|
DWORD acc = 0;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
HINSTANCE hinst;
|
|
|
|
DWORD (*GetLongPathNameAPtr) (LPCSTR, LPSTR, DWORD) = 0;
|
|
|
|
hinst = LoadLibrary ("kernel32");
|
|
|
|
if (hinst)
|
|
|
|
GetLongPathNameAPtr = (DWORD (*) (LPCSTR, LPSTR, DWORD))
|
|
|
|
GetProcAddress (hinst, "GetLongPathNameA");
|
|
|
|
/* subsequent calls of kernel function with NULL cause SegFault in W2K!! */
|
|
|
|
if (1 || !GetLongPathNameAPtr)
|
|
|
|
GetLongPathNameAPtr = get_long_path_name_w32impl;
|
|
|
|
|
|
|
|
while (ptr != NULL)
|
|
|
|
{
|
|
|
|
next = ptr;
|
|
|
|
ptr = strchr (ptr, ';');
|
|
|
|
if (ptr)
|
|
|
|
*ptr++ = 0;
|
|
|
|
len = (*GetLongPathNameAPtr) (next, NULL, 0);
|
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create long 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 (;;)
|
|
|
|
{
|
|
|
|
len = (*GetLongPathNameAPtr) (ptr, sptr, acc);
|
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create long name of %s\n", prog_name,
|
|
|
|
ptr);
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = strrchr (ptr, 0);
|
|
|
|
sptr = strrchr (sptr, 0);
|
|
|
|
if (ptr == end)
|
|
|
|
break;
|
|
|
|
*ptr = *sptr = ';';
|
|
|
|
++ptr, ++sptr;
|
|
|
|
acc -= len + 1;
|
|
|
|
}
|
|
|
|
return sbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_long_name (const char *filename)
|
|
|
|
{
|
|
|
|
char *sbuf, buf[MAX_PATH];
|
|
|
|
DWORD len;
|
|
|
|
HINSTANCE hinst;
|
|
|
|
DWORD (*GetLongPathNameAPtr) (LPCSTR, LPSTR, DWORD) = 0;
|
|
|
|
hinst = LoadLibrary ("kernel32");
|
|
|
|
if (hinst)
|
|
|
|
GetLongPathNameAPtr = (DWORD (*) (LPCSTR, LPSTR, DWORD))
|
|
|
|
GetProcAddress (hinst, "GetLongPathNameA");
|
|
|
|
if (!GetLongPathNameAPtr)
|
|
|
|
GetLongPathNameAPtr = get_long_path_name_w32impl;
|
|
|
|
|
|
|
|
len = (*GetLongPathNameAPtr) (filename, buf, MAX_PATH);
|
2002-05-15 13:36:00 +02:00
|
|
|
if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
2002-05-22 12:05:41 +02:00
|
|
|
fprintf (stderr, "%s: cannot create long name of %s\n", prog_name,
|
2002-03-07 04:00:14 +01:00
|
|
|
filename);
|
|
|
|
exit (2);
|
|
|
|
}
|
2002-05-22 12:05:41 +02:00
|
|
|
sbuf = (char *) malloc (++len);
|
|
|
|
if (sbuf == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
return strcpy (sbuf, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dowin (char option)
|
|
|
|
{
|
|
|
|
char *buf, buf1[MAX_PATH], buf2[MAX_PATH];
|
|
|
|
DWORD len = MAX_PATH;
|
|
|
|
WIN32_FIND_DATA w32_fd;
|
|
|
|
LPITEMIDLIST id;
|
|
|
|
HINSTANCE hinst;
|
|
|
|
BOOL (*GetProfilesDirectoryAPtr) (LPSTR, LPDWORD) = 0;
|
|
|
|
|
|
|
|
buf = buf1;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'D':
|
|
|
|
SHGetSpecialFolderLocation (NULL, allusers_flag ?
|
|
|
|
CSIDL_COMMON_DESKTOPDIRECTORY : CSIDL_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);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
SHGetSpecialFolderLocation (NULL, allusers_flag ?
|
|
|
|
CSIDL_COMMON_PROGRAMS : CSIDL_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);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
hinst = LoadLibrary ("userenv");
|
|
|
|
if (hinst)
|
|
|
|
GetProfilesDirectoryAPtr = (BOOL (*) (LPSTR, LPDWORD))
|
|
|
|
GetProcAddress (hinst, "GetProfilesDirectoryA");
|
|
|
|
if (GetProfilesDirectoryAPtr)
|
|
|
|
(*GetProfilesDirectoryAPtr) (buf, &len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetWindowsDirectory (buf, MAX_PATH);
|
|
|
|
strcat (buf, "\\Profiles");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
GetSystemDirectory (buf, MAX_PATH);
|
|
|
|
FindFirstFile (buf, &w32_fd);
|
|
|
|
strcpy (strrchr (buf, '\\') + 1, w32_fd.cFileName);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
GetWindowsDirectory (buf, MAX_PATH);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage (stderr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!windows_flag)
|
|
|
|
{
|
|
|
|
cygwin_conv_to_posix_path (buf, buf2);
|
|
|
|
buf = buf2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (shortname_flag)
|
|
|
|
buf = get_short_name (buf);
|
|
|
|
}
|
|
|
|
printf ("%s\n", buf);
|
|
|
|
exit (0);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-06-07 21:25:26 +02:00
|
|
|
static void
|
|
|
|
convert_slashes (char* name)
|
|
|
|
{
|
|
|
|
while ((name = strchr (name, '\\')) != NULL)
|
|
|
|
{
|
|
|
|
if (*name == '\\')
|
|
|
|
*name = '/';
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_mixed_name (const char* filename)
|
|
|
|
{
|
|
|
|
char* mixed_buf = strdup (filename);
|
|
|
|
|
|
|
|
if (mixed_buf == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_slashes (mixed_buf);
|
|
|
|
|
|
|
|
return mixed_buf;
|
|
|
|
}
|
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
static void
|
2000-04-13 07:23:30 +02:00
|
|
|
doit (char *filename)
|
2000-04-11 04:57:25 +02:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
2001-12-26 18:46:12 +01:00
|
|
|
int retval;
|
2002-03-07 04:00:14 +01:00
|
|
|
int (*conv_func) (const char *, char *);
|
2000-04-11 04:57:25 +02:00
|
|
|
|
|
|
|
if (path_flag)
|
|
|
|
{
|
2002-03-07 04:00:14 +01:00
|
|
|
if (cygwin_posix_path_list_p (filename) ? unix_flag : windows_flag)
|
2000-04-11 04:57:25 +02:00
|
|
|
{
|
|
|
|
/* The path is already in the right format. */
|
|
|
|
puts (filename);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-07 04:00:14 +01:00
|
|
|
if (!path_flag)
|
2001-12-26 18:46:12 +01:00
|
|
|
{
|
|
|
|
len = strlen (filename) + 100;
|
|
|
|
if (len == 100)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
if (!ignore_flag)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: can't convert empty path\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
exit (0);
|
|
|
|
}
|
2001-12-26 18:46:12 +01:00
|
|
|
}
|
2000-04-11 04:57:25 +02:00
|
|
|
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
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
cygwin_posix_to_win32_path_list (filename, buf);
|
|
|
|
if (shortname_flag)
|
|
|
|
buf = get_short_paths (buf);
|
2002-05-22 12:05:41 +02:00
|
|
|
if (longname_flag)
|
|
|
|
buf = get_long_paths (buf);
|
2002-06-07 21:25:26 +02:00
|
|
|
if (mixed_flag)
|
|
|
|
buf = get_mixed_name (buf);
|
2002-03-07 04:00:14 +01:00
|
|
|
}
|
2000-04-11 04:57:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unix_flag)
|
2002-03-07 04:00:14 +01:00
|
|
|
conv_func = (absolute_flag ? cygwin_conv_to_full_posix_path :
|
|
|
|
cygwin_conv_to_posix_path);
|
2000-04-11 04:57:25 +02:00
|
|
|
else
|
2002-03-07 04:00:14 +01:00
|
|
|
conv_func = (absolute_flag ? cygwin_conv_to_full_win32_path :
|
|
|
|
cygwin_conv_to_win32_path);
|
2001-12-26 18:46:12 +01:00
|
|
|
retval = conv_func (filename, buf);
|
2002-06-07 21:25:26 +02:00
|
|
|
if (mixed_flag)
|
|
|
|
buf = get_mixed_name (buf);
|
2001-12-26 18:46:12 +01:00
|
|
|
if (retval < 0)
|
2002-03-07 04:00:14 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: error converting \"%s\"\n",
|
|
|
|
prog_name, filename);
|
|
|
|
exit (1);
|
|
|
|
}
|
2002-05-22 12:05:41 +02:00
|
|
|
if (!unix_flag)
|
|
|
|
{
|
|
|
|
if (shortname_flag)
|
|
|
|
buf = get_short_name (buf);
|
|
|
|
if (longname_flag)
|
|
|
|
buf = get_long_name (buf);
|
|
|
|
}
|
2000-04-11 04:57:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
puts (buf);
|
|
|
|
}
|
|
|
|
|
2002-03-07 04:00:14 +01:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
|
|
|
const char *v = strchr (version, ':');
|
|
|
|
int len;
|
|
|
|
if (!v)
|
|
|
|
{
|
|
|
|
v = "?";
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v += 2;
|
|
|
|
len = strchr (v, ' ') - v;
|
|
|
|
}
|
|
|
|
printf ("\
|
|
|
|
cygpath (cygwin) %.*s\n\
|
|
|
|
Path Conversion Utility\n\
|
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.\n\
|
|
|
|
Compiled on %s", len, v, __DATE__);
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2002-03-07 04:00:14 +01:00
|
|
|
int c, o = 0;
|
2000-04-13 07:23:30 +02:00
|
|
|
int options_from_file_flag;
|
2000-02-17 20:38:33 +01:00
|
|
|
char *filename;
|
|
|
|
|
|
|
|
prog_name = strrchr (argv[0], '/');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = strrchr (argv[0], '\\');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = argv[0];
|
2001-12-26 18:46:12 +01:00
|
|
|
else
|
|
|
|
prog_name++;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
path_flag = 0;
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 0;
|
2000-09-19 03:26:45 +02:00
|
|
|
shortname_flag = 0;
|
2002-05-22 12:05:41 +02:00
|
|
|
longname_flag = 0;
|
2002-06-07 21:25:26 +02:00
|
|
|
mixed_flag = 0;
|
2000-09-19 03:26:45 +02:00
|
|
|
ignore_flag = 0;
|
2000-04-13 07:23:30 +02:00
|
|
|
options_from_file_flag = 0;
|
2002-01-17 17:54:01 +01:00
|
|
|
allusers_flag = 0;
|
|
|
|
output_flag = 0;
|
2002-06-07 21:25:26 +02:00
|
|
|
while ((c = getopt_long (argc, argv, (char *) "hac:f:opslSuvwt:WiDPAH",
|
|
|
|
long_options, (int *) NULL)) != EOF)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2000-04-13 07:23:30 +02:00
|
|
|
case 'a':
|
|
|
|
absolute_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
CloseHandle ((HANDLE) strtoul (optarg, NULL, 16));
|
|
|
|
break;
|
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
case 'f':
|
|
|
|
file_arg = optarg;
|
|
|
|
break;
|
|
|
|
|
2000-04-13 07:23:30 +02:00
|
|
|
case 'o':
|
|
|
|
options_from_file_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
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;
|
|
|
|
|
2002-05-22 12:05:41 +02:00
|
|
|
case 'l':
|
|
|
|
if (unix_flag || shortname_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
longname_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-09-19 03:26:45 +02:00
|
|
|
case 's':
|
2002-05-22 12:05:41 +02:00
|
|
|
if (unix_flag || longname_flag)
|
2000-09-19 03:26:45 +02:00
|
|
|
usage (stderr, 1);
|
|
|
|
shortname_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2002-06-07 21:25:26 +02:00
|
|
|
case 't':
|
|
|
|
if (unix_flag || (optarg == NULL))
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
|
|
|
windows_flag = 1;
|
|
|
|
windows_format_arg = (*optarg == '=') ? (optarg + 1) : (optarg);
|
|
|
|
|
|
|
|
if (strcasecmp (windows_format_arg, "mixed") == 0)
|
|
|
|
mixed_flag = 1;
|
|
|
|
else if (strcasecmp (windows_format_arg, "dos") == 0)
|
|
|
|
/* nothing */;
|
|
|
|
else
|
|
|
|
usage (stderr, 1);
|
|
|
|
break;
|
|
|
|
|
2002-01-17 17:54:01 +01:00
|
|
|
case 'A':
|
|
|
|
allusers_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
2002-05-15 13:36:00 +02:00
|
|
|
case 'H':
|
2002-01-17 17:54:01 +01:00
|
|
|
case 'P':
|
2000-05-19 19:06:28 +02:00
|
|
|
case 'S':
|
2002-01-17 17:54:01 +01:00
|
|
|
case 'W':
|
|
|
|
if (output_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
output_flag = 1;
|
2002-05-15 13:36:00 +02:00
|
|
|
o = c;
|
2002-01-17 17:54:01 +01:00
|
|
|
break;
|
2000-05-19 19:06:28 +02:00
|
|
|
|
2000-09-19 03:26:45 +02:00
|
|
|
case 'i':
|
|
|
|
ignore_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
case 'h':
|
|
|
|
usage (stdout, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2002-03-07 04:00:14 +01:00
|
|
|
print_version ();
|
2000-02-17 20:38:33 +01:00
|
|
|
exit (0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage (stderr, 1);
|
|
|
|
break;
|
|
|
|
}
|
2002-01-17 17:54:01 +01:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-04-13 07:23:30 +02:00
|
|
|
if (options_from_file_flag && !file_arg)
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
2002-05-22 12:05:41 +02:00
|
|
|
if (!output_flag && !unix_flag && !windows_flag && !options_from_file_flag)
|
2000-02-17 20:38:33 +01:00
|
|
|
usage (stderr, 1);
|
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
if (!file_arg)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-05-22 12:05:41 +02:00
|
|
|
if (output_flag)
|
|
|
|
dowin (o);
|
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
if (optind != argc - 1)
|
|
|
|
usage (stderr, 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
filename = argv[optind];
|
2000-04-13 07:23:30 +02:00
|
|
|
doit (filename);
|
2000-04-11 04:57:25 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
{
|
2000-04-11 04:57:25 +02:00
|
|
|
FILE *fp;
|
|
|
|
char buf[PATH_MAX * 2 + 1];
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
if (argv[optind])
|
|
|
|
usage (stderr, 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-04-11 04:57:25 +02:00
|
|
|
if (strcmp (file_arg, "-") != 0)
|
|
|
|
fp = fopen (file_arg, "rt");
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2000-04-11 04:57:25 +02:00
|
|
|
{
|
|
|
|
fp = stdin;
|
|
|
|
setmode (0, O_TEXT);
|
|
|
|
}
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
perror ("cygpath");
|
|
|
|
exit (1);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-04-13 07:23:30 +02:00
|
|
|
setbuf (stdout, NULL);
|
2000-04-11 04:57:25 +02:00
|
|
|
while (fgets (buf, sizeof (buf), fp) != NULL)
|
|
|
|
{
|
2000-04-13 07:23:30 +02:00
|
|
|
char *s = buf;
|
|
|
|
char *p = strchr (s, '\n');
|
2000-04-11 04:57:25 +02:00
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
2000-04-13 07:23:30 +02:00
|
|
|
if (options_from_file_flag && *s == '-')
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
for (c = *++s; c && !isspace (c); c = *++s)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
absolute_flag = 1;
|
|
|
|
break;
|
2000-09-19 03:26:45 +02:00
|
|
|
case 'i':
|
|
|
|
ignore_flag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
shortname_flag = 1;
|
2002-05-22 12:05:41 +02:00
|
|
|
longname_flag = 0;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
shortname_flag = 0;
|
|
|
|
longname_flag = 1;
|
2000-09-19 03:26:45 +02:00
|
|
|
break;
|
2000-04-13 07:23:30 +02:00
|
|
|
case 'w':
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
windows_flag = 0;
|
|
|
|
unix_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
path_flag = 1;
|
2002-05-22 12:05:41 +02:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'H':
|
|
|
|
case 'P':
|
|
|
|
case 'S':
|
|
|
|
case 'W':
|
2002-06-07 21:25:26 +02:00
|
|
|
output_flag = 1;
|
2002-05-22 12:05:41 +02:00
|
|
|
o = c;
|
2002-06-07 21:25:26 +02:00
|
|
|
break;
|
2000-04-13 07:23:30 +02:00
|
|
|
}
|
|
|
|
if (*s)
|
|
|
|
do
|
|
|
|
s++;
|
|
|
|
while (*s && isspace (*s));
|
|
|
|
}
|
2002-05-22 12:05:41 +02:00
|
|
|
if (*s && !output_flag)
|
2000-04-13 07:23:30 +02:00
|
|
|
doit (s);
|
2002-05-22 12:05:41 +02:00
|
|
|
if (!*s && output_flag)
|
|
|
|
dowin (o);
|
2000-04-11 04:57:25 +02:00
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|