putenv() and setenv() on Unix - c

I need to get input from user and deal with variables. I need to have next features:
set varname = somevalue: set the value of the environment variable named varname to the value specified by somevalue.
delete varname: remove the named environment variable.
print varname: prints out the current value of the named environment variable.
What I have till now is this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
int main(int argc, char **argv) {
char * s;
char * command;
char * varName;
char * varValue;
while (s = readline("prompt> ")) {
/* Initialise char* variables */
command = NULL;
varName = NULL;
varValue = NULL;
add_history(s); /* adds the line to the readline history buffer */
printf("> %s\n", s); //print > sign
int cmdNo = 1;
int i;
// parse through entire string
for (i = 0; i < strlen(s); i++)
{
// check for space or = and jump over it
while ((isspace(s[i]) || s[i] == '=') && (i < strlen(s)))
{
i++;
}
// check if i is greater than string size
if (i >= strlen(s))
{
printf("Bad command format!\n");
break;
}
// if cmdNo == 1, get the command
if (cmdNo == 1)
{
int commandSize = 0;
int index = i;
// point index to space
while (!isspace(s[index]))
{
commandSize++;
index++;
}
// get command
command = (char*)malloc(commandSize + 1);
int destIndex = 0;
// copy command into command array
while (i<index)
{
command[destIndex] = s[i];
destIndex++;
i++;
}
// adding terminate character
command[destIndex] = '\0';
// increase command number by 1
cmdNo++;
}
// if cmdNo == 2 we deal with variable name
else if (cmdNo == 2)
{
// variable name size
int varNameSize = 0;
int index = i;
// point index to space
while (!isspace(s[index]))
{
varNameSize++;
index++;
}
// get var name
varName = (char*)malloc(varNameSize + 1);
int index2 = 0;
while (i<index)
{
varName[index2] = s[i];
index2++;
i++;
}
// add terminate char
varName[index2] = '\0';
// increment cmdNo by 1
cmdNo++;
}
// if cmdNo == 3 we deal with variable value
else if (cmdNo == 3)
{
int valueSize = 0;
int index = i;
// point index to space
while (!isspace(s[index]) && s[index] != '\0')
{
valueSize++;
index++;
}
// get variable value
varValue = (char*)malloc(valueSize + 1);
int index2 = 0;
while (i<index)
{
varValue[index2] = s[i];
index2++;
i++;
}
// add terminate char
varValue[index2] = '\0';
}
}
// print command, variable name and value
if (command != NULL)
{
printf("%s", command);
}
if (varName != NULL)
{
printf(" %s", varName);
}
if (varValue != NULL)
{
printf(" %s\n", varValue);
}
/* clean up! */
free(s);
free(command);
free(varName);
free(varValue);
}
return(0);
}
Obviously, I had to put somewhere putenv(), setenv() or clearenv(). I don't have much experience with these commands.
Also, there is some error (Segmentation fault). This is response of system

The crash is caused by your loops while (!isspace(s[index])) for cmdNo 1 and 2 -- if there is no third (or second) word on the input line, they'll run past the NUL terminator in the string and (probably) crash. You need a check for NUL in these loops as you check in cmdNo 3 case.
You also have a problem if you have more than 3 words on the input line -- you'll go into an infinite loop on the 4th word.
Rather than duplicating the code for the word in if/else if/else if as you have done, its much better to put the words in an array. You could even use strtok_r or strsep rather than manually parsing out the characters.

Related

Get user command in C in multiple lines

I am trying to write a function that gets multiple lines if ends with \ and just stop if don't have that. I am very new with C so having trouble to do it.
So something like
User input:
Hello, I am blablabla \
I like bablabla \
My favorite color is (stop here)
But in my current function when the user press enter is over and just the first line is saved.
I know that I need to check if in the end have a backslash just keep going and appending, I am just not sure how to do that using getline.
char *getCommand(void){
char* line; //string from user
ssize_t linesize = 0;
//getting command from user if reaches end of file exit or if something went wrong to read file.
if(getline(&line, &linesize, stdin)==-1){
if(feof(stdin)){
exit(0);
}
else{
fprintf(stderr, "Error reading the command: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
return line;
}
What your code is basically doing is reading one line. Few things to note:
feof(stdin) is wrong.
getline() dynamically allocates a string, so you need to free it yourself.
That said, you can implement it this way:
// Returns the number of commands read, -1 if it fails
int getCommand(char **commands, int max_commands)
{
int i, num_cmds = 0;
for (i = 0; i < max_commands; i++) {
char *line = NULL; // This must be initialized to NULL
ssize_t linesize = 0;
if(getline(&line, &linesize, stdin) == -1)
return -1;
line[strcspn(line, "\n")] = '\0'; // Replace \n with a null-terminator
commands[i] = line;
num_cmds++;
if (!strchr(line, '\\')) // if the line doesn't contain a \ then exit
return num_cmds;
}
return num_cmds;
}
commands is an array of strings that will hold your commands. max_commands is the maximum number of commands your array may hold (i.e. its size).
You can use it this way:
int main(void)
{
const int max_commands = 120;
char *commands[max_commands];
int num_cmds = getCommand(commands, max_commands);
if (num_cmds == -1) {
fprintf(stderr, "Error reading commands\n");
return 1;
}
int i;
for (i = 0; i < num_cmds; i++) {
printf("command %d: %s\n", i + 1, commands[i]);
free(commands[i]); // clear memory allocated by getline
}
}
Given your input as an example, here is what you will get:
Hello, I am blablabla \
I like bablabla \
My favorite color is
command 1: Hello, I am blablabla \
command 2: I like bablabla \
command 3: My favorite color is
EDIT: If the \ needs to be at the end of the line, then replace if (!strchr(line, '\\')) with
if (line[strlen(line)-1] != '\\').
You can use this code to solve your problem, I opted to use scanf instead of getline, but the final working is the same:
#include <stdio.h>
#include <stdlib.h>
// Set limit to line
#define LINE_BUFFER 1024
// Get length a string
unsigned int length(const char * str) {
int count = 0;
while (str[count] != '\0') count++;
return count;
}
// Concatenate two strings to a target variable (dest)
int concat(const char * src_1, const char * src_2, char * dest, size_t sizeof_dest) {
// Get lengths from sources
unsigned int src_1_length = length(src_1);
unsigned int src_2_length = length(src_2);
// Calculate minimum length for dest
unsigned int dst_length = src_1_length + src_2_length;
if(sizeof_dest < dst_length)
// Has no minimum length for concatenation
return -1;
int index = 0;
for(int i = 0; i < src_1_length; i++) {
index++;
dest[i] = src_1[i];
}
for(int i = 0; i < src_2_length; i++) dest[index + i] = src_2[i];
return 0;
}
// Read multiline
char * getCommand() {
char * command = NULL;
while(1) {
char line[LINE_BUFFER];
scanf("%[^\n]s", line);
fflush(stdin);
// Get line length
unsigned int line_length = length(line);
// Checking last character
// zero - false
// nonzero - true
char has_slash = line[line_length - 1] == '\\' ? 1 : 0;
// Update slash to breakline
if(has_slash) line[line_length - 1] = '\n';
if(command == NULL) {
command = (char *) malloc(line_length * sizeof(char));
// Copy line to command
for(int i = 0; i < line_length; i++) command[i] = line[i];
} else {
// Concatenate command with current line for command update
unsigned int command_length = length(command);
unsigned int tmp_command_length = line_length + command_length;
char tmp_command[tmp_command_length];
if(concat(command, line, tmp_command, sizeof(tmp_command)) != 0) {
printf("Error in concatenating '%s' with '%s'\n", command, line);
}
// Free memory from old command
free(command);
// Allocating memory for new updated command
command = (char *) malloc(tmp_command_length * sizeof(char));
// Copy command plus current line to new command
for(int i = 0; i < tmp_command_length; i++) command[i] = tmp_command[i];
}
if(!has_slash) break;
}
return command;
}
Click here to access the code repository on Github if you want to improve or fix something. Your collaboration is very welcome.
Quick example of implementation:
Let's assume this is the main file (main.c)
// IMPORTANT: Paste the code above here
// ...
int main() {
char * command = getCommand();
// Print result
printf("\n---- BEGIN ---\n");
printf("\n%s\n", command);
printf("\n---- END ---\n");
// Always clear data allocated in heap memory
free(command);
return 1;
}
Now let's compile the file via terminal, you can use the gcc or clang compilers. In this example I will use clang.
$ clang main.c -o getcommand
(if you are using gcc, just change the clang to gcc)
Run the compiled file:
$ ./getcommand
Right after type your test text
Hello, I am blablabla \
I like blablabla \
My favorite color is
The output should be as follows:
---- BEGIN ---
Hello, I am blablabla
I like blablabla
My favorite color is
---- END ---

char* function returning Null

I am currently working on a spam filter program that takes in and reads text files. In the initializeTraining function, I call the preprocess function which reads in each string from each line in a given text-file.
Once the newDict function is executed from the line first=newDict(string, NULL);, the program, however, returns an error stating that there is a load of null pointer of type 'char' at line while(string[i] !='\0' && i<WORDLENGTH) { in the newDict function.
It seems that the preprocess function is returning null pointers despite the fact that it still takes in the passed-in strings from the text file. Is there something that I'm doing wrong in the preprocess function?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
WORDLENGTH is the length of the word in the linked list named as dictionary
MAILSEPARATOR is the totken to differentiate the mails that are included in one file.
it is also the token to update updated in linkedlist
*/
#define WORDLENGTH 20
#define MAILSEPARATOR "#####"
/*
define DEBUG as 0 to disable debug mode and to 1 to enable the mode.
*/
#define DEBUG 0
typedef struct dictionary dict;
typedef dict* word_dict;
typedef enum {false, true} bool;
/*
linked list, count is for the total word count and
occur is the numbers of the mails that had the word
*/
struct dictionary{
char word[WORDLENGTH];
int occur;
int count;
word_dict next;
bool updated;
};
// if there is no matching words after searching, create a new node
word_dict newDict(char *string, word_dict next){
word_dict target = (word_dict)malloc(sizeof(dict));
int i = 0;
while(string[i] !='\0' && i<WORDLENGTH) {
target->word[i] = string[i];
i++;
}
target->count = 1;
target->next = next;
target->occur = 1;
target->updated = true;
return target;
}
/*
preprocessor, convert string to lowercase
and trim the puctuations at the back
*/
char* preprocess(char* string){
#if DEBUG
printf("\nbefore preprocess, string: %s \n", string);
#endif
int i=0;
while(string[i] != '\0') { // convert to lower case
if (string[i] >= 65 && string[i] < 90) {
string[i] += 32;
i++;
}
while(true) {
i--;
if(i < 0) {
#if DEBUG
printf("word of only punctuations \n");
#endif
return NULL;
} else if((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
string[i+1]='\0';
break;
}
}
i=0;
while(true) {
if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
break;
} else {
string = &string[i+1];
}
i++;
}
}
#if DEBUG
printf("_after preprocess, string: %s\n", string);
#endif
return string;
}
/*
initialize training
reads the sample mails and creates a linked list of
the percentages of the words occuring in the sample mails
*/
word_dict initializeTraining(char* filename){
FILE *fp = NULL;
fp = fopen(filename, "r");
if(fp == NULL) {
printf("no file found\n");
return NULL;
}
char* string;
string = (char*)malloc(sizeof(char)*50);
word_dict first = NULL;
fscanf(fp, "%s\n", string);
string = preprocess(string);
first = newDict(string, NULL);
while(fscanf(fp,"%s", string) == 1) {
first = searchDict(string, first);
}
fclose(fp);
free(string);
return first;
}
/*
tests whether the mail is pam or not
takes the filename of the test mail,
returns true or false depending on the email's content
*/
bool bayesian_spam_filter(char * filename_for_test_email) {
word_dict spamDict=initializeTraining("spam.txt");
word_dict nonspamDict=initializeTraining("not_spam.txt");
#if DEBUG
printDict(spamDict);
printDict(nonspamDict);
#endif
FILE *stream=NULL;
stream = fopen(filename_for_test_email, "r");
if(stream == NULL){
printf("no file found\n");
return false;
}
char* string;
string = (char*)malloc(sizeof(char)*50);
int ps, pn; // probability of spam mail and non-spam mail
double prob = 0.5;
while(fscanf(stream,"%s", string) == 1){
char* tempString; // for handling the errors happening from string being null during preprocessing
tempString = preprocess(string);
if(tempString == NULL){
continue;
}
if((ps = searchTest(tempString, spamDict)) != 0) {
if((pn = searchTest(tempString, nonspamDict)) != 0) {
printf("ps:%3d, pn:%3d, %s\n", ps, pn, tempString);
prob = prob * (double) ps / ((prob* (double)ps + (1 - prob) * (double) pn));
printf("this prob: %.10f\n", prob);
}
}
}
//printf("%d, %d \n", pSProduct, pNProduct);
//proba=(float)(pSProduct/(pSProduct+pNProduct));
printf("Probability of mail being spam: %.10f\n", prob);
fclose(stream);
free(string);
if (prob > 0.9) {
return true;
}
return false;
}
It seems that the preprocess function is returning null pointers
Hardly surprising when it contains a line return NULL;. At that point, you should instead set the first character of the string to '\0' and return it since the surrounding code expects to get the string returned in all cases.
Another problem can be found in this section:
i=0;
while(true) {
if ((string[i] >= 97 && string[i] <= 122) || (string[i] >= 48 && string[i] <= 57)){
break;
} else {
string = &string[i+1];
}
i++;
}
Let's say the string is ".a" and go through the loop. Since the first character is not a letter, we do not break, instead we update the string pointer so that it points to "a" now. Then, we increment i. In the next iteration, string[i] is the null terminator, which is not a letter, so we continue. Since we are past the string's data, what follows is undefined behavior.
The simple fix for this is to not increment i and rather stick with [0] since you always want to remove from the beginning. The proper fix is to use i but without incrementing the string pointer, since you want to free it later – you must call free on the pointer returned by malloc, therefore modifying the pointer leads to undefined behavior! Instead of returning the string from preprocess, return the offset from the start (as counted by i), this way freeing the string later will work properly. The calling code would then look like this:
int offset = preprocess(string);
first = newDict(string + offset, NULL);

receiving Seg Fault for my minishell program

I am writing a mini-shell program. It gives me seg fault in the line "args = my_str2vect(line);". The "my_str2vect" function works fine though, as well as the my_strncpy function and my_strlen function(returns the length of the char array). So what's wrong with that line?
#define _POSIX_SOURCE
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include "../../include/my.h"
pid_t pid;
int childrunning = 0;
void minishell_loop();
/*
Your shell must also support CTRL+C by using UNIX signals.
If you type CTRL+C your prompt must not exit
and instead kill the currently running process (or do nothing if there is none).
*/
void int_handler(int sig){
if(childrunning == 1){
kill(pid, SIGUSR2);
}else{
my_str("\n");
minishell_loop();
}
}
void killkillkill(int sig){
exit(0);
}
/*
Function Declarations for builtin shell commands:
*/
int minishell_cd(char **args);
int minishell_help(char **args);
int minishell_exit(char **args);
/*
List of builtin commands, followed by their corresponding functions.
*/
char *builtin_str[] = {
"cd",
"help",
"exit"
};
int (*builtin_func[]) (char **) = {
&minishell_cd,
&minishell_help,
&minishell_exit
};
int minishell_mum_bultins() {
return sizeof(builtin_str) / sizeof(char *);
}
/*
Builtin function implementations.
*/
/**
#brief Bultin command: change directory.
#param args List of args. args[0] is "cd". args[1] is the directory.
#return Always returns 1, to continue executing.
*/
int minishell_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "minishell: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("minishell");
}
}
return 1;
}
/**
#brief Builtin command: print help.
#param args List of args. Not examined.
#return Always returns 1, to continue executing.
*/
int minishell_help(char **args)
{
int i;
printf("Tianwei's minishell, version 1.01\n");
printf("These shell commands are defined internally.\n");
for (i = 0; i < minishell_mum_bultins(); i++) {
printf(" %s\n", builtin_str[i]);
}
return 1;
}
/**
#brief Builtin command: exit.
#param args List of args. Not examined.
#return Always returns 0, to terminate execution.
*/
int minishell_exit(char **args)
{
return 0;
}
/**
#brief Launch a program and wait for it to terminate.
#param args Null terminated list of arguments (including program).
#return Always returns 1, to continue execution.
*/
int minishell_launch(char **args)
{
signal(SIGUSR2, killkillkill);
int status;
pid = fork();
if (pid == 0) {
// Child process
childrunning = 1;
if (execvp(args[0], args) == -1) {
perror("minishell");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("minishell");
} else {
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
/**
#brief Execute shell built-in or launch program.
#param args Null terminated list of arguments.
#return 1 if the shell should continue running, 0 if it should terminate
*/
int minishell_execute(char **args)
{
int i;
my_str(args[0]);
if (args[0] == NULL) {
my_str("args[0] is NULL\n");
// An empty command was entered.
return 1;
}
for (i = 0; i < minishell_mum_bultins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
my_str("1\n");
return (*builtin_func[i])(args);
}
}
my_str("0\n");
return minishell_launch(args);
}
#define MINISHELL_RL_BUFSIZE 1024
/**
#brief Read a line of input from stdin.
#return The line from stdin.
*/
char *minishell_readln(void)
{
int bufsize = MINISHELL_RL_BUFSIZE;
int position = 0;
char* buffer = malloc(1024 * sizeof(char));
int c;
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
fprintf(stderr, "minishell: Command line too long!\n");
exit(EXIT_FAILURE);
}
}
}
/**
#brief Loop getting input and executing it.
*/
void minishell_loop(void)
{
signal(SIGINT, int_handler);
char* line;
char** args;
int status;
do {
printf("MINISHELL: /home/tianwei/$ ");
line = minishell_readln();
my_str("0\n");
my_str(line);
args = my_str2vect(line);
my_str(args[0]);
my_str("0\n");
status = minishell_execute(args);
my_str("1\n");
free(line);
free(*args);
free(args);
} while (status);
}
/**
#brief Main entry point.
#param argc Argument count.
#param argv Argument vector.
#return status code
*/
int main(int argc, char **argv)
{
// Load config files, if any.
// Run command loop.
minishell_loop();
// Perform any shutdown/cleanup.
return EXIT_SUCCESS;
}
This is the code for my_str2vect function
#include "../../include/my.h"
char** my_str2vect(char* str){
// Takes a string
// Allocates a new vector (array of string ended by a NULL),
// Splits apart the input string x at each space character
// Returns the newly allocated array of strings
// Any number of ' ','\t', and '\n's can separate words.
// I.e. "hello \t\t\n class,\nhow are you?" -> {"hello", "class,", "how", "are","you?", NULL}
int max_num_words = 0;
int a = 0;
int b = 0;
int max_num_char = 0;
while(str[a] != '\0'){ // find the number of words and the length of the longest word
if(str[a] != ' ' && str[a] != '\t' && str[a] != '\n'){
++max_num_words;
++a;
++b;
while((str[a] != ' ' && str[a] != '\t' && str[a] != '\n') && str[a] != '\0'){
++a;
++b;
}
if(b > max_num_char){
max_num_char = b;
}
b = 0;
while((str[a] == ' ' || str[a] == '\t' || str[a] == '\n') && str[a] != '\0'){
++a;
}
}
}
char** output = (char **)malloc(sizeof(char *) * (max_num_words + 1)); // Allocate a 2D array first.
for(int c = 0; c < max_num_words + 1; ++c){
output[c] = (char *)malloc(sizeof(char) * (max_num_char + 1));
}
int i = 0;
int j = 0;
while(i < my_strlen(str) && j < max_num_words){ // Put the characters into the 2D array.
int k = 0;
while(i < my_strlen(str) && (str[i] == ' ' || str[i] == '\t' || str[i] == '\n')){
++i;
}
while(i < my_strlen(str) && (!(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))){
++i;
++k;
}
if(i-k < my_strlen(str) && j < max_num_words){
my_strncpy(output[j], &str[i-k], k);
}
++j;
}
output[j] = NULL;
return output;
}
This is my_strncpy function
#include "../../include/my.h"
char *my_strncpy(char *dst, char *src, int n)
{
/* Same as my_strcpy except:
* Only copies n chars or until the end of src*/
if(src != NULL && dst != NULL){
int i = 0;
while(i < n && src[i] != '\0'){
dst[i] = src[i];
++i;
}
dst[i] = '\0';
}
return dst;
}
I didn't try to execute your code but here is what strikes me at first sight in the my_str2vect function:
in the first while loop, a is only incremented in the if. You are likely to meet a forever loop if your line begins with a space
in the second while loop, you don't check for the end of string '\0'. This may well be the reason for your crash, but there may be others.
PS: since you are parsing a string, you should take a look at strtok and sscanf, they would ease your life

fgets giving me a segfault when passing only spaces

I am trying to make a shell and one of the conditions i am looking out for is when the user enters a bunch of spaces. However, I get a segfault from fgets when I input any number of spaces in the terminal. It can be one space, or it can be a whole bunch of them followed by a random character. I keep getting a segfault.
Development:
I noticed that I don't get a segfault when I remove my tokenize function. Why would this be the case?
Here's my code:
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
/* Initialize variables and methods */
int status;
int i;
int one_nonspace = -1;
int on = 1;
char input[101];
char temp[101];
char* tokenized;
char operators[3] = {'>', '<', '|'};
char** tokens;
void getInput(char *prmpt, char *buff){
printf(">:");
fgets(buff, 101, stdin); //segfault here when input spaces.
/*printf("works!");*/
if(one_nonspace != -1){
printf("spaces");
memcpy( temp, &buff[i], (101-i) );
temp[100] = '\0';
}
if(buff[strlen(buff) -1] != '\n'){
int over = 0;
while(fgetc(stdin) != '\n')
over++;
if(over>0)
{
printf("Command is over 100 characters. Please try again\n");
status = 1;
}
}
else{
buff[strlen(buff) - 1] = '\0';
status = 0;
}
}
char** tokenize(char* a_str)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last = 0;
char delim[2];
delim[0] = ' ';
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (' ' == *tmp)
{
count++;
last = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
/* Create a parser Feb 2*/
int main(int argc, char **argv){
while(on){
getInput(">: ", input);
tokenized = input;
if(status == 0){
/*printf("%s\n", input);*/
}
/*Split the line into tokens*/
if(input[0] != ' ')
tokens = tokenize(tokenized);
/*if tokens[0] == 'exit', then quit.
*/
if(strcmp(*(tokens),"exit") == 0){
break;}
/*2/3 Now we need to do something with the split up tokens*/
/*printf("input after token: %s\n", input);*/
/*Free the tokens at the end!!! Remember this!*/
if (tokens)
{
int i;
for (i = 0; *(tokens + i); i++)
{
printf("%s\n", *(tokens + i));
free(*(tokens + i));
}
free(tokens);
}
}
return 0;
}
Here's the problem:
/*Split the line into tokens*/
if(input[0] != ' ')
tokens = tokenize(tokenized);
/*if tokens[0] == 'exit', then quit.
*/
if(strcmp(*(tokens),"exit") == 0){
break;}
When input begins with a space character, you skip the tokenize function and attempt to dereference tokens - a NULL pointer.
Edit: you are trying to debug with print statements and that is a valid method, but remember to flush the buffers or you won't get an accurate idea of where the problem is if the crash happens before there's any output. You can flush them explicitly with fflush or simply use newline characters if you're on a terminal, as they are usually line buffered.

Remove punctuation at beginning and end of a string

I have a string and I want to remove all the punctuation from the beginning and the end of it only, but not the middle.
I have wrote a code to remove the punctuation from the first and last character of a string only, which is clearly very inefficient and useless if a string has 2 or more punctuations at the end.
Here is an example:
{ Hello ""I am:: a Str-ing!! }
Desired output
{ Hello I am a Str-ing }
Are there any functions that I could use? Thanks.
This is what I've done so far. I'm actually editing the string in a linked-list
if(ispunct(removeend->string[(strlen(removeend->string))-1]) != 0) {
removeend->string[(strlen(removeend->string))-1] = '\0';
}
else {}
Iterate over the string, use isalpha() to check each character, write the characters which pass into a new string.
char *rm_punct(char *str) {
char *h = str;
char *t = str + strlen(str) - 1;
while (ispunct(*p)) p++;
while (ispunct(*t) && p < t) { *t = 0; t--; }
/* also if you want to preserve the original address */
{ int i;
for (i = 0; i <= t - p + 1; i++) {
str[i] = p[i];
} p = str; } /* --- */
return p;
}
Iterate over the string, use isalpha() to check each character, after the first character that passes start writing into a new string.
Iterate over the new string backwards, replace all punctuation with \0 until you find a character which isn't punctuation.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char* trim_ispunct(char* str){
int i ;
char* p;
if(str == NULL || *str == '\0') return str;
for(i=strlen(str)-1; ispunct(str[i]);--i)
str[i]='\0';
for(p=str;ispunct(*p);++p);
return strcpy(str, p);
}
int main(){
//test
char str[][16] = { "Hello", "\"\"I", "am::", "a", "Str-ing!!" };
int i, size = sizeof(str)/sizeof(str[0]);
for(i = 0;i<size;++i)
printf("%s\n", trim_ispunct(str[i]));
return 0;
}
/* result:
Hello
I
am
a
Str-ing
*/
Ok, in a while iteration, call multiple times the strtok function to separate each single string by the character (white space). You could also use sscanf instead of strtok.
Then, for each string, you have to do a for cycle, but beginning from the end of the string up to the beginning.As soon as you encounter !isalpha(current character) put a \0 in the current string position. You have eliminated the tail's punctuation chars.
Now, do another for cycle on the same string. Now from 0 to strlen(currentstring). While is !isalpha(current character) continue. If isalpha put the current character in in a buffer and all the remaining characters. The buffer is the cleaned string. Copy it into the original string.
Repeat the above two steps for the others strtok's outputs. End.
Construct a tiny state machine. The cha2class() function divides the characters into equivalence classes. The state machine will always skip punctuation, except when it has alphanumeric characters on the left and the right; in that case it will be preserved. (that is the memmove() in state 3)
#include <stdio.h>
#include <string.h>
#define IS_ALPHA 1
#define IS_WHITE 2
#define IS_PUNCT 3
int cha2class(int ch);
void scrutinize(char *str);
int cha2class(int ch)
{
if (ch >= 'a' && ch <= 'z') return IS_ALPHA;
if (ch >= 'A' && ch <= 'Z') return IS_ALPHA;
if (ch == ' ' || ch == '\t') return IS_WHITE;
if (ch == EOF || ch == 0) return IS_WHITE;
return IS_PUNCT;
}
void scrutinize(char *str)
{
size_t pos,dst,start;
int typ, state ;
state = 0;
for (dst = pos = start=0; ; pos++) {
typ = cha2class(str[pos]);
switch(state) {
case 0: /* BOF, white seen */
if (typ==IS_WHITE) break;
else if (typ==IS_ALPHA) { start = pos; state =1; }
else if (typ==IS_PUNCT) { start = pos; state =2; continue;}
break;
case 1: /* inside a word */
if (typ==IS_ALPHA) break;
else if (typ==IS_WHITE) { state=0; }
else if (typ==IS_PUNCT) { start = pos; state =3;continue; }
break;
case 2: /* inside punctuation after whitespace: skip it */
if (typ==IS_PUNCT) continue;
else if (typ==IS_WHITE) { state=0; }
else if (typ==IS_ALPHA) {state=1; }
break;
case 3: /* inside punctuation after a word */
if (typ==IS_PUNCT) continue;
else if (typ==IS_WHITE) { state=0; }
else if (typ==IS_ALPHA) {
memmove(str+dst, str+start, pos-start); dst += pos-start;
state =1; }
break;
}
str[dst++] = str[pos];
if (str[pos] == '\0') break;
}
}
int main (int argc, char **argv)
{
char test[] = ".This! is... ???a.string?" ;
scrutinize(test);
printf("Result=%s\n", test);
return 0;
}
int main (int argc, char **argv)
{
char test[] = ".This! is... ???a.string?" ;
scrutinize(test);
printf("Result=%s\n", test);
return 0;
}
OUTPUT:
Result=This is a.string

Resources