So I have created a char** variable called myargv which should act like an array that stores strings at each index. I have a function that returns a char** to the myargv and the function seems to work fine when I print everything out from within, but when I try to print out stuff from the myargv from main, it doesn't work anymore.... Can anyone help me?
char **findArgs(char *line)
{
int i = 0;
char **temp, *tokTemp;
char **myargv;
tokTemp = strtok(line, " ");
myargv = malloc(sizeof(*myargv));
while (tokTemp != NULL)
{
if (strcmp(tokTemp, ">") == 0 || strcmp(tokTemp, "<") == 0 || strcmp(tokTemp, ">>") == 0)
{
break;
}
else
{
myargv[i] = malloc(strlen(tokTemp) + 1);
//myargv[i] = malloc(sizeof(char)*strlen(tokTemp));
strcpy(myargv[i], tokTemp);
//printf("myargv[%d] = %s \n", i, myargv[i]);
temp = realloc(myargv, (i+2)*sizeof(*myargv));
if (temp != NULL)
{
myargv = temp;
}
tokTemp = strtok(NULL, " ");
i++;
}
}
myargv[i] = NULL;
//printf("myargv[0] = %s\n", myargv[0]);
return myargv;
}
int main(int argc, char *argv[], char *env[])
{
int cmdInt, pid, status, i = 0, ioNumber = 0;
char input[64], lineBUFFER[64], lineBUFFER2[64], lineBUFFER3[64], lineBUFFER4[64], homePath[64], fileName[64], cmdPathFINAL[64];
char *cmd;
char **myargv, **cmdPath;
int myFile;
while(1)
{
printf("command: ");
gets(input);
strcpy(lineBUFFER, input);
strcpy(lineBUFFER2, input);
strcpy(lineBUFFER3, input);
strcpy(lineBUFFER4, input);
cmd = strtok(lineBUFFER3, " ");
cmdInt = findCommand(lineBUFFER2);
ioNumber = ioCheck(lineBUFFER4, fileName);
//printf("ioCheck = %d \n", ioNumber);
//printf("Filename: %s \n", fileName);
myargv = findArgs(lineBUFFER);
//printf("myargv[0] = %s\n", myargv[0]);
findHome(env, homePath);
//printf("Home path = %s\n", homePath);
switch(cmdInt)
{
case 0 :
if (myargv[1] == NULL)
{
chdir(homePath);
}
else
{
//printf("1st argument: %s, 2nd argument: %s, 3rd argument: %s \n", myargv[1], myargv[2], myargv[3]);
chdir(myargv[1]);
}
break;
case 1 :
exit(1);
break;
default :
pid = fork();
if (pid == 0)
{
//printf("Parent %d waits for child %d to die. \n", getpid(), pid);
pid = wait(&status);
printf("dead child = %d, how = %04x \n", pid, status);
exit(100);
}
else
{
ioNumber = ioCheck(lineBUFFER4, fileName);
//printf("ioCheck = %d \n", ioNumber);
//printf("Filename: %s \n", fileName);
cmdPath = getPath2(env);
//printf("cmdPath[0] = %s\n", cmdPath[0]);
findPath(cmd, cmdPath, cmdPathFINAL);
printf("Command Path = %s\n", cmdPathFINAL);
if (ioNumber == 1)
{
close(0);
myFile = open(fileName, O_RDONLY);
}
else if (ioNumber == 2)
{
close(1);
myFile = open(fileName, O_WRONLY|O_CREAT, 0644);
}
else if (ioNumber == 3)
{
close(1);
myFile = open(fileName, O_WRONLY|O_APPEND);
}
execve(cmdPathFINAL, myargv, env);
//printf("child %d dies by exit () \n", getpid());
exit(100);
}
break;
}
}
if (ioNumber == 1 || ioNumber == 2 || ioNumber == 3)
{
close(myFile);
}
return 0;
}
The following works on both Window and SuSE linux 10.4 x86_64 kernel 2.6.16. There are two versions of the parser: getArgs and getArgs2. Didn't give the code a thorough review, but hopefully it will help you.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int getArgs(const char *line, char *args[], int argc)
{
int len, i = 0;
const char* delims = " ";
char *ltok = strdup(line);
char *tok = strtok(ltok, delims);
for (; tok != NULL; i++) {
if (strchr("><|", *tok) != NULL) /* not sure why this test is needed for CLI */
break;
if (args != NULL && i < argc) {
args[i] = strdup(tok);
}
tok = strtok(NULL, delims);
}
free(ltok);
return i;
}
int getArgs2(const char *line, char **args, int argc)
{
int len, i = 0;
const char *p, *q;
const char *delims = " ";
p = line;
while (strchr(delims, *p) != NULL && *p != '\0')
p++;
q = p;
while (strchr(delims, *q) == NULL && *q != '\0')
q++;
for (; q > p; i++) {
if (strchr("><|", *p) != NULL) /* not sure why this test is needed for CLI */
break;
len = q-p;
if (args != NULL && i < argc) {
args[i] = (char *)malloc(len + 1);
strncpy(args[i], p, len)[len] = '\0';
}
p = q;
while (strchr(delims, *p) != NULL && *p != '\0')
p++;
q = p;
while (strchr(delims, *q) == NULL && *q != '\0')
q++;
}
return i;
}
int main(int argc, char *argv[])
{
char line[256], *q;
char **args;
int count, n;
for(;;) {
printf("command: ");
if (fgets(line, sizeof(line)/sizeof(line[0]), stdin) == NULL)
break;
q = line;
while (isspace(*q))
q++;
if (*q == '\0')
break;
while (*q != '\0' && *q != '\n' && *q != '\r')
q++;
*q = '\0';
for(count = 0, q = line; *q != '\0'; q++)
if (*q == ' ')
count++;
printf("Input command: %d character(s), %d space(s) for \"%s\"\n", strlen(line), count, line);
count = getArgs(line, NULL, 0);
if (count > 0) {
args = (char **)malloc(sizeof(char *)*count);
count = getArgs(line, args, count);
} else {
args = NULL;
}
printf("Parsed %d arg(s) from line\n", count);
printf("--------------------------------------\n");
for (n = 0; n < count; n++) {
printf("%d: %s\n", n+1, args[n]);
free(args[n]);
}
free(args);
}
return 0;
}
Related
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
I have csv file with below format :
name,birthmonth,country,hobby
jack,jan,england,soccer
roben,july,germany,soccer
emma,dec,china,tennis
yannick,sep,france,music
alex,nov,england,cricket
thomas,apr,germany,tennis
mike,oct,netherlands,cycling
michelle,feb,france,poetry
yui,mar,japan,coding
feng,jun,china,reading
I want to parse this file using C, and put all the lines with same country name in a consecutive manner i.e shown below:
name,birthmonth,country,hobby
jack,jan,england,soccer
alex,nov,england,cricket
roben,july,germany,soccer
thomas,apr,germany,tennis
emma,dec,china,tennis
feng,jun,china,reading
yannick,sep,france,music
michelle,feb,france,poetry
mike,oct,netherlands,cycling
yui,mar,japan,coding
So far, I have tried this code below, however not able to match things properly and proceed further:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<fcntl.h>
#include<string.h>
int main (int argc, char **argv) {
//int line;
char line[200];
char *inputFile = argv[1];
FILE *input_csv_file;
char a,b,c,d,e;
input_csv_file = fopen(inputFile, "rt");
if(input_csv_file ==0) {
printf("Can not open input file \n");
}
else {
//while((line = fgetc(input_csv_file)) != EOF) {
while(fgets(line, sizeof line, input_csv_file) != NULL) {
printf ("line = %s\n", line);
if(sscanf(line, "%s,%s,%s,%s,%s", a,b,c,d,e)) {
//if(sscanf(line, "%[^,], %[^,], %[^,], %[^,], %[^,]", a,b,c,d,e)) {
printf("d=%s\n",d);
}
}
}
return 0;
}
I am a newbie in C/C++. Any help would be much appreciated
Thanks.
I could write the code to get the required output. Below is the code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<fcntl.h>
#include<string.h>
int main(int argc, char ** argv)
{
struct filedata {
char nation[8];
char content[50];
};
char line[100];
char *inputFile = argv[1];
FILE *input_csv_file;
int iter = 0, c;
char * tok;
int count = 0;
char ch;
char country[] = "country";
char header_line[50];
input_csv_file = fopen(inputFile, "rt");
//count line numbers of the input csv
for(ch = getc(input_csv_file); ch!= EOF; ch=getc(input_csv_file))
if(ch == '\n')
count = count + 1;
fclose(input_csv_file);
count = count -1;
struct filedata * record[count];
input_csv_file = fopen(inputFile, "rt");
if(input_csv_file == 0)
{
printf("Can not open input file\n");
} else
{
while(fgets(line, sizeof line, input_csv_file) != NULL)
{
//printf("-- line = %s\n", line);
int s_line = sizeof line;
char dup_line[s_line];
strcpy(dup_line, line);
int h = 0;
int s_token;
tok = strtok(line, ",");
while(tok != NULL)
{
h++;
if(h == 3)
{
s_token = sizeof tok;
break;
}
tok = strtok(NULL, ",");
}
// skipping the line having column headers
if(compare_col(tok, country) == 0) {
strcpy(header_line, dup_line);
continue;
}
iter++;
c = iter - 1;
record[c] = (struct filedata*)malloc(sizeof(struct filedata));
strcpy(record[c]->nation, tok);
strcpy(record[c]->content, dup_line);
} //while
struct filedata * temp;
FILE * fptr;
fptr = fopen("nation_csv.txt", "w");
if(fptr == NULL)
{
printf("Error in opening the file to write\n");
exit(1);
}
// sorting the arr of struct nation wise
for(iter=1; iter < count; iter++)
for(c =0 ; c < count -1; c++) {
if(strcmp(record[c]->nation, record[c+1]->nation) > 0) {
temp = record[c];
record[c] = record[c+1];
record[c+1] = temp;
}
}
for(iter=0; iter < count; ++iter)
{
if(iter == 0) {
fprintf(fptr, "%s", header_line);
continue;
}
fprintf(fptr, "%s", record[iter]->content);
}
fclose(fptr);
}
fclose(input_csv_file);
}
int compare_col(char a[], char b[] )
{
int c = 0;
while(a[c] == b[c]) {
if(a[c] == '\0' || b[c] == '\0')
break;
c++;
}
if(a[c] == '\0' && b[c] == '\0')
return 0;
else
return -1;
}
Thanks for all your inputs. Any further inputs to make it better are much appreciated.
Thanks
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?
I am new to C programming and currently learning this into a course. I'm facing an issues while trying to practice the below history function.
I'm able to display the shell commands. However, when I type history, the past shell commands are not getting saved into the history buffer.
Can anyone help me to find where I went wrong?
Here is my code:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define BUFSIZE 20
#define MAX_WORD_IN_LINE 20
int tokenize(char *str, char **args)
{
int i, argc = 0;
char *token;
token = strtok(str," \t\n");
for(i=0; token!=NULL;i++)
{
args[i] = token;
printf("args[%d] = %s\n", i, args[i]);
token = strtok(NULL, " \t\n");
argc++;
}
return argc;
}
void display_strings(char **p)
{
if (p == NULL) return;
while(*p != NULL){
printf("%s\n",*p);
p++;
}
}
int history(char *hist[], int current){
int i = current;
int hist_num = 1;
do {
if (hist[i]) {
printf("%4d %s\n", hist_num, hist[i]);
hist_num++;
}
i = (i + 1) % BUFSIZE;
} while (i != current);
return 0;
}
int main(void){
char *args[MAX_WORD_IN_LINE];
char buffer[BUFSIZE];
char *hist[BUFSIZE];
int i,current=0;
pid_t pid;
int argc;
for(i=0;i<BUFSIZE;i++)
hist[i]= NULL;
while(1) {
memset(args,0,MAX_WORD_IN_LINE);
printf("osh> ");
fgets(buffer, BUFSIZE, stdin);
argc = tokenize(buffer, args);
//display_strings(args);
// skip on empty command
if (argc == 0) continue;
if (strcmp(args[0],"quit") == 0) break;
else if (strcmp(args[0], "hello") == 0) printf("Hello there. How are you?\n");
else if (strcmp(args[0],"history")==0) history(hist,current);
else {
pid = fork();
if (pid == 0) {
hist[current]=strdup(args[0]);
current++;
execvp(args[0], args);
return 0;
}
You need to make a copy of the string that args[0] points to when you save it in hist. Currently, you're just assigning the pointer to the current args[0], and it will be overwritten by the next command. When you print the history, you'll just get the last command repeatedly. So use:
hist[current] = strdup(args[0]);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Search and Print all non-duplicate struct names inside input file */
int main(int argc, char *argv[])
{
char temp[64], buf[64], filename[128], array[1024] = "";
char *ptr, *line = NULL;
char *tmp1, *tmp2;
ssize_t rv;
size_t len;
int count = 0;
FILE *fp;
if (argc < 2) {
printf("enter file name at cmd line...\n");
return -1;
}
sprintf(filename, argv[1], strlen(argv[1]));
fp = fopen(argv[1], "r");
if (!fp) {
printf("File could not be opened: %s\n", argv[1]);
return -1;
}
while ((rv = getline(&line, &len, fp)) != -1) {
ptr = strstr(line, "struct");
if (ptr) {
ptr += strlen("struct");
while (*ptr == ' ')
ptr++;
tmp1 = strchr(ptr, ' ');
tmp2 = strchr(ptr, ';');
len = 0;
if (tmp1 == NULL && tmp2 == NULL) {
continue;
}
else if (tmp1 == NULL && tmp2 != NULL) {
len = tmp2 - ptr;
}
else if (tmp1 != NULL && tmp2 == NULL) {
len = tmp1 - ptr;
}
else if (tmp1 && tmp2) {
len = tmp1 < tmp2 ? tmp1 - ptr : tmp2 - ptr;
}
if (len) {
snprintf(temp, len+1, "%s", ptr);
if (!strstr(array, temp)) {
sprintf(buf, "%2d. ", count++);
strcat(buf, temp);
strcat(array, buf);
strcat(array, "\n");
}
}
}
}
fclose(fp);
if (line)
free(line);
printf("%s\n", array);
return 0;
}
Above program finds struct names correctly, however I see chars like , and ) at the end of output names. How to remove it? Below is sample output:
[root#mnm-server programs]# ./a.out /usr/src/linux/drivers/net/ethernet/smsc/smsc911x.c
0. smsc911x_data
1. smsc911x_ops
2. smsc911x_platform_config
3. phy_device
4. mii_bus
5. net_device
6. napi_struct
7. regulator_bulk_data
8. clk
9. platform_device
10. smsc911x_data,
11. sk_buff
12. net_device_stats
13. netdev_hw_addr
14. sockaddr
15. ethtool_drvinfo
16. ethtool_eeprom
17. ethtool_ops
18. net_device_ops
19. ures,
20. resource
21. device_node
22. smsc911x_data))
23. dev_pm_ops
24. of_device_id
25. platform_driver
Notice output of line 10 and 22. One approach would be to do strchr for ,, ), ; and remove char from end. However, this is not a clean solution if the number of non-alphabetic characters increases.
NOTE: The best solution I found for this is here.
Thanks to inputs from Daniel Jour, the following code handles all cases of struct name* ptr;, struct name{ };, struct { };
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* Search and Print all non-duplicate struct names inside input file */
int main(int argc, char *argv[])
{
char temp[64], buf[64], filename[128], array[1024] = "";
char *ptr, *line = NULL;
size_t len;
int count = 0, flag = 0;
FILE *fp;
if (argc < 2) {
printf("enter file name at cmd line...\n");
return EXIT_FAILURE;
}
sprintf(filename, argv[1], strlen(argv[1]));
fp = fopen(argv[1], "r");
if (!fp) {
printf("File could not be opened: %s\n", argv[1]);
return EXIT_FAILURE;
}
while ((getline(&line, &len, fp)) != -1) {
ptr = flag ? line : strstr(line, "struct ");
if (ptr) {
if (!flag)
ptr += strlen("struct ");
while (*ptr == ' ')
ptr++;
len = 0;
while (isalnum(*ptr) || *ptr == '_' || *ptr == '{' || *ptr == '}') {
if (*ptr == '{') {
flag++;
}
else if (*ptr == '}') {
len = 0;
flag--;
do {
ptr++;
} while (*ptr == ' ');
ptr--;
}
else if ((*ptr != '{') || (*ptr != '}')) {
len++;
}
ptr++;
}
if (len && !flag) {
ptr -= len;
snprintf(temp, len+1, "%s", ptr);
if (!strstr(array, temp)) {
sprintf(buf, "%2d. ", count++);
strcat(buf, temp);
strcat(array, buf);
strcat(array, "\n");
}
}
}
}
fclose(fp);
if (line)
free(line);
printf("%s\n", array);
return EXIT_SUCCESS;
}
This program doesn't handle cases like func(struct x, struct y), interested users can fix it or just use grep -o "struct [^ ;,)]\+" # | awk '{print $2}' | sort -u. Output of the above program for pre-processed file hworld.i:
[root#server]# cat hworld.c
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
[root#server]# gcc -Wall --save-temps hworld.c
[root#server]# ./find_structs hworld.i
0. _IO_FILE
1. _IO_marker
2. _IO_FILE_plus