How to pass a string into a system call in xv6 - c

I have to pass a string into a system call in xv6, so I know I have to use argstr(), my question lies with implementing it.
Here is what I have added to sysproc.c:
int sys_hello(char **str)
{
int n = 15;
argstr(n, **str);
for (int i = 0; i < n; i++){
cprintf("%c testplzprint",str[i]);
}
cprintf("Hello World\n");
return 0;
}
I am calling it from testcase.c:
#include "types.h"
#include "stat.h"
#include "user.h"
int main(void)
{
for (int i = 0; i < 5; i++){
hello("itworks");
}
exit();
}
I first implemented it without the testcase and argstr() and just printed hello and it worked well. I'm guessing my problem lies in how I am using argstr(). I really appreciate any help!
Update: sorry for not providing output, here it is right now.
js9313rr#smaug:/tmp/.x2go-js9313rr/media/disk/_cygdrive_C_Users_real0_Desktop_school_SPRING1_352/CSIS352/OS/OS lab 4/xv6-public-master$ make
gcc -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pie -no-pie -c -o sysproc.o sysproc.c
sysproc.c: In function \u2018sys_hello\u2019:
sysproc.c:97:13: error: passing argument 2 of \u2018argstr\u2019 makes pointer from integer without a cast [-Werror=int-conversion]
argstr(n, **str);
^
In file included from sysproc.c:3:0:
defs.h:154:17: note: expected \u2018char **\u2019 but argument is of type \u2018char\u2019
int argstr(int, char**);
^~~~~~
cc1: all warnings being treated as errors
<builtin>: recipe for target 'sysproc.o' failed
make: *** [sysproc.o] Error 1

make the hello prototype clear:
Do you want a array of strings ou just a string?
Your sys_hello prototype is wrong, it should be sys_hello(void).
Here an example with an hello syscall taking one string as parameter:
int sys_hello(void)
char *string;
if(argstr(0, &string) < 0){
return -1;
}
cprintf(string);
return 0;

Related

What does this error mean? "expected identifier or '('"

I'm new to coding and I don't know what to do with this.
#include <stdio.h>
int get_positive_int(void);
int main(void)
{
int i = get_positive_int();
printf("%i\n", i);
}
int get_positive_int(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n > 8 || n < 1);
return n;
}
for (int i = 1; i < 9; i++)
{
printf("%i#\n");
}
This is what I got:
$ make mario
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow mario.c -lcrypt -lcs50 -lm -o mario
mario.c:24:1: error: expected identifier or '('
for (int i = 1; i < 9; i++)
^
1 error generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
It seems you come from a script language background.
In C, all executed code (in contrast to declarations, prototypes, ...) has to be inside a function. It will also only be executed if the funciton is called.
Only main() is called automatically. Everything else has to be in functions which get called (indirectly) from there. (Skipping the discussion of interrrupts and other root context constructs...)
You do have a main() already.
Do you think that your goal can be achieved by moving the problematic code into that?
If not you have to plan which function contains that code and how/when it will be called.

gcc compiler ignores uninitialized variable warning for debug build

gcc compiler ignores uninitialized variable warning for debug build. This looks very weird for me, can some one help me to understand this ?
## Program
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, max;
int count;
if (argc < 2) {
return -1;
}
max = atoi(argv[1]);
for (i = 0; i < max; i++) {
count++;
}
printf("count is %d\n", count);
return 0;
}
gcc a.c -g -Wall -Werror
No warning
gcc a.c -O3 -Wall -Werror
a.c: In function ‘main’:
a.c:8:9: error: ‘count’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
int count;
^~~~~
cc1: all warnings being treated as errors
gcc version: 7.4.0
Though it may look weird, this behavior is documented for -Wmaybe-uninitialized gcc option:
-Wmaybe-uninitialized
For an automatic (i.e. local) variable, if there exists a path from the function entry to a use of the variable that is initialized, but there exist some other paths for which the variable is not initialized, the compiler emits a warning if it cannot prove the uninitialized paths are not executed at run time.
These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables.
I guess the reason is that the cost of analyzing not initialized variables is too much for not optimizing compilation. That's why it is done only for optimizing one.

C palindrome program - undefined reference to main

I wrote an function, which checks, if string of char is palindrome or not.
//pan.c
#include <stdbool.h>
#include "funs.h"
#include <stdio.h>
#include <string.h>
bool palindrom(char napis[])
{
int begin, middle, end, length = 0;
while(napis[length] != '\0')
length++;
end = length - 1;
middle = length;
for (begin = 0; begin < middle; begin++)
{
if(napis[begin] != napis[end])
{
return false;
break;
}
end--;
}
if(begin == middle)
return true;
}
I also created funs.h
//funs.h
#include <stdbool.h>
bool palindrom();
Now, I'm trying to use this function in my main function
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "funs.h"
int main()
{
char text[100];
bool result;
printf("Enter an char: ");
scanf("%s", text);
result = palindrom(text);
if(result)
{
printf("Brawo!\n");
}
else
{
printf("Gówno!\n");
}
return 0;
}
I also created makefile:
# Makefile
all: main
main: main.o pan.o
clang -o main main.o pan.o
main.o: main.c
clang -c main.c
pan.o: pan.c
clang -c pan.c
clean:
rm -f main *.o *~
Everything seems fine and works in single file, but when I try to compile them separately they "don't see" each other. Makefile also seems to work badly, but I can't see any mistakes. Can you help me fix it?
When I try "make" command it returns "makefile:15: *** missing separator. Stop." comment and do nothing.
Did you actually look at line 15 of your makefile? Notice that it's flush against the margin instead of indented by a tab character.
When I'm compiling pan.c with "clang pan.c -Wall --pedantic -std=c11 -o pan" command: 1 warning generated. /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-l‌​inux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference to main'
Indeed, your pan.c does not have a main() function. So don't try to compile it by itself. How about
clang main.c pan.c -Wall --pedantic -std=c11 -o pan

Math function compiler linking error

The following code compiles on other systems, but not on my Ubuntu 12.04 64bit guest in Virtualbox 4.3.10 on a Windows 7 64bit host.
hello.c
#include "header.h"
int main(int argc, char *argv[]){
int i;
for(i=0; i<argc; i++)
printf("%s\n", argv[i]);
double x;
x = testfunction();
printf("%f \n", x);
return 0;
}
hello2.c
#include "header.h"
double testfunction ()
{
int i = 1;
double j = 0;
for(i=0; i<1000000; i++)
j += sin(i/M_PI);
return j;
}
header.h
#include <math.h>
#include <stdio.h>
double testfunction();
When I attempt to compile using
gcc -lm -o hello hello.c hello2.c
I receive the error
/tmp/ccirukEU.o: In function testfunction':
hello2.c:(.text+0x33): undefined reference tosin'
collect2: ld returned 1 exit status
The error remains even if I include math.h directly in hello2.c. Calculating sin(2/M_Pi) rather than sin(i/M_Pi) removes the error, possibly because gcc then works out the sine itself rather than using the math library.
Use -lm in the end, as in:
gcc -o hello hello.c hello2.c -lm
This ensures that the linker can realize that there are dependencies missing. Using -lm in the beginning is known to raise problems, because by the time the linker looks at the math library, it hasn't looked at your code yet, so there are no dependencies unresolved.

C Undefined reference

I have the following code:
main.c
#include "checksum.h"
void main()
{
char *Buf ="GPGGA204502.005106.9813N11402.2921W1090.91065.02M-16.27M";
checksum(Buf);
}
checksum.c
#include <stdio.h>
#include <string.h>
checksum(char *Buff)
{
int i;
unsigned char XOR;
unsigned long iLen = strlen(Buff);
printf("Calculating checksum...\n");
for (XOR = 0, i = 0; i < iLen; i++)
XOR ^= (unsigned char)Buff[i];
printf("%X \n",XOR);
}
checksum.h
#ifndef CHECKSUM_H_INCLUDED
#define CHECKSUM_H_INCLUDED
void checksum(char *Buff);
#endif
When compiling I get the following error:
/tmp/ccFQS7Ih.o: In function `main':
main.c:(.text+0x18): undefined reference to `checksum'
collect2: ld returned 1 exit status
I can't figure out what the problem is?
You are compiling only one file not both. More precisely, you are not linking the files together.
I don't know your compiler, but with gcc, it would be something like this:
gcc -c main.c <-- compile only
gcc -c checksum.c <-- compile only
gcc main.o checksum.o <-- link the two
Edit: To automate this process, take a look at the make program which reads Makefiles.
You could also try
gcc -o program.out main.c checksum.c which will compile and link both files together
I think: in checksum.c, you should include checksum.h.

Resources