void _verr(int fatal, int errcode, const char *fmt, ...) { time_t t; va_list ap; char buf[STR_SIZE("yyyy-mm-dd hh:mm:ss")]; struct tm *tm; t = time(NULL); tm = localtime(&t); if (0 == strftime(buf, ARRAY_SIZE(buf), "%F %T", tm)) { buf[0] = '\0'; } if (NULL == err_file) { err_file = stderr; } fprintf(err_file, "[%s] %s: ", buf, __progname); if (NULL != fmt) { va_start(ap, fmt); vfprintf(err_file, fmt, ap); va_end(ap); if (errcode) { fprintf(err_file, ": "); } } if (errcode) { fputs(strerror(errcode), err_file); } fprintf(err_file, "\n"); if (fatal) { exit(BANIPD_EXIT_FAILURE); } }
BOOL ExpEnvVars(CString& str) { // Expand possible environment variables in the specified string TCHAR tmp[BUFF_SIZE]; DWORD cnt = ExpandEnvironmentStrings(str, tmp, STR_SIZE(tmp)); str = tmp; return (cnt > 0); }
// // Split_Lines: C // // Given a string series, split lines on CR-LF. Give back array of strings. // // Note: The definition of "line" in POSIX is a sequence of characters that // end with a newline. Hence, the last line of a file should have a newline // marker, or it's not a "line") // // https://stackoverflow.com/a/729795 // // This routine does not require it. // // !!! CR support is likely to be removed...and CR will be handled as a normal // character, with special code needed to process it. // REBARR *Split_Lines(const REBVAL *str) { REBDSP dsp_orig = DSP; REBCNT len = VAL_LEN_AT(str); REBCNT i = VAL_INDEX(str); if (i == len) return Make_Array(0); DECLARE_MOLD (mo); Push_Mold(mo); REBCHR(const*) cp = VAL_STRING_AT(str); REBUNI c; cp = NEXT_CHR(&c, cp); for (; i < len; ++i, cp = NEXT_CHR(&c, cp)) { if (c != LF && c != CR) { Append_Codepoint(mo->series, c); continue; } Init_Text(DS_PUSH(), Pop_Molded_String(mo)); SET_CELL_FLAG(DS_TOP, NEWLINE_BEFORE); Push_Mold(mo); if (c == CR) { REBCHR(const*) tp = NEXT_CHR(&c, cp); if (c == LF) { ++i; cp = tp; // treat CR LF as LF, lone CR as LF } } } // If there's any remainder we pushed in the buffer, consider the end of // string to be an implicit line-break if (STR_SIZE(mo->series) == mo->offset) Drop_Mold(mo); else { Init_Text(DS_PUSH(), Pop_Molded_String(mo)); SET_CELL_FLAG(DS_TOP, NEWLINE_BEFORE); } return Pop_Stack_Values_Core(dsp_orig, ARRAY_FLAG_NEWLINE_AT_TAIL); }
// // Trim_Tail: C // // Used to trim off hanging spaces during FORM and MOLD. // void Trim_Tail(REB_MOLD *mo, REBYTE ascii) { assert(ascii < 0x80); // more work needed for multi-byte characters REBCNT len = STR_LEN(mo->series); REBSIZ size = STR_SIZE(mo->series); for (; size > 0; --size, --len) { REBYTE b = *BIN_AT(SER(mo->series), size - 1); if (b != ascii) break; } TERM_STR_LEN_SIZE(mo->series, len, size); }
DWORD RecDelRegKey(LPCTSTR pKeyName, HKEY hStartKey) { // Delete all registry keys below the one specified DWORD dwRtn, dwSubKeyLength; TCHAR szSubKey[255]; HKEY hKey; if (!hStartKey) hStartKey = gRegRootKey; if (!pKeyName) pKeyName = AppRegRoot; if (pKeyName && STR_LEN(pKeyName)) { if((dwRtn=RegOpenKeyEx(hStartKey,pKeyName, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hKey )) == ERROR_SUCCESS) { while (dwRtn == ERROR_SUCCESS) { dwSubKeyLength = STR_SIZE(szSubKey); dwRtn=RegEnumKeyEx( hKey, 0, // always index zero szSubKey, &dwSubKeyLength, NULL, NULL, NULL, NULL ); if(dwRtn == ERROR_NO_MORE_ITEMS) { dwRtn = RegDeleteKey(hStartKey, pKeyName); break; } else if (dwRtn == ERROR_SUCCESS) dwRtn = RecDelRegKey(szSubKey, hKey); } RegCloseKey(hKey); } } else dwRtn = ERROR_BADKEY; return dwRtn; }
HFSCatalogNodeID newFolder(const char* pathName, Volume* volume) { HFSPlusCatalogFolder* parentFolder; HFSPlusCatalogFolder folder; HFSPlusCatalogKey key; HFSPlusCatalogThread thread; uint32_t newFolderID; int threadLength; char* path; char* name; char* curChar; char* lastSeparator; path = strdup(pathName); curChar = path; lastSeparator = NULL; while((*curChar) != '\0') { if((*curChar) == '/') lastSeparator = curChar; curChar++; } if(lastSeparator == NULL) { parentFolder = (HFSPlusCatalogFolder*) getRecordFromPath("/", volume, NULL, NULL); name = path; } else { name = lastSeparator + 1; *lastSeparator = '\0'; parentFolder = (HFSPlusCatalogFolder*) getRecordFromPath(path, volume, NULL, NULL); } if(parentFolder == NULL || parentFolder->recordType != kHFSPlusFolderRecord) { free(path); free(parentFolder); return FALSE; } newFolderID = volume->volumeHeader->nextCatalogID++; volume->volumeHeader->folderCount++; folder.recordType = kHFSPlusFolderRecord; folder.flags = kHFSHasFolderCountMask; folder.valence = 0; folder.folderID = newFolderID; folder.createDate = UNIX_TO_APPLE_TIME(time(NULL)); folder.contentModDate = folder.createDate; folder.attributeModDate = folder.createDate; folder.accessDate = folder.createDate; folder.backupDate = folder.createDate; folder.permissions.ownerID = parentFolder->permissions.ownerID; folder.permissions.groupID = parentFolder->permissions.groupID; folder.permissions.adminFlags = 0; folder.permissions.ownerFlags = 0; folder.permissions.fileMode = S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; folder.permissions.special.iNodeNum = 0; memset(&folder.userInfo, 0, sizeof(folder.userInfo)); memset(&folder.finderInfo, 0, sizeof(folder.finderInfo)); folder.textEncoding = 0; folder.folderCount = 0; key.parentID = parentFolder->folderID; ASCIIToUnicode(name, &key.nodeName); key.keyLength = sizeof(key.parentID) + STR_SIZE(key.nodeName); thread.recordType = kHFSPlusFolderThreadRecord; thread.reserved = 0; thread.parentID = parentFolder->folderID; ASCIIToUnicode(name, &thread.nodeName); threadLength = sizeof(thread.recordType) + sizeof(thread.reserved) + sizeof(thread.parentID) + STR_SIZE(thread.nodeName); flipCatalogThread(&thread, TRUE); flipCatalogFolder(&folder); ASSERT(addToBTree(volume->catalogTree, (BTKey*)(&key), sizeof(HFSPlusCatalogFolder), (unsigned char *)(&folder)), "addToBTree"); key.nodeName.length = 0; key.parentID = newFolderID; key.keyLength = sizeof(key.parentID) + sizeof(key.nodeName.length); ASSERT(addToBTree(volume->catalogTree, (BTKey*)(&key), threadLength, (unsigned char *)(&thread)), "addToBTree"); parentFolder->folderCount++; parentFolder->valence++; updateCatalog(volume, (HFSPlusCatalogRecord*) parentFolder); updateVolume(volume); free(parentFolder); free(path); return newFolderID; }
queue_err_t queue_open(void *p, const char *name, int flags) { int id; FILE *fp; key_t key; mode_t oldmask; systemv_queue_t *q; struct msqid_ds buf; char *s, filename[MAXPATHLEN]; q = (systemv_queue_t *) p; if (HAS_FLAG(flags, QUEUE_FL_OWNER)) { if (NULL == (s = strchr(name, ':')) || '\0' == *s) { id = 'b'; if (strlcpy(filename, name, STR_SIZE(filename)) >= STR_SIZE(filename)) { errno = E2BIG; return QUEUE_ERR_GENERAL_FAILURE; } } else { id = s[1]; if (s - name >= STR_SIZE(filename)) { errno = E2BIG; return QUEUE_ERR_GENERAL_FAILURE; } strncpy(filename, name, s - name); } if (NULL == (fp = fopen(filename, "wx"))) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } if (1 != fwrite(&id, sizeof(id), 1, fp)) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } fflush(fp); } else { if (strlcpy(filename, name, STR_SIZE(filename)) >= STR_SIZE(filename)) { errno = E2BIG; return QUEUE_ERR_GENERAL_FAILURE; } if (NULL == (fp = fopen(filename, "r"))) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } if (1 != fread(&id, sizeof(id), 1, fp) < sizeof(id)) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } } fclose(fp); if (-1 == (key = ftok(filename, id))) { // NOTE: errno is not set by ftok // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } if (HAS_FLAG(flags, QUEUE_FL_OWNER)) { if (NULL == (q->filename = strdup(filename))) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } oldmask = umask(0); q->qid = msgget(key, 0660 | IPC_CREAT | IPC_EXCL); umask(oldmask); } else { q->qid = msgget(key, 0660); } if (-1 == q->qid) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } if (!HAS_FLAG(flags, QUEUE_FL_SENDER)) { #if 0 https://svnweb.freebsd.org/base/head/sys/kern/sysv_msg.c?revision=282213&view=markup #define IPCID_TO_IX(id) ((id) & 0xffff) #define IPCID_TO_SEQ(id) (((id) >> 16) & 0xffff) #define IXSEQ_TO_IPCID(ix,perm) (((perm.seq) << 16) | (ix & 0xffff)) #endif CAP_RIGHTS_LIMIT(q->qid, CAP_READ); #if 0 } else { CAP_RIGHTS_LIMIT(q->qid, CAP_WRITE); #endif } if (0 != msgctl(q->qid, IPC_STAT, &buf)) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } q->buffer_size = buf.msg_qbytes / 8; if (NULL == (q->buffer = malloc(sizeof(long) + sizeof(q->buffer) * q->buffer_size))) { // TODO: error debug(""); return QUEUE_ERR_GENERAL_FAILURE; } *(long *) q->buffer = 1; /* mtype is an integer greater than 0 */ return QUEUE_ERR_OK; }
// // MAKE_Tuple: C // REB_R MAKE_Tuple( REBVAL *out, enum Reb_Kind kind, const REBVAL *opt_parent, const REBVAL *arg ){ assert(kind == REB_TUPLE); if (opt_parent) fail (Error_Bad_Make_Parent(kind, opt_parent)); if (IS_TUPLE(arg)) return Move_Value(out, arg); RESET_CELL(out, REB_TUPLE, CELL_MASK_NONE); REBYTE *vp = VAL_TUPLE(out); // !!! Net lookup parses IP addresses out of `tcp://93.184.216.34` or // similar URL!s. In Rebol3 these captures come back the same type // as the input instead of as STRING!, which was a latent bug in the // network code of the 12-Dec-2012 release: // // https://github.com/rebol/rebol/blob/master/src/mezz/sys-ports.r#L110 // // All attempts to convert a URL!-flavored IP address failed. Taking // URL! here fixes it, though there are still open questions. // if (IS_TEXT(arg) or IS_URL(arg)) { REBSIZ size; const REBYTE *bp = Analyze_String_For_Scan(&size, arg, MAX_SCAN_TUPLE); if (Scan_Tuple(out, bp, size) == nullptr) fail (arg); return out; } if (ANY_ARRAY(arg)) { REBCNT len = 0; REBINT n; RELVAL *item = VAL_ARRAY_AT(arg); for (; NOT_END(item); ++item, ++vp, ++len) { if (len >= MAX_TUPLE) goto bad_make; if (IS_INTEGER(item)) { n = Int32(item); } else if (IS_CHAR(item)) { n = VAL_CHAR(item); } else goto bad_make; if (n > 255 || n < 0) goto bad_make; *vp = n; } VAL_TUPLE_LEN(out) = len; for (; len < MAX_TUPLE; len++) *vp++ = 0; return out; } REBCNT alen; if (IS_ISSUE(arg)) { REBSTR *spelling = VAL_STRING(arg); const REBYTE *ap = STR_HEAD(spelling); size_t size = STR_SIZE(spelling); // UTF-8 len if (size & 1) fail (arg); // must have even # of chars size /= 2; if (size > MAX_TUPLE) fail (arg); // valid even for UTF-8 VAL_TUPLE_LEN(out) = size; for (alen = 0; alen < size; alen++) { REBYTE decoded; if ((ap = Scan_Hex2(&decoded, ap)) == NULL) fail (arg); *vp++ = decoded; } } else if (IS_BINARY(arg)) { REBYTE *ap = VAL_BIN_AT(arg); REBCNT len = VAL_LEN_AT(arg); if (len > MAX_TUPLE) len = MAX_TUPLE; VAL_TUPLE_LEN(out) = len; for (alen = 0; alen < len; alen++) *vp++ = *ap++; } else fail (arg); for (; alen < MAX_TUPLE; alen++) *vp++ = 0; return out; bad_make: fail (Error_Bad_Make(REB_TUPLE, arg)); }