Пример #1
0
/* This returns a new stream opened on a temporary file (generated
   by tmpnam).  The file is opened with mode "w+b" (binary read/write).
   If we couldn't generate a unique filename or the file couldn't
   be opened, NULL is returned.  */
FILE *
tmpfile (void)
{
  char buf[FILENAME_MAX];
  int fd;
  FILE *f;

  if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
    return NULL;
  int flags = 0;
#ifdef FLAGS
  flags = FLAGS;
#endif
  fd = __gen_tempname (buf, 0, flags, __GT_FILE);
  if (fd < 0)
    return NULL;

  /* Note that this relies on the Unix semantics that
     a file is not really removed until it is closed.  */
  (void) __unlink (buf);

  if ((f = __fdopen (fd, "w+b")) == NULL)
    __close (fd);

  return f;
}
Пример #2
0
/* Generate a unique filename in P_tmpdir.  If S is NULL return NULL.
   This makes this function thread safe.  */
char * tmpnam_r (char *s)
{
    if (s == NULL)
	return NULL;

    if (__path_search (s, L_tmpnam, NULL, NULL, 0))
	return NULL;
    if (__gen_tempname (s, __GT_NOCREATE))
	return NULL;

    return s;
}
Пример #3
0
/* Generate a unique temporary filename using up to five characters of
   PFX if it is not NULL.  The directory to put this file in is
   searched for as follows: First the environment variable "TMPDIR" is
   checked.  If it contains the name of a directory, that directory is
   used.  If not and if DIR is not NULL, that value is checked.  If
   that fails, P_tmpdir is tried and finally "/tmp".  The storage for
   the filename is allocated by `malloc'.  */
char *
tempnam (const char *dir, const char *pfx)
{
  char buf[FILENAME_MAX];

  if (__path_search (buf, FILENAME_MAX, dir, pfx, 1))
    return NULL;

  if (__gen_tempname (buf, 0, 0, __GT_NOCREATE))
    return NULL;

  return __strdup (buf);
}
Пример #4
0
/* This returns a new stream opened on a temporary file (generated
   by tmpnam).  The file is opened with mode "w+b" (binary read/write).
   If we couldn't generate a unique filename or the file couldn't
   be opened, NULL is returned.  */
FILE * tmpfile (void)
{
    char buf[FILENAME_MAX];
    int fd;
    FILE *f;

    if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
	return NULL;
    fd = __gen_tempname (buf, __GT_FILE);
    if (fd < 0)
	return NULL;

    /* Note that this relies on the Unix semantics that
       a file is not really removed until it is closed.  */
    (void) remove (buf);

    if ((f = fdopen (fd, "w+b")) == NULL)
	close (fd);

    return f;
}
Пример #5
0
attribute_compat_text_section
__old_tmpfile (void)
{
  char buf[FILENAME_MAX];
  int fd;
  FILE *f;

  if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
    return NULL;
  fd = __gen_tempname (buf, 0, 0, __GT_FILE);
  if (fd < 0)
    return NULL;

  /* Note that this relies on the Unix semantics that
     a file is not really removed until it is closed.  */
  (void) __unlink (buf);

  if ((f = _IO_old_fdopen (fd, "w+b")) == NULL)
    __close (fd);

  return f;
}
Пример #6
0
/* Generate a unique filename in P_tmpdir.

   This function is *not* thread safe!  */
char *
tmpnam (char *s)
{
  /* By using two buffers we manage to be thread safe in the case
     where S != NULL.  */
  char tmpbufmem[L_tmpnam];
  char *tmpbuf = s ?: tmpbufmem;

  /* In the following call we use the buffer pointed to by S if
     non-NULL although we don't know the size.  But we limit the size
     to L_tmpnam characters in any case.  */
  if (__builtin_expect (__path_search (tmpbuf, L_tmpnam, NULL, NULL, 0),
			0))
    return NULL;

  if (__builtin_expect (__gen_tempname (tmpbuf, __GT_NOCREATE), 0))
    return NULL;

  if (s == NULL)
    return (char *) memcpy (tmpnam_buffer, tmpbuf, L_tmpnam);

  return s;
}
/* This returns a new stream opened on a temporary file (generated
   by tmpnam).  The file is opened with mode "w+b" (binary read/write).
   If we couldn't generate a unique filename or the file couldn't
   be opened, NULL is returned.  */
FILE *
tmpfile (void)
{
  char buf[FILENAME_MAX];
  int fd;
  FILE *f;

  if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
    return NULL;
  fd = __gen_tempname (buf, GEN_THIS);
  if (fd < 0)
    return NULL;

#ifndef __WIN32__
	/* Note that this relies on the Unix semantics that
     a file is not really removed until it is closed.  */
  (void) remove (buf);
#endif
  if ((f = __fdopen (fd, "w+b")) == NULL)
    __close (fd);

  return f;
}
Пример #8
0
/* Using Unix sockets this way is a security risk.  */
static int
gaih_local(const char *name, const struct gaih_service *service,
		const struct addrinfo *req, struct addrinfo **pai)
{
	struct utsname utsname;
	struct addrinfo *ai = *pai;

	if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
		return (GAIH_OKIFUNSPEC | -EAI_NONAME);

	if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
		if (uname(&utsname) < 0)
			return -EAI_SYSTEM;

	if (name != NULL) {
		if (strcmp(name, "localhost") &&
		    strcmp(name, "local") &&
		    strcmp(name, "unix") &&
		    strcmp(name, utsname.nodename))
			return (GAIH_OKIFUNSPEC | -EAI_NONAME);
		}

		if (req->ai_protocol || req->ai_socktype) {
			const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;

			while (tp->name[0]
			    && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
			       || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
			       || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
			) {
				++tp;
			}
			if (! tp->name[0]) {
				if (req->ai_socktype)
					return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
				return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
			}
		}

		*pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
				+ ((req->ai_flags & AI_CANONNAME)
				? (strlen(utsname.nodename) + 1) : 0));
		if (ai == NULL)
			return -EAI_MEMORY;

		ai->ai_next = NULL;
		ai->ai_flags = req->ai_flags;
		ai->ai_family = AF_LOCAL;
		ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
		ai->ai_protocol = req->ai_protocol;
		ai->ai_addrlen = sizeof(struct sockaddr_un);
		ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
#if SALEN
		((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
#endif /* SALEN */

		((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
		memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);

		if (service) {
			struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;

			if (strchr(service->name, '/') != NULL) {
				if (strlen(service->name) >= sizeof(sunp->sun_path))
					return GAIH_OKIFUNSPEC | -EAI_SERVICE;

			strcpy(sunp->sun_path, service->name);
		} else {
			if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
				return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
			stpcpy(stpcpy(sunp->sun_path, P_tmpdir "/"), service->name);
		}
	} else {
		/* This is a dangerous use of the interface since there is a time
		   window between the test for the file and the actual creation
		   (done by the caller) in which a file with the same name could
		   be created.  */
		char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;

		if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
		 || __gen_tempname(buf, __GT_NOCREATE) != 0
		) {
			return -EAI_SYSTEM;
		}
	}

	ai->ai_canonname = NULL;
	if (req->ai_flags & AI_CANONNAME)
		ai->ai_canonname = strcpy((char *)(ai + 1) + sizeof(struct sockaddr_un),
				utsname.nodename);
	return 0;
}