C program that executes command fails with sleep - c

I wrote a program that read a bash file line by line and execute its command written inside.
It seems to execute standard command normally (albeit there is not output on the terminal), but when it reads the sleep command, there is an error:
sleep 3: missing operand
Try 'sleep 3 --help' for more information.
char** string_separator(char* string, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = string;
char* last_dot = 0;
char delimiter[2];
delimiter[0] = a_delim;
delimiter[1] = 0;
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_dot = tmp;
}
tmp++;
}
count += last_dot < (string + strlen(string) - 1);
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(string,delimiter);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delimiter);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
FILE * fp;
fp = fopen(argv[i], "r");
char buf[100];
int bytes_read = 0;
char * inputCopy = malloc(255 * sizeof(char));
const char delim[] = " ";
while (fgets(buf, sizeof buf, fp) != NULL) {
if (strstr(buffer, "#!/")) {
} else {
char ** strtoken = string_separator(buf, '\n');
char * firstWord = getFirstWord(buf, inputCopy, delim);
pid_t pid;
pid = fork();
if (pid == 0) {
execvpe(firstWord, strtoken, NULL);
exit(1);
} else {
int status;
waitpid(pid, & status, 0);
}
}
}
}
return 0;
}
Why is the program not working with the sleep command?
EDIT: Made the modification in the code such as function definition and indentation, I think the problem might be with the string_separator function, it might not separate the string into char array as expected.

Related

Removing extra line at the end of a file parsed into a double dimensional array of characters (char **) in C

I am currently developing a function in C that parses a file into a double dimensional array of characters (char **), the problem is that I get an extra line at the end, and I don't see how to fix that.
Can you help me?
Ps: My school requires me to use getline() and fopen().
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void my_free_word_array(char **word_array)
{
size_t i = 0;
if (!word_array) {
return;
}
while (word_array[i] != NULL) {
free(word_array[i]);
++i;
}
free(word_array);
}
ssize_t my_put_str_arr(char **arr, int fd)
{
char cr = '\n';
ssize_t count = 0;
size_t i = 0;
if (!arr)
return -1;
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
count += write(fd, &cr, 1);
i++;
}
return count;
}
void append_word_array(char ***array, char *line)
{
size_t array_len = 0;
while ((*array)[array_len] != NULL) {
array_len++;
}
size_t len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
(*array)[array_len] = strdup(line);
(*array) = realloc((*array), (array_len + 2) * sizeof(char *));
(*array)[array_len + 1] = NULL;
}
void fill_from_file(char ***array, FILE *file)
{
char *line_buff = NULL;
size_t line_buff_size = 0;
ssize_t line_size = getline(&line_buff, &line_buff_size, file);
while (line_size >= 0) {
append_word_array(array, line_buff);
free(line_buff);
line_buff = NULL;
line_size = getline(&line_buff, &line_buff_size, file);
}
free(line_buff);
}
char **my_load_file_to_word_array(const char *filepath)
{
char **word_array = NULL;
FILE *file = fopen(filepath, "r");
if (!file) {
return NULL;
}
word_array = malloc(sizeof(char *));
if (!word_array) {
return NULL;
}
word_array[0] = NULL;
fill_from_file(&word_array, file);
fclose(file);
return word_array;
}
int main (int argc, char **argv)
{
char **file = my_load_file_to_word_array(argv[1]);
my_put_str_arr(file, 1);
my_free_word_array(file);
return 0;
}
Here is the content of the tested file (I added the \n \0 to make it easier for you to see):
My name is Saul.\n
I am Saul Goodman.\n
Better call Saul.\0
And this is the result I get :
My name is Saul.
I am Saul Goodman.
Better call Saul.
The "problem" with your code is that the function my_put_str_arr() prints the stored lines eached followed by a single \n character. If you don't want to print the last \n you would need to test if a next line exists. You could change your loop as follows:
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
i++;
if (arr[i]) {
count += write(fd, &cr, 1);
}
}

Get_next_line : Abort when BUFFER_SIZE between 8 and 15 on Mac

I am currently completing my "Get_next_line" project. It is a function that reads a file and allows you to read a line ending with a newline character from a file descriptor. When you call the function again on the same file, it grabs the next line. This project deals with memory allocation and when to free and allocate memory to prevent leaks. The value (-1) is returned if an error occurred, (0) is returned if the file is finished reading, and (1) is returned if a line is read.
However, while testing my final code I encounter an abort when BUFFER_SIZE is between 8 and 15. This error only appears on Mac. Does anyone have an idea of what might be the issue?
Compile with :
gcc -Wall -Wextra -Werror -D BUFFER_SIZE=32 get_next_line.c get_next_line_utils.c
The get_next_line() function and some support code;
#include "get_next_line.h"
int ft_backslash(const char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == '\n')
return (1);
if (s[i] == '\n')
return (1);
return (0);
}
int ft_read_buffer(int fd, char *buf)
{
int ret;
ret = 0;
ret = read(fd, buf, BUFFER_SIZE);
if (ret < 0)
return (-1);
buf[ret] = '\0';
return (ret);
}
char *ft_treat_save(char *save, char *buf)
{
char *tmp;
if (save == NULL)
save = ft_strdup(buf);
else
{
tmp = ft_strdup(save);
free (save);
save = ft_strjoin(tmp, buf);
free (tmp);
}
return (save);
}
char *ft_treat_tmp(char *save)
{
char *tmp;
tmp = ft_strdup(save);
free (save);
return (tmp);
}
int get_next_line(int fd, char **line)
{
int ret;
char buf[BUFFER_SIZE];
char *tmp;
static char *save;
ret = 1;
if (fd < 0 || fd > 255 || BUFFER_SIZE <= 0 || line == NULL)
return (-1);
*line = NULL;
tmp = NULL;
while (ret > 0)
{
if ((ret = ft_read_buffer(fd, buf)) < 0)
return (-1);
save = ft_treat_save(save, buf);
if (ft_backslash(save) == 1)
{
tmp = ft_treat_tmp(save);
*line = ft_strcut_front(tmp);
save = ft_strcut_back(tmp);
return (1);
}
}
tmp = ft_treat_tmp(save);
save = NULL;
*line = ft_strdup(tmp);
free (tmp);
return (0);
}
Other support code:
size_t ft_strlen(char *str)
{
size_t i;
i = 0;
if (str)
while (str[i])
i++;
return (i);
}
char *ft_strdup(char *str)
{
int i;
char *dst;
dst = malloc(sizeof(char) * ((ft_strlen(str) + 1)));
if (!(dst))
{
free (dst);
return (NULL);
}
i = -1;
while (str[++i])
dst[i] = str[i];
dst[i] = '\0';
return (dst);
}
char *ft_strjoin(char *s1, char *s2)
{
int i;
int j;
char *join;
i = -1;
j = 0;
if (!s1 && !s2)
return (NULL);
join = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 2));
if (!(join))
return (NULL);
if (BUFFER_SIZE == 1)
while (s1[++i + 1] != '\0')
join[i] = s1[i];
else
while (s1[++i] != '\0')
join[i] = s1[i];
while (s2[j])
join[i++] = s2[j++];
join[i] = '\0';
return (join);
}
char *ft_strcut_front(char *str)
{
int i;
char *front;
i = 0;
while (str[i] != '\n')
i++;
front = malloc(sizeof(char) * (i + 1));
if (!(front))
return (NULL);
i = 0;
while (str[i] != '\n')
{
front[i] = str[i];
i++;
}
front[i] = '\0';
return (front);
}
char *ft_strcut_back(char *str)
{
int i;
int j;
char *back;
i = 0;
while (str[i] != '\n')
i++;
i++;
back = malloc(sizeof(char) * ((ft_strlen(str) - i) + 1));
if (!(back))
return (NULL);
j = 0;
while (str[i] != '\0')
back[j++] = str[i++];
back[j] = '\0';
free (str);
return (back);
}
The main() function:
int main(void)
{
int fd, ret, line_count;
char *line;
line_count = 1;
ret = 0;
line = NULL;
fd = open("baudelaire.txt", O_RDONLY);
while ((ret = get_next_line(fd, &line)) > 0)
{
printf(" \n [ Return: %d ] | A line has been read #%d => |%s|\n", ret, line_count, line);
line_count++;
}
printf(" \n [ Return: %d ] A line has been read #%d: |%s\n", ret, line_count++, line);
printf("\n");
if (ret == -1)
printf("-----------\n An error happened\n");
else if (ret == 0)
{
printf("-----------\n EOF has been reached\n");
}
close(fd);
}
Problem solved. I forgot to add + 1 to my buf size... stupid error ! Thank you

executing commands in own shell - C

I have troubles with creating my own shell in C. Here is the code I have so far
#include "interpreter.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
void readconfig(int *timeout, char *file){
FILE * fp;
char * line = NULL;
size_t len = 0;
char * token;
fp = fopen(CONFIG, "r");
if (fp == NULL)
{
*timeout = DEFAULTTIME;
}
else
{
while ((getline(&line, &len, fp)) != -1){
token = strtok(line, TOKEN);
if ((strcmp(token,TIME_PERIOD) == 0)){
*timeout = atoi(strtok(NULL, TOKEN));
}
if ((strcmp(token,HISTORY)==0)){
strcpy(file,trim_command(strtok(NULL, TOKEN)));
}
}
}
}
int waitPeriod(pid_t pid){
if(pid) {
int stat_val;
pid_t child_pid;
int time = 0;
while(time < TIME_PERIOD){
child_pid = waitpid(pid, &stat_val, WNOHANG);
if (child_pid == pid){
break;
} else if (child_pid == 0) {
time++;
sleep(1);
}
}
if (time == TIME_PERIOD){
printf("Process ended. Execution time was over. \n");
kill(pid, SIGINT);
if (kill(pid, 0) == 0){
kill(pid, SIGKILL);
}
}
if(WIFEXITED(stat_val)) {
return WEXITSTATUS(stat_val);
}
return 0;
} else {
return -1;
}
}
void splitCommand(char ** parameters, char * command, int count){
char * cm = strtok(command, " ");
int i = 0;
while (cm != NULL){
parameters[i] = malloc(strlen(cm) * sizeof(char *));
parameters[i] = cm;
i++;
cm = strtok (NULL, " ");
}
i++;
parameters[i] = NULL;
}
int getParamsCount(char command[]){
int i;
int count = 1;
int com_length = strlen(command);
for(i=0; i<com_length; i++){
if (command[i] == ' '){
count++;
}
}
return count;
}
void getHistory(FILE * history_file, char ** history_commands){
char line[MAX_INPUT];
int i = 0;
int j;
rewind(history_file);
while (fgets(line, MAX_INPUT, history_file)!= NULL){
history_commands[i] = malloc(strlen(line) * sizeof(char *));
history_commands[i] = strdup(line);
for(j=0; j<MAX_INPUT; j++){
if (history_commands[i][j] == '\n'){
history_commands[i][j] = '\0';
break;
}
}
i++;
}
}
int getNumberOfRows(FILE * history_file){
rewind(history_file);
int lines = 0;
char ch;
while(!feof(history_file)){
ch = fgetc(history_file);
if(ch == '\n'){
lines++;
}
}
return lines;
}
void printCommands(char ** history_commands, int lines){
int i = 0;
int j = 0;
for(j = i; j < lines; j++){
printf("[%d] %s\n", j, history_commands[j]);
}
}
int runCommand(pid_t pid, char * command, char ** parameters, int child){
int count = 0;
switch(pid) {
case -1 :
return -1;
case 0 :
count = getParamsCount(command);
parameters = malloc((count+1) * sizeof(char *));
splitCommand(parameters, command, count);
if (strncmp("cd", parameters[0], 2) == 0){
chdir(parameters[1]);
} else {
execvp(parameters[0], parameters[0]);
}
free(parameters);
return 0;
default :
child = waitPeriod(pid);
printf("Child status: %d\n", child);
command[0] = 0;
free(command);
return 0;
}
}
char * trim_command(char * string){
int l = strlen(string);
int i;
for (i = 0; i<l; i++){
if (string[i] == '\n'){
string[i] = '\0';
}
}
return string;
}
int main(){
char * command;
FILE * history;
char cwd[MAX_INPUT];
pid_t pid = 0;
int child = 0;
int number_of_rows = 0;
int count = 0;
char ** history_commands;
char ** parameters[10];
printf(" Safe comand line interpreter \n");
printf(" Type \"quit\" to cancel interpreter or command \n");
printf(" Type \"history\" to show full command history \n");
printf(" Type \"history number\" to execute command from history \n");
printf("________________________________________________________________________________________\n");
sleep(1);
history = fopen(HISTORY, "a+");
if (history == NULL){
printf("Could not open history file\n");
return -1;
}
while(1){
getcwd(cwd, sizeof(cwd));
printf("Command: [%d]%s : ", child, cwd);
command = malloc(MAX_INPUT * sizeof(char *));
fgets(command, MAX_INPUT, stdin);
trim_command(command);
if (strncmp(QUIT, command, 4) == 0) {
fclose(history);
return EXIT_SUCCESS;
} else if (strncmp("history", command, 7) == 0) {
number_of_rows = getNumberOfRows(history);
history_commands = malloc(number_of_rows * sizeof(char *));
getHistory(history, history_commands);
count = getParamsCount(command);
if (strcmp("history", command) == 0){
printf("HISTORY OF COMMANDS\n");
printCommands(history_commands, number_of_rows);
} else {
char * cm = strtok(command, " ");
cm = strtok(NULL, " ");
int x;
x = atoi(cm);
if (x >= number_of_rows){
printf("Command does not exist\n");
continue;
}
command = strdup(history_commands[x]);
count = getParamsCount(command);
trim_command(command);
fprintf(history, "%s\n", command);
pid = fork();
runCommand(pid, command, parameters, child);
}
} else if (command[0] != 0){
fprintf(history, "%s\n", command);
pid = fork();
runCommand(pid, command, parameters, child);
}
}
fclose(history);
return 0;
}
The problem is, it doesn't execute cd command. When I comment malloc and free lines in runCommand method, cd command starts working, but every other command without parameter is not working...
could somebody help me with this?

reading a text file and sort it in a new file

I have a school assignment in which we have to read a text file, sort the words by alphabetical order and write the result into a new text file.
I've already got a program that can read the file and print it on the screen and a different program to sort words which you have to type in. Now I'm trying to merge these two programs, so that the data which been read out of the file will be put into the sorting program.
The program that we use to make the code is called CodeBlocks. Below are the two programs. I hope that you can give me advice and an example how to fix this because I tried everything I know but couldn't get it working.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#define MAX_NUMBER_WORDS 100
char* ReadFile(char *filename)
{
char *buffer = NULL;
int string_size, read_size;
FILE *handler = fopen(filename, "r");
if (handler)
{
//seek the last byte of the file
fseek(handler, 0, SEEK_END);
//offset from the first to the last byte, or in other words, filesize
string_size = ftell(handler);
//go back to the start of the file
rewind(handler);
//allocate a string that can hold it all
buffer = (char*)malloc(sizeof(char) * (string_size + 1));
//read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, handler);
//fread doesnt set it so put a \0 in the last position
//and buffer is now officialy a string
buffer[string_size] = '\0';
if (string_size != read_size)
{
//something went wrong, throw away the memory and set
//the buffer to NULL
free(buffer);
buffer = NULL;
}
}
return buffer;
}
int numberOfWordsInDict(char **dict)
{
int i;
for (i = 0; i < MAX_NUMBER_WORDS; i++)
{
if (dict[i] == NULL)
return i;
}
return MAX_NUMBER_WORDS;
}
void printDict(char **dict)
{
int i;
printf("Dictionary:\n");
for (i = 0; i < numberOfWordsInDict(dict); i++)
printf("- %s\n", dict[i]);
if (numberOfWordsInDict(dict) == 0)
printf("The dictionary is empty.\n");
}
void swapWords(char **dict, char *word, char *word2)
{
int i, p1 = -1, p2 = -1;
char *tmp;
for (i = 0; i < numberOfWordsInDict(dict); i++)
{
if (strcmp(dict[i], word) == 0)
p1 = i;
if (strcmp(dict[i], word2) == 0)
p2 = i;
}
if (p1 != -1 && p2 != -1)
{
tmp = dict[p1];
dict[p1] = dict[p2];
dict[p2] = tmp;
}
}
void sortDict(char **dict)
{
int swap;
int i = 0;
do
{
swap = 0;
for (i = 0; i < numberOfWordsInDict(dict) - 1; i++)
{
if (strcmp(dict[i], dict[i + 1]) > 0)
{
swapWords(dict, dict[i], dict[i + 1]);
swap = 1;
}
}
} while (swap == 1);
}
void splitSentenceToWords(char **words, char *sentence)
{
int p1 = 0, p2 = 0;
int nrwords = 0;
char *word;
while (sentence[p2] != '\0')
{
if (isspace(sentence[p2]) && p1 != p2)
{
word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
words[nrwords] = word;
strncpy(words[nrwords], &sentence[p1], p2 - p1);
words[nrwords][p2 - p1] = '\0';
nrwords++;
p1 = p2 + 1;
p2 = p1;
}
else
{
p2++;
}
}
if (p1 != p2)
{
word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
words[nrwords] = word;
strncpy(words[nrwords], &sentence[p1], p2 - p1);
words[nrwords][p2 - p1] = '\0';
nrwords++;
p1 = p2 + 1;
p2 = p1;
}
}
int main(void)
{
char sentence[1024];
char *dict[MAX_NUMBER_WORDS] = {};
char *words[MAX_NUMBER_WORDS] = {};
char *string = ReadFile("test.txt");
if (string)
{
puts(string);
free(string);
}
//printf("Type een zin in: ");
scanf("%[^\n]s", &sentence);
splitSentenceToWords(words, &sentence);
printDict(words);
printf("Words has been sorted\n");
sortDict(words);
printDict(words);
return 0;
}
You are on the right track. The problem is that after your read in your file, you are not using the input to build your word list. Instead of;
splitSentenceToWords(words, &sentence);
try:
splitSentenceToWords(words, &string);
Delete
free(string)
This will get you started. You will have to clean this up when you understand it a bit better.

Find longest comment line in a file in C

So I have this function to find the longest line in a file:
int LongestLine(FILE *filename) {
char buf[MAX_LINE_LENGTH] = {0};
char line_val[MAX_LINE_LENGTH] = {0};
int line_len = -1;
int line_num = -1;
int cur_line = 1;
filename = fopen(filename, "r");
while(fgets(buf, MAX_LINE_LENGTH, filename) != NULL) {
int len_tmp = strlen(buf) - 1;
if(buf[len_tmp] == '\n')
buf[len_tmp] = '\0';
if(line_len < len_tmp) {
strncpy(line_val, buf, len_tmp + 1);
line_len = len_tmp;
line_num = cur_line;
}
cur_line++;
}
return line_num;
}
and I was thinking of combining it with this one:
bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
But.. however, the LongestLine() function returns an integer. So how can I use both functions so that I may find the longest line starting with let's say //?
Add a call to startsWith (to see if it is a comment) in your if statement to decide if a line is the new longest:
if( startsWith("//",buf) && (line_len < len_tmp) ) {

Resources