nsFileSpec dirSpec; nsresult rv = cookie_ProfileDirectory(dirSpec); if (NS_FAILED(rv)) {
if(!(fp = NET_XP_FileOpen(NULL, xpHTTPCookie, XP_FILE_READ))) return;  net_lock_cookie_list(); list_ptr = net_cookie_list;   while(NET_XP_FileReadLine(buffer, LINE_BUFFER_SIZE, fp)) { added_to_list = FALSE;  if (*buffer == '#' || *buffer == CR || *buffer == LF || *buffer == 0) continue;  host = buffer;  if( !(is_domain = PL_strchr(host, '\t')) ) continue; *is_domain++ = '\0'; if(*is_domain == CR || *is_domain == LF || *is_domain == 0) continue;  if( !(path = PL_strchr(is_domain, '\t')) ) continue; *path++ = '\0'; if(*path == CR || *path == LF || *path == 0) continue;  if( !(xxx = PL_strchr(path, '\t')) ) continue; *xxx++ = '\0'; if(*xxx == CR || *xxx == LF || *xxx == 0) continue;  if( !(expires = PL_strchr(xxx, '\t')) ) continue; *expires++ = '\0'; if(*expires == CR || *expires == LF || *expires == 0) continue;  if( !(name = PL_strchr(expires, '\t')) ) continue; *name++ = '\0'; if(*name == CR || *name == LF || *name == 0) continue;  if( !(cookie = PL_strchr(name, '\t')) ) continue; *cookie++ = '\0'; if(*cookie == CR || *cookie == LF || *cookie == 0) continue;   XP_StripLine(cookie);   new_cookie = PR_NEW(net_CookieStruct); if(!new_cookie) { net_unlock_cookie_list(); return; }  memset(new_cookie, 0, sizeof(net_CookieStruct));   StrAllocCopy(new_cookie->cookie, cookie); StrAllocCopy(new_cookie->name, name); StrAllocCopy(new_cookie->path, path); StrAllocCopy(new_cookie->host, host); new_cookie->expires = atol(expires);  HG87365  if(!PL_strcmp(is_domain, "TRUE")) new_cookie->is_domain = TRUE; else new_cookie->is_domain = FALSE;  if(!net_cookie_list) { net_cookie_list = XP_ListNew(); if(!net_cookie_list) { net_unlock_cookie_list(); return; } }   new_len = PL_strlen(new_cookie->path); while((tmp_cookie_ptr = (net_CookieStruct *) XP_ListNextObject(list_ptr)) != NULL) { if(new_len > PL_strlen(tmp_cookie_ptr->path)) { XP_ListInsertObject(net_cookie_list, tmp_cookie_ptr, new_cookie); added_to_list = TRUE; break; } }   if(!added_to_list) XP_ListAddObjectToEnd(net_cookie_list, new_cookie); }  NET_XP_FileClose(fp); net_unlock_cookie_list();  cookies_changed = FALSE;
net_ReadCookies(){  XP_List * list_ptr;  net_CookieStruct *new_cookie, *tmp_cookie_ptr;  size_t new_len;  nsAutoString * buffer;  Bool added_to_list;  net_ReadCookiePermissions();  nsFileSpec dirSpec;  nsresult rv = cookie_ProfileDirectory(dirSpec);  if (NS_FAILED(rv)) {    return;  }  nsInputFileStream strm(dirSpec + "cookies.txt");  if (!strm.is_open()) {    /* file doesn't exist -- that's not an error */    return;  }  net_lock_cookie_list();  list_ptr = net_cookie_list;  /* format is:   *   * host \t is_domain \t path \t xxx \t expires \t name \t cookie   *   * if this format isn't respected we move onto the next line in the file.   * is_domain is TRUE or FALSE	-- defaulting to FALSE   * xxx is TRUE or FALSE   -- should default to TRUE   * expires is a time_t integer   * cookie can have tabs   */  while (cookie_GetLine(strm,buffer) != -1){    added_to_list = FALSE;    if (buffer->CharAt(0) == '#' || buffer->CharAt(0) == CR ||        buffer->CharAt(0) == LF || buffer->CharAt(0) == 0) {      PR_Free(buffer);      continue;    }    int hostIndex, isDomainIndex, pathIndex, xxxIndex, expiresIndex, nameIndex, cookieIndex;    hostIndex = 0;    if ((isDomainIndex=buffer->Find('\t', hostIndex)+1) == 0 ||        (pathIndex=buffer->Find('\t', isDomainIndex)+1) == 0 ||        (xxxIndex=buffer->Find('\t', pathIndex)+1) == 0 ||        (expiresIndex=buffer->Find('\t', xxxIndex)+1) == 0 ||        (nameIndex=buffer->Find('\t', expiresIndex)+1) == 0 ||        (cookieIndex=buffer->Find('\t', nameIndex)+1) == 0 ) {      PR_Free(buffer);      continue;    }    nsString host, isDomain, path, xxx, expires, name, cookie;    buffer->Mid(host, hostIndex, isDomainIndex-hostIndex-1);    buffer->Mid(isDomain, isDomainIndex, pathIndex-isDomainIndex-1);    buffer->Mid(path, pathIndex, xxxIndex-pathIndex-1);    buffer->Mid(xxx, xxxIndex, expiresIndex-xxxIndex-1);    buffer->Mid(expires, expiresIndex, nameIndex-expiresIndex-1);    buffer->Mid(name, nameIndex, cookieIndex-nameIndex-1);    buffer->Mid(cookie, cookieIndex, buffer->Length()-cookieIndex);    PR_Free(buffer);    /* create a new cookie_struct and fill it in */    new_cookie = PR_NEW(net_CookieStruct);    if (!new_cookie) {      net_unlock_cookie_list();      strm.close();      return;    }    memset(new_cookie, 0, sizeof(net_CookieStruct));    new_cookie->name = name.ToNewCString();    new_cookie->cookie = cookie.ToNewCString();    new_cookie->host = host.ToNewCString();    new_cookie->path = path.ToNewCString();    if (isDomain == "TRUE") {      new_cookie->is_domain = PR_TRUE;    } else {      new_cookie->is_domain = PR_FALSE;    }    if (xxx == "TRUE") {      new_cookie->xxx = PR_TRUE;    } else {      new_cookie->xxx = PR_FALSE;    }    char * expiresCString = expires.ToNewCString();    new_cookie->expires = atol(expiresCString);    delete[] expiresCString;    /* start new cookie list if one does not already exist */    if (!net_cookie_list) {      net_cookie_list = XP_ListNew();      if (!net_cookie_list) {        net_unlock_cookie_list();        strm.close();        return;      }    }    /* add new cookie to the list so that it is before any strings of smaller length */    new_len = PL_strlen(new_cookie->path);    while ((tmp_cookie_ptr = (net_CookieStruct *) XP_ListNextObject(list_ptr)) != NULL) {      if (new_len > PL_strlen(tmp_cookie_ptr->path)) {        XP_ListInsertObject(net_cookie_list, tmp_cookie_ptr, new_cookie);        added_to_list = TRUE;        break;      }    }    /* no shorter strings found in list so add new cookie at end */	    if (!added_to_list) {      XP_ListAddObjectToEnd(net_cookie_list, new_cookie);    }  }  strm.close();  net_unlock_cookie_list();  cookies_changed = FALSE;  return;}