MCPcopy Create free account
hub / github.com/coreutils/coreutils / create

Function create

src/split.c:515–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

513/* Create or truncate a file. */
514
515static int
516create (char const *name)
517{
518 if (!filter_command)
519 {
520 if (verbose)
521 fprintf (stdout, _("creating file %s\n"), quoteaf (name));
522
523 int oflags = O_WRONLY | O_CREAT | O_BINARY;
524 int fd = open (name, oflags | O_EXCL, MODE_RW_UGO);
525 if (0 <= fd || errno != EEXIST)
526 return fd;
527 fd = open (name, oflags, MODE_RW_UGO);
528 if (fd < 0)
529 return fd;
530 struct stat out_stat_buf;
531 if (fstat (fd, &out_stat_buf) != 0)
532 error (EXIT_FAILURE, errno, _("failed to stat %s"), quoteaf (name));
533 if (psame_inode (&in_stat_buf, &out_stat_buf))
534 error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
535 quoteaf (name));
536 if (ftruncate (fd, 0) < 0
537 && (S_ISREG (out_stat_buf.st_mode) || S_TYPEISSHM (&out_stat_buf)))
538 error (EXIT_FAILURE, errno, _("%s: error truncating"), quotef (name));
539
540 return fd;
541 }
542 else
543 {
544 if (setenv ("FILE", name, 1) != 0)
545 error (EXIT_FAILURE, errno,
546 _("failed to set FILE environment variable"));
547 if (verbose)
548 fprintf (stdout, _("executing with FILE=%s\n"), quotef (name));
549
550 int result;
551 int fd_pair[2];
552 pid_t child_pid;
553
554 posix_spawnattr_t attr;
555 posix_spawn_file_actions_t actions;
556
557 sigset_t set;
558 sigemptyset (&set);
559 if (default_SIGPIPE)
560 sigaddset (&set, SIGPIPE);
561
562 if ( (result = posix_spawnattr_init (&attr))
563 || (result = posix_spawnattr_setflags (&attr,
564 (POSIX_SPAWN_USEVFORK
565 | POSIX_SPAWN_SETSIGDEF)))
566 || (result = posix_spawnattr_setsigdefault (&attr, &set))
567 || (result = posix_spawn_file_actions_init (&actions))
568 )
569 error (EXIT_FAILURE, result, _("posix_spawn initialization failed"));
570
571 if (pipe (fd_pair) != 0)
572 error (EXIT_FAILURE, errno, _("failed to create pipe"));

Callers 2

cwriteFunction · 0.85
ofile_openFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected