2001-12-12 06:51:01 +08:00
|
|
|
/* cygpath.cc -- convert pathnames between Windows and Unix format
|
2007-03-31 08:03:25 +08:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
2006, 2007 Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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-18 00:54:01 +08:00
|
|
|
#define NOCOMATTRIBUTE
|
|
|
|
|
|
|
|
#include <shlobj.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <getopt.h>
|
2000-05-19 02:39:11 +08:00
|
|
|
#include <windows.h>
|
2000-04-11 10:57:25 +08:00
|
|
|
#include <io.h>
|
|
|
|
#include <sys/fcntl.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <sys/cygwin.h>
|
2000-04-13 13:23:30 +08:00
|
|
|
#include <ctype.h>
|
2004-02-21 12:51:15 +08:00
|
|
|
#include <errno.h>
|
2006-08-02 17:48:18 +08:00
|
|
|
#include <ddk/ntddk.h>
|
|
|
|
#include <ddk/winddk.h>
|
|
|
|
#include <ddk/ntifs.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-03-07 11:00:14 +08:00
|
|
|
static const char version[] = "$Revision$";
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static char *prog_name;
|
2007-01-09 20:17:05 +08:00
|
|
|
static char *file_arg, *output_arg;
|
2000-04-13 13:23:30 +08:00
|
|
|
static int path_flag, unix_flag, windows_flag, absolute_flag;
|
2002-05-22 18:05:41 +08:00
|
|
|
static int shortname_flag, longname_flag;
|
|
|
|
static int ignore_flag, allusers_flag, output_flag;
|
2002-06-08 03:25:26 +08:00
|
|
|
static int mixed_flag;
|
2002-07-02 04:54:35 +08:00
|
|
|
static const char *format_type_arg;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-03-07 11:00:14 +08:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{(char *) "absolute", no_argument, NULL, 'a'},
|
2002-07-02 04:54:35 +08:00
|
|
|
{(char *) "close", required_argument, NULL, 'c'},
|
|
|
|
{(char *) "dos", no_argument, NULL, 'd'},
|
|
|
|
{(char *) "file", required_argument, NULL, 'f'},
|
|
|
|
{(char *) "help", no_argument, NULL, 'h'},
|
|
|
|
{(char *) "ignore", no_argument, NULL, 'i'},
|
|
|
|
{(char *) "long-name", no_argument, NULL, 'l'},
|
|
|
|
{(char *) "mixed", no_argument, NULL, 'm'},
|
2004-02-25 09:23:20 +08:00
|
|
|
{(char *) "mode", no_argument, NULL, 'M'},
|
2002-03-07 11:00:14 +08:00
|
|
|
{(char *) "option", no_argument, NULL, 'o'},
|
|
|
|
{(char *) "path", no_argument, NULL, 'p'},
|
2002-07-02 04:54:35 +08:00
|
|
|
{(char *) "short-name", no_argument, NULL, 's'},
|
|
|
|
{(char *) "type", required_argument, NULL, 't'},
|
2002-03-07 11:00:14 +08:00
|
|
|
{(char *) "unix", no_argument, NULL, 'u'},
|
|
|
|
{(char *) "version", no_argument, NULL, 'v'},
|
|
|
|
{(char *) "windows", no_argument, NULL, 'w'},
|
|
|
|
{(char *) "allusers", no_argument, NULL, 'A'},
|
|
|
|
{(char *) "desktop", no_argument, NULL, 'D'},
|
2002-05-15 19:36:00 +08:00
|
|
|
{(char *) "homeroot", no_argument, NULL, 'H'},
|
2007-01-09 20:17:05 +08:00
|
|
|
{(char *) "mydocs", no_argument, NULL, 'O'},
|
2002-07-02 04:54:35 +08:00
|
|
|
{(char *) "smprograms", no_argument, NULL, 'P'},
|
|
|
|
{(char *) "sysdir", no_argument, NULL, 'S'},
|
|
|
|
{(char *) "windir", no_argument, NULL, 'W'},
|
2007-01-09 20:17:05 +08:00
|
|
|
{(char *) "folder", required_argument, NULL, 'F'},
|
2002-03-07 11:00:14 +08:00
|
|
|
{0, no_argument, 0, 0}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2007-01-09 20:17:05 +08:00
|
|
|
static char options[] = "ac:df:hilmMopst:uvwADHOPSWF:";
|
2002-09-15 23:19:49 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
2002-03-07 11:00:14 +08:00
|
|
|
usage (FILE * stream, int status)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-19 09:26:45 +08:00
|
|
|
if (!ignore_flag || !status)
|
|
|
|
fprintf (stream, "\
|
2005-02-24 12:32:45 +08:00
|
|
|
Usage: %s (-d|-m|-u|-w|-t TYPE) [-f FILE] [OPTION]... NAME...\n\
|
|
|
|
%s [-c HANDLE] \n\
|
2007-01-09 20:17:05 +08:00
|
|
|
%s [-ADHOPSW] \n\
|
|
|
|
%s [-F ID] \n\
|
2003-04-27 05:52:03 +08:00
|
|
|
Convert Unix and Windows format paths, or output system path information\n\
|
2003-09-11 10:56:40 +08:00
|
|
|
\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
Output type options:\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-d, --dos print DOS (short) form of NAMEs (C:\\PROGRA~1\\)\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-m, --mixed like --windows, but with regular slashes (C:/WINNT)\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-M, --mode report on mode of file (binmode or textmode)\n\
|
|
|
|
-u, --unix (default) print Unix form of NAMEs (/cygdrive/c/winnt)\n\
|
2005-02-24 12:32:45 +08:00
|
|
|
-w, --windows print Windows form of NAMEs (C:\\WINNT)\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-t, --type TYPE print TYPE form: 'dos', 'mixed', 'unix', or 'windows'\n\
|
2002-06-19 10:09:41 +08:00
|
|
|
Path conversion options:\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-a, --absolute output absolute path\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-l, --long-name print Windows long form of NAMEs (with -w, -m only)\n\
|
|
|
|
-p, --path NAME is a PATH list (i.e., '/bin:/usr/bin')\n\
|
|
|
|
-s, --short-name print DOS (short) form of NAMEs (with -w, -m only)\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
System information:\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-A, --allusers use `All Users' instead of current user for -D, -O, -P\n\
|
|
|
|
-D, --desktop output `Desktop' directory and exit\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-H, --homeroot output `Profiles' directory (home root) and exit\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-O, --mydocs output `My Documents' 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\
|
|
|
|
-F, --folder ID output special folder with numeric ID and exit\n\
|
|
|
|
", prog_name, prog_name, prog_name, prog_name);
|
2004-03-16 05:34:12 +08:00
|
|
|
if (ignore_flag)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (stream != stdout)
|
|
|
|
fprintf(stream, "Try `%s --help' for more information.\n", prog_name);
|
|
|
|
else
|
2002-07-02 04:54:35 +08:00
|
|
|
{
|
2004-03-16 05:34:12 +08:00
|
|
|
fprintf (stream, "\
|
2002-06-19 10:09:41 +08:00
|
|
|
Other options:\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-f, --file FILE read FILE for input; use - to read from STDIN\n\
|
|
|
|
-o, --option read options from FILE as well (for use with --file)\n\
|
|
|
|
-c, --close HANDLE close HANDLE (for use in captured process)\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-i, --ignore ignore missing argument\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
-h, --help output usage information and exit\n\
|
2007-01-09 20:17:05 +08:00
|
|
|
-v, --version output version information and exit\n\
|
2002-07-02 04:54:35 +08:00
|
|
|
");
|
|
|
|
}
|
2000-09-19 09:26:45 +08:00
|
|
|
exit (ignore_flag ? 0 : status);
|
|
|
|
}
|
|
|
|
|
2006-08-02 17:48:18 +08:00
|
|
|
static inline BOOLEAN
|
|
|
|
RtlAllocateUnicodeString (PUNICODE_STRING uni, ULONG size)
|
|
|
|
{
|
|
|
|
uni->Length = 0;
|
|
|
|
uni->MaximumLength = 512;
|
|
|
|
uni->Buffer = (WCHAR *) malloc (size);
|
|
|
|
return uni->Buffer != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_device_name (char *path)
|
|
|
|
{
|
|
|
|
UNICODE_STRING ntdev, tgtdev, ntdevdir;
|
|
|
|
ANSI_STRING ans;
|
|
|
|
OBJECT_ATTRIBUTES ntobj;
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE lnk, dir;
|
|
|
|
char *ret = strdup (path);
|
|
|
|
PDIRECTORY_BASIC_INFORMATION odi = (PDIRECTORY_BASIC_INFORMATION)
|
|
|
|
alloca (4096);
|
|
|
|
BOOLEAN restart;
|
|
|
|
ULONG cont;
|
|
|
|
|
|
|
|
if (strncasecmp (path, "\\Device\\", 8))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!RtlAllocateUnicodeString (&ntdev, MAX_PATH * 2))
|
|
|
|
return ret;
|
|
|
|
if (!RtlAllocateUnicodeString (&tgtdev, MAX_PATH * 2))
|
|
|
|
return ret;
|
|
|
|
RtlInitAnsiString (&ans, path);
|
|
|
|
RtlAnsiStringToUnicodeString (&ntdev, &ans, FALSE);
|
|
|
|
|
|
|
|
/* First check if the given device name is a symbolic link itself. If so,
|
|
|
|
query it and use the new name as actual device name to search for in the
|
|
|
|
DOS device name directory. If not, just use the incoming device name. */
|
|
|
|
InitializeObjectAttributes (&ntobj, &ntdev, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
|
|
|
status = ZwOpenSymbolicLinkObject (&lnk, SYMBOLIC_LINK_QUERY, &ntobj);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
status = ZwQuerySymbolicLinkObject (lnk, &tgtdev, NULL);
|
|
|
|
ZwClose (lnk);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
goto out;
|
|
|
|
RtlCopyUnicodeString (&ntdev, &tgtdev);
|
|
|
|
}
|
|
|
|
else if (status != STATUS_OBJECT_TYPE_MISMATCH)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
/* There are two DOS device directories, the local and the global dir.
|
|
|
|
Try both, local first. */
|
|
|
|
RtlInitUnicodeString (&ntdevdir, i ? L"\\GLOBAL??" : L"\\??");
|
|
|
|
|
|
|
|
/* Open the directory... */
|
|
|
|
InitializeObjectAttributes (&ntobj, &ntdevdir, OBJ_CASE_INSENSITIVE,
|
|
|
|
NULL, NULL);
|
|
|
|
status = ZwOpenDirectoryObject (&dir, DIRECTORY_QUERY, &ntobj);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* ...and scan it. */
|
|
|
|
for (restart = TRUE, cont = 0;
|
|
|
|
NT_SUCCESS (ZwQueryDirectoryObject (dir, odi, 4096, TRUE,
|
|
|
|
restart, &cont, NULL));
|
|
|
|
restart = FALSE)
|
|
|
|
{
|
|
|
|
/* For each entry check if it's a symbolic link. */
|
|
|
|
InitializeObjectAttributes (&ntobj, &odi->ObjectName,
|
|
|
|
OBJ_CASE_INSENSITIVE, dir, NULL);
|
|
|
|
status = ZwOpenSymbolicLinkObject (&lnk, SYMBOLIC_LINK_QUERY, &ntobj);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
continue;
|
|
|
|
tgtdev.Length = 0;
|
|
|
|
tgtdev.MaximumLength = 512;
|
|
|
|
/* If so, query it and compare the target of the symlink with the
|
|
|
|
incoming device name. */
|
|
|
|
status = ZwQuerySymbolicLinkObject (lnk, &tgtdev, NULL);
|
|
|
|
ZwClose (lnk);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
continue;
|
|
|
|
if (RtlEqualUnicodeString (&ntdev, &tgtdev, TRUE))
|
|
|
|
{
|
|
|
|
/* If the comparison succeeds, the name of the directory entry is
|
|
|
|
a valid DOS device name, if prepended with "\\.\". Return that
|
|
|
|
valid DOS path. */
|
|
|
|
ULONG len = RtlUnicodeStringToAnsiSize (&odi->ObjectName);
|
|
|
|
ret = (char *) malloc (len + 4);
|
|
|
|
strcpy (ret, "\\\\.\\");
|
|
|
|
ans.Length = 0;
|
|
|
|
ans.MaximumLength = len;
|
|
|
|
ans.Buffer = ret + 4;
|
|
|
|
RtlUnicodeStringToAnsiString (&ans, &odi->ObjectName, FALSE);
|
|
|
|
ZwClose (dir);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ZwClose (dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free (tgtdev.Buffer);
|
|
|
|
free (ntdev.Buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_device_paths (char *path)
|
|
|
|
{
|
|
|
|
char *sbuf;
|
|
|
|
char *ptr;
|
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
ptr = path;
|
|
|
|
while ((ptr = strchr (ptr, ';')))
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *paths[n];
|
|
|
|
DWORD acc = 0;
|
|
|
|
int i;
|
|
|
|
if (!n)
|
|
|
|
return strdup ("");
|
|
|
|
|
|
|
|
for (i = 0, ptr = path; ptr; i++)
|
|
|
|
{
|
|
|
|
char *next = ptr;
|
|
|
|
ptr = strchr (ptr, ';');
|
|
|
|
if (ptr)
|
|
|
|
*ptr++ = 0;
|
|
|
|
paths[i] = get_device_name (next);
|
|
|
|
acc += strlen (paths[i]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sbuf = (char *) malloc (acc + 1);
|
|
|
|
if (sbuf == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sbuf[0] = '\0';
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
strcat (strcat (sbuf, paths[i]), ";");
|
|
|
|
free (paths[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
strchr (sbuf, '\0')[-1] = '\0';
|
|
|
|
return sbuf;
|
|
|
|
}
|
|
|
|
|
2000-09-19 09:26:45 +08:00
|
|
|
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 11:00:14 +08:00
|
|
|
next = ptr;
|
|
|
|
ptr = strchr (ptr, ';');
|
|
|
|
if (ptr)
|
|
|
|
*ptr++ = 0;
|
|
|
|
len = GetShortPathName (next, NULL, 0);
|
2003-06-13 04:40:58 +08:00
|
|
|
if (!len)
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
next);
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
acc += len + 1;
|
2000-09-19 09:26:45 +08:00
|
|
|
}
|
2002-03-07 11:00:14 +08:00
|
|
|
sptr = sbuf = (char *) malloc (acc + 1);
|
2000-09-19 09:26:45 +08:00
|
|
|
if (sbuf == NULL)
|
|
|
|
{
|
2002-03-07 11:00:14 +08:00
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
2000-09-19 09:26:45 +08:00
|
|
|
}
|
2002-03-07 11:00:14 +08:00
|
|
|
ptr = path;
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-05-15 19:36:00 +08:00
|
|
|
len = GetShortPathName (ptr, sptr, acc);
|
2003-06-13 04:40:58 +08:00
|
|
|
if (!len)
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
ptr);
|
|
|
|
exit (2);
|
|
|
|
}
|
2000-09-19 09:26:45 +08:00
|
|
|
|
2002-03-07 11:00:14 +08:00
|
|
|
ptr = strrchr (ptr, 0);
|
|
|
|
sptr = strrchr (sptr, 0);
|
|
|
|
if (ptr == end)
|
|
|
|
break;
|
|
|
|
*sptr = ';';
|
|
|
|
++ptr, ++sptr;
|
2002-05-22 18:05:41 +08:00
|
|
|
acc -= len + 1;
|
2002-03-07 11:00:14 +08:00
|
|
|
}
|
2000-09-19 09:26:45 +08:00
|
|
|
return sbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_short_name (const char *filename)
|
|
|
|
{
|
2002-05-22 18:05:41 +08:00
|
|
|
char *sbuf, buf[MAX_PATH];
|
|
|
|
DWORD len = GetShortPathName (filename, buf, MAX_PATH);
|
2003-06-13 04:40:58 +08:00
|
|
|
if (!len)
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
|
|
|
|
filename);
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
sbuf = (char *) malloc (++len);
|
2000-09-19 09:26:45 +08:00
|
|
|
if (sbuf == NULL)
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
return strcpy (sbuf, buf);
|
|
|
|
}
|
|
|
|
|
2002-10-31 10:40:26 +08:00
|
|
|
static DWORD WINAPI
|
2002-05-22 18:05:41 +08:00
|
|
|
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;
|
2002-10-31 10:40:26 +08:00
|
|
|
|
2002-05-22 18:05:41 +08:00
|
|
|
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, ".."))
|
|
|
|
{
|
2007-03-31 08:03:25 +08:00
|
|
|
HANDLE h;
|
|
|
|
h = FindFirstFile (buf2, &w32_fd);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2002-05-22 18:05:41 +08:00
|
|
|
strcpy (ptr, w32_fd.cFileName);
|
2007-03-31 08:03:25 +08:00
|
|
|
FindClose (h);
|
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
|
|
|
ptr += strlen (ptr);
|
|
|
|
if (pelem)
|
|
|
|
{
|
|
|
|
*ptr++ = '\\';
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sbuf)
|
|
|
|
strcpy (sbuf, buf2);
|
|
|
|
SetLastError (0);
|
|
|
|
return strlen (buf2) + (sbuf ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2002-10-31 10:40:26 +08:00
|
|
|
get_long_name (const char *filename, DWORD& len)
|
2002-05-22 18:05:41 +08:00
|
|
|
{
|
2002-10-31 10:40:26 +08:00
|
|
|
char *sbuf, buf[MAX_PATH];
|
|
|
|
static HINSTANCE k32 = LoadLibrary ("kernel32.dll");
|
|
|
|
static DWORD (WINAPI *GetLongPathName) (LPCSTR, LPSTR, DWORD) =
|
2006-02-17 20:17:47 +08:00
|
|
|
(DWORD (WINAPI *) (LPCSTR, LPSTR, DWORD)) GetProcAddress (k32, "GetLongPathNameA");
|
2002-10-31 10:40:26 +08:00
|
|
|
if (!GetLongPathName)
|
|
|
|
GetLongPathName = get_long_path_name_w32impl;
|
2002-05-22 18:05:41 +08:00
|
|
|
|
2002-10-31 10:40:26 +08:00
|
|
|
len = GetLongPathName (filename, buf, MAX_PATH);
|
2006-07-28 05:07:33 +08:00
|
|
|
if (len == 0)
|
2002-05-22 18:05:41 +08:00
|
|
|
{
|
2006-07-31 15:31:56 +08:00
|
|
|
DWORD err = GetLastError ();
|
|
|
|
|
|
|
|
if (err == ERROR_INVALID_PARAMETER)
|
2006-07-28 05:07:33 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: cannot create long name of %s\n", prog_name,
|
|
|
|
filename);
|
|
|
|
exit (2);
|
|
|
|
}
|
2006-07-31 15:31:56 +08:00
|
|
|
else if (err == ERROR_FILE_NOT_FOUND)
|
|
|
|
len = get_long_path_name_w32impl (filename, buf, MAX_PATH);
|
|
|
|
else
|
2007-03-31 08:03:25 +08:00
|
|
|
{
|
2006-07-31 15:31:56 +08:00
|
|
|
buf[0] = '\0';
|
|
|
|
strncat (buf, filename, MAX_PATH - 1);
|
|
|
|
len = strlen (buf);
|
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
2002-10-31 10:40:26 +08:00
|
|
|
sbuf = (char *) malloc (len + 1);
|
|
|
|
if (!sbuf)
|
2002-05-22 18:05:41 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
2002-10-31 10:40:26 +08:00
|
|
|
return strcpy (sbuf, buf);
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2002-10-31 10:40:26 +08:00
|
|
|
get_long_paths (char *path)
|
2002-05-22 18:05:41 +08:00
|
|
|
{
|
2002-10-31 10:40:26 +08:00
|
|
|
char *sbuf;
|
|
|
|
char *ptr;
|
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
ptr = path;
|
|
|
|
while ((ptr = strchr (ptr, ';')))
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
2002-10-31 10:40:26 +08:00
|
|
|
ptr++;
|
|
|
|
n++;
|
2002-03-07 11:00:14 +08:00
|
|
|
}
|
2002-10-31 10:40:26 +08:00
|
|
|
|
|
|
|
char *paths[n];
|
|
|
|
DWORD acc = 0;
|
|
|
|
int i;
|
|
|
|
if (!n)
|
|
|
|
return strdup ("");
|
|
|
|
|
|
|
|
for (i = 0, ptr = path; ptr; i++)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *next = ptr;
|
|
|
|
ptr = strchr (ptr, ';');
|
|
|
|
if (ptr)
|
|
|
|
*ptr++ = 0;
|
|
|
|
paths[i] = get_long_name (next, len);
|
|
|
|
acc += len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sbuf = (char *) malloc (acc + 1);
|
2002-05-22 18:05:41 +08:00
|
|
|
if (sbuf == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
2002-10-31 10:40:26 +08:00
|
|
|
|
|
|
|
sbuf[0] = '\0';
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
strcat (strcat (sbuf, paths[i]), ";");
|
|
|
|
free (paths[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
strchr (sbuf, '\0')[-1] = '\0';
|
|
|
|
return sbuf;
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
|
|
|
|
2002-06-19 10:09:41 +08: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;
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:17:05 +08:00
|
|
|
static bool
|
|
|
|
get_special_folder (char* path, int id)
|
|
|
|
{
|
|
|
|
path[0] = 0;
|
|
|
|
LPITEMIDLIST pidl = 0;
|
|
|
|
if (SHGetSpecialFolderLocation (NULL, id, &pidl) != S_OK)
|
|
|
|
return false;
|
|
|
|
if (!SHGetPathFromIDList (pidl, path) || !path[0])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_user_folder (char* path, int id, int allid)
|
|
|
|
{
|
|
|
|
if (!get_special_folder (path, allusers_flag ? allid : id) && allusers_flag)
|
|
|
|
get_special_folder (path, id); // Fix for Win9x without any "All Users"
|
|
|
|
}
|
|
|
|
|
2002-05-22 18:05:41 +08:00
|
|
|
static void
|
|
|
|
dowin (char option)
|
|
|
|
{
|
|
|
|
char *buf, buf1[MAX_PATH], buf2[MAX_PATH];
|
|
|
|
DWORD len = MAX_PATH;
|
|
|
|
WIN32_FIND_DATA w32_fd;
|
2002-10-31 10:40:26 +08:00
|
|
|
HINSTANCE k32;
|
2002-05-22 18:05:41 +08:00
|
|
|
BOOL (*GetProfilesDirectoryAPtr) (LPSTR, LPDWORD) = 0;
|
2002-10-31 10:40:26 +08:00
|
|
|
|
2002-05-22 18:05:41 +08:00
|
|
|
buf = buf1;
|
2007-01-09 20:17:05 +08:00
|
|
|
buf[0] = 0;
|
2002-05-22 18:05:41 +08:00
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'D':
|
2007-01-09 20:17:05 +08:00
|
|
|
get_user_folder (buf, CSIDL_DESKTOPDIRECTORY,
|
|
|
|
CSIDL_COMMON_DESKTOPDIRECTORY);
|
2002-05-22 18:05:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2007-01-09 20:17:05 +08:00
|
|
|
get_user_folder (buf, CSIDL_PROGRAMS, CSIDL_COMMON_PROGRAMS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
get_user_folder (buf, CSIDL_PERSONAL, CSIDL_COMMON_DOCUMENTS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
{
|
|
|
|
int val = -1, len = -1;
|
|
|
|
if (!(sscanf (output_arg, "%i%n", &val, &len) == 1
|
|
|
|
&& len == (int) strlen (output_arg) && val >= 0))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: syntax error in special folder ID %s\n",
|
|
|
|
prog_name, output_arg);
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-03-31 08:03:25 +08:00
|
|
|
get_special_folder (buf, val);
|
2007-01-09 20:17:05 +08:00
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
2002-10-31 10:40:26 +08:00
|
|
|
k32 = LoadLibrary ("userenv");
|
|
|
|
if (k32)
|
2002-05-22 18:05:41 +08:00
|
|
|
GetProfilesDirectoryAPtr = (BOOL (*) (LPSTR, LPDWORD))
|
2002-10-31 10:40:26 +08:00
|
|
|
GetProcAddress (k32, "GetProfilesDirectoryA");
|
2002-05-22 18:05:41 +08:00
|
|
|
if (GetProfilesDirectoryAPtr)
|
2002-06-19 10:09:41 +08:00
|
|
|
(*GetProfilesDirectoryAPtr) (buf, &len);
|
2002-05-22 18:05:41 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:17:05 +08:00
|
|
|
if (!buf[0])
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: failed to retrieve special folder path\n", prog_name);
|
|
|
|
}
|
|
|
|
else if (!windows_flag)
|
2002-05-22 18:05:41 +08:00
|
|
|
{
|
2004-02-21 12:51:15 +08:00
|
|
|
if (cygwin_conv_to_posix_path (buf, buf2))
|
|
|
|
fprintf (stderr, "%s: error converting \"%s\" - %s\n",
|
|
|
|
prog_name, buf, strerror (errno));
|
|
|
|
else
|
|
|
|
buf = buf2;
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (shortname_flag)
|
2002-06-19 10:09:41 +08:00
|
|
|
buf = get_short_name (buf);
|
|
|
|
if (mixed_flag)
|
|
|
|
buf = get_mixed_name (buf);
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
|
|
|
printf ("%s\n", buf);
|
|
|
|
exit (0);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2004-02-25 09:23:20 +08:00
|
|
|
static void
|
|
|
|
report_mode (char *filename)
|
|
|
|
{
|
|
|
|
switch (cygwin_internal (CW_GET_BINMODE, filename))
|
|
|
|
{
|
|
|
|
case O_BINARY:
|
|
|
|
printf ("%s: binary\n", filename);
|
|
|
|
break;
|
|
|
|
case O_TEXT:
|
|
|
|
printf ("%s: text\n", filename);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "%s: file '%s' - %s\n", prog_name, filename,
|
|
|
|
strerror (errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-11 10:57:25 +08:00
|
|
|
static void
|
2000-04-13 13:23:30 +08:00
|
|
|
doit (char *filename)
|
2000-04-11 10:57:25 +08:00
|
|
|
{
|
|
|
|
char *buf;
|
2002-10-31 10:40:26 +08:00
|
|
|
DWORD len;
|
2004-02-21 12:51:15 +08:00
|
|
|
int err;
|
2002-03-07 11:00:14 +08:00
|
|
|
int (*conv_func) (const char *, char *);
|
2000-04-11 10:57:25 +08:00
|
|
|
|
2002-03-07 11:00:14 +08:00
|
|
|
if (!path_flag)
|
2001-12-27 01:46:12 +08:00
|
|
|
{
|
2002-10-31 10:40:26 +08:00
|
|
|
len = strlen (filename);
|
|
|
|
if (len)
|
2003-02-13 06:48:13 +08:00
|
|
|
len += MAX_PATH + 1001;
|
2002-10-31 10:40:26 +08:00
|
|
|
else if (ignore_flag)
|
|
|
|
exit (0);
|
|
|
|
else
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
2002-10-31 10:40:26 +08:00
|
|
|
fprintf (stderr, "%s: can't convert empty path\n", prog_name);
|
|
|
|
exit (1);
|
2002-03-07 11:00:14 +08:00
|
|
|
}
|
2001-12-27 01:46:12 +08:00
|
|
|
}
|
2002-10-31 10:40:26 +08:00
|
|
|
else if (unix_flag)
|
|
|
|
len = cygwin_win32_to_posix_path_list_buf_size (filename);
|
2000-04-11 10:57:25 +08:00
|
|
|
else
|
2002-10-31 10:40:26 +08:00
|
|
|
len = cygwin_posix_to_win32_path_list_buf_size (filename);
|
2000-04-11 10:57:25 +08:00
|
|
|
|
|
|
|
buf = (char *) malloc (len);
|
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: out of memory\n", prog_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_flag)
|
|
|
|
{
|
|
|
|
if (unix_flag)
|
2004-02-21 12:51:15 +08:00
|
|
|
err = cygwin_win32_to_posix_path_list (filename, buf);
|
2000-04-11 10:57:25 +08:00
|
|
|
else
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
2004-02-21 12:51:15 +08:00
|
|
|
err = cygwin_posix_to_win32_path_list (filename, buf);
|
|
|
|
if (err)
|
|
|
|
/* oops */;
|
2006-08-02 17:48:18 +08:00
|
|
|
buf = get_device_paths (buf);
|
2002-03-07 11:00:14 +08:00
|
|
|
if (shortname_flag)
|
|
|
|
buf = get_short_paths (buf);
|
2002-05-22 18:05:41 +08:00
|
|
|
if (longname_flag)
|
|
|
|
buf = get_long_paths (buf);
|
2002-06-08 03:25:26 +08:00
|
|
|
if (mixed_flag)
|
|
|
|
buf = get_mixed_name (buf);
|
2002-03-07 11:00:14 +08:00
|
|
|
}
|
2004-02-21 12:51:15 +08:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: error converting \"%s\" - %s\n",
|
|
|
|
prog_name, filename, strerror (errno));
|
|
|
|
exit (1);
|
|
|
|
}
|
2000-04-11 10:57:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unix_flag)
|
2002-03-07 11:00:14 +08:00
|
|
|
conv_func = (absolute_flag ? cygwin_conv_to_full_posix_path :
|
|
|
|
cygwin_conv_to_posix_path);
|
2000-04-11 10:57:25 +08:00
|
|
|
else
|
2002-03-07 11:00:14 +08:00
|
|
|
conv_func = (absolute_flag ? cygwin_conv_to_full_win32_path :
|
|
|
|
cygwin_conv_to_win32_path);
|
2004-02-21 12:51:15 +08:00
|
|
|
err = conv_func (filename, buf);
|
|
|
|
if (err)
|
2002-03-07 11:00:14 +08:00
|
|
|
{
|
2004-02-21 12:51:15 +08:00
|
|
|
fprintf (stderr, "%s: error converting \"%s\" - %s\n",
|
|
|
|
prog_name, filename, strerror (errno));
|
2002-03-07 11:00:14 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
if (!unix_flag)
|
|
|
|
{
|
2006-08-02 17:48:18 +08:00
|
|
|
buf = get_device_name (buf);
|
2003-02-13 06:48:13 +08:00
|
|
|
if (shortname_flag)
|
|
|
|
buf = get_short_name (buf);
|
|
|
|
if (longname_flag)
|
|
|
|
buf = get_long_name (buf, len);
|
2006-02-17 20:17:47 +08:00
|
|
|
if (mixed_flag)
|
|
|
|
buf = get_mixed_name (buf);
|
2002-05-22 18:05:41 +08:00
|
|
|
}
|
2000-04-11 10:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
puts (buf);
|
|
|
|
}
|
|
|
|
|
2002-03-07 11:00:14 +08: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\
|
2005-05-17 09:26:16 +08:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.\n\
|
2002-09-15 23:19:49 +08:00
|
|
|
Compiled on %s\n\
|
|
|
|
", len, v, __DATE__);
|
2002-03-07 11:00:14 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2002-03-07 11:00:14 +08:00
|
|
|
int c, o = 0;
|
2000-04-13 13:23:30 +08:00
|
|
|
int options_from_file_flag;
|
2004-02-25 09:23:20 +08:00
|
|
|
int mode_flag;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
prog_name = strrchr (argv[0], '/');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = strrchr (argv[0], '\\');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = argv[0];
|
2001-12-27 01:46:12 +08:00
|
|
|
else
|
|
|
|
prog_name++;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
path_flag = 0;
|
2002-07-02 04:54:35 +08:00
|
|
|
unix_flag = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
windows_flag = 0;
|
2000-09-19 09:26:45 +08:00
|
|
|
shortname_flag = 0;
|
2002-05-22 18:05:41 +08:00
|
|
|
longname_flag = 0;
|
2002-06-08 03:25:26 +08:00
|
|
|
mixed_flag = 0;
|
2000-09-19 09:26:45 +08:00
|
|
|
ignore_flag = 0;
|
2000-04-13 13:23:30 +08:00
|
|
|
options_from_file_flag = 0;
|
2002-01-18 00:54:01 +08:00
|
|
|
allusers_flag = 0;
|
|
|
|
output_flag = 0;
|
2004-02-25 09:23:20 +08:00
|
|
|
mode_flag = 0;
|
2002-09-15 23:19:49 +08:00
|
|
|
while ((c = getopt_long (argc, argv, options,
|
2002-06-08 03:25:26 +08:00
|
|
|
long_options, (int *) NULL)) != EOF)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2000-04-13 13:23:30 +08:00
|
|
|
case 'a':
|
|
|
|
absolute_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
CloseHandle ((HANDLE) strtoul (optarg, NULL, 16));
|
|
|
|
break;
|
|
|
|
|
2002-07-02 04:54:35 +08:00
|
|
|
case 'd':
|
2002-10-31 10:40:26 +08:00
|
|
|
if (windows_flag)
|
2002-07-02 04:54:35 +08:00
|
|
|
usage (stderr, 1);
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
shortname_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-04-11 10:57:25 +08:00
|
|
|
case 'f':
|
|
|
|
file_arg = optarg;
|
|
|
|
break;
|
|
|
|
|
2004-02-25 09:23:20 +08:00
|
|
|
case 'M':
|
|
|
|
mode_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-04-13 13:23:30 +08:00
|
|
|
case 'o':
|
|
|
|
options_from_file_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'p':
|
|
|
|
path_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2002-07-02 04:54:35 +08:00
|
|
|
if (windows_flag || mixed_flag)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage (stderr, 1);
|
|
|
|
unix_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
2002-07-02 04:54:35 +08:00
|
|
|
if (windows_flag || mixed_flag)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage (stderr, 1);
|
2002-07-02 04:54:35 +08:00
|
|
|
unix_flag = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
windows_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2002-07-02 04:54:35 +08:00
|
|
|
case 'm':
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
mixed_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2002-05-22 18:05:41 +08:00
|
|
|
case 'l':
|
|
|
|
longname_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-09-19 09:26:45 +08:00
|
|
|
case 's':
|
|
|
|
shortname_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2002-06-08 03:25:26 +08:00
|
|
|
case 't':
|
2002-07-02 04:54:35 +08:00
|
|
|
if (optarg == NULL)
|
2002-06-08 03:25:26 +08:00
|
|
|
usage (stderr, 1);
|
|
|
|
|
2002-07-02 04:54:35 +08:00
|
|
|
format_type_arg = (*optarg == '=') ? (optarg + 1) : (optarg);
|
|
|
|
if (strcasecmp (format_type_arg, "dos") == 0)
|
2002-10-31 10:40:26 +08:00
|
|
|
{
|
|
|
|
if (windows_flag || longname_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
shortname_flag = 1;
|
|
|
|
}
|
2002-07-02 04:54:35 +08:00
|
|
|
else if (strcasecmp (format_type_arg, "mixed") == 0)
|
2002-10-31 10:40:26 +08:00
|
|
|
{
|
|
|
|
unix_flag = 0;
|
2002-06-08 03:25:26 +08:00
|
|
|
mixed_flag = 1;
|
2002-10-31 10:40:26 +08:00
|
|
|
}
|
2002-07-02 04:54:35 +08:00
|
|
|
else if (strcasecmp (format_type_arg, "unix") == 0)
|
2002-10-31 10:40:26 +08:00
|
|
|
{
|
|
|
|
if (windows_flag)
|
|
|
|
usage (stderr, 1);
|
2002-07-02 04:54:35 +08:00
|
|
|
unix_flag = 1;
|
2002-10-31 10:40:26 +08:00
|
|
|
}
|
2002-07-02 04:54:35 +08:00
|
|
|
else if (strcasecmp (format_type_arg, "windows") == 0)
|
2002-10-31 10:40:26 +08:00
|
|
|
{
|
|
|
|
if (mixed_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
}
|
2002-06-08 03:25:26 +08:00
|
|
|
else
|
|
|
|
usage (stderr, 1);
|
|
|
|
break;
|
|
|
|
|
2002-01-18 00:54:01 +08:00
|
|
|
case 'A':
|
|
|
|
allusers_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
2002-05-15 19:36:00 +08:00
|
|
|
case 'H':
|
2007-01-09 20:17:05 +08:00
|
|
|
case 'O':
|
2002-01-18 00:54:01 +08:00
|
|
|
case 'P':
|
2000-05-20 01:06:28 +08:00
|
|
|
case 'S':
|
2002-01-18 00:54:01 +08:00
|
|
|
case 'W':
|
|
|
|
if (output_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
output_flag = 1;
|
2002-05-15 19:36:00 +08:00
|
|
|
o = c;
|
2002-01-18 00:54:01 +08:00
|
|
|
break;
|
2000-05-20 01:06:28 +08:00
|
|
|
|
2007-01-09 20:17:05 +08:00
|
|
|
case 'F':
|
|
|
|
if (output_flag || !optarg)
|
|
|
|
usage (stderr, 1);
|
|
|
|
output_flag = 1;
|
|
|
|
output_arg = optarg;
|
|
|
|
o = c;
|
|
|
|
break;
|
|
|
|
|
2000-09-19 09:26:45 +08:00
|
|
|
case 'i':
|
|
|
|
ignore_flag = 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2002-03-07 11:00:14 +08:00
|
|
|
print_version ();
|
2000-02-18 03:38:33 +08:00
|
|
|
exit (0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage (stderr, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-13 13:23:30 +08:00
|
|
|
if (options_from_file_flag && !file_arg)
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
2002-07-02 04:54:35 +08:00
|
|
|
if (longname_flag && !windows_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
|
|
|
if (shortname_flag && !windows_flag)
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
|
|
|
if (!unix_flag && !windows_flag && !mixed_flag && !options_from_file_flag)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage (stderr, 1);
|
|
|
|
|
2000-04-11 10:57:25 +08:00
|
|
|
if (!file_arg)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-05-22 18:05:41 +08:00
|
|
|
if (output_flag)
|
|
|
|
dowin (o);
|
|
|
|
|
2003-11-08 02:30:35 +08:00
|
|
|
if (optind > argc - 1)
|
2000-04-11 10:57:25 +08:00
|
|
|
usage (stderr, 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-02-25 09:23:20 +08:00
|
|
|
for (int i = optind; argv[i]; i++)
|
|
|
|
if (mode_flag)
|
|
|
|
report_mode (argv[i]);
|
|
|
|
else
|
|
|
|
doit (argv[i]);
|
2000-04-11 10:57:25 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
2000-04-11 10:57:25 +08:00
|
|
|
FILE *fp;
|
|
|
|
char buf[PATH_MAX * 2 + 1];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-04-11 10:57:25 +08:00
|
|
|
if (argv[optind])
|
|
|
|
usage (stderr, 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-04-11 10:57:25 +08:00
|
|
|
if (strcmp (file_arg, "-") != 0)
|
|
|
|
fp = fopen (file_arg, "rt");
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2000-04-11 10:57:25 +08:00
|
|
|
{
|
|
|
|
fp = stdin;
|
|
|
|
setmode (0, O_TEXT);
|
|
|
|
}
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
perror ("cygpath");
|
|
|
|
exit (1);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-04-13 13:23:30 +08:00
|
|
|
setbuf (stdout, NULL);
|
2000-04-11 10:57:25 +08:00
|
|
|
while (fgets (buf, sizeof (buf), fp) != NULL)
|
|
|
|
{
|
2000-04-13 13:23:30 +08:00
|
|
|
char *s = buf;
|
|
|
|
char *p = strchr (s, '\n');
|
2000-04-11 10:57:25 +08:00
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
2000-04-13 13:23:30 +08: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 09:26:45 +08:00
|
|
|
case 'i':
|
|
|
|
ignore_flag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
shortname_flag = 1;
|
2002-05-22 18:05:41 +08:00
|
|
|
longname_flag = 0;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
shortname_flag = 0;
|
|
|
|
longname_flag = 1;
|
2000-09-19 09:26:45 +08:00
|
|
|
break;
|
2002-07-02 04:54:35 +08:00
|
|
|
case 'm':
|
|
|
|
unix_flag = 0;
|
|
|
|
windows_flag = 1;
|
|
|
|
mixed_flag = 1;
|
2000-04-13 13:23:30 +08: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 18:05:41 +08:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'H':
|
2007-01-09 20:17:05 +08:00
|
|
|
case 'O':
|
2002-05-22 18:05:41 +08:00
|
|
|
case 'P':
|
|
|
|
case 'S':
|
|
|
|
case 'W':
|
2002-06-08 03:25:26 +08:00
|
|
|
output_flag = 1;
|
2002-05-22 18:05:41 +08:00
|
|
|
o = c;
|
2002-06-08 03:25:26 +08:00
|
|
|
break;
|
2000-04-13 13:23:30 +08:00
|
|
|
}
|
|
|
|
if (*s)
|
|
|
|
do
|
|
|
|
s++;
|
|
|
|
while (*s && isspace (*s));
|
|
|
|
}
|
2002-05-22 18:05:41 +08:00
|
|
|
if (*s && !output_flag)
|
2000-04-13 13:23:30 +08:00
|
|
|
doit (s);
|
2002-05-22 18:05:41 +08:00
|
|
|
if (!*s && output_flag)
|
|
|
|
dowin (o);
|
2000-04-11 10:57:25 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|