Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving remote file information

Posted on 1997-07-07
2
Medium Priority
?
151 Views
Last Modified: 2013-12-25
Is it possible to use PERL and/or CGI to query a remote server to retrieve file information?  Basically, I am looking to get the date created or last modified date of files on different servers.  If it is possible to do this, how is it done (please provide code if possible).  What information can you retrieve about remote files on servers you access anonymously?  Thanks for your assistance.  
0
Comment
Question by:mtob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 400 total points
ID: 1828904
Sure, i'll give a ready routine to get file informations through use of the web server's HEAD method.

You can embedd this routine anywhere you need; just pass it the right parameters - this means you could put it into a program to invoke from the shell, or into a CGI.

Please check function return types and include files on your OS, since there may be slight differences.

--//--
#include <stdlib.h>
#include <stdio.h>
#include <string.h>     /* bzero() and bcopy() */
#include <netdb.h>      /* gethostbyname() */
#include <sys/socket.h> /* socket() and connect() */
#include <arpa/inet.h>  /* htons() */
#include <errno.h>      /* errno */

#define PORT 80

/* get_url()
 * answer: file to write to (must be already open for writing - it may be stdin)
 * host: e.g. "www.experts-exchange.com"
 * url : e.g. "/topics/comp/lang/c"
 * err_str: to eventually store an error message
 *
 * returns 0 if succesful, errno otherwise
 */

int get_url(FILE *answer, const char *host, const char *url, char err_str[])
{
  int sock;
  struct hostent *hep;
  struct sockaddr_in sai;
  FILE *infile, *outfile;
  char buff[BUFSIZ];

  if(!(hep = gethostbyname(host))) {
    strcpy(err_str, "gethostbyname error");
    return errno;
  }
  if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    strcpy(err_str, "socket error");
    return errno;
  }

  bzero((char *)&sai, sizeof(struct sockaddr_in));
  sai.sin_family = AF_INET;
  sai.sin_port = htons((unsigned short)PORT);
  bcopy(hep->h_addr, (char *)&(sai.sin_addr.s_addr), hep->h_length); /* note: could just be 'sai.sin_addr' */

  if(connect(sock, (struct sockaddr *)&sai, sizeof(sai)) == -1) {
    strcpy(err_str, "connect error");
    return errno;
  }

  /* fine: now we are connected */
  /* let's work with stdio */
  if(!(infile = fdopen(sock, "r"))) {
    strcpy(err_str, "fdopen(in) error");
    return errno;
  }
  if(!(outfile = fdopen(sock, "w"))) {
    strcpy(err_str, "fdopen(out) error");
    return errno;
  }

  /* now, the query: i'll leave error checking now on! */
  fprintf(outfile, "HEAD %s HTTP1.0\n", url);
  fflush(outfile); /* this is mandatory: outfile would buffer */

  while(fgets(buff, BUFSIZ, infile)) fputs(buff, answer);
  fflush(answer); /* this is not mandatory:) */

  /* cleanup */
  /* note: the two fclose's are also mandatory: */
  /* the file tables are not automagically deallocated */
  /* if you just close the socket (at least on my system), */
  /* and you could go out of resources if you loop */
  /* to connect to more than one site */
  fclose(infile); fclose(outfile);
  close(sock);

  return 0;
}
--//--

Cheers, julio

P.S. maybe this is worth more than 100 points...
0
 

Author Comment

by:mtob
ID: 1828905
You're right, it was worth more.  Thank you very much.  If you could help though, is there a PERL way of doing it.  I can do it through C if I have to but would much rather through PERL.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question