static int addLine (HelpPageEntry *page, const wchar_t *characters) { if (page->lineCount == page->lineLimit) { unsigned int newLimit = page->lineLimit? page->lineLimit<<1: 0X40; HelpLineEntry *newTable = realloc(page->lineTable, ARRAY_SIZE(newTable, newLimit)); if (!newTable) { logMallocError(); return 0; } page->lineTable = newTable; page->lineLimit = newLimit; } { HelpLineEntry *line = &page->lineTable[page->lineCount]; size_t length = wcslen(characters); if ((line->length = length) > page->lineLength) page->lineLength = length; if (!(line->characters = malloc(ARRAY_SIZE(line->characters, length)))) { logMallocError(); return 0; } wmemcpy(line->characters, characters, length); } page->lineCount += 1; return 1; }
static NoteDevice * pcmConstruct (int errorLevel) { NoteDevice *device; if ((device = malloc(sizeof(*device)))) { if ((device->pcm = openPcmDevice(errorLevel, opt_pcmDevice))) { device->blockSize = getPcmBlockSize(device->pcm); device->sampleRate = getPcmSampleRate(device->pcm); device->channelCount = getPcmChannelCount(device->pcm); device->amplitudeFormat = getPcmAmplitudeFormat(device->pcm); device->blockUsed = 0; if ((device->blockAddress = malloc(device->blockSize))) { logMessage(LOG_DEBUG, "PCM enabled: blk=%d rate=%d chan=%d fmt=%d", device->blockSize, device->sampleRate, device->channelCount, device->amplitudeFormat); return device; } else { logMallocError(); } closePcmDevice(device->pcm); } free(device); } else { logMallocError(); } logMessage(LOG_DEBUG, "PCM not available"); return NULL; }
static int extendSetting (char **setting, const char *value, int prepend) { if (value && *value) { if (!*setting) { if (!(*setting = strdup(value))) { logMallocError(); return 0; } } else { size_t newSize = strlen(*setting) + 1 + strlen(value) + 1; char *newSetting = malloc(newSize); if (!newSetting) { logMallocError(); return 0; } if (prepend) { snprintf(newSetting, newSize, "%s,%s", value, *setting); } else { snprintf(newSetting, newSize, "%s,%s", *setting, value); } free(*setting); *setting = newSetting; } } return 1; }
int readLine (FILE *file, char **buffer, size_t *size) { char *line; if (ferror(file)) return 0; if (feof(file)) return 0; if (!*size) { if (!(*buffer = malloc(*size = 0X80))) { logMallocError(); return 0; } } if ((line = fgets(*buffer, *size, file))) { size_t length = strlen(line); /* Line length including new-line. */ /* No trailing new-line means that the buffer isn't big enough. */ while (line[length-1] != '\n') { /* If necessary, extend the buffer. */ if ((*size - (length + 1)) == 0) { size_t newSize = *size << 1; char *newBuffer = realloc(*buffer, newSize); if (!newBuffer) { logMallocError(); return 0; } *buffer = newBuffer; *size = newSize; } /* Read the rest of the line into the end of the buffer. */ if (!(line = fgets(&(*buffer)[length], *size-length, file))) { if (!ferror(file)) return 1; logSystemError("read"); return 0; } length += strlen(line); /* New total line length. */ line = *buffer; /* Point to the beginning of the line. */ } if (--length > 0) if (line[length-1] == '\r') --length; line[length] = 0; /* Remove trailing new-line. */ return 1; } else if (ferror(file)) { logSystemError("read"); } return 0; }
char * getProgramPath (void) { char *path = NULL; HMODULE handle; if ((handle = GetModuleHandle(NULL))) { size_t size = 0X80; char *buffer = NULL; while (1) { { char *newBuffer = realloc(buffer, size<<=1); if (!newBuffer) { logMallocError(); break; } buffer = newBuffer; } { DWORD length = GetModuleFileName(handle, buffer, size); if (!length) { logWindowsSystemError("GetModuleFileName"); break; } if (length < size) { buffer[length] = 0; if ((path = strdup(buffer))) { while (length > 0) if (path[--length] == '\\') path[length] = '/'; } else { logMallocError(); } break; } } } free(buffer); } else { logWindowsSystemError("GetModuleHandle"); } return path; }
KeyboardMonitorObject * newKeyboardMonitorObject (const KeyboardProperties *properties, KeyEventHandler handleKeyEvent) { KeyboardMonitorObject *kmo; if ((kmo = malloc(sizeof(*kmo)))) { memset(kmo, 0, sizeof(*kmo)); kmo->requiredProperties = *properties; kmo->handleKeyEvent = handleKeyEvent; if (newKeyboardMonitorExtension(&kmo->kmx)) { if ((kmo->instanceQueue = newQueue(NULL, NULL))) { if (monitorKeyboards(kmo)) { kmo->isActive = 1; return kmo; } deallocateQueue(kmo->instanceQueue); } destroyKeyboardMonitorExtension(kmo->kmx); } free(kmo); } else { logMallocError(); } return NULL; }
KeyboardInstanceObject * newKeyboardInstanceObject (KeyboardMonitorObject *kmo) { KeyboardInstanceObject *kio; unsigned int count = BITMASK_ELEMENT_COUNT(keyCodeCount, BITMASK_ELEMENT_SIZE(unsigned char)); size_t size = sizeof(*kio) + count; if ((kio = malloc(size))) { memset(kio, 0, size); kio->kmo = kmo; kio->actualProperties = anyKeyboard; kio->events.buffer = NULL; kio->events.size = 0; kio->events.count = 0; kio->deferred.modifiersOnly = 0; kio->deferred.size = count; if (newKeyboardInstanceExtension(&kio->kix)) { if (enqueueItem(kmo->instanceQueue, kio)) { return kio; } destroyKeyboardInstanceExtension(kio->kix); } free(kio); } else { logMallocError(); } return NULL; }
int asyncMakeHandle ( AsyncHandle *handle, Element *(*newElement) (const void *parameters), const void *parameters ) { if (handle) { if (!(*handle = malloc(sizeof(**handle)))) { logMallocError(); return 0; } } { Element *element = newElement(parameters); if (element) { if (handle) { memset(*handle, 0, sizeof(**handle)); (*handle)->element = element; (*handle)->identifier = getElementIdentifier(element); (*handle)->tsd = asyncGetThreadSpecificData(); } return 1; } } if (handle) free(*handle); return 0; }
static int usbMakeData_CDC_ACM (UsbDevice *device, UsbSerialData **serialData) { UsbSerialData *usd; if ((usd = malloc(sizeof(*usd)))) { memset(usd, 0, sizeof(*usd)); usd->device = device; if ((usd->interface = usbFindCommunicationInterface(device))) { unsigned char interfaceNumber = usd->interface->bInterfaceNumber; if (usbClaimInterface(device, interfaceNumber)) { if (usbSetAlternative(device, usd->interface->bInterfaceNumber, usd->interface->bAlternateSetting)) { if ((usd->endpoint = usbFindInterruptInputEndpoint(device, usd->interface))) { usbBeginInput(device, USB_ENDPOINT_NUMBER(usd->endpoint)); *serialData = usd; return 1; } } usbReleaseInterface(device, interfaceNumber); } } free(usd); } else { logMallocError(); } return 0; }
static int processConfigurationLine ( char *line, void *data ) { const ConfigurationFileProcessingData *conf = data; static const char *delimiters = " \t"; /* Characters which separate words. */ char *directive; /* Points to first word of each line. */ /* Remove comment from end of line. */ { char *comment = strchr(line, '#'); if (comment) *comment = 0; } if ((directive = strtok(line, delimiters))) { int optionIndex; for (optionIndex=0; optionIndex<conf->info->optionCount; ++optionIndex) { const OptionEntry *option = &conf->info->optionTable[optionIndex]; if ((option->flags & OPT_Config) && option->word) { if (strcasecmp(directive, option->word) == 0) { const char *operand = strtok(NULL, delimiters); if (!operand) { logMessage(LOG_ERR, "%s: %s", gettext("operand not supplied for configuration directive"), line); conf->info->warning = 1; } else if (strtok(NULL, delimiters)) { while (strtok(NULL, delimiters)); logMessage(LOG_ERR, "%s: %s", gettext("too many operands for configuration directive"), line); conf->info->warning = 1; } else { char **setting = &conf->settings[optionIndex]; if (*setting && !(option->argument && (option->flags & OPT_Extend))) { logMessage(LOG_ERR, "%s: %s", gettext("configuration directive specified more than once"), line); conf->info->warning = 1; free(*setting); *setting = NULL; } if (*setting) { if (!extendSetting(setting, operand, 0)) return 0; } else { if (!(*setting = strdup(operand))) { logMallocError(); return 0; } } } return 1; } } } logMessage(LOG_ERR, "%s: %s", gettext("unknown configuration directive"), line); conf->info->warning = 1; } return 1; }
static GioHandle * connectBluetoothResource ( const char *identifier, const GioDescriptor *descriptor ) { GioHandle *handle = malloc(sizeof(*handle)); if (handle) { BluetoothConnectionRequest request; bthInitializeConnectionRequest(&request); request.identifier = identifier; request.channel = descriptor->bluetooth.channelNumber; request.discover = descriptor->bluetooth.discoverChannel; memset(handle, 0, sizeof(*handle)); if ((handle->connection = bthOpenConnection(&request))) { return handle; } free(handle); } else { logMallocError(); } return NULL; }
static int handleLearnCommands (int command, void *data) { switch (command & BRL_MSK_CMD) { case BRL_CMD_LEARN: { LearnModeParameters *lmp; if ((lmp = malloc(sizeof(*lmp)))) { memset(lmp, 0, sizeof(*lmp)); lmp->timeout = LEARN_MODE_TIMEOUT; if (asyncAddTask(NULL, presentLearnMode, lmp)) break; free(lmp); } else { logMallocError(); } brl.hasFailed = 1; break; } default: return 0; } return 1; }
static void noMemory (void *data) { LineProcessingData *lpd = data; logMallocError(); lpd->exitStatus = PROG_EXIT_FATAL; }
char * selectTextTable (const char *directory) { const char *locale = getTextTableLocale(); if (locale) { char name[strlen(locale) + 1]; { size_t length = strcspn(locale, ".@"); strncpy(name, locale, length); name[length] = 0; } if (strcmp(name, "C") == 0) { name[0] = 0; } else if (!testTextTable(directory, name)) { char *delimiter = strchr(name, '_'); if (delimiter) { *delimiter = 0; if (!testTextTable(directory, name)) name[0] = 0; } } if (name[0]) { char *textTableName = strdup(name); if (textTableName) return textTableName; logMallocError(); } } return NULL; }
static int usbAddHostDevice (const char *path) { int ok = 0; UsbHostDevice *host; if ((host = malloc(sizeof(*host)))) { if ((host->usbfsPath = strdup(path))) { host->sysfsPath = usbMakeSysfsPath(host->usbfsPath); if (!usbReadHostDeviceDescriptor(host)) { ok = 1; } else if (enqueueItem(usbHostDevices, host)) { return 1; } if (host->sysfsPath) free(host->sysfsPath); free(host->usbfsPath); } else { logSystemError("strdup"); } free(host); } else { logMallocError(); } return ok; }
static char * formatSocketAddress (const struct sockaddr *address) { char *string; switch (address->sa_family) { #ifdef AF_LOCAL case AF_LOCAL: { const struct sockaddr_un *localAddress = (const struct sockaddr_un *)address; string = strdup(localAddress->sun_path); break; } #endif /* AF_LOCAL */ case AF_INET: { const struct sockaddr_in *inetAddress = (const struct sockaddr_in *)address; const char *host = inet_ntoa(inetAddress->sin_addr); unsigned short port = ntohs(inetAddress->sin_port); char buffer[strlen(host) + 7]; snprintf(buffer, sizeof(buffer), "%s:%u", host, port); string = strdup(buffer); break; } default: string = strdup(""); break; } if (!string) logMallocError(); return string; }
static void processInternalSettings ( OptionProcessingInformation *info, int config ) { for (unsigned int optionIndex=0; optionIndex<info->optionCount; ++optionIndex) { const OptionEntry *option = &info->optionTable[optionIndex]; if (!(option->flags & OPT_Config) == !config) { const char *setting = option->internal.setting; char *newSetting = NULL; if (!setting) setting = option->argument? "": FLAG_FALSE_WORD; if (option->internal.adjust) { if (*setting) { if ((newSetting = strdup(setting))) { if (option->internal.adjust(&newSetting)) { setting = newSetting; } } else { logMallocError(); } } } ensureSetting(info, option, setting); if (newSetting) free(newSetting); } } }
static int brl_construct (BrailleDisplay *brl, char **parameters, const char *device) { if ((brl->data = malloc(sizeof(*brl->data)))) { memset(brl->data, 0, sizeof(*brl->data)); if (connectResource(brl, device)) { unsigned char response[MAXIMUM_RESPONSE_SIZE]; if (probeBrailleDisplay(brl, PROBE_RETRY_LIMIT, NULL, PROBE_INPUT_TIMEOUT, writeIdentityRequest, readPacket, &response, sizeof(response), isIdentityResponse)) { setBrailleKeyTable(brl, &KEY_TABLE_DEFINITION(all)); MAKE_OUTPUT_TABLE(0X01, 0X04, 0X10, 0X02, 0X08, 0X20, 0X40, 0X80); brl->textColumns = MAXIMUM_CELL_COUNT; brl->data->forceRewrite = 1; return 1; } disconnectBrailleResource(brl, NULL); } free(brl->data); } else { logMallocError(); } return 0; }
int addTextTableAlias (TextTableData *ttd, wchar_t from, wchar_t to) { if (ttd->alias.count == ttd->alias.size) { size_t newSize = ttd->alias.size? (ttd->alias.size << 1): 0X10; TextTableAliasEntry *newArray; if (!(newArray = realloc(ttd->alias.array, ARRAY_SIZE(newArray, newSize)))) { logMallocError(); return 0; } ttd->alias.array = newArray; ttd->alias.size = newSize; } { unsigned int cellNumber = UNICODE_CELL_NUMBER(from); UnicodeRowEntry *row = getUnicodeRowEntry(ttd, from, 1); if (!row) return 0; BITMASK_SET(row->cellAliased, cellNumber); } { TextTableAliasEntry *alias = &ttd->alias.array[ttd->alias.count++]; memset(alias, 0, sizeof(*alias)); alias->from = from; alias->to = to; } return 1; }
static MenuItem * newMenuItem (Menu *menu, unsigned char *setting, const MenuString *name) { if (menu->items.count == menu->items.size) { unsigned int newSize = menu->items.size? (menu->items.size << 1): 0X10; MenuItem *newArray = realloc(menu->items.array, (newSize * sizeof(*newArray))); if (!newArray) { logMallocError(); return NULL; } menu->items.array = newArray; menu->items.size = newSize; } { MenuItem *item = getMenuItem(menu, menu->items.count++); item->menu = menu; item->setting = setting; item->name.label = getLocalText(name->label); item->name.comment = getLocalText(name->comment); item->methods = NULL; item->test = NULL; item->changed = NULL; item->minimum = 0; item->maximum = 0; item->divisor = 1; return item; } }
static int processConfigurationDirective ( const wchar_t *keyword, const char *value, const ConfigurationFileProcessingData *conf ) { const ConfigurationDirective *directive = findConfigurationDirective(keyword, conf); if (directive) { const OptionEntry *option = &conf->info->optionTable[directive->option]; char **setting = &conf->settings[directive->option]; if (*setting && !(option->argument && (option->flags & OPT_Extend))) { logMessage(LOG_ERR, "%s: %" PRIws, gettext("configuration directive specified more than once"), keyword); conf->info->warning = 1; free(*setting); *setting = NULL; } if (*setting) { if (!extendStringSetting(setting, value, 0)) return 0; } else { if (!(*setting = strdup(value))) { logMallocError(); return 0; } } } else { logMessage(LOG_ERR, "%s: %" PRIws, gettext("unknown configuration directive"), keyword); conf->info->warning = 1; } return 1; }
char * getPathDirectory (const char *path) { size_t length = strlen(path); size_t end = stripPathDelimiter(path, length); if (end) { while (--end) if (isPathDelimiter(path[end-1])) break; if ((length = end)) if ((end = stripPathDelimiter(path, length))) length = end; } if (!length) length = strlen(path = "."); { char *directory = malloc(length + 1); if (directory) { memcpy(directory, path, length); directory[length] = 0; } else { logMallocError(); } return directory; } }
static GioHandle * connectSerialResource ( const char *identifier, const GioDescriptor *descriptor ) { GioHandle *handle = malloc(sizeof(*handle)); if (handle) { memset(handle, 0, sizeof(*handle)); if ((handle->device = serialOpenDevice(identifier))) { if (serialSetParameters(handle->device, descriptor->serial.parameters)) { handle->parameters = *descriptor->serial.parameters; return handle; } serialCloseDevice(handle->device); } free(handle); } else { logMallocError(); } return NULL; }
void beginProgram (int argumentCount, char **argumentVector) { #if defined(GRUB_RUNTIME) #else /* at exit */ atexit(endProgram); #endif /* at exit */ initializeSystemObject(); prepareLocale(); if ((programPath = getProgramPath())) { registerProgramMemory("program-path", &programPath); } else { programPath = argumentVector[0]; } if (!isExplicitPath(programPath)) { char *path = findProgram(programPath); if (!path) path = testProgram(".", programPath); if (path) programPath = path; } if (isExplicitPath(programPath)) { #if defined(HAVE_REALPATH) && defined(PATH_MAX) if (!isAbsolutePath(programPath)) { char buffer[PATH_MAX]; char *path = realpath(programPath, buffer); if (path) { char *realPath = strdup(path); if (realPath) { programPath = realPath; } else { logMallocError(); } } else { logSystemError("realpath"); } } #endif /* defined(HAVE_REALPATH) && defined(PATH_MAX) */ if (!isAbsolutePath(programPath)) { char *directory; if ((directory = getWorkingDirectory())) { char *path; if ((path = makePath(directory, programPath))) programPath = path; free(directory); } } } programName = locatePathName(programPath); pushLogPrefix(programName); }
static wchar_t * cpbAllocateCharacters (size_t count) { { wchar_t *characters; if ((characters = malloc(count * sizeof(*characters)))) return characters; } logMallocError(); return NULL; }
static int spk_construct (volatile SpeechSynthesizer *spk, char **parameters) { if ((spk_buffer = malloc(spk_size))) { return 1; } else { logMallocError(); } return 0; }
static void cpbPushContent (const wchar_t *characters, size_t length) { if (length > 0) { Queue *queue = cpbGetHistoryQueue(1); if (queue) { Element *element = getQueueTail(queue); if (element) { const HistoryEntry *entry = getElementItem(element); if (length == entry->length) { if (wmemcmp(characters, entry->characters, length) == 0) { return; } } } { HistoryEntry *entry; if ((entry = malloc(sizeof(*entry)))) { if ((entry->characters = cpbAllocateCharacters(length))) { wmemcpy(entry->characters, characters, length); entry->length = length; if (enqueueItem(queue, entry)) { return; } free(entry->characters); } else { logMallocError(); } free(entry); } else { logMallocError(); } } } } }
SerialDevice * serialOpenDevice (const char *identifier) { char **parameters = serialGetDeviceParameters(identifier); if (parameters) { SerialDevice *serial; if ((serial = malloc(sizeof(*serial)))) { char *path; { const char *name = parameters[SERIAL_DEV_NAME]; if (!*name) name = SERIAL_FIRST_DEVICE; path = getDevicePath(name); } if (path) { int connected; serial->fileDescriptor = -1; serial->stream = NULL; connected = serialConnectDevice(serial, path); free(path); path = NULL; if (connected) { int ok = 1; if (!serialConfigureBaud(serial, parameters[SERIAL_DEV_BAUD])) ok = 0; if (!serialConfigureDataBits(serial, parameters[SERIAL_DEV_DATA_BITS])) ok = 0; if (!serialConfigureStopBits(serial, parameters[SERIAL_DEV_STOP_BITS])) ok = 0; if (!serialConfigureParity(serial, parameters[SERIAL_DEV_PARITY])) ok = 0; if (!serialConfigureFlowControl(serial, parameters[SERIAL_DEV_FLOW_CONTROL])) ok = 0; deallocateStrings(parameters); if (ok) return serial; serialCloseDevice(serial); return NULL; } } free(serial); } else { logMallocError(); } deallocateStrings(parameters); } return NULL; }
char * getBootParameters (const char *name) { size_t nameLength = strlen(name); char *parameters; if ((parameters = strdup(""))) { const char *path = "/proc/cmdline"; FILE *file; if ((file = fopen(path, "r"))) { char buffer[0X1000]; char *line = fgets(buffer, sizeof(buffer), file); if (line) { char *token; while ((token = strtok(line, " \n"))) { line = NULL; if ((strncmp(token, name, nameLength) == 0) && (token[nameLength] == '=')) { char *newParameters = strdup(token + nameLength + 1); if (newParameters) { free(parameters); parameters = newParameters; } else { logMallocError(); } } } } fclose(file); } } else { logMallocError(); } return parameters; }
void onProgramExit (ProgramExitHandler *handler) { ProgramExitEntry *pxe; if ((pxe = malloc(sizeof(*pxe)))) { pxe->handler = handler; pxe->next = programExitEntries; programExitEntries = pxe; } else { logMallocError(); } }