0

I am learner in C and I need to compile my code using shellforge and hence cannot use any library functions as they provide an error.

Can someone help me with the same.

This is what I have so far, I need help in comparing the 2 passwords and only then writing it to the file. Any help/modifications to my code to achieve this with an explanation will be highly appreciated.

#include <stdio.h>

int main()
{
 int fd;
 char username;
 char kbinput[KBINPUT_SIZE];
 char kbinputpassone[KBINPUTPASSONE_SIZE];
 char kbinputpasstwo[KBINPUTPASSTWO_SIZE];
 char passwordone;
 char passwordtwo;
 int count = 0;

 int ret;
 fd = open("test.txt", O_CREAT | O_RDWR,0666);
 if(fd == -1)
 {
  printf("Error!");
 exit(1);
 }

 write(1,"Enter the username",18);
do
{
   read(0, &username, 1);
  if (username == '\n')
  break;
  kbinput[count++] = username;
 }
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(fd,username,10);


 write(1,"Enter the password",18);
  count =0;
 do
 {
   read(0, &passwordone, 1);
  if (passwordone == '\n')
  break;
  kbinputpassone[count++] = passwordone;
 }
  while (count < KBINPUTPASSONE_SIZE - 1);
  kbinputpassone[count] = 0;


  write(1,"Enter the password again",25);
   count=0;
  do
  {
  read(0, &passwordtwo, 1);
  if (passwordtwo == '\n')
  break;
  kbinputpasstwo[count++] = passwordtwo;
 }
 while (count < KBINPUTPASSTWO_SIZE - 1);
 kbinputpasstwo[count] = 0;


 write(1,"The Passwords match",20);
 write(fd, passwordone, 15);

 return 0;

}
  • You don't appear to reset `count` to 0 before reading in either of the two passwords – Chris Turner Aug 06 '18 at 15:03
  • @ChrisTurner could you guide me as to how could i get this corrected. Could you help me or provide me a proper code which would work so I can try to understand from that. – Craig Peris Aug 07 '18 at 04:42
  • it's as simple as putting "count = 0;" before the loops – Chris Turner Aug 07 '18 at 08:27
  • @ChrisTurner , I have added count=0 before the do while loops, can you tell me if my program will correctly accept the user,pass and pass2 and compare pass1 and pass2. I am not sure about the same and hence requesting further guidance on the comparision and if a sample code can be provided to accomplish this. – Craig Peris Aug 07 '18 at 09:24
  • Is there any reason why do you call `write(0, "string", number)` instead of just `printf("string")` or `puts`? To check if two strings are equal you can use `if (!strcmp(kbinputpassone, kbinputpasstwo)) { printf("equal\n"); }`. Remember to `close(fd);` All the code to read username, passone and passtwo, you can create a function and also i guess it's more readable to iterate over buffer with a for loop: `void read_function(char *buf, size_t size) { char c; for (int count = 0; count < size; ++count) { read(STDIN_FILENO, &c, 1); if (c == '\n') { buf[count] = '\0'; break; } buf[count] = c; } }` – KamilCuk Aug 07 '18 at 09:35
  • @KamilCuk I am trying to compile my code in shellforge and it does not allow to use any library functions or add any libraries , hence I have used write function rather than printf and fgets. and strcmp is also a library function . Also, I am very new to C system calls so I am not following what the for loop is doing exactly – Craig Peris Aug 07 '18 at 09:44
  • I'm not going to pretend to know what "shellforge" is, but writing a C program without using any "library functions" is sort of like building a house using only a hammer. If you want to learn C, then learn C. – hymie Aug 07 '18 at 10:23

0 Answers0