LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2004, 02:13 PM   #1
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Rep: Reputation: 30
c program backdate


I have a question about this program. This program is supposed to take the inode information modtime and atime of a file and modify it. I did not really think much about this code so there are some consistencies but its better than nothing. My code is:

Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>

main(int argc, char *argv[])
{
	DIR *dirp;
	struct dirent *dentry;
	struct stat buffer;
	struct utimbuf date;
	char* fname;
	int days;

	if(argc < 1 || argc > 3)
	{
		fprintf(stderr, "usage: backdate [path] filename days\n");
		exit(1);
	}
      if(argc == 2)
		{
		fname = argv[1];
		days = argv[2];
		}
		else
		{
			if(chdir(argv[1]) == NULL)
				{
				fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
				exit(1);
				}
			else
			{
			fname = argv[2];
			days = argv[3];
			}
		}

	while((dentry = readdir(chdir(argv[1]))) != NULL)
	{
		if(strcmp(dentry->d_name, fname) == 0)
			break;
		else if(dentry == NULL)
		{
			printf("%s does not exist \n", fname);
			exit(0);
		}
	}
	date.actime = buffer.st_atime-(days*60*60*24);
	date.modtime = buffer.st_mtime-(days*60*60*24);
	printf("Inode has been modified\n");
}
When i compile it, it gives me the errors:

backdate.c:54: request for member `actime' in something not a structure or union
backdate.c:55: request for member `modtime' in something not a structure or union

whats that mean? and how do i fix it?
 
Old 04-21-2004, 03:16 PM   #2
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
Is there a good debugging tool for unix? When i run this program it keeps giving me the same error. What the heck is "segmantation fault"?
 
Old 04-21-2004, 05:05 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Is there a good debugging tool for unix?
Yes: gdb
Best run from within emacs IMHO.
(M-x gdb)
 
Old 04-21-2004, 05:15 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by dilberim82
What the heck is "segmantation fault"?
A severe programming error, i.e. referencing some pointer that points to memory not assigned (allocated / declared) for your program.
 
Old 04-21-2004, 05:25 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <utime.h>  /* FIXED: You forgot this one */

int main(int argc, char *argv[])   /* FIXED: forgot return type, not important though. */
{
	 DIR *dirp;
	 struct dirent *dentry;
	 struct stat buffer;
	 struct utimbuf date;
	 char* fname;
	 int days;

	 if(argc < 1 || argc > 3) {
		  fprintf(stderr, "usage: backdate [path] filename days\n");
		  exit(1);
	 }
	 if(argc == 2) {
		  fname = argv[1];
		  days = atoi(argv[2]);  /* FIXED: need to convert string to int */
	 } else {
		  if(chdir(argv[1]) < 0) {  /* FIXED: chdir returns int not pointer */
			   fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
			   exit(1);
		  } else {
			   fname = argv[2];
			   days = atoi(argv[3]);  /* FIXED: need to convert string to int */
		  }
	 }

	 while((dentry = readdir(chdir(argv[1]))) != NULL) {  

         /* After checking the line above I gave up.  Doesn't make sense.
            Please read the man pages on the functions you call
	    and try to fix at least some things yourself first.			
	 */

Last edited by Hko; 04-21-2004 at 05:29 PM.
 
Old 04-21-2004, 10:23 PM   #6
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <utime.h>  /* FIXED: You forgot this one */

int main(int argc, char *argv[])   /* FIXED: forgot return type, not important though. */
{
	 DIR* dp;
	 struct dirent *dentry;
	 struct stat buffer;
	 struct utimbuf date;
	 char fname[50];
	 int days, time1, time2;

	 if(argc < 3 || argc > 4) {
		  fprintf(stderr, "usage: backdate [path] filename days\n");
		  exit(1);
	 }
	 if(argc == 3) {
		  dp = opendir(".");
		  strcpy(fname, argv[1]);
		  days = atoi(argv[2]);  /* FIXED: need to convert string to int */
		  printf("Days in 4 args is: %d\t%s\n", days*60*60*24, fname);
	 } else {
		  dp = opendir(argv[1]);
		  if(dp == NULL) {  /* FIXED: chdir returns int not pointer */
			   fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
			   exit(1);
		  } else {
			   strcpy(fname, argv[2]);
			   days = atoi(argv[3]);  /* FIXED: need to convert string to int */
		  printf("Days in 4 args is: %d\t%s\n", days*60*60*24, fname);
		  }
	 }

	while((dentry = readdir(dp)) != NULL)
	{

	  if(strcmp(dentry->d_name, fname) == 0)
	  {
      printf("file is in the directory");
      break;
      }
	}
		if(dentry == NULL)
		{
		printf("could not find %s \n", fname);
		exit(0);
		}
	if(stat(fname, &buffer) == -1){
		fprintf(stderr, "incorrect stat access\n");
		exit(1);
	}

	    date.actime = buffer.st_atime - (days*60*60*24);
		date.modtime = buffer.st_mtime - (days*60*60*24);

		printf("Inode has been modified\n");

closedir(dp);
return(0);
}
First, thank you for the replies Hko. I think this fixed most of the errors but i still didn't get the result i want. What am i doing wrong?
 
Old 04-21-2004, 10:31 PM   #7
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
well on a second look, it does modify the date.actime and date.modtime but when i do an ls, it does not show up there. am i missing something here?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sms program and database in linux web program in windows.. does not see each other.. keikun_naruchan Programming 0 07-06-2005 01:40 AM
Total recovery: Which program? ghost4linux, YaST2? Best drive imaging program? lagu2653 Linux - Software 1 06-20-2005 01:44 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration