0

I am trying to make a simple program that just writes your working directory to a file, and I cannot, for the life of me, figure out what I am doing wrong. No matter what I do, my buffer is storing null after my call to getcwd(). I suspect it may have to do with permissions, but allegedly, linux now did some wizardry to ensure that getcwd almost never has access problems (keyword, "almost"). Can anyone test it on their machines? Or is there an obvious bug I am missing?

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        printf("Error is with fopen if stops here\n");
        FILE* out_file = fopen("dir_loc.sh","w+");
        char* loc = malloc(sizeof(char)*10000);
        size_t size = sizeof(loc);
        printf("Error is with cwd if stops here\n");
        loc = getcwd(loc,size);
        printf("%s",loc);
        fprintf(out_file,"cd %s",loc);
        printf("Error is with fclose if stops here\n");
        free(loc);
        fclose(out_file);
        return 0;
}

compiled with gcc main.c (the file is named "main.c")

EDIT: As was mentioned by different posters, sizeof(loc) was taking the size of a char pointer, and not the size of the amount of space allocated to that pointer. Changed it to malloc(sizeof(char)*1000) and it all works gravy.

Ronald
  • 11
  • 3
  • Minor Nitpick: `sizeof(char)` is guaranteed to be 1 so having it in there is at best syntactic sugar. – Magisch Jul 01 '16 at 06:19
  • 3
    `sizeof(loc)` does not do what you think it does. – Paul R Jul 01 '16 at 06:19
  • 1
    Also, `sizeof(loc)` will give you the size of a char pointer, not the size of your memory allocated to `loc` – Magisch Jul 01 '16 at 06:20
  • `sizeof(loc)` -> `sizeof(char)*10000`. BTW: `sizeof(char)`is 1 by definition so you can safely drop it. So finally`sizeof(loc)` -> `10000`. – Jabberwocky Jul 01 '16 at 06:20
  • 1
    As @PaulR points out, what do you suspect you get when you take the `sizeof apointer`? Much different than the `sizeof an_array`. – David C. Rankin Jul 01 '16 at 06:22

1 Answers1

2

Your problem is here:

size_t size = sizeof(loc);

You're getting the size of a char pointer, not the allocated memory for your char.

Change it to:

size_t size = sizeof(char) * 10000;

or even to

size_t size = 10000;

since sizeof(char) is guaranteed to be 1.

And since you're using size in your subsequent call to getcwd, you're obviously gonna have too little space to store most paths, so your result is unsurprising

If you don't want to go about changing multiple different numbers in the code every time you make a change, you can use #DEFINE text replacement to solve that.

Like this:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define LOC_ARRAY_SIZE 10000 // Here you define the array size

int main(int argc, char *argv[])
{
        printf("Error is with fopen if stops here\n");
        FILE* out_file = fopen("dir_loc.sh","w+");
        char* loc = malloc(sizeof(char)*LOC_ARRAY_SIZE); // sizeof(char) could be omitted
        size_t size = sizeof(char)*LOC_ARRAY_SIZE;
        printf("Error is with cwd if stops here\n");
        loc = getcwd(loc,size);
        printf("%s",loc);
        fprintf(out_file,"cd %s",loc);
        printf("Error is with fclose if stops here\n");
        free(loc);
        fclose(out_file);
        return 0;
}
Magisch
  • 7,312
  • 9
  • 36
  • 52
  • More particularly, since `size` was (probably) being set to either 4 or 8, it simply wasn't big enough to store most paths for the current directory, unless you happen to be running in `/tmp` or something similar. – Jonathan Leffler Jul 01 '16 at 06:27