static void start(Test *t) { t->fd = tmpfd(); strcpy(t->dir, TmpDirPat); mktemp(t->dir); t->pid = fork(); if (t->pid < 0) { die(1, errno, "fork"); } else if (!t->pid) { setpgid(0, 0); if (dup2(t->fd, 1) == -1) { die(3, errno, "dup2"); } if (close(t->fd) == -1) { die(3, errno, "fclose"); } if (dup2(1, 2) == -1) { die(3, errno, "dup2"); } curdir = t->dir; t->f(); _exit(0); } setpgid(t->pid, t->pid); }
void runSuccess() { char buf[] = "temp"; //@ assert \valid(buf+(0..\block_length(buf)-1)); //@ assert \exists integer x; buf[x] == '\0'; mktemp(buf); }
int main(int argc, string argv[]) { string *mdtab, *names, *exprs, prog; int nexp = 0, i, j; initparam(argv, defv); mdtab = getmapdefs(); // get list of mapping vars for (i = 0; mdtab[i] != NULL; i += 2) if (getparamstat(mdtab[i]) & ARGPARAM) // if var has assigned value nexp++; eprintf("[%s: %scounted %d variable assignments]\n", getprog(), nexp > 0 ? "" : "warning: ", nexp); names = (string *) allocate(sizeof(string *) * (nexp + 1)); exprs = (string *) allocate(sizeof(string *) * (nexp + 1)); for (i = j = 0; mdtab[i] != NULL; i += 2) if (getparamstat(mdtab[i]) & ARGPARAM) { // if var has assigned value exprs[j] = getparam(mdtab[i]); // list value given as expr names[j] = mdtab[i+1]; // and name of access macro j++; } exprs[j] = names[j] = NULL; prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, NULL, strnull(getparam("t")) ? NULL : getparam("t"), Precision, NDIM, TRUE); execmap(prog); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); return (0); }
char *tempnam( char *dir, char *pfx ) { char *s; if ( dir == NULL ) { dir = "/tmp"; } /* * allocate space for dir + '/' + pfx (up to 5 chars) + 6 trailing 'X's + 0 byte */ if (( s = (char *)slapi_ch_malloc( strlen( dir ) + 14 )) == NULL ) { return( NULL ); } strcpy( s, dir ); strcat( s, "/" ); if ( pfx != NULL ) { strcat( s, pfx ); } strcat( s, "XXXXXX" ); mktemp( s ); if ( *s == '\0' ) { slapi_ch_free( (void**)&s ); } return( s ); }
select_file_name (char * fname) { next_file_num++; /* advance counter */ sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num); mktemp(fname); /* make sure file name is unique */ /* mktemp replaces the trailing XXXXXX with a unique string of characters */ }
void CRYPTO_thread_setup(void) { int i; char filename[20]; lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); if (!lock_cs) { /* Nothing we can do about this...void function! */ return; } strcpy(filename, "/tmp/mttest.XXXXXX"); mktemp(filename); usconfig(CONF_STHREADIOOFF); usconfig(CONF_STHREADMALLOCOFF); usconfig(CONF_INITUSERS, 100); usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); arena = usinit(filename); unlink(filename); for (i = 0; i < CRYPTO_num_locks(); i++) { lock_cs[i] = usnewsema(arena, 1); } CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id); CRYPTO_set_locking_callback((void (*)())irix_locking_callback); }
/* Replace all file names in a list with temporary files with the same * name extension as the original name. */ static void m2c_replace_temp_file_name(struct list_t *file_list) { int index; char *name; char *name_ext; char new_name[MAX_STRING_SIZE]; LIST_FOR_EACH(file_list, index) { /* Get old name and its extension */ name = list_get(file_list, index); name_ext = rindex(name, '.'); if (!name_ext || rindex(name_ext, '/')) name_ext = ""; /* Create new file */ strcpy(new_name, "/tmp/XXXXXX"); mktemp(new_name); if (name_ext) strcat(new_name, name_ext); /* Replace it */ free(name); list_set(file_list, index, xstrdup(new_name)); }
int main(int argc, char **argv) { char *temp; struct stat fileExists; if(argc < 2) temp = strcat(argv[0], "XXXXXX"); else temp = strcat(argv[1], "XXXXXX"); char * file = mktemp(temp); if(fopen(file, "w+") != NULL) { fprintf(stdout, "%s\n", file); } else { perror("Error: "); return errno; } int result = stat(file, &fileExists); if(result == 0) fprintf(stdout, "File exists\n"); else fprintf(stdout, "File does not exist\n"); return 0; }
/* check if directory is writable */ boolean directory_is_writable (char *path) { FILE *f; char *s; int fd; #if defined(_WIN32) s = concat_strings(path, "\\ctm.XXXXXX"); /* mingw doesn't have mkstemp */ s = mktemp(s); f = fopen(s, "w"); #else s = concat_strings(path, "/ctm.XXXXXX"); fd = mkstemp(s); if (fd == -1) return FALSE; f = fdopen(fd, "w"); #endif if (f == NULL) { return FALSE; } else { fclose (f); unlink(s); return TRUE; } }
/* Convert BSDF (MGF) geometry to Radiance description */ static int geomBSDF(const SDData *bsp) { char tmpfile[64]; char command[SDnameLn+64]; int fd; /* write MGF to temp file */ fd = open(mktemp(strcpy(tmpfile,TEMPLATE)), O_WRONLY|O_CREAT|O_EXCL, 0600); if (fd < 0) { fprintf(stderr, "Cannot open temp file '%s'\n", tmpfile); return(0); } (void)write(fd, bsp->mgf, strlen(bsp->mgf)); close(fd); /* set up command */ if (do_instance) { sprintf(command, "mgf2rad %s | oconv -f - > %s.oct", tmpfile, bsp->name); } else { fflush(stdout); sprintf(command, "mgf2rad %s", tmpfile); } if (system(command)) { fprintf(stderr, "Error running: %s\n", command); return(0); } unlink(tmpfile); /* remove temp file */ if (do_instance) { /* need to output instance? */ printf("void instance %s_g\n", bsp->name); printf("1 %s.oct\n0\n0\n\n", bsp->name); } return(1); }
/* Preparation. */ void do_prepare (int argc, char *argv[]) { size_t test_dir_len; test_dir_len = strlen (test_dir); /* Generate the circular symlinks. */ name1 = malloc (test_dir_len + sizeof ("/canonXXXXXX")); mempcpy (mempcpy (name1, test_dir, test_dir_len), "/canonXXXXXX", sizeof ("/canonXXXXXX")); name2 = strdup (name1); add_temp_file (mktemp (name1)); add_temp_file (mktemp (name2)); }
int main(int argc, string argv[]) { string prog, itags[MaxBodyFields]; stream xstr; int nprof, nbody, nsamp; real *prof1, *prof2, rrange[2], tnow; bodyptr btab = NULL; initparam(argv, defv); layout_body(fields, Precision, NDIM); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); xstr = execmap(prog); get_history(xstr); nprof = getiparam("nprof"); prof1 = (real *) allocate((2 + nprof) * sizeof(real)); prof2 = (real *) allocate((2 + nprof) * sizeof(real)); setrange(rrange, getparam("rrange")); nsamp = 0; while (get_snap(xstr, &btab, &nbody, &tnow, itags, FALSE)) { setprofile(prof1, prof2, nprof, rrange, btab, nbody); nsamp++; } if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); if (nsamp == 0) error("%s: no data in input\n", getargv0()); listdensity(prof1, prof2, nprof, rrange, nsamp); return (0); }
static inline int gg_mkstemp(char *path) { mode_t old_umask, file_mask; int ret; file_mask = S_IRWXO | S_IRWXG; old_umask = umask(file_mask); #ifdef HAVE_MKSTEMP ret = mkstemp(path); #else #ifdef _WIN32 if (_mktemp_s(path, strlen(path) + 1) != 0) #else /* coverity[secure_temp : FALSE] * * mktemp may be unsafe, because it creates files with predictable * names, but it's not a real problem for automatic tests. */ if (strcmp(mktemp(path), "") == 0) #endif ret = -1; else ret = open(path, O_EXCL | O_RDWR | O_CREAT, file_mask); #endif umask(old_umask); return ret; }
int main(int argc, string argv[]) { string prog, itags[MaxBodyFields], otags[MaxBodyFields]; stream xstr, ostr; bodyptr btab = NULL; int nbody; real tnow; initparam(argv, defv); exprs[0] = getparam("weight"); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, types, NULL, Precision, NDIM, TRUE); xstr = execmap(prog); if (get_tag_ok(xstr, "History")) skip_item(xstr); get_history(xstr); ostr = stropen(getparam("out"), "w"); put_history(ostr); new_field(&WeightField, RealType, "Weight"); new_field(&WeightField + 1, NULL, NULL); while (get_snap(xstr, &btab, &nbody, &tnow, itags, TRUE)) { snaprect(btab, nbody); del_tag(otags, itags, "Weight"); put_snap(ostr, &btab, &nbody, &tnow, otags); } strclose(ostr); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); return (0); }
/** * Creates a new C source file and adds a standard header. If filename is * empty, a unique random name is produced and used. */ void create_src_file(std::string& filename, std::ofstream& ofs) { if (filename.empty()) { // fill filename with unique random word const char* filename_pattern = "./GiNaCXXXXXX"; char* new_filename = new char[strlen(filename_pattern)+1]; strcpy(new_filename, filename_pattern); if (!mktemp(new_filename)) { delete[] new_filename; throw std::runtime_error("mktemp failed"); } filename = std::string(new_filename); ofs.open(new_filename, std::ios::out); delete[] new_filename; } else { // use parameter as filename ofs.open(filename.c_str(), std::ios::out); } if (!ofs) { throw std::runtime_error("could not create source code file for compilation"); } ofs << "#include <stddef.h> " << std::endl; ofs << "#include <stdlib.h> " << std::endl; ofs << "#include <math.h> " << std::endl; ofs << std::endl; }
/* create an empty temporary file and return its name */ static char *scratch_file(void) { FILE *f; char *temp; char *path = malloc(256); if (path == NULL) return NULL; if ( (temp = getenv("TEMP")) != NULL ) strcpy(path, temp); else if ( (temp = getenv("TMP")) != NULL ) strcpy(path, temp); else strcpy(path, "c:\\"); /* Prevent X's in path from being converted by mktemp. */ for ( temp = path; *temp; temp++ ) { *temp = (char)tolower(*temp); if (*temp == '/') *temp = '\\'; } if ( strlen(path) && (path[strlen(path)-1] != '\\') ) strcat(path, "\\"); strcat(path, "ptXXXXXX"); mktemp(path); f = fopen(path, "w"); if (f==NULL) {perror(cmd); exit(1);} fclose(f); return path; }
//create temp file void RCommitLog::createTempLog() { std::string tempdir; #ifdef _WIN32 DWORD tmplen = GetTempPath(0, 0); if(tmplen == 0) return; std::vector<TCHAR> temp(tmplen+1); tmplen = GetTempPath(static_cast<DWORD>(temp.size()), &temp[0]); if(tmplen == 0 || tmplen >= temp.size()) return; tempdir = std::string(temp.begin(), temp.begin() + static_cast<std::size_t>(tmplen)); tempdir += "\\"; #else tempdir = "/tmp/"; #endif char tmplate[1024]; snprintf(tmplate, 1024, "%sgource-XXXXXX", tempdir.c_str()); #ifdef _WIN32 if(mktemp(tmplate) < 0) return; #else if(mkstemp(tmplate) < 0) return; #endif temp_file = std::string(tmplate); }
static char * unique_filename(const char *path, const char *prefix, int *pFd) #endif { #ifndef HAVE_MKSTEMP #ifndef X_NOT_POSIX return ((char *) tempnam (path, prefix)); #else char tempFile[PATH_MAX]; char *tmp; snprintf (tempFile, sizeof(tempFile), "%s/%sXXXXXX", path, prefix); tmp = (char *) mktemp (tempFile); if (tmp) { return strdup(tmp); } else return (NULL); #endif #else char tempFile[PATH_MAX]; char *ptr; snprintf (tempFile, sizeof(tempFile), "%s/%sXXXXXX", path, prefix); ptr = strdup(tempFile); if (ptr != NULL) { *pFd = mkstemp(ptr); } return ptr; #endif }
static int do_test (int argc, char *argv[]) { int retval; if (mktemp (tmpname) == NULL) { perror ("mktemp"); return 1; } /* Try to generate a FIFO. */ if (mknod (tmpname, 0600 | S_IFIFO, 0) < 0) { perror ("mknod"); /* We cannot make this an error. */ return 0; } retval = real_test (); remove (tmpname); return retval; }
/** * librdf_files_temporary_file_name: * * Create a temporary file name. * * @deprecated: Do not use this, it is unsafe. * * Return value: a new filename or NULL on failure. **/ char * librdf_files_temporary_file_name(void) { #if defined(HAVE_MKSTEMP) || defined(HAVE_MKTEMP) const char *tmp_dir; size_t length; char *name; static const char * const file_template="librdf_tmp_XXXXXX"; /* FIXME */ #ifdef HAVE_MKSTEMP int fd; #endif /* FIXME: unix dependencies */ tmp_dir=getenv("TMPDIR"); if(!tmp_dir) tmp_dir="/tmp"; length=strlen(tmp_dir) + strlen(file_template) + 2; /* 2: / sep and \/0 */ name = LIBRDF_MALLOC(char*, length); if(!name) return NULL; /* FIXME: unix dependency - file/dir separator */ sprintf(name, "%s/%s", tmp_dir, file_template); #ifdef HAVE_MKSTEMP /* Proritise mkstemp() since GNU libc says: Never use mktemp(). */ fd=mkstemp(name); if(fd<0) { LIBRDF_FREE(char*, name); return NULL; } close(fd); unlink(name); return name; #else return mktemp(name); #endif #else #ifdef HAVE_TMPNAM /* GNU libc says: Never use this function. Use mkstemp(3) instead. */ char *name; char *new_name; name=tmpnam(NULL); /* NULL ensures statically allocated */ new_name = LIBRDF_MALLOC(char*, strlen(name) + 1); if(!new_name) return NULL; strcpy(new_name, name); return name; #else /* not tmpnam(), mkstemp() or mktemp() */ HELP #endif #endif }
startnlfile() { nlsize = 0; (void) mktemp(symname); (void) mktemp(strname); (void) mktemp(filesname); (void) mktemp(linesname); symfp = fopen(symname, "w"); strfp = fopen(strname, "w"); filesfp = fopen(filesname, "w"); linesfp = fopen(linesname, "w"); if (symfp==NULL || strfp==NULL || filesfp==NULL || linesfp==NULL) { fprintf(stderr, "can't create /tmp/obj"); pexit(NOSTART); } newfile(filename, 1); }
char* maketmp(void) { static char temp[] = "/tmp/mkargXXXXXX"; mktemp(temp); return temp; }
int mkstemp(char *tmpl) { int ret=-1; mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE); return ret; }
int main() { tmpname = mktemp("T:flockXXXXXX"); int fd = open(tmpname, O_CREAT); TEST((fd != -1)); TEST((flock(fd, LOCK_SH|LOCK_NB) == 0)); TEST((flock(fd, LOCK_UN) == 0)); TEST((flock(fd, LOCK_EX|LOCK_NB) == 0)); TEST((flock(fd, LOCK_UN) == 0)); TEST((flock(fd, LOCK_SH) == 0)); TEST((flock(fd, LOCK_UN) == 0)); TEST((flock(fd, LOCK_EX) == 0)); TEST((flock(fd, LOCK_UN) == 0)); close(fd); /* Create NPROCS processes increasing counter ITERATIONS times in an ugly way */ int counter = 0; struct Process *procs[NPROCS]; ULONG ids[NPROCS]; struct TagItem tags[] = { { NP_Entry, (IPTR) entry }, { NP_Name, (IPTR) "flocker" }, { NP_Output, (IPTR) Output() }, { NP_CloseOutput, (IPTR) FALSE }, { NP_UserData, (IPTR) &counter }, { NP_NotifyOnDeath, (IPTR) TRUE }, { TAG_DONE, 0 } }; int i; InitSemaphore(&sem); for(i = 0; i < NPROCS; i++) { procs[i] = CreateNewProc(tags); TEST((procs[i])); ids[i] = GetETask(procs[i])->et_UniqueID; Signal((struct Task *)procs[i], SIGBREAKF_CTRL_C); } for(i = 0; i < NPROCS; i++) { ChildWait(ids[i]); ChildFree(ids[i]); } putchar('\n'); TEST((counter == NPROCS * ITERATIONS)); cleanup(); return OK; }
int main(int argc, char **argv) { FILE *fp; char *tmp_fname; char tmp_msg[PATH_MAX]; int rc=-1; strncpy(fname, "/tmp/std_config_node_text_XXXXX", sizeof(fname)); fname[sizeof(fname)-1] = 0; tmp_fname = mktemp(fname); if ((tmp_fname == NULL) || (strlen(tmp_fname) == 0)) { perror("Unable to create temporary xml file: "); return -1; } fp=fopen(tmp_fname,"w+"); if (fp == NULL) { perror("Unable to create a temporary file: "); return -1; } rc=generate_xmlfile(fp); if (rc != 0) { if( fclose (fp) != 0) { snprintf(tmp_msg,PATH_MAX,"Unable to close the temporary file created:%s",tmp_fname); perror(tmp_msg); } rc = unlink(tmp_fname); if(rc != 0) { snprintf(tmp_msg,PATH_MAX,"Unable to delete the temporary file created:%s",tmp_fname); perror(tmp_msg); } return -1; } ::testing::InitGoogleTest(&argc, argv); rc=RUN_ALL_TESTS(); if( fclose (fp) != 0) { snprintf(tmp_msg,PATH_MAX,"Unable to close the temporary file created:%s",tmp_fname); perror(tmp_msg); } rc = unlink(tmp_fname); if(rc != 0) { snprintf(tmp_msg,PATH_MAX,"Unable to delete the temporary file created:%s",tmp_fname); perror(tmp_msg); } return rc; }
int mktemp_main(int argc UNUSED_PARAM, char **argv) { const char *path; char *chp; unsigned opts; enum { OPT_d = 1 << 0, OPT_q = 1 << 1, OPT_t = 1 << 2, OPT_p = 1 << 3, OPT_u = 1 << 4, }; path = getenv("TMPDIR"); if (!path || path[0] == '\0') path = "/tmp"; opt_complementary = "?1"; /* 1 argument max */ opts = getopt32(argv, "dqtp:u", &path); chp = argv[optind]; if (!chp) { /* GNU coreutils 8.4: * bare "mktemp" -> "mktemp -t tmp.XXXXXX" */ chp = xstrdup("tmp.XXXXXX"); opts |= OPT_t; } #if 0 /* Don't allow directory separator in template */ if ((opts & OPT_t) && bb_basename(chp) != chp) { errno = EINVAL; goto error; } #endif if (opts & (OPT_t|OPT_p)) chp = concat_path_file(path, chp); if (opts & OPT_u) { chp = mktemp(chp); if (chp[0] == '\0') goto error; } else if (opts & OPT_d) { if (mkdtemp(chp) == NULL) goto error; } else { if (mkstemp(chp) < 0) goto error; } puts(chp); return EXIT_SUCCESS; error: if (opts & OPT_q) return EXIT_FAILURE; /* don't use chp as it gets mangled in case of error */ bb_perror_nomsg_and_die(); }
int download_file(const char * dst, const char * src, const char * md5) { if (!(*src) || !(*dst)) { return NODE_ERR_REQ_DATA; } char temp_file[PATH_MAX] = {0}; snprintf(temp_file, PATH_MAX, "%sXXXXXX", dst); mktemp(temp_file); g_http_transfer.download_file(src, temp_file); DEBUG_LOG("downloaded %s to %s", src, dst); if (NULL != md5) { char temp_md5[MD5_LEN + 1] = {0}; if (0 != get_file_md5(temp_file, temp_md5)) { ERROR_LOG("download file %s to %s failed", src, dst); return NODE_ERR_DOWNLOAD_FAIL; } if (0 != strncmp(md5, temp_md5, MD5_LEN)) { ERROR_LOG("download file %s to %s, \noriginal md5: %s, actual md5: %s, mismatch", src, dst, md5, temp_md5); remove(temp_file); return NODE_ERR_MD5_MISMATCH; } } int ret = rename(temp_file, dst); if (0 != ret) { remove(temp_file); if (EBUSY == errno) { return NODE_ERR_FILE_BUSY; } else if (EACCES == errno) { return NODE_ERR_ACCESS; } else { return NODE_ERR_UNKNOWN; } } return 0; }
int bx_mkstemp(char *tpl) { mktemp(tpl); return ::open(tpl, O_RDWR | O_CREAT | O_TRUNC # ifdef O_BINARY | O_BINARY # endif , S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP); }
void spool(char *path) /* Place a file into the spool directory, either by copying it, or by leaving * a reference. */ { char *file; int j, u; mktemp(jobX); file= mktemp(tmpX); if (path[0] == '/') { int f; if ((f= open(path, O_RDONLY)) >= 0) { close(f); file= path; } } if (file != path) { int c; FILE *t; if ((t= fopen(tmpX, "w")) == nil) spoolerr(tmpX); while ((c= getchar()) != EOF && putc(c, t) != EOF) {} if (ferror(stdin)) spoolerr(path); if (ferror(t) || fclose(t) == EOF) spoolerr(tmpX); fclose(stdin); } if ((j= open(jobX, O_WRONLY|O_CREAT|O_EXCL, 0000)) < 0) spoolerr(jobX); u= getuid(); if (write(j, file, strlen(file)+1) < 0 || write(j, &u, sizeof(u)) < 0 || write(j, path, strlen(path)+1) < 0 || close(j) < 0 || chmod(jobX, 0600) < 0 ) spoolerr(jobX); }
int main() { char *path1; char *path2; char *cwd; int status; path1 = mkdtemp(strdup(TEMPLATE)); if (path1 == NULL) exit(EXIT_FAILURE); status = chdir(path1); if (status < 0) exit(EXIT_FAILURE); status = mkdir(COOKIE1, 0777); if (status < 0) exit(EXIT_FAILURE); status = chdir(COOKIE1); if (status < 0) exit(EXIT_FAILURE); status = creat(COOKIE2, O_RDWR); if (status < 0) exit(EXIT_FAILURE); close(status); path2 = mktemp(strdup(TEMPLATE)); status = rename(path1, path2); if (status < 0) exit(EXIT_FAILURE); status = access(COOKIE2, F_OK); if (status < 0) exit(EXIT_FAILURE); cwd = get_current_dir_name(); if (cwd == NULL || memcmp(cwd, path2, strlen(path2)) != 0) exit(EXIT_FAILURE); status = unlink(COOKIE2); if (status < 0) exit(EXIT_FAILURE); status = rmdir(cwd); if (status < 0) exit(EXIT_FAILURE); /* TODO: if (get_current_dir_name() != NULL || errno != ENOENT) exit(EXIT_FAILURE); */ exit(EXIT_SUCCESS); }