Imported Upstream version 7.48.0
[platform/upstream/curl.git] / docs / examples / synctime.c
index cd7d073..1c787bb 100644 (file)
@@ -5,11 +5,11 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
+ * are also available at https://curl.haxx.se/docs/copyright.html.
  *
  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  * copies of the Software, and permit persons to whom the Software is
  * KIND, either express or implied.
  *
  ***************************************************************************/
+/* <DESC>
+ * Set your system time from a remote HTTP server's Date: header.
+ * </DESC>
+ */
 /* This example code only builds as-is on Windows.
  *
  * While Unix/Linux user, you do not need this software.
@@ -125,7 +129,7 @@ size_t SyncTime_CURL_WriteOutput(void *ptr, size_t size, size_t nmemb,
                                  void *stream)
 {
   fwrite(ptr, size, nmemb, stream);
-  return(nmemb*size);
+  return (nmemb*size);
 }
 
 size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
@@ -134,17 +138,17 @@ size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
   int   i, RetVal;
   char  TmpStr1[26], TmpStr2[26];
 
-  if (ShowAllHeader == 1)
+  if(ShowAllHeader == 1)
     fprintf(stderr, "%s", (char *)(ptr));
 
-  if (strncmp((char *)(ptr), "Date:", 5) == 0) {
-    if (ShowAllHeader == 0)
+  if(strncmp((char *)(ptr), "Date:", 5) == 0) {
+    if(ShowAllHeader == 0)
       fprintf(stderr, "HTTP Server. %s", (char *)(ptr));
 
-    if (AutoSyncTime == 1) {
+    if(AutoSyncTime == 1) {
       *TmpStr1 = 0;
       *TmpStr2 = 0;
-      if (strlen((char *)(ptr)) > 50) /* Can prevent buffer overflow to
+      if(strlen((char *)(ptr)) > 50) /* Can prevent buffer overflow to
                                          TmpStr1 & 2? */
         AutoSyncTime = 0;
       else {
@@ -152,11 +156,10 @@ size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
                          TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
                          &SYSTime.wHour, &SYSTime.wMinute, &SYSTime.wSecond);
 
-        if (RetVal == 7) {
-
+        if(RetVal == 7) {
           SYSTime.wMilliseconds = 500;    /* adjust to midpoint, 0.5 sec */
-          for (i=0; i<12; i++) {
-            if (strcmp(MthStr[i], TmpStr2) == 0) {
+          for(i=0; i<12; i++) {
+            if(strcmp(MthStr[i], TmpStr2) == 0) {
               SYSTime.wMonth = i+1;
               break;
             }
@@ -170,21 +173,21 @@ size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
     }
   }
 
-  if (strncmp((char *)(ptr), "X-Cache: HIT", 12) == 0) {
+  if(strncmp((char *)(ptr), "X-Cache: HIT", 12) == 0) {
     fprintf(stderr, "ERROR: HTTP Server data is cached."
             " Server Date is no longer valid.\n");
     AutoSyncTime = 0;
   }
-  return(nmemb*size);
+  return (nmemb*size);
 }
 
 void SyncTime_CURL_Init(CURL *curl, char *proxy_port,
                         char *proxy_user_password)
 {
-  if (strlen(proxy_port) > 0)
+  if(strlen(proxy_port) > 0)
     curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);
 
-  if (strlen(proxy_user_password) > 0)
+  if(strlen(proxy_user_password) > 0)
     curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);
 
 #ifdef SYNCTIME_UA
@@ -201,7 +204,7 @@ int SyncTime_CURL_Fetch(CURL *curl, char *URL_Str, char *OutFileName,
   CURLcode res;
 
   outfile = NULL;
-  if (HttpGetBody == HTTP_COMMAND_HEAD)
+  if(HttpGetBody == HTTP_COMMAND_HEAD)
     curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
   else {
     outfile = fopen(OutFileName, "wb");
@@ -210,7 +213,7 @@ int SyncTime_CURL_Fetch(CURL *curl, char *URL_Str, char *OutFileName,
 
   curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
   res = curl_easy_perform(curl);
-  if (outfile != NULL)
+  if(outfile != NULL)
     fclose(outfile);
   return res;  /* (CURLE_OK) */
 }
@@ -240,7 +243,7 @@ int conf_init(conf_t *conf)
   int i;
 
   *conf->http_proxy       = 0;
-  for (i=0; i<MAX_STRING1; i++)
+  for(i=0; i<MAX_STRING1; i++)
     conf->proxy_user[i]     = 0;    /* Clean up password from memory */
   *conf->timeserver       = 0;
   return 1;
@@ -268,24 +271,24 @@ int main(int argc, char *argv[])
   RetValue        = 0;    /* Successful Exit */
   conf_init(conf);
 
-  if (argc > 1) {
-    while (OptionIndex < argc) {
-      if (strncmp(argv[OptionIndex], "--server=", 9) == 0)
+  if(argc > 1) {
+    while(OptionIndex < argc) {
+      if(strncmp(argv[OptionIndex], "--server=", 9) == 0)
         snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]);
 
-      if (strcmp(argv[OptionIndex], "--showall") == 0)
+      if(strcmp(argv[OptionIndex], "--showall") == 0)
         ShowAllHeader = 1;
 
-      if (strcmp(argv[OptionIndex], "--synctime") == 0)
+      if(strcmp(argv[OptionIndex], "--synctime") == 0)
         AutoSyncTime = 1;
 
-      if (strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
+      if(strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
         snprintf(conf->proxy_user, MAX_STRING, "%s", &argv[OptionIndex][13]);
 
-      if (strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
+      if(strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
         snprintf(conf->http_proxy, MAX_STRING, "%s", &argv[OptionIndex][8]);
 
-      if ((strcmp(argv[OptionIndex], "--help") == 0) ||
+      if((strcmp(argv[OptionIndex], "--help") == 0) ||
           (strcmp(argv[OptionIndex], "/?") == 0)) {
         showUsage();
         return 0;
@@ -294,13 +297,13 @@ int main(int argc, char *argv[])
     }
   }
 
-  if (*conf->timeserver == 0)     /* Use default server for time information */
+  if(*conf->timeserver == 0)     /* Use default server for time information */
     snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]);
 
   /* Init CURL before usage */
   curl_global_init(CURL_GLOBAL_ALL);
   curl = curl_easy_init();
-  if (curl) {
+  if(curl) {
     SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user);
 
     /* Calculating time diff between GMT and localtime */
@@ -312,7 +315,7 @@ int main(int argc, char *argv[])
     tzonediffFloat = difftime(tt_local, tt_gmt);
     tzonediffWord  = (int)(tzonediffFloat/3600.0);
 
-    if ((double)(tzonediffWord * 3600) == tzonediffFloat)
+    if((double)(tzonediffWord * 3600) == tzonediffFloat)
       snprintf(tzoneBuf, 15, "%+03d'00'", tzonediffWord);
     else
       snprintf(tzoneBuf, 15, "%+03d'30'", tzonediffWord);
@@ -341,9 +344,9 @@ int main(int argc, char *argv[])
              LOCALTime.wMilliseconds);
     fprintf(stderr, "\nAfter  HTTP. Date: %s%s\n", timeBuf, tzoneBuf);
 
-    if (AutoSyncTime == 3) {
+    if(AutoSyncTime == 3) {
       /* Synchronising computer clock */
-      if (!SetSystemTime(&SYSTime)) {  /* Set system time */
+      if(!SetSystemTime(&SYSTime)) {  /* Set system time */
         fprintf(stderr, "ERROR: Unable to set system time.\n");
         RetValue = 1;
       }