本文整理汇总了C++中creat函数的典型用法代码示例。如果您正苦于以下问题:C++ creat函数的具体用法?C++ creat怎么用?C++ creat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了creat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cmd_send
//.........这里部分代码省略.........
}
ret = is_subvol_ro(&send, snapshot_parent);
if (ret < 0)
goto out;
if (!ret) {
ret = -EINVAL;
error("parent subvolume %s is not read-only",
snapshot_parent);
goto out;
}
full_send = 0;
break;
case 'i':
error("option -i was removed, use -c instead");
ret = 1;
goto out;
case GETOPT_VAL_SEND_NO_DATA:
send_flags |= BTRFS_SEND_FLAG_NO_FILE_DATA;
break;
case '?':
default:
error("send arguments invalid");
ret = 1;
goto out;
}
}
if (check_argc_min(argc - optind, 1))
usage(cmd_send_usage);
if (outname[0]) {
send.dump_fd = creat(outname, 0600);
if (send.dump_fd == -1) {
ret = -errno;
error("cannot create '%s': %s", outname, strerror(-ret));
goto out;
}
}
if (isatty(send.dump_fd)) {
error(
"not dumping send stream into a terminal, redirect it into a file");
ret = 1;
goto out;
}
/* use first send subvol to determine mount_root */
subvol = argv[optind];
subvol = realpath(argv[optind], NULL);
if (!subvol) {
ret = -errno;
error("unable to resolve %s", argv[optind]);
goto out;
}
ret = init_root_path(&send, subvol);
if (ret < 0)
goto out;
if (snapshot_parent != NULL) {
ret = get_root_id(&send,
get_subvol_name(send.root_path, snapshot_parent),
&parent_root_id);
示例2: main
int
main(int argc, char *argv[])
{
unsigned char body[64*1024];
unsigned char out[65*1024];
unsigned char *publisher = 0;
char *infname = 0, *outfname = 0;
int i = 0, f, len, opt, plen;
char *prefix[CCNL_MAX_NAME_COMP], *cp;
private_key_path = 0;
witness = 0;
while ((opt = getopt(argc, argv, "hi:o:p:k:w:")) != -1) {
switch (opt) {
case 'i':
infname = optarg;
break;
case 'o':
outfname = optarg;
break;
case 'k':
private_key_path = optarg;
break;
case 'w':
witness = optarg;
break;
case 'p':
publisher = (unsigned char*)optarg;
plen = unescape_component(publisher);
if (plen != 32) {
fprintf(stderr,
"publisher key digest has wrong length (%d instead of 32)\n",
plen);
exit(-1);
}
break;
case 'h':
default:
Usage:
fprintf(stderr, "usage: %s [options] URI\n"
" -i FNAME input file (instead of stdin)\n"
" -o FNAME output file (instead of stdout)\n"
" -p DIGEST publisher fingerprint\n"
" -k FNAME publisher private key\n"
" -w STRING witness\n" ,
argv[0]);
exit(1);
}
}
if (!argv[optind])
goto Usage;
cp = strtok(argv[optind], "/");
while (i < (CCNL_MAX_NAME_COMP - 1) && cp) {
prefix[i++] = cp;
cp = strtok(NULL, "/");
}
prefix[i] = NULL;
if (infname) {
f = open(infname, O_RDONLY);
if (f < 0)
perror("file open:");
} else
f = 0;
len = read(f, body, sizeof(body));
close(f);
len = mkContent(prefix, publisher, plen, body, len, out);
if (outfname) {
f = creat(outfname, 0666);
if (f < 0)
perror("file open:");
} else
f = 1;
write(f, out, len);
close(f);
return 0;
}
示例3: quit
//.........这里部分代码省略.........
* If he has specified "append" don't copy his mailbox,
* just copy saveable entries at the end.
*/
mbox = expand("&");
mcount = c;
if (value("append") == NOSTR) {
if ((obuf = Fopen(tempQuit, "w")) == NULL) {
perror(tempQuit);
Fclose(fbuf);
return;
}
if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
perror(tempQuit);
rm(tempQuit);
Fclose(obuf);
Fclose(fbuf);
return;
}
rm(tempQuit);
if ((abuf = Fopen(mbox, "r")) != NULL) {
while ((c = getc(abuf)) != EOF)
(void) putc(c, obuf);
Fclose(abuf);
}
if (ferror(obuf)) {
perror(tempQuit);
Fclose(ibuf);
Fclose(obuf);
Fclose(fbuf);
return;
}
Fclose(obuf);
close(creat(mbox, 0600));
if ((obuf = Fopen(mbox, "r+")) == NULL) {
perror(mbox);
Fclose(ibuf);
Fclose(fbuf);
return;
}
}
if (value("append") != NOSTR) {
if ((obuf = Fopen(mbox, "a")) == NULL) {
perror(mbox);
Fclose(fbuf);
return;
}
fchmod(fileno(obuf), 0600);
}
for (mp = &message[0]; mp < &message[msgCount]; mp++)
if (mp->m_flag & MBOX)
if (send(mp, obuf, saveignore, NOSTR) < 0) {
perror(mbox);
Fclose(ibuf);
Fclose(obuf);
Fclose(fbuf);
return;
}
/*
* Copy the user's old mbox contents back
* to the end of the stuff we just saved.
* If we are appending, this is unnecessary.
*/
if (value("append") == NOSTR) {
示例4: copy_file
/*
* Copy a file to a new destination
*/
static boolean_t
copy_file (const char *path, const char *dest, const struct stat *statptr)
{
int in_fd, out_fd;
struct stat in_stat;
off_t offset = 0;
/*
* Stat the file if the caller hasn't
*/
if (statptr == NULL)
{
if (stat (path, &in_stat) == -1)
{
fprintf (stderr, "Unable to stat file %s: %s\n", path, strerror (errno));
return B_FALSE;
}
}
else
in_stat = *statptr;
/*
* Open the files
*/
if ((in_fd = open (path, O_RDONLY)) == -1)
{
fprintf (stderr, "Unable to open file %s: %s\n", path, strerror (errno));
return B_FALSE;
}
if ((out_fd = creat (dest, in_stat.st_mode)) == -1)
{
if (errno == EEXIST)
{
if (unlink (dest) == -1)
{
fprintf (stderr, "Unable to remove file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
if ((out_fd = creat (dest, in_stat.st_mode)) == -1)
{
fprintf (stderr, "Unable to recreate file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
}
else
{
fprintf (stderr, "Unable to create file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
}
/*
* Copy ownership
*/
if (chown (dest, in_stat.st_uid, in_stat.st_gid) == -1)
{
fprintf (stderr, "Unable to chown file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
(void) close (out_fd);
return B_FALSE;
}
/*
* Copy contents over
*/
if (sendfile (out_fd, in_fd, &offset, in_stat.st_size) == -1)
{
fprintf (stderr, "Unable to copy file %s: %s\n", path, strerror (errno));
(void) close (in_fd);
(void) close (out_fd);
return B_FALSE;
}
(void) close (in_fd);
(void) close (out_fd);
return B_TRUE;
}
示例5: getlock
void
getlock(void)
{
int i = 0, fd;
fflush(stdout);
/* we ignore QUIT and INT at this point */
if (link(HLOCK, LLOCK) == -1) {
int errnosv = errno;
perror(HLOCK);
printf("Cannot link %s to %s\n", LLOCK, HLOCK);
switch (errnosv) {
case ENOENT:
printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
break;
case EACCES:
printf("It seems you don't have write permission here.\n");
break;
case EEXIST:
printf("(Try again or rm %s.)\n", LLOCK);
break;
default:
printf("I don't know what is wrong.");
}
getret();
error("%s", "");
/* NOTREACHED */
}
regularize(lock);
glo(0);
if (locknum > 25)
locknum = 25;
do {
if (locknum)
lock[0] = 'a' + i++;
if ((fd = open(lock, O_RDONLY)) == -1) {
if (errno == ENOENT) /* no such file */
goto gotlock;
perror(lock);
unlink(LLOCK);
error("Cannot open %s", lock);
}
if (veryold(fd)) /* if true, this closes fd and unlinks lock */
goto gotlock;
close(fd);
} while (i < locknum);
unlink(LLOCK);
error(locknum ? "Too many hacks running now."
: "There is a game in progress under your name.");
gotlock:
fd = creat(lock, FMASK);
if (unlink(LLOCK) == -1)
error("Cannot unlink %s.", LLOCK);
if (fd == -1) {
error("cannot creat lock file.");
} else {
if (write(fd, (char *)&hackpid, sizeof(hackpid))
!= sizeof(hackpid))
error("cannot write lock");
if (close(fd) == -1)
error("cannot close lock");
}
}
示例6: test11
/* Test open/lock/close without unlocking */
static void test11(void)
{
int rc;
int fd;
int gid;
char buf[10000];
/* Create the test file. */
fd = creat(mainpath, 0);
ASSERTF(fd >= 0, "creat failed for '%s': %s",
mainpath, strerror(errno));
memset(buf, 0x5a, sizeof(buf));
rc = write(fd, buf, sizeof(buf));
ASSERTF(rc == sizeof(buf), "write failed for '%s': %s",
mainpath, strerror(errno));
close(fd);
/* Open/lock and close many times. Open with different
* flags. */
for (gid = 1; gid < 10000; gid++) {
int oflags = O_RDONLY;
switch (gid % 10) {
case 0:
oflags = O_RDONLY;
break;
case 1:
oflags = O_WRONLY;
break;
case 2:
oflags = O_WRONLY | O_APPEND;
break;
case 3:
oflags = O_WRONLY | O_CLOEXEC;
break;
case 4:
oflags = O_WRONLY | O_DIRECT;
break;
case 5:
oflags = O_WRONLY | O_NOATIME;
break;
case 6:
oflags = O_WRONLY | O_SYNC;
break;
case 7:
oflags = O_RDONLY | O_DIRECT;
break;
case 8:
oflags = O_RDWR;
break;
case 9:
oflags = O_RDONLY | O_LOV_DELAY_CREATE;
break;
}
fd = open(mainpath, oflags);
ASSERTF(fd >= 0, "open failed for '%s': %s (oflags=%d, gid=%d)",
mainpath, strerror(errno), oflags, gid);
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == 0, "cannot lock '%s': %s (oflags=%d, gid=%d)",
mainpath, strerror(errno), oflags, gid);
close(fd);
}
}
示例7: test10
/* Test lock / unlock */
static void test10(void)
{
int rc;
int fd;
int gid;
int i;
/* Create the test file, and open it. */
fd = creat(mainpath, 0);
ASSERTF(fd >= 0, "creat failed for '%s': %s",
mainpath, strerror(errno));
/* Valid command first. */
gid = 1234;
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == 0, "cannot lock '%s': %s",
mainpath, strerror(errno));
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == 0, "cannot unlock '%s': %s",
mainpath, strerror(errno));
/* Again */
gid = 768;
for (i = 0; i < 1000; i++) {
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == 0,
"cannot lock '%s': %s (loop %d)",
mainpath, strerror(errno), i);
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == 0,
"cannot unlock '%s': %s (loop %d)",
mainpath, strerror(errno), i);
}
/* Lock twice. */
gid = 97486;
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == 0, "cannot lock '%s': %s", mainpath, strerror(errno));
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == -1 && errno == EINVAL,
"lock unexpectedly granted for '%s': %s",
mainpath, strerror(errno));
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == 0, "cannot unlock '%s': %s", mainpath, strerror(errno));
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == -1 && errno == EINVAL, "unexpected unlock retval: %d %s",
rc, strerror(errno));
/* 0 is an invalid gid */
gid = 0;
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == -1 && errno == EINVAL, "unexpected lock retval: %s",
strerror(errno));
/* Lock/unlock with a different gid */
gid = 3543;
rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
ASSERTF(rc == 0, "cannot lock '%s': %s", mainpath, strerror(errno));
for (gid = -10; gid < 10; gid++) {
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == -1 && errno == EINVAL,
"unexpected unlock retval: %d %s (gid %d)",
rc, strerror(errno), gid);
}
gid = 3543;
rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
ASSERTF(rc == 0, "cannot unlock '%s': %s", mainpath, strerror(errno));
close(fd);
}
示例8: main
int
main(int argc, char *argv[])
{
char **argp = ++argv;
char *fname;
char *op;
int max_pages;
int rest_secs;
int iteration = 0;
#define NARGS 4
if(argc < NARGS + 1)
{
printf("frw <fname> <op=r/w> <max_pages> <rest_secs>\n");
printf("op = r - read , w - random, s - fsync write\n");
return -2;
}
fname = *argp++;
op = *argp++;
sscanf(*argp++, "%d", &max_pages);
sscanf(*argp++, "%d", &rest_secs);
printf("fname = %s op=%s max_pages = %d rest_secs = %d\n", fname, op, max_pages, rest_secs);
if(op[0] == 'r')
{
slam_fd = open(fname, O_RDWR);
}
else
{
slam_fd = open(fname, O_WRONLY| O_TRUNC);
if(slam_fd < 0 )
slam_fd = creat(fname, 0666);
}
if(slam_fd < 0)
{
perror("open:");
return -2;
}
signal(SIGINT, sigterm_hdlr);
signal(SIGTERM, sigterm_hdlr);
while(1)
{
timeit(NULL);
if(op[0] == 'r')
{
read_fd(slam_fd, max_pages, op[1]=='r');
timeit("read");
}
else
{
write_fd(slam_fd, max_pages, op[1]=='r');
if(op[0] == 's')
{
fsync(slam_fd);
timeit("write fsync");
}
else
{
timeit("write");
}
}
printf("%d:iter=%d\n", getpid(), iteration++);
sleep(rest_secs);
}
return 0;
}
示例9: dopopen
/* Function for opening subprocesses. Returns 0 on success and -1 on failure.
On failure, errmsg_out shall contain a '\0'-terminated error message. */
static int dopopen(const char *const *args, /* program arguments with NULL sentinel */
const char *executable, /* actual executable */
struct fdinfo fdinfo[3], /* info for stdin/stdout/stderr */
int close_fds, /* 1 to close all fds */
int binary, /* 1 to use binary files */
const char *cwd, /* working directory for program */
struct proc *proc, /* populated on success! */
FILE *pipe_ends_out[3], /* pipe ends are put here */
char errmsg_out[], /* written to on failure */
size_t errmsg_len /* length of errmsg_out (EXCLUDING sentinel) */
)
#if defined(OS_POSIX)
{
int fds[3];
int i;
struct fdinfo *fdi;
int piperw[2];
int errpipe[2]; /* pipe for returning error status */
int flags;
int en; /* saved errno */
int count;
pid_t pid;
errmsg_out[errmsg_len] = '\0';
for (i=0; i<3; ++i)
pipe_ends_out[i] = NULL;
/* Manage stdin/stdout/stderr */
for (i=0; i<3; ++i) {
fdi = &fdinfo[i];
switch (fdi->mode) {
case FDMODE_INHERIT:
inherit:
fds[i] = dup(i);
if (fds[i] == -1) {
fd_failure:
strncpy(errmsg_out, strerror(errno), errmsg_len + 1);
closefds(fds, i);
closefiles(pipe_ends_out, i);
return -1;
}
break;
case FDMODE_FILENAME:
if (i == STDIN_FILENO) {
if ((fds[i] = open(fdi->info.filename, O_RDONLY)) == -1) goto fd_failure;
} else {
if ((fds[i] = creat(fdi->info.filename, 0666)) == -1) goto fd_failure;
}
break;
case FDMODE_FILEDES:
if ((fds[i] = dup(fdi->info.filedes)) == -1) goto fd_failure;
break;
case FDMODE_FILEOBJ:
if ((fds[i] = dup(fileno(fdi->info.fileobj))) == -1) goto fd_failure;
break;
case FDMODE_PIPE:
if (pipe(piperw) == -1) goto fd_failure;
if (i == STDIN_FILENO) {
fds[i] = piperw[0]; /* give read end to process */
if ((pipe_ends_out[i] = fdopen(piperw[1], "w")) == NULL) goto fd_failure;
} else {
fds[i] = piperw[1]; /* give write end to process */
if ((pipe_ends_out[i] = fdopen(piperw[0], "r")) == NULL) goto fd_failure;
}
break;
case FDMODE_STDOUT:
if (i == STDERR_FILENO) {
if ((fds[STDERR_FILENO] = dup(fds[STDOUT_FILENO])) == -1) goto fd_failure;
} else goto inherit;
break;
}
}
/* Find executable name */
if (!executable) {
/* use first arg */
executable = args[0];
}
assert(executable != NULL);
/* Create a pipe for returning error status */
if (pipe(errpipe) == -1) {
strncpy(errmsg_out, strerror(errno), errmsg_len + 1);
closefds(fds, 3);
closefiles(pipe_ends_out, 3);
return -1;
}
/* Make write end close on exec */
flags = fcntl(errpipe[1], F_GETFD);
if (flags == -1) {
pipe_failure:
strncpy(errmsg_out, strerror(errno), errmsg_len + 1);
closefds(errpipe, 2);
closefds(fds, 3);
closefiles(pipe_ends_out, 3);
return -1;
}
//.........这里部分代码省略.........
示例10: main
int main(int argc, char** argv) {
int numPlayers;
char s[BUFFSIZE];
char s2[10];
int win[MAX_PLAYERS];
int player;
int seed;
int result;
int i;
success = 1;
if ((rfs = fopen(GAME_RESULTS, "r")) == NULL) {
printf("could not open %s\n", GAME_RESULTS);
exit(1);
}
if ((wfd = creat(ALT_GAME_RESULTS, PERMISSIONS)) == -1) {
printf("could not open %s\n", ALT_GAME_RESULTS);
exit(1);
}
// setup
fgets(s, BUFFSIZE, rfs);
write(wfd, s, strlen(s));
numPlayers = (int) strtol(s, (char**) NULL, 10);
fgets(s, BUFFSIZE, rfs);
write(wfd, s, strlen(s));
seed = (int) strtol(s, (char**) NULL, 10);
fgets(s, BUFFSIZE, rfs);
write(wfd, s, strlen(s));
k[0] = (int) strtol(strtok(s, ","), (char**) NULL, 10);
for (i = 1; i < 10; i++) {
k[i] = (int) strtol(strtok(NULL, ","), (char**) NULL, 10);
}
result = initializeGame(numPlayers, k, seed, &gs);
if (result == -1) {
printf("initializeGame returned -1\n");
exit(1);
}
// play game
player = 0;
while(1) {
if (gs.whoseTurn != player) {
success = 0;
printf("wrong turn, expected %d, found %d\n",
player, gs.whoseTurn);
player = gs.whoseTurn;
}
fgets(s, BUFFSIZE, rfs);
if (s[0] == '*')
break;
sprintf(s, "> player %d:\n", player);
write(wfd, s, strlen(s));
act(s);
write(wfd, "a\n", 2);
buy(s);
write(wfd, "b\n", 2);
if (isGameOver(&gs))
break;
if (gs.outpostPlayed) {
if (gs.whoseTurn != player) {
success = 0;
printf("wrong turn after outpost, expected %d, found %d\n",
player, gs.whoseTurn);
player = gs.whoseTurn;
}
fgets(s, BUFFSIZE, rfs);
if (s[0] == '*')
break;
sprintf(s, "> outpost:\n");
write(wfd, s, strlen(s));
act(s);
write(wfd, "a\n", 2);
buy(s);
write(wfd, "b\n", 2);
if (isGameOver(&gs))
break;
}
endTurn(&gs);
player = (player + 1) % numPlayers;
}
// end game
if (isGameOver(&gs)) {
getWinners(win, &gs);
sprintf(s, "* ");
} else {
getWinners(win, &gs);
sprintf(s, "game is not over ");
}
for (i = 0; i < gs.numPlayers; i++) {
if (win[i]) {
sprintf(s2, "%d,", i);
strcat(s, s2);
}
}
s[strlen(s)-1] = '\n';
write(wfd, s, strlen(s));
fclose(rfs);
//.........这里部分代码省略.........
示例11: main
int
main (void)
{
int result1; /* Skip because of no symlink support. */
int result2; /* Skip because of no lutimens support. */
int fd;
/* Clean up any trash from prior testsuite runs. */
ignore_value (system ("rm -rf " BASE "*"));
/* Test behaviour for invalid file descriptors. */
{
errno = 0;
ASSERT (utimensat (-1, "foo", NULL, 0) == -1);
ASSERT (errno == EBADF);
}
{
close (99);
errno = 0;
ASSERT (utimensat (99, "foo", NULL, 0) == -1);
ASSERT (errno == EBADF);
}
/* Basic tests. */
result1 = test_utimens (do_utimensat, true);
result2 = test_lutimens (do_lutimensat, result1 == 0);
dfd = open (".", O_RDONLY);
ASSERT (0 <= dfd);
ASSERT (test_utimens (do_utimensat, false) == result1);
ASSERT (test_lutimens (do_lutimensat, false) == result2);
/* We expect 0/0, 0/77, or 77/77, but not 77/0. */
ASSERT (result1 <= result2);
/* Directory-relative tests. */
ASSERT (mkdir (BASE "dir", 0700) == 0);
ASSERT (chdir (BASE "dir") == 0);
fd = creat ("file", 0600);
ASSERT (0 <= fd);
errno = 0;
ASSERT (utimensat (fd, ".", NULL, 0) == -1);
ASSERT (errno == ENOTDIR);
{
struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
struct stat st;
ASSERT (utimensat (dfd, BASE "dir/file", ts, AT_SYMLINK_NOFOLLOW) == 0);
ASSERT (stat ("file", &st) == 0);
ASSERT (st.st_atime == Y2K);
ASSERT (get_stat_atime_ns (&st) == 0);
ASSERT (st.st_mtime == Y2K);
ASSERT (get_stat_mtime_ns (&st) == 0);
}
ASSERT (close (fd) == 0);
ASSERT (close (dfd) == 0);
errno = 0;
ASSERT (utimensat (dfd, ".", NULL, 0) == -1);
ASSERT (errno == EBADF);
/* Cleanup. */
ASSERT (chdir ("..") == 0);
ASSERT (unlink (BASE "dir/file") == 0);
ASSERT (rmdir (BASE "dir") == 0);
return result1 | result2;
}
示例12: readBinary
int readBinary(const char *inputfile1, const char *inputfile2, const char *outputfile) {
char product[52];
float weight;
int reference, stock;
int nlines1=0, nlines2=0;
int i=0;
char line [86];
int fh1;
int fh2;
int fd;
// reads lines and records of inputfile1 and saves them to an array of records
if((fh1 = open(inputfile1,O_RDONLY)) < 0) {
perror("Error opening binary file");
return 1;
} else {
Record rec;
while(read(fh1, &rec, sizeof(Record))> 0) {
nlines1++;
}
close(fh1);
}
Record recs1[nlines1];
if((fh1 = open(inputfile1,O_RDONLY)) < 0) {
perror("Error opening binary file");
return 1;
} else {
Record rec;
while(read(fh1, &recs1[i], sizeof(Record))>0) {
i++;
}
}
close(fh1);
// reads lines and records of inputfile1 and saves them to an array of records
if((fh2 = open(inputfile2,O_RDONLY)) < 0) {
perror("Error opening binary file");
return 1;
} else {
Record rec;
while(read(fh2, &rec, sizeof(Record))> 0) {
nlines2++;
}
close(fh2);
}
Record recs2[nlines2];
i=0;
if((fh2 = open(inputfile2,O_RDONLY)) < 0) {
perror("Error opening binary file");
return 1;
} else {
Record rec;
while(read(fh2, &recs2[i], sizeof(Record))>0) {
i++;
}
}
close(fh2);
// create binary file
if((fd = creat(outputfile,0666)) < 0) {
perror("Error when creating binary file");
return 1;
}
recMergeBatch(recs1, recs2, nlines1, nlines2, fd);
close(fd);
return 0;
}
示例13: test22a
void test22a()
{
int fd1, fd2;
int i, oldmask;
int stat_loc; /* For the wait sys call. */
subtest = 1;
system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
System("rm -rf ../DIR_22/*");
oldmask = 0123; /* Set oldmask and umask. */
umask(oldmask); /* Set oldmask and umask. */
/* Check all the possible values of umask. */
for (i = 0000; i <= 0777; i++) {
if (oldmask != umask(i)) e(1); /* set umask() */
fd1 = open("open", O_CREAT, 0777);
if (fd1 != 3) e(2); /* test open(), */
fd2 = creat("creat", 0777);
if (fd2 != 4) e(3); /* creat(), */
if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
if (umode("open") != i) e(6); /* see if they have */
if (umode("creat") != i) e(7); /* the proper mode */
if (umode("dir") != i) e(8);
if (umode("fifo") != i) e(9);
/* Clean up */
if (close(fd1) != 0) e(10);
if (close(fd2) != 0) e(11); /* close fd's and */
unlink("open"); /* clean the dir */
unlink("creat");
rmdir("dir");
unlink("fifo");
oldmask = i; /* save current mask */
}
/* Check-reset mask */
if (umask(0124) != 0777) e(12);
/* Check if a umask of 0000 leaves the modes alone. */
if (umask(0000) != 0124) e(13);
for (i = 0000; i <= 0777; i++) {
fd1 = open("open", O_CREAT, i);
if (fd1 != 3) e(14); /* test open(), */
fd2 = creat("creat", i);
if (fd2 != 4) e(15); /* creat(), */
if (mkdir("dir", i) != 0) e(16); /* mkdir(), */
if (mkfifo("fifo", i) != 0) e(17); /* and mkfifo(). */
if (mode("open") != i) e(18); /* see if they have */
if (mode("creat") != i) e(19); /* the proper mode */
if (mode("dir") != i) e(20);
if (mode("fifo") != i) e(21);
/* Clean up */
if (close(fd1) != 0) e(22);
if (close(fd2) != 0) e(23);
if (unlink("open") != 0) e(24);
unlink("creat");
rmdir("dir");
unlink("fifo");
}
/* Check if umask survives a fork() */
if (umask(0124) != 0000) e(25);
switch (fork()) {
case -1: fprintf(stderr, "Can't fork\n"); break;
case 0:
mkdir("bar", 0777); /* child makes a dir */
exit(0);
default:
if (wait(&stat_loc) == -1) e(26);
}
if (umode("bar") != 0124) e(27);
rmdir("bar");
/* Check if umask in child changes umask in parent. */
switch (fork()) {
case -1: fprintf(stderr, "Can't fork\n"); break;
case 0:
switch (fork()) {
case -1:
fprintf(stderr, "Can't fork\n");
break;
case 0:
if (umask(0432) != 0124) e(28);
exit(0);
default:
if (wait(&stat_loc) == -1) e(29);
}
if (umask(0423) != 0124) e(30);
exit(0);
default:
if (wait(&stat_loc) == -1) e(31);
}
if (umask(0342) != 0124) e(32);
//.........这里部分代码省略.........
示例14: generate_sockets
void generate_sockets(void)
{
struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
};
int fd, n;
int cachefile;
unsigned int nr_to_create = NR_SOCKET_FDS;
unsigned long domain, type, protocol;
unsigned int buffer[3];
cachefile = creat(cachefilename, S_IWUSR|S_IRUSR);
if (cachefile < 0) {
printf("Couldn't open cachefile for writing! (%s)\n",
strerror(errno));
exit(EXIT_FAILURE);
}
if (verbose)
output(2, "taking writer lock for cachefile\n");
fl.l_pid = getpid();
fl.l_type = F_WRLCK;
if (fcntl(cachefile, F_SETLKW, &fl) == -1) {
perror("fcntl F_WRLCK F_SETLKW");
exit(EXIT_FAILURE);
}
if (verbose)
output(2, "took writer lock for cachefile\n");
while (nr_to_create > 0) {
if (shm->exit_reason != STILL_RUNNING)
return;
/* Pretend we're child 0 and we've called sys_socket */
sanitise_socket(0);
//FIXME: If we passed a specific domain, we want to sanitise
// the proto/type fields. Split it out of sanitise_socket()
if (do_specific_proto == TRUE)
domain = specific_proto;
else
domain = shm->a1[0];
type = shm->a2[0];
protocol = shm->a3[0];
fd = open_socket(domain, type, protocol);
if (fd > -1) {
nr_to_create--;
buffer[0] = domain;
buffer[1] = type;
buffer[2] = protocol;
n = write(cachefile, &buffer, sizeof(int) * 3);
if (n == -1) {
printf("something went wrong writing the cachefile!\n");
exit(EXIT_FAILURE);
}
if (nr_to_create == 0)
goto done;
}
/* check for ctrl-c */
if (shm->exit_reason != STILL_RUNNING)
return;
//FIXME: If we've passed -P and we're spinning here without making progress
// then we should abort after a few hundred loops.
}
done:
fl.l_type = F_UNLCK;
if (fcntl(cachefile, F_SETLK, &fl) == -1) {
perror("fcntl F_SETLK");
exit(1);
}
if (verbose)
output(2, "dropped writer lock for cachefile\n");
output(1, "created %d sockets\n", nr_sockets);
close(cachefile);
}
示例15: CheckPromises
int CheckPromises(enum cfagenttype ag)
{ char cmd[CF_BUFSIZE], cfpromises[CF_MAXVARSIZE];
char filename[CF_MAXVARSIZE];
struct stat sb;
int fd;
if ((ag != cf_agent) && (ag != cf_executor) && (ag != cf_server))
{
return true;
}
CfOut(cf_verbose,""," -> Verifying the syntax of the inputs...\n");
snprintf(cfpromises,sizeof(cfpromises),"%s%cbin%ccf-promises%s",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR,EXEC_SUFFIX);
if (cfstat(cfpromises,&sb) == -1)
{
CfOut(cf_error,"","cf-promises%s needs to be installed in %s%cbin for pre-validation of full configuration",EXEC_SUFFIX,CFWORKDIR,FILE_SEPARATOR);
return false;
}
/* If we are cf-agent, check syntax before attempting to run */
snprintf(cmd, sizeof(cmd), "\"%s\" -f \"", cfpromises);
if (IsFileOutsideDefaultRepository(VINPUTFILE))
{
strlcat(cmd, VINPUTFILE, CF_BUFSIZE);
}
else
{
strlcat(cmd, CFWORKDIR, CF_BUFSIZE);
strlcat(cmd, FILE_SEPARATOR_STR "inputs" FILE_SEPARATOR_STR, CF_BUFSIZE);
strlcat(cmd, VINPUTFILE, CF_BUFSIZE);
}
strlcat(cmd, "\"", CF_BUFSIZE);
if (CBUNDLESEQUENCE)
{
strlcat(cmd, " -b \"", CF_BUFSIZE);
strlcat(cmd, CBUNDLESEQUENCE_STR, CF_BUFSIZE);
strlcat(cmd, "\"", CF_BUFSIZE);
}
if(BOOTSTRAP)
{
// avoids license complains from commercial cf-promises during bootstrap - see Nova_CheckLicensePromise
strlcat(cmd, " -D bootstrap_mode", CF_BUFSIZE);
}
/* Check if reloading policy will succeed */
CfOut(cf_verbose, "", "Checking policy with command \"%s\"", cmd);
if (ShellCommandReturnsZero(cmd,true))
{
if (MINUSF)
{
snprintf(filename,CF_MAXVARSIZE,"%s/state/validated_%s",CFWORKDIR,CanonifyName(VINPUTFILE));
MapName(filename);
}
else
{
snprintf(filename,CF_MAXVARSIZE,"%s/masterfiles/cf_promises_validated",CFWORKDIR);
MapName(filename);
}
MakeParentDirectory(filename,true);
if ((fd = creat(filename,0600)) != -1)
{
close(fd);
CfOut(cf_verbose,""," -> Caching the state of validation\n");
}
else
{
CfOut(cf_verbose,"creat"," -> Failed to cache the state of validation\n");
}
return true;
}
else
{
return false;
}
}