Search code examples
cunixunix-timestampstat

C UNIX write() function inserting strange characters instead of spaces


this is for an assignment (not mine, but someone I'm helping, if that matters), but were supposed to write a program that mimics the unix ar command. I am very close to writing the file info to the header, using the stat() function, but I'm getting @^ instead of spaces when writing to the file.

here is an example of output

1-s.txt2^@^@^@^@^@^@^@^@Wed Oct 30 149972^@14601^@100640^@^@101^@^@^@^@^@^@^@ and it should be 1-s.txt2/ Wed Oct 30 149972 14601 100640 101

except the date should be a unix timestamp, any help with that would also be appreciated.

Thanks!!!

struct ar_hdr headerstruct;

void setfileinfo(const struct stat *sfileinfo){

sprintf(headerstruct.ar_name, "%s", global_argv[3]);

sprintf(headerstruct.ar_date, "%s", ctime(&sfileinfo->st_mtime));

sprintf(headerstruct.ar_uid, "%ld", (long)sfileinfo->st_uid);

sprintf(headerstruct.ar_gid, "%ld", (long) sfileinfo->st_gid);

sprintf(headerstruct.ar_mode, "%lo",(unsigned long)sfileinfo->st_mode);

sprintf(headerstruct.ar_size, "%lld",(long long)sfileinfo->st_size);

char filemag[2] = "`\n";

int fd;
fd = open(global_argv[2], O_RDWR);
lseek(fd, 0, SEEK_END);
write(fd, headerstruct.ar_name, 16);
write(fd, headerstruct.ar_date, 12);
write(fd, headerstruct.ar_uid, 6);
write(fd, headerstruct.ar_gid, 6);
write(fd, headerstruct.ar_mode, 8);
write(fd, headerstruct.ar_size, 10);
write(fd, filemag ,2);

return;

}

Solution

  • Since the ar header requires space padding, you might consider using memset to prefill the data structure or specific members with spaces. For example:

        memset(&headerstruct, ' ', sizeof(headerstruct));
    

    Additionally, if you want to avoid null-terminated strings in the header, you should use something like memcpy or strncpy (with an appropriate length) instead of sprintf, as sprintf will insert a zero byte at the end of the string.