Current Events > Last time I'll post about this, but anyone know C really well? I have an error

Topic List
Page List: 1
Milkman5
08/18/17 9:25:26 PM
#1:


in my code and I can't figure it out.

I tried over and over to fix it and it still doesn't work.

I just need someone who knows C to look over it and maybe see what's wrong
... Copied to Clipboard!
Manocheese
08/18/17 9:26:18 PM
#2:


I know all about C!

https://www.youtube.com/watch?v=Ye8mB6VsUHw

---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
treewojima
08/18/17 9:29:36 PM
#3:


post the code
... Copied to Clipboard!
Milkman5
08/18/17 9:35:50 PM
#5:


#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]){

int i=1, files=argc-1;
pid_t cpid;

struct stat sb;
int userID = getuid();
int groupID = getgid();


if(argc < 2){
printf("failure");
printf("\n");
}
else{
while(--argc > 0){
*++argv;
cpid = fork();
if (cpid == 0) {
printf("File: ");
printf("%s", argv[i+1]);
printf("\n");

stat(argv[i+1], &sb);

//For user
if (userID = sb.st_uid){
printf("You have owner permissions: ");
printf( (sb.st_mode & S_IRUSR) ? "read " : "");
printf( (sb.st_mode & S_IWUSR) ? "write " : "");
printf( (sb.st_mode & S_IXUSR) ? "execute" : "");
printf("\n");
}


//For group
else if (groupID = sb.st_gid){
printf("You have group permissions: ");
printf( (sb.st_mode & S_IRGRP) ? "read " : "");
printf( (sb.st_mode & S_IWGRP) ? "write " : "");
printf( (sb.st_mode & S_IXGRP) ? "execute " : "");
printf("\n");
}

//For other/general
else{
printf("You have general permissions: ");
printf( (sb.st_mode & S_IROTH) ? "read " : "");
printf( (sb.st_mode & S_IWOTH) ? "write " : "");
printf( (sb.st_mode & S_IXOTH) ? "execute" : "");
printf("\n");
}


exit(0);
}
}
if (cpid != 0) {
for (i=1; i <= files; ++i) {
wait();
}
}
}
}
... Copied to Clipboard!
Kaname_Madoka
08/18/17 9:36:09 PM
#6:


nice pastebin
---
Drawn for me by | Popcorn_Fairy: https://i.imgtc.com/cvSNxRT.jpg | Volkswagen_Bros: http://i.imgur.com/86XOVXb.jpg | ShinobiNinjaX: https://imgur.com/bPb5vEV
... Copied to Clipboard!
Manocheese
08/18/17 9:36:15 PM
#7:


That's not a Pastebin.
---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
Milkman5
08/18/17 9:36:26 PM
#8:


I can't get stat to point to the right files on the command line
... Copied to Clipboard!
Milkman5
08/18/17 9:37:02 PM
#9:


Kaname_Madoka posted...
nice pastebin

Manocheese posted...
That's not a Pastebin.


I changed my mind because pastebin might show up on google and my prof will think I stole the code from myself
... Copied to Clipboard!
Milkman5
08/18/17 9:37:46 PM
#10:


it's really annoying to post your code online because everyone is so paranoid of plagiarism
... Copied to Clipboard!
Manocheese
08/18/17 9:38:07 PM
#11:


I'm not reading all that, but you probably didn't mean to do this:

if (userID = sb.st_uid)
---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
Kaname_Madoka
08/18/17 9:39:15 PM
#12:


just use pokepaste

no one will look there
---
Drawn for me by | Popcorn_Fairy: https://i.imgtc.com/cvSNxRT.jpg | Volkswagen_Bros: http://i.imgur.com/86XOVXb.jpg | ShinobiNinjaX: https://imgur.com/bPb5vEV
... Copied to Clipboard!
Milkman5
08/18/17 9:40:17 PM
#13:


Manocheese posted...
I'm not reading all that, but you probably didn't mean to do this:

if (userID = sb.st_uid)


it checks the user ID with the file's user ID.

userID is an int of getuid()


That's all correct, it works outside of child processes.


My problem is stat isn't working in loops/in child processes.

If I get rid of pid and all the loops, the code works how it is supposed to
... Copied to Clipboard!
Manocheese
08/18/17 10:00:51 PM
#14:


Have you tried writing to the console to trace the execution of the program?
---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
Milkman5
08/18/17 10:09:03 PM
#15:


Manocheese posted...
Have you tried writing to the console to trace the execution of the program?


what do you mean? how do I do that within a shell?
... Copied to Clipboard!
Manocheese
08/18/17 10:16:03 PM
#16:


That's a Unix question more than a C question, but I'm guessing you just call printf.
---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
Milkman5
08/18/17 10:33:45 PM
#17:


I'm already printing every step and it gives me the wrong thing
... Copied to Clipboard!
Hexagon
08/18/17 10:47:51 PM
#18:


That's some very interesting use of incrementing with ++, -- . I find it very hard to follow with all the functions that are defined else where. If you printed every step maybe could you tell us the first line where its wrong and what it should be so we can look for syntax errors or something.
... Copied to Clipboard!
Manocheese
08/18/17 11:27:36 PM
#19:


Are you sure you want > and not >= here?

while(--argc > 0)
---
()_() Hardcore - We'll probably be modded for this...
(o.o) http://manocheese.googlepages.com/manocheesery
... Copied to Clipboard!
Milkman5
08/18/17 11:36:51 PM
#20:


ok, I'll give it a shot.

Hexagon posted...
That's some very interesting use of incrementing with ++, -- . I find it very hard to follow with all the functions that are defined else where. If you printed every step maybe could you tell us the first line where its wrong and what it should be so we can look for syntax errors or something.



I've tried other incrementing methods, this is just what is left after fucking around.

I can post my original child code before the new stuff which I know 100% works, but I already tried dropping my stat code in there and it didn't work.

I've been trying to solve this for weeks and will likely fail this course because I can't solve this one issue
... Copied to Clipboard!
Milkman5
08/18/17 11:39:43 PM
#21:


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main (int argc, char *argv[]){

//if no filenames are given, it fails.
if(argc < 2){
printf("failure");
printf("\n");
}
else{

pid_t pids[argc-1];
int i;
int n = argc-1;


for(i=0;i<argc-1;i++){
if ((pids[i] = fork()) < 0){
perror("fork");
abort();
}
else if (pids[i] == 0) {//children
printf("Filename: ");
printf("%s", argv[i+1]);
printf(" ");
printf("PID: ");
printf("%ld", (long)getpid());
printf("\n");
sleep(1);
//^ to test for parallel code
exit(0);
}

}

int status;
pid_t pid;
while (n>0){
pid = wait(&status);
--n;
}

system("ps -H");


}
}

here was my previous code, it didn't work in this either even though I know this is function child/fork code
... Copied to Clipboard!
Milkman5
08/18/17 11:40:51 PM
#22:


Damn, I don't know what to do. I've asked CE for weeks, asked classmates what the error is and no one can solve it. I don't want to put it up on stack because it will show up on google.

How am I supposed to solve this? I just don't understand where I can even find what my issue is
... Copied to Clipboard!
Milkman5
08/18/17 11:56:02 PM
#23:


Holy shit, I solved it by starting all over.

Fuck! I may be prematurely celebrating. But I think it works now!
... Copied to Clipboard!
Milkman5
08/19/17 12:00:19 AM
#24:


Ok I figured it out, but now I'm having an issue with them running parallel and printing weird.
... Copied to Clipboard!
_Kowalski_
08/19/17 12:03:06 AM
#25:


You got this!
---
-daveypots-
... Copied to Clipboard!
Polycosm
08/19/17 12:07:44 AM
#26:


This topic just made me realize how much I've forgotten when it comes to programming. Like, I'm struggling to understand your code, and I was a CS major. Damn.

Anyway... I'm no help here. Good luck!
---
BKSheikah owned me so thoroughly in the 2017 guru contest, I'd swear he used the Lens of Truth to pick his bracket. (thengamer.com/guru)
... Copied to Clipboard!
Milkman5
08/19/17 12:20:48 AM
#27:


I solved it! It runs perfectly! I fixed the issue by printing everything within the same bracket!

oh my god, man! it actually works after all this time, I just had to rewrite it
... Copied to Clipboard!
Topic List
Page List: 1