/* Look up user and/or group names.
- Copyright (C) 1988, 1992 Free Software Foundation
+ Copyright (C) 1988 Free Software Foundation
This file is part of GNU Tar.
*
* This file should be modified for non-unix systems to do something
* reasonable.
- */
-
+ *
+ * @(#)names.c 1.3 10/30/87 - gnu
+ */
#include <sys/types.h>
#include "tar.h"
-#include "port.h"
+
+extern char *strncpy();
#ifndef NONAMES
/* Whole module goes away if NONAMES defined. Otherwise... */
-#include <stdio.h>
#include <pwd.h>
#include <grp.h>
-static int saveuid = -993;
-static char saveuname[TUNMLEN];
-static int my_uid = -993;
+static int saveuid = -993;
+static char saveuname[TUNMLEN];
+static int my_uid = -993;
-static int savegid = -993;
-static char savegname[TGNMLEN];
-static int my_gid = -993;
+static int savegid = -993;
+static char savegname[TGNMLEN];
+static int my_gid = -993;
#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
* pages" code, roughly doubling the program size. Thanks guys.
*/
void
-finduname (uname, uid)
- char uname[TUNMLEN];
- int uid;
+finduname(uname, uid)
+ char uname[TUNMLEN];
+ int uid;
{
- struct passwd *pw;
-#ifndef HAVE_GETPWUID
- extern struct passwd *getpwuid ();
-#endif
-
- if (uid != saveuid)
- {
- saveuid = uid;
- saveuname[0] = '\0';
- pw = getpwuid (uid);
- if (pw)
- strncpy (saveuname, pw->pw_name, TUNMLEN);
- }
- strncpy (uname, saveuname, TUNMLEN);
+ struct passwd *pw;
+ extern struct passwd *getpwuid ();
+
+ if (uid != saveuid) {
+ saveuid = uid;
+ saveuname[0] = '\0';
+ pw = getpwuid(uid);
+ if (pw)
+ strncpy(saveuname, pw->pw_name, TUNMLEN);
+ }
+ strncpy(uname, saveuname, TUNMLEN);
}
int
-finduid (uname)
- char uname[TUNMLEN];
+finduid(uname)
+ char uname[TUNMLEN];
{
- struct passwd *pw;
- extern struct passwd *getpwnam ();
-
- if (uname[0] != saveuname[0] /* Quick test w/o proc call */
- || 0 != strncmp (uname, saveuname, TUNMLEN))
- {
- strncpy (saveuname, uname, TUNMLEN);
- pw = getpwnam (uname);
- if (pw)
- {
- saveuid = pw->pw_uid;
+ struct passwd *pw;
+ extern struct passwd *getpwnam();
+
+ if (uname[0] != saveuname[0] /* Quick test w/o proc call */
+ || 0!=strncmp(uname, saveuname, TUNMLEN)) {
+ strncpy(saveuname, uname, TUNMLEN);
+ pw = getpwnam(uname);
+ if (pw) {
+ saveuid = pw->pw_uid;
+ } else {
+ saveuid = myuid;
+ }
}
- else
- {
- saveuid = myuid;
- }
- }
- return saveuid;
+ return saveuid;
}
void
-findgname (gname, gid)
- char gname[TGNMLEN];
- int gid;
+findgname(gname, gid)
+ char gname[TGNMLEN];
+ int gid;
{
- struct group *gr;
-#ifndef HAVE_GETGRGID
- extern struct group *getgrgid ();
-#endif
-
- if (gid != savegid)
- {
- savegid = gid;
- savegname[0] = '\0';
- (void) setgrent ();
- gr = getgrgid (gid);
- if (gr)
- strncpy (savegname, gr->gr_name, TGNMLEN);
- }
- (void) strncpy (gname, savegname, TGNMLEN);
+ struct group *gr;
+ extern struct group *getgrgid ();
+
+ if (gid != savegid) {
+ savegid = gid;
+ savegname[0] = '\0';
+ (void)setgrent();
+ gr = getgrgid(gid);
+ if (gr)
+ strncpy(savegname, gr->gr_name, TGNMLEN);
+ }
+ (void) strncpy(gname, savegname, TGNMLEN);
}
int
-findgid (gname)
- char gname[TUNMLEN];
+findgid(gname)
+ char gname[TUNMLEN];
{
- struct group *gr;
- extern struct group *getgrnam ();
-
- if (gname[0] != savegname[0] /* Quick test w/o proc call */
- || 0 != strncmp (gname, savegname, TUNMLEN))
- {
- strncpy (savegname, gname, TUNMLEN);
- gr = getgrnam (gname);
- if (gr)
- {
- savegid = gr->gr_gid;
- }
- else
- {
- savegid = mygid;
+ struct group *gr;
+ extern struct group *getgrnam();
+
+ if (gname[0] != savegname[0] /* Quick test w/o proc call */
+ || 0!=strncmp(gname, savegname, TUNMLEN)) {
+ strncpy(savegname, gname, TUNMLEN);
+ gr = getgrnam(gname);
+ if (gr) {
+ savegid = gr->gr_gid;
+ } else {
+ savegid = mygid;
+ }
}
- }
- return savegid;
+ return savegid;
}
-
#endif
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/*
+ * @(#)open3.h 1.4 87/11/11
+ *
* open3.h -- #defines for the various flags for the Sys V style 3-argument
* open() call. On BSD or System 5, the system already has this in an
* include file. This file is needed for V7 and MINIX systems for the
* benefit of open3() in port.c, a routine that emulates the 3-argument
- * call using system calls available on V7/MINIX.
+ * call using system calls available on V7/MINIX.
*
* This file is needed by PD tar even if we aren't using the
* emulator, since the #defines for O_WRONLY, etc. are used in
*/
/* Only one of the next three should be specified */
-#define O_RDONLY 0 /* only allow read */
-#define O_WRONLY 1 /* only allow write */
-#define O_RDWR 2 /* both are allowed */
+#define O_RDONLY 0 /* only allow read */
+#define O_WRONLY 1 /* only allow write */
+#define O_RDWR 2 /* both are allowed */
/* The rest of these can be OR-ed in to the above. */
/*
* it defined.
*/
#ifndef O_NDELAY
-#define O_NDELAY 4 /* don't block on opening devices that would
+#define O_NDELAY 4 /* don't block on opening devices that would
* block on open -- ignored by emulator. */
#endif
-#define O_CREAT 8 /* create file if needed */
-#define O_EXCL 16 /* file cannot already exist */
-#define O_TRUNC 32 /* truncate file on open */
-#define O_APPEND 64 /* always write at end of file -- ignored by emul */
+#define O_CREAT 8 /* create file if needed */
+#define O_EXCL 16 /* file cannot already exist */
+#define O_TRUNC 32 /* truncate file on open */
+#define O_APPEND 64 /* always write at end of file -- ignored by emul */
#ifdef EMUL_OPEN3
/*
--- /dev/null
+/* tcexparg.c - Unix-style command line wildcards for Turbo C 2.0
+
+ This file is in the public domain.
+
+ Compile your main program with -Dmain=_main and link with this file.
+
+ After that, it is just as if the operating system had expanded the
+ arguments, except that they are not sorted. The program name and all
+ arguments that are expanded from wildcards are lowercased.
+
+ Syntax for wildcards:
+ * Matches zero or more of any character (except a '.' at
+ the beginning of a name).
+ ? Matches any single character.
+ [r3z] Matches 'r', '3', or 'z'.
+ [a-d] Matches a single character in the range 'a' through 'd'.
+ [!a-d] Matches any single character except a character in the
+ range 'a' through 'd'.
+
+ The period between the filename root and its extension need not be
+ given explicitly. Thus, the pattern `a*e' will match 'abacus.exe'
+ and 'axyz.e' as well as 'apple'. Comparisons are not case sensitive.
+
+ Authors:
+ The expargs code is a modification of wildcard expansion code
+ written for Turbo C 1.0 by
+ Richard Hargrove
+ Texas Instruments, Inc.
+ P.O. Box 869305, m/s 8473
+ Plano, Texas 75086
+ 214/575-4128
+ and posted to USENET in September, 1987.
+
+ The wild_match code was written by Rich Salz, rsalz@bbn.com,
+ posted to net.sources in November, 1986.
+
+ The code connecting the two is by Mike Slomin, bellcore!lcuxa!mike2,
+ posted to comp.sys.ibm.pc in November, 1988.
+
+ Major performance enhancements and bug fixes, and source cleanup,
+ by David MacKenzie, djm@ai.mit.edu. */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <dos.h>
+#include <dir.h>
+
+/* Number of new arguments to allocate space for at a time. */
+#define ARGS_INCREMENT 10
+
+/* The name this program was run with, for error messages. */
+static char *program_name;
+
+static char **grow_argv (char **new_argv, int new_argc);
+static void fatal_error (const char *message);
+
+int wild_match (char *string, char *pattern);
+char *basename (char *path);
+
+char **expargs (int *, char **);
+
+#ifdef main
+#undef main
+#endif
+
+int
+main (int argc, char **argv, char **envp)
+{
+ argv = expargs (&argc, argv);
+ return _main (argc, argv, envp);
+}
+
+char **
+expargs (int *pargc, char **argv)
+{
+ char path[MAXPATH + 1];
+ char **new_argv;
+ struct ffblk block;
+ char *path_base;
+ char *arg_base;
+ int argind;
+ int new_argc;
+ int path_length;
+ int matched;
+
+ program_name = argv[0];
+ if (program_name && *program_name)
+ strlwr (program_name);
+ new_argv = grow_argv (NULL, 0);
+ new_argv[0] = argv[0];
+ new_argc = 1;
+
+ for (argind = 1; argind < *pargc; ++argind)
+ {
+ matched = 0;
+ if (strpbrk (argv[argind], "?*[") != NULL)
+ {
+ strncpy (path, argv[argind], MAXPATH - 3);
+ path_base = basename (path);
+ strcpy (path_base, "*.*");
+ arg_base = argv[argind] + (path_base - path);
+
+ if (!findfirst (path, &block, FA_DIREC))
+ {
+ strlwr (path);
+ do
+ {
+ /* Only match "." and ".." explicitly. */
+ if (*block.ff_name == '.' && *arg_base != '.')
+ continue;
+ path_length = stpcpy (path_base, block.ff_name) - path + 1;
+ strlwr (path_base);
+ if (wild_match (path, argv[argind]))
+ {
+ matched = 1;
+ new_argv[new_argc] = (char *) malloc (path_length);
+ if (new_argv[new_argc] == NULL)
+ fatal_error ("memory exhausted");
+ strcpy (new_argv[new_argc++], path);
+ new_argv = grow_argv (new_argv, new_argc);
+ }
+ }
+ while (!findnext (&block));
+ }
+ }
+ if (matched == 0)
+ new_argv[new_argc++] = argv[argind];
+ new_argv = grow_argv (new_argv, new_argc);
+ }
+
+ *pargc = new_argc;
+ new_argv[new_argc] = NULL;
+ return &new_argv[0];
+}
+
+/* Return a pointer to the last element of PATH. */
+
+char *
+basename (char *path)
+{
+ char *tail;
+
+ for (tail = path; *path; ++path)
+ if (*path == ':' || *path == '\\')
+ tail = path + 1;
+ return tail;
+}
+
+static char **
+grow_argv (char **new_argv, int new_argc)
+{
+ if (new_argc % ARGS_INCREMENT == 0)
+ {
+ new_argv = (char **) realloc
+ (new_argv, sizeof (char *) * (new_argc + ARGS_INCREMENT));
+ if (new_argv == NULL)
+ fatal_error ("memory exhausted");
+ }
+ return new_argv;
+}
+
+static void
+fatal_error (const char *message)
+{
+ putc ('\n', stderr);
+ if (program_name && *program_name)
+ {
+ fputs (program_name, stderr);
+ fputs (": ", stderr);
+ }
+ fputs (message, stderr);
+ putc ('\n', stderr);
+ exit (1);
+}
+
+/* Shell-style pattern matching for ?, \, [], and * characters.
+ I'm putting this replacement in the public domain.
+
+ Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986. */
+
+/* The character that inverts a character class; '!' or '^'. */
+#define INVERT '!'
+
+static int star (char *string, char *pattern);
+
+/* Return nonzero if `string' matches Unix-style wildcard pattern
+ `pattern'; zero if not. */
+
+int
+wild_match (char *string, char *pattern)
+{
+ int prev; /* Previous character in character class. */
+ int matched; /* If 1, character class has been matched. */
+ int reverse; /* If 1, character class is inverted. */
+
+ for (; *pattern; string++, pattern++)
+ switch (*pattern)
+ {
+ case '\\':
+ /* Literal match with following character; fall through. */
+ pattern++;
+ default:
+ if (*string != *pattern)
+ return 0;
+ continue;
+ case '?':
+ /* Match anything. */
+ if (*string == '\0')
+ return 0;
+ continue;
+ case '*':
+ /* Trailing star matches everything. */
+ return *++pattern ? star (string, pattern) : 1;
+ case '[':
+ /* Check for inverse character class. */
+ reverse = pattern[1] == INVERT;
+ if (reverse)
+ pattern++;
+ for (prev = 256, matched = 0; *++pattern && *pattern != ']';
+ prev = *pattern)
+ if (*pattern == '-'
+ ? *string <= *++pattern && *string >= prev
+ : *string == *pattern)
+ matched = 1;
+ if (matched == reverse)
+ return 0;
+ continue;
+ }
+
+ return *string == '\0';
+}
+
+static int
+star (char *string, char *pattern)
+{
+ while (wild_match (string, pattern) == 0)
+ if (*++string == '\0')
+ return 0;
+ return 1;
+}