OK, I got it to work!!! I don't know why what I changed made it work but heres what did. At the beginning of the whole thing it asks whether you want to open or delete a file, and for what we were trying to fix, I would type in open( all that really matters is the "o") but anyways I was using
Code:
scanf(" %s", choice)
The problem was comming later when I wanted to know the program the user wanted to open. The whole issue started because some programs had spaces and we were trying to make those work also. But, when it asked for the program you wanted to open it just skipped right past the fgets() step. But by changing the scanf() with the choice, to another fgets() statement it worked. I am guessing there was a newline character somewhere in the buffer we couldn't figure out how to get rid of. But it works now, heres the final code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
main()
{
char choice[8];
printf(" This program can open applications or delete files\n");
printf(" Please select what you like to do\n");
printf(" enter 'Delete' to delete files, or 'open' to open\n");
fgets(choice, 8, stdin);
if((toupper(choice[0]) == 'O')||(toupper(choice[0]) == 'D'))
{
if(toupper(choice[0]) == 'O')
{
char empty1[40];
char empty2[65];
char applic[25];
char applic2[28];
printf("Type in the name of the app");
fgets(applic, 28, stdin);
char *p = strchr(applic,'\n'); if ( p ) *p = '\0';
sprintf(applic2, "\"%s.app\"", applic);
sprintf(empty1, "open -a %s", applic2);
system(empty1);
}
if(toupper(choice[0]) == 'D')
{
char location[45], location2[46], name[23], systemname[28];
printf("Type the location of the file\n");
printf("Ex: Macintosh HD is in the folder '/Users/Alex/Desktop'\n");
printf("Keep in mind capitalization DOES COUNT\n");
printf("so now tell me the address of the file\n");
scanf(" %s", location);
sprintf(location2, "%s/", location);
printf("Now tell me the name and type of file it is\n");
printf("Ex: examplefile.txt\n");
scanf(" %s", name);
printf(" %s\n", name);
sprintf(systemname, "%s%s", location2, name);
printf("%s.....", systemname);
remove(systemname);
printf("deleted\n");
}
}
else
{
printf("An error occured please restart\n");
}
return 0;
}