This is a discussion on Reading directory contents and getting access times, issues with struct stat within the C Programming forums, part of the General Programming Boards category; So my code is supposed to take two inputs one being an int that will be representing a date in ...

So my code is supposed to take two inputs one being an int that will be representing a date in unix time, and the other is supposed to be a directory to search through. it then checks the files access time and if it is within a day of the inputted date it will print the file name and the date is was accessed. I apologize in advance for bad naming, my code:

ive come to the conclusion through testing that my problem lies with the fixtemp function and the line "filepath = strcat(tmp,dirp->d_name);" because it causes an issue with struct stat for some reason. I was under the impression that if i gave a valid filepath struct stat would work.

i have a section of code commented out, im kinda lookin for an opinion on it, would that recursive call to tsSearch work or would it just give me the same problem with the struct stat?

Also i know it isnt wrapped around anything at the moment while it is commented out, but i was also having issues when checking for "." and "..", i tried srcmp as well but it didnt work.

You need to learn more about strings.
Not sure why your write str_copy(), which does in fact have a bug when there are strcpy() and strcat() functions available in standard library.
It is never advisable to modify argv[]. You are attempting to concatenate a "/". Instead you must copy argv[] to your own buffer and modify that.

Also in your format_time() function you are returning the address of a local variable, this variable is destroyed when the function returns. You also seem to be using uninitialized memory in your tsSearch() function. You need to check your str_cat, and str_cpy functions to insure that they are placing the end of string character onto the end of the strings.

I was going to mention that format_time()'s local variable string[] can not be relied upon persisting upon the function's exit. However, the way the pointer is immediately used, and knowing that stacked values are not being trashed as evidenced by the working example, I let it go. It is poor form though.

Just a couple other things I see. In your getATime() function, the printf statement is attempting to print an undefined value, buf. You declare the variable buf two lines before, but fail to initialize it. You have a similar problem in your tsSearch() function. In the while loop, you attempt to copy tmp to temp, but tmp has not been initialized.

What exactly is the purpose of fixtemp()? You don't need to fill a C string with null characters. The first null character encountered represents the end of the string. Anything following that first null character is never read, so it doesn't matter what characters may be there.

I agree with nonoob - unless you have an overriding reason to recreate the wheel, I believe you should stick with the standard library functions (e.g. strcpy, strcat).

You need to learn more about strings.
Not sure why your write str_copy(), which does in fact have a bug when there are strcpy() and strcat() functions available in standard library.
It is never advisable to modify argv[]. You are attempting to concatenate a "/". Instead you must copy argv[] to your own buffer and modify that.

when i use the given strcopy(), i get errors like these:
tsSearch2.c.text+0x27c): undefined reference to `strcopy'

so r u saying i should do a strcopy(path,temp) and then concatenate the "/" to temp? is this what was giving me the issues with struct stat? i was under the impression it was the '\0' characters that were causing that problem.

Originally Posted by jimblumberg

I would suggest that you check your compiler settings to insure that warnings are emitted. When I compile this code these are the warnings I receive:

Also in your format_time() function you are returning the address of a local variable, this variable is destroyed when the function returns. You also seem to be using uninitialized memory in your tsSearch() function. You need to check your str_cat, and str_cpy functions to insure that they are placing the end of string character onto the end of the strings.

Jim

sry,i meant to delete the str_cat() function as it isnt even being used. i ignored those warnings of previous implicit declarations. i didnt think they were a problem since my previous homework gave me those warnings in which i was to code those string functions and it worked despite the warnings. and the format_time() code was given to us by the professor in a lecture, its not my code and im not entirely sure how it works, im just using it to change the unix time into a readable date.

Sorry kmess. Your advice is flawed.
getATime does not need a known buf. It is a parameter that's assigned within the function. Not that it's used.
The tsSearch() function copies temp to tmp using the user written copy function which does source -> destination.

i ignored those warnings of previous implicit declarations. i didnt think they were a problem since my previous homework gave me those warnings in which i was to code those string functions and it worked despite the warnings.

You should consider all warnings as errors. Your code should compile without any warnings or errors.

Sorry kmess. Your advice is flawed.
getATime does not need a known buf. It is a parameter that's assigned within the function. Not that it's used.
The tsSearch() function copies temp to tmp using the user written copy function which does source -> destination.

i have it commented out at the moment but getATime() is supposed to take the file path i give it and give me the access time of that file. i was going to ask about that if i stil had problems after i got the issues that i mentioned in my opening post resolved.

since it was only implcit declarations i ignored them.
fixtemp() takes a string that is a directory path with a file name appended, and it removes the file name at the end of the string for the next loop.

The problem is that you are not sending a string you are sending a char *. Therefore you have the conflicting types warning message. You have never initialized your tmp variable or your filepath, or your temp variables.

The problem is that you are not sending a string you are sending a char *. Therefore you have the conflicting types warning message. You have never initialized your tmp variable or your filepath, or your temp variables.

Jim

sry i meant char *, im not sure what you mean by i havent initialized my variables. strcopy doesnt initialize them? also doesn't struct stat just take a char* of a file path(my reason for passing it to the function getATime which intend to use eventually)?

You need to allocate memory for your pointers. Either statically tmp[100] or dynamically with malloc/free. I would suggest the static method for now. strcpy(), strcat() do not allocate memory, the destination variable must already have memory allocated that is large enough to hold the string.